Skip to content

Commit

Permalink
fix: add telemetry events (#985)
Browse files Browse the repository at this point in the history
Co-authored-by: Apoorv Taneja <purutaneja.com@gmail.com>
  • Loading branch information
himanshu-dixit and plxity authored Dec 12, 2024
1 parent 542244b commit 0e4b94b
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 22 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "composio-core",
"version": "0.4.1-beta",
"version": "0.4.3-beta",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion js/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ACTIONS = {
// actions list end here
};

const COMPOSIO_VERSION = `0.4.1-beta`;
const COMPOSIO_VERSION = `0.4.3-beta`;

module.exports = {
APPS,
Expand Down
19 changes: 18 additions & 1 deletion js/src/frameworks/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
import { COMPOSIO_BASE_URL } from "../sdk/client/core/OpenAPI";
import { WorkspaceConfig } from "../env/config";
import { Workspace } from "../env";
import { ActionsListResponseDTO } from "../sdk/client";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events";

// Type definitions
type Optional<T> = T | null;
Expand All @@ -21,6 +22,7 @@ export class CloudflareToolSet extends BaseComposioToolSet {
// Class constants
static FRAMEWORK_NAME = "cloudflare";
static DEFAULT_ENTITY_ID = "default";
fileName: string = "js/src/frameworks/cloudflare.ts";

/**
* Initialize a new CloudflareToolSet instance
Expand Down Expand Up @@ -58,6 +60,11 @@ export class CloudflareToolSet extends BaseComposioToolSet {
usecaseLimit?: Optional<number>;
filterByAvailableApps?: Optional<boolean>;
}): Promise<Sequence<AiTextGenerationToolInput>> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getTools",
file: this.fileName,
params: filters,
});
const actions = await this.getToolsSchema(filters);
return (
actions.map((action) => {
Expand Down Expand Up @@ -99,6 +106,11 @@ export class CloudflareToolSet extends BaseComposioToolSet {
},
entityId: Optional<string> = null
): Promise<string> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "executeToolCall",
file: this.fileName,
params: { tool, entityId },
});
return JSON.stringify(
await this.executeAction({
action: tool.name,
Expand All @@ -122,6 +134,11 @@ export class CloudflareToolSet extends BaseComposioToolSet {
result: AiTextGenerationOutput,
entityId: Optional<string> = null
): Promise<Sequence<string>> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "handleToolCall",
file: this.fileName,
params: { result, entityId },
});
const outputs = [];
if ("tool_calls" in result && Array.isArray(result.tool_calls)) {
for (const tool_call of result.tool_calls) {
Expand Down
9 changes: 9 additions & 0 deletions js/src/frameworks/langchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { COMPOSIO_BASE_URL } from "../sdk/client/core/OpenAPI";
import type { Optional, Dict, Sequence } from "../sdk/types";
import { WorkspaceConfig } from "../env/config";
import { Workspace } from "../env";
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";

export class LangchainToolSet extends BaseComposioToolSet {
/**
Expand All @@ -13,6 +15,7 @@ export class LangchainToolSet extends BaseComposioToolSet {
*/
static FRAMEWORK_NAME = "langchain";
static DEFAULT_ENTITY_ID = "default";
fileName: string = "js/src/frameworks/langchain.ts";

constructor(
config: {
Expand Down Expand Up @@ -72,6 +75,12 @@ export class LangchainToolSet extends BaseComposioToolSet {
},
entityId: Optional<string> = null
): Promise<Sequence<DynamicStructuredTool>> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getTools",
file: this.fileName,
params: { filters, entityId },
});

const tools = await this.getToolsSchema(filters, entityId);
return tools.map((tool) => this._wrapTool(tool, entityId || this.entityId));
}
Expand Down
37 changes: 37 additions & 0 deletions js/src/frameworks/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Workspace } from "../env";
import logger from "../utils/logger";
import { ActionsListResponseDTO } from "../sdk/client";
import { Stream } from "openai/streaming";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events";

