@tambo-ai/react 1.0.0 → 1.0.2

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.
Files changed (56) hide show
  1. package/README.md +42 -20
  2. package/dist/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
  3. package/dist/v1/hooks/use-tambo-v1-send-message.js +4 -31
  4. package/dist/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
  5. package/dist/v1/hooks/use-tambo-v1.js +2 -2
  6. package/dist/v1/hooks/use-tambo-v1.js.map +1 -1
  7. package/dist/v1/hooks/use-tambo-v1.test.js +2 -0
  8. package/dist/v1/hooks/use-tambo-v1.test.js.map +1 -1
  9. package/dist/v1/utils/event-accumulator.d.ts +3 -0
  10. package/dist/v1/utils/event-accumulator.d.ts.map +1 -1
  11. package/dist/v1/utils/event-accumulator.js +26 -5
  12. package/dist/v1/utils/event-accumulator.js.map +1 -1
  13. package/dist/v1/utils/event-accumulator.test.js +113 -0
  14. package/dist/v1/utils/event-accumulator.test.js.map +1 -1
  15. package/dist/v1/utils/tool-call-tracker.d.ts +26 -4
  16. package/dist/v1/utils/tool-call-tracker.d.ts.map +1 -1
  17. package/dist/v1/utils/tool-call-tracker.js +82 -5
  18. package/dist/v1/utils/tool-call-tracker.js.map +1 -1
  19. package/dist/v1/utils/tool-call-tracker.test.js +178 -0
  20. package/dist/v1/utils/tool-call-tracker.test.js.map +1 -1
  21. package/dist/v1/utils/unstrictify.d.ts +32 -0
  22. package/dist/v1/utils/unstrictify.d.ts.map +1 -0
  23. package/dist/v1/utils/unstrictify.js +159 -0
  24. package/dist/v1/utils/unstrictify.js.map +1 -0
  25. package/dist/v1/utils/unstrictify.test.d.ts +2 -0
  26. package/dist/v1/utils/unstrictify.test.d.ts.map +1 -0
  27. package/dist/v1/utils/unstrictify.test.js +187 -0
  28. package/dist/v1/utils/unstrictify.test.js.map +1 -0
  29. package/esm/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
  30. package/esm/v1/hooks/use-tambo-v1-send-message.js +4 -31
  31. package/esm/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
  32. package/esm/v1/hooks/use-tambo-v1.js +2 -2
  33. package/esm/v1/hooks/use-tambo-v1.js.map +1 -1
  34. package/esm/v1/hooks/use-tambo-v1.test.js +2 -0
  35. package/esm/v1/hooks/use-tambo-v1.test.js.map +1 -1
  36. package/esm/v1/utils/event-accumulator.d.ts +3 -0
  37. package/esm/v1/utils/event-accumulator.d.ts.map +1 -1
  38. package/esm/v1/utils/event-accumulator.js +26 -5
  39. package/esm/v1/utils/event-accumulator.js.map +1 -1
  40. package/esm/v1/utils/event-accumulator.test.js +113 -0
  41. package/esm/v1/utils/event-accumulator.test.js.map +1 -1
  42. package/esm/v1/utils/tool-call-tracker.d.ts +26 -4
  43. package/esm/v1/utils/tool-call-tracker.d.ts.map +1 -1
  44. package/esm/v1/utils/tool-call-tracker.js +82 -5
  45. package/esm/v1/utils/tool-call-tracker.js.map +1 -1
  46. package/esm/v1/utils/tool-call-tracker.test.js +178 -0
  47. package/esm/v1/utils/tool-call-tracker.test.js.map +1 -1
  48. package/esm/v1/utils/unstrictify.d.ts +32 -0
  49. package/esm/v1/utils/unstrictify.d.ts.map +1 -0
  50. package/esm/v1/utils/unstrictify.js +155 -0
  51. package/esm/v1/utils/unstrictify.js.map +1 -0
  52. package/esm/v1/utils/unstrictify.test.d.ts +2 -0
  53. package/esm/v1/utils/unstrictify.test.d.ts.map +1 -0
  54. package/esm/v1/utils/unstrictify.test.js +185 -0
  55. package/esm/v1/utils/unstrictify.test.js.map +1 -0
  56. package/package.json +3 -3
