@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 70 lines 2.69 KB
Raw
1 import { Effect } from 'effect';
2 import * as FileSystem from 'effect/FileSystem';
3 import * as Path from 'effect/Path';
4 import { rejectLocalFilesWithoutUpload } from 'src/services/tool-file-uploads';
5 import {
6 getOrFetchToolInputDefinition,
7 ToolInputValidationError,
8 validateToolInputArguments,
9 validateToolInputArgumentsWithDefinition,
10 } from 'src/services/tool-input-validation';
11 import { isMetaToolSlug } from 'src/utils/meta-tool-slugs';
12 import { perfDebugLog } from './tools.execute.debug';
13
14 // What a dry run checks, matching the real call: the arguments against the
15 // schema unless checks are skipped, then any local file the tool cannot upload.
16 // The real call refuses that file whatever the skip flags say, loading the
17 // schema for it and going on without one, and never for a meta tool, which
18 // uploads nothing. Returns the schema only when the arguments were checked.
19 export const checkDryRunArguments = (params: {
20 readonly slug: string;
21 readonly args: Record<string, unknown>;
22 readonly projectScope: { readonly orgId?: string; readonly projectId?: string };
23 readonly verificationDisabled: boolean;
24 }) =>
25 Effect.gen(function* () {
26 const { slug, args, verificationDisabled } = params;
27 if (verificationDisabled && isMetaToolSlug(slug)) return null;
28
29 const definition = verificationDisabled
30 ? yield* getOrFetchToolInputDefinition(slug, params.projectScope).pipe(
31 Effect.catch(() => Effect.succeed(null))
32 )
33 : yield* getOrFetchToolInputDefinition(slug, params.projectScope);
34 if (!definition) return null;
35
36 if (!verificationDisabled) {
37 yield* validateToolInputArgumentsWithDefinition(slug, args, definition);
38 }
39 if (!isMetaToolSlug(slug)) {
40 yield* rejectLocalFilesWithoutUpload({
41 fs: yield* FileSystem.FileSystem,
42 path: yield* Path.Path,
43 toolSlug: slug,
44 arguments_: args,
45 inputSchema: definition.schema,
46 });
47 }
48 return verificationDisabled ? null : definition;
49 });
50
51 // Gates the tool call: the request is only sent once the arguments pass. A
52 // schema that cannot be loaded does not block the call, the server validates too.
53 export const validateToolInputBeforeExecute = (params: {
54 readonly slug: string;
55 readonly args: Record<string, unknown>;
56 readonly resolvedProject: {
57 readonly orgId: string;
58 readonly projectId: string;
59 };
60 }) =>
61 validateToolInputArguments(params.slug, params.args, {
62 orgId: params.resolvedProject.orgId,
63 projectId: params.resolvedProject.projectId,
64 }).pipe(
65 Effect.asVoid,
66 Effect.catchIf(
67 error => !(error instanceof ToolInputValidationError),
68 () => perfDebugLog('execute.validation.schema_unavailable', { slug: params.slug })
69 )
70 );