@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,148 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import React, { createContext, useContext, useMemo, } from "react";
|
|
3
|
+
const TamboPropStreamContext = createContext(null);
|
|
4
|
+
/**
|
|
5
|
+
* Loading component that renders children when the stream is in a loading state
|
|
6
|
+
* @param props - The props for the Loading component
|
|
7
|
+
* @param props.key - The key to identify this loading state
|
|
8
|
+
* @param props.children - The children to render when loading
|
|
9
|
+
* @param props.className - Optional className for styling
|
|
10
|
+
* @returns The Loading component
|
|
11
|
+
*/
|
|
12
|
+
const Loading = ({ streamKey = "default", children, className, }) => {
|
|
13
|
+
const { getStatusForKey } = useTamboStream();
|
|
14
|
+
const status = getStatusForKey(streamKey);
|
|
15
|
+
if (!status.isPending && !status.isStreaming) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return (React.createElement("div", { className: className, "data-stream-key": streamKey, "data-stream-state": "loading" }, children));
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Empty component that renders children when the stream has no data
|
|
22
|
+
* @param props - The props for the Empty component
|
|
23
|
+
* @param props.key - The key to identify this empty state
|
|
24
|
+
* @param props.children - The children to render when empty
|
|
25
|
+
* @param props.className - Optional className for styling
|
|
26
|
+
* @returns The Empty component
|
|
27
|
+
*/
|
|
28
|
+
const Empty = ({ streamKey = "default", children, className, }) => {
|
|
29
|
+
const { data, getStatusForKey } = useTamboStream();
|
|
30
|
+
const status = getStatusForKey(streamKey);
|
|
31
|
+
// Get the specific data for this key
|
|
32
|
+
const keyData = data && typeof data === "object" && !Array.isArray(data)
|
|
33
|
+
? data[streamKey]
|
|
34
|
+
: data;
|
|
35
|
+
// Show empty state when not loading, not streaming, not successful, and no data for this key
|
|
36
|
+
const shouldShowEmpty = !status.isPending &&
|
|
37
|
+
!status.isStreaming &&
|
|
38
|
+
!status.isSuccess &&
|
|
39
|
+
!status.isError &&
|
|
40
|
+
(keyData === undefined || keyData === null || keyData === "");
|
|
41
|
+
if (!shouldShowEmpty) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return (React.createElement("div", { className: className, "data-stream-key": streamKey, "data-stream-state": "empty" }, children));
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Complete component that renders children when the stream has data
|
|
48
|
+
* @param props - The props for the Complete component
|
|
49
|
+
* @param props.key - The key to identify this complete state
|
|
50
|
+
* @param props.children - The children to render when complete
|
|
51
|
+
* @param props.className - Optional className for styling
|
|
52
|
+
* @returns The Complete component
|
|
53
|
+
*/
|
|
54
|
+
const Complete = ({ streamKey = "default", children, className, }) => {
|
|
55
|
+
const { data, getStatusForKey } = useTamboStream();
|
|
56
|
+
const status = getStatusForKey(streamKey);
|
|
57
|
+
// Get the specific data for this key
|
|
58
|
+
const keyData = data && typeof data === "object" && !Array.isArray(data)
|
|
59
|
+
? data[streamKey]
|
|
60
|
+
: data;
|
|
61
|
+
// Show complete when we have data for this key and the stream is successful
|
|
62
|
+
const shouldShowComplete = status.isSuccess && keyData !== undefined && keyData !== null;
|
|
63
|
+
if (!shouldShowComplete) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
return (React.createElement("div", { className: className, "data-stream-key": streamKey, "data-stream-state": "complete" }, children));
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Hook to use the TamboStream context
|
|
70
|
+
* @returns The TamboStream context
|
|
71
|
+
*/
|
|
72
|
+
export const useTamboStream = () => {
|
|
73
|
+
const context = useContext(TamboPropStreamContext);
|
|
74
|
+
if (!context) {
|
|
75
|
+
throw new Error("useTamboStream must be used within a TamboPropStreamProvider");
|
|
76
|
+
}
|
|
77
|
+
return context;
|
|
78
|
+
};
|
|
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
|
+
const TamboPropStreamProviderComponent = ({ children, data, streamStatus }) => {
|
|
89
|
+
// Create a default stream status if none provided
|
|
90
|
+
const defaultStreamStatus = useMemo(() => ({
|
|
91
|
+
isPending: false, // No external stream, so not pending
|
|
92
|
+
isStreaming: false, // No external stream, so not streaming
|
|
93
|
+
isSuccess: true, // If no stream status provided, assume success
|
|
94
|
+
isError: false,
|
|
95
|
+
streamError: undefined,
|
|
96
|
+
}), []);
|
|
97
|
+
const finalStreamStatus = streamStatus ?? defaultStreamStatus;
|
|
98
|
+
// Track status by key for compound components
|
|
99
|
+
const keyStatusMap = useMemo(() => {
|
|
100
|
+
const map = new Map();
|
|
101
|
+
// Track per-key status based on data structure
|
|
102
|
+
// If data is an object with keys, create status for each key
|
|
103
|
+
if (data && typeof data === "object" && !Array.isArray(data)) {
|
|
104
|
+
Object.keys(data).forEach((key) => {
|
|
105
|
+
const keyData = data[key];
|
|
106
|
+
const hasData = keyData !== undefined && keyData !== null && keyData !== "";
|
|
107
|
+
map.set(key, {
|
|
108
|
+
// If no external stream, show loading when key has no data
|
|
109
|
+
isPending: finalStreamStatus.isPending ||
|
|
110
|
+
(!finalStreamStatus.isStreaming && !hasData),
|
|
111
|
+
isStreaming: finalStreamStatus.isStreaming && !hasData,
|
|
112
|
+
isSuccess: finalStreamStatus.isSuccess && hasData,
|
|
113
|
+
isError: finalStreamStatus.isError,
|
|
114
|
+
error: finalStreamStatus.streamError,
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// Always set default status for fallback
|
|
119
|
+
map.set("default", {
|
|
120
|
+
isPending: finalStreamStatus.isPending,
|
|
121
|
+
isStreaming: finalStreamStatus.isStreaming,
|
|
122
|
+
isSuccess: finalStreamStatus.isSuccess,
|
|
123
|
+
isError: finalStreamStatus.isError,
|
|
124
|
+
error: finalStreamStatus.streamError,
|
|
125
|
+
});
|
|
126
|
+
return map;
|
|
127
|
+
}, [finalStreamStatus, data]);
|
|
128
|
+
const getStatusForKey = useMemo(() => (key) => {
|
|
129
|
+
return (keyStatusMap.get(key) ??
|
|
130
|
+
keyStatusMap.get("default") ?? {
|
|
131
|
+
isPending: false,
|
|
132
|
+
isStreaming: false,
|
|
133
|
+
isSuccess: false,
|
|
134
|
+
isError: false,
|
|
135
|
+
});
|
|
136
|
+
}, [keyStatusMap]);
|
|
137
|
+
const contextValue = useMemo(() => ({
|
|
138
|
+
data,
|
|
139
|
+
streamStatus: finalStreamStatus,
|
|
140
|
+
getStatusForKey,
|
|
141
|
+
}), [data, finalStreamStatus, getStatusForKey]);
|
|
142
|
+
return (React.createElement(TamboPropStreamContext.Provider, { value: contextValue }, children));
|
|
143
|
+
};
|
|
144
|
+
export const TamboPropStreamProvider = TamboPropStreamProviderComponent;
|
|
145
|
+
TamboPropStreamProvider.Loading = Loading;
|
|
146
|
+
TamboPropStreamProvider.Empty = Empty;
|
|
147
|
+
TamboPropStreamProvider.Complete = Complete;
|
|
148
|
+
//# 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,OAAO,KAAK,EAAE,EACZ,aAAa,EAEb,UAAU,EACV,OAAO,GACR,MAAM,OAAO,CAAC;AAkBf,MAAM,sBAAsB,GAC1B,aAAa,CAAqC,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,cAAc,EAAE,CAAC;IAC7C,MAAM,MAAM,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAE1C,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,CACL,6BACE,SAAS,EAAE,SAAS,qBACH,SAAS,uBACR,SAAS,IAE1B,QAAQ,CACL,CACP,CAAC;AACJ,CA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G,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,OAAO,CAC1B,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,oBAAC,sBAAsB,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,IACjD,QAAQ,CACuB,CACnC,CAAC;AACJ,CAAC,CAAC;AAUF,MAAM,CAAC,MAAM,uBAAuB,GAClC,gCAAmE,CAAC;AAEtE,uBAAuB,CAAC,OAAO,GAAG,OAAO,CAAC;AAC1C,uBAAuB,CAAC,KAAK,GAAG,KAAK,CAAC;AACtC,uBAAuB,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"}
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
import React, { createContext, useContext } from "react";
|
|
3
3
|
import { TamboClientProvider, useTamboClient, useTamboQueryClient, } from "./tambo-client-provider";
|
|
4
4
|
import { TamboComponentProvider, useTamboComponent, } from "./tambo-component-provider";
|
|
5
|
+
import { TamboInteractableProvider, useTamboInteractable, } from "./tambo-interactable-provider";
|
|
5
6
|
import { TamboRegistryProvider, } from "./tambo-registry-provider";
|
|
6
7
|
import { TamboThreadProvider, useTamboThread, } from "./tambo-thread-provider";
|
|
7
8
|
/**
|
|
8
9
|
* The TamboProvider gives full access to the whole Tambo API. This includes the
|
|
9
|
-
* TamboAI client, the component registry,
|
|
10
|
+
* TamboAI client, the component registry, the current thread context, and interactable components.
|
|
10
11
|
* @param props - The props for the TamboProvider
|
|
11
12
|
* @param props.children - The children to wrap
|
|
12
13
|
* @param props.tamboUrl - The URL of the Tambo API
|
|
@@ -26,12 +27,13 @@ export const TamboProvider = ({ children, tamboUrl, apiKey, userToken, component
|
|
|
26
27
|
React.createElement(TamboRegistryProvider, { components: components, tools: tools },
|
|
27
28
|
React.createElement(TamboThreadProvider, { streaming: streaming },
|
|
28
29
|
React.createElement(TamboComponentProvider, null,
|
|
29
|
-
React.createElement(
|
|
30
|
+
React.createElement(TamboInteractableProvider, null,
|
|
31
|
+
React.createElement(TamboCompositeProvider, null, children)))))));
|
|
30
32
|
};
|
|
31
33
|
export const TamboContext = createContext({});
|
|
32
34
|
/**
|
|
33
35
|
* TamboCompositeProvider is a provider that combines the TamboClient,
|
|
34
|
-
* TamboThread, and
|
|
36
|
+
* TamboThread, TamboComponent, and TamboInteractable providers
|
|
35
37
|
* @param props - The props for the TamboCompositeProvider
|
|
36
38
|
* @param props.children - The children to wrap
|
|
37
39
|
* @returns The wrapped component
|
|
@@ -41,18 +43,21 @@ export const TamboCompositeProvider = ({ children, }) => {
|
|
|
41
43
|
const client = useTamboClient();
|
|
42
44
|
const queryClient = useTamboQueryClient();
|
|
43
45
|
const componentRegistry = useTamboComponent();
|
|
46
|
+
const interactableComponents = useTamboInteractable();
|
|
44
47
|
return (React.createElement(TamboContext.Provider, { value: {
|
|
45
48
|
client,
|
|
46
49
|
queryClient,
|
|
47
50
|
...componentRegistry,
|
|
48
51
|
...threads,
|
|
52
|
+
...interactableComponents,
|
|
49
53
|
} }, children));
|
|
50
54
|
};
|
|
51
55
|
/**
|
|
52
56
|
* The useTambo hook provides access to the Tambo API. This is the primary entrypoint
|
|
53
57
|
* for the Tambo React SDK.
|
|
54
58
|
*
|
|
55
|
-
* This includes the TamboAI client, the component registry,
|
|
59
|
+
* This includes the TamboAI client, the component registry, the current thread context,
|
|
60
|
+
* and interactable component management.
|
|
56
61
|
* @returns The Tambo API
|
|
57
62
|
*/
|
|
58
63
|
export const useTambo = () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,KAAK,EAAE,EAAqB,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"tambo-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,KAAK,EAAE,EAAqB,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAE5E,OAAO,EAEL,mBAAmB,EAEnB,cAAc,EACd,mBAAmB,GACpB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,yBAAyB,EACzB,oBAAoB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,qBAAqB,GAEtB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,mBAAmB,EAEnB,cAAc,GACf,MAAM,yBAAyB,CAAC;AAEjC;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,aAAa,GAMtB,CAAC,EACH,QAAQ,EACR,QAAQ,EACR,MAAM,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E;IAC3B,OAAO,UAAU,CAAC,YAAY,CAAC,CAAC;AAClC,CAAC,CAAC","sourcesContent":["\"use client\";\nimport React, { PropsWithChildren, createContext, useContext } from \"react\";\nimport { TamboInteractableContext } from \"../model/tambo-interactable\";\nimport {\n TamboClientContextProps,\n TamboClientProvider,\n TamboClientProviderProps,\n useTamboClient,\n useTamboQueryClient,\n} from \"./tambo-client-provider\";\nimport {\n TamboComponentContextProps,\n TamboComponentProvider,\n useTamboComponent,\n} from \"./tambo-component-provider\";\nimport {\n TamboInteractableProvider,\n useTamboInteractable,\n} from \"./tambo-interactable-provider\";\nimport {\n TamboRegistryProvider,\n TamboRegistryProviderProps,\n} from \"./tambo-registry-provider\";\nimport {\n TamboThreadContextProps,\n TamboThreadProvider,\n TamboThreadProviderProps,\n useTamboThread,\n} from \"./tambo-thread-provider\";\n\n/**\n * The TamboProvider gives full access to the whole Tambo API. This includes the\n * TamboAI client, the component registry, the current thread context, and interactable components.\n * @param props - The props for the TamboProvider\n * @param props.children - The children to wrap\n * @param props.tamboUrl - The URL of the Tambo API\n * @param props.apiKey - The API key for the Tambo API\n * @param props.components - The components to register\n * @param props.environment - The environment to use for the Tambo API\n * @param props.tools - The tools to register\n * @param props.streaming - Whether to stream the response by default. Defaults to true.\n * @returns The TamboProvider component\n */\nexport const TamboProvider: React.FC<\n PropsWithChildren<\n TamboClientProviderProps &\n TamboRegistryProviderProps &\n TamboThreadProviderProps\n >\n> = ({\n children,\n tamboUrl,\n apiKey,\n userToken,\n components,\n environment,\n tools,\n streaming,\n}) => {\n // Should only be used in browser\n if (typeof window === \"undefined\") {\n console.error(\"TamboProvider must be used within a browser\");\n }\n\n return (\n <TamboClientProvider\n tamboUrl={tamboUrl}\n apiKey={apiKey}\n environment={environment}\n userToken={userToken}\n >\n <TamboRegistryProvider components={components} tools={tools}>\n <TamboThreadProvider streaming={streaming}>\n <TamboComponentProvider>\n <TamboInteractableProvider>\n <TamboCompositeProvider>{children}</TamboCompositeProvider>\n </TamboInteractableProvider>\n </TamboComponentProvider>\n </TamboThreadProvider>\n </TamboRegistryProvider>\n </TamboClientProvider>\n );\n};\n\nexport type TamboContextProps = TamboClientContextProps &\n TamboThreadContextProps &\n TamboComponentContextProps &\n TamboInteractableContext;\n\nexport const TamboContext = createContext<TamboContextProps>(\n {} as TamboContextProps,\n);\n\n/**\n * TamboCompositeProvider is a provider that combines the TamboClient,\n * TamboThread, TamboComponent, and TamboInteractable providers\n * @param props - The props for the TamboCompositeProvider\n * @param props.children - The children to wrap\n * @returns The wrapped component\n */\nexport const TamboCompositeProvider: React.FC<PropsWithChildren> = ({\n children,\n}) => {\n const threads = useTamboThread();\n const client = useTamboClient();\n const queryClient = useTamboQueryClient();\n const componentRegistry = useTamboComponent();\n const interactableComponents = useTamboInteractable();\n\n return (\n <TamboContext.Provider\n value={{\n client,\n queryClient,\n ...componentRegistry,\n ...threads,\n ...interactableComponents,\n }}\n >\n {children}\n </TamboContext.Provider>\n );\n};\n\n/**\n * The useTambo hook provides access to the Tambo API. This is the primary entrypoint\n * for the Tambo React SDK.\n *\n * This includes the TamboAI client, the component registry, the current thread context,\n * and interactable component management.\n * @returns The Tambo API\n */\nexport const useTambo = () => {\n return useContext(TamboContext);\n};\n"]}
|