@tambo-ai/react 0.38.1 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/hooks/use-component-state.js +0 -1
- package/dist/hooks/use-component-state.js.map +1 -1
- package/dist/hooks/use-thread-input.d.ts +1 -0
- package/dist/hooks/use-thread-input.d.ts.map +1 -1
- package/dist/hooks/use-thread-input.js +2 -1
- package/dist/hooks/use-thread-input.js.map +1 -1
- package/dist/mcp/mcp-client.d.ts +15 -15
- package/dist/providers/__tests__/tambo-prop-stream-provider.test.js +144 -224
- package/dist/providers/__tests__/tambo-prop-stream-provider.test.js.map +1 -1
- package/dist/providers/__tests__/tambo-thread-provider.test.js +65 -0
- package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +4 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/tambo-prop-stream-provider/index.d.ts +19 -0
- package/dist/providers/tambo-prop-stream-provider/index.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/index.js +43 -0
- package/dist/providers/tambo-prop-stream-provider/index.js.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/pending.d.ts +12 -0
- package/dist/providers/tambo-prop-stream-provider/pending.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/pending.js +31 -0
- package/dist/providers/tambo-prop-stream-provider/pending.js.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/provider.d.ts +17 -0
- package/dist/providers/tambo-prop-stream-provider/provider.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/provider.js +107 -0
- package/dist/providers/tambo-prop-stream-provider/provider.js.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/streaming.d.ts +12 -0
- package/dist/providers/tambo-prop-stream-provider/streaming.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/streaming.js +28 -0
- package/dist/providers/tambo-prop-stream-provider/streaming.js.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/success.d.ts +12 -0
- package/dist/providers/tambo-prop-stream-provider/success.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/success.js +28 -0
- package/dist/providers/tambo-prop-stream-provider/success.js.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/types.d.ts +25 -0
- package/dist/providers/tambo-prop-stream-provider/types.d.ts.map +1 -0
- package/dist/providers/tambo-prop-stream-provider/types.js +6 -0
- package/dist/providers/tambo-prop-stream-provider/types.js.map +1 -0
- package/dist/providers/tambo-thread-provider.d.ts +1 -0
- package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
- package/dist/providers/tambo-thread-provider.js +13 -2
- package/dist/providers/tambo-thread-provider.js.map +1 -1
- package/dist/util/registry.d.ts +4 -1
- package/dist/util/registry.d.ts.map +1 -1
- package/dist/util/registry.js +9 -11
- package/dist/util/registry.js.map +1 -1
- package/esm/hooks/use-component-state.js +0 -1
- package/esm/hooks/use-component-state.js.map +1 -1
- package/esm/hooks/use-thread-input.d.ts +1 -0
- package/esm/hooks/use-thread-input.d.ts.map +1 -1
- package/esm/hooks/use-thread-input.js +2 -1
- package/esm/hooks/use-thread-input.js.map +1 -1
- package/esm/mcp/mcp-client.d.ts +15 -15
- package/esm/providers/__tests__/tambo-prop-stream-provider.test.js +144 -224
- package/esm/providers/__tests__/tambo-prop-stream-provider.test.js.map +1 -1
- package/esm/providers/__tests__/tambo-thread-provider.test.js +65 -0
- package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/esm/providers/index.d.ts +2 -2
- package/esm/providers/index.d.ts.map +1 -1
- package/esm/providers/index.js +1 -1
- package/esm/providers/index.js.map +1 -1
- package/esm/providers/tambo-prop-stream-provider/index.d.ts +19 -0
- package/esm/providers/tambo-prop-stream-provider/index.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/index.js +22 -0
- package/esm/providers/tambo-prop-stream-provider/index.js.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/pending.d.ts +12 -0
- package/esm/providers/tambo-prop-stream-provider/pending.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/pending.js +24 -0
- package/esm/providers/tambo-prop-stream-provider/pending.js.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/provider.d.ts +17 -0
- package/esm/providers/tambo-prop-stream-provider/provider.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/provider.js +70 -0
- package/esm/providers/tambo-prop-stream-provider/provider.js.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/streaming.d.ts +12 -0
- package/esm/providers/tambo-prop-stream-provider/streaming.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/streaming.js +21 -0
- package/esm/providers/tambo-prop-stream-provider/streaming.js.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/success.d.ts +12 -0
- package/esm/providers/tambo-prop-stream-provider/success.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/success.js +21 -0
- package/esm/providers/tambo-prop-stream-provider/success.js.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/types.d.ts +25 -0
- package/esm/providers/tambo-prop-stream-provider/types.d.ts.map +1 -0
- package/esm/providers/tambo-prop-stream-provider/types.js +3 -0
- package/esm/providers/tambo-prop-stream-provider/types.js.map +1 -0
- package/esm/providers/tambo-thread-provider.d.ts +1 -0
- package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
- package/esm/providers/tambo-thread-provider.js +14 -3
- package/esm/providers/tambo-thread-provider.js.map +1 -1
- package/esm/util/registry.d.ts +4 -1
- package/esm/util/registry.d.ts.map +1 -1
- package/esm/util/registry.js +7 -9
- package/esm/util/registry.js.map +1 -1
- package/package.json +12 -11
- package/dist/providers/tambo-prop-stream-provider.d.ts +0 -96
- package/dist/providers/tambo-prop-stream-provider.d.ts.map +0 -1
- package/dist/providers/tambo-prop-stream-provider.js +0 -185
- package/dist/providers/tambo-prop-stream-provider.js.map +0 -1
- package/esm/providers/tambo-prop-stream-provider.d.ts +0 -96
- package/esm/providers/tambo-prop-stream-provider.d.ts.map +0 -1
- package/esm/providers/tambo-prop-stream-provider.js +0 -148
- package/esm/providers/tambo-prop-stream-provider.js.map +0 -1
|
@@ -1,96 +0,0 @@
|
|
|
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.streamKey - 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.streamKey - 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.streamKey - 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
|
|
@@ -1 +0,0 @@
|
|
|
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,6CAA6C;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,0CAA0C;IAC1C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,2CAA2C;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,8CAA8C;IAC9C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,QAAA,MAAM,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,YAAY,CAqBnC,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,KAAK,EAAE,KAAK,CAAC,EAAE,CAAC,UAAU,CAmC/B,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC,aAAa,CA+BrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,mCAQ1B,CAAC;AAEF;;;;;;;;GAQG;AACH,QAAA,MAAM,gCAAgC,EAAE,KAAK,CAAC,EAAE,CAC9C,iBAAiB,CAAC,4BAA4B,CAAC,CA2FhD,CAAC;AAGF,KAAK,+BAA+B,GAClC,OAAO,gCAAgC,GAAG;IACxC,OAAO,EAAE,OAAO,OAAO,CAAC;IACxB,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,QAAQ,EAAE,OAAO,QAAQ,CAAC;CAC3B,CAAC;AAEJ,eAAO,MAAM,uBAAuB,EACE,+BAA+B,CAAC"}
|
|
@@ -1,185 +0,0 @@
|
|
|
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.streamKey - 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.streamKey - 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.streamKey - 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
|
|
@@ -1 +0,0 @@
|
|
|
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,MAAM,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAE1C,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;QAC7C,OAAO,IAAI,CAAC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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.streamKey - 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.streamKey - 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.streamKey - 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,96 +0,0 @@
|
|
|
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.streamKey - 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.streamKey - 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.streamKey - 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
|
|
@@ -1 +0,0 @@
|
|
|
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,6CAA6C;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,0CAA0C;IAC1C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,2CAA2C;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,8CAA8C;IAC9C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,QAAA,MAAM,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,YAAY,CAqBnC,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,KAAK,EAAE,KAAK,CAAC,EAAE,CAAC,UAAU,CAmC/B,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC,aAAa,CA+BrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,mCAQ1B,CAAC;AAEF;;;;;;;;GAQG;AACH,QAAA,MAAM,gCAAgC,EAAE,KAAK,CAAC,EAAE,CAC9C,iBAAiB,CAAC,4BAA4B,CAAC,CA2FhD,CAAC;AAGF,KAAK,+BAA+B,GAClC,OAAO,gCAAgC,GAAG;IACxC,OAAO,EAAE,OAAO,OAAO,CAAC;IACxB,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,QAAQ,EAAE,OAAO,QAAQ,CAAC;CAC3B,CAAC;AAEJ,eAAO,MAAM,uBAAuB,EACE,+BAA+B,CAAC"}
|
|
@@ -1,148 +0,0 @@
|
|
|
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.streamKey - 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.streamKey - 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.streamKey - 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
|
|
@@ -1 +0,0 @@
|
|
|
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,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,KAAK,GAAyB,CAAC,EACnC,SAAS,GAAG,SAAS,EACrB,QAAQ,EACR,SAAS,GACV,EAAE,EAAE;IACH,MAAM,EAAE,IAAI,EAAE,eAAe,EAAE,GAAG,cAAc,EAAE,CAAC;IACnD,MAAM,MAAM,GAAG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AO,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.streamKey - 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.streamKey - 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.streamKey - 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"]}
|