@tambo-ai/react 0.33.0 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/hooks/__tests__/use-tambo-stream-status.test.d.ts +2 -0
  2. package/dist/hooks/__tests__/use-tambo-stream-status.test.d.ts.map +1 -0
  3. package/dist/hooks/__tests__/use-tambo-stream-status.test.js +335 -0
  4. package/dist/hooks/__tests__/use-tambo-stream-status.test.js.map +1 -0
  5. package/dist/hooks/index.d.ts +1 -0
  6. package/dist/hooks/index.d.ts.map +1 -1
  7. package/dist/hooks/index.js +3 -1
  8. package/dist/hooks/index.js.map +1 -1
  9. package/dist/hooks/use-tambo-stream-status.d.ts +89 -0
  10. package/dist/hooks/use-tambo-stream-status.d.ts.map +1 -0
  11. package/dist/hooks/use-tambo-stream-status.js +212 -0
  12. package/dist/hooks/use-tambo-stream-status.js.map +1 -0
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3 -1
  16. package/dist/index.js.map +1 -1
  17. package/esm/hooks/__tests__/use-tambo-stream-status.test.d.ts +2 -0
  18. package/esm/hooks/__tests__/use-tambo-stream-status.test.d.ts.map +1 -0
  19. package/esm/hooks/__tests__/use-tambo-stream-status.test.js +333 -0
  20. package/esm/hooks/__tests__/use-tambo-stream-status.test.js.map +1 -0
  21. package/esm/hooks/index.d.ts +1 -0
  22. package/esm/hooks/index.d.ts.map +1 -1
  23. package/esm/hooks/index.js +1 -0
  24. package/esm/hooks/index.js.map +1 -1
  25. package/esm/hooks/use-tambo-stream-status.d.ts +89 -0
  26. package/esm/hooks/use-tambo-stream-status.d.ts.map +1 -0
  27. package/esm/hooks/use-tambo-stream-status.js +209 -0
  28. package/esm/hooks/use-tambo-stream-status.js.map +1 -0
  29. package/esm/index.d.ts +1 -0
  30. package/esm/index.d.ts.map +1 -1
  31. package/esm/index.js +1 -0
  32. package/esm/index.js.map +1 -1
  33. package/package.json +11 -11
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ "use client";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useTamboStreamStatus = useTamboStreamStatus;
5
+ const react_1 = require("react");
6
+ const generate_component_response_1 = require("../model/generate-component-response");
7
+ const tambo_thread_provider_1 = require("../providers/tambo-thread-provider");
8
+ const use_current_message_1 = require("./use-current-message");
9
+ /**
10
+ * SSR Guard - throws during server-side rendering.
11
+ * Ensures the hook is only used in browser contexts.
12
+ * @throws {Error} When called during server-side rendering
13
+ */
14
+ function assertClientSide() {
15
+ if (typeof window === "undefined") {
16
+ throw new Error("useTamboStreamStatus can only be used in browser contexts. " +
17
+ "This hook is not compatible with SSR/SSG. " +
18
+ "Consider wrapping it in useEffect or using dynamic imports.");
19
+ }
20
+ }
21
+ /**
22
+ * Track streaming status for individual props by monitoring their values.
23
+ * Monitors when props receive their first token and when they complete streaming.
24
+ * Maintains stable state per message - once props complete for a message, they stay complete.
25
+ * @template Props - The type of the component props being tracked
26
+ * @param props - The current component props object
27
+ * @param generationStage - The current generation stage from the LLM
28
+ * @param messageId - The ID of the current message to track component-specific state
29
+ * @returns A record mapping each prop key to its PropStatus
30
+ */
31
+ function usePropsStreamingStatus(props, generationStage, messageId) {
32
+ const [propTracking, setPropTracking] = (0, react_1.useState)({});
33
+ /** Reset tracking only when the message changes */
34
+ (0, react_1.useEffect)(() => {
35
+ setPropTracking((prev) => {
36
+ // If we have tracking data for a different message, reset
37
+ const hasOldMessageData = Object.values(prev).some((track) => track.messageId && track.messageId !== messageId);
38
+ return hasOldMessageData ? {} : prev;
39
+ });
40
+ }, [messageId]);
41
+ /** Track when props start streaming (receive first token) and when they complete */
42
+ (0, react_1.useEffect)(() => {
43
+ if (!props)
44
+ return;
45
+ setPropTracking((prev) => {
46
+ const updated = { ...prev };
47
+ let hasChanges = false;
48
+ // First pass: identify which props are starting now
49
+ const propsStartingNow = [];
50
+ Object.entries(props).forEach(([key, value]) => {
51
+ const current = prev[key] || {
52
+ hasStarted: false,
53
+ isComplete: false,
54
+ };
55
+ /** A prop starts streaming when it has a non-empty value for the first time */
56
+ const hasContent = value !== undefined && value !== null && value !== "";
57
+ const justStarted = hasContent && !current.hasStarted;
58
+ if (justStarted) {
59
+ propsStartingNow.push(key);
60
+ }
61
+ });
62
+ // Second pass: update tracking and mark previous props as complete
63
+ Object.entries(props).forEach(([key, value]) => {
64
+ const current = prev[key] || {
65
+ hasStarted: false,
66
+ isComplete: false,
67
+ };
68
+ /** A prop starts streaming when it has a non-empty value for the first time */
69
+ const hasContent = value !== undefined && value !== null && value !== "";
70
+ const justStarted = hasContent && !current.hasStarted;
71
+ /**
72
+ * A prop is complete when it has started and either:
73
+ * 1. A following prop has started, OR
74
+ * 2. Generation is complete (for the final prop)
75
+ */
76
+ const hasFollowingPropStarted = propsStartingNow.some((startingKey) => startingKey !== key);
77
+ const isGenerationComplete = generationStage === generate_component_response_1.GenerationStage.COMPLETE;
78
+ const isComplete = current.hasStarted &&
79
+ (hasFollowingPropStarted || isGenerationComplete) &&
80
+ !current.isComplete;
81
+ // Once a prop is complete for this message, it stays complete
82
+ if (current.isComplete && current.messageId === messageId) {
83
+ // Skip - already complete for this message
84
+ return;
85
+ }
86
+ if (justStarted || isComplete) {
87
+ updated[key] = {
88
+ ...current,
89
+ hasStarted: justStarted ? true : current.hasStarted,
90
+ isComplete: isComplete ? true : current.isComplete,
91
+ messageId,
92
+ };
93
+ hasChanges = true;
94
+ }
95
+ });
96
+ return hasChanges ? updated : prev;
97
+ });
98
+ }, [props, generationStage, messageId]);
99
+ /** Convert tracking state to PropStatus objects */
100
+ return (0, react_1.useMemo)(() => {
101
+ if (!props)
102
+ return {};
103
+ const result = {};
104
+ Object.keys(props).forEach((key) => {
105
+ const tracking = propTracking[key] || {
106
+ hasStarted: false,
107
+ isComplete: false,
108
+ messageId: "",
109
+ };
110
+ // If this prop is complete for this message, it stays complete
111
+ const isCompleteForThisMessage = tracking.isComplete && tracking.messageId === messageId;
112
+ // Only consider generation stage if this prop isn't already complete for this message
113
+ const isGenerationStreaming = !isCompleteForThisMessage &&
114
+ generationStage === generate_component_response_1.GenerationStage.STREAMING_RESPONSE;
115
+ result[key] = {
116
+ isPending: !tracking.hasStarted && !isCompleteForThisMessage,
117
+ isStreaming: tracking.hasStarted &&
118
+ !isCompleteForThisMessage &&
119
+ isGenerationStreaming,
120
+ isSuccess: isCompleteForThisMessage,
121
+ error: tracking.error,
122
+ };
123
+ });
124
+ return result;
125
+ }, [props, propTracking, generationStage, messageId]);
126
+ }
127
+ /**
128
+ * Derives global StreamStatus from generation stage and individual prop statuses.
129
+ * Aggregates individual prop states into a unified stream status.
130
+ * @template Props - The type of the component props
131
+ * @param generationStage - The current generation stage from the LLM
132
+ * @param propStatus - Status record for each individual prop
133
+ * @param hasComponent - Whether a component exists in the current message
134
+ * @param generationError - Any error from the generation process itself
135
+ * @returns The aggregated StreamStatus for the entire component
136
+ */
137
+ function deriveGlobalStreamStatus(generationStage, propStatus, hasComponent, generationError) {
138
+ const propStatuses = Object.values(propStatus);
139
+ // If all props are already successful, the component is complete regardless of generation stage
140
+ const allPropsSuccessful = propStatuses.length > 0 && propStatuses.every((p) => p.isSuccess);
141
+ // Only consider generation stage if not all props are complete
142
+ const isGenerationStreaming = !allPropsSuccessful &&
143
+ generationStage === generate_component_response_1.GenerationStage.STREAMING_RESPONSE;
144
+ const isGenerationError = generationStage === generate_component_response_1.GenerationStage.ERROR;
145
+ /** Find first error from generation or any prop */
146
+ const firstError = generationError ?? propStatuses.find((p) => p.error)?.error;
147
+ return {
148
+ /** isPending: no component yet OR (has component but no props have started) */
149
+ isPending: !hasComponent ||
150
+ (!isGenerationStreaming &&
151
+ !allPropsSuccessful &&
152
+ propStatuses.every((p) => p.isPending)),
153
+ /** isStreaming: any prop is streaming (generation stage doesn't matter if props are complete) */
154
+ isStreaming: propStatuses.some((p) => p.isStreaming),
155
+ /** isSuccess: all props successful (component is stable once all props complete) */
156
+ isSuccess: allPropsSuccessful,
157
+ /** isError: generation error OR any prop error */
158
+ isError: isGenerationError ||
159
+ propStatuses.some((p) => p.error) ||
160
+ !!generationError,
161
+ streamError: firstError,
162
+ };
163
+ }
164
+ /**
165
+ * Hook that exposes per-prop and global streaming status for tambo-ai components.
166
+ * Provides streaming readiness flags so consumers can show loaders, skeletons,
167
+ * or errors while LLM-generated props stream in.
168
+ * This hook tracks status for the specific component in the current message only.
169
+ * Once a component's props complete streaming, they remain stable regardless of
170
+ * other components being generated in the thread.
171
+ * @template Props - The type of the component props being tracked (defaults to Record<string, any>)
172
+ * @returns An object containing both global streamStatus and per-prop propStatus
173
+ * @throws {Error} When used during SSR/SSG
174
+ * @example
175
+ * ```tsx
176
+ * // Wait for entire stream
177
+ * const { streamStatus } = useTamboStreamStatus();
178
+ * if (!streamStatus.isSuccess) return <Spinner />;
179
+ * return <Card {...props} />;
180
+ * ```
181
+ * @example
182
+ * ```tsx
183
+ * // Highlight in-flight props
184
+ * const { propStatus } = useTamboStreamStatus<Props>();
185
+ * <h2 className={propStatus.title.isStreaming ? "animate-pulse" : ""}>
186
+ * {title}
187
+ * </h2>
188
+ * ```
189
+ */
190
+ function useTamboStreamStatus() {
191
+ /** SSR Guard - ensure client-side only execution */
192
+ assertClientSide();
193
+ const { generationStage } = (0, tambo_thread_provider_1.useTamboThread)();
194
+ const message = (0, use_current_message_1.useTamboCurrentMessage)();
195
+ /** Get the current component props from the message */
196
+ const componentProps = message?.component?.props || {};
197
+ /** Track per-prop streaming status */
198
+ const propStatus = usePropsStreamingStatus(componentProps, generationStage, message?.id ?? "");
199
+ /** Derive global stream status from prop statuses and generation stage */
200
+ const streamStatus = (0, react_1.useMemo)(() => {
201
+ const generationError = message?.error
202
+ ? new Error(message.error)
203
+ : undefined;
204
+ const hasComponent = !!message?.component;
205
+ return deriveGlobalStreamStatus(generationStage, propStatus, hasComponent, generationError);
206
+ }, [generationStage, propStatus, message?.error, message?.component]);
207
+ return {
208
+ streamStatus,
209
+ propStatus,
210
+ };
211
+ }
212
+ //# sourceMappingURL=use-tambo-stream-status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-tambo-stream-status.js","sourceRoot":"","sources":["../../src/hooks/use-tambo-stream-status.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;AA+Tb,oDAwCC;AAtWD,iCAAqD;AACrD,sFAAuE;AACvE,8EAAoE;AACpE,+DAA+D;AAqE/D;;;;GAIG;AACH,SAAS,gBAAgB;IACvB,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CACb,6DAA6D;YAC3D,4CAA4C;YAC5C,6DAA6D,CAChE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,uBAAuB,CAC9B,KAAwB,EACxB,eAAgC,EAChC,SAAiB;IAEjB,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAU9C,EAAE,CAAC,CAAC;IAEN,mDAAmD;IACnD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,eAAe,CAAC,CAAC,IAAI,EAAE,EAAE;YACvB,0DAA0D;YAC1D,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAChD,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,CAC5D,CAAC;YACF,OAAO,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;QACvC,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,oFAAoF;IACpF,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,CAAC,KAAK;YAAE,OAAO;QAEnB,eAAe,CAAC,CAAC,IAAI,EAAE,EAAE;YACvB,MAAM,OAAO,GAAG,EAAE,GAAG,IAAI,EAAE,CA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,EACf,UAAU,EACV,YAAY,EACZ,eAAe,CAChB,CAAC;IACJ,CAAC,EAAE,CAAC,eAAe,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC;IAEtE,OAAO;QACL,YAAY;QACZ,UAAU;KACX,CAAC;AACJ,CAAC","sourcesContent":["\"use client\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { GenerationStage } from \"../model/generate-component-response\";\nimport { useTamboThread } from \"../providers/tambo-thread-provider\";\nimport { useTamboCurrentMessage } from \"./use-current-message\";\n\n/**\n * Global stream status flags for a specific component in a message.\n * Represents the aggregate state across all props for this component only.\n * Once a component completes, its status remains stable regardless of other generations.\n */\nexport interface StreamStatus {\n /**\n * Indicates no tokens have been received for any prop and generation is not active.\n * Useful for showing initial loading states before any data arrives.\n */\n isPending: boolean;\n\n /**\n * Indicates active streaming - either generation is streaming OR at least one prop is still streaming.\n * Use this to show loading animations or skeleton states during data transmission.\n */\n isStreaming: boolean;\n\n /**\n * Indicates successful completion - generation is complete AND every prop finished without error.\n * Safe to render the final component when this is true.\n */\n isSuccess: boolean;\n\n /**\n * Indicates a fatal error occurred in any prop or the stream itself.\n * Check streamError for details about what went wrong.\n */\n isError: boolean;\n\n /**\n * The first fatal error encountered during streaming (if any).\n * Will be undefined if no errors occurred.\n */\n streamError?: Error;\n}\n\n/**\n * Streaming status flags for individual component props.\n * Tracks the state of each prop as it streams from the LLM.\n */\nexport interface PropStatus {\n /**\n * Indicates no tokens have been received for this specific prop yet.\n * The prop value is still undefined, null, or empty string.\n */\n isPending: boolean;\n\n /**\n * Indicates at least one token has been received but streaming is not complete.\n * The prop has partial content that may still be updating.\n */\n isStreaming: boolean;\n\n /**\n * Indicates this prop has finished streaming successfully.\n * The prop value is complete and stable.\n */\n isSuccess: boolean;\n\n /**\n * The error that occurred during streaming (if any).\n * Will be undefined if no error occurred for this prop.\n */\n error?: Error;\n}\n\n/**\n * SSR Guard - throws during server-side rendering.\n * Ensures the hook is only used in browser contexts.\n * @throws {Error} When called during server-side rendering\n */\nfunction assertClientSide() {\n if (typeof window === \"undefined\") {\n throw new Error(\n \"useTamboStreamStatus can only be used in browser contexts. \" +\n \"This hook is not compatible with SSR/SSG. \" +\n \"Consider wrapping it in useEffect or using dynamic imports.\",\n );\n }\n}\n\n/**\n * Track streaming status for individual props by monitoring their values.\n * Monitors when props receive their first token and when they complete streaming.\n * Maintains stable state per message - once props complete for a message, they stay complete.\n * @template Props - The type of the component props being tracked\n * @param props - The current component props object\n * @param generationStage - The current generation stage from the LLM\n * @param messageId - The ID of the current message to track component-specific state\n * @returns A record mapping each prop key to its PropStatus\n */\nfunction usePropsStreamingStatus<Props extends Record<string, any>>(\n props: Props | undefined,\n generationStage: GenerationStage,\n messageId: string,\n): Record<keyof Props, PropStatus> {\n const [propTracking, setPropTracking] = useState<\n Record<\n string,\n {\n hasStarted: boolean;\n isComplete: boolean;\n error?: Error;\n messageId: string;\n }\n >\n >({});\n\n /** Reset tracking only when the message changes */\n useEffect(() => {\n setPropTracking((prev) => {\n // If we have tracking data for a different message, reset\n const hasOldMessageData = Object.values(prev).some(\n (track) => track.messageId && track.messageId !== messageId,\n );\n return hasOldMessageData ? {} : prev;\n });\n }, [messageId]);\n\n /** Track when props start streaming (receive first token) and when they complete */\n useEffect(() => {\n if (!props) return;\n\n setPropTracking((prev) => {\n const updated = { ...prev };\n let hasChanges = false;\n\n // First pass: identify which props are starting now\n const propsStartingNow: string[] = [];\n Object.entries(props).forEach(([key, value]) => {\n const current = prev[key] || {\n hasStarted: false,\n isComplete: false,\n };\n\n /** A prop starts streaming when it has a non-empty value for the first time */\n const hasContent =\n value !== undefined && value !== null && value !== \"\";\n const justStarted = hasContent && !current.hasStarted;\n\n if (justStarted) {\n propsStartingNow.push(key);\n }\n });\n\n // Second pass: update tracking and mark previous props as complete\n Object.entries(props).forEach(([key, value]) => {\n const current = prev[key] || {\n hasStarted: false,\n isComplete: false,\n };\n\n /** A prop starts streaming when it has a non-empty value for the first time */\n const hasContent =\n value !== undefined && value !== null && value !== \"\";\n const justStarted = hasContent && !current.hasStarted;\n\n /**\n * A prop is complete when it has started and either:\n * 1. A following prop has started, OR\n * 2. Generation is complete (for the final prop)\n */\n const hasFollowingPropStarted = propsStartingNow.some(\n (startingKey) => startingKey !== key,\n );\n const isGenerationComplete =\n generationStage === GenerationStage.COMPLETE;\n const isComplete =\n current.hasStarted &&\n (hasFollowingPropStarted || isGenerationComplete) &&\n !current.isComplete;\n\n // Once a prop is complete for this message, it stays complete\n if (current.isComplete && current.messageId === messageId) {\n // Skip - already complete for this message\n return;\n }\n\n if (justStarted || isComplete) {\n updated[key] = {\n ...current,\n hasStarted: justStarted ? true : current.hasStarted,\n isComplete: isComplete ? true : current.isComplete,\n messageId,\n };\n hasChanges = true;\n }\n });\n\n return hasChanges ? updated : prev;\n });\n }, [props, generationStage, messageId]);\n\n /** Convert tracking state to PropStatus objects */\n return useMemo(() => {\n if (!props) return {} as Record<keyof Props, PropStatus>;\n\n const result = {} as Record<keyof Props, PropStatus>;\n\n Object.keys(props).forEach((key) => {\n const tracking = propTracking[key] || {\n hasStarted: false,\n isComplete: false,\n messageId: \"\",\n };\n\n // If this prop is complete for this message, it stays complete\n const isCompleteForThisMessage =\n tracking.isComplete && tracking.messageId === messageId;\n\n // Only consider generation stage if this prop isn't already complete for this message\n const isGenerationStreaming =\n !isCompleteForThisMessage &&\n generationStage === GenerationStage.STREAMING_RESPONSE;\n\n result[key as keyof Props] = {\n isPending: !tracking.hasStarted && !isCompleteForThisMessage,\n isStreaming:\n tracking.hasStarted &&\n !isCompleteForThisMessage &&\n isGenerationStreaming,\n isSuccess: isCompleteForThisMessage,\n error: tracking.error,\n };\n });\n\n return result;\n }, [props, propTracking, generationStage, messageId]);\n}\n\n/**\n * Derives global StreamStatus from generation stage and individual prop statuses.\n * Aggregates individual prop states into a unified stream status.\n * @template Props - The type of the component props\n * @param generationStage - The current generation stage from the LLM\n * @param propStatus - Status record for each individual prop\n * @param hasComponent - Whether a component exists in the current message\n * @param generationError - Any error from the generation process itself\n * @returns The aggregated StreamStatus for the entire component\n */\nfunction deriveGlobalStreamStatus<Props extends Record<string, any>>(\n generationStage: GenerationStage,\n propStatus: Record<keyof Props, PropStatus>,\n hasComponent: boolean,\n generationError?: Error,\n): StreamStatus {\n const propStatuses = Object.values(propStatus) as PropStatus[];\n\n // If all props are already successful, the component is complete regardless of generation stage\n const allPropsSuccessful =\n propStatuses.length > 0 && propStatuses.every((p) => p.isSuccess);\n\n // Only consider generation stage if not all props are complete\n const isGenerationStreaming =\n !allPropsSuccessful &&\n generationStage === GenerationStage.STREAMING_RESPONSE;\n const isGenerationError = generationStage === GenerationStage.ERROR;\n\n /** Find first error from generation or any prop */\n const firstError =\n generationError ?? propStatuses.find((p) => p.error)?.error;\n\n return {\n /** isPending: no component yet OR (has component but no props have started) */\n isPending:\n !hasComponent ||\n (!isGenerationStreaming &&\n !allPropsSuccessful &&\n propStatuses.every((p) => p.isPending)),\n\n /** isStreaming: any prop is streaming (generation stage doesn't matter if props are complete) */\n isStreaming: propStatuses.some((p) => p.isStreaming),\n\n /** isSuccess: all props successful (component is stable once all props complete) */\n isSuccess: allPropsSuccessful,\n\n /** isError: generation error OR any prop error */\n isError:\n isGenerationError ||\n propStatuses.some((p) => p.error) ||\n !!generationError,\n\n streamError: firstError,\n };\n}\n\n/**\n * Hook that exposes per-prop and global streaming status for tambo-ai components.\n * Provides streaming readiness flags so consumers can show loaders, skeletons,\n * or errors while LLM-generated props stream in.\n * This hook tracks status for the specific component in the current message only.\n * Once a component's props complete streaming, they remain stable regardless of\n * other components being generated in the thread.\n * @template Props - The type of the component props being tracked (defaults to Record<string, any>)\n * @returns An object containing both global streamStatus and per-prop propStatus\n * @throws {Error} When used during SSR/SSG\n * @example\n * ```tsx\n * // Wait for entire stream\n * const { streamStatus } = useTamboStreamStatus();\n * if (!streamStatus.isSuccess) return <Spinner />;\n * return <Card {...props} />;\n * ```\n * @example\n * ```tsx\n * // Highlight in-flight props\n * const { propStatus } = useTamboStreamStatus<Props>();\n * <h2 className={propStatus.title.isStreaming ? \"animate-pulse\" : \"\"}>\n * {title}\n * </h2>\n * ```\n */\nexport function useTamboStreamStatus<\n Props extends Record<string, any> = Record<string, any>,\n>(): {\n streamStatus: StreamStatus;\n propStatus: Record<keyof Props, PropStatus>;\n} {\n /** SSR Guard - ensure client-side only execution */\n assertClientSide();\n\n const { generationStage } = useTamboThread();\n const message = useTamboCurrentMessage();\n\n /** Get the current component props from the message */\n const componentProps = (message?.component?.props as Props) || ({} as Props);\n\n /** Track per-prop streaming status */\n const propStatus = usePropsStreamingStatus(\n componentProps,\n generationStage,\n message?.id ?? \"\",\n );\n\n /** Derive global stream status from prop statuses and generation stage */\n const streamStatus = useMemo(() => {\n const generationError = message?.error\n ? new Error(message.error)\n : undefined;\n const hasComponent = !!message?.component;\n return deriveGlobalStreamStatus(\n generationStage,\n propStatus,\n hasComponent,\n generationError,\n );\n }, [generationStage, propStatus, message?.error, message?.component]);\n\n return {\n streamStatus,\n propStatus,\n };\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  export { useTamboComponentState } from "./hooks/use-component-state";
3
3
  export { TamboMessageProvider, useTamboCurrentMessage, useTamboMessageContext, } from "./hooks/use-current-message";
