@composio/core 0.15.0 → 0.16.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.
@@ -470,14 +470,12 @@ export class Tools<
470
470
  'important' in queryParams.data ? queryParams.data.important : shouldAutoApplyImportant;
471
471
 
472
472
  // check if the query params contains atleast one of the following: tools, toolkits, search, authConfigIds
473
- if (
474
- !(
475
- 'tools' in queryParams.data ||
476
- 'toolkits' in queryParams.data ||
477
- 'search' in queryParams.data ||
478
- 'authConfigIds' in queryParams.data
479
- )
480
- ) {
473
+ if (!(
474
+ 'tools' in queryParams.data ||
475
+ 'toolkits' in queryParams.data ||
476
+ 'search' in queryParams.data ||
477
+ 'authConfigIds' in queryParams.data
478
+ )) {
481
479
  throw new ValidationError(
482
480
  'Invalid tool list parameters, atleast one of the following parameters is required: tools, toolkits, search, authConfigIds'
483
481
  );
@@ -766,33 +764,44 @@ export class Tools<
766
764
  const { signal: _, ...modifiers } = options ?? {};
767
765
 
768
766
  if (typeof arg2 === 'string') {
769
- const tool = await this.getRawComposioToolBySlug(
770
- arg2,
771
- {
772
- modifySchema: options?.modifySchema as TransformToolSchemaModifier,
773
- },
774
- requestOptions
775
- );
776
- return this.wrapToolsForProvider(
777
- userId,
778
- [tool],
779
- modifiers as ExecuteToolModifiers
780
- ) as TToolCollection;
767
+ const rawTool = await this.getRawComposioToolBySlug(arg2, undefined, requestOptions);
768
+ const [tool] = await this.applySchemaModifiers([rawTool], options?.modifySchema);
769
+ return this.wrapToolsForProvider(userId, [tool], modifiers as ExecuteToolModifiers, [
770
+ rawTool,
771
+ ]) as TToolCollection;
781
772
  } else {
782
- const tools = await this.getRawComposioTools(
783
- arg2,
784
- {
785
- modifySchema: options?.modifySchema as TransformToolSchemaModifier,
786
- },
787
- requestOptions
788
- );
773
+ const rawTools = await this.getRawComposioTools(arg2, undefined, requestOptions);
774
+ const tools = await this.applySchemaModifiers(rawTools, options?.modifySchema);
789
775
  return this.wrapToolsForProvider(
790
776
  userId,
791
777
  tools,
792
- modifiers as ExecuteToolModifiers
778
+ modifiers as ExecuteToolModifiers,
779
+ rawTools
793
780
  ) as TToolCollection;
794
781
  }
795
782
  }
783
+
784
+ private async applySchemaModifiers(
785
+ tools: Tool[],
786
+ modifier?: TransformToolSchemaModifier
787
+ ): Promise<Tool[]> {
788
+ if (modifier && typeof modifier !== 'function') {
789
+ throw new ComposioInvalidModifierError('Invalid schema modifier. Not a function.');
790
+ }
791
+ return Promise.all(
792
+ tools.map(tool => {
793
+ const schema = ToolSchema.parse(tool);
794
+ return modifier
795
+ ? modifier({
796
+ toolSlug: tool.slug,
797
+ toolkitSlug: tool.toolkit?.slug ?? 'unknown',
798
+ schema,
799
+ })
800
+ : schema;
801
+ })
802
+ );
803
+ }
804
+
796
805
  /**
797
806
  * @internal
798
807
  * Creates a global execute tool function.
@@ -817,14 +826,16 @@ export class Tools<
817
826
  * @param userId - The user id to get the tools for
818
827
  * @param tools - The tools to wrap
819
828
  * @param modifiers - The modifiers to be applied to the tools
829
+ * @param rawTools - The fetched schemas used during execution
820
830
  * @returns The wrapped tools
821
831
  */
822
832
  wrapToolsForProvider<T extends TProvider>(
823
833
  userId: string,
824
834
  tools: Tool[],
825
- modifiers?: ExecuteToolModifiers
835
+ modifiers?: ExecuteToolModifiers,
836
+ rawTools: Tool[] = tools.map(tool => ToolSchema.parse(tool))
826
837
  ): ReturnType<T['wrapTools']> {
827
- const executeToolFn = this.createExecuteToolFn(userId, modifiers);
838
+ const executeToolFn = this.createExecuteToolFn(userId, modifiers, rawTools);
828
839
  return this.provider.wrapTools(tools, executeToolFn) as ReturnType<T['wrapTools']>;
829
840
  }
830
841
 
@@ -854,21 +865,27 @@ export class Tools<
854
865
  *
855
866
  * @param {string} userId - The user id
856
867
  * @param {ExecuteToolModifiers} modifiers - The modifiers to be applied to the tool
868
+ * @param {Tool[]} tools - The fetched tools available to the provider
857
869
  * @returns {ExecuteToolFn} The execute tool function
858
870
  */
859
- private createExecuteToolFn(userId: string, modifiers?: ExecuteToolModifiers): ExecuteToolFn {
871
+ private createExecuteToolFn(
872
+ userId: string,
873
+ modifiers: ExecuteToolModifiers | undefined,
874
+ tools: Tool[]
875
+ ): ExecuteToolFn {
876
+ const toolBySlug = new Map(tools.map(tool => [tool.slug.toUpperCase(), tool]));
860
877
  const executeToolFn = async (toolSlug: string, input: Record<string, unknown>) => {
861
- return await this.execute(
862
- toolSlug,
863
- {
864
- userId,
865
- arguments: input,
866
- // dangerously skip version check for agentic tool execution via providers
867
- // this can be safe because most agentic flows users fetch latest version and then execute the tool
868
- dangerouslySkipVersionCheck: true,
869
- },
870
- modifiers
871
- );
878
+ const body: ToolExecuteParams = {
879
+ userId,
880
+ arguments: input,
881
+ // dangerously skip version check for agentic tool execution via providers
882
+ // this can be safe because most agentic flows users fetch latest version and then execute the tool
883
+ dangerouslySkipVersionCheck: true,
884
+ };
885
+ const tool = toolBySlug.get(toolSlug.toUpperCase());
886
+ return tool
887
+ ? this.executeWithTool(toolSlug, this.parseToolExecuteParams(body), modifiers, tool)
888
+ : this.execute(toolSlug, body, modifiers);
872
889
  };
873
890
  return executeToolFn;
874
891
  }
@@ -961,6 +978,52 @@ export class Tools<
961
978
  }
962
979
  }
