@tambo-ai/react 0.46.5 → 0.47.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.js +9 -5
- package/dist/context-helpers/__tests__/context-helpers-provider.test.js.map +1 -1
- package/dist/context-helpers/registry.d.ts +1 -22
- package/dist/context-helpers/registry.d.ts.map +1 -1
- package/dist/context-helpers/registry.js +1 -35
- package/dist/context-helpers/registry.js.map +1 -1
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.js +0 -34
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.js.map +1 -1
- package/dist/providers/tambo-context-helpers-provider.d.ts +1 -1
- package/dist/providers/tambo-context-helpers-provider.d.ts.map +1 -1
- package/dist/providers/tambo-context-helpers-provider.js +33 -21
- package/dist/providers/tambo-context-helpers-provider.js.map +1 -1
- package/dist/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/dist/providers/tambo-interactable-provider.js +85 -83
- package/dist/providers/tambo-interactable-provider.js.map +1 -1
- package/esm/context-helpers/__tests__/context-helpers-provider.test.js +10 -6
- package/esm/context-helpers/__tests__/context-helpers-provider.test.js.map +1 -1
- package/esm/context-helpers/registry.d.ts +1 -22
- package/esm/context-helpers/registry.d.ts.map +1 -1
- package/esm/context-helpers/registry.js +1 -31
- package/esm/context-helpers/registry.js.map +1 -1
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.js +0 -34
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.js.map +1 -1
- package/esm/providers/tambo-context-helpers-provider.d.ts +1 -1
- package/esm/providers/tambo-context-helpers-provider.d.ts.map +1 -1
- package/esm/providers/tambo-context-helpers-provider.js +35 -23
- package/esm/providers/tambo-context-helpers-provider.js.map +1 -1
- package/esm/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/esm/providers/tambo-interactable-provider.js +85 -83
- package/esm/providers/tambo-interactable-provider.js.map +1 -1
- package/package.json +1 -1
|
@@ -18,7 +18,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
19
|
const react_1 = require("@testing-library/react");
|
|
20
20
|
const react_2 = __importDefault(require("react"));
|
|
21
|
-
const registry_1 = require("../../context-helpers/registry");
|
|
22
21
|
const tambo_context_helpers_provider_1 = require("../../providers/tambo-context-helpers-provider");
|
|
23
22
|
const index_1 = require("../index");
|
|
24
23
|
/**
|
|
@@ -35,7 +34,6 @@ function wrapper(helpers) {
|
|
|
35
34
|
describe("Context Helpers API", () => {
|
|
36
35
|
// Ensure the global registry doesn't leak state between tests
|
|
37
36
|
beforeEach(() => {
|
|
38
|
-
(0, registry_1.setHelpers)({});
|
|
39
37
|
jest.clearAllMocks();
|
|
40
38
|
});
|
|
41
39
|
test("returns empty array when no helpers provided", async () => {
|
|
@@ -104,7 +102,9 @@ describe("Context Helpers API", () => {
|
|
|
104
102
|
// Initially empty
|
|
105
103
|
expect(await result.current.getAdditionalContext()).toHaveLength(0);
|
|
106
104
|
// Add
|
|
107
|
-
|
|
105
|
+
(0, react_1.act)(() => {
|
|
106
|
+
result.current.addContextHelper("dyn", () => ({ x: 10 }));
|
|
107
|
+
});
|
|
108
108
|
let contexts = await result.current.getAdditionalContext();
|
|
109
109
|
expect(contexts).toHaveLength(1);
|
|
110
110
|
expect(contexts[0]).toEqual({
|
|
@@ -112,7 +112,9 @@ describe("Context Helpers API", () => {
|
|
|
112
112
|
context: { x: 10 },
|
|
113
113
|
});
|
|
114
114
|
// Update
|
|
115
|
-
|
|
115
|
+
(0, react_1.act)(() => {
|
|
116
|
+
result.current.addContextHelper("dyn", () => ({ x: 20 }));
|
|
117
|
+
});
|
|
116
118
|
contexts = await result.current.getAdditionalContext();
|
|
117
119
|
expect(contexts).toHaveLength(1);
|
|
118
120
|
expect(contexts[0]).toEqual({
|
|
@@ -120,7 +122,9 @@ describe("Context Helpers API", () => {
|
|
|
120
122
|
context: { x: 20 },
|
|
121
123
|
});
|
|
122
124
|
// Remove
|
|
123
|
-
|
|
125
|
+
(0, react_1.act)(() => {
|
|
126
|
+
result.current.removeContextHelper("dyn");
|
|
127
|
+
});
|
|
124
128
|
contexts = await result.current.getAdditionalContext();
|
|
125
129
|
expect(contexts).toHaveLength(0);
|
|
126
130
|
});
|
|
@@ -1 +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,kDAAoD;AACpD,kDAAiD;AACjD,6DAA4D;AAC5D,mGAGwD;AACxD,oCAKkB;AAElB;;;;GAIG;AACH,SAAS,OAAO,CAAC,OAAyC;IACxD,8CAA8C;IAC9C,OAAO,CAAC,EAAE,QAAQ,EAAqB,EAAE,EAAE,CACzC,eAAK,CAAC,aAAa,CACjB,4DAA2B,EAC3B;QACE,cAAc,EAAE,OAAO;KACxB,EACD,QAAQ,CACT,CAAC;AACN,CAAC;AAED,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;IACnC,8DAA8D;IAC9D,UAAU,CAAC,GAAG,EAAE;QACd,IAAA,qBAAU,EAAC,EAAE,CAAC,CAAC;QACf,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,8CAA8C,EAAE,KAAK,IAAI,EAAE;QAC9D,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,kBAAU,EAAC,GAAG,EAAE,CAAC,IAAA,uDAAsB,GAAE,EAAE;YAC5D,OAAO,EAAE,OAAO,EAAE;SACnB,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC;QAC7D,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3C,MA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AAC,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
|
+
{"version":3,"file":"context-helpers-provider.test.js","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers-provider.test.tsx"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;;;AAEH,kDAAyD;AACzD,kDA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C,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 { act, renderHook } from \"@testing-library/react\";\nimport React, { PropsWithChildren } from \"react\";\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 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 act(() => {\n result.current.addContextHelper(\"dyn\", () => ({ x: 10 }));\n });\n\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 act(() => {\n result.current.addContextHelper(\"dyn\", () => ({ x: 20 }));\n });\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 act(() => {\n result.current.removeContextHelper(\"dyn\");\n });\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"]}
|
|
@@ -3,32 +3,11 @@
|
|
|
3
3
|
* Consumers can add/remove helpers and resolve additional context anywhere.
|
|
4
4
|
*/
|
|
5
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
6
|
/**
|
|
28
7
|
* Resolve all helpers to AdditionalContext entries, skipping null/undefined and errors.
|
|
29
8
|
* @returns The resolved additional context.
|
|
30
9
|
*/
|
|
31
|
-
export declare function resolveAdditionalContext(): Promise<{
|
|
10
|
+
export declare function resolveAdditionalContext(helpers: Record<string, HelperFn>): Promise<{
|
|
32
11
|
name: string;
|
|
33
12
|
context: any;
|
|
34
13
|
}[]>;
|
|
@@ -1 +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;
|
|
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;AAEpC;;;GAGG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,GAChC,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAE,EAAE,CAAC,CAkB3C"}
|
|
@@ -4,46 +4,12 @@
|
|
|
4
4
|
* Consumers can add/remove helpers and resolve additional context anywhere.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.getHelpers = getHelpers;
|
|
8
|
-
exports.setHelpers = setHelpers;
|
|
9
|
-
exports.addHelper = addHelper;
|
|
10
|
-
exports.removeHelper = removeHelper;
|
|
11
7
|
exports.resolveAdditionalContext = resolveAdditionalContext;
|
|
12
|
-
let helpers = {};
|
|
13
|
-
/**
|
|
14
|
-
* Get the current helpers map (by reference).
|
|
15
|
-
* @returns The current helpers map.
|
|
16
|
-
*/
|
|
17
|
-
function getHelpers() {
|
|
18
|
-
return helpers;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Replace the entire helpers map (used for hydration/reset in tests).
|
|
22
|
-
* @param next - The new helpers map.
|
|
23
|
-
*/
|
|
24
|
-
function setHelpers(next) {
|
|
25
|
-
helpers = next;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Add or replace a helper.
|
|
29
|
-
* @param name - The name of the helper.
|
|
30
|
-
* @param fn - The helper function.
|
|
31
|
-
*/
|
|
32
|
-
function addHelper(name, fn) {
|
|
33
|
-
helpers[name] = fn;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Remove a helper by name.
|
|
37
|
-
* @param name - The name of the helper.
|
|
38
|
-
*/
|
|
39
|
-
function removeHelper(name) {
|
|
40
|
-
delete helpers[name];
|
|
41
|
-
}
|
|
42
8
|
/**
|
|
43
9
|
* Resolve all helpers to AdditionalContext entries, skipping null/undefined and errors.
|
|
44
10
|
* @returns The resolved additional context.
|
|
45
11
|
*/
|
|
46
|
-
async function resolveAdditionalContext() {
|
|
12
|
+
async function resolveAdditionalContext(helpers) {
|
|
47
13
|
const entries = Object.entries(helpers);
|
|
48
14
|
if (entries.length === 0)
|
|
49
15
|
return [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/context-helpers/registry.ts"],"names":[],"mappings":";AAAA;;;GAGG;;
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/context-helpers/registry.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAYH,4DAoBC;AAxBD;;;GAGG;AACI,KAAK,UAAU,wBAAwB,CAC5C,OAAiC;IAEjC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACxC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAEpC,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAC/B,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE;QAC/B,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,EAAE,EAAE,CAAC;YACzB,IAAI,KAAK,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;YAC/B,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,gCAAgC,IAAI,GAAG,EAAE,KAAK,CAAC,CAAC;YAC9D,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC,CAAC,CACH,CAAC;IAEF,OAAO,OAAO,CAAC,MAAM,CAAC,OAAO,CAAqC,CAAC;AACrE,CAAC","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\n/**\n * Resolve all helpers to AdditionalContext entries, skipping null/undefined and errors.\n * @returns The resolved additional context.\n */\nexport async function resolveAdditionalContext(\n helpers: Record<string, HelperFn>,\n): Promise<{ name: string; context: any }[]> {\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"]}
|
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const react_1 = require("@testing-library/react");
|
|
7
7
|
const react_2 = __importDefault(require("react"));
|
|
8
8
|
const context_helpers_1 = require("../../context-helpers");
|
|
9
|
-
const registry_1 = require("../../context-helpers/registry");
|
|
10
9
|
const tambo_context_helpers_provider_1 = require("../tambo-context-helpers-provider");
|
|
11
10
|
/**
|
|
12
11
|
* Test suite for TamboContextHelpersProvider (simplified API, registry-backed)
|
|
@@ -23,43 +22,11 @@ const tambo_context_helpers_provider_1 = require("../tambo-context-helpers-provi
|
|
|
23
22
|
describe("TamboContextHelpersProvider", () => {
|
|
24
23
|
// Ensure registry is clean for each test to avoid cross-test contamination
|
|
25
24
|
beforeEach(() => {
|
|
26
|
-
(0, registry_1.setHelpers)({});
|
|
27
25
|
jest.clearAllMocks();
|
|
28
26
|
});
|
|
29
27
|
// Base wrapper with no helpers provided
|
|
30
28
|
const wrapper = ({ children }) => (react_2.default.createElement(tambo_context_helpers_provider_1.TamboContextHelpersProvider, null, children));
|
|
31
29
|
describe("useTamboContextHelpers", () => {
|
|
32
|
-
/**
|
|
33
|
-
* NOTE: The hook is registry-backed and safe outside provider. It should not throw.
|
|
34
|
-
* This replaces the previous behavior that threw without a provider.
|
|
35
|
-
*/
|
|
36
|
-
it("should be safe outside provider (registry-backed no provider)", async () => {
|
|
37
|
-
const consoleSpy = jest
|
|
38
|
-
.spyOn(console, "error")
|
|
39
|
-
.mockImplementation(() => { });
|
|
40
|
-
const { result } = (0, react_1.renderHook)(() => (0, tambo_context_helpers_provider_1.useTamboContextHelpers)());
|
|
41
|
-
// Should return callable functions
|
|
42
|
-
expect(typeof result.current.getAdditionalContext).toBe("function");
|
|
43
|
-
expect(typeof result.current.getContextHelpers).toBe("function");
|
|
44
|
-
expect(typeof result.current.addContextHelper).toBe("function");
|
|
45
|
-
expect(typeof result.current.removeContextHelper).toBe("function");
|
|
46
|
-
// Starts empty
|
|
47
|
-
expect(await result.current.getAdditionalContext()).toHaveLength(0);
|
|
48
|
-
// Add a helper and verify
|
|
49
|
-
(0, react_1.act)(() => {
|
|
50
|
-
result.current.addContextHelper("outsideHelper", () => ({ ok: true }));
|
|
51
|
-
});
|
|
52
|
-
const contexts = await result.current.getAdditionalContext();
|
|
53
|
-
expect(contexts).toContainEqual({
|
|
54
|
-
name: "outsideHelper",
|
|
55
|
-
context: { ok: true },
|
|
56
|
-
});
|
|
57
|
-
// Cleanup
|
|
58
|
-
(0, react_1.act)(() => {
|
|
59
|
-
result.current.removeContextHelper("outsideHelper");
|
|
60
|
-
});
|
|
61
|
-
consoleSpy.mockRestore();
|
|
62
|
-
});
|
|
63
30
|
/**
|
|
64
31
|
* Verifies that the hook returns the expected API functions when used within a provider.
|
|
65
32
|
*/
|
|
@@ -134,7 +101,6 @@ describe("TamboContextHelpersProvider", () => {
|
|
|
134
101
|
*/
|
|
135
102
|
describe("Custom Context Helpers via contextHelpers config", () => {
|
|
136
103
|
beforeEach(() => {
|
|
137
|
-
(0, registry_1.setHelpers)({});
|
|
138
104
|
jest.clearAllMocks();
|
|
139
105
|
});
|
|
140
106
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-context-helpers-provider.test.js","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-context-helpers-provider.test.tsx"],"names":[],"mappings":";;;;;AAAA,kDAAyD;AACzD,kDAA0B;AAC1B,2DAG+B;AAC/B,6DAA4D;AAC5D,sFAG2C;AAE3C;;;;;;;;;;;GAWG;AACH,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IAC3C,2EAA2E;IAC3E,UAAU,CAAC,GAAG,EAAE;QACd,IAAA,qBAAU,EAAC,EAAE,CAAC,CAAC;QACf,IAAI,CAA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CAAC,IAAA,uDAAsB,GAAE,EAAE;YAC5D,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CACzB,8BAAC,4DAA2B,IAC1B,cAAc,EAAE;oBACd,UAAU,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;iBAC7C,IAEA,QAAQ,CACmB,CAC/B;SACF,CAAC,CAAC;QAEH,yBAAyB;QACzB,IAAI,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC;QAC3D,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC;YAC9B,IAAI,EAAE,YAAY;YAClB,OAAO,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE;SAC5B,CAAC,CAAC;QAEH,oBAAoB;QACpB,IAAA,WAAG,EAAC,GAAG,EAAE;YACP,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,YAAY,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC;gBACzD,OAAO,EAAE,IAAI;aACd,CAAC,CAAC,CAAC;QACN,CAAC,CAAC,CAAC;QAEH,wBAAwB;QACxB,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC;QACvD,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC;YAC9B,IAAI,EAAE,YAAY;YAClB,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;SAC3B,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport {\n currentPageContextHelper,\n currentTimeContextHelper,\n} from \"../../context-helpers\";\nimport { setHelpers } from \"../../context-helpers/registry\";\nimport {\n TamboContextHelpersProvider,\n useTamboContextHelpers,\n} from \"../tambo-context-helpers-provider\";\n\n/**\n * Test suite for TamboContextHelpersProvider (simplified API, registry-backed)\n *\n * The simplified API:\n * - Accepts a plain map of { key: () => any | null | undefined | Promise<any | null | undefined> }.\n * - The key becomes the context name sent to the model.\n * - Returning null/undefined from a helper skips inclusion.\n * - Prebuilt helpers are just functions (e.g., prebuiltUserTime, prebuiltUserPage).\n *\n * The hook is now registry-backed and safe to call outside a provider. When used\n * outside a provider, it proxies to a global registry and still works.\n */\ndescribe(\"TamboContextHelpersProvider\", () => {\n // Ensure registry is clean for each test to avoid cross-test contamination\n beforeEach(() => {\n setHelpers({});\n jest.clearAllMocks();\n });\n\n // Base wrapper with no helpers provided\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider>{children}</TamboContextHelpersProvider>\n );\n\n describe(\"useTamboContextHelpers\", () => {\n /**\n * NOTE: The hook is registry-backed and safe outside provider. It should not throw.\n * This replaces the previous behavior that threw without a provider.\n */\n it(\"should be safe outside provider (registry-backed no provider)\", async () => {\n const consoleSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n const { result } = renderHook(() => useTamboContextHelpers());\n\n // Should return callable functions\n expect(typeof result.current.getAdditionalContext).toBe(\"function\");\n expect(typeof result.current.getContextHelpers).toBe(\"function\");\n expect(typeof result.current.addContextHelper).toBe(\"function\");\n expect(typeof result.current.removeContextHelper).toBe(\"function\");\n\n // Starts empty\n expect(await result.current.getAdditionalContext()).toHaveLength(0);\n\n // Add a helper and verify\n act(() => {\n result.current.addContextHelper(\"outsideHelper\", () => ({ ok: true }));\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"outsideHelper\",\n context: { ok: true },\n });\n\n // Cleanup\n act(() => {\n result.current.removeContextHelper(\"outsideHelper\");\n });\n consoleSpy.mockRestore();\n });\n\n /**\n * Verifies that the hook returns the expected API functions when used within a provider.\n */\n it(\"should provide context helpers functions (inside provider)\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n expect(result.current).toHaveProperty(\"getAdditionalContext\");\n expect(result.current).toHaveProperty(\"getContextHelpers\");\n expect(result.current).toHaveProperty(\"addContextHelper\");\n expect(result.current).toHaveProperty(\"removeContextHelper\");\n });\n\n /**\n * With no helpers provided, no additional context should be returned.\n */\n it(\"should return no additional context when no helpers are provided\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toHaveLength(0);\n });\n\n /**\n * When helpers are provided, getAdditionalContext should aggregate them.\n * Note: prebuiltUserPage may be null in non-browser envs, so we only assert userTime when present.\n */\n it(\"should get additional context when helpers are provided\", async () => {\n const withHelpers = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n userTime: currentTimeContextHelper,\n userPage: currentPageContextHelper,\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n );\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: withHelpers,\n });\n\n const contexts = await result.current.getAdditionalContext();\n const names = contexts.map((c) => c.name);\n\n // userTime should be present from prebuilt helper\n expect(names).toContain(\"userTime\");\n // userPage may be absent on server; do not assert strictly\n });\n\n /**\n * Errors thrown by helper functions should be caught and skipped, not crash the system.\n */\n it(\"should handle errors in context helper functions gracefully\", async () => {\n const consoleErrorSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n\n const badHelper = () => {\n throw new Error(\"Test error\");\n };\n\n const withBadHelper = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider contextHelpers={{ badHelper }}>\n {children}\n </TamboContextHelpersProvider>\n );\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: withBadHelper,\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts.length).toBe(0);\n expect(consoleErrorSpy).toHaveBeenCalledWith(\n expect.stringContaining(\"Error running context helper badHelper\"),\n expect.any(Error),\n );\n consoleErrorSpy.mockRestore();\n });\n });\n});\n\n/**\n * Test suite for Custom Context Helpers using the simplified API.\n *\n * Focuses on:\n * - Passing custom helpers via the provider prop.\n * - Overriding prebuilt helpers with custom implementations.\n * - Dynamic add/remove helper management at runtime.\n * - Supporting both sync and async helpers.\n * - Using the config key as the context name.\n * - Graceful error handling for custom helpers.\n */\ndescribe(\"Custom Context Helpers via contextHelpers config\", () => {\n beforeEach(() => {\n setHelpers({});\n jest.clearAllMocks();\n });\n\n /**\n * Custom helpers can be added through the provider configuration.\n * The key becomes the context name and the function returns the raw value.\n */\n it(\"should accept custom helpers through contextHelpers config\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n customData: async () => ({ custom: \"value\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"customData\",\n context: { custom: \"value\" },\n });\n });\n\n /**\n * Built-in helpers can be overridden by providing a function under the same key.\n */\n it(\"should allow custom helpers to override built-in ones\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n userTime: async () => ({ customTime: \"override\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n const userTimeContext = contexts.find((c) => c.name === \"userTime\");\n expect(userTimeContext?.context).toEqual({ customTime: \"override\" });\n });\n\n /**\n * Helpers can be added dynamically at runtime via addContextHelper.\n */\n it(\"should handle dynamic addition of custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider contextHelpers={{}}>\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n // Initially none\n expect(await result.current.getAdditionalContext()).toHaveLength(0);\n\n act(() => {\n result.current.addContextHelper(\"dynamicHelper\", async () => ({\n dynamic: true,\n }));\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"dynamicHelper\",\n context: { dynamic: true },\n });\n });\n\n /**\n * Helpers can be removed dynamically via removeContextHelper.\n */\n it(\"should handle dynamic removal of context helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n customHelper: async () => ({ test: \"data\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n // Verify exists\n let contexts = await result.current.getAdditionalContext();\n expect(contexts.find((c) => c.name === \"customHelper\")).toBeDefined();\n\n // Remove\n act(() => {\n result.current.removeContextHelper(\"customHelper\");\n });\n\n // Verify removed\n contexts = await result.current.getAdditionalContext();\n expect(contexts.find((c) => c.name === \"customHelper\")).toBeUndefined();\n });\n\n /**\n * Multiple custom helpers should be supported; helpers returning null/undefined are skipped.\n */\n it(\"should handle multiple custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n helper1: async () => ({ data: \"one\" }),\n helper2: async () => ({ data: \"two\" }),\n helper3: () => null, // disabled by returning null\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"helper1\",\n context: { data: \"one\" },\n });\n expect(contexts).toContainEqual({\n name: \"helper2\",\n context: { data: \"two\" },\n });\n expect(contexts.find((c) => c.name === \"helper3\")).toBeUndefined();\n });\n\n /**\n * Both synchronous and asynchronous helpers should be supported.\n */\n it(\"should handle sync and async custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n syncHelper: () => ({ sync: true }),\n asyncHelper: async () => {\n await new Promise((resolve) => setTimeout(resolve, 10));\n return { async: true };\n },\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"syncHelper\",\n context: { sync: true },\n });\n expect(contexts).toContainEqual({\n name: \"asyncHelper\",\n context: { async: true },\n });\n });\n\n /**\n * The key used in the contextHelpers map becomes the context name.\n */\n it(\"should use key name as context name for custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n myCustomKey: async () => ({ value: \"test\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"myCustomKey\",\n context: { value: \"test\" },\n });\n });\n\n /**\n * Errors thrown by custom helpers should be logged and skipped, not crash the system.\n */\n it(\"should handle errors in custom helpers gracefully\", async () => {\n const consoleErrorSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n errorHelper: async () => {\n throw new Error(\"Custom helper error\");\n },\n goodHelper: async () => ({ good: \"data\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"goodHelper\",\n context: { good: \"data\" },\n });\n expect(contexts.find((c) => c.name === \"errorHelper\")).toBeUndefined();\n expect(consoleErrorSpy).toHaveBeenCalledWith(\n expect.stringContaining(\"Error running context helper errorHelper\"),\n expect.any(Error),\n );\n consoleErrorSpy.mockRestore();\n });\n\n /**\n * Removing non-existent helpers should be a no-op without throwing errors.\n */\n it(\"should handle removing non-existent helper gracefully\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider contextHelpers={{}}>\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n expect(() => {\n act(() => {\n result.current.removeContextHelper(\"nonExistent\");\n });\n }).not.toThrow();\n });\n\n /**\n * Adding with an existing name should update/replace the helper implementation.\n */\n it(\"should allow updating existing helper via addContextHelper\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n testHelper: async () => ({ original: true }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n // Verify original helper\n let contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"testHelper\",\n context: { original: true },\n });\n\n // Update the helper\n act(() => {\n result.current.addContextHelper(\"testHelper\", async () => ({\n updated: true,\n }));\n });\n\n // Verify updated helper\n contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"testHelper\",\n context: { updated: true },\n });\n });\n});\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-context-helpers-provider.test.js","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-context-helpers-provider.test.tsx"],"names":[],"mappings":";;;;;AAAA,kDAAyD;AACzD,kDAA0B;AAC1B,2DAG+B;AAC/B,sFAG2C;AAE3C;;;;;;;;;;;GAWG;AACH,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IAC3C,2EAA2E;IAC3E,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,wCAAwC;IACxC,MAAM,OAAO,GAAG,CAAC,EAAE,QAAQ,EAAiC,EAAE,EAAE,CAAC,CAC/D,8BAAC,4DAA2B,QAAE,QAAQ,CAA+B,CACtE,CAAC;IAEF,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;QACtC;;WAEG;QACH,EAAE,CAAC,4DAA4D,EAAE,GAAG,EAAE;YACpE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,kBAAU,EAAC,GAAG,EAAE,CAAC,IAAA,uDAAsB,GAAE,EAAE;gBAC5D,OAAO;aACR,CAAC,CAAC;YACH,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,sBAAsB,CAAC,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MAAM,MAAM,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC;QACvD,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC;YAC9B,IAAI,EAAE,YAAY;YAClB,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;SAC3B,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport {\n currentPageContextHelper,\n currentTimeContextHelper,\n} from \"../../context-helpers\";\nimport {\n TamboContextHelpersProvider,\n useTamboContextHelpers,\n} from \"../tambo-context-helpers-provider\";\n\n/**\n * Test suite for TamboContextHelpersProvider (simplified API, registry-backed)\n *\n * The simplified API:\n * - Accepts a plain map of { key: () => any | null | undefined | Promise<any | null | undefined> }.\n * - The key becomes the context name sent to the model.\n * - Returning null/undefined from a helper skips inclusion.\n * - Prebuilt helpers are just functions (e.g., prebuiltUserTime, prebuiltUserPage).\n *\n * The hook is now registry-backed and safe to call outside a provider. When used\n * outside a provider, it proxies to a global registry and still works.\n */\ndescribe(\"TamboContextHelpersProvider\", () => {\n // Ensure registry is clean for each test to avoid cross-test contamination\n beforeEach(() => {\n jest.clearAllMocks();\n });\n\n // Base wrapper with no helpers provided\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider>{children}</TamboContextHelpersProvider>\n );\n\n describe(\"useTamboContextHelpers\", () => {\n /**\n * Verifies that the hook returns the expected API functions when used within a provider.\n */\n it(\"should provide context helpers functions (inside provider)\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n expect(result.current).toHaveProperty(\"getAdditionalContext\");\n expect(result.current).toHaveProperty(\"getContextHelpers\");\n expect(result.current).toHaveProperty(\"addContextHelper\");\n expect(result.current).toHaveProperty(\"removeContextHelper\");\n });\n\n /**\n * With no helpers provided, no additional context should be returned.\n */\n it(\"should return no additional context when no helpers are provided\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toHaveLength(0);\n });\n\n /**\n * When helpers are provided, getAdditionalContext should aggregate them.\n * Note: prebuiltUserPage may be null in non-browser envs, so we only assert userTime when present.\n */\n it(\"should get additional context when helpers are provided\", async () => {\n const withHelpers = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n userTime: currentTimeContextHelper,\n userPage: currentPageContextHelper,\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n );\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: withHelpers,\n });\n\n const contexts = await result.current.getAdditionalContext();\n const names = contexts.map((c) => c.name);\n\n // userTime should be present from prebuilt helper\n expect(names).toContain(\"userTime\");\n // userPage may be absent on server; do not assert strictly\n });\n\n /**\n * Errors thrown by helper functions should be caught and skipped, not crash the system.\n */\n it(\"should handle errors in context helper functions gracefully\", async () => {\n const consoleErrorSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n\n const badHelper = () => {\n throw new Error(\"Test error\");\n };\n\n const withBadHelper = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider contextHelpers={{ badHelper }}>\n {children}\n </TamboContextHelpersProvider>\n );\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: withBadHelper,\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts.length).toBe(0);\n expect(consoleErrorSpy).toHaveBeenCalledWith(\n expect.stringContaining(\"Error running context helper badHelper\"),\n expect.any(Error),\n );\n consoleErrorSpy.mockRestore();\n });\n });\n});\n\n/**\n * Test suite for Custom Context Helpers using the simplified API.\n *\n * Focuses on:\n * - Passing custom helpers via the provider prop.\n * - Overriding prebuilt helpers with custom implementations.\n * - Dynamic add/remove helper management at runtime.\n * - Supporting both sync and async helpers.\n * - Using the config key as the context name.\n * - Graceful error handling for custom helpers.\n */\ndescribe(\"Custom Context Helpers via contextHelpers config\", () => {\n beforeEach(() => {\n jest.clearAllMocks();\n });\n\n /**\n * Custom helpers can be added through the provider configuration.\n * The key becomes the context name and the function returns the raw value.\n */\n it(\"should accept custom helpers through contextHelpers config\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n customData: async () => ({ custom: \"value\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"customData\",\n context: { custom: \"value\" },\n });\n });\n\n /**\n * Built-in helpers can be overridden by providing a function under the same key.\n */\n it(\"should allow custom helpers to override built-in ones\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n userTime: async () => ({ customTime: \"override\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n const userTimeContext = contexts.find((c) => c.name === \"userTime\");\n expect(userTimeContext?.context).toEqual({ customTime: \"override\" });\n });\n\n /**\n * Helpers can be added dynamically at runtime via addContextHelper.\n */\n it(\"should handle dynamic addition of custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider contextHelpers={{}}>\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n // Initially none\n expect(await result.current.getAdditionalContext()).toHaveLength(0);\n\n act(() => {\n result.current.addContextHelper(\"dynamicHelper\", async () => ({\n dynamic: true,\n }));\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"dynamicHelper\",\n context: { dynamic: true },\n });\n });\n\n /**\n * Helpers can be removed dynamically via removeContextHelper.\n */\n it(\"should handle dynamic removal of context helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n customHelper: async () => ({ test: \"data\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n // Verify exists\n let contexts = await result.current.getAdditionalContext();\n expect(contexts.find((c) => c.name === \"customHelper\")).toBeDefined();\n\n // Remove\n act(() => {\n result.current.removeContextHelper(\"customHelper\");\n });\n\n // Verify removed\n contexts = await result.current.getAdditionalContext();\n expect(contexts.find((c) => c.name === \"customHelper\")).toBeUndefined();\n });\n\n /**\n * Multiple custom helpers should be supported; helpers returning null/undefined are skipped.\n */\n it(\"should handle multiple custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n helper1: async () => ({ data: \"one\" }),\n helper2: async () => ({ data: \"two\" }),\n helper3: () => null, // disabled by returning null\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"helper1\",\n context: { data: \"one\" },\n });\n expect(contexts).toContainEqual({\n name: \"helper2\",\n context: { data: \"two\" },\n });\n expect(contexts.find((c) => c.name === \"helper3\")).toBeUndefined();\n });\n\n /**\n * Both synchronous and asynchronous helpers should be supported.\n */\n it(\"should handle sync and async custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n syncHelper: () => ({ sync: true }),\n asyncHelper: async () => {\n await new Promise((resolve) => setTimeout(resolve, 10));\n return { async: true };\n },\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"syncHelper\",\n context: { sync: true },\n });\n expect(contexts).toContainEqual({\n name: \"asyncHelper\",\n context: { async: true },\n });\n });\n\n /**\n * The key used in the contextHelpers map becomes the context name.\n */\n it(\"should use key name as context name for custom helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n myCustomKey: async () => ({ value: \"test\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"myCustomKey\",\n context: { value: \"test\" },\n });\n });\n\n /**\n * Errors thrown by custom helpers should be logged and skipped, not crash the system.\n */\n it(\"should handle errors in custom helpers gracefully\", async () => {\n const consoleErrorSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n errorHelper: async () => {\n throw new Error(\"Custom helper error\");\n },\n goodHelper: async () => ({ good: \"data\" }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n const contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"goodHelper\",\n context: { good: \"data\" },\n });\n expect(contexts.find((c) => c.name === \"errorHelper\")).toBeUndefined();\n expect(consoleErrorSpy).toHaveBeenCalledWith(\n expect.stringContaining(\"Error running context helper errorHelper\"),\n expect.any(Error),\n );\n consoleErrorSpy.mockRestore();\n });\n\n /**\n * Removing non-existent helpers should be a no-op without throwing errors.\n */\n it(\"should handle removing non-existent helper gracefully\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider contextHelpers={{}}>\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n expect(() => {\n act(() => {\n result.current.removeContextHelper(\"nonExistent\");\n });\n }).not.toThrow();\n });\n\n /**\n * Adding with an existing name should update/replace the helper implementation.\n */\n it(\"should allow updating existing helper via addContextHelper\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: ({ children }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n testHelper: async () => ({ original: true }),\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n ),\n });\n\n // Verify original helper\n let contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"testHelper\",\n context: { original: true },\n });\n\n // Update the helper\n act(() => {\n result.current.addContextHelper(\"testHelper\", async () => ({\n updated: true,\n }));\n });\n\n // Verify updated helper\n contexts = await result.current.getAdditionalContext();\n expect(contexts).toContainEqual({\n name: \"testHelper\",\n context: { updated: true },\n });\n });\n});\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from "react";
|
|
2
|
-
import { AdditionalContext,
|
|
2
|
+
import { AdditionalContext, ContextHelperFn, ContextHelpers } from "../context-helpers";
|
|
3
3
|
export interface TamboContextHelpersProviderProps {
|
|
4
4
|
/**
|
|
5
5
|
* A dictionary of context helper functions.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-context-helpers-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-context-helpers-provider.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAEZ,iBAAiB,
|
|
1
|
+
{"version":3,"file":"tambo-context-helpers-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-context-helpers-provider.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAEZ,iBAAiB,EAMlB,MAAM,OAAO,CAAC;AACf,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,cAAc,EACf,MAAM,oBAAoB,CAAC;AAG5B,MAAM,WAAW,gCAAgC;IAC/C;;;;OAIG;IACH,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;AAED,MAAM,WAAW,+BAA+B;IAC9C,iEAAiE;IACjE,oBAAoB,EAAE,MAAM,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC;IACzD,0CAA0C;IAC1C,iBAAiB,EAAE,MAAM,cAAc,CAAC;IACxC,6CAA6C;IAC7C,gBAAgB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,KAAK,IAAI,CAAC;IAClE,sCAAsC;IACtC,mBAAmB,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7C;AAKD;;;;;;;;GAQG;AACH,eAAO,MAAM,2BAA2B,EAAE,KAAK,CAAC,EAAE,CAChD,iBAAiB,CAAC,gCAAgC,CAAC,CAkEpD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,uCAmBlC,CAAC"}
|
|
@@ -48,38 +48,42 @@ const TamboContextHelpersContext = (0, react_1.createContext)(null);
|
|
|
48
48
|
* @returns The provider that exposes context helper APIs via useTamboContextHelpers.
|
|
49
49
|
*/
|
|
50
50
|
const TamboContextHelpersProvider = ({ children, contextHelpers }) => {
|
|
51
|
+
const [helpers, setHelpers] = (0, react_1.useState)({});
|
|
52
|
+
const addContextHelper = (0, react_1.useCallback)((name, fn) => {
|
|
53
|
+
setHelpers((prev) => ({ ...prev, [name]: fn }));
|
|
54
|
+
}, []);
|
|
55
|
+
const removeContextHelper = (0, react_1.useCallback)((name, fn) => {
|
|
56
|
+
setHelpers((prev) => {
|
|
57
|
+
const { [name]: registeredFn, ...rest } = prev;
|
|
58
|
+
if (fn === undefined || registeredFn === fn) {
|
|
59
|
+
return rest;
|
|
60
|
+
}
|
|
61
|
+
return prev;
|
|
62
|
+
});
|
|
63
|
+
}, []);
|
|
51
64
|
// Hydrate the global registry with initial helpers (runs on prop changes)
|
|
52
|
-
react_1.
|
|
65
|
+
(0, react_1.useEffect)(() => {
|
|
53
66
|
const addedEntries = [];
|
|
54
67
|
if (contextHelpers) {
|
|
55
68
|
for (const [name, fn] of Object.entries(contextHelpers)) {
|
|
56
|
-
(
|
|
69
|
+
addContextHelper(name, fn);
|
|
57
70
|
addedEntries.push([name, fn]);
|
|
58
71
|
}
|
|
59
72
|
}
|
|
60
73
|
return () => {
|
|
61
|
-
const current = (0, registry_1.getHelpers)();
|
|
62
74
|
for (const [name, fn] of addedEntries) {
|
|
63
75
|
// Only remove if the registry still points to the same function
|
|
64
|
-
|
|
65
|
-
(0, registry_1.removeHelper)(name);
|
|
66
|
-
}
|
|
76
|
+
removeContextHelper(name, fn);
|
|
67
77
|
}
|
|
68
78
|
};
|
|
69
|
-
}, [contextHelpers]);
|
|
79
|
+
}, [addContextHelper, contextHelpers, removeContextHelper]);
|
|
70
80
|
const getAdditionalContext = (0, react_1.useCallback)(async () => {
|
|
71
|
-
const contexts = await (0, registry_1.resolveAdditionalContext)();
|
|
81
|
+
const contexts = await (0, registry_1.resolveAdditionalContext)(helpers);
|
|
72
82
|
return contexts;
|
|
73
|
-
}, []);
|
|
83
|
+
}, [helpers]);
|
|
74
84
|
const getContextHelpers = (0, react_1.useCallback)(() => {
|
|
75
|
-
return
|
|
76
|
-
}, []);
|
|
77
|
-
const addContextHelper = (0, react_1.useCallback)((name, helper) => {
|
|
78
|
-
(0, registry_1.addHelper)(name, helper);
|
|
79
|
-
}, []);
|
|
80
|
-
const removeContextHelper = (0, react_1.useCallback)((name) => {
|
|
81
|
-
(0, registry_1.removeHelper)(name);
|
|
82
|
-
}, []);
|
|
85
|
+
return helpers;
|
|
86
|
+
}, [helpers]);
|
|
83
87
|
const value = (0, react_1.useMemo)(() => ({
|
|
84
88
|
getAdditionalContext,
|
|
85
89
|
getContextHelpers,
|
|
@@ -105,10 +109,18 @@ const useTamboContextHelpers = () => {
|
|
|
105
109
|
return context;
|
|
106
110
|
// Fallback to global registry so the API is standalone outside any provider
|
|
107
111
|
return {
|
|
108
|
-
getAdditionalContext: async () =>
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
getAdditionalContext: async () => {
|
|
113
|
+
throw new Error("No provider found");
|
|
114
|
+
},
|
|
115
|
+
getContextHelpers: () => {
|
|
116
|
+
throw new Error("No provider found");
|
|
117
|
+
},
|
|
118
|
+
addContextHelper: (_name, _helper) => {
|
|
119
|
+
throw new Error("No provider found");
|
|
120
|
+
},
|
|
121
|
+
removeContextHelper: (_name) => {
|
|
122
|
+
throw new Error("No provider found");
|
|
123
|
+
},
|
|
112
124
|
};
|
|
113
125
|
};
|
|
114
126
|
exports.useTamboContextHelpers = useTamboContextHelpers;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-context-helpers-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-context-helpers-provider.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACb,+
|
|
1
|
+
{"version":3,"file":"tambo-context-helpers-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-context-helpers-provider.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACb,+CAQe;AAMf,0DAAuE;AAsBvE,MAAM,0BAA0B,GAC9B,IAAA,qBAAa,EAAyC,IAAI,CAAC,CAAC;AAE9D;;;;;;;;GAQG;AACI,MAAM,2BAA2B,GAEpC,CAAC,EAAE,QAAQ,EAAE,cAAc,EAAE,EAAE,EAAE;IACnC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAkC,EAAE,CAAC,CAAC;IAC5E,MAAM,gBAAgB,GAAG,IAAA,mBAAW,EAAC,CAAC,IAAY,EAAE,EAAmB,EAAE,EAAE;QACzE,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IAClD,CAAC,EAAE,EAAE,CAAC,CAAC;IACP,MAAM,mBAAmB,GAAG,IAAA,mBAAW,EACrC,CAAC,IAAY,EAAE,EAAoB,EAAE,EAAE;QACrC,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE;YAClB,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,YAAY,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC;YAC/C,IAAI,EAAE,KAAK,SAAS,IAAI,YAAY,KAAK,EAA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B,CAAC,EACF;QACE,oBAAoB;QACpB,iBAAiB;QACjB,gBAAgB;QAChB,mBAAmB;KACpB,CACF,CAAC;IAEF,OAAO,CACL,8BAAC,0BAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC9C,QAAQ,CAC2B,CACvC,CAAC;AACJ,CAAC,CAAC;AAnEW,QAAA,2BAA2B,+BAmEtC;AAEF;;;;GAIG;AACI,MAAM,sBAAsB,GAAG,GAAG,EAAE;IACzC,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,0BAA0B,CAAC,CAAC;IACvD,IAAI,OAAO;QAAE,OAAO,OAAO,CAAC;IAE5B,4EAA4E;IAC5E,OAAO;QACL,oBAAoB,EAAE,KAAK,IAAI,EAAE;YAC/B,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QACD,iBAAiB,EAAE,GAAG,EAAE;YACtB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QACD,gBAAgB,EAAE,CAAC,KAAa,EAAE,OAAwB,EAAE,EAAE;YAC5D,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QACD,mBAAmB,EAAE,CAAC,KAAa,EAAE,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;KACiC,CAAC;AACvC,CAAC,CAAC;AAnBW,QAAA,sBAAsB,0BAmBjC","sourcesContent":["\"use client\";\nimport React, {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n} from \"react\";\nimport {\n AdditionalContext,\n ContextHelperFn,\n ContextHelpers,\n} from \"../context-helpers\";\nimport { resolveAdditionalContext } from \"../context-helpers/registry\";\n\nexport interface TamboContextHelpersProviderProps {\n /**\n * A dictionary of context helper functions.\n * The key becomes the AdditionalContext.name and the function returns the value.\n * Return null/undefined to skip including that context.\n */\n contextHelpers?: ContextHelpers;\n}\n\nexport interface TamboContextHelpersContextProps {\n /** Get all additional context by running all helper functions */\n getAdditionalContext: () => Promise<AdditionalContext[]>;\n /** Get the current context helpers map */\n getContextHelpers: () => ContextHelpers;\n /** Add or update a context helper by name */\n addContextHelper: (name: string, helper: ContextHelperFn) => void;\n /** Remove a context helper by name */\n removeContextHelper: (name: string) => void;\n}\n\nconst TamboContextHelpersContext =\n createContext<TamboContextHelpersContextProps | null>(null);\n\n/**\n * Provider for managing additional context helpers.\n * Accepts a map of { key: () => any | null | undefined | Promise<any | null | undefined> }.\n * Returning null/undefined skips inclusion; returned values are wrapped as { name: key, context: value }.\n * @param props - The props for the TamboContextHelpersProvider.\n * @param props.contextHelpers - A dictionary of context helper functions keyed by context name.\n * @param props.children - The children to render.\n * @returns The provider that exposes context helper APIs via useTamboContextHelpers.\n */\nexport const TamboContextHelpersProvider: React.FC<\n PropsWithChildren<TamboContextHelpersProviderProps>\n> = ({ children, contextHelpers }) => {\n const [helpers, setHelpers] = useState<Record<string, ContextHelperFn>>({});\n const addContextHelper = useCallback((name: string, fn: ContextHelperFn) => {\n setHelpers((prev) => ({ ...prev, [name]: fn }));\n }, []);\n const removeContextHelper = useCallback(\n (name: string, fn?: ContextHelperFn) => {\n setHelpers((prev) => {\n const { [name]: registeredFn, ...rest } = prev;\n if (fn === undefined || registeredFn === fn) {\n return rest;\n }\n return prev;\n });\n },\n [],\n );\n // Hydrate the global registry with initial helpers (runs on prop changes)\n useEffect(() => {\n const addedEntries: [string, ContextHelperFn][] = [];\n\n if (contextHelpers) {\n for (const [name, fn] of Object.entries(contextHelpers)) {\n addContextHelper(name, fn);\n addedEntries.push([name, fn]);\n }\n }\n\n return () => {\n for (const [name, fn] of addedEntries) {\n // Only remove if the registry still points to the same function\n removeContextHelper(name, fn);\n }\n };\n }, [addContextHelper, contextHelpers, removeContextHelper]);\n\n const getAdditionalContext = useCallback(async () => {\n const contexts = await resolveAdditionalContext(helpers);\n return contexts as AdditionalContext[];\n }, [helpers]);\n\n const getContextHelpers = useCallback(() => {\n return helpers as ContextHelpers;\n }, [helpers]);\n\n const value = useMemo(\n () => ({\n getAdditionalContext,\n getContextHelpers,\n addContextHelper,\n removeContextHelper,\n }),\n [\n getAdditionalContext,\n getContextHelpers,\n addContextHelper,\n removeContextHelper,\n ],\n );\n\n return (\n <TamboContextHelpersContext.Provider value={value}>\n {children}\n </TamboContextHelpersContext.Provider>\n );\n};\n\n/**\n * Hook to access context helpers functionality.\n * Safe to call even when no provider is present: proxies to the global registry.\n * @returns The context helpers context props (registry-backed).\n */\nexport const useTamboContextHelpers = () => {\n const context = useContext(TamboContextHelpersContext);\n if (context) return context;\n\n // Fallback to global registry so the API is standalone outside any provider\n return {\n getAdditionalContext: async () => {\n throw new Error(\"No provider found\");\n },\n getContextHelpers: () => {\n throw new Error(\"No provider found\");\n },\n addContextHelper: (_name: string, _helper: ContextHelperFn) => {\n throw new Error(\"No provider found\");\n },\n removeContextHelper: (_name: string) => {\n throw new Error(\"No provider found\");\n },\n } as TamboContextHelpersContextProps;\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-interactable-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-interactable-provider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAEZ,iBAAiB,EAKlB,MAAM,OAAO,CAAC;AAEf,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,6BAA6B,CAAC;AAGrC,QAAA,MAAM,wBAAwB,yCAQ5B,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,
|
|
1
|
+
{"version":3,"file":"tambo-interactable-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-interactable-provider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAEZ,iBAAiB,EAKlB,MAAM,OAAO,CAAC;AAEf,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,6BAA6B,CAAC;AAGrC,QAAA,MAAM,wBAAwB,yCAQ5B,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CA8PjE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,gCAEhC,CAAC"}
|