@tambo-ai/react 0.42.0 → 0.43.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/__tests__/context-helpers-provider.test.d.ts +15 -0
- package/dist/context-helpers/__tests__/context-helpers-provider.test.d.ts.map +1 -0
- package/dist/context-helpers/__tests__/context-helpers-provider.test.js +147 -0
- package/dist/context-helpers/__tests__/context-helpers-provider.test.js.map +1 -0
- package/dist/context-helpers/__tests__/context-helpers.test.js +30 -35
- package/dist/context-helpers/__tests__/context-helpers.test.js.map +1 -1
- package/dist/context-helpers/current-page-context-helper.d.ts +7 -0
- package/dist/context-helpers/current-page-context-helper.d.ts.map +1 -0
- package/dist/context-helpers/current-page-context-helper.js +24 -0
- package/dist/context-helpers/current-page-context-helper.js.map +1 -0
- package/dist/context-helpers/current-time-context-helper.d.ts +7 -0
- package/dist/context-helpers/current-time-context-helper.d.ts.map +1 -0
- package/dist/context-helpers/current-time-context-helper.js +21 -0
- package/dist/context-helpers/current-time-context-helper.js.map +1 -0
- package/dist/context-helpers/index.d.ts +2 -7
- package/dist/context-helpers/index.d.ts.map +1 -1
- package/dist/context-helpers/index.js +2 -20
- package/dist/context-helpers/index.js.map +1 -1
- package/dist/context-helpers/registry.d.ts +35 -0
- package/dist/context-helpers/registry.d.ts.map +1 -0
- package/dist/context-helpers/registry.js +64 -0
- package/dist/context-helpers/registry.js.map +1 -0
- package/dist/context-helpers/types.d.ts +6 -16
- package/dist/context-helpers/types.d.ts.map +1 -1
- package/dist/context-helpers/types.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -4
- package/dist/index.js.map +1 -1
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.js +278 -137
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.js.map +1 -1
- package/dist/providers/__tests__/tambo-thread-provider.test.js +29 -7
- package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/dist/providers/tambo-context-helpers-provider.d.ts +24 -15
- package/dist/providers/tambo-context-helpers-provider.d.ts.map +1 -1
- package/dist/providers/tambo-context-helpers-provider.js +56 -39
- package/dist/providers/tambo-context-helpers-provider.js.map +1 -1
- package/dist/providers/tambo-interactable-provider.js +1 -1
- package/dist/providers/tambo-interactable-provider.js.map +1 -1
- package/esm/context-helpers/__tests__/context-helpers-provider.test.d.ts +15 -0
- package/esm/context-helpers/__tests__/context-helpers-provider.test.d.ts.map +1 -0
- package/esm/context-helpers/__tests__/context-helpers-provider.test.js +142 -0
- package/esm/context-helpers/__tests__/context-helpers-provider.test.js.map +1 -0
- package/esm/context-helpers/__tests__/context-helpers.test.js +31 -36
- package/esm/context-helpers/__tests__/context-helpers.test.js.map +1 -1
- package/esm/context-helpers/current-page-context-helper.d.ts +7 -0
- package/esm/context-helpers/current-page-context-helper.d.ts.map +1 -0
- package/esm/context-helpers/current-page-context-helper.js +20 -0
- package/esm/context-helpers/current-page-context-helper.js.map +1 -0
- package/esm/context-helpers/current-time-context-helper.d.ts +7 -0
- package/esm/context-helpers/current-time-context-helper.d.ts.map +1 -0
- package/esm/context-helpers/current-time-context-helper.js +17 -0
- package/esm/context-helpers/current-time-context-helper.js.map +1 -0
- package/esm/context-helpers/index.d.ts +2 -7
- package/esm/context-helpers/index.d.ts.map +1 -1
- package/esm/context-helpers/index.js +2 -19
- package/esm/context-helpers/index.js.map +1 -1
- package/esm/context-helpers/registry.d.ts +35 -0
- package/esm/context-helpers/registry.d.ts.map +1 -0
- package/esm/context-helpers/registry.js +57 -0
- package/esm/context-helpers/registry.js.map +1 -0
- package/esm/context-helpers/types.d.ts +6 -16
- package/esm/context-helpers/types.d.ts.map +1 -1
- package/esm/context-helpers/types.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.js +279 -105
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.js.map +1 -1
- package/esm/providers/__tests__/tambo-thread-provider.test.js +29 -7
- package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/esm/providers/tambo-context-helpers-provider.d.ts +24 -15
- package/esm/providers/tambo-context-helpers-provider.d.ts.map +1 -1
- package/esm/providers/tambo-context-helpers-provider.js +57 -40
- package/esm/providers/tambo-context-helpers-provider.js.map +1 -1
- package/esm/providers/tambo-interactable-provider.js +1 -1
- package/esm/providers/tambo-interactable-provider.js.map +1 -1
- package/package.json +6 -6
- package/dist/context-helpers/user-page.d.ts +0 -7
- package/dist/context-helpers/user-page.d.ts.map +0 -1
- package/dist/context-helpers/user-page.js +0 -31
- package/dist/context-helpers/user-page.js.map +0 -1
- package/dist/context-helpers/user-time.d.ts +0 -7
- package/dist/context-helpers/user-time.d.ts.map +0 -1
- package/dist/context-helpers/user-time.js +0 -20
- package/dist/context-helpers/user-time.js.map +0 -1
- package/esm/context-helpers/user-page.d.ts +0 -7
- package/esm/context-helpers/user-page.d.ts.map +0 -1
- package/esm/context-helpers/user-page.js +0 -28
- package/esm/context-helpers/user-page.js.map +0 -1
- package/esm/context-helpers/user-time.d.ts +0 -7
- package/esm/context-helpers/user-time.d.ts.map +0 -1
- package/esm/context-helpers/user-time.js +0 -17
- package/esm/context-helpers/user-time.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-interactable-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-interactable-provider.tsx"],"names":[],"mappings":";AAAA,0DAA0D;AAC1D,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACb,+CAOe;AACf,6BAAwB;AAKxB,yEAA+D;AAE/D,MAAM,wBAAwB,GAAG,IAAA,qBAAa,EAA2B;IACvE,sBAAsB,EAAE,EAAE;IAC1B,wBAAwB,EAAE,GAAG,EAAE,CAAC,EAAE;IAClC,2BAA2B,EAAE,GAAG,EAAE,G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AAK,IAC5C,QAAQ,CACyB,CACrC,CAAC;AACJ,CAAC,CAAC;AA5PW,QAAA,yBAAyB,6BA4PpC;AAEF;;;;GAIG;AACI,MAAM,oBAAoB,GAAG,GAAG,EAAE;IACvC,OAAO,IAAA,kBAAU,EAAC,wBAAwB,CAAC,CAAC;AAC9C,CAAC,CAAC;AAFW,QAAA,oBAAoB,wBAE/B","sourcesContent":["// react-sdk/src/providers/tambo-interactable-provider.tsx\n\"use client\";\nimport React, {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n useEffect,\n useState,\n} from \"react\";\nimport { z } from \"zod\";\nimport {\n TamboInteractableComponent,\n type TamboInteractableContext,\n} from \"../model/tambo-interactable\";\nimport { useTamboComponent } from \"./tambo-component-provider\";\n\nconst TamboInteractableContext = createContext<TamboInteractableContext>({\n interactableComponents: [],\n addInteractableComponent: () => \"\",\n removeInteractableComponent: () => {},\n updateInteractableComponentProps: () => {},\n getInteractableComponent: () => undefined,\n getInteractableComponentsByName: () => [],\n clearAllInteractableComponents: () => {},\n});\n\n/**\n * The TamboInteractableProvider manages a list of components that are currently\n * interactable, allowing tambo to interact with them by updating their props. It also registers tools\n * for Tambo to perform CRUD operations on the components list.\n * @param props - The props for the TamboInteractableProvider\n * @param props.children - The children to wrap\n * @returns The TamboInteractableProvider component\n */\nexport const TamboInteractableProvider: React.FC<PropsWithChildren> = ({\n children,\n}) => {\n const [interactableComponents, setInteractableComponents] = useState<\n TamboInteractableComponent[]\n >([]);\n const { registerTool } = useTamboComponent();\n\n useEffect(() => {\n registerTool({\n name: \"get_all_interactable_components\",\n description:\n \"Get all currently interactable components with their details including their current props. These are components that you can interact with on behalf of the user.\",\n tool: () => {\n return {\n components: interactableComponents,\n };\n },\n toolSchema: z.function().returns(\n z.object({\n components: z.array(\n z.object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n propsSchema: z.object({}).optional(),\n }),\n ),\n }),\n ),\n });\n\n registerTool({\n name: \"get_interactable_component_by_id\",\n description: \"Get a specific interactable component by its ID\",\n tool: (componentId: string) => {\n const component = interactableComponents.find(\n (c) => c.id === componentId,\n );\n\n if (!component) {\n return {\n success: false,\n error: `Component with ID ${componentId} not found`,\n };\n }\n\n return {\n success: true,\n component: {\n id: component.id,\n componentName: component.name,\n props: component.props,\n },\n };\n },\n toolSchema: z\n .function()\n .args(z.string())\n .returns(\n z.object({\n success: z.boolean(),\n component: z\n .object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n })\n .optional(),\n error: z.string().optional(),\n }),\n ),\n });\n\n registerTool({\n name: \"remove_interactable_component\",\n description: \"Remove an interactable component from the system\",\n tool: (componentId: string) => {\n const component = interactableComponents.find(\n (c) => c.id === componentId,\n );\n\n if (!component) {\n return {\n success: false,\n error: `Component with ID ${componentId} not found`,\n };\n }\n\n setInteractableComponents((prev) =>\n prev.filter((c) => c.id !== componentId),\n );\n\n return {\n success: true,\n componentId,\n removedComponent: {\n id: component.id,\n componentName: component.name,\n props: component.props,\n },\n };\n },\n toolSchema: z\n .function()\n .args(z.string())\n .returns(\n z.object({\n success: z.boolean(),\n componentId: z.string(),\n removedComponent: z.object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n }),\n error: z.string().optional(),\n }),\n ),\n });\n }, [interactableComponents, registerTool]);\n\n const updateInteractableComponentProps = useCallback(\n (id: string, newProps: Record<string, any>) => {\n let updateResult = \"Updated successfully\";\n\n setInteractableComponents((prev) => {\n const componentExists = prev.some((c) => c.id === id);\n\n if (!componentExists) {\n updateResult = `Error: Component with ID ${id} not found`;\n return prev;\n }\n\n const updatedComponents = prev.map((c) =>\n c.id === id ? { ...c, props: { ...c.props, ...newProps } } : c,\n );\n\n // Check if the update actually changed anything\n const originalComponent = prev.find((c) => c.id === id);\n const updatedComponent = updatedComponents.find((c) => c.id === id);\n\n if (!originalComponent || !updatedComponent) {\n updateResult = `Error: Failed to update component with ID ${id}`;\n return prev;\n }\n\n // Check if props actually changed\n const propsChanged =\n JSON.stringify(originalComponent.props) !==\n JSON.stringify(updatedComponent.props);\n\n if (!propsChanged) {\n updateResult = `Warning: No changes detected for component with ID ${id}. The update might not have worked.`;\n return prev;\n }\n\n return updatedComponents;\n });\n\n return updateResult;\n },\n [],\n );\n\n const registerInteractableComponentUpdateTool = useCallback(\n (component: TamboInteractableComponent) => {\n const schemaForArgs =\n typeof component.propsSchema === \"object\" &&\n \"describe\" in component.propsSchema\n ? component.propsSchema\n : z.object({});\n\n registerTool({\n name: `update_interactable_component_${component.id}`,\n description: `Update the props of interactable component ${component.id} (${component.name})`,\n tool: (componentId: string, newProps: any) => {\n return updateInteractableComponentProps(componentId, newProps);\n },\n toolSchema: z\n .function()\n .args(\n z\n .string()\n .describe(\"The ID of the interactable component to update\"),\n schemaForArgs.describe(\n \"The new props to update the component with\",\n ),\n )\n .returns(z.string()),\n });\n },\n [registerTool, updateInteractableComponentProps],\n );\n\n const addInteractableComponent = useCallback(\n (\n component: Omit<TamboInteractableComponent, \"id\" | \"createdAt\">,\n ): string => {\n const id = `${component.name}-${Math.random().toString(36).substr(2, 9)}`;\n const newComponent: TamboInteractableComponent = {\n ...component,\n id,\n };\n\n registerInteractableComponentUpdateTool(newComponent);\n\n setInteractableComponents((prev) => {\n return [...prev, newComponent];\n });\n\n return id;\n },\n [registerInteractableComponentUpdateTool],\n );\n\n const removeInteractableComponent = useCallback((id: string) => {\n setInteractableComponents((prev) => prev.filter((c) => c.id !== id));\n }, []);\n\n const getInteractableComponent = useCallback(\n (id: string) => {\n return interactableComponents.find((c) => c.id === id);\n },\n [interactableComponents],\n );\n\n const getInteractableComponentsByName = useCallback(\n (componentName: string) => {\n return interactableComponents.filter((c) => c.name === componentName);\n },\n [interactableComponents],\n );\n\n const clearAllInteractableComponents = useCallback(() => {\n setInteractableComponents([]);\n }, []);\n\n const value: TamboInteractableContext = {\n interactableComponents,\n addInteractableComponent,\n removeInteractableComponent,\n updateInteractableComponentProps,\n getInteractableComponent,\n getInteractableComponentsByName,\n clearAllInteractableComponents,\n };\n\n return (\n <TamboInteractableContext.Provider value={value}>\n {children}\n </TamboInteractableContext.Provider>\n );\n};\n\n/**\n * The useTamboInteractable hook provides access to the interactable component\n * management functions.\n * @returns The interactable component management functions\n */\nexport const useTamboInteractable = () => {\n return useContext(TamboInteractableContext);\n};\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-interactable-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-interactable-provider.tsx"],"names":[],"mappings":";AAAA,0DAA0D;AAC1D,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACb,+CAOe;AACf,6BAAwB;AAKxB,yEAA+D;AAE/D,MAAM,wBAAwB,GAAG,IAAA,qBAAa,EAA2B;IACvE,sBAAsB,EAAE,EAAE;IAC1B,wBAAwB,EAAE,GAAG,EAAE,CAAC,EAAE;IAClC,2BAA2B,EAAE,GAAG,EAAE,GAAE,CAAC;IACrC,gCAAgC,EAAE,GAAG,EAAE,GAAE,CAAC;IAC1C,wBAAwB,EAAE,GAAG,EAAE,CAAC,SAAS;IACzC,+BAA+B,EAAE,GAAG,EAAE,CAAC,EAAE;IACzC,8BAA8B,EAAE,GAAG,EAAE,GAAE,CAAC;CACzC,CAAC,CAAC;AAEH;;;;;;;GAOG;AACI,MAAM,yBAAyB,GAAgC,CAAC,EACrE,QAAQ,GACT,EAAE,EAAE;IACH,MAAM,CAAC,sBAAsB,EAAE,yBAAyB,CAAC,GAAG,IAAA,gBAAQ,EAElE,EAAE,CAAC,CAAC;IACN,MAAM,EAAE,YAAY,EAAE,GAAG,IAAA,4CAAiB,GAAE,CAAC;IAE7C,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,YAAY,CAAC;YACX,IAAI,EAAE,iCAAiC;YACvC,WAAW,EACT,2OAA2O;YAC7O,IAAI,EAAE,GAAG,EAAE;gBACT,OAAO;oBACL,UAAU,EAAE,sBAAsB;iBACnC,CAAC;YACJ,CAAC;YACD,UAAU,EAAE,OAAC,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EF,MAAM,+BAA+B,GAAG,IAAA,mBAAW,EACjD,CAAC,aAAqB,EAAE,EAAE;QACxB,OAAO,sBAAsB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,aAAa,CAAC,CAAC;IACxE,CAAC,EACD,CAAC,sBAAsB,CAAC,CACzB,CAAC;IAEF,MAAM,8BAA8B,GAAG,IAAA,mBAAW,EAAC,GAAG,EAAE;QACtD,yBAAyB,CAAC,EAAE,CAAC,CAAC;IAChC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,KAAK,GAA6B;QACtC,sBAAsB;QACtB,wBAAwB;QACxB,2BAA2B;QAC3B,gCAAgC;QAChC,wBAAwB;QACxB,+BAA+B;QAC/B,8BAA8B;KAC/B,CAAC;IAEF,OAAO,CACL,8BAAC,wBAAwB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC5C,QAAQ,CACyB,CACrC,CAAC;AACJ,CAAC,CAAC;AA5PW,QAAA,yBAAyB,6BA4PpC;AAEF;;;;GAIG;AACI,MAAM,oBAAoB,GAAG,GAAG,EAAE;IACvC,OAAO,IAAA,kBAAU,EAAC,wBAAwB,CAAC,CAAC;AAC9C,CAAC,CAAC;AAFW,QAAA,oBAAoB,wBAE/B","sourcesContent":["// react-sdk/src/providers/tambo-interactable-provider.tsx\n\"use client\";\nimport React, {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n useEffect,\n useState,\n} from \"react\";\nimport { z } from \"zod\";\nimport {\n TamboInteractableComponent,\n type TamboInteractableContext,\n} from \"../model/tambo-interactable\";\nimport { useTamboComponent } from \"./tambo-component-provider\";\n\nconst TamboInteractableContext = createContext<TamboInteractableContext>({\n interactableComponents: [],\n addInteractableComponent: () => \"\",\n removeInteractableComponent: () => {},\n updateInteractableComponentProps: () => {},\n getInteractableComponent: () => undefined,\n getInteractableComponentsByName: () => [],\n clearAllInteractableComponents: () => {},\n});\n\n/**\n * The TamboInteractableProvider manages a list of components that are currently\n * interactable, allowing tambo to interact with them by updating their props. It also registers tools\n * for Tambo to perform CRUD operations on the components list.\n * @param props - The props for the TamboInteractableProvider\n * @param props.children - The children to wrap\n * @returns The TamboInteractableProvider component\n */\nexport const TamboInteractableProvider: React.FC<PropsWithChildren> = ({\n children,\n}) => {\n const [interactableComponents, setInteractableComponents] = useState<\n TamboInteractableComponent[]\n >([]);\n const { registerTool } = useTamboComponent();\n\n useEffect(() => {\n registerTool({\n name: \"get_all_interactable_components\",\n description:\n \"Only use this tool if the user is asking about interactable components.Get all currently interactable components with their details including their current props. These are components that you can interact with on behalf of the user.\",\n tool: () => {\n return {\n components: interactableComponents,\n };\n },\n toolSchema: z.function().returns(\n z.object({\n components: z.array(\n z.object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n propsSchema: z.object({}).optional(),\n }),\n ),\n }),\n ),\n });\n\n registerTool({\n name: \"get_interactable_component_by_id\",\n description: \"Get a specific interactable component by its ID\",\n tool: (componentId: string) => {\n const component = interactableComponents.find(\n (c) => c.id === componentId,\n );\n\n if (!component) {\n return {\n success: false,\n error: `Component with ID ${componentId} not found`,\n };\n }\n\n return {\n success: true,\n component: {\n id: component.id,\n componentName: component.name,\n props: component.props,\n },\n };\n },\n toolSchema: z\n .function()\n .args(z.string())\n .returns(\n z.object({\n success: z.boolean(),\n component: z\n .object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n })\n .optional(),\n error: z.string().optional(),\n }),\n ),\n });\n\n registerTool({\n name: \"remove_interactable_component\",\n description: \"Remove an interactable component from the system\",\n tool: (componentId: string) => {\n const component = interactableComponents.find(\n (c) => c.id === componentId,\n );\n\n if (!component) {\n return {\n success: false,\n error: `Component with ID ${componentId} not found`,\n };\n }\n\n setInteractableComponents((prev) =>\n prev.filter((c) => c.id !== componentId),\n );\n\n return {\n success: true,\n componentId,\n removedComponent: {\n id: component.id,\n componentName: component.name,\n props: component.props,\n },\n };\n },\n toolSchema: z\n .function()\n .args(z.string())\n .returns(\n z.object({\n success: z.boolean(),\n componentId: z.string(),\n removedComponent: z.object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n }),\n error: z.string().optional(),\n }),\n ),\n });\n }, [interactableComponents, registerTool]);\n\n const updateInteractableComponentProps = useCallback(\n (id: string, newProps: Record<string, any>) => {\n let updateResult = \"Updated successfully\";\n\n setInteractableComponents((prev) => {\n const componentExists = prev.some((c) => c.id === id);\n\n if (!componentExists) {\n updateResult = `Error: Component with ID ${id} not found`;\n return prev;\n }\n\n const updatedComponents = prev.map((c) =>\n c.id === id ? { ...c, props: { ...c.props, ...newProps } } : c,\n );\n\n // Check if the update actually changed anything\n const originalComponent = prev.find((c) => c.id === id);\n const updatedComponent = updatedComponents.find((c) => c.id === id);\n\n if (!originalComponent || !updatedComponent) {\n updateResult = `Error: Failed to update component with ID ${id}`;\n return prev;\n }\n\n // Check if props actually changed\n const propsChanged =\n JSON.stringify(originalComponent.props) !==\n JSON.stringify(updatedComponent.props);\n\n if (!propsChanged) {\n updateResult = `Warning: No changes detected for component with ID ${id}. The update might not have worked.`;\n return prev;\n }\n\n return updatedComponents;\n });\n\n return updateResult;\n },\n [],\n );\n\n const registerInteractableComponentUpdateTool = useCallback(\n (component: TamboInteractableComponent) => {\n const schemaForArgs =\n typeof component.propsSchema === \"object\" &&\n \"describe\" in component.propsSchema\n ? component.propsSchema\n : z.object({});\n\n registerTool({\n name: `update_interactable_component_${component.id}`,\n description: `Update the props of interactable component ${component.id} (${component.name})`,\n tool: (componentId: string, newProps: any) => {\n return updateInteractableComponentProps(componentId, newProps);\n },\n toolSchema: z\n .function()\n .args(\n z\n .string()\n .describe(\"The ID of the interactable component to update\"),\n schemaForArgs.describe(\n \"The new props to update the component with\",\n ),\n )\n .returns(z.string()),\n });\n },\n [registerTool, updateInteractableComponentProps],\n );\n\n const addInteractableComponent = useCallback(\n (\n component: Omit<TamboInteractableComponent, \"id\" | \"createdAt\">,\n ): string => {\n const id = `${component.name}-${Math.random().toString(36).substr(2, 9)}`;\n const newComponent: TamboInteractableComponent = {\n ...component,\n id,\n };\n\n registerInteractableComponentUpdateTool(newComponent);\n\n setInteractableComponents((prev) => {\n return [...prev, newComponent];\n });\n\n return id;\n },\n [registerInteractableComponentUpdateTool],\n );\n\n const removeInteractableComponent = useCallback((id: string) => {\n setInteractableComponents((prev) => prev.filter((c) => c.id !== id));\n }, []);\n\n const getInteractableComponent = useCallback(\n (id: string) => {\n return interactableComponents.find((c) => c.id === id);\n },\n [interactableComponents],\n );\n\n const getInteractableComponentsByName = useCallback(\n (componentName: string) => {\n return interactableComponents.filter((c) => c.name === componentName);\n },\n [interactableComponents],\n );\n\n const clearAllInteractableComponents = useCallback(() => {\n setInteractableComponents([]);\n }, []);\n\n const value: TamboInteractableContext = {\n interactableComponents,\n addInteractableComponent,\n removeInteractableComponent,\n updateInteractableComponentProps,\n getInteractableComponent,\n getInteractableComponentsByName,\n clearAllInteractableComponents,\n };\n\n return (\n <TamboInteractableContext.Provider value={value}>\n {children}\n </TamboInteractableContext.Provider>\n );\n};\n\n/**\n * The useTamboInteractable hook provides access to the interactable component\n * management functions.\n * @returns The interactable component management functions\n */\nexport const useTamboInteractable = () => {\n return useContext(TamboInteractableContext);\n};\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the Context Helpers API.
|
|
3
|
+
*
|
|
4
|
+
* The new API treats a context helper as a plain function that returns a value
|
|
5
|
+
* to include in context, or null/undefined to skip. The provider wraps each
|
|
6
|
+
* value with its key name as { name, context } before sending with messages.
|
|
7
|
+
*
|
|
8
|
+
* This test suite validates that:
|
|
9
|
+
* - Helper functions can return sync/async values.
|
|
10
|
+
* - Returning null/undefined causes the helper to be skipped.
|
|
11
|
+
* - Errors in helpers are caught and skipped.
|
|
12
|
+
* - The provider aggregates helpers passed in its props and returns AdditionalContext[].
|
|
13
|
+
*/
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=context-helpers-provider.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-helpers-provider.test.d.ts","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers-provider.test.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the Context Helpers API.
|
|
3
|
+
*
|
|
4
|
+
* The new API treats a context helper as a plain function that returns a value
|
|
5
|
+
* to include in context, or null/undefined to skip. The provider wraps each
|
|
6
|
+
* value with its key name as { name, context } before sending with messages.
|
|
7
|
+
*
|
|
8
|
+
* This test suite validates that:
|
|
9
|
+
* - Helper functions can return sync/async values.
|
|
10
|
+
* - Returning null/undefined causes the helper to be skipped.
|
|
11
|
+
* - Errors in helpers are caught and skipped.
|
|
12
|
+
* - The provider aggregates helpers passed in its props and returns AdditionalContext[].
|
|
13
|
+
*/
|
|
14
|
+
import { renderHook } from "@testing-library/react";
|
|
15
|
+
import React from "react";
|
|
16
|
+
import { setHelpers } from "../../context-helpers/registry";
|
|
17
|
+
import { TamboContextHelpersProvider, useTamboContextHelpers, } from "../../providers/tambo-context-helpers-provider";
|
|
18
|
+
import { currentPageContextHelper, currentTimeContextHelper, } from "../index";
|
|
19
|
+
/**
|
|
20
|
+
* Test wrapper to provide the TamboContextHelpersProvider for hooks.
|
|
21
|
+
* @param helpers - A dictionary of context helper functions.
|
|
22
|
+
* @returns A wrapper component that provides the TamboContextHelpersProvider.
|
|
23
|
+
*/
|
|
24
|
+
function wrapper(helpers) {
|
|
25
|
+
// eslint-disable-next-line react/display-name
|
|
26
|
+
return ({ children }) => React.createElement(TamboContextHelpersProvider, {
|
|
27
|
+
contextHelpers: helpers,
|
|
28
|
+
}, children);
|
|
29
|
+
}
|
|
30
|
+
describe("Context Helpers API", () => {
|
|
31
|
+
// Ensure the global registry doesn't leak state between tests
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
setHelpers({});
|
|
34
|
+
jest.clearAllMocks();
|
|
35
|
+
});
|
|
36
|
+
test("returns empty array when no helpers provided", async () => {
|
|
37
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
38
|
+
wrapper: wrapper(),
|
|
39
|
+
});
|
|
40
|
+
const contexts = await result.current.getAdditionalContext();
|
|
41
|
+
expect(Array.isArray(contexts)).toBe(true);
|
|
42
|
+
expect(contexts).toHaveLength(0);
|
|
43
|
+
});
|
|
44
|
+
test("collects sync and async helper results", async () => {
|
|
45
|
+
const syncHelper = () => ({ a: 1 });
|
|
46
|
+
const asyncHelper = async () => ({ b: 2 });
|
|
47
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
48
|
+
wrapper: wrapper({
|
|
49
|
+
sync: syncHelper,
|
|
50
|
+
async: asyncHelper,
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
const contexts = await result.current.getAdditionalContext();
|
|
54
|
+
// Validate shape and presence
|
|
55
|
+
const byName = new Map(contexts.map((c) => [c.name, c.context]));
|
|
56
|
+
expect(byName.get("sync")).toEqual({ a: 1 });
|
|
57
|
+
expect(byName.get("async")).toEqual({ b: 2 });
|
|
58
|
+
});
|
|
59
|
+
test("skips null and undefined results", async () => {
|
|
60
|
+
const nullHelper = () => null;
|
|
61
|
+
const undefinedHelper = () => undefined;
|
|
62
|
+
const validHelper = () => ({ ok: true });
|
|
63
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
64
|
+
wrapper: wrapper({
|
|
65
|
+
nullHelper,
|
|
66
|
+
undefinedHelper,
|
|
67
|
+
validHelper,
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
const contexts = await result.current.getAdditionalContext();
|
|
71
|
+
const names = contexts.map((c) => c.name);
|
|
72
|
+
expect(names).toContain("validHelper");
|
|
73
|
+
expect(names).not.toContain("nullHelper");
|
|
74
|
+
expect(names).not.toContain("undefinedHelper");
|
|
75
|
+
});
|
|
76
|
+
test("errors in helpers are caught and skipped", async () => {
|
|
77
|
+
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {
|
|
78
|
+
// silence expected error
|
|
79
|
+
});
|
|
80
|
+
const badHelper = () => {
|
|
81
|
+
throw new Error("boom");
|
|
82
|
+
};
|
|
83
|
+
const goodHelper = () => ({ ok: 1 });
|
|
84
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
85
|
+
wrapper: wrapper({ badHelper, goodHelper }),
|
|
86
|
+
});
|
|
87
|
+
const contexts = await result.current.getAdditionalContext();
|
|
88
|
+
expect(contexts).toHaveLength(1);
|
|
89
|
+
expect(contexts[0]).toEqual({
|
|
90
|
+
name: "goodHelper",
|
|
91
|
+
context: { ok: 1 },
|
|
92
|
+
});
|
|
93
|
+
consoleSpy.mockRestore();
|
|
94
|
+
});
|
|
95
|
+
test("dynamic add/remove helpers works", async () => {
|
|
96
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
97
|
+
wrapper: wrapper(),
|
|
98
|
+
});
|
|
99
|
+
// Initially empty
|
|
100
|
+
expect(await result.current.getAdditionalContext()).toHaveLength(0);
|
|
101
|
+
// Add
|
|
102
|
+
result.current.addContextHelper("dyn", () => ({ x: 10 }));
|
|
103
|
+
let contexts = await result.current.getAdditionalContext();
|
|
104
|
+
expect(contexts).toHaveLength(1);
|
|
105
|
+
expect(contexts[0]).toEqual({
|
|
106
|
+
name: "dyn",
|
|
107
|
+
context: { x: 10 },
|
|
108
|
+
});
|
|
109
|
+
// Update
|
|
110
|
+
result.current.addContextHelper("dyn", () => ({ x: 20 }));
|
|
111
|
+
contexts = await result.current.getAdditionalContext();
|
|
112
|
+
expect(contexts).toHaveLength(1);
|
|
113
|
+
expect(contexts[0]).toEqual({
|
|
114
|
+
name: "dyn",
|
|
115
|
+
context: { x: 20 },
|
|
116
|
+
});
|
|
117
|
+
// Remove
|
|
118
|
+
result.current.removeContextHelper("dyn");
|
|
119
|
+
contexts = await result.current.getAdditionalContext();
|
|
120
|
+
expect(contexts).toHaveLength(0);
|
|
121
|
+
});
|
|
122
|
+
test("prebuilt helpers can be passed directly", async () => {
|
|
123
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
124
|
+
wrapper: wrapper({
|
|
125
|
+
userTime: currentTimeContextHelper,
|
|
126
|
+
userPage: currentPageContextHelper,
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
const contexts = await result.current.getAdditionalContext();
|
|
130
|
+
// Should include both entries unless environment prevents userPage (SSR)
|
|
131
|
+
const names = contexts.map((c) => c.name);
|
|
132
|
+
expect(names).toContain("userTime");
|
|
133
|
+
// userPage may be null on non-browser envs; allow either outcome:
|
|
134
|
+
// if present, ensure expected keys exist
|
|
135
|
+
if (names.includes("userPage")) {
|
|
136
|
+
const userPage = contexts.find((c) => c.name === "userPage").context;
|
|
137
|
+
expect(typeof userPage.url === "string" || userPage.url === undefined).toBe(true);
|
|
138
|
+
expect(typeof userPage.title === "string" || userPage.title === undefined).toBe(true);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
//# sourceMappingURL=context-helpers-provider.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-helpers-provider.test.js","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers-provider.test.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAA4B,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAC5D,OAAO,EACL,2BAA2B,EAC3B,sBAAsB,GACvB,MAAM,gDAAgD,CAAC;AACxD,OAAO,EAGL,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,UAAU,CAAC;AAElB;;;;GAIG;AACH,SAAS,OAAO,CAAC,OAAyC;IACxD,8CAA8C;IAC9C,OAAO,CAAC,EAAE,QAAQ,EAA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CAAC;IACnC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACzD,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,sBAAsB,EAAE,EAAE;YAC5D,OAAO,EAAE,OAAO,CAAC;gBACf,QAAQ,EAAE,wBAAwB;gBAClC,QAAQ,EAAE,wBAAwB;aACnC,CAAC;SACH,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC;QAE7D,yEAAyE;QACzE,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAC1C,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QACpC,kEAAkE;QAClE,yCAAyC;QACzC,IAAI,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YAC/B,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAE,CAAC,OAG7D,CAAC;YACF,MAAM,CACJ,OAAO,QAAQ,CAAC,GAAG,KAAK,QAAQ,IAAI,QAAQ,CAAC,GAAG,KAAK,SAAS,CAC/D,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACb,MAAM,CACJ,OAAO,QAAQ,CAAC,KAAK,KAAK,QAAQ,IAAI,QAAQ,CAAC,KAAK,KAAK,SAAS,CACnE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["/**\n * Tests for the Context Helpers API.\n *\n * The new API treats a context helper as a plain function that returns a value\n * to include in context, or null/undefined to skip. The provider wraps each\n * value with its key name as { name, context } before sending with messages.\n *\n * This test suite validates that:\n * - Helper functions can return sync/async values.\n * - Returning null/undefined causes the helper to be skipped.\n * - Errors in helpers are caught and skipped.\n * - The provider aggregates helpers passed in its props and returns AdditionalContext[].\n */\n\nimport { renderHook } from \"@testing-library/react\";\nimport React, { PropsWithChildren } from \"react\";\nimport { setHelpers } from \"../../context-helpers/registry\";\nimport {\n TamboContextHelpersProvider,\n useTamboContextHelpers,\n} from \"../../providers/tambo-context-helpers-provider\";\nimport {\n type AdditionalContext,\n type ContextHelperFn,\n currentPageContextHelper,\n currentTimeContextHelper,\n} from \"../index\";\n\n/**\n * Test wrapper to provide the TamboContextHelpersProvider for hooks.\n * @param helpers - A dictionary of context helper functions.\n * @returns A wrapper component that provides the TamboContextHelpersProvider.\n */\nfunction wrapper(helpers?: Record<string, ContextHelperFn>) {\n // eslint-disable-next-line react/display-name\n return ({ children }: PropsWithChildren) =>\n React.createElement(\n TamboContextHelpersProvider,\n {\n contextHelpers: helpers,\n },\n children,\n );\n}\n\ndescribe(\"Context Helpers API\", () => {\n // Ensure the global registry doesn't leak state between tests\n beforeEach(() => {\n setHelpers({});\n jest.clearAllMocks();\n });\n\n test(\"returns empty array when no helpers provided\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: wrapper(),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(Array.isArray(contexts)).toBe(true);\n expect(contexts).toHaveLength(0);\n });\n\n test(\"collects sync and async helper results\", async () => {\n const syncHelper: ContextHelperFn = () => ({ a: 1 });\n const asyncHelper: ContextHelperFn = async () => ({ b: 2 });\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: wrapper({\n sync: syncHelper,\n async: asyncHelper,\n }),\n });\n\n const contexts = await result.current.getAdditionalContext();\n\n // Validate shape and presence\n const byName = new Map(contexts.map((c) => [c.name, c.context]));\n expect(byName.get(\"sync\")).toEqual({ a: 1 });\n expect(byName.get(\"async\")).toEqual({ b: 2 });\n });\n\n test(\"skips null and undefined results\", async () => {\n const nullHelper: ContextHelperFn = () => null;\n const undefinedHelper: ContextHelperFn = () => undefined;\n const validHelper: ContextHelperFn = () => ({ ok: true });\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: wrapper({\n nullHelper,\n undefinedHelper,\n validHelper,\n }),\n });\n\n const contexts = await result.current.getAdditionalContext();\n const names = contexts.map((c) => c.name);\n\n expect(names).toContain(\"validHelper\");\n expect(names).not.toContain(\"nullHelper\");\n expect(names).not.toContain(\"undefinedHelper\");\n });\n\n test(\"errors in helpers are caught and skipped\", async () => {\n const consoleSpy = jest.spyOn(console, \"error\").mockImplementation(() => {\n // silence expected error\n });\n\n const badHelper: ContextHelperFn = () => {\n throw new Error(\"boom\");\n };\n const goodHelper: ContextHelperFn = () => ({ ok: 1 });\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: wrapper({ badHelper, goodHelper }),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toHaveLength(1);\n expect(contexts[0]).toEqual<AdditionalContext>({\n name: \"goodHelper\",\n context: { ok: 1 },\n });\n\n consoleSpy.mockRestore();\n });\n\n test(\"dynamic add/remove helpers works\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: wrapper(),\n });\n\n // Initially empty\n expect(await result.current.getAdditionalContext()).toHaveLength(0);\n\n // Add\n result.current.addContextHelper(\"dyn\", () => ({ x: 10 }));\n let contexts = await result.current.getAdditionalContext();\n expect(contexts).toHaveLength(1);\n expect(contexts[0]).toEqual<AdditionalContext>({\n name: \"dyn\",\n context: { x: 10 },\n });\n\n // Update\n result.current.addContextHelper(\"dyn\", () => ({ x: 20 }));\n contexts = await result.current.getAdditionalContext();\n expect(contexts).toHaveLength(1);\n expect(contexts[0]).toEqual<AdditionalContext>({\n name: \"dyn\",\n context: { x: 20 },\n });\n\n // Remove\n result.current.removeContextHelper(\"dyn\");\n contexts = await result.current.getAdditionalContext();\n expect(contexts).toHaveLength(0);\n });\n\n test(\"prebuilt helpers can be passed directly\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: wrapper({\n userTime: currentTimeContextHelper,\n userPage: currentPageContextHelper,\n }),\n });\n\n const contexts = await result.current.getAdditionalContext();\n\n // Should include both entries unless environment prevents userPage (SSR)\n const names = contexts.map((c) => c.name);\n expect(names).toContain(\"userTime\");\n // userPage may be null on non-browser envs; allow either outcome:\n // if present, ensure expected keys exist\n if (names.includes(\"userPage\")) {\n const userPage = contexts.find((c) => c.name === \"userPage\")!.context as {\n url?: string;\n title?: string;\n };\n expect(\n typeof userPage.url === \"string\" || userPage.url === undefined,\n ).toBe(true);\n expect(\n typeof userPage.title === \"string\" || userPage.title === undefined,\n ).toBe(true);\n }\n });\n});\n"]}
|
|
@@ -1,41 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
expect(
|
|
14
|
-
|
|
15
|
-
expect(
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { currentPageContextHelper, currentTimeContextHelper } from "../index";
|
|
2
|
+
/**
|
|
3
|
+
* Tests for prebuilt context helper functions.
|
|
4
|
+
*
|
|
5
|
+
* These helpers now return raw values (or null) instead of { name, context }.
|
|
6
|
+
* The provider is responsible for wrapping values as { name, context }.
|
|
7
|
+
*/
|
|
8
|
+
describe("Context Helpers (prebuilt functions)", () => {
|
|
9
|
+
describe("currentTimeContextHelper", () => {
|
|
10
|
+
it("should return user time context with required fields", () => {
|
|
11
|
+
const context = currentTimeContextHelper();
|
|
12
|
+
// Should not be null (error case)
|
|
13
|
+
expect(context).not.toBeNull();
|
|
14
|
+
// Shape: { timestamp: string }
|
|
15
|
+
expect(context).toHaveProperty("timestamp");
|
|
16
|
+
expect(typeof context.timestamp).toBe("string");
|
|
17
|
+
// Verify timestamp string parses
|
|
18
|
+
expect(() => new Date(context.timestamp)).not.toThrow();
|
|
18
19
|
});
|
|
19
20
|
});
|
|
20
|
-
describe("
|
|
21
|
-
it("should return page context
|
|
22
|
-
const context =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
expect(context
|
|
30
|
-
expect(context
|
|
31
|
-
|
|
32
|
-
expect(typeof context.
|
|
33
|
-
expect(typeof context.context.pathname).toBe("string");
|
|
34
|
-
expect(typeof context.context.hostname).toBe("string");
|
|
35
|
-
expect(typeof context.context.search).toBe("string");
|
|
36
|
-
expect(typeof context.context.hash).toBe("string");
|
|
37
|
-
expect(typeof context.context.title).toBe("string");
|
|
38
|
-
expect(typeof context.context.referrer).toBe("string");
|
|
21
|
+
describe("currentPageContextHelper", () => {
|
|
22
|
+
it("should return page context in browser, or null otherwise", () => {
|
|
23
|
+
const context = currentPageContextHelper();
|
|
24
|
+
if (context === null) {
|
|
25
|
+
// Non-browser environments should return null to skip
|
|
26
|
+
expect(context).toBeNull();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// Shape: { url: string, title: string }
|
|
30
|
+
expect(context).toHaveProperty("url");
|
|
31
|
+
expect(context).toHaveProperty("title");
|
|
32
|
+
expect(typeof context.url).toBe("string");
|
|
33
|
+
expect(typeof context.title).toBe("string");
|
|
39
34
|
});
|
|
40
35
|
});
|
|
41
36
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-helpers.test.js","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"context-helpers.test.js","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAC;AAE9E;;;;;GAKG;AACH,QAAQ,CAAC,sCAAsC,EAAE,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,CAAC,CAAC,CAAC","sourcesContent":["import { currentPageContextHelper, currentTimeContextHelper } from \"../index\";\n\n/**\n * Tests for prebuilt context helper functions.\n *\n * These helpers now return raw values (or null) instead of { name, context }.\n * The provider is responsible for wrapping values as { name, context }.\n */\ndescribe(\"Context Helpers (prebuilt functions)\", () => {\n describe(\"currentTimeContextHelper\", () => {\n it(\"should return user time context with required fields\", () => {\n const context = currentTimeContextHelper();\n\n // Should not be null (error case)\n expect(context).not.toBeNull();\n\n // Shape: { timestamp: string }\n expect(context).toHaveProperty(\"timestamp\");\n expect(typeof context!.timestamp).toBe(\"string\");\n\n // Verify timestamp string parses\n expect(() => new Date(context!.timestamp as string)).not.toThrow();\n });\n });\n\n describe(\"currentPageContextHelper\", () => {\n it(\"should return page context in browser, or null otherwise\", () => {\n const context = currentPageContextHelper();\n\n if (context === null) {\n // Non-browser environments should return null to skip\n expect(context).toBeNull();\n return;\n }\n\n // Shape: { url: string, title: string }\n expect(context).toHaveProperty(\"url\");\n expect(context).toHaveProperty(\"title\");\n\n expect(typeof context.url).toBe(\"string\");\n expect(typeof context.title).toBe(\"string\");\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ContextHelperFn } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Prebuilt context helper that provides information about the user's current page.
|
|
4
|
+
* @returns a value to include it, or null/undefined to skip.
|
|
5
|
+
*/
|
|
6
|
+
export declare const currentPageContextHelper: ContextHelperFn;
|
|
7
|
+
//# sourceMappingURL=current-page-context-helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-page-context-helper.d.ts","sourceRoot":"","sources":["../../src/context-helpers/current-page-context-helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C;;;GAGG;AACH,eAAO,MAAM,wBAAwB,EAAE,eAatC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prebuilt context helper that provides information about the user's current page.
|
|
3
|
+
* @returns a value to include it, or null/undefined to skip.
|
|
4
|
+
*/
|
|
5
|
+
export const currentPageContextHelper = () => {
|
|
6
|
+
try {
|
|
7
|
+
if (typeof window === "undefined") {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
url: window.location.href,
|
|
12
|
+
title: document.title,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
console.error("prebuiltUserPage failed:", e);
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=current-page-context-helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-page-context-helper.js","sourceRoot":"","sources":["../../src/context-helpers/current-page-context-helper.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAoB,GAAG,EAAE;IAC5D,IAAI,CAAC;QACH,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAClC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO;YACL,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI;YACzB,KAAK,EAAE,QAAQ,CAAC,KAAK;SACtB,CAAC;IACJ,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,CAAC,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { ContextHelperFn } from \"./types\";\n\n/**\n * Prebuilt context helper that provides information about the user's current page.\n * @returns a value to include it, or null/undefined to skip.\n */\nexport const currentPageContextHelper: ContextHelperFn = () => {\n try {\n if (typeof window === \"undefined\") {\n return null;\n }\n return {\n url: window.location.href,\n title: document.title,\n };\n } catch (e) {\n console.error(\"prebuiltUserPage failed:\", e);\n return null;\n }\n};\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ContextHelperFn } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Prebuilt context helper that provides information about the user's current time.
|
|
4
|
+
* @returns a value to include it, or null/undefined to skip.
|
|
5
|
+
*/
|
|
6
|
+
export declare const currentTimeContextHelper: ContextHelperFn;
|
|
7
|
+
//# sourceMappingURL=current-time-context-helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-time-context-helper.d.ts","sourceRoot":"","sources":["../../src/context-helpers/current-time-context-helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C;;;GAGG;AACH,eAAO,MAAM,wBAAwB,EAAE,eAUtC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prebuilt context helper that provides information about the user's current time.
|
|
3
|
+
* @returns a value to include it, or null/undefined to skip.
|
|
4
|
+
*/
|
|
5
|
+
export const currentTimeContextHelper = () => {
|
|
6
|
+
try {
|
|
7
|
+
const now = new Date();
|
|
8
|
+
return {
|
|
9
|
+
timestamp: now.toString(),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
console.error("prebuiltUserTime failed:", e);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=current-time-context-helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-time-context-helper.js","sourceRoot":"","sources":["../../src/context-helpers/current-time-context-helper.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAoB,GAAG,EAAE;IAC5D,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,OAAO;YACL,SAAS,EAAE,GAAG,CAAC,QAAQ,EAAE;SAC1B,CAAC;IACJ,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,CAAC,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { ContextHelperFn } from \"./types\";\n\n/**\n * Prebuilt context helper that provides information about the user's current time.\n * @returns a value to include it, or null/undefined to skip.\n */\nexport const currentTimeContextHelper: ContextHelperFn = () => {\n try {\n const now = new Date();\n return {\n timestamp: now.toString(),\n };\n } catch (e) {\n console.error(\"prebuiltUserTime failed:\", e);\n return null;\n }\n};\n"]}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export * from "./current-page-context-helper";
|
|
2
|
+
export * from "./current-time-context-helper";
|
|
2
3
|
export * from "./types";
|
|
3
|
-
export * from "./user-page";
|
|
4
|
-
export * from "./user-time";
|
|
5
|
-
/**
|
|
6
|
-
* Pre-built context helpers that can be enabled/disabled
|
|
7
|
-
*/
|
|
8
|
-
export declare const DEFAULT_CONTEXT_HELPERS: AdditionalContextHelper[];
|
|
9
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context-helpers/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context-helpers/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,SAAS,CAAC"}
|
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export * from "./current-page-context-helper";
|
|
2
|
+
export * from "./current-time-context-helper";
|
|
3
3
|
export * from "./types";
|
|
4
|
-
export * from "./user-page";
|
|
5
|
-
export * from "./user-time";
|
|
6
|
-
/**
|
|
7
|
-
* Pre-built context helpers that can be enabled/disabled
|
|
8
|
-
*/
|
|
9
|
-
export const DEFAULT_CONTEXT_HELPERS = [
|
|
10
|
-
{
|
|
11
|
-
name: "userTime",
|
|
12
|
-
enabled: false, // Default to disabled
|
|
13
|
-
run: getUserTimeContext,
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
name: "userPage",
|
|
17
|
-
enabled: false, // Default to disabled
|
|
18
|
-
run: getUserPageContext,
|
|
19
|
-
},
|
|
20
|
-
];
|
|
21
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context-helpers/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context-helpers/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,SAAS,CAAC","sourcesContent":["export * from \"./current-page-context-helper\";\nexport * from \"./current-time-context-helper\";\nexport * from \"./types\";\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global context helpers registry.
|
|
3
|
+
* Consumers can add/remove helpers and resolve additional context anywhere.
|
|
4
|
+
*/
|
|
5
|
+
export type HelperFn = () => any | null | undefined | Promise<any | null | undefined>;
|
|
6
|
+
/**
|
|
7
|
+
* Get the current helpers map (by reference).
|
|
8
|
+
* @returns The current helpers map.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getHelpers(): Record<string, HelperFn>;
|
|
11
|
+
/**
|
|
12
|
+
* Replace the entire helpers map (used for hydration/reset in tests).
|
|
13
|
+
* @param next - The new helpers map.
|
|
14
|
+
*/
|
|
15
|
+
export declare function setHelpers(next: Record<string, HelperFn>): void;
|
|
16
|
+
/**
|
|
17
|
+
* Add or replace a helper.
|
|
18
|
+
* @param name - The name of the helper.
|
|
19
|
+
* @param fn - The helper function.
|
|
20
|
+
*/
|
|
21
|
+
export declare function addHelper(name: string, fn: HelperFn): void;
|
|
22
|
+
/**
|
|
23
|
+
* Remove a helper by name.
|
|
24
|
+
* @param name - The name of the helper.
|
|
25
|
+
*/
|
|
26
|
+
export declare function removeHelper(name: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* Resolve all helpers to AdditionalContext entries, skipping null/undefined and errors.
|
|
29
|
+
* @returns The resolved additional context.
|
|
30
|
+
*/
|
|
31
|
+
export declare function resolveAdditionalContext(): Promise<{
|
|
32
|
+
name: string;
|
|
33
|
+
context: any;
|
|
34
|
+
}[]>;
|
|
35
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/context-helpers/registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,QAAQ,GAAG,MACnB,GAAG,GACH,IAAI,GACJ,SAAS,GACT,OAAO,CAAC,GAAG,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAIpC;;;GAGG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAErD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,QAExD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,QAEnD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,QAExC;AAED;;;GAGG;AACH,wBAAsB,wBAAwB,IAAI,OAAO,CACvD;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAE,EAAE,CACjC,CAkBA"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global context helpers registry.
|
|
3
|
+
* Consumers can add/remove helpers and resolve additional context anywhere.
|
|
4
|
+
*/
|
|
5
|
+
let helpers = {};
|
|
6
|
+
/**
|
|
7
|
+
* Get the current helpers map (by reference).
|
|
8
|
+
* @returns The current helpers map.
|
|
9
|
+
*/
|
|
10
|
+
export function getHelpers() {
|
|
11
|
+
return helpers;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Replace the entire helpers map (used for hydration/reset in tests).
|
|
15
|
+
* @param next - The new helpers map.
|
|
16
|
+
*/
|
|
17
|
+
export function setHelpers(next) {
|
|
18
|
+
helpers = next;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Add or replace a helper.
|
|
22
|
+
* @param name - The name of the helper.
|
|
23
|
+
* @param fn - The helper function.
|
|
24
|
+
*/
|
|
25
|
+
export function addHelper(name, fn) {
|
|
26
|
+
helpers[name] = fn;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Remove a helper by name.
|
|
30
|
+
* @param name - The name of the helper.
|
|
31
|
+
*/
|
|
32
|
+
export function removeHelper(name) {
|
|
33
|
+
delete helpers[name];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Resolve all helpers to AdditionalContext entries, skipping null/undefined and errors.
|
|
37
|
+
* @returns The resolved additional context.
|
|
38
|
+
*/
|
|
39
|
+
export async function resolveAdditionalContext() {
|
|
40
|
+
const entries = Object.entries(helpers);
|
|
41
|
+
if (entries.length === 0)
|
|
42
|
+
return [];
|
|
43
|
+
const results = await Promise.all(entries.map(async ([name, fn]) => {
|
|
44
|
+
try {
|
|
45
|
+
const value = await fn();
|
|
46
|
+
if (value == null)
|
|
47
|
+
return null;
|
|
48
|
+
return { name, context: value };
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error(`Error running context helper ${name}:`, error);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}));
|
|
55
|
+
return results.filter(Boolean);
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRo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","sourcesContent":["/**\n * Global context helpers registry.\n * Consumers can add/remove helpers and resolve additional context anywhere.\n */\n\nexport type HelperFn = () =>\n | any\n | null\n | undefined\n | Promise<any | null | undefined>;\n\nlet helpers: Record<string, HelperFn> = {};\n\n/**\n * Get the current helpers map (by reference).\n * @returns The current helpers map.\n */\nexport function getHelpers(): Record<string, HelperFn> {\n return helpers;\n}\n\n/**\n * Replace the entire helpers map (used for hydration/reset in tests).\n * @param next - The new helpers map.\n */\nexport function setHelpers(next: Record<string, HelperFn>) {\n helpers = next;\n}\n\n/**\n * Add or replace a helper.\n * @param name - The name of the helper.\n * @param fn - The helper function.\n */\nexport function addHelper(name: string, fn: HelperFn) {\n helpers[name] = fn;\n}\n\n/**\n * Remove a helper by name.\n * @param name - The name of the helper.\n */\nexport function removeHelper(name: string) {\n delete helpers[name];\n}\n\n/**\n * Resolve all helpers to AdditionalContext entries, skipping null/undefined and errors.\n * @returns The resolved additional context.\n */\nexport async function resolveAdditionalContext(): Promise<\n { name: string; context: any }[]\n> {\n const entries = Object.entries(helpers);\n if (entries.length === 0) return [];\n\n const results = await Promise.all(\n entries.map(async ([name, fn]) => {\n try {\n const value = await fn();\n if (value == null) return null;\n return { name, context: value };\n } catch (error) {\n console.error(`Error running context helper ${name}:`, error);\n return null;\n }\n }),\n );\n\n return results.filter(Boolean) as { name: string; context: any }[];\n}\n"]}
|