@composio/core 0.1.0-rc.5 → 0.1.3-alpha.0
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 +1174 -464
- package/dist/index.d.cts +1304 -150
- package/dist/index.d.ts +1304 -150
- package/dist/index.js +1171 -463
- package/package.json +9 -4
package/dist/index.cjs
CHANGED
|
@@ -30,11 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
33
|
+
BaseAgenticToolset: () => BaseAgenticToolset,
|
|
34
|
+
BaseNonAgenticToolset: () => BaseNonAgenticToolset,
|
|
34
35
|
BaseTelemetryTransport: () => BaseTelemetryTransport,
|
|
35
36
|
Composio: () => Composio,
|
|
36
37
|
ComposioToolset: () => ComposioToolset,
|
|
37
38
|
OpenAIToolset: () => OpenAIToolset,
|
|
39
|
+
ToolListParamsSchema: () => ToolListParamsSchema,
|
|
38
40
|
jsonSchemaToModel: () => jsonSchemaToModel
|
|
39
41
|
});
|
|
40
42
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -42,105 +44,197 @@ module.exports = __toCommonJS(index_exports);
|
|
|
42
44
|
// src/composio.ts
|
|
43
45
|
var import_client = __toESM(require("@composio/client"), 1);
|
|
44
46
|
|
|
45
|
-
// src/
|
|
46
|
-
var
|
|
47
|
-
|
|
47
|
+
// src/types/tool.types.ts
|
|
48
|
+
var import_zod = require("zod");
|
|
49
|
+
var ToolkitSchema = import_zod.z.object({
|
|
50
|
+
slug: import_zod.z.string().describe("The slug of the toolkit"),
|
|
51
|
+
name: import_zod.z.string().describe("The name of the toolkit"),
|
|
52
|
+
logo: import_zod.z.string().describe("The logo of the toolkit").optional()
|
|
53
|
+
});
|
|
54
|
+
var ToolSchema = import_zod.z.object({
|
|
55
|
+
slug: import_zod.z.string().describe('The slug of the tool. eg. "GOOGLE_SEARCH"'),
|
|
56
|
+
name: import_zod.z.string().describe(`The name of the tool. eg. "Google Search"`),
|
|
57
|
+
description: import_zod.z.string().optional().describe("The description of the tool"),
|
|
58
|
+
inputParameters: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional().describe("The input parameters of the tool"),
|
|
59
|
+
outputParameters: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional().describe("The output parameters of the tool"),
|
|
60
|
+
tags: import_zod.z.optional(import_zod.z.array(import_zod.z.string())).describe("The tags of the tool. eg: Important").default([]),
|
|
61
|
+
toolkit: import_zod.z.optional(ToolkitSchema).describe("The toolkit of the tool"),
|
|
62
|
+
version: import_zod.z.optional(import_zod.z.string()).describe('The version of the tool, e.g. "1.0.0"')
|
|
63
|
+
});
|
|
64
|
+
var ToolListResponseSchema = import_zod.z.object({
|
|
65
|
+
items: import_zod.z.array(ToolSchema),
|
|
66
|
+
nextCursor: import_zod.z.string().nullable().optional(),
|
|
67
|
+
totalPages: import_zod.z.number()
|
|
68
|
+
});
|
|
69
|
+
var ToolListParamsSchema = import_zod.z.object({
|
|
70
|
+
cursor: import_zod.z.string().optional(),
|
|
71
|
+
important: import_zod.z.string().optional(),
|
|
72
|
+
limit: import_zod.z.string().optional(),
|
|
73
|
+
search: import_zod.z.string().optional(),
|
|
74
|
+
toolkits: import_zod.z.string().optional(),
|
|
75
|
+
tools: import_zod.z.array(import_zod.z.string()).optional()
|
|
76
|
+
});
|
|
77
|
+
var CustomAuthParamsSchema = import_zod.z.object({
|
|
78
|
+
baseURL: import_zod.z.string().optional(),
|
|
79
|
+
body: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
|
|
80
|
+
parameters: import_zod.z.array(
|
|
81
|
+
import_zod.z.object({
|
|
82
|
+
in: import_zod.z.enum(["query", "header"]),
|
|
83
|
+
name: import_zod.z.string(),
|
|
84
|
+
value: import_zod.z.union([import_zod.z.string(), import_zod.z.number()])
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
});
|
|
88
|
+
var ToolExecuteParamsSchema = import_zod.z.object({
|
|
89
|
+
allowTracing: import_zod.z.boolean().optional(),
|
|
90
|
+
connectedAccountId: import_zod.z.string().optional(),
|
|
91
|
+
customAuthParams: CustomAuthParamsSchema.optional(),
|
|
92
|
+
arguments: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
|
|
93
|
+
userId: import_zod.z.string(),
|
|
94
|
+
version: import_zod.z.string().optional(),
|
|
95
|
+
text: import_zod.z.string().optional()
|
|
96
|
+
});
|
|
97
|
+
var ToolExecuteResponseSchema = import_zod.z.object({
|
|
98
|
+
data: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()),
|
|
99
|
+
error: import_zod.z.string().nullable(),
|
|
100
|
+
successful: import_zod.z.boolean(),
|
|
101
|
+
logId: import_zod.z.string().optional(),
|
|
102
|
+
sessionInfo: import_zod.z.unknown().optional()
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// src/models/CustomTools.ts
|
|
106
|
+
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"), 1);
|
|
107
|
+
var CustomTools = class {
|
|
48
108
|
client;
|
|
109
|
+
customToolsRegistry = /* @__PURE__ */ new Map();
|
|
49
110
|
constructor(client) {
|
|
50
111
|
this.client = client;
|
|
51
112
|
}
|
|
52
113
|
/**
|
|
53
|
-
*
|
|
54
|
-
* This
|
|
55
|
-
* @
|
|
114
|
+
* Create a custom tool and registers it in the registry.
|
|
115
|
+
* This is just an in memory registry and is not persisted.
|
|
116
|
+
* @param {CustomToolOptions} toolOptions CustomToolOptions
|
|
117
|
+
* @returns {Tool} The tool created
|
|
56
118
|
*/
|
|
57
|
-
async
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
60
|
-
|
|
61
|
-
items: [],
|
|
62
|
-
next_cursor: null,
|
|
63
|
-
total_pages: 0
|
|
64
|
-
};
|
|
119
|
+
async createTool(toolOptions) {
|
|
120
|
+
const { slug, execute, inputParams, name, description } = toolOptions;
|
|
121
|
+
if (!slug || !execute || !inputParams || !name) {
|
|
122
|
+
throw new Error("Invalid tool options");
|
|
65
123
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
124
|
+
const paramsSchema = await (0, import_zod_to_json_schema.default)(inputParams, {
|
|
125
|
+
name: "input"
|
|
126
|
+
});
|
|
127
|
+
const paramsSchemaJson = paramsSchema.definitions.input;
|
|
128
|
+
const toolSchema = {
|
|
129
|
+
name,
|
|
130
|
+
slug,
|
|
131
|
+
description,
|
|
132
|
+
inputParameters: {
|
|
133
|
+
title: name,
|
|
134
|
+
type: "object",
|
|
135
|
+
description,
|
|
136
|
+
properties: paramsSchemaJson.properties,
|
|
137
|
+
required: paramsSchemaJson.required
|
|
138
|
+
},
|
|
139
|
+
// the output parameters are not used yet
|
|
140
|
+
outputParameters: {
|
|
141
|
+
type: "object",
|
|
142
|
+
title: `Response for ${name}`,
|
|
143
|
+
properties: []
|
|
144
|
+
},
|
|
145
|
+
tags: [],
|
|
146
|
+
toolkit: { name: "custom", slug: "custom" }
|
|
147
|
+
};
|
|
148
|
+
this.customToolsRegistry.set(slug.toLowerCase(), {
|
|
149
|
+
options: toolOptions,
|
|
150
|
+
schema: toolSchema
|
|
151
|
+
});
|
|
152
|
+
return toolSchema;
|
|
87
153
|
}
|
|
88
154
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* No filtering is done on the tools, the list is cached in the backend, no further optimization is required.
|
|
93
|
-
* @returns {string} - The list of all available tools as a string
|
|
155
|
+
* Get all the custom tools from the registry.
|
|
156
|
+
* @param {string[]} param0.toolSlugs The slugs of the tools to get
|
|
157
|
+
* @returns {ToolList} The list of tools
|
|
94
158
|
*/
|
|
95
|
-
async
|
|
96
|
-
|
|
159
|
+
async getCustomTools({ toolSlugs }) {
|
|
160
|
+
const tools = [];
|
|
161
|
+
if (toolSlugs) {
|
|
162
|
+
for (const slug of toolSlugs) {
|
|
163
|
+
const tool = this.customToolsRegistry.get(slug.toLowerCase());
|
|
164
|
+
if (tool) {
|
|
165
|
+
tools.push(tool.schema);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
return Array.from(this.customToolsRegistry.values()).map((tool) => tool.schema);
|
|
170
|
+
}
|
|
171
|
+
return tools;
|
|
97
172
|
}
|
|
98
173
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* @param {string} slug - The ID of the tool to find input for
|
|
104
|
-
* @param {ToolGetInputParams} body - The parameters to be passed to the tool
|
|
105
|
-
* @returns
|
|
174
|
+
* Get a custom tool by slug from the registry.
|
|
175
|
+
* @param {string} slug The slug of the tool to get
|
|
176
|
+
* @returns {Tool} The tool
|
|
106
177
|
*/
|
|
107
|
-
async
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
var Toolkits = class {
|
|
114
|
-
FILE_NAME = "core/models/Toolkits.ts";
|
|
115
|
-
client;
|
|
116
|
-
constructor(client) {
|
|
117
|
-
this.client = client;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* List all toolkits available in the Composio SDK.
|
|
121
|
-
* This method fetches the toolkits from the Composio API.
|
|
122
|
-
* @returns {Promise<Toolkit[]>} List of toolkits
|
|
123
|
-
*/
|
|
124
|
-
async list(query, options) {
|
|
125
|
-
return this.client.toolkits.list(query, options);
|
|
178
|
+
async getCustomToolBySlug(slug) {
|
|
179
|
+
const tool = this.customToolsRegistry.get(slug.toLowerCase());
|
|
180
|
+
if (!tool) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
return tool.schema;
|
|
126
184
|
}
|
|
127
185
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* @param
|
|
131
|
-
* @
|
|
132
|
-
* @returns {Promise<ToolkitRetrieveResponse>} The toolkit object
|
|
186
|
+
* Get the connected account for the user and toolkit.
|
|
187
|
+
* @param {string} toolkitSlug The slug of the toolkit
|
|
188
|
+
* @param {ExecuteMetadata} metadata The metadata of the execution
|
|
189
|
+
* @returns {ConnectedAccount} The connected account
|
|
133
190
|
*/
|
|
134
|
-
async
|
|
135
|
-
|
|
191
|
+
async getConnectedAccount(toolkitSlug, metadata) {
|
|
192
|
+
const connectedAccounts = await this.client.connectedAccounts.list({
|
|
193
|
+
toolkit_slug: toolkitSlug,
|
|
194
|
+
user_id: metadata.userId
|
|
195
|
+
});
|
|
196
|
+
if (!connectedAccounts.items.length) {
|
|
197
|
+
throw new Error(`No connected accounts found for toolkit ${toolkitSlug}`);
|
|
198
|
+
}
|
|
199
|
+
return metadata.connectedAccountId ? connectedAccounts.items.find((item) => item.id === metadata.connectedAccountId) : connectedAccounts.items[0];
|
|
136
200
|
}
|
|
137
201
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* @
|
|
202
|
+
* Execute a custom tool
|
|
203
|
+
* @param {slug} slug The slug of the tool to execute
|
|
204
|
+
* @param {Record<string, unknown>} inputParams The input parameters for the tool
|
|
205
|
+
* @param {ExecuteMetadata} metadata The metadata of the execution
|
|
206
|
+
* @returns {Promise<ToolExecuteResponse>} The response from the tool
|
|
141
207
|
*/
|
|
142
|
-
async
|
|
143
|
-
|
|
208
|
+
async executeCustomTool(slug, inputParams, metadata) {
|
|
209
|
+
const tool = this.customToolsRegistry.get(slug.toLowerCase());
|
|
210
|
+
if (!tool) {
|
|
211
|
+
throw new Error(`Tool with slug ${slug} not found`);
|
|
212
|
+
}
|
|
213
|
+
let authCredentials = {};
|
|
214
|
+
const { toolkitSlug, execute } = tool.options;
|
|
215
|
+
if (toolkitSlug) {
|
|
216
|
+
const connectedAccount = await this.getConnectedAccount(toolkitSlug, metadata);
|
|
217
|
+
if (!connectedAccount) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`Connected account not found for toolkit ${toolkitSlug} for user ${metadata.userId}`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
authCredentials = connectedAccount.data?.connectionParams;
|
|
223
|
+
}
|
|
224
|
+
if (typeof execute !== "function") {
|
|
225
|
+
throw new Error("Invalid execute function");
|
|
226
|
+
}
|
|
227
|
+
const executeToolRequest = async (data) => {
|
|
228
|
+
return this.client.tools.proxy({
|
|
229
|
+
...data,
|
|
230
|
+
connected_account_id: metadata.connectedAccountId
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
return execute(
|
|
234
|
+
inputParams,
|
|
235
|
+
authCredentials,
|
|
236
|
+
executeToolRequest
|
|
237
|
+
);
|
|
144
238
|
}
|
|
145
239
|
};
|
|
146
240
|
|
|
@@ -215,8 +309,468 @@ var Logger = class {
|
|
|
215
309
|
}
|
|
216
310
|
}
|
|
217
311
|
};
|
|
218
|
-
var logger = new Logger();
|
|
219
|
-
var logger_default = logger;
|
|
312
|
+
var logger = new Logger();
|
|
313
|
+
var logger_default = logger;
|
|
314
|
+
|
|
315
|
+
// src/models/Tools.ts
|
|
316
|
+
var Tools = class {
|
|
317
|
+
client;
|
|
318
|
+
customTools;
|
|
319
|
+
toolset;
|
|
320
|
+
constructor(client, toolset) {
|
|
321
|
+
this.client = client;
|
|
322
|
+
this.customTools = new CustomTools(client);
|
|
323
|
+
this.toolset = toolset;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Transform the tool cases
|
|
327
|
+
* @param {ToolRetrieveResponse | ComposioToolListResponse['items'][0]} tool - The tool to transform
|
|
328
|
+
* @returns {Tool} The transformed tool
|
|
329
|
+
*/
|
|
330
|
+
transformToolCases(tool) {
|
|
331
|
+
return ToolSchema.parse({
|
|
332
|
+
...tool,
|
|
333
|
+
inputParameters: tool.input_parameters,
|
|
334
|
+
outputParameters: tool.output_parameters
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Transform the tool execute response to camelcase
|
|
339
|
+
* @param {ComposioToolExecuteResponse} response - The response to transform
|
|
340
|
+
* @returns {ToolExecuteResponse} The transformed response
|
|
341
|
+
*/
|
|
342
|
+
transformToolExecuteResponse(response) {
|
|
343
|
+
return ToolExecuteResponseSchema.parse({
|
|
344
|
+
data: response.data,
|
|
345
|
+
error: response.error,
|
|
346
|
+
successful: response.successful,
|
|
347
|
+
logId: response.log_id,
|
|
348
|
+
sessionInfo: response.session_info
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Check if the connected account exists for the given tools.
|
|
353
|
+
* @param {string} userId - The user id.
|
|
354
|
+
* @param {ToolList} tools - The tools to check.
|
|
355
|
+
* @returns {Promise<boolean>} True if the connected account exists for the given tools, false otherwise.
|
|
356
|
+
*/
|
|
357
|
+
async checkIfConnectedAccountExistsForTools(userId, tools) {
|
|
358
|
+
if (!tools.length) {
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
const connectedAccounts = await this.client.connectedAccounts.list({
|
|
362
|
+
user_id: userId
|
|
363
|
+
});
|
|
364
|
+
if (connectedAccounts.items.length === 0) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
const connectedToolkitSlugs = connectedAccounts.items.reduce(
|
|
368
|
+
(acc, account) => {
|
|
369
|
+
if (account.toolkit.slug) {
|
|
370
|
+
acc[account.toolkit.slug] = true;
|
|
371
|
+
}
|
|
372
|
+
return acc;
|
|
373
|
+
},
|
|
374
|
+
{}
|
|
375
|
+
);
|
|
376
|
+
const toolSlugs = tools.reduce(
|
|
377
|
+
(acc, tool) => {
|
|
378
|
+
acc[tool.slug] = true;
|
|
379
|
+
return acc;
|
|
380
|
+
},
|
|
381
|
+
{}
|
|
382
|
+
);
|
|
383
|
+
for (const tool of Object.keys(toolSlugs)) {
|
|
384
|
+
if (!connectedToolkitSlugs[tool]) {
|
|
385
|
+
logger_default.warn(`Tool ${tool} requires a connected account but no connected account was found`);
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Get the connected account id for a given tool
|
|
393
|
+
* @param {string} userId - The user id
|
|
394
|
+
* @param {string} toolSlug - The tool slug
|
|
395
|
+
* @returns {Promise<string | null>} The connected account id or null if the toolkit is a no auth app
|
|
396
|
+
*/
|
|
397
|
+
async getConnectedAccountIdForTool(userId, toolSlug) {
|
|
398
|
+
const tool = await this.getComposioToolBySlug(userId, toolSlug);
|
|
399
|
+
if (!tool.toolkit) {
|
|
400
|
+
throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
|
|
401
|
+
}
|
|
402
|
+
const toolkit = await this.client.toolkits.retrieve(tool.toolkit.slug);
|
|
403
|
+
if (!toolkit) {
|
|
404
|
+
throw new Error(`Unable to find toolkit for tool ${toolSlug}`);
|
|
405
|
+
}
|
|
406
|
+
const isNoAuthApp = toolkit.auth_config_details?.some(
|
|
407
|
+
(authConfigDetails) => authConfigDetails.mode === "NO_AUTH"
|
|
408
|
+
);
|
|
409
|
+
if (!isNoAuthApp) {
|
|
410
|
+
const connectedAccounts = await this.client.connectedAccounts.list({
|
|
411
|
+
user_id: userId,
|
|
412
|
+
toolkit_slug: tool.toolkit.slug
|
|
413
|
+
});
|
|
414
|
+
if (connectedAccounts.items.length === 0) {
|
|
415
|
+
throw new Error("No connected accounts found");
|
|
416
|
+
}
|
|
417
|
+
logger_default.warn(
|
|
418
|
+
`Using the first connected account for tool ${toolSlug}. To change this behaviour please explicitly pass a connectedAccountId for the tool`
|
|
419
|
+
);
|
|
420
|
+
return connectedAccounts.items[0].id;
|
|
421
|
+
}
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Lists all tools available in the Composio SDK as well as custom tools.
|
|
426
|
+
* This method fetches the tools from the Composio API and wraps them using the toolset.
|
|
427
|
+
* @returns {ToolList} List of tools
|
|
428
|
+
*/
|
|
429
|
+
async getComposioTools(userId, query = {}, modifier) {
|
|
430
|
+
const queryParams = ToolListParamsSchema.safeParse(query);
|
|
431
|
+
if (queryParams.error) {
|
|
432
|
+
throw new Error(JSON.stringify(queryParams.error.flatten()));
|
|
433
|
+
}
|
|
434
|
+
const tools = await this.client.tools.list({
|
|
435
|
+
cursor: queryParams.data.cursor,
|
|
436
|
+
important: queryParams.data.important,
|
|
437
|
+
limit: queryParams.data.limit,
|
|
438
|
+
search: queryParams.data.search,
|
|
439
|
+
toolkit_slug: queryParams.data.toolkits,
|
|
440
|
+
tool_slugs: queryParams.data.tools?.join(",")
|
|
441
|
+
});
|
|
442
|
+
if (!tools) {
|
|
443
|
+
return [];
|
|
444
|
+
}
|
|
445
|
+
const caseTransformedTools = tools.items.map((tool) => this.transformToolCases(tool));
|
|
446
|
+
const customTools = await this.customTools.getCustomTools({
|
|
447
|
+
toolSlugs: queryParams.data.tools
|
|
448
|
+
});
|
|
449
|
+
let modifiedTools = [...caseTransformedTools, ...customTools];
|
|
450
|
+
if (modifier) {
|
|
451
|
+
if (typeof modifier === "function") {
|
|
452
|
+
modifiedTools = modifiedTools.map((tool) => {
|
|
453
|
+
return modifier(tool.slug, tool);
|
|
454
|
+
});
|
|
455
|
+
} else {
|
|
456
|
+
throw new Error("Invalid schema modifier. Not a function.");
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return modifiedTools;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Retrieves a tool by its Slug.
|
|
463
|
+
* @param slug The ID of the tool to be retrieved
|
|
464
|
+
* @returns {Promise<Tool>} The tool
|
|
465
|
+
*/
|
|
466
|
+
async getComposioToolBySlug(userId, slug, modifier) {
|
|
467
|
+
const customTool = await this.customTools.getCustomToolBySlug(slug);
|
|
468
|
+
if (customTool) {
|
|
469
|
+
return customTool;
|
|
470
|
+
}
|
|
471
|
+
const tool = await this.client.tools.retrieve(slug);
|
|
472
|
+
if (!tool) {
|
|
473
|
+
throw new Error(`Tool with slug ${slug} not found`);
|
|
474
|
+
}
|
|
475
|
+
let modifiedToool = this.transformToolCases(tool);
|
|
476
|
+
if (modifier) {
|
|
477
|
+
if (typeof modifier === "function") {
|
|
478
|
+
modifiedToool = modifier(slug, modifiedToool);
|
|
479
|
+
} else {
|
|
480
|
+
throw new Error("Invalid schema modifier. Not a function.");
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return modifiedToool;
|
|
484
|
+
}
|
|
485
|
+
async get(userId, arg2, options) {
|
|
486
|
+
const executeToolFn = this.createExecuteToolFn(userId, options);
|
|
487
|
+
if (typeof arg2 === "string") {
|
|
488
|
+
const tool = await this.getComposioToolBySlug(userId, arg2, options?.modifyToolSchema);
|
|
489
|
+
return this.toolset.wrapTool(tool, executeToolFn);
|
|
490
|
+
} else {
|
|
491
|
+
const tools = await this.getComposioTools(userId, arg2, options?.modifyToolSchema);
|
|
492
|
+
return this.toolset.wrapTools(tools, executeToolFn);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Creates a function that executes a tool.
|
|
497
|
+
* This function is used by agentic the toolsets to execute the tool
|
|
498
|
+
* @param {string} userId - The user id
|
|
499
|
+
* @param {ExecuteToolModifiers} modifiers - The modifiers to be applied to the tool
|
|
500
|
+
* @returns {ExecuteToolFn} The execute tool function
|
|
501
|
+
*/
|
|
502
|
+
createExecuteToolFn(userId, modifiers) {
|
|
503
|
+
const executeToolFn = async (toolSlug, input) => {
|
|
504
|
+
return await this.execute(
|
|
505
|
+
toolSlug,
|
|
506
|
+
{
|
|
507
|
+
userId,
|
|
508
|
+
arguments: input
|
|
509
|
+
},
|
|
510
|
+
modifiers
|
|
511
|
+
);
|
|
512
|
+
};
|
|
513
|
+
return executeToolFn;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Exectes a given tool with the provided parameters.
|
|
517
|
+
*
|
|
518
|
+
* This method calls the Composio API to execute the tool and returns the response.
|
|
519
|
+
*
|
|
520
|
+
* @param {string} slug - The ID of the tool to be executed
|
|
521
|
+
* @param {ToolExecuteParams} body - The parameters to be passed to the tool
|
|
522
|
+
* @returns {Promise<ToolExecuteResponse>} - The response from the tool execution
|
|
523
|
+
*/
|
|
524
|
+
async execute(slug, body, modifiers) {
|
|
525
|
+
if (modifiers && modifiers.beforeToolExecute) {
|
|
526
|
+
if (typeof modifiers.beforeToolExecute === "function") {
|
|
527
|
+
body = modifiers.beforeToolExecute(slug, body);
|
|
528
|
+
} else {
|
|
529
|
+
throw new Error("Invalid beforeToolExecute modifier. Not a function.");
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
let result;
|
|
533
|
+
const customTool = await this.customTools.getCustomToolBySlug(slug);
|
|
534
|
+
if (customTool) {
|
|
535
|
+
result = await this.customTools.executeCustomTool(slug, body, {
|
|
536
|
+
userId: body.userId || "default",
|
|
537
|
+
connectedAccountId: body.connectedAccountId
|
|
538
|
+
});
|
|
539
|
+
} else {
|
|
540
|
+
let connectedAccountId = body.connectedAccountId;
|
|
541
|
+
if (!connectedAccountId) {
|
|
542
|
+
connectedAccountId = await this.getConnectedAccountIdForTool(body.userId, slug) || void 0;
|
|
543
|
+
}
|
|
544
|
+
result = await this.client.tools.execute(slug, {
|
|
545
|
+
allow_tracing: body.allowTracing,
|
|
546
|
+
connected_account_id: body.connectedAccountId,
|
|
547
|
+
custom_auth_params: body.customAuthParams,
|
|
548
|
+
arguments: body.arguments,
|
|
549
|
+
entity_id: body.userId,
|
|
550
|
+
version: body.version,
|
|
551
|
+
text: body.text
|
|
552
|
+
});
|
|
553
|
+
result = this.transformToolExecuteResponse(result);
|
|
554
|
+
}
|
|
555
|
+
if (modifiers && modifiers.afterToolExecute) {
|
|
556
|
+
if (typeof modifiers.afterToolExecute === "function") {
|
|
557
|
+
result = modifiers.afterToolExecute(slug, result);
|
|
558
|
+
} else {
|
|
559
|
+
throw new Error("Invalid afterToolExecute modifier. Not a function.");
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return result;
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Fetches the list of all available tools in the Composio SDK.
|
|
566
|
+
*
|
|
567
|
+
* This method is mostly used by the CLI to get the list of tools.
|
|
568
|
+
* No filtering is done on the tools, the list is cached in the backend, no further optimization is required.
|
|
569
|
+
* @returns {string} - The list of all available tools as a string
|
|
570
|
+
*/
|
|
571
|
+
async getToolsEnum() {
|
|
572
|
+
return this.client.tools.retrieveEnum();
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Fetches the input parameters for a given tool.
|
|
576
|
+
*
|
|
577
|
+
* This method is used to get the input parameters for a tool before executing it.
|
|
578
|
+
*
|
|
579
|
+
* @param {string} slug - The ID of the tool to find input for
|
|
580
|
+
* @param {ToolGetInputParams} body - The parameters to be passed to the tool
|
|
581
|
+
* @returns
|
|
582
|
+
*/
|
|
583
|
+
async getInput(slug, body) {
|
|
584
|
+
return this.client.tools.getInput(slug, body);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Proxies a custom request to a toolkit/integration
|
|
588
|
+
* @param body
|
|
589
|
+
* @returns
|
|
590
|
+
*/
|
|
591
|
+
async proxyExecute(body) {
|
|
592
|
+
return this.client.tools.proxy(body);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Execute a custom tool
|
|
596
|
+
* @param body
|
|
597
|
+
* @returns
|
|
598
|
+
*/
|
|
599
|
+
async createCustomTool(body) {
|
|
600
|
+
return this.customTools.createTool(body);
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
// src/types/toolkit.types.ts
|
|
605
|
+
var import_zod2 = require("zod");
|
|
606
|
+
var ToolkitMangedByEnumSchema = import_zod2.z.enum(["all", "composio_managed", "project_managed"]);
|
|
607
|
+
var ToolkitSortByEnumSchema = import_zod2.z.enum(["usage", "alphabetically"]);
|
|
608
|
+
var ToolkitsListParamsSchema = import_zod2.z.object({
|
|
609
|
+
category: import_zod2.z.string().optional(),
|
|
610
|
+
isLocal: import_zod2.z.boolean().optional(),
|
|
611
|
+
managedBy: ToolkitMangedByEnumSchema.optional(),
|
|
612
|
+
sortBy: ToolkitSortByEnumSchema.optional()
|
|
613
|
+
});
|
|
614
|
+
var ToolKitMetaSchema = import_zod2.z.object({
|
|
615
|
+
categories: import_zod2.z.array(
|
|
616
|
+
import_zod2.z.object({
|
|
617
|
+
id: import_zod2.z.string(),
|
|
618
|
+
name: import_zod2.z.string()
|
|
619
|
+
})
|
|
620
|
+
),
|
|
621
|
+
createdAt: import_zod2.z.string(),
|
|
622
|
+
description: import_zod2.z.string(),
|
|
623
|
+
logo: import_zod2.z.string(),
|
|
624
|
+
toolsCount: import_zod2.z.number(),
|
|
625
|
+
triggersCount: import_zod2.z.number(),
|
|
626
|
+
updatedAt: import_zod2.z.string()
|
|
627
|
+
});
|
|
628
|
+
var ToolKitItemSchema = import_zod2.z.object({
|
|
629
|
+
name: import_zod2.z.string(),
|
|
630
|
+
slug: import_zod2.z.string(),
|
|
631
|
+
meta: ToolKitMetaSchema,
|
|
632
|
+
isLocalToolkit: import_zod2.z.boolean(),
|
|
633
|
+
authSchemes: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
634
|
+
composioManagedAuthSchemes: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
635
|
+
noAuth: import_zod2.z.boolean().optional()
|
|
636
|
+
});
|
|
637
|
+
var ToolKitListResponseSchema = import_zod2.z.object({
|
|
638
|
+
items: import_zod2.z.array(ToolKitItemSchema),
|
|
639
|
+
nextCursor: import_zod2.z.string().nullable(),
|
|
640
|
+
totalPages: import_zod2.z.number()
|
|
641
|
+
});
|
|
642
|
+
var ToolkitAuthFieldSchema = import_zod2.z.object({
|
|
643
|
+
name: import_zod2.z.string(),
|
|
644
|
+
displayName: import_zod2.z.string(),
|
|
645
|
+
type: import_zod2.z.string(),
|
|
646
|
+
default: import_zod2.z.string().nullable().optional()
|
|
647
|
+
});
|
|
648
|
+
var ToolkitAuthConfigDetailsSchema = import_zod2.z.object({
|
|
649
|
+
name: import_zod2.z.string(),
|
|
650
|
+
mode: import_zod2.z.string(),
|
|
651
|
+
fields: import_zod2.z.array(
|
|
652
|
+
import_zod2.z.object({
|
|
653
|
+
authConfigCreation: import_zod2.z.object({
|
|
654
|
+
optional: import_zod2.z.array(ToolkitAuthFieldSchema),
|
|
655
|
+
required: import_zod2.z.array(ToolkitAuthFieldSchema)
|
|
656
|
+
}),
|
|
657
|
+
connectedAccountInitiation: import_zod2.z.object({
|
|
658
|
+
optional: import_zod2.z.array(ToolkitAuthFieldSchema),
|
|
659
|
+
required: import_zod2.z.array(ToolkitAuthFieldSchema)
|
|
660
|
+
})
|
|
661
|
+
})
|
|
662
|
+
),
|
|
663
|
+
proxy: import_zod2.z.object({
|
|
664
|
+
baseUrul: import_zod2.z.string()
|
|
665
|
+
})
|
|
666
|
+
});
|
|
667
|
+
var ToolkitRetrieveResponseSchema = import_zod2.z.object({
|
|
668
|
+
name: import_zod2.z.string(),
|
|
669
|
+
slug: import_zod2.z.string(),
|
|
670
|
+
meta: ToolKitMetaSchema,
|
|
671
|
+
isLocalToolkit: import_zod2.z.boolean(),
|
|
672
|
+
composioManagedAuthSchemes: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
673
|
+
authConfigDetails: import_zod2.z.array(ToolkitAuthConfigDetailsSchema).optional()
|
|
674
|
+
});
|
|
675
|
+
var ToolkitCategorySchema = import_zod2.z.object({
|
|
676
|
+
id: import_zod2.z.string(),
|
|
677
|
+
name: import_zod2.z.string()
|
|
678
|
+
});
|
|
679
|
+
var ToolkitRetrieveCategoriesResponseSchema = import_zod2.z.object({
|
|
680
|
+
items: import_zod2.z.array(ToolkitCategorySchema),
|
|
681
|
+
nextCursor: import_zod2.z.string().nullable(),
|
|
682
|
+
totalPages: import_zod2.z.number()
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
// src/models/Toolkits.ts
|
|
686
|
+
var Toolkits = class {
|
|
687
|
+
client;
|
|
688
|
+
constructor(client) {
|
|
689
|
+
this.client = client;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* List all toolkits available in the Composio SDK.
|
|
693
|
+
* This method fetches the toolkits from the Composio API.
|
|
694
|
+
* @returns {Promise<Toolkit[]>} List of toolkits
|
|
695
|
+
*/
|
|
696
|
+
async getToolkits(query) {
|
|
697
|
+
const parsedQuery = ToolkitsListParamsSchema.parse(query);
|
|
698
|
+
const result = await this.client.toolkits.list({
|
|
699
|
+
category: parsedQuery.category,
|
|
700
|
+
is_local: parsedQuery.isLocal,
|
|
701
|
+
managed_by: parsedQuery.managedBy,
|
|
702
|
+
sort_by: parsedQuery.sortBy
|
|
703
|
+
});
|
|
704
|
+
const parsedResult = ToolKitListResponseSchema.parse({
|
|
705
|
+
items: result.items.map((item) => {
|
|
706
|
+
const parsedItem = ToolKitItemSchema.parse({
|
|
707
|
+
name: item.name,
|
|
708
|
+
slug: item.slug,
|
|
709
|
+
meta: item.meta,
|
|
710
|
+
isLocalToolkit: item.is_local_toolkit,
|
|
711
|
+
authSchemes: item.auth_schemes,
|
|
712
|
+
composioManagedAuthSchemes: item.composio_managed_auth_schemes,
|
|
713
|
+
noAuth: item.no_auth
|
|
714
|
+
});
|
|
715
|
+
return parsedItem;
|
|
716
|
+
}),
|
|
717
|
+
nextCursor: result.next_cursor,
|
|
718
|
+
totalPages: result.total_pages
|
|
719
|
+
});
|
|
720
|
+
return parsedResult;
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Fetches a toolkit by its Slug.
|
|
724
|
+
* This method retrieves the toolkit from the Composio API.
|
|
725
|
+
* @param slug - The ID of the toolkit to be retrieved
|
|
726
|
+
* @param options - Request options
|
|
727
|
+
* @returns {Promise<ToolkitRetrieveResponse>} The toolkit object
|
|
728
|
+
*/
|
|
729
|
+
async getToolkitBySlug(slug) {
|
|
730
|
+
const result = await this.client.toolkits.retrieve(slug);
|
|
731
|
+
const parsedResult = ToolkitRetrieveResponseSchema.parse({
|
|
732
|
+
name: result.name,
|
|
733
|
+
slug: result.slug,
|
|
734
|
+
meta: result.meta,
|
|
735
|
+
isLocalToolkit: result.is_local_toolkit,
|
|
736
|
+
composioManagedAuthSchemes: result.composio_managed_auth_schemes,
|
|
737
|
+
authConfigDetails: result.auth_config_details
|
|
738
|
+
});
|
|
739
|
+
return parsedResult;
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* Fetches a toolkit by its Slug or a list of toolkits.
|
|
743
|
+
* This method retrieves the toolkit from the Composio API.
|
|
744
|
+
* @param arg - The ID of the toolkit to be retrieved or a list of parameters
|
|
745
|
+
* @returns {Promise<ToolkitRetrieveResponse | ToolKitListResponse>} The toolkit object or a list of toolkits
|
|
746
|
+
*/
|
|
747
|
+
async get(arg) {
|
|
748
|
+
if (typeof arg === "string") {
|
|
749
|
+
return this.getToolkitBySlug(arg);
|
|
750
|
+
}
|
|
751
|
+
return this.getToolkits(arg);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Fetches all categories available in the Composio SDK
|
|
755
|
+
*
|
|
756
|
+
* @returns {Promise<ToolkitRetrieveCategoriesResponse>} List of categories
|
|
757
|
+
*/
|
|
758
|
+
async listCategories() {
|
|
759
|
+
const result = await this.client.toolkits.retrieveCategories();
|
|
760
|
+
const parsedResult = ToolkitRetrieveCategoriesResponseSchema.parse({
|
|
761
|
+
items: result.items.map((item) => {
|
|
762
|
+
const parsedItem = ToolkitCategorySchema.parse({
|
|
763
|
+
id: item.id,
|
|
764
|
+
name: item.name
|
|
765
|
+
});
|
|
766
|
+
return parsedItem;
|
|
767
|
+
}),
|
|
768
|
+
nextCursor: result.next_cursor,
|
|
769
|
+
totalPages: result.total_pages
|
|
770
|
+
});
|
|
771
|
+
return parsedResult;
|
|
772
|
+
}
|
|
773
|
+
};
|
|
220
774
|
|
|
221
775
|
// src/utils/pusher.ts
|
|
222
776
|
var PUSHER_KEY = process.env.CLIENT_PUSHER_KEY || "ff9f18c208855d77a152";
|
|
@@ -311,7 +865,6 @@ var PusherUtils = class _PusherUtils {
|
|
|
311
865
|
|
|
312
866
|
// src/models/Session.ts
|
|
313
867
|
var Session = class {
|
|
314
|
-
FILE_NAME = "core/models/Session.ts";
|
|
315
868
|
client;
|
|
316
869
|
constructor(client) {
|
|
317
870
|
this.client = client;
|
|
@@ -327,7 +880,6 @@ var Session = class {
|
|
|
327
880
|
|
|
328
881
|
// src/models/Triggers.ts
|
|
329
882
|
var Triggers = class {
|
|
330
|
-
FILE_NAME = "core/models/Triggers.ts";
|
|
331
883
|
client;
|
|
332
884
|
constructor(client) {
|
|
333
885
|
this.client = client;
|
|
@@ -461,53 +1013,6 @@ var Triggers = class {
|
|
|
461
1013
|
}
|
|
462
1014
|
};
|
|
463
1015
|
|
|
464
|
-
// src/toolset/BaseToolset.ts
|
|
465
|
-
var BaseComposioToolset = class {
|
|
466
|
-
client;
|
|
467
|
-
DEFAULT_ENTITY_ID = "default";
|
|
468
|
-
/**
|
|
469
|
-
* Set the client for the toolset. This is automatically done by the Composio class.
|
|
470
|
-
* @param client - The Composio client.
|
|
471
|
-
*/
|
|
472
|
-
setClient(client) {
|
|
473
|
-
this.client = client;
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Get a tool from the client.
|
|
477
|
-
* @param slug - The slug of the tool.
|
|
478
|
-
* @returns The tool.
|
|
479
|
-
*/
|
|
480
|
-
async getTool(slug) {
|
|
481
|
-
const tool = await this.client?.tools.get(slug);
|
|
482
|
-
return this._wrapTool(tool);
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* Ensure the client is initialized.
|
|
486
|
-
* This is automatically done by the Composio class.
|
|
487
|
-
*/
|
|
488
|
-
ensureClient() {
|
|
489
|
-
if (!this.client) {
|
|
490
|
-
throw new Error(
|
|
491
|
-
"Client not initialized. Make sure the toolset is properly initialized with Composio."
|
|
492
|
-
);
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
// src/toolset/ComposioToolset.ts
|
|
498
|
-
var ComposioToolset = class extends BaseComposioToolset {
|
|
499
|
-
FILE_NAME = "core/toolset/ComposioToolset.ts";
|
|
500
|
-
_wrapTool = (tool) => {
|
|
501
|
-
return tool;
|
|
502
|
-
};
|
|
503
|
-
async getTools(params) {
|
|
504
|
-
const tools = await this.client?.tools.list(params);
|
|
505
|
-
return tools?.items.map((tool) => this._wrapTool(tool)) ?? [];
|
|
506
|
-
}
|
|
507
|
-
async test() {
|
|
508
|
-
}
|
|
509
|
-
};
|
|
510
|
-
|
|
511
1016
|
// src/models/AuthConfigs.ts
|
|
512
1017
|
var AuthConfigs = class {
|
|
513
1018
|
FILE_NAME = "core/models/AuthConfigs.ts";
|
|
@@ -591,9 +1096,176 @@ var AuthConfigs = class {
|
|
|
591
1096
|
}
|
|
592
1097
|
};
|
|
593
1098
|
|
|
1099
|
+
// src/types/connectedAccounts.types.ts
|
|
1100
|
+
var import_zod3 = require("zod");
|
|
1101
|
+
var ConnectedAccountStatuses = {
|
|
1102
|
+
ACTIVE: "ACTIVE",
|
|
1103
|
+
INACTIVE: "INACTIVE",
|
|
1104
|
+
PENDING: "DELETED",
|
|
1105
|
+
INITIATED: "INITIATED",
|
|
1106
|
+
EXPIRED: "EXPIRED",
|
|
1107
|
+
FAILED: "FAILED"
|
|
1108
|
+
};
|
|
1109
|
+
var ConnectedAccountStatusSchema = import_zod3.z.enum([
|
|
1110
|
+
ConnectedAccountStatuses.ACTIVE,
|
|
1111
|
+
ConnectedAccountStatuses.INACTIVE,
|
|
1112
|
+
ConnectedAccountStatuses.PENDING,
|
|
1113
|
+
ConnectedAccountStatuses.INITIATED,
|
|
1114
|
+
ConnectedAccountStatuses.EXPIRED,
|
|
1115
|
+
ConnectedAccountStatuses.FAILED
|
|
1116
|
+
]);
|
|
1117
|
+
var CreateConnectedAccountParamsSchema = import_zod3.z.object({
|
|
1118
|
+
authConfig: import_zod3.z.object({
|
|
1119
|
+
id: import_zod3.z.string()
|
|
1120
|
+
}),
|
|
1121
|
+
connection: import_zod3.z.object({
|
|
1122
|
+
data: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).optional(),
|
|
1123
|
+
redirectUrl: import_zod3.z.string().optional(),
|
|
1124
|
+
userId: import_zod3.z.string().optional()
|
|
1125
|
+
})
|
|
1126
|
+
});
|
|
1127
|
+
var DefaultCreateConnectedAccountParamsSchema = import_zod3.z.object({
|
|
1128
|
+
auth_config: import_zod3.z.object({
|
|
1129
|
+
id: import_zod3.z.string()
|
|
1130
|
+
}),
|
|
1131
|
+
connection: import_zod3.z.object({
|
|
1132
|
+
data: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).optional(),
|
|
1133
|
+
redirect_uri: import_zod3.z.string().optional(),
|
|
1134
|
+
user_id: import_zod3.z.string().optional()
|
|
1135
|
+
})
|
|
1136
|
+
});
|
|
1137
|
+
var CreateConnectedAccountOptionsSchema = import_zod3.z.object({
|
|
1138
|
+
redirectUrl: import_zod3.z.string().optional(),
|
|
1139
|
+
data: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).optional()
|
|
1140
|
+
});
|
|
1141
|
+
var CreateConnectedAccountResponseSchema = import_zod3.z.object({
|
|
1142
|
+
id: import_zod3.z.string(),
|
|
1143
|
+
status: ConnectedAccountStatusSchema,
|
|
1144
|
+
redirectUrl: import_zod3.z.string().nullable()
|
|
1145
|
+
});
|
|
1146
|
+
var ConnectedAccountAuthSchemes = {
|
|
1147
|
+
OAUTH2: "OAUTH2",
|
|
1148
|
+
OAUTH1: "OAUTH1",
|
|
1149
|
+
OAUTH1A: "OAUTH1A",
|
|
1150
|
+
API_KEY: "API_KEY",
|
|
1151
|
+
BASIC: "BASIC",
|
|
1152
|
+
BILLCOM_AUTH: "BILLCOM_AUTH",
|
|
1153
|
+
BEARER_TOKEN: "BEARER_TOKEN",
|
|
1154
|
+
GOOGLE_SERVICE_ACCOUNT: "GOOGLE_SERVICE_ACCOUNT",
|
|
1155
|
+
NO_AUTH: "NO_AUTH",
|
|
1156
|
+
BASIC_WITH_JWT: "BASIC_WITH_JWT",
|
|
1157
|
+
COMPOSIO_LINK: "COMPOSIO_LINK",
|
|
1158
|
+
CALCOM_AUTH: "CALCOM_AUTH",
|
|
1159
|
+
SNOWFLAKE: "SNOWFLAKE"
|
|
1160
|
+
};
|
|
1161
|
+
var ConnectedAccountAuthSchemesSchema = import_zod3.z.enum([
|
|
1162
|
+
ConnectedAccountAuthSchemes.OAUTH2,
|
|
1163
|
+
ConnectedAccountAuthSchemes.OAUTH1,
|
|
1164
|
+
ConnectedAccountAuthSchemes.OAUTH1A,
|
|
1165
|
+
ConnectedAccountAuthSchemes.API_KEY,
|
|
1166
|
+
ConnectedAccountAuthSchemes.BASIC,
|
|
1167
|
+
ConnectedAccountAuthSchemes.BILLCOM_AUTH,
|
|
1168
|
+
ConnectedAccountAuthSchemes.BEARER_TOKEN,
|
|
1169
|
+
ConnectedAccountAuthSchemes.GOOGLE_SERVICE_ACCOUNT,
|
|
1170
|
+
ConnectedAccountAuthSchemes.NO_AUTH,
|
|
1171
|
+
ConnectedAccountAuthSchemes.BASIC_WITH_JWT,
|
|
1172
|
+
ConnectedAccountAuthSchemes.COMPOSIO_LINK,
|
|
1173
|
+
ConnectedAccountAuthSchemes.CALCOM_AUTH,
|
|
1174
|
+
ConnectedAccountAuthSchemes.SNOWFLAKE
|
|
1175
|
+
]);
|
|
1176
|
+
var ConnectedAccountAuthConfigSchema = import_zod3.z.object({
|
|
1177
|
+
id: import_zod3.z.string(),
|
|
1178
|
+
authScheme: ConnectedAccountAuthSchemesSchema,
|
|
1179
|
+
isComposioManaged: import_zod3.z.boolean(),
|
|
1180
|
+
isDisabled: import_zod3.z.boolean()
|
|
1181
|
+
});
|
|
1182
|
+
var ConnectedAccountRetrieveResponseSchema = import_zod3.z.object({
|
|
1183
|
+
id: import_zod3.z.string(),
|
|
1184
|
+
uuid: import_zod3.z.string(),
|
|
1185
|
+
authConfig: ConnectedAccountAuthConfigSchema,
|
|
1186
|
+
userId: import_zod3.z.string(),
|
|
1187
|
+
data: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()),
|
|
1188
|
+
params: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()),
|
|
1189
|
+
status: ConnectedAccountStatusSchema,
|
|
1190
|
+
statusReason: import_zod3.z.string().nullable(),
|
|
1191
|
+
toolkit: import_zod3.z.object({
|
|
1192
|
+
slug: import_zod3.z.string()
|
|
1193
|
+
}),
|
|
1194
|
+
testRequestEndpoint: import_zod3.z.string().optional(),
|
|
1195
|
+
isDisabled: import_zod3.z.boolean(),
|
|
1196
|
+
createdAt: import_zod3.z.string(),
|
|
1197
|
+
updatedAt: import_zod3.z.string()
|
|
1198
|
+
});
|
|
1199
|
+
|
|
1200
|
+
// src/models/ConnectionRequest.ts
|
|
1201
|
+
var ConnectionRequest = class {
|
|
1202
|
+
FILE_NAME = "core/models/ConnectionRequest.ts";
|
|
1203
|
+
client;
|
|
1204
|
+
connectedAccountId;
|
|
1205
|
+
connectedAccountStatus;
|
|
1206
|
+
constructor(client, connectedAccountId, connectedAccountStatus) {
|
|
1207
|
+
this.client = client;
|
|
1208
|
+
this.connectedAccountId = connectedAccountId;
|
|
1209
|
+
this.connectedAccountStatus = connectedAccountStatus;
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Transform the response from the Composio API to the SDK format
|
|
1213
|
+
* @param response
|
|
1214
|
+
* @returns
|
|
1215
|
+
*/
|
|
1216
|
+
async transformResponse(response) {
|
|
1217
|
+
const parsedResponse = ConnectedAccountRetrieveResponseSchema.parse({
|
|
1218
|
+
...response,
|
|
1219
|
+
userId: response.user_id,
|
|
1220
|
+
// Add the missing userId property
|
|
1221
|
+
authConfig: {
|
|
1222
|
+
...response.auth_config,
|
|
1223
|
+
authScheme: response.auth_config.auth_scheme,
|
|
1224
|
+
isComposioManaged: response.auth_config.is_composio_managed,
|
|
1225
|
+
isDisabled: response.auth_config.is_disabled
|
|
1226
|
+
},
|
|
1227
|
+
statusReason: response.status_reason,
|
|
1228
|
+
isDisabled: response.is_disabled,
|
|
1229
|
+
createdAt: response.created_at,
|
|
1230
|
+
updatedAt: response.updated_at,
|
|
1231
|
+
testRequestEndpoint: response.test_request_endpoint
|
|
1232
|
+
});
|
|
1233
|
+
return parsedResponse;
|
|
1234
|
+
}
|
|
1235
|
+
/**
|
|
1236
|
+
* Wait for the connection to be established.
|
|
1237
|
+
* The function will poll the Composio API every second until the connection is established or the timeout is reached.
|
|
1238
|
+
* @param {number} timeout - The time in milliseconds to wait for the connection to be established. Default is 30 seconds.
|
|
1239
|
+
* @returns
|
|
1240
|
+
*/
|
|
1241
|
+
async waitForConnection(timeout = 6e4) {
|
|
1242
|
+
if (this.connectedAccountStatus === ConnectedAccountStatuses.ACTIVE) {
|
|
1243
|
+
const response = await this.client.connectedAccounts.retrieve(this.connectedAccountId);
|
|
1244
|
+
return Promise.resolve(this.transformResponse(response));
|
|
1245
|
+
}
|
|
1246
|
+
return new Promise((resolve, reject) => {
|
|
1247
|
+
const interval = setInterval(async () => {
|
|
1248
|
+
try {
|
|
1249
|
+
const response = await this.client.connectedAccounts.retrieve(this.connectedAccountId);
|
|
1250
|
+
if (response.status === ConnectedAccountStatuses.ACTIVE) {
|
|
1251
|
+
clearInterval(interval);
|
|
1252
|
+
resolve(this.transformResponse(response));
|
|
1253
|
+
}
|
|
1254
|
+
} catch (error) {
|
|
1255
|
+
clearInterval(interval);
|
|
1256
|
+
reject(error);
|
|
1257
|
+
}
|
|
1258
|
+
}, 1e3);
|
|
1259
|
+
setTimeout(() => {
|
|
1260
|
+
clearInterval(interval);
|
|
1261
|
+
reject(new Error("Connection request timed out"));
|
|
1262
|
+
}, timeout);
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
|
|
594
1267
|
// src/models/ConnectedAccounts.ts
|
|
595
1268
|
var ConnectedAccounts = class {
|
|
596
|
-
FILE_NAME = "core/models/ConnectedAccounts.ts";
|
|
597
1269
|
client;
|
|
598
1270
|
constructor(client) {
|
|
599
1271
|
this.client = client;
|
|
@@ -604,8 +1276,8 @@ var ConnectedAccounts = class {
|
|
|
604
1276
|
* @param {RequestOptions} options - Request options
|
|
605
1277
|
* @returns {Promise<ConnectedAccountListResponse>} List of connected accounts
|
|
606
1278
|
*/
|
|
607
|
-
async list(query
|
|
608
|
-
return this.client.connectedAccounts.list(query
|
|
1279
|
+
async list(query) {
|
|
1280
|
+
return this.client.connectedAccounts.list(query);
|
|
609
1281
|
}
|
|
610
1282
|
/**
|
|
611
1283
|
* Create a new connected account
|
|
@@ -613,8 +1285,8 @@ var ConnectedAccounts = class {
|
|
|
613
1285
|
* @param {RequestOptions} options - Request options
|
|
614
1286
|
* @returns {Promise<ConnectedAccountCreateResponse>} Created connected account
|
|
615
1287
|
*/
|
|
616
|
-
async create(data
|
|
617
|
-
return this.client.connectedAccounts.create(data
|
|
1288
|
+
async create(data) {
|
|
1289
|
+
return this.client.connectedAccounts.create(data);
|
|
618
1290
|
}
|
|
619
1291
|
/**
|
|
620
1292
|
* Get a connected account by nanoid
|
|
@@ -622,8 +1294,8 @@ var ConnectedAccounts = class {
|
|
|
622
1294
|
* @param {RequestOptions} options - Request options
|
|
623
1295
|
* @returns {Promise<ConnectedAccountRetrieveResponse>} Connected account details
|
|
624
1296
|
*/
|
|
625
|
-
async get(nanoid
|
|
626
|
-
return this.client.connectedAccounts.retrieve(nanoid
|
|
1297
|
+
async get(nanoid) {
|
|
1298
|
+
return this.client.connectedAccounts.retrieve(nanoid);
|
|
627
1299
|
}
|
|
628
1300
|
/**
|
|
629
1301
|
* Delete a connected account
|
|
@@ -631,8 +1303,8 @@ var ConnectedAccounts = class {
|
|
|
631
1303
|
* @param {RequestOptions} options - Request options
|
|
632
1304
|
* @returns {Promise<ConnectedAccountDeleteResponse>} Response from deletion
|
|
633
1305
|
*/
|
|
634
|
-
async delete(nanoid
|
|
635
|
-
return this.client.connectedAccounts.delete(nanoid
|
|
1306
|
+
async delete(nanoid) {
|
|
1307
|
+
return this.client.connectedAccounts.delete(nanoid);
|
|
636
1308
|
}
|
|
637
1309
|
/**
|
|
638
1310
|
* Refresh a connected account's credentials
|
|
@@ -640,8 +1312,8 @@ var ConnectedAccounts = class {
|
|
|
640
1312
|
* @param {RequestOptions} options - Request options
|
|
641
1313
|
* @returns {Promise<ConnectedAccountRefreshResponse>} Refreshed connected account details
|
|
642
1314
|
*/
|
|
643
|
-
async refresh(nanoid
|
|
644
|
-
return this.client.connectedAccounts.refresh(nanoid
|
|
1315
|
+
async refresh(nanoid) {
|
|
1316
|
+
return this.client.connectedAccounts.refresh(nanoid);
|
|
645
1317
|
}
|
|
646
1318
|
/**
|
|
647
1319
|
* Update the status of a connected account
|
|
@@ -650,8 +1322,8 @@ var ConnectedAccounts = class {
|
|
|
650
1322
|
* @param {RequestOptions} options - Request options
|
|
651
1323
|
* @returns {Promise<ConnectedAccountUpdateStatusResponse>} Updated connected account details
|
|
652
1324
|
*/
|
|
653
|
-
async updateStatus(nanoid, params
|
|
654
|
-
return this.client.connectedAccounts.updateStatus(nanoid, params
|
|
1325
|
+
async updateStatus(nanoid, params) {
|
|
1326
|
+
return this.client.connectedAccounts.updateStatus(nanoid, params);
|
|
655
1327
|
}
|
|
656
1328
|
/**
|
|
657
1329
|
* Enable a connected account
|
|
@@ -659,8 +1331,8 @@ var ConnectedAccounts = class {
|
|
|
659
1331
|
* @param {RequestOptions} options - Request options
|
|
660
1332
|
* @returns {Promise<ConnectedAccountUpdateStatusResponse>} Updated connected account details
|
|
661
1333
|
*/
|
|
662
|
-
async enable(nanoid
|
|
663
|
-
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: true }
|
|
1334
|
+
async enable(nanoid) {
|
|
1335
|
+
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: true });
|
|
664
1336
|
}
|
|
665
1337
|
/**
|
|
666
1338
|
* Disable a connected account
|
|
@@ -668,8 +1340,29 @@ var ConnectedAccounts = class {
|
|
|
668
1340
|
* @param {RequestOptions} options - Request options
|
|
669
1341
|
* @returns {Promise<ConnectedAccountUpdateStatusResponse>} Updated connected account details
|
|
670
1342
|
*/
|
|
671
|
-
async disable(nanoid
|
|
672
|
-
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: false }
|
|
1343
|
+
async disable(nanoid) {
|
|
1344
|
+
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: false });
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* Compound function to create a new connected account.
|
|
1348
|
+
* This function creates a new connected account and returns a connection request.
|
|
1349
|
+
* Users can then wait for the connection to be established using the `waitForConnection` method.
|
|
1350
|
+
* @param {CreateConnectedAccountParams} data - Data for creating a new connected account
|
|
1351
|
+
* @returns {Promise<ConnectionRequest>} Connection request object
|
|
1352
|
+
*/
|
|
1353
|
+
async createConnectedAccount(userId, authConfigId, options) {
|
|
1354
|
+
const { redirectUrl, data } = options;
|
|
1355
|
+
const response = await this.create({
|
|
1356
|
+
auth_config: {
|
|
1357
|
+
id: authConfigId
|
|
1358
|
+
},
|
|
1359
|
+
connection: {
|
|
1360
|
+
data,
|
|
1361
|
+
redirect_uri: redirectUrl,
|
|
1362
|
+
user_id: userId
|
|
1363
|
+
}
|
|
1364
|
+
});
|
|
1365
|
+
return new ConnectionRequest(this.client, response.id, response.status);
|
|
673
1366
|
}
|
|
674
1367
|
};
|
|
675
1368
|
|
|
@@ -847,13 +1540,14 @@ var Telemetry = class {
|
|
|
847
1540
|
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
848
1541
|
return key !== "constructor" && descriptor && typeof descriptor.value === "function" && descriptor.value.constructor.name === "AsyncFunction";
|
|
849
1542
|
});
|
|
1543
|
+
const instrumentedClassName = instance.constructor?.name || fileName || "unknown";
|
|
850
1544
|
for (const name of methodNames) {
|
|
851
1545
|
const originalMethod = instance[name];
|
|
852
1546
|
instance[name] = async (...args) => {
|
|
853
1547
|
const telemetryPayload = {
|
|
854
1548
|
eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
|
|
855
1549
|
data: {
|
|
856
|
-
fileName:
|
|
1550
|
+
fileName: instrumentedClassName,
|
|
857
1551
|
method: name,
|
|
858
1552
|
params: args
|
|
859
1553
|
},
|
|
@@ -866,7 +1560,7 @@ var Telemetry = class {
|
|
|
866
1560
|
const telemetryPayload2 = {
|
|
867
1561
|
eventName: TELEMETRY_EVENTS.SDK_METHOD_ERROR,
|
|
868
1562
|
data: {
|
|
869
|
-
fileName:
|
|
1563
|
+
fileName: instrumentedClassName,
|
|
870
1564
|
method: name,
|
|
871
1565
|
params: args,
|
|
872
1566
|
error
|
|
@@ -878,89 +1572,294 @@ var Telemetry = class {
|
|
|
878
1572
|
}
|
|
879
1573
|
};
|
|
880
1574
|
}
|
|
881
|
-
return instance;
|
|
1575
|
+
return instance;
|
|
1576
|
+
}
|
|
1577
|
+
/**
|
|
1578
|
+
* Send the telemetry payload to the server.
|
|
1579
|
+
* @param payload - the telemetry payload to send
|
|
1580
|
+
* @returns
|
|
1581
|
+
*/
|
|
1582
|
+
async sendTelemetry(payload) {
|
|
1583
|
+
const isTelemetryDisabled = getEnvVariable("TELEMETRY_DISABLED", "false") === "true";
|
|
1584
|
+
if (isTelemetryDisabled) {
|
|
1585
|
+
return;
|
|
1586
|
+
}
|
|
1587
|
+
const url = `${TELEMETRY_URL}/api/sdk_metrics/telemetry`;
|
|
1588
|
+
const reqPayload = {
|
|
1589
|
+
data: payload,
|
|
1590
|
+
url,
|
|
1591
|
+
method: "POST",
|
|
1592
|
+
headers: { "Content-Type": "application/json" }
|
|
1593
|
+
};
|
|
1594
|
+
this.transport.send(reqPayload);
|
|
1595
|
+
}
|
|
1596
|
+
};
|
|
1597
|
+
|
|
1598
|
+
// src/utils/sdk.ts
|
|
1599
|
+
var userDataPath = () => {
|
|
1600
|
+
try {
|
|
1601
|
+
const path = require("path");
|
|
1602
|
+
const os = require("os");
|
|
1603
|
+
return path.join(os.homedir(), COMPOSIO_DIR, USER_DATA_FILE_NAME);
|
|
1604
|
+
} catch (_error) {
|
|
1605
|
+
return null;
|
|
1606
|
+
}
|
|
1607
|
+
};
|
|
1608
|
+
var getUserDataJson = () => {
|
|
1609
|
+
try {
|
|
1610
|
+
const fs = require("fs");
|
|
1611
|
+
const data = fs.readFileSync(userDataPath(), "utf8");
|
|
1612
|
+
return JSON.parse(data);
|
|
1613
|
+
} catch (_error) {
|
|
1614
|
+
return {};
|
|
1615
|
+
}
|
|
1616
|
+
};
|
|
1617
|
+
function getSDKConfig(baseUrl, apiKey) {
|
|
1618
|
+
const userData = getUserDataJson();
|
|
1619
|
+
const { api_key: apiKeyFromUserConfig, base_url: baseURLFromUserConfig } = userData;
|
|
1620
|
+
const baseURLParsed = baseUrl || getEnvVariable("COMPOSIO_BASE_URL") || baseURLFromUserConfig || DEFAULT_BASE_URL;
|
|
1621
|
+
const apiKeyParsed = apiKey || getEnvVariable("COMPOSIO_API_KEY") || apiKeyFromUserConfig || "";
|
|
1622
|
+
return { baseURL: baseURLParsed, apiKey: apiKeyParsed };
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
// src/utils/version.ts
|
|
1626
|
+
function isNewerVersion(version1, version2) {
|
|
1627
|
+
const v1 = version1.split(".").map(Number);
|
|
1628
|
+
const v2 = version2.split(".").map(Number);
|
|
1629
|
+
for (let i = 0; i < 3; i++) {
|
|
1630
|
+
if (v1[i] > v2[i]) return true;
|
|
1631
|
+
if (v1[i] < v2[i]) return false;
|
|
1632
|
+
}
|
|
1633
|
+
return false;
|
|
1634
|
+
}
|
|
1635
|
+
async function checkForLatestVersionFromNPM(currentVersion) {
|
|
1636
|
+
try {
|
|
1637
|
+
const packageName = "composio-core";
|
|
1638
|
+
const currentVersionFromPackageJson = currentVersion;
|
|
1639
|
+
const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`);
|
|
1640
|
+
const data = await response.json();
|
|
1641
|
+
const latestVersion = data.version;
|
|
1642
|
+
if (isNewerVersion(latestVersion, currentVersionFromPackageJson) && !IS_DEVELOPMENT_OR_CI) {
|
|
1643
|
+
logger_default.info(
|
|
1644
|
+
`\u{1F680} Upgrade available! Your composio-core version (${currentVersionFromPackageJson}) is behind. Latest version: ${latestVersion}.`
|
|
1645
|
+
);
|
|
1646
|
+
}
|
|
1647
|
+
} catch (_error) {
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
// src/toolset/BaseToolset.ts
|
|
1652
|
+
var BaseToolset = class {
|
|
1653
|
+
/**
|
|
1654
|
+
* @internal
|
|
1655
|
+
* The function to execute a tool.
|
|
1656
|
+
* This is set automatically injected by the core SDK.
|
|
1657
|
+
*/
|
|
1658
|
+
_globalExecuteToolFn;
|
|
1659
|
+
/**
|
|
1660
|
+
* @internal
|
|
1661
|
+
* Set the function to execute a tool.
|
|
1662
|
+
* This is set automatically injected by the core SDK.
|
|
1663
|
+
*/
|
|
1664
|
+
_setExecuteToolFn(executeToolFn) {
|
|
1665
|
+
this._globalExecuteToolFn = executeToolFn;
|
|
1666
|
+
}
|
|
1667
|
+
/**
|
|
1668
|
+
* @public
|
|
1669
|
+
* Gloabl function to execute a tool.
|
|
1670
|
+
* This function is used by toolset providers to implement helper functions to execute tools.
|
|
1671
|
+
* This is a 1:1 mapping of the `execute` method in the `Tools` class.
|
|
1672
|
+
* @param {string} toolSlug - The slug of the tool to execute.
|
|
1673
|
+
* @param {Record<string, unknown>} input - The input to the tool.
|
|
1674
|
+
* @returns {Promise<string>} The result of the tool execution.
|
|
1675
|
+
*/
|
|
1676
|
+
executeTool(toolSlug, body, modifers) {
|
|
1677
|
+
return this._globalExecuteToolFn(toolSlug, body, modifers);
|
|
1678
|
+
}
|
|
1679
|
+
};
|
|
1680
|
+
var BaseNonAgenticToolset = class extends BaseToolset {
|
|
1681
|
+
_isAgentic = false;
|
|
1682
|
+
};
|
|
1683
|
+
var BaseAgenticToolset = class extends BaseToolset {
|
|
1684
|
+
_isAgentic = true;
|
|
1685
|
+
};
|
|
1686
|
+
|
|
1687
|
+
// src/toolset/OpenAIToolset.ts
|
|
1688
|
+
var OpenAIToolset = class extends BaseNonAgenticToolset {
|
|
1689
|
+
name = "openai";
|
|
1690
|
+
/**
|
|
1691
|
+
* Absract method to wrap a tool in the toolset.
|
|
1692
|
+
* This method is implemented by the toolset.
|
|
1693
|
+
* @param tool - The tool to wrap.
|
|
1694
|
+
* @returns The wrapped tool.
|
|
1695
|
+
*/
|
|
1696
|
+
wrapTool = (tool) => {
|
|
1697
|
+
const formattedSchema = {
|
|
1698
|
+
name: tool.slug,
|
|
1699
|
+
description: tool.description,
|
|
1700
|
+
parameters: tool.inputParameters
|
|
1701
|
+
};
|
|
1702
|
+
return {
|
|
1703
|
+
type: "function",
|
|
1704
|
+
function: formattedSchema
|
|
1705
|
+
};
|
|
1706
|
+
};
|
|
1707
|
+
wrapTools = (tools) => {
|
|
1708
|
+
return tools.map((tool) => this.wrapTool(tool));
|
|
1709
|
+
};
|
|
1710
|
+
/**
|
|
1711
|
+
* Execute a tool call.
|
|
1712
|
+
* @param {OpenAI.ChatCompletionMessageToolCall} tool - The tool to execute.
|
|
1713
|
+
* @param {string} userId - The user id.
|
|
1714
|
+
* @returns {Promise<string>} The result of the tool call.
|
|
1715
|
+
*/
|
|
1716
|
+
async executeToolCall(userId, tool, options, modifiers) {
|
|
1717
|
+
const payload = {
|
|
1718
|
+
arguments: JSON.parse(tool.function.arguments),
|
|
1719
|
+
connectedAccountId: options.connectedAccountId,
|
|
1720
|
+
customAuthParams: options.customAuthParams,
|
|
1721
|
+
userId
|
|
1722
|
+
};
|
|
1723
|
+
const result = await this.executeTool(tool.function.name, payload, modifiers);
|
|
1724
|
+
return JSON.stringify(result);
|
|
1725
|
+
}
|
|
1726
|
+
/**
|
|
1727
|
+
* Handle the tool call from the assistant.
|
|
1728
|
+
*
|
|
1729
|
+
* @param {OpenAI.ChatCompletion} chatCompletion - The chat completion object.
|
|
1730
|
+
* @param {string} userId - The user id.
|
|
1731
|
+
* @returns {Promise<string[]>} The results of the tool call.
|
|
1732
|
+
*/
|
|
1733
|
+
async handleToolCall(userId, chatCompletion, options, modifiers) {
|
|
1734
|
+
const outputs = [];
|
|
1735
|
+
for (const message of chatCompletion.choices) {
|
|
1736
|
+
if (message.message.tool_calls) {
|
|
1737
|
+
outputs.push(
|
|
1738
|
+
await this.executeToolCall(userId, message.message.tool_calls[0], options, modifiers)
|
|
1739
|
+
);
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
return outputs;
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* Handles all the tool calls from the assistant.
|
|
1746
|
+
*
|
|
1747
|
+
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1748
|
+
* @param {string} userId - The user id.
|
|
1749
|
+
* @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.
|
|
1750
|
+
*/
|
|
1751
|
+
async handleAssistantMessage(userId, run, options, modifiers) {
|
|
1752
|
+
const tool_calls = run.required_action?.submit_tool_outputs?.tool_calls || [];
|
|
1753
|
+
const tool_outputs = await Promise.all(
|
|
1754
|
+
tool_calls.map(async (tool_call) => {
|
|
1755
|
+
logger_default.debug(`Executing tool call: ${tool_call.id}`);
|
|
1756
|
+
const tool_response = await this.executeToolCall(
|
|
1757
|
+
userId,
|
|
1758
|
+
tool_call,
|
|
1759
|
+
options,
|
|
1760
|
+
modifiers
|
|
1761
|
+
);
|
|
1762
|
+
logger_default.debug(`Tool call ${tool_call.id} executed with response: ${tool_response}`);
|
|
1763
|
+
return {
|
|
1764
|
+
tool_call_id: tool_call.id,
|
|
1765
|
+
output: JSON.stringify(tool_response)
|
|
1766
|
+
};
|
|
1767
|
+
})
|
|
1768
|
+
);
|
|
1769
|
+
return tool_outputs;
|
|
1770
|
+
}
|
|
1771
|
+
/**
|
|
1772
|
+
* Waits for the assistant stream and handles the tool calls.
|
|
1773
|
+
*
|
|
1774
|
+
* @param {OpenAI} client - The OpenAI client.
|
|
1775
|
+
* @param {Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>} runStream - The run stream.
|
|
1776
|
+
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1777
|
+
* @param {string} userId - The user id.
|
|
1778
|
+
* @returns {AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>} The run stream.
|
|
1779
|
+
*/
|
|
1780
|
+
async *waitAndHandleAssistantStreamToolCalls(userId, client, runStream, thread, options, modifiers) {
|
|
1781
|
+
let runId = null;
|
|
1782
|
+
for await (const event of runStream) {
|
|
1783
|
+
yield event;
|
|
1784
|
+
if (event.event === "thread.run.created") {
|
|
1785
|
+
const { id } = event.data;
|
|
1786
|
+
runId = id;
|
|
1787
|
+
}
|
|
1788
|
+
if (!runId) {
|
|
1789
|
+
continue;
|
|
1790
|
+
}
|
|
1791
|
+
if (event.event === "thread.run.requires_action") {
|
|
1792
|
+
const toolOutputs = await this.handleAssistantMessage(
|
|
1793
|
+
userId,
|
|
1794
|
+
event.data,
|
|
1795
|
+
options,
|
|
1796
|
+
modifiers
|
|
1797
|
+
);
|
|
1798
|
+
await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
|
|
1799
|
+
tool_outputs: toolOutputs
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
if ([
|
|
1803
|
+
"thread.run.completed",
|
|
1804
|
+
"thread.run.failed",
|
|
1805
|
+
"thread.run.cancelled",
|
|
1806
|
+
"thread.run.expired"
|
|
1807
|
+
].includes(event.event)) {
|
|
1808
|
+
break;
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
if (!runId) {
|
|
1812
|
+
throw new Error("No run ID found");
|
|
1813
|
+
}
|
|
1814
|
+
let finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1815
|
+
while (["queued", "in_progress", "requires_action"].includes(finalRun.status)) {
|
|
1816
|
+
if (finalRun.status === "requires_action") {
|
|
1817
|
+
const toolOutputs = await this.handleAssistantMessage(userId, finalRun, options, modifiers);
|
|
1818
|
+
finalRun = await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
|
|
1819
|
+
tool_outputs: toolOutputs
|
|
1820
|
+
});
|
|
1821
|
+
} else {
|
|
1822
|
+
finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1823
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
882
1826
|
}
|
|
883
1827
|
/**
|
|
884
|
-
*
|
|
885
|
-
*
|
|
886
|
-
* @
|
|
1828
|
+
* Waits for the assistant tool calls and handles them.
|
|
1829
|
+
*
|
|
1830
|
+
* @param {OpenAI} client - The OpenAI client.
|
|
1831
|
+
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1832
|
+
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1833
|
+
* @param {string} userId - The user id.
|
|
1834
|
+
* @returns {Promise<OpenAI.Beta.Threads.Run>} The run object.
|
|
887
1835
|
*/
|
|
888
|
-
async
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
1836
|
+
async waitAndHandleAssistantToolCalls(userId, client, run, thread, options, modifiers) {
|
|
1837
|
+
while (["queued", "in_progress", "requires_action"].includes(run.status)) {
|
|
1838
|
+
const tool_outputs = await this.handleAssistantMessage(userId, run, options, modifiers);
|
|
1839
|
+
if (run.status === "requires_action") {
|
|
1840
|
+
run = await client.beta.threads.runs.submitToolOutputs(thread.id, run.id, {
|
|
1841
|
+
tool_outputs
|
|
1842
|
+
});
|
|
1843
|
+
} else {
|
|
1844
|
+
run = await client.beta.threads.runs.retrieve(thread.id, run.id);
|
|
1845
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1846
|
+
}
|
|
892
1847
|
}
|
|
893
|
-
|
|
894
|
-
const reqPayload = {
|
|
895
|
-
data: payload,
|
|
896
|
-
url,
|
|
897
|
-
method: "POST",
|
|
898
|
-
headers: { "Content-Type": "application/json" }
|
|
899
|
-
};
|
|
900
|
-
this.transport.send(reqPayload);
|
|
901
|
-
}
|
|
902
|
-
};
|
|
903
|
-
|
|
904
|
-
// src/utils/sdk.ts
|
|
905
|
-
var userDataPath = () => {
|
|
906
|
-
try {
|
|
907
|
-
const path = require("path");
|
|
908
|
-
const os = require("os");
|
|
909
|
-
return path.join(os.homedir(), COMPOSIO_DIR, USER_DATA_FILE_NAME);
|
|
910
|
-
} catch (_error) {
|
|
911
|
-
return null;
|
|
912
|
-
}
|
|
913
|
-
};
|
|
914
|
-
var getUserDataJson = () => {
|
|
915
|
-
try {
|
|
916
|
-
const fs = require("fs");
|
|
917
|
-
const data = fs.readFileSync(userDataPath(), "utf8");
|
|
918
|
-
return JSON.parse(data);
|
|
919
|
-
} catch (_error) {
|
|
920
|
-
return {};
|
|
1848
|
+
return run;
|
|
921
1849
|
}
|
|
922
1850
|
};
|
|
923
|
-
function getSDKConfig(baseUrl, apiKey) {
|
|
924
|
-
const userData = getUserDataJson();
|
|
925
|
-
const { api_key: apiKeyFromUserConfig, base_url: baseURLFromUserConfig } = userData;
|
|
926
|
-
const baseURLParsed = baseUrl || getEnvVariable("COMPOSIO_BASE_URL") || baseURLFromUserConfig || DEFAULT_BASE_URL;
|
|
927
|
-
const apiKeyParsed = apiKey || getEnvVariable("COMPOSIO_API_KEY") || apiKeyFromUserConfig || "";
|
|
928
|
-
return { baseURL: baseURLParsed, apiKey: apiKeyParsed };
|
|
929
|
-
}
|
|
930
1851
|
|
|
931
1852
|
// package.json
|
|
932
|
-
var version = "0.1.
|
|
1853
|
+
var version = "0.1.1-alpha.0";
|
|
933
1854
|
|
|
934
|
-
// src/utils/
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
for (let i = 0; i < 3; i++) {
|
|
939
|
-
if (v1[i] > v2[i]) return true;
|
|
940
|
-
if (v1[i] < v2[i]) return false;
|
|
941
|
-
}
|
|
942
|
-
return false;
|
|
943
|
-
}
|
|
944
|
-
async function checkForLatestVersionFromNPM() {
|
|
945
|
-
try {
|
|
946
|
-
const packageName = "composio-core";
|
|
947
|
-
const currentVersionFromPackageJson = version;
|
|
948
|
-
const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`);
|
|
949
|
-
const data = await response.json();
|
|
950
|
-
const latestVersion = data.version;
|
|
951
|
-
if (isNewerVersion(latestVersion, currentVersionFromPackageJson) && !IS_DEVELOPMENT_OR_CI) {
|
|
952
|
-
logger_default.info(
|
|
953
|
-
`\u{1F680} Upgrade available! Your composio-core version (${currentVersionFromPackageJson}) is behind. Latest version: ${latestVersion}.`
|
|
954
|
-
);
|
|
955
|
-
}
|
|
956
|
-
} catch (_error) {
|
|
957
|
-
}
|
|
1855
|
+
// src/utils/uuid.ts
|
|
1856
|
+
var import_uuid = require("uuid");
|
|
1857
|
+
function getRandomUUID() {
|
|
1858
|
+
return (0, import_uuid.v4)();
|
|
958
1859
|
}
|
|
959
1860
|
|
|
960
1861
|
// src/composio.ts
|
|
961
1862
|
var Composio = class {
|
|
962
|
-
DEFAULT_USER_ID = "default";
|
|
963
|
-
FILE_NAME = "core/composio.ts";
|
|
964
1863
|
/**
|
|
965
1864
|
* The Composio API client.
|
|
966
1865
|
* @type {ComposioClient}
|
|
@@ -968,15 +1867,10 @@ var Composio = class {
|
|
|
968
1867
|
client;
|
|
969
1868
|
/**
|
|
970
1869
|
* The configuration for the Composio SDK.
|
|
971
|
-
* @type {ComposioConfig<
|
|
1870
|
+
* @type {ComposioConfig<TToolset>}
|
|
972
1871
|
*/
|
|
973
1872
|
config;
|
|
974
1873
|
telemetry;
|
|
975
|
-
/**
|
|
976
|
-
* Context variables for the Composio SDK.
|
|
977
|
-
*/
|
|
978
|
-
userId;
|
|
979
|
-
connectedAccountIds;
|
|
980
1874
|
/**
|
|
981
1875
|
* Core models for Composio.
|
|
982
1876
|
*/
|
|
@@ -985,7 +1879,9 @@ var Composio = class {
|
|
|
985
1879
|
triggers;
|
|
986
1880
|
toolset;
|
|
987
1881
|
authConfigs;
|
|
1882
|
+
// connected accounts
|
|
988
1883
|
connectedAccounts;
|
|
1884
|
+
createConnectedAccount;
|
|
989
1885
|
/**
|
|
990
1886
|
* @param {Object} config Configuration for the Composio SDK.
|
|
991
1887
|
* @param {string} config.apiKey The API key for the Composio SDK.
|
|
@@ -993,7 +1889,7 @@ var Composio = class {
|
|
|
993
1889
|
* @param {string} config.runtime The runtime for the Composio SDK.
|
|
994
1890
|
* @param {boolean} config.allowTracking Whether to allow analytics / tracking. Defaults to true.
|
|
995
1891
|
* @param {boolean} config.allowTracing Whether to allow tracing. Defaults to true.
|
|
996
|
-
* @param {
|
|
1892
|
+
* @param {TToolset} config.toolset The toolset to use for this Composio instance.
|
|
997
1893
|
*/
|
|
998
1894
|
constructor(config) {
|
|
999
1895
|
const { baseURL: baseURLParsed, apiKey: apiKeyParsed } = getSDKConfig(
|
|
@@ -1003,11 +1899,6 @@ var Composio = class {
|
|
|
1003
1899
|
if (IS_DEVELOPMENT_OR_CI) {
|
|
1004
1900
|
logger_default.info(`Initializing Composio w API Key: [REDACTED] and baseURL: ${baseURLParsed}`);
|
|
1005
1901
|
}
|
|
1006
|
-
if (config.userId && config.connectedAccountIds) {
|
|
1007
|
-
logger_default.warn(
|
|
1008
|
-
"When both userId and connectedAccountIds are provided, preference will be given to connectedAccountIds"
|
|
1009
|
-
);
|
|
1010
|
-
}
|
|
1011
1902
|
this.client = new import_client.default({
|
|
1012
1903
|
apiKey: apiKeyParsed,
|
|
1013
1904
|
baseURL: baseURLParsed
|
|
@@ -1016,15 +1907,14 @@ var Composio = class {
|
|
|
1016
1907
|
...config,
|
|
1017
1908
|
allowTracking: config.allowTracking ?? true
|
|
1018
1909
|
};
|
|
1019
|
-
this.
|
|
1020
|
-
this.
|
|
1021
|
-
this.toolset
|
|
1022
|
-
this.toolset.setClient(this);
|
|
1023
|
-
this.tools = new Tools(this.client);
|
|
1910
|
+
this.toolset = config.toolset ?? new OpenAIToolset();
|
|
1911
|
+
this.tools = new Tools(this.client, this.toolset);
|
|
1912
|
+
this.toolset._setExecuteToolFn(this.tools.execute);
|
|
1024
1913
|
this.toolkits = new Toolkits(this.client);
|
|
1025
1914
|
this.triggers = new Triggers(this.client);
|
|
1026
1915
|
this.authConfigs = new AuthConfigs(this.client);
|
|
1027
1916
|
this.connectedAccounts = new ConnectedAccounts(this.client);
|
|
1917
|
+
this.createConnectedAccount = this.connectedAccounts.createConnectedAccount;
|
|
1028
1918
|
if (this.config.allowTracking ?? true) {
|
|
1029
1919
|
this.initializeTelemetry(
|
|
1030
1920
|
{
|
|
@@ -1033,14 +1923,15 @@ var Composio = class {
|
|
|
1033
1923
|
frameworkRuntime: "node",
|
|
1034
1924
|
source: "node",
|
|
1035
1925
|
// @TODO: get the source
|
|
1036
|
-
sessionId: this.userId,
|
|
1037
1926
|
composioVersion: version,
|
|
1038
|
-
isBrowser: typeof window !== "undefined"
|
|
1927
|
+
isBrowser: typeof window !== "undefined",
|
|
1928
|
+
sessionId: getRandomUUID()
|
|
1929
|
+
// @TODO: get the session id
|
|
1039
1930
|
},
|
|
1040
1931
|
config.telemetryTransport
|
|
1041
1932
|
);
|
|
1042
1933
|
}
|
|
1043
|
-
checkForLatestVersionFromNPM();
|
|
1934
|
+
checkForLatestVersionFromNPM(version);
|
|
1044
1935
|
}
|
|
1045
1936
|
/**
|
|
1046
1937
|
* Initialize the instrumentations and telemetry for the Composio SDK.
|
|
@@ -1057,14 +1948,6 @@ var Composio = class {
|
|
|
1057
1948
|
this.telemetry.instrumentTelemetry(this.connectedAccounts);
|
|
1058
1949
|
this.telemetry.instrumentTelemetry(this.toolset);
|
|
1059
1950
|
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Get the connected account id for the given app name.
|
|
1062
|
-
* @param appName - The name of the app.
|
|
1063
|
-
* @returns {string | undefined} The connected account id for the given app name.
|
|
1064
|
-
*/
|
|
1065
|
-
getConnectedAccountId(appName) {
|
|
1066
|
-
return this.connectedAccountIds?.[appName];
|
|
1067
|
-
}
|
|
1068
1951
|
/**
|
|
1069
1952
|
* Get the Composio SDK client.
|
|
1070
1953
|
* @returns {ComposioClient} The Composio API client.
|
|
@@ -1076,56 +1959,74 @@ var Composio = class {
|
|
|
1076
1959
|
return this.client;
|
|
1077
1960
|
}
|
|
1078
1961
|
/**
|
|
1079
|
-
*
|
|
1080
|
-
* @
|
|
1962
|
+
* Fetch all the tools from Composio.
|
|
1963
|
+
* @param {ToolListParams} params Parameters to fetch the tools.
|
|
1964
|
+
* @param {ToolsetOptions<TToolset>} modifiers Modifiers to apply to the tools.
|
|
1965
|
+
* @returns {Promise<ReturnType<TToolset['getTools']>>} The tools from the toolset.
|
|
1081
1966
|
*/
|
|
1082
|
-
|
|
1083
|
-
return this.
|
|
1967
|
+
getTools(userId, filters, modifiers) {
|
|
1968
|
+
return this.tools.get(userId, filters, modifiers);
|
|
1084
1969
|
}
|
|
1085
1970
|
/**
|
|
1086
|
-
*
|
|
1087
|
-
* @param
|
|
1088
|
-
* @
|
|
1971
|
+
* Fetch a tool from Composio by its slug.
|
|
1972
|
+
* @param {string} slug slug of the tool
|
|
1973
|
+
* @param {ToolsetOptions<TToolset>} modifiers to be applied to the tool
|
|
1974
|
+
* @returns {Promise<ReturnType<TToolset['getToolBySlug']>>} The tool from the toolset.
|
|
1089
1975
|
*/
|
|
1090
|
-
|
|
1091
|
-
return this.
|
|
1976
|
+
getToolBySlug(userId, slug, modifiers) {
|
|
1977
|
+
return this.tools.get(userId, slug, modifiers);
|
|
1978
|
+
}
|
|
1979
|
+
};
|
|
1980
|
+
|
|
1981
|
+
// src/toolset/ComposioToolset.ts
|
|
1982
|
+
var ComposioToolset = class extends BaseNonAgenticToolset {
|
|
1983
|
+
name = "ComposioToolset";
|
|
1984
|
+
wrapTool = (tool) => {
|
|
1985
|
+
return tool;
|
|
1986
|
+
};
|
|
1987
|
+
wrapTools(tools) {
|
|
1988
|
+
return tools.map((tool) => this.wrapTool(tool));
|
|
1092
1989
|
}
|
|
1093
1990
|
};
|
|
1094
1991
|
|
|
1992
|
+
// src/telemetry/TelemetryTransport.ts
|
|
1993
|
+
var BaseTelemetryTransport = class {
|
|
1994
|
+
};
|
|
1995
|
+
|
|
1095
1996
|
// src/utils/jsonSchema.ts
|
|
1096
|
-
var
|
|
1997
|
+
var import_zod4 = require("zod");
|
|
1097
1998
|
function jsonSchemaPropertiesToTSTypes(value) {
|
|
1098
1999
|
if (!value.type) {
|
|
1099
|
-
return
|
|
2000
|
+
return import_zod4.z.object({});
|
|
1100
2001
|
}
|
|
1101
2002
|
let zodType;
|
|
1102
2003
|
switch (value.type) {
|
|
1103
2004
|
case "string":
|
|
1104
|
-
zodType =
|
|
2005
|
+
zodType = import_zod4.z.string().describe(
|
|
1105
2006
|
(value.description || "") + (value.examples ? `
|
|
1106
2007
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1107
2008
|
);
|
|
1108
2009
|
break;
|
|
1109
2010
|
case "number":
|
|
1110
|
-
zodType =
|
|
2011
|
+
zodType = import_zod4.z.number().describe(
|
|
1111
2012
|
(value.description || "") + (value.examples ? `
|
|
1112
2013
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1113
2014
|
);
|
|
1114
2015
|
break;
|
|
1115
2016
|
case "integer":
|
|
1116
|
-
zodType =
|
|
2017
|
+
zodType = import_zod4.z.number().int().describe(
|
|
1117
2018
|
(value.description || "") + (value.examples ? `
|
|
1118
2019
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1119
2020
|
);
|
|
1120
2021
|
break;
|
|
1121
2022
|
case "boolean":
|
|
1122
|
-
zodType =
|
|
2023
|
+
zodType = import_zod4.z.boolean().describe(
|
|
1123
2024
|
(value.description || "") + (value.examples ? `
|
|
1124
2025
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1125
2026
|
);
|
|
1126
2027
|
break;
|
|
1127
2028
|
case "array":
|
|
1128
|
-
zodType =
|
|
2029
|
+
zodType = import_zod4.z.array(
|
|
1129
2030
|
jsonSchemaPropertiesToTSTypes(
|
|
1130
2031
|
value.items
|
|
1131
2032
|
)
|
|
@@ -1141,7 +2042,7 @@ Examples: ${value.examples.join(", ")}` : "")
|
|
|
1141
2042
|
);
|
|
1142
2043
|
break;
|
|
1143
2044
|
case "null":
|
|
1144
|
-
zodType =
|
|
2045
|
+
zodType = import_zod4.z.null().describe(value.description || "");
|
|
1145
2046
|
break;
|
|
1146
2047
|
default:
|
|
1147
2048
|
throw new Error(`Unsupported JSON schema type: ${value.type}`);
|
|
@@ -1152,7 +2053,7 @@ function jsonSchemaToModel(jsonSchema) {
|
|
|
1152
2053
|
const properties = jsonSchema.properties;
|
|
1153
2054
|
const requiredFields = jsonSchema.required || [];
|
|
1154
2055
|
if (!properties) {
|
|
1155
|
-
return
|
|
2056
|
+
return import_zod4.z.object({});
|
|
1156
2057
|
}
|
|
1157
2058
|
const zodSchema = {};
|
|
1158
2059
|
for (const [key, _] of Object.entries(properties)) {
|
|
@@ -1164,7 +2065,7 @@ function jsonSchemaToModel(jsonSchema) {
|
|
|
1164
2065
|
schema
|
|
1165
2066
|
)
|
|
1166
2067
|
);
|
|
1167
|
-
zodType =
|
|
2068
|
+
zodType = import_zod4.z.union(anyOfTypes).describe(
|
|
1168
2069
|
(value.description || "") + (value.examples ? `
|
|
1169
2070
|
Examples: ${value.examples.join(", ")}` : "")
|
|
1170
2071
|
);
|
|
@@ -1174,7 +2075,7 @@ Examples: ${value.examples.join(", ")}` : "")
|
|
|
1174
2075
|
schema
|
|
1175
2076
|
)
|
|
1176
2077
|
);
|
|
1177
|
-
zodType =
|
|
2078
|
+
zodType = import_zod4.z.intersection(
|
|
1178
2079
|
allOfTypes[0],
|
|
1179
2080
|
allOfTypes.slice(1).reduce((acc, schema) => acc.and(schema), allOfTypes[0])
|
|
1180
2081
|
).describe(
|
|
@@ -1198,207 +2099,16 @@ Examples: ${value.examples.join(", ")}` : "")
|
|
|
1198
2099
|
zodSchema[key] = zodType.optional();
|
|
1199
2100
|
}
|
|
1200
2101
|
}
|
|
1201
|
-
return
|
|
2102
|
+
return import_zod4.z.object(zodSchema);
|
|
1202
2103
|
}
|
|
1203
|
-
|
|
1204
|
-
// src/telemetry/TelemetryTransport.ts
|
|
1205
|
-
var BaseTelemetryTransport = class {
|
|
1206
|
-
};
|
|
1207
|
-
|
|
1208
|
-
// src/toolset/OpenAIToolset.ts
|
|
1209
|
-
var OpenAIToolset = class extends BaseComposioToolset {
|
|
1210
|
-
static FRAMEWORK_NAME = "openai";
|
|
1211
|
-
FILE_NAME = "toolsets/openai/src/index.ts";
|
|
1212
|
-
/**
|
|
1213
|
-
* Absract method to wrap a tool in the toolset.
|
|
1214
|
-
* This method is implemented by the toolset.
|
|
1215
|
-
* @param tool - The tool to wrap.
|
|
1216
|
-
* @returns The wrapped tool.
|
|
1217
|
-
*/
|
|
1218
|
-
_wrapTool = (tool) => {
|
|
1219
|
-
const formattedSchema = {
|
|
1220
|
-
name: tool.slug,
|
|
1221
|
-
description: tool.description,
|
|
1222
|
-
parameters: tool.input_parameters
|
|
1223
|
-
};
|
|
1224
|
-
return {
|
|
1225
|
-
type: "function",
|
|
1226
|
-
function: formattedSchema
|
|
1227
|
-
};
|
|
1228
|
-
};
|
|
1229
|
-
/**
|
|
1230
|
-
* Get all the tools from the Composio in OpenAI format.
|
|
1231
|
-
* @param params - The parameters for the tool list.
|
|
1232
|
-
* @returns The tools.
|
|
1233
|
-
*/
|
|
1234
|
-
async getTools(params) {
|
|
1235
|
-
const tools = await this.client?.tools.list(params);
|
|
1236
|
-
return tools?.items.map((tool) => this._wrapTool(tool)) ?? [];
|
|
1237
|
-
}
|
|
1238
|
-
/**
|
|
1239
|
-
* Execute a tool call.
|
|
1240
|
-
* @param {OpenAI.ChatCompletionMessageToolCall} tool - The tool to execute.
|
|
1241
|
-
* @param {string} userId - The user id.
|
|
1242
|
-
* @returns {Promise<string>} The result of the tool call.
|
|
1243
|
-
*/
|
|
1244
|
-
async executeToolCall(tool, userId) {
|
|
1245
|
-
if (!this.client) {
|
|
1246
|
-
throw new Error("Client not set");
|
|
1247
|
-
}
|
|
1248
|
-
const toolSchema = await this.client.tools.get(tool.function.name);
|
|
1249
|
-
const appName = toolSchema?.toolkit?.name.toLowerCase();
|
|
1250
|
-
const connectedAccountId = this.client.getConnectedAccountId(appName);
|
|
1251
|
-
const payload = {
|
|
1252
|
-
entity_id: userId ?? this.client.userId,
|
|
1253
|
-
connected_account_id: connectedAccountId,
|
|
1254
|
-
arguments: JSON.parse(tool.function.arguments)
|
|
1255
|
-
};
|
|
1256
|
-
const results = await this.client.tools.execute(toolSchema.slug, payload);
|
|
1257
|
-
return JSON.stringify(results);
|
|
1258
|
-
}
|
|
1259
|
-
/**
|
|
1260
|
-
* Handle the tool call from the assistant.
|
|
1261
|
-
*
|
|
1262
|
-
* @param {OpenAI.ChatCompletion} chatCompletion - The chat completion object.
|
|
1263
|
-
* @param {string} userId - The user id.
|
|
1264
|
-
* @returns {Promise<string[]>} The results of the tool call.
|
|
1265
|
-
*/
|
|
1266
|
-
async handleToolCall(chatCompletion, userId) {
|
|
1267
|
-
const outputs = [];
|
|
1268
|
-
for (const message of chatCompletion.choices) {
|
|
1269
|
-
if (message.message.tool_calls) {
|
|
1270
|
-
outputs.push(
|
|
1271
|
-
await this.executeToolCall(message.message.tool_calls[0], userId)
|
|
1272
|
-
);
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
return outputs;
|
|
1276
|
-
}
|
|
1277
|
-
/**
|
|
1278
|
-
* Handles all the tool calls from the assistant.
|
|
1279
|
-
*
|
|
1280
|
-
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1281
|
-
* @param {string} userId - The user id.
|
|
1282
|
-
* @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.
|
|
1283
|
-
*/
|
|
1284
|
-
async handleAssistantMessage(run, userId) {
|
|
1285
|
-
const tool_calls = run.required_action?.submit_tool_outputs?.tool_calls || [];
|
|
1286
|
-
const tool_outputs = await Promise.all(
|
|
1287
|
-
tool_calls.map(async (tool_call) => {
|
|
1288
|
-
logger_default.debug(`Executing tool call: ${tool_call.id}`);
|
|
1289
|
-
const tool_response = await this.executeToolCall(
|
|
1290
|
-
tool_call,
|
|
1291
|
-
userId
|
|
1292
|
-
);
|
|
1293
|
-
logger_default.debug(`Tool call ${tool_call.id} executed with response: ${tool_response}`);
|
|
1294
|
-
return {
|
|
1295
|
-
tool_call_id: tool_call.id,
|
|
1296
|
-
output: JSON.stringify(tool_response)
|
|
1297
|
-
};
|
|
1298
|
-
})
|
|
1299
|
-
);
|
|
1300
|
-
return tool_outputs;
|
|
1301
|
-
}
|
|
1302
|
-
/**
|
|
1303
|
-
* Waits for the assistant stream and handles the tool calls.
|
|
1304
|
-
*
|
|
1305
|
-
* @param {OpenAI} client - The OpenAI client.
|
|
1306
|
-
* @param {Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>} runStream - The run stream.
|
|
1307
|
-
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1308
|
-
* @param {string} userId - The user id.
|
|
1309
|
-
* @returns {AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>} The run stream.
|
|
1310
|
-
*/
|
|
1311
|
-
async *waitAndHandleAssistantStreamToolCalls(client, runStream, thread, userId) {
|
|
1312
|
-
const defaultUserId = this.client?.userId;
|
|
1313
|
-
let runId = null;
|
|
1314
|
-
for await (const event of runStream) {
|
|
1315
|
-
yield event;
|
|
1316
|
-
if (event.event === "thread.run.created") {
|
|
1317
|
-
const { id } = event.data;
|
|
1318
|
-
runId = id;
|
|
1319
|
-
}
|
|
1320
|
-
if (!runId) {
|
|
1321
|
-
continue;
|
|
1322
|
-
}
|
|
1323
|
-
if (event.event === "thread.run.requires_action") {
|
|
1324
|
-
const toolOutputs = await this.handleAssistantMessage(
|
|
1325
|
-
event.data,
|
|
1326
|
-
userId ?? defaultUserId
|
|
1327
|
-
);
|
|
1328
|
-
await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
|
|
1329
|
-
tool_outputs: toolOutputs
|
|
1330
|
-
});
|
|
1331
|
-
}
|
|
1332
|
-
if ([
|
|
1333
|
-
"thread.run.completed",
|
|
1334
|
-
"thread.run.failed",
|
|
1335
|
-
"thread.run.cancelled",
|
|
1336
|
-
"thread.run.expired"
|
|
1337
|
-
].includes(event.event)) {
|
|
1338
|
-
break;
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
if (!runId) {
|
|
1342
|
-
throw new Error("No run ID found");
|
|
1343
|
-
}
|
|
1344
|
-
let finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1345
|
-
while (["queued", "in_progress", "requires_action"].includes(finalRun.status)) {
|
|
1346
|
-
if (finalRun.status === "requires_action") {
|
|
1347
|
-
const toolOutputs = await this.handleAssistantMessage(
|
|
1348
|
-
finalRun,
|
|
1349
|
-
userId ?? defaultUserId
|
|
1350
|
-
);
|
|
1351
|
-
finalRun = await client.beta.threads.runs.submitToolOutputs(
|
|
1352
|
-
thread.id,
|
|
1353
|
-
runId,
|
|
1354
|
-
{
|
|
1355
|
-
tool_outputs: toolOutputs
|
|
1356
|
-
}
|
|
1357
|
-
);
|
|
1358
|
-
} else {
|
|
1359
|
-
finalRun = await client.beta.threads.runs.retrieve(thread.id, runId);
|
|
1360
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
}
|
|
1364
|
-
/**
|
|
1365
|
-
* Waits for the assistant tool calls and handles them.
|
|
1366
|
-
*
|
|
1367
|
-
* @param {OpenAI} client - The OpenAI client.
|
|
1368
|
-
* @param {OpenAI.Beta.Threads.Run} run - The run object.
|
|
1369
|
-
* @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
|
|
1370
|
-
* @param {string} userId - The user id.
|
|
1371
|
-
* @returns {Promise<OpenAI.Beta.Threads.Run>} The run object.
|
|
1372
|
-
*/
|
|
1373
|
-
async waitAndHandleAssistantToolCalls(client, run, thread, userId) {
|
|
1374
|
-
const defaultUserId = this.client?.userId;
|
|
1375
|
-
while (["queued", "in_progress", "requires_action"].includes(run.status)) {
|
|
1376
|
-
const tool_outputs = await this.handleAssistantMessage(
|
|
1377
|
-
run,
|
|
1378
|
-
userId ?? defaultUserId
|
|
1379
|
-
);
|
|
1380
|
-
if (run.status === "requires_action") {
|
|
1381
|
-
run = await client.beta.threads.runs.submitToolOutputs(
|
|
1382
|
-
thread.id,
|
|
1383
|
-
run.id,
|
|
1384
|
-
{
|
|
1385
|
-
tool_outputs
|
|
1386
|
-
}
|
|
1387
|
-
);
|
|
1388
|
-
} else {
|
|
1389
|
-
run = await client.beta.threads.runs.retrieve(thread.id, run.id);
|
|
1390
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
return run;
|
|
1394
|
-
}
|
|
1395
|
-
};
|
|
1396
2104
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1397
2105
|
0 && (module.exports = {
|
|
1398
|
-
|
|
2106
|
+
BaseAgenticToolset,
|
|
2107
|
+
BaseNonAgenticToolset,
|
|
1399
2108
|
BaseTelemetryTransport,
|
|
1400
2109
|
Composio,
|
|
1401
2110
|
ComposioToolset,
|
|
1402
2111
|
OpenAIToolset,
|
|
2112
|
+
ToolListParamsSchema,
|
|
1403
2113
|
jsonSchemaToModel
|
|
1404
2114
|
});
|