type Optional<T> = T | null;
type Sequence<T> = Array<T>;
Expand All @@ -15,6 +17,8 @@ export class OpenAIToolSet extends BaseComposioToolSet {
static FRAMEWORK_NAME = "openai";
static DEFAULT_ENTITY_ID = "default";

fileName: string = "js/src/frameworks/openai.ts";

/**
* Composio toolset for OpenAI framework.
*
Expand Down Expand Up @@ -51,6 +55,12 @@ export class OpenAIToolSet extends BaseComposioToolSet {
},
entityId?: Optional<string>
): Promise<Sequence<OpenAI.ChatCompletionTool>> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getTools",
file: this.fileName,
params: filters,
});

const mainActions = await this.getToolsSchema(filters, entityId);
return (
mainActions.map(
Expand All @@ -74,6 +84,11 @@ export class OpenAIToolSet extends BaseComposioToolSet {
tool: OpenAI.ChatCompletionMessageToolCall,
entityId: Optional<string> = null
): Promise<string> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "executeToolCall",
file: this.fileName,
params: { tool, entityId },
});
return JSON.stringify(
await this.executeAction({
action: tool.function.name,
Expand All @@ -87,6 +102,11 @@ export class OpenAIToolSet extends BaseComposioToolSet {
chatCompletion: OpenAI.ChatCompletion,
entityId: Optional<string> = null
): Promise<Sequence<string>> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "handleToolCall",
file: this.fileName,
params: { chatCompletion, entityId },
});
const outputs = [];
for (const message of chatCompletion.choices) {
if (message.message.tool_calls) {
Expand All @@ -104,6 +124,11 @@ export class OpenAIToolSet extends BaseComposioToolSet {
): Promise<
Array<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput>
> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "handleAssistantMessage",
file: this.fileName,
params: { run, entityId },
});
const tool_calls =
run.required_action?.submit_tool_outputs?.tool_calls || [];
const tool_outputs: Array<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput> =
Expand Down Expand Up @@ -134,6 +159,12 @@ export class OpenAIToolSet extends BaseComposioToolSet {
thread: OpenAI.Beta.Threads.Thread,
entityId: string | null = null
): AsyncGenerator<any, void, unknown> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "waitAndHandleAssistantStreamToolCalls",
file: this.fileName,
params: { client, runStream, thread, entityId },
});

let runId = null;

// Start processing the runStream events
Expand Down Expand Up @@ -213,6 +244,12 @@ export class OpenAIToolSet extends BaseComposioToolSet {
thread: OpenAI.Beta.Threads.Thread,
entityId: Optional<string> = null
): Promise<OpenAI.Beta.Threads.Run> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "waitAndHandleAssistantToolCalls",
file: this.fileName,
params: { client, run, thread, entityId },
});

while (["queued", "in_progress", "requires_action"].includes(run.status)) {
logger.debug(`Current run status: ${run.status}`);
const tool_outputs = await this.handleAssistantMessage(
Expand Down
15 changes: 15 additions & 0 deletions js/src/frameworks/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { jsonSchemaToModel } from "../utils/shared";
import { z } from "zod";
import { CEG } from "../sdk/utils/error";
import { SDK_ERROR_CODES } from "../sdk/utils/errors/src/constants";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events";
type Optional<T> = T | null;

const zExecuteToolCallParams = z.object({
Expand All @@ -20,6 +22,7 @@ const zExecuteToolCallParams = z.object({
});

export class VercelAIToolSet extends BaseComposioToolSet {
fileName: string = "js/src/frameworks/vercel.ts";
constructor(
config: {
apiKey?: Optional<string>;
Expand Down Expand Up @@ -61,6 +64,12 @@ export class VercelAIToolSet extends BaseComposioToolSet {
usecaseLimit?: Optional<number>;
filterByAvailableApps?: Optional<boolean>;
}): Promise<{ [key: string]: any }> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getTools",
file: this.fileName,
params: filters,
});

const {
apps,
tags,
Expand Down Expand Up @@ -92,6 +101,12 @@ export class VercelAIToolSet extends BaseComposioToolSet {
tool: { name: string; arguments: unknown },
entityId: Optional<string> = null
): Promise<string> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "executeToolCall",
file: this.fileName,
params: { tool, entityId },
});

return JSON.stringify(
await this.executeAction({
action: tool.name,
Expand Down
13 changes: 12 additions & 1 deletion js/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class Composio {
integrations: Integrations;
activeTriggers: ActiveTriggers;

fileName: string = "js/src/sdk/index.ts";

/**
* Initializes a new instance of the Composio class.
*
Expand All @@ -45,7 +47,6 @@ export class Composio {
baseUrl,
apiKey
);
const loggingLevel = getLogLevel();

ComposioSDKContext.apiKey = apiKeyParsed;
ComposioSDKContext.baseURL = baseURLParsed;
Expand Down Expand Up @@ -121,6 +122,11 @@ export class Composio {
* @returns {Entity} An instance of the Entity class.
*/
getEntity(id: string = "default"): Entity {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getEntity",
file: this.fileName,
params: { id },
});
return new Entity(this.backendClient, id);
}

