@tambo-ai/react 0.71.0 → 0.72.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/v1/hooks/use-tambo-v1-component-state.d.ts +44 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.js +134 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.d.ts +2 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.js +292 -0
- package/dist/v1/hooks/use-tambo-v1-component-state.test.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts +62 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.js +76 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.d.ts +2 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.js +168 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -0
- package/dist/v1/index.d.ts +23 -12
- package/dist/v1/index.d.ts.map +1 -1
- package/dist/v1/index.js +48 -14
- package/dist/v1/index.js.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.d.ts +43 -1
- package/dist/v1/providers/tambo-v1-provider.d.ts.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.js +24 -3
- package/dist/v1/providers/tambo-v1-provider.js.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.test.js +58 -0
- package/dist/v1/providers/tambo-v1-provider.test.js.map +1 -1
- package/dist/v1/types/message.d.ts +27 -2
- package/dist/v1/types/message.d.ts.map +1 -1
- package/dist/v1/types/message.js.map +1 -1
- package/dist/v1/utils/component-renderer.d.ts +89 -0
- package/dist/v1/utils/component-renderer.d.ts.map +1 -0
- package/dist/v1/utils/component-renderer.js +216 -0
- package/dist/v1/utils/component-renderer.js.map +1 -0
- package/dist/v1/utils/component-renderer.test.d.ts +2 -0
- package/dist/v1/utils/component-renderer.test.d.ts.map +1 -0
- package/dist/v1/utils/component-renderer.test.js +380 -0
- package/dist/v1/utils/component-renderer.test.js.map +1 -0
- package/dist/v1/utils/event-accumulator.js +28 -8
- package/dist/v1/utils/event-accumulator.js.map +1 -1
- package/dist/v1/utils/event-accumulator.test.js +201 -6
- package/dist/v1/utils/event-accumulator.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-component-state.d.ts +44 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.js +131 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.d.ts +2 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.js +290 -0
- package/esm/v1/hooks/use-tambo-v1-component-state.test.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts +62 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.js +73 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.d.ts +2 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.js +166 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -0
- package/esm/v1/index.d.ts +23 -12
- package/esm/v1/index.d.ts.map +1 -1
- package/esm/v1/index.js +31 -12
- package/esm/v1/index.js.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.d.ts +43 -1
- package/esm/v1/providers/tambo-v1-provider.d.ts.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.js +24 -4
- package/esm/v1/providers/tambo-v1-provider.js.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.test.js +59 -1
- package/esm/v1/providers/tambo-v1-provider.test.js.map +1 -1
- package/esm/v1/types/message.d.ts +27 -2
- package/esm/v1/types/message.d.ts.map +1 -1
- package/esm/v1/types/message.js.map +1 -1
- package/esm/v1/utils/component-renderer.d.ts +89 -0
- package/esm/v1/utils/component-renderer.d.ts.map +1 -0
- package/esm/v1/utils/component-renderer.js +175 -0
- package/esm/v1/utils/component-renderer.js.map +1 -0
- package/esm/v1/utils/component-renderer.test.d.ts +2 -0
- package/esm/v1/utils/component-renderer.test.d.ts.map +1 -0
- package/esm/v1/utils/component-renderer.test.js +375 -0
- package/esm/v1/utils/component-renderer.test.js.map +1 -0
- package/esm/v1/utils/event-accumulator.js +28 -8
- package/esm/v1/utils/event-accumulator.js.map +1 -1
- package/esm/v1/utils/event-accumulator.test.js +201 -6
- package/esm/v1/utils/event-accumulator.test.js.map +1 -1
- package/package.json +1 -1
|
@@ -4,8 +4,9 @@ import React from "react";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { useTamboClient } from "../../providers/tambo-client-provider";
|
|
6
6
|
import { useTamboRegistry } from "../../providers/tambo-registry-provider";
|
|
7
|
+
import { useTamboContextHelpers } from "../../providers/tambo-context-helpers-provider";
|
|
7
8
|
import { useStreamState, useThreadManagement } from "./tambo-v1-stream-context";
|
|
8
|
-
import { TamboV1Provider } from "./tambo-v1-provider";
|
|
9
|
+
import { TamboV1Provider, useContextKey } from "./tambo-v1-provider";
|
|
9
10
|
// Mock the client provider to capture the apiKey
|
|
10
11
|
jest.mock("../../providers/tambo-client-provider", () => ({
|
|
11
12
|
useTamboClient: jest.fn(),
|
|
@@ -114,5 +115,62 @@ describe("TamboV1Provider", () => {
|
|
|
114
115
|
const { result } = renderHook(() => useTamboRegistry(), { wrapper });
|
|
115
116
|
expect(result.current.onCallUnregisteredTool).toBe(onCallUnregisteredTool);
|
|
116
117
|
});
|
|
118
|
+
it("registers static resources when provided", () => {
|
|
119
|
+
const resources = [
|
|
120
|
+
{
|
|
121
|
+
uri: "resource://test/example",
|
|
122
|
+
name: "Test Resource",
|
|
123
|
+
description: "A test resource",
|
|
124
|
+
mimeType: "text/plain",
|
|
125
|
+
},
|
|
126
|
+
];
|
|
127
|
+
const wrapper = ({ children }) => (React.createElement(TamboV1Provider, { apiKey: "test-api-key", resources: resources }, children));
|
|
128
|
+
const { result } = renderHook(() => useTamboRegistry(), { wrapper });
|
|
129
|
+
expect(result.current.resources).toHaveLength(1);
|
|
130
|
+
expect(result.current.resources[0].uri).toBe("resource://test/example");
|
|
131
|
+
expect(result.current.resources[0].name).toBe("Test Resource");
|
|
132
|
+
});
|
|
133
|
+
it("registers resource source when listResources and getResource provided", () => {
|
|
134
|
+
const listResources = jest.fn().mockResolvedValue({ resources: [] });
|
|
135
|
+
const getResource = jest.fn().mockResolvedValue({ contents: [] });
|
|
136
|
+
const wrapper = ({ children }) => (React.createElement(TamboV1Provider, { apiKey: "test-api-key", listResources: listResources, getResource: getResource }, children));
|
|
137
|
+
const { result } = renderHook(() => useTamboRegistry(), { wrapper });
|
|
138
|
+
expect(result.current.resourceSource).toBeDefined();
|
|
139
|
+
expect(result.current.resourceSource?.listResources).toBe(listResources);
|
|
140
|
+
expect(result.current.resourceSource?.getResource).toBe(getResource);
|
|
141
|
+
});
|
|
142
|
+
it("provides contextKey via useContextKey hook", () => {
|
|
143
|
+
const wrapper = ({ children }) => (React.createElement(TamboV1Provider, { apiKey: "test-api-key", contextKey: "my-context-key" }, children));
|
|
144
|
+
const { result } = renderHook(() => useContextKey(), { wrapper });
|
|
145
|
+
expect(result.current).toBe("my-context-key");
|
|
146
|
+
});
|
|
147
|
+
it("returns undefined from useContextKey when no contextKey provided", () => {
|
|
148
|
+
const wrapper = ({ children }) => (React.createElement(TamboV1Provider, { apiKey: "test-api-key" }, children));
|
|
149
|
+
const { result } = renderHook(() => useContextKey(), { wrapper });
|
|
150
|
+
expect(result.current).toBeUndefined();
|
|
151
|
+
});
|
|
152
|
+
it("provides context helpers via useTamboContextHelpers hook", async () => {
|
|
153
|
+
const contextHelpers = {
|
|
154
|
+
getUserName: () => "Test User",
|
|
155
|
+
getCurrentTime: () => new Date().toISOString(),
|
|
156
|
+
};
|
|
157
|
+
const wrapper = ({ children }) => (React.createElement(TamboV1Provider, { apiKey: "test-api-key", contextHelpers: contextHelpers }, children));
|
|
158
|
+
const { result, rerender } = renderHook(() => useTamboContextHelpers(), {
|
|
159
|
+
wrapper,
|
|
160
|
+
});
|
|
161
|
+
// Helpers are registered via useEffect, so we need to trigger a rerender
|
|
162
|
+
await act(async () => {
|
|
163
|
+
rerender();
|
|
164
|
+
});
|
|
165
|
+
const helpers = result.current.getContextHelpers();
|
|
166
|
+
expect(helpers.getUserName).toBe(contextHelpers.getUserName);
|
|
167
|
+
expect(helpers.getCurrentTime).toBe(contextHelpers.getCurrentTime);
|
|
168
|
+
});
|
|
169
|
+
it("returns empty contextHelpers when none provided", async () => {
|
|
170
|
+
const wrapper = ({ children }) => (React.createElement(TamboV1Provider, { apiKey: "test-api-key" }, children));
|
|
171
|
+
const { result } = renderHook(() => useTamboContextHelpers(), { wrapper });
|
|
172
|
+
const helpers = result.current.getContextHelpers();
|
|
173
|
+
expect(Object.keys(helpers)).toHaveLength(0);
|
|
174
|
+
});
|
|
117
175
|
});
|
|
118
176
|
//# sourceMappingURL=tambo-v1-provider.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-v1-provider.test.js","sourceRoot":"","sources":["../../../src/v1/providers/tambo-v1-provider.test.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,GAAG,EAA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AC,CAAC;QACtD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAC/C,yBAAyB,CAC1B,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,sBAAsB,GAAG,IAAI;aAChC,EAAE,EAAE;aACJ,iBAAiB,CAAC,iBAAiB,CAAC,CAAC;QAExC,MAAM,OAAO,GAAG,CAAC,EAAE,QAAQ,EAAiC,EAAE,EAAE,CAAC,CAC/D,oBAAC,eAAe,IACd,MAAM,EAAC,cAAc,EACrB,sBAAsB,EAAE,sBAAsB,IAE7C,QAAQ,CACO,CACnB,CAAC;QAEF,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,gBAAgB,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;QAErE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { QueryClient, useQueryClient } from \"@tanstack/react-query\";\nimport { renderHook, act } from \"@testing-library/react\";\nimport React from \"react\";\nimport { z } from \"zod\";\nimport { useTamboClient } from \"../../providers/tambo-client-provider\";\nimport { useTamboRegistry } from \"../../providers/tambo-registry-provider\";\nimport { useStreamState, useThreadManagement } from \"./tambo-v1-stream-context\";\nimport { TamboV1Provider } from \"./tambo-v1-provider\";\n\n// Mock the client provider to capture the apiKey\njest.mock(\"../../providers/tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n TamboClientProvider: ({ children }: { children: React.ReactNode }) =>\n children,\n}));\n\ndescribe(\"TamboV1Provider\", () => {\n const mockClient = {\n apiKey: \"test-api-key\",\n threads: {},\n } as unknown as TamboAI;\n\n beforeEach(() => {\n jest.mocked(useTamboClient).mockReturnValue(mockClient);\n });\n\n it(\"provides access to registry context\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.componentList).toBeDefined();\n expect(result.current.toolRegistry).toBeDefined();\n expect(typeof result.current.registerComponent).toBe(\"function\");\n expect(typeof result.current.registerTool).toBe(\"function\");\n });\n\n it(\"provides access to stream context\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useStreamState(), { wrapper });\n\n expect(result.current.threadMap).toBeDefined();\n expect(result.current.currentThreadId).toBeNull();\n });\n\n it(\"manages threads via useThreadManagement\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(\n () => ({\n state: useStreamState(),\n management: useThreadManagement(),\n }),\n { wrapper },\n );\n\n // Initially no thread\n expect(result.current.state.currentThreadId).toBeNull();\n\n // Initialize and switch to a thread\n act(() => {\n result.current.management.initThread(\"thread_123\");\n result.current.management.switchThread(\"thread_123\");\n });\n\n expect(result.current.state.currentThreadId).toBe(\"thread_123\");\n expect(result.current.state.threadMap.thread_123).toBeDefined();\n });\n\n it(\"provides access to query client\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useQueryClient(), { wrapper });\n\n expect(result.current).toBeInstanceOf(QueryClient);\n });\n\n it(\"uses custom query client when provided\", () => {\n const customClient = new QueryClient();\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" queryClient={customClient}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useQueryClient(), { wrapper });\n\n expect(result.current).toBe(customClient);\n });\n\n it(\"registers components when provided\", () => {\n const TestComponent = () => <div>Test</div>;\n const propsSchema = z.object({\n title: z.string().describe(\"The title\"),\n });\n const components = [\n {\n name: \"TestComponent\",\n description: \"A test component\",\n component: TestComponent,\n propsSchema,\n },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" components={components}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.componentList.TestComponent).toBeDefined();\n expect(result.current.componentList.TestComponent.name).toBe(\n \"TestComponent\",\n );\n });\n\n it(\"registers tools when provided\", () => {\n const inputSchema = z.object({\n query: z.string().describe(\"Search query\"),\n });\n const outputSchema = z.string().describe(\"Result string\");\n const tools = [\n {\n name: \"testTool\",\n description: \"A test tool\",\n tool: async () => \"result\",\n inputSchema,\n outputSchema,\n },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" tools={tools}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.toolRegistry.testTool).toBeDefined();\n expect(result.current.toolRegistry.testTool.name).toBe(\"testTool\");\n });\n\n it(\"registers MCP servers when provided\", () => {\n const mcpServers = [\n { url: \"https://mcp.example.com\", name: \"Example MCP\" },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" mcpServers={mcpServers}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.mcpServerInfos).toHaveLength(1);\n expect(result.current.mcpServerInfos[0].url).toBe(\n \"https://mcp.example.com\",\n );\n });\n\n it(\"provides onCallUnregisteredTool to registry\", () => {\n const onCallUnregisteredTool = jest\n .fn()\n .mockResolvedValue(\"fallback result\");\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider\n apiKey=\"test-api-key\"\n onCallUnregisteredTool={onCallUnregisteredTool}\n >\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.onCallUnregisteredTool).toBe(onCallUnregisteredTool);\n });\n});\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-v1-provider.test.js","sourceRoot":"","sources":["../../../src/v1/providers/tambo-v1-provider.test.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yCAAyC,CAAC;AAC3E,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAErE,iDAAiD;AACjD,IAAI,CAAC,IAAI,CAAC,uCAAuC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxD,cAAc,EAAE,IAAI,CAAC,EAAE,EAAE;IACzB,mBAAmB,EAAE,CAAC,EAAE,QAAQ,EAAiC,EAAE,EAAE,CACnE,QAAQ;CACX,CAAC,CAAC,CAAC;AAEJ,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,MAAM,UAAU,GAAG;QACjB,MAAM,EAAE,cAAc;QACtB,OAAO,EAAE,EAAE;KACU,CAAC;IAExB,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,MAAM,CAAC,cAAc,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AC,CAAC;QAE3E,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,iBAAiB,EAAE,CAAC;QACnD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { QueryClient, useQueryClient } from \"@tanstack/react-query\";\nimport { renderHook, act } from \"@testing-library/react\";\nimport React from \"react\";\nimport { z } from \"zod\";\nimport { useTamboClient } from \"../../providers/tambo-client-provider\";\nimport { useTamboRegistry } from \"../../providers/tambo-registry-provider\";\nimport { useTamboContextHelpers } from \"../../providers/tambo-context-helpers-provider\";\nimport { useStreamState, useThreadManagement } from \"./tambo-v1-stream-context\";\nimport { TamboV1Provider, useContextKey } from \"./tambo-v1-provider\";\n\n// Mock the client provider to capture the apiKey\njest.mock(\"../../providers/tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n TamboClientProvider: ({ children }: { children: React.ReactNode }) =>\n children,\n}));\n\ndescribe(\"TamboV1Provider\", () => {\n const mockClient = {\n apiKey: \"test-api-key\",\n threads: {},\n } as unknown as TamboAI;\n\n beforeEach(() => {\n jest.mocked(useTamboClient).mockReturnValue(mockClient);\n });\n\n it(\"provides access to registry context\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.componentList).toBeDefined();\n expect(result.current.toolRegistry).toBeDefined();\n expect(typeof result.current.registerComponent).toBe(\"function\");\n expect(typeof result.current.registerTool).toBe(\"function\");\n });\n\n it(\"provides access to stream context\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useStreamState(), { wrapper });\n\n expect(result.current.threadMap).toBeDefined();\n expect(result.current.currentThreadId).toBeNull();\n });\n\n it(\"manages threads via useThreadManagement\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(\n () => ({\n state: useStreamState(),\n management: useThreadManagement(),\n }),\n { wrapper },\n );\n\n // Initially no thread\n expect(result.current.state.currentThreadId).toBeNull();\n\n // Initialize and switch to a thread\n act(() => {\n result.current.management.initThread(\"thread_123\");\n result.current.management.switchThread(\"thread_123\");\n });\n\n expect(result.current.state.currentThreadId).toBe(\"thread_123\");\n expect(result.current.state.threadMap.thread_123).toBeDefined();\n });\n\n it(\"provides access to query client\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useQueryClient(), { wrapper });\n\n expect(result.current).toBeInstanceOf(QueryClient);\n });\n\n it(\"uses custom query client when provided\", () => {\n const customClient = new QueryClient();\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" queryClient={customClient}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useQueryClient(), { wrapper });\n\n expect(result.current).toBe(customClient);\n });\n\n it(\"registers components when provided\", () => {\n const TestComponent = () => <div>Test</div>;\n const propsSchema = z.object({\n title: z.string().describe(\"The title\"),\n });\n const components = [\n {\n name: \"TestComponent\",\n description: \"A test component\",\n component: TestComponent,\n propsSchema,\n },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" components={components}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.componentList.TestComponent).toBeDefined();\n expect(result.current.componentList.TestComponent.name).toBe(\n \"TestComponent\",\n );\n });\n\n it(\"registers tools when provided\", () => {\n const inputSchema = z.object({\n query: z.string().describe(\"Search query\"),\n });\n const outputSchema = z.string().describe(\"Result string\");\n const tools = [\n {\n name: \"testTool\",\n description: \"A test tool\",\n tool: async () => \"result\",\n inputSchema,\n outputSchema,\n },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" tools={tools}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.toolRegistry.testTool).toBeDefined();\n expect(result.current.toolRegistry.testTool.name).toBe(\"testTool\");\n });\n\n it(\"registers MCP servers when provided\", () => {\n const mcpServers = [\n { url: \"https://mcp.example.com\", name: \"Example MCP\" },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" mcpServers={mcpServers}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.mcpServerInfos).toHaveLength(1);\n expect(result.current.mcpServerInfos[0].url).toBe(\n \"https://mcp.example.com\",\n );\n });\n\n it(\"provides onCallUnregisteredTool to registry\", () => {\n const onCallUnregisteredTool = jest\n .fn()\n .mockResolvedValue(\"fallback result\");\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider\n apiKey=\"test-api-key\"\n onCallUnregisteredTool={onCallUnregisteredTool}\n >\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.onCallUnregisteredTool).toBe(onCallUnregisteredTool);\n });\n\n it(\"registers static resources when provided\", () => {\n const resources = [\n {\n uri: \"resource://test/example\",\n name: \"Test Resource\",\n description: \"A test resource\",\n mimeType: \"text/plain\",\n },\n ];\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" resources={resources}>\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.resources).toHaveLength(1);\n expect(result.current.resources[0].uri).toBe(\"resource://test/example\");\n expect(result.current.resources[0].name).toBe(\"Test Resource\");\n });\n\n it(\"registers resource source when listResources and getResource provided\", () => {\n const listResources = jest.fn().mockResolvedValue({ resources: [] });\n const getResource = jest.fn().mockResolvedValue({ contents: [] });\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider\n apiKey=\"test-api-key\"\n listResources={listResources}\n getResource={getResource}\n >\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboRegistry(), { wrapper });\n\n expect(result.current.resourceSource).toBeDefined();\n expect(result.current.resourceSource?.listResources).toBe(listResources);\n expect(result.current.resourceSource?.getResource).toBe(getResource);\n });\n\n it(\"provides contextKey via useContextKey hook\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" contextKey=\"my-context-key\">\n {children}\n </TamboV1Provider>\n );\n\n const { result } = renderHook(() => useContextKey(), { wrapper });\n\n expect(result.current).toBe(\"my-context-key\");\n });\n\n it(\"returns undefined from useContextKey when no contextKey provided\", () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useContextKey(), { wrapper });\n\n expect(result.current).toBeUndefined();\n });\n\n it(\"provides context helpers via useTamboContextHelpers hook\", async () => {\n const contextHelpers = {\n getUserName: () => \"Test User\",\n getCurrentTime: () => new Date().toISOString(),\n };\n\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\" contextHelpers={contextHelpers}>\n {children}\n </TamboV1Provider>\n );\n\n const { result, rerender } = renderHook(() => useTamboContextHelpers(), {\n wrapper,\n });\n\n // Helpers are registered via useEffect, so we need to trigger a rerender\n await act(async () => {\n rerender();\n });\n\n const helpers = result.current.getContextHelpers();\n expect(helpers.getUserName).toBe(contextHelpers.getUserName);\n expect(helpers.getCurrentTime).toBe(contextHelpers.getCurrentTime);\n });\n\n it(\"returns empty contextHelpers when none provided\", async () => {\n const wrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboV1Provider apiKey=\"test-api-key\">{children}</TamboV1Provider>\n );\n\n const { result } = renderHook(() => useTamboContextHelpers(), { wrapper });\n\n const helpers = result.current.getContextHelpers();\n expect(Object.keys(helpers)).toHaveLength(0);\n });\n});\n"]}
|
|
@@ -5,18 +5,43 @@
|
|
|
5
5
|
* Messages use Anthropic-style content blocks pattern where a message
|
|
6
6
|
* contains an array of content blocks (text, tool calls, tool results, components).
|
|
7
7
|
*/
|
|
8
|
+
import type { ReactElement } from "react";
|
|
8
9
|
export type { TextContent, ToolUseContent, ToolResultContent, ComponentContent, ResourceContent, } from "@tambo-ai/typescript-sdk/resources/threads/threads";
|
|
9
10
|
export type { InputMessage } from "@tambo-ai/typescript-sdk/resources/threads/runs";
|
|
10
11
|
export type { MessageListResponse, MessageGetResponse, } from "@tambo-ai/typescript-sdk/resources/threads/messages";
|
|
11
12
|
import type { TextContent, ToolUseContent, ToolResultContent, ComponentContent, ResourceContent } from "@tambo-ai/typescript-sdk/resources/threads/threads";
|
|
13
|
+
/**
|
|
14
|
+
* Streaming state for component content blocks.
|
|
15
|
+
* Tracks the lifecycle of component prop/state streaming.
|
|
16
|
+
*/
|
|
17
|
+
export type ComponentStreamingState = "started" | "streaming" | "done";
|
|
18
|
+
/**
|
|
19
|
+
* Extended ComponentContent with streaming state and rendered element.
|
|
20
|
+
* Used by the v1 SDK to track component rendering lifecycle.
|
|
21
|
+
*/
|
|
22
|
+
export interface V1ComponentContent extends ComponentContent {
|
|
23
|
+
/**
|
|
24
|
+
* Current streaming state of this component's props.
|
|
25
|
+
* - 'started': Component block created, awaiting props
|
|
26
|
+
* - 'streaming': Props are being streamed
|
|
27
|
+
* - 'done': Props streaming complete
|
|
28
|
+
*/
|
|
29
|
+
streamingState: ComponentStreamingState;
|
|
30
|
+
/**
|
|
31
|
+
* The rendered React element for this component.
|
|
32
|
+
* undefined if not yet rendered, null if the component couldn't be found in the registry.
|
|
33
|
+
*/
|
|
34
|
+
renderedComponent?: ReactElement | null;
|
|
35
|
+
}
|
|
12
36
|
/**
|
|
13
37
|
* Message role (from SDK)
|
|
14
38
|
*/
|
|
15
39
|
export type MessageRole = "user" | "assistant";
|
|
16
40
|
/**
|
|
17
|
-
* Union type of all content block types
|
|
41
|
+
* Union type of all content block types.
|
|
42
|
+
* Uses V1ComponentContent which includes streaming state and rendered component.
|
|
18
43
|
*/
|
|
19
|
-
export type Content = TextContent | ToolUseContent | ToolResultContent |
|
|
44
|
+
export type Content = TextContent | ToolUseContent | ToolResultContent | V1ComponentContent | ResourceContent;
|
|
20
45
|
/**
|
|
21
46
|
* Message in a thread (simplified from SDK's MessageGetResponse)
|
|
22
47
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message.d.ts","sourceRoot":"","sources":["../../../src/v1/types/message.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"message.d.ts","sourceRoot":"","sources":["../../../src/v1/types/message.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAG1C,YAAY,EACV,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,GAChB,MAAM,oDAAoD,CAAC;AAG5D,YAAY,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAEpF,YAAY,EACV,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,qDAAqD,CAAC;AAG7D,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EAChB,MAAM,oDAAoD,CAAC;AAE5D;;;GAGG;AACH,MAAM,MAAM,uBAAuB,GAAG,SAAS,GAAG,WAAW,GAAG,MAAM,CAAC;AAEvE;;;GAGG;AACH,MAAM,WAAW,kBAAmB,SAAQ,gBAAgB;IAC1D;;;;;OAKG;IACH,cAAc,EAAE,uBAAuB,CAAC;IAExC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;CACzC;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,WAAW,CAAC;AAE/C;;;GAGG;AACH,MAAM,MAAM,OAAO,GACf,WAAW,GACX,cAAc,GACd,iBAAiB,GACjB,kBAAkB,GAClB,eAAe,CAAC;AAEpB;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IAEX,uCAAuC;IACvC,IAAI,EAAE,WAAW,CAAC;IAElB,oCAAoC;IACpC,OAAO,EAAE,OAAO,EAAE,CAAC;IAEnB,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;IAElB,uBAAuB;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/v1/types/message.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG","sourcesContent":["/**\n * Message and Content Types for v1 API\n *\n * Re-exports message and content types from `@tambo-ai/typescript-sdk`.\n * Messages use Anthropic-style content blocks pattern where a message\n * contains an array of content blocks (text, tool calls, tool results, components).\n */\n\n// Re-export content block types from TypeScript SDK\nexport type {\n TextContent,\n ToolUseContent,\n ToolResultContent,\n ComponentContent,\n ResourceContent,\n} from \"@tambo-ai/typescript-sdk/resources/threads/threads\";\n\n// Re-export message types from TypeScript SDK\nexport type { InputMessage } from \"@tambo-ai/typescript-sdk/resources/threads/runs\";\n\nexport type {\n MessageListResponse,\n MessageGetResponse,\n} from \"@tambo-ai/typescript-sdk/resources/threads/messages\";\n\n// Import for Content union type\nimport type {\n TextContent,\n ToolUseContent,\n ToolResultContent,\n ComponentContent,\n ResourceContent,\n} from \"@tambo-ai/typescript-sdk/resources/threads/threads\";\n\n/**\n * Message role (from SDK)\n */\nexport type MessageRole = \"user\" | \"assistant\";\n\n/**\n * Union type of all content block types
|
|
1
|
+
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/v1/types/message.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG","sourcesContent":["/**\n * Message and Content Types for v1 API\n *\n * Re-exports message and content types from `@tambo-ai/typescript-sdk`.\n * Messages use Anthropic-style content blocks pattern where a message\n * contains an array of content blocks (text, tool calls, tool results, components).\n */\n\nimport type { ReactElement } from \"react\";\n\n// Re-export content block types from TypeScript SDK\nexport type {\n TextContent,\n ToolUseContent,\n ToolResultContent,\n ComponentContent,\n ResourceContent,\n} from \"@tambo-ai/typescript-sdk/resources/threads/threads\";\n\n// Re-export message types from TypeScript SDK\nexport type { InputMessage } from \"@tambo-ai/typescript-sdk/resources/threads/runs\";\n\nexport type {\n MessageListResponse,\n MessageGetResponse,\n} from \"@tambo-ai/typescript-sdk/resources/threads/messages\";\n\n// Import for Content union type\nimport type {\n TextContent,\n ToolUseContent,\n ToolResultContent,\n ComponentContent,\n ResourceContent,\n} from \"@tambo-ai/typescript-sdk/resources/threads/threads\";\n\n/**\n * Streaming state for component content blocks.\n * Tracks the lifecycle of component prop/state streaming.\n */\nexport type ComponentStreamingState = \"started\" | \"streaming\" | \"done\";\n\n/**\n * Extended ComponentContent with streaming state and rendered element.\n * Used by the v1 SDK to track component rendering lifecycle.\n */\nexport interface V1ComponentContent extends ComponentContent {\n /**\n * Current streaming state of this component's props.\n * - 'started': Component block created, awaiting props\n * - 'streaming': Props are being streamed\n * - 'done': Props streaming complete\n */\n streamingState: ComponentStreamingState;\n\n /**\n * The rendered React element for this component.\n * undefined if not yet rendered, null if the component couldn't be found in the registry.\n */\n renderedComponent?: ReactElement | null;\n}\n\n/**\n * Message role (from SDK)\n */\nexport type MessageRole = \"user\" | \"assistant\";\n\n/**\n * Union type of all content block types.\n * Uses V1ComponentContent which includes streaming state and rendered component.\n */\nexport type Content =\n | TextContent\n | ToolUseContent\n | ToolResultContent\n | V1ComponentContent\n | ResourceContent;\n\n/**\n * Message in a thread (simplified from SDK's MessageGetResponse)\n */\nexport interface TamboV1Message {\n /** Unique message identifier */\n id: string;\n\n /** Message role (user or assistant) */\n role: MessageRole;\n\n /** Content blocks in the message */\n content: Content[];\n\n /** When the message was created */\n createdAt: string;\n\n /** Message metadata */\n metadata?: Record<string, unknown>;\n}\n"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { ComponentRegistry } from "../../model/component-metadata";
|
|
2
|
+
import type { Content, TamboV1Message, V1ComponentContent } from "../types/message";
|
|
3
|
+
/**
|
|
4
|
+
* Context for component content blocks.
|
|
5
|
+
* Provides access to the component ID and thread ID for component state hooks.
|
|
6
|
+
*/
|
|
7
|
+
export interface V1ComponentContentContext {
|
|
8
|
+
/** Component instance ID */
|
|
9
|
+
componentId: string;
|
|
10
|
+
/** Thread ID the component belongs to */
|
|
11
|
+
threadId: string;
|
|
12
|
+
/** Message ID the component belongs to */
|
|
13
|
+
messageId: string;
|
|
14
|
+
/** Component name */
|
|
15
|
+
componentName: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Hook to access the current component content context.
|
|
19
|
+
* Must be used within a rendered component.
|
|
20
|
+
* @returns Component content context
|
|
21
|
+
* @throws {Error} If used outside a rendered component
|
|
22
|
+
*/
|
|
23
|
+
export declare function useV1ComponentContent(): V1ComponentContentContext;
|
|
24
|
+
/**
|
|
25
|
+
* Hook to optionally access the current component content context.
|
|
26
|
+
* Returns null if not within a rendered component.
|
|
27
|
+
* @returns Component content context or null
|
|
28
|
+
*/
|
|
29
|
+
export declare function useV1ComponentContentOptional(): V1ComponentContentContext | null;
|
|
30
|
+
/**
|
|
31
|
+
* Options for rendering a component content block.
|
|
32
|
+
*/
|
|
33
|
+
export interface RenderComponentOptions {
|
|
34
|
+
/** Thread ID for the component context */
|
|
35
|
+
threadId: string;
|
|
36
|
+
/** Message ID the component belongs to */
|
|
37
|
+
messageId: string;
|
|
38
|
+
/** Component registry to look up components */
|
|
39
|
+
componentList: ComponentRegistry;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if a content block is a component.
|
|
43
|
+
* @param content - Content block to check
|
|
44
|
+
* @returns True if content is a V1ComponentContent
|
|
45
|
+
*/
|
|
46
|
+
export declare function isComponentContent(content: Content): content is V1ComponentContent;
|
|
47
|
+
/**
|
|
48
|
+
* Render a component content block into a React element.
|
|
49
|
+
*
|
|
50
|
+
* Looks up the component in the registry, creates a React element with props,
|
|
51
|
+
* and wraps it with the component content context provider.
|
|
52
|
+
* @param content - Component content block to render
|
|
53
|
+
* @param options - Rendering options including registry and context info
|
|
54
|
+
* @returns V1ComponentContent with the renderedComponent attached
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* const rendered = renderComponentContent(componentContent, {
|
|
58
|
+
* threadId: 'thread_123',
|
|
59
|
+
* messageId: 'msg_456',
|
|
60
|
+
* componentList: registry.componentList,
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* // Use in JSX:
|
|
64
|
+
* {rendered.renderedComponent}
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare function renderComponentContent(content: V1ComponentContent, options: RenderComponentOptions): V1ComponentContent;
|
|
68
|
+
/**
|
|
69
|
+
* Render all component content blocks in a message.
|
|
70
|
+
*
|
|
71
|
+
* Renders component content blocks and attaches renderedComponent.
|
|
72
|
+
* Non-component content blocks are passed through unchanged.
|
|
73
|
+
* @param content - Array of content blocks
|
|
74
|
+
* @param options - Rendering options including registry and context info
|
|
75
|
+
* @returns Array of content with rendered components
|
|
76
|
+
*/
|
|
77
|
+
export declare function renderMessageContent(content: Content[], options: RenderComponentOptions): Content[];
|
|
78
|
+
/**
|
|
79
|
+
* Render all components in a message.
|
|
80
|
+
*
|
|
81
|
+
* Creates a new message object with all component content blocks rendered.
|
|
82
|
+
* @param message - Message to render components for
|
|
83
|
+
* @param options - Rendering options (threadId is extracted from message if not provided)
|
|
84
|
+
* @returns Message with rendered component content
|
|
85
|
+
*/
|
|
86
|
+
export declare function renderMessageComponents(message: TamboV1Message, options: Omit<RenderComponentOptions, "messageId"> & {
|
|
87
|
+
threadId: string;
|
|
88
|
+
}): TamboV1Message;
|
|
89
|
+
//# sourceMappingURL=component-renderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component-renderer.d.ts","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"AAuDA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;AAE1B;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,4BAA4B;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,qBAAqB;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB;AA+BD;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,yBAAyB,CAQjE;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,IAAI,yBAAyB,GAAG,IAAI,CAEhF;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,0CAA0C;IAC1C,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,aAAa,EAAE,iBAAiB,CAAC;CAClC;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,OAAO,GACf,OAAO,IAAI,kBAAkB,CAE/B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,sBAAsB,GAC9B,kBAAkB,CAwDpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,OAAO,EAAE,EAClB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,EAAE,CAQX;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,IAAI,CAAC,sBAAsB,EAAE,WAAW,CAAC,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,GACxE,cAAc,CAUhB"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
/**
|
|
3
|
+
* Component Renderer Utility for v1 API
|
|
4
|
+
*
|
|
5
|
+
* Provides utilities for rendering React components from component content blocks.
|
|
6
|
+
* Components are looked up in the registry and wrapped with context providers.
|
|
7
|
+
*/
|
|
8
|
+
import React, { createContext, createElement, useContext, useMemo, } from "react";
|
|
9
|
+
/**
|
|
10
|
+
* Props that should be filtered out when rendering components.
|
|
11
|
+
* These could be used for event handler injection or other security concerns.
|
|
12
|
+
*/
|
|
13
|
+
const DANGEROUS_PROP_PATTERNS = [
|
|
14
|
+
/^on[A-Z]/, // Event handlers (onClick, onError, etc.)
|
|
15
|
+
/^dangerouslySetInnerHTML$/,
|
|
16
|
+
];
|
|
17
|
+
const DANGEROUS_PROP_NAMES = new Set(["ref", "key", "children"]);
|
|
18
|
+
/**
|
|
19
|
+
* Sanitize props by removing potentially dangerous properties.
|
|
20
|
+
* Filters out event handlers, refs, and other props that could be abused.
|
|
21
|
+
* @param props - Raw props from the component content
|
|
22
|
+
* @returns Sanitized props safe for spreading to components
|
|
23
|
+
*/
|
|
24
|
+
function sanitizeProps(props) {
|
|
25
|
+
const sanitized = {};
|
|
26
|
+
for (const [key, value] of Object.entries(props)) {
|
|
27
|
+
// Skip dangerous prop names
|
|
28
|
+
if (DANGEROUS_PROP_NAMES.has(key)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
// Skip props matching dangerous patterns
|
|
32
|
+
if (DANGEROUS_PROP_PATTERNS.some((pattern) => pattern.test(key))) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
sanitized[key] = value;
|
|
36
|
+
}
|
|
37
|
+
return sanitized;
|
|
38
|
+
}
|
|
39
|
+
const ComponentContentContext = createContext(null);
|
|
40
|
+
/**
|
|
41
|
+
* Provider for component content context.
|
|
42
|
+
* Wraps rendered components to provide access to component metadata.
|
|
43
|
+
* @returns Provider component with memoized context value
|
|
44
|
+
*/
|
|
45
|
+
function V1ComponentContentProvider({ children, componentId, threadId, messageId, componentName, }) {
|
|
46
|
+
// Memoize context value to prevent unnecessary re-renders of consumers
|
|
47
|
+
const value = useMemo(() => ({ componentId, threadId, messageId, componentName }), [componentId, threadId, messageId, componentName]);
|
|
48
|
+
return (React.createElement(ComponentContentContext.Provider, { value: value }, children));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Hook to access the current component content context.
|
|
52
|
+
* Must be used within a rendered component.
|
|
53
|
+
* @returns Component content context
|
|
54
|
+
* @throws {Error} If used outside a rendered component
|
|
55
|
+
*/
|
|
56
|
+
export function useV1ComponentContent() {
|
|
57
|
+
const context = useContext(ComponentContentContext);
|
|
58
|
+
if (!context) {
|
|
59
|
+
throw new Error("useV1ComponentContent must be used within a rendered component");
|
|
60
|
+
}
|
|
61
|
+
return context;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Hook to optionally access the current component content context.
|
|
65
|
+
* Returns null if not within a rendered component.
|
|
66
|
+
* @returns Component content context or null
|
|
67
|
+
*/
|
|
68
|
+
export function useV1ComponentContentOptional() {
|
|
69
|
+
return useContext(ComponentContentContext);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if a content block is a component.
|
|
73
|
+
* @param content - Content block to check
|
|
74
|
+
* @returns True if content is a V1ComponentContent
|
|
75
|
+
*/
|
|
76
|
+
export function isComponentContent(content) {
|
|
77
|
+
return content.type === "component";
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Render a component content block into a React element.
|
|
81
|
+
*
|
|
82
|
+
* Looks up the component in the registry, creates a React element with props,
|
|
83
|
+
* and wraps it with the component content context provider.
|
|
84
|
+
* @param content - Component content block to render
|
|
85
|
+
* @param options - Rendering options including registry and context info
|
|
86
|
+
* @returns V1ComponentContent with the renderedComponent attached
|
|
87
|
+
* @example
|
|
88
|
+
* ```tsx
|
|
89
|
+
* const rendered = renderComponentContent(componentContent, {
|
|
90
|
+
* threadId: 'thread_123',
|
|
91
|
+
* messageId: 'msg_456',
|
|
92
|
+
* componentList: registry.componentList,
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* // Use in JSX:
|
|
96
|
+
* {rendered.renderedComponent}
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function renderComponentContent(content, options) {
|
|
100
|
+
const { threadId, messageId, componentList } = options;
|
|
101
|
+
// Look up component in registry
|
|
102
|
+
const registeredComponent = componentList[content.name];
|
|
103
|
+
if (!registeredComponent) {
|
|
104
|
+
console.warn(`Component "${content.name}" not found in registry`);
|
|
105
|
+
return {
|
|
106
|
+
...content,
|
|
107
|
+
renderedComponent: null,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const Component = registeredComponent.component;
|
|
111
|
+
const LoadingComponent = registeredComponent.loadingComponent;
|
|
112
|
+
// Determine if we should show loading state
|
|
113
|
+
const isStreaming = content.streamingState !== "done";
|
|
114
|
+
// Sanitize props to prevent injection of event handlers or other dangerous props
|
|
115
|
+
const props = sanitizeProps(content.props);
|
|
116
|
+
// Create the component element
|
|
117
|
+
let element;
|
|
118
|
+
if (isStreaming && LoadingComponent) {
|
|
119
|
+
// Show loading component during streaming (with props for partial data display)
|
|
120
|
+
element = createElement(LoadingComponent, props);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Show main component - props stream in as they're filled out
|
|
124
|
+
element = createElement(Component, {
|
|
125
|
+
...props,
|
|
126
|
+
// Pass state as initialState prop only if not already provided
|
|
127
|
+
...(props.initialState === undefined
|
|
128
|
+
? { initialState: content.state }
|
|
129
|
+
: {}),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
// Wrap with component content context
|
|
133
|
+
const wrappedElement = (React.createElement(V1ComponentContentProvider, { componentId: content.id, threadId: threadId, messageId: messageId, componentName: content.name }, element));
|
|
134
|
+
return {
|
|
135
|
+
...content,
|
|
136
|
+
renderedComponent: wrappedElement,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Render all component content blocks in a message.
|
|
141
|
+
*
|
|
142
|
+
* Renders component content blocks and attaches renderedComponent.
|
|
143
|
+
* Non-component content blocks are passed through unchanged.
|
|
144
|
+
* @param content - Array of content blocks
|
|
145
|
+
* @param options - Rendering options including registry and context info
|
|
146
|
+
* @returns Array of content with rendered components
|
|
147
|
+
*/
|
|
148
|
+
export function renderMessageContent(content, options) {
|
|
149
|
+
return content.map((block) => {
|
|
150
|
+
if (isComponentContent(block)) {
|
|
151
|
+
return renderComponentContent(block, options);
|
|
152
|
+
}
|
|
153
|
+
// Pass through non-component content unchanged
|
|
154
|
+
return block;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Render all components in a message.
|
|
159
|
+
*
|
|
160
|
+
* Creates a new message object with all component content blocks rendered.
|
|
161
|
+
* @param message - Message to render components for
|
|
162
|
+
* @param options - Rendering options (threadId is extracted from message if not provided)
|
|
163
|
+
* @returns Message with rendered component content
|
|
164
|
+
*/
|
|
165
|
+
export function renderMessageComponents(message, options) {
|
|
166
|
+
const renderedContent = renderMessageContent(message.content, {
|
|
167
|
+
...options,
|
|
168
|
+
messageId: message.id,
|
|
169
|
+
});
|
|
170
|
+
return {
|
|
171
|
+
...message,
|
|
172
|
+
content: renderedContent,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=component-renderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component-renderer.js","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,EACZ,aAAa,EACb,aAAa,EACb,UAAU,EACV,OAAO,GAER,MAAM,OAAO,CAAC;AAEf;;;GAGG;AACH,MAAM,uBAAuB,GAAG;IAC9B,UAAU,EAAE,0CAA0C;IACtD,2BAA2B;CAC5B,CAAC;AAEF,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC;AAEjE;;;;;GAKG;AACH,SAAS,aAAa,CACpB,KAA8B;IAE9B,MAAM,SAAS,GAA4B,EAAE,CAAC;IAE9C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACjD,4BAA4B;QAC5B,IAAI,oBAAoB,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAClC,SAAS;QACX,CAAC;QAED,yCAAyC;QACzC,IAAI,uBAAuB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACjE,SAAS;QACX,CAAC;QAED,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACzB,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAuBD,MAAM,uBAAuB,GAAG,aAAa,CAC3C,IAAI,CACL,CAAC;AAEF;;;;GAIG;AACH,SAAS,0BAA0B,CAAC,EAClC,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,SAAS,EACT,aAAa,GAC6C;IAC1D,uEAAuE;IACvE,MAAM,KA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,CAAC;IACf,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAuB,EACvB,OAAyE;IAEzE,MAAM,eAAe,GAAG,oBAAoB,CAAC,OAAO,CAAC,OAAO,EAAE;QAC5D,GAAG,OAAO;QACV,SAAS,EAAE,OAAO,CAAC,EAAE;KACtB,CAAC,CAAC;IAEH,OAAO;QACL,GAAG,OAAO;QACV,OAAO,EAAE,eAAe;KACzB,CAAC;AACJ,CAAC","sourcesContent":["\"use client\";\n\n/**\n * Component Renderer Utility for v1 API\n *\n * Provides utilities for rendering React components from component content blocks.\n * Components are looked up in the registry and wrapped with context providers.\n */\n\nimport React, {\n createContext,\n createElement,\n useContext,\n useMemo,\n type ReactElement,\n} from \"react\";\n\n/**\n * Props that should be filtered out when rendering components.\n * These could be used for event handler injection or other security concerns.\n */\nconst DANGEROUS_PROP_PATTERNS = [\n /^on[A-Z]/, // Event handlers (onClick, onError, etc.)\n /^dangerouslySetInnerHTML$/,\n];\n\nconst DANGEROUS_PROP_NAMES = new Set([\"ref\", \"key\", \"children\"]);\n\n/**\n * Sanitize props by removing potentially dangerous properties.\n * Filters out event handlers, refs, and other props that could be abused.\n * @param props - Raw props from the component content\n * @returns Sanitized props safe for spreading to components\n */\nfunction sanitizeProps(\n props: Record<string, unknown>,\n): Record<string, unknown> {\n const sanitized: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(props)) {\n // Skip dangerous prop names\n if (DANGEROUS_PROP_NAMES.has(key)) {\n continue;\n }\n\n // Skip props matching dangerous patterns\n if (DANGEROUS_PROP_PATTERNS.some((pattern) => pattern.test(key))) {\n continue;\n }\n\n sanitized[key] = value;\n }\n\n return sanitized;\n}\nimport type { ComponentRegistry } from \"../../model/component-metadata\";\nimport type {\n Content,\n TamboV1Message,\n V1ComponentContent,\n} from \"../types/message\";\n\n/**\n * Context for component content blocks.\n * Provides access to the component ID and thread ID for component state hooks.\n */\nexport interface V1ComponentContentContext {\n /** Component instance ID */\n componentId: string;\n /** Thread ID the component belongs to */\n threadId: string;\n /** Message ID the component belongs to */\n messageId: string;\n /** Component name */\n componentName: string;\n}\n\nconst ComponentContentContext = createContext<V1ComponentContentContext | null>(\n null,\n);\n\n/**\n * Provider for component content context.\n * Wraps rendered components to provide access to component metadata.\n * @returns Provider component with memoized context value\n */\nfunction V1ComponentContentProvider({\n children,\n componentId,\n threadId,\n messageId,\n componentName,\n}: V1ComponentContentContext & { children: React.ReactNode }) {\n // Memoize context value to prevent unnecessary re-renders of consumers\n const value = useMemo(\n () => ({ componentId, threadId, messageId, componentName }),\n [componentId, threadId, messageId, componentName],\n );\n\n return (\n <ComponentContentContext.Provider value={value}>\n {children}\n </ComponentContentContext.Provider>\n );\n}\n\n/**\n * Hook to access the current component content context.\n * Must be used within a rendered component.\n * @returns Component content context\n * @throws {Error} If used outside a rendered component\n */\nexport function useV1ComponentContent(): V1ComponentContentContext {\n const context = useContext(ComponentContentContext);\n if (!context) {\n throw new Error(\n \"useV1ComponentContent must be used within a rendered component\",\n );\n }\n return context;\n}\n\n/**\n * Hook to optionally access the current component content context.\n * Returns null if not within a rendered component.\n * @returns Component content context or null\n */\nexport function useV1ComponentContentOptional(): V1ComponentContentContext | null {\n return useContext(ComponentContentContext);\n}\n\n/**\n * Options for rendering a component content block.\n */\nexport interface RenderComponentOptions {\n /** Thread ID for the component context */\n threadId: string;\n /** Message ID the component belongs to */\n messageId: string;\n /** Component registry to look up components */\n componentList: ComponentRegistry;\n}\n\n/**\n * Check if a content block is a component.\n * @param content - Content block to check\n * @returns True if content is a V1ComponentContent\n */\nexport function isComponentContent(\n content: Content,\n): content is V1ComponentContent {\n return content.type === \"component\";\n}\n\n/**\n * Render a component content block into a React element.\n *\n * Looks up the component in the registry, creates a React element with props,\n * and wraps it with the component content context provider.\n * @param content - Component content block to render\n * @param options - Rendering options including registry and context info\n * @returns V1ComponentContent with the renderedComponent attached\n * @example\n * ```tsx\n * const rendered = renderComponentContent(componentContent, {\n * threadId: 'thread_123',\n * messageId: 'msg_456',\n * componentList: registry.componentList,\n * });\n *\n * // Use in JSX:\n * {rendered.renderedComponent}\n * ```\n */\nexport function renderComponentContent(\n content: V1ComponentContent,\n options: RenderComponentOptions,\n): V1ComponentContent {\n const { threadId, messageId, componentList } = options;\n\n // Look up component in registry\n const registeredComponent = componentList[content.name];\n\n if (!registeredComponent) {\n console.warn(`Component \"${content.name}\" not found in registry`);\n return {\n ...content,\n renderedComponent: null,\n };\n }\n\n const Component = registeredComponent.component;\n const LoadingComponent = registeredComponent.loadingComponent;\n\n // Determine if we should show loading state\n const isStreaming = content.streamingState !== \"done\";\n\n // Sanitize props to prevent injection of event handlers or other dangerous props\n const props = sanitizeProps(content.props as Record<string, unknown>);\n\n // Create the component element\n let element: ReactElement;\n\n if (isStreaming && LoadingComponent) {\n // Show loading component during streaming (with props for partial data display)\n element = createElement(LoadingComponent, props);\n } else {\n // Show main component - props stream in as they're filled out\n element = createElement(Component, {\n ...props,\n // Pass state as initialState prop only if not already provided\n ...(props.initialState === undefined\n ? { initialState: content.state }\n : {}),\n });\n }\n\n // Wrap with component content context\n const wrappedElement = (\n <V1ComponentContentProvider\n componentId={content.id}\n threadId={threadId}\n messageId={messageId}\n componentName={content.name}\n >\n {element}\n </V1ComponentContentProvider>\n );\n\n return {\n ...content,\n renderedComponent: wrappedElement,\n };\n}\n\n/**\n * Render all component content blocks in a message.\n *\n * Renders component content blocks and attaches renderedComponent.\n * Non-component content blocks are passed through unchanged.\n * @param content - Array of content blocks\n * @param options - Rendering options including registry and context info\n * @returns Array of content with rendered components\n */\nexport function renderMessageContent(\n content: Content[],\n options: RenderComponentOptions,\n): Content[] {\n return content.map((block) => {\n if (isComponentContent(block)) {\n return renderComponentContent(block, options);\n }\n // Pass through non-component content unchanged\n return block;\n });\n}\n\n/**\n * Render all components in a message.\n *\n * Creates a new message object with all component content blocks rendered.\n * @param message - Message to render components for\n * @param options - Rendering options (threadId is extracted from message if not provided)\n * @returns Message with rendered component content\n */\nexport function renderMessageComponents(\n message: TamboV1Message,\n options: Omit<RenderComponentOptions, \"messageId\"> & { threadId: string },\n): TamboV1Message {\n const renderedContent = renderMessageContent(message.content, {\n ...options,\n messageId: message.id,\n });\n\n return {\n ...message,\n content: renderedContent,\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component-renderer.test.d.ts","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.test.tsx"],"names":[],"mappings":""}
|