@tambo-ai/react 0.37.2 → 0.38.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/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/model/tambo-interactable.d.ts +24 -0
- package/dist/model/tambo-interactable.d.ts.map +1 -0
- package/dist/model/tambo-interactable.js +3 -0
- package/dist/model/tambo-interactable.js.map +1 -0
- package/dist/providers/__tests__/tambo-prop-stream-provider.test.d.ts +2 -0
- package/dist/providers/__tests__/tambo-prop-stream-provider.test.d.ts.map +1 -0
- package/dist/providers/__tests__/tambo-prop-stream-provider.test.js +278 -0
- package/dist/providers/__tests__/tambo-prop-stream-provider.test.js.map +1 -0
- package/dist/providers/__tests__/tambo-thread-provider.test.js +7 -0
- package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/dist/providers/hoc/with-tambo-interactable.d.ts +34 -0
- package/dist/providers/hoc/with-tambo-interactable.d.ts.map +1 -0
- package/dist/providers/hoc/with-tambo-interactable.js +119 -0
- package/dist/providers/hoc/with-tambo-interactable.js.map +1 -0
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +3 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/tambo-interactable-provider.d.ts +20 -0
- package/dist/providers/tambo-interactable-provider.d.ts.map +1 -0
- package/dist/providers/tambo-interactable-provider.js +243 -0
- package/dist/providers/tambo-interactable-provider.js.map +1 -0
- package/dist/providers/tambo-prop-stream-provider.d.ts +96 -0
- package/dist/providers/tambo-prop-stream-provider.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider.js +185 -0
- package/dist/providers/tambo-prop-stream-provider.js.map +1 -0
- package/dist/providers/tambo-provider.d.ts +6 -4
- package/dist/providers/tambo-provider.d.ts.map +1 -1
- package/dist/providers/tambo-provider.js +9 -4
- package/dist/providers/tambo-provider.js.map +1 -1
- package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
- package/dist/providers/tambo-thread-provider.js +14 -0
- package/dist/providers/tambo-thread-provider.js.map +1 -1
- package/esm/index.d.ts +4 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -1
- package/esm/index.js.map +1 -1
- package/esm/model/tambo-interactable.d.ts +24 -0
- package/esm/model/tambo-interactable.d.ts.map +1 -0
- package/esm/model/tambo-interactable.js +2 -0
- package/esm/model/tambo-interactable.js.map +1 -0
- package/esm/providers/__tests__/tambo-prop-stream-provider.test.d.ts +2 -0
- package/esm/providers/__tests__/tambo-prop-stream-provider.test.d.ts.map +1 -0
- package/esm/providers/__tests__/tambo-prop-stream-provider.test.js +273 -0
- package/esm/providers/__tests__/tambo-prop-stream-provider.test.js.map +1 -0
- package/esm/providers/__tests__/tambo-thread-provider.test.js +7 -0
- package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/esm/providers/hoc/with-tambo-interactable.d.ts +34 -0
- package/esm/providers/hoc/with-tambo-interactable.d.ts.map +1 -0
- package/esm/providers/hoc/with-tambo-interactable.js +83 -0
- package/esm/providers/hoc/with-tambo-interactable.js.map +1 -0
- package/esm/providers/index.d.ts +1 -0
- package/esm/providers/index.d.ts.map +1 -1
- package/esm/providers/index.js +1 -0
- package/esm/providers/index.js.map +1 -1
- package/esm/providers/tambo-interactable-provider.d.ts +20 -0
- package/esm/providers/tambo-interactable-provider.d.ts.map +1 -0
- package/esm/providers/tambo-interactable-provider.js +205 -0
- package/esm/providers/tambo-interactable-provider.js.map +1 -0
- package/esm/providers/tambo-prop-stream-provider.d.ts +96 -0
- package/esm/providers/tambo-prop-stream-provider.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider.js +148 -0
- package/esm/providers/tambo-prop-stream-provider.js.map +1 -0
- package/esm/providers/tambo-provider.d.ts +6 -4
- package/esm/providers/tambo-provider.d.ts.map +1 -1
- package/esm/providers/tambo-provider.js +9 -4
- package/esm/providers/tambo-provider.js.map +1 -1
- package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
- package/esm/providers/tambo-thread-provider.js +14 -0
- package/esm/providers/tambo-thread-provider.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// react-sdk/src/providers/tambo-interactable-provider.tsx
|
|
3
|
+
"use client";
|
|
4
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
+
if (k2 === undefined) k2 = k;
|
|
6
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(o, k2, desc);
|
|
11
|
+
}) : (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
o[k2] = m[k];
|
|
14
|
+
}));
|
|
15
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
+
}) : function(o, v) {
|
|
18
|
+
o["default"] = v;
|
|
19
|
+
});
|
|
20
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
21
|
+
var ownKeys = function(o) {
|
|
22
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
23
|
+
var ar = [];
|
|
24
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
25
|
+
return ar;
|
|
26
|
+
};
|
|
27
|
+
return ownKeys(o);
|
|
28
|
+
};
|
|
29
|
+
return function (mod) {
|
|
30
|
+
if (mod && mod.__esModule) return mod;
|
|
31
|
+
var result = {};
|
|
32
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
33
|
+
__setModuleDefault(result, mod);
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.useTamboInteractable = exports.TamboInteractableProvider = void 0;
|
|
39
|
+
const react_1 = __importStar(require("react"));
|
|
40
|
+
const zod_1 = require("zod");
|
|
41
|
+
const tambo_component_provider_1 = require("./tambo-component-provider");
|
|
42
|
+
const TamboInteractableContext = (0, react_1.createContext)({
|
|
43
|
+
interactableComponents: [],
|
|
44
|
+
addInteractableComponent: () => "",
|
|
45
|
+
removeInteractableComponent: () => { },
|
|
46
|
+
updateInteractableComponentProps: () => { },
|
|
47
|
+
getInteractableComponent: () => undefined,
|
|
48
|
+
getInteractableComponentsByName: () => [],
|
|
49
|
+
clearAllInteractableComponents: () => { },
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* The TamboInteractableProvider manages a list of components that are currently
|
|
53
|
+
* interactable, allowing tambo to interact with them by updating their props. It also registers tools
|
|
54
|
+
* for Tambo to perform CRUD operations on the components list.
|
|
55
|
+
* @param props - The props for the TamboInteractableProvider
|
|
56
|
+
* @param props.children - The children to wrap
|
|
57
|
+
* @returns The TamboInteractableProvider component
|
|
58
|
+
*/
|
|
59
|
+
const TamboInteractableProvider = ({ children, }) => {
|
|
60
|
+
const [interactableComponents, setInteractableComponents] = (0, react_1.useState)([]);
|
|
61
|
+
const { registerTool } = (0, tambo_component_provider_1.useTamboComponent)();
|
|
62
|
+
(0, react_1.useEffect)(() => {
|
|
63
|
+
registerTool({
|
|
64
|
+
name: "get_all_interactable_components",
|
|
65
|
+
description: "Get all currently interactable components with their details including their current props. These are components that you can interact with on behalf of the user.",
|
|
66
|
+
tool: () => {
|
|
67
|
+
return {
|
|
68
|
+
components: interactableComponents,
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
toolSchema: zod_1.z.function().returns(zod_1.z.object({
|
|
72
|
+
components: zod_1.z.array(zod_1.z.object({
|
|
73
|
+
id: zod_1.z.string(),
|
|
74
|
+
componentName: zod_1.z.string(),
|
|
75
|
+
props: zod_1.z.record(zod_1.z.any()),
|
|
76
|
+
propsSchema: zod_1.z.object({}).optional(),
|
|
77
|
+
})),
|
|
78
|
+
})),
|
|
79
|
+
});
|
|
80
|
+
registerTool({
|
|
81
|
+
name: "get_interactable_component_by_id",
|
|
82
|
+
description: "Get a specific interactable component by its ID",
|
|
83
|
+
tool: (componentId) => {
|
|
84
|
+
const component = interactableComponents.find((c) => c.id === componentId);
|
|
85
|
+
if (!component) {
|
|
86
|
+
return {
|
|
87
|
+
success: false,
|
|
88
|
+
error: `Component with ID ${componentId} not found`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
success: true,
|
|
93
|
+
component: {
|
|
94
|
+
id: component.id,
|
|
95
|
+
componentName: component.name,
|
|
96
|
+
props: component.props,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
toolSchema: zod_1.z
|
|
101
|
+
.function()
|
|
102
|
+
.args(zod_1.z.string())
|
|
103
|
+
.returns(zod_1.z.object({
|
|
104
|
+
success: zod_1.z.boolean(),
|
|
105
|
+
component: zod_1.z
|
|
106
|
+
.object({
|
|
107
|
+
id: zod_1.z.string(),
|
|
108
|
+
componentName: zod_1.z.string(),
|
|
109
|
+
props: zod_1.z.record(zod_1.z.any()),
|
|
110
|
+
})
|
|
111
|
+
.optional(),
|
|
112
|
+
error: zod_1.z.string().optional(),
|
|
113
|
+
})),
|
|
114
|
+
});
|
|
115
|
+
registerTool({
|
|
116
|
+
name: "remove_interactable_component",
|
|
117
|
+
description: "Remove an interactable component from the system",
|
|
118
|
+
tool: (componentId) => {
|
|
119
|
+
const component = interactableComponents.find((c) => c.id === componentId);
|
|
120
|
+
if (!component) {
|
|
121
|
+
return {
|
|
122
|
+
success: false,
|
|
123
|
+
error: `Component with ID ${componentId} not found`,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
setInteractableComponents((prev) => prev.filter((c) => c.id !== componentId));
|
|
127
|
+
return {
|
|
128
|
+
success: true,
|
|
129
|
+
componentId,
|
|
130
|
+
removedComponent: {
|
|
131
|
+
id: component.id,
|
|
132
|
+
componentName: component.name,
|
|
133
|
+
props: component.props,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
toolSchema: zod_1.z
|
|
138
|
+
.function()
|
|
139
|
+
.args(zod_1.z.string())
|
|
140
|
+
.returns(zod_1.z.object({
|
|
141
|
+
success: zod_1.z.boolean(),
|
|
142
|
+
componentId: zod_1.z.string(),
|
|
143
|
+
removedComponent: zod_1.z.object({
|
|
144
|
+
id: zod_1.z.string(),
|
|
145
|
+
componentName: zod_1.z.string(),
|
|
146
|
+
props: zod_1.z.record(zod_1.z.any()),
|
|
147
|
+
}),
|
|
148
|
+
error: zod_1.z.string().optional(),
|
|
149
|
+
})),
|
|
150
|
+
});
|
|
151
|
+
}, [interactableComponents, registerTool]);
|
|
152
|
+
const updateInteractableComponentProps = (0, react_1.useCallback)((id, newProps) => {
|
|
153
|
+
let updateResult = "Updated successfully";
|
|
154
|
+
setInteractableComponents((prev) => {
|
|
155
|
+
const componentExists = prev.some((c) => c.id === id);
|
|
156
|
+
if (!componentExists) {
|
|
157
|
+
updateResult = `Error: Component with ID ${id} not found`;
|
|
158
|
+
return prev;
|
|
159
|
+
}
|
|
160
|
+
const updatedComponents = prev.map((c) => c.id === id ? { ...c, props: { ...c.props, ...newProps } } : c);
|
|
161
|
+
// Check if the update actually changed anything
|
|
162
|
+
const originalComponent = prev.find((c) => c.id === id);
|
|
163
|
+
const updatedComponent = updatedComponents.find((c) => c.id === id);
|
|
164
|
+
if (!originalComponent || !updatedComponent) {
|
|
165
|
+
updateResult = `Error: Failed to update component with ID ${id}`;
|
|
166
|
+
return prev;
|
|
167
|
+
}
|
|
168
|
+
// Check if props actually changed
|
|
169
|
+
const propsChanged = JSON.stringify(originalComponent.props) !==
|
|
170
|
+
JSON.stringify(updatedComponent.props);
|
|
171
|
+
if (!propsChanged) {
|
|
172
|
+
updateResult = `Warning: No changes detected for component with ID ${id}. The update might not have worked.`;
|
|
173
|
+
return prev;
|
|
174
|
+
}
|
|
175
|
+
return updatedComponents;
|
|
176
|
+
});
|
|
177
|
+
return updateResult;
|
|
178
|
+
}, []);
|
|
179
|
+
const registerInteractableComponentUpdateTool = (0, react_1.useCallback)((component) => {
|
|
180
|
+
const schemaForArgs = typeof component.propsSchema === "object" &&
|
|
181
|
+
"describe" in component.propsSchema
|
|
182
|
+
? component.propsSchema
|
|
183
|
+
: zod_1.z.object({});
|
|
184
|
+
registerTool({
|
|
185
|
+
name: `update_interactable_component_${component.id}`,
|
|
186
|
+
description: `Update the props of interactable component ${component.id} (${component.name})`,
|
|
187
|
+
tool: (componentId, newProps) => {
|
|
188
|
+
return updateInteractableComponentProps(componentId, newProps);
|
|
189
|
+
},
|
|
190
|
+
toolSchema: zod_1.z
|
|
191
|
+
.function()
|
|
192
|
+
.args(zod_1.z
|
|
193
|
+
.string()
|
|
194
|
+
.describe("The ID of the interactable component to update"), schemaForArgs.describe("The new props to update the component with"))
|
|
195
|
+
.returns(zod_1.z.string()),
|
|
196
|
+
});
|
|
197
|
+
}, [registerTool, updateInteractableComponentProps]);
|
|
198
|
+
const addInteractableComponent = (0, react_1.useCallback)((component) => {
|
|
199
|
+
const id = `${component.name}-${Math.random().toString(36).substr(2, 9)}`;
|
|
200
|
+
const newComponent = {
|
|
201
|
+
...component,
|
|
202
|
+
id,
|
|
203
|
+
};
|
|
204
|
+
registerInteractableComponentUpdateTool(newComponent);
|
|
205
|
+
setInteractableComponents((prev) => {
|
|
206
|
+
return [...prev, newComponent];
|
|
207
|
+
});
|
|
208
|
+
return id;
|
|
209
|
+
}, [registerInteractableComponentUpdateTool]);
|
|
210
|
+
const removeInteractableComponent = (0, react_1.useCallback)((id) => {
|
|
211
|
+
setInteractableComponents((prev) => prev.filter((c) => c.id !== id));
|
|
212
|
+
}, []);
|
|
213
|
+
const getInteractableComponent = (0, react_1.useCallback)((id) => {
|
|
214
|
+
return interactableComponents.find((c) => c.id === id);
|
|
215
|
+
}, [interactableComponents]);
|
|
216
|
+
const getInteractableComponentsByName = (0, react_1.useCallback)((componentName) => {
|
|
217
|
+
return interactableComponents.filter((c) => c.name === componentName);
|
|
218
|
+
}, [interactableComponents]);
|
|
219
|
+
const clearAllInteractableComponents = (0, react_1.useCallback)(() => {
|
|
220
|
+
setInteractableComponents([]);
|
|
221
|
+
}, []);
|
|
222
|
+
const value = {
|
|
223
|
+
interactableComponents,
|
|
224
|
+
addInteractableComponent,
|
|
225
|
+
removeInteractableComponent,
|
|
226
|
+
updateInteractableComponentProps,
|
|
227
|
+
getInteractableComponent,
|
|
228
|
+
getInteractableComponentsByName,
|
|
229
|
+
clearAllInteractableComponents,
|
|
230
|
+
};
|
|
231
|
+
return (react_1.default.createElement(TamboInteractableContext.Provider, { value: value }, children));
|
|
232
|
+
};
|
|
233
|
+
exports.TamboInteractableProvider = TamboInteractableProvider;
|
|
234
|
+
/**
|
|
235
|
+
* The useTamboInteractable hook provides access to the interactable component
|
|
236
|
+
* management functions.
|
|
237
|
+
* @returns The interactable component management functions
|
|
238
|
+
*/
|
|
239
|
+
const useTamboInteractable = () => {
|
|
240
|
+
return (0, react_1.useContext)(TamboInteractableContext);
|
|
241
|
+
};
|
|
242
|
+
exports.useTamboInteractable = useTamboInteractable;
|
|
243
|
+
//# sourceMappingURL=tambo-interactable-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tambo-interactable-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-interactable-provider.tsx"],"names":[],"mappings":";AAAA,0DAA0D;AAC1D,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACb,+CAOe;AACf,6BAAwB;AAKxB,yEAA+D;AAE/D,MAAM,wBAAwB,G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gCAAgC;QAChC,wBAAwB;QACxB,+BAA+B;QAC/B,8BAA8B;KAC/B,CAAC;IAEF,OAAO,CACL,8BAAC,wBAAwB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC5C,QAAQ,CACyB,CACrC,CAAC;AACJ,CAAC,CAAC;AA5PW,QAAA,yBAAyB,6BA4PpC;AAEF;;;;GAIG;AACI,MAAM,oBAAoB,GAAG,GAAG,EAAE;IACvC,OAAO,IAAA,kBAAU,EAAC,wBAAwB,CAAC,CAAC;AAC9C,CAAC,CAAC;AAFW,QAAA,oBAAoB,wBAE/B","sourcesContent":["// react-sdk/src/providers/tambo-interactable-provider.tsx\n\"use client\";\nimport React, {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n useEffect,\n useState,\n} from \"react\";\nimport { z } from \"zod\";\nimport {\n TamboInteractableComponent,\n type TamboInteractableContext,\n} from \"../model/tambo-interactable\";\nimport { useTamboComponent } from \"./tambo-component-provider\";\n\nconst TamboInteractableContext = createContext<TamboInteractableContext>({\n interactableComponents: [],\n addInteractableComponent: () => \"\",\n removeInteractableComponent: () => {},\n updateInteractableComponentProps: () => {},\n getInteractableComponent: () => undefined,\n getInteractableComponentsByName: () => [],\n clearAllInteractableComponents: () => {},\n});\n\n/**\n * The TamboInteractableProvider manages a list of components that are currently\n * interactable, allowing tambo to interact with them by updating their props. It also registers tools\n * for Tambo to perform CRUD operations on the components list.\n * @param props - The props for the TamboInteractableProvider\n * @param props.children - The children to wrap\n * @returns The TamboInteractableProvider component\n */\nexport const TamboInteractableProvider: React.FC<PropsWithChildren> = ({\n children,\n}) => {\n const [interactableComponents, setInteractableComponents] = useState<\n TamboInteractableComponent[]\n >([]);\n const { registerTool } = useTamboComponent();\n\n useEffect(() => {\n registerTool({\n name: \"get_all_interactable_components\",\n description:\n \"Get all currently interactable components with their details including their current props. These are components that you can interact with on behalf of the user.\",\n tool: () => {\n return {\n components: interactableComponents,\n };\n },\n toolSchema: z.function().returns(\n z.object({\n components: z.array(\n z.object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n propsSchema: z.object({}).optional(),\n }),\n ),\n }),\n ),\n });\n\n registerTool({\n name: \"get_interactable_component_by_id\",\n description: \"Get a specific interactable component by its ID\",\n tool: (componentId: string) => {\n const component = interactableComponents.find(\n (c) => c.id === componentId,\n );\n\n if (!component) {\n return {\n success: false,\n error: `Component with ID ${componentId} not found`,\n };\n }\n\n return {\n success: true,\n component: {\n id: component.id,\n componentName: component.name,\n props: component.props,\n },\n };\n },\n toolSchema: z\n .function()\n .args(z.string())\n .returns(\n z.object({\n success: z.boolean(),\n component: z\n .object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n })\n .optional(),\n error: z.string().optional(),\n }),\n ),\n });\n\n registerTool({\n name: \"remove_interactable_component\",\n description: \"Remove an interactable component from the system\",\n tool: (componentId: string) => {\n const component = interactableComponents.find(\n (c) => c.id === componentId,\n );\n\n if (!component) {\n return {\n success: false,\n error: `Component with ID ${componentId} not found`,\n };\n }\n\n setInteractableComponents((prev) =>\n prev.filter((c) => c.id !== componentId),\n );\n\n return {\n success: true,\n componentId,\n removedComponent: {\n id: component.id,\n componentName: component.name,\n props: component.props,\n },\n };\n },\n toolSchema: z\n .function()\n .args(z.string())\n .returns(\n z.object({\n success: z.boolean(),\n componentId: z.string(),\n removedComponent: z.object({\n id: z.string(),\n componentName: z.string(),\n props: z.record(z.any()),\n }),\n error: z.string().optional(),\n }),\n ),\n });\n }, [interactableComponents, registerTool]);\n\n const updateInteractableComponentProps = useCallback(\n (id: string, newProps: Record<string, any>) => {\n let updateResult = \"Updated successfully\";\n\n setInteractableComponents((prev) => {\n const componentExists = prev.some((c) => c.id === id);\n\n if (!componentExists) {\n updateResult = `Error: Component with ID ${id} not found`;\n return prev;\n }\n\n const updatedComponents = prev.map((c) =>\n c.id === id ? { ...c, props: { ...c.props, ...newProps } } : c,\n );\n\n // Check if the update actually changed anything\n const originalComponent = prev.find((c) => c.id === id);\n const updatedComponent = updatedComponents.find((c) => c.id === id);\n\n if (!originalComponent || !updatedComponent) {\n updateResult = `Error: Failed to update component with ID ${id}`;\n return prev;\n }\n\n // Check if props actually changed\n const propsChanged =\n JSON.stringify(originalComponent.props) !==\n JSON.stringify(updatedComponent.props);\n\n if (!propsChanged) {\n updateResult = `Warning: No changes detected for component with ID ${id}. The update might not have worked.`;\n return prev;\n }\n\n return updatedComponents;\n });\n\n return updateResult;\n },\n [],\n );\n\n const registerInteractableComponentUpdateTool = useCallback(\n (component: TamboInteractableComponent) => {\n const schemaForArgs =\n typeof component.propsSchema === \"object\" &&\n \"describe\" in component.propsSchema\n ? component.propsSchema\n : z.object({});\n\n registerTool({\n name: `update_interactable_component_${component.id}`,\n description: `Update the props of interactable component ${component.id} (${component.name})`,\n tool: (componentId: string, newProps: any) => {\n return updateInteractableComponentProps(componentId, newProps);\n },\n toolSchema: z\n .function()\n .args(\n z\n .string()\n .describe(\"The ID of the interactable component to update\"),\n schemaForArgs.describe(\n \"The new props to update the component with\",\n ),\n )\n .returns(z.string()),\n });\n },\n [registerTool, updateInteractableComponentProps],\n );\n\n const addInteractableComponent = useCallback(\n (\n component: Omit<TamboInteractableComponent, \"id\" | \"createdAt\">,\n ): string => {\n const id = `${component.name}-${Math.random().toString(36).substr(2, 9)}`;\n const newComponent: TamboInteractableComponent = {\n ...component,\n id,\n };\n\n registerInteractableComponentUpdateTool(newComponent);\n\n setInteractableComponents((prev) => {\n return [...prev, newComponent];\n });\n\n return id;\n },\n [registerInteractableComponentUpdateTool],\n );\n\n const removeInteractableComponent = useCallback((id: string) => {\n setInteractableComponents((prev) => prev.filter((c) => c.id !== id));\n }, []);\n\n const getInteractableComponent = useCallback(\n (id: string) => {\n return interactableComponents.find((c) => c.id === id);\n },\n [interactableComponents],\n );\n\n const getInteractableComponentsByName = useCallback(\n (componentName: string) => {\n return interactableComponents.filter((c) => c.name === componentName);\n },\n [interactableComponents],\n );\n\n const clearAllInteractableComponents = useCallback(() => {\n setInteractableComponents([]);\n }, []);\n\n const value: TamboInteractableContext = {\n interactableComponents,\n addInteractableComponent,\n removeInteractableComponent,\n updateInteractableComponentProps,\n getInteractableComponent,\n getInteractableComponentsByName,\n clearAllInteractableComponents,\n };\n\n return (\n <TamboInteractableContext.Provider value={value}>\n {children}\n </TamboInteractableContext.Provider>\n );\n};\n\n/**\n * The useTamboInteractable hook provides access to the interactable component\n * management functions.\n * @returns The interactable component management functions\n */\nexport const useTamboInteractable = () => {\n return useContext(TamboInteractableContext);\n};\n"]}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
import { StreamStatus } from "../hooks/use-tambo-stream-status";
|
|
3
|
+
interface TamboPropStreamContextValue {
|
|
4
|
+
/** The stream data */
|
|
5
|
+
data: any;
|
|
6
|
+
/** The stream status */
|
|
7
|
+
streamStatus: StreamStatus;
|
|
8
|
+
/** Get the status for a specific key */
|
|
9
|
+
getStatusForKey: (key: string) => {
|
|
10
|
+
isPending: boolean;
|
|
11
|
+
isStreaming: boolean;
|
|
12
|
+
isSuccess: boolean;
|
|
13
|
+
isError: boolean;
|
|
14
|
+
error?: Error;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface TamboPropStreamProviderProps {
|
|
18
|
+
/** The stream data */
|
|
19
|
+
data: any;
|
|
20
|
+
/** Optional stream status for more granular control */
|
|
21
|
+
streamStatus?: StreamStatus;
|
|
22
|
+
}
|
|
23
|
+
export interface LoadingProps {
|
|
24
|
+
/** The key to identify this loading state */
|
|
25
|
+
streamKey?: string;
|
|
26
|
+
/** The children to render when loading */
|
|
27
|
+
children: React.ReactNode;
|
|
28
|
+
/** Optional className for styling */
|
|
29
|
+
className?: string;
|
|
30
|
+
}
|
|
31
|
+
export interface EmptyProps {
|
|
32
|
+
/** The key to identify this empty state */
|
|
33
|
+
streamKey?: string;
|
|
34
|
+
/** The children to render when empty */
|
|
35
|
+
children: React.ReactNode;
|
|
36
|
+
/** Optional className for styling */
|
|
37
|
+
className?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface CompleteProps {
|
|
40
|
+
/** The key to identify this complete state */
|
|
41
|
+
streamKey?: string;
|
|
42
|
+
/** The children to render when complete */
|
|
43
|
+
children: React.ReactNode;
|
|
44
|
+
/** Optional className for styling */
|
|
45
|
+
className?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Loading component that renders children when the stream is in a loading state
|
|
49
|
+
* @param props - The props for the Loading component
|
|
50
|
+
* @param props.key - The key to identify this loading state
|
|
51
|
+
* @param props.children - The children to render when loading
|
|
52
|
+
* @param props.className - Optional className for styling
|
|
53
|
+
* @returns The Loading component
|
|
54
|
+
*/
|
|
55
|
+
declare const Loading: React.FC<LoadingProps>;
|
|
56
|
+
/**
|
|
57
|
+
* Empty component that renders children when the stream has no data
|
|
58
|
+
* @param props - The props for the Empty component
|
|
59
|
+
* @param props.key - The key to identify this empty state
|
|
60
|
+
* @param props.children - The children to render when empty
|
|
61
|
+
* @param props.className - Optional className for styling
|
|
62
|
+
* @returns The Empty component
|
|
63
|
+
*/
|
|
64
|
+
declare const Empty: React.FC<EmptyProps>;
|
|
65
|
+
/**
|
|
66
|
+
* Complete component that renders children when the stream has data
|
|
67
|
+
* @param props - The props for the Complete component
|
|
68
|
+
* @param props.key - The key to identify this complete state
|
|
69
|
+
* @param props.children - The children to render when complete
|
|
70
|
+
* @param props.className - Optional className for styling
|
|
71
|
+
* @returns The Complete component
|
|
72
|
+
*/
|
|
73
|
+
declare const Complete: React.FC<CompleteProps>;
|
|
74
|
+
/**
|
|
75
|
+
* Hook to use the TamboStream context
|
|
76
|
+
* @returns The TamboStream context
|
|
77
|
+
*/
|
|
78
|
+
export declare const useTamboStream: () => TamboPropStreamContextValue;
|
|
79
|
+
/**
|
|
80
|
+
* The TamboStreamProvider provides a context for managing stream states
|
|
81
|
+
* with compound components for Loading, Empty, and Complete states.
|
|
82
|
+
* @param props - The props for the TamboStreamProvider
|
|
83
|
+
* @param props.children - The children to wrap
|
|
84
|
+
* @param props.data - The stream data
|
|
85
|
+
* @param props.streamStatus - Optional stream status for more granular control
|
|
86
|
+
* @returns The TamboStreamProvider component
|
|
87
|
+
*/
|
|
88
|
+
declare const TamboPropStreamProviderComponent: React.FC<PropsWithChildren<TamboPropStreamProviderProps>>;
|
|
89
|
+
type TamboPropStreamProviderCompound = typeof TamboPropStreamProviderComponent & {
|
|
90
|
+
Loading: typeof Loading;
|
|
91
|
+
Empty: typeof Empty;
|
|
92
|
+
Complete: typeof Complete;
|
|
93
|
+
};
|
|
94
|
+
export declare const TamboPropStreamProvider: TamboPropStreamProviderCompound;
|
|
95
|
+
export {};
|
|
96
|
+
//# sourceMappingURL=tambo-prop-stream-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tambo-prop-stream-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-prop-stream-provider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAEZ,iBAAiB,EAGlB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAEhE,UAAU,2BAA2B;IACnC,sBAAsB;IACtB,IAAI,EAAE,GAAG,CAAC;IACV,wBAAwB;IACxB,YAAY,EAAE,YAAY,CAAC;IAC3B,wCAAwC;IACxC,eAAe,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK;QAChC,SAAS,EAAE,OAAO,CAAC;QACnB,WAAW,EAAE,OAAO,CAAC;QACrB,SAAS,EAAE,OAAO,CAAC;QACnB,OAAO,EAAE,OAAO,CAAC;QACjB,KAAK,CAAC,EAAE,KAAK,CAAC;KACf,CAAC;CACH;AAKD,MAAM,WAAW,4BAA4B;IAC3C,sBAAsB;IACtB,IAAI,EAAE,GAAG,CAAC;IACV,uDAAuD;IACvD,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAED,MAAM,WAAW,YAAY;IAC3B,6CAA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@@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.TamboPropStreamProvider = exports.useTamboStream = void 0;
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const TamboPropStreamContext = (0, react_1.createContext)(null);
|
|
40
|
+
/**
|
|
41
|
+
* Loading component that renders children when the stream is in a loading state
|
|
42
|
+
* @param props - The props for the Loading component
|
|
43
|
+
* @param props.key - The key to identify this loading state
|
|
44
|
+
* @param props.children - The children to render when loading
|
|
45
|
+
* @param props.className - Optional className for styling
|
|
46
|
+
* @returns The Loading component
|
|
47
|
+
*/
|
|
48
|
+
const Loading = ({ streamKey = "default", children, className, }) => {
|
|
49
|
+
const { getStatusForKey } = (0, exports.useTamboStream)();
|
|
50
|
+
const status = getStatusForKey(streamKey);
|
|
51
|
+
if (!status.isPending && !status.isStreaming) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return (react_1.default.createElement("div", { className: className, "data-stream-key": streamKey, "data-stream-state": "loading" }, children));
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Empty component that renders children when the stream has no data
|
|
58
|
+
* @param props - The props for the Empty component
|
|
59
|
+
* @param props.key - The key to identify this empty state
|
|
60
|
+
* @param props.children - The children to render when empty
|
|
61
|
+
* @param props.className - Optional className for styling
|
|
62
|
+
* @returns The Empty component
|
|
63
|
+
*/
|
|
64
|
+
const Empty = ({ streamKey = "default", children, className, }) => {
|
|
65
|
+
const { data, getStatusForKey } = (0, exports.useTamboStream)();
|
|
66
|
+
const status = getStatusForKey(streamKey);
|
|
67
|
+
// Get the specific data for this key
|
|
68
|
+
const keyData = data && typeof data === "object" && !Array.isArray(data)
|
|
69
|
+
? data[streamKey]
|
|
70
|
+
: data;
|
|
71
|
+
// Show empty state when not loading, not streaming, not successful, and no data for this key
|
|
72
|
+
const shouldShowEmpty = !status.isPending &&
|
|
73
|
+
!status.isStreaming &&
|
|
74
|
+
!status.isSuccess &&
|
|
75
|
+
!status.isError &&
|
|
76
|
+
(keyData === undefined || keyData === null || keyData === "");
|
|
77
|
+
if (!shouldShowEmpty) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return (react_1.default.createElement("div", { className: className, "data-stream-key": streamKey, "data-stream-state": "empty" }, children));
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Complete component that renders children when the stream has data
|
|
84
|
+
* @param props - The props for the Complete component
|
|
85
|
+
* @param props.key - The key to identify this complete state
|
|
86
|
+
* @param props.children - The children to render when complete
|
|
87
|
+
* @param props.className - Optional className for styling
|
|
88
|
+
* @returns The Complete component
|
|
89
|
+
*/
|
|
90
|
+
const Complete = ({ streamKey = "default", children, className, }) => {
|
|
91
|
+
const { data, getStatusForKey } = (0, exports.useTamboStream)();
|
|
92
|
+
const status = getStatusForKey(streamKey);
|
|
93
|
+
// Get the specific data for this key
|
|
94
|
+
const keyData = data && typeof data === "object" && !Array.isArray(data)
|
|
95
|
+
? data[streamKey]
|
|
96
|
+
: data;
|
|
97
|
+
// Show complete when we have data for this key and the stream is successful
|
|
98
|
+
const shouldShowComplete = status.isSuccess && keyData !== undefined && keyData !== null;
|
|
99
|
+
if (!shouldShowComplete) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return (react_1.default.createElement("div", { className: className, "data-stream-key": streamKey, "data-stream-state": "complete" }, children));
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Hook to use the TamboStream context
|
|
106
|
+
* @returns The TamboStream context
|
|
107
|
+
*/
|
|
108
|
+
const useTamboStream = () => {
|
|
109
|
+
const context = (0, react_1.useContext)(TamboPropStreamContext);
|
|
110
|
+
if (!context) {
|
|
111
|
+
throw new Error("useTamboStream must be used within a TamboPropStreamProvider");
|
|
112
|
+
}
|
|
113
|
+
return context;
|
|
114
|
+
};
|
|
115
|
+
exports.useTamboStream = useTamboStream;
|
|
116
|
+
/**
|
|
117
|
+
* The TamboStreamProvider provides a context for managing stream states
|
|
118
|
+
* with compound components for Loading, Empty, and Complete states.
|
|
119
|
+
* @param props - The props for the TamboStreamProvider
|
|
120
|
+
* @param props.children - The children to wrap
|
|
121
|
+
* @param props.data - The stream data
|
|
122
|
+
* @param props.streamStatus - Optional stream status for more granular control
|
|
123
|
+
* @returns The TamboStreamProvider component
|
|
124
|
+
*/
|
|
125
|
+
const TamboPropStreamProviderComponent = ({ children, data, streamStatus }) => {
|
|
126
|
+
// Create a default stream status if none provided
|
|
127
|
+
const defaultStreamStatus = (0, react_1.useMemo)(() => ({
|
|
128
|
+
isPending: false, // No external stream, so not pending
|
|
129
|
+
isStreaming: false, // No external stream, so not streaming
|
|
130
|
+
isSuccess: true, // If no stream status provided, assume success
|
|
131
|
+
isError: false,
|
|
132
|
+
streamError: undefined,
|
|
133
|
+
}), []);
|
|
134
|
+
const finalStreamStatus = streamStatus ?? defaultStreamStatus;
|
|
135
|
+
// Track status by key for compound components
|
|
136
|
+
const keyStatusMap = (0, react_1.useMemo)(() => {
|
|
137
|
+
const map = new Map();
|
|
138
|
+
// Track per-key status based on data structure
|
|
139
|
+
// If data is an object with keys, create status for each key
|
|
140
|
+
if (data && typeof data === "object" && !Array.isArray(data)) {
|
|
141
|
+
Object.keys(data).forEach((key) => {
|
|
142
|
+
const keyData = data[key];
|
|
143
|
+
const hasData = keyData !== undefined && keyData !== null && keyData !== "";
|
|
144
|
+
map.set(key, {
|
|
145
|
+
// If no external stream, show loading when key has no data
|
|
146
|
+
isPending: finalStreamStatus.isPending ||
|
|
147
|
+
(!finalStreamStatus.isStreaming && !hasData),
|
|
148
|
+
isStreaming: finalStreamStatus.isStreaming && !hasData,
|
|
149
|
+
isSuccess: finalStreamStatus.isSuccess && hasData,
|
|
150
|
+
isError: finalStreamStatus.isError,
|
|
151
|
+
error: finalStreamStatus.streamError,
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// Always set default status for fallback
|
|
156
|
+
map.set("default", {
|
|
157
|
+
isPending: finalStreamStatus.isPending,
|
|
158
|
+
isStreaming: finalStreamStatus.isStreaming,
|
|
159
|
+
isSuccess: finalStreamStatus.isSuccess,
|
|
160
|
+
isError: finalStreamStatus.isError,
|
|
161
|
+
error: finalStreamStatus.streamError,
|
|
162
|
+
});
|
|
163
|
+
return map;
|
|
164
|
+
}, [finalStreamStatus, data]);
|
|
165
|
+
const getStatusForKey = (0, react_1.useMemo)(() => (key) => {
|
|
166
|
+
return (keyStatusMap.get(key) ??
|
|
167
|
+
keyStatusMap.get("default") ?? {
|
|
168
|
+
isPending: false,
|
|
169
|
+
isStreaming: false,
|
|
170
|
+
isSuccess: false,
|
|
171
|
+
isError: false,
|
|
172
|
+
});
|
|
173
|
+
}, [keyStatusMap]);
|
|
174
|
+
const contextValue = (0, react_1.useMemo)(() => ({
|
|
175
|
+
data,
|
|
176
|
+
streamStatus: finalStreamStatus,
|
|
177
|
+
getStatusForKey,
|
|
178
|
+
}), [data, finalStreamStatus, getStatusForKey]);
|
|
179
|
+
return (react_1.default.createElement(TamboPropStreamContext.Provider, { value: contextValue }, children));
|
|
180
|
+
};
|
|
181
|
+
exports.TamboPropStreamProvider = TamboPropStreamProviderComponent;
|
|
182
|
+
exports.TamboPropStreamProvider.Loading = Loading;
|
|
183
|
+
exports.TamboPropStreamProvider.Empty = Empty;
|
|
184
|
+
exports.TamboPropStreamProvider.Complete = Complete;
|
|
185
|
+
//# sourceMappingURL=tambo-prop-stream-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tambo-prop-stream-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-prop-stream-provider.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEb,+CAKe;AAkBf,MAAM,sBAAsB,GAC1B,IAAA,qBAAa,EAAqC,IAAI,CAAC,CAAC;AAoC1D;;;;;;;GAOG;AACH,MAAM,OAAO,GAA2B,CAAC,EACvC,SAAS,GAAG,SAAS,EACrB,QAAQ,EACR,SAAS,GACV,EAAE,EAAE;IACH,MAAM,EAAE,eAAe,EAAE,GAAG,IAAA,sBAAc,GAAE,CAAC;IAC7C,MAAM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AAC,OAAO;YAClC,KAAK,EAAE,iBAAiB,CAAC,WAAW;SACrC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAC,CAAC;IAE9B,MAAM,eAAe,GAAG,IAAA,eAAO,EAC7B,GAAG,EAAE,CAAC,CAAC,GAAW,EAAE,EAAE;QACpB,OAAO,CACL,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC;YACrB,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI;YAC7B,SAAS,EAAE,KAAK;YAChB,WAAW,EAAE,KAAK;YAClB,SAAS,EAAE,KAAK;YAChB,OAAO,EAAE,KAAK;SACf,CACF,CAAC;IACJ,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,MAAM,YAAY,GAAG,IAAA,eAAO,EAC1B,GAAG,EAAE,CAAC,CAAC;QACL,IAAI;QACJ,YAAY,EAAE,iBAAiB;QAC/B,eAAe;KAChB,CAAC,EACF,CAAC,IAAI,EAAE,iBAAiB,EAAE,eAAe,CAAC,CAC3C,CAAC;IAEF,OAAO,CACL,8BAAC,sBAAsB,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,IACjD,QAAQ,CACuB,CACnC,CAAC;AACJ,CAAC,CAAC;AAUW,QAAA,uBAAuB,GAClC,gCAAmE,CAAC;AAEtE,+BAAuB,CAAC,OAAO,GAAG,OAAO,CAAC;AAC1C,+BAAuB,CAAC,KAAK,GAAG,KAAK,CAAC;AACtC,+BAAuB,CAAC,QAAQ,GAAG,QAAQ,CAAC","sourcesContent":["\"use client\";\n\nimport React, {\n createContext,\n PropsWithChildren,\n useContext,\n useMemo,\n} from \"react\";\nimport { StreamStatus } from \"../hooks/use-tambo-stream-status\";\n\ninterface TamboPropStreamContextValue {\n /** The stream data */\n data: any;\n /** The stream status */\n streamStatus: StreamStatus;\n /** Get the status for a specific key */\n getStatusForKey: (key: string) => {\n isPending: boolean;\n isStreaming: boolean;\n isSuccess: boolean;\n isError: boolean;\n error?: Error;\n };\n}\n\nconst TamboPropStreamContext =\n createContext<TamboPropStreamContextValue | null>(null);\n\nexport interface TamboPropStreamProviderProps {\n /** The stream data */\n data: any;\n /** Optional stream status for more granular control */\n streamStatus?: StreamStatus;\n}\n\nexport interface LoadingProps {\n /** The key to identify this loading state */\n streamKey?: string;\n /** The children to render when loading */\n children: React.ReactNode;\n /** Optional className for styling */\n className?: string;\n}\n\nexport interface EmptyProps {\n /** The key to identify this empty state */\n streamKey?: string;\n /** The children to render when empty */\n children: React.ReactNode;\n /** Optional className for styling */\n className?: string;\n}\n\nexport interface CompleteProps {\n /** The key to identify this complete state */\n streamKey?: string;\n /** The children to render when complete */\n children: React.ReactNode;\n /** Optional className for styling */\n className?: string;\n}\n\n/**\n * Loading component that renders children when the stream is in a loading state\n * @param props - The props for the Loading component\n * @param props.key - The key to identify this loading state\n * @param props.children - The children to render when loading\n * @param props.className - Optional className for styling\n * @returns The Loading component\n */\nconst Loading: React.FC<LoadingProps> = ({\n streamKey = \"default\",\n children,\n className,\n}) => {\n const { getStatusForKey } = useTamboStream();\n const status = getStatusForKey(streamKey);\n\n if (!status.isPending && !status.isStreaming) {\n return null;\n }\n\n return (\n <div\n className={className}\n data-stream-key={streamKey}\n data-stream-state=\"loading\"\n >\n {children}\n </div>\n );\n};\n\n/**\n * Empty component that renders children when the stream has no data\n * @param props - The props for the Empty component\n * @param props.key - The key to identify this empty state\n * @param props.children - The children to render when empty\n * @param props.className - Optional className for styling\n * @returns The Empty component\n */\nconst Empty: React.FC<EmptyProps> = ({\n streamKey = \"default\",\n children,\n className,\n}) => {\n const { data, getStatusForKey } = useTamboStream();\n const status = getStatusForKey(streamKey);\n\n // Get the specific data for this key\n const keyData =\n data && typeof data === \"object\" && !Array.isArray(data)\n ? data[streamKey]\n : data;\n\n // Show empty state when not loading, not streaming, not successful, and no data for this key\n const shouldShowEmpty =\n !status.isPending &&\n !status.isStreaming &&\n !status.isSuccess &&\n !status.isError &&\n (keyData === undefined || keyData === null || keyData === \"\");\n\n if (!shouldShowEmpty) {\n return null;\n }\n\n return (\n <div\n className={className}\n data-stream-key={streamKey}\n data-stream-state=\"empty\"\n >\n {children}\n </div>\n );\n};\n\n/**\n * Complete component that renders children when the stream has data\n * @param props - The props for the Complete component\n * @param props.key - The key to identify this complete state\n * @param props.children - The children to render when complete\n * @param props.className - Optional className for styling\n * @returns The Complete component\n */\nconst Complete: React.FC<CompleteProps> = ({\n streamKey = \"default\",\n children,\n className,\n}) => {\n const { data, getStatusForKey } = useTamboStream();\n const status = getStatusForKey(streamKey);\n\n // Get the specific data for this key\n const keyData =\n data && typeof data === \"object\" && !Array.isArray(data)\n ? data[streamKey]\n : data;\n\n // Show complete when we have data for this key and the stream is successful\n const shouldShowComplete =\n status.isSuccess && keyData !== undefined && keyData !== null;\n\n if (!shouldShowComplete) {\n return null;\n }\n\n return (\n <div\n className={className}\n data-stream-key={streamKey}\n data-stream-state=\"complete\"\n >\n {children}\n </div>\n );\n};\n\n/**\n * Hook to use the TamboStream context\n * @returns The TamboStream context\n */\nexport const useTamboStream = () => {\n const context = useContext(TamboPropStreamContext);\n if (!context) {\n throw new Error(\n \"useTamboStream must be used within a TamboPropStreamProvider\",\n );\n }\n return context;\n};\n\n/**\n * The TamboStreamProvider provides a context for managing stream states\n * with compound components for Loading, Empty, and Complete states.\n * @param props - The props for the TamboStreamProvider\n * @param props.children - The children to wrap\n * @param props.data - The stream data\n * @param props.streamStatus - Optional stream status for more granular control\n * @returns The TamboStreamProvider component\n */\nconst TamboPropStreamProviderComponent: React.FC<\n PropsWithChildren<TamboPropStreamProviderProps>\n> = ({ children, data, streamStatus }) => {\n // Create a default stream status if none provided\n const defaultStreamStatus: StreamStatus = useMemo(\n () => ({\n isPending: false, // No external stream, so not pending\n isStreaming: false, // No external stream, so not streaming\n isSuccess: true, // If no stream status provided, assume success\n isError: false,\n streamError: undefined,\n }),\n [],\n );\n\n const finalStreamStatus = streamStatus ?? defaultStreamStatus;\n\n // Track status by key for compound components\n const keyStatusMap = useMemo(() => {\n const map = new Map<\n string,\n {\n isPending: boolean;\n isStreaming: boolean;\n isSuccess: boolean;\n isError: boolean;\n error?: Error;\n }\n >();\n\n // Track per-key status based on data structure\n // If data is an object with keys, create status for each key\n if (data && typeof data === \"object\" && !Array.isArray(data)) {\n Object.keys(data).forEach((key) => {\n const keyData = data[key];\n const hasData =\n keyData !== undefined && keyData !== null && keyData !== \"\";\n\n map.set(key, {\n // If no external stream, show loading when key has no data\n isPending:\n finalStreamStatus.isPending ||\n (!finalStreamStatus.isStreaming && !hasData),\n isStreaming: finalStreamStatus.isStreaming && !hasData,\n isSuccess: finalStreamStatus.isSuccess && hasData,\n isError: finalStreamStatus.isError,\n error: finalStreamStatus.streamError,\n });\n });\n }\n\n // Always set default status for fallback\n map.set(\"default\", {\n isPending: finalStreamStatus.isPending,\n isStreaming: finalStreamStatus.isStreaming,\n isSuccess: finalStreamStatus.isSuccess,\n isError: finalStreamStatus.isError,\n error: finalStreamStatus.streamError,\n });\n\n return map;\n }, [finalStreamStatus, data]);\n\n const getStatusForKey = useMemo(\n () => (key: string) => {\n return (\n keyStatusMap.get(key) ??\n keyStatusMap.get(\"default\") ?? {\n isPending: false,\n isStreaming: false,\n isSuccess: false,\n isError: false,\n }\n );\n },\n [keyStatusMap],\n );\n\n const contextValue = useMemo(\n () => ({\n data,\n streamStatus: finalStreamStatus,\n getStatusForKey,\n }),\n [data, finalStreamStatus, getStatusForKey],\n );\n\n return (\n <TamboPropStreamContext.Provider value={contextValue}>\n {children}\n </TamboPropStreamContext.Provider>\n );\n};\n\n// Create the compound component type\ntype TamboPropStreamProviderCompound =\n typeof TamboPropStreamProviderComponent & {\n Loading: typeof Loading;\n Empty: typeof Empty;\n Complete: typeof Complete;\n };\n\nexport const TamboPropStreamProvider =\n TamboPropStreamProviderComponent as TamboPropStreamProviderCompound;\n\nTamboPropStreamProvider.Loading = Loading;\nTamboPropStreamProvider.Empty = Empty;\nTamboPropStreamProvider.Complete = Complete;\n"]}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from "react";
|
|
2
|
+
import { TamboInteractableContext } from "../model/tambo-interactable";
|
|
2
3
|
import { TamboClientContextProps, TamboClientProviderProps } from "./tambo-client-provider";
|
|
3
4
|
import { TamboComponentContextProps } from "./tambo-component-provider";
|
|
4
5
|
import { TamboRegistryProviderProps } from "./tambo-registry-provider";
|
|
5
6
|
import { TamboThreadContextProps, TamboThreadProviderProps } from "./tambo-thread-provider";
|
|
6
7
|
/**
|
|
7
8
|
* The TamboProvider gives full access to the whole Tambo API. This includes the
|
|
8
|
-
* TamboAI client, the component registry,
|
|
9
|
+
* TamboAI client, the component registry, the current thread context, and interactable components.
|
|
9
10
|
* @param props - The props for the TamboProvider
|
|
10
11
|
* @param props.children - The children to wrap
|
|
11
12
|
* @param props.tamboUrl - The URL of the Tambo API
|
|
@@ -17,11 +18,11 @@ import { TamboThreadContextProps, TamboThreadProviderProps } from "./tambo-threa
|
|
|
17
18
|
* @returns The TamboProvider component
|
|
18
19
|
*/
|
|
19
20
|
export declare const TamboProvider: React.FC<PropsWithChildren<TamboClientProviderProps & TamboRegistryProviderProps & TamboThreadProviderProps>>;
|
|
20
|
-
export type TamboContextProps = TamboClientContextProps & TamboThreadContextProps & TamboComponentContextProps;
|
|
21
|
+
export type TamboContextProps = TamboClientContextProps & TamboThreadContextProps & TamboComponentContextProps & TamboInteractableContext;
|
|
21
22
|
export declare const TamboContext: React.Context<TamboContextProps>;
|
|
22
23
|
/**
|
|
23
24
|
* TamboCompositeProvider is a provider that combines the TamboClient,
|
|
24
|
-
* TamboThread, and
|
|
25
|
+
* TamboThread, TamboComponent, and TamboInteractable providers
|
|
25
26
|
* @param props - The props for the TamboCompositeProvider
|
|
26
27
|
* @param props.children - The children to wrap
|
|
27
28
|
* @returns The wrapped component
|
|
@@ -31,7 +32,8 @@ export declare const TamboCompositeProvider: React.FC<PropsWithChildren>;
|
|
|
31
32
|
* The useTambo hook provides access to the Tambo API. This is the primary entrypoint
|
|
32
33
|
* for the Tambo React SDK.
|
|
33
34
|
*
|
|
34
|
-
* This includes the TamboAI client, the component registry,
|
|
35
|
+
* This includes the TamboAI client, the component registry, the current thread context,
|
|
36
|
+
* and interactable component management.
|
|
35
37
|
* @returns The Tambo API
|
|
36
38
|
*/
|
|
37
39
|
export declare const useTambo: () => TamboContextProps;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-provider.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,iBAAiB,EAA6B,MAAM,OAAO,CAAC;AAC5E,OAAO,EACL,uBAAuB,EAEvB,wBAAwB,EAGzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,0BAA0B,EAG3B,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"tambo-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-provider.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,iBAAiB,EAA6B,MAAM,OAAO,CAAC;AAC5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AACvE,OAAO,EACL,uBAAuB,EAEvB,wBAAwB,EAGzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,0BAA0B,EAG3B,MAAM,4BAA4B,CAAC;AAKpC,OAAO,EAEL,0BAA0B,EAC3B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,uBAAuB,EAEvB,wBAAwB,EAEzB,MAAM,yBAAyB,CAAC;AAEjC;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAClC,iBAAiB,CACf,wBAAwB,GACtB,0BAA0B,GAC1B,wBAAwB,CAC3B,CAkCF,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,uBAAuB,GACrD,uBAAuB,GACvB,0BAA0B,GAC1B,wBAAwB,CAAC;AAE3B,eAAO,MAAM,YAAY,kCAExB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CAsB9D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,yBAEpB,CAAC"}
|