Expand Down Expand Up @@ -148,6 +154,11 @@ export class Composio {
| "BEARER_TOKEN"
| "BASIC_WITH_JWT";
}> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getExpectedParamsForUser",
file: this.fileName,
params: params,
});
const { app } = params;
let { integrationId } = params;
if (integrationId === null && app === null) {
Expand Down
39 changes: 39 additions & 0 deletions js/src/sdk/models/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { CEG } from "../utils/error";
import logger from "../../utils/logger";
import { SDK_ERROR_CODES } from "../utils/errors/src/constants";
import { z } from "zod";
import { TELEMETRY_LOGGER } from "../utils/telemetry";
import { TELEMETRY_EVENTS } from "../utils/telemetry/events";

const LABELS = {
PRIMARY: "primary",
Expand Down Expand Up @@ -50,6 +52,8 @@ export class Entity {
integrations: Integrations;
activeTriggers: ActiveTriggers;

fileName: string = "js/src/sdk/models/Entity.ts";

constructor(backendClient: BackendClient, id: string = "default") {
this.backendClient = backendClient;
this.id = id;
Expand All @@ -67,6 +71,11 @@ export class Entity {
text,
connectedAccountId,
}: TExecuteActionParams) {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "execute",
file: this.fileName,
params: { actionName, params, text, connectedAccountId },
});
try {
ZExecuteActionParams.parse({
actionName,
Expand Down Expand Up @@ -128,6 +137,11 @@ export class Entity {
app?: string;
connectedAccountId?: string;
}): Promise<any | null> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getConnection",
file: this.fileName,
params: { app, connectedAccountId },
});
try {
if (connectedAccountId) {
return await this.connectedAccounts.get({
Expand Down Expand Up @@ -187,6 +201,11 @@ export class Entity {
triggerName: string,
config: { [key: string]: any }
) {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "setupTrigger",
file: this.fileName,
params: { app, triggerName, config },
});
try {
const connectedAccount = await this.getConnection({ app });
if (!connectedAccount) {
Expand All @@ -209,6 +228,11 @@ export class Entity {
}

async disableTrigger(triggerId: string) {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "disableTrigger",
file: this.fileName,
params: { triggerId },
});
try {
await this.activeTriggers.disable({ triggerId: triggerId });
return { status: "success" };
Expand All @@ -221,6 +245,11 @@ export class Entity {
/**
* Get all connections for an entity.
*/
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getConnections",
file: this.fileName,
params: {},
});
try {
const connectedAccounts = await this.connectedAccounts.list({
// @ts-ignore
Expand All @@ -236,6 +265,11 @@ export class Entity {
/**
* Get all active triggers for an entity.
*/
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "getActiveTriggers",
file: this.fileName,
params: {},
});
try {
const connectedAccounts = await this.getConnections();
const activeTriggers = await this.activeTriggers.list({
Expand All @@ -253,6 +287,11 @@ export class Entity {
async initiateConnection(
data: TInitiateConnectionParams
): Promise<ConnectionRequest> {
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "initiateConnection",
file: this.fileName,
params: { data },
});
try {
const { appName, authMode, authConfig, integrationId, connectionData } =
ZInitiateConnectionParams.parse(data);
Expand Down
Loading

0 comments on commit 0e4b94b

Please sign in to comment.