963
980
 
981
+ private async executeWithTool(
982
+ slug: string,
983
+ body: ToolExecuteParams,
984
+ options: (ExecuteToolModifiers & ComposioRequestOptions) | undefined,
985
+ tool: Tool
986
+ ): Promise<ToolExecuteResponse> {
987
+ const requestOptions: ComposioRequestOptions | undefined =
988
+ options?.signal != null ? { signal: options.signal } : undefined;
989
+ const { signal: _, ...modifiers } = options ?? {};
990
+ const toolkitSlug = tool.toolkit?.slug ?? 'unknown';
991
+
992
+ const params = await this.applyBeforeExecuteModifiers(
993
+ tool,
994
+ {
995
+ toolSlug: slug,
996
+ toolkitSlug,
997
+ params: body,
998
+ },
999
+ modifiers as ExecuteToolModifiers,
1000
+ requestOptions
1001
+ );
1002
+
1003
+ let result = await this.executeComposioTool(tool, params, requestOptions);
1004
+
1005
+ result = await this.applyAfterExecuteModifiers(
1006
+ tool,
1007
+ {
1008
+ toolSlug: slug,
1009
+ toolkitSlug,
1010
+ result,
1011
+ },
1012
+ (modifiers as ExecuteToolModifiers).afterExecute,
1013
+ requestOptions
1014
+ );
1015
+
1016
+ return result;
1017
+ }
1018
+
1019
+ private parseToolExecuteParams(body: ToolExecuteParams): ToolExecuteParams {
1020
+ const executeParams = ToolExecuteParamsSchema.safeParse(body);
1021
+ if (!executeParams.success) {
1022
+ throw new ValidationError('Invalid tool execute parameters', { cause: executeParams.error });
1023
+ }
1024
+ return executeParams.data;
1025
+ }
1026
+
964
1027
  /**
965
1028
  * Executes a given tool with the provided parameters.
966
1029
  *
@@ -1046,51 +1109,19 @@ export class Tools<
1046
1109
  body: ToolExecuteParams,
1047
1110
  options?: ExecuteToolModifiers & ComposioRequestOptions
1048
1111
  ): Promise<ToolExecuteResponse> {
1049
- const executeParams = ToolExecuteParamsSchema.safeParse(body);
1050
- if (!executeParams.success) {
1051
- throw new ValidationError('Invalid tool execute parameters', { cause: executeParams.error });
1052
- }
1112
+ const executeParams = this.parseToolExecuteParams(body);
1053
1113
 
1054
1114
  const requestOptions: ComposioRequestOptions | undefined =
1055
1115
  options?.signal != null ? { signal: options.signal } : undefined;
1056
- const { signal: _, ...modifiers } = options ?? {};
1057
1116
 
1058
1117
  const tool = await this.getRawComposioToolBySlug(
1059
1118
  slug,
1060
1119
  {
1061
- version: body.version,
1062
- },
1063
- requestOptions
1064
- );
1065
- const toolkitSlug = tool.toolkit?.slug ?? 'unknown';
1066
-
1067
- // Apply before execute modifiers
1068
- const params = await this.applyBeforeExecuteModifiers(
1069
- tool,
1070
- {
1071
- toolSlug: slug,
1072
- toolkitSlug,
1073
- params: executeParams.data,
1120
+ version: executeParams.version,
1074
1121
  },
1075
- modifiers as ExecuteToolModifiers,
1076
1122
  requestOptions
1077
1123
  );
1078
-
1079
- let result = await this.executeComposioTool(tool, params, requestOptions);
1080
-
1081
- // Apply after execute modifiers
1082
- result = await this.applyAfterExecuteModifiers(
1083
- tool,
1084
- {
1085
- toolSlug: slug,
1086
- toolkitSlug,
1087
- result,
1088
- },
1089
- (modifiers as ExecuteToolModifiers).afterExecute,
1090
- requestOptions
1091
- );
1092
-
1093
- return result;
1124
+ return this.executeWithTool(slug, executeParams, options, tool);
1094
1125
  }
1095
1126
 
1096
1127
  /**
@@ -139,13 +139,19 @@ const ParametersSchema = z.preprocess(
139
139
  oneOf: z.array(JSONSchemaPropertySchema).optional(),
140
140
  allOf: z.array(JSONSchemaPropertySchema).optional(),
141
141
  not: JSONSchemaPropertySchema.optional(),
142
- properties: z.record(z.string(), JSONSchemaPropertySchema),
142
+ properties: z.record(z.string(), JSONSchemaPropertySchema).optional(),
143
143
  required: z.array(z.string()).optional(),
144
144
  title: z.string().optional(),
145
145
  default: z.unknown().optional(),
146
146
  nullable: z.boolean().optional(),
147
147
  description: z.string().optional(),
148
- additionalProperties: z.boolean().default(false).optional(),
148
+ // Dynamic-key constraints the Composio API ships at the parameters root.
149
+ // Both were previously lost on parse: `patternProperties` was stripped as an
150
+ // unknown key, and a schema-valued `additionalProperties` was rejected
151
+ // outright by the boolean-only schema. Omission still stays omission —
152
+ // downstream converters, not the parser, decide the implicit policy.
153
+ patternProperties: z.record(z.string(), JSONSchemaPropertySchema).optional(),
154
+ additionalProperties: z.union([z.boolean(), JSONSchemaPropertySchema]).optional(),
149
155
  // Definition blocks targeted by `$ref` pointers. The Composio API ships
150
156
  // these at the parameters root (e.g. `data` → `$ref` → `#/$defs/...`), but
151
157
  // because `z.object` strips unknown keys they were being dropped on parse —
@@ -258,6 +258,91 @@ export function dereferenceJsonSchema<T = unknown>(
258
258
  return out as T;
259
259
  }
260
260
 
261
+ /**
262
+ * Returns a deep-cloned JSON Schema in which every node that carries a
263
+ * `properties` keyword also carries `type: "object"` when it has no explicit
264
+ * `type`. OpenAI tolerates the omission, but Google Gemini enforces OpenAPI
265
+ * 3.0 strictly and rejects function declarations whose nested objects are
266
+ * missing the `type` (see https://github.com/ComposioHQ/composio/issues/4022).
267
+ *
268
+ * The function follows JSON Schema's schema-bearing keywords while keeping
269
+ * property maps and instance values as containers. Nodes that already declare
270
+ * a `type` are left untouched.
271
+ */
272
+ export function ensureObjectTypeOnProperties<T = unknown>(schema: T): T {
273
+ type WalkMode = 'schema' | 'schema-array' | 'schema-map' | 'dependencies-map' | 'value';
274
+
275
+ const schemaKeywords = new Set([
276
+ 'additionalItems',
277
+ 'additionalProperties',
278
+ 'contains',
279
+ 'contentSchema',
280
+ 'else',
281
+ 'if',
282
+ 'not',
283
+ 'propertyNames',
284
+ 'then',
285
+ 'unevaluatedItems',
286
+ 'unevaluatedProperties',
287
+ ]);
288
+ const schemaArrayKeywords = new Set(['allOf', 'anyOf', 'oneOf', 'prefixItems']);
289
+ const schemaMapKeywords = new Set([
290
+ '$defs',
291
+ 'definitions',
292
+ 'dependentSchemas',
293
+ 'patternProperties',
294
+ 'properties',
295
+ ]);
296
+
297
+ function walk(value: unknown, mode: WalkMode, depth = 0): unknown {
298
+ if (depth > MAX_NODE_DEPTH) {
299
+ throw new RangeError(`JSON Schema exceeds maximum nesting depth of ${MAX_NODE_DEPTH}`);
300
+ }
301
+
302
+ if (Array.isArray(value)) {
303
+ const itemMode = mode === 'schema-array' ? 'schema' : 'value';
304
+ return value.map(item => walk(item, itemMode, depth + 1));
305
+ }
306
+
307
+ if (!isPlainObject(value)) return value;
308
+
309
+ const clone: Record<string, unknown> = {};
310
+ for (const [key, child] of Object.entries(value)) {
311
+ let childMode: WalkMode = 'value';
312
+ if (mode === 'schema-map') {
313
+ childMode = 'schema';
314
+ } else if (mode === 'dependencies-map') {
315
+ childMode = Array.isArray(child) ? 'value' : 'schema';
316
+ } else if (mode === 'schema') {
317
+ if (schemaMapKeywords.has(key)) {
318
+ childMode = 'schema-map';
319
+ } else if (schemaArrayKeywords.has(key) || (key === 'items' && Array.isArray(child))) {
320
+ childMode = 'schema-array';
321
+ } else if (schemaKeywords.has(key) || key === 'items') {
322
+ childMode = 'schema';
323
+ } else if (key === 'dependencies') {
324
+ childMode = 'dependencies-map';
325
+ }
326
+ }
327
+
328
+ Object.defineProperty(clone, key, {
329
+ configurable: true,
330
+ enumerable: true,
331
+ value: walk(child, childMode, depth + 1),
332
+ writable: true,
333
+ });
334
+ }
335
+
336
+ if (mode === 'schema' && clone.properties !== undefined && clone.type === undefined) {
337
+ clone.type = 'object';
338
+ }
339
+
340
+ return clone;
341
+ }
342
+
343
+ return walk(schema, 'schema') as T;
344
+ }
345
+
261
346
  /**
262
347
  * Returns a deep-cloned JSON Schema whose `required` arrays contain each entry
263
348
  * at most once. Duplicate entries are invalid in JSON Schema 2020-12 but do
@@ -328,7 +413,7 @@ export const removeNonRequiredProperties = <
328
413
  type: 'object';
329
414
  properties: Record<string, unknown>;
330
415
  required?: string[];
331
- additionalProperties?: boolean;
416
+ additionalProperties?: unknown;
332
417
  },
333
418
  >(
334
419
  schema: T