@composio/core 0.31.0 → 0.32.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-9Vlb4XaS.mjs → CustomTool-B4dQn2M5.mjs} +1 -1
- package/dist/{CustomTool-Ds5aLDth.d.mts → CustomTool-CBx9kzea.d.mts} +1 -1
- package/dist/{FileModifierErrors-WrqokuIF.mjs → FileModifierErrors-D-AKc3ds.mjs} +1 -1
- package/dist/{SsrfErrors-BSVMW915.mjs → SsrfErrors-BekZPeWY.mjs} +59 -12
- package/dist/{Telemetry-a7SOQXcg.mjs → Telemetry-BWKZmDxM.mjs} +2 -2
- package/dist/composio-CPf6NHnB.d.mts +20334 -0
- package/dist/{customTool.types-DHxXBU6E.d.mts → customTool.types-BSbIG1OG.d.mts} +291 -68
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +20 -12
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +28 -10
- package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +44 -32
- package/dist/docs/reference/sdk-reference/typescript/custom-toolkits.mdx +8 -6
- package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +4 -3
- package/dist/docs/reference/sdk-reference/typescript/index.mdx +4 -2
- package/dist/docs/reference/sdk-reference/typescript/keyring.mdx +6 -3
- package/dist/docs/reference/sdk-reference/typescript/logs.mdx +5 -3
- package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +53 -42
- package/dist/docs/reference/sdk-reference/typescript/meta.json +1 -0
- package/dist/docs/reference/sdk-reference/typescript/remote-file.mdx +5 -14
- package/dist/docs/reference/sdk-reference/typescript/session-configs.mdx +5 -3
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +7 -5
- package/dist/docs/reference/sdk-reference/typescript/session-webhooks.mdx +64 -0
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +16 -16
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +36 -8
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +14 -11
- package/dist/docs/reference/sdk-reference/typescript/tools.mdx +16 -18
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +42 -30
- package/dist/docs/reference/sdk-reference/typescript/usage.mdx +6 -4
- package/dist/docs/reference/sdk-reference/typescript/webhook-endpoints.mdx +9 -7
- package/dist/docs/reference/sdk-reference/typescript/webhook-subscriptions.mdx +13 -9
- package/dist/docs/reference/sdk-reference/typescript/webhooks.mdx +3 -1
- package/dist/{errors-xC13uvaA.mjs → errors-CtIKA9BH.mjs} +2 -2
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-CAwIbWYH.mjs → fileUtils.node-BJUYMEph.mjs} +7 -7
- package/dist/index.d.mts +6 -11
- package/dist/index.mjs +526 -233
- package/dist/{jsonSchema-Cc4EtHRw.mjs → jsonSchema-BZoXGUCr.mjs} +2 -2
- package/dist/models/Files.node.mjs +3 -3
- package/dist/{sensitiveFileUploadPaths-CSyJ3Ku8.mjs → sensitiveFileUploadPaths-CcbMAYCC.mjs} +21 -15
- package/dist/{ssrfGuard.node-CdIkwqVu.mjs → ssrfGuard.node-B749ae5Q.mjs} +1 -1
- package/dist/{types-DcdZy5Nc.d.mts → types-ns3Q_WrP.d.mts} +2 -2
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +5 -5
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- package/dist/utils/ssrfGuard.node.mjs +1 -1
- package/dist/utils/ssrfGuard.workerd.mjs +1 -1
- package/package.json +2 -2
- package/src/composio.ts +26 -7
- package/src/errors/ComposioError.ts +3 -2
- package/src/index.ts +19 -0
- package/src/lib/toolRouterParams.ts +38 -14
- package/src/models/AuthConfigs.ts +10 -3
- package/src/models/ConnectedAccounts.ts +30 -17
- package/src/models/CustomTool.ts +0 -10
- package/src/models/Keyring.ts +2 -1
- package/src/models/MCP.ts +43 -35
- package/src/models/SessionWebhooks.ts +136 -0
- package/src/models/ToolRouter.ts +39 -4
- package/src/models/ToolRouterSession.ts +2 -2
- package/src/models/ToolRouterSessionFileMount.ts +3 -1
- package/src/models/Toolkits.ts +1 -0
- package/src/models/Tools.ts +4 -8
- package/src/models/Triggers.ts +48 -213
- package/src/models/Webhooks.ts +2 -0
- package/src/provider/ComposioProvider.ts +0 -1
- package/src/telemetry/BatchProcessor.ts +1 -1
- package/src/telemetry/Telemetry.ts +1 -1
- package/src/types/connectedAccountAuthStates.types.ts +0 -1
- package/src/types/provider.types.ts +0 -1
- package/src/types/sessionWebhooks.types.ts +292 -0
- package/src/types/toolRouter.types.ts +33 -4
- package/src/types/triggers.types.ts +4 -0
- package/src/utils/crypto.ts +6 -5
- package/src/utils/env.ts +2 -5
- package/src/utils/fileUtils.node.ts +4 -3
- package/src/utils/logger.ts +19 -9
- package/src/utils/pusher.ts +5 -2
- package/src/utils/readResponseBody.ts +30 -17
- package/src/utils/transformers/authConfigs.ts +0 -1
- package/src/utils/userCallback.ts +3 -1
- package/src/utils/webhookVerification.ts +211 -0
- package/dist/composio-Ba89XGHL.d.mts +0 -9736
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { z } from 'zod/v3';
|
|
2
|
+
|
|
3
|
+
export const SessionWebhookEventTypes = {
|
|
4
|
+
BEFORE_EXECUTE: 'composio.session_hook.before_execute',
|
|
5
|
+
AFTER_EXECUTE: 'composio.session_hook.after_execute',
|
|
6
|
+
} as const;
|
|
7
|
+
|
|
8
|
+
export type SessionWebhookEventType =
|
|
9
|
+
(typeof SessionWebhookEventTypes)[keyof typeof SessionWebhookEventTypes];
|
|
10
|
+
|
|
11
|
+
const SessionWebhookMetadataSchema = z
|
|
12
|
+
.object({
|
|
13
|
+
/** ID of the project */
|
|
14
|
+
project_id: z.string(),
|
|
15
|
+
/** ID of the session the call was made in */
|
|
16
|
+
session_id: z.string(),
|
|
17
|
+
/** ID of the user the session belongs to */
|
|
18
|
+
user_id: z.string(),
|
|
19
|
+
/** ID of the hook that sent this event, as set on the session */
|
|
20
|
+
hook_id: z.string(),
|
|
21
|
+
/**
|
|
22
|
+
* ID of the call. The `before_execute` and `after_execute` events for one
|
|
23
|
+
* call carry the same ID. Omitted by an API version that predates it.
|
|
24
|
+
*/
|
|
25
|
+
tool_call_id: z.string().optional(),
|
|
26
|
+
})
|
|
27
|
+
.passthrough();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Where an event came from. `hook_id` is the hook's id on the session, and
|
|
31
|
+
* `tool_call_id` pairs a call's `before_execute` event with its `after_execute`.
|
|
32
|
+
*/
|
|
33
|
+
export type SessionWebhookMetadata = z.infer<typeof SessionWebhookMetadataSchema>;
|
|
34
|
+
|
|
35
|
+
const SessionWebhookToolCallSchema = z
|
|
36
|
+
.object({
|
|
37
|
+
/** Slug of the toolkit (for example `gmail`) */
|
|
38
|
+
toolkit_slug: z.string(),
|
|
39
|
+
/** Slug of the tool (for example `GMAIL_SEND_EMAIL`) */
|
|
40
|
+
tool_slug: z.string(),
|
|
41
|
+
/** Version of the toolkit the tool runs from */
|
|
42
|
+
toolkit_version: z.string(),
|
|
43
|
+
/** ID of the connected account the call uses. Omitted when the call doesn't use one. */
|
|
44
|
+
connected_account_id: z.string().optional(),
|
|
45
|
+
/** The arguments the tool is called with */
|
|
46
|
+
arguments: z.record(z.unknown()),
|
|
47
|
+
})
|
|
48
|
+
.passthrough();
|
|
49
|
+
|
|
50
|
+
/** A tool call. */
|
|
51
|
+
export type SessionWebhookToolCall = z.infer<typeof SessionWebhookToolCallSchema>;
|
|
52
|
+
|
|
53
|
+
const SessionWebhookToolResponseSchema = z
|
|
54
|
+
.object({
|
|
55
|
+
/** The data the tool returned */
|
|
56
|
+
data: z.record(z.unknown()),
|
|
57
|
+
/** Error message when the tool failed, otherwise null */
|
|
58
|
+
error: z.string().nullable(),
|
|
59
|
+
})
|
|
60
|
+
.passthrough();
|
|
61
|
+
|
|
62
|
+
/** What a tool call returned. */
|
|
63
|
+
export type SessionWebhookToolResponse = z.infer<typeof SessionWebhookToolResponseSchema>;
|
|
64
|
+
|
|
65
|
+
const SessionWebhookHttpMethodSchema = z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']);
|
|
66
|
+
|
|
67
|
+
const SessionWebhookHttpParameterSchema = z
|
|
68
|
+
.object({
|
|
69
|
+
/** Header or query parameter name */
|
|
70
|
+
name: z.string(),
|
|
71
|
+
/** Header or query parameter value */
|
|
72
|
+
value: z.string(),
|
|
73
|
+
/** Whether the parameter is a header or a query parameter */
|
|
74
|
+
type: z.enum(['header', 'query']),
|
|
75
|
+
})
|
|
76
|
+
.passthrough();
|
|
77
|
+
|
|
78
|
+
const BinaryBodyUrlSchema = z.object({
|
|
79
|
+
url: z.string(),
|
|
80
|
+
content_type: z.string().optional(),
|
|
81
|
+
base64: z.never().optional(),
|
|
82
|
+
});
|
|
83
|
+
const BinaryBodyBase64Schema = z.object({
|
|
84
|
+
base64: z.string(),
|
|
85
|
+
content_type: z.string().optional(),
|
|
86
|
+
url: z.never().optional(),
|
|
87
|
+
});
|
|
88
|
+
const BinaryBodySchema = z.union([
|
|
89
|
+
BinaryBodyUrlSchema.passthrough(),
|
|
90
|
+
BinaryBodyBase64Schema.passthrough(),
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
const SessionWebhookHttpRequestSchema = z
|
|
94
|
+
.object({
|
|
95
|
+
/** Slug of the toolkit whose API is called (for example `github`) */
|
|
96
|
+
toolkit_slug: z.string(),
|
|
97
|
+
/** ID of the connected account the call uses. Omitted when the call doesn't use one. */
|
|
98
|
+
connected_account_id: z.string().optional(),
|
|
99
|
+
method: SessionWebhookHttpMethodSchema,
|
|
100
|
+
/** The endpoint as the agent gave it: a path relative to the toolkit's API base URL, or a full URL */
|
|
101
|
+
endpoint: z.string(),
|
|
102
|
+
/** Headers and query parameters the agent added. The credentials Composio adds are not included. */
|
|
103
|
+
parameters: z.array(SessionWebhookHttpParameterSchema),
|
|
104
|
+
/** The request body. Omitted when there is none or the request uploads a file. */
|
|
105
|
+
body: z.unknown().optional(),
|
|
106
|
+
/** The file the request uploads, as a URL or base64-encoded bytes. */
|
|
107
|
+
binary_body: BinaryBodySchema.optional(),
|
|
108
|
+
})
|
|
109
|
+
.passthrough();
|
|
110
|
+
|
|
111
|
+
/** A proxy call: an HTTP request to a toolkit's API. */
|
|
112
|
+
export type SessionWebhookHttpRequest = z.infer<typeof SessionWebhookHttpRequestSchema>;
|
|
113
|
+
|
|
114
|
+
const SessionWebhookHttpResponseSchema = z
|
|
115
|
+
.object({
|
|
116
|
+
/** HTTP status the app's API returned */
|
|
117
|
+
status: z.number(),
|
|
118
|
+
/** Headers the app's API returned. Credential values Composio added are redacted. */
|
|
119
|
+
headers: z.record(z.string()),
|
|
120
|
+
/** Response body the app's API returned. Omitted when it is binary. */
|
|
121
|
+
data: z.unknown().optional(),
|
|
122
|
+
/** The file the API returned, as a temporary download link. Present when the response is binary. */
|
|
123
|
+
binary_data: z
|
|
124
|
+
.object({
|
|
125
|
+
url: z.string(),
|
|
126
|
+
content_type: z.string(),
|
|
127
|
+
size: z.number(),
|
|
128
|
+
expires_at: z.string().optional(),
|
|
129
|
+
})
|
|
130
|
+
.passthrough()
|
|
131
|
+
.optional(),
|
|
132
|
+
})
|
|
133
|
+
.passthrough();
|
|
134
|
+
|
|
135
|
+
/** What a proxy call returned. */
|
|
136
|
+
export type SessionWebhookHttpResponse = z.infer<typeof SessionWebhookHttpResponseSchema>;
|
|
137
|
+
|
|
138
|
+
const sessionWebhookEnvelopeFields = {
|
|
139
|
+
/** Unique event ID (`msg_<uuid>`). Also sent as the `webhook-id` header. */
|
|
140
|
+
id: z.string(),
|
|
141
|
+
/** ISO-8601 timestamp of when the event was created */
|
|
142
|
+
timestamp: z.string(),
|
|
143
|
+
metadata: SessionWebhookMetadataSchema,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Sent before a call runs. `data.type` says whether it is a tool call
|
|
148
|
+
* (`tool_call`) or a proxy call (`http_request`).
|
|
149
|
+
*/
|
|
150
|
+
const SessionWebhookBeforeExecuteEventSchema = z
|
|
151
|
+
.object({
|
|
152
|
+
...sessionWebhookEnvelopeFields,
|
|
153
|
+
type: z.literal(SessionWebhookEventTypes.BEFORE_EXECUTE),
|
|
154
|
+
data: z.discriminatedUnion('type', [
|
|
155
|
+
z
|
|
156
|
+
.object({ type: z.literal('tool_execute'), tool_call: SessionWebhookToolCallSchema })
|
|
157
|
+
.passthrough(),
|
|
158
|
+
z
|
|
159
|
+
.object({ type: z.literal('proxy_execute'), http_request: SessionWebhookHttpRequestSchema })
|
|
160
|
+
.passthrough(),
|
|
161
|
+
]),
|
|
162
|
+
})
|
|
163
|
+
.passthrough();
|
|
164
|
+
|
|
165
|
+
export type SessionWebhookBeforeExecuteEvent = z.infer<
|
|
166
|
+
typeof SessionWebhookBeforeExecuteEventSchema
|
|
167
|
+
>;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Sent after a call ran, with what it returned: `tool_response` for a tool
|
|
171
|
+
* call, `http_response` for a proxy call.
|
|
172
|
+
*/
|
|
173
|
+
const SessionWebhookAfterExecuteEventSchema = z
|
|
174
|
+
.object({
|
|
175
|
+
...sessionWebhookEnvelopeFields,
|
|
176
|
+
type: z.literal(SessionWebhookEventTypes.AFTER_EXECUTE),
|
|
177
|
+
data: z.discriminatedUnion('type', [
|
|
178
|
+
z
|
|
179
|
+
.object({
|
|
180
|
+
type: z.literal('tool_execute'),
|
|
181
|
+
tool_call: SessionWebhookToolCallSchema,
|
|
182
|
+
tool_response: SessionWebhookToolResponseSchema,
|
|
183
|
+
})
|
|
184
|
+
.passthrough(),
|
|
185
|
+
z
|
|
186
|
+
.object({
|
|
187
|
+
type: z.literal('proxy_execute'),
|
|
188
|
+
http_request: SessionWebhookHttpRequestSchema,
|
|
189
|
+
http_response: SessionWebhookHttpResponseSchema,
|
|
190
|
+
})
|
|
191
|
+
.passthrough(),
|
|
192
|
+
]),
|
|
193
|
+
})
|
|
194
|
+
.passthrough();
|
|
195
|
+
|
|
196
|
+
export type SessionWebhookAfterExecuteEvent = z.infer<typeof SessionWebhookAfterExecuteEventSchema>;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* An event Composio sends to the session webhook URL, as returned by
|
|
200
|
+
* `composio.sessions.webhooks.parse()`.
|
|
201
|
+
*/
|
|
202
|
+
export const SessionWebhookEventSchema = z.discriminatedUnion('type', [
|
|
203
|
+
SessionWebhookBeforeExecuteEventSchema,
|
|
204
|
+
SessionWebhookAfterExecuteEventSchema,
|
|
205
|
+
]);
|
|
206
|
+
|
|
207
|
+
export type SessionWebhookEvent = z.infer<typeof SessionWebhookEventSchema>;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Parameters of `composio.sessions.webhooks.parse()`.
|
|
211
|
+
*/
|
|
212
|
+
export type ParseSessionWebhookParams = {
|
|
213
|
+
/** The raw, unparsed request body. The signature covers its exact bytes. */
|
|
214
|
+
body: string | Uint8Array;
|
|
215
|
+
/** The request headers. Names are matched case-insensitively. */
|
|
216
|
+
headers: Headers | Record<string, string | string[] | undefined>;
|
|
217
|
+
/**
|
|
218
|
+
* The project's session webhook secret, shown once in the dashboard when the
|
|
219
|
+
* session webhook URL is saved or its secret is rotated.
|
|
220
|
+
*/
|
|
221
|
+
verifySecret: string;
|
|
222
|
+
/**
|
|
223
|
+
* Maximum allowed age of the event in seconds (default: 300 = 5 minutes).
|
|
224
|
+
* Set to 0 to disable timestamp validation.
|
|
225
|
+
*/
|
|
226
|
+
tolerance?: number;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// Composio rejects an answer with a field it doesn't know, except on a block,
|
|
230
|
+
// and then applies the hook's `fallback_action`.
|
|
231
|
+
|
|
232
|
+
/** Stops the call. `message` is shown to the agent. */
|
|
233
|
+
export type SessionWebhookBlockResponse = {
|
|
234
|
+
action: 'block';
|
|
235
|
+
message: string;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Fields of a proxy request to replace. Fields left out keep their original
|
|
240
|
+
* value. Set at most one of `body` and `binary_body`. The toolkit cannot be changed.
|
|
241
|
+
*/
|
|
242
|
+
export type SessionWebhookHttpRequestOverride = {
|
|
243
|
+
method?: z.infer<typeof SessionWebhookHttpMethodSchema>;
|
|
244
|
+
endpoint?: string;
|
|
245
|
+
parameters?: Array<{ name: string; value: string; type: 'header' | 'query' }>;
|
|
246
|
+
} & (
|
|
247
|
+
| { body?: unknown; binary_body?: never }
|
|
248
|
+
| {
|
|
249
|
+
body?: never;
|
|
250
|
+
binary_body: z.infer<typeof BinaryBodyUrlSchema> | z.infer<typeof BinaryBodyBase64Schema>;
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Fields of a proxy response to replace. Fields left out keep their original
|
|
256
|
+
* value. The status and a binary file cannot be changed.
|
|
257
|
+
*/
|
|
258
|
+
export type SessionWebhookHttpResponseOverride = {
|
|
259
|
+
data?: unknown;
|
|
260
|
+
/** Replaces the API's headers in full */
|
|
261
|
+
headers?: Record<string, string>;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The answer to a `before_execute` event: let the call run, run it with
|
|
266
|
+
* changes, or block it. Use `tool_call` to change a tool call and
|
|
267
|
+
* `http_request` to change a proxy call, never both.
|
|
268
|
+
*/
|
|
269
|
+
export type SessionWebhookBeforeExecuteResponse =
|
|
270
|
+
| {
|
|
271
|
+
action: 'continue';
|
|
272
|
+
override?:
|
|
273
|
+
| { tool_call: { arguments: Record<string, unknown> }; http_request?: null }
|
|
274
|
+
| { http_request: SessionWebhookHttpRequestOverride; tool_call?: null }
|
|
275
|
+
| null;
|
|
276
|
+
}
|
|
277
|
+
| SessionWebhookBlockResponse;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The answer to an `after_execute` event: pass the result on to the agent,
|
|
281
|
+
* pass on a changed result, or block it. Use `tool_response` to change a tool
|
|
282
|
+
* call's result and `http_response` to change a proxy call's, never both.
|
|
283
|
+
*/
|
|
284
|
+
export type SessionWebhookAfterExecuteResponse =
|
|
285
|
+
| {
|
|
286
|
+
action: 'continue';
|
|
287
|
+
override?:
|
|
288
|
+
| { tool_response: { data: Record<string, unknown> }; http_response?: null }
|
|
289
|
+
| { http_response: SessionWebhookHttpResponseOverride; tool_response?: null }
|
|
290
|
+
| null;
|
|
291
|
+
}
|
|
292
|
+
| SessionWebhookBlockResponse;
|
|
@@ -364,6 +364,28 @@ export type ToolRouterHookPromptInjectionCheckHandler = z.infer<
|
|
|
364
364
|
typeof ToolRouterHookPromptInjectionCheckHandlerSchema
|
|
365
365
|
>;
|
|
366
366
|
|
|
367
|
+
/**
|
|
368
|
+
* Sends the call to the project's session webhook URL and waits for your
|
|
369
|
+
* endpoint's answer: let it run, block it, or change it. Read the request in
|
|
370
|
+
* your endpoint with `composio.sessions.webhooks.parse()`.
|
|
371
|
+
*/
|
|
372
|
+
export const ToolRouterHookWebhookHandlerSchema = z
|
|
373
|
+
.object({
|
|
374
|
+
type: z.literal('webhook'),
|
|
375
|
+
fallbackAction: z
|
|
376
|
+
.enum(['block', 'continue'])
|
|
377
|
+
.describe(
|
|
378
|
+
"What happens to the call when your endpoint times out, can't be reached, or sends an answer that can't be read."
|
|
379
|
+
),
|
|
380
|
+
timeoutMs: z
|
|
381
|
+
.number()
|
|
382
|
+
.int()
|
|
383
|
+
.optional()
|
|
384
|
+
.describe('How long to wait for your endpoint to answer, in milliseconds. Defaults to 3000.'),
|
|
385
|
+
})
|
|
386
|
+
.strict();
|
|
387
|
+
export type ToolRouterHookWebhookHandler = z.infer<typeof ToolRouterHookWebhookHandlerSchema>;
|
|
388
|
+
|
|
367
389
|
const toolRouterHookIdentityFields = {
|
|
368
390
|
id: z.string().describe('Unique within the session. Shows up in results and logs.'),
|
|
369
391
|
name: z
|
|
@@ -376,14 +398,18 @@ const toolRouterHookIdentityFields = {
|
|
|
376
398
|
/**
|
|
377
399
|
* A hook on a session's tool calls. A `before_execute` hook can block a call
|
|
378
400
|
* before it runs; an `after_execute` hook can block its result before the
|
|
379
|
-
* agent sees it.
|
|
401
|
+
* agent sees it. A hook with a `webhook` handler can also change the call or
|
|
402
|
+
* its result.
|
|
380
403
|
*/
|
|
381
404
|
export const ToolRouterHookSchema = z.discriminatedUnion('type', [
|
|
382
405
|
z
|
|
383
406
|
.object({
|
|
384
407
|
type: z.literal('before_execute'),
|
|
385
408
|
...toolRouterHookIdentityFields,
|
|
386
|
-
handler:
|
|
409
|
+
handler: z.discriminatedUnion('type', [
|
|
410
|
+
ToolRouterHookConditionCheckHandlerSchema,
|
|
411
|
+
ToolRouterHookWebhookHandlerSchema,
|
|
412
|
+
]),
|
|
387
413
|
})
|
|
388
414
|
.strict(),
|
|
389
415
|
z
|
|
@@ -393,6 +419,7 @@ export const ToolRouterHookSchema = z.discriminatedUnion('type', [
|
|
|
393
419
|
handler: z.discriminatedUnion('type', [
|
|
394
420
|
ToolRouterHookConditionCheckHandlerSchema,
|
|
395
421
|
ToolRouterHookPromptInjectionCheckHandlerSchema,
|
|
422
|
+
ToolRouterHookWebhookHandlerSchema,
|
|
396
423
|
]),
|
|
397
424
|
})
|
|
398
425
|
.strict(),
|
|
@@ -847,8 +874,9 @@ export type ToolRouterSessionSearchResponse = z.infer<typeof ToolRouterSessionSe
|
|
|
847
874
|
|
|
848
875
|
/**
|
|
849
876
|
* What one session hook decided about a call. `unavailable` means the hook
|
|
850
|
-
* could not be evaluated (for example it timed out)
|
|
851
|
-
* call
|
|
877
|
+
* could not be evaluated (for example it timed out), and `detail` says why
|
|
878
|
+
* when the API gives a reason. An unavailable hook does not block the call,
|
|
879
|
+
* unless it has a `webhook` handler with `fallbackAction: 'block'`.
|
|
852
880
|
*/
|
|
853
881
|
export const ToolRouterHookResultSchema = z.discriminatedUnion('status', [
|
|
854
882
|
z.object({
|
|
@@ -859,6 +887,7 @@ export const ToolRouterHookResultSchema = z.discriminatedUnion('status', [
|
|
|
859
887
|
z.object({
|
|
860
888
|
id: z.string().describe('The hook id'),
|
|
861
889
|
status: z.literal('unavailable'),
|
|
890
|
+
detail: z.string().optional().describe('Why the hook could not be evaluated'),
|
|
862
891
|
}),
|
|
863
892
|
]);
|
|
864
893
|
export type ToolRouterHookResult = z.infer<typeof ToolRouterHookResultSchema>;
|
|
@@ -362,6 +362,8 @@ export const VerifyWebhookParamsSchema = z.object({
|
|
|
362
362
|
* @default 300 (5 minutes)
|
|
363
363
|
*/
|
|
364
364
|
tolerance: z.number().optional().default(300),
|
|
365
|
+
/** Maximum UTF-8 size of the supplied payload in bytes. */
|
|
366
|
+
maxBodyBytes: z.number().int().nonnegative().safe().optional(),
|
|
365
367
|
});
|
|
366
368
|
|
|
367
369
|
export type VerifyWebhookParams = z.input<typeof VerifyWebhookParamsSchema>;
|
|
@@ -399,6 +401,8 @@ export type ParseWebhookOptions = {
|
|
|
399
401
|
* validation.
|
|
400
402
|
*/
|
|
401
403
|
tolerance?: number;
|
|
404
|
+
/** Maximum request body size in bytes. Defaults to 10 MiB. */
|
|
405
|
+
maxBodyBytes?: number;
|
|
402
406
|
};
|
|
403
407
|
|
|
404
408
|
/**
|
package/src/utils/crypto.ts
CHANGED
|
@@ -3,9 +3,11 @@ import { arrayBufferToBase64 } from './buffer';
|
|
|
3
3
|
/**
|
|
4
4
|
* Computes HMAC-SHA256 using Web Crypto API
|
|
5
5
|
*/
|
|
6
|
-
export async function hmacSha256Base64(
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
export async function hmacSha256Base64(
|
|
7
|
+
secret: string,
|
|
8
|
+
message: Uint8Array<ArrayBuffer>
|
|
9
|
+
): Promise<string> {
|
|
10
|
+
const keyData = new TextEncoder().encode(secret);
|
|
9
11
|
const key = await globalThis.crypto.subtle.importKey(
|
|
10
12
|
'raw',
|
|
11
13
|
keyData,
|
|
@@ -13,8 +15,7 @@ export async function hmacSha256Base64(secret: string, message: string): Promise
|
|
|
13
15
|
false,
|
|
14
16
|
['sign']
|
|
15
17
|
);
|
|
16
|
-
const
|
|
17
|
-
const signatureBuffer = await globalThis.crypto.subtle.sign('HMAC', key, signatureData);
|
|
18
|
+
const signatureBuffer = await globalThis.crypto.subtle.sign('HMAC', key, message);
|
|
18
19
|
|
|
19
20
|
return arrayBufferToBase64(signatureBuffer);
|
|
20
21
|
}
|
package/src/utils/env.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export const getEnvVariable = (
|
|
2
|
-
name: string,
|
|
3
|
-
defaultValue: string | undefined = undefined
|
|
4
|
-
): string | undefined => {
|
|
1
|
+
export const getEnvVariable = (name: string, defaultValue?: string): string | undefined => {
|
|
5
2
|
try {
|
|
6
3
|
return process.env[name] || defaultValue;
|
|
7
4
|
} catch (_e) {
|
|
@@ -28,7 +25,7 @@ export const getEnvsWithPrefix = (prefix: string): Record<string, unknown> => {
|
|
|
28
25
|
} else {
|
|
29
26
|
return {};
|
|
30
27
|
}
|
|
31
|
-
} catch
|
|
28
|
+
} catch {
|
|
32
29
|
return {};
|
|
33
30
|
}
|
|
34
31
|
};
|
|
@@ -4,6 +4,7 @@ import { platform } from '#platform';
|
|
|
4
4
|
import ComposioClient from '@composio/client';
|
|
5
5
|
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
|
|
6
6
|
import logger from './logger';
|
|
7
|
+
import { describeThrown } from './userCallback';
|
|
7
8
|
import { getRandomShortId } from './uuid';
|
|
8
9
|
import { base64ToUint8Array, uint8ArrayToBase64 } from './buffer';
|
|
9
10
|
import type { FileDownloadData, FileUploadData } from '../types/files.types';
|
|
@@ -166,7 +167,7 @@ const readFileContent = async (
|
|
|
166
167
|
mimeType: getMimeTypeFromFileName(filePath),
|
|
167
168
|
};
|
|
168
169
|
} catch (error) {
|
|
169
|
-
throw new Error(`Error reading file at ${filePath}: ${error}`);
|
|
170
|
+
throw new Error(`Error reading file at ${filePath}: ${describeThrown(error)}`);
|
|
170
171
|
}
|
|
171
172
|
};
|
|
172
173
|
|
|
@@ -472,7 +473,7 @@ export const saveFile = (
|
|
|
472
473
|
} catch (err) {
|
|
473
474
|
logger.warn(
|
|
474
475
|
`fileDownloadDir "${absDir}" is not writable, falling back to the default ` +
|
|
475
|
-
`(~/.composio/files). Error: ${err}`
|
|
476
|
+
`(~/.composio/files). Error: ${describeThrown(err)}`
|
|
476
477
|
);
|
|
477
478
|
composioFilesDir = getComposioTempFilesDir(true);
|
|
478
479
|
}
|
|
@@ -495,7 +496,7 @@ export const saveFile = (
|
|
|
495
496
|
|
|
496
497
|
return filePath;
|
|
497
498
|
} catch (_error) {
|
|
498
|
-
logger.debug(`Error saving file: ${_error}`);
|
|
499
|
+
logger.debug(`Error saving file: ${describeThrown(_error)}`);
|
|
499
500
|
return null;
|
|
500
501
|
}
|
|
501
502
|
};
|
package/src/utils/logger.ts
CHANGED
|
@@ -76,16 +76,9 @@ export class Logger {
|
|
|
76
76
|
.map((arg, index) => {
|
|
77
77
|
if (typeof arg === 'object') {
|
|
78
78
|
return JSON.stringify(arg);
|
|
79
|
-
} else {
|
|
80
|
-
if (index === 0) {
|
|
81
|
-
if (args.length > 1) {
|
|
82
|
-
return pc.yellow(`${arg}`);
|
|
83
|
-
} else {
|
|
84
|
-
return String(arg);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return String(arg);
|
|
88
79
|
}
|
|
80
|
+
const text = formatLogValue(arg);
|
|
81
|
+
return index === 0 && args.length > 1 ? pc.yellow(text) : text;
|
|
89
82
|
})
|
|
90
83
|
.join('\n');
|
|
91
84
|
|
|
@@ -126,6 +119,23 @@ export class Logger {
|
|
|
126
119
|
}
|
|
127
120
|
}
|
|
128
121
|
|
|
122
|
+
function formatLogValue(value: unknown): string {
|
|
123
|
+
switch (typeof value) {
|
|
124
|
+
case 'string':
|
|
125
|
+
return value;
|
|
126
|
+
case 'function':
|
|
127
|
+
return value.toString();
|
|
128
|
+
case 'number':
|
|
129
|
+
case 'boolean':
|
|
130
|
+
case 'bigint':
|
|
131
|
+
case 'symbol':
|
|
132
|
+
case 'undefined':
|
|
133
|
+
return String(value);
|
|
134
|
+
default:
|
|
135
|
+
return Object.prototype.toString.call(value);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
129
139
|
// Create and export a default logger instance
|
|
130
140
|
const logger = new Logger();
|
|
131
141
|
export default logger;
|
package/src/utils/pusher.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PusherSubscriptionError } from '../errors/PusherErrors';
|
|
2
2
|
import { CLIENT_PUSHER_KEY } from './constants';
|
|
3
3
|
import logger from './logger';
|
|
4
|
+
import { describeThrown } from './userCallback';
|
|
4
5
|
|
|
5
6
|
const PUSHER_KEY = CLIENT_PUSHER_KEY || 'ff9f18c208855d77a152';
|
|
6
7
|
const PUSHER_CLUSTER = 'mt1';
|
|
@@ -124,7 +125,9 @@ export class PusherUtils {
|
|
|
124
125
|
try {
|
|
125
126
|
await PusherUtils.pusherClient.subscribe(channelName).bind(event, fn);
|
|
126
127
|
} catch (error) {
|
|
127
|
-
logger.error(
|
|
128
|
+
logger.error(
|
|
129
|
+
`Error subscribing to ${channelName} with event ${event}: ${describeThrown(error)}`
|
|
130
|
+
);
|
|
128
131
|
}
|
|
129
132
|
}
|
|
130
133
|
|
|
@@ -239,7 +242,7 @@ export class PusherUtils {
|
|
|
239
242
|
|
|
240
243
|
// Add subscription error handling
|
|
241
244
|
channel.bind('pusher:subscription_error', (data: Record<string, unknown>) => {
|
|
242
|
-
const error = data.error ?
|
|
245
|
+
const error = data.error ? describeThrown(data.error) : 'Unknown subscription error';
|
|
243
246
|
throw new PusherSubscriptionError(`Trigger subscription error`, {
|
|
244
247
|
cause: error,
|
|
245
248
|
});
|
|
@@ -4,35 +4,40 @@ export const MAX_URL_UPLOAD_SIZE_BYTES = 100 * 1024 * 1024;
|
|
|
4
4
|
const sizeLimitError = (actualSize: number, maxBytes: number): Error =>
|
|
5
5
|
new Error(`File size (${actualSize} bytes) exceeds maximum allowed size (${maxBytes} bytes)`);
|
|
6
6
|
|
|
7
|
+
export const assertValidBodyLimit = (maxBytes: number): void => {
|
|
8
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0) {
|
|
9
|
+
throw new RangeError('maxBytes must be a non-negative safe integer');
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
7
13
|
/**
|
|
8
|
-
* Read a
|
|
14
|
+
* Read a Fetch request or response within a byte limit.
|
|
9
15
|
*
|
|
10
16
|
* `Content-Length` is checked as an early rejection, but the streamed byte
|
|
11
17
|
* count is authoritative because the header can be absent or dishonest.
|
|
12
18
|
*/
|
|
13
|
-
export const
|
|
14
|
-
|
|
15
|
-
maxBytes: number
|
|
19
|
+
export const readBodyWithLimit = async (
|
|
20
|
+
{ body, headers }: Pick<Response, 'body' | 'headers'>,
|
|
21
|
+
maxBytes: number,
|
|
22
|
+
makeSizeLimitError: (actualSize: number, maxBytes: number) => Error
|
|
16
23
|
): Promise<Uint8Array> => {
|
|
17
|
-
|
|
18
|
-
throw new RangeError('maxBytes must be a non-negative safe integer');
|
|
19
|
-
}
|
|
24
|
+
assertValidBodyLimit(maxBytes);
|
|
20
25
|
|
|
21
|
-
const contentLength =
|
|
26
|
+
const contentLength = headers.get('content-length')?.trim();
|
|
22
27
|
if (contentLength && /^\d+$/.test(contentLength)) {
|
|
23
28
|
const declaredSize = Number(contentLength);
|
|
24
29
|
if (!Number.isSafeInteger(declaredSize) || declaredSize > maxBytes) {
|
|
25
|
-
const error =
|
|
26
|
-
|
|
30
|
+
const error = makeSizeLimitError(declaredSize, maxBytes);
|
|
31
|
+
void body?.cancel(error).catch(() => undefined);
|
|
27
32
|
throw error;
|
|
28
33
|
}
|
|
29
34
|
}
|
|
30
35
|
|
|
31
|
-
if (!
|
|
36
|
+
if (!body) {
|
|
32
37
|
return new Uint8Array();
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
const reader =
|
|
40
|
+
const reader = body.getReader();
|
|
36
41
|
const chunks: Uint8Array[] = [];
|
|
37
42
|
let totalBytes = 0;
|
|
38
43
|
|
|
@@ -44,8 +49,10 @@ export const readResponseBodyWithLimit = async (
|
|
|
44
49
|
|
|
45
50
|
totalBytes += value.byteLength;
|
|
46
51
|
if (totalBytes > maxBytes) {
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
const error = makeSizeLimitError(totalBytes, maxBytes);
|
|
53
|
+
// A cloned request can keep the cancellation pending on its other branch.
|
|
54
|
+
void reader.cancel(error).catch(() => undefined);
|
|
55
|
+
throw error;
|
|
49
56
|
}
|
|
50
57
|
chunks.push(value);
|
|
51
58
|
}
|
|
@@ -53,11 +60,17 @@ export const readResponseBodyWithLimit = async (
|
|
|
53
60
|
reader.releaseLock();
|
|
54
61
|
}
|
|
55
62
|
|
|
56
|
-
const
|
|
63
|
+
const bytes = new Uint8Array(totalBytes);
|
|
57
64
|
let offset = 0;
|
|
58
65
|
for (const chunk of chunks) {
|
|
59
|
-
|
|
66
|
+
bytes.set(chunk, offset);
|
|
60
67
|
offset += chunk.byteLength;
|
|
61
68
|
}
|
|
62
|
-
return
|
|
69
|
+
return bytes;
|
|
63
70
|
};
|
|
71
|
+
|
|
72
|
+
/** Read a Fetch response within the file size limit. */
|
|
73
|
+
export const readResponseBodyWithLimit = (
|
|
74
|
+
response: Response,
|
|
75
|
+
maxBytes: number = MAX_URL_UPLOAD_SIZE_BYTES
|
|
76
|
+
): Promise<Uint8Array> => readBodyWithLimit(response, maxBytes, sizeLimitError);
|
|
@@ -11,7 +11,9 @@ import logger from './logger';
|
|
|
11
11
|
export function describeThrown(value: unknown): string {
|
|
12
12
|
try {
|
|
13
13
|
if (value instanceof Error) return value.message;
|
|
14
|
-
if (typeof value === 'object' && value !== null)
|
|
14
|
+
if (typeof value === 'object' && value !== null) {
|
|
15
|
+
return JSON.stringify(value) ?? Object.prototype.toString.call(value);
|
|
16
|
+
}
|
|
15
17
|
return String(value);
|
|
16
18
|
} catch {
|
|
17
19
|
try {
|