| 886 | | DynamicTool<ToolOutputT>; |
| 887 | |
| 888 | export function tool< |
| 889 | SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema = |
| 890 | InteropZodObject, |
| 891 | NameT extends string = string, |
| 892 | SchemaOutputT = ToolInputSchemaOutputType<SchemaT>, |
| 893 | SchemaInputT = ToolInputSchemaInputType<SchemaT>, |
| 894 | ToolOutputT = ToolOutputType, |
| 895 | ToolEventT = ToolEventType, |
| 896 | TState = unknown, |
| 897 | TContext = unknown, |
| 898 | >( |
| 899 | func: ( |
| 900 | input: SchemaOutputT, |
| 901 | runtime: ToolRuntime<TState, TContext> |
| 902 | ) => ToolOutputT | Promise<ToolOutputT>, |
| 903 | fields: ToolWrapperParams<SchemaT, NameT> |
| 904 | ): |
| 905 | | DynamicStructuredTool< |
| 906 | SchemaT, |
| 907 | SchemaOutputT, |
| 908 | SchemaInputT, |
| 909 | ToolOutputT, |
| 910 | ToolEventT, |
| 911 | NameT |
| 912 | > |
| 913 | | DynamicTool<ToolOutputT, ToolEventT> { |
| 914 | const isSimpleStringSchema = isSimpleStringZodSchema(fields.schema); |
| 915 | const isStringJSONSchema = validatesOnlyStrings(fields.schema); |
| 916 | |
| 917 | // If the schema is not provided, or it's a simple string schema, create a DynamicTool |
| 918 | if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) { |
| 919 | return new DynamicTool<ToolOutputT, ToolEventT>({ |
| 920 | ...fields, |
| 921 | description: |
| 922 | fields.description ?? |
| 923 | (fields.schema as { description?: string } | undefined)?.description ?? |
| 924 | `${fields.name} tool`, |
| 925 | func: async (input, runManager, config) => { |
| 926 | return new Promise<ToolOutputT>((resolve, reject) => { |
| 927 | const childConfig = patchConfig(config, { |
| 928 | callbacks: runManager?.getChild(), |
| 929 | }); |
| 930 | // oxlint-disable-next-line no-void |
| 931 | void AsyncLocalStorageProviderSingleton.runWithConfig( |
| 932 | pickRunnableConfigKeys(childConfig), |
| 933 | async () => { |
| 934 | try { |
| 935 | // oxlint-disable-next-line @typescript-eslint/no-explicit-any |
| 936 | resolve(func(input as any, childConfig as any)); |
| 937 | } catch (e) { |
| 938 | reject(e); |
| 939 | } |
| 940 | } |
| 941 | ); |
| 942 | }); |
| 943 | }, |
| 944 | }); |
| 945 | } |