@tambo-ai/react 0.41.2 → 0.42.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.test.d.ts +2 -0
- package/dist/context-helpers/__tests__/context-helpers.test.d.ts.map +1 -0
- package/dist/context-helpers/__tests__/context-helpers.test.js +44 -0
- package/dist/context-helpers/__tests__/context-helpers.test.js.map +1 -0
- package/dist/context-helpers/index.d.ts +9 -0
- package/dist/context-helpers/index.d.ts.map +1 -0
- package/dist/context-helpers/index.js +38 -0
- package/dist/context-helpers/index.js.map +1 -0
- package/dist/context-helpers/types.d.ts +30 -0
- package/dist/context-helpers/types.d.ts.map +1 -0
- package/dist/context-helpers/types.js +3 -0
- package/dist/context-helpers/types.js.map +1 -0
- package/dist/context-helpers/user-page.d.ts +7 -0
- package/dist/context-helpers/user-page.d.ts.map +1 -0
- package/dist/context-helpers/user-page.js +31 -0
- package/dist/context-helpers/user-page.js.map +1 -0
- package/dist/context-helpers/user-time.d.ts +7 -0
- package/dist/context-helpers/user-time.d.ts.map +1 -0
- package/dist/context-helpers/user-time.js +20 -0
- package/dist/context-helpers/user-time.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.d.ts +2 -0
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.d.ts.map +1 -0
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.js +194 -0
- package/dist/providers/__tests__/tambo-context-helpers-provider.test.js.map +1 -0
- package/dist/providers/__tests__/tambo-thread-provider.test.js +51 -31
- package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +4 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/tambo-context-helpers-provider.d.ts +28 -0
- package/dist/providers/tambo-context-helpers-provider.d.ts.map +1 -0
- package/dist/providers/tambo-context-helpers-provider.js +98 -0
- package/dist/providers/tambo-context-helpers-provider.js.map +1 -0
- package/dist/providers/tambo-provider.d.ts +4 -2
- package/dist/providers/tambo-provider.d.ts.map +1 -1
- package/dist/providers/tambo-provider.js +10 -5
- package/dist/providers/tambo-provider.js.map +1 -1
- package/dist/providers/tambo-stubs.d.ts +4 -0
- package/dist/providers/tambo-stubs.d.ts.map +1 -1
- package/dist/providers/tambo-stubs.js +6 -3
- package/dist/providers/tambo-stubs.js.map +1 -1
- package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
- package/dist/providers/tambo-thread-provider.js +10 -1
- package/dist/providers/tambo-thread-provider.js.map +1 -1
- package/dist/util/registry.d.ts +0 -8
- package/dist/util/registry.d.ts.map +1 -1
- package/dist/util/registry.js +1 -15
- package/dist/util/registry.js.map +1 -1
- package/esm/context-helpers/__tests__/context-helpers.test.d.ts +2 -0
- package/esm/context-helpers/__tests__/context-helpers.test.d.ts.map +1 -0
- package/esm/context-helpers/__tests__/context-helpers.test.js +42 -0
- package/esm/context-helpers/__tests__/context-helpers.test.js.map +1 -0
- package/esm/context-helpers/index.d.ts +9 -0
- package/esm/context-helpers/index.d.ts.map +1 -0
- package/esm/context-helpers/index.js +21 -0
- package/esm/context-helpers/index.js.map +1 -0
- package/esm/context-helpers/types.d.ts +30 -0
- package/esm/context-helpers/types.d.ts.map +1 -0
- package/esm/context-helpers/types.js +2 -0
- package/esm/context-helpers/types.js.map +1 -0
- package/esm/context-helpers/user-page.d.ts +7 -0
- package/esm/context-helpers/user-page.d.ts.map +1 -0
- package/esm/context-helpers/user-page.js +28 -0
- package/esm/context-helpers/user-page.js.map +1 -0
- package/esm/context-helpers/user-time.d.ts +7 -0
- package/esm/context-helpers/user-time.d.ts.map +1 -0
- package/esm/context-helpers/user-time.js +17 -0
- package/esm/context-helpers/user-time.js.map +1 -0
- package/esm/index.d.ts +3 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -1
- package/esm/index.js.map +1 -1
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.d.ts +2 -0
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.d.ts.map +1 -0
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.js +156 -0
- package/esm/providers/__tests__/tambo-context-helpers-provider.test.js.map +1 -0
- package/esm/providers/__tests__/tambo-thread-provider.test.js +51 -31
- package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/esm/providers/index.d.ts +1 -0
- package/esm/providers/index.d.ts.map +1 -1
- package/esm/providers/index.js +1 -0
- package/esm/providers/index.js.map +1 -1
- package/esm/providers/tambo-context-helpers-provider.d.ts +28 -0
- package/esm/providers/tambo-context-helpers-provider.d.ts.map +1 -0
- package/esm/providers/tambo-context-helpers-provider.js +60 -0
- package/esm/providers/tambo-context-helpers-provider.js.map +1 -0
- package/esm/providers/tambo-provider.d.ts +4 -2
- package/esm/providers/tambo-provider.d.ts.map +1 -1
- package/esm/providers/tambo-provider.js +10 -5
- package/esm/providers/tambo-provider.js.map +1 -1
- package/esm/providers/tambo-stubs.d.ts +4 -0
- package/esm/providers/tambo-stubs.d.ts.map +1 -1
- package/esm/providers/tambo-stubs.js +6 -3
- package/esm/providers/tambo-stubs.js.map +1 -1
- package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
- package/esm/providers/tambo-thread-provider.js +11 -2
- package/esm/providers/tambo-thread-provider.js.map +1 -1
- package/esm/util/registry.d.ts +0 -8
- package/esm/util/registry.d.ts.map +1 -1
- package/esm/util/registry.js +0 -13
- package/esm/util/registry.js.map +1 -1
- package/package.json +1 -1
package/dist/util/registry.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.mapTamboToolToContextTool = exports.
|
|
6
|
+
exports.mapTamboToolToContextTool = exports.getComponentFromRegistry = exports.convertPropsToJsonSchema = exports.getUnassociatedTools = exports.getAvailableComponents = void 0;
|
|
7
7
|
const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
|
|
8
8
|
/**
|
|
9
9
|
* Get all the available components from the component registry
|
|
@@ -78,20 +78,6 @@ const getComponentFromRegistry = (componentName, componentRegistry) => {
|
|
|
78
78
|
return componentEntry;
|
|
79
79
|
};
|
|
80
80
|
exports.getComponentFromRegistry = getComponentFromRegistry;
|
|
81
|
-
const getDefaultContextObject = () => {
|
|
82
|
-
return {
|
|
83
|
-
localTime: new Date().toLocaleString(),
|
|
84
|
-
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
/**
|
|
88
|
-
* Get the client context for the current thread, such as the current time in the user's timezone
|
|
89
|
-
* @returns a string of context additions that will be added to the prompt when the thread is advanced.
|
|
90
|
-
*/
|
|
91
|
-
const getSystemContext = () => {
|
|
92
|
-
return getDefaultContextObject();
|
|
93
|
-
};
|
|
94
|
-
exports.getSystemContext = getSystemContext;
|
|
95
81
|
/**
|
|
96
82
|
* Map a Tambo tool to a context tool
|
|
97
83
|
* @param tool - The tool to map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/util/registry.ts"],"names":[],"mappings":";;;;;;AAEA,4EAAiD;AAYjD;;;;;;GAMG;AACI,MAAM,sBAAsB,GAAG,CACpC,iBAAoC,EACpC,YAA+B,EAC/B,gBAAuC,EACT,EAAE;IAChC,MAAM,mBAAmB,GAAiC,EAAE,CAAC;IAE7D,KAAK,MAAM,CAAC,IAAI,EAAE,cAAc,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACvE,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,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Q,KAAK,GAAG,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;QACnC,MAAM,WAAW,GAAG,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC;QAC5C,MAAM,UAAU,GAAG,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;QACvC,MAAM,MAAM,GAAG,IAAA,4BAAe,EAAC,KAAK,CAAC,CAAC;QAEtC,OAAO;YACL,IAAI;YACJ,IAAI;YACJ,WAAW;YACX,UAAU;YACV,MAAM;SACP,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,cAAc,GAAG,CAAC,MAAoB,EAAU,EAAE;IACtD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;IACtC,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB,KAAK,YAAY;YACf,OAAO,SAAS,CAAC;QACnB,KAAK,UAAU;YACb,OAAO,OAAO,CAAC;QACjB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC;QAChB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC;QAChB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB;YACE,OAAO,CAAC,IAAI,CAAC,4BAA4B,EAAE,QAAQ,CAAC,CAAC;YACrD,OAAO,QAAQ,CAAC;IACpB,CAAC;AACH,CAAC,CAAC","sourcesContent":["import TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { z } from \"zod\";\nimport zodToJsonSchema from \"zod-to-json-schema\";\nimport {\n ComponentContextToolMetadata,\n ComponentRegistry,\n JSONSchemaLite,\n ParameterSpec,\n RegisteredComponent,\n TamboTool,\n TamboToolAssociations,\n TamboToolRegistry,\n} from \"../model/component-metadata\";\n\n/**\n * Get all the available components from the component registry\n * @param componentRegistry - The component registry\n * @param toolRegistry - The tool registry\n * @param toolAssociations - The tool associations\n * @returns The available components\n */\nexport const getAvailableComponents = (\n componentRegistry: ComponentRegistry,\n toolRegistry: TamboToolRegistry,\n toolAssociations: TamboToolAssociations,\n): TamboAI.AvailableComponent[] => {\n const availableComponents: TamboAI.AvailableComponent[] = [];\n\n for (const [name, componentEntry] of Object.entries(componentRegistry)) {\n const associatedToolNames = toolAssociations[name] || [];\n\n const contextTools = [\n ...associatedToolNames.map((toolName) => {\n const tool = toolRegistry[toolName];\n if (!tool) return null;\n return mapTamboToolToContextTool(tool);\n }),\n ].filter((tool): tool is ComponentContextToolMetadata => tool !== null);\n\n availableComponents.push({\n name: componentEntry.name,\n description: componentEntry.description,\n props: componentEntry.props,\n contextTools,\n });\n }\n\n return availableComponents;\n};\n\n/**\n * Get tools from tool registry that are not associated with any component\n * @param toolRegistry - The tool registry\n * @param toolAssociations - The tool associations\n * @returns The tools that are not associated with any component\n */\nexport const getUnassociatedTools = (\n toolRegistry: TamboToolRegistry,\n toolAssociations: TamboToolAssociations,\n): TamboTool[] => {\n return Object.values(toolRegistry).filter((tool) => {\n // Check if the tool's name appears in any of the tool association arrays\n return !Object.values(toolAssociations).flat().includes(tool.name);\n });\n};\n\n/**\n * Helper function to convert component props from Zod schema to JSON Schema\n * @param component - The component to convert\n * @returns The converted props\n */\nexport const convertPropsToJsonSchema = (\n component: RegisteredComponent,\n): any => {\n if (!component.props) {\n return component.props;\n }\n\n // Check if props is a Zod schema (we can't directly check the type, so we check for _def)\n if (component.props._def && typeof component.props.parse === \"function\") {\n // Use two-step type assertion for safety\n return zodToJsonSchema(component.props as unknown as z.ZodTypeAny);\n }\n\n return component.props;\n};\n\n/**\n * Get a component by name from the component registry\n * @param componentName - The name of the component to get\n * @param componentRegistry - The component registry\n * @returns The component registration information\n */\nexport const getComponentFromRegistry = (\n componentName: string,\n componentRegistry: ComponentRegistry,\n): RegisteredComponent => {\n const componentEntry = componentRegistry[componentName];\n\n if (!componentEntry) {\n throw new Error(\n `Tambo tried to use Component ${componentName}, but it was not found.`,\n );\n }\n\n return componentEntry;\n};\n\n/**\n * Map a Tambo tool to a context tool\n * @param tool - The tool to map\n * @returns The context tool\n */\nexport const mapTamboToolToContextTool = (\n tool: TamboTool,\n): ComponentContextToolMetadata => {\n const parameters = getParametersFromZodFunction(tool.toolSchema);\n\n return {\n name: tool.name,\n description: tool.description,\n parameters,\n };\n};\n\nfunction isJsonSchema(\n schema: unknown,\n): schema is ReturnType<typeof zodToJsonSchema> {\n return (\n typeof schema === \"object\" &&\n schema !== null &&\n \"type\" in schema &&\n typeof (schema as { type: unknown }).type === \"string\" &&\n (schema as { type: string }).type === \"object\"\n );\n}\n\nconst getParametersFromZodFunction = (\n schema: z.ZodFunction<any, any> | JSONSchemaLite,\n): ParameterSpec[] => {\n if (isJsonSchema(schema)) {\n return [\n {\n name: \"args\",\n type: \"object\",\n description: schema.description ?? \"\",\n isRequired: true,\n schema: schema,\n },\n ];\n }\n\n const parameters: z.ZodTuple = schema.parameters();\n return parameters.items.map((param, index): ParameterSpec => {\n const name = `param${index + 1}`;\n const type = getZodBaseType(param);\n const description = param.description ?? \"\";\n const isRequired = !param.isOptional();\n const schema = zodToJsonSchema(param);\n\n return {\n name,\n type,\n description,\n isRequired,\n schema,\n };\n });\n};\n\nconst getZodBaseType = (schema: z.ZodTypeAny): string => {\n const typeName = schema._def.typeName;\n switch (typeName) {\n case \"ZodString\":\n return \"string\";\n case \"ZodNumber\":\n return \"number\";\n case \"ZodBoolean\":\n return \"boolean\";\n case \"ZodArray\":\n return \"array\";\n case \"ZodEnum\":\n return \"enum\";\n case \"ZodDate\":\n return \"date\";\n case \"ZodObject\":\n return \"object\";\n default:\n console.warn(\"falling back to string for\", typeName);\n return \"string\";\n }\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-helpers.test.d.ts","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getUserPageContext, getUserTimeContext } from "../index";
|
|
2
|
+
describe("Context Helpers", () => {
|
|
3
|
+
describe("getUserTimeContext", () => {
|
|
4
|
+
it("should return user time context with all required fields", () => {
|
|
5
|
+
const context = getUserTimeContext();
|
|
6
|
+
expect(context.name).toBe("userTime");
|
|
7
|
+
expect(context.context).toHaveProperty("localTime");
|
|
8
|
+
expect(context.context).toHaveProperty("timezone");
|
|
9
|
+
expect(context.context).toHaveProperty("timestamp");
|
|
10
|
+
expect(context.context).toHaveProperty("offsetMinutes");
|
|
11
|
+
// Verify data types
|
|
12
|
+
expect(typeof context.context.localTime).toBe("string");
|
|
13
|
+
expect(typeof context.context.timezone).toBe("string");
|
|
14
|
+
expect(typeof context.context.timestamp).toBe("string");
|
|
15
|
+
expect(typeof context.context.offsetMinutes).toBe("number");
|
|
16
|
+
// Verify timestamp is valid ISO string
|
|
17
|
+
expect(() => new Date(context.context.timestamp)).not.toThrow();
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe("getUserPageContext", () => {
|
|
21
|
+
it("should return page context with default values in test environment", () => {
|
|
22
|
+
const context = getUserPageContext();
|
|
23
|
+
expect(context.name).toBe("userPage");
|
|
24
|
+
expect(context.context).toHaveProperty("url");
|
|
25
|
+
expect(context.context).toHaveProperty("pathname");
|
|
26
|
+
expect(context.context).toHaveProperty("hostname");
|
|
27
|
+
expect(context.context).toHaveProperty("search");
|
|
28
|
+
expect(context.context).toHaveProperty("hash");
|
|
29
|
+
expect(context.context).toHaveProperty("title");
|
|
30
|
+
expect(context.context).toHaveProperty("referrer");
|
|
31
|
+
// Verify data types
|
|
32
|
+
expect(typeof context.context.url).toBe("string");
|
|
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");
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
//# sourceMappingURL=context-helpers.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-helpers.test.js","sourceRoot":"","sources":["../../../src/context-helpers/__tests__/context-helpers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAElE,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,QAAQ,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAClC,EAAE,CAAC,0DAA0D,EAAE,GAAG,EAAE;YAClE,MAAM,OAAO,GAAG,kBAAkB,EAAE,CAAC;YAErC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACtC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;YACpD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;YACnD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;YACpD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC;YAExD,oBAAoB;YACpB,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACxD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACvD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACxD,MAAM,CAAC,OAAO,OAAO,CA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AAC;YACvD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACvD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACrD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACnD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACpD,MAAM,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { getUserPageContext, getUserTimeContext } from \"../index\";\n\ndescribe(\"Context Helpers\", () => {\n describe(\"getUserTimeContext\", () => {\n it(\"should return user time context with all required fields\", () => {\n const context = getUserTimeContext();\n\n expect(context.name).toBe(\"userTime\");\n expect(context.context).toHaveProperty(\"localTime\");\n expect(context.context).toHaveProperty(\"timezone\");\n expect(context.context).toHaveProperty(\"timestamp\");\n expect(context.context).toHaveProperty(\"offsetMinutes\");\n\n // Verify data types\n expect(typeof context.context.localTime).toBe(\"string\");\n expect(typeof context.context.timezone).toBe(\"string\");\n expect(typeof context.context.timestamp).toBe(\"string\");\n expect(typeof context.context.offsetMinutes).toBe(\"number\");\n\n // Verify timestamp is valid ISO string\n expect(() => new Date(context.context.timestamp)).not.toThrow();\n });\n });\n\n describe(\"getUserPageContext\", () => {\n it(\"should return page context with default values in test environment\", () => {\n const context = getUserPageContext();\n\n expect(context.name).toBe(\"userPage\");\n expect(context.context).toHaveProperty(\"url\");\n expect(context.context).toHaveProperty(\"pathname\");\n expect(context.context).toHaveProperty(\"hostname\");\n expect(context.context).toHaveProperty(\"search\");\n expect(context.context).toHaveProperty(\"hash\");\n expect(context.context).toHaveProperty(\"title\");\n expect(context.context).toHaveProperty(\"referrer\");\n\n // Verify data types\n expect(typeof context.context.url).toBe(\"string\");\n expect(typeof context.context.pathname).toBe(\"string\");\n expect(typeof context.context.hostname).toBe(\"string\");\n expect(typeof context.context.search).toBe(\"string\");\n expect(typeof context.context.hash).toBe(\"string\");\n expect(typeof context.context.title).toBe(\"string\");\n expect(typeof context.context.referrer).toBe(\"string\");\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AdditionalContextHelper } from "./types";
|
|
2
|
+
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
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context-helpers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAIlD,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAE5B;;GAEG;AACH,eAAO,MAAM,uBAAuB,EAAE,uBAAuB,EAW5D,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getUserPageContext } from "./user-page";
|
|
2
|
+
import { getUserTimeContext } from "./user-time";
|
|
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
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context-helpers/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAE5B;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAA8B;IAChE;QACE,IAAI,EAAE,UAAU;QAChB,OAAO,EAAE,KAAK,EAAE,sBAAsB;QACtC,GAAG,EAAE,kBAAkB;KACxB;IACD;QACE,IAAI,EAAE,UAAU;QAChB,OAAO,EAAE,KAAK,EAAE,sBAAsB;QACtC,GAAG,EAAE,kBAAkB;KACxB;CACF,CAAC","sourcesContent":["import { AdditionalContextHelper } from \"./types\";\nimport { getUserPageContext } from \"./user-page\";\nimport { getUserTimeContext } from \"./user-time\";\n\nexport * from \"./types\";\nexport * from \"./user-page\";\nexport * from \"./user-time\";\n\n/**\n * Pre-built context helpers that can be enabled/disabled\n */\nexport const DEFAULT_CONTEXT_HELPERS: AdditionalContextHelper[] = [\n {\n name: \"userTime\",\n enabled: false, // Default to disabled\n run: getUserTimeContext,\n },\n {\n name: \"userPage\",\n enabled: false, // Default to disabled\n run: getUserPageContext,\n },\n];\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for additional context that can be added to messages
|
|
3
|
+
*/
|
|
4
|
+
export interface AdditionalContext {
|
|
5
|
+
/** The name of the context type */
|
|
6
|
+
name: string;
|
|
7
|
+
/** The context data */
|
|
8
|
+
context: any;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Interface for a context helper that can generate additional context
|
|
12
|
+
*/
|
|
13
|
+
export interface AdditionalContextHelper {
|
|
14
|
+
/** The name of the context helper */
|
|
15
|
+
name: string;
|
|
16
|
+
/** Whether this helper is enabled */
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
/** Function that generates the additional context */
|
|
19
|
+
run: () => AdditionalContext | Promise<AdditionalContext>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Configuration for context helpers
|
|
23
|
+
*/
|
|
24
|
+
export interface ContextHelpersConfig {
|
|
25
|
+
/** Enable/disable user time context helper */
|
|
26
|
+
userTime?: boolean;
|
|
27
|
+
/** Enable/disable user page context helper */
|
|
28
|
+
userPage?: boolean;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/context-helpers/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,uBAAuB;IACvB,OAAO,EAAE,GAAG,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,qDAAqD;IACrD,GAAG,EAAE,MAAM,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAC3D;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/context-helpers/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Interface for additional context that can be added to messages\n */\nexport interface AdditionalContext {\n /** The name of the context type */\n name: string;\n /** The context data */\n context: any;\n}\n\n/**\n * Interface for a context helper that can generate additional context\n */\nexport interface AdditionalContextHelper {\n /** The name of the context helper */\n name: string;\n /** Whether this helper is enabled */\n enabled: boolean;\n /** Function that generates the additional context */\n run: () => AdditionalContext | Promise<AdditionalContext>;\n}\n\n/**\n * Configuration for context helpers\n */\nexport interface ContextHelpersConfig {\n /** Enable/disable user time context helper */\n userTime?: boolean;\n /** Enable/disable user page context helper */\n userPage?: boolean;\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AdditionalContext } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Context helper that provides information about the user's current page
|
|
4
|
+
* @returns Additional context with current page information
|
|
5
|
+
*/
|
|
6
|
+
export declare function getUserPageContext(): AdditionalContext;
|
|
7
|
+
//# sourceMappingURL=user-page.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-page.d.ts","sourceRoot":"","sources":["../../src/context-helpers/user-page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE5C;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,iBAAiB,CAuBtD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context helper that provides information about the user's current page
|
|
3
|
+
* @returns Additional context with current page information
|
|
4
|
+
*/
|
|
5
|
+
export function getUserPageContext() {
|
|
6
|
+
// Only run in browser environment
|
|
7
|
+
if (typeof window === "undefined") {
|
|
8
|
+
return {
|
|
9
|
+
name: "userPage",
|
|
10
|
+
context: {
|
|
11
|
+
error: "Not in browser environment",
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
name: "userPage",
|
|
17
|
+
context: {
|
|
18
|
+
url: window.location.href,
|
|
19
|
+
pathname: window.location.pathname,
|
|
20
|
+
hostname: window.location.hostname,
|
|
21
|
+
search: window.location.search,
|
|
22
|
+
hash: window.location.hash,
|
|
23
|
+
title: document.title,
|
|
24
|
+
referrer: document.referrer,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=user-page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-page.js","sourceRoot":"","sources":["../../src/context-helpers/user-page.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,UAAU,kBAAkB;IAChC,kCAAkC;IAClC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO;YACL,IAAI,EAAE,UAAU;YAChB,OAAO,EAAE;gBACP,KAAK,EAAE,4BAA4B;aACpC;SACF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,OAAO,EAAE;YACP,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI;YACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,QAAQ;YAClC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,QAAQ;YAClC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM;YAC9B,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI;YAC1B,KAAK,EAAE,QAAQ,CAAC,KAAK;YACrB,QAAQ,EAAE,QAAQ,CAAC,QAAQ;SAC5B;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { AdditionalContext } from \"./types\";\n\n/**\n * Context helper that provides information about the user's current page\n * @returns Additional context with current page information\n */\nexport function getUserPageContext(): AdditionalContext {\n // Only run in browser environment\n if (typeof window === \"undefined\") {\n return {\n name: \"userPage\",\n context: {\n error: \"Not in browser environment\",\n },\n };\n }\n\n return {\n name: \"userPage\",\n context: {\n url: window.location.href,\n pathname: window.location.pathname,\n hostname: window.location.hostname,\n search: window.location.search,\n hash: window.location.hash,\n title: document.title,\n referrer: document.referrer,\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AdditionalContext } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Context helper that provides the user's current time and timezone
|
|
4
|
+
* @returns Additional context with user's time information
|
|
5
|
+
*/
|
|
6
|
+
export declare function getUserTimeContext(): AdditionalContext;
|
|
7
|
+
//# sourceMappingURL=user-time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-time.d.ts","sourceRoot":"","sources":["../../src/context-helpers/user-time.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE5C;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,iBAAiB,CAYtD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context helper that provides the user's current time and timezone
|
|
3
|
+
* @returns Additional context with user's time information
|
|
4
|
+
*/
|
|
5
|
+
export function getUserTimeContext() {
|
|
6
|
+
const now = new Date();
|
|
7
|
+
return {
|
|
8
|
+
name: "userTime",
|
|
9
|
+
context: {
|
|
10
|
+
localTime: now.toLocaleString(),
|
|
11
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
12
|
+
timestamp: now.toISOString(),
|
|
13
|
+
offsetMinutes: now.getTimezoneOffset(),
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=user-time.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-time.js","sourceRoot":"","sources":["../../src/context-helpers/user-time.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;IAEvB,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,OAAO,EAAE;YACP,SAAS,EAAE,GAAG,CAAC,cAAc,EAAE;YAC/B,QAAQ,EAAE,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ;YAC1D,SAAS,EAAE,GAAG,CAAC,WAAW,EAAE;YAC5B,aAAa,EAAE,GAAG,CAAC,iBAAiB,EAAE;SACvC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { AdditionalContext } from \"./types\";\n\n/**\n * Context helper that provides the user's current time and timezone\n * @returns Additional context with user's time information\n */\nexport function getUserTimeContext(): AdditionalContext {\n const now = new Date();\n\n return {\n name: \"userTime\",\n context: {\n localTime: now.toLocaleString(),\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\n timestamp: now.toISOString(),\n offsetMinutes: now.getTimezoneOffset(),\n },\n };\n}\n"]}
|
package/esm/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { useTamboStreamingProps } from "./hooks/use-streaming-props";
|
|
|
5
5
|
export * from "./hooks/use-suggestions";
|
|
6
6
|
export { useTamboStreamStatus, type PropStatus, type StreamStatus, } from "./hooks/use-tambo-stream-status";
|
|
7
7
|
export { useTamboThreadInput } from "./hooks/use-thread-input";
|
|
8
|
-
export { TamboClientProvider, TamboComponentProvider, TamboPropStreamProvider, TamboProvider, TamboStubProvider, TamboThreadProvider, useTambo, useTamboClient, useTamboStream, useTamboThread, type TamboComponent, type TamboRegistryContext, type TamboStubProviderProps, } from "./providers";
|
|
8
|
+
export { TamboClientProvider, TamboComponentProvider, TamboContextHelpersProvider, TamboPropStreamProvider, TamboProvider, TamboStubProvider, TamboThreadProvider, useTambo, useTamboClient, useTamboContextHelpers, useTamboStream, useTamboThread, type TamboComponent, type TamboContextHelpersContextProps, type TamboContextHelpersProviderProps, type TamboRegistryContext, type TamboStubProviderProps, } from "./providers";
|
|
9
9
|
export type { APIError, RateLimitError, TamboAIError, } from "@tambo-ai/typescript-sdk";
|
|
10
10
|
export type { Suggestion, SuggestionGenerateParams, SuggestionGenerateResponse, SuggestionListResponse, } from "@tambo-ai/typescript-sdk/resources/beta/threads/suggestions";
|
|
11
11
|
export { useTamboThreadList } from "./hooks/use-tambo-threads";
|
|
@@ -15,4 +15,6 @@ export { type TamboThread } from "./model/tambo-thread";
|
|
|
15
15
|
export type { TamboInteractableComponent as InteractableComponent, TamboInteractableContext, } from "./model/tambo-interactable";
|
|
16
16
|
export { withTamboInteractable as withInteractable, type InteractableConfig, type WithTamboInteractableProps, } from "./providers/hoc/with-tambo-interactable";
|
|
17
17
|
export { useTamboInteractable } from "./providers/tambo-interactable-provider";
|
|
18
|
+
export { DEFAULT_CONTEXT_HELPERS, getUserPageContext, getUserTimeContext, } from "./context-helpers";
|
|
19
|
+
export type { AdditionalContext, AdditionalContextHelper, ContextHelpersConfig, } from "./context-helpers";
|
|
18
20
|
//# sourceMappingURL=index.d.ts.map
|
package/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wKAAwK;AAExK,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,cAAc,yBAAyB,CAAC;AACxC,OAAO,EACL,oBAAoB,EACpB,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAG/D,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,uBAAuB,EACvB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,QAAQ,EACR,cAAc,EACd,cAAc,EACd,cAAc,EACd,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACz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TamboStreamStatus, } from "./hooks/use-tambo-stream-status";
|
|
7
7
|
export { useTamboThreadInput } from "./hooks/use-thread-input";
|
|
8
8
|
// Re-export provider components
|
|
9
|
-
export { TamboClientProvider, TamboComponentProvider, TamboPropStreamProvider, TamboProvider, TamboStubProvider, TamboThreadProvider, useTambo, useTamboClient, useTamboStream, useTamboThread, } from "./providers";
|
|
9
|
+
export { TamboClientProvider, TamboComponentProvider, TamboContextHelpersProvider, TamboPropStreamProvider, TamboProvider, TamboStubProvider, TamboThreadProvider, useTambo, useTamboClient, useTamboContextHelpers, useTamboStream, useTamboThread, } from "./providers";
|
|
10
10
|
export { useTamboThreadList } from "./hooks/use-tambo-threads";
|
|
11
11
|
export { GenerationStage, } from "./model/generate-component-response";
|
|
12
12
|
export { withTamboInteractable as withInteractable, } from "./providers/hoc/with-tambo-interactable";
|
|
13
13
|
export { useTamboInteractable } from "./providers/tambo-interactable-provider";
|
|
14
|
+
// Context helpers exports
|
|
15
|
+
export { DEFAULT_CONTEXT_HELPERS, getUserPageContext, getUserTimeContext, } from "./context-helpers";
|
|
14
16
|
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wKAAwK;AAExK,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,cAAc,yBAAyB,CAAC;AACxC,OAAO,EACL,oBAAoB,GAGrB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE/D,gCAAgC;AAChC,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,uBAAuB,EACvB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,QAAQ,EACR,cAAc,EACd,cAAc,EACd,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wKAAwK;AAExK,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,cAAc,yBAAyB,CAAC;AACxC,OAAO,EACL,oBAAoB,GAGrB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE/D,gCAAgC;AAChC,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,2BAA2B,EAC3B,uBAAuB,EACvB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,QAAQ,EACR,cAAc,EACd,sBAAsB,EACtB,cAAc,EACd,cAAc,GAMf,MAAM,aAAa,CAAC;AAcrB,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAQ/D,OAAO,EACL,eAAe,GAEhB,MAAM,qCAAqC,CAAC;AAO7C,OAAO,EACL,qBAAqB,IAAI,gBAAgB,GAG1C,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAC;AAE/E,0BAA0B;AAC1B,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,mBAAmB,CAAC","sourcesContent":["/** Exports for the library. Only publically available exports are re-exported here. Anything not exported here is not supported and may change or break at any time. */\n\nexport { useTamboComponentState } from \"./hooks/use-component-state\";\nexport {\n TamboMessageProvider,\n useTamboCurrentMessage,\n useTamboMessageContext,\n} from \"./hooks/use-current-message\";\nexport { useTamboStreamingProps } from \"./hooks/use-streaming-props\";\nexport * from \"./hooks/use-suggestions\";\nexport {\n useTamboStreamStatus,\n type PropStatus,\n type StreamStatus,\n} from \"./hooks/use-tambo-stream-status\";\nexport { useTamboThreadInput } from \"./hooks/use-thread-input\";\n\n// Re-export provider components\nexport {\n TamboClientProvider,\n TamboComponentProvider,\n TamboContextHelpersProvider,\n TamboPropStreamProvider,\n TamboProvider,\n TamboStubProvider,\n TamboThreadProvider,\n useTambo,\n useTamboClient,\n useTamboContextHelpers,\n useTamboStream,\n useTamboThread,\n type TamboComponent,\n type TamboContextHelpersContextProps,\n type TamboContextHelpersProviderProps,\n type TamboRegistryContext,\n type TamboStubProviderProps,\n} from \"./providers\";\n\n// Re-export types from Tambo Node SDK\nexport type {\n APIError,\n RateLimitError,\n TamboAIError,\n} from \"@tambo-ai/typescript-sdk\";\nexport type {\n Suggestion,\n SuggestionGenerateParams,\n SuggestionGenerateResponse,\n SuggestionListResponse,\n} from \"@tambo-ai/typescript-sdk/resources/beta/threads/suggestions\";\nexport { useTamboThreadList } from \"./hooks/use-tambo-threads\";\nexport {\n type ComponentContextToolMetadata,\n type ComponentRegistry,\n type ParameterSpec,\n type RegisteredComponent,\n type TamboTool,\n} from \"./model/component-metadata\";\nexport {\n GenerationStage,\n type TamboThreadMessage,\n} from \"./model/generate-component-response\";\nexport { type TamboThread } from \"./model/tambo-thread\";\n\nexport type {\n TamboInteractableComponent as InteractableComponent,\n TamboInteractableContext,\n} from \"./model/tambo-interactable\";\nexport {\n withTamboInteractable as withInteractable,\n type InteractableConfig,\n type WithTamboInteractableProps,\n} from \"./providers/hoc/with-tambo-interactable\";\nexport { useTamboInteractable } from \"./providers/tambo-interactable-provider\";\n\n// Context helpers exports\nexport {\n DEFAULT_CONTEXT_HELPERS,\n getUserPageContext,\n getUserTimeContext,\n} from \"./context-helpers\";\nexport type {\n AdditionalContext,\n AdditionalContextHelper,\n ContextHelpersConfig,\n} from \"./context-helpers\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tambo-context-helpers-provider.test.d.ts","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-context-helpers-provider.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import * as contextHelpers from "../../context-helpers";
|
|
4
|
+
import { DEFAULT_CONTEXT_HELPERS } from "../../context-helpers";
|
|
5
|
+
import { TamboContextHelpersProvider, useTamboContextHelpers, } from "../tambo-context-helpers-provider";
|
|
6
|
+
describe("TamboContextHelpersProvider", () => {
|
|
7
|
+
// Create a wrapper component that provides the context for testing hooks
|
|
8
|
+
const wrapper = ({ children }) => (React.createElement(TamboContextHelpersProvider, null, children));
|
|
9
|
+
describe("useTamboContextHelpers", () => {
|
|
10
|
+
/**
|
|
11
|
+
* Test: Hook throws error when used outside of provider
|
|
12
|
+
* This ensures developers get a clear error message if they forget to wrap
|
|
13
|
+
* their components with the TamboContextHelpersProvider
|
|
14
|
+
*/
|
|
15
|
+
it("should throw error when used outside provider", () => {
|
|
16
|
+
// Mock console.error to prevent error output in test logs
|
|
17
|
+
const consoleSpy = jest
|
|
18
|
+
.spyOn(console, "error")
|
|
19
|
+
.mockImplementation(() => { });
|
|
20
|
+
// Verify that using the hook without a provider throws the expected error
|
|
21
|
+
expect(() => {
|
|
22
|
+
renderHook(() => useTamboContextHelpers());
|
|
23
|
+
}).toThrow("useTamboContextHelpers must be used within a TamboContextHelpersProvider");
|
|
24
|
+
// Restore console.error to its original implementation
|
|
25
|
+
consoleSpy.mockRestore();
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Test: Hook provides the expected API functions
|
|
29
|
+
* Verifies that all required functions are available when the hook is used
|
|
30
|
+
* within the provider context
|
|
31
|
+
*/
|
|
32
|
+
it("should provide context helpers functions", () => {
|
|
33
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
34
|
+
wrapper,
|
|
35
|
+
});
|
|
36
|
+
// Verify all expected functions are present in the hook's return value
|
|
37
|
+
expect(result.current).toHaveProperty("getAdditionalContext");
|
|
38
|
+
expect(result.current).toHaveProperty("getContextHelpers");
|
|
39
|
+
expect(result.current).toHaveProperty("setContextHelperEnabled");
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Test: Default configuration is applied correctly
|
|
43
|
+
* Ensures that the default context helpers are loaded with their
|
|
44
|
+
* expected enabled/disabled states
|
|
45
|
+
*/
|
|
46
|
+
it("should return default context helpers configuration", () => {
|
|
47
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
48
|
+
wrapper,
|
|
49
|
+
});
|
|
50
|
+
const helpers = result.current.getContextHelpers();
|
|
51
|
+
// Verify we have the expected number of default helpers
|
|
52
|
+
expect(helpers).toHaveLength(DEFAULT_CONTEXT_HELPERS.length);
|
|
53
|
+
// Check specific helpers have their expected default states
|
|
54
|
+
const userTimeHelper = helpers.find((h) => h.name === "userTime");
|
|
55
|
+
const userPageHelper = helpers.find((h) => h.name === "userPage");
|
|
56
|
+
// userTime should be disabled by default
|
|
57
|
+
expect(userTimeHelper?.enabled).toBe(false);
|
|
58
|
+
// userPage should be disabled by default
|
|
59
|
+
expect(userPageHelper?.enabled).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
/**
|
|
62
|
+
* Test: Context helper enabled state can be toggled
|
|
63
|
+
* Verifies that the setContextHelperEnabled function correctly updates
|
|
64
|
+
* the enabled state of individual helpers
|
|
65
|
+
*/
|
|
66
|
+
it("should allow toggling context helper enabled state", () => {
|
|
67
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
68
|
+
wrapper,
|
|
69
|
+
});
|
|
70
|
+
// Enable the userPage helper using the provided function
|
|
71
|
+
act(() => {
|
|
72
|
+
result.current.setContextHelperEnabled("userPage", true);
|
|
73
|
+
});
|
|
74
|
+
// Verify the helper's state was updated
|
|
75
|
+
const helpers = result.current.getContextHelpers();
|
|
76
|
+
const userPageHelper = helpers.find((h) => h.name === "userPage");
|
|
77
|
+
expect(userPageHelper?.enabled).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* Test: Only enabled helpers contribute to additional context
|
|
81
|
+
* Verifies that getAdditionalContext only runs and returns data from
|
|
82
|
+
* helpers that are currently enabled
|
|
83
|
+
*/
|
|
84
|
+
it("should get additional context from enabled helpers", async () => {
|
|
85
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
86
|
+
wrapper,
|
|
87
|
+
});
|
|
88
|
+
const contexts = await result.current.getAdditionalContext();
|
|
89
|
+
// By default, no context helpers are enabled, so we should get no contexts
|
|
90
|
+
expect(contexts).toHaveLength(0);
|
|
91
|
+
});
|
|
92
|
+
/**
|
|
93
|
+
* Test: Provider respects configuration prop
|
|
94
|
+
* Verifies that initial configuration can be customized through the
|
|
95
|
+
* contextHelpers prop on the provider
|
|
96
|
+
*/
|
|
97
|
+
it("should respect contextHelpers configuration prop", () => {
|
|
98
|
+
// Create a custom wrapper with specific configuration
|
|
99
|
+
const customWrapper = ({ children }) => (React.createElement(TamboContextHelpersProvider, { contextHelpers: {
|
|
100
|
+
userTime: false, // Disable userTime
|
|
101
|
+
userPage: true, // Enable userPage
|
|
102
|
+
} }, children));
|
|
103
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
104
|
+
wrapper: customWrapper,
|
|
105
|
+
});
|
|
106
|
+
// Verify the custom configuration is applied
|
|
107
|
+
const helpers = result.current.getContextHelpers();
|
|
108
|
+
const userTimeHelper = helpers.find((h) => h.name === "userTime");
|
|
109
|
+
const userPageHelper = helpers.find((h) => h.name === "userPage");
|
|
110
|
+
expect(userTimeHelper?.enabled).toBe(false);
|
|
111
|
+
expect(userPageHelper?.enabled).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
/**
|
|
114
|
+
* Test: Errors in context helpers are handled gracefully
|
|
115
|
+
* Verifies that if a context helper throws an error, it doesn't crash
|
|
116
|
+
* the entire system and other helpers continue to work
|
|
117
|
+
*/
|
|
118
|
+
it("should handle errors in context helper functions gracefully", async () => {
|
|
119
|
+
// Mock console.error to capture error logs
|
|
120
|
+
const consoleErrorSpy = jest
|
|
121
|
+
.spyOn(console, "error")
|
|
122
|
+
.mockImplementation(() => { });
|
|
123
|
+
// Create a helper that will throw an error when run
|
|
124
|
+
const errorHelper = {
|
|
125
|
+
name: "errorHelper",
|
|
126
|
+
enabled: true,
|
|
127
|
+
run: () => {
|
|
128
|
+
throw new Error("Test error");
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
// Temporarily add the error helper to the default helpers
|
|
132
|
+
// Store original helpers to restore later
|
|
133
|
+
const originalHelpers = DEFAULT_CONTEXT_HELPERS;
|
|
134
|
+
Object.defineProperty(contextHelpers, "DEFAULT_CONTEXT_HELPERS", {
|
|
135
|
+
value: [...originalHelpers, errorHelper],
|
|
136
|
+
writable: true,
|
|
137
|
+
});
|
|
138
|
+
const { result } = renderHook(() => useTamboContextHelpers(), {
|
|
139
|
+
wrapper,
|
|
140
|
+
});
|
|
141
|
+
// Call getAdditionalContext, which should handle the error gracefully
|
|
142
|
+
const contexts = await result.current.getAdditionalContext();
|
|
143
|
+
// Should have no contexts because the error helper should be skipped
|
|
144
|
+
expect(contexts.length).toBe(0);
|
|
145
|
+
// Verify that the error was logged appropriately
|
|
146
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining("Error running context helper errorHelper"), expect.any(Error));
|
|
147
|
+
// Restore the original helpers and console.error
|
|
148
|
+
Object.defineProperty(contextHelpers, "DEFAULT_CONTEXT_HELPERS", {
|
|
149
|
+
value: originalHelpers,
|
|
150
|
+
writable: true,
|
|
151
|
+
});
|
|
152
|
+
consoleErrorSpy.mockRestore();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
//# sourceMappingURL=tambo-context-helpers-provider.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tambo-context-helpers-provider.test.js","sourceRo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AE,EAAE;gBAC5D,OAAO;aACR,CAAC,CAAC;YAEH,sEAAsE;YACtE,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC;YAE7D,qEAAqE;YACrE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAEhC,iDAAiD;YACjD,MAAM,CAAC,eAAe,CAAC,CAAC,oBAAoB,CAC1C,MAAM,CAAC,gBAAgB,CAAC,0CAA0C,CAAC,EACnE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAClB,CAAC;YAEF,iDAAiD;YACjD,MAAM,CAAC,cAAc,CAAC,cAAc,EAAE,yBAAyB,EAAE;gBAC/D,KAAK,EAAE,eAAe;gBACtB,QAAQ,EAAE,IAAI;aACf,CAAC,CAAC;YACH,eAAe,CAAC,WAAW,EAAE,CAAC;QAChC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport * as contextHelpers from \"../../context-helpers\";\nimport { DEFAULT_CONTEXT_HELPERS } from \"../../context-helpers\";\nimport {\n TamboContextHelpersProvider,\n useTamboContextHelpers,\n} from \"../tambo-context-helpers-provider\";\n\ndescribe(\"TamboContextHelpersProvider\", () => {\n // Create a wrapper component that provides the context for testing hooks\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider>{children}</TamboContextHelpersProvider>\n );\n\n describe(\"useTamboContextHelpers\", () => {\n /**\n * Test: Hook throws error when used outside of provider\n * This ensures developers get a clear error message if they forget to wrap\n * their components with the TamboContextHelpersProvider\n */\n it(\"should throw error when used outside provider\", () => {\n // Mock console.error to prevent error output in test logs\n const consoleSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n\n // Verify that using the hook without a provider throws the expected error\n expect(() => {\n renderHook(() => useTamboContextHelpers());\n }).toThrow(\n \"useTamboContextHelpers must be used within a TamboContextHelpersProvider\",\n );\n\n // Restore console.error to its original implementation\n consoleSpy.mockRestore();\n });\n\n /**\n * Test: Hook provides the expected API functions\n * Verifies that all required functions are available when the hook is used\n * within the provider context\n */\n it(\"should provide context helpers functions\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n\n // Verify all expected functions are present in the hook's return value\n expect(result.current).toHaveProperty(\"getAdditionalContext\");\n expect(result.current).toHaveProperty(\"getContextHelpers\");\n expect(result.current).toHaveProperty(\"setContextHelperEnabled\");\n });\n\n /**\n * Test: Default configuration is applied correctly\n * Ensures that the default context helpers are loaded with their\n * expected enabled/disabled states\n */\n it(\"should return default context helpers configuration\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n\n const helpers = result.current.getContextHelpers();\n\n // Verify we have the expected number of default helpers\n expect(helpers).toHaveLength(DEFAULT_CONTEXT_HELPERS.length);\n\n // Check specific helpers have their expected default states\n const userTimeHelper = helpers.find((h) => h.name === \"userTime\");\n const userPageHelper = helpers.find((h) => h.name === \"userPage\");\n\n // userTime should be disabled by default\n expect(userTimeHelper?.enabled).toBe(false);\n // userPage should be disabled by default\n expect(userPageHelper?.enabled).toBe(false);\n });\n\n /**\n * Test: Context helper enabled state can be toggled\n * Verifies that the setContextHelperEnabled function correctly updates\n * the enabled state of individual helpers\n */\n it(\"should allow toggling context helper enabled state\", () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n\n // Enable the userPage helper using the provided function\n act(() => {\n result.current.setContextHelperEnabled(\"userPage\", true);\n });\n\n // Verify the helper's state was updated\n const helpers = result.current.getContextHelpers();\n const userPageHelper = helpers.find((h) => h.name === \"userPage\");\n\n expect(userPageHelper?.enabled).toBe(true);\n });\n\n /**\n * Test: Only enabled helpers contribute to additional context\n * Verifies that getAdditionalContext only runs and returns data from\n * helpers that are currently enabled\n */\n it(\"should get additional context from enabled helpers\", async () => {\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n\n const contexts = await result.current.getAdditionalContext();\n\n // By default, no context helpers are enabled, so we should get no contexts\n expect(contexts).toHaveLength(0);\n });\n\n /**\n * Test: Provider respects configuration prop\n * Verifies that initial configuration can be customized through the\n * contextHelpers prop on the provider\n */\n it(\"should respect contextHelpers configuration prop\", () => {\n // Create a custom wrapper with specific configuration\n const customWrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboContextHelpersProvider\n contextHelpers={{\n userTime: false, // Disable userTime\n userPage: true, // Enable userPage\n }}\n >\n {children}\n </TamboContextHelpersProvider>\n );\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper: customWrapper,\n });\n\n // Verify the custom configuration is applied\n const helpers = result.current.getContextHelpers();\n const userTimeHelper = helpers.find((h) => h.name === \"userTime\");\n const userPageHelper = helpers.find((h) => h.name === \"userPage\");\n\n expect(userTimeHelper?.enabled).toBe(false);\n expect(userPageHelper?.enabled).toBe(true);\n });\n\n /**\n * Test: Errors in context helpers are handled gracefully\n * Verifies that if a context helper throws an error, it doesn't crash\n * the entire system and other helpers continue to work\n */\n it(\"should handle errors in context helper functions gracefully\", async () => {\n // Mock console.error to capture error logs\n const consoleErrorSpy = jest\n .spyOn(console, \"error\")\n .mockImplementation(() => {});\n\n // Create a helper that will throw an error when run\n const errorHelper = {\n name: \"errorHelper\",\n enabled: true,\n run: () => {\n throw new Error(\"Test error\");\n },\n };\n\n // Temporarily add the error helper to the default helpers\n // Store original helpers to restore later\n const originalHelpers = DEFAULT_CONTEXT_HELPERS;\n Object.defineProperty(contextHelpers, \"DEFAULT_CONTEXT_HELPERS\", {\n value: [...originalHelpers, errorHelper],\n writable: true,\n });\n\n const { result } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n\n // Call getAdditionalContext, which should handle the error gracefully\n const contexts = await result.current.getAdditionalContext();\n\n // Should have no contexts because the error helper should be skipped\n expect(contexts.length).toBe(0);\n\n // Verify that the error was logged appropriately\n expect(consoleErrorSpy).toHaveBeenCalledWith(\n expect.stringContaining(\"Error running context helper errorHelper\"),\n expect.any(Error),\n );\n\n // Restore the original helpers and console.error\n Object.defineProperty(contextHelpers, \"DEFAULT_CONTEXT_HELPERS\", {\n value: originalHelpers,\n writable: true,\n });\n consoleErrorSpy.mockRestore();\n });\n });\n});\n"]}
|