4
4
  export { useTamboStreamingProps } from "./hooks/use-streaming-props";
5
+ export { useTamboStreamStatus, type StreamStatus, type PropStatus, } from "./hooks/use-tambo-stream-status";
5
6
  export * from "./hooks/use-suggestions";
6
7
  export { useTamboThreadInput } from "./hooks/use-thread-input";
7
8
  export { TamboClientProvider, TamboComponentProvider, TamboProvider, TamboStubProvider, TamboThreadProvider, useTambo, useTamboClient, useTamboThread, type TamboComponent, type TamboRegistryContext, type TamboStubProviderProps, } from "./providers";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wKAAwK;AACxK,OAAO,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AAK,YAAY,EACjB,KAAK,UAAU,GAChB,MAAM,iCAAiC,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAG/D,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,QAAQ,EACR,cAAc,EACd,cAAc,EACd,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,KAAK,sBAAsB,GAC5B,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,QAAQ,EACR,cAAc,EACd,YAAY,GACb,MAAM,0BAA0B,CAAC;AAClC,YAAY,EACV,UAAU,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,6DAA6D,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EACL,KAAK,4BAA4B,EACjC,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,SAAS,GACf,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,eAAe,EACf,KAAK,kBAAkB,GACxB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.GenerationStage = exports.useTamboThreadList = exports.useTamboThread = exports.useTamboClient = exports.useTambo = exports.TamboThreadProvider = exports.TamboStubProvider = exports.TamboProvider = exports.TamboComponentProvider = exports.TamboClientProvider = exports.useTamboThreadInput = exports.useTamboStreamingProps = exports.useTamboMessageContext = exports.useTamboCurrentMessage = exports.TamboMessageProvider = exports.useTamboComponentState = void 0;
