@tambo-ai/react 0.37.3 → 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/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/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/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/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-tambo-interactable.js","sourceRoot":"","sources":["../../../src/providers/hoc/with-tambo-interactable.tsx"],"names":[],"mappings":";AAAA,gDAAgD;AAChD,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCb,sDAiFC;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AAiB,CAAC,EAAE,CAAC,CAAC;gBACtB,mBAAmB,EAAE,CAAC,EAAE,CAAC,CAAC;gBAC1B,aAAa,CAAC,OAAO,GAAG,IAAI,CAAC;YAC/B,CAAC;QACH,CAAC,EAAE,CAAC,wBAAwB,EAAE,cAAc,EAAE,mBAAmB,CAAC,CAAC,CAAC;QAEpE,8DAA8D;QAC9D,IAAA,iBAAS,EAAC,GAAG,EAAE;YACb,iBAAiB,EAAE,CAAC;QACtB,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC,CAAC;QAExB,kEAAkE;QAClE,IAAA,iBAAS,EAAC,GAAG,EAAE;YACb,IAAI,cAAc,IAAI,aAAa,CAAC,OAAO,EAAE,CAAC;gBAC5C,gEAAgE;gBAChE,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;gBAChE,MAAM,kBAAkB,GAAG,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC;gBAE1D,IAAI,eAAe,KAAK,kBAAkB,EAAE,CAAC;oBAC3C,gCAAgC,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;oBACjE,aAAa,EAAE,CAAC,cAAc,CAAC,CAAC;oBAChC,eAAe,CAAC,OAAO,GAAG,cAAc,CAAC;gBAC3C,CAAC;YACH,CAAC;QACH,CAAC,EAAE;YACD,cAAc;YACd,cAAc;YACd,gCAAgC;YAChC,aAAa;SACd,CAAC,CAAC;QAEH,OAAO,8BAAC,gBAAgB,OAAM,cAAoB,GAAI,CAAC;IACzD,CAAC,CAAC;IAEF,wBAAwB,CAAC,WAAW,GAAG,yBAAyB,WAAW,GAAG,CAAC;IAE/E,OAAO,wBAAwB,CAAC;AAClC,CAAC","sourcesContent":["// react-sdk/src/providers/with-interactable.tsx\n\"use client\";\nimport React, { useCallback, useEffect, useRef, useState } from \"react\";\nimport { z } from \"zod\";\nimport { useTamboInteractable } from \"../tambo-interactable-provider\";\n\nexport interface InteractableConfig {\n componentName: string;\n description: string;\n propsSchema?: z.ZodTypeAny;\n}\n\nexport interface WithTamboInteractableProps {\n interactableId?: string;\n onInteractableReady?: (id: string) => void;\n onPropsUpdate?: (newProps: Record<string, any>) => void;\n}\n\n/**\n * Higher-Order Component that makes any component interactable by tambo.\n * @param WrappedComponent - The component to make interactable\n * @param config - Configuration for the interactable component\n * @returns A new component that is automatically registered as interactable\n * @example\n * ```tsx\n * const MyInteractableNote = withTamboInteractable(MyNote, {\n * componentName: \"MyNote\",\n * description: \"A note component\",\n * propsSchema: z.object({\n * title: z.string(),\n * content: z.string(),\n * }),\n * });\n *\n * // Usage\n * <MyInteractableNote title=\"My Note\" content=\"This is my note\" />\n * ```\n */\nexport function withTamboInteractable<P extends object>(\n WrappedComponent: React.ComponentType<P>,\n config: InteractableConfig,\n) {\n const displayName =\n WrappedComponent.displayName ?? WrappedComponent.name ?? \"Component\";\n\n const TamboInteractableWrapper: React.FC<P & WithTamboInteractableProps> = (\n props,\n ) => {\n const {\n addInteractableComponent,\n updateInteractableComponentProps,\n getInteractableComponent,\n } = useTamboInteractable();\n\n const [interactableId, setInteractableId] = useState<string | null>(null);\n const isInitialized = useRef(false);\n const lastParentProps = useRef<Record<string, any>>({});\n\n // Extract interactable-specific props\n const { onInteractableReady, onPropsUpdate, ...componentProps } =\n props as P & WithTamboInteractableProps;\n\n // Get the current interactable component to track prop updates\n const currentInteractable = interactableId\n ? getInteractableComponent(interactableId)\n : null;\n\n // Use the props from the interactable component if available, otherwise use the passed props\n // We need to be careful not to create a loop, so we only use stored props if they're different from passed props\n const effectiveProps = currentInteractable?.props ?? componentProps;\n\n // Memoize the registration function\n const registerComponent = useCallback(() => {\n if (!isInitialized.current) {\n const id = addInteractableComponent({\n name: config.componentName,\n description: config.description,\n component: WrappedComponent,\n props: componentProps,\n propsSchema: config.propsSchema,\n });\n\n setInteractableId(id);\n onInteractableReady?.(id);\n isInitialized.current = true;\n }\n }, [addInteractableComponent, componentProps, onInteractableReady]);\n\n // Register the component as interactable on mount (only once)\n useEffect(() => {\n registerComponent();\n }, [registerComponent]);\n\n // Update the interactable component when props change from parent\n useEffect(() => {\n if (interactableId && isInitialized.current) {\n // Only update if the props are different from what we last sent\n const lastPropsString = JSON.stringify(lastParentProps.current);\n const currentPropsString = JSON.stringify(componentProps);\n\n if (lastPropsString !== currentPropsString) {\n updateInteractableComponentProps(interactableId, componentProps);\n onPropsUpdate?.(componentProps);\n lastParentProps.current = componentProps;\n }\n }\n }, [\n interactableId,\n componentProps,\n updateInteractableComponentProps,\n onPropsUpdate,\n ]);\n\n return <WrappedComponent {...(effectiveProps as P)} />;\n };\n\n TamboInteractableWrapper.displayName = `withTamboInteractable(${displayName})`;\n\n return TamboInteractableWrapper;\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type { TamboComponent, TamboTool } from "../model/component-metadata";
|
|
2
2
|
export { TamboClientProvider, useTamboClient } from "./tambo-client-provider";
|
|
3
3
|
export { TamboComponentProvider, useTamboComponent, } from "./tambo-component-provider";
|
|
4
|
+
export { TamboPropStreamProvider } from "./tambo-prop-stream-provider";
|
|
4
5
|
export { TamboContext, TamboProvider, useTambo } from "./tambo-provider";
|
|
5
6
|
export { TamboRegistryProvider, useTamboRegistry, type TamboRegistryContext, } from "./tambo-registry-provider";
|
|
6
7
|
export { TamboStubProvider, type TamboStubProviderProps } from "./tambo-stubs";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9E,OAAO,EACL,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,oBAAoB,GAC1B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,KAAK,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,cAAc,GACf,MAAM,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9E,OAAO,EACL,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,oBAAoB,GAC1B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,KAAK,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,cAAc,GACf,MAAM,yBAAyB,CAAC"}
|
package/dist/providers/index.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useTamboThread = exports.TamboThreadProvider = exports.TamboThreadContext = exports.TamboStubProvider = exports.useTamboRegistry = exports.TamboRegistryProvider = exports.useTambo = exports.TamboProvider = exports.TamboContext = exports.useTamboComponent = exports.TamboComponentProvider = exports.useTamboClient = exports.TamboClientProvider = void 0;
|
|
3
|
+
exports.useTamboThread = exports.TamboThreadProvider = exports.TamboThreadContext = exports.TamboStubProvider = exports.useTamboRegistry = exports.TamboRegistryProvider = exports.useTambo = exports.TamboProvider = exports.TamboContext = exports.TamboPropStreamProvider = exports.useTamboComponent = exports.TamboComponentProvider = exports.useTamboClient = exports.TamboClientProvider = void 0;
|
|
4
4
|
var tambo_client_provider_1 = require("./tambo-client-provider");
|
|
5
5
|
Object.defineProperty(exports, "TamboClientProvider", { enumerable: true, get: function () { return tambo_client_provider_1.TamboClientProvider; } });
|
|
6
6
|
Object.defineProperty(exports, "useTamboClient", { enumerable: true, get: function () { return tambo_client_provider_1.useTamboClient; } });
|
|
7
7
|
var tambo_component_provider_1 = require("./tambo-component-provider");
|
|
8
8
|
Object.defineProperty(exports, "TamboComponentProvider", { enumerable: true, get: function () { return tambo_component_provider_1.TamboComponentProvider; } });
|
|
9
9
|
Object.defineProperty(exports, "useTamboComponent", { enumerable: true, get: function () { return tambo_component_provider_1.useTamboComponent; } });
|
|
10
|
+
var tambo_prop_stream_provider_1 = require("./tambo-prop-stream-provider");
|
|
11
|
+
Object.defineProperty(exports, "TamboPropStreamProvider", { enumerable: true, get: function () { return tambo_prop_stream_provider_1.TamboPropStreamProvider; } });
|
|
10
12
|
var tambo_provider_1 = require("./tambo-provider");
|
|
11
13
|
Object.defineProperty(exports, "TamboContext", { enumerable: true, get: function () { return tambo_provider_1.TamboContext; } });
|
|
12
14
|
Object.defineProperty(exports, "TamboProvider", { enumerable: true, get: function () { return tambo_provider_1.TamboProvider; } });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":";;;AACA,iEAA8E;AAArE,4HAAA,mBAAmB,OAAA;AAAE,uHAAA,cAAc,OAAA;AAC5C,uEAGoC;AAFlC,kIAAA,sBAAsB,OAAA;AACtB,6HAAA,iBAAiB,OAAA;AAEnB,mDAAyE;AAAhE,8GAAA,YAAY,OAAA;AAAE,+GAAA,aAAa,OAAA;AAAE,0GAAA,QAAQ,OAAA;AAC9C,qEAImC;AAHjC,gIAAA,qBAAqB,OAAA;AACrB,2HAAA,gBAAgB,OAAA;AAGlB,6CAA+E;AAAtE,gHAAA,iBAAiB,OAAA;AAC1B,iEAIiC;AAH/B,2HAAA,kBAAkB,OAAA;AAClB,4HAAA,mBAAmB,OAAA;AACnB,uHAAA,cAAc,OAAA","sourcesContent":["export type { TamboComponent, TamboTool } from \"../model/component-metadata\";\nexport { TamboClientProvider, useTamboClient } from \"./tambo-client-provider\";\nexport {\n TamboComponentProvider,\n useTamboComponent,\n} from \"./tambo-component-provider\";\nexport { TamboContext, TamboProvider, useTambo } from \"./tambo-provider\";\nexport {\n TamboRegistryProvider,\n useTamboRegistry,\n type TamboRegistryContext,\n} from \"./tambo-registry-provider\";\nexport { TamboStubProvider, type TamboStubProviderProps } from \"./tambo-stubs\";\nexport {\n TamboThreadContext,\n TamboThreadProvider,\n useTamboThread,\n} from \"./tambo-thread-provider\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":";;;AACA,iEAA8E;AAArE,4HAAA,mBAAmB,OAAA;AAAE,uHAAA,cAAc,OAAA;AAC5C,uEAGoC;AAFlC,kIAAA,sBAAsB,OAAA;AACtB,6HAAA,iBAAiB,OAAA;AAEnB,2EAAuE;AAA9D,qIAAA,uBAAuB,OAAA;AAChC,mDAAyE;AAAhE,8GAAA,YAAY,OAAA;AAAE,+GAAA,aAAa,OAAA;AAAE,0GAAA,QAAQ,OAAA;AAC9C,qEAImC;AAHjC,gIAAA,qBAAqB,OAAA;AACrB,2HAAA,gBAAgB,OAAA;AAGlB,6CAA+E;AAAtE,gHAAA,iBAAiB,OAAA;AAC1B,iEAIiC;AAH/B,2HAAA,kBAAkB,OAAA;AAClB,4HAAA,mBAAmB,OAAA;AACnB,uHAAA,cAAc,OAAA","sourcesContent":["export type { TamboComponent, TamboTool } from \"../model/component-metadata\";\nexport { TamboClientProvider, useTamboClient } from \"./tambo-client-provider\";\nexport {\n TamboComponentProvider,\n useTamboComponent,\n} from \"./tambo-component-provider\";\nexport { TamboPropStreamProvider } from \"./tambo-prop-stream-provider\";\nexport { TamboContext, TamboProvider, useTambo } from \"./tambo-provider\";\nexport {\n TamboRegistryProvider,\n useTamboRegistry,\n type TamboRegistryContext,\n} from \"./tambo-registry-provider\";\nexport { TamboStubProvider, type TamboStubProviderProps } from \"./tambo-stubs\";\nexport {\n TamboThreadContext,\n TamboThreadProvider,\n useTamboThread,\n} from \"./tambo-thread-provider\";\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
import { type TamboInteractableContext } from "../model/tambo-interactable";
|
|
3
|
+
declare const TamboInteractableContext: React.Context<TamboInteractableContext>;
|
|
4
|
+
/**
|
|
5
|
+
* The TamboInteractableProvider manages a list of components that are currently
|
|
6
|
+
* interactable, allowing tambo to interact with them by updating their props. It also registers tools
|
|
7
|
+
* for Tambo to perform CRUD operations on the components list.
|
|
8
|
+
* @param props - The props for the TamboInteractableProvider
|
|
9
|
+
* @param props.children - The children to wrap
|
|
10
|
+
* @returns The TamboInteractableProvider component
|
|
11
|
+
*/
|
|
12
|
+
export declare const TamboInteractableProvider: React.FC<PropsWithChildren>;
|
|
13
|
+
/**
|
|
14
|
+
* The useTamboInteractable hook provides access to the interactable component
|
|
15
|
+
* management functions.
|
|
16
|
+
* @returns The interactable component management functions
|
|
17
|
+
*/
|
|
18
|
+
export declare const useTamboInteractable: () => TamboInteractableContext;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=tambo-interactable-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tambo-interactable-provider.d.ts","sourceRoot":"","sources":["../../src/providers/tambo-interactable-provider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAEZ,iBAAiB,EAKlB,MAAM,OAAO,CAAC;AAEf,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,6BAA6B,CAAC;AAGrC,QAAA,MAAM,wBAAwB,yCAQ5B,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CA4PjE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,gCAEhC,CAAC"}
|
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;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AC;IAEP,MAAM,KAAK,GAA6B;QACtC,sBAAsB;QACtB,wBAAwB;QACxB,2BAA2B;QAC3B,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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3B,CAAC;AAEJ,eAAO,MAAM,uBAAuB,EACE,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,185 @@
|
|
|
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
|