@tambo-ai/react 0.66.2 → 0.67.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/context-helpers/current-interactables-context-helper.d.ts +2 -2
- package/dist/context-helpers/current-interactables-context-helper.d.ts.map +1 -1
- package/dist/context-helpers/current-interactables-context-helper.js +17 -23
- package/dist/context-helpers/current-interactables-context-helper.js.map +1 -1
- package/dist/hoc/with-tambo-interactable.d.ts.map +1 -1
- package/dist/hoc/with-tambo-interactable.js +0 -1
- package/dist/hoc/with-tambo-interactable.js.map +1 -1
- package/dist/hooks/use-component-state.d.ts +16 -1
- package/dist/hooks/use-component-state.d.ts.map +1 -1
- package/dist/hooks/use-component-state.js +72 -12
- package/dist/hooks/use-component-state.js.map +1 -1
- package/dist/hooks/use-component-state.test.js +42 -0
- package/dist/hooks/use-component-state.test.js.map +1 -1
- package/dist/hooks/use-current-message.d.ts +1 -0
- package/dist/hooks/use-current-message.d.ts.map +1 -1
- package/dist/hooks/use-current-message.js +5 -5
- package/dist/hooks/use-current-message.js.map +1 -1
- package/dist/model/tambo-interactable.d.ts +6 -0
- package/dist/model/tambo-interactable.d.ts.map +1 -1
- package/dist/model/tambo-interactable.js.map +1 -1
- package/dist/providers/tambo-interactable-provider.d.ts +1 -0
- package/dist/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/dist/providers/tambo-interactable-provider.js +37 -7
- package/dist/providers/tambo-interactable-provider.js.map +1 -1
- package/dist/providers/tambo-interactable-provider.test.d.ts +2 -0
- package/dist/providers/tambo-interactable-provider.test.d.ts.map +1 -0
- package/dist/providers/tambo-interactable-provider.test.js +62 -0
- package/dist/providers/tambo-interactable-provider.test.js.map +1 -0
- package/dist/schema/zod.d.ts.map +1 -1
- package/dist/schema/zod.js +3 -3
- package/dist/schema/zod.js.map +1 -1
- package/dist/schema/zod.test.d.ts +2 -0
- package/dist/schema/zod.test.d.ts.map +1 -0
- package/dist/schema/zod.test.js +551 -0
- package/dist/schema/zod.test.js.map +1 -0
- package/esm/context-helpers/current-interactables-context-helper.d.ts +2 -2
- package/esm/context-helpers/current-interactables-context-helper.d.ts.map +1 -1
- package/esm/context-helpers/current-interactables-context-helper.js +17 -23
- package/esm/context-helpers/current-interactables-context-helper.js.map +1 -1
- package/esm/hoc/with-tambo-interactable.d.ts.map +1 -1
- package/esm/hoc/with-tambo-interactable.js +0 -1
- package/esm/hoc/with-tambo-interactable.js.map +1 -1
- package/esm/hooks/use-component-state.d.ts +16 -1
- package/esm/hooks/use-component-state.d.ts.map +1 -1
- package/esm/hooks/use-component-state.js +74 -14
- package/esm/hooks/use-component-state.js.map +1 -1
- package/esm/hooks/use-component-state.test.js +40 -1
- package/esm/hooks/use-component-state.test.js.map +1 -1
- package/esm/hooks/use-current-message.d.ts +1 -0
- package/esm/hooks/use-current-message.d.ts.map +1 -1
- package/esm/hooks/use-current-message.js +1 -1
- package/esm/hooks/use-current-message.js.map +1 -1
- package/esm/model/tambo-interactable.d.ts +6 -0
- package/esm/model/tambo-interactable.d.ts.map +1 -1
- package/esm/model/tambo-interactable.js.map +1 -1
- package/esm/providers/tambo-interactable-provider.d.ts +1 -0
- package/esm/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/esm/providers/tambo-interactable-provider.js +38 -8
- package/esm/providers/tambo-interactable-provider.js.map +1 -1
- package/esm/providers/tambo-interactable-provider.test.d.ts +2 -0
- package/esm/providers/tambo-interactable-provider.test.d.ts.map +1 -0
- package/esm/providers/tambo-interactable-provider.test.js +57 -0
- package/esm/providers/tambo-interactable-provider.test.js.map +1 -0
- package/esm/schema/zod.d.ts.map +1 -1
- package/esm/schema/zod.js +3 -3
- package/esm/schema/zod.js.map +1 -1
- package/esm/schema/zod.test.d.ts +2 -0
- package/esm/schema/zod.test.d.ts.map +1 -0
- package/esm/schema/zod.test.js +516 -0
- package/esm/schema/zod.test.js.map +1 -0
- package/package.json +2 -2
|
@@ -24,33 +24,27 @@ export const currentInteractablesContextHelper = () => {
|
|
|
24
24
|
/**
|
|
25
25
|
* Creates an interactables context helper with access to the current components.
|
|
26
26
|
* This is used internally by TamboInteractableProvider.
|
|
27
|
-
* @param
|
|
27
|
+
* @param components Array of interactable components
|
|
28
28
|
* @returns Context helper function
|
|
29
29
|
*/
|
|
30
|
-
export const createInteractablesContextHelper = (
|
|
30
|
+
export const createInteractablesContextHelper = (components) => {
|
|
31
31
|
return () => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!Array.isArray(components) || components.length === 0) {
|
|
35
|
-
return null; // No interactable components on the page
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
description: "These are the interactable components currently visible on the page that you can read and modify. Each component has an id, componentName, current props, and optional schema. You can use tools to update these components on behalf of the user.",
|
|
39
|
-
components: components.map((component) => ({
|
|
40
|
-
id: component.id,
|
|
41
|
-
componentName: component.name,
|
|
42
|
-
description: component.description,
|
|
43
|
-
props: component.props,
|
|
44
|
-
propsSchema: component.propsSchema
|
|
45
|
-
? "Available - use component-specific update tools"
|
|
46
|
-
: "Not specified",
|
|
47
|
-
})),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
catch (e) {
|
|
51
|
-
console.error("currentInteractablesContextHelper failed:", e);
|
|
52
|
-
return null;
|
|
32
|
+
if (!Array.isArray(components) || components.length === 0) {
|
|
33
|
+
return null; // No interactable components on the page
|
|
53
34
|
}
|
|
35
|
+
return {
|
|
36
|
+
description: "These are the interactable components currently visible on the page that you can read and modify. Each component has an id, componentName, current props, current state,and optional schema. You can use tools to update these components on behalf of the user. Don't tell the user the ID of the components, only the name, unless they ask for it.",
|
|
37
|
+
components: components.map((component) => ({
|
|
38
|
+
id: component.id,
|
|
39
|
+
componentName: component.name,
|
|
40
|
+
description: component.description,
|
|
41
|
+
props: component.props,
|
|
42
|
+
propsSchema: component.propsSchema
|
|
43
|
+
? "Available - use component-specific update tools"
|
|
44
|
+
: "Not specified",
|
|
45
|
+
state: component.state,
|
|
46
|
+
})),
|
|
47
|
+
};
|
|
54
48
|
};
|
|
55
49
|
};
|
|
56
50
|
//# sourceMappingURL=current-interactables-context-helper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"current-interactables-context-helper.js","sourceRoot":"","sources":["../../src/context-helpers/current-interactables-context-helper.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAoB,GAAG,EAAE;IACrE,mFAAmF;IACnF,wEAAwE;IACxE,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAC9C,
|
|
1
|
+
{"version":3,"file":"current-interactables-context-helper.js","sourceRoot":"","sources":["../../src/context-helpers/current-interactables-context-helper.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAoB,GAAG,EAAE;IACrE,mFAAmF;IACnF,wEAAwE;IACxE,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAC9C,UAAiB,EACA,EAAE;IACnB,OAAO,GAAG,EAAE;QACV,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1D,OAAO,IAAI,CAAC,CAAC,yCAAyC;QACxD,CAAC;QAED,OAAO;YACL,WAAW,EACT,uVAAuV;YACzV,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;gBACzC,EAAE,EAAE,SAAS,CAAC,EAAE;gBAChB,aAAa,EAAE,SAAS,CAAC,IAAI;gBAC7B,WAAW,EAAE,SAAS,CAAC,WAAW;gBAClC,KAAK,EAAE,SAAS,CAAC,KAAK;gBACtB,WAAW,EAAE,SAAS,CAAC,WAAW;oBAChC,CAAC,CAAC,iDAAiD;oBACnD,CAAC,CAAC,eAAe;gBACnB,KAAK,EAAE,SAAS,CAAC,KAAK;aACvB,CAAC,CAAC;SACJ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { ContextHelperFn } from \"./types\";\n\n/**\n * Prebuilt context helper that provides information about all interactable components currently on the page.\n * This gives the AI awareness of what components it can interact with and their current state.\n * @returns an object with description and components, or null to skip including this context.\n * To disable this helper, override it with a function that returns null:\n * @example\n * ```tsx\n * // To disable the default interactables context\n * const { addContextHelper } = useTamboContextHelpers();\n * addContextHelper(\"interactables\", () => null);\n *\n * // To customize the context\n * addContextHelper(\"interactables\", () => ({\n * description: \"Custom description\",\n * components: getCustomComponentsSubset()\n * }));\n * ```\n */\nexport const currentInteractablesContextHelper: ContextHelperFn = () => {\n // This will be provided by the interactable provider when it registers this helper\n // Since we're provider-only now, this function gets replaced at runtime\n return null;\n};\n\n/**\n * Creates an interactables context helper with access to the current components.\n * This is used internally by TamboInteractableProvider.\n * @param components Array of interactable components\n * @returns Context helper function\n */\nexport const createInteractablesContextHelper = (\n components: any[],\n): ContextHelperFn => {\n return () => {\n if (!Array.isArray(components) || components.length === 0) {\n return null; // No interactable components on the page\n }\n\n return {\n description:\n \"These are the interactable components currently visible on the page that you can read and modify. Each component has an id, componentName, current props, current state,and optional schema. You can use tools to update these components on behalf of the user. Don't tell the user the ID of the components, only the name, unless they ask for it.\",\n components: components.map((component) => ({\n id: component.id,\n componentName: component.name,\n description: component.description,\n props: component.props,\n propsSchema: component.propsSchema\n ? \"Available - use component-specific update tools\"\n : \"Not specified\",\n state: component.state,\n })),\n };\n };\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-tambo-interactable.d.ts","sourceRoot":"","sources":["../../src/hoc/with-tambo-interactable.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"with-tambo-interactable.d.ts","sourceRoot":"","sources":["../../src/hoc/with-tambo-interactable.tsx"],"names":[],"mappings":"AACA,OAAO,KAAmD,MAAM,OAAO,CAAC;AAIxE,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAE5C,MAAM,WAAW,kBAAkB;IACjC,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,eAAe,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,mBAAmB,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3C,aAAa,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;CACzD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,gBAAgB,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EACxC,MAAM,EAAE,kBAAkB,4CAgH3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-tambo-interactable.js","sourceRoot":"","sources":["../../src/hoc/with-tambo-interactable.tsx"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"with-tambo-interactable.js","sourceRoot":"","sources":["../../src/hoc/with-tambo-interactable.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,KAAK,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,0CAA0C,CAAC;AAehF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,qBAAqB,CACnC,gBAAwC,EACxC,MAA0B;IAE1B,MAAM,WAAW,GACf,gBAAgB,CAAC,WAAW,IAA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IACnB,OAAO,EAAE,cAAc,EACvB,oBAAoB,EAAE;gBACpB,EAAE,EAAE,cAAc;gBAClB,aAAa,EAAE,MAAM,CAAC,aAAa;gBACnC,WAAW,EAAE,MAAM,CAAC,WAAW;aAChC;YAED,oBAAC,gBAAgB,OAAM,cAAoB,GAAI,CAC1B,CACxB,CAAC;IACJ,CAAC,CAAC;IAEF,wBAAwB,CAAC,WAAW,GAAG,yBAAyB,WAAW,GAAG,CAAC;IAE/E,OAAO,wBAAwB,CAAC;AAClC,CAAC","sourcesContent":["\"use client\";\nimport React, { useCallback, useEffect, useRef, useState } from \"react\";\nimport { TamboMessageProvider } from \"../hooks/use-current-message\";\nimport { TamboThreadMessage } from \"../model/generate-component-response\";\nimport { useTamboInteractable } from \"../providers/tambo-interactable-provider\";\nimport { SupportedSchema } from \"../schema\";\n\nexport interface InteractableConfig {\n componentName: string;\n description: string;\n propsSchema?: SupportedSchema;\n}\n\nexport interface WithTamboInteractableProps {\n interactableId?: string;\n onInteractableReady?: (id: string) => void;\n onPropsUpdate?: (newProps: Record<string, any>) => void;\n}\n\n/**\n * Higher-Order Component that makes any component interactable by tambo.\n * @param WrappedComponent - The component to make interactable\n * @param config - Configuration for the interactable component\n * @returns A new component that is automatically registered as interactable\n * @example\n * ```tsx\n * const MyInteractableNote = withTamboInteractable(MyNote, {\n * componentName: \"MyNote\",\n * description: \"A note component\",\n * propsSchema: z.object({\n * title: z.string(),\n * content: z.string(),\n * }),\n * });\n *\n * // Usage\n * <MyInteractableNote title=\"My Note\" content=\"This is my note\" />\n * ```\n */\nexport function withTamboInteractable<P extends object>(\n WrappedComponent: React.ComponentType<P>,\n config: InteractableConfig,\n) {\n const displayName =\n WrappedComponent.displayName ?? WrappedComponent.name ?? \"Component\";\n\n const TamboInteractableWrapper: React.FC<P & WithTamboInteractableProps> = (\n props,\n ) => {\n const {\n addInteractableComponent,\n updateInteractableComponentProps,\n getInteractableComponent,\n } = useTamboInteractable();\n\n const [interactableId, setInteractableId] = useState<string | null>(null);\n const isInitialized = useRef(false);\n const lastParentProps = useRef<Record<string, any>>({});\n\n // Extract interactable-specific props\n const { onInteractableReady, onPropsUpdate, ...componentProps } = props;\n\n // Get the current interactable component to track prop updates\n const currentInteractable = interactableId\n ? getInteractableComponent(interactableId)\n : null;\n\n // Use the props from the interactable component if available, otherwise use the passed props\n // We need to be careful not to create a loop, so we only use stored props if they're different from passed props\n const effectiveProps = currentInteractable?.props ?? componentProps;\n\n // Memoize the registration function\n const registerComponent = useCallback(() => {\n if (!isInitialized.current) {\n const id = addInteractableComponent({\n name: config.componentName,\n description: config.description,\n component: WrappedComponent,\n props: componentProps,\n propsSchema: config.propsSchema,\n });\n\n setInteractableId(id);\n onInteractableReady?.(id);\n isInitialized.current = true;\n }\n }, [addInteractableComponent, componentProps, onInteractableReady]);\n\n // Register the component as interactable on mount (only once)\n useEffect(() => {\n registerComponent();\n }, [registerComponent]);\n\n // Update the interactable component when props change from parent\n useEffect(() => {\n if (interactableId && isInitialized.current) {\n // Only update if the props are different from what we last sent\n const lastPropsString = JSON.stringify(lastParentProps.current);\n const currentPropsString = JSON.stringify(componentProps);\n\n if (lastPropsString !== currentPropsString) {\n updateInteractableComponentProps(interactableId, componentProps);\n onPropsUpdate?.(componentProps);\n lastParentProps.current = componentProps;\n }\n }\n }, [\n interactableId,\n componentProps,\n updateInteractableComponentProps,\n onPropsUpdate,\n ]);\n\n // If the interactable ID is not yet set, render the component without provider\n if (!interactableId) {\n return <WrappedComponent {...(effectiveProps as P)} />;\n }\n\n // Create a minimal message with interactable metadata\n // This allows useTamboCurrentComponent to work with standalone interactable components\n const minimalMessage: TamboThreadMessage = {\n id: interactableId,\n role: \"assistant\" as const,\n content: [],\n threadId: \"\",\n createdAt: new Date().toISOString(),\n component: {\n componentName: config.componentName,\n componentState: {},\n message: \"\",\n props: effectiveProps,\n },\n componentState: {},\n };\n\n // Wrap with TamboMessageProvider including interactable metadata\n return (\n <TamboMessageProvider\n message={minimalMessage}\n interactableMetadata={{\n id: interactableId,\n componentName: config.componentName,\n description: config.description,\n }}\n >\n <WrappedComponent {...(effectiveProps as P)} />\n </TamboMessageProvider>\n );\n };\n\n TamboInteractableWrapper.displayName = `withTamboInteractable(${displayName})`;\n\n return TamboInteractableWrapper;\n}\n"]}
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
type StateUpdateResult<T> = [currentState: T, setState: (newState: T) => void];
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* A React hook that acts like useState, but also automatically updates the thread message's componentState.
|
|
4
|
+
* If used within an interactable component (wrapped with withTamboInteractable), it updates the
|
|
5
|
+
* interactable provider's global state (sent to Tambo on every request) instead of the remote thread message state.
|
|
6
|
+
* For generated components, it updates both the local and remote thread message's componentState.
|
|
7
|
+
*
|
|
8
|
+
* Benefits: Passes user changes to AI, and when threads are returned, state is preserved.
|
|
9
|
+
* Works in both generative and interactable component contexts.
|
|
10
|
+
* @param keyName - The unique key to identify this state value within the message's componentState object
|
|
11
|
+
* @param initialValue - Optional initial value for the state, used if no componentState value exists in the Tambo message containing this hook usage.
|
|
12
|
+
* @param setFromProp - Optional value used to set the state value, only while no componentState value exists in the Tambo message containing this hook usage. Use this to allow streaming updates from a prop to the state value.
|
|
13
|
+
* @param debounceTime - Optional debounce time in milliseconds (default: 500ms) to limit API calls.
|
|
14
|
+
* @returns A tuple containing:
|
|
15
|
+
* - The current state value
|
|
16
|
+
* - A setter function to update the state (updates UI immediately, debounces server sync)
|
|
17
|
+
* @example
|
|
18
|
+
* const [count, setCount] = useTamboComponentState("counter", 0);
|
|
4
19
|
*
|
|
5
20
|
* Use `setFromProp` to seed state from streamed props. During streaming,
|
|
6
21
|
* state updates as new prop values arrive. Once streaming completes,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-component-state.d.ts","sourceRoot":"","sources":["../../src/hooks/use-component-state.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-component-state.d.ts","sourceRoot":"","sources":["../../src/hooks/use-component-state.tsx"],"names":[],"mappings":"AAOA,KAAK,iBAAiB,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,GAAG,SAAS,EAClD,OAAO,EAAE,MAAM,EACf,YAAY,CAAC,EAAE,CAAC,EAChB,WAAW,CAAC,EAAE,CAAC,EACf,YAAY,CAAC,EAAE,MAAM,GACpB,iBAAiB,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AACpC,wBAAgB,sBAAsB,CAAC,CAAC,EACtC,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,CAAC,EACf,WAAW,CAAC,EAAE,CAAC,EACf,YAAY,CAAC,EAAE,MAAM,GACpB,iBAAiB,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { useCallback, useContext, useEffect, useState } from "react";
|
|
3
3
|
import { useDebouncedCallback } from "use-debounce";
|
|
4
4
|
import { useTamboClient, useTamboThread } from "..";
|
|
5
|
-
import {
|
|
5
|
+
import { useTamboInteractable } from "../providers/tambo-interactable-provider";
|
|
6
|
+
import { TamboMessageContext } from "./use-current-message";
|
|
6
7
|
export function useTamboComponentState(keyName, initialValue, setFromProp, debounceTime = 500) {
|
|
7
|
-
const message =
|
|
8
|
+
const message = useContext(TamboMessageContext);
|
|
8
9
|
const { updateThreadMessage } = useTamboThread();
|
|
9
10
|
const client = useTamboClient();
|
|
11
|
+
const componentId = message?.interactableMetadata?.id ?? null;
|
|
12
|
+
const { setInteractableState, getInteractableComponentState } = useTamboInteractable();
|
|
10
13
|
const messageState = message?.componentState?.[keyName];
|
|
11
|
-
const
|
|
14
|
+
const interactableState = componentId
|
|
15
|
+
? getInteractableComponentState(componentId)?.[keyName]
|
|
16
|
+
: undefined;
|
|
17
|
+
const initialState = interactableState ?? messageState ?? initialValue;
|
|
18
|
+
const [localState, setLocalState] = useState(initialState);
|
|
12
19
|
const [initializedFromThreadMessage, setInitializedFromThreadMessage] = useState(messageState ? true : false);
|
|
13
20
|
// Optimistically update the local thread message's componentState
|
|
14
21
|
const updateLocalThreadMessage = useCallback(async (newState, existingMessage) => {
|
|
22
|
+
if (!existingMessage) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
15
25
|
const updatedMessage = {
|
|
16
26
|
threadId: existingMessage.threadId,
|
|
17
27
|
componentState: {
|
|
@@ -23,6 +33,9 @@ export function useTamboComponentState(keyName, initialValue, setFromProp, debou
|
|
|
23
33
|
}, [updateThreadMessage, keyName]);
|
|
24
34
|
// Debounced callback to update the remote thread message's componentState
|
|
25
35
|
const updateRemoteThreadMessage = useDebouncedCallback(async (newState, existingMessage) => {
|
|
36
|
+
if (!existingMessage) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
26
39
|
await client.beta.threads.messages.updateComponentState(existingMessage.id, {
|
|
27
40
|
id: existingMessage.threadId,
|
|
28
41
|
state: { [keyName]: newState },
|
|
@@ -30,26 +43,73 @@ export function useTamboComponentState(keyName, initialValue, setFromProp, debou
|
|
|
30
43
|
}, debounceTime);
|
|
31
44
|
const setValue = useCallback((newState) => {
|
|
32
45
|
setLocalState(newState);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
if (componentId) {
|
|
47
|
+
// For interactable components, update the interactable provider's state
|
|
48
|
+
setInteractableState(componentId, keyName, newState);
|
|
49
|
+
}
|
|
50
|
+
else if (message) {
|
|
51
|
+
// For generated components, update both local and remote thread message state
|
|
52
|
+
void updateLocalThreadMessage(newState, message);
|
|
53
|
+
void updateRemoteThreadMessage(newState, message);
|
|
54
|
+
}
|
|
55
|
+
}, [
|
|
56
|
+
message,
|
|
57
|
+
updateLocalThreadMessage,
|
|
58
|
+
updateRemoteThreadMessage,
|
|
59
|
+
setInteractableState,
|
|
60
|
+
componentId,
|
|
61
|
+
keyName,
|
|
62
|
+
]);
|
|
63
|
+
const existingInteractableState = componentId
|
|
64
|
+
? getInteractableComponentState(componentId)?.[keyName]
|
|
65
|
+
: undefined;
|
|
66
|
+
const shouldUpdateInteractableInitial = !!componentId &&
|
|
67
|
+
existingInteractableState === undefined &&
|
|
68
|
+
initialValue !== undefined;
|
|
69
|
+
// Set initial value in interactable state if we're in an interactable context and there's no existing state
|
|
37
70
|
useEffect(() => {
|
|
38
|
-
|
|
39
|
-
|
|
71
|
+
if (!shouldUpdateInteractableInitial) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
setInteractableState(componentId, keyName, initialValue);
|
|
75
|
+
}, [
|
|
76
|
+
shouldUpdateInteractableInitial,
|
|
77
|
+
componentId,
|
|
78
|
+
keyName,
|
|
79
|
+
initialValue,
|
|
80
|
+
setInteractableState,
|
|
81
|
+
]);
|
|
82
|
+
const shouldSyncFromMessage = !!message && messageState !== undefined && messageState !== null;
|
|
83
|
+
// Mirror the thread message's componentState value to the local state and interactable state
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!shouldSyncFromMessage) {
|
|
40
86
|
return;
|
|
41
87
|
}
|
|
42
88
|
setInitializedFromThreadMessage(true);
|
|
43
|
-
|
|
44
|
-
|
|
89
|
+
const stateValue = messageState;
|
|
90
|
+
setLocalState(stateValue);
|
|
91
|
+
if (componentId) {
|
|
92
|
+
setInteractableState(componentId, keyName, stateValue);
|
|
93
|
+
}
|
|
94
|
+
}, [
|
|
95
|
+
shouldSyncFromMessage,
|
|
96
|
+
messageState,
|
|
97
|
+
keyName,
|
|
98
|
+
setInteractableState,
|
|
99
|
+
componentId,
|
|
100
|
+
]);
|
|
45
101
|
// For editable fields that are set from a prop to allow streaming updates, don't overwrite a fetched state value set from the thread message with prop value on initial load.
|
|
46
102
|
useEffect(() => {
|
|
47
103
|
if (setFromProp !== undefined && !initializedFromThreadMessage) {
|
|
48
104
|
setLocalState(setFromProp);
|
|
49
105
|
}
|
|
50
106
|
}, [setFromProp, initializedFromThreadMessage]);
|
|
51
|
-
// Ensure pending changes are flushed on unmount
|
|
107
|
+
// Ensure pending changes are flushed on unmount (only for generated components)
|
|
52
108
|
useEffect(() => {
|
|
109
|
+
// Only flush remote updates for generated components, not interactable components
|
|
110
|
+
if (componentId) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
53
113
|
return () => {
|
|
54
114
|
async function flushUpdates() {
|
|
55
115
|
try {
|
|
@@ -62,7 +122,7 @@ export function useTamboComponentState(keyName, initialValue, setFromProp, debou
|
|
|
62
122
|
// Fire-and-forget cleanup (errors handled inside)
|
|
63
123
|
void flushUpdates();
|
|
64
124
|
};
|
|
65
|
-
}, [updateRemoteThreadMessage]);
|
|
125
|
+
}, [updateRemoteThreadMessage, componentId]);
|
|
66
126
|
return [localState, setValue];
|
|
67
127
|
}
|
|
68
128
|
//# sourceMappingURL=use-component-state.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-component-state.js","sourceRoot":"","sources":["../../src/hooks/use-component-state.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAsB,cAAc,EAAE,cAAc,EAAE,MAAM,IAAI,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AA+B/D,MAAM,UAAU,sBAAsB,CACpC,OAAe,EACf,YAAgB,EAChB,WAAe,EACf,YAAY,GAAG,GAAG;IAElB,MAAM,OAAO,GAAG,sBAAsB,EAAE,CAAC;IACzC,MAAM,EAAE,mBAAmB,EAAE,GAAG,cAAc,EAAE,CAAC;IACjD,MAAM,MAAM,GAAG,cAA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useState } from \"react\";\nimport { useDebouncedCallback } from \"use-debounce\";\nimport { TamboThreadMessage, useTamboClient, useTamboThread } from \"..\";\nimport { useTamboCurrentMessage } from \"./use-current-message\";\n\ntype StateUpdateResult<T> = [currentState: T, setState: (newState: T) => void];\n\n/**\n * Like useState, but syncs to the thread message's `componentState`.\n *\n * Use `setFromProp` to seed state from streamed props. During streaming,\n * state updates as new prop values arrive. Once streaming completes,\n * user edits take precedence over the original prop value.\n *\n * Pair with `useTamboStreamStatus` to disable inputs while streaming.\n * @see {@link https://docs.tambo.co/concepts/streaming/streaming-best-practices}\n * @param keyName - Unique key within the message's componentState\n * @param initialValue - Default value if no componentState exists\n * @param setFromProp - Seeds state from props (updates during streaming, then user edits take over)\n * @param debounceTime - Server sync debounce in ms (default: 500)\n * @returns A tuple of [currentState, setState] similar to React's useState\n */\nexport function useTamboComponentState<S = undefined>(\n keyName: string,\n initialValue?: S,\n setFromProp?: S,\n debounceTime?: number,\n): StateUpdateResult<S | undefined>;\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue: S,\n setFromProp?: S,\n debounceTime?: number,\n): StateUpdateResult<S>;\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue?: S,\n setFromProp?: S,\n debounceTime = 500,\n): StateUpdateResult<S> {\n const message = useTamboCurrentMessage();\n const { updateThreadMessage } = useTamboThread();\n const client = useTamboClient();\n const messageState = message?.componentState?.[keyName];\n const [localState, setLocalState] = useState<S | undefined>(\n (messageState as S) ?? initialValue,\n );\n const [initializedFromThreadMessage, setInitializedFromThreadMessage] =\n useState(messageState ? true : false);\n\n // Optimistically update the local thread message's componentState\n const updateLocalThreadMessage = useCallback(\n async (newState: S, existingMessage: TamboThreadMessage) => {\n const updatedMessage = {\n threadId: existingMessage.threadId,\n componentState: {\n ...existingMessage.componentState,\n [keyName]: newState,\n },\n };\n await updateThreadMessage(existingMessage.id, updatedMessage, false);\n },\n [updateThreadMessage, keyName],\n );\n\n // Debounced callback to update the remote thread message's componentState\n const updateRemoteThreadMessage = useDebouncedCallback(\n async (newState: S, existingMessage: TamboThreadMessage) => {\n await client.beta.threads.messages.updateComponentState(\n existingMessage.id,\n {\n id: existingMessage.threadId,\n state: { [keyName]: newState },\n },\n );\n },\n debounceTime,\n );\n\n const setValue = useCallback(\n (newState: S) => {\n setLocalState(newState);\n void updateLocalThreadMessage(newState, message);\n void updateRemoteThreadMessage(newState, message);\n },\n [message, updateLocalThreadMessage, updateRemoteThreadMessage],\n );\n\n // Mirror the thread message's componentState value to the local state\n useEffect(() => {\n const messageState = message?.componentState?.[keyName];\n if (!messageState) {\n return;\n }\n setInitializedFromThreadMessage(true);\n setLocalState(message.componentState?.[keyName] as S);\n }, [message?.componentState?.[keyName], message, keyName]);\n\n // For editable fields that are set from a prop to allow streaming updates, don't overwrite a fetched state value set from the thread message with prop value on initial load.\n useEffect(() => {\n if (setFromProp !== undefined && !initializedFromThreadMessage) {\n setLocalState(setFromProp as S);\n }\n }, [setFromProp, initializedFromThreadMessage]);\n\n // Ensure pending changes are flushed on unmount\n useEffect(() => {\n return () => {\n async function flushUpdates() {\n try {\n await updateRemoteThreadMessage.flush();\n } catch (error) {\n console.error(\n \"Failed to flush pending thread message updates:\",\n error,\n );\n }\n }\n // Fire-and-forget cleanup (errors handled inside)\n void flushUpdates();\n };\n }, [updateRemoteThreadMessage]);\n\n return [localState as S, setValue];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"use-component-state.js","sourceRoot":"","sources":["../../src/hooks/use-component-state.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAsB,cAAc,EAAE,cAAc,EAAE,MAAM,IAAI,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,0CAA0C,CAAC;AAChF,OAAO,EAAE,mBAA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AAM,yBAAyB,CAAC,KAAK,EAAE,CAAC;gBAC1C,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CACX,iDAAiD,EACjD,KAAK,CACN,CAAC;gBACJ,CAAC;YACH,CAAC;YACD,kDAAkD;YAClD,KAAK,YAAY,EAAE,CAAC;QACtB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,yBAAyB,EAAE,WAAW,CAAC,CAAC,CAAC;IAE7C,OAAO,CAAC,UAAe,EAAE,QAAQ,CAAC,CAAC;AACrC,CAAC","sourcesContent":["\"use client\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { useDebouncedCallback } from \"use-debounce\";\nimport { TamboThreadMessage, useTamboClient, useTamboThread } from \"..\";\nimport { useTamboInteractable } from \"../providers/tambo-interactable-provider\";\nimport { TamboMessageContext } from \"./use-current-message\";\n\ntype StateUpdateResult<T> = [currentState: T, setState: (newState: T) => void];\n\n/**\n * A React hook that acts like useState, but also automatically updates the thread message's componentState.\n * If used within an interactable component (wrapped with withTamboInteractable), it updates the\n * interactable provider's global state (sent to Tambo on every request) instead of the remote thread message state.\n * For generated components, it updates both the local and remote thread message's componentState.\n *\n * Benefits: Passes user changes to AI, and when threads are returned, state is preserved.\n * Works in both generative and interactable component contexts.\n * @param keyName - The unique key to identify this state value within the message's componentState object\n * @param initialValue - Optional initial value for the state, used if no componentState value exists in the Tambo message containing this hook usage.\n * @param setFromProp - Optional value used to set the state value, only while no componentState value exists in the Tambo message containing this hook usage. Use this to allow streaming updates from a prop to the state value.\n * @param debounceTime - Optional debounce time in milliseconds (default: 500ms) to limit API calls.\n * @returns A tuple containing:\n * - The current state value\n * - A setter function to update the state (updates UI immediately, debounces server sync)\n * @example\n * const [count, setCount] = useTamboComponentState(\"counter\", 0);\n *\n * Use `setFromProp` to seed state from streamed props. During streaming,\n * state updates as new prop values arrive. Once streaming completes,\n * user edits take precedence over the original prop value.\n *\n * Pair with `useTamboStreamStatus` to disable inputs while streaming.\n * @see {@link https://docs.tambo.co/concepts/streaming/streaming-best-practices}\n * @param keyName - Unique key within the message's componentState\n * @param initialValue - Default value if no componentState exists\n * @param setFromProp - Seeds state from props (updates during streaming, then user edits take over)\n * @param debounceTime - Server sync debounce in ms (default: 500)\n * @returns A tuple of [currentState, setState] similar to React's useState\n */\nexport function useTamboComponentState<S = undefined>(\n keyName: string,\n initialValue?: S,\n setFromProp?: S,\n debounceTime?: number,\n): StateUpdateResult<S | undefined>;\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue: S,\n setFromProp?: S,\n debounceTime?: number,\n): StateUpdateResult<S>;\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue?: S,\n setFromProp?: S,\n debounceTime = 500,\n): StateUpdateResult<S> {\n const message = useContext(TamboMessageContext);\n const { updateThreadMessage } = useTamboThread();\n const client = useTamboClient();\n const componentId = message?.interactableMetadata?.id ?? null;\n const { setInteractableState, getInteractableComponentState } =\n useTamboInteractable();\n const messageState = message?.componentState?.[keyName];\n const interactableState = componentId\n ? getInteractableComponentState(componentId)?.[keyName]\n : undefined;\n const initialState =\n (interactableState as S) ?? (messageState as S) ?? initialValue;\n const [localState, setLocalState] = useState<S | undefined>(initialState);\n const [initializedFromThreadMessage, setInitializedFromThreadMessage] =\n useState(messageState ? true : false);\n\n // Optimistically update the local thread message's componentState\n const updateLocalThreadMessage = useCallback(\n async (newState: S, existingMessage: TamboThreadMessage | null) => {\n if (!existingMessage) {\n return;\n }\n const updatedMessage = {\n threadId: existingMessage.threadId,\n componentState: {\n ...existingMessage.componentState,\n [keyName]: newState,\n },\n };\n await updateThreadMessage(existingMessage.id, updatedMessage, false);\n },\n [updateThreadMessage, keyName],\n );\n\n // Debounced callback to update the remote thread message's componentState\n const updateRemoteThreadMessage = useDebouncedCallback(\n async (newState: S, existingMessage: TamboThreadMessage | null) => {\n if (!existingMessage) {\n return;\n }\n await client.beta.threads.messages.updateComponentState(\n existingMessage.id,\n {\n id: existingMessage.threadId,\n state: { [keyName]: newState },\n },\n );\n },\n debounceTime,\n );\n\n const setValue = useCallback(\n (newState: S) => {\n setLocalState(newState);\n if (componentId) {\n // For interactable components, update the interactable provider's state\n setInteractableState(componentId, keyName, newState);\n } else if (message) {\n // For generated components, update both local and remote thread message state\n void updateLocalThreadMessage(newState, message);\n void updateRemoteThreadMessage(newState, message);\n }\n },\n [\n message,\n updateLocalThreadMessage,\n updateRemoteThreadMessage,\n setInteractableState,\n componentId,\n keyName,\n ],\n );\n\n const existingInteractableState = componentId\n ? getInteractableComponentState(componentId)?.[keyName]\n : undefined;\n const shouldUpdateInteractableInitial =\n !!componentId &&\n existingInteractableState === undefined &&\n initialValue !== undefined;\n\n // Set initial value in interactable state if we're in an interactable context and there's no existing state\n useEffect(() => {\n if (!shouldUpdateInteractableInitial) {\n return;\n }\n setInteractableState(componentId, keyName, initialValue!);\n }, [\n shouldUpdateInteractableInitial,\n componentId,\n keyName,\n initialValue,\n setInteractableState,\n ]);\n\n const shouldSyncFromMessage =\n !!message && messageState !== undefined && messageState !== null;\n\n // Mirror the thread message's componentState value to the local state and interactable state\n useEffect(() => {\n if (!shouldSyncFromMessage) {\n return;\n }\n setInitializedFromThreadMessage(true);\n const stateValue = messageState as S;\n setLocalState(stateValue);\n if (componentId) {\n setInteractableState(componentId, keyName, stateValue);\n }\n }, [\n shouldSyncFromMessage,\n messageState,\n keyName,\n setInteractableState,\n componentId,\n ]);\n\n // For editable fields that are set from a prop to allow streaming updates, don't overwrite a fetched state value set from the thread message with prop value on initial load.\n useEffect(() => {\n if (setFromProp !== undefined && !initializedFromThreadMessage) {\n setLocalState(setFromProp as S);\n }\n }, [setFromProp, initializedFromThreadMessage]);\n\n // Ensure pending changes are flushed on unmount (only for generated components)\n useEffect(() => {\n // Only flush remote updates for generated components, not interactable components\n if (componentId) {\n return;\n }\n return () => {\n async function flushUpdates() {\n try {\n await updateRemoteThreadMessage.flush();\n } catch (error) {\n console.error(\n \"Failed to flush pending thread message updates:\",\n error,\n );\n }\n }\n // Fire-and-forget cleanup (errors handled inside)\n void flushUpdates();\n };\n }, [updateRemoteThreadMessage, componentId]);\n\n return [localState as S, setValue];\n}\n"]}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import React from "react";
|
|
2
3
|
import { useTamboClient } from "../providers/tambo-client-provider";
|
|
4
|
+
import { useTamboInteractable } from "../providers/tambo-interactable-provider";
|
|
3
5
|
import { useTamboThread } from "../providers/tambo-thread-provider";
|
|
4
6
|
import { useTamboComponentState } from "./use-component-state";
|
|
5
|
-
import { useTamboCurrentMessage } from "./use-current-message";
|
|
7
|
+
import { TamboMessageContext, useTamboCurrentMessage, } from "./use-current-message";
|
|
6
8
|
// Mock the required providers
|
|
7
9
|
jest.mock("../providers/tambo-client-provider", () => ({
|
|
8
10
|
useTamboClient: jest.fn(),
|
|
@@ -12,6 +14,10 @@ jest.mock("../providers/tambo-thread-provider", () => ({
|
|
|
12
14
|
}));
|
|
13
15
|
jest.mock("./use-current-message", () => ({
|
|
14
16
|
useTamboCurrentMessage: jest.fn(),
|
|
17
|
+
TamboMessageContext: React.createContext(null),
|
|
18
|
+
}));
|
|
19
|
+
jest.mock("../providers/tambo-interactable-provider", () => ({
|
|
20
|
+
useTamboInteractable: jest.fn(),
|
|
15
21
|
}));
|
|
16
22
|
// Create a mock debounced function with flush method
|
|
17
23
|
const createMockDebouncedFunction = (fn) => {
|
|
@@ -40,8 +46,34 @@ describe("useTamboComponentState", () => {
|
|
|
40
46
|
});
|
|
41
47
|
const mockUpdateThreadMessage = jest.fn();
|
|
42
48
|
const mockUpdateComponentState = jest.fn();
|
|
49
|
+
const mockSetInteractableState = jest.fn();
|
|
50
|
+
const mockGetInteractableComponentState = jest.fn();
|
|
51
|
+
// Track context values for mocking
|
|
52
|
+
let mockMessage = null;
|
|
43
53
|
beforeEach(() => {
|
|
44
54
|
jest.clearAllMocks();
|
|
55
|
+
// Reset context values
|
|
56
|
+
mockMessage = createMockMessage();
|
|
57
|
+
// Mock useContext to return appropriate values based on context
|
|
58
|
+
const originalUseContext = React.useContext;
|
|
59
|
+
jest.spyOn(React, "useContext").mockImplementation((context) => {
|
|
60
|
+
if (context === TamboMessageContext) {
|
|
61
|
+
// Return the message from useTamboCurrentMessage mock if available
|
|
62
|
+
try {
|
|
63
|
+
const currentMessageMock = jest.mocked(useTamboCurrentMessage);
|
|
64
|
+
const mockImpl = currentMessageMock.getMockImplementation();
|
|
65
|
+
if (mockImpl) {
|
|
66
|
+
return mockImpl();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Fallback to mockMessage
|
|
71
|
+
}
|
|
72
|
+
return mockMessage;
|
|
73
|
+
}
|
|
74
|
+
// For other contexts, use the original implementation
|
|
75
|
+
return originalUseContext(context);
|
|
76
|
+
});
|
|
45
77
|
// Setup default mock for useDebouncedCallback
|
|
46
78
|
jest
|
|
47
79
|
.mocked(useDebouncedCallback)
|
|
@@ -60,6 +92,13 @@ describe("useTamboComponentState", () => {
|
|
|
60
92
|
updateThreadMessage: mockUpdateThreadMessage,
|
|
61
93
|
});
|
|
62
94
|
jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage());
|
|
95
|
+
jest.mocked(useTamboInteractable).mockReturnValue({
|
|
96
|
+
setInteractableState: mockSetInteractableState,
|
|
97
|
+
getInteractableComponentState: mockGetInteractableComponentState,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
afterEach(() => {
|
|
101
|
+
jest.restoreAllMocks();
|
|
63
102
|
});
|
|
64
103
|
describe("Initial State Management", () => {
|
|
65
104
|
it("should initialize with initialValue when no componentState exists", () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-component-state.test.js","sourceRoot":"","sources":["../../src/hooks/use-component-state.test.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEzD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,EAAE,EAAE,OAAO,EAAE,WAAW,EAAE;aACzC,CAAC,CAAC;YACH,MAAM,QAAQ,GAAG,iBAAiB,CAAC;gBACjC,EAAE,EAAE,UAAU;gBACd,cAAc,EAAE,EAAE,OAAO,EAAE,WAAW,EAAE;aACzC,CAAC,CAAC;YAEH,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,UAAU,CACrC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;gBACd,IAAI,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;gBAC7D,OAAO,sBAAsB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;YACtD,CAAC,EACD,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,CACxC,CAAC;YAEF,uBAAuB;YACvB,uBAAuB,CAAC,SAAS,EAAE,CAAC;YAEpC,oDAAoD;YACpD,QAAQ,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;YAEhC,wCAAwC;YACxC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook } from \"@testing-library/react\";\nimport { TamboThreadMessage } from \"../model/generate-component-response\";\nimport { useTamboClient } from \"../providers/tambo-client-provider\";\nimport { useTamboThread } from \"../providers/tambo-thread-provider\";\nimport { PartialTamboAI } from \"../testing/types\";\nimport { useTamboComponentState } from \"./use-component-state\";\nimport { useTamboCurrentMessage } from \"./use-current-message\";\n\n// Mock the required providers\njest.mock(\"../providers/tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n}));\n\njest.mock(\"../providers/tambo-thread-provider\", () => ({\n useTamboThread: jest.fn(),\n}));\n\njest.mock(\"./use-current-message\", () => ({\n useTamboCurrentMessage: jest.fn(),\n}));\n\n// Create a mock debounced function with flush method\nconst createMockDebouncedFunction = (fn: any) => {\n const debouncedFn = jest.fn((...args: any[]) => fn(...args)) 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 useDebouncedCallback\nimport { useDebouncedCallback } from \"use-debounce\";\n\ndescribe(\"useTamboComponentState\", () => {\n // Helper function to create mock TamboThreadMessage\n const createMockMessage = (\n overrides: Partial<TamboThreadMessage> = {},\n ): TamboThreadMessage => ({\n id: \"test-message-id\",\n threadId: \"test-thread-id\",\n componentState: {},\n content: [{ type: \"text\", text: \"Test message\" }],\n createdAt: new Date().toISOString(),\n role: \"assistant\",\n ...overrides,\n });\n\n const mockUpdateThreadMessage = jest.fn();\n const mockUpdateComponentState = jest.fn();\n\n beforeEach(() => {\n jest.clearAllMocks();\n\n // Setup default mock for useDebouncedCallback\n jest\n .mocked(useDebouncedCallback)\n .mockImplementation((fn) => createMockDebouncedFunction(fn));\n\n // Setup default mocks\n jest.mocked(useTamboClient).mockReturnValue({\n beta: {\n threads: {\n messages: {\n updateComponentState: mockUpdateComponentState,\n },\n },\n },\n } satisfies PartialTamboAI as any);\n\n jest.mocked(useTamboThread).mockReturnValue({\n updateThreadMessage: mockUpdateThreadMessage,\n } as any);\n\n jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage());\n });\n\n describe(\"Initial State Management\", () => {\n it(\"should initialize with initialValue when no componentState exists\", () => {\n const initialValue = \"test-initial\";\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", initialValue),\n );\n\n expect(result.current[0]).toBe(initialValue);\n });\n\n it(\"should use existing componentState value over initialValue\", () => {\n const initialValue = \"initial\";\n const existingValue = \"existing\";\n jest.mocked(useTamboCurrentMessage).mockReturnValue(\n createMockMessage({\n componentState: { testKey: existingValue },\n }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", initialValue),\n );\n\n expect(result.current[0]).toBe(existingValue);\n });\n\n it(\"should handle undefined initialValue gracefully\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result } = renderHook(() => useTamboComponentState(\"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.mocked(useTamboCurrentMessage).mockReturnValue(\n createMockMessage({\n componentState: { testKey: value },\n }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"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 setValue is called\", () => {\n const initialValue = \"initial\";\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(\n createMockMessage({ componentState: { testKey: initialValue } }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"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 trigger local thread message update when setValue is called\", () => {\n const message = createMockMessage({\n componentState: { testKey: \"initial\" },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n const newValue = \"updated\";\n act(() => {\n result.current[1](newValue);\n });\n\n expect(mockUpdateThreadMessage).toHaveBeenCalledWith(\n message.id,\n {\n threadId: message.threadId,\n componentState: {\n testKey: newValue,\n },\n },\n false,\n );\n });\n\n it(\"should trigger debounced remote API call when setValue is called\", () => {\n const message = createMockMessage({\n componentState: { testKey: \"initial\" },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n const newValue = \"updated\";\n act(() => {\n result.current[1](newValue);\n });\n\n // The debounced function should be called\n expect(mockUpdateComponentState).toHaveBeenCalledWith(message.id, {\n id: message.threadId,\n state: { testKey: newValue },\n });\n });\n\n it(\"should work with complex objects and arrays\", () => {\n const initialValue = { name: \"test\", items: [1, 2, 3] };\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(\n createMockMessage({ componentState: { testKey: initialValue } }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", 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 expect(mockUpdateThreadMessage).toHaveBeenCalledWith(\n expect.any(String),\n expect.objectContaining({\n componentState: {\n testKey: newValue,\n },\n }),\n false,\n );\n });\n });\n\n describe(\"Debouncing Behavior\", () => {\n it(\"should use default debounce time of 500ms\", () => {\n renderHook(() => useTamboComponentState(\"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 const customDebounceTime = 1000;\n\n renderHook(() =>\n useTamboComponentState(\n \"testKey\",\n \"initial\",\n undefined,\n customDebounceTime,\n ),\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\n // Mock the debounced callback to return our specific mock\n jest.mocked(useDebouncedCallback).mockReturnValue(mockDebouncedFn);\n\n const { unmount } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n unmount();\n\n expect(mockFlush).toHaveBeenCalled();\n });\n });\n\n describe(\"Multi-Hook Scenarios\", () => {\n it(\"should handle multiple hooks with different keyNames independently\", () => {\n const message = createMockMessage({\n componentState: {\n key1: \"value1\",\n key2: \"value2\",\n },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result: result1 } = renderHook(() =>\n useTamboComponentState(\"key1\", \"default1\"),\n );\n const { result: result2 } = renderHook(() =>\n useTamboComponentState(\"key2\", \"default2\"),\n );\n\n expect(result1.current[0]).toBe(\"value1\");\n expect(result2.current[0]).toBe(\"value2\");\n\n // Update first hook\n act(() => {\n result1.current[1](\"updated1\");\n });\n\n expect(result1.current[0]).toBe(\"updated1\");\n expect(result2.current[0]).toBe(\"value2\"); // Should remain unchanged\n });\n\n it(\"should preserve existing componentState when updating another key\", () => {\n const message = createMockMessage({\n componentState: {\n existingKey: \"existing\",\n testKey: \"initial\",\n },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n act(() => {\n result.current[1](\"updated\");\n });\n\n expect(mockUpdateThreadMessage).toHaveBeenCalledWith(\n message.id,\n {\n threadId: message.threadId,\n componentState: {\n existingKey: \"existing\", // Should preserve existing keys\n testKey: \"updated\",\n },\n },\n false,\n );\n });\n });\n\n describe(\"SetFromProp Feature\", () => {\n it(\"should set value from prop when hasSetFromMessage is false\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const propValue = \"from-prop\";\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\", propValue),\n );\n\n // Initially, hasSetFromMessage should be false, so prop value should be used\n expect(result.current[0]).toBe(propValue);\n });\n\n it(\"should ignore setFromProp when initialized from message state\", async () => {\n const existingValue = \"existing\";\n jest.mocked(useTamboCurrentMessage).mockReturnValue(\n createMockMessage({\n componentState: { testKey: existingValue },\n }),\n );\n\n const propValue = \"from-prop\";\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\", propValue),\n );\n\n // Should use existing value from message, not prop value\n await act(async () => {\n await new Promise((resolve) => setTimeout(resolve, 0));\n });\n\n expect(result.current[0]).toBe(existingValue);\n });\n\n it(\"should update state from setFromProp changes when no message state exists\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result, rerender } = renderHook(\n ({ propValue }) =>\n useTamboComponentState(\"testKey\", \"initial\", propValue),\n { initialProps: { propValue: \"prop1\" } },\n );\n\n expect(result.current[0]).toBe(\"prop1\");\n\n // Change prop value\n rerender({ propValue: \"prop2\" });\n\n // Since hasSetFromMessage is still false (no message state),\n // it should update to new prop value\n expect(result.current[0]).toBe(\"prop2\");\n });\n\n it(\"should handle undefined setFromProp gracefully\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\", undefined),\n );\n\n expect(result.current[0]).toBe(\"initial\");\n });\n });\n\n describe(\"Message State Sync\", () => {\n it(\"should sync with message.componentState changes\", () => {\n const { result, rerender } = renderHook(\n ({ message }) => {\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n return useTamboComponentState(\"testKey\", \"initial\");\n },\n {\n initialProps: {\n message: createMockMessage({\n componentState: { testKey: \"value1\" },\n }),\n },\n },\n );\n\n // Change the message\n const newMessage = createMockMessage({\n componentState: { testKey: \"value2\" },\n });\n\n rerender({ message: newMessage });\n\n // The hook should sync with the new message state\n expect(result.current[0]).toBe(\"value2\");\n expect(mockUpdateThreadMessage).not.toHaveBeenCalled();\n });\n\n it(\"should handle message without componentState gracefully\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(\n createMockMessage({ componentState: undefined as any }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n expect(result.current[0]).toBe(\"initial\");\n });\n\n it(\"should preserve state when message updates but componentState[keyName] unchanged\", () => {\n const message1 = createMockMessage({\n id: \"message1\",\n componentState: { testKey: \"unchanged\" },\n });\n const message2 = createMockMessage({\n id: \"message2\",\n componentState: { testKey: \"unchanged\" },\n });\n\n const { result, rerender } = renderHook(\n ({ message }) => {\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n return useTamboComponentState(\"testKey\", \"initial\");\n },\n { initialProps: { message: message1 } },\n );\n\n // Clear previous calls\n mockUpdateThreadMessage.mockClear();\n\n // Change message but keep same componentState value\n rerender({ message: message2 });\n\n // Should preserve the \"unchanged\" value\n expect(result.current[0]).toBe(\"unchanged\");\n });\n });\n});\n"]}
|
|
1
|
+
{"version":3,"file":"use-component-state.test.js","sourceRoot":"","sources":["../../src/hooks/use-component-state.test.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,0CAA0C,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EACL,mBAAmB,EACnB,sBAAsB,GACvB,MAAM,uBAAuB,CAAC;AAE/B,8BAA8B;AAC9B,IAAI,CAAC,IAAI,CAAC,oCAAoC,EAAE,GAAG,EAAE,CAAC,CAAC;IACrD,cAAc,EAAE,IAAI,CAAC,EAAE,EAAE;CAC1B,CAAC,CAAC,CAAC;AAEJ,IAAI,CAAC,IAAI,CAAC,oCAAoC,EAAE,GAAG,EAAE,CAAC,CAAC;IACrD,cAAc,EAAE,IAAI,CAAC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SAAS,CAAC,CAAC;YACtD,CAAC,EACD,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,CACxC,CAAC;YAEF,uBAAuB;YACvB,uBAAuB,CAAC,SAAS,EAAE,CAAC;YAEpC,oDAAoD;YACpD,QAAQ,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;YAEhC,wCAAwC;YACxC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport { TamboThreadMessage } from \"../model/generate-component-response\";\nimport { useTamboClient } from \"../providers/tambo-client-provider\";\nimport { useTamboInteractable } from \"../providers/tambo-interactable-provider\";\nimport { useTamboThread } from \"../providers/tambo-thread-provider\";\nimport { PartialTamboAI } from \"../testing/types\";\nimport { useTamboComponentState } from \"./use-component-state\";\nimport {\n TamboMessageContext,\n useTamboCurrentMessage,\n} from \"./use-current-message\";\n\n// Mock the required providers\njest.mock(\"../providers/tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n}));\n\njest.mock(\"../providers/tambo-thread-provider\", () => ({\n useTamboThread: jest.fn(),\n}));\n\njest.mock(\"./use-current-message\", () => ({\n useTamboCurrentMessage: jest.fn(),\n TamboMessageContext: React.createContext<TamboThreadMessage | null>(null),\n}));\n\njest.mock(\"../providers/tambo-interactable-provider\", () => ({\n useTamboInteractable: jest.fn(),\n}));\n\n// Create a mock debounced function with flush method\nconst createMockDebouncedFunction = (fn: any) => {\n const debouncedFn = jest.fn((...args: any[]) => fn(...args)) 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 useDebouncedCallback\nimport { useDebouncedCallback } from \"use-debounce\";\n\ndescribe(\"useTamboComponentState\", () => {\n // Helper function to create mock TamboThreadMessage\n const createMockMessage = (\n overrides: Partial<TamboThreadMessage> = {},\n ): TamboThreadMessage => ({\n id: \"test-message-id\",\n threadId: \"test-thread-id\",\n componentState: {},\n content: [{ type: \"text\", text: \"Test message\" }],\n createdAt: new Date().toISOString(),\n role: \"assistant\",\n ...overrides,\n });\n\n const mockUpdateThreadMessage = jest.fn();\n const mockUpdateComponentState = jest.fn();\n const mockSetInteractableState = jest.fn();\n const mockGetInteractableComponentState = jest.fn();\n\n // Track context values for mocking\n let mockMessage: TamboThreadMessage | null = null;\n\n beforeEach(() => {\n jest.clearAllMocks();\n\n // Reset context values\n mockMessage = createMockMessage();\n\n // Mock useContext to return appropriate values based on context\n const originalUseContext = React.useContext;\n jest.spyOn(React, \"useContext\").mockImplementation((context) => {\n if (context === TamboMessageContext) {\n // Return the message from useTamboCurrentMessage mock if available\n try {\n const currentMessageMock = jest.mocked(useTamboCurrentMessage);\n const mockImpl = currentMessageMock.getMockImplementation();\n if (mockImpl) {\n return mockImpl();\n }\n } catch {\n // Fallback to mockMessage\n }\n return mockMessage;\n }\n // For other contexts, use the original implementation\n return originalUseContext(context);\n });\n\n // Setup default mock for useDebouncedCallback\n jest\n .mocked(useDebouncedCallback)\n .mockImplementation((fn) => createMockDebouncedFunction(fn));\n\n // Setup default mocks\n jest.mocked(useTamboClient).mockReturnValue({\n beta: {\n threads: {\n messages: {\n updateComponentState: mockUpdateComponentState,\n },\n },\n },\n } satisfies PartialTamboAI as any);\n\n jest.mocked(useTamboThread).mockReturnValue({\n updateThreadMessage: mockUpdateThreadMessage,\n } as any);\n\n jest.mocked(useTamboCurrentMessage).mockReturnValue(createMockMessage());\n\n jest.mocked(useTamboInteractable).mockReturnValue({\n setInteractableState: mockSetInteractableState,\n getInteractableComponentState: mockGetInteractableComponentState,\n } as any);\n });\n\n afterEach(() => {\n jest.restoreAllMocks();\n });\n\n describe(\"Initial State Management\", () => {\n it(\"should initialize with initialValue when no componentState exists\", () => {\n const initialValue = \"test-initial\";\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", initialValue),\n );\n\n expect(result.current[0]).toBe(initialValue);\n });\n\n it(\"should use existing componentState value over initialValue\", () => {\n const initialValue = \"initial\";\n const existingValue = \"existing\";\n jest.mocked(useTamboCurrentMessage).mockReturnValue(\n createMockMessage({\n componentState: { testKey: existingValue },\n }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", initialValue),\n );\n\n expect(result.current[0]).toBe(existingValue);\n });\n\n it(\"should handle undefined initialValue gracefully\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result } = renderHook(() => useTamboComponentState(\"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.mocked(useTamboCurrentMessage).mockReturnValue(\n createMockMessage({\n componentState: { testKey: value },\n }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"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 setValue is called\", () => {\n const initialValue = \"initial\";\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(\n createMockMessage({ componentState: { testKey: initialValue } }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"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 trigger local thread message update when setValue is called\", () => {\n const message = createMockMessage({\n componentState: { testKey: \"initial\" },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n const newValue = \"updated\";\n act(() => {\n result.current[1](newValue);\n });\n\n expect(mockUpdateThreadMessage).toHaveBeenCalledWith(\n message.id,\n {\n threadId: message.threadId,\n componentState: {\n testKey: newValue,\n },\n },\n false,\n );\n });\n\n it(\"should trigger debounced remote API call when setValue is called\", () => {\n const message = createMockMessage({\n componentState: { testKey: \"initial\" },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n const newValue = \"updated\";\n act(() => {\n result.current[1](newValue);\n });\n\n // The debounced function should be called\n expect(mockUpdateComponentState).toHaveBeenCalledWith(message.id, {\n id: message.threadId,\n state: { testKey: newValue },\n });\n });\n\n it(\"should work with complex objects and arrays\", () => {\n const initialValue = { name: \"test\", items: [1, 2, 3] };\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(\n createMockMessage({ componentState: { testKey: initialValue } }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", 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 expect(mockUpdateThreadMessage).toHaveBeenCalledWith(\n expect.any(String),\n expect.objectContaining({\n componentState: {\n testKey: newValue,\n },\n }),\n false,\n );\n });\n });\n\n describe(\"Debouncing Behavior\", () => {\n it(\"should use default debounce time of 500ms\", () => {\n renderHook(() => useTamboComponentState(\"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 const customDebounceTime = 1000;\n\n renderHook(() =>\n useTamboComponentState(\n \"testKey\",\n \"initial\",\n undefined,\n customDebounceTime,\n ),\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\n // Mock the debounced callback to return our specific mock\n jest.mocked(useDebouncedCallback).mockReturnValue(mockDebouncedFn);\n\n const { unmount } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n unmount();\n\n expect(mockFlush).toHaveBeenCalled();\n });\n });\n\n describe(\"Multi-Hook Scenarios\", () => {\n it(\"should handle multiple hooks with different keyNames independently\", () => {\n const message = createMockMessage({\n componentState: {\n key1: \"value1\",\n key2: \"value2\",\n },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result: result1 } = renderHook(() =>\n useTamboComponentState(\"key1\", \"default1\"),\n );\n const { result: result2 } = renderHook(() =>\n useTamboComponentState(\"key2\", \"default2\"),\n );\n\n expect(result1.current[0]).toBe(\"value1\");\n expect(result2.current[0]).toBe(\"value2\");\n\n // Update first hook\n act(() => {\n result1.current[1](\"updated1\");\n });\n\n expect(result1.current[0]).toBe(\"updated1\");\n expect(result2.current[0]).toBe(\"value2\"); // Should remain unchanged\n });\n\n it(\"should preserve existing componentState when updating another key\", () => {\n const message = createMockMessage({\n componentState: {\n existingKey: \"existing\",\n testKey: \"initial\",\n },\n });\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n act(() => {\n result.current[1](\"updated\");\n });\n\n expect(mockUpdateThreadMessage).toHaveBeenCalledWith(\n message.id,\n {\n threadId: message.threadId,\n componentState: {\n existingKey: \"existing\", // Should preserve existing keys\n testKey: \"updated\",\n },\n },\n false,\n );\n });\n });\n\n describe(\"SetFromProp Feature\", () => {\n it(\"should set value from prop when hasSetFromMessage is false\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const propValue = \"from-prop\";\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\", propValue),\n );\n\n // Initially, hasSetFromMessage should be false, so prop value should be used\n expect(result.current[0]).toBe(propValue);\n });\n\n it(\"should ignore setFromProp when initialized from message state\", async () => {\n const existingValue = \"existing\";\n jest.mocked(useTamboCurrentMessage).mockReturnValue(\n createMockMessage({\n componentState: { testKey: existingValue },\n }),\n );\n\n const propValue = \"from-prop\";\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\", propValue),\n );\n\n // Should use existing value from message, not prop value\n await act(async () => {\n await new Promise((resolve) => setTimeout(resolve, 0));\n });\n\n expect(result.current[0]).toBe(existingValue);\n });\n\n it(\"should update state from setFromProp changes when no message state exists\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result, rerender } = renderHook(\n ({ propValue }) =>\n useTamboComponentState(\"testKey\", \"initial\", propValue),\n { initialProps: { propValue: \"prop1\" } },\n );\n\n expect(result.current[0]).toBe(\"prop1\");\n\n // Change prop value\n rerender({ propValue: \"prop2\" });\n\n // Since hasSetFromMessage is still false (no message state),\n // it should update to new prop value\n expect(result.current[0]).toBe(\"prop2\");\n });\n\n it(\"should handle undefined setFromProp gracefully\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(createMockMessage({ componentState: {} }));\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\", undefined),\n );\n\n expect(result.current[0]).toBe(\"initial\");\n });\n });\n\n describe(\"Message State Sync\", () => {\n it(\"should sync with message.componentState changes\", () => {\n const { result, rerender } = renderHook(\n ({ message }) => {\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n return useTamboComponentState(\"testKey\", \"initial\");\n },\n {\n initialProps: {\n message: createMockMessage({\n componentState: { testKey: \"value1\" },\n }),\n },\n },\n );\n\n // Change the message\n const newMessage = createMockMessage({\n componentState: { testKey: \"value2\" },\n });\n\n rerender({ message: newMessage });\n\n // The hook should sync with the new message state\n expect(result.current[0]).toBe(\"value2\");\n expect(mockUpdateThreadMessage).not.toHaveBeenCalled();\n });\n\n it(\"should handle message without componentState gracefully\", () => {\n jest\n .mocked(useTamboCurrentMessage)\n .mockReturnValue(\n createMockMessage({ componentState: undefined as any }),\n );\n\n const { result } = renderHook(() =>\n useTamboComponentState(\"testKey\", \"initial\"),\n );\n\n expect(result.current[0]).toBe(\"initial\");\n });\n\n it(\"should preserve state when message updates but componentState[keyName] unchanged\", () => {\n const message1 = createMockMessage({\n id: \"message1\",\n componentState: { testKey: \"unchanged\" },\n });\n const message2 = createMockMessage({\n id: \"message2\",\n componentState: { testKey: \"unchanged\" },\n });\n\n const { result, rerender } = renderHook(\n ({ message }) => {\n jest.mocked(useTamboCurrentMessage).mockReturnValue(message);\n return useTamboComponentState(\"testKey\", \"initial\");\n },\n { initialProps: { message: message1 } },\n );\n\n // Clear previous calls\n mockUpdateThreadMessage.mockClear();\n\n // Change message but keep same componentState value\n rerender({ message: message2 });\n\n // Should preserve the \"unchanged\" value\n expect(result.current[0]).toBe(\"unchanged\");\n });\n });\n});\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { InteractableMetadata, TamboThreadMessage } from "../model/generate-component-response";
|
|
3
|
+
export declare const TamboMessageContext: React.Context<TamboThreadMessage | null>;
|
|
3
4
|
export interface TamboMessageProviderProps {
|
|
4
5
|
children: React.ReactNode;
|
|
5
6
|
message: TamboThreadMessage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-current-message.d.ts","sourceRoot":"","sources":["../../src/hooks/use-current-message.tsx"],"names":[],"mappings":"AACA,OAAO,KAAoC,MAAM,OAAO,CAAC;AACzD,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"use-current-message.d.ts","sourceRoot":"","sources":["../../src/hooks/use-current-message.tsx"],"names":[],"mappings":"AACA,OAAO,KAAoC,MAAM,OAAO,CAAC;AACzD,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,sCAAsC,CAAC;AAE9C,eAAO,MAAM,mBAAmB,0CAE/B,CAAC;AAEF,MAAM,WAAW,yBAAyB;IACxC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,OAAO,EAAE,kBAAkB,CAAC;IAC5B,kFAAkF;IAClF,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,EAAE,KAAK,CAAC,EAAE,CAAC,yBAAyB,CAkBpE,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,QAAQ,EAAE,KAAK,CAAC,SAAS,EACzB,OAAO,EAAE,kBAAkB,EAC3B,oBAAoB,CAAC,EAAE,oBAAoB,qBAU5C;AAED;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,0BAQlC,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,sCAAsC;IACtC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,kFAAkF;IAClF,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,8EAA8E;IAC9E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,uFAAuF;IACvF,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,wBAAwB,QAAO,qBAAqB,GAAG,IAiBnE,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import React, { createContext, useContext } from "react";
|
|
3
|
-
const TamboMessageContext = createContext(null);
|
|
3
|
+
export const TamboMessageContext = createContext(null);
|
|
4
4
|
/**
|
|
5
5
|
* Wraps all components, so that they can find what message they are in.
|
|
6
6
|
* Also supports optional interactable metadata for components wrapped with withInteractable.
|