@@ -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;AAEb;;;;GAIG;AAEH,OAAc,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAE,SAA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AAM,UAAU,GAAkB;wBAChC,IAAI,EAAE,SAAS,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,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"]}
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;AAEb;;;;GAIG;AAEH,OAAc,EAAE,UA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,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\";\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 * 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(registry.toolRegistry);\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 ? toolTracker.parsePartialArgs(event.toolCallId)\n : undefined;\n\n dispatch({\n type: \"EVENT\",\n event,\n threadId: actualThreadId,\n parsedToolArgs,\n toolSchemas: toolTracker.toolSchemas,\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 // Handle custom events\n if (event.type === EventType.CUSTOM) {\n const customEvent = asTamboCustomEvent(event);\n\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"]}
@@ -72,7 +72,7 @@ export function useTambo() {
72
72
  ]);
73
73
  // Update a thread's name
74
74
  const updateThreadName = useCallback(async (threadId, name) => {
75
- await client.threads.update(threadId, { name });
75
+ await client.threads.update(threadId, { name, userKey });
76
76
  if (threadMapRef.current[threadId]) {
77
77
  dispatch({
78
78
  type: "UPDATE_THREAD_NAME",
@@ -93,7 +93,7 @@ export function useTambo() {
93
93
  catch (error) {
94
94
  console.warn("[useTambo] Failed to invalidate thread queries after rename:", error);
95
95
  }
96
- }, [client, dispatch, queryClient]);
96
+ }, [client, userKey, dispatch, queryClient]);
97
97
  // Memoize the return object to prevent unnecessary re-renders
98
98
  return useMemo(() => {
99
99
  const thread = threadState;
@@ -1 +1 @@
1
- {"version":3,"file":"use-tambo-v1.js","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAE,EACZ,WAAW,EACX,UAAU,EACV,OAAO,EACP,MAAM,GAEP,MAAM,OAAO,CAAC;AACf,OAAO,EACL,cAAc,EACd,mBAAmB,GACpB,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAE9D,OAAO,EACL,oBAAoB,GAErB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,mBAAmB,GAEpB,MAAM,sCAAsC,CAAC;AAQ9C,OAAO,EACL,qBAAqB,GAEtB,MAAM,4BAA4B,CAAC;AA+JpC;;;GAG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ACT,SAAS;YACT,YAAY,EAAE,SAAS,CAAC,MAAM,KAAK,YAAY;YAC/C,gBAAgB;SACjB,CAAC;IACJ,CAAC,EAAE;QACD,SAAS;QACT,gBAAgB;QAChB,MAAM;QACN,WAAW;QACX,QAAQ;QACR,WAAW,CAAC,eAAe;QAC3B,gBAAgB;QAChB,QAAQ;QACR,SAAS;KACV,CAAC,CAAC;AACL,CAAC","sourcesContent":["\"use client\";\n\n/**\n * useTambo - Main Hook\n *\n * Combines all contexts into a single hook for convenient access\n * to thread state, streaming status, registry, and client.\n */\n\nimport { EventType } from \"@ag-ui/core\";\nimport type TamboAI from \"@tambo-ai/typescript-sdk\";\nimport React, {\n useCallback,\n useContext,\n useMemo,\n useRef,\n type ReactElement,\n} from \"react\";\nimport {\n useTamboClient,\n useTamboQueryClient,\n} from \"../../providers/tambo-client-provider\";\nimport { useTamboConfig } from \"../providers/tambo-v1-provider\";\nimport { useTamboAuthState } from \"./use-tambo-v1-auth-state\";\nimport type { TamboAuthState } from \"../types/auth\";\nimport {\n TamboRegistryContext,\n type TamboRegistryContext as TamboRegistryContextType,\n} from \"../../providers/tambo-registry-provider\";\nimport { ComponentRenderer } from \"../components/v1-component-renderer\";\nimport {\n useStreamDispatch,\n useStreamState,\n useThreadManagement,\n type ThreadManagement,\n} from \"../providers/tambo-v1-stream-context\";\nimport type {\n Content,\n TamboToolDisplayProps,\n TamboThreadMessage,\n TamboToolUseContent,\n} from \"../types/message\";\nimport type { StreamingState } from \"../types/thread\";\nimport {\n isPlaceholderThreadId,\n type ThreadState,\n} from \"../utils/event-accumulator\";\n\n/**\n * Return type for useTambo hook\n */\nexport interface UseTamboReturn {\n /**\n * The Tambo API client instance\n */\n client: TamboAI;\n\n /**\n * Current thread state for the given threadId, or undefined if not loaded\n */\n thread: ThreadState | undefined;\n\n /**\n * Messages in the current thread\n */\n messages: TamboThreadMessage[];\n\n /**\n * Current streaming state\n */\n streamingState: StreamingState;\n\n /**\n * Whether the thread is currently streaming a response\n */\n isStreaming: boolean;\n\n /**\n * Whether the thread is waiting for the AI to start responding\n */\n isWaiting: boolean;\n\n /**\n * Whether the thread is idle (not streaming or waiting)\n */\n isIdle: boolean;\n\n /**\n * Register a component with the registry\n */\n registerComponent: TamboRegistryContextType[\"registerComponent\"];\n\n /**\n * Register a tool with the registry\n */\n registerTool: TamboRegistryContextType[\"registerTool\"];\n\n /**\n * Register multiple tools with the registry\n */\n registerTools: TamboRegistryContextType[\"registerTools\"];\n\n /**\n * The component registry (Map of name -> component definition)\n */\n componentList: TamboRegistryContextType[\"componentList\"];\n\n /**\n * The tool registry (Map of name -> tool definition)\n */\n toolRegistry: TamboRegistryContextType[\"toolRegistry\"];\n\n /**\n * Current thread ID (always available - uses \"placeholder\" for new threads)\n */\n currentThreadId: string;\n\n /**\n * Initialize a new thread in the stream context\n */\n initThread: ThreadManagement[\"initThread\"];\n\n /**\n * Switch the current active thread\n */\n switchThread: ThreadManagement[\"switchThread\"];\n\n /**\n * Start a new thread (generates a temporary ID)\n */\n startNewThread: ThreadManagement[\"startNewThread\"];\n\n /**\n * Dispatch function for stream events (advanced usage)\n */\n dispatch: ReturnType<typeof useStreamDispatch>;\n\n /**\n * Cancel the current run on this thread.\n * Optimistically updates local state and sends cancellation request to the API.\n * No-op if there's no active run or thread is a placeholder.\n */\n cancelRun: () => Promise<void>;\n\n /**\n * Current authentication state.\n * Use this to show auth-related UI or conditionally render features.\n */\n authState: TamboAuthState;\n\n /**\n * Shorthand for `authState.status === \"identified\"`.\n * When true, the SDK is ready to make API calls.\n */\n isIdentified: boolean;\n\n /**\n * Update a thread's name.\n * Useful for implementing manual thread renaming UI in history sidebars.\n * Cache invalidation is best-effort; failures will be logged and won't reject.\n * @param threadId - ID of the thread to rename\n * @param name - New name for the thread\n * @returns Promise that resolves when the update completes\n */\n updateThreadName: (threadId: string, name: string) => Promise<void>;\n}\n\n/**\n * Main hook for accessing Tambo functionality.\n *\n * Combines thread state, streaming status, registry, and client\n * into a single convenient hook.\n *\n * Messages returned include renderedComponent on component content blocks,\n * allowing direct rendering via {content.renderedComponent}.\n * @param threadId - Optional thread ID to get state for\n * @returns Combined context with thread state, messages, and utilities\n * @example\n * ```tsx\n * function ChatInterface() {\n * const {\n * thread,\n * messages,\n * isStreaming,\n * registerComponent,\n * } = useTambo('thread_123');\n *\n * return (\n * <div>\n * {messages.map(msg => <Message key={msg.id} message={msg} />)}\n * {isStreaming && <LoadingIndicator />}\n * </div>\n * );\n * }\n * ```\n */\n/**\n * Cache entry for a rendered component wrapper.\n * Stores the element and the props JSON used to create it.\n */\ninterface ComponentCacheEntry {\n element: ReactElement;\n propsJson: string;\n}\n\n/**\n *\n * @returns The combined Tambo context\n */\nexport function useTambo(): UseTamboReturn {\n const client = useTamboClient();\n const queryClient = useTamboQueryClient();\n const { userKey } = useTamboConfig();\n const streamState = useStreamState();\n const dispatch = useStreamDispatch();\n const registry = useContext(TamboRegistryContext);\n const threadManagement = useThreadManagement();\n const authState = useTamboAuthState();\n\n // Cache for rendered component wrappers - maintains stable element references\n // across renders when props haven't changed\n const componentCacheRef = useRef<Map<string, ComponentCacheEntry>>(new Map());\n\n // Get thread state for the current thread\n const threadState = streamState.threadMap[streamState.currentThreadId];\n\n // Keep a live snapshot of the threadMap for callbacks without forcing them to\n // re-create on every stream state update.\n const threadMapRef = useRef(streamState.threadMap);\n threadMapRef.current = streamState.threadMap;\n\n // Cancel the current run on this thread\n const cancelRun = useCallback(async () => {\n const runId = threadState?.streaming.runId;\n const threadId = streamState.currentThreadId;\n\n // No-op if there's no active run or thread is a placeholder\n if (!runId || isPlaceholderThreadId(threadId)) {\n return;\n }\n\n // Optimistically update local state with RUN_ERROR event\n dispatch({\n type: \"EVENT\",\n threadId,\n event: {\n type: EventType.RUN_ERROR,\n message: \"Run cancelled\",\n code: \"CANCELLED\",\n timestamp: Date.now(),\n },\n });\n\n // Call API to cancel the run\n try {\n await client.threads.runs.delete(runId, { threadId, userKey });\n } catch (error) {\n // Log but don't rethrow - local state is already updated\n console.warn(\"Failed to cancel run on server:\", error);\n }\n }, [\n client,\n userKey,\n streamState.currentThreadId,\n threadState?.streaming.runId,\n dispatch,\n ]);\n\n // Update a thread's name\n const updateThreadName = useCallback(\n async (threadId: string, name: string) => {\n await client.threads.update(threadId, { name });\n\n if (threadMapRef.current[threadId]) {\n dispatch({\n type: \"UPDATE_THREAD_NAME\",\n threadId,\n name: name,\n });\n }\n\n try {\n await Promise.all([\n queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", \"list\"],\n }),\n queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", threadId],\n }),\n ]);\n } catch (error) {\n console.warn(\n \"[useTambo] Failed to invalidate thread queries after rename:\",\n error,\n );\n }\n },\n [client, dispatch, queryClient],\n );\n\n // Memoize the return object to prevent unnecessary re-renders\n return useMemo(() => {\n const thread = threadState;\n const rawMessages = thread?.thread.messages ?? [];\n const streamingState: StreamingState = thread?.streaming ?? {\n status: \"idle\" as const,\n };\n\n // Build a set of tool_use IDs that have completed (have a matching tool_result)\n // We need to look across all messages since tool_result might be in a different message\n const completedToolIds = new Set<string>();\n for (const msg of rawMessages) {\n for (const content of msg.content) {\n if (content.type === \"tool_result\") {\n completedToolIds.add(content.toolUseId);\n }\n }\n }\n\n // Transform messages to add computed properties to content blocks\n const messages = rawMessages.map((message): TamboThreadMessage => {\n const transformedContent = message.content.map((content): Content => {\n // Transform tool_use content to add computed state\n if (content.type === \"tool_use\") {\n const hasCompleted = completedToolIds.has(content.id);\n const input = content.input ?? {};\n\n // Extract Tambo display props from input\n const tamboDisplayProps: TamboToolDisplayProps = {\n _tambo_statusMessage:\n typeof input._tambo_statusMessage === \"string\"\n ? input._tambo_statusMessage\n : undefined,\n _tambo_completionStatusMessage:\n typeof input._tambo_completionStatusMessage === \"string\"\n ? input._tambo_completionStatusMessage\n : undefined,\n };\n\n // Compute status message based on completion state\n const statusMessage = hasCompleted\n ? (tamboDisplayProps._tambo_completionStatusMessage ??\n `Called ${content.name}`)\n : (tamboDisplayProps._tambo_statusMessage ??\n `Calling ${content.name}`);\n\n // Filter out _tambo_* properties from input - consumers only see actual tool params\n const cleanInput: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(input)) {\n if (!key.startsWith(\"_tambo_\")) {\n cleanInput[key] = value;\n }\n }\n\n const v1Content: TamboToolUseContent = {\n ...content,\n input: cleanInput,\n hasCompleted,\n statusMessage,\n tamboDisplayProps,\n };\n return v1Content;\n }\n\n if (content.type !== \"component\") {\n return content;\n }\n\n const componentContent = content;\n const propsJson = JSON.stringify(componentContent.props ?? {});\n const cache = componentCacheRef.current;\n const cached = cache.get(componentContent.id);\n\n // Return cached element if props haven't changed\n if (cached?.propsJson === propsJson) {\n return {\n ...componentContent,\n renderedComponent: cached.element,\n };\n }\n\n // Create new wrapper element\n const element = React.createElement(ComponentRenderer, {\n key: componentContent.id,\n content: componentContent,\n threadId: streamState.currentThreadId,\n messageId: message.id,\n });\n\n // Update cache\n cache.set(componentContent.id, { element, propsJson });\n\n return {\n ...componentContent,\n renderedComponent: element,\n };\n });\n\n return {\n ...message,\n content: transformedContent,\n };\n });\n\n return {\n client,\n thread,\n messages,\n streamingState,\n isStreaming: streamingState.status === \"streaming\",\n isWaiting: streamingState.status === \"waiting\",\n isIdle: streamingState.status === \"idle\",\n registerComponent: registry.registerComponent,\n registerTool: registry.registerTool,\n registerTools: registry.registerTools,\n componentList: registry.componentList,\n toolRegistry: registry.toolRegistry,\n currentThreadId: streamState.currentThreadId,\n initThread: threadManagement.initThread,\n switchThread: threadManagement.switchThread,\n startNewThread: threadManagement.startNewThread,\n dispatch,\n cancelRun,\n authState,\n isIdentified: authState.status === \"identified\",\n updateThreadName,\n };\n }, [\n cancelRun,\n updateThreadName,\n client,\n threadState,\n registry,\n streamState.currentThreadId,\n threadManagement,\n dispatch,\n authState,\n ]);\n}\n"]}
1
+ {"version":3,"file":"use-tambo-v1.js","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1.ts"],"names":[],"mappings":"A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BAAkB;aAC5B,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO;YACL,MAAM;YACN,MAAM;YACN,QAAQ;YACR,cAAc;YACd,WAAW,EAAE,cAAc,CAAC,MAAM,KAAK,WAAW;YAClD,SAAS,EAAE,cAAc,CAAC,MAAM,KAAK,SAAS;YAC9C,MAAM,EAAE,cAAc,CAAC,MAAM,KAAK,MAAM;YACxC,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;YAC7C,YAAY,EAAE,QAAQ,CAAC,YAAY;YACnC,aAAa,EAAE,QAAQ,CAAC,aAAa;YACrC,aAAa,EAAE,QAAQ,CAAC,aAAa;YACrC,YAAY,EAAE,QAAQ,CAAC,YAAY;YACnC,eAAe,EAAE,WAAW,CAAC,eAAe;YAC5C,UAAU,EAAE,gBAAgB,CAAC,UAAU;YACvC,YAAY,EAAE,gBAAgB,CAAC,YAAY;YAC3C,cAAc,EAAE,gBAAgB,CAAC,cAAc;YAC/C,QAAQ;YACR,SAAS;YACT,SAAS;YACT,YAAY,EAAE,SAAS,CAAC,MAAM,KAAK,YAAY;YAC/C,gBAAgB;SACjB,CAAC;IACJ,CAAC,EAAE;QACD,SAAS;QACT,gBAAgB;QAChB,MAAM;QACN,WAAW;QACX,QAAQ;QACR,WAAW,CAAC,eAAe;QAC3B,gBAAgB;QAChB,QAAQ;QACR,SAAS;KACV,CAAC,CAAC;AACL,CAAC","sourcesContent":["\"use client\";\n\n/**\n * useTambo - Main Hook\n *\n * Combines all contexts into a single hook for convenient access\n * to thread state, streaming status, registry, and client.\n */\n\nimport { EventType } from \"@ag-ui/core\";\nimport type TamboAI from \"@tambo-ai/typescript-sdk\";\nimport React, {\n useCallback,\n useContext,\n useMemo,\n useRef,\n type ReactElement,\n} from \"react\";\nimport {\n useTamboClient,\n useTamboQueryClient,\n} from \"../../providers/tambo-client-provider\";\nimport { useTamboConfig } from \"../providers/tambo-v1-provider\";\nimport { useTamboAuthState } from \"./use-tambo-v1-auth-state\";\nimport type { TamboAuthState } from \"../types/auth\";\nimport {\n TamboRegistryContext,\n type TamboRegistryContext as TamboRegistryContextType,\n} from \"../../providers/tambo-registry-provider\";\nimport { ComponentRenderer } from \"../components/v1-component-renderer\";\nimport {\n useStreamDispatch,\n useStreamState,\n useThreadManagement,\n type ThreadManagement,\n} from \"../providers/tambo-v1-stream-context\";\nimport type {\n Content,\n TamboToolDisplayProps,\n TamboThreadMessage,\n TamboToolUseContent,\n} from \"../types/message\";\nimport type { StreamingState } from \"../types/thread\";\nimport {\n isPlaceholderThreadId,\n type ThreadState,\n} from \"../utils/event-accumulator\";\n\n/**\n * Return type for useTambo hook\n */\nexport interface UseTamboReturn {\n /**\n * The Tambo API client instance\n */\n client: TamboAI;\n\n /**\n * Current thread state for the given threadId, or undefined if not loaded\n */\n thread: ThreadState | undefined;\n\n /**\n * Messages in the current thread\n */\n messages: TamboThreadMessage[];\n\n /**\n * Current streaming state\n */\n streamingState: StreamingState;\n\n /**\n * Whether the thread is currently streaming a response\n */\n isStreaming: boolean;\n\n /**\n * Whether the thread is waiting for the AI to start responding\n */\n isWaiting: boolean;\n\n /**\n * Whether the thread is idle (not streaming or waiting)\n */\n isIdle: boolean;\n\n /**\n * Register a component with the registry\n */\n registerComponent: TamboRegistryContextType[\"registerComponent\"];\n\n /**\n * Register a tool with the registry\n */\n registerTool: TamboRegistryContextType[\"registerTool\"];\n\n /**\n * Register multiple tools with the registry\n */\n registerTools: TamboRegistryContextType[\"registerTools\"];\n\n /**\n * The component registry (Map of name -> component definition)\n */\n componentList: TamboRegistryContextType[\"componentList\"];\n\n /**\n * The tool registry (Map of name -> tool definition)\n */\n toolRegistry: TamboRegistryContextType[\"toolRegistry\"];\n\n /**\n * Current thread ID (always available - uses \"placeholder\" for new threads)\n */\n currentThreadId: string;\n\n /**\n * Initialize a new thread in the stream context\n */\n initThread: ThreadManagement[\"initThread\"];\n\n /**\n * Switch the current active thread\n */\n switchThread: ThreadManagement[\"switchThread\"];\n\n /**\n * Start a new thread (generates a temporary ID)\n */\n startNewThread: ThreadManagement[\"startNewThread\"];\n\n /**\n * Dispatch function for stream events (advanced usage)\n */\n dispatch: ReturnType<typeof useStreamDispatch>;\n\n /**\n * Cancel the current run on this thread.\n * Optimistically updates local state and sends cancellation request to the API.\n * No-op if there's no active run or thread is a placeholder.\n */\n cancelRun: () => Promise<void>;\n\n /**\n * Current authentication state.\n * Use this to show auth-related UI or conditionally render features.\n */\n authState: TamboAuthState;\n\n /**\n * Shorthand for `authState.status === \"identified\"`.\n * When true, the SDK is ready to make API calls.\n */\n isIdentified: boolean;\n\n /**\n * Update a thread's name.\n * Useful for implementing manual thread renaming UI in history sidebars.\n * Cache invalidation is best-effort; failures will be logged and won't reject.\n * @param threadId - ID of the thread to rename\n * @param name - New name for the thread\n * @returns Promise that resolves when the update completes\n */\n updateThreadName: (threadId: string, name: string) => Promise<void>;\n}\n\n/**\n * Main hook for accessing Tambo functionality.\n *\n * Combines thread state, streaming status, registry, and client\n * into a single convenient hook.\n *\n * Messages returned include renderedComponent on component content blocks,\n * allowing direct rendering via {content.renderedComponent}.\n * @param threadId - Optional thread ID to get state for\n * @returns Combined context with thread state, messages, and utilities\n * @example\n * ```tsx\n * function ChatInterface() {\n * const {\n * thread,\n * messages,\n * isStreaming,\n * registerComponent,\n * } = useTambo('thread_123');\n *\n * return (\n * <div>\n * {messages.map(msg => <Message key={msg.id} message={msg} />)}\n * {isStreaming && <LoadingIndicator />}\n * </div>\n * );\n * }\n * ```\n */\n/**\n * Cache entry for a rendered component wrapper.\n * Stores the element and the props JSON used to create it.\n */\ninterface ComponentCacheEntry {\n element: ReactElement;\n propsJson: string;\n}\n\n/**\n *\n * @returns The combined Tambo context\n */\nexport function useTambo(): UseTamboReturn {\n const client = useTamboClient();\n const queryClient = useTamboQueryClient();\n const { userKey } = useTamboConfig();\n const streamState = useStreamState();\n const dispatch = useStreamDispatch();\n const registry = useContext(TamboRegistryContext);\n const threadManagement = useThreadManagement();\n const authState = useTamboAuthState();\n\n // Cache for rendered component wrappers - maintains stable element references\n // across renders when props haven't changed\n const componentCacheRef = useRef<Map<string, ComponentCacheEntry>>(new Map());\n\n // Get thread state for the current thread\n const threadState = streamState.threadMap[streamState.currentThreadId];\n\n // Keep a live snapshot of the threadMap for callbacks without forcing them to\n // re-create on every stream state update.\n const threadMapRef = useRef(streamState.threadMap);\n threadMapRef.current = streamState.threadMap;\n\n // Cancel the current run on this thread\n const cancelRun = useCallback(async () => {\n const runId = threadState?.streaming.runId;\n const threadId = streamState.currentThreadId;\n\n // No-op if there's no active run or thread is a placeholder\n if (!runId || isPlaceholderThreadId(threadId)) {\n return;\n }\n\n // Optimistically update local state with RUN_ERROR event\n dispatch({\n type: \"EVENT\",\n threadId,\n event: {\n type: EventType.RUN_ERROR,\n message: \"Run cancelled\",\n code: \"CANCELLED\",\n timestamp: Date.now(),\n },\n });\n\n // Call API to cancel the run\n try {\n await client.threads.runs.delete(runId, { threadId, userKey });\n } catch (error) {\n // Log but don't rethrow - local state is already updated\n console.warn(\"Failed to cancel run on server:\", error);\n }\n }, [\n client,\n userKey,\n streamState.currentThreadId,\n threadState?.streaming.runId,\n dispatch,\n ]);\n\n // Update a thread's name\n const updateThreadName = useCallback(\n async (threadId: string, name: string) => {\n await client.threads.update(threadId, { name, userKey });\n\n if (threadMapRef.current[threadId]) {\n dispatch({\n type: \"UPDATE_THREAD_NAME\",\n threadId,\n name: name,\n });\n }\n\n try {\n await Promise.all([\n queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", \"list\"],\n }),\n queryClient.invalidateQueries({\n queryKey: [\"v1-threads\", threadId],\n }),\n ]);\n } catch (error) {\n console.warn(\n \"[useTambo] Failed to invalidate thread queries after rename:\",\n error,\n );\n }\n },\n [client, userKey, dispatch, queryClient],\n );\n\n // Memoize the return object to prevent unnecessary re-renders\n return useMemo(() => {\n const thread = threadState;\n const rawMessages = thread?.thread.messages ?? [];\n const streamingState: StreamingState = thread?.streaming ?? {\n status: \"idle\" as const,\n };\n\n // Build a set of tool_use IDs that have completed (have a matching tool_result)\n // We need to look across all messages since tool_result might be in a different message\n const completedToolIds = new Set<string>();\n for (const msg of rawMessages) {\n for (const content of msg.content) {\n if (content.type === \"tool_result\") {\n completedToolIds.add(content.toolUseId);\n }\n }\n }\n\n // Transform messages to add computed properties to content blocks\n const messages = rawMessages.map((message): TamboThreadMessage => {\n const transformedContent = message.content.map((content): Content => {\n // Transform tool_use content to add computed state\n if (content.type === \"tool_use\") {\n const hasCompleted = completedToolIds.has(content.id);\n const input = content.input ?? {};\n\n // Extract Tambo display props from input\n const tamboDisplayProps: TamboToolDisplayProps = {\n _tambo_statusMessage:\n typeof input._tambo_statusMessage === \"string\"\n ? input._tambo_statusMessage\n : undefined,\n _tambo_completionStatusMessage:\n typeof input._tambo_completionStatusMessage === \"string\"\n ? input._tambo_completionStatusMessage\n : undefined,\n };\n\n // Compute status message based on completion state\n const statusMessage = hasCompleted\n ? (tamboDisplayProps._tambo_completionStatusMessage ??\n `Called ${content.name}`)\n : (tamboDisplayProps._tambo_statusMessage ??\n `Calling ${content.name}`);\n\n // Filter out _tambo_* properties from input - consumers only see actual tool params\n const cleanInput: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(input)) {\n if (!key.startsWith(\"_tambo_\")) {\n cleanInput[key] = value;\n }\n }\n\n const v1Content: TamboToolUseContent = {\n ...content,\n input: cleanInput,\n hasCompleted,\n statusMessage,\n tamboDisplayProps,\n };\n return v1Content;\n }\n\n if (content.type !== \"component\") {\n return content;\n }\n\n const componentContent = content;\n const propsJson = JSON.stringify(componentContent.props ?? {});\n const cache = componentCacheRef.current;\n const cached = cache.get(componentContent.id);\n\n // Return cached element if props haven't changed\n if (cached?.propsJson === propsJson) {\n return {\n ...componentContent,\n renderedComponent: cached.element,\n };\n }\n\n // Create new wrapper element\n const element = React.createElement(ComponentRenderer, {\n key: componentContent.id,\n content: componentContent,\n threadId: streamState.currentThreadId,\n messageId: message.id,\n });\n\n // Update cache\n cache.set(componentContent.id, { element, propsJson });\n\n return {\n ...componentContent,\n renderedComponent: element,\n };\n });\n\n return {\n ...message,\n content: transformedContent,\n };\n });\n\n return {\n client,\n thread,\n messages,\n streamingState,\n isStreaming: streamingState.status === \"streaming\",\n isWaiting: streamingState.status === \"waiting\",\n isIdle: streamingState.status === \"idle\",\n registerComponent: registry.registerComponent,\n registerTool: registry.registerTool,\n registerTools: registry.registerTools,\n componentList: registry.componentList,\n toolRegistry: registry.toolRegistry,\n currentThreadId: streamState.currentThreadId,\n initThread: threadManagement.initThread,\n switchThread: threadManagement.switchThread,\n startNewThread: threadManagement.startNewThread,\n dispatch,\n cancelRun,\n authState,\n isIdentified: authState.status === \"identified\",\n updateThreadName,\n };\n }, [\n cancelRun,\n updateThreadName,\n client,\n threadState,\n registry,\n streamState.currentThreadId,\n threadManagement,\n dispatch,\n authState,\n ]);\n}\n"]}
@@ -1049,6 +1049,7 @@ describe("useTambo", () => {
1049
1049
  });
1050
1050
  expect(mockUpdate).toHaveBeenCalledWith("thread_456", {
1051
1051
  name: "My New Thread",
1052
+ userKey: undefined,
1052
1053
  });
1053
1054
  expect(result.current.thread?.thread.name).toBe("My New Thread");
1054
1055
  const invalidatedKeys = invalidateQueriesSpy.mock.calls
@@ -1079,6 +1080,7 @@ describe("useTambo", () => {
1079
1080
  });
1080
1081
  expect(mockUpdate).toHaveBeenCalledWith("thread_789", {
1081
1082
  name: "New Title",
1083
+ userKey: undefined,
1082
1084
  });
1083
1085
  const invalidatedKeys = invalidateQueriesSpy.mock.calls
1084
1086
  .map(([arg]) => arg.queryKey)