@composio/core 0.26.0 → 0.27.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-DfP4ldQl.d.mts → CustomTool-CqPoLUD6.d.mts} +1 -1
- package/dist/{composio-BMDH-1Qj.d.mts → composio-BUa10iB3.d.mts} +51 -51
- package/dist/{customTool.types-BoOELgJF.d.mts → customTool.types-gX421uPT.d.mts} +769 -13
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +3 -3
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +2 -1
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +5 -1
- package/dist/experimental/index.d.mts +2 -2
- package/dist/index.d.mts +10 -10
- package/dist/index.mjs +6447 -6315
- package/dist/{types-DHNV_T_z.d.mts → types-DXxfzBX3.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.workerd.d.mts +1 -1
- package/package.json +1 -1
- package/src/lib/toolRouterParams.ts +26 -3
- package/src/models/SessionContext.ts +2 -17
- package/src/models/ToolRouter.ts +6 -1
- package/src/models/ToolRouterSession.ts +4 -19
- package/src/types/toolRouter.types.ts +155 -10
- package/src/utils/transformers/toolRouterResponseTransform.ts +123 -5
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as ComposioConfig } from "./composio-
|
|
1
|
+
import { Tr as BaseComposioProvider } from "./customTool.types-gX421uPT.mjs";
|
|
2
|
+
import { n as ComposioConfig } from "./composio-BUa10iB3.mjs";
|
|
3
3
|
//#region src/utils/config-defaults/types.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Configuration defaults for Composio.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Ka as ToolExecuteResponse, Va as Tool, Wa as ToolExecuteParams } from "../../customTool.types-gX421uPT.mjs";
|
|
2
2
|
import ComposioClient from "@composio/client";
|
|
3
3
|
//#region src/utils/modifiers/FileToolModifier.node.d.ts
|
|
4
4
|
export declare class FileToolModifier {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Ka as ToolExecuteResponse, Va as Tool, Wa as ToolExecuteParams } from "../../customTool.types-gX421uPT.mjs";
|
|
2
2
|
import ComposioClient from "@composio/client";
|
|
3
3
|
//#region src/utils/modifiers/FileToolModifier.workerd.d.ts
|
|
4
4
|
export declare class FileToolModifier {
|
package/package.json
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
ToolRouterSandboxConfig,
|
|
20
20
|
ToolRouterInstant,
|
|
21
21
|
ToolRouterProxyExecute,
|
|
22
|
+
ToolRouterHook,
|
|
22
23
|
} from '../types/toolRouter.types';
|
|
23
24
|
import { ValidationError } from '../errors';
|
|
24
25
|
import { z } from 'zod';
|
|
@@ -189,11 +190,29 @@ export type SessionPatchExperimentalBody = WithNull<
|
|
|
189
190
|
'permissions' | 'link_url_overwrite' | 'fast_mode' | 'submit_feedback'
|
|
190
191
|
>;
|
|
191
192
|
|
|
193
|
+
/** A hook as the API takes it, in snake_case. The generated client does not type `hooks` yet. */
|
|
194
|
+
export type SessionHookBody = {
|
|
195
|
+
type: 'before_execute' | 'after_execute';
|
|
196
|
+
id: string;
|
|
197
|
+
name?: string;
|
|
198
|
+
applies_to: 'all' | { toolkits: string[] };
|
|
199
|
+
handler:
|
|
200
|
+
| { type: 'condition_check'; condition: string; action: 'block' }
|
|
201
|
+
| { type: 'prompt_injection_check'; action: 'block' };
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/** Request body of `sessions.create()`: the generated `SessionCreateParams` plus `hooks`. */
|
|
205
|
+
export type SessionCreateBody = SessionCreateParams & { hooks?: SessionHookBody[] };
|
|
206
|
+
|
|
207
|
+
/** `hooks` as sent on create and update: `appliesTo` becomes `applies_to`. */
|
|
208
|
+
export const transformToolRouterHooksParams = (hooks: ToolRouterHook[]): SessionHookBody[] =>
|
|
209
|
+
hooks.map(({ appliesTo, ...hook }) => ({ ...hook, applies_to: appliesTo }));
|
|
210
|
+
|
|
192
211
|
/**
|
|
193
212
|
* Request body of `session.update()`. Extends the generated `SessionPatchParams`
|
|
194
|
-
* with the nullable policy blocks (`null` removes the stored override)
|
|
195
|
-
* `expected_config_version` root precondition, none of which the
|
|
196
|
-
* client types yet.
|
|
213
|
+
* with the nullable policy blocks (`null` removes the stored override), `hooks`
|
|
214
|
+
* and the `expected_config_version` root precondition, none of which the
|
|
215
|
+
* generated client types yet.
|
|
197
216
|
*/
|
|
198
217
|
export type SessionPatchBody = Omit<
|
|
199
218
|
WithNull<
|
|
@@ -205,6 +224,7 @@ export type SessionPatchBody = Omit<
|
|
|
205
224
|
manage_connections?: SessionPatchManageConnectionsBody | null;
|
|
206
225
|
multi_account?: SessionPatchMultiAccountBody | null;
|
|
207
226
|
experimental?: SessionPatchExperimentalBody | null;
|
|
227
|
+
hooks?: SessionHookBody[] | null;
|
|
208
228
|
expected_config_version?: number;
|
|
209
229
|
};
|
|
210
230
|
|
|
@@ -419,6 +439,9 @@ export const transformToolRouterUpdateParams = (
|
|
|
419
439
|
? null
|
|
420
440
|
: transformToolRouterProxyExecuteParams(config.proxyExecute);
|
|
421
441
|
}
|
|
442
|
+
if (config.hooks !== undefined) {
|
|
443
|
+
params.hooks = config.hooks === null ? null : transformToolRouterHooksParams(config.hooks);
|
|
444
|
+
}
|
|
422
445
|
if (config.preload !== undefined) {
|
|
423
446
|
params.preload = config.preload;
|
|
424
447
|
}
|
|
@@ -24,8 +24,8 @@ import {
|
|
|
24
24
|
executeCustomTool,
|
|
25
25
|
} from './customToolExecution';
|
|
26
26
|
import {
|
|
27
|
-
assertNotInputRequired,
|
|
28
27
|
transformExecuteResponse,
|
|
28
|
+
transformProxyExecuteResponse,
|
|
29
29
|
} from '../utils/transformers/toolRouterResponseTransform';
|
|
30
30
|
import type { SessionExecuteParams } from '@composio/client/resources/tool-router/session/session.mjs';
|
|
31
31
|
import { inlineCustomToolsExperimental } from './inlineCustomToolsPayload';
|
|
@@ -144,24 +144,9 @@ export class SessionContextImpl implements SessionContext {
|
|
|
144
144
|
requestOptions?.signal
|
|
145
145
|
);
|
|
146
146
|
|
|
147
|
-
|
|
147
|
+
return transformProxyExecuteResponse(
|
|
148
148
|
response,
|
|
149
149
|
`${validated.data.method} proxy call for toolkit ${validated.data.toolkit}`
|
|
150
150
|
);
|
|
151
|
-
return {
|
|
152
|
-
status: response.status,
|
|
153
|
-
data: response.data,
|
|
154
|
-
headers: response.headers,
|
|
155
|
-
...(response.binary_data
|
|
156
|
-
? {
|
|
157
|
-
binaryData: {
|
|
158
|
-
contentType: response.binary_data.content_type,
|
|
159
|
-
size: response.binary_data.size,
|
|
160
|
-
url: response.binary_data.url,
|
|
161
|
-
expiresAt: response.binary_data.expires_at,
|
|
162
|
-
},
|
|
163
|
-
}
|
|
164
|
-
: {}),
|
|
165
|
-
};
|
|
166
151
|
}
|
|
167
152
|
}
|
package/src/models/ToolRouter.ts
CHANGED
|
@@ -52,6 +52,8 @@ import {
|
|
|
52
52
|
transformToolRouterMultiAccountParams,
|
|
53
53
|
transformToolRouterInstantParams,
|
|
54
54
|
transformToolRouterProxyExecuteParams,
|
|
55
|
+
transformToolRouterHooksParams,
|
|
56
|
+
type SessionCreateBody,
|
|
55
57
|
resolveToolRouterSandboxConfig,
|
|
56
58
|
} from '../lib/toolRouterParams';
|
|
57
59
|
import { PRELOAD_TOOLS_ALL } from '../lib/toolRouterConstants';
|
|
@@ -275,7 +277,7 @@ export class ToolRouter<
|
|
|
275
277
|
])
|
|
276
278
|
);
|
|
277
279
|
|
|
278
|
-
const payload:
|
|
280
|
+
const payload: SessionCreateBody = {
|
|
279
281
|
user_id: userId,
|
|
280
282
|
auth_configs: routerConfig.authConfigs,
|
|
281
283
|
connected_accounts: connectedAccountsPayload,
|
|
@@ -293,6 +295,9 @@ export class ToolRouter<
|
|
|
293
295
|
...(routerConfig.proxyExecute !== undefined && {
|
|
294
296
|
proxy_execute: transformToolRouterProxyExecuteParams(routerConfig.proxyExecute),
|
|
295
297
|
}),
|
|
298
|
+
...(routerConfig.hooks !== undefined && {
|
|
299
|
+
hooks: transformToolRouterHooksParams(routerConfig.hooks),
|
|
300
|
+
}),
|
|
296
301
|
preload: routerConfig.preload,
|
|
297
302
|
...(isDirectToolsPreset && {
|
|
298
303
|
search: { enable: false },
|
|
@@ -39,9 +39,9 @@ import {
|
|
|
39
39
|
type ToolRouterSessionDeleteResponse,
|
|
40
40
|
} from '../types/toolRouter.types';
|
|
41
41
|
import {
|
|
42
|
-
assertNotInputRequired,
|
|
43
42
|
transformSearchResponse,
|
|
44
43
|
transformExecuteResponse,
|
|
44
|
+
transformProxyExecuteResponse,
|
|
45
45
|
} from '../utils/transformers/toolRouterResponseTransform';
|
|
46
46
|
import { SessionMetaToolOptions } from '../types/modifiers.types';
|
|
47
47
|
import { ConnectionRequest } from '../types/connectionRequest.types';
|
|
@@ -697,7 +697,7 @@ export class ToolRouterSession<
|
|
|
697
697
|
* @param options.account - Account identifier for direct app tool execution. Accepted on every project: in multi-account sessions it picks the account; on single-account projects it must match one of the session's active connections for the toolkit. Helper/meta tools either ignore this top-level field or define their own account-selection fields.
|
|
698
698
|
* @param options.inputResponses - Experimental. The user's answers to an earlier `ComposioToolInputRequiredError`, keyed by the IDs in its `inputRequests`. Repeat the same call with them.
|
|
699
699
|
* @param options.requestState - Experimental. The `requestState` of that error, sent back unchanged.
|
|
700
|
-
* @returns The tool execution result
|
|
700
|
+
* @returns The tool execution result. `resultType: 'blocked'` means a session hook blocked the call; `error` then holds the hook's message
|
|
701
701
|
* @throws {ComposioToolInputRequiredError} If the tool needs input from the user (for example an approval) before it can run
|
|
702
702
|
*/
|
|
703
703
|
async execute(
|
|
@@ -815,7 +815,7 @@ export class ToolRouterSession<
|
|
|
815
815
|
* The backend resolves the connected account from the toolkit within the session.
|
|
816
816
|
*
|
|
817
817
|
* @param params - Proxy request parameters (toolkit, endpoint, method, body, headers/query params)
|
|
818
|
-
* @returns The proxied API response with status, data,
|
|
818
|
+
* @returns The proxied API response with status, data and headers (`resultType: 'completed'`), or `resultType: 'blocked'` with the hook's message when a session hook blocked the request
|
|
819
819
|
* @throws {ComposioToolInputRequiredError} If the call needs input from the user before it can run
|
|
820
820
|
*/
|
|
821
821
|
async proxyExecute(
|
|
@@ -838,25 +838,10 @@ export class ToolRouterSession<
|
|
|
838
838
|
requestOptions?.signal
|
|
839
839
|
);
|
|
840
840
|
|
|
841
|
-
|
|
841
|
+
return transformProxyExecuteResponse(
|
|
842
842
|
response,
|
|
843
843
|
`${validated.data.method} proxy call for toolkit ${validated.data.toolkit}`
|
|
844
844
|
);
|
|
845
|
-
return {
|
|
846
|
-
status: response.status,
|
|
847
|
-
data: response.data,
|
|
848
|
-
headers: response.headers,
|
|
849
|
-
...(response.binary_data
|
|
850
|
-
? {
|
|
851
|
-
binaryData: {
|
|
852
|
-
contentType: response.binary_data.content_type,
|
|
853
|
-
size: response.binary_data.size,
|
|
854
|
-
url: response.binary_data.url,
|
|
855
|
-
expiresAt: response.binary_data.expires_at,
|
|
856
|
-
},
|
|
857
|
-
}
|
|
858
|
-
: {}),
|
|
859
|
-
};
|
|
860
845
|
}
|
|
861
846
|
|
|
862
847
|
/**
|
|
@@ -329,6 +329,75 @@ export const ToolRouterProxyExecuteSchema = z.union([
|
|
|
329
329
|
]);
|
|
330
330
|
export type ToolRouterProxyExecute = z.infer<typeof ToolRouterProxyExecuteSchema>;
|
|
331
331
|
|
|
332
|
+
/** Which tool calls a hook runs on: `'all'`, or only calls to the listed toolkits (including their proxy calls). */
|
|
333
|
+
export const ToolRouterHookAppliesToSchema = z.union([
|
|
334
|
+
z.literal('all'),
|
|
335
|
+
z.object({ toolkits: z.array(z.string()).describe('Toolkit slugs') }).strict(),
|
|
336
|
+
]);
|
|
337
|
+
export type ToolRouterHookAppliesTo = z.infer<typeof ToolRouterHookAppliesToSchema>;
|
|
338
|
+
|
|
339
|
+
/** Blocks a call or result when it matches `condition`, a plain sentence describing what to block. */
|
|
340
|
+
export const ToolRouterHookConditionCheckHandlerSchema = z
|
|
341
|
+
.object({
|
|
342
|
+
type: z.literal('condition_check'),
|
|
343
|
+
condition: z
|
|
344
|
+
.string()
|
|
345
|
+
.describe(
|
|
346
|
+
'What to block, written as a plain sentence. For example: "Sending email to anyone outside example.org".'
|
|
347
|
+
),
|
|
348
|
+
action: z.literal('block'),
|
|
349
|
+
})
|
|
350
|
+
.strict();
|
|
351
|
+
export type ToolRouterHookConditionCheckHandler = z.infer<
|
|
352
|
+
typeof ToolRouterHookConditionCheckHandlerSchema
|
|
353
|
+
>;
|
|
354
|
+
|
|
355
|
+
/** A pre-defined check that blocks results with text that tries to give the agent instructions. */
|
|
356
|
+
export const ToolRouterHookPromptInjectionCheckHandlerSchema = z
|
|
357
|
+
.object({
|
|
358
|
+
type: z.literal('prompt_injection_check'),
|
|
359
|
+
action: z.literal('block'),
|
|
360
|
+
})
|
|
361
|
+
.strict();
|
|
362
|
+
export type ToolRouterHookPromptInjectionCheckHandler = z.infer<
|
|
363
|
+
typeof ToolRouterHookPromptInjectionCheckHandlerSchema
|
|
364
|
+
>;
|
|
365
|
+
|
|
366
|
+
const toolRouterHookIdentityFields = {
|
|
367
|
+
id: z.string().describe('Unique within the session. Shows up in results and logs.'),
|
|
368
|
+
name: z
|
|
369
|
+
.string()
|
|
370
|
+
.optional()
|
|
371
|
+
.describe('A readable name. The agent sees it when this hook blocks a call.'),
|
|
372
|
+
appliesTo: ToolRouterHookAppliesToSchema,
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* A hook on a session's tool calls. A `before_execute` hook can block a call
|
|
377
|
+
* before it runs; an `after_execute` hook can block its result before the
|
|
378
|
+
* agent sees it.
|
|
379
|
+
*/
|
|
380
|
+
export const ToolRouterHookSchema = z.discriminatedUnion('type', [
|
|
381
|
+
z
|
|
382
|
+
.object({
|
|
383
|
+
type: z.literal('before_execute'),
|
|
384
|
+
...toolRouterHookIdentityFields,
|
|
385
|
+
handler: ToolRouterHookConditionCheckHandlerSchema,
|
|
386
|
+
})
|
|
387
|
+
.strict(),
|
|
388
|
+
z
|
|
389
|
+
.object({
|
|
390
|
+
type: z.literal('after_execute'),
|
|
391
|
+
...toolRouterHookIdentityFields,
|
|
392
|
+
handler: z.discriminatedUnion('type', [
|
|
393
|
+
ToolRouterHookConditionCheckHandlerSchema,
|
|
394
|
+
ToolRouterHookPromptInjectionCheckHandlerSchema,
|
|
395
|
+
]),
|
|
396
|
+
})
|
|
397
|
+
.strict(),
|
|
398
|
+
]);
|
|
399
|
+
export type ToolRouterHook = z.infer<typeof ToolRouterHookSchema>;
|
|
400
|
+
|
|
332
401
|
const ToolRouterCreateSessionConfigBaseSchema = z
|
|
333
402
|
.object({
|
|
334
403
|
sessionPreset: z
|
|
@@ -414,6 +483,13 @@ const ToolRouterCreateSessionConfigBaseSchema = z
|
|
|
414
483
|
'Proxy execute configuration for this session; a boolean is shorthand for `{ enable }`. Rejected by the API when enable is false and sandbox.enableProxyExecution is true.'
|
|
415
484
|
),
|
|
416
485
|
|
|
486
|
+
hooks: z
|
|
487
|
+
.array(ToolRouterHookSchema)
|
|
488
|
+
.optional()
|
|
489
|
+
.describe(
|
|
490
|
+
"Hooks that run on this session's tool calls. A hook can block a call before it runs, or block its result before the agent sees it."
|
|
491
|
+
),
|
|
492
|
+
|
|
417
493
|
preload: z
|
|
418
494
|
.object({
|
|
419
495
|
tools: z
|
|
@@ -480,6 +556,7 @@ const ToolRouterCreateSessionConfigBaseSchema = z
|
|
|
480
556
|
config.toolkits !== undefined && 'toolkits',
|
|
481
557
|
config.tools !== undefined && 'tools',
|
|
482
558
|
config.tags !== undefined && 'tags',
|
|
559
|
+
config.hooks !== undefined && 'hooks',
|
|
483
560
|
config.experimental.customTools !== undefined && 'experimental.customTools',
|
|
484
561
|
config.experimental.customToolkits !== undefined && 'experimental.customToolkits',
|
|
485
562
|
].filter((field): field is string => field !== false)
|
|
@@ -544,10 +621,11 @@ export const ToolRouterCreateSessionConfigSchema = z
|
|
|
544
621
|
* @param {boolean} [multiAccount.requireExplicitSelection] - When true, require explicit account selection when multiple accounts are connected
|
|
545
622
|
* @param {boolean | object} [proxyExecute] - Proxy execute configuration for this session. A boolean is shorthand for `{ enable }`.
|
|
546
623
|
* @param {boolean} proxyExecute.enable - `true` exposes the `COMPOSIO_PROXY_EXECUTE` meta tool. `false` also turns off the sandbox `proxy_execute()` helper.
|
|
624
|
+
* @param {ToolRouterHook[]} [hooks] - Hooks that run on this session's tool calls. A `before_execute` hook can block a call before it runs; an `after_execute` hook can block its result before the agent sees it. Cannot be combined with `experimental.sessionConfigId`.
|
|
547
625
|
* @param {object} [preload] - Tools to preload into session.tools() and the MCP tool list
|
|
548
626
|
* @param {string[] | 'all'} [preload.tools] - Tool slugs to preload, or "all" to preload every app tool allowed by the session filters. "all" requires a positive filter such as toolkits, tools, or tags; the backend validates and caps the final tool set.
|
|
549
627
|
* @param {object} [experimental] - Experimental features configuration. Not stable; may change or be removed.
|
|
550
|
-
* @param {string} [experimental.sessionConfigId] - ID of a saved Session config (`sc_…`) to apply at creation; see `composio.sessionConfigs`. Cannot be combined with `toolkits`, `tools`, `tags`, `experimental.customTools` or `experimental.customToolkits`. Per-session fields such as `authConfigs`, `connectedAccounts`, `manageConnections`, `sandbox`, `multiAccount` and `preload` stay allowed.
|
|
628
|
+
* @param {string} [experimental.sessionConfigId] - ID of a saved Session config (`sc_…`) to apply at creation; see `composio.sessionConfigs`. Cannot be combined with `toolkits`, `tools`, `tags`, `hooks`, `experimental.customTools` or `experimental.customToolkits`. Per-session fields such as `authConfigs`, `connectedAccounts`, `manageConnections`, `sandbox`, `multiAccount` and `preload` stay allowed.
|
|
551
629
|
*/
|
|
552
630
|
export type ToolRouterCreateSessionConfig =
|
|
553
631
|
InlineAccessCreateSessionConfig | SavedConfigCreateSessionConfig;
|
|
@@ -565,11 +643,12 @@ type InlineAccessCreateSessionConfig = Omit<ParsedCreateSessionConfig, 'experime
|
|
|
565
643
|
/** Create input that takes its access policy from a saved Session config. */
|
|
566
644
|
type SavedConfigCreateSessionConfig = Omit<
|
|
567
645
|
ParsedCreateSessionConfig,
|
|
568
|
-
'toolkits' | 'tools' | 'tags' | 'experimental'
|
|
646
|
+
'toolkits' | 'tools' | 'tags' | 'hooks' | 'experimental'
|
|
569
647
|
> & {
|
|
570
648
|
toolkits?: never;
|
|
571
649
|
tools?: never;
|
|
572
650
|
tags?: never;
|
|
651
|
+
hooks?: never;
|
|
573
652
|
experimental: Omit<
|
|
574
653
|
ParsedCreateSessionExperimental,
|
|
575
654
|
'sessionConfigId' | 'customTools' | 'customToolkits'
|
|
@@ -745,6 +824,37 @@ export const ToolRouterSessionSearchResponseSchema = z.object({
|
|
|
745
824
|
});
|
|
746
825
|
export type ToolRouterSessionSearchResponse = z.infer<typeof ToolRouterSessionSearchResponseSchema>;
|
|
747
826
|
|
|
827
|
+
// --- Session hook results (camelCase) ---
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
* What one session hook decided about a call. `unavailable` means the hook
|
|
831
|
+
* could not be evaluated (for example it timed out); it does not block the
|
|
832
|
+
* call.
|
|
833
|
+
*/
|
|
834
|
+
export const ToolRouterHookResultSchema = z.discriminatedUnion('status', [
|
|
835
|
+
z.object({
|
|
836
|
+
id: z.string().describe('The hook id'),
|
|
837
|
+
status: z.literal('evaluated'),
|
|
838
|
+
outcome: z.enum(['continue', 'block']).describe('What the hook decided'),
|
|
839
|
+
}),
|
|
840
|
+
z.object({
|
|
841
|
+
id: z.string().describe('The hook id'),
|
|
842
|
+
status: z.literal('unavailable'),
|
|
843
|
+
}),
|
|
844
|
+
]);
|
|
845
|
+
export type ToolRouterHookResult = z.infer<typeof ToolRouterHookResultSchema>;
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Why a session hook blocked a call. `request_blocked`: a `before_execute` hook
|
|
849
|
+
* blocked it and the tool did not run. `response_blocked`: the tool ran and an
|
|
850
|
+
* `after_execute` hook blocked its result.
|
|
851
|
+
*/
|
|
852
|
+
export const ToolRouterBlockReasonSchema = z.object({
|
|
853
|
+
type: z.enum(['request_blocked', 'response_blocked']),
|
|
854
|
+
hookId: z.string(),
|
|
855
|
+
});
|
|
856
|
+
export type ToolRouterBlockReason = z.infer<typeof ToolRouterBlockReasonSchema>;
|
|
857
|
+
|
|
748
858
|
// --- Session execute response schema (camelCase) ---
|
|
749
859
|
|
|
750
860
|
export const ToolRouterSessionExecuteResponseSchema = z.object({
|
|
@@ -752,13 +862,19 @@ export const ToolRouterSessionExecuteResponseSchema = z.object({
|
|
|
752
862
|
error: z.string().nullable(),
|
|
753
863
|
logId: z.string(),
|
|
754
864
|
/**
|
|
755
|
-
* Whether the tool ran and succeeded (`completed`)
|
|
756
|
-
* (`failed`)
|
|
757
|
-
*
|
|
865
|
+
* Whether the tool ran and succeeded (`completed`), ran and failed
|
|
866
|
+
* (`failed`), or was blocked by a session hook (`blocked`). A failed
|
|
867
|
+
* execution can carry a `null` or empty `error`, so read this rather than
|
|
868
|
+
* `error` to tell them apart. A blocked call has empty `data` and the
|
|
869
|
+
* agent-facing message in `error`.
|
|
758
870
|
*/
|
|
759
|
-
resultType: z.enum(['completed', 'failed']),
|
|
871
|
+
resultType: z.enum(['completed', 'failed', 'blocked']),
|
|
760
872
|
/** Actual Instant usage charge when the Session opts into returning it. */
|
|
761
873
|
instantCharge: z.unknown().optional(),
|
|
874
|
+
/** Set when `resultType` is `blocked`: which hook blocked the call, and at which stage. */
|
|
875
|
+
blockReason: ToolRouterBlockReasonSchema.optional(),
|
|
876
|
+
/** Every session hook that applied to this call. Absent when none applied. */
|
|
877
|
+
hookResults: z.array(ToolRouterHookResultSchema).optional(),
|
|
762
878
|
});
|
|
763
879
|
export type ToolRouterSessionExecuteResponse = z.infer<
|
|
764
880
|
typeof ToolRouterSessionExecuteResponseSchema
|
|
@@ -766,7 +882,9 @@ export type ToolRouterSessionExecuteResponse = z.infer<
|
|
|
766
882
|
|
|
767
883
|
// --- Session proxy execute response (matches ToolProxyResponse from client) ---
|
|
768
884
|
|
|
769
|
-
|
|
885
|
+
/** A proxied request that was sent. */
|
|
886
|
+
export interface ToolRouterSessionProxyExecuteCompleted {
|
|
887
|
+
resultType: 'completed';
|
|
770
888
|
/** The HTTP status code returned from the proxied API */
|
|
771
889
|
status: number;
|
|
772
890
|
/** The response data from the proxied API */
|
|
@@ -780,8 +898,28 @@ export interface ToolRouterSessionProxyExecuteResponse {
|
|
|
780
898
|
url: string;
|
|
781
899
|
expiresAt?: string;
|
|
782
900
|
};
|
|
901
|
+
/** Every session hook that applied to this request. Absent when none applied. */
|
|
902
|
+
hookResults?: ToolRouterHookResult[];
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/** A proxied request that a session hook blocked. */
|
|
906
|
+
export interface ToolRouterSessionProxyExecuteBlocked {
|
|
907
|
+
resultType: 'blocked';
|
|
908
|
+
/** Which hook blocked the request, and at which stage. */
|
|
909
|
+
blockReason: ToolRouterBlockReason;
|
|
910
|
+
/** A message for the agent. It names the hook when the hook has a `name`. */
|
|
911
|
+
message: string;
|
|
912
|
+
/** Every session hook that applied to this request. */
|
|
913
|
+
hookResults?: ToolRouterHookResult[];
|
|
783
914
|
}
|
|
784
915
|
|
|
916
|
+
/**
|
|
917
|
+
* Result of `session.proxyExecute()`. Check `resultType` before reading
|
|
918
|
+
* `status`: a request a session hook blocked has no HTTP response.
|
|
919
|
+
*/
|
|
920
|
+
export type ToolRouterSessionProxyExecuteResponse =
|
|
921
|
+
ToolRouterSessionProxyExecuteCompleted | ToolRouterSessionProxyExecuteBlocked;
|
|
922
|
+
|
|
785
923
|
/**
|
|
786
924
|
* Experimental features on a tool router session.
|
|
787
925
|
* Contains features that may be modified or removed in future versions.
|
|
@@ -1096,6 +1234,11 @@ export const ToolRouterUpdateSessionConfigSchema = z
|
|
|
1096
1234
|
.describe(
|
|
1097
1235
|
'Replaces the proxy execute block; a boolean is shorthand for `{ enable }` and `null` removes it'
|
|
1098
1236
|
),
|
|
1237
|
+
hooks: z
|
|
1238
|
+
.array(ToolRouterHookSchema)
|
|
1239
|
+
.nullable()
|
|
1240
|
+
.optional()
|
|
1241
|
+
.describe('Replaces the whole list of hooks; `null` removes every hook'),
|
|
1099
1242
|
preload: z
|
|
1100
1243
|
.object({
|
|
1101
1244
|
tools: z.union([z.array(z.string()), z.literal('all')]).optional(),
|
|
@@ -1136,6 +1279,7 @@ export const ToolRouterUpdateSessionConfigSchema = z
|
|
|
1136
1279
|
config.toolkits !== undefined && 'toolkits',
|
|
1137
1280
|
config.tools !== undefined && 'tools',
|
|
1138
1281
|
config.tags !== undefined && 'tags',
|
|
1282
|
+
config.hooks !== undefined && 'hooks',
|
|
1139
1283
|
].filter((field): field is string => field !== false)
|
|
1140
1284
|
);
|
|
1141
1285
|
}
|
|
@@ -1151,8 +1295,8 @@ export const ToolRouterUpdateSessionConfigSchema = z
|
|
|
1151
1295
|
|
|
1152
1296
|
/**
|
|
1153
1297
|
* Options for `session.update()`. `experimental.sessionConfigId` applies a
|
|
1154
|
-
* saved Session config and cannot be combined with `toolkits`, `tools
|
|
1155
|
-
* `tags` (including `null`).
|
|
1298
|
+
* saved Session config and cannot be combined with `toolkits`, `tools`,
|
|
1299
|
+
* `tags` or `hooks` (including `null`).
|
|
1156
1300
|
*/
|
|
1157
1301
|
export type ToolRouterUpdateSessionConfig =
|
|
1158
1302
|
InlineAccessUpdateSessionConfig | SavedConfigUpdateSessionConfig;
|
|
@@ -1169,11 +1313,12 @@ type InlineAccessUpdateSessionConfig = Omit<ParsedUpdateSessionConfig, 'experime
|
|
|
1169
1313
|
/** Update input that applies a saved Session config. */
|
|
1170
1314
|
type SavedConfigUpdateSessionConfig = Omit<
|
|
1171
1315
|
ParsedUpdateSessionConfig,
|
|
1172
|
-
'toolkits' | 'tools' | 'tags' | 'experimental'
|
|
1316
|
+
'toolkits' | 'tools' | 'tags' | 'hooks' | 'experimental'
|
|
1173
1317
|
> & {
|
|
1174
1318
|
toolkits?: never;
|
|
1175
1319
|
tools?: never;
|
|
1176
1320
|
tags?: never;
|
|
1321
|
+
hooks?: never;
|
|
1177
1322
|
experimental: Omit<ParsedUpdateSessionExperimental, 'sessionConfigId'> & {
|
|
1178
1323
|
sessionConfigId: string;
|
|
1179
1324
|
};
|
|
@@ -8,10 +8,53 @@ import type {
|
|
|
8
8
|
import z from 'zod/v3';
|
|
9
9
|
import { ComposioToolInputRequiredError } from '../../errors/ToolRouterErrors';
|
|
10
10
|
import { ValidationError } from '../../errors/ValidationErrors';
|
|
11
|
-
import
|
|
11
|
+
import {
|
|
12
|
+
ToolRouterHookResultSchema,
|
|
13
|
+
type ToolRouterSessionExecuteResponse,
|
|
14
|
+
type ToolRouterSessionProxyExecuteResponse,
|
|
15
|
+
} from '../../types/toolRouter.types';
|
|
12
16
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The `result_type` values of a call that ran. `input_required` and `blocked`
|
|
19
|
+
* are handled before this is read. `hook_results` is validated in its own step
|
|
20
|
+
* so a shape drift there doesn't read as an unknown `result_type`.
|
|
21
|
+
*/
|
|
22
|
+
const ExecutedResponseSchema = z.object({
|
|
23
|
+
result_type: z.enum(['completed', 'failed']),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/** A call a session hook blocked. The generated client does not type it yet. */
|
|
27
|
+
const BlockedResponseSchema = z.object({
|
|
28
|
+
result_type: z.literal('blocked'),
|
|
29
|
+
reason: z.object({
|
|
30
|
+
type: z.enum(['request_blocked', 'response_blocked']),
|
|
31
|
+
hook_id: z.string(),
|
|
32
|
+
}),
|
|
33
|
+
message: z.string(),
|
|
34
|
+
// Only session execute carries these; session proxy execute leaves them out.
|
|
35
|
+
log_id: z.string().optional(),
|
|
36
|
+
instant_charge: z.unknown().optional(),
|
|
37
|
+
hook_results: z.array(ToolRouterHookResultSchema).optional(),
|
|
38
|
+
});
|
|
39
|
+
type BlockedResponse = z.infer<typeof BlockedResponseSchema>;
|
|
40
|
+
|
|
41
|
+
const IsBlockedSchema = z.object({ result_type: z.literal('blocked') });
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Reads a `blocked` response, or returns `undefined` for any other
|
|
45
|
+
* `result_type`. A `blocked` response that does not match its schema is
|
|
46
|
+
* raised rather than read as a result.
|
|
47
|
+
*/
|
|
48
|
+
function parseBlockedResponse(raw: unknown, subject: string): BlockedResponse | undefined {
|
|
49
|
+
if (!IsBlockedSchema.safeParse(raw).success) return undefined;
|
|
50
|
+
const blocked = BlockedResponseSchema.safeParse(raw);
|
|
51
|
+
if (!blocked.success) {
|
|
52
|
+
throw new ValidationError(`${subject} returned a malformed blocked response`, {
|
|
53
|
+
cause: blocked.error,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return blocked.data;
|
|
57
|
+
}
|
|
15
58
|
|
|
16
59
|
/**
|
|
17
60
|
* Whether a session tool execution succeeded. `resultType` decides: a `failed`
|
|
@@ -208,10 +251,27 @@ export function transformSearchResponse(raw: RawSearchResponse) {
|
|
|
208
251
|
* Transforms a raw session execute API response to camelCase.
|
|
209
252
|
*
|
|
210
253
|
* @throws {ComposioToolInputRequiredError} If the tool asked for user input instead of running
|
|
211
|
-
* @throws {ValidationError} If the response carries no `result_type`,
|
|
254
|
+
* @throws {ValidationError} If the response carries no `result_type`, one this SDK does not know, or malformed `hook_results`
|
|
212
255
|
*/
|
|
213
|
-
export function transformExecuteResponse(
|
|
256
|
+
export function transformExecuteResponse(
|
|
257
|
+
raw: SessionExecuteResponse,
|
|
258
|
+
toolSlug: string
|
|
259
|
+
): ToolRouterSessionExecuteResponse {
|
|
214
260
|
assertNotInputRequired(raw, `Tool ${toolSlug}`);
|
|
261
|
+
const blocked = parseBlockedResponse(raw, `Tool ${toolSlug}`);
|
|
262
|
+
if (blocked) {
|
|
263
|
+
// The agent reads `error`, so a blocked call reaches it as a failed call
|
|
264
|
+
// with the hook's message.
|
|
265
|
+
return {
|
|
266
|
+
data: {},
|
|
267
|
+
error: blocked.message,
|
|
268
|
+
logId: blocked.log_id ?? '',
|
|
269
|
+
resultType: 'blocked',
|
|
270
|
+
blockReason: { type: blocked.reason.type, hookId: blocked.reason.hook_id },
|
|
271
|
+
...(blocked.hook_results !== undefined && { hookResults: blocked.hook_results }),
|
|
272
|
+
...(blocked.instant_charge !== undefined && { instantCharge: blocked.instant_charge }),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
215
275
|
const executed = ExecutedResponseSchema.safeParse(raw);
|
|
216
276
|
if (!executed.success) {
|
|
217
277
|
throw new ValidationError(
|
|
@@ -219,11 +279,69 @@ export function transformExecuteResponse(raw: SessionExecuteResponse, toolSlug:
|
|
|
219
279
|
{ cause: executed.error }
|
|
220
280
|
);
|
|
221
281
|
}
|
|
282
|
+
const hookResults = CompletedHookResultsSchema.safeParse(raw);
|
|
283
|
+
if (!hookResults.success) {
|
|
284
|
+
throw new ValidationError(`Tool ${toolSlug} returned malformed hook results`, {
|
|
285
|
+
cause: hookResults.error,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
222
288
|
return {
|
|
223
289
|
data: raw.data,
|
|
224
290
|
error: raw.error,
|
|
225
291
|
logId: raw.log_id,
|
|
226
292
|
resultType: executed.data.result_type,
|
|
227
293
|
...(raw.instant_charge !== undefined && { instantCharge: raw.instant_charge }),
|
|
294
|
+
...(hookResults.data.hook_results !== undefined && {
|
|
295
|
+
hookResults: hookResults.data.hook_results,
|
|
296
|
+
}),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Hook results on a call that ran: session execute, or a proxied request that was sent. */
|
|
301
|
+
const CompletedHookResultsSchema = z.object({
|
|
302
|
+
hook_results: z.array(ToolRouterHookResultSchema).optional(),
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Projects a session proxy execute response onto the SDK's shape: a request a
|
|
307
|
+
* session hook blocked comes back as `resultType: 'blocked'` with no HTTP
|
|
308
|
+
* response.
|
|
309
|
+
*/
|
|
310
|
+
export function transformProxyExecuteResponse(
|
|
311
|
+
raw: SessionProxyExecuteResponse,
|
|
312
|
+
subject: string
|
|
313
|
+
): ToolRouterSessionProxyExecuteResponse {
|
|
314
|
+
assertNotInputRequired(raw, subject);
|
|
315
|
+
const blocked = parseBlockedResponse(raw, subject);
|
|
316
|
+
if (blocked) {
|
|
317
|
+
return {
|
|
318
|
+
resultType: 'blocked',
|
|
319
|
+
blockReason: { type: blocked.reason.type, hookId: blocked.reason.hook_id },
|
|
320
|
+
message: blocked.message,
|
|
321
|
+
...(blocked.hook_results !== undefined && { hookResults: blocked.hook_results }),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
const hookResults = CompletedHookResultsSchema.safeParse(raw);
|
|
325
|
+
if (!hookResults.success) {
|
|
326
|
+
throw new ValidationError(`${subject} returned malformed hook results`, {
|
|
327
|
+
cause: hookResults.error,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
resultType: 'completed',
|
|
332
|
+
status: raw.status,
|
|
333
|
+
data: raw.data,
|
|
334
|
+
headers: raw.headers,
|
|
335
|
+
...(raw.binary_data && {
|
|
336
|
+
binaryData: {
|
|
337
|
+
contentType: raw.binary_data.content_type,
|
|
338
|
+
size: raw.binary_data.size,
|
|
339
|
+
url: raw.binary_data.url,
|
|
340
|
+
expiresAt: raw.binary_data.expires_at,
|
|
341
|
+
},
|
|
342
|
+
}),
|
|
343
|
+
...(hookResults.data.hook_results !== undefined && {
|
|
344
|
+
hookResults: hookResults.data.hook_results,
|
|
345
|
+
}),
|
|
228
346
|
};
|
|
229
347
|
}
|