@tambo-ai/react 1.0.0-rc.3 → 1.0.0-rc.4
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/model/component-metadata.d.ts +4 -4
- package/dist/model/component-metadata.js.map +1 -1
- package/dist/util/registry-validators.js +1 -1
- package/dist/util/registry-validators.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.d.ts +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.js +9 -9
- package/dist/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1.d.ts +9 -0
- package/dist/v1/hooks/use-tambo-v1.d.ts.map +1 -1
- package/dist/v1/hooks/use-tambo-v1.js +31 -3
- package/dist/v1/hooks/use-tambo-v1.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1.test.js +122 -3
- package/dist/v1/hooks/use-tambo-v1.test.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.test.js +2 -2
- package/dist/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
- package/dist/v1/types/message.d.ts +0 -2
- package/dist/v1/types/message.d.ts.map +1 -1
- package/dist/v1/types/message.js.map +1 -1
- package/dist/v1/types/thread.d.ts +2 -2
- package/dist/v1/types/thread.d.ts.map +1 -1
- package/dist/v1/types/thread.js.map +1 -1
- package/dist/v1/utils/event-accumulator.d.ts +5 -5
- package/dist/v1/utils/event-accumulator.d.ts.map +1 -1
- package/dist/v1/utils/event-accumulator.js +2 -2
- package/dist/v1/utils/event-accumulator.js.map +1 -1
- package/dist/v1/utils/event-accumulator.test.js +11 -11
- package/dist/v1/utils/event-accumulator.test.js.map +1 -1
- package/esm/model/component-metadata.d.ts +4 -4
- package/esm/model/component-metadata.js.map +1 -1
- package/esm/util/registry-validators.js +1 -1
- package/esm/util/registry-validators.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.d.ts +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.js +9 -9
- package/esm/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1.d.ts +9 -0
- package/esm/v1/hooks/use-tambo-v1.d.ts.map +1 -1
- package/esm/v1/hooks/use-tambo-v1.js +32 -4
- package/esm/v1/hooks/use-tambo-v1.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1.test.js +122 -3
- package/esm/v1/hooks/use-tambo-v1.test.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.test.js +2 -2
- package/esm/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
- package/esm/v1/types/message.d.ts +0 -2
- package/esm/v1/types/message.d.ts.map +1 -1
- package/esm/v1/types/message.js.map +1 -1
- package/esm/v1/types/thread.d.ts +2 -2
- package/esm/v1/types/thread.d.ts.map +1 -1
- package/esm/v1/types/thread.js.map +1 -1
- package/esm/v1/utils/event-accumulator.d.ts +5 -5
- package/esm/v1/utils/event-accumulator.d.ts.map +1 -1
- package/esm/v1/utils/event-accumulator.js +2 -2
- package/esm/v1/utils/event-accumulator.js.map +1 -1
- package/esm/v1/utils/event-accumulator.test.js +11 -11
- package/esm/v1/utils/event-accumulator.test.js.map +1 -1
- package/package.json +3 -3
|
@@ -207,7 +207,7 @@ export type TamboToolStandardSchema<Input extends StandardSchemaV1 = StandardSch
|
|
|
207
207
|
* inputSchema and outputSchema with either Standard Schema compliant validators
|
|
208
208
|
* (like Zod 3.25.76, Zod 4.x) or raw JSON Schema objects.
|
|
209
209
|
* @deprecated replace `toolSchema` with `inputSchema` and `outputSchema` instead.
|
|
210
|
-
* @see {@link https://docs.tambo.co/reference/react-sdk/migration
|
|
210
|
+
* @see {@link https://docs.tambo.co/reference/react-sdk/migration}
|
|
211
211
|
*/
|
|
212
212
|
export type UnsupportedSchemaTamboTool = Omit<TamboTool, "tool" | "inputSchema" | "outputSchema"> & {
|
|
213
213
|
/**
|
|
@@ -297,7 +297,7 @@ type OptionalSchemaProps<T> = Omit<T, "inputSchema" | "outputSchema"> & {
|
|
|
297
297
|
*/
|
|
298
298
|
export interface RegisterToolsFn {
|
|
299
299
|
/**
|
|
300
|
-
* @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration
|
|
300
|
+
* @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration | Migration Guide} to update
|
|
301
301
|
* your tool definitions to use `inputSchema` and `outputSchema` instead.
|
|
302
302
|
*/
|
|
303
303
|
(tools: UnsupportedSchemaTamboTool[], warnOnOverwrite?: boolean): void;
|
|
@@ -328,7 +328,7 @@ export interface RegisterToolFn {
|
|
|
328
328
|
(tool: TamboToolUnknown, warnOnOverwrite?: boolean): void;
|
|
329
329
|
(tool: TamboTool, warnOnOverwrite?: boolean): void;
|
|
330
330
|
/**
|
|
331
|
-
* @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration
|
|
331
|
+
* @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration | Migration Guide} to update
|
|
332
332
|
* your tool definitions to use `inputSchema` and `outputSchema` instead.
|
|
333
333
|
* @param tool - The unsupported schema Tambo tool to register
|
|
334
334
|
* @param warnOnOverwrite - Whether to warn if the tool is being overwritten
|
|
@@ -341,7 +341,7 @@ export interface RegisterToolFn {
|
|
|
341
341
|
*/
|
|
342
342
|
export interface DefineToolFn {
|
|
343
343
|
/**
|
|
344
|
-
* @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration
|
|
344
|
+
* @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration | Migration Guide} to update
|
|
345
345
|
* your tool definitions to use `inputSchema` and `outputSchema` instead.
|
|
346
346
|
* @param tool The tool definition to register
|
|
347
347
|
* @returns The registered tool definition
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"component-metadata.js","sourceRoot":"","sources":["../../src/model/component-metadata.ts"],"names":[],"mappings":"","sourcesContent":["import type { ToolAnnotations as MCPToolAnnotations } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { JSONSchema7 } from \"json-schema\";\nimport { ComponentType } from \"react\";\n\n/**\n * A schema type that accepts either a Standard Schema compliant validator\n * (e.g., Zod, Valibot, ArkType) or a raw JSON Schema object.\n *\n * Standard Schema is a specification that provides a unified interface for\n * TypeScript validation libraries. Libraries like Zod implement this spec,\n * allowing us to accept any compliant validator without depending on a specific library.\n * @see https://standardschema.dev/\n */\nexport type SupportedSchema<Input = unknown, Output = Input> =\n | StandardSchemaV1<Input, Output>\n | JSONSchema7;\n\n/**\n * Annotations describing a tool's behavior, aligned with the MCP (Model Context Protocol)\n * specification. These hints help clients understand how tools behave and can be used\n * to optimize tool execution strategies.\n * @see https://modelcontextprotocol.io/specification/2025-06-18/schema#toolannotations\n */\nexport type ToolAnnotations = MCPToolAnnotations & {\n /**\n * Indicates that the tool is safe to be called repeatedly while a response is\n * being streamed. This is typically used for read-only tools that do not\n * cause side effects.\n */\n tamboStreamableHint?: boolean;\n};\n\n/** Extension of the ToolParameters interface from Tambo AI to include JSONSchema definition */\nexport type ParameterSpec = TamboAI.ToolParameters & {\n schema?: JSONSchema7;\n};\n\n/**\n * Extends the base ContextTool interface from Tambo AI to include schema information\n * for parameter validation.\n */\nexport interface ComponentContextToolMetadata\n extends TamboAI.ComponentContextToolMetadata {\n parameters: ParameterSpec[];\n /**\n * Optional per-tool call limit. When set, this overrides the project's\n * global tool call limit for this specific tool.\n *\n * This is useful for tools that should only be called once or twice\n * regardless of the project's global limit.\n */\n maxCalls?: number;\n /**\n * Annotations describing the tool's behavior. See {@link ToolAnnotations}.\n */\n annotations?: ToolAnnotations;\n}\n\nexport interface ComponentContextTool {\n getComponentContext: (...args: any[]) => Promise<any>;\n definition: ComponentContextToolMetadata;\n}\n\nexport interface RegisteredComponent extends TamboAI.AvailableComponent {\n component: ComponentType<any>;\n loadingComponent?: ComponentType<any>;\n}\n\nexport type ComponentRegistry = Record<string, RegisteredComponent>;\n\nexport type TamboToolRegistry = Record<string, TamboTool>;\n\n/**\n * A JSON Schema that is compatible with the MCP.\n * This is a simplified JSON Schema that is compatible with the MCPClient and the tool's inputSchema.\n *\n * Do not export this type from the SDK.\n */\nexport type JSONSchemaLite = JSONSchema7 & {\n description?: string;\n};\n\ntype MaybeAsync<T> = T | Promise<T>;\n\n/**\n * TamboTool is a type that represents a tool that can be registered with Tambo.\n *\n * It is preferable to use the `defineTool` helper function to create tools, as\n * it provides better type inference and safety.\n * @example\n * ```ts\n * import { TamboTool, defineTool } from \"@tambo-ai/react\";\n * import { z } from \"zod\";\n *\n * const locationToLatLon = defineTool({\n * name: \"location_to_latlon\",\n * description:\n * \"Fetch latitude and longitude from a location string. Returns an object with 'lat' and 'lon' properties.\",\n * tool: async ({ location }) => getLatLonFromLocation(location),\n * inputSchema: z.object({\n * location: z.string(),\n * }),\n * outputSchema: z.object({\n * lat: z.number(),\n * lon: z.number(),\n * }),\n * });\n * ```\n */\nexport interface TamboTool<\n Params = any,\n Returns = any,\n Rest extends any[] = [],\n> {\n /**\n * Unique identifier for the tool\n */\n name: string;\n /**\n * Description of what the tool does - used by AI to determine when to use it\n */\n description: string;\n /**\n * Optional human-readable name of the tool for display purposes.\n */\n title?: string;\n /**\n * Optional limit for how many times this tool may be called while\n * generating a single response. If present, this value overrides the\n * project's global `maxToolCallLimit` for this tool.\n * @example 1\n */\n maxCalls?: number;\n /**\n * Annotations describing the tool's behavior, aligned with the MCP specification.\n * Use `tamboStreamableHint: true` to enable streaming execution of partial tool calls.\n * @see {@link ToolAnnotations}\n * @example\n * ```ts\n * const setTextTool: TamboTool<{ text: string }> = {\n * name: \"set_text\",\n * description: \"Set the displayed text\",\n * annotations: {\n * tamboStreamableHint: true, // tool is safe for streaming calls\n * },\n * tool: ({ text }) => setText(text),\n * inputSchema: z.object({ text: z.string() }),\n * outputSchema: z.void(),\n * };\n * ```\n */\n annotations?: ToolAnnotations;\n\n /**\n * The function that implements the tool's logic. This function will be called\n * by Tambo when the model decides to invoke the tool.\n * @param params - The input parameters for the tool. These are validated\n * against the inputSchema before being passed so are guaranteed to be correct\n * when called by the model.\n * @returns The result of the tool execution, which can be a value or a\n * Promise resolving to a value\n */\n tool: (params: Params, ...rest: Rest) => MaybeAsync<Returns>;\n\n /**\n * The schema for the tool's input parameters. This can be a validator from\n * any Standard Schema compliant library (Zod 3.24+, Zod 4.x) or a\n * raw JSON Schema object.\n *\n * This schema is used to validate and parse the parameters before passing\n * them to the tool function.\n */\n inputSchema: SupportedSchema<Params>;\n\n /**\n * The schema for the tool's output/return value. This can be any Standard Schema\n * compliant validator (Zod 3.24+, Zod 4.x) or a raw JSON Schema object.\n *\n * This is used to inform the model about the structure of the tool's return value\n * and is not used for runtime validation at this stage.\n */\n outputSchema: SupportedSchema<Returns>;\n\n /**\n * Optional function to transform the tool's return value into an array of content parts.\n * If not provided, the return value will be converted to a string and wrapped in a text content part.\n * @param result - The result returned by the tool function\n * @returns An array of content parts to be sent back to the AI\n */\n transformToContent?: (\n result: any,\n ) =>\n | Promise<TamboAI.Beta.Threads.ChatCompletionContentPart[]>\n | TamboAI.Beta.Threads.ChatCompletionContentPart[];\n}\n\n/**\n * A tool that uses JSON Schema compliant input and output schemas.\n * This does not provide type safety for the tool's parameters and return value.\n * @internal\n */\nexport type TamboToolJSONSchema<\n Args extends unknown[] = unknown[],\n Returns = unknown,\n> = Omit<TamboTool<Args, Returns>, \"tool\" | \"inputSchema\" | \"outputSchema\"> & {\n tool: (...args: Args) => MaybeAsync<Returns>;\n inputSchema: JSONSchemaLite;\n outputSchema: JSONSchemaLite;\n};\n\n/**\n * A tool that could not be matched to any known schema types.\n * This means type safety cannot be guaranteed.\n * @internal\n */\nexport type TamboToolUnknown = Omit<\n TamboTool,\n \"tool\" | \"inputSchema\" | \"outputSchema\"\n> & {\n tool: (...args: unknown[]) => MaybeAsync<unknown>;\n inputSchema: SupportedSchema;\n outputSchema: SupportedSchema;\n};\n\n/**\n * A tool that uses Standard Schema compliant input and output schemas.\n * This provides full type safety for the tool's parameters and return value.\n * @internal\n */\nexport type TamboToolStandardSchema<\n Input extends StandardSchemaV1 = StandardSchemaV1,\n Output extends StandardSchemaV1 = StandardSchemaV1,\n> = Omit<\n TamboTool<\n StandardSchemaV1.InferOutput<Input>,\n StandardSchemaV1.InferOutput<Output>\n >,\n \"tool\" | \"inputSchema\" | \"outputSchema\"\n> & {\n tool: (\n ...args: [StandardSchemaV1.InferOutput<Input>]\n ) => MaybeAsync<StandardSchemaV1.InferOutput<Output>>;\n inputSchema: Input;\n outputSchema: Output;\n};\n\n/**\n * If you're seeing this type, it means that you are using a deprecated and now\n * unsupported schema type for defining Tambo tools.\n *\n * Follow the migration guide to update your tool definitions to use\n * inputSchema and outputSchema with either Standard Schema compliant validators\n * (like Zod 3.25.76, Zod 4.x) or raw JSON Schema objects.\n * @deprecated replace `toolSchema` with `inputSchema` and `outputSchema` instead.\n * @see {@link https://docs.tambo.co/reference/react-sdk/migration/toolschema}\n */\nexport type UnsupportedSchemaTamboTool = Omit<\n TamboTool,\n \"tool\" | \"inputSchema\" | \"outputSchema\"\n> & {\n /**\n * @deprecated replace `toolSchema` with `inputSchema` and `outputSchema` instead.\n */\n toolSchema: any;\n tool: (...args: any[]) => MaybeAsync<any>;\n inputSchema?: never;\n outputSchema?: never;\n};\n\nexport type TamboToolAssociations = Record<string, string[]>;\n/**\n * A component that can be registered with the TamboRegistryProvider.\n */\n\nexport interface TamboComponent {\n /** The name of the component */\n name: string;\n /** The description of the component */\n description: string;\n /**\n * The React component to render.\n *\n * Make sure to pass the Component itself, not an instance of the component. For example,\n * if you have a component like this:\n *\n * ```tsx\n * const MyComponent = () => {\n * return <div>My Component</div>;\n * };\n * ```\n *\n * You should pass the `Component`:\n *\n * ```tsx\n * const components = [MyComponent];\n * <TamboRegistryProvider components={components} />\n * ```\n */\n component: ComponentType<any>;\n\n /**\n * Schema describing the component's props. Accepts any Standard Schema\n * compliant validator (Zod, Valibot, ArkType, etc.) or a raw JSON Schema\n * object.\n *\n * Either this or propsDefinition must be provided, but not both.\n * @example\n * ```typescript\n * import { z } from \"zod/v4\";\n *\n * const component: TamboComponent = {\n * name: \"MyComponent\",\n * description: \"A sample component\",\n * component: MyComponent,\n * propsSchema: z.object({\n * title: z.string(),\n * count: z.number().optional()\n * })\n * };\n * ```\n */\n propsSchema?: SupportedSchema;\n /**\n * The props definition of the component as a JSON object.\n * Either this or propsSchema must be provided, but not both.\n * @deprecated Use propsSchema instead.\n */\n propsDefinition?: any;\n /** The loading component to render while the component is loading */\n loadingComponent?: ComponentType<any>;\n /** The tools that are associated with the component */\n associatedTools?: TamboTool[];\n /** Annotations describing the component's behavior. */\n annotations?: ToolAnnotations;\n}\n\ntype OptionalSchemaProps<T> = Omit<T, \"inputSchema\" | \"outputSchema\"> & {\n inputSchema?: T extends { inputSchema: infer I } ? I : never;\n outputSchema?: T extends { outputSchema: infer O } ? O : never;\n};\n\n/**\n * Registers one or more Tambo tools.\n * @param tools - An array of Tambo tools to register\n * @param warnOnOverwrite - Whether to warn if any tool is being overwritten\n */\nexport interface RegisterToolsFn {\n /**\n * @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration/toolschema | Migration Guide} to update\n * your tool definitions to use `inputSchema` and `outputSchema` instead.\n */\n (tools: UnsupportedSchemaTamboTool[], warnOnOverwrite?: boolean): void;\n /**\n * Register one or more Tambo tools. For better type inference, consider registering tools individually using the\n * `registerTool` function or use the `defineTool` helper when defining your tools.\n * @example\n * ```typescript\n * import { defineTool } from \"@tambo-ai/react\";\n * const tools = [\n * defineTool({...});\n * defineTool({...});\n * ];\n * registerTools(tools);\n * @param tools - An array of Tambo tools to register\n * @param warnOnOverwrite - Whether to warn if any tool is being overwritten\n */\n (tools: TamboTool[], warnOnOverwrite?: boolean): void;\n}\n\n/**\n * Function interface for registering a Tambo tool with full type inference.\n * This function has multiple overloads to handle different schema types. This\n * is a utility function and does not perform any runtime logic.\n */\nexport interface RegisterToolFn {\n <Args extends StandardSchemaV1, Returns extends StandardSchemaV1>(\n tool: TamboToolStandardSchema<Args, Returns>,\n warnOnOverwrite?: boolean,\n ): void;\n <Args extends any[], Returns = any>(\n tool: TamboToolJSONSchema<Args, Returns>,\n warnOnOverwrite?: boolean,\n ): void;\n (tool: TamboToolUnknown, warnOnOverwrite?: boolean): void;\n (tool: TamboTool, warnOnOverwrite?: boolean): void;\n /**\n * @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration/toolschema | Migration Guide} to update\n * your tool definitions to use `inputSchema` and `outputSchema` instead.\n * @param tool - The unsupported schema Tambo tool to register\n * @param warnOnOverwrite - Whether to warn if the tool is being overwritten\n */\n (tool: UnsupportedSchemaTamboTool, warnOnOverwrite?: boolean): void;\n}\n\n/**\n * Function interface for defining a Tambo tool with full type inference. This function has multiple overloads to handle\n * different schema types. This is a utility function and does not perform any runtime logic.\n */\nexport interface DefineToolFn {\n /**\n * @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration/toolschema | Migration Guide} to update\n * your tool definitions to use `inputSchema` and `outputSchema` instead.\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n (tool: UnsupportedSchemaTamboTool): typeof tool;\n /**\n * Provides type safety for defining a Tambo Tool.\n *\n * Tambo uses the [standard-schema.dev](https://standard-schema.dev) spec which means you can use any Standard Schema\n * compliant validator (Zod 3.24+, Zod 4.x). This definition ensures the input and output types are correctly\n * inferred from the provided schemas.\n * @example\n * ```typescript\n * import { z } from \"zod/v4\";\n *\n * const myTool = defineTool({\n * name: \"myTool\",\n * description: \"An example tool\",\n * inputSchema: z.object({\n * input: z.string().describe(\"Input description\")\n * }),\n * outputSchema: z.number().describe(\"Result description\"),\n * tool: ({ input }) => input.length,\n * });\n * ```\n * @see {@link https://standard-schema.dev/}\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n <Input extends StandardSchemaV1, Output extends StandardSchemaV1>(\n tool: OptionalSchemaProps<TamboToolStandardSchema<Input, Output>>,\n ): TamboToolStandardSchema<Input, Output>;\n /**\n * Provides type safety for defining a Tambo Tool with JSON Schema input and output.\n *\n * This overload is used when providing raw JSON Schema objects instead of StandardSchema validators.\n * Type inference is limited when using raw JSON Schema.\n * @see {@link https://standard-schema.dev/}\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n <I extends any[], O = any>(\n tool: OptionalSchemaProps<TamboToolJSONSchema<I, O>>,\n ): TamboToolJSONSchema<I, O>;\n /**\n * Provides type safety for defining a Tambo Tool.\n *\n * This overload is used when the schema types could not be matched to known types.\n * Type safety cannot be guaranteed.\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n (tool: OptionalSchemaProps<TamboToolUnknown>): TamboToolUnknown;\n /**\n * Provides type safety for defining a Tambo Tool.\n *\n * This overload is used when providing a fully defined TamboTool.\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n (tool: OptionalSchemaProps<TamboTool>): TamboTool;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"component-metadata.js","sourceRoot":"","sources":["../../src/model/component-metadata.ts"],"names":[],"mappings":"","sourcesContent":["import type { ToolAnnotations as MCPToolAnnotations } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { JSONSchema7 } from \"json-schema\";\nimport { ComponentType } from \"react\";\n\n/**\n * A schema type that accepts either a Standard Schema compliant validator\n * (e.g., Zod, Valibot, ArkType) or a raw JSON Schema object.\n *\n * Standard Schema is a specification that provides a unified interface for\n * TypeScript validation libraries. Libraries like Zod implement this spec,\n * allowing us to accept any compliant validator without depending on a specific library.\n * @see https://standardschema.dev/\n */\nexport type SupportedSchema<Input = unknown, Output = Input> =\n | StandardSchemaV1<Input, Output>\n | JSONSchema7;\n\n/**\n * Annotations describing a tool's behavior, aligned with the MCP (Model Context Protocol)\n * specification. These hints help clients understand how tools behave and can be used\n * to optimize tool execution strategies.\n * @see https://modelcontextprotocol.io/specification/2025-06-18/schema#toolannotations\n */\nexport type ToolAnnotations = MCPToolAnnotations & {\n /**\n * Indicates that the tool is safe to be called repeatedly while a response is\n * being streamed. This is typically used for read-only tools that do not\n * cause side effects.\n */\n tamboStreamableHint?: boolean;\n};\n\n/** Extension of the ToolParameters interface from Tambo AI to include JSONSchema definition */\nexport type ParameterSpec = TamboAI.ToolParameters & {\n schema?: JSONSchema7;\n};\n\n/**\n * Extends the base ContextTool interface from Tambo AI to include schema information\n * for parameter validation.\n */\nexport interface ComponentContextToolMetadata\n extends TamboAI.ComponentContextToolMetadata {\n parameters: ParameterSpec[];\n /**\n * Optional per-tool call limit. When set, this overrides the project's\n * global tool call limit for this specific tool.\n *\n * This is useful for tools that should only be called once or twice\n * regardless of the project's global limit.\n */\n maxCalls?: number;\n /**\n * Annotations describing the tool's behavior. See {@link ToolAnnotations}.\n */\n annotations?: ToolAnnotations;\n}\n\nexport interface ComponentContextTool {\n getComponentContext: (...args: any[]) => Promise<any>;\n definition: ComponentContextToolMetadata;\n}\n\nexport interface RegisteredComponent extends TamboAI.AvailableComponent {\n component: ComponentType<any>;\n loadingComponent?: ComponentType<any>;\n}\n\nexport type ComponentRegistry = Record<string, RegisteredComponent>;\n\nexport type TamboToolRegistry = Record<string, TamboTool>;\n\n/**\n * A JSON Schema that is compatible with the MCP.\n * This is a simplified JSON Schema that is compatible with the MCPClient and the tool's inputSchema.\n *\n * Do not export this type from the SDK.\n */\nexport type JSONSchemaLite = JSONSchema7 & {\n description?: string;\n};\n\ntype MaybeAsync<T> = T | Promise<T>;\n\n/**\n * TamboTool is a type that represents a tool that can be registered with Tambo.\n *\n * It is preferable to use the `defineTool` helper function to create tools, as\n * it provides better type inference and safety.\n * @example\n * ```ts\n * import { TamboTool, defineTool } from \"@tambo-ai/react\";\n * import { z } from \"zod\";\n *\n * const locationToLatLon = defineTool({\n * name: \"location_to_latlon\",\n * description:\n * \"Fetch latitude and longitude from a location string. Returns an object with 'lat' and 'lon' properties.\",\n * tool: async ({ location }) => getLatLonFromLocation(location),\n * inputSchema: z.object({\n * location: z.string(),\n * }),\n * outputSchema: z.object({\n * lat: z.number(),\n * lon: z.number(),\n * }),\n * });\n * ```\n */\nexport interface TamboTool<\n Params = any,\n Returns = any,\n Rest extends any[] = [],\n> {\n /**\n * Unique identifier for the tool\n */\n name: string;\n /**\n * Description of what the tool does - used by AI to determine when to use it\n */\n description: string;\n /**\n * Optional human-readable name of the tool for display purposes.\n */\n title?: string;\n /**\n * Optional limit for how many times this tool may be called while\n * generating a single response. If present, this value overrides the\n * project's global `maxToolCallLimit` for this tool.\n * @example 1\n */\n maxCalls?: number;\n /**\n * Annotations describing the tool's behavior, aligned with the MCP specification.\n * Use `tamboStreamableHint: true` to enable streaming execution of partial tool calls.\n * @see {@link ToolAnnotations}\n * @example\n * ```ts\n * const setTextTool: TamboTool<{ text: string }> = {\n * name: \"set_text\",\n * description: \"Set the displayed text\",\n * annotations: {\n * tamboStreamableHint: true, // tool is safe for streaming calls\n * },\n * tool: ({ text }) => setText(text),\n * inputSchema: z.object({ text: z.string() }),\n * outputSchema: z.void(),\n * };\n * ```\n */\n annotations?: ToolAnnotations;\n\n /**\n * The function that implements the tool's logic. This function will be called\n * by Tambo when the model decides to invoke the tool.\n * @param params - The input parameters for the tool. These are validated\n * against the inputSchema before being passed so are guaranteed to be correct\n * when called by the model.\n * @returns The result of the tool execution, which can be a value or a\n * Promise resolving to a value\n */\n tool: (params: Params, ...rest: Rest) => MaybeAsync<Returns>;\n\n /**\n * The schema for the tool's input parameters. This can be a validator from\n * any Standard Schema compliant library (Zod 3.24+, Zod 4.x) or a\n * raw JSON Schema object.\n *\n * This schema is used to validate and parse the parameters before passing\n * them to the tool function.\n */\n inputSchema: SupportedSchema<Params>;\n\n /**\n * The schema for the tool's output/return value. This can be any Standard Schema\n * compliant validator (Zod 3.24+, Zod 4.x) or a raw JSON Schema object.\n *\n * This is used to inform the model about the structure of the tool's return value\n * and is not used for runtime validation at this stage.\n */\n outputSchema: SupportedSchema<Returns>;\n\n /**\n * Optional function to transform the tool's return value into an array of content parts.\n * If not provided, the return value will be converted to a string and wrapped in a text content part.\n * @param result - The result returned by the tool function\n * @returns An array of content parts to be sent back to the AI\n */\n transformToContent?: (\n result: any,\n ) =>\n | Promise<TamboAI.Beta.Threads.ChatCompletionContentPart[]>\n | TamboAI.Beta.Threads.ChatCompletionContentPart[];\n}\n\n/**\n * A tool that uses JSON Schema compliant input and output schemas.\n * This does not provide type safety for the tool's parameters and return value.\n * @internal\n */\nexport type TamboToolJSONSchema<\n Args extends unknown[] = unknown[],\n Returns = unknown,\n> = Omit<TamboTool<Args, Returns>, \"tool\" | \"inputSchema\" | \"outputSchema\"> & {\n tool: (...args: Args) => MaybeAsync<Returns>;\n inputSchema: JSONSchemaLite;\n outputSchema: JSONSchemaLite;\n};\n\n/**\n * A tool that could not be matched to any known schema types.\n * This means type safety cannot be guaranteed.\n * @internal\n */\nexport type TamboToolUnknown = Omit<\n TamboTool,\n \"tool\" | \"inputSchema\" | \"outputSchema\"\n> & {\n tool: (...args: unknown[]) => MaybeAsync<unknown>;\n inputSchema: SupportedSchema;\n outputSchema: SupportedSchema;\n};\n\n/**\n * A tool that uses Standard Schema compliant input and output schemas.\n * This provides full type safety for the tool's parameters and return value.\n * @internal\n */\nexport type TamboToolStandardSchema<\n Input extends StandardSchemaV1 = StandardSchemaV1,\n Output extends StandardSchemaV1 = StandardSchemaV1,\n> = Omit<\n TamboTool<\n StandardSchemaV1.InferOutput<Input>,\n StandardSchemaV1.InferOutput<Output>\n >,\n \"tool\" | \"inputSchema\" | \"outputSchema\"\n> & {\n tool: (\n ...args: [StandardSchemaV1.InferOutput<Input>]\n ) => MaybeAsync<StandardSchemaV1.InferOutput<Output>>;\n inputSchema: Input;\n outputSchema: Output;\n};\n\n/**\n * If you're seeing this type, it means that you are using a deprecated and now\n * unsupported schema type for defining Tambo tools.\n *\n * Follow the migration guide to update your tool definitions to use\n * inputSchema and outputSchema with either Standard Schema compliant validators\n * (like Zod 3.25.76, Zod 4.x) or raw JSON Schema objects.\n * @deprecated replace `toolSchema` with `inputSchema` and `outputSchema` instead.\n * @see {@link https://docs.tambo.co/reference/react-sdk/migration}\n */\nexport type UnsupportedSchemaTamboTool = Omit<\n TamboTool,\n \"tool\" | \"inputSchema\" | \"outputSchema\"\n> & {\n /**\n * @deprecated replace `toolSchema` with `inputSchema` and `outputSchema` instead.\n */\n toolSchema: any;\n tool: (...args: any[]) => MaybeAsync<any>;\n inputSchema?: never;\n outputSchema?: never;\n};\n\nexport type TamboToolAssociations = Record<string, string[]>;\n/**\n * A component that can be registered with the TamboRegistryProvider.\n */\n\nexport interface TamboComponent {\n /** The name of the component */\n name: string;\n /** The description of the component */\n description: string;\n /**\n * The React component to render.\n *\n * Make sure to pass the Component itself, not an instance of the component. For example,\n * if you have a component like this:\n *\n * ```tsx\n * const MyComponent = () => {\n * return <div>My Component</div>;\n * };\n * ```\n *\n * You should pass the `Component`:\n *\n * ```tsx\n * const components = [MyComponent];\n * <TamboRegistryProvider components={components} />\n * ```\n */\n component: ComponentType<any>;\n\n /**\n * Schema describing the component's props. Accepts any Standard Schema\n * compliant validator (Zod, Valibot, ArkType, etc.) or a raw JSON Schema\n * object.\n *\n * Either this or propsDefinition must be provided, but not both.\n * @example\n * ```typescript\n * import { z } from \"zod/v4\";\n *\n * const component: TamboComponent = {\n * name: \"MyComponent\",\n * description: \"A sample component\",\n * component: MyComponent,\n * propsSchema: z.object({\n * title: z.string(),\n * count: z.number().optional()\n * })\n * };\n * ```\n */\n propsSchema?: SupportedSchema;\n /**\n * The props definition of the component as a JSON object.\n * Either this or propsSchema must be provided, but not both.\n * @deprecated Use propsSchema instead.\n */\n propsDefinition?: any;\n /** The loading component to render while the component is loading */\n loadingComponent?: ComponentType<any>;\n /** The tools that are associated with the component */\n associatedTools?: TamboTool[];\n /** Annotations describing the component's behavior. */\n annotations?: ToolAnnotations;\n}\n\ntype OptionalSchemaProps<T> = Omit<T, \"inputSchema\" | \"outputSchema\"> & {\n inputSchema?: T extends { inputSchema: infer I } ? I : never;\n outputSchema?: T extends { outputSchema: infer O } ? O : never;\n};\n\n/**\n * Registers one or more Tambo tools.\n * @param tools - An array of Tambo tools to register\n * @param warnOnOverwrite - Whether to warn if any tool is being overwritten\n */\nexport interface RegisterToolsFn {\n /**\n * @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration | Migration Guide} to update\n * your tool definitions to use `inputSchema` and `outputSchema` instead.\n */\n (tools: UnsupportedSchemaTamboTool[], warnOnOverwrite?: boolean): void;\n /**\n * Register one or more Tambo tools. For better type inference, consider registering tools individually using the\n * `registerTool` function or use the `defineTool` helper when defining your tools.\n * @example\n * ```typescript\n * import { defineTool } from \"@tambo-ai/react\";\n * const tools = [\n * defineTool({...});\n * defineTool({...});\n * ];\n * registerTools(tools);\n * @param tools - An array of Tambo tools to register\n * @param warnOnOverwrite - Whether to warn if any tool is being overwritten\n */\n (tools: TamboTool[], warnOnOverwrite?: boolean): void;\n}\n\n/**\n * Function interface for registering a Tambo tool with full type inference.\n * This function has multiple overloads to handle different schema types. This\n * is a utility function and does not perform any runtime logic.\n */\nexport interface RegisterToolFn {\n <Args extends StandardSchemaV1, Returns extends StandardSchemaV1>(\n tool: TamboToolStandardSchema<Args, Returns>,\n warnOnOverwrite?: boolean,\n ): void;\n <Args extends any[], Returns = any>(\n tool: TamboToolJSONSchema<Args, Returns>,\n warnOnOverwrite?: boolean,\n ): void;\n (tool: TamboToolUnknown, warnOnOverwrite?: boolean): void;\n (tool: TamboTool, warnOnOverwrite?: boolean): void;\n /**\n * @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration | Migration Guide} to update\n * your tool definitions to use `inputSchema` and `outputSchema` instead.\n * @param tool - The unsupported schema Tambo tool to register\n * @param warnOnOverwrite - Whether to warn if the tool is being overwritten\n */\n (tool: UnsupportedSchemaTamboTool, warnOnOverwrite?: boolean): void;\n}\n\n/**\n * Function interface for defining a Tambo tool with full type inference. This function has multiple overloads to handle\n * different schema types. This is a utility function and does not perform any runtime logic.\n */\nexport interface DefineToolFn {\n /**\n * @deprecated Follow the {@link https://docs.tambo.co/reference/react-sdk/migration | Migration Guide} to update\n * your tool definitions to use `inputSchema` and `outputSchema` instead.\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n (tool: UnsupportedSchemaTamboTool): typeof tool;\n /**\n * Provides type safety for defining a Tambo Tool.\n *\n * Tambo uses the [standard-schema.dev](https://standard-schema.dev) spec which means you can use any Standard Schema\n * compliant validator (Zod 3.24+, Zod 4.x). This definition ensures the input and output types are correctly\n * inferred from the provided schemas.\n * @example\n * ```typescript\n * import { z } from \"zod/v4\";\n *\n * const myTool = defineTool({\n * name: \"myTool\",\n * description: \"An example tool\",\n * inputSchema: z.object({\n * input: z.string().describe(\"Input description\")\n * }),\n * outputSchema: z.number().describe(\"Result description\"),\n * tool: ({ input }) => input.length,\n * });\n * ```\n * @see {@link https://standard-schema.dev/}\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n <Input extends StandardSchemaV1, Output extends StandardSchemaV1>(\n tool: OptionalSchemaProps<TamboToolStandardSchema<Input, Output>>,\n ): TamboToolStandardSchema<Input, Output>;\n /**\n * Provides type safety for defining a Tambo Tool with JSON Schema input and output.\n *\n * This overload is used when providing raw JSON Schema objects instead of StandardSchema validators.\n * Type inference is limited when using raw JSON Schema.\n * @see {@link https://standard-schema.dev/}\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n <I extends any[], O = any>(\n tool: OptionalSchemaProps<TamboToolJSONSchema<I, O>>,\n ): TamboToolJSONSchema<I, O>;\n /**\n * Provides type safety for defining a Tambo Tool.\n *\n * This overload is used when the schema types could not be matched to known types.\n * Type safety cannot be guaranteed.\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n (tool: OptionalSchemaProps<TamboToolUnknown>): TamboToolUnknown;\n /**\n * Provides type safety for defining a Tambo Tool.\n *\n * This overload is used when providing a fully defined TamboTool.\n * @param tool The tool definition to register\n * @returns The registered tool definition\n */\n (tool: OptionalSchemaProps<TamboTool>): TamboTool;\n}\n"]}
|
|
@@ -59,7 +59,7 @@ function validateTool(tool) {
|
|
|
59
59
|
if ("toolSchema" in tool) {
|
|
60
60
|
throw new Error(`Tool "${toolName}" uses deprecated "toolSchema" property. ` +
|
|
61
61
|
`Migrate to "inputSchema" and "outputSchema" properties. ` +
|
|
62
|
-
`See migration guide: https://docs.tambo.co/reference/react-sdk/migration
|
|
62
|
+
`See migration guide: https://docs.tambo.co/reference/react-sdk/migration`);
|
|
63
63
|
}
|
|
64
64
|
// Validate required properties exist
|
|
65
65
|
if (!("name" in tool) || typeof tool.name !== "string") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry-validators.js","sourceRoot":"","sources":["../../src/util/registry-validators.ts"],"names":[],"mappings":";;AAwDA,oCAyEC;AAgDD,kEA+BC;AAUD,0DAgBC;AAxOD,sCAKmB;AACnB,uEAA4D;AAE5D;;;;GAIG;AACH,SAAS,cAAc,CAAC,MAAmB;IACzC,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,UAAU,KAAK,SAAS,CAAC;AACrE,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,MAAe,EAAE,OAAe;IAC1D,IAAI,UAAuB,CAAC;IAE5B,IAAI,IAAA,yBAAgB,EAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,IAAI,CAAC;YACH,UAAU,GAAG,IAAA,2BAAkB,EAAC,MAAM,CAAC,CAAC;QAC1C,CAAC;QAAC,MAAM,CAAC;YACP,6DAA6D;YAC7D,OAAO;QACT,CAAC;IACH,CAAC;SAAM,IAAI,IAAA,4BAAmB,EAAC,MAAM,CAAC,EAAE,CAAC;QACvC,UAAU,GAAG,MAAM,CAAC;IACtB,CAAC;SAAM,CAAC;QACN,sCAAsC;QACtC,OAAO;IACT,CAAC;IAED,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,EA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,CAAC,CAAC;IAErE,OAAO,EAAE,KAAK,EAAE,CAAC;AACnB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,uBAAuB,CACrC,aAAqB,EACrB,QAAgB,EAChB,eAAwB,EACxB,UAAmB;IAEnB,oCAAoC;IACpC,IAAA,yCAAe,EAAC,aAAa,EAAE,WAAW,CAAC,CAAC;IAC5C,IAAA,yCAAe,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAElC,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,aAAa,aAAa,wBAAwB,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,wBAAwB,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC","sourcesContent":["import type { JSONSchema7 } from \"json-schema\";\nimport type { TamboComponent, TamboTool } from \"../model/component-metadata\";\nimport {\n assertNoRecordSchema,\n isStandardSchema,\n looksLikeJSONSchema,\n schemaToJsonSchema,\n} from \"../schema\";\nimport { assertValidName } from \"./validate-component-name\";\n\n/**\n * Checks if a JSON Schema represents an object type.\n * @param schema - The JSON Schema to check\n * @returns True if the schema is an object type\n */\nfunction isObjectSchema(schema: JSONSchema7): boolean {\n return schema.type === \"object\" || schema.properties !== undefined;\n}\n\n/**\n * Validates that a schema is an object type.\n * Throws an error if it's not.\n * @param schema - The schema to validate (Standard Schema or JSON Schema)\n * @param context - Description of where the schema is used (for error messages)\n */\nfunction assertObjectSchema(schema: unknown, context: string): void {\n let jsonSchema: JSONSchema7;\n\n if (isStandardSchema(schema)) {\n try {\n jsonSchema = schemaToJsonSchema(schema);\n } catch {\n // If conversion fails, we can't validate - let it fail later\n return;\n }\n } else if (looksLikeJSONSchema(schema)) {\n jsonSchema = schema;\n } else {\n // Unknown schema type, can't validate\n return;\n }\n\n if (!isObjectSchema(jsonSchema)) {\n throw new Error(\n `${context} must be an object schema (e.g., z.object({...})). ` +\n `Received type: \"${jsonSchema.type ?? \"unknown\"}\". ` +\n `Tool parameters are passed as a single object argument.`,\n );\n }\n}\n\n/**\n * Validates a tool before registration.\n * Throws an error if the tool is invalid.\n * @param tool - The tool to validate\n */\nexport function validateTool(tool: unknown): asserts tool is TamboTool {\n // Basic runtime type checks before narrowing\n if (!tool || typeof tool !== \"object\") {\n throw new Error(\"Tool must be an object\");\n }\n\n // Extract name for error messages (before we know it's valid)\n const toolName =\n \"name\" in tool && typeof tool.name === \"string\" ? tool.name : \"<unknown>\";\n\n // Check for deprecated toolSchema - throw error with migration instructions\n if (\"toolSchema\" in tool) {\n throw new Error(\n `Tool \"${toolName}\" uses deprecated \"toolSchema\" property. ` +\n `Migrate to \"inputSchema\" and \"outputSchema\" properties. ` +\n `See migration guide: https://docs.tambo.co/reference/react-sdk/migration/toolschema`,\n );\n }\n\n // Validate required properties exist\n if (!(\"name\" in tool) || typeof tool.name !== \"string\") {\n throw new Error(\"Tool must have a 'name' property of type string\");\n }\n\n if (!(\"description\" in tool) || typeof tool.description !== \"string\") {\n throw new Error(\n `Tool \"${tool.name}\" must have a 'description' property of type string`,\n );\n }\n\n if (!(\"tool\" in tool) || typeof tool.tool !== \"function\") {\n throw new Error(\n `Tool \"${tool.name}\" must have a 'tool' property of type function`,\n );\n }\n\n if (!(\"inputSchema\" in tool)) {\n throw new Error(`Tool \"${tool.name}\" must have an 'inputSchema' property`);\n }\n\n if (!(\"outputSchema\" in tool)) {\n throw new Error(`Tool \"${tool.name}\" must have an 'outputSchema' property`);\n }\n\n // Validate tool name format\n assertValidName(tool.name, \"tool\");\n\n // Validate tool schemas - inputSchema must be an object schema\n if (tool.inputSchema) {\n assertObjectSchema(tool.inputSchema, `inputSchema of tool \"${tool.name}\"`);\n assertNoRecordSchema(\n tool.inputSchema,\n `inputSchema of tool \"${tool.name}\"`,\n );\n }\n\n // Validate maxCalls if provided - must be a positive integer\n if (\n \"maxCalls\" in tool &&\n tool.maxCalls !== undefined &&\n tool.maxCalls !== null\n ) {\n const maxCalls = tool.maxCalls;\n if (\n typeof maxCalls !== \"number\" ||\n !Number.isInteger(maxCalls) ||\n maxCalls < 0\n ) {\n throw new Error(\n `maxCalls for tool \"${tool.name}\" must be a positive integer`,\n );\n }\n }\n}\n\n/**\n * Converts a props schema to a serialized JSON Schema format.\n * @param propsDefinition - Deprecated: legacy props definition (will log warning)\n * @param propsSchema - The props schema (Standard Schema or JSON Schema)\n * @param name - Component/tool name for error messages\n * @returns Serialized JSON Schema object\n */\nfunction getSerializedProps(\n propsDefinition: unknown,\n propsSchema: unknown,\n name: string,\n): Record<string, unknown> {\n if (propsDefinition) {\n console.warn(`propsDefinition is deprecated. Use propsSchema instead.`);\n return propsDefinition as Record<string, unknown>;\n }\n\n // Check for Standard Schema (Zod, Valibot, ArkType, etc.)\n if (isStandardSchema(propsSchema)) {\n try {\n return schemaToJsonSchema(propsSchema) as Record<string, unknown>;\n } catch (error) {\n console.error(\n `Error converting ${name} props schema to JSON Schema:`,\n error,\n );\n throw new Error(\n `Error converting ${name} props schema to JSON Schema: ${error}`,\n );\n }\n }\n\n // Check for JSON Schema\n if (looksLikeJSONSchema(propsSchema)) {\n return propsSchema as Record<string, unknown>;\n }\n\n throw new Error(`Invalid props schema for ${name}`);\n}\n\n/**\n * Validates a component and prepares its props for registration.\n * Throws an error if the component is invalid.\n * @param component - The component to validate and prepare\n * @returns Object containing the serialized props\n */\nexport function validateAndPrepareComponent(component: TamboComponent): {\n props: Record<string, unknown>;\n} {\n const { name, propsSchema, propsDefinition } = component;\n\n // Validate component name\n assertValidName(name, \"component\");\n\n // Validate that at least one props definition is provided\n if (!propsSchema && !propsDefinition) {\n throw new Error(\n `Component ${name} must have either propsSchema (recommended) or propsDefinition defined`,\n );\n }\n\n // Validate that only one props definition is provided\n if (propsSchema && propsDefinition) {\n throw new Error(\n `Component ${name} cannot have both propsSchema and propsDefinition defined. Use only one. We recommend using propsSchema.`,\n );\n }\n\n // Validate that the propsSchema does not include record types\n if (propsSchema) {\n assertNoRecordSchema(propsSchema, `propsSchema of component \"${name}\"`);\n }\n\n // Convert propsSchema to JSON Schema if it exists\n const props = getSerializedProps(propsDefinition, propsSchema, name);\n\n return { props };\n}\n\n/**\n * Validates a tool association between a component and tool.\n * Throws an error if the association is invalid.\n * @param componentName - The component name\n * @param toolName - The tool name\n * @param componentExists - Whether the component exists in the registry\n * @param toolExists - Whether the tool exists in the registry\n */\nexport function validateToolAssociation(\n componentName: string,\n toolName: string,\n componentExists: boolean,\n toolExists: boolean,\n): void {\n // Validate component and tool names\n assertValidName(componentName, \"component\");\n assertValidName(toolName, \"tool\");\n\n if (!componentExists) {\n throw new Error(`Component ${componentName} not found in registry`);\n }\n if (!toolExists) {\n throw new Error(`Tool ${toolName} not found in registry`);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"registry-validators.js","sourceRoot":"","sources":["../../src/util/registry-validators.ts"],"names":[],"mappings":";;AAwDA,oCAyEC;AAgDD,kEA+BC;AAUD,0DAgBC;AAxOD,sCAKmB;AACnB,uEAA4D;AAE5D;;;;GAIG;AACH,SAAS,cAAc,CAAC,MAAmB;IACzC,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,UAAU,KAAK,SAAS,CAAC;AACrE,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,MAAe,EAAE,OAAe;IAC1D,IAAI,UAAuB,CAAC;IAE5B,IAAI,IAAA,yBAAgB,EAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,IAAI,CAAC;YACH,UAAU,GAAG,IAAA,2BAAkB,EAAC,MAAM,CAAC,CAAC;QAC1C,CAAC;QAAC,MAAM,CAAC;YACP,6DAA6D;YAC7D,OAAO;QACT,CAAC;IACH,CAAC;SAAM,IAAI,IAAA,4BAAmB,EAAC,MAAM,CAAC,EAAE,CAAC;QACvC,UAAU,GAAG,MAAM,CAAC;IACtB,CAAC;SAAM,CAAC;QACN,sCAAsC;QACtC,O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AAM,KAAK,GAAG,kBAAkB,CAAC,eAAe,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;IAErE,OAAO,EAAE,KAAK,EAAE,CAAC;AACnB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,uBAAuB,CACrC,aAAqB,EACrB,QAAgB,EAChB,eAAwB,EACxB,UAAmB;IAEnB,oCAAoC;IACpC,IAAA,yCAAe,EAAC,aAAa,EAAE,WAAW,CAAC,CAAC;IAC5C,IAAA,yCAAe,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAElC,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,aAAa,aAAa,wBAAwB,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,wBAAwB,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC","sourcesContent":["import type { JSONSchema7 } from \"json-schema\";\nimport type { TamboComponent, TamboTool } from \"../model/component-metadata\";\nimport {\n assertNoRecordSchema,\n isStandardSchema,\n looksLikeJSONSchema,\n schemaToJsonSchema,\n} from \"../schema\";\nimport { assertValidName } from \"./validate-component-name\";\n\n/**\n * Checks if a JSON Schema represents an object type.\n * @param schema - The JSON Schema to check\n * @returns True if the schema is an object type\n */\nfunction isObjectSchema(schema: JSONSchema7): boolean {\n return schema.type === \"object\" || schema.properties !== undefined;\n}\n\n/**\n * Validates that a schema is an object type.\n * Throws an error if it's not.\n * @param schema - The schema to validate (Standard Schema or JSON Schema)\n * @param context - Description of where the schema is used (for error messages)\n */\nfunction assertObjectSchema(schema: unknown, context: string): void {\n let jsonSchema: JSONSchema7;\n\n if (isStandardSchema(schema)) {\n try {\n jsonSchema = schemaToJsonSchema(schema);\n } catch {\n // If conversion fails, we can't validate - let it fail later\n return;\n }\n } else if (looksLikeJSONSchema(schema)) {\n jsonSchema = schema;\n } else {\n // Unknown schema type, can't validate\n return;\n }\n\n if (!isObjectSchema(jsonSchema)) {\n throw new Error(\n `${context} must be an object schema (e.g., z.object({...})). ` +\n `Received type: \"${jsonSchema.type ?? \"unknown\"}\". ` +\n `Tool parameters are passed as a single object argument.`,\n );\n }\n}\n\n/**\n * Validates a tool before registration.\n * Throws an error if the tool is invalid.\n * @param tool - The tool to validate\n */\nexport function validateTool(tool: unknown): asserts tool is TamboTool {\n // Basic runtime type checks before narrowing\n if (!tool || typeof tool !== \"object\") {\n throw new Error(\"Tool must be an object\");\n }\n\n // Extract name for error messages (before we know it's valid)\n const toolName =\n \"name\" in tool && typeof tool.name === \"string\" ? tool.name : \"<unknown>\";\n\n // Check for deprecated toolSchema - throw error with migration instructions\n if (\"toolSchema\" in tool) {\n throw new Error(\n `Tool \"${toolName}\" uses deprecated \"toolSchema\" property. ` +\n `Migrate to \"inputSchema\" and \"outputSchema\" properties. ` +\n `See migration guide: https://docs.tambo.co/reference/react-sdk/migration`,\n );\n }\n\n // Validate required properties exist\n if (!(\"name\" in tool) || typeof tool.name !== \"string\") {\n throw new Error(\"Tool must have a 'name' property of type string\");\n }\n\n if (!(\"description\" in tool) || typeof tool.description !== \"string\") {\n throw new Error(\n `Tool \"${tool.name}\" must have a 'description' property of type string`,\n );\n }\n\n if (!(\"tool\" in tool) || typeof tool.tool !== \"function\") {\n throw new Error(\n `Tool \"${tool.name}\" must have a 'tool' property of type function`,\n );\n }\n\n if (!(\"inputSchema\" in tool)) {\n throw new Error(`Tool \"${tool.name}\" must have an 'inputSchema' property`);\n }\n\n if (!(\"outputSchema\" in tool)) {\n throw new Error(`Tool \"${tool.name}\" must have an 'outputSchema' property`);\n }\n\n // Validate tool name format\n assertValidName(tool.name, \"tool\");\n\n // Validate tool schemas - inputSchema must be an object schema\n if (tool.inputSchema) {\n assertObjectSchema(tool.inputSchema, `inputSchema of tool \"${tool.name}\"`);\n assertNoRecordSchema(\n tool.inputSchema,\n `inputSchema of tool \"${tool.name}\"`,\n );\n }\n\n // Validate maxCalls if provided - must be a positive integer\n if (\n \"maxCalls\" in tool &&\n tool.maxCalls !== undefined &&\n tool.maxCalls !== null\n ) {\n const maxCalls = tool.maxCalls;\n if (\n typeof maxCalls !== \"number\" ||\n !Number.isInteger(maxCalls) ||\n maxCalls < 0\n ) {\n throw new Error(\n `maxCalls for tool \"${tool.name}\" must be a positive integer`,\n );\n }\n }\n}\n\n/**\n * Converts a props schema to a serialized JSON Schema format.\n * @param propsDefinition - Deprecated: legacy props definition (will log warning)\n * @param propsSchema - The props schema (Standard Schema or JSON Schema)\n * @param name - Component/tool name for error messages\n * @returns Serialized JSON Schema object\n */\nfunction getSerializedProps(\n propsDefinition: unknown,\n propsSchema: unknown,\n name: string,\n): Record<string, unknown> {\n if (propsDefinition) {\n console.warn(`propsDefinition is deprecated. Use propsSchema instead.`);\n return propsDefinition as Record<string, unknown>;\n }\n\n // Check for Standard Schema (Zod, Valibot, ArkType, etc.)\n if (isStandardSchema(propsSchema)) {\n try {\n return schemaToJsonSchema(propsSchema) as Record<string, unknown>;\n } catch (error) {\n console.error(\n `Error converting ${name} props schema to JSON Schema:`,\n error,\n );\n throw new Error(\n `Error converting ${name} props schema to JSON Schema: ${error}`,\n );\n }\n }\n\n // Check for JSON Schema\n if (looksLikeJSONSchema(propsSchema)) {\n return propsSchema as Record<string, unknown>;\n }\n\n throw new Error(`Invalid props schema for ${name}`);\n}\n\n/**\n * Validates a component and prepares its props for registration.\n * Throws an error if the component is invalid.\n * @param component - The component to validate and prepare\n * @returns Object containing the serialized props\n */\nexport function validateAndPrepareComponent(component: TamboComponent): {\n props: Record<string, unknown>;\n} {\n const { name, propsSchema, propsDefinition } = component;\n\n // Validate component name\n assertValidName(name, \"component\");\n\n // Validate that at least one props definition is provided\n if (!propsSchema && !propsDefinition) {\n throw new Error(\n `Component ${name} must have either propsSchema (recommended) or propsDefinition defined`,\n );\n }\n\n // Validate that only one props definition is provided\n if (propsSchema && propsDefinition) {\n throw new Error(\n `Component ${name} cannot have both propsSchema and propsDefinition defined. Use only one. We recommend using propsSchema.`,\n );\n }\n\n // Validate that the propsSchema does not include record types\n if (propsSchema) {\n assertNoRecordSchema(propsSchema, `propsSchema of component \"${name}\"`);\n }\n\n // Convert propsSchema to JSON Schema if it exists\n const props = getSerializedProps(propsDefinition, propsSchema, name);\n\n return { props };\n}\n\n/**\n * Validates a tool association between a component and tool.\n * Throws an error if the association is invalid.\n * @param componentName - The component name\n * @param toolName - The tool name\n * @param componentExists - Whether the component exists in the registry\n * @param toolExists - Whether the tool exists in the registry\n */\nexport function validateToolAssociation(\n componentName: string,\n toolName: string,\n componentExists: boolean,\n toolExists: boolean,\n): void {\n // Validate component and tool names\n assertValidName(componentName, \"component\");\n assertValidName(toolName, \"tool\");\n\n if (!componentExists) {\n throw new Error(`Component ${componentName} not found in registry`);\n }\n if (!toolExists) {\n throw new Error(`Tool ${toolName} not found in registry`);\n }\n}\n"]}
|
|
@@ -127,7 +127,7 @@ export declare function createRunStream(params: CreateRunStreamParams): Promise<
|
|
|
127
127
|
export declare function useTamboSendMessage(threadId?: string): import("@tanstack/react-query").UseMutationResult<{
|
|
128
128
|
threadId: string | undefined;
|
|
129
129
|
preMutationMessageCount: number;
|
|
130
|
-
|
|
130
|
+
threadAlreadyHasName: boolean;
|
|
131
131
|
}, Error, SendMessageOptions, unknown>;
|
|
132
132
|
export {};
|
|
133
133
|
//# sourceMappingURL=use-tambo-v1-send-message.d.ts.map
|
|
@@ -107,13 +107,13 @@ function dispatchToolResults(dispatch, targetThreadId, toolResults) {
|
|
|
107
107
|
* Checks whether a thread name should be auto-generated based on config
|
|
108
108
|
* and the current thread state.
|
|
109
109
|
* @param threadId - The thread ID (undefined or placeholder means no)
|
|
110
|
-
* @param
|
|
110
|
+
* @param threadAlreadyHasName - Whether the thread already has a name
|
|
111
111
|
* @param preMutationMessageCount - Message count before the current mutation
|
|
112
112
|
* @param autoGenerateNameThreshold - Minimum message count to trigger generation
|
|
113
113
|
* @returns Whether to generate a thread name
|
|
114
114
|
*/
|
|
115
|
-
function shouldGenerateThreadName(threadId,
|
|
116
|
-
return (!
|
|
115
|
+
function shouldGenerateThreadName(threadId, threadAlreadyHasName, preMutationMessageCount, autoGenerateNameThreshold) {
|
|
116
|
+
return (!threadAlreadyHasName &&
|
|
117
117
|
!!threadId &&
|
|
118
118
|
!(0, event_accumulator_1.isPlaceholderThreadId)(threadId) &&
|
|
119
119
|
// +2 accounts for the user message and assistant response just added
|
|
@@ -147,7 +147,7 @@ function parseToolCallArgs(toolTracker, toolCallId) {
|
|
|
147
147
|
return undefined;
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
|
-
* Generates a thread name via the beta API, dispatches the
|
|
150
|
+
* Generates a thread name via the beta API, dispatches the name update,
|
|
151
151
|
* and invalidates the thread list cache. Errors are logged, never thrown.
|
|
152
152
|
* @param client - The Tambo API client
|
|
153
153
|
* @param dispatch - Stream state dispatcher
|
|
@@ -159,9 +159,9 @@ async function generateThreadName(client, dispatch, queryClient, threadId) {
|
|
|
159
159
|
const threadWithName = await client.beta.threads.generateName(threadId);
|
|
160
160
|
if (threadWithName.name) {
|
|
161
161
|
dispatch({
|
|
162
|
-
type: "
|
|
162
|
+
type: "UPDATE_THREAD_NAME",
|
|
163
163
|
threadId,
|
|
164
|
-
|
|
164
|
+
name: threadWithName.name,
|
|
165
165
|
});
|
|
166
166
|
await queryClient.invalidateQueries({
|
|
167
167
|
queryKey: ["v1-threads", "list"],
|
|
@@ -344,7 +344,7 @@ function useTamboSendMessage(threadId) {
|
|
|
344
344
|
// Capture pre-mutation state for auto thread name generation
|
|
345
345
|
const existingThread = streamState.threadMap[apiThreadId ?? ""];
|
|
346
346
|
const preMutationMessageCount = existingThread?.thread.messages.length ?? 0;
|
|
347
|
-
const
|
|
347
|
+
const threadAlreadyHasName = !!existingThread?.thread.name;
|
|
348
348
|
const toolTracker = new tool_call_tracker_1.ToolCallTracker();
|
|
349
349
|
const throttledStreamable = (0, tool_executor_1.createThrottledStreamableExecutor)(toolTracker, registry.toolRegistry);
|
|
350
350
|
// Generate a stable message ID for the user message
|
|
@@ -458,7 +458,7 @@ function useTamboSendMessage(threadId) {
|
|
|
458
458
|
return {
|
|
459
459
|
threadId: actualThreadId,
|
|
460
460
|
preMutationMessageCount,
|
|
461
|
-
|
|
461
|
+
threadAlreadyHasName,
|
|
462
462
|
};
|
|
463
463
|
}
|
|
464
464
|
catch (error) {
|
|
@@ -484,7 +484,7 @@ function useTamboSendMessage(threadId) {
|
|
|
484
484
|
queryKey: ["v1-threads", result.threadId],
|
|
485
485
|
});
|
|
486
486
|
if (autoGenerateThreadName &&
|
|
487
|
-
shouldGenerateThreadName(result.threadId, result.
|
|
487
|
+
shouldGenerateThreadName(result.threadId, result.threadAlreadyHasName, result.preMutationMessageCount, autoGenerateNameThreshold)) {
|
|
488
488
|
await generateThreadName(client, dispatch, queryClient, result.threadId);
|
|
489
489
|
}
|
|
490
490
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tambo-v1-send-message.js","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1-send-message.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;AA4Yb,0CA8DC;AA8CD,kDA+PC;AArvBD;;;;GAIG;AAEH,iCAA0C;AAC1C,sCAA4D;AAC5D,0CAAgF;AAGhF,iFAG+C;AAC/C,qEAAiE;AACjE,qFAGiD;AACjD,kFAG8C;AAC9C,sEAAgE;AAChE,uEAA8D;AAC9D,mGAAwF;AAGxF,kEAGoC;AACpC,sEAGsC;AACtC,4DAA4D;AAC5D,0DAGgC;AAGhC,kEAA6D;AAC7D,+CAAyD;AAEzD;;;;;;GAMG;AACH,SAAS,mBAAmB,CAC1B,QAAsC,EACtC,cAAsB,EACtB,SAAiB,EACjB,WAAmB;IAEnB,QAAQ,CAAC;QACP,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,cAAc;QACxB,KAAK,EAAE;YACL,IAAI,EAAE,gBAAS,CAAC,kBAAkB;YAClC,SAAS;Y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oBACL,QAAQ,EAAE,cAAc;oBACxB,uBAAuB;oBACvB,qBAAqB;iBACtB,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,gEAAgE;gBAChE,mEAAmE;gBACnE,IAAI,cAAc,EAAE,CAAC;oBACnB,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,yBAAyB,CAAC;oBACrE,MAAM,UAAU,GAAkB;wBAChC,IAAI,EAAE,gBAAS,CAAC,SAAS;wBACzB,OAAO,EAAE,YAAY;qBACtB,CAAC;oBACF,QAAQ,CAAC;wBACP,IAAI,EAAE,OAAO;wBACb,KAAK,EAAE,UAAU;wBACjB,QAAQ,EAAE,cAAc;qBACzB,CAAC,CAAC;gBACL,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;QACD,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YAC1B,MAAM,WAAW,CAAC,iBAAiB,CAAC;gBAClC,QAAQ,EAAE,CAAC,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;aAC1C,CAAC,CAAC;YAEH,IACE,sBAAsB;gBACtB,wBAAwB,CACtB,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,qBAAqB,EAC5B,MAAM,CAAC,uBAAuB,EAC9B,yBAAyB,CAC1B,EACD,CAAC;gBACD,MAAM,kBAAkB,CACtB,MAAM,EACN,QAAQ,EACR,WAAW,EACX,MAAM,CAAC,QAAQ,CAChB,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YACjB,OAAO,CAAC,KAAK,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["\"use client\";\n\n/**\n * Send Message Hook\n *\n * React Query mutation hook for sending messages and handling streaming responses.\n */\n\nimport React, { useContext } from \"react\";\nimport { EventType, type RunErrorEvent } from \"@ag-ui/core\";\nimport { asTamboCustomEvent, type RunAwaitingInputEvent } from \"../types/event\";\nimport type TamboAI from \"@tambo-ai/typescript-sdk\";\nimport type { Stream } from \"@tambo-ai/typescript-sdk/core/streaming\";\nimport {\n useTamboClient,\n useTamboQueryClient,\n} from \"../../providers/tambo-client-provider\";\nimport { useTamboMutation } from \"../../hooks/react-query-hooks\";\nimport {\n TamboRegistryContext,\n type TamboRegistryContext as TamboRegistry,\n} from \"../../providers/tambo-registry-provider\";\nimport {\n useStreamDispatch,\n useStreamState,\n} from \"../providers/tambo-v1-stream-context\";\nimport { useTamboConfig } from \"../providers/tambo-v1-provider\";\nimport { useTamboAuthState } from \"./use-tambo-v1-auth-state\";\nimport { useTamboContextHelpers } from \"../../providers/tambo-context-helpers-provider\";\nimport type { InitialInputMessage, InputMessage } from \"../types/message\";\nimport type { ToolChoice } from \"../types/tool-choice\";\nimport {\n isPlaceholderThreadId,\n type StreamAction,\n} from \"../utils/event-accumulator\";\nimport {\n toAvailableComponents,\n toAvailableTools,\n} from \"../utils/registry-conversion\";\nimport { handleEventStream } from \"../utils/stream-handler\";\nimport {\n executeAllPendingTools,\n createThrottledStreamableExecutor,\n} from \"../utils/tool-executor\";\nimport type { ToolResultContent } from \"@tambo-ai/typescript-sdk/resources/threads/threads\";\nimport type { RunCreateParams } from \"@tambo-ai/typescript-sdk/resources/threads/runs\";\nimport { ToolCallTracker } from \"../utils/tool-call-tracker\";\nimport { parse as parsePartialJson } from \"partial-json\";\n\n/**\n * Dispatches synthetic AG-UI events to show a user message in the thread.\n * @param dispatch - Stream state dispatcher\n * @param targetThreadId - Thread to add the message to\n * @param messageId - Stable ID for the user message\n * @param messageText - Text content of the message\n */\nfunction dispatchUserMessage(\n dispatch: React.Dispatch<StreamAction>,\n targetThreadId: string,\n messageId: string,\n messageText: string,\n): void {\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: \"user\" as const,\n },\n });\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: messageText,\n },\n });\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n },\n });\n}\n\n/**\n * Dispatches synthetic events for tool results as optimistic local state.\n * The server doesn't echo these back for client-side tools.\n * @param dispatch - Stream state dispatcher\n * @param targetThreadId - Thread to add results to\n * @param toolResults - Tool execution results to dispatch\n */\nfunction dispatchToolResults(\n dispatch: React.Dispatch<StreamAction>,\n targetThreadId: string,\n toolResults: ToolResultContent[],\n): void {\n if (toolResults.length === 0) return;\n\n const toolResultMessageId = `msg_tool_result_${Date.now()}`;\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_START,\n messageId: toolResultMessageId,\n role: \"user\" as const,\n },\n });\n\n for (const result of toolResults) {\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TOOL_CALL_RESULT,\n toolCallId: result.toolUseId,\n messageId: toolResultMessageId,\n content:\n result.content\n .filter((c) => c.type === \"text\")\n .map((c) => (c as { type: \"text\"; text: string }).text)\n .join(\"\") || JSON.stringify(result.content),\n },\n });\n }\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_END,\n messageId: toolResultMessageId,\n },\n });\n}\n\n/**\n * Checks whether a thread name should be auto-generated based on config\n * and the current thread state.\n * @param threadId - The thread ID (undefined or placeholder means no)\n * @param threadAlreadyHasTitle - Whether the thread already has a title\n * @param preMutationMessageCount - Message count before the current mutation\n * @param autoGenerateNameThreshold - Minimum message count to trigger generation\n * @returns Whether to generate a thread name\n */\nfunction shouldGenerateThreadName(\n threadId: string | undefined,\n threadAlreadyHasTitle: boolean,\n preMutationMessageCount: number,\n autoGenerateNameThreshold: number,\n): threadId is string {\n return (\n !threadAlreadyHasTitle &&\n !!threadId &&\n !isPlaceholderThreadId(threadId) &&\n // +2 accounts for the user message and assistant response just added\n preMutationMessageCount + 2 >= autoGenerateNameThreshold\n );\n}\n\n/**\n * Attempts to parse partial JSON from accumulated tool call args.\n *\n * Returns a parsed object if the accumulated args are parseable as\n * a JSON object, or undefined if parsing fails or the result is not\n * a plain object (e.g. array or primitive).\n * @param toolTracker - Tracker holding pending tool call state\n * @param toolCallId - The tool call ID to parse args for\n * @returns Parsed args object, or undefined if not parseable yet\n */\nfunction parseToolCallArgs(\n toolTracker: ToolCallTracker,\n toolCallId: string,\n): Record<string, unknown> | undefined {\n const accToolCall = toolTracker.getAccumulatingToolCall(toolCallId);\n if (!accToolCall) return undefined;\n\n try {\n const parsed: unknown = parsePartialJson(accToolCall.accumulatedArgs);\n if (\n typeof parsed === \"object\" &&\n parsed !== null &&\n !Array.isArray(parsed)\n ) {\n return parsed as Record<string, unknown>;\n }\n } catch {\n /* not parseable yet */\n }\n return undefined;\n}\n\n/**\n * Generates a thread name via the beta API, dispatches the title update,\n * and invalidates the thread list cache. Errors are logged, never thrown.\n * @param client - The Tambo API client\n * @param dispatch - Stream state dispatcher\n * @param queryClient - React Query client for cache invalidation\n * @param threadId - The thread to generate a name for\n */\nasync function generateThreadName(\n client: TamboAI,\n dispatch: React.Dispatch<StreamAction>,\n queryClient: ReturnType<typeof useTamboQueryClient>,\n threadId: string,\n): Promise<void> {\n try {\n const threadWithName = await client.beta.threads.generateName(threadId);\n if (threadWithName.name) {\n dispatch({\n type: \"UPDATE_THREAD_TITLE\",\n threadId,\n title: threadWithName.name,\n });\n await queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", \"list\"],\n });\n }\n } catch (error) {\n console.error(\n \"[useTamboSendMessage] Failed to auto-generate thread name:\",\n error,\n );\n }\n}\n\n/**\n * Options for sending a message\n */\nexport interface SendMessageOptions {\n /**\n * The message to send\n */\n message: InputMessage;\n\n /**\n * User message text for optimistic display.\n * If provided, synthetic AG-UI events will be dispatched to show\n * the user message in the thread immediately after getting the threadId.\n */\n userMessageText?: string;\n\n /**\n * Enable debug logging for the stream\n */\n debug?: boolean;\n\n /**\n * How the model should use tools. Defaults to \"auto\".\n * - \"auto\": Model decides whether to use tools\n * - \"required\": Model must use at least one tool\n * - \"none\": Model cannot use tools\n * - { name: \"toolName\" }: Model must use the specified tool\n */\n toolChoice?: ToolChoice;\n}\n\n/**\n * Parameters for creating a run stream\n */\nexport interface CreateRunStreamParams {\n client: TamboAI;\n threadId: string | undefined;\n message: InputMessage;\n registry: TamboRegistry;\n userKey: string | undefined;\n /**\n * Previous run ID for continuing a thread with existing messages.\n * Required when threadId is provided and the thread has previous runs.\n */\n previousRunId: string | undefined;\n /**\n * Additional context gathered from context helpers (including interactables).\n * Merged into the message's additionalContext before sending.\n */\n additionalContext?: Record<string, unknown>;\n /**\n * How the model should use tools.\n */\n toolChoice?: ToolChoice;\n /**\n * Initial messages to seed the thread with when creating a new thread.\n * Only used when threadId is undefined (new thread creation).\n */\n initialMessages?: InitialInputMessage[];\n}\n\n/**\n * Stream types from the SDK\n */\ntype RunStream = Stream<TamboAI.Threads.Runs.RunRunResponse>;\ntype CreateStream = Stream<TamboAI.Threads.Runs.RunCreateResponse>;\n\n/**\n * Result from creating a run stream\n */\nexport interface CreateRunStreamResult {\n stream: RunStream | CreateStream;\n initialThreadId: string | undefined;\n}\n\n/**\n * Parameters for executing tools and continuing the run\n */\ninterface ExecuteToolsParams {\n event: RunAwaitingInputEvent;\n toolTracker: ToolCallTracker;\n registry: TamboRegistry;\n client: TamboAI;\n threadId: string;\n runId: string;\n userKey: string | undefined;\n additionalContext?: Record<string, unknown>;\n toolChoice?: ToolChoice;\n}\n\n/**\n * Result from executing tools and continuing the run\n */\ninterface ExecuteToolsResult {\n stream: RunStream;\n toolResults: ToolResultContent[];\n}\n\n/**\n * Executes pending tools and returns a continuation stream.\n *\n * This function does NOT process the continuation stream - it just executes\n * the tools and returns the new stream for the caller to process. This enables\n * the flat loop pattern that correctly handles multi-round tool execution.\n * @param params - The parameters for tool execution\n * @returns The continuation stream and tool results for optimistic local state updates\n */\nasync function executeToolsAndContinue(\n params: ExecuteToolsParams,\n): Promise<ExecuteToolsResult> {\n const {\n event,\n toolTracker,\n registry,\n client,\n threadId,\n runId,\n userKey,\n additionalContext,\n toolChoice,\n } = params;\n\n const pendingToolCallIds = event.value.pendingToolCalls.map(\n (tc) => tc.toolCallId,\n );\n const toolCallsToExecute = toolTracker.getToolCallsById(pendingToolCallIds);\n\n // Execute tools\n const toolResults = await executeAllPendingTools(\n toolCallsToExecute,\n registry.toolRegistry,\n );\n\n // Clear executed tool calls before continuing\n toolTracker.clearToolCalls(pendingToolCallIds);\n\n // Return the continuation stream and tool results\n const stream = await client.threads.runs.run(threadId, {\n message: {\n role: \"user\",\n content: toolResults,\n additionalContext,\n },\n previousRunId: runId,\n availableComponents: toAvailableComponents(registry.componentList),\n tools: toAvailableTools(registry.toolRegistry),\n userKey,\n toolChoice,\n });\n\n return { stream, toolResults };\n}\n\n/**\n * Creates a run stream by calling the appropriate API method.\n *\n * If threadId is provided, runs on existing thread via client.threads.runs.run().\n * If no threadId, creates new thread via client.threads.runs.create().\n * @param params - The parameters for creating the run stream\n * @returns The stream and initial thread ID (undefined if creating new thread)\n */\nexport async function createRunStream(\n params: CreateRunStreamParams,\n): Promise<CreateRunStreamResult> {\n const {\n client,\n threadId,\n message,\n registry,\n userKey,\n previousRunId,\n additionalContext,\n toolChoice,\n initialMessages,\n } = params;\n\n // Merge helper context with any caller-provided additionalContext on the message\n const mergedContext =\n additionalContext || message.additionalContext\n ? {\n ...((message.additionalContext as Record<string, unknown>) ?? {}),\n ...additionalContext,\n }\n : undefined;\n const messageWithContext: InputMessage = mergedContext\n ? { ...message, additionalContext: mergedContext }\n : message;\n\n // Convert registry components/tools to API format\n const availableComponents = toAvailableComponents(registry.componentList);\n const availableTools = toAvailableTools(registry.toolRegistry);\n\n if (threadId) {\n // Run on existing thread\n const stream = await client.threads.runs.run(threadId, {\n message: messageWithContext,\n availableComponents,\n tools: availableTools,\n userKey,\n previousRunId,\n toolChoice,\n });\n return { stream, initialThreadId: threadId };\n } else {\n // Create new thread - include initialMessages if provided\n const threadConfig: RunCreateParams.Thread = {};\n if (userKey) {\n threadConfig.userKey = userKey;\n }\n if (initialMessages?.length) {\n threadConfig.initialMessages = initialMessages;\n }\n\n const stream = await client.threads.runs.create({\n message: messageWithContext,\n availableComponents,\n tools: availableTools,\n thread: Object.keys(threadConfig).length > 0 ? threadConfig : undefined,\n toolChoice,\n });\n // threadId will be extracted from first event (RUN_STARTED)\n return { stream, initialThreadId: undefined };\n }\n}\n\n/**\n * Hook to send a message and handle streaming responses.\n *\n * This hook handles two scenarios:\n * - If threadId provided: runs on existing thread via client.threads.runs.run()\n * - If no threadId: creates new thread via client.threads.runs.create()\n *\n * The hook:\n * - Sends a user message to the API\n * - Streams AG-UI events in real-time\n * - Dispatches events to the stream reducer\n * - Extracts threadId from events when creating new thread\n * - Handles tool execution (Phase 6)\n * - Invalidates thread queries on completion\n * @param threadId - Optional thread ID to send message to. If not provided, creates new thread\n * @returns React Query mutation object with threadId in mutation result\n * @example\n * ```tsx\n * function ChatInput({ threadId }: { threadId?: string }) {\n * const sendMessage = useTamboSendMessage(threadId);\n *\n * const handleSubmit = async (text: string) => {\n * const result = await sendMessage.mutateAsync({\n * message: {\n * role: \"user\",\n * content: [{ type: \"text\", text }],\n * },\n * });\n *\n * // If threadId wasn't provided, a new thread was created\n * if (!threadId) {\n * console.log(\"Created thread:\", result.threadId);\n * }\n * };\n *\n * return (\n * <div>\n * <input onSubmit={handleSubmit} />\n * {sendMessage.isPending && <Spinner />}\n * </div>\n * );\n * }\n * ```\n */\nexport function useTamboSendMessage(threadId?: string) {\n const client = useTamboClient();\n const dispatch = useStreamDispatch();\n const streamState = useStreamState();\n const {\n userKey,\n autoGenerateThreadName = true,\n autoGenerateNameThreshold = 3,\n initialMessages,\n } = useTamboConfig();\n const registry = useContext(TamboRegistryContext);\n const queryClient = useTamboQueryClient();\n const { getAdditionalContext } = useTamboContextHelpers();\n const authState = useTamboAuthState();\n\n if (!registry) {\n throw new Error(\n \"useTamboSendMessage must be used within TamboRegistryProvider\",\n );\n }\n\n // Placeholder ID isn't a valid API thread ID - treat as new thread creation\n const isNewThread = isPlaceholderThreadId(threadId);\n const apiThreadId = isNewThread ? undefined : threadId;\n\n // Get previousRunId from the thread's streaming state (active run) or\n // lastCompletedRunId (persisted after run finishes / loaded from API).\n // The latter is essential after page reload when streaming state is gone.\n const threadState = apiThreadId\n ? streamState.threadMap[apiThreadId]\n : undefined;\n const previousRunId =\n threadState?.streaming.runId ?? threadState?.lastCompletedRunId;\n\n return useTamboMutation({\n mutationFn: async (options: SendMessageOptions) => {\n if (authState.status !== \"identified\") {\n const messages: Record<string, string> = {\n unauthenticated:\n \"Cannot send message: no userKey or userToken provided. \" +\n \"Configure authentication in TamboProvider.\",\n exchanging:\n \"Cannot send message: token exchange is still in progress. \" +\n \"Wait for authentication to complete.\",\n error:\n \"Cannot send message: token exchange failed. \" +\n \"Check your userToken configuration.\",\n invalid:\n \"Cannot send message: both userKey and userToken were provided. \" +\n \"You must provide one or the other, not both.\",\n };\n throw new Error(messages[authState.status]);\n }\n\n const { message, userMessageText, debug = false, toolChoice } = options;\n\n // Capture pre-mutation state for auto thread name generation\n const existingThread = streamState.threadMap[apiThreadId ?? \"\"];\n const preMutationMessageCount =\n existingThread?.thread.messages.length ?? 0;\n const threadAlreadyHasTitle = !!existingThread?.thread.title;\n\n const toolTracker = new ToolCallTracker();\n const throttledStreamable = createThrottledStreamableExecutor(\n toolTracker,\n registry.toolRegistry,\n );\n\n // Generate a stable message ID for the user message\n const userMessageId = userMessageText\n ? `user_msg_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`\n : undefined;\n\n // Add user message immediately for instant feedback\n // Use threadId (which could be temp_xxx for new threads) for display\n if (threadId && userMessageText && userMessageId) {\n dispatchUserMessage(dispatch, threadId, userMessageId, userMessageText);\n }\n\n // Gather additional context from all registered context helpers.\n // TODO: This snapshot is captured once and reused for the entire multi-round\n // tool loop. If interactables change during streaming (e.g. user interactions),\n // continuations will send stale context. Re-gather before each continuation\n // if freshness matters.\n const helperContexts = await getAdditionalContext();\n const additionalContext: Record<string, unknown> = {};\n for (const helperContext of helperContexts) {\n additionalContext[helperContext.name] = helperContext.context;\n }\n\n // Create the run stream\n // Include initialMessages only on first send (new thread creation)\n const { stream, initialThreadId } = await createRunStream({\n client,\n threadId: apiThreadId,\n message,\n registry,\n userKey,\n previousRunId,\n additionalContext,\n toolChoice,\n initialMessages: isNewThread ? initialMessages : undefined,\n });\n\n let actualThreadId = initialThreadId;\n let runId: string | undefined;\n let currentStream: CreateRunStreamResult[\"stream\"] = stream;\n\n try {\n // Outer loop handles stream replacement for multi-round tool execution.\n // When we hit awaiting_input, we execute tools, get a new stream, and continue.\n // This flat loop pattern correctly handles tool→AI→tool→AI chains.\n while (true) {\n let pendingAwaitingInput: RunAwaitingInputEvent | undefined;\n\n // Process current stream until completion or awaiting_input\n for await (const event of handleEventStream(currentStream, {\n debug,\n })) {\n // Extract threadId and runId from RUN_STARTED event\n if (event.type === EventType.RUN_STARTED) {\n runId = event.runId;\n actualThreadId ??= event.threadId;\n\n // For threads with no ID at all: add user message now that we have the real threadId\n // Note: temp thread migration (temp_xxx -> real ID) is handled automatically\n // by the reducer when it sees RUN_STARTED with a different threadId\n if (!threadId && userMessageText && userMessageId) {\n dispatchUserMessage(\n dispatch,\n actualThreadId,\n userMessageId,\n userMessageText,\n );\n }\n } else if (!actualThreadId) {\n throw new Error(\n `Expected first event to be RUN_STARTED with threadId, got: ${event.type}`,\n );\n }\n\n toolTracker.handleEvent(event);\n\n // Parse partial JSON once for TOOL_CALL_ARGS — reused by both dispatch and streamable execution\n const parsedToolArgs =\n event.type === EventType.TOOL_CALL_ARGS\n ? parseToolCallArgs(toolTracker, event.toolCallId)\n : undefined;\n\n dispatch({\n type: \"EVENT\",\n event,\n threadId: actualThreadId,\n parsedToolArgs,\n });\n\n // Schedule debounced streamable tool execution with the same pre-parsed args\n if (parsedToolArgs && event.type === EventType.TOOL_CALL_ARGS) {\n throttledStreamable.schedule(event.toolCallId, parsedToolArgs);\n }\n\n // Check for awaiting_input - if found, break to execute tools\n if (event.type === EventType.CUSTOM) {\n const customEvent = asTamboCustomEvent(event);\n if (customEvent?.name === \"tambo.run.awaiting_input\") {\n pendingAwaitingInput = customEvent;\n break; // Exit stream loop to handle tool execution\n }\n }\n }\n\n // Flush any pending debounced streamable calls before continuing\n throttledStreamable.flush();\n\n // If stream finished without awaiting_input, we're done\n if (!pendingAwaitingInput) {\n break;\n }\n\n // Execute tools and get continuation stream\n // These checks should never fail since awaiting_input comes after RUN_STARTED\n if (!runId || !actualThreadId) {\n throw new Error(\n \"Cannot continue run after awaiting_input: missing runId or threadId\",\n );\n }\n\n const { stream: continuationStream, toolResults } =\n await executeToolsAndContinue({\n event: pendingAwaitingInput,\n toolTracker,\n registry,\n client,\n threadId: actualThreadId,\n runId,\n userKey,\n additionalContext,\n toolChoice,\n });\n\n dispatchToolResults(dispatch, actualThreadId, toolResults);\n\n currentStream = continuationStream;\n }\n\n return {\n threadId: actualThreadId,\n preMutationMessageCount,\n threadAlreadyHasTitle,\n };\n } catch (error) {\n // Dispatch a synthetic RUN_ERROR event to clean up thread state\n // This ensures the thread doesn't stay stuck in \"streaming\" status\n if (actualThreadId) {\n const errorMessage =\n error instanceof Error ? error.message : \"Unknown streaming error\";\n const errorEvent: RunErrorEvent = {\n type: EventType.RUN_ERROR,\n message: errorMessage,\n };\n dispatch({\n type: \"EVENT\",\n event: errorEvent,\n threadId: actualThreadId,\n });\n }\n throw error;\n }\n },\n onSuccess: async (result) => {\n await queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", result.threadId],\n });\n\n if (\n autoGenerateThreadName &&\n shouldGenerateThreadName(\n result.threadId,\n result.threadAlreadyHasTitle,\n result.preMutationMessageCount,\n autoGenerateNameThreshold,\n )\n ) {\n await generateThreadName(\n client,\n dispatch,\n queryClient,\n result.threadId,\n );\n }\n },\n onError: (error) => {\n console.error(\"[useTamboSendMessage] Mutation failed:\", error);\n },\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"use-tambo-v1-send-message.js","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1-send-message.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;AA4Yb,0CA8DC;AA8CD,kDA+PC;AArvBD;;;;GAIG;AAEH,iCAA0C;AAC1C,sCAA4D;AAC5D,0CAAgF;AAGhF,iFAG+C;AAC/C,qEAAiE;AACjE,qFAGiD;AACjD,kFAG8C;AAC9C,sEAAgE;AAChE,uEAA8D;AAC9D,mGAAwF;AAGxF,kEAGoC;AACpC,sEAGsC;AACtC,4DAA4D;AAC5D,0DAGgC;AAGhC,kEAA6D;AAC7D,+CAAyD;AAEzD;;;;;;GAMG;AACH,SAAS,mBAAmB,CAC1B,QAAsC,EACtC,cAAsB,EACtB,SAAiB,EACjB,WAAmB;IAEnB,QAAQ,CAAC;QACP,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,cAAc;QACxB,KAAK,EAAE;YACL,IAAI,EAAE,gBAAS,CAAC,kBAAkB;YAClC,SAAS;YACT,IAAI,EAAE,MAAe;SACtB;KACF,CAAC,CAAC;IAEH,QAAQ,CAAC;QACP,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,cAAc;QACxB,KAAK,EAAE;YACL,IAAI,EAAE,gBAAS,CAAC,oBAAoB;YACpC,SAAS;YACT,KAAK,EAAE,WAAW;SACnB;KACF,CAAC,CAAC;IAEH,QAAQ,CAAC;QACP,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,cAAc;QACxB,KAAK,EAAE;YACL,IAAI,EAAE,gBAAS,CAAC,gBAAgB;YAChC,SAAS;SACV;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,SAAS,mBAAmB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AAO;wBACb,KAAK,EAAE,UAAU;wBACjB,QAAQ,EAAE,cAAc;qBACzB,CAAC,CAAC;gBACL,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;QACD,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YAC1B,MAAM,WAAW,CAAC,iBAAiB,CAAC;gBAClC,QAAQ,EAAE,CAAC,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;aAC1C,CAAC,CAAC;YAEH,IACE,sBAAsB;gBACtB,wBAAwB,CACtB,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,oBAAoB,EAC3B,MAAM,CAAC,uBAAuB,EAC9B,yBAAyB,CAC1B,EACD,CAAC;gBACD,MAAM,kBAAkB,CACtB,MAAM,EACN,QAAQ,EACR,WAAW,EACX,MAAM,CAAC,QAAQ,CAChB,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YACjB,OAAO,CAAC,KAAK,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["\"use client\";\n\n/**\n * Send Message Hook\n *\n * React Query mutation hook for sending messages and handling streaming responses.\n */\n\nimport React, { useContext } from \"react\";\nimport { EventType, type RunErrorEvent } from \"@ag-ui/core\";\nimport { asTamboCustomEvent, type RunAwaitingInputEvent } from \"../types/event\";\nimport type TamboAI from \"@tambo-ai/typescript-sdk\";\nimport type { Stream } from \"@tambo-ai/typescript-sdk/core/streaming\";\nimport {\n useTamboClient,\n useTamboQueryClient,\n} from \"../../providers/tambo-client-provider\";\nimport { useTamboMutation } from \"../../hooks/react-query-hooks\";\nimport {\n TamboRegistryContext,\n type TamboRegistryContext as TamboRegistry,\n} from \"../../providers/tambo-registry-provider\";\nimport {\n useStreamDispatch,\n useStreamState,\n} from \"../providers/tambo-v1-stream-context\";\nimport { useTamboConfig } from \"../providers/tambo-v1-provider\";\nimport { useTamboAuthState } from \"./use-tambo-v1-auth-state\";\nimport { useTamboContextHelpers } from \"../../providers/tambo-context-helpers-provider\";\nimport type { InitialInputMessage, InputMessage } from \"../types/message\";\nimport type { ToolChoice } from \"../types/tool-choice\";\nimport {\n isPlaceholderThreadId,\n type StreamAction,\n} from \"../utils/event-accumulator\";\nimport {\n toAvailableComponents,\n toAvailableTools,\n} from \"../utils/registry-conversion\";\nimport { handleEventStream } from \"../utils/stream-handler\";\nimport {\n executeAllPendingTools,\n createThrottledStreamableExecutor,\n} from \"../utils/tool-executor\";\nimport type { ToolResultContent } from \"@tambo-ai/typescript-sdk/resources/threads/threads\";\nimport type { RunCreateParams } from \"@tambo-ai/typescript-sdk/resources/threads/runs\";\nimport { ToolCallTracker } from \"../utils/tool-call-tracker\";\nimport { parse as parsePartialJson } from \"partial-json\";\n\n/**\n * Dispatches synthetic AG-UI events to show a user message in the thread.\n * @param dispatch - Stream state dispatcher\n * @param targetThreadId - Thread to add the message to\n * @param messageId - Stable ID for the user message\n * @param messageText - Text content of the message\n */\nfunction dispatchUserMessage(\n dispatch: React.Dispatch<StreamAction>,\n targetThreadId: string,\n messageId: string,\n messageText: string,\n): void {\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: \"user\" as const,\n },\n });\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: messageText,\n },\n });\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n },\n });\n}\n\n/**\n * Dispatches synthetic events for tool results as optimistic local state.\n * The server doesn't echo these back for client-side tools.\n * @param dispatch - Stream state dispatcher\n * @param targetThreadId - Thread to add results to\n * @param toolResults - Tool execution results to dispatch\n */\nfunction dispatchToolResults(\n dispatch: React.Dispatch<StreamAction>,\n targetThreadId: string,\n toolResults: ToolResultContent[],\n): void {\n if (toolResults.length === 0) return;\n\n const toolResultMessageId = `msg_tool_result_${Date.now()}`;\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_START,\n messageId: toolResultMessageId,\n role: \"user\" as const,\n },\n });\n\n for (const result of toolResults) {\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TOOL_CALL_RESULT,\n toolCallId: result.toolUseId,\n messageId: toolResultMessageId,\n content:\n result.content\n .filter((c) => c.type === \"text\")\n .map((c) => (c as { type: \"text\"; text: string }).text)\n .join(\"\") || JSON.stringify(result.content),\n },\n });\n }\n\n dispatch({\n type: \"EVENT\",\n threadId: targetThreadId,\n event: {\n type: EventType.TEXT_MESSAGE_END,\n messageId: toolResultMessageId,\n },\n });\n}\n\n/**\n * Checks whether a thread name should be auto-generated based on config\n * and the current thread state.\n * @param threadId - The thread ID (undefined or placeholder means no)\n * @param threadAlreadyHasName - Whether the thread already has a name\n * @param preMutationMessageCount - Message count before the current mutation\n * @param autoGenerateNameThreshold - Minimum message count to trigger generation\n * @returns Whether to generate a thread name\n */\nfunction shouldGenerateThreadName(\n threadId: string | undefined,\n threadAlreadyHasName: boolean,\n preMutationMessageCount: number,\n autoGenerateNameThreshold: number,\n): threadId is string {\n return (\n !threadAlreadyHasName &&\n !!threadId &&\n !isPlaceholderThreadId(threadId) &&\n // +2 accounts for the user message and assistant response just added\n preMutationMessageCount + 2 >= autoGenerateNameThreshold\n );\n}\n\n/**\n * Attempts to parse partial JSON from accumulated tool call args.\n *\n * Returns a parsed object if the accumulated args are parseable as\n * a JSON object, or undefined if parsing fails or the result is not\n * a plain object (e.g. array or primitive).\n * @param toolTracker - Tracker holding pending tool call state\n * @param toolCallId - The tool call ID to parse args for\n * @returns Parsed args object, or undefined if not parseable yet\n */\nfunction parseToolCallArgs(\n toolTracker: ToolCallTracker,\n toolCallId: string,\n): Record<string, unknown> | undefined {\n const accToolCall = toolTracker.getAccumulatingToolCall(toolCallId);\n if (!accToolCall) return undefined;\n\n try {\n const parsed: unknown = parsePartialJson(accToolCall.accumulatedArgs);\n if (\n typeof parsed === \"object\" &&\n parsed !== null &&\n !Array.isArray(parsed)\n ) {\n return parsed as Record<string, unknown>;\n }\n } catch {\n /* not parseable yet */\n }\n return undefined;\n}\n\n/**\n * Generates a thread name via the beta API, dispatches the name update,\n * and invalidates the thread list cache. Errors are logged, never thrown.\n * @param client - The Tambo API client\n * @param dispatch - Stream state dispatcher\n * @param queryClient - React Query client for cache invalidation\n * @param threadId - The thread to generate a name for\n */\nasync function generateThreadName(\n client: TamboAI,\n dispatch: React.Dispatch<StreamAction>,\n queryClient: ReturnType<typeof useTamboQueryClient>,\n threadId: string,\n): Promise<void> {\n try {\n const threadWithName = await client.beta.threads.generateName(threadId);\n if (threadWithName.name) {\n dispatch({\n type: \"UPDATE_THREAD_NAME\",\n threadId,\n name: threadWithName.name,\n });\n await queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", \"list\"],\n });\n }\n } catch (error) {\n console.error(\n \"[useTamboSendMessage] Failed to auto-generate thread name:\",\n error,\n );\n }\n}\n\n/**\n * Options for sending a message\n */\nexport interface SendMessageOptions {\n /**\n * The message to send\n */\n message: InputMessage;\n\n /**\n * User message text for optimistic display.\n * If provided, synthetic AG-UI events will be dispatched to show\n * the user message in the thread immediately after getting the threadId.\n */\n userMessageText?: string;\n\n /**\n * Enable debug logging for the stream\n */\n debug?: boolean;\n\n /**\n * How the model should use tools. Defaults to \"auto\".\n * - \"auto\": Model decides whether to use tools\n * - \"required\": Model must use at least one tool\n * - \"none\": Model cannot use tools\n * - { name: \"toolName\" }: Model must use the specified tool\n */\n toolChoice?: ToolChoice;\n}\n\n/**\n * Parameters for creating a run stream\n */\nexport interface CreateRunStreamParams {\n client: TamboAI;\n threadId: string | undefined;\n message: InputMessage;\n registry: TamboRegistry;\n userKey: string | undefined;\n /**\n * Previous run ID for continuing a thread with existing messages.\n * Required when threadId is provided and the thread has previous runs.\n */\n previousRunId: string | undefined;\n /**\n * Additional context gathered from context helpers (including interactables).\n * Merged into the message's additionalContext before sending.\n */\n additionalContext?: Record<string, unknown>;\n /**\n * How the model should use tools.\n */\n toolChoice?: ToolChoice;\n /**\n * Initial messages to seed the thread with when creating a new thread.\n * Only used when threadId is undefined (new thread creation).\n */\n initialMessages?: InitialInputMessage[];\n}\n\n/**\n * Stream types from the SDK\n */\ntype RunStream = Stream<TamboAI.Threads.Runs.RunRunResponse>;\ntype CreateStream = Stream<TamboAI.Threads.Runs.RunCreateResponse>;\n\n/**\n * Result from creating a run stream\n */\nexport interface CreateRunStreamResult {\n stream: RunStream | CreateStream;\n initialThreadId: string | undefined;\n}\n\n/**\n * Parameters for executing tools and continuing the run\n */\ninterface ExecuteToolsParams {\n event: RunAwaitingInputEvent;\n toolTracker: ToolCallTracker;\n registry: TamboRegistry;\n client: TamboAI;\n threadId: string;\n runId: string;\n userKey: string | undefined;\n additionalContext?: Record<string, unknown>;\n toolChoice?: ToolChoice;\n}\n\n/**\n * Result from executing tools and continuing the run\n */\ninterface ExecuteToolsResult {\n stream: RunStream;\n toolResults: ToolResultContent[];\n}\n\n/**\n * Executes pending tools and returns a continuation stream.\n *\n * This function does NOT process the continuation stream - it just executes\n * the tools and returns the new stream for the caller to process. This enables\n * the flat loop pattern that correctly handles multi-round tool execution.\n * @param params - The parameters for tool execution\n * @returns The continuation stream and tool results for optimistic local state updates\n */\nasync function executeToolsAndContinue(\n params: ExecuteToolsParams,\n): Promise<ExecuteToolsResult> {\n const {\n event,\n toolTracker,\n registry,\n client,\n threadId,\n runId,\n userKey,\n additionalContext,\n toolChoice,\n } = params;\n\n const pendingToolCallIds = event.value.pendingToolCalls.map(\n (tc) => tc.toolCallId,\n );\n const toolCallsToExecute = toolTracker.getToolCallsById(pendingToolCallIds);\n\n // Execute tools\n const toolResults = await executeAllPendingTools(\n toolCallsToExecute,\n registry.toolRegistry,\n );\n\n // Clear executed tool calls before continuing\n toolTracker.clearToolCalls(pendingToolCallIds);\n\n // Return the continuation stream and tool results\n const stream = await client.threads.runs.run(threadId, {\n message: {\n role: \"user\",\n content: toolResults,\n additionalContext,\n },\n previousRunId: runId,\n availableComponents: toAvailableComponents(registry.componentList),\n tools: toAvailableTools(registry.toolRegistry),\n userKey,\n toolChoice,\n });\n\n return { stream, toolResults };\n}\n\n/**\n * Creates a run stream by calling the appropriate API method.\n *\n * If threadId is provided, runs on existing thread via client.threads.runs.run().\n * If no threadId, creates new thread via client.threads.runs.create().\n * @param params - The parameters for creating the run stream\n * @returns The stream and initial thread ID (undefined if creating new thread)\n */\nexport async function createRunStream(\n params: CreateRunStreamParams,\n): Promise<CreateRunStreamResult> {\n const {\n client,\n threadId,\n message,\n registry,\n userKey,\n previousRunId,\n additionalContext,\n toolChoice,\n initialMessages,\n } = params;\n\n // Merge helper context with any caller-provided additionalContext on the message\n const mergedContext =\n additionalContext || message.additionalContext\n ? {\n ...((message.additionalContext as Record<string, unknown>) ?? {}),\n ...additionalContext,\n }\n : undefined;\n const messageWithContext: InputMessage = mergedContext\n ? { ...message, additionalContext: mergedContext }\n : message;\n\n // Convert registry components/tools to API format\n const availableComponents = toAvailableComponents(registry.componentList);\n const availableTools = toAvailableTools(registry.toolRegistry);\n\n if (threadId) {\n // Run on existing thread\n const stream = await client.threads.runs.run(threadId, {\n message: messageWithContext,\n availableComponents,\n tools: availableTools,\n userKey,\n previousRunId,\n toolChoice,\n });\n return { stream, initialThreadId: threadId };\n } else {\n // Create new thread - include initialMessages if provided\n const threadConfig: RunCreateParams.Thread = {};\n if (userKey) {\n threadConfig.userKey = userKey;\n }\n if (initialMessages?.length) {\n threadConfig.initialMessages = initialMessages;\n }\n\n const stream = await client.threads.runs.create({\n message: messageWithContext,\n availableComponents,\n tools: availableTools,\n thread: Object.keys(threadConfig).length > 0 ? threadConfig : undefined,\n toolChoice,\n });\n // threadId will be extracted from first event (RUN_STARTED)\n return { stream, initialThreadId: undefined };\n }\n}\n\n/**\n * Hook to send a message and handle streaming responses.\n *\n * This hook handles two scenarios:\n * - If threadId provided: runs on existing thread via client.threads.runs.run()\n * - If no threadId: creates new thread via client.threads.runs.create()\n *\n * The hook:\n * - Sends a user message to the API\n * - Streams AG-UI events in real-time\n * - Dispatches events to the stream reducer\n * - Extracts threadId from events when creating new thread\n * - Handles tool execution (Phase 6)\n * - Invalidates thread queries on completion\n * @param threadId - Optional thread ID to send message to. If not provided, creates new thread\n * @returns React Query mutation object with threadId in mutation result\n * @example\n * ```tsx\n * function ChatInput({ threadId }: { threadId?: string }) {\n * const sendMessage = useTamboSendMessage(threadId);\n *\n * const handleSubmit = async (text: string) => {\n * const result = await sendMessage.mutateAsync({\n * message: {\n * role: \"user\",\n * content: [{ type: \"text\", text }],\n * },\n * });\n *\n * // If threadId wasn't provided, a new thread was created\n * if (!threadId) {\n * console.log(\"Created thread:\", result.threadId);\n * }\n * };\n *\n * return (\n * <div>\n * <input onSubmit={handleSubmit} />\n * {sendMessage.isPending && <Spinner />}\n * </div>\n * );\n * }\n * ```\n */\nexport function useTamboSendMessage(threadId?: string) {\n const client = useTamboClient();\n const dispatch = useStreamDispatch();\n const streamState = useStreamState();\n const {\n userKey,\n autoGenerateThreadName = true,\n autoGenerateNameThreshold = 3,\n initialMessages,\n } = useTamboConfig();\n const registry = useContext(TamboRegistryContext);\n const queryClient = useTamboQueryClient();\n const { getAdditionalContext } = useTamboContextHelpers();\n const authState = useTamboAuthState();\n\n if (!registry) {\n throw new Error(\n \"useTamboSendMessage must be used within TamboRegistryProvider\",\n );\n }\n\n // Placeholder ID isn't a valid API thread ID - treat as new thread creation\n const isNewThread = isPlaceholderThreadId(threadId);\n const apiThreadId = isNewThread ? undefined : threadId;\n\n // Get previousRunId from the thread's streaming state (active run) or\n // lastCompletedRunId (persisted after run finishes / loaded from API).\n // The latter is essential after page reload when streaming state is gone.\n const threadState = apiThreadId\n ? streamState.threadMap[apiThreadId]\n : undefined;\n const previousRunId =\n threadState?.streaming.runId ?? threadState?.lastCompletedRunId;\n\n return useTamboMutation({\n mutationFn: async (options: SendMessageOptions) => {\n if (authState.status !== \"identified\") {\n const messages: Record<string, string> = {\n unauthenticated:\n \"Cannot send message: no userKey or userToken provided. \" +\n \"Configure authentication in TamboProvider.\",\n exchanging:\n \"Cannot send message: token exchange is still in progress. \" +\n \"Wait for authentication to complete.\",\n error:\n \"Cannot send message: token exchange failed. \" +\n \"Check your userToken configuration.\",\n invalid:\n \"Cannot send message: both userKey and userToken were provided. \" +\n \"You must provide one or the other, not both.\",\n };\n throw new Error(messages[authState.status]);\n }\n\n const { message, userMessageText, debug = false, toolChoice } = options;\n\n // Capture pre-mutation state for auto thread name generation\n const existingThread = streamState.threadMap[apiThreadId ?? \"\"];\n const preMutationMessageCount =\n existingThread?.thread.messages.length ?? 0;\n const threadAlreadyHasName = !!existingThread?.thread.name;\n\n const toolTracker = new ToolCallTracker();\n const throttledStreamable = createThrottledStreamableExecutor(\n toolTracker,\n registry.toolRegistry,\n );\n\n // Generate a stable message ID for the user message\n const userMessageId = userMessageText\n ? `user_msg_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`\n : undefined;\n\n // Add user message immediately for instant feedback\n // Use threadId (which could be temp_xxx for new threads) for display\n if (threadId && userMessageText && userMessageId) {\n dispatchUserMessage(dispatch, threadId, userMessageId, userMessageText);\n }\n\n // Gather additional context from all registered context helpers.\n // TODO: This snapshot is captured once and reused for the entire multi-round\n // tool loop. If interactables change during streaming (e.g. user interactions),\n // continuations will send stale context. Re-gather before each continuation\n // if freshness matters.\n const helperContexts = await getAdditionalContext();\n const additionalContext: Record<string, unknown> = {};\n for (const helperContext of helperContexts) {\n additionalContext[helperContext.name] = helperContext.context;\n }\n\n // Create the run stream\n // Include initialMessages only on first send (new thread creation)\n const { stream, initialThreadId } = await createRunStream({\n client,\n threadId: apiThreadId,\n message,\n registry,\n userKey,\n previousRunId,\n additionalContext,\n toolChoice,\n initialMessages: isNewThread ? initialMessages : undefined,\n });\n\n let actualThreadId = initialThreadId;\n let runId: string | undefined;\n let currentStream: CreateRunStreamResult[\"stream\"] = stream;\n\n try {\n // Outer loop handles stream replacement for multi-round tool execution.\n // When we hit awaiting_input, we execute tools, get a new stream, and continue.\n // This flat loop pattern correctly handles tool→AI→tool→AI chains.\n while (true) {\n let pendingAwaitingInput: RunAwaitingInputEvent | undefined;\n\n // Process current stream until completion or awaiting_input\n for await (const event of handleEventStream(currentStream, {\n debug,\n })) {\n // Extract threadId and runId from RUN_STARTED event\n if (event.type === EventType.RUN_STARTED) {\n runId = event.runId;\n actualThreadId ??= event.threadId;\n\n // For threads with no ID at all: add user message now that we have the real threadId\n // Note: temp thread migration (temp_xxx -> real ID) is handled automatically\n // by the reducer when it sees RUN_STARTED with a different threadId\n if (!threadId && userMessageText && userMessageId) {\n dispatchUserMessage(\n dispatch,\n actualThreadId,\n userMessageId,\n userMessageText,\n );\n }\n } else if (!actualThreadId) {\n throw new Error(\n `Expected first event to be RUN_STARTED with threadId, got: ${event.type}`,\n );\n }\n\n toolTracker.handleEvent(event);\n\n // Parse partial JSON once for TOOL_CALL_ARGS — reused by both dispatch and streamable execution\n const parsedToolArgs =\n event.type === EventType.TOOL_CALL_ARGS\n ? parseToolCallArgs(toolTracker, event.toolCallId)\n : undefined;\n\n dispatch({\n type: \"EVENT\",\n event,\n threadId: actualThreadId,\n parsedToolArgs,\n });\n\n // Schedule debounced streamable tool execution with the same pre-parsed args\n if (parsedToolArgs && event.type === EventType.TOOL_CALL_ARGS) {\n throttledStreamable.schedule(event.toolCallId, parsedToolArgs);\n }\n\n // Check for awaiting_input - if found, break to execute tools\n if (event.type === EventType.CUSTOM) {\n const customEvent = asTamboCustomEvent(event);\n if (customEvent?.name === \"tambo.run.awaiting_input\") {\n pendingAwaitingInput = customEvent;\n break; // Exit stream loop to handle tool execution\n }\n }\n }\n\n // Flush any pending debounced streamable calls before continuing\n throttledStreamable.flush();\n\n // If stream finished without awaiting_input, we're done\n if (!pendingAwaitingInput) {\n break;\n }\n\n // Execute tools and get continuation stream\n // These checks should never fail since awaiting_input comes after RUN_STARTED\n if (!runId || !actualThreadId) {\n throw new Error(\n \"Cannot continue run after awaiting_input: missing runId or threadId\",\n );\n }\n\n const { stream: continuationStream, toolResults } =\n await executeToolsAndContinue({\n event: pendingAwaitingInput,\n toolTracker,\n registry,\n client,\n threadId: actualThreadId,\n runId,\n userKey,\n additionalContext,\n toolChoice,\n });\n\n dispatchToolResults(dispatch, actualThreadId, toolResults);\n\n currentStream = continuationStream;\n }\n\n return {\n threadId: actualThreadId,\n preMutationMessageCount,\n threadAlreadyHasName,\n };\n } catch (error) {\n // Dispatch a synthetic RUN_ERROR event to clean up thread state\n // This ensures the thread doesn't stay stuck in \"streaming\" status\n if (actualThreadId) {\n const errorMessage =\n error instanceof Error ? error.message : \"Unknown streaming error\";\n const errorEvent: RunErrorEvent = {\n type: EventType.RUN_ERROR,\n message: errorMessage,\n };\n dispatch({\n type: \"EVENT\",\n event: errorEvent,\n threadId: actualThreadId,\n });\n }\n throw error;\n }\n },\n onSuccess: async (result) => {\n await queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", result.threadId],\n });\n\n if (\n autoGenerateThreadName &&\n shouldGenerateThreadName(\n result.threadId,\n result.threadAlreadyHasName,\n result.preMutationMessageCount,\n autoGenerateNameThreshold,\n )\n ) {\n await generateThreadName(\n client,\n dispatch,\n queryClient,\n result.threadId,\n );\n }\n },\n onError: (error) => {\n console.error(\"[useTamboSendMessage] Mutation failed:\", error);\n },\n });\n}\n"]}
|
|
@@ -93,6 +93,15 @@ export interface UseTamboReturn {
|
|
|
93
93
|
* When true, the SDK is ready to make API calls.
|
|
94
94
|
*/
|
|
95
95
|
isIdentified: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Update a thread's name.
|
|
98
|
+
* Useful for implementing manual thread renaming UI in history sidebars.
|
|
99
|
+
* Cache invalidation is best-effort; failures will be logged and won't reject.
|
|
100
|
+
* @param threadId - ID of the thread to rename
|
|
101
|
+
* @param name - New name for the thread
|
|
102
|
+
* @returns Promise that resolves when the update completes
|
|
103
|
+
*/
|
|
104
|
+
updateThreadName: (threadId: string, name: string) => Promise<void>;
|
|
96
105
|
}
|
|
97
106
|
/**
|
|
98
107
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tambo-v1.d.ts","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,OAAO,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"use-tambo-v1.d.ts","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,OAAO,MAAM,0BAA0B,CAAC;AAcpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAEL,KAAK,oBAAoB,IAAI,wBAAwB,EACtD,MAAM,yCAAyC,CAAC;AAEjD,OAAO,EACL,iBAAiB,EAGjB,KAAK,gBAAgB,EACtB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,KAAK,EAGV,kBAAkB,EAEnB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAEL,KAAK,WAAW,EACjB,MAAM,4BAA4B,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAEhC;;OAEG;IACH,QAAQ,EAAE,kBAAkB,EAAE,CAAC;IAE/B;;OAEG;IACH,cAAc,EAAE,cAAc,CAAC;IAE/B;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;OAEG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,iBAAiB,EAAE,wBAAwB,CAAC,mBAAmB,CAAC,CAAC;IAEjE;;OAEG;IACH,YAAY,EAAE,wBAAwB,CAAC,cAAc,CAAC,CAAC;IAEvD;;OAEG;IACH,aAAa,EAAE,wBAAwB,CAAC,eAAe,CAAC,CAAC;IAEzD;;OAEG;IACH,aAAa,EAAE,wBAAwB,CAAC,eAAe,CAAC,CAAC;IAEzD;;OAEG;IACH,YAAY,EAAE,wBAAwB,CAAC,cAAc,CAAC,CAAC;IAEvD;;OAEG;IACH,eAAe,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,UAAU,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAE3C;;OAEG;IACH,YAAY,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;IAE/C;;OAEG;IACH,cAAc,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;IAEnD;;OAEG;IACH,QAAQ,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC;IAE/C;;;;OAIG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAE/B;;;OAGG;IACH,SAAS,EAAE,cAAc,CAAC;IAE1B;;;OAGG;IACH,YAAY,EAAE,OAAO,CAAC;IAEtB;;;;;;;OAOG;IACH,gBAAgB,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACrE;AAwCD;;;GAGG;AACH,wBAAgB,QAAQ,IAAI,cAAc,CAqOzC"}
|
|
@@ -56,6 +56,7 @@ const event_accumulator_1 = require("../utils/event-accumulator");
|
|
|
56
56
|
*/
|
|
57
57
|
function useTambo() {
|
|
58
58
|
const client = (0, tambo_client_provider_1.useTamboClient)();
|
|
59
|
+
const queryClient = (0, tambo_client_provider_1.useTamboQueryClient)();
|
|
59
60
|
const { userKey } = (0, tambo_v1_provider_1.useTamboConfig)();
|
|
60
61
|
const streamState = (0, tambo_v1_stream_context_1.useStreamState)();
|
|
61
62
|
const dispatch = (0, tambo_v1_stream_context_1.useStreamDispatch)();
|
|
@@ -67,6 +68,10 @@ function useTambo() {
|
|
|
67
68
|
const componentCacheRef = (0, react_1.useRef)(new Map());
|
|
68
69
|
// Get thread state for the current thread
|
|
69
70
|
const threadState = streamState.threadMap[streamState.currentThreadId];
|
|
71
|
+
// Keep a live snapshot of the threadMap for callbacks without forcing them to
|
|
72
|
+
// re-create on every stream state update.
|
|
73
|
+
const threadMapRef = (0, react_1.useRef)(streamState.threadMap);
|
|
74
|
+
threadMapRef.current = streamState.threadMap;
|
|
70
75
|
// Cancel the current run on this thread
|
|
71
76
|
const cancelRun = (0, react_1.useCallback)(async () => {
|
|
72
77
|
const runId = threadState?.streaming.runId;
|
|
@@ -101,6 +106,30 @@ function useTambo() {
|
|
|
101
106
|
threadState?.streaming.runId,
|
|
102
107
|
dispatch,
|
|
103
108
|
]);
|
|
109
|
+
// Update a thread's name
|
|
110
|
+
const updateThreadName = (0, react_1.useCallback)(async (threadId, name) => {
|
|
111
|
+
await client.threads.update(threadId, { name });
|
|
112
|
+
if (threadMapRef.current[threadId]) {
|
|
113
|
+
dispatch({
|
|
114
|
+
type: "UPDATE_THREAD_NAME",
|
|
115
|
+
threadId,
|
|
116
|
+
name: name,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
await Promise.all([
|
|
121
|
+
queryClient.invalidateQueries({
|
|
122
|
+
queryKey: ["v1-threads", "list"],
|
|
123
|
+
}),
|
|
124
|
+
queryClient.invalidateQueries({
|
|
125
|
+
queryKey: ["v1-threads", threadId],
|
|
126
|
+
}),
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
console.warn("[useTambo] Failed to invalidate thread queries after rename:", error);
|
|
131
|
+
}
|
|
132
|
+
}, [client, dispatch, queryClient]);
|
|
104
133
|
// Memoize the return object to prevent unnecessary re-renders
|
|
105
134
|
return (0, react_1.useMemo)(() => {
|
|
106
135
|
const thread = threadState;
|
|
@@ -127,9 +156,6 @@ function useTambo() {
|
|
|
127
156
|
const input = content.input ?? {};
|
|
128
157
|
// Extract Tambo display props from input
|
|
129
158
|
const tamboDisplayProps = {
|
|
130
|
-
_tambo_displayMessage: typeof input._tambo_displayMessage === "string"
|
|
131
|
-
? input._tambo_displayMessage
|
|
132
|
-
: undefined,
|
|
133
159
|
_tambo_statusMessage: typeof input._tambo_statusMessage === "string"
|
|
134
160
|
? input._tambo_statusMessage
|
|
135
161
|
: undefined,
|
|
@@ -213,9 +239,11 @@ function useTambo() {
|
|
|
213
239
|
cancelRun,
|
|
214
240
|
authState,
|
|
215
241
|
isIdentified: authState.status === "identified",
|
|
242
|
+
updateThreadName,
|
|
216
243
|
};
|
|
217
244
|
}, [
|
|
218
245
|
cancelRun,
|
|
246
|
+
updateThreadName,
|
|
219
247
|
client,
|
|
220
248
|
threadState,
|
|
221
249
|
registry,
|