@tambo-ai/react 0.71.0 → 0.73.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.
- package/dist/v1/hooks/use-tambo-v1-component-state.d.ts +44 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.js +134 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.d.ts +2 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.js +292 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-messages.test.js +22 -9
- package/dist/v1/hooks/use-tambo-v1-messages.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.d.ts +1 -0
- package/dist/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.js +9 -2
- package/dist/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.test.js +22 -9
- package/dist/v1/hooks/use-tambo-v1-send-message.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-suggestions.d.ts +91 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.js +152 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.d.ts +2 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.js +511 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts +11 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.js +16 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.d.ts +2 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.js +297 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-list.d.ts +6 -4
- package/dist/v1/hooks/use-tambo-v1-thread-list.d.ts.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-list.js +2 -2
- package/dist/v1/hooks/use-tambo-v1-thread-list.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-list.test.js +2 -2
- package/dist/v1/hooks/use-tambo-v1-thread-list.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1.test.js +16 -7
- package/dist/v1/hooks/use-tambo-v1.test.js.map +1 -1
- package/dist/v1/index.d.ts +39 -19
- package/dist/v1/index.d.ts.map +1 -1
- package/dist/v1/index.js +60 -34
- package/dist/v1/index.js.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.d.ts +61 -1
- package/dist/v1/providers/tambo-v1-provider.d.ts.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.js +35 -3
- package/dist/v1/providers/tambo-v1-provider.js.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.test.js +78 -3
- package/dist/v1/providers/tambo-v1-provider.test.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.d.ts +19 -10
- package/dist/v1/providers/tambo-v1-stream-context.d.ts.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.js +43 -53
- package/dist/v1/providers/tambo-v1-stream-context.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.test.js +94 -19
- package/dist/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stub-provider.d.ts +74 -0
- package/dist/v1/providers/tambo-v1-stub-provider.d.ts.map +1 -0
- package/dist/v1/providers/tambo-v1-stub-provider.js +212 -0
- package/dist/v1/providers/tambo-v1-stub-provider.js.map +1 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.d.ts +2 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.d.ts.map +1 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.js +162 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.js.map +1 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.d.ts +105 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.d.ts.map +1 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.js +191 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.js.map +1 -0
- package/dist/v1/types/message.d.ts +27 -2
- package/dist/v1/types/message.d.ts.map +1 -1
- package/dist/v1/types/message.js.map +1 -1
- package/dist/v1/utils/component-renderer.d.ts +37 -0
- package/dist/v1/utils/component-renderer.d.ts.map +1 -0
- package/dist/v1/utils/component-renderer.js +70 -0
- package/dist/v1/utils/component-renderer.js.map +1 -0
- package/dist/v1/utils/component-renderer.test.d.ts +2 -0
- package/dist/v1/utils/component-renderer.test.d.ts.map +1 -0
- package/dist/v1/utils/component-renderer.test.js +45 -0
- package/dist/v1/utils/component-renderer.test.js.map +1 -0
- package/dist/v1/utils/event-accumulator.js +28 -8
- package/dist/v1/utils/event-accumulator.js.map +1 -1
- package/dist/v1/utils/event-accumulator.test.js +201 -6
- package/dist/v1/utils/event-accumulator.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-component-state.d.ts +44 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.js +131 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.d.ts +2 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.js +290 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-messages.test.js +22 -9
- package/esm/v1/hooks/use-tambo-v1-messages.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.d.ts +1 -0
- package/esm/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.js +9 -2
- package/esm/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.test.js +22 -9
- package/esm/v1/hooks/use-tambo-v1-send-message.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-suggestions.d.ts +91 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.js +149 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.d.ts +2 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.js +506 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts +11 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.js +12 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.d.ts +2 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.js +292 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-list.d.ts +6 -4
- package/esm/v1/hooks/use-tambo-v1-thread-list.d.ts.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-list.js +2 -2
- package/esm/v1/hooks/use-tambo-v1-thread-list.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-list.test.js +2 -2
- package/esm/v1/hooks/use-tambo-v1-thread-list.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1.test.js +16 -7
- package/esm/v1/hooks/use-tambo-v1.test.js.map +1 -1
- package/esm/v1/index.d.ts +39 -19
- package/esm/v1/index.d.ts.map +1 -1
- package/esm/v1/index.js +43 -19
- package/esm/v1/index.js.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.d.ts +61 -1
- package/esm/v1/providers/tambo-v1-provider.d.ts.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.js +34 -4
- package/esm/v1/providers/tambo-v1-provider.js.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.test.js +79 -4
- package/esm/v1/providers/tambo-v1-provider.test.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.d.ts +19 -10
- package/esm/v1/providers/tambo-v1-stream-context.d.ts.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.js +44 -54
- package/esm/v1/providers/tambo-v1-stream-context.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.test.js +95 -20
- package/esm/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stub-provider.d.ts +74 -0
- package/esm/v1/providers/tambo-v1-stub-provider.d.ts.map +1 -0
- package/esm/v1/providers/tambo-v1-stub-provider.js +176 -0
- package/esm/v1/providers/tambo-v1-stub-provider.js.map +1 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.d.ts +2 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.d.ts.map +1 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.js +157 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.js.map +1 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.d.ts +105 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.d.ts.map +1 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.js +153 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.js.map +1 -0
- package/esm/v1/types/message.d.ts +27 -2
- package/esm/v1/types/message.d.ts.map +1 -1
- package/esm/v1/types/message.js.map +1 -1
- package/esm/v1/utils/component-renderer.d.ts +37 -0
- package/esm/v1/utils/component-renderer.d.ts.map +1 -0
- package/esm/v1/utils/component-renderer.js +33 -0
- package/esm/v1/utils/component-renderer.js.map +1 -0
- package/esm/v1/utils/component-renderer.test.d.ts +2 -0
- package/esm/v1/utils/component-renderer.test.d.ts.map +1 -0
- package/esm/v1/utils/component-renderer.test.js +40 -0
- package/esm/v1/utils/component-renderer.test.js.map +1 -0
- package/esm/v1/utils/event-accumulator.js +28 -8
- package/esm/v1/utils/event-accumulator.js.map +1 -1
- package/esm/v1/utils/event-accumulator.test.js +201 -6
- package/esm/v1/utils/event-accumulator.test.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return type for useTamboV1ComponentState hook.
|
|
3
|
+
* Similar to useState but with additional metadata.
|
|
4
|
+
*/
|
|
5
|
+
export type UseTamboV1ComponentStateReturn<S> = [
|
|
6
|
+
currentState: S,
|
|
7
|
+
setState: (newState: S | ((prev: S) => S)) => void,
|
|
8
|
+
meta: {
|
|
9
|
+
isPending: boolean;
|
|
10
|
+
error: Error | null;
|
|
11
|
+
flush: () => void;
|
|
12
|
+
}
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Hook for managing component state with bidirectional server sync.
|
|
16
|
+
*
|
|
17
|
+
* This hook acts like useState but automatically syncs state changes
|
|
18
|
+
* to the Tambo backend. Server-side state updates are also reflected
|
|
19
|
+
* in the component.
|
|
20
|
+
*
|
|
21
|
+
* Must be used within a component rendered via the component renderer.
|
|
22
|
+
* @param keyName - The unique key to identify this state value within the component's state
|
|
23
|
+
* @param initialValue - Initial value for the state (used if no server state exists)
|
|
24
|
+
* @param debounceTime - Debounce time in milliseconds (default: 500ms)
|
|
25
|
+
* @returns Tuple of [currentState, setState, meta]
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* function Counter() {
|
|
29
|
+
* const [count, setCount, { isPending }] = useTamboV1ComponentState('count', 0);
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <div>
|
|
33
|
+
* <span>{count}</span>
|
|
34
|
+
* <button onClick={() => setCount(c => c + 1)} disabled={isPending}>
|
|
35
|
+
* Increment
|
|
36
|
+
* </button>
|
|
37
|
+
* </div>
|
|
38
|
+
* );
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function useTamboV1ComponentState<S = undefined>(keyName: string, initialValue?: S, debounceTime?: number): UseTamboV1ComponentStateReturn<S | undefined>;
|
|
43
|
+
export declare function useTamboV1ComponentState<S>(keyName: string, initialValue: S, debounceTime?: number): UseTamboV1ComponentStateReturn<S>;
|
|
44
|
+
//# sourceMappingURL=use-tambo-v1-component-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tambo-v1-component-state.d.ts","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1-component-state.ts"],"names":[],"mappings":"AAoBA;;;GAGG;AACH,MAAM,MAAM,8BAA8B,CAAC,CAAC,IAAI;IAC9C,YAAY,EAAE,CAAC;IACf,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,IAAI;IAClD,IAAI,EAAE;QACJ,SAAS,EAAE,OAAO,CAAC;QACnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;QACpB,KAAK,EAAE,MAAM,IAAI,CAAC;KACnB;CACF,CAAC;AAgCF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,GAAG,SAAS,EACpD,OAAO,EAAE,MAAM,EACf,YAAY,CAAC,EAAE,CAAC,EAChB,YAAY,CAAC,EAAE,MAAM,GACpB,8BAA8B,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AACjD,wBAAgB,wBAAwB,CAAC,CAAC,EACxC,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,CAAC,EACf,YAAY,CAAC,EAAE,MAAM,GACpB,8BAA8B,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useTamboV1ComponentState = useTamboV1ComponentState;
|
|
5
|
+
/**
|
|
6
|
+
* useTamboV1ComponentState - Component State Hook for v1 API
|
|
7
|
+
*
|
|
8
|
+
* Provides bidirectional state synchronization between React components
|
|
9
|
+
* and the Tambo backend. State changes are debounced before syncing to
|
|
10
|
+
* the server, and server state updates are reflected in the component.
|
|
11
|
+
*
|
|
12
|
+
* Must be used within a component rendered via the component renderer.
|
|
13
|
+
*/
|
|
14
|
+
const react_1 = require("react");
|
|
15
|
+
const use_debounce_1 = require("use-debounce");
|
|
16
|
+
const fast_equals_1 = require("fast-equals");
|
|
17
|
+
const tambo_client_provider_1 = require("../../providers/tambo-client-provider");
|
|
18
|
+
const component_renderer_1 = require("../utils/component-renderer");
|
|
19
|
+
const tambo_v1_stream_context_1 = require("../providers/tambo-v1-stream-context");
|
|
20
|
+
/**
|
|
21
|
+
* Find a component content block by ID in a specific thread.
|
|
22
|
+
* Only searches the specified thread to prevent cross-thread data access
|
|
23
|
+
* and improve performance (O(m*k) instead of O(n*m*k)).
|
|
24
|
+
* @param streamState - The current stream state
|
|
25
|
+
* @param threadId - The thread ID to search in
|
|
26
|
+
* @param componentId - The component ID to find
|
|
27
|
+
* @returns The component content block, or undefined if not found
|
|
28
|
+
*/
|
|
29
|
+
function findComponentContent(streamState, threadId, componentId) {
|
|
30
|
+
// Only search the specified thread (not all threads)
|
|
31
|
+
const threadState = streamState.threadMap[threadId];
|
|
32
|
+
if (!threadState) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
for (const message of threadState.thread.messages) {
|
|
36
|
+
for (const content of message.content) {
|
|
37
|
+
if (content.type === "component" && content.id === componentId) {
|
|
38
|
+
return content;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
function useTamboV1ComponentState(keyName, initialValue, debounceTime = 500) {
|
|
45
|
+
const client = (0, tambo_client_provider_1.useTamboClient)();
|
|
46
|
+
const { componentId, threadId } = (0, component_renderer_1.useV1ComponentContent)();
|
|
47
|
+
const streamState = (0, tambo_v1_stream_context_1.useStreamState)();
|
|
48
|
+
// Find the component content to get server state (only search current thread)
|
|
49
|
+
const componentContent = findComponentContent(streamState, threadId, componentId);
|
|
50
|
+
const serverState = componentContent?.state;
|
|
51
|
+
const serverValue = serverState?.[keyName];
|
|
52
|
+
// Local state - initialized from server state or initial value
|
|
53
|
+
const [localState, setLocalState] = (0, react_1.useState)(() => serverValue ?? initialValue);
|
|
54
|
+
// Track pending state and errors
|
|
55
|
+
const [isPending, setIsPending] = (0, react_1.useState)(false);
|
|
56
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
57
|
+
// Track the last value we sent to avoid overwriting with stale server state
|
|
58
|
+
const lastSentValueRef = (0, react_1.useRef)(undefined);
|
|
59
|
+
// Track whether there's a pending local change that hasn't synced yet
|
|
60
|
+
const hasPendingLocalChangeRef = (0, react_1.useRef)(false);
|
|
61
|
+
// Track in-flight sync requests to avoid stale completions clearing pending state
|
|
62
|
+
const syncSeqRef = (0, react_1.useRef)(0);
|
|
63
|
+
// Debounced function to sync state to server
|
|
64
|
+
const syncToServer = (0, use_debounce_1.useDebouncedCallback)(async (newState) => {
|
|
65
|
+
const seq = ++syncSeqRef.current;
|
|
66
|
+
setIsPending(true);
|
|
67
|
+
setError(null);
|
|
68
|
+
lastSentValueRef.current = newState;
|
|
69
|
+
try {
|
|
70
|
+
await client.threads.state.updateState(componentId, {
|
|
71
|
+
threadId,
|
|
72
|
+
state: { [keyName]: newState },
|
|
73
|
+
});
|
|
74
|
+
// Clear pending flag after successful sync
|
|
75
|
+
hasPendingLocalChangeRef.current = false;
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
// Clear pending flag on error to allow server reconciliation
|
|
79
|
+
hasPendingLocalChangeRef.current = false;
|
|
80
|
+
const syncError = err instanceof Error ? err : new Error(String(err));
|
|
81
|
+
setError(syncError);
|
|
82
|
+
console.error(`[useTamboV1ComponentState] Failed to sync state for ${componentId}:`, syncError);
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
// Only clear isPending if this is the most recent request
|
|
86
|
+
if (seq === syncSeqRef.current) {
|
|
87
|
+
setIsPending(false);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}, debounceTime);
|
|
91
|
+
// setState function that updates local state and triggers debounced sync
|
|
92
|
+
const setState = (0, react_1.useCallback)((newState) => {
|
|
93
|
+
setLocalState((prev) => {
|
|
94
|
+
const nextState = typeof newState === "function"
|
|
95
|
+
? newState(prev)
|
|
96
|
+
: newState;
|
|
97
|
+
// Mark that we have a pending local change
|
|
98
|
+
hasPendingLocalChangeRef.current = true;
|
|
99
|
+
// Trigger debounced sync to server
|
|
100
|
+
void syncToServer(nextState);
|
|
101
|
+
return nextState;
|
|
102
|
+
});
|
|
103
|
+
}, [syncToServer]);
|
|
104
|
+
// Sync from server state when it changes (e.g., from streaming events)
|
|
105
|
+
(0, react_1.useEffect)(() => {
|
|
106
|
+
if (serverValue === undefined) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// Don't overwrite local changes that haven't synced yet
|
|
110
|
+
if (hasPendingLocalChangeRef.current) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
// Only sync if the server value is different from what we last sent
|
|
114
|
+
// This prevents overwriting local state with stale server values
|
|
115
|
+
if (lastSentValueRef.current !== undefined &&
|
|
116
|
+
(0, fast_equals_1.deepEqual)(serverValue, lastSentValueRef.current)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
// Use functional update to avoid localState in deps
|
|
120
|
+
setLocalState((prev) => (0, fast_equals_1.deepEqual)(serverValue, prev) ? prev : serverValue);
|
|
121
|
+
}, [serverValue]);
|
|
122
|
+
// Flush pending updates on unmount
|
|
123
|
+
(0, react_1.useEffect)(() => {
|
|
124
|
+
return () => {
|
|
125
|
+
void syncToServer.flush();
|
|
126
|
+
};
|
|
127
|
+
}, [syncToServer]);
|
|
128
|
+
// Flush function for immediate sync
|
|
129
|
+
const flush = (0, react_1.useCallback)(() => {
|
|
130
|
+
void syncToServer.flush();
|
|
131
|
+
}, [syncToServer]);
|
|
132
|
+
return [localState, setState, { isPending, error, flush }];
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=use-tambo-v1-component-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tambo-v1-component-state.js","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1-component-state.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;AAsGb,4DAiIC;AArOD;;;;;;;;GAQG;AAEH,iCAAiE;AACjE,+CAAoD;AACpD,6CAAwC;AACxC,iFAAuE;AACvE,oEAAoE;AACpE,kFAAsE;AAiBtE;;;;;;;;GAQG;AACH,SAAS,oBAAoB,CAC3B,WAA8C,EAC9C,QAAgB,EAChB,WAAmB;IAEnB,qDAAqD;IACrD,MAAM,WAAW,GAAG,WAAW,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IACpD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,KAAK,MAAM,OAAO,IAAI,WAAW,CAAC,MAAM,CA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AC,YAAY,CAAC,CAAC,CAAC;IAEnB,OAAO,CAAC,UAAU,EAAE,QAAQ,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["\"use client\";\n\n/**\n * useTamboV1ComponentState - Component State Hook for v1 API\n *\n * Provides bidirectional state synchronization between React components\n * and the Tambo backend. State changes are debounced before syncing to\n * the server, and server state updates are reflected in the component.\n *\n * Must be used within a component rendered via the component renderer.\n */\n\nimport { useCallback, useEffect, useState, useRef } from \"react\";\nimport { useDebouncedCallback } from \"use-debounce\";\nimport { deepEqual } from \"fast-equals\";\nimport { useTamboClient } from \"../../providers/tambo-client-provider\";\nimport { useV1ComponentContent } from \"../utils/component-renderer\";\nimport { useStreamState } from \"../providers/tambo-v1-stream-context\";\nimport type { V1ComponentContent } from \"../types/message\";\n\n/**\n * Return type for useTamboV1ComponentState hook.\n * Similar to useState but with additional metadata.\n */\nexport type UseTamboV1ComponentStateReturn<S> = [\n currentState: S,\n setState: (newState: S | ((prev: S) => S)) => void,\n meta: {\n isPending: boolean;\n error: Error | null;\n flush: () => void;\n },\n];\n\n/**\n * Find a component content block by ID in a specific thread.\n * Only searches the specified thread to prevent cross-thread data access\n * and improve performance (O(m*k) instead of O(n*m*k)).\n * @param streamState - The current stream state\n * @param threadId - The thread ID to search in\n * @param componentId - The component ID to find\n * @returns The component content block, or undefined if not found\n */\nfunction findComponentContent(\n streamState: ReturnType<typeof useStreamState>,\n threadId: string,\n componentId: string,\n): V1ComponentContent | undefined {\n // Only search the specified thread (not all threads)\n const threadState = streamState.threadMap[threadId];\n if (!threadState) {\n return undefined;\n }\n\n for (const message of threadState.thread.messages) {\n for (const content of message.content) {\n if (content.type === \"component\" && content.id === componentId) {\n return content;\n }\n }\n }\n return undefined;\n}\n\n/**\n * Hook for managing component state with bidirectional server sync.\n *\n * This hook acts like useState but automatically syncs state changes\n * to the Tambo backend. Server-side state updates are also reflected\n * in the component.\n *\n * Must be used within a component rendered via the component renderer.\n * @param keyName - The unique key to identify this state value within the component's state\n * @param initialValue - Initial value for the state (used if no server state exists)\n * @param debounceTime - Debounce time in milliseconds (default: 500ms)\n * @returns Tuple of [currentState, setState, meta]\n * @example\n * ```tsx\n * function Counter() {\n * const [count, setCount, { isPending }] = useTamboV1ComponentState('count', 0);\n *\n * return (\n * <div>\n * <span>{count}</span>\n * <button onClick={() => setCount(c => c + 1)} disabled={isPending}>\n * Increment\n * </button>\n * </div>\n * );\n * }\n * ```\n */\nexport function useTamboV1ComponentState<S = undefined>(\n keyName: string,\n initialValue?: S,\n debounceTime?: number,\n): UseTamboV1ComponentStateReturn<S | undefined>;\nexport function useTamboV1ComponentState<S>(\n keyName: string,\n initialValue: S,\n debounceTime?: number,\n): UseTamboV1ComponentStateReturn<S>;\nexport function useTamboV1ComponentState<S>(\n keyName: string,\n initialValue?: S,\n debounceTime = 500,\n): UseTamboV1ComponentStateReturn<S> {\n const client = useTamboClient();\n const { componentId, threadId } = useV1ComponentContent();\n const streamState = useStreamState();\n\n // Find the component content to get server state (only search current thread)\n const componentContent = findComponentContent(\n streamState,\n threadId,\n componentId,\n );\n const serverState = componentContent?.state as\n | Record<string, unknown>\n | undefined;\n const serverValue = serverState?.[keyName] as S | undefined;\n\n // Local state - initialized from server state or initial value\n const [localState, setLocalState] = useState<S>(\n () => serverValue ?? (initialValue as S),\n );\n\n // Track pending state and errors\n const [isPending, setIsPending] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n // Track the last value we sent to avoid overwriting with stale server state\n const lastSentValueRef = useRef<S | undefined>(undefined);\n\n // Track whether there's a pending local change that hasn't synced yet\n const hasPendingLocalChangeRef = useRef(false);\n\n // Track in-flight sync requests to avoid stale completions clearing pending state\n const syncSeqRef = useRef(0);\n\n // Debounced function to sync state to server\n const syncToServer = useDebouncedCallback(async (newState: S) => {\n const seq = ++syncSeqRef.current;\n setIsPending(true);\n setError(null);\n lastSentValueRef.current = newState;\n\n try {\n await client.threads.state.updateState(componentId, {\n threadId,\n state: { [keyName]: newState },\n });\n // Clear pending flag after successful sync\n hasPendingLocalChangeRef.current = false;\n } catch (err) {\n // Clear pending flag on error to allow server reconciliation\n hasPendingLocalChangeRef.current = false;\n const syncError = err instanceof Error ? err : new Error(String(err));\n setError(syncError);\n console.error(\n `[useTamboV1ComponentState] Failed to sync state for ${componentId}:`,\n syncError,\n );\n } finally {\n // Only clear isPending if this is the most recent request\n if (seq === syncSeqRef.current) {\n setIsPending(false);\n }\n }\n }, debounceTime);\n\n // setState function that updates local state and triggers debounced sync\n const setState = useCallback(\n (newState: S | ((prev: S) => S)) => {\n setLocalState((prev) => {\n const nextState =\n typeof newState === \"function\"\n ? (newState as (prev: S) => S)(prev)\n : newState;\n\n // Mark that we have a pending local change\n hasPendingLocalChangeRef.current = true;\n\n // Trigger debounced sync to server\n void syncToServer(nextState);\n\n return nextState;\n });\n },\n [syncToServer],\n );\n\n // Sync from server state when it changes (e.g., from streaming events)\n useEffect(() => {\n if (serverValue === undefined) {\n return;\n }\n\n // Don't overwrite local changes that haven't synced yet\n if (hasPendingLocalChangeRef.current) {\n return;\n }\n\n // Only sync if the server value is different from what we last sent\n // This prevents overwriting local state with stale server values\n if (\n lastSentValueRef.current !== undefined &&\n deepEqual(serverValue, lastSentValueRef.current)\n ) {\n return;\n }\n\n // Use functional update to avoid localState in deps\n setLocalState((prev) =>\n deepEqual(serverValue, prev) ? prev : serverValue,\n );\n }, [serverValue]);\n\n // Flush pending updates on unmount\n useEffect(() => {\n return () => {\n void syncToServer.flush();\n };\n }, [syncToServer]);\n\n // Flush function for immediate sync\n const flush = useCallback(() => {\n void syncToServer.flush();\n }, [syncToServer]);\n\n return [localState, setState, { isPending, error, flush }];\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tambo-v1-component-state.test.d.ts","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1-component-state.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("@testing-library/react");
|
|
4
|
+
const use_tambo_v1_component_state_1 = require("./use-tambo-v1-component-state");
|
|
5
|
+
// Mock the required modules
|
|
6
|
+
jest.mock("../../providers/tambo-client-provider", () => ({
|
|
7
|
+
useTamboClient: jest.fn(),
|
|
8
|
+
}));
|
|
9
|
+
jest.mock("../providers/tambo-v1-stream-context", () => ({
|
|
10
|
+
useStreamState: jest.fn(),
|
|
11
|
+
}));
|
|
12
|
+
jest.mock("../utils/component-renderer", () => ({
|
|
13
|
+
useV1ComponentContent: jest.fn(),
|
|
14
|
+
}));
|
|
15
|
+
// Create a mock debounced function with flush method
|
|
16
|
+
const createMockDebouncedFunction = (fn) => {
|
|
17
|
+
const debouncedFn = jest.fn((...args) => fn(...args));
|
|
18
|
+
debouncedFn.flush = jest.fn();
|
|
19
|
+
debouncedFn.cancel = jest.fn();
|
|
20
|
+
debouncedFn.isPending = jest.fn(() => false);
|
|
21
|
+
return debouncedFn;
|
|
22
|
+
};
|
|
23
|
+
// Mock use-debounce
|
|
24
|
+
jest.mock("use-debounce", () => ({
|
|
25
|
+
useDebouncedCallback: jest.fn(),
|
|
26
|
+
}));
|
|
27
|
+
// Import the mocked modules
|
|
28
|
+
const tambo_client_provider_1 = require("../../providers/tambo-client-provider");
|
|
29
|
+
const tambo_v1_stream_context_1 = require("../providers/tambo-v1-stream-context");
|
|
30
|
+
const component_renderer_1 = require("../utils/component-renderer");
|
|
31
|
+
const use_debounce_1 = require("use-debounce");
|
|
32
|
+
describe("useTamboV1ComponentState", () => {
|
|
33
|
+
const mockUpdateState = jest.fn();
|
|
34
|
+
const mockComponentId = "comp_test123";
|
|
35
|
+
const mockThreadId = "thread_abc";
|
|
36
|
+
const mockMessageId = "msg_xyz";
|
|
37
|
+
// Helper to create mock stream state
|
|
38
|
+
const createMockStreamState = (componentState) => ({
|
|
39
|
+
threadMap: {
|
|
40
|
+
[mockThreadId]: {
|
|
41
|
+
thread: {
|
|
42
|
+
id: mockThreadId,
|
|
43
|
+
messages: [
|
|
44
|
+
{
|
|
45
|
+
id: mockMessageId,
|
|
46
|
+
role: "assistant",
|
|
47
|
+
content: [
|
|
48
|
+
{
|
|
49
|
+
type: "component",
|
|
50
|
+
id: mockComponentId,
|
|
51
|
+
name: "TestComponent",
|
|
52
|
+
props: {},
|
|
53
|
+
state: componentState,
|
|
54
|
+
streamingState: "done",
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
createdAt: new Date().toISOString(),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
status: "idle",
|
|
61
|
+
createdAt: new Date().toISOString(),
|
|
62
|
+
updatedAt: new Date().toISOString(),
|
|
63
|
+
},
|
|
64
|
+
streaming: { status: "idle" },
|
|
65
|
+
accumulatingToolArgs: new Map(),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
currentThreadId: mockThreadId,
|
|
69
|
+
});
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
jest.clearAllMocks();
|
|
72
|
+
// Setup default mocks
|
|
73
|
+
jest.mocked(tambo_client_provider_1.useTamboClient).mockReturnValue({
|
|
74
|
+
threads: {
|
|
75
|
+
state: {
|
|
76
|
+
updateState: mockUpdateState,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
jest.mocked(component_renderer_1.useV1ComponentContent).mockReturnValue({
|
|
81
|
+
componentId: mockComponentId,
|
|
82
|
+
threadId: mockThreadId,
|
|
83
|
+
messageId: mockMessageId,
|
|
84
|
+
componentName: "TestComponent",
|
|
85
|
+
});
|
|
86
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState());
|
|
87
|
+
// Setup default mock for useDebouncedCallback
|
|
88
|
+
jest
|
|
89
|
+
.mocked(use_debounce_1.useDebouncedCallback)
|
|
90
|
+
.mockImplementation((fn) => createMockDebouncedFunction(fn));
|
|
91
|
+
});
|
|
92
|
+
afterEach(() => {
|
|
93
|
+
jest.restoreAllMocks();
|
|
94
|
+
});
|
|
95
|
+
describe("Initial State Management", () => {
|
|
96
|
+
it("should initialize with initialValue when no server state exists", () => {
|
|
97
|
+
const initialValue = "test-initial";
|
|
98
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
99
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", initialValue));
|
|
100
|
+
expect(result.current[0]).toBe(initialValue);
|
|
101
|
+
});
|
|
102
|
+
it("should use server state over initialValue", () => {
|
|
103
|
+
const initialValue = "initial";
|
|
104
|
+
const serverValue = "server-value";
|
|
105
|
+
jest
|
|
106
|
+
.mocked(tambo_v1_stream_context_1.useStreamState)
|
|
107
|
+
.mockReturnValue(createMockStreamState({ testKey: serverValue }));
|
|
108
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", initialValue));
|
|
109
|
+
expect(result.current[0]).toBe(serverValue);
|
|
110
|
+
});
|
|
111
|
+
it("should handle undefined initialValue gracefully", () => {
|
|
112
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
113
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey"));
|
|
114
|
+
expect(result.current[0]).toBeUndefined();
|
|
115
|
+
});
|
|
116
|
+
it("should handle different data types correctly", () => {
|
|
117
|
+
const testCases = [
|
|
118
|
+
{ value: "string" },
|
|
119
|
+
{ value: 42 },
|
|
120
|
+
{ value: true },
|
|
121
|
+
{ value: { name: "test" } },
|
|
122
|
+
{ value: [1, 2, 3] },
|
|
123
|
+
];
|
|
124
|
+
testCases.forEach(({ value }) => {
|
|
125
|
+
jest
|
|
126
|
+
.mocked(tambo_v1_stream_context_1.useStreamState)
|
|
127
|
+
.mockReturnValue(createMockStreamState({ testKey: value }));
|
|
128
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", value));
|
|
129
|
+
expect(result.current[0]).toEqual(value);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe("State Updates", () => {
|
|
134
|
+
it("should update local state immediately when setState is called", () => {
|
|
135
|
+
const initialValue = "initial";
|
|
136
|
+
jest
|
|
137
|
+
.mocked(tambo_v1_stream_context_1.useStreamState)
|
|
138
|
+
.mockReturnValue(createMockStreamState({ testKey: initialValue }));
|
|
139
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", initialValue));
|
|
140
|
+
const newValue = "updated";
|
|
141
|
+
(0, react_1.act)(() => {
|
|
142
|
+
result.current[1](newValue);
|
|
143
|
+
});
|
|
144
|
+
expect(result.current[0]).toBe(newValue);
|
|
145
|
+
});
|
|
146
|
+
it("should support functional updates", () => {
|
|
147
|
+
const initialValue = 5;
|
|
148
|
+
jest
|
|
149
|
+
.mocked(tambo_v1_stream_context_1.useStreamState)
|
|
150
|
+
.mockReturnValue(createMockStreamState({ counter: initialValue }));
|
|
151
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("counter", initialValue));
|
|
152
|
+
(0, react_1.act)(() => {
|
|
153
|
+
result.current[1]((prev) => (prev ?? 0) + 1);
|
|
154
|
+
});
|
|
155
|
+
expect(result.current[0]).toBe(6);
|
|
156
|
+
});
|
|
157
|
+
it("should trigger debounced API call when setState is called", () => {
|
|
158
|
+
const initialValue = "initial";
|
|
159
|
+
jest
|
|
160
|
+
.mocked(tambo_v1_stream_context_1.useStreamState)
|
|
161
|
+
.mockReturnValue(createMockStreamState({ testKey: initialValue }));
|
|
162
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", initialValue));
|
|
163
|
+
const newValue = "updated";
|
|
164
|
+
(0, react_1.act)(() => {
|
|
165
|
+
result.current[1](newValue);
|
|
166
|
+
});
|
|
167
|
+
// The debounced function should be called
|
|
168
|
+
expect(mockUpdateState).toHaveBeenCalledWith(mockComponentId, {
|
|
169
|
+
threadId: mockThreadId,
|
|
170
|
+
state: { testKey: newValue },
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
it("should work with complex objects", () => {
|
|
174
|
+
const initialValue = { name: "test", items: [1, 2, 3] };
|
|
175
|
+
jest
|
|
176
|
+
.mocked(tambo_v1_stream_context_1.useStreamState)
|
|
177
|
+
.mockReturnValue(createMockStreamState({ data: initialValue }));
|
|
178
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("data", initialValue));
|
|
179
|
+
const newValue = { name: "updated", items: [4, 5, 6] };
|
|
180
|
+
(0, react_1.act)(() => {
|
|
181
|
+
result.current[1](newValue);
|
|
182
|
+
});
|
|
183
|
+
expect(result.current[0]).toEqual(newValue);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
describe("Metadata", () => {
|
|
187
|
+
it("should return isPending and error in meta", () => {
|
|
188
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
189
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
190
|
+
expect(result.current[2]).toEqual({
|
|
191
|
+
isPending: false,
|
|
192
|
+
error: null,
|
|
193
|
+
flush: expect.any(Function),
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
it("should provide a flush function", () => {
|
|
197
|
+
const mockFlush = jest.fn();
|
|
198
|
+
const mockDebouncedFn = createMockDebouncedFunction(jest.fn());
|
|
199
|
+
mockDebouncedFn.flush = mockFlush;
|
|
200
|
+
jest.mocked(use_debounce_1.useDebouncedCallback).mockReturnValue(mockDebouncedFn);
|
|
201
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
202
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
203
|
+
(0, react_1.act)(() => {
|
|
204
|
+
result.current[2].flush();
|
|
205
|
+
});
|
|
206
|
+
expect(mockFlush).toHaveBeenCalled();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
describe("Debouncing Behavior", () => {
|
|
210
|
+
it("should use default debounce time of 500ms", () => {
|
|
211
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
212
|
+
(0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
213
|
+
expect(use_debounce_1.useDebouncedCallback).toHaveBeenCalledWith(expect.any(Function), 500);
|
|
214
|
+
});
|
|
215
|
+
it("should use custom debounce time when provided", () => {
|
|
216
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
217
|
+
const customDebounceTime = 1000;
|
|
218
|
+
(0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial", customDebounceTime));
|
|
219
|
+
expect(use_debounce_1.useDebouncedCallback).toHaveBeenCalledWith(expect.any(Function), customDebounceTime);
|
|
220
|
+
});
|
|
221
|
+
it("should flush debounced callback on unmount", () => {
|
|
222
|
+
const mockFlush = jest.fn();
|
|
223
|
+
const mockDebouncedFn = createMockDebouncedFunction(jest.fn());
|
|
224
|
+
mockDebouncedFn.flush = mockFlush;
|
|
225
|
+
jest.mocked(use_debounce_1.useDebouncedCallback).mockReturnValue(mockDebouncedFn);
|
|
226
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
227
|
+
const { unmount } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
228
|
+
unmount();
|
|
229
|
+
expect(mockFlush).toHaveBeenCalled();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe("Server State Sync", () => {
|
|
233
|
+
it("should sync with server state changes from streaming", () => {
|
|
234
|
+
const streamState = createMockStreamState({ testKey: "initial" });
|
|
235
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(streamState);
|
|
236
|
+
const { result, rerender } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
237
|
+
expect(result.current[0]).toBe("initial");
|
|
238
|
+
// Simulate server state change from streaming
|
|
239
|
+
const updatedStreamState = createMockStreamState({
|
|
240
|
+
testKey: "updated-from-server",
|
|
241
|
+
});
|
|
242
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(updatedStreamState);
|
|
243
|
+
rerender();
|
|
244
|
+
expect(result.current[0]).toBe("updated-from-server");
|
|
245
|
+
});
|
|
246
|
+
it("should handle component not found in stream state", () => {
|
|
247
|
+
// Empty stream state (no matching component)
|
|
248
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue({
|
|
249
|
+
threadMap: {},
|
|
250
|
+
currentThreadId: null,
|
|
251
|
+
});
|
|
252
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "default"));
|
|
253
|
+
expect(result.current[0]).toBe("default");
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
describe("Error Handling", () => {
|
|
257
|
+
it("should handle API errors gracefully", async () => {
|
|
258
|
+
const mockError = new Error("API Error");
|
|
259
|
+
mockUpdateState.mockRejectedValue(mockError);
|
|
260
|
+
// Create a sync mock that calls the function immediately
|
|
261
|
+
jest.mocked(use_debounce_1.useDebouncedCallback).mockImplementation((fn) => {
|
|
262
|
+
const debouncedFn = Object.assign(async (...args) => fn(...args), {
|
|
263
|
+
flush: jest.fn(),
|
|
264
|
+
cancel: jest.fn(),
|
|
265
|
+
isPending: () => false,
|
|
266
|
+
});
|
|
267
|
+
return debouncedFn;
|
|
268
|
+
});
|
|
269
|
+
jest.mocked(tambo_v1_stream_context_1.useStreamState).mockReturnValue(createMockStreamState({}));
|
|
270
|
+
const consoleSpy = jest.spyOn(console, "error").mockImplementation();
|
|
271
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
272
|
+
await (0, react_1.act)(async () => {
|
|
273
|
+
result.current[1]("new-value");
|
|
274
|
+
// Wait for async operations
|
|
275
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
276
|
+
});
|
|
277
|
+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to sync state"), mockError);
|
|
278
|
+
consoleSpy.mockRestore();
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
describe("Context Requirements", () => {
|
|
282
|
+
it("should throw when used outside component content context", () => {
|
|
283
|
+
jest.mocked(component_renderer_1.useV1ComponentContent).mockImplementation(() => {
|
|
284
|
+
throw new Error("useV1ComponentContent must be used within a rendered component");
|
|
285
|
+
});
|
|
286
|
+
expect(() => {
|
|
287
|
+
(0, react_1.renderHook)(() => (0, use_tambo_v1_component_state_1.useTamboV1ComponentState)("testKey", "initial"));
|
|
288
|
+
}).toThrow("useV1ComponentContent must be used within a rendered component");
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
//# sourceMappingURL=use-tambo-v1-component-state.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tambo-v1-component-state.test.js","sourceRoot":"","sources":["../../../src/v1/hooks/use-tambo-v1-component-state.test.tsx"],"names":[],"mappings":";;AAAA,kDAAyD;AACzD,iFAA0E;AAE1E,4BAA4B;AAC5B,IAAI,CAAC,IAAI,CAAC,uCAAuC,EAAE,G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,EAAE;gBACzD,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;YACJ,CAAC,CAAC,CAAC;YAEH,MAAM,CAAC,GAAG,EAAE;gBACV,IAAA,kBAAU,EAAC,GAAG,EAAE,CAAC,IAAA,uDAAwB,EAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;YACnE,CAAC,CAAC,CAAC,OAAO,CACR,gEAAgE,CACjE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook } from \"@testing-library/react\";\nimport { useTamboV1ComponentState } from \"./use-tambo-v1-component-state\";\n\n// Mock the required modules\njest.mock(\"../../providers/tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n}));\n\njest.mock(\"../providers/tambo-v1-stream-context\", () => ({\n useStreamState: jest.fn(),\n}));\n\njest.mock(\"../utils/component-renderer\", () => ({\n useV1ComponentContent: jest.fn(),\n}));\n\n// Create a mock debounced function with flush method\nconst createMockDebouncedFunction = (fn: (...args: unknown[]) => unknown) => {\n const debouncedFn = jest.fn((...args: unknown[]) =>\n fn(...args),\n ) as jest.Mock & {\n flush: jest.Mock;\n cancel: jest.Mock;\n isPending: () => boolean;\n };\n debouncedFn.flush = jest.fn();\n debouncedFn.cancel = jest.fn();\n debouncedFn.isPending = jest.fn(() => false);\n return debouncedFn;\n};\n\n// Mock use-debounce\njest.mock(\"use-debounce\", () => ({\n useDebouncedCallback: jest.fn(),\n}));\n\n// Import the mocked modules\nimport { useTamboClient } from \"../../providers/tambo-client-provider\";\nimport { useStreamState } from \"../providers/tambo-v1-stream-context\";\nimport { useV1ComponentContent } from \"../utils/component-renderer\";\nimport { useDebouncedCallback } from \"use-debounce\";\nimport type { StreamState } from \"../utils/event-accumulator\";\nimport type { V1ComponentContent } from \"../types/message\";\n\ndescribe(\"useTamboV1ComponentState\", () => {\n const mockUpdateState = jest.fn();\n const mockComponentId = \"comp_test123\";\n const mockThreadId = \"thread_abc\";\n const mockMessageId = \"msg_xyz\";\n\n // Helper to create mock stream state\n const createMockStreamState = (\n componentState?: Record<string, unknown>,\n ): StreamState => ({\n threadMap: {\n [mockThreadId]: {\n thread: {\n id: mockThreadId,\n messages: [\n {\n id: mockMessageId,\n role: \"assistant\",\n content: [\n {\n type: \"component\",\n id: mockComponentId,\n name: \"TestComponent\",\n props: {},\n state: componentState,\n streamingState: \"done\",\n } as V1ComponentContent,\n ],\n createdAt: new Date().toISOString(),\n },\n ],\n status: \"idle\",\n createdAt: new Date().toISOString(),\n updatedAt: new Date().toISOString(),\n },\n streaming: { status: \"idle\" },\n accumulatingToolArgs: new Map(),\n },\n },\n currentThreadId: mockThreadId,\n });\n\n beforeEach(() => {\n jest.clearAllMocks();\n\n // Setup default mocks\n jest.mocked(useTamboClient).mockReturnValue({\n threads: {\n state: {\n updateState: mockUpdateState,\n },\n },\n } as unknown as ReturnType<typeof useTamboClient>);\n\n jest.mocked(useV1ComponentContent).mockReturnValue({\n componentId: mockComponentId,\n threadId: mockThreadId,\n messageId: mockMessageId,\n componentName: \"TestComponent\",\n });\n\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState());\n\n // Setup default mock for useDebouncedCallback\n jest\n .mocked(useDebouncedCallback)\n .mockImplementation((fn) => createMockDebouncedFunction(fn));\n });\n\n afterEach(() => {\n jest.restoreAllMocks();\n });\n\n describe(\"Initial State Management\", () => {\n it(\"should initialize with initialValue when no server state exists\", () => {\n const initialValue = \"test-initial\";\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", initialValue),\n );\n\n expect(result.current[0]).toBe(initialValue);\n });\n\n it(\"should use server state over initialValue\", () => {\n const initialValue = \"initial\";\n const serverValue = \"server-value\";\n jest\n .mocked(useStreamState)\n .mockReturnValue(createMockStreamState({ testKey: serverValue }));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", initialValue),\n );\n\n expect(result.current[0]).toBe(serverValue);\n });\n\n it(\"should handle undefined initialValue gracefully\", () => {\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n const { result } = renderHook(() => useTamboV1ComponentState(\"testKey\"));\n\n expect(result.current[0]).toBeUndefined();\n });\n\n it(\"should handle different data types correctly\", () => {\n const testCases = [\n { value: \"string\" },\n { value: 42 },\n { value: true },\n { value: { name: \"test\" } },\n { value: [1, 2, 3] },\n ];\n\n testCases.forEach(({ value }) => {\n jest\n .mocked(useStreamState)\n .mockReturnValue(createMockStreamState({ testKey: value }));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", value),\n );\n\n expect(result.current[0]).toEqual(value);\n });\n });\n });\n\n describe(\"State Updates\", () => {\n it(\"should update local state immediately when setState is called\", () => {\n const initialValue = \"initial\";\n jest\n .mocked(useStreamState)\n .mockReturnValue(createMockStreamState({ testKey: initialValue }));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", initialValue),\n );\n\n const newValue = \"updated\";\n act(() => {\n result.current[1](newValue);\n });\n\n expect(result.current[0]).toBe(newValue);\n });\n\n it(\"should support functional updates\", () => {\n const initialValue = 5;\n jest\n .mocked(useStreamState)\n .mockReturnValue(createMockStreamState({ counter: initialValue }));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"counter\", initialValue),\n );\n\n act(() => {\n result.current[1]((prev) => (prev ?? 0) + 1);\n });\n\n expect(result.current[0]).toBe(6);\n });\n\n it(\"should trigger debounced API call when setState is called\", () => {\n const initialValue = \"initial\";\n jest\n .mocked(useStreamState)\n .mockReturnValue(createMockStreamState({ testKey: initialValue }));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", initialValue),\n );\n\n const newValue = \"updated\";\n act(() => {\n result.current[1](newValue);\n });\n\n // The debounced function should be called\n expect(mockUpdateState).toHaveBeenCalledWith(mockComponentId, {\n threadId: mockThreadId,\n state: { testKey: newValue },\n });\n });\n\n it(\"should work with complex objects\", () => {\n const initialValue = { name: \"test\", items: [1, 2, 3] };\n jest\n .mocked(useStreamState)\n .mockReturnValue(createMockStreamState({ data: initialValue }));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"data\", initialValue),\n );\n\n const newValue = { name: \"updated\", items: [4, 5, 6] };\n act(() => {\n result.current[1](newValue);\n });\n\n expect(result.current[0]).toEqual(newValue);\n });\n });\n\n describe(\"Metadata\", () => {\n it(\"should return isPending and error in meta\", () => {\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"initial\"),\n );\n\n expect(result.current[2]).toEqual({\n isPending: false,\n error: null,\n flush: expect.any(Function),\n });\n });\n\n it(\"should provide a flush function\", () => {\n const mockFlush = jest.fn();\n const mockDebouncedFn = createMockDebouncedFunction(jest.fn());\n mockDebouncedFn.flush = mockFlush;\n jest.mocked(useDebouncedCallback).mockReturnValue(mockDebouncedFn);\n\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"initial\"),\n );\n\n act(() => {\n result.current[2].flush();\n });\n\n expect(mockFlush).toHaveBeenCalled();\n });\n });\n\n describe(\"Debouncing Behavior\", () => {\n it(\"should use default debounce time of 500ms\", () => {\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n renderHook(() => useTamboV1ComponentState(\"testKey\", \"initial\"));\n\n expect(useDebouncedCallback).toHaveBeenCalledWith(\n expect.any(Function),\n 500,\n );\n });\n\n it(\"should use custom debounce time when provided\", () => {\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n const customDebounceTime = 1000;\n\n renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"initial\", customDebounceTime),\n );\n\n expect(useDebouncedCallback).toHaveBeenCalledWith(\n expect.any(Function),\n customDebounceTime,\n );\n });\n\n it(\"should flush debounced callback on unmount\", () => {\n const mockFlush = jest.fn();\n const mockDebouncedFn = createMockDebouncedFunction(jest.fn());\n mockDebouncedFn.flush = mockFlush;\n jest.mocked(useDebouncedCallback).mockReturnValue(mockDebouncedFn);\n\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n const { unmount } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"initial\"),\n );\n\n unmount();\n\n expect(mockFlush).toHaveBeenCalled();\n });\n });\n\n describe(\"Server State Sync\", () => {\n it(\"should sync with server state changes from streaming\", () => {\n const streamState = createMockStreamState({ testKey: \"initial\" });\n jest.mocked(useStreamState).mockReturnValue(streamState);\n\n const { result, rerender } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"initial\"),\n );\n\n expect(result.current[0]).toBe(\"initial\");\n\n // Simulate server state change from streaming\n const updatedStreamState = createMockStreamState({\n testKey: \"updated-from-server\",\n });\n jest.mocked(useStreamState).mockReturnValue(updatedStreamState);\n\n rerender();\n\n expect(result.current[0]).toBe(\"updated-from-server\");\n });\n\n it(\"should handle component not found in stream state\", () => {\n // Empty stream state (no matching component)\n jest.mocked(useStreamState).mockReturnValue({\n threadMap: {},\n currentThreadId: null,\n });\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"default\"),\n );\n\n expect(result.current[0]).toBe(\"default\");\n });\n });\n\n describe(\"Error Handling\", () => {\n it(\"should handle API errors gracefully\", async () => {\n const mockError = new Error(\"API Error\");\n mockUpdateState.mockRejectedValue(mockError);\n\n // Create a sync mock that calls the function immediately\n jest.mocked(useDebouncedCallback).mockImplementation((fn) => {\n const debouncedFn = Object.assign(\n async (...args: unknown[]) => fn(...args),\n {\n flush: jest.fn(),\n cancel: jest.fn(),\n isPending: () => false,\n },\n ) as unknown as ReturnType<typeof useDebouncedCallback>;\n return debouncedFn;\n });\n\n jest.mocked(useStreamState).mockReturnValue(createMockStreamState({}));\n\n const consoleSpy = jest.spyOn(console, \"error\").mockImplementation();\n\n const { result } = renderHook(() =>\n useTamboV1ComponentState(\"testKey\", \"initial\"),\n );\n\n await act(async () => {\n result.current[1](\"new-value\");\n // Wait for async operations\n await new Promise((resolve) => setTimeout(resolve, 0));\n });\n\n expect(consoleSpy).toHaveBeenCalledWith(\n expect.stringContaining(\"Failed to sync state\"),\n mockError,\n );\n\n consoleSpy.mockRestore();\n });\n });\n\n describe(\"Context Requirements\", () => {\n it(\"should throw when used outside component content context\", () => {\n jest.mocked(useV1ComponentContent).mockImplementation(() => {\n throw new Error(\n \"useV1ComponentContent must be used within a rendered component\",\n );\n });\n\n expect(() => {\n renderHook(() => useTamboV1ComponentState(\"testKey\", \"initial\"));\n }).toThrow(\n \"useV1ComponentContent must be used within a rendered component\",\n );\n });\n });\n});\n"]}
|
|
@@ -10,24 +10,33 @@ const tambo_v1_stream_context_1 = require("../providers/tambo-v1-stream-context"
|
|
|
10
10
|
const use_tambo_v1_messages_1 = require("./use-tambo-v1-messages");
|
|
11
11
|
describe("useTamboV1Messages", () => {
|
|
12
12
|
function TestWrapper({ children }) {
|
|
13
|
-
return
|
|
13
|
+
return react_2.default.createElement(tambo_v1_stream_context_1.TamboV1StreamProvider, null, children);
|
|
14
14
|
}
|
|
15
15
|
it("returns empty messages when thread has no messages", () => {
|
|
16
|
-
const { result } = (0, react_1.renderHook)(() => (
|
|
17
|
-
|
|
16
|
+
const { result } = (0, react_1.renderHook)(() => ({
|
|
17
|
+
messages: (0, use_tambo_v1_messages_1.useTamboV1Messages)("thread_123"),
|
|
18
|
+
dispatch: (0, tambo_v1_stream_context_1.useStreamDispatch)(),
|
|
19
|
+
}), { wrapper: TestWrapper });
|
|
20
|
+
// Initialize thread first
|
|
21
|
+
(0, react_1.act)(() => {
|
|
22
|
+
result.current.dispatch({ type: "INIT_THREAD", threadId: "thread_123" });
|
|
18
23
|
});
|
|
19
|
-
expect(result.current.messages).toEqual([]);
|
|
20
|
-
expect(result.current.hasMessages).toBe(false);
|
|
21
|
-
expect(result.current.messageCount).toBe(0);
|
|
22
|
-
expect(result.current.lastMessage).toBeUndefined();
|
|
23
|
-
expect(result.current.userMessages).toEqual([]);
|
|
24
|
-
expect(result.current.assistantMessages).toEqual([]);
|
|
24
|
+
expect(result.current.messages.messages).toEqual([]);
|
|
25
|
+
expect(result.current.messages.hasMessages).toBe(false);
|
|
26
|
+
expect(result.current.messages.messageCount).toBe(0);
|
|
27
|
+
expect(result.current.messages.lastMessage).toBeUndefined();
|
|
28
|
+
expect(result.current.messages.userMessages).toEqual([]);
|
|
29
|
+
expect(result.current.messages.assistantMessages).toEqual([]);
|
|
25
30
|
});
|
|
26
31
|
it("returns messages after events are dispatched", () => {
|
|
27
32
|
const { result } = (0, react_1.renderHook)(() => ({
|
|
28
33
|
messages: (0, use_tambo_v1_messages_1.useTamboV1Messages)("thread_123"),
|
|
29
34
|
dispatch: (0, tambo_v1_stream_context_1.useStreamDispatch)(),
|
|
30
35
|
}), { wrapper: TestWrapper });
|
|
36
|
+
// Initialize thread first
|
|
37
|
+
(0, react_1.act)(() => {
|
|
38
|
+
result.current.dispatch({ type: "INIT_THREAD", threadId: "thread_123" });
|
|
39
|
+
});
|
|
31
40
|
// Simulate a text message being received
|
|
32
41
|
(0, react_1.act)(() => {
|
|
33
42
|
result.current.dispatch({
|
|
@@ -74,6 +83,10 @@ describe("useTamboV1Messages", () => {
|
|
|
74
83
|
messages: (0, use_tambo_v1_messages_1.useTamboV1Messages)("thread_123"),
|
|
75
84
|
dispatch: (0, tambo_v1_stream_context_1.useStreamDispatch)(),
|
|
76
85
|
}), { wrapper: TestWrapper });
|
|
86
|
+
// Initialize thread first
|
|
87
|
+
(0, react_1.act)(() => {
|
|
88
|
+
result.current.dispatch({ type: "INIT_THREAD", threadId: "thread_123" });
|
|
89
|
+
});
|
|
77
90
|
// Add user message
|
|
78
91
|
(0, react_1.act)(() => {
|
|
79
92
|
result.current.dispatch({
|