17
+ exports.GenerationStage = exports.useTamboThreadList = exports.useTamboThread = exports.useTamboClient = exports.useTambo = exports.TamboThreadProvider = exports.TamboStubProvider = exports.TamboProvider = exports.TamboComponentProvider = exports.TamboClientProvider = exports.useTamboThreadInput = exports.useTamboStreamStatus = exports.useTamboStreamingProps = exports.useTamboMessageContext = exports.useTamboCurrentMessage = exports.TamboMessageProvider = exports.useTamboComponentState = void 0;
18
18
  /** Exports for the library. Only publically available exports are re-exported here. Anything not exported here is not supported and may change or break at any time. */
19
19
  var use_component_state_1 = require("./hooks/use-component-state");
20
20
  Object.defineProperty(exports, "useTamboComponentState", { enumerable: true, get: function () { return use_component_state_1.useTamboComponentState; } });
@@ -24,6 +24,8 @@ Object.defineProperty(exports, "useTamboCurrentMessage", { enumerable: true, get
24
24
  Object.defineProperty(exports, "useTamboMessageContext", { enumerable: true, get: function () { return use_current_message_1.useTamboMessageContext; } });
25
25
  var use_streaming_props_1 = require("./hooks/use-streaming-props");
26
26
  Object.defineProperty(exports, "useTamboStreamingProps", { enumerable: true, get: function () { return use_streaming_props_1.useTamboStreamingProps; } });
27
+ var use_tambo_stream_status_1 = require("./hooks/use-tambo-stream-status");
28
+ Object.defineProperty(exports, "useTamboStreamStatus", { enumerable: true, get: function () { return use_tambo_stream_status_1.useTamboStreamStatus; } });
27
29
  __exportStar(require("./hooks/use-suggestions"), exports);
28
30
  var use_thread_input_1 = require("./hooks/use-thread-input");
29
31
  Object.defineProperty(exports, "useTamboThreadInput", { enumerable: true, get: function () { return use_thread_input_1.useTamboThreadInput; } });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,wKAAwK;AACxK,mEAAqE;AAA5D,6HAAA,sBAAsB,OAAA;AAC/B,mEAIqC;AAHnC,2HAAA,oBAAoB,OAAA;AACpB,6HAAA,sBAAsB,OAAA;AACtB,6HAAA,sBAAsB,OAAA;AAExB,mEAAqE;AAA5D,6HAAA,sBAAsB,OAAA;AAC/B,0DAAwC;AACxC,6DAA+D;AAAtD,uHAAA,mBAAmB,OAAA;AAE5B,gCAAgC;AAChC,yCAYqB;AAXnB,gHAAA,mBAAmB,OAAA;AACnB,mHAAA,sBAAsB,OAAA;AACtB,0GAAA,aAAa,OAAA;AACb,8GAAA,iBAAiB,OAAA;AACjB,gHAAA,mBAAmB,OAAA;AACnB,qGAAA,QAAQ,OAAA;AACR,2GAAA,cAAc,OAAA;AACd,2GAAA,cAAc,OAAA;AAkBhB,+DAA+D;AAAtD,uHAAA,kBAAkB,OAAA;AAQ3B,mFAG6C;AAF3C,8HAAA,eAAe,OAAA","sourcesContent":["/** Exports for the library. Only publically available exports are re-exported here. Anything not exported here is not supported and may change or break at any time. */\nexport { useTamboComponentState } from \"./hooks/use-component-state\";\nexport {\n TamboMessageProvider,\n useTamboCurrentMessage,\n useTamboMessageContext,\n} from \"./hooks/use-current-message\";\nexport { useTamboStreamingProps } from \"./hooks/use-streaming-props\";\nexport * from \"./hooks/use-suggestions\";\nexport { useTamboThreadInput } from \"./hooks/use-thread-input\";\n\n// Re-export provider components\nexport {\n TamboClientProvider,\n TamboComponentProvider,\n TamboProvider,\n TamboStubProvider,\n TamboThreadProvider,\n useTambo,\n useTamboClient,\n useTamboThread,\n type TamboComponent,\n type TamboRegistryContext,\n type TamboStubProviderProps,\n} from \"./providers\";\n\n// Re-export types from Tambo Node SDK\nexport type {\n APIError,\n RateLimitError,\n TamboAIError,\n} from \"@tambo-ai/typescript-sdk\";\nexport type {\n Suggestion,\n SuggestionGenerateParams,\n SuggestionGenerateResponse,\n SuggestionListResponse,\n} from \"@tambo-ai/typescript-sdk/resources/beta/threads/suggestions\";\nexport { useTamboThreadList } from \"./hooks/use-tambo-threads\";\nexport {\n type ComponentContextToolMetadata,\n type ComponentRegistry,\n type ParameterSpec,\n type RegisteredComponent,\n type TamboTool,\n} from \"./model/component-metadata\";\nexport {\n GenerationStage,\n type TamboThreadMessage,\n} from \"./model/generate-component-response\";\nexport { type TamboThread } from \"./model/tambo-thread\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,wKAAwK;AACxK,mEAAqE;AAA5D,6HAAA,sBAAsB,OAAA;AAC/B,mEAIqC;AAHnC,2HAAA,oBAAoB,OAAA;AACpB,6HAAA,sBAAsB,OAAA;AACtB,6HAAA,sBAAsB,OAAA;AAExB,mEAAqE;AAA5D,6HAAA,sBAAsB,OAAA;AAC/B,2EAIyC;AAHvC,+HAAA,oBAAoB,OAAA;AAItB,0DAAwC;AACxC,6DAA+D;AAAtD,uHAAA,mBAAmB,OAAA;AAE5B,gCAAgC;AAChC,yCAYqB;AAXnB,gHAAA,mBAAmB,OAAA;AACnB,mHAAA,sBAAsB,OAAA;AACtB,0GAAA,aAAa,OAAA;AACb,8GAAA,iBAAiB,OAAA;AACjB,gHAAA,mBAAmB,OAAA;AACnB,qGAAA,QAAQ,OAAA;AACR,2GAAA,cAAc,OAAA;AACd,2GAAA,cAAc,OAAA;AAkBhB,+DAA+D;AAAtD,uHAAA,kBAAkB,OAAA;AAQ3B,mFAG6C;AAF3C,8HAAA,eAAe,OAAA","sourcesContent":["/** Exports for the library. Only publically available exports are re-exported here. Anything not exported here is not supported and may change or break at any time. */\nexport { useTamboComponentState } from \"./hooks/use-component-state\";\nexport {\n TamboMessageProvider,\n useTamboCurrentMessage,\n useTamboMessageContext,\n} from \"./hooks/use-current-message\";\nexport { useTamboStreamingProps } from \"./hooks/use-streaming-props\";\nexport {\n useTamboStreamStatus,\n type StreamStatus,\n type PropStatus,\n} from \"./hooks/use-tambo-stream-status\";\nexport * from \"./hooks/use-suggestions\";\nexport { useTamboThreadInput } from \"./hooks/use-thread-input\";\n\n// Re-export provider components\nexport {\n TamboClientProvider,\n TamboComponentProvider,\n TamboProvider,\n TamboStubProvider,\n TamboThreadProvider,\n useTambo,\n useTamboClient,\n useTamboThread,\n type TamboComponent,\n type TamboRegistryContext,\n type TamboStubProviderProps,\n} from \"./providers\";\n\n// Re-export types from Tambo Node SDK\nexport type {\n APIError,\n RateLimitError,\n TamboAIError,\n} from \"@tambo-ai/typescript-sdk\";\nexport type {\n Suggestion,\n SuggestionGenerateParams,\n SuggestionGenerateResponse,\n SuggestionListResponse,\n} from \"@tambo-ai/typescript-sdk/resources/beta/threads/suggestions\";\nexport { useTamboThreadList } from \"./hooks/use-tambo-threads\";\nexport {\n type ComponentContextToolMetadata,\n type ComponentRegistry,\n type ParameterSpec,\n type RegisteredComponent,\n type TamboTool,\n} from \"./model/component-metadata\";\nexport {\n GenerationStage,\n type TamboThreadMessage,\n} from \"./model/generate-component-response\";\nexport { type TamboThread } from \"./model/tambo-thread\";\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=use-tambo-stream-status.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-tambo-stream-status.test.d.ts","sourceRoot":"","sources":["../../../src/hooks/__tests__/use-tambo-stream-status.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,333 @@
1
+ import { renderHook } from "@testing-library/react";
2
+ import { GenerationStage, } from "../../model/generate-component-response";
3
+ import { useTamboThread, } from "../../providers/tambo-thread-provider";
4
+ import { useTamboCurrentMessage } from "../use-current-message";
5
+ import { useTamboStreamStatus } from "../use-tambo-stream-status";
6
+ // Mock the required providers
7
+ jest.mock("../../providers/tambo-thread-provider", () => ({
8
+ useTamboThread: jest.fn(),
9
+ }));
10
+ jest.mock("../use-current-message", () => ({
11
+ useTamboCurrentMessage: jest.fn(),
12
+ }));
13
+ // Mock window for SSR tests
14
+ const originalWindow = global.window;
15
+ // Helper function to create mock ComponentDecisionV2
16
+ const createMockComponent = (props = {}) => ({
17
+ componentName: "TestComponent",
18
+ componentState: {},
19
+ message: "Component generated",
20
+ props,
21
+ reasoning: "Test reasoning",
22
+ });
23
+ // Helper function to create mock TamboThreadMessage
24
+ const createMockMessage = (overrides = {}) => ({
25
+ id: "test-message",
26
+ componentState: {},
27
+ content: [{ type: "text", text: "test content" }],
28
+ createdAt: new Date().toISOString(),
29
+ role: "assistant",
30
+ threadId: "test-thread",
31
+ ...overrides,
32
+ });
33
+ describe("useTamboStreamStatus", () => {
34
+ beforeEach(() => {
35
+ // Restore window for client-side tests
36
+ global.window = originalWindow;
37
+ // Default mock implementations
38
+ jest.mocked(useTamboThread).mockReturnValue({
39
+ generationStage: GenerationStage.IDLE,
40
+ });
41
+ jest.mocked(useTamboCurrentMessage).mockReturnValue({
42
+ id: "test-message",
43
+ component: {
44
+ props: {},
45
+ },
46
+ });
47
+ });
48
+ afterEach(() => {
49
+ jest.clearAllMocks();
50
+ });
51
+ describe("Initial State", () => {
52
+ it("should start with all flags as pending when idle and no props", () => {
53
+ jest.mocked(useTamboThread).mockReturnValue({
54
+ generationStage: GenerationStage.IDLE,
55
+ });
56
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
57
+ component: createMockComponent({ title: "", body: "" }),
58
+ }));
59
+ const { result } = renderHook(() => useTamboStreamStatus());
60
+ expect(result.current.streamStatus).toEqual({
61
+ isPending: true,
62
+ isStreaming: false,
63
+ isSuccess: false,
64
+ isError: false,
65
+ streamError: undefined,
66
+ });
67
+ expect(result.current.propStatus.title).toEqual({
68
+ isPending: true,
69
+ isStreaming: false,
70
+ isSuccess: false,
71
+ error: undefined,
72
+ });
73
+ expect(result.current.propStatus.body).toEqual({
74
+ isPending: true,
75
+ isStreaming: false,
76
+ isSuccess: false,
77
+ error: undefined,
78
+ });
79
+ });
80
+ });
81
+ describe("Generation vs Props Streaming", () => {
82
+ it("should show generation streaming but props still pending when STREAMING_RESPONSE with no prop content", () => {
83
+ jest.mocked(useTamboThread).mockReturnValue({
84
+ generationStage: GenerationStage.STREAMING_RESPONSE,
85
+ });
86
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
87
+ component: createMockComponent({ title: "", body: "" }),
88
+ }));
89
+ const { result } = renderHook(() => useTamboStreamStatus());
90
+ // Global streaming should be false because no props are actually streaming yet
91
+ expect(result.current.streamStatus.isStreaming).toBe(false);
92
+ expect(result.current.streamStatus.isPending).toBe(false);
93
+ // Individual props should be pending because they haven't started streaming yet
94
+ expect(result.current.propStatus.title.isPending).toBe(true);
95
+ expect(result.current.propStatus.title.isStreaming).toBe(false);
96
+ expect(result.current.propStatus.body.isPending).toBe(true);
97
+ expect(result.current.propStatus.body.isStreaming).toBe(false);
98
+ });
99
+ it("should show prop streaming when props receive content during STREAMING_RESPONSE", () => {
100
+ jest.mocked(useTamboThread).mockReturnValue({
101
+ generationStage: GenerationStage.STREAMING_RESPONSE,
102
+ });
103
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
104
+ component: createMockComponent({ title: "Hello", body: "" }),
105
+ }));
106
+ const { result } = renderHook(() => useTamboStreamStatus());
107
+ // Title prop should be streaming since it has content
108
+ expect(result.current.propStatus.title.isStreaming).toBe(true);
109
+ expect(result.current.propStatus.title.isPending).toBe(false);
110
+ // Body prop should still be pending since it has no content
111
+ expect(result.current.propStatus.body.isStreaming).toBe(false);
112
+ expect(result.current.propStatus.body.isPending).toBe(true);
113
+ // Global should be streaming because at least one prop is streaming
114
+ expect(result.current.streamStatus.isStreaming).toBe(true);
115
+ });
116
+ });
117
+ describe("Boolean Lifecycle", () => {
118
+ it("should transition through Init -> Streaming -> Success lifecycle", () => {
119
+ // Start with IDLE (Init phase)
120
+ jest.mocked(useTamboThread).mockReturnValue({
121
+ generationStage: GenerationStage.IDLE,
122
+ });
123
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
124
+ component: createMockComponent({ title: "", body: "" }),
125
+ }));
126
+ const { result, rerender } = renderHook(() => useTamboStreamStatus());
127
+ // Phase 1: Init - isPending = true
128
+ expect(result.current.streamStatus.isPending).toBe(true);
129
+ expect(result.current.streamStatus.isStreaming).toBe(false);
130
+ expect(result.current.streamStatus.isSuccess).toBe(false);
131
+ // Phase 2: Streaming - move to STREAMING_RESPONSE with content
132
+ jest.mocked(useTamboThread).mockReturnValue({
133
+ generationStage: GenerationStage.STREAMING_RESPONSE,
134
+ });
135
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
136
+ component: createMockComponent({
137
+ title: "Hello World",
138
+ body: "Some content",
139
+ }),
140
+ }));
141
+ rerender();
142
+ expect(result.current.streamStatus.isPending).toBe(false);
143
+ expect(result.current.streamStatus.isStreaming).toBe(true);
144
+ expect(result.current.streamStatus.isSuccess).toBe(false);
145
+ // Phase 3: Complete - move to COMPLETE
146
+ jest.mocked(useTamboThread).mockReturnValue({
147
+ generationStage: GenerationStage.COMPLETE,
148
+ });
149
+ rerender();
150
+ expect(result.current.streamStatus.isPending).toBe(false);
151
+ expect(result.current.streamStatus.isStreaming).toBe(false);
152
+ expect(result.current.streamStatus.isSuccess).toBe(true);
153
+ });
154
+ it("should handle error state correctly", () => {
155
+ jest.mocked(useTamboThread).mockReturnValue({
156
+ generationStage: GenerationStage.ERROR,
157
+ });
158
+ const errorMessage = "Generation failed";
159
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
160
+ component: createMockComponent({ title: "", body: "" }),
161
+ error: errorMessage,
162
+ }));
163
+ const { result } = renderHook(() => useTamboStreamStatus());
164
+ expect(result.current.streamStatus.isPending).toBe(true);
165
+ expect(result.current.streamStatus.isStreaming).toBe(false);
166
+ expect(result.current.streamStatus.isSuccess).toBe(false);
167
+ expect(result.current.streamStatus.isError).toBe(true);
168
+ expect(result.current.streamStatus.streamError?.message).toBe(errorMessage);
169
+ });
170
+ });
171
+ describe("Derivation Rules", () => {
172
+ it("should derive isPending correctly (no generation activity AND all props pending)", () => {
173
+ jest.mocked(useTamboThread).mockReturnValue({
174
+ generationStage: GenerationStage.IDLE,
175
+ });
176
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
177
+ component: createMockComponent({ title: "", body: "", footer: "" }),
178
+ }));
179
+ const { result } = renderHook(() => useTamboStreamStatus());
180
+ // All props are pending and no generation activity
181
+ expect(result.current.streamStatus.isPending).toBe(true);
182
+ expect(result.current.propStatus.title.isPending).toBe(true);
183
+ expect(result.current.propStatus.body.isPending).toBe(true);
184
+ expect(result.current.propStatus.footer.isPending).toBe(true);
185
+ });
186
+ it("should derive isStreaming correctly (generation streaming OR any prop streaming)", () => {
187
+ jest.mocked(useTamboThread).mockReturnValue({
188
+ generationStage: GenerationStage.COMPLETE,
189
+ });
190
+ // One prop still streaming
191
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
192
+ component: createMockComponent({
193
+ title: "Complete Title",
194
+ body: "Still streaming...",
195
+ }),
196
+ }));
197
+ const { result } = renderHook(() => useTamboStreamStatus());
198
+ // Should be streaming because at least one prop is streaming
199
+ expect(result.current.streamStatus.isStreaming).toBe(false); // Note: this will be false in our implementation because props are considered complete when generation is COMPLETE
200
+ });
201
+ it("should derive isSuccess correctly (generation complete AND all props successful)", () => {
202
+ // Step 1: Start with streaming, props empty
203
+ jest.mocked(useTamboThread).mockReturnValue({
204
+ generationStage: GenerationStage.STREAMING_RESPONSE,
205
+ });
206
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
207
+ component: createMockComponent({
208
+ title: "",
209
+ body: "",
210
+ }),
211
+ }));
212
+ const { result, rerender } = renderHook(() => useTamboStreamStatus());
213
+ // Step 2: Simulate streaming in title
214
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
215
+ component: createMockComponent({
216
+ title: "Complete Title",
217
+ body: "",
218
+ }),
219
+ }));
220
+ rerender();
221
+ // Step 3: Simulate streaming in body
222
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
223
+ component: createMockComponent({
224
+ title: "Complete Title",
225
+ body: "Complete Body",
226
+ }),
227
+ }));
228
+ rerender();
229
+ // Step 4: Generation complete
230
+ jest.mocked(useTamboThread).mockReturnValue({
231
+ generationStage: GenerationStage.COMPLETE,
232
+ });
233
+ rerender();
234
+ // Now both props should be successful
235
+ expect(result.current.propStatus.title.isSuccess).toBe(true);
236
+ expect(result.current.propStatus.body.isSuccess).toBe(true);
237
+ expect(result.current.streamStatus.isSuccess).toBe(true);
238
+ });
239
+ it("should derive isError correctly (generation error OR any prop error)", () => {
240
+ jest.mocked(useTamboThread).mockReturnValue({
241
+ generationStage: GenerationStage.COMPLETE,
242
+ });
243
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
244
+ component: createMockComponent({
245
+ title: "Good Title",
246
+ body: "Good Body",
247
+ }),
248
+ error: "Something went wrong",
249
+ }));
250
+ const { result } = renderHook(() => useTamboStreamStatus());
251
+ expect(result.current.streamStatus.isError).toBe(true);
252
+ expect(result.current.streamStatus.streamError?.message).toBe("Something went wrong");
253
+ });
254
+ });
255
+ describe("Type Safety", () => {
256
+ it("should provide strongly typed prop status based on generic", () => {
257
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
258
+ component: createMockComponent({
259
+ title: "Test",
260
+ description: "Test desc",
261
+ count: 42,
262
+ }),
263
+ }));
264
+ const { result } = renderHook(() => useTamboStreamStatus());
265
+ // TypeScript should infer these keys correctly
266
+ expect(result.current.propStatus.title).toBeDefined();
267
+ expect(result.current.propStatus.description).toBeDefined();
268
+ expect(result.current.propStatus.count).toBeDefined();
269
+ // This would cause a TypeScript error if uncommented:
270
+ // expect(result.current.propStatus.nonExistentProp).toBeDefined();
271
+ });
272
+ it("should work without generic type parameter", () => {
273
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
274
+ component: createMockComponent({ dynamicProp: "value" }),
275
+ }));
276
+ const { result } = renderHook(() => useTamboStreamStatus());
277
+ expect(result.current.streamStatus).toBeDefined();
278
+ expect(result.current.propStatus).toBeDefined();
279
+ });
280
+ });
281
+ describe("Edge Cases", () => {
282
+ it("should handle missing message gracefully", () => {
283
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(undefined);
284
+ const { result } = renderHook(() => useTamboStreamStatus());
285
+ expect(result.current.streamStatus.isPending).toBe(true);
286
+ expect(result.current.propStatus).toEqual({});
287
+ });
288
+ it("should handle missing component props gracefully", () => {
289
+ jest.mocked(useTamboCurrentMessage).mockReturnValue({
290
+ id: "test-message",
291
+ // Missing component property
292
+ });
293
+ const { result } = renderHook(() => useTamboStreamStatus());
294
+ expect(result.current.streamStatus.isPending).toBe(true);
295
+ expect(result.current.propStatus).toEqual({});
296
+ });
297
+ it("should reset prop tracking when generation restarts", () => {
298
+ // Step 1: Complete a message
299
+ jest.mocked(useTamboThread).mockReturnValue({
300
+ generationStage: GenerationStage.STREAMING_RESPONSE,
301
+ });
302
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
303
+ component: createMockComponent({ title: "" }),
304
+ }));
305
+ const { result, rerender } = renderHook(() => useTamboStreamStatus());
306
+ // Simulate streaming in title
307
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
308
+ component: createMockComponent({ title: "First Title" }),
309
+ }));
310
+ rerender();
311
+ // Complete generation
312
+ jest.mocked(useTamboThread).mockReturnValue({
313
+ generationStage: GenerationStage.COMPLETE,
314
+ });
315
+ rerender();
316
+ // Should be successful initially
317
+ expect(result.current.propStatus.title.isSuccess).toBe(true);
318
+ // Step 2: Start new generation with a new message ID to trigger reset
319
+ jest.mocked(useTamboThread).mockReturnValue({
320
+ generationStage: GenerationStage.CHOOSING_COMPONENT,
321
+ });
322
+ jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage({
323
+ id: "new-message-id", // Different message ID to trigger reset
324
+ component: createMockComponent({ title: "" }),
325
+ }));
326
+ rerender();
327
+ // Should reset to pending
328
+ expect(result.current.propStatus.title.isPending).toBe(true);
329
+ expect(result.current.propStatus.title.isSuccess).toBe(false);
330
+ });
331
+ });
332
+ });
333
+ //# sourceMappingURL=use-tambo-stream-status.test.js.map