@tambo-ai/react 0.72.0 → 0.73.1
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/mcp/mcp-hooks.d.ts +4 -0
- package/dist/mcp/mcp-hooks.d.ts.map +1 -1
- package/dist/mcp/mcp-hooks.js +4 -0
- package/dist/mcp/mcp-hooks.js.map +1 -1
- package/dist/providers/tambo-provider.d.ts +3 -0
- package/dist/providers/tambo-provider.d.ts.map +1 -1
- package/dist/providers/tambo-provider.js +3 -0
- package/dist/providers/tambo-provider.js.map +1 -1
- package/dist/util/resource-content-resolver.d.ts.map +1 -1
- package/dist/util/resource-content-resolver.js +2 -0
- package/dist/util/resource-content-resolver.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-messages.test.js +22 -9
- package/dist/v1/hooks/use-tambo-v1-messages.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.d.ts +1 -0
- package/dist/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.js +9 -2
- package/dist/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-send-message.test.js +22 -9
- package/dist/v1/hooks/use-tambo-v1-send-message.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-suggestions.d.ts +91 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.js +152 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.d.ts +2 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.d.ts.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.js +511 -0
- package/dist/v1/hooks/use-tambo-v1-suggestions.test.js.map +1 -0
- package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts +6 -57
- package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-input.js +7 -67
- package/dist/v1/hooks/use-tambo-v1-thread-input.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.js +201 -72
- package/dist/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-list.d.ts +6 -4
- package/dist/v1/hooks/use-tambo-v1-thread-list.d.ts.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-list.js +2 -2
- package/dist/v1/hooks/use-tambo-v1-thread-list.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1-thread-list.test.js +2 -2
- package/dist/v1/hooks/use-tambo-v1-thread-list.test.js.map +1 -1
- package/dist/v1/hooks/use-tambo-v1.test.js +16 -7
- package/dist/v1/hooks/use-tambo-v1.test.js.map +1 -1
- package/dist/v1/index.d.ts +22 -13
- package/dist/v1/index.d.ts.map +1 -1
- package/dist/v1/index.js +31 -39
- package/dist/v1/index.js.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.d.ts +27 -9
- package/dist/v1/providers/tambo-v1-provider.d.ts.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.js +22 -11
- package/dist/v1/providers/tambo-v1-provider.js.map +1 -1
- package/dist/v1/providers/tambo-v1-provider.test.js +27 -10
- package/dist/v1/providers/tambo-v1-provider.test.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.d.ts +19 -10
- package/dist/v1/providers/tambo-v1-stream-context.d.ts.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.js +43 -53
- package/dist/v1/providers/tambo-v1-stream-context.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stream-context.test.js +94 -19
- package/dist/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
- package/dist/v1/providers/tambo-v1-stub-provider.d.ts +74 -0
- package/dist/v1/providers/tambo-v1-stub-provider.d.ts.map +1 -0
- package/dist/v1/providers/tambo-v1-stub-provider.js +212 -0
- package/dist/v1/providers/tambo-v1-stub-provider.js.map +1 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.d.ts +2 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.d.ts.map +1 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.js +162 -0
- package/dist/v1/providers/tambo-v1-stub-provider.test.js.map +1 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.d.ts +105 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.d.ts.map +1 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.js +191 -0
- package/dist/v1/providers/tambo-v1-thread-input-provider.js.map +1 -0
- package/dist/v1/utils/component-renderer.d.ts +15 -67
- package/dist/v1/utils/component-renderer.d.ts.map +1 -1
- package/dist/v1/utils/component-renderer.js +3 -149
- package/dist/v1/utils/component-renderer.js.map +1 -1
- package/dist/v1/utils/component-renderer.test.js +15 -350
- package/dist/v1/utils/component-renderer.test.js.map +1 -1
- package/esm/mcp/mcp-hooks.d.ts +4 -0
- package/esm/mcp/mcp-hooks.d.ts.map +1 -1
- package/esm/mcp/mcp-hooks.js +4 -0
- package/esm/mcp/mcp-hooks.js.map +1 -1
- package/esm/providers/tambo-provider.d.ts +3 -0
- package/esm/providers/tambo-provider.d.ts.map +1 -1
- package/esm/providers/tambo-provider.js +3 -0
- package/esm/providers/tambo-provider.js.map +1 -1
- package/esm/util/resource-content-resolver.d.ts.map +1 -1
- package/esm/util/resource-content-resolver.js +2 -0
- package/esm/util/resource-content-resolver.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-messages.test.js +22 -9
- package/esm/v1/hooks/use-tambo-v1-messages.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.d.ts +1 -0
- package/esm/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.js +9 -2
- package/esm/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-send-message.test.js +22 -9
- package/esm/v1/hooks/use-tambo-v1-send-message.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-suggestions.d.ts +91 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.js +149 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.d.ts +2 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.d.ts.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.js +506 -0
- package/esm/v1/hooks/use-tambo-v1-suggestions.test.js.map +1 -0
- package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts +6 -57
- package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-input.js +5 -66
- package/esm/v1/hooks/use-tambo-v1-thread-input.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.js +199 -73
- package/esm/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-list.d.ts +6 -4
- package/esm/v1/hooks/use-tambo-v1-thread-list.d.ts.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-list.js +2 -2
- package/esm/v1/hooks/use-tambo-v1-thread-list.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1-thread-list.test.js +2 -2
- package/esm/v1/hooks/use-tambo-v1-thread-list.test.js.map +1 -1
- package/esm/v1/hooks/use-tambo-v1.test.js +16 -7
- package/esm/v1/hooks/use-tambo-v1.test.js.map +1 -1
- package/esm/v1/index.d.ts +22 -13
- package/esm/v1/index.d.ts.map +1 -1
- package/esm/v1/index.js +23 -18
- package/esm/v1/index.js.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.d.ts +27 -9
- package/esm/v1/providers/tambo-v1-provider.d.ts.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.js +20 -10
- package/esm/v1/providers/tambo-v1-provider.js.map +1 -1
- package/esm/v1/providers/tambo-v1-provider.test.js +28 -11
- package/esm/v1/providers/tambo-v1-provider.test.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.d.ts +19 -10
- package/esm/v1/providers/tambo-v1-stream-context.d.ts.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.js +44 -54
- package/esm/v1/providers/tambo-v1-stream-context.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stream-context.test.js +95 -20
- package/esm/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
- package/esm/v1/providers/tambo-v1-stub-provider.d.ts +74 -0
- package/esm/v1/providers/tambo-v1-stub-provider.d.ts.map +1 -0
- package/esm/v1/providers/tambo-v1-stub-provider.js +176 -0
- package/esm/v1/providers/tambo-v1-stub-provider.js.map +1 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.d.ts +2 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.d.ts.map +1 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.js +157 -0
- package/esm/v1/providers/tambo-v1-stub-provider.test.js.map +1 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.d.ts +105 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.d.ts.map +1 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.js +153 -0
- package/esm/v1/providers/tambo-v1-thread-input-provider.js.map +1 -0
- package/esm/v1/utils/component-renderer.d.ts +15 -67
- package/esm/v1/utils/component-renderer.d.ts.map +1 -1
- package/esm/v1/utils/component-renderer.js +4 -146
- package/esm/v1/utils/component-renderer.js.map +1 -1
- package/esm/v1/utils/component-renderer.test.js +16 -351
- package/esm/v1/utils/component-renderer.test.js.map +1 -1
- package/package.json +2 -2
|
@@ -2,47 +2,17 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Component Renderer Utility for v1 API
|
|
4
4
|
*
|
|
5
|
-
* Provides
|
|
6
|
-
* Components
|
|
5
|
+
* Provides the component content context for rendered components.
|
|
6
|
+
* Components can use useV1ComponentContent() to access their context.
|
|
7
7
|
*/
|
|
8
|
-
import React, { createContext,
|
|
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
|
-
}
|
|
8
|
+
import React, { createContext, useContext, useMemo } from "react";
|
|
39
9
|
const ComponentContentContext = createContext(null);
|
|
40
10
|
/**
|
|
41
11
|
* Provider for component content context.
|
|
42
12
|
* Wraps rendered components to provide access to component metadata.
|
|
43
13
|
* @returns Provider component with memoized context value
|
|
44
14
|
*/
|
|
45
|
-
function V1ComponentContentProvider({ children, componentId, threadId, messageId, componentName, }) {
|
|
15
|
+
export function V1ComponentContentProvider({ children, componentId, threadId, messageId, componentName, }) {
|
|
46
16
|
// Memoize context value to prevent unnecessary re-renders of consumers
|
|
47
17
|
const value = useMemo(() => ({ componentId, threadId, messageId, componentName }), [componentId, threadId, messageId, componentName]);
|
|
48
18
|
return (React.createElement(ComponentContentContext.Provider, { value: value }, children));
|
|
@@ -60,116 +30,4 @@ export function useV1ComponentContent() {
|
|
|
60
30
|
}
|
|
61
31
|
return context;
|
|
62
32
|
}
|
|
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
33
|
//# sourceMappingURL=component-renderer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"component-renderer.js","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"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","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"]}
|
|
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,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAiBlE,MAAM,uBAAuB,GAAG,aAAa,CAC3C,IAAI,CACL,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,0BAA0B,CAAC,EACzC,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,SAAS,EACT,aAAa,GAC6C;IAC1D,uEAAuE;IACvE,MAAM,KAAK,GAAG,OAAO,CACnB,GAAG,EAAE,CAAC,CAAC,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,CAAC,EAC3D,CAAC,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,CAAC,CAClD,CAAC;IAEF,OAAO,CACL,oBAAC,uBAAuB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC3C,QAAQ,CACwB,CACpC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB;IACnC,MAAM,OAAO,GAAG,UAAU,CAAC,uBAAuB,CAAC,CAAC;IACpD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["\"use client\";\n\n/**\n * Component Renderer Utility for v1 API\n *\n * Provides the component content context for rendered components.\n * Components can use useV1ComponentContent() to access their context.\n */\n\nimport React, { createContext, useContext, useMemo } from \"react\";\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 */\nexport function 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"]}
|
|
@@ -1,367 +1,27 @@
|
|
|
1
|
-
// React import needed for JSX transform (jsxImportSource is not set to react-jsx)
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3
1
|
import React from "react";
|
|
4
2
|
import { render, screen } from "@testing-library/react";
|
|
5
|
-
import {
|
|
6
|
-
// Test component that displays its props
|
|
7
|
-
function TestComponent({ title, count }) {
|
|
8
|
-
return (React.createElement("div", { "data-testid": "test-component" },
|
|
9
|
-
React.createElement("span", { "data-testid": "title" }, title),
|
|
10
|
-
React.createElement("span", { "data-testid": "count" }, count)));
|
|
11
|
-
}
|
|
12
|
-
// Test loading component
|
|
13
|
-
function TestLoadingComponent() {
|
|
14
|
-
return React.createElement("div", { "data-testid": "loading" }, "Loading...");
|
|
15
|
-
}
|
|
3
|
+
import { useV1ComponentContent, V1ComponentContentProvider, } from "./component-renderer";
|
|
16
4
|
// Test component that uses the content context
|
|
17
|
-
|
|
5
|
+
const ContextAwareComponent = () => {
|
|
18
6
|
const context = useV1ComponentContent();
|
|
19
7
|
return (React.createElement("div", { "data-testid": "context-aware" },
|
|
20
8
|
React.createElement("span", { "data-testid": "componentId" }, context.componentId),
|
|
21
9
|
React.createElement("span", { "data-testid": "threadId" }, context.threadId),
|
|
22
|
-
React.createElement("span", { "data-testid": "messageId" }, context.messageId)
|
|
23
|
-
}
|
|
24
|
-
const mockRegistry = {
|
|
25
|
-
TestComponent: {
|
|
26
|
-
component: TestComponent,
|
|
27
|
-
name: "TestComponent",
|
|
28
|
-
description: "A test component",
|
|
29
|
-
props: {},
|
|
30
|
-
contextTools: [],
|
|
31
|
-
},
|
|
32
|
-
TestWithLoading: {
|
|
33
|
-
component: TestComponent,
|
|
34
|
-
loadingComponent: TestLoadingComponent,
|
|
35
|
-
name: "TestWithLoading",
|
|
36
|
-
description: "A test component with loading state",
|
|
37
|
-
props: {},
|
|
38
|
-
contextTools: [],
|
|
39
|
-
},
|
|
40
|
-
ContextAware: {
|
|
41
|
-
component: ContextAwareComponent,
|
|
42
|
-
name: "ContextAware",
|
|
43
|
-
description: "A context-aware component",
|
|
44
|
-
props: {},
|
|
45
|
-
contextTools: [],
|
|
46
|
-
},
|
|
10
|
+
React.createElement("span", { "data-testid": "messageId" }, context.messageId),
|
|
11
|
+
React.createElement("span", { "data-testid": "componentName" }, context.componentName)));
|
|
47
12
|
};
|
|
48
|
-
describe("
|
|
49
|
-
it("
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
id: "comp_1",
|
|
53
|
-
name: "Test",
|
|
54
|
-
props: {},
|
|
55
|
-
streamingState: "done",
|
|
56
|
-
};
|
|
57
|
-
expect(isComponentContent(content)).toBe(true);
|
|
58
|
-
});
|
|
59
|
-
it("returns false for text content", () => {
|
|
60
|
-
const content = { type: "text", text: "hello" };
|
|
61
|
-
expect(isComponentContent(content)).toBe(false);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
describe("renderComponentContent", () => {
|
|
65
|
-
it("renders a component from registry", () => {
|
|
66
|
-
const content = {
|
|
67
|
-
type: "component",
|
|
68
|
-
id: "comp_1",
|
|
69
|
-
name: "TestComponent",
|
|
70
|
-
props: { title: "Hello", count: 42 },
|
|
71
|
-
streamingState: "done",
|
|
72
|
-
};
|
|
73
|
-
const result = renderComponentContent(content, {
|
|
74
|
-
threadId: "thread_1",
|
|
75
|
-
messageId: "msg_1",
|
|
76
|
-
componentList: mockRegistry,
|
|
77
|
-
});
|
|
78
|
-
expect(result.renderedComponent).not.toBeNull();
|
|
79
|
-
// Render and check output
|
|
80
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
81
|
-
expect(screen.getByTestId("title")).toHaveTextContent("Hello");
|
|
82
|
-
expect(screen.getByTestId("count")).toHaveTextContent("42");
|
|
83
|
-
});
|
|
84
|
-
it("returns null renderedComponent for unknown component", () => {
|
|
85
|
-
const content = {
|
|
86
|
-
type: "component",
|
|
87
|
-
id: "comp_1",
|
|
88
|
-
name: "UnknownComponent",
|
|
89
|
-
props: {},
|
|
90
|
-
streamingState: "done",
|
|
91
|
-
};
|
|
92
|
-
const consoleWarn = jest.spyOn(console, "warn").mockImplementation();
|
|
93
|
-
const result = renderComponentContent(content, {
|
|
94
|
-
threadId: "thread_1",
|
|
95
|
-
messageId: "msg_1",
|
|
96
|
-
componentList: mockRegistry,
|
|
97
|
-
});
|
|
98
|
-
expect(result.renderedComponent).toBeNull();
|
|
99
|
-
expect(consoleWarn).toHaveBeenCalledWith('Component "UnknownComponent" not found in registry');
|
|
100
|
-
consoleWarn.mockRestore();
|
|
101
|
-
});
|
|
102
|
-
it("shows loading component when streaming", () => {
|
|
103
|
-
const content = {
|
|
104
|
-
type: "component",
|
|
105
|
-
id: "comp_1",
|
|
106
|
-
name: "TestWithLoading",
|
|
107
|
-
props: { title: "Loading Test", count: 0 },
|
|
108
|
-
streamingState: "streaming",
|
|
109
|
-
};
|
|
110
|
-
const result = renderComponentContent(content, {
|
|
111
|
-
threadId: "thread_1",
|
|
112
|
-
messageId: "msg_1",
|
|
113
|
-
componentList: mockRegistry,
|
|
114
|
-
});
|
|
115
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
116
|
-
expect(screen.getByTestId("loading")).toHaveTextContent("Loading...");
|
|
117
|
-
});
|
|
118
|
-
it("shows main component when done streaming", () => {
|
|
119
|
-
const content = {
|
|
120
|
-
type: "component",
|
|
121
|
-
id: "comp_1",
|
|
122
|
-
name: "TestWithLoading",
|
|
123
|
-
props: { title: "Done Test", count: 99 },
|
|
124
|
-
streamingState: "done",
|
|
125
|
-
};
|
|
126
|
-
const result = renderComponentContent(content, {
|
|
127
|
-
threadId: "thread_1",
|
|
128
|
-
messageId: "msg_1",
|
|
129
|
-
componentList: mockRegistry,
|
|
130
|
-
});
|
|
131
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
132
|
-
expect(screen.getByTestId("title")).toHaveTextContent("Done Test");
|
|
133
|
-
expect(screen.getByTestId("count")).toHaveTextContent("99");
|
|
134
|
-
});
|
|
135
|
-
it("provides component context to rendered components", () => {
|
|
136
|
-
const content = {
|
|
137
|
-
type: "component",
|
|
138
|
-
id: "comp_123",
|
|
139
|
-
name: "ContextAware",
|
|
140
|
-
props: {},
|
|
141
|
-
streamingState: "done",
|
|
142
|
-
};
|
|
143
|
-
const result = renderComponentContent(content, {
|
|
144
|
-
threadId: "thread_456",
|
|
145
|
-
messageId: "msg_789",
|
|
146
|
-
componentList: mockRegistry,
|
|
147
|
-
});
|
|
148
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
13
|
+
describe("V1ComponentContentProvider", () => {
|
|
14
|
+
it("provides context to child components", () => {
|
|
15
|
+
render(React.createElement(V1ComponentContentProvider, { componentId: "comp_123", threadId: "thread_456", messageId: "msg_789", componentName: "TestComponent" },
|
|
16
|
+
React.createElement(ContextAwareComponent, null)));
|
|
149
17
|
expect(screen.getByTestId("componentId")).toHaveTextContent("comp_123");
|
|
150
18
|
expect(screen.getByTestId("threadId")).toHaveTextContent("thread_456");
|
|
151
19
|
expect(screen.getByTestId("messageId")).toHaveTextContent("msg_789");
|
|
20
|
+
expect(screen.getByTestId("componentName")).toHaveTextContent("TestComponent");
|
|
152
21
|
});
|
|
153
22
|
});
|
|
154
|
-
describe("
|
|
155
|
-
it("
|
|
156
|
-
const content = [
|
|
157
|
-
{ type: "text", text: "Hello" },
|
|
158
|
-
{
|
|
159
|
-
type: "component",
|
|
160
|
-
id: "comp_1",
|
|
161
|
-
name: "TestComponent",
|
|
162
|
-
props: { title: "Test", count: 1 },
|
|
163
|
-
streamingState: "done",
|
|
164
|
-
},
|
|
165
|
-
{ type: "text", text: "World" },
|
|
166
|
-
];
|
|
167
|
-
const result = renderMessageContent(content, {
|
|
168
|
-
threadId: "thread_1",
|
|
169
|
-
messageId: "msg_1",
|
|
170
|
-
componentList: mockRegistry,
|
|
171
|
-
});
|
|
172
|
-
expect(result).toHaveLength(3);
|
|
173
|
-
expect(result[0]).toEqual({ type: "text", text: "Hello" });
|
|
174
|
-
expect(result[1].renderedComponent).not.toBeNull();
|
|
175
|
-
expect(result[2]).toEqual({ type: "text", text: "World" });
|
|
176
|
-
});
|
|
177
|
-
it("handles multiple component blocks in a message", () => {
|
|
178
|
-
const content = [
|
|
179
|
-
{
|
|
180
|
-
type: "component",
|
|
181
|
-
id: "comp_1",
|
|
182
|
-
name: "TestComponent",
|
|
183
|
-
props: { title: "First", count: 1 },
|
|
184
|
-
streamingState: "done",
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
type: "component",
|
|
188
|
-
id: "comp_2",
|
|
189
|
-
name: "TestComponent",
|
|
190
|
-
props: { title: "Second", count: 2 },
|
|
191
|
-
streamingState: "done",
|
|
192
|
-
},
|
|
193
|
-
];
|
|
194
|
-
const result = renderMessageContent(content, {
|
|
195
|
-
threadId: "thread_1",
|
|
196
|
-
messageId: "msg_1",
|
|
197
|
-
componentList: mockRegistry,
|
|
198
|
-
});
|
|
199
|
-
expect(result).toHaveLength(2);
|
|
200
|
-
expect(result[0].renderedComponent).not.toBeNull();
|
|
201
|
-
expect(result[1].renderedComponent).not.toBeNull();
|
|
202
|
-
});
|
|
203
|
-
it("handles tool_use content blocks unchanged", () => {
|
|
204
|
-
const content = [
|
|
205
|
-
{
|
|
206
|
-
type: "tool_use",
|
|
207
|
-
id: "tool_1",
|
|
208
|
-
name: "search",
|
|
209
|
-
input: { query: "test" },
|
|
210
|
-
},
|
|
211
|
-
];
|
|
212
|
-
const result = renderMessageContent(content, {
|
|
213
|
-
threadId: "thread_1",
|
|
214
|
-
messageId: "msg_1",
|
|
215
|
-
componentList: mockRegistry,
|
|
216
|
-
});
|
|
217
|
-
expect(result).toHaveLength(1);
|
|
218
|
-
expect(result[0]).toEqual(content[0]);
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
describe("renderComponentContent edge cases", () => {
|
|
222
|
-
it("shows main component when streamingState is 'started' (no loading component)", () => {
|
|
223
|
-
const content = {
|
|
224
|
-
type: "component",
|
|
225
|
-
id: "comp_1",
|
|
226
|
-
name: "TestComponent",
|
|
227
|
-
props: { title: "Started", count: 0 },
|
|
228
|
-
streamingState: "started",
|
|
229
|
-
};
|
|
230
|
-
const result = renderComponentContent(content, {
|
|
231
|
-
threadId: "thread_1",
|
|
232
|
-
messageId: "msg_1",
|
|
233
|
-
componentList: mockRegistry,
|
|
234
|
-
});
|
|
235
|
-
// TestComponent has no loading component, so it shows main even when streaming
|
|
236
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
237
|
-
expect(screen.getByTestId("title")).toHaveTextContent("Started");
|
|
238
|
-
});
|
|
239
|
-
it("shows loading component when streamingState is 'started' and loading exists", () => {
|
|
240
|
-
const content = {
|
|
241
|
-
type: "component",
|
|
242
|
-
id: "comp_1",
|
|
243
|
-
name: "TestWithLoading",
|
|
244
|
-
props: { title: "Started", count: 0 },
|
|
245
|
-
streamingState: "started",
|
|
246
|
-
};
|
|
247
|
-
const result = renderComponentContent(content, {
|
|
248
|
-
threadId: "thread_1",
|
|
249
|
-
messageId: "msg_1",
|
|
250
|
-
componentList: mockRegistry,
|
|
251
|
-
});
|
|
252
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
253
|
-
expect(screen.getByTestId("loading")).toHaveTextContent("Loading...");
|
|
254
|
-
});
|
|
255
|
-
it("passes state as initialState prop", () => {
|
|
256
|
-
// Component that reads initialState
|
|
257
|
-
function StatefulComponent({ initialState, }) {
|
|
258
|
-
return (React.createElement("div", { "data-testid": "initial-count" }, initialState?.count ?? "none"));
|
|
259
|
-
}
|
|
260
|
-
const registryWithStateful = {
|
|
261
|
-
StatefulComponent: {
|
|
262
|
-
component: StatefulComponent,
|
|
263
|
-
name: "StatefulComponent",
|
|
264
|
-
description: "A stateful component",
|
|
265
|
-
props: {},
|
|
266
|
-
contextTools: [],
|
|
267
|
-
},
|
|
268
|
-
};
|
|
269
|
-
const content = {
|
|
270
|
-
type: "component",
|
|
271
|
-
id: "comp_1",
|
|
272
|
-
name: "StatefulComponent",
|
|
273
|
-
props: {},
|
|
274
|
-
state: { count: 42 },
|
|
275
|
-
streamingState: "done",
|
|
276
|
-
};
|
|
277
|
-
const result = renderComponentContent(content, {
|
|
278
|
-
threadId: "thread_1",
|
|
279
|
-
messageId: "msg_1",
|
|
280
|
-
componentList: registryWithStateful,
|
|
281
|
-
});
|
|
282
|
-
render(React.createElement(React.Fragment, null, result.renderedComponent));
|
|
283
|
-
expect(screen.getByTestId("initial-count")).toHaveTextContent("42");
|
|
284
|
-
});
|
|
285
|
-
it("preserves original content properties in returned object", () => {
|
|
286
|
-
const content = {
|
|
287
|
-
type: "component",
|
|
288
|
-
id: "comp_custom",
|
|
289
|
-
name: "TestComponent",
|
|
290
|
-
props: { title: "Test", count: 5 },
|
|
291
|
-
state: { value: "preserved" },
|
|
292
|
-
streamingState: "done",
|
|
293
|
-
};
|
|
294
|
-
const result = renderComponentContent(content, {
|
|
295
|
-
threadId: "thread_1",
|
|
296
|
-
messageId: "msg_1",
|
|
297
|
-
componentList: mockRegistry,
|
|
298
|
-
});
|
|
299
|
-
expect(result.id).toBe("comp_custom");
|
|
300
|
-
expect(result.name).toBe("TestComponent");
|
|
301
|
-
expect(result.props).toEqual({ title: "Test", count: 5 });
|
|
302
|
-
expect(result.state).toEqual({ value: "preserved" });
|
|
303
|
-
expect(result.streamingState).toBe("done");
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
describe("useV1ComponentContentOptional", () => {
|
|
307
|
-
it("returns null when used outside rendered component", () => {
|
|
308
|
-
function TestConsumer() {
|
|
309
|
-
const context = useV1ComponentContentOptional();
|
|
310
|
-
return (React.createElement("div", { "data-testid": "context" }, context ? "has context" : "no context"));
|
|
311
|
-
}
|
|
312
|
-
render(React.createElement(TestConsumer, null));
|
|
313
|
-
expect(screen.getByTestId("context")).toHaveTextContent("no context");
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
describe("renderMessageComponents", () => {
|
|
317
|
-
it("renders all components in a message", () => {
|
|
318
|
-
const message = {
|
|
319
|
-
id: "msg_1",
|
|
320
|
-
role: "assistant",
|
|
321
|
-
content: [
|
|
322
|
-
{ type: "text", text: "Here is the weather:" },
|
|
323
|
-
{
|
|
324
|
-
type: "component",
|
|
325
|
-
id: "comp_1",
|
|
326
|
-
name: "TestComponent",
|
|
327
|
-
props: { title: "Weather", count: 72 },
|
|
328
|
-
streamingState: "done",
|
|
329
|
-
},
|
|
330
|
-
],
|
|
331
|
-
createdAt: "2024-01-01T00:00:00.000Z",
|
|
332
|
-
};
|
|
333
|
-
const result = renderMessageComponents(message, {
|
|
334
|
-
threadId: "thread_1",
|
|
335
|
-
componentList: mockRegistry,
|
|
336
|
-
});
|
|
337
|
-
expect(result.id).toBe("msg_1");
|
|
338
|
-
expect(result.content).toHaveLength(2);
|
|
339
|
-
expect(result.content[0]).toEqual({
|
|
340
|
-
type: "text",
|
|
341
|
-
text: "Here is the weather:",
|
|
342
|
-
});
|
|
343
|
-
expect(result.content[1].renderedComponent).not.toBeNull();
|
|
344
|
-
});
|
|
345
|
-
it("preserves message metadata", () => {
|
|
346
|
-
const message = {
|
|
347
|
-
id: "msg_123",
|
|
348
|
-
role: "assistant",
|
|
349
|
-
content: [],
|
|
350
|
-
createdAt: "2024-01-01T00:00:00.000Z",
|
|
351
|
-
metadata: { custom: "value" },
|
|
352
|
-
};
|
|
353
|
-
const result = renderMessageComponents(message, {
|
|
354
|
-
threadId: "thread_1",
|
|
355
|
-
componentList: mockRegistry,
|
|
356
|
-
});
|
|
357
|
-
expect(result.id).toBe("msg_123");
|
|
358
|
-
expect(result.role).toBe("assistant");
|
|
359
|
-
expect(result.createdAt).toBe("2024-01-01T00:00:00.000Z");
|
|
360
|
-
expect(result.metadata).toEqual({ custom: "value" });
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
describe("useV1ComponentContent error handling", () => {
|
|
364
|
-
it("throws when used outside rendered component", () => {
|
|
23
|
+
describe("useV1ComponentContent", () => {
|
|
24
|
+
it("throws when used outside provider", () => {
|
|
365
25
|
function TestConsumer() {
|
|
366
26
|
useV1ComponentContent();
|
|
367
27
|
return React.createElement("div", null, "Should not render");
|
|
@@ -371,5 +31,10 @@ describe("useV1ComponentContent error handling", () => {
|
|
|
371
31
|
expect(() => render(React.createElement(TestConsumer, null))).toThrow("useV1ComponentContent must be used within a rendered component");
|
|
372
32
|
consoleSpy.mockRestore();
|
|
373
33
|
});
|
|
34
|
+
it("returns context when used within provider", () => {
|
|
35
|
+
render(React.createElement(V1ComponentContentProvider, { componentId: "comp_test", threadId: "thread_test", messageId: "msg_test", componentName: "TestComp" },
|
|
36
|
+
React.createElement(ContextAwareComponent, null)));
|
|
37
|
+
expect(screen.getByTestId("componentId")).toHaveTextContent("comp_test");
|
|
38
|
+
});
|
|
374
39
|
});
|
|
375
40
|
//# sourceMappingURL=component-renderer.test.js.map
|