@composio/core 0.1.0-rc.5 → 0.1.0-rc.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +956 -392
- package/dist/index.d.cts +586 -133
- package/dist/index.d.ts +586 -133
- package/dist/index.js +953 -391
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -8,105 +8,197 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
8
8
|
// src/composio.ts
|
|
9
9
|
import ComposioClient from "@composio/client";
|
|
10
10
|
|
|
11
|
-
// src/
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
// src/types/tool.types.ts
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
var ToolkitSchema = z.object({
|
|
14
|
+
slug: z.string().describe("The slug of the toolkit"),
|
|
15
|
+
name: z.string().describe("The name of the toolkit"),
|
|
16
|
+
logo: z.string().describe("The logo of the toolkit").optional()
|
|
17
|
+
});
|
|
18
|
+
var ToolSchema = z.object({
|
|
19
|
+
slug: z.string().describe('The slug of the tool. eg. "GOOGLE_SEARCH"'),
|
|
20
|
+
name: z.string().describe(`The name of the tool. eg. "Google Search"`),
|
|
21
|
+
description: z.string().optional().describe("The description of the tool"),
|
|
22
|
+
inputParameters: z.record(z.string(), z.unknown()).optional().describe("The input parameters of the tool"),
|
|
23
|
+
outputParameters: z.record(z.string(), z.unknown()).optional().describe("The output parameters of the tool"),
|
|
24
|
+
tags: z.optional(z.array(z.string())).describe("The tags of the tool. eg: Important").default([]),
|
|
25
|
+
toolkit: z.optional(ToolkitSchema).describe("The toolkit of the tool"),
|
|
26
|
+
version: z.optional(z.string()).describe('The version of the tool, e.g. "1.0.0"')
|
|
27
|
+
});
|
|
28
|
+
var ToolListResponseSchema = z.object({
|
|
29
|
+
items: z.array(ToolSchema),
|
|
30
|
+
nextCursor: z.string().nullable().optional(),
|
|
31
|
+
totalPages: z.number()
|
|
32
|
+
});
|
|
33
|
+
var ToolListParamsSchema = z.object({
|
|
34
|
+
cursor: z.string().optional(),
|
|
35
|
+
important: z.string().optional(),
|
|
36
|
+
limit: z.string().optional(),
|
|
37
|
+
search: z.string().optional(),
|
|
38
|
+
toolkitSlug: z.string().optional(),
|
|
39
|
+
toolSlugs: z.array(z.string()).optional()
|
|
40
|
+
});
|
|
41
|
+
var CustomAuthParamsSchema = z.object({
|
|
42
|
+
baseURL: z.string().optional(),
|
|
43
|
+
body: z.record(z.string(), z.unknown()).optional(),
|
|
44
|
+
parameters: z.array(
|
|
45
|
+
z.object({
|
|
46
|
+
in: z.enum(["query", "header"]),
|
|
47
|
+
name: z.string(),
|
|
48
|
+
value: z.union([z.string(), z.number()])
|
|
49
|
+
})
|
|
50
|
+
)
|
|
51
|
+
});
|
|
52
|
+
var ToolExecuteParamsSchema = z.object({
|
|
53
|
+
allowTracing: z.boolean().optional(),
|
|
54
|
+
connectedAccountId: z.string().optional(),
|
|
55
|
+
customAuthParams: CustomAuthParamsSchema.optional(),
|
|
56
|
+
arguments: z.record(z.string(), z.unknown()).optional(),
|
|
57
|
+
userId: z.string(),
|
|
58
|
+
version: z.string().optional(),
|
|
59
|
+
text: z.string().optional()
|
|
60
|
+
});
|
|
61
|
+
var ToolExecuteResponseSchema = z.object({
|
|
62
|
+
data: z.record(z.string(), z.unknown()),
|
|
63
|
+
error: z.string().nullable(),
|
|
64
|
+
successful: z.boolean(),
|
|
65
|
+
logId: z.string().optional(),
|
|
66
|
+
sessionInfo: z.unknown().optional()
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// src/models/CustomTools.ts
|
|
70
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
|
71
|
+
var CustomTools = class {
|
|
14
72
|
client;
|
|
73
|
+
customToolsRegistry = /* @__PURE__ */ new Map();
|
|
15
74
|
constructor(client) {
|
|
16
75
|
this.client = client;
|
|
17
76
|
}
|
|
18
77
|
/**
|
|
19
|
-
*
|
|
20
|
-
* This
|
|
21
|
-
* @
|
|
78
|
+
* Create a custom tool and registers it in the registry.
|
|
79
|
+
* This is just an in memory registry and is not persisted.
|
|
80
|
+
* @param {CustomToolOptions} toolOptions CustomToolOptions
|
|
81
|
+
* @returns {Tool} The tool created
|
|
22
82
|
*/
|
|
23
|
-
async
|
|
24
|
-
const
|
|
25
|
-
if (!
|
|
26
|
-
|
|
27
|
-
items: [],
|
|
28
|
-
next_cursor: null,
|
|
29
|
-
total_pages: 0
|
|
30
|
-
};
|
|
83
|
+
async createTool(toolOptions) {
|
|
84
|
+
const { slug, execute, inputParams, name, description } = toolOptions;
|
|
85
|
+
if (!slug || !execute || !inputParams || !name) {
|
|
86
|
+
throw new Error("Invalid tool options");
|
|
31
87
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
async getToolsEnum() {
|
|
62
|
-
return this.client.tools.retrieveEnum();
|
|
88
|
+
const paramsSchema = await zodToJsonSchema(inputParams, {
|
|
89
|
+
name: "input"
|
|
90
|
+
});
|
|
91
|
+
const paramsSchemaJson = paramsSchema.definitions.input;
|
|
92
|
+
const toolSchema = {
|
|
93
|
+
name,
|
|
94
|
+
slug,
|
|
95
|
+
description,
|
|
96
|
+
inputParameters: {
|
|
97
|
+
title: name,
|
|
98
|
+
type: "object",
|
|
99
|
+
description,
|
|
100
|
+
properties: paramsSchemaJson.properties,
|
|
101
|
+
required: paramsSchemaJson.required
|
|
102
|
+
},
|
|
103
|
+
// the output parameters are not used yet
|
|
104
|
+
outputParameters: {
|
|
105
|
+
type: "object",
|
|
106
|
+
title: `Response for ${name}`,
|
|
107
|
+
properties: []
|
|
108
|
+
},
|
|
109
|
+
tags: [],
|
|
110
|
+
toolkit: { name: "custom", slug: "custom" }
|
|
111
|
+
};
|
|
112
|
+
this.customToolsRegistry.set(slug.toLowerCase(), {
|
|
113
|
+
options: toolOptions,
|
|
114
|
+
schema: toolSchema
|
|
115
|
+
});
|
|
116
|
+
return toolSchema;
|
|
63
117
|
}
|
|
64
118
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* @param {string} slug - The ID of the tool to find input for
|
|
70
|
-
* @param {ToolGetInputParams} body - The parameters to be passed to the tool
|
|
71
|
-
* @returns
|
|
119
|
+
* Get all the custom tools from the registry.
|
|
120
|
+
* @param {string[]} param0.toolSlugs The slugs of the tools to get
|
|
121
|
+
* @returns {ToolList} The list of tools
|
|
72
122
|
*/
|
|
73
|
-
async
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
123
|
+
async getCustomTools({ toolSlugs }) {
|
|
124
|
+
const tools = [];
|
|
125
|
+
if (toolSlugs) {
|
|
126
|
+
for (const slug of toolSlugs) {
|
|
127
|
+
const tool = this.customToolsRegistry.get(slug.toLowerCase());
|
|
128
|
+
if (tool) {
|
|
129
|
+
tools.push(tool.schema);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
return Array.from(this.customToolsRegistry.values()).map((tool) => tool.schema);
|
|
134
|
+
}
|
|
135
|
+
return tools;
|
|
84
136
|
}
|
|
85
137
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* @returns {
|
|
138
|
+
* Get a custom tool by slug from the registry.
|
|
139
|
+
* @param {string} slug The slug of the tool to get
|
|
140
|
+
* @returns {Tool} The tool
|
|
89
141
|
*/
|
|
90
|
-
async
|
|
91
|
-
|
|
142
|
+
async getCustomToolBySlug(slug) {
|
|
143
|
+
const tool = this.customToolsRegistry.get(slug.toLowerCase());
|
|
144
|
+
if (!tool) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
return tool.schema;
|
|
92
148
|
}
|
|
93
149
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* @param
|
|
97
|
-
* @
|
|
98
|
-
* @returns {Promise<ToolkitRetrieveResponse>} The toolkit object
|
|
150
|
+
* Get the connected account for the user and toolkit.
|
|
151
|
+
* @param {string} toolkitSlug The slug of the toolkit
|
|
152
|
+
* @param {ExecuteMetadata} metadata The metadata of the execution
|
|
153
|
+
* @returns {ConnectedAccount} The connected account
|
|
99
154
|
*/
|
|
100
|
-
async
|
|
101
|
-
|
|
155
|
+
async getConnectedAccount(toolkitSlug, metadata) {
|
|
156
|
+
const connectedAccounts = await this.client.connectedAccounts.list({
|
|
157
|
+
toolkit_slug: toolkitSlug,
|
|
158
|
+
user_id: metadata.userId
|
|
159
|
+
});
|
|
160
|
+
if (!connectedAccounts.items.length) {
|
|
161
|
+
throw new Error(`No connected accounts found for toolkit ${toolkitSlug}`);
|
|
162
|
+
}
|
|
163
|
+
return metadata.connectedAccountId ? connectedAccounts.items.find((item) => item.id === metadata.connectedAccountId) : connectedAccounts.items[0];
|
|
102
164
|
}
|
|
103
165
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* @
|
|
166
|
+
* Execute a custom tool
|
|
167
|
+
* @param {slug} slug The slug of the tool to execute
|
|
168
|
+
* @param {Record<string, unknown>} inputParams The input parameters for the tool
|
|
169
|
+
* @param {ExecuteMetadata} metadata The metadata of the execution
|
|
170
|
+
* @returns {Promise<ToolExecuteResponse>} The response from the tool
|
|
107
171
|
*/
|
|
108
|
-
async
|
|
109
|
-
|
|
172
|
+
async executeCustomTool(slug, inputParams, metadata) {
|
|
173
|
+
const tool = this.customToolsRegistry.get(slug.toLowerCase());
|
|
174
|
+
if (!tool) {
|
|
175
|
+
throw new Error(`Tool with slug ${slug} not found`);
|
|
176
|
+
}
|
|
177
|
+
let authCredentials = {};
|
|
178
|
+
const { toolkitSlug, execute } = tool.options;
|
|
179
|
+
if (toolkitSlug) {
|
|
180
|
+
const connectedAccount = await this.getConnectedAccount(toolkitSlug, metadata);
|
|
181
|
+
if (!connectedAccount) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`Connected account not found for toolkit ${toolkitSlug} for user ${metadata.userId}`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
authCredentials = connectedAccount.data?.connectionParams;
|
|
187
|
+
}
|
|
188
|
+
if (typeof execute !== "function") {
|
|
189
|
+
throw new Error("Invalid execute function");
|
|
190
|
+
}
|
|
191
|
+
const executeToolRequest = async (data) => {
|
|
192
|
+
return this.client.tools.proxy({
|
|
193
|
+
...data,
|
|
194
|
+
connected_account_id: metadata.connectedAccountId
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
return execute(
|
|
198
|
+
inputParams,
|
|
199
|
+
authCredentials,
|
|
200
|
+
executeToolRequest
|
|
201
|
+
);
|
|
110
202
|
}
|
|
111
203
|
};
|
|
112
204
|
|
|
@@ -184,6 +276,306 @@ var Logger = class {
|
|
|
184
276
|
var logger = new Logger();
|
|
185
277
|
var logger_default = logger;
|
|
186
278
|
|
|
279
|
+
// src/models/Tools.ts
|
|
280
|
+
var Tools = class {
|
|
281
|
+
client;
|
|
282
|
+
customTools;
|
|
283
|
+
toolset;
|
|
284
|
+
constructor(client, toolset) {
|
|
285
|
+
this.client = client;
|
|
286
|
+
this.customTools = new CustomTools(client);
|
|
287
|
+
this.toolset = toolset;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Transform the tool cases
|
|
291
|
+
* @param {ToolRetrieveResponse | ComposioToolListResponse['items'][0]} tool - The tool to transform
|
|
292
|
+
* @returns {Tool} The transformed tool
|
|
293
|
+
*/
|
|
294
|
+
transformToolCases(tool) {
|
|
295
|
+
return ToolSchema.parse({
|
|
296
|
+
...tool,
|
|
297
|
+
inputParameters: tool.input_parameters,
|
|
298
|
+
outputParameters: tool.output_parameters
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Transform the tool execute response to camelcase
|
|
303
|
+
* @param {ComposioToolExecuteResponse} response - The response to transform
|
|
304
|
+
* @returns {ToolExecuteResponse} The transformed response
|
|
305
|
+
*/
|
|
306
|
+
transformToolExecuteResponse(response) {
|
|
307
|
+
return ToolExecuteResponseSchema.parse({
|
|
308
|
+
data: response.data,
|
|
309
|
+
error: response.error,
|
|
310
|
+
successful: response.successful,
|
|
311
|
+
logId: response.log_id,
|
|
312
|
+
sessionInfo: response.session_info
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Check if the connected account exists for the given tools.
|
|
317
|
+
* @param {string} userId - The user id.
|
|
318
|
+
* @param {ToolList} tools - The tools to check.
|
|
319
|
+
* @returns {Promise<boolean>} True if the connected account exists for the given tools, false otherwise.
|
|
320
|
+
*/
|
|
321
|
+
async checkIfConnectedAccountExistsForTools(userId, tools) {
|
|
322
|
+
if (!tools.length) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
const connectedAccounts = await this.client.connectedAccounts.list({
|
|
326
|
+
user_id: userId
|
|
327
|
+
});
|
|
328
|
+
if (connectedAccounts.items.length === 0) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
const connectedToolkitSlugs = connectedAccounts.items.reduce(
|
|
332
|
+
(acc, account) => {
|
|
333
|
+
if (account.toolkit.slug) {
|
|
334
|
+
acc[account.toolkit.slug] = true;
|
|
335
|
+
}
|
|
336
|
+
return acc;
|
|
337
|
+
},
|
|
338
|
+
{}
|
|
339
|
+
);
|
|
340
|
+
const toolSlugs = tools.reduce(
|
|
341
|
+
(acc, tool) => {
|
|
342
|
+
acc[tool.slug] = true;
|
|
343
|
+
return acc;
|
|
344
|
+
},
|
|
345
|
+
{}
|
|
346
|
+
);
|
|
347
|
+
for (const tool of Object.keys(toolSlugs)) {
|
|
348
|
+
if (!connectedToolkitSlugs[tool]) {
|
|
349
|
+
logger_default.warn(`Tool ${tool} requires a connected account but no connected account was found`);
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Get the connected account id for a given tool
|
|
357
|
+
* @param {string} userId - The user id
|
|
358
|
+
* @param {string} toolSlug - The tool slug
|
|
359
|
+
* @returns {Promise<string | null>} The connected account id or null if the toolkit is a no auth app
|
|
360
|
+
*/
|
|
361
|
+
async getConnectedAccountIdForTool(userId, toolSlug) {
|
|
362
|
+
const tool = await this.getComposioToolBySlug(userId, toolSlug);
|
|
363
|
+
if (!tool.toolkit) {
|
|
364
|
+
throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
|
|
365
|
+
}
|
|
366
|
+
const toolkit = await this.client.toolkits.retrieve(tool.toolkit.slug);
|
|
367
|
+
if (!toolkit) {
|
|
368
|
+
throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
|
|
369
|
+
}
|
|
370
|
+
const isNoAuthApp = toolkit.auth_config_details?.some(
|
|
371
|
+
(authConfigDetails) => authConfigDetails.mode === "NO_AUTH"
|
|
372
|
+
);
|
|
373
|
+
if (!isNoAuthApp) {
|
|
374
|
+
const connectedAccounts = await this.client.connectedAccounts.list({
|
|
375
|
+
user_id: userId,
|
|
376
|
+
toolkit_slug: tool.toolkit.slug
|
|
377
|
+
});
|
|
378
|
+
if (connectedAccounts.items.length === 0) {
|
|
379
|
+
throw new Error("No connected accounts found");
|
|
380
|
+
}
|
|
381
|
+
logger_default.warn(
|
|
382
|
+
`Using the first connected account for tool ${toolSlug}. To change this behaviour please explicitly pass a connectedAccountId for the tool`
|
|
383
|
+
);
|
|
384
|
+
return connectedAccounts.items[0].id;
|
|
385
|
+
}
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Lists all tools available in the Composio SDK as well as custom tools.
|
|
390
|
+
* This method fetches the tools from the Composio API and wraps them using the toolset.
|
|
391
|
+
* @returns {ToolList} List of tools
|
|
392
|
+
*/
|
|
393
|
+
async getComposioTools(userId, query = {}, modifier) {
|
|
394
|
+
const queryParams = ToolListParamsSchema.safeParse(query);
|
|
395
|
+
if (queryParams.error) {
|
|
396
|
+
throw new Error(JSON.stringify(queryParams.error.flatten()));
|
|
397
|
+
}
|
|
398
|
+
const tools = await this.client.tools.list({
|
|
399
|
+
cursor: queryParams.data.cursor,
|
|
400
|
+
important: queryParams.data.important,
|
|
401
|
+
limit: queryParams.data.limit,
|
|
402
|
+
search: queryParams.data.search,
|
|
403
|
+
toolkit_slug: queryParams.data.toolkitSlug,
|
|
404
|
+
tool_slugs: queryParams.data.toolSlugs?.join(",")
|
|
405
|
+
});
|
|
406
|
+
if (!tools) {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
const caseTransformedTools = tools.items.map((tool) => this.transformToolCases(tool));
|
|
410
|
+
const customTools = await this.customTools.getCustomTools({
|
|
411
|
+
toolSlugs: queryParams.data.toolSlugs
|
|
412
|
+
});
|
|
413
|
+
let modifiedTools = [...caseTransformedTools, ...customTools];
|
|
414
|
+
if (modifier) {
|
|
415
|
+
if (typeof modifier === "function") {
|
|
416
|
+
modifiedTools = modifiedTools.map((tool) => {
|
|
417
|
+
return modifier(tool.slug, tool);
|
|
418
|
+
});
|
|
419
|
+
} else {
|
|
420
|
+
throw new Error("Invalid schema modifier. Not a function.");
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return modifiedTools;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Retrieves a tool by its Slug.
|
|
427
|
+
* @param slug The ID of the tool to be retrieved
|
|
428
|
+
* @returns {Promise<Tool>} The tool
|
|
429
|
+
*/
|
|
430
|
+
async getComposioToolBySlug(userId, slug, modifier) {
|
|
431
|
+
const customTool = await this.customTools.getCustomToolBySlug(slug);
|
|
432
|
+
if (customTool) {
|
|
433
|
+
return customTool;
|
|
434
|
+
}
|
|
435
|
+
const tool = await this.client.tools.retrieve(slug);
|
|
436
|
+
if (!tool) {
|
|
437
|
+
throw new Error(`Tool with slug ${slug} not found`);
|
|
438
|
+
}
|
|
439
|
+
let modifiedToool = this.transformToolCases(tool);
|
|
440
|
+
if (modifier) {
|
|
441
|
+
if (typeof modifier === "function") {
|
|
442
|
+
modifiedToool = modifier(slug, modifiedToool);
|
|
443
|
+
} else {
|
|
444
|
+
throw new Error("Invalid schema modifier. Not a function.");
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return modifiedToool;
|
|
448
|
+
}
|
|
449
|
+
async get(userId, arg1, options) {
|
|
450
|
+
if (typeof arg1 === "string") {
|
|
451
|
+
return this.toolset.getToolBySlug(userId, arg1, options);
|
|
452
|
+
} else {
|
|
453
|
+
return this.toolset.getTools(userId, arg1, options);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Exectes a given tool with the provided parameters.
|
|
458
|
+
*
|
|
459
|
+
* This method calls the Composio API to execute the tool and returns the response.
|
|
460
|
+
*
|
|
461
|
+
* @param {string} slug - The ID of the tool to be executed
|
|
462
|
+
* @param {ToolExecuteParams} body - The parameters to be passed to the tool
|
|
463
|
+
* @returns {Promise<ToolExecuteResponse>} - The response from the tool execution
|
|
464
|
+
*/
|
|
465
|
+
async execute(slug, body, modifiers) {
|
|
466
|
+
if (modifiers && modifiers.beforeToolExecute) {
|
|
467
|
+
if (typeof modifiers.beforeToolExecute === "function") {
|
|
468
|
+
body = modifiers.beforeToolExecute(slug, body);
|
|
469
|
+
} else {
|
|
470
|
+
throw new Error("Invalid beforeToolExecute modifier. Not a function.");
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
let result;
|
|
474
|
+
const customTool = await this.customTools.getCustomToolBySlug(slug);
|
|
475
|
+
if (customTool) {
|
|
476
|
+
result = await this.customTools.executeCustomTool(slug, body, {
|
|
477
|
+
userId: body.userId || "default",
|
|
478
|
+
connectedAccountId: body.connectedAccountId
|
|
479
|
+
});
|
|
480
|
+
} else {
|
|
481
|
+
let connectedAccountId = body.connectedAccountId;
|
|
482
|
+
if (!connectedAccountId) {
|
|
483
|
+
connectedAccountId = await this.getConnectedAccountIdForTool(body.userId, slug) || void 0;
|
|
484
|
+
}
|
|
485
|
+
result = await this.client.tools.execute(slug, {
|
|
486
|
+
allow_tracing: body.allowTracing,
|
|
487
|
+
connected_account_id: body.connectedAccountId,
|
|
488
|
+
custom_auth_params: body.customAuthParams,
|
|
489
|
+
arguments: body.arguments,
|
|
490
|
+
entity_id: body.userId,
|
|
491
|
+
version: body.version,
|
|
492
|
+
text: body.text
|
|
493
|
+
});
|
|
494
|
+
result = this.transformToolExecuteResponse(result);
|
|
495
|
+
}
|
|
496
|
+
if (modifiers && modifiers.afterToolExecute) {
|
|
497
|
+
if (typeof modifiers.afterToolExecute === "function") {
|
|
498
|
+
result = modifiers.afterToolExecute(slug, result);
|
|
499
|
+
} else {
|
|
500
|
+
throw new Error("Invalid afterToolExecute modifier. Not a function.");
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return result;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Fetches the list of all available tools in the Composio SDK.
|
|
507
|
+
*
|
|
508
|
+
* This method is mostly used by the CLI to get the list of tools.
|
|
509
|
+
* No filtering is done on the tools, the list is cached in the backend, no further optimization is required.
|
|
510
|
+
* @returns {string} - The list of all available tools as a string
|
|
511
|
+
*/
|
|
512
|
+
async getToolsEnum() {
|
|
513
|
+
return this.client.tools.retrieveEnum();
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Fetches the input parameters for a given tool.
|
|
517
|
+
*
|
|
518
|
+
* This method is used to get the input parameters for a tool before executing it.
|
|
519
|
+
*
|
|
520
|
+
* @param {string} slug - The ID of the tool to find input for
|
|
521
|
+
* @param {ToolGetInputParams} body - The parameters to be passed to the tool
|
|
522
|
+
* @returns
|
|
523
|
+
*/
|
|
524
|
+
async getInput(slug, body) {
|
|
525
|
+
return this.client.tools.getInput(slug, body);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Proxies a custom request to a toolkit/integration
|
|
529
|
+
* @param body
|
|
530
|
+
* @returns
|
|
531
|
+
*/
|
|
532
|
+
async proxyExecute(body) {
|
|
533
|
+
return this.client.tools.proxy(body);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Execute a custom tool
|
|
537
|
+
* @param body
|
|
538
|
+
* @returns
|
|
539
|
+
*/
|
|
540
|
+
async createCustomTool(body) {
|
|
541
|
+
return this.customTools.createTool(body);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
// src/models/Toolkits.ts
|
|
546
|
+
var Toolkits = class {
|
|
547
|
+
client;
|
|
548
|
+
constructor(client) {
|
|
549
|
+
this.client = client;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* List all toolkits available in the Composio SDK.
|
|
553
|
+
* This method fetches the toolkits from the Composio API.
|
|
554
|
+
* @returns {Promise<Toolkit[]>} List of toolkits
|
|
555
|
+
*/
|
|
556
|
+
async getToolkits(query, options) {
|
|
557
|
+
return this.client.toolkits.list(query, options);
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Fetches a toolkit by its Slug.
|
|
561
|
+
* This method retrieves the toolkit from the Composio API.
|
|
562
|
+
* @param slug - The ID of the toolkit to be retrieved
|
|
563
|
+
* @param options - Request options
|
|
564
|
+
* @returns {Promise<ToolkitRetrieveResponse>} The toolkit object
|
|
565
|
+
*/
|
|
566
|
+
async getToolkitBySlug(slug, options) {
|
|
567
|
+
return this.client.toolkits.retrieve(slug, options);
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Fetches all categories available in the Composio SDK
|
|
571
|
+
*
|
|
572
|
+
* @returns {Promise<ToolkitRetrieveCategoriesResponse>} List of categories
|
|
573
|
+
*/
|
|
574
|
+
async listCategories(options) {
|
|
575
|
+
return this.client.toolkits.retrieveCategories(options);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
|
|
187
579
|
// src/utils/pusher.ts
|
|
188
580
|
var PUSHER_KEY = process.env.CLIENT_PUSHER_KEY || "ff9f18c208855d77a152";
|
|
189
581
|
var PUSHER_CLUSTER = "mt1";
|
|
@@ -277,7 +669,6 @@ var PusherUtils = class _PusherUtils {
|
|
|
277
669
|
|
|
278
670
|
// src/models/Session.ts
|
|
279
671
|
var Session = class {
|
|
280
|
-
FILE_NAME = "core/models/Session.ts";
|
|
281
672
|
client;
|
|
282
673
|
constructor(client) {
|
|
283
674
|
this.client = client;
|
|
@@ -293,7 +684,6 @@ var Session = class {
|
|
|
293
684
|
|
|
294
685
|
// src/models/Triggers.ts
|
|
295
686
|
var Triggers = class {
|
|
296
|
-
FILE_NAME = "core/models/Triggers.ts";
|
|
297
687
|
client;
|
|
298
688
|
constructor(client) {
|
|
299
689
|
this.client = client;
|
|
@@ -427,68 +817,21 @@ var Triggers = class {
|
|
|
427
817
|
}
|
|
428
818
|
};
|
|
429
819
|
|
|
430
|
-
// src/
|
|
431
|
-
var
|
|
820
|
+
// src/models/AuthConfigs.ts
|
|
821
|
+
var AuthConfigs = class {
|
|
822
|
+
FILE_NAME = "core/models/AuthConfigs.ts";
|
|
432
823
|
client;
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Set the client for the toolset. This is automatically done by the Composio class.
|
|
436
|
-
* @param client - The Composio client.
|
|
437
|
-
*/
|
|
438
|
-
setClient(client) {
|
|
824
|
+
constructor(client) {
|
|
439
825
|
this.client = client;
|
|
440
826
|
}
|
|
441
827
|
/**
|
|
442
|
-
*
|
|
443
|
-
* @param
|
|
444
|
-
* @
|
|
828
|
+
* List all auth configs
|
|
829
|
+
* @param {AuthConfigListParams} query - Query parameters for filtering auth configs
|
|
830
|
+
* @param {RequestOptions} options - Request options
|
|
831
|
+
* @returns {Promise<AuthConfigListResponse>} List of auth configs
|
|
445
832
|
*/
|
|
446
|
-
async
|
|
447
|
-
|
|
448
|
-
return this._wrapTool(tool);
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* Ensure the client is initialized.
|
|
452
|
-
* This is automatically done by the Composio class.
|
|
453
|
-
*/
|
|
454
|
-
ensureClient() {
|
|
455
|
-
if (!this.client) {
|
|
456
|
-
throw new Error(
|
|
457
|
-
"Client not initialized. Make sure the toolset is properly initialized with Composio."
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
};
|
|
462
|
-
|
|
463
|
-
// src/toolset/ComposioToolset.ts
|
|
464
|
-
var ComposioToolset = class extends BaseComposioToolset {
|
|
465
|
-
FILE_NAME = "core/toolset/ComposioToolset.ts";
|
|
466
|
-
_wrapTool = (tool) => {
|
|
467
|
-
return tool;
|
|
468
|
-
};
|
|
469
|
-
async getTools(params) {
|
|
470
|
-
const tools = await this.client?.tools.list(params);
|
|
471
|
-
return tools?.items.map((tool) => this._wrapTool(tool)) ?? [];
|
|
472
|
-
}
|
|
473
|
-
async test() {
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
|
|
477
|
-
// src/models/AuthConfigs.ts
|
|
478
|
-
var AuthConfigs = class {
|
|
479
|
-
FILE_NAME = "core/models/AuthConfigs.ts";
|
|
480
|
-
client;
|
|
481
|
-
constructor(client) {
|
|
482
|
-
this.client = client;
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* List all auth configs
|
|
486
|
-
* @param {AuthConfigListParams} query - Query parameters for filtering auth configs
|
|
487
|
-
* @param {RequestOptions} options - Request options
|
|
488
|
-
* @returns {Promise<AuthConfigListResponse>} List of auth configs
|
|
489
|
-
*/
|
|
490
|
-
async list(query, options) {
|
|
491
|
-
return this.client.authConfigs.list(query, options);
|
|
833
|
+
async list(query, options) {
|
|
834
|
+
return this.client.authConfigs.list(query, options);
|
|
492
835
|
}
|
|
493
836
|
/**
|
|
494
837
|
* Create a new auth config
|
|
@@ -557,9 +900,161 @@ var AuthConfigs = class {
|
|
|
557
900
|
}
|
|
558
901
|
};
|
|
559
902
|
|
|
903
|
+
// src/types/connectedAccounts.types.ts
|
|
904
|
+
import { z as z2 } from "zod";
|
|
905
|
+
var ConnectedAccountStatuses = {
|
|
906
|
+
ACTIVE: "ACTIVE",
|
|
907
|
+
INACTIVE: "INACTIVE",
|
|
908
|
+
PENDING: "DELETED",
|
|
909
|
+
INITIATED: "INITIATED",
|
|
910
|
+
EXPIRED: "EXPIRED",
|
|
911
|
+
FAILED: "FAILED"
|
|
912
|
+
};
|
|
913
|
+
var ConnectedAccountStatusSchema = z2.enum([
|
|
914
|
+
ConnectedAccountStatuses.ACTIVE,
|
|
915
|
+
ConnectedAccountStatuses.INACTIVE,
|
|
916
|
+
ConnectedAccountStatuses.PENDING,
|
|
917
|
+
ConnectedAccountStatuses.INITIATED,
|
|
918
|
+
ConnectedAccountStatuses.EXPIRED,
|
|
919
|
+
ConnectedAccountStatuses.FAILED
|
|
920
|
+
]);
|
|
921
|
+
var CreateConnectedAccountParamsSchema = z2.object({
|
|
922
|
+
authConfig: z2.object({
|
|
923
|
+
id: z2.string()
|
|
924
|
+
}),
|
|
925
|
+
connection: z2.object({
|
|
926
|
+
data: z2.record(z2.string(), z2.unknown()).optional(),
|
|
927
|
+
rediretUrl: z2.string().optional(),
|
|
928
|
+
userId: z2.string().optional()
|
|
929
|
+
})
|
|
930
|
+
});
|
|
931
|
+
var CreateConnectedAccountResponseSchema = z2.object({
|
|
932
|
+
id: z2.string(),
|
|
933
|
+
status: ConnectedAccountStatusSchema,
|
|
934
|
+
redirectUrl: z2.string().nullable()
|
|
935
|
+
});
|
|
936
|
+
var ConnectedAccountAuthSchemes = {
|
|
937
|
+
OAUTH2: "OAUTH2",
|
|
938
|
+
OAUTH1: "OAUTH1",
|
|
939
|
+
OAUTH1A: "OAUTH1A",
|
|
940
|
+
API_KEY: "API_KEY",
|
|
941
|
+
BASIC: "BASIC",
|
|
942
|
+
BILLCOM_AUTH: "BILLCOM_AUTH",
|
|
943
|
+
BEARER_TOKEN: "BEARER_TOKEN",
|
|
944
|
+
GOOGLE_SERVICE_ACCOUNT: "GOOGLE_SERVICE_ACCOUNT",
|
|
945
|
+
NO_AUTH: "NO_AUTH",
|
|
946
|
+
BASIC_WITH_JWT: "BASIC_WITH_JWT",
|
|
947
|
+
COMPOSIO_LINK: "COMPOSIO_LINK",
|
|
948
|
+
CALCOM_AUTH: "CALCOM_AUTH",
|
|
949
|
+
SNOWFLAKE: "SNOWFLAKE"
|
|
950
|
+
};
|
|
951
|
+
var ConnectedAccountAuthSchemesSchema = z2.enum([
|
|
952
|
+
ConnectedAccountAuthSchemes.OAUTH2,
|
|
953
|
+
ConnectedAccountAuthSchemes.OAUTH1,
|
|
954
|
+
ConnectedAccountAuthSchemes.OAUTH1A,
|
|
955
|
+
ConnectedAccountAuthSchemes.API_KEY,
|
|
956
|
+
ConnectedAccountAuthSchemes.BASIC,
|
|
957
|
+
ConnectedAccountAuthSchemes.BILLCOM_AUTH,
|
|
958
|
+
ConnectedAccountAuthSchemes.BEARER_TOKEN,
|
|
959
|
+
ConnectedAccountAuthSchemes.GOOGLE_SERVICE_ACCOUNT,
|
|
960
|
+
ConnectedAccountAuthSchemes.NO_AUTH,
|
|
961
|
+
ConnectedAccountAuthSchemes.BASIC_WITH_JWT,
|
|
962
|
+
ConnectedAccountAuthSchemes.COMPOSIO_LINK,
|
|
963
|
+
ConnectedAccountAuthSchemes.CALCOM_AUTH,
|
|
964
|
+
ConnectedAccountAuthSchemes.SNOWFLAKE
|
|
965
|
+
]);
|
|
966
|
+
var ConnectedAccountAuthConfigSchema = z2.object({
|
|
967
|
+
id: z2.string(),
|
|
968
|
+
authScheme: ConnectedAccountAuthSchemesSchema,
|
|
969
|
+
isComposioManaged: z2.boolean(),
|
|
970
|
+
isDisabled: z2.boolean()
|
|
971
|
+
});
|
|
972
|
+
var ConnectedAccountRetrieveResponseSchema = z2.object({
|
|
973
|
+
id: z2.string(),
|
|
974
|
+
uuid: z2.string(),
|
|
975
|
+
authConfig: ConnectedAccountAuthConfigSchema,
|
|
976
|
+
userId: z2.string(),
|
|
977
|
+
data: z2.record(z2.string(), z2.unknown()),
|
|
978
|
+
params: z2.record(z2.string(), z2.unknown()),
|
|
979
|
+
status: ConnectedAccountStatusSchema,
|
|
980
|
+
statusReason: z2.string().nullable(),
|
|
981
|
+
toolkit: z2.object({
|
|
982
|
+
slug: z2.string()
|
|
983
|
+
}),
|
|
984
|
+
testRequestEndpoint: z2.string().optional(),
|
|
985
|
+
isDisabled: z2.boolean(),
|
|
986
|
+
createdAt: z2.string(),
|
|
987
|
+
updatedAt: z2.string()
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
// src/models/ConnectionRequest.ts
|
|
991
|
+
var ConnectionRequest = class {
|
|
992
|
+
FILE_NAME = "core/models/ConnectionRequest.ts";
|
|
993
|
+
client;
|
|
994
|
+
connectedAccountId;
|
|
995
|
+
connectedAccountStatus;
|
|
996
|
+
constructor(client, connectedAccountId, connectedAccountStatus) {
|
|
997
|
+
this.client = client;
|
|
998
|
+
this.connectedAccountId = connectedAccountId;
|
|
999
|
+
this.connectedAccountStatus = connectedAccountStatus;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Transform the response from the Composio API to the SDK format
|
|
1003
|
+
* @param response
|
|
1004
|
+
* @returns
|
|
1005
|
+
*/
|
|
1006
|
+
async transformResponse(response) {
|
|
1007
|
+
return {
|
|
1008
|
+
...response,
|
|
1009
|
+
userId: response.user_id,
|
|
1010
|
+
// Add the missing userId property
|
|
1011
|
+
authConfig: {
|
|
1012
|
+
...response.auth_config,
|
|
1013
|
+
authScheme: response.auth_config.auth_scheme,
|
|
1014
|
+
isComposioManaged: response.auth_config.is_composio_managed,
|
|
1015
|
+
isDisabled: response.auth_config.is_disabled
|
|
1016
|
+
},
|
|
1017
|
+
statusReason: response.status_reason,
|
|
1018
|
+
isDisabled: response.is_disabled,
|
|
1019
|
+
createdAt: response.created_at,
|
|
1020
|
+
updatedAt: response.updated_at,
|
|
1021
|
+
testRequestEndpoint: response.test_request_endpoint
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Wait for the connection to be established.
|
|
1026
|
+
* The function will poll the Composio API every second until the connection is established or the timeout is reached.
|
|
1027
|
+
* @param {number} timeout - The time in milliseconds to wait for the connection to be established. Default is 30 seconds.
|
|
1028
|
+
* @returns
|
|
1029
|
+
*/
|
|
1030
|
+
async waitForConnection(timeout = 6e4) {
|
|
1031
|
+
if (this.connectedAccountStatus === ConnectedAccountStatuses.ACTIVE) {
|
|
1032
|
+
const response = await this.client.connectedAccounts.retrieve(this.connectedAccountId);
|
|
1033
|
+
return Promise.resolve(this.transformResponse(response));
|
|
1034
|
+
}
|
|
1035
|
+
return new Promise((resolve, reject) => {
|
|
1036
|
+
const interval = setInterval(async () => {
|
|
1037
|
+
try {
|
|
1038
|
+
const response = await this.client.connectedAccounts.retrieve(this.connectedAccountId);
|
|
1039
|
+
if (response.status === ConnectedAccountStatuses.ACTIVE) {
|
|
1040
|
+
clearInterval(interval);
|
|
1041
|
+
resolve(this.transformResponse(response));
|
|
1042
|
+
}
|
|
1043
|
+
} catch (error) {
|
|
1044
|
+
clearInterval(interval);
|
|
1045
|
+
reject(error);
|
|
1046
|
+
}
|
|
1047
|
+
}, 1e3);
|
|
1048
|
+
setTimeout(() => {
|
|
1049
|
+
clearInterval(interval);
|
|
1050
|
+
reject(new Error("Connection request timed out"));
|
|
1051
|
+
}, timeout);
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
|
|
560
1056
|
// src/models/ConnectedAccounts.ts
|
|
561
1057
|
var ConnectedAccounts = class {
|
|
562
|
-
FILE_NAME = "core/models/ConnectedAccounts.ts";
|
|
563
1058
|
client;
|
|
564
1059
|
constructor(client) {
|
|
565
1060
|
this.client = client;
|
|
@@ -637,6 +1132,27 @@ var ConnectedAccounts = class {
|
|
|
637
1132
|
async disable(nanoid, options) {
|
|
638
1133
|
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: false }, options);
|
|
639
1134
|
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Compound function to create a new connected account.
|
|
1137
|
+
* This function creates a new connected account and returns a connection request.
|
|
1138
|
+
* Users can then wait for the connection to be established using the `waitForConnection` method.
|
|
1139
|
+
* @param {CreateConnectedAccountParams} data - Data for creating a new connected account
|
|
1140
|
+
* @returns {Promise<ConnectionRequest>} Connection request object
|
|
1141
|
+
*/
|
|
1142
|
+
async createConnectedAccount(data) {
|
|
1143
|
+
const { authConfig, connection } = data;
|
|
1144
|
+
const response = await this.create({
|
|
1145
|
+
auth_config: {
|
|
1146
|
+
id: authConfig.id
|
|
1147
|
+
},
|
|
1148
|
+
connection: {
|
|
1149
|
+
data: connection.data,
|
|
1150
|
+
redirect_uri: connection.rediretUrl,
|
|
1151
|
+
user_id: connection.userId
|
|
1152
|
+
}
|
|
1153
|
+
});
|
|
1154
|
+
return new ConnectionRequest(this.client, response.id, response.status);
|
|
1155
|
+
}
|
|
640
1156
|
};
|
|
641
1157
|
|
|
642
1158
|
// src/types/telemetry.types.ts
|
|
@@ -813,13 +1329,14 @@ var Telemetry = class {
|
|
|
813
1329
|
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
814
1330
|
return key !== "constructor" && descriptor && typeof descriptor.value === "function" && descriptor.value.constructor.name === "AsyncFunction";
|
|
815
1331
|
});
|
|
1332
|
+
const instrumentedClassName = instance.constructor?.name || fileName || "unknown";
|
|
816
1333
|
for (const name of methodNames) {
|
|
817
1334
|
const originalMethod = instance[name];
|
|
818
1335
|
instance[name] = async (...args) => {
|
|
819
1336
|
const telemetryPayload = {
|
|
820
1337
|
eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
|
|
821
1338
|
data: {
|
|
822
|
-
fileName:
|
|
1339
|
+
fileName: instrumentedClassName,
|
|
823
1340
|
method: name,
|
|
824
1341
|
params: args
|
|
825
1342
|
},
|
|
@@ -832,7 +1349,7 @@ var Telemetry = class {
|
|
|
832
1349
|
const telemetryPayload2 = {
|
|
833
1350
|
eventName: TELEMETRY_EVENTS.SDK_METHOD_ERROR,
|
|
834
1351
|
data: {
|
|
835
|
-
fileName:
|
|
1352
|
+
fileName: instrumentedClassName,
|
|
836
1353
|
method: name,
|
|
837
1354
|
params: args,
|
|
838
1355
|
error
|
|
@@ -894,9 +1411,6 @@ function getSDKConfig(baseUrl, apiKey) {
|
|
|
894
1411
|
return { baseURL: baseURLParsed, apiKey: apiKeyParsed };
|
|
895
1412
|
}
|
|
896
1413
|
|
|
897
|
-
// package.json
|
|
898
|
-
var version = "0.1.0-rc.5";
|
|
899
|
-
|
|
900
1414
|
// src/utils/version.ts
|
|
901
1415
|
function isNewerVersion(version1, version2) {
|
|
902
1416
|
const v1 = version1.split(".").map(Number);
|
|
@@ -907,10 +1421,10 @@ function isNewerVersion(version1, version2) {
|
|
|
907
1421
|
}
|
|
908
1422
|
return false;
|
|
909
1423
|
}
|
|
910
|
-
async function checkForLatestVersionFromNPM() {
|
|
1424
|
+
async function checkForLatestVersionFromNPM(currentVersion) {
|
|
911
1425
|
try {
|
|
912
1426
|
const packageName = "composio-core";
|
|
913
|
-
const currentVersionFromPackageJson =
|
|
1427
|
+
const currentVersionFromPackageJson = currentVersion;
|
|
914
1428
|
const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`);
|
|
915
1429
|
const data = await response.json();
|
|
916
1430
|
const latestVersion = data.version;
|
|
@@ -923,10 +1437,229 @@ async function checkForLatestVersionFromNPM() {
|
|
|
923
1437
|
}
|
|
924
1438
|
}
|
|
925
1439
|
|
|
1440
|
+
// src/toolset/BaseToolset.ts
|
|
1441
|
+
var BaseNonAgenticToolset = class {
|
|
1442
|
+
composio;
|
|
1443
|
+
setComposio(composio) {
|
|
1444
|
+
this.composio = composio;
|
|
1445
|
+
}
|
|
1446
|
+
getComposio() {
|
|
1447
|
+
if (!this.composio) {
|
|
1448
|
+
throw new Error(
|
|
1449
|
+
"Client not initialized. Make sure the toolset is properly initialized with Composio."
|
|
1450
|
+
);
|
|
1451
|
+
}
|
|
1452
|
+
return this.composio;
|
|
1453
|
+
}
|
|
1454
|
+
};
|
|
1455
|
+
var BaseAgenticToolset = class {
|
|
1456
|
+
composio;
|
|
1457
|
+
setComposio(composio) {
|
|
1458
|
+
this.composio = composio;
|
|
1459
|
+
}
|
|
1460
|
+
getComposio() {
|
|
1461
|
+
if (!this.composio) {
|
|
1462
|
+
throw new Error(
|
|
1463
|
+
"Client not initialized. Make sure the toolset is properly initialized with Composio."
|
|
1464
|
+
);
|
|
1465
|
+
}
|
|
1466
|
+
return this.composio;
|
|
1467
|
+
}
|
|
1468
|
+
};
|
|
1469
|
+
|
|
1470
|
+
// src/toolset/OpenAIToolset.ts
|
|
1471
|
+
var OpenAIToolset = class extends BaseNonAgenticToolset {
|
|
1472
|
+
/**
|
|
1473
|
+
* Absract method to wrap a tool in the toolset.
|
|
1474
|
+
* This method is implemented by the toolset.
|
|
1475
|
+
* @param tool - The tool to wrap.
|
|
1476
|
+
* @returns The wrapped tool.
|
|
1477
|
+
*/
|
|
1478
|
+
wrapTool = (tool) => {
|
|
1479
|
+
const formattedSchema = {
|
|
1480
|
+
name: tool.slug,
|
|
1481
|
+
description: tool.description,
|
|
1482
|
+
parameters: tool.inputParameters
|
|
1483
|
+
};
|
|
1484
|
+
return {
|
|
1485
|
+
type: "function",
|
|
1486
|
+
function: formattedSchema
|
|
1487
|
+
};
|
|
1488
|
+
};
|
|
1489
|
+
/**
|
|
1490
|
+
* Get all the tools from the Composio in OpenAI format.
|
|
1491
|
+
* @param params - The parameters for the tool list.
|
|
1492
|
+
* @param modifiers - Optional modifiers to transform tool schemas
|
|
1493
|
+
* @returns The tools.
|
|
1494
|
+
*/
|
|
1495
|
+
async getTools(userId, params, modifiers) {
|
|
1496
|
+
const tools = await this.getComposio()?.tools.getComposioTools(
|
|
1497
|
+
userId,
|
|
1498
|
+
params,
|
|
1499
|
+
modifiers?.modifyToolSchema
|
|
1500
|
+
);
|
|
1501
|
+
return tools?.map((tool) => this.wrapTool(tool)) ?? [];
|
|
1502
|
+
}
|
|
1503
|
+
async getToolBySlug(userId, slug, modifiers) {
|
|
1504
|
+
const tool = await this.getComposio().tools.getComposioToolBySlug(
|
|
1505
|
+
userId,
|
|
1506
|
+
slug,
|
|
1507
|
+
modifiers?.modifyToolSchema
|
|
1508
|
+
);
|
|
1509
|
+
return this.wrapTool(tool);
|
|
1510
|
+
}
|
|
1511
|
+
/**
|
|
1512
|
+
* Execute a tool call.
|
|
1513
|
+
* @param {OpenAI.ChatCompletionMessageToolCall} tool - The tool to execute.
|
|
1514
|
+
* @param {string} userId - The user id.
|
|
1515
|
+
* @returns {Promise<string>} The result of the tool call.
|
|
1516
|
+
*/
|
|
1517
|
+
async executeToolCall(userId, tool, modifiers) {
|
|
1518
|
+
const toolSchema = await this.getComposio().tools.getComposioToolBySlug(
|
|
1519
|
+
userId,
|
|
1520
|
+
tool.function.name
|
|
1521
|
+
);
|
|
1522
|
+
const appSlug = toolSchema?.toolkit?.slug.toLowerCase();
|
|
1523
|
+
if (!appSlug) {
|
|
1524
|
+
throw new Error("App slug not found");
|
|
1525
|
+
}
|
|
1526
|
+
const payload = {
|
|
1527
|
+
userId,
|
|
1528
|
+
arguments: JSON.parse(tool.function.arguments)
|
|
1529
|
+
};
|
|
1530
|
+
const results = await this.getComposio().tools.execute(toolSchema.slug, payload, modifiers);
|
|
1531
|
+
return JSON.stringify(results);
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* Handle the tool call from the assistant.
|
|
1535
|
+
*
|
|
1536
|
+
* @param {OpenAI.ChatCompletion} chatCompletion - The chat completion object.
|
|
1537
|
+
* @param {string} userId - The user id.
|
|
1538
|
+
* @returns {Promise<string[]>} The results of the tool call.
|
|
1539
|
+
*/
|
|
1540
|
+
async handleToolCall(userId, chatCompletion, modifiers) {
|
|
1541
|
+
const outputs = [];
|
|
1542
|
+
for (const message of chatCompletion.choices) {
|
|
1543
|
+
if (message.message.tool_calls) {
|
|
1544
|
+
outputs.push(await this.executeToolCall(userId, message.message.tool_calls[0], modifiers));
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
return outputs;
|
|
1548
|
+
}
|
|
1549
|
+
/**
|
|
1550
|
+
* Handles all the tool calls from the assistant.
|
|
1551
|
+
*
|
|
1552
|
+
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1553
|
+
* @param {string} userId - The user id.
|
|
1554
|
+
* @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.
|
|
1555
|
+
*/
|
|
1556
|
+
async handleAssistantMessage(userId, run, modifiers) {
|
|
1557
|
+
const tool_calls = run.required_action?.submit_tool_outputs?.tool_calls || [];
|
|
1558
|
+
const tool_outputs = await Promise.all(
|
|
1559
|
+
tool_calls.map(async (tool_call) => {
|
|
1560
|
+
logger_default.debug(`Executing tool call: ${tool_call.id}`);
|
|
1561
|
+
const tool_response = await this.executeToolCall(
|
|
1562
|
+
userId,
|
|
1563
|
+
tool_call,
|
|
1564
|
+
modifiers
|
|
1565
|
+
);
|
|
1566
|
+
logger_default.debug(`Tool call ${tool_call.id} executed with response: ${tool_response}`);
|
|
1567
|
+
return {
|
|
1568
|
+
tool_call_id: tool_call.id,
|
|
1569
|
+
output: JSON.stringify(tool_response)
|
|
1570
|
+
};
|
|
1571
|
+
})
|
|
1572
|
+
);
|
|
1573
|
+
return tool_outputs;
|
|
1574
|
+
}
|
|
1575
|
+
/**
|
|
1576
|
+
* Waits for the assistant stream and handles the tool calls.
|
|
1577
|
+
*
|
|
1578
|
+
* @param {OpenAI} client - The OpenAI client.
|
|
1579
|
+
* @param {Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>} runStream - The run stream.
|
|
1580
|
+
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1581
|
+
* @param {string} userId - The user id.
|
|
1582
|
+
* @returns {AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>} The run stream.
|
|
1583
|
+
*/
|
|
1584
|
+
async *waitAndHandleAssistantStreamToolCalls(userId, client, runStream, thread, modifiers) {
|
|
1585
|
+
let runId = null;
|
|
1586
|
+
for await (const event of runStream) {
|
|
1587
|
+
yield event;
|
|
1588
|
+
if (event.event === "thread.run.created") {
|
|
1589
|
+
const { id } = event.data;
|
|
1590
|
+
runId = id;
|
|
1591
|
+
}
|
|
1592
|
+
if (!runId) {
|
|
1593
|
+
continue;
|
|
1594
|
+
}
|
|
1595
|
+
if (event.event === "thread.run.requires_action") {
|
|
1596
|
+
const toolOutputs = await this.handleAssistantMessage(userId, event.data, modifiers);
|
|
1597
|
+
await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
|
|
1598
|
+
tool_outputs: toolOutputs
|
|
1599
|
+
});
|
|
1600
|
+
}
|
|
1601
|
+
if ([
|
|
1602
|
+
"thread.run.completed",
|
|
1603
|
+
"thread.run.failed",
|
|
1604
|
+
"thread.run.cancelled",
|
|
1605
|
+
"thread.run.expired"
|
|
1606
|
+
].includes(event.event)) {
|
|
1607
|
+
break;
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
if (!runId) {
|
|
1611
|
+
throw new Error("No run ID found");
|
|
1612
|
+
}
|
|
1613
|
+
let finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1614
|
+
while (["queued", "in_progress", "requires_action"].includes(finalRun.status)) {
|
|
1615
|
+
if (finalRun.status === "requires_action") {
|
|
1616
|
+
const toolOutputs = await this.handleAssistantMessage(userId, finalRun, modifiers);
|
|
1617
|
+
finalRun = await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
|
|
1618
|
+
tool_outputs: toolOutputs
|
|
1619
|
+
});
|
|
1620
|
+
} else {
|
|
1621
|
+
finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1622
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
/**
|
|
1627
|
+
* Waits for the assistant tool calls and handles them.
|
|
1628
|
+
*
|
|
1629
|
+
* @param {OpenAI} client - The OpenAI client.
|
|
1630
|
+
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1631
|
+
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1632
|
+
* @param {string} userId - The user id.
|
|
1633
|
+
* @returns {Promise<OpenAI.Beta.Threads.Run>} The run object.
|
|
1634
|
+
*/
|
|
1635
|
+
async waitAndHandleAssistantToolCalls(userId, client, run, thread, modifiers) {
|
|
1636
|
+
while (["queued", "in_progress", "requires_action"].includes(run.status)) {
|
|
1637
|
+
const tool_outputs = await this.handleAssistantMessage(userId, run, modifiers);
|
|
1638
|
+
if (run.status === "requires_action") {
|
|
1639
|
+
run = await client.beta.threads.runs.submitToolOutputs(thread.id, run.id, {
|
|
1640
|
+
tool_outputs
|
|
1641
|
+
});
|
|
1642
|
+
} else {
|
|
1643
|
+
run = await client.beta.threads.runs.retrieve(thread.id, run.id);
|
|
1644
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
return run;
|
|
1648
|
+
}
|
|
1649
|
+
};
|
|
1650
|
+
|
|
1651
|
+
// package.json
|
|
1652
|
+
var version = "0.1.0-rc.7";
|
|
1653
|
+
|
|
1654
|
+
// src/utils/uuid.ts
|
|
1655
|
+
import { v4 as uuidv4 } from "uuid";
|
|
1656
|
+
function getRandomUUID() {
|
|
1657
|
+
return uuidv4();
|
|
1658
|
+
}
|
|
1659
|
+
|
|
926
1660
|
// src/composio.ts
|
|
927
1661
|
var Composio = class {
|
|
928
1662
|
DEFAULT_USER_ID = "default";
|
|
929
|
-
FILE_NAME = "core/composio.ts";
|
|
930
1663
|
/**
|
|
931
1664
|
* The Composio API client.
|
|
932
1665
|
* @type {ComposioClient}
|
|
@@ -934,15 +1667,10 @@ var Composio = class {
|
|
|
934
1667
|
client;
|
|
935
1668
|
/**
|
|
936
1669
|
* The configuration for the Composio SDK.
|
|
937
|
-
* @type {ComposioConfig<
|
|
1670
|
+
* @type {ComposioConfig<TToolset>}
|
|
938
1671
|
*/
|
|
939
1672
|
config;
|
|
940
1673
|
telemetry;
|
|
941
|
-
/**
|
|
942
|
-
* Context variables for the Composio SDK.
|
|
943
|
-
*/
|
|
944
|
-
userId;
|
|
945
|
-
connectedAccountIds;
|
|
946
1674
|
/**
|
|
947
1675
|
* Core models for Composio.
|
|
948
1676
|
*/
|
|
@@ -951,7 +1679,9 @@ var Composio = class {
|
|
|
951
1679
|
triggers;
|
|
952
1680
|
toolset;
|
|
953
1681
|
authConfigs;
|
|
1682
|
+
// connected accounts
|
|
954
1683
|
connectedAccounts;
|
|
1684
|
+
createConnectedAccount;
|
|
955
1685
|
/**
|
|
956
1686
|
* @param {Object} config Configuration for the Composio SDK.
|
|
957
1687
|
* @param {string} config.apiKey The API key for the Composio SDK.
|
|
@@ -959,7 +1689,7 @@ var Composio = class {
|
|
|
959
1689
|
* @param {string} config.runtime The runtime for the Composio SDK.
|
|
960
1690
|
* @param {boolean} config.allowTracking Whether to allow analytics / tracking. Defaults to true.
|
|
961
1691
|
* @param {boolean} config.allowTracing Whether to allow tracing. Defaults to true.
|
|
962
|
-
* @param {
|
|
1692
|
+
* @param {TToolset} config.toolset The toolset to use for this Composio instance.
|
|
963
1693
|
*/
|
|
964
1694
|
constructor(config) {
|
|
965
1695
|
const { baseURL: baseURLParsed, apiKey: apiKeyParsed } = getSDKConfig(
|
|
@@ -969,11 +1699,6 @@ var Composio = class {
|
|
|
969
1699
|
if (IS_DEVELOPMENT_OR_CI) {
|
|
970
1700
|
logger_default.info(`Initializing Composio w API Key: [REDACTED] and baseURL: ${baseURLParsed}`);
|
|
971
1701
|
}
|
|
972
|
-
if (config.userId && config.connectedAccountIds) {
|
|
973
|
-
logger_default.warn(
|
|
974
|
-
"When both userId and connectedAccountIds are provided, preference will be given to connectedAccountIds"
|
|
975
|
-
);
|
|
976
|
-
}
|
|
977
1702
|
this.client = new ComposioClient({
|
|
978
1703
|
apiKey: apiKeyParsed,
|
|
979
1704
|
baseURL: baseURLParsed
|
|
@@ -982,15 +1707,14 @@ var Composio = class {
|
|
|
982
1707
|
...config,
|
|
983
1708
|
allowTracking: config.allowTracking ?? true
|
|
984
1709
|
};
|
|
985
|
-
this.
|
|
986
|
-
this.
|
|
987
|
-
this.
|
|
988
|
-
this.toolset.setClient(this);
|
|
989
|
-
this.tools = new Tools(this.client);
|
|
1710
|
+
this.toolset = config.toolset ?? new OpenAIToolset();
|
|
1711
|
+
this.toolset.setComposio(this);
|
|
1712
|
+
this.tools = new Tools(this.client, this.toolset);
|
|
990
1713
|
this.toolkits = new Toolkits(this.client);
|
|
991
1714
|
this.triggers = new Triggers(this.client);
|
|
992
1715
|
this.authConfigs = new AuthConfigs(this.client);
|
|
993
1716
|
this.connectedAccounts = new ConnectedAccounts(this.client);
|
|
1717
|
+
this.createConnectedAccount = this.connectedAccounts.createConnectedAccount;
|
|
994
1718
|
if (this.config.allowTracking ?? true) {
|
|
995
1719
|
this.initializeTelemetry(
|
|
996
1720
|
{
|
|
@@ -999,14 +1723,15 @@ var Composio = class {
|
|
|
999
1723
|
frameworkRuntime: "node",
|
|
1000
1724
|
source: "node",
|
|
1001
1725
|
// @TODO: get the source
|
|
1002
|
-
sessionId: this.userId,
|
|
1003
1726
|
composioVersion: version,
|
|
1004
|
-
isBrowser: typeof window !== "undefined"
|
|
1727
|
+
isBrowser: typeof window !== "undefined",
|
|
1728
|
+
sessionId: getRandomUUID()
|
|
1729
|
+
// @TODO: get the session id
|
|
1005
1730
|
},
|
|
1006
1731
|
config.telemetryTransport
|
|
1007
1732
|
);
|
|
1008
1733
|
}
|
|
1009
|
-
checkForLatestVersionFromNPM();
|
|
1734
|
+
checkForLatestVersionFromNPM(version);
|
|
1010
1735
|
}
|
|
1011
1736
|
/**
|
|
1012
1737
|
* Initialize the instrumentations and telemetry for the Composio SDK.
|
|
@@ -1023,14 +1748,6 @@ var Composio = class {
|
|
|
1023
1748
|
this.telemetry.instrumentTelemetry(this.connectedAccounts);
|
|
1024
1749
|
this.telemetry.instrumentTelemetry(this.toolset);
|
|
1025
1750
|
}
|
|
1026
|
-
/**
|
|
1027
|
-
* Get the connected account id for the given app name.
|
|
1028
|
-
* @param appName - The name of the app.
|
|
1029
|
-
* @returns {string | undefined} The connected account id for the given app name.
|
|
1030
|
-
*/
|
|
1031
|
-
getConnectedAccountId(appName) {
|
|
1032
|
-
return this.connectedAccountIds?.[appName];
|
|
1033
|
-
}
|
|
1034
1751
|
/**
|
|
1035
1752
|
* Get the Composio SDK client.
|
|
1036
1753
|
* @returns {ComposioClient} The Composio API client.
|
|
@@ -1042,56 +1759,92 @@ var Composio = class {
|
|
|
1042
1759
|
return this.client;
|
|
1043
1760
|
}
|
|
1044
1761
|
/**
|
|
1045
|
-
*
|
|
1046
|
-
* @
|
|
1762
|
+
* Fetch all the tools from Composio.
|
|
1763
|
+
* @param {ToolListParams} params Parameters to fetch the tools.
|
|
1764
|
+
* @param {ToolsetOptions<TToolset>} modifiers Modifiers to apply to the tools.
|
|
1765
|
+
* @returns {Promise<ReturnType<TToolset['getTools']>>} The tools from the toolset.
|
|
1047
1766
|
*/
|
|
1048
|
-
|
|
1049
|
-
return this.
|
|
1767
|
+
getTools(userId, filters, modifiers) {
|
|
1768
|
+
return this.tools.get(userId, filters, modifiers);
|
|
1050
1769
|
}
|
|
1051
1770
|
/**
|
|
1052
|
-
*
|
|
1053
|
-
* @param
|
|
1054
|
-
* @
|
|
1771
|
+
* Fetch a tool from Composio by its slug.
|
|
1772
|
+
* @param {string} slug slug of the tool
|
|
1773
|
+
* @param {ToolsetOptions<TToolset>} modifiers to be applied to the tool
|
|
1774
|
+
* @returns {Promise<ReturnType<TToolset['getToolBySlug']>>} The tool from the toolset.
|
|
1055
1775
|
*/
|
|
1056
|
-
|
|
1057
|
-
return this.
|
|
1776
|
+
getToolBySlug(userId, slug, modifiers) {
|
|
1777
|
+
return this.tools.get(userId, slug, modifiers);
|
|
1058
1778
|
}
|
|
1059
1779
|
};
|
|
1060
1780
|
|
|
1781
|
+
// src/toolset/ComposioToolset.ts
|
|
1782
|
+
var ComposioToolset = class extends BaseNonAgenticToolset {
|
|
1783
|
+
FILE_NAME = "core/toolset/ComposioToolset.ts";
|
|
1784
|
+
constructor() {
|
|
1785
|
+
super();
|
|
1786
|
+
}
|
|
1787
|
+
wrapTool = (tool) => {
|
|
1788
|
+
return tool;
|
|
1789
|
+
};
|
|
1790
|
+
async getTools(userId, params, options) {
|
|
1791
|
+
const tools = await this.getComposio().tools.getComposioTools(
|
|
1792
|
+
userId,
|
|
1793
|
+
params,
|
|
1794
|
+
options?.modifyToolSchema
|
|
1795
|
+
);
|
|
1796
|
+
return tools?.map((tool) => this.wrapTool(tool)) ?? [];
|
|
1797
|
+
}
|
|
1798
|
+
async getToolBySlug(userId, slug, modifiers) {
|
|
1799
|
+
const tool = await this.getComposio().tools.getComposioToolBySlug(
|
|
1800
|
+
userId,
|
|
1801
|
+
slug,
|
|
1802
|
+
modifiers?.modifyToolSchema
|
|
1803
|
+
);
|
|
1804
|
+
return this.wrapTool(tool);
|
|
1805
|
+
}
|
|
1806
|
+
async test() {
|
|
1807
|
+
}
|
|
1808
|
+
};
|
|
1809
|
+
|
|
1810
|
+
// src/telemetry/TelemetryTransport.ts
|
|
1811
|
+
var BaseTelemetryTransport = class {
|
|
1812
|
+
};
|
|
1813
|
+
|
|
1061
1814
|
// src/utils/jsonSchema.ts
|
|
1062
|
-
import { z } from "zod";
|
|
1815
|
+
import { z as z3 } from "zod";
|
|
1063
1816
|
function jsonSchemaPropertiesToTSTypes(value) {
|
|
1064
1817
|
if (!value.type) {
|
|
1065
|
-
return
|
|
1818
|
+
return z3.object({});
|
|
1066
1819
|
}
|
|
1067
1820
|
let zodType;
|
|
1068
1821
|
switch (value.type) {
|
|
1069
1822
|
case "string":
|
|
1070
|
-
zodType =
|
|
1823
|
+
zodType = z3.string().describe(
|
|
1071
1824
|
(value.description || "") + (value.examples ? `
|
|
1072
1825
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1073
1826
|
);
|
|
1074
1827
|
break;
|
|
1075
1828
|
case "number":
|
|
1076
|
-
zodType =
|
|
1829
|
+
zodType = z3.number().describe(
|
|
1077
1830
|
(value.description || "") + (value.examples ? `
|
|
1078
1831
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1079
1832
|
);
|
|
1080
1833
|
break;
|
|
1081
1834
|
case "integer":
|
|
1082
|
-
zodType =
|
|
1835
|
+
zodType = z3.number().int().describe(
|
|
1083
1836
|
(value.description || "") + (value.examples ? `
|
|
1084
1837
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1085
1838
|
);
|
|
1086
1839
|
break;
|
|
1087
1840
|
case "boolean":
|
|
1088
|
-
zodType =
|
|
1841
|
+
zodType = z3.boolean().describe(
|
|
1089
1842
|
(value.description || "") + (value.examples ? `
|
|
1090
1843
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1091
1844
|
);
|
|
1092
1845
|
break;
|
|
1093
1846
|
case "array":
|
|
1094
|
-
zodType =
|
|
1847
|
+
zodType = z3.array(
|
|
1095
1848
|
jsonSchemaPropertiesToTSTypes(
|
|
1096
1849
|
value.items
|
|
1097
1850
|
)
|
|
@@ -1107,7 +1860,7 @@ Examples: ${value.examples.join(", ")}` : "")
|
|
|
1107
1860
|
);
|
|
1108
1861
|
break;
|
|
1109
1862
|
case "null":
|
|
1110
|
-
zodType =
|
|
1863
|
+
zodType = z3.null().describe(value.description || "");
|
|
1111
1864
|
break;
|
|
1112
1865
|
default:
|
|
1113
1866
|
throw new Error(`Unsupported JSON schema type: ${value.type}`);
|
|
@@ -1118,7 +1871,7 @@ function jsonSchemaToModel(jsonSchema) {
|
|
|
1118
1871
|
const properties = jsonSchema.properties;
|
|
1119
1872
|
const requiredFields = jsonSchema.required || [];
|
|
1120
1873
|
if (!properties) {
|
|
1121
|
-
return
|
|
1874
|
+
return z3.object({});
|
|
1122
1875
|
}
|
|
1123
1876
|
const zodSchema = {};
|
|
1124
1877
|
for (const [key, _] of Object.entries(properties)) {
|
|
@@ -1130,7 +1883,7 @@ function jsonSchemaToModel(jsonSchema) {
|
|
|
1130
1883
|
schema
|
|
1131
1884
|
)
|
|
1132
1885
|
);
|
|
1133
|
-
zodType =
|
|
1886
|
+
zodType = z3.union(anyOfTypes).describe(
|
|
1134
1887
|
(value.description || "") + (value.examples ? `
|
|
1135
1888
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1136
1889
|
);
|
|
@@ -1140,7 +1893,7 @@ Examples: ${value.examples.join(", ")}` : "")
|
|
|
1140
1893
|
schema
|
|
1141
1894
|
)
|
|
1142
1895
|
);
|
|
1143
|
-
zodType =
|
|
1896
|
+
zodType = z3.intersection(
|
|
1144
1897
|
allOfTypes[0],
|
|
1145
1898
|
allOfTypes.slice(1).reduce((acc, schema) => acc.and(schema), allOfTypes[0])
|
|
1146
1899
|
).describe(
|
|
@@ -1164,206 +1917,15 @@ Examples: ${value.examples.join(", ")}` : "")
|
|
|
1164
1917
|
zodSchema[key] = zodType.optional();
|
|
1165
1918
|
}
|
|
1166
1919
|
}
|
|
1167
|
-
return
|
|
1920
|
+
return z3.object(zodSchema);
|
|
1168
1921
|
}
|
|
1169
|
-
|
|
1170
|
-
// src/telemetry/TelemetryTransport.ts
|
|
1171
|
-
var BaseTelemetryTransport = class {
|
|
1172
|
-
};
|
|
1173
|
-
|
|
1174
|
-
// src/toolset/OpenAIToolset.ts
|
|
1175
|
-
var OpenAIToolset = class extends BaseComposioToolset {
|
|
1176
|
-
static FRAMEWORK_NAME = "openai";
|
|
1177
|
-
FILE_NAME = "toolsets/openai/src/index.ts";
|
|
1178
|
-
/**
|
|
1179
|
-
* Absract method to wrap a tool in the toolset.
|
|
1180
|
-
* This method is implemented by the toolset.
|
|
1181
|
-
* @param tool - The tool to wrap.
|
|
1182
|
-
* @returns The wrapped tool.
|
|
1183
|
-
*/
|
|
1184
|
-
_wrapTool = (tool) => {
|
|
1185
|
-
const formattedSchema = {
|
|
1186
|
-
name: tool.slug,
|
|
1187
|
-
description: tool.description,
|
|
1188
|
-
parameters: tool.input_parameters
|
|
1189
|
-
};
|
|
1190
|
-
return {
|
|
1191
|
-
type: "function",
|
|
1192
|
-
function: formattedSchema
|
|
1193
|
-
};
|
|
1194
|
-
};
|
|
1195
|
-
/**
|
|
1196
|
-
* Get all the tools from the Composio in OpenAI format.
|
|
1197
|
-
* @param params - The parameters for the tool list.
|
|
1198
|
-
* @returns The tools.
|
|
1199
|
-
*/
|
|
1200
|
-
async getTools(params) {
|
|
1201
|
-
const tools = await this.client?.tools.list(params);
|
|
1202
|
-
return tools?.items.map((tool) => this._wrapTool(tool)) ?? [];
|
|
1203
|
-
}
|
|
1204
|
-
/**
|
|
1205
|
-
* Execute a tool call.
|
|
1206
|
-
* @param {OpenAI.ChatCompletionMessageToolCall} tool - The tool to execute.
|
|
1207
|
-
* @param {string} userId - The user id.
|
|
1208
|
-
* @returns {Promise<string>} The result of the tool call.
|
|
1209
|
-
*/
|
|
1210
|
-
async executeToolCall(tool, userId) {
|
|
1211
|
-
if (!this.client) {
|
|
1212
|
-
throw new Error("Client not set");
|
|
1213
|
-
}
|
|
1214
|
-
const toolSchema = await this.client.tools.get(tool.function.name);
|
|
1215
|
-
const appName = toolSchema?.toolkit?.name.toLowerCase();
|
|
1216
|
-
const connectedAccountId = this.client.getConnectedAccountId(appName);
|
|
1217
|
-
const payload = {
|
|
1218
|
-
entity_id: userId ?? this.client.userId,
|
|
1219
|
-
connected_account_id: connectedAccountId,
|
|
1220
|
-
arguments: JSON.parse(tool.function.arguments)
|
|
1221
|
-
};
|
|
1222
|
-
const results = await this.client.tools.execute(toolSchema.slug, payload);
|
|
1223
|
-
return JSON.stringify(results);
|
|
1224
|
-
}
|
|
1225
|
-
/**
|
|
1226
|
-
* Handle the tool call from the assistant.
|
|
1227
|
-
*
|
|
1228
|
-
* @param {OpenAI.ChatCompletion} chatCompletion - The chat completion object.
|
|
1229
|
-
* @param {string} userId - The user id.
|
|
1230
|
-
* @returns {Promise<string[]>} The results of the tool call.
|
|
1231
|
-
*/
|
|
1232
|
-
async handleToolCall(chatCompletion, userId) {
|
|
1233
|
-
const outputs = [];
|
|
1234
|
-
for (const message of chatCompletion.choices) {
|
|
1235
|
-
if (message.message.tool_calls) {
|
|
1236
|
-
outputs.push(
|
|
1237
|
-
await this.executeToolCall(message.message.tool_calls[0], userId)
|
|
1238
|
-
);
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1241
|
-
return outputs;
|
|
1242
|
-
}
|
|
1243
|
-
/**
|
|
1244
|
-
* Handles all the tool calls from the assistant.
|
|
1245
|
-
*
|
|
1246
|
-
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1247
|
-
* @param {string} userId - The user id.
|
|
1248
|
-
* @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.
|
|
1249
|
-
*/
|
|
1250
|
-
async handleAssistantMessage(run, userId) {
|
|
1251
|
-
const tool_calls = run.required_action?.submit_tool_outputs?.tool_calls || [];
|
|
1252
|
-
const tool_outputs = await Promise.all(
|
|
1253
|
-
tool_calls.map(async (tool_call) => {
|
|
1254
|
-
logger_default.debug(`Executing tool call: ${tool_call.id}`);
|
|
1255
|
-
const tool_response = await this.executeToolCall(
|
|
1256
|
-
tool_call,
|
|
1257
|
-
userId
|
|
1258
|
-
);
|
|
1259
|
-
logger_default.debug(`Tool call ${tool_call.id} executed with response: ${tool_response}`);
|
|
1260
|
-
return {
|
|
1261
|
-
tool_call_id: tool_call.id,
|
|
1262
|
-
output: JSON.stringify(tool_response)
|
|
1263
|
-
};
|
|
1264
|
-
})
|
|
1265
|
-
);
|
|
1266
|
-
return tool_outputs;
|
|
1267
|
-
}
|
|
1268
|
-
/**
|
|
1269
|
-
* Waits for the assistant stream and handles the tool calls.
|
|
1270
|
-
*
|
|
1271
|
-
* @param {OpenAI} client - The OpenAI client.
|
|
1272
|
-
* @param {Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>} runStream - The run stream.
|
|
1273
|
-
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1274
|
-
* @param {string} userId - The user id.
|
|
1275
|
-
* @returns {AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>} The run stream.
|
|
1276
|
-
*/
|
|
1277
|
-
async *waitAndHandleAssistantStreamToolCalls(client, runStream, thread, userId) {
|
|
1278
|
-
const defaultUserId = this.client?.userId;
|
|
1279
|
-
let runId = null;
|
|
1280
|
-
for await (const event of runStream) {
|
|
1281
|
-
yield event;
|
|
1282
|
-
if (event.event === "thread.run.created") {
|
|
1283
|
-
const { id } = event.data;
|
|
1284
|
-
runId = id;
|
|
1285
|
-
}
|
|
1286
|
-
if (!runId) {
|
|
1287
|
-
continue;
|
|
1288
|
-
}
|
|
1289
|
-
if (event.event === "thread.run.requires_action") {
|
|
1290
|
-
const toolOutputs = await this.handleAssistantMessage(
|
|
1291
|
-
event.data,
|
|
1292
|
-
userId ?? defaultUserId
|
|
1293
|
-
);
|
|
1294
|
-
await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
|
|
1295
|
-
tool_outputs: toolOutputs
|
|
1296
|
-
});
|
|
1297
|
-
}
|
|
1298
|
-
if ([
|
|
1299
|
-
"thread.run.completed",
|
|
1300
|
-
"thread.run.failed",
|
|
1301
|
-
"thread.run.cancelled",
|
|
1302
|
-
"thread.run.expired"
|
|
1303
|
-
].includes(event.event)) {
|
|
1304
|
-
break;
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
if (!runId) {
|
|
1308
|
-
throw new Error("No run ID found");
|
|
1309
|
-
}
|
|
1310
|
-
let finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1311
|
-
while (["queued", "in_progress", "requires_action"].includes(finalRun.status)) {
|
|
1312
|
-
if (finalRun.status === "requires_action") {
|
|
1313
|
-
const toolOutputs = await this.handleAssistantMessage(
|
|
1314
|
-
finalRun,
|
|
1315
|
-
userId ?? defaultUserId
|
|
1316
|
-
);
|
|
1317
|
-
finalRun = await client.beta.threads.runs.submitToolOutputs(
|
|
1318
|
-
thread.id,
|
|
1319
|
-
runId,
|
|
1320
|
-
{
|
|
1321
|
-
tool_outputs: toolOutputs
|
|
1322
|
-
}
|
|
1323
|
-
);
|
|
1324
|
-
} else {
|
|
1325
|
-
finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1326
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
/**
|
|
1331
|
-
* Waits for the assistant tool calls and handles them.
|
|
1332
|
-
*
|
|
1333
|
-
* @param {OpenAI} client - The OpenAI client.
|
|
1334
|
-
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1335
|
-
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1336
|
-
* @param {string} userId - The user id.
|
|
1337
|
-
* @returns {Promise<OpenAI.Beta.Threads.Run>} The run object.
|
|
1338
|
-
*/
|
|
1339
|
-
async waitAndHandleAssistantToolCalls(client, run, thread, userId) {
|
|
1340
|
-
const defaultUserId = this.client?.userId;
|
|
1341
|
-
while (["queued", "in_progress", "requires_action"].includes(run.status)) {
|
|
1342
|
-
const tool_outputs = await this.handleAssistantMessage(
|
|
1343
|
-
run,
|
|
1344
|
-
userId ?? defaultUserId
|
|
1345
|
-
);
|
|
1346
|
-
if (run.status === "requires_action") {
|
|
1347
|
-
run = await client.beta.threads.runs.submitToolOutputs(
|
|
1348
|
-
thread.id,
|
|
1349
|
-
run.id,
|
|
1350
|
-
{
|
|
1351
|
-
tool_outputs
|
|
1352
|
-
}
|
|
1353
|
-
);
|
|
1354
|
-
} else {
|
|
1355
|
-
run = await client.beta.threads.runs.retrieve(thread.id, run.id);
|
|
1356
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1357
|
-
}
|
|
1358
|
-
}
|
|
1359
|
-
return run;
|
|
1360
|
-
}
|
|
1361
|
-
};
|
|
1362
1922
|
export {
|
|
1363
|
-
|
|
1923
|
+
BaseAgenticToolset,
|
|
1924
|
+
BaseNonAgenticToolset,
|
|
1364
1925
|
BaseTelemetryTransport,
|
|
1365
1926
|
Composio,
|
|
1366
1927
|
ComposioToolset,
|
|
1367
1928
|
OpenAIToolset,
|
|
1929
|
+
ToolListParamsSchema,
|
|
1368
1930
|
jsonSchemaToModel
|
|
1369
1931
|
};
|