@composio/core 0.18.1 → 0.20.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/{CustomTool-VRfWzMix.d.mts → CustomTool-B3m-188G.d.mts} +1 -1
- package/dist/{CustomTool-Bz0f52QR.mjs → CustomTool-fYEFjSLM.mjs} +1 -1
- package/dist/{FileModifierErrors-CwB-Inm9.mjs → FileModifierErrors-CdGSABoq.mjs} +1 -1
- package/dist/{FileToolModifier.utils.neutral-D_YkHwZP.mjs → FileToolModifier.utils.neutral-DWFpnsj2.mjs} +4 -2
- package/dist/{SsrfErrors-xhdVE4yw.mjs → SsrfErrors-DiQldIdC.mjs} +32 -17
- package/dist/{Telemetry-Dw9W5RSp.mjs → Telemetry-JRJJYS4I.mjs} +2 -2
- package/dist/{cancellation-P5YUlcfz.mjs → cancellation-Dwa9lGHA.mjs} +7 -5
- package/dist/{composio-Cb3d1jFe.d.mts → composio-B36OXii1.d.mts} +3017 -144
- package/dist/{customTool.types-B6ej_8XZ.d.mts → customTool.types-C436M_D0.d.mts} +525 -101
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +10 -10
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +8 -15
- package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +101 -18
- package/dist/docs/reference/sdk-reference/typescript/custom-toolkits.mdx +123 -0
- package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +9 -2
- package/dist/docs/reference/sdk-reference/typescript/index.mdx +7 -0
- package/dist/docs/reference/sdk-reference/typescript/keyring.mdx +56 -0
- package/dist/docs/reference/sdk-reference/typescript/logs.mdx +78 -0
- package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +15 -15
- package/dist/docs/reference/sdk-reference/typescript/meta.json +8 -1
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +10 -10
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +117 -24
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +24 -10
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +151 -14
- package/dist/docs/reference/sdk-reference/typescript/tools.mdx +50 -40
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +26 -25
- package/dist/docs/reference/sdk-reference/typescript/usage.mdx +78 -0
- package/dist/docs/reference/sdk-reference/typescript/webhook-endpoints.mdx +162 -0
- package/dist/docs/reference/sdk-reference/typescript/webhook-subscriptions.mdx +218 -0
- package/dist/docs/reference/sdk-reference/typescript/webhooks.mdx +20 -0
- package/dist/{errors-Ue9wMrPK.mjs → errors-D_A4b2c2.mjs} +143 -4
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-KE5Fa-af.mjs → fileUtils.node-DNLhLg0W.mjs} +8 -6
- package/dist/index.d.mts +418 -146
- package/dist/index.mjs +2728 -349
- package/dist/models/Files.node.d.mts +2 -3
- package/dist/models/Files.node.mjs +2 -2
- package/dist/models/Files.workerd.d.mts +2 -3
- package/dist/platform/node.d.mts +2 -3
- package/dist/platform/workerd.d.mts +2 -3
- package/dist/{sensitiveFileUploadPaths-QN7w4n8v.mjs → sensitiveFileUploadPaths-BlLp-Yo2.mjs} +10 -7
- package/dist/{ssrfGuard.node-BLX6b-Rf.mjs → ssrfGuard.node-BZ270bcu.mjs} +104 -14
- package/dist/{types-CGdgK442.d.mts → types-Br3GBya5.d.mts} +2 -2
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +3 -4
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.node.mjs +6 -6
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.workerd.mjs +1 -1
- package/dist/utils/ssrfGuard.node.d.mts +19 -10
- package/dist/utils/ssrfGuard.node.mjs +1 -1
- package/dist/utils/ssrfGuard.workerd.d.mts +12 -4
- package/dist/utils/ssrfGuard.workerd.mjs +2 -2
- package/package.json +9 -9
- package/src/composio.ts +207 -10
- package/src/errors/ConnectedAccountsErrors.ts +47 -0
- package/src/errors/SDKErrors.ts +52 -0
- package/src/errors/ToolErrors.ts +24 -0
- package/src/errors/ToolRouterErrors.ts +57 -0
- package/src/errors/index.ts +3 -0
- package/src/index.ts +15 -0
- package/src/lib/toolRouterMcp.ts +132 -0
- package/src/lib/toolRouterParams.ts +116 -29
- package/src/models/ConnectedAccounts.ts +118 -2
- package/src/models/CustomToolkits.ts +169 -0
- package/src/models/Experimental.ts +27 -0
- package/src/models/Keyring.ts +65 -0
- package/src/models/Logs.ts +101 -0
- package/src/models/MCP.ts +29 -15
- package/src/models/ToolRouter.ts +43 -14
- package/src/models/ToolRouterSession.ts +224 -16
- package/src/models/Toolkits.ts +181 -2
- package/src/models/Tools.ts +58 -26
- package/src/models/Triggers.ts +10 -90
- package/src/models/Usage.ts +119 -0
- package/src/models/Webhooks.ts +520 -0
- package/src/services/pusher/Pusher.ts +46 -16
- package/src/types/authConfigs.types.ts +2 -0
- package/src/types/connectedAccounts.types.ts +54 -0
- package/src/types/customToolkits.types.ts +102 -0
- package/src/types/keyring.types.ts +32 -0
- package/src/types/logs.types.ts +108 -0
- package/src/types/toolRouter.types.ts +259 -14
- package/src/types/toolkit.types.ts +161 -0
- package/src/types/usage.types.ts +81 -0
- package/src/types/webhooks.types.ts +143 -0
- package/src/utils/constants.ts +1 -5
- package/src/utils/jsonSchema.ts +4 -1
- package/src/utils/logger.ts +39 -7
- package/src/utils/sdk.ts +385 -22
- package/src/utils/ssrfGuard.node.ts +127 -13
- package/src/utils/ssrfGuard.workerd.ts +11 -1
- package/src/utils/transformers/connectedAccounts.ts +37 -2
- package/src/utils/transformers/customToolkits.ts +98 -0
- package/src/utils/transformers/keyring.ts +24 -0
- package/src/utils/transformers/logs.ts +85 -0
- package/src/utils/transformers/toolkits.ts +183 -3
- package/src/utils/transformers/triggers.ts +2 -2
- package/src/utils/transformers/usage.ts +77 -0
- package/src/utils/transformers/webhooks.ts +139 -0
package/src/models/Tools.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import ComposioClient from '@composio/client';
|
|
1
|
+
import ComposioClient, { APIError } from '@composio/client';
|
|
2
2
|
import { FileToolModifier } from '#file_tool_modifier';
|
|
3
3
|
import {
|
|
4
4
|
Tool,
|
|
@@ -42,6 +42,7 @@ import logger from '../utils/logger';
|
|
|
42
42
|
import { ExecuteToolFn, GlobalExecuteToolFn } from '../types/provider.types';
|
|
43
43
|
import {
|
|
44
44
|
ComposioInvalidModifierError,
|
|
45
|
+
ComposioToolFetchError,
|
|
45
46
|
ComposioToolNotFoundError,
|
|
46
47
|
ComposioProviderNotDefinedError,
|
|
47
48
|
ComposioToolVersionRequiredError,
|
|
@@ -540,7 +541,9 @@ export class Tools<
|
|
|
540
541
|
...(limit ? { limit } : {}),
|
|
541
542
|
...('tags' in queryParams.data ? { tags: queryParams.data.tags } : {}),
|
|
542
543
|
...('scopes' in queryParams.data ? { scopes: queryParams.data.scopes } : {}),
|
|
543
|
-
|
|
544
|
+
// `search` is the SDK's public option; the API deprecated the `search`
|
|
545
|
+
// wire param in favour of `query`, so send the replacement.
|
|
546
|
+
...('search' in queryParams.data ? { query: queryParams.data.search } : {}),
|
|
544
547
|
...('authConfigIds' in queryParams.data
|
|
545
548
|
? { auth_config_ids: queryParams.data.authConfigIds }
|
|
546
549
|
: {}),
|
|
@@ -710,7 +713,17 @@ export class Tools<
|
|
|
710
713
|
if (error instanceof ComposioRequestCancelledError) {
|
|
711
714
|
throw error;
|
|
712
715
|
}
|
|
713
|
-
|
|
716
|
+
// The tools endpoint reports an unknown slug as 404 (or 400 for a
|
|
717
|
+
// malformed one). Anything else (401, 5xx, network) is not "not found",
|
|
718
|
+
// so keep the client error reachable as `cause` under a generic error.
|
|
719
|
+
if (error instanceof APIError && (error.status === 404 || error.status === 400)) {
|
|
720
|
+
throw new ComposioToolNotFoundError(`Tool with slug ${slug} not found`, {
|
|
721
|
+
meta: { slug },
|
|
722
|
+
cause: error,
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
throw new ComposioToolFetchError(`Unable to retrieve tool with slug ${slug}`, {
|
|
726
|
+
meta: { slug },
|
|
714
727
|
cause: error,
|
|
715
728
|
});
|
|
716
729
|
}
|
|
@@ -1097,6 +1110,10 @@ export class Tools<
|
|
|
1097
1110
|
*
|
|
1098
1111
|
* @example Execute with a specific version (recommended for production)
|
|
1099
1112
|
* ```typescript
|
|
1113
|
+
* // Look up the tool's current version once, then pin that string in your code or config
|
|
1114
|
+
* const { version } = await composio.tools.getRawComposioToolBySlug('GITHUB_GET_REPOS');
|
|
1115
|
+
* console.log(version); // e.g. '20250909_00'
|
|
1116
|
+
*
|
|
1100
1117
|
* const result = await composio.tools.execute('GITHUB_GET_REPOS', {
|
|
1101
1118
|
* userId: 'default',
|
|
1102
1119
|
* version: '20250909_00',
|
|
@@ -1283,21 +1300,27 @@ export class Tools<
|
|
|
1283
1300
|
}
|
|
1284
1301
|
|
|
1285
1302
|
/**
|
|
1286
|
-
*
|
|
1303
|
+
* Generates arguments for a tool from a natural-language description of the task.
|
|
1287
1304
|
*
|
|
1288
|
-
*
|
|
1305
|
+
* Composio uses an LLM to fill the tool's input parameters from `text`. Review the
|
|
1306
|
+
* generated arguments before passing them to `tools.execute()`.
|
|
1289
1307
|
*
|
|
1290
|
-
* @param {string} slug - The
|
|
1291
|
-
* @param {ToolGetInputParams} body - The
|
|
1292
|
-
* @
|
|
1308
|
+
* @param {string} slug - The slug of the tool to generate arguments for
|
|
1309
|
+
* @param {ToolGetInputParams} body - The generation request
|
|
1310
|
+
* @param {string} body.text - What you want the tool to do, in natural language
|
|
1311
|
+
* @param {string} [body.custom_description] - Extra context about the tool for the LLM
|
|
1312
|
+
* @param {string} [body.system_prompt] - System prompt that steers the LLM
|
|
1313
|
+
* @param {string} [body.version] - Tool version to generate arguments for
|
|
1314
|
+
* @returns {Promise<ToolGetInputResponse>} The generated `arguments`, or an `error` when generation fails
|
|
1293
1315
|
*
|
|
1294
1316
|
* @example
|
|
1295
1317
|
* ```typescript
|
|
1296
|
-
*
|
|
1297
|
-
*
|
|
1298
|
-
*
|
|
1318
|
+
* const { arguments: args, error } = await composio.tools.getInput('GITHUB_CREATE_ISSUE', {
|
|
1319
|
+
* text: 'Open an issue in composiohq/composio titled "Docs typo" describing the broken link',
|
|
1320
|
+
* version: '20250909_00',
|
|
1299
1321
|
* });
|
|
1300
|
-
*
|
|
1322
|
+
* if (error) throw new Error(error);
|
|
1323
|
+
* console.log(args); // { owner: 'composiohq', repo: 'composio', title: 'Docs typo', ... }
|
|
1301
1324
|
* ```
|
|
1302
1325
|
*/
|
|
1303
1326
|
async getInput(
|
|
@@ -1312,26 +1335,35 @@ export class Tools<
|
|
|
1312
1335
|
}
|
|
1313
1336
|
|
|
1314
1337
|
/**
|
|
1315
|
-
*
|
|
1338
|
+
* Sends an HTTP request to a toolkit's API, authenticated as a connected account.
|
|
1316
1339
|
*
|
|
1317
|
-
*
|
|
1318
|
-
*
|
|
1340
|
+
* Use it to call an endpoint that no predefined tool covers. Composio injects the
|
|
1341
|
+
* connected account's credentials on the server side.
|
|
1319
1342
|
*
|
|
1320
|
-
*
|
|
1321
|
-
*
|
|
1343
|
+
* A relative `endpoint` is appended to the toolkit's base URL, and that base URL can
|
|
1344
|
+
* already include a path. Google Calendar's base URL is
|
|
1345
|
+
* `https://www.googleapis.com/calendar/v3`, so pass `/users/me/calendarList`, not
|
|
1346
|
+
* `/calendar/v3/users/me/calendarList` (which resolves to `/calendar/v3/calendar/v3/...`
|
|
1347
|
+
* and returns a 404 from Google). An absolute URL on the same domain is sent as-is.
|
|
1348
|
+
*
|
|
1349
|
+
* @param {ToolProxyParams} body - The proxy request
|
|
1350
|
+
* @param {string} body.endpoint - Path relative to the toolkit's base URL, or an absolute URL
|
|
1351
|
+
* @param {'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'} body.method - HTTP method
|
|
1352
|
+
* @param {string} [body.connectedAccountId] - The connected account to authenticate as
|
|
1353
|
+
* @param {unknown} [body.body] - JSON request body
|
|
1354
|
+
* @param {Array<{ in: 'query' | 'header'; name: string; value: string | number }>} [body.parameters] - Extra query parameters or headers
|
|
1355
|
+
* @returns {Promise<ToolProxyResponse>} The upstream status, headers, and parsed body
|
|
1322
1356
|
*
|
|
1323
1357
|
* @example
|
|
1324
1358
|
* ```typescript
|
|
1325
|
-
* //
|
|
1326
|
-
* const
|
|
1327
|
-
*
|
|
1328
|
-
*
|
|
1329
|
-
*
|
|
1330
|
-
*
|
|
1331
|
-
* method: 'GET'
|
|
1332
|
-
* }
|
|
1359
|
+
* // Google Calendar's base URL is https://www.googleapis.com/calendar/v3
|
|
1360
|
+
* const { status, data } = await composio.tools.proxyExecute({
|
|
1361
|
+
* endpoint: '/users/me/calendarList',
|
|
1362
|
+
* method: 'GET',
|
|
1363
|
+
* connectedAccountId: 'ca_...',
|
|
1364
|
+
* parameters: [{ in: 'query', name: 'maxResults', value: 10 }],
|
|
1333
1365
|
* });
|
|
1334
|
-
* console.log(
|
|
1366
|
+
* console.log(status, data);
|
|
1335
1367
|
* ```
|
|
1336
1368
|
*/
|
|
1337
1369
|
async proxyExecute(
|
package/src/models/Triggers.ts
CHANGED
|
@@ -33,11 +33,10 @@ import {
|
|
|
33
33
|
WebhookTriggerPayloadV3Schema,
|
|
34
34
|
WebhookVersion,
|
|
35
35
|
WebhookVersions,
|
|
36
|
-
DefaultWebhookSubscriptionEvents,
|
|
37
36
|
SetWebhookSubscriptionParams,
|
|
38
|
-
SetWebhookSubscriptionParamsSchema,
|
|
39
37
|
WebhookSubscription,
|
|
40
38
|
} from '../types/triggers.types';
|
|
39
|
+
import { upsertWebhookSubscription } from './Webhooks';
|
|
41
40
|
import logger from '../utils/logger';
|
|
42
41
|
import { telemetry } from '../telemetry/Telemetry';
|
|
43
42
|
import { ValidationError } from '../errors';
|
|
@@ -75,63 +74,6 @@ const toStringOrDefault = (value: unknown, defaultValue: string): string => {
|
|
|
75
74
|
return str.length > 0 ? str : defaultValue;
|
|
76
75
|
};
|
|
77
76
|
|
|
78
|
-
const WEBHOOK_SUBSCRIPTIONS_PATH = '/api/v3.1/webhook_subscriptions';
|
|
79
|
-
|
|
80
|
-
type RawWebhookSubscription = Record<string, unknown> & {
|
|
81
|
-
id?: unknown;
|
|
82
|
-
webhook_url?: unknown;
|
|
83
|
-
webhookUrl?: unknown;
|
|
84
|
-
version?: unknown;
|
|
85
|
-
enabled_events?: unknown;
|
|
86
|
-
enabledEvents?: unknown;
|
|
87
|
-
secret?: unknown;
|
|
88
|
-
created_at?: unknown;
|
|
89
|
-
createdAt?: unknown;
|
|
90
|
-
updated_at?: unknown;
|
|
91
|
-
updatedAt?: unknown;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
type RawWebhookSubscriptionListResponse = Record<string, unknown> & {
|
|
95
|
-
items?: unknown;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const asRecord = (value: unknown): Record<string, unknown> =>
|
|
99
|
-
value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
100
|
-
? (value as Record<string, unknown>)
|
|
101
|
-
: {};
|
|
102
|
-
|
|
103
|
-
const firstString = (value: unknown): string | undefined =>
|
|
104
|
-
typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
105
|
-
|
|
106
|
-
const stringArray = (value: unknown): string[] =>
|
|
107
|
-
Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : [];
|
|
108
|
-
|
|
109
|
-
const transformWebhookSubscription = (subscription: unknown): WebhookSubscription => {
|
|
110
|
-
const raw = asRecord(subscription) as RawWebhookSubscription;
|
|
111
|
-
|
|
112
|
-
// Map to camelCase explicitly — do NOT spread `...raw`, or the response's
|
|
113
|
-
// snake_case keys (webhook_url, enabled_events, created_at, ...) leak into the
|
|
114
|
-
// public object alongside their camelCase counterparts.
|
|
115
|
-
return {
|
|
116
|
-
id: firstString(raw.id) ?? '',
|
|
117
|
-
webhookUrl: firstString(raw.webhook_url) ?? firstString(raw.webhookUrl) ?? '',
|
|
118
|
-
version: (firstString(raw.version) ?? WebhookVersions.V3) as WebhookVersion,
|
|
119
|
-
enabledEvents: stringArray(raw.enabled_events).length
|
|
120
|
-
? stringArray(raw.enabled_events)
|
|
121
|
-
: stringArray(raw.enabledEvents),
|
|
122
|
-
secret: firstString(raw.secret),
|
|
123
|
-
createdAt: firstString(raw.created_at) ?? firstString(raw.createdAt),
|
|
124
|
-
updatedAt: firstString(raw.updated_at) ?? firstString(raw.updatedAt),
|
|
125
|
-
};
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const firstWebhookSubscriptionId = (
|
|
129
|
-
response: RawWebhookSubscriptionListResponse
|
|
130
|
-
): string | undefined => {
|
|
131
|
-
const firstItem = Array.isArray(response.items) ? response.items[0] : undefined;
|
|
132
|
-
return firstString(asRecord(firstItem).id);
|
|
133
|
-
};
|
|
134
|
-
|
|
135
77
|
/**
|
|
136
78
|
* The signature header names Composio sends with every webhook delivery.
|
|
137
79
|
* @private
|
|
@@ -215,7 +157,7 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
215
157
|
|
|
216
158
|
constructor(client: ComposioClient, config?: ComposioConfig<TProvider>) {
|
|
217
159
|
this.client = client;
|
|
218
|
-
this.pusherService = new PusherService(client);
|
|
160
|
+
this.pusherService = new PusherService(client, { defaultHeaders: config?.defaultHeaders });
|
|
219
161
|
this.toolkitVersions = config?.toolkitVersions ?? CONFIG_DEFAULTS.toolkitVersions;
|
|
220
162
|
telemetry.instrument(this, 'Triggers');
|
|
221
163
|
}
|
|
@@ -234,34 +176,7 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
234
176
|
* ```
|
|
235
177
|
*/
|
|
236
178
|
async setWebhookSubscription(params: SetWebhookSubscriptionParams): Promise<WebhookSubscription> {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (!parsedParams.success) {
|
|
240
|
-
throw new ValidationError(`Invalid parameters passed to set webhook subscription`, {
|
|
241
|
-
cause: parsedParams.error,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const body = {
|
|
246
|
-
webhook_url: parsedParams.data.webhookUrl,
|
|
247
|
-
enabled_events: parsedParams.data.enabledEvents ?? [...DefaultWebhookSubscriptionEvents],
|
|
248
|
-
version: parsedParams.data.version ?? WebhookVersions.V3,
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
const existing = await this.client.get<RawWebhookSubscriptionListResponse>(
|
|
252
|
-
WEBHOOK_SUBSCRIPTIONS_PATH,
|
|
253
|
-
{ query: { limit: 1 } }
|
|
254
|
-
);
|
|
255
|
-
const subscriptionId = firstWebhookSubscriptionId(existing);
|
|
256
|
-
|
|
257
|
-
const subscription = subscriptionId
|
|
258
|
-
? await this.client.patch<RawWebhookSubscription>(
|
|
259
|
-
`${WEBHOOK_SUBSCRIPTIONS_PATH}/${encodeURIComponent(subscriptionId)}`,
|
|
260
|
-
{ body }
|
|
261
|
-
)
|
|
262
|
-
: await this.client.post<RawWebhookSubscription>(WEBHOOK_SUBSCRIPTIONS_PATH, { body });
|
|
263
|
-
|
|
264
|
-
return transformWebhookSubscription(subscription);
|
|
179
|
+
return upsertWebhookSubscription(this.client, params);
|
|
265
180
|
}
|
|
266
181
|
|
|
267
182
|
/**
|
|
@@ -612,6 +527,10 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
612
527
|
*
|
|
613
528
|
* @param fn - The function to call when a trigger is received
|
|
614
529
|
* @param filters - The filters to apply to the triggers
|
|
530
|
+
* @param onSubscriptionError - Optional callback invoked with the raw Pusher payload when
|
|
531
|
+
* the underlying subscription fails (for example on auth or permission rejection). The
|
|
532
|
+
* subscribe promise still resolves; this is the only programmatic signal of the failure.
|
|
533
|
+
* Errors thrown from — or promises rejected by — the callback are contained and logged.
|
|
615
534
|
*
|
|
616
535
|
* @example
|
|
617
536
|
* ```ts
|
|
@@ -623,7 +542,8 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
623
542
|
*/
|
|
624
543
|
async subscribe(
|
|
625
544
|
fn: (_data: IncomingTriggerPayload) => void,
|
|
626
|
-
filters: TriggerSubscribeParams = {}
|
|
545
|
+
filters: TriggerSubscribeParams = {},
|
|
546
|
+
onSubscriptionError?: (data: Record<string, unknown>) => void
|
|
627
547
|
) {
|
|
628
548
|
if (!fn) throw new Error('Function is required for trigger subscription');
|
|
629
549
|
|
|
@@ -651,7 +571,7 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
651
571
|
} else {
|
|
652
572
|
logger.debug('Trigger does not match filters', JSON.stringify(parsedFilters.data, null, 2));
|
|
653
573
|
}
|
|
654
|
-
});
|
|
574
|
+
}, onSubscriptionError);
|
|
655
575
|
}
|
|
656
576
|
|
|
657
577
|
/**
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `composio.experimental.usage`: project-scoped metering.
|
|
3
|
+
* **Experimental — shape may change in future releases.**
|
|
4
|
+
*
|
|
5
|
+
* @module Usage
|
|
6
|
+
*/
|
|
7
|
+
import ComposioClient from '@composio/client';
|
|
8
|
+
import {
|
|
9
|
+
UsageBreakdownParams,
|
|
10
|
+
UsageBreakdownParamsSchema,
|
|
11
|
+
UsageBreakdownResponse,
|
|
12
|
+
UsageSummaryParams,
|
|
13
|
+
UsageSummaryParamsSchema,
|
|
14
|
+
UsageSummaryResponse,
|
|
15
|
+
} from '../types/usage.types';
|
|
16
|
+
import { ComposioRequestOptions } from '../types/requestOptions.types';
|
|
17
|
+
import { ValidationError } from '../errors/ValidationErrors';
|
|
18
|
+
import { telemetry } from '../telemetry/Telemetry';
|
|
19
|
+
import { withCancellation } from '../utils/cancellation';
|
|
20
|
+
import {
|
|
21
|
+
transformUsageBreakdownParams,
|
|
22
|
+
transformUsageBreakdownResponse,
|
|
23
|
+
transformUsageSummaryParams,
|
|
24
|
+
transformUsageSummaryResponse,
|
|
25
|
+
} from '../utils/transformers/usage';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* `composio.experimental.usage` — metering for the project the API key
|
|
29
|
+
* belongs to. **Experimental — shape may change in future releases.**
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const summary = await composio.experimental.usage.summary();
|
|
34
|
+
* console.log(summary.entities.tool_calls?.totalQuantity);
|
|
35
|
+
*
|
|
36
|
+
* const byTool = await composio.experimental.usage.breakdown('tool_calls', {
|
|
37
|
+
* groupBy: 'tool_slug',
|
|
38
|
+
* limit: 10,
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export class Usage {
|
|
43
|
+
private client: ComposioClient;
|
|
44
|
+
|
|
45
|
+
constructor(client: ComposioClient) {
|
|
46
|
+
this.client = client;
|
|
47
|
+
telemetry.instrument(this, 'Usage');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Usage totals per metering entity type for a time range.
|
|
52
|
+
* **Experimental — shape may change in future releases.**
|
|
53
|
+
*
|
|
54
|
+
* @param {UsageSummaryParams} [params] - Optional range, `entityTypes` and filters
|
|
55
|
+
* @returns {Promise<UsageSummaryResponse>} Totals keyed by entity type
|
|
56
|
+
* @throws {ValidationError} If the params fail validation
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const summary = await composio.experimental.usage.summary({
|
|
61
|
+
* from: Date.now() - 7 * 24 * 60 * 60 * 1000,
|
|
62
|
+
* filters: { userId: ['user_123'] },
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
async summary(
|
|
67
|
+
params?: UsageSummaryParams,
|
|
68
|
+
requestOptions?: ComposioRequestOptions
|
|
69
|
+
): Promise<UsageSummaryResponse> {
|
|
70
|
+
const parsedParams = UsageSummaryParamsSchema.safeParse(params ?? {});
|
|
71
|
+
if (!parsedParams.success) {
|
|
72
|
+
throw new ValidationError('Failed to parse usage summary params', {
|
|
73
|
+
cause: parsedParams.error,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const body = transformUsageSummaryParams(parsedParams.data);
|
|
77
|
+
const result = await withCancellation(
|
|
78
|
+
() => this.client.project.usage.retrieveSummary(body, requestOptions),
|
|
79
|
+
requestOptions?.signal
|
|
80
|
+
);
|
|
81
|
+
return transformUsageSummaryResponse(result);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Usage for one metering entity type, grouped by a dimension.
|
|
86
|
+
* **Experimental — shape may change in future releases.**
|
|
87
|
+
*
|
|
88
|
+
* @param {string} entityType - Entity type to break down (e.g. `tool_calls`, `sessions`)
|
|
89
|
+
* @param {UsageBreakdownParams} [params] - Optional range, `groupBy`, ordering, `limit` and filters
|
|
90
|
+
* @returns {Promise<UsageBreakdownResponse>} Totals plus per-group rows
|
|
91
|
+
* @throws {ValidationError} If the params fail validation
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const byUser = await composio.experimental.usage.breakdown('sessions', {
|
|
96
|
+
* groupBy: 'user_id',
|
|
97
|
+
* orderBy: 'event_count',
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
async breakdown(
|
|
102
|
+
entityType: string,
|
|
103
|
+
params?: UsageBreakdownParams,
|
|
104
|
+
requestOptions?: ComposioRequestOptions
|
|
105
|
+
): Promise<UsageBreakdownResponse> {
|
|
106
|
+
const parsedParams = UsageBreakdownParamsSchema.safeParse(params ?? {});
|
|
107
|
+
if (!parsedParams.success) {
|
|
108
|
+
throw new ValidationError('Failed to parse usage breakdown params', {
|
|
109
|
+
cause: parsedParams.error,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
const body = transformUsageBreakdownParams(parsedParams.data);
|
|
113
|
+
const result = await withCancellation(
|
|
114
|
+
() => this.client.project.usage.retrieve(entityType, body, requestOptions),
|
|
115
|
+
requestOptions?.signal
|
|
116
|
+
);
|
|
117
|
+
return transformUsageBreakdownResponse(result);
|
|
118
|
+
}
|
|
119
|
+
}
|