@convex-dev/agent 0.2.6 → 0.2.8-alpha.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/UIMessages.d.ts +43 -0
- package/dist/UIMessages.d.ts.map +1 -0
- package/dist/{react/toUIMessages.js → UIMessages.js} +97 -4
- package/dist/UIMessages.js.map +1 -0
- package/dist/client/createTool.js +1 -1
- package/dist/client/createTool.js.map +1 -1
- package/dist/client/index.d.ts +39 -19
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +22 -19
- package/dist/client/index.js.map +1 -1
- package/dist/client/mockModel.d.ts +2 -0
- package/dist/client/mockModel.d.ts.map +1 -1
- package/dist/client/mockModel.js +11 -7
- package/dist/client/mockModel.js.map +1 -1
- package/dist/client/search.d.ts.map +1 -1
- package/dist/client/search.js +2 -0
- package/dist/client/search.js.map +1 -1
- package/dist/client/start.d.ts +1 -0
- package/dist/client/start.d.ts.map +1 -1
- package/dist/client/start.js +0 -1
- package/dist/client/start.js.map +1 -1
- package/dist/client/streaming.d.ts +23 -4
- package/dist/client/streaming.d.ts.map +1 -1
- package/dist/client/streaming.js +32 -6
- package/dist/client/streaming.js.map +1 -1
- package/dist/client/types.d.ts +19 -13
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +2 -0
- package/dist/component/messages.js +1 -1
- package/dist/component/messages.js.map +1 -1
- package/dist/component/schema.d.ts +10 -4
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +4 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/streams.d.ts +2 -4
- package/dist/component/streams.d.ts.map +1 -1
- package/dist/component/streams.js +42 -43
- package/dist/component/streams.js.map +1 -1
- package/dist/deltas.d.ts +48 -0
- package/dist/deltas.d.ts.map +1 -0
- package/dist/deltas.js +468 -0
- package/dist/deltas.js.map +1 -0
- package/dist/react/index.d.ts +4 -77
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +4 -178
- package/dist/react/index.js.map +1 -1
- package/dist/react/optimisticallySendMessage.d.ts +36 -3
- package/dist/react/optimisticallySendMessage.d.ts.map +1 -1
- package/dist/react/optimisticallySendMessage.js +37 -5
- package/dist/react/optimisticallySendMessage.js.map +1 -1
- package/dist/react/types.d.ts +4 -18
- package/dist/react/types.d.ts.map +1 -1
- package/dist/react/useStreamingUIMessages.d.ts +22 -0
- package/dist/react/useStreamingUIMessages.d.ts.map +1 -0
- package/dist/react/useStreamingUIMessages.js +204 -0
- package/dist/react/useStreamingUIMessages.js.map +1 -0
- package/dist/react/useThreadMessages.d.ts +103 -0
- package/dist/react/useThreadMessages.d.ts.map +1 -0
- package/dist/react/useThreadMessages.js +139 -0
- package/dist/react/useThreadMessages.js.map +1 -0
- package/dist/react/useUIMessages.d.ts +91 -0
- package/dist/react/useUIMessages.d.ts.map +1 -0
- package/dist/react/useUIMessages.js +115 -0
- package/dist/react/useUIMessages.js.map +1 -0
- package/dist/validators.d.ts +3 -1
- package/dist/validators.d.ts.map +1 -1
- package/dist/validators.js +1 -0
- package/dist/validators.js.map +1 -1
- package/package.json +4 -4
- package/src/{react/toUIMessages.ts → UIMessages.ts} +132 -15
- package/src/client/createTool.ts +1 -1
- package/src/client/index.ts +60 -41
- package/src/client/mockModel.ts +13 -8
- package/src/client/search.ts +2 -0
- package/src/client/start.ts +2 -1
- package/src/client/streaming.test.ts +186 -0
- package/src/client/streaming.ts +50 -9
- package/src/client/types.ts +19 -21
- package/src/component/_generated/api.d.ts +2 -0
- package/src/component/messages.ts +1 -1
- package/src/component/schema.ts +6 -0
- package/src/component/streams.ts +49 -66
- package/src/{react/deltas.test.ts → deltas.test.ts} +57 -46
- package/src/deltas.ts +589 -0
- package/src/{react/fromUIMessages.test.ts → fromUIMessages.test.ts} +108 -43
- package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -1
- package/src/react/index.ts +8 -250
- package/src/react/optimisticallySendMessage.ts +57 -8
- package/src/react/types.ts +6 -39
- package/src/react/useStreamingUIMessages.ts +294 -0
- package/src/react/useThreadMessages.ts +251 -0
- package/src/react/useUIMessages.ts +206 -0
- package/src/{react/toUIMessages.test.ts → toUIMessages.test.ts} +3 -3
- package/src/validators.ts +3 -0
- package/dist/client/textStreamParts.d.ts +0 -5
- package/dist/client/textStreamParts.d.ts.map +0 -1
- package/dist/client/textStreamParts.js +0 -44
- package/dist/client/textStreamParts.js.map +0 -1
- package/dist/react/deltas.d.ts +0 -27
- package/dist/react/deltas.d.ts.map +0 -1
- package/dist/react/deltas.js +0 -627
- package/dist/react/deltas.js.map +0 -1
- package/dist/react/fromUIMessages.d.ts +0 -13
- package/dist/react/fromUIMessages.d.ts.map +0 -1
- package/dist/react/fromUIMessages.js +0 -70
- package/dist/react/fromUIMessages.js.map +0 -1
- package/dist/react/toUIMessages.d.ts +0 -19
- package/dist/react/toUIMessages.d.ts.map +0 -1
- package/dist/react/toUIMessages.js.map +0 -1
- package/src/client/textStreamParts.ts +0 -62
- package/src/react/deltas.ts +0 -774
- package/src/react/fromUIMessages.ts +0 -85
package/dist/react/index.js
CHANGED
|
@@ -1,186 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { omit } from "convex-helpers";
|
|
3
|
-
import { useQuery, } from "convex/react";
|
|
4
|
-
import { usePaginatedQuery } from "convex-helpers/react";
|
|
5
2
|
import { useMemo, useState } from "react";
|
|
6
|
-
|
|
7
|
-
import { mergeDeltas } from "./deltas.js";
|
|
3
|
+
export { toUIMessages } from "../UIMessages.js";
|
|
8
4
|
export { optimisticallySendMessage } from "./optimisticallySendMessage.js";
|
|
9
5
|
export { useSmoothText } from "./useSmoothText.js";
|
|
10
6
|
export { SmoothText } from "./SmoothText.js";
|
|
11
|
-
export {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
* This hook is a wrapper around `usePaginatedQuery` and `useStreamingThreadMessages`.
|
|
16
|
-
* It will fetch both full messages and streaming messages, and merge them together.
|
|
17
|
-
*
|
|
18
|
-
* The query must take as arguments `{ threadId, paginationOpts }` and return a
|
|
19
|
-
* pagination result of objects that extend `MessageDoc`.
|
|
20
|
-
*
|
|
21
|
-
* For streaming, it should look like this:
|
|
22
|
-
* ```ts
|
|
23
|
-
* export const listThreadMessages = query({
|
|
24
|
-
* args: {
|
|
25
|
-
* threadId: v.string(),
|
|
26
|
-
* paginationOpts: paginationOptsValidator,
|
|
27
|
-
* streamArgs: vStreamArgs,
|
|
28
|
-
* ... other arguments you want
|
|
29
|
-
* },
|
|
30
|
-
* handler: async (ctx, { threadId, paginationOpts, streamArgs }) => {
|
|
31
|
-
* // await authorizeThreadAccess(ctx, threadId);
|
|
32
|
-
* const paginated = await agent.listMessages(ctx, { threadId, paginationOpts });
|
|
33
|
-
* const streams = await agent.syncStreams(ctx, { threadId, streamArgs });
|
|
34
|
-
* // Here you could filter out / modify the documents & stream deltas.
|
|
35
|
-
* return { ...paginated, streams };
|
|
36
|
-
* },
|
|
37
|
-
* });
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* Then the hook can be used like this:
|
|
41
|
-
* ```ts
|
|
42
|
-
* const messages = useThreadMessages(
|
|
43
|
-
* api.myModule.listThreadMessages,
|
|
44
|
-
* { threadId },
|
|
45
|
-
* { initialNumItems: 10, stream: true }
|
|
46
|
-
* );
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @param query The query to use to fetch messages.
|
|
50
|
-
* It must take as arguments `{ threadId, paginationOpts }` and return a
|
|
51
|
-
* pagination result of objects that extend `MessageDoc`.
|
|
52
|
-
* To support streaming, it must also take in `streamArgs: vStreamArgs` and
|
|
53
|
-
* return a `streams` object returned from `agent.syncStreams`.
|
|
54
|
-
* @param args The arguments to pass to the query other than `paginationOpts`
|
|
55
|
-
* and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that
|
|
56
|
-
* you want to pass to the query.
|
|
57
|
-
* @param options The options for the query. Similar to usePaginatedQuery.
|
|
58
|
-
* To enable streaming, pass `stream: true`.
|
|
59
|
-
* @returns The messages. If stream is true, it will return a list of messages
|
|
60
|
-
* that includes both full messages and streaming messages.
|
|
61
|
-
*/
|
|
62
|
-
export function useThreadMessages(query, args, options) {
|
|
63
|
-
// These are full messages
|
|
64
|
-
const paginated = usePaginatedQuery(query, args, { initialNumItems: options.initialNumItems });
|
|
65
|
-
let startOrder = paginated.results.at(-1)?.order ?? 0;
|
|
66
|
-
for (let i = paginated.results.length - 1; i >= 0; i--) {
|
|
67
|
-
const m = paginated.results[i];
|
|
68
|
-
if (!m.streaming && m.status === "pending") {
|
|
69
|
-
// round down to the nearest 10 for some cache benefits
|
|
70
|
-
startOrder = m.order - (m.order % 10);
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// These are streaming messages that will not include full messages.
|
|
75
|
-
const streamMessages = useStreamingThreadMessages(query, !options.stream ||
|
|
76
|
-
args === "skip" ||
|
|
77
|
-
paginated.status === "LoadingFirstPage"
|
|
78
|
-
? "skip"
|
|
79
|
-
: { ...args, startOrder });
|
|
80
|
-
const merged = useMemo(() => {
|
|
81
|
-
const streamListMessages = streamMessages?.map((m) => ({
|
|
82
|
-
...m,
|
|
83
|
-
streaming: !m.status || m.status === "pending",
|
|
84
|
-
})) ?? [];
|
|
85
|
-
return {
|
|
86
|
-
...paginated,
|
|
87
|
-
results: paginated.results
|
|
88
|
-
.map((m) => ({ ...m, streaming: false }))
|
|
89
|
-
// Note: this is intentionally after paginated results.
|
|
90
|
-
.concat(streamListMessages)
|
|
91
|
-
.sort((a, b) => a.order === b.order ? a.stepOrder - b.stepOrder : a.order - b.order)
|
|
92
|
-
.reduce((msgs, msg) => {
|
|
93
|
-
const last = msgs.at(-1);
|
|
94
|
-
if (!last) {
|
|
95
|
-
return [msg];
|
|
96
|
-
}
|
|
97
|
-
if (last.order !== msg.order || last.stepOrder !== msg.stepOrder) {
|
|
98
|
-
return [...msgs, msg];
|
|
99
|
-
}
|
|
100
|
-
if (last.status === "pending" &&
|
|
101
|
-
(msg.streaming || msg.status !== "pending")) {
|
|
102
|
-
// Let's prefer a streaming or finalized message over a pending
|
|
103
|
-
// one.
|
|
104
|
-
return [...msgs.slice(0, -1), msg];
|
|
105
|
-
}
|
|
106
|
-
// skip the new one if the previous one (listed) was finalized
|
|
107
|
-
return msgs;
|
|
108
|
-
}, []),
|
|
109
|
-
};
|
|
110
|
-
}, [paginated, streamMessages]);
|
|
111
|
-
return merged;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* A hook that fetches streaming messages from a thread.
|
|
115
|
-
* This ONLY returns streaming messages. To get both, use `useThreadMessages`.
|
|
116
|
-
*
|
|
117
|
-
* @param query The query to use to fetch messages.
|
|
118
|
-
* It must take as arguments `{ threadId, paginationOpts, streamArgs }` and
|
|
119
|
-
* return a `streams` object returned from `agent.syncStreams`.
|
|
120
|
-
* @param args The arguments to pass to the query other than `paginationOpts`
|
|
121
|
-
* and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that
|
|
122
|
-
* you want to pass to the query.
|
|
123
|
-
* @returns The streaming messages.
|
|
124
|
-
*/
|
|
125
|
-
export function useStreamingThreadMessages(query, args) {
|
|
126
|
-
// Invariant: streamMessages[streamId] is comprised of all deltas up to the
|
|
127
|
-
// cursor. There can be multiple messages in the same stream, e.g. for tool
|
|
128
|
-
// calls.
|
|
129
|
-
const [streams, setStreams] = useState([]);
|
|
130
|
-
const queryArgs = args === "skip" ? args : omit(args, ["startOrder"]);
|
|
131
|
-
// Get all the active streams
|
|
132
|
-
const streamList = useQuery(query, queryArgs === "skip"
|
|
133
|
-
? queryArgs
|
|
134
|
-
: {
|
|
135
|
-
...queryArgs,
|
|
136
|
-
paginationOpts: { cursor: null, numItems: 0 },
|
|
137
|
-
streamArgs: {
|
|
138
|
-
kind: "list",
|
|
139
|
-
startOrder: queryArgs.startOrder ?? 0,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
// Get the cursors for all the active streams
|
|
143
|
-
const cursors = useMemo(() => {
|
|
144
|
-
if (!streamList?.streams)
|
|
145
|
-
return [];
|
|
146
|
-
if (streamList.streams.kind !== "list") {
|
|
147
|
-
throw new Error("Expected list streams");
|
|
148
|
-
}
|
|
149
|
-
return streamList.streams.messages.map(({ streamId }) => {
|
|
150
|
-
const stream = streams.find((s) => s.streamId === streamId);
|
|
151
|
-
const cursor = stream?.cursor ?? 0;
|
|
152
|
-
return { streamId, cursor };
|
|
153
|
-
});
|
|
154
|
-
}, [streamList, streams]);
|
|
155
|
-
// Get the deltas for all the active streams, if any.
|
|
156
|
-
const cursorQuery = useQuery(query, queryArgs === "skip" || !streamList
|
|
157
|
-
? "skip"
|
|
158
|
-
: {
|
|
159
|
-
...queryArgs,
|
|
160
|
-
paginationOpts: { cursor: null, numItems: 0 },
|
|
161
|
-
streamArgs: { kind: "deltas", cursors },
|
|
162
|
-
});
|
|
163
|
-
// Merge any deltas into the streamChunks, keeping it unmodified if unchanged.
|
|
164
|
-
const threadId = args === "skip" ? undefined : args.threadId;
|
|
165
|
-
const [messages, newStreams, changed] = useMemo(() => {
|
|
166
|
-
if (!threadId)
|
|
167
|
-
return [undefined, [], false];
|
|
168
|
-
if (!streamList)
|
|
169
|
-
return [undefined, [], false];
|
|
170
|
-
if (cursorQuery && cursorQuery.streams?.kind !== "deltas") {
|
|
171
|
-
throw new Error("Expected deltas streams");
|
|
172
|
-
}
|
|
173
|
-
return mergeDeltas(threadId, streamList.streams.messages, streams, cursorQuery?.streams?.deltas ?? []);
|
|
174
|
-
}, [threadId, cursorQuery, streams, streamList]);
|
|
175
|
-
// Now assemble the chunks into messages
|
|
176
|
-
if (!threadId) {
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
if (changed) {
|
|
180
|
-
setStreams(newStreams);
|
|
181
|
-
}
|
|
182
|
-
return messages;
|
|
183
|
-
}
|
|
7
|
+
export { useThreadMessages, useStreamingThreadMessages, } from "./useThreadMessages.js";
|
|
8
|
+
export { useUIMessages } from "./useUIMessages.js";
|
|
9
|
+
export { useStreamingUIMessages } from "./useStreamingUIMessages.js";
|
|
184
10
|
/**
|
|
185
11
|
* @deprecated use useThreadMessages or useStreamingThreadMessages instead
|
|
186
12
|
* Use this hook to stream text from a server action, using the
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAkB,MAAM,kBAAkB,CAAC;AAEhE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAEL,iBAAiB,EACjB,0BAA0B,GAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAwB,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAErE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAC9B,GAAW,EACX,QAAuB,EACvB,KAAc;IAEd,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IACrC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC9C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IAEvD,MAAM,UAAU,GAAG,OAAO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AC;YAC3B,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAClD,QAAQ,CAAC,CAAC,CAAC,CAAC;YACd,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;IACH,CAAC,EACD,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,CACvB,CAAC;IACF,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,UAAU,CAAU,CAAC;AACzD,CAAC"}
|
|
@@ -1,7 +1,40 @@
|
|
|
1
|
-
import type { MessageDoc } from "../
|
|
1
|
+
import type { MessageDoc, StreamArgs } from "../validators.js";
|
|
2
2
|
import type { OptimisticLocalStore } from "convex/browser";
|
|
3
|
-
import type {
|
|
4
|
-
|
|
3
|
+
import type { UIMessage } from "../UIMessages.js";
|
|
4
|
+
import type { FunctionReference, PaginationOptions, PaginationResult } from "convex/server";
|
|
5
|
+
import type { SyncStreamsReturnValue } from "@convex-dev/agent";
|
|
6
|
+
/**
|
|
7
|
+
* Adds a sent message to the end of a list of messages, so it shows up until
|
|
8
|
+
* the message is saved on the server and arrives in the query.
|
|
9
|
+
* It generates a message with fields that match both MessageDoc and UIMessage,
|
|
10
|
+
* for convenience. It will not include any other fields you might have in your
|
|
11
|
+
* regular query, however.
|
|
12
|
+
*
|
|
13
|
+
* @param query The query used to fetch messages, typically with
|
|
14
|
+
* useThreadMessages or useUIMessages.
|
|
15
|
+
* @returns A function that can be used to optimistically send a message.
|
|
16
|
+
* If your mutation takes different arguments than { threadId, prompt }, you can
|
|
17
|
+
* use it as a helper function in your optimistic update:
|
|
18
|
+
* ```ts
|
|
19
|
+
* const sendMessage = useMutation(
|
|
20
|
+
* api.chatStreaming.streamStoryAsynchronously,
|
|
21
|
+
* ).withOptimisticUpdate(
|
|
22
|
+
* (store, args) => {
|
|
23
|
+
* optimisticallySendMessage(api.chatStreaming.listThreadMessages)(store, {
|
|
24
|
+
* threadId:
|
|
25
|
+
* prompt: whatever you would have passed to the mutation,
|
|
26
|
+
* })
|
|
27
|
+
* }
|
|
28
|
+
* );
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function optimisticallySendMessage(query: FunctionReference<"query", "public", {
|
|
32
|
+
threadId: string;
|
|
33
|
+
paginationOpts: PaginationOptions;
|
|
34
|
+
streamArgs?: StreamArgs;
|
|
35
|
+
}, PaginationResult<MessageDoc | UIMessage> & {
|
|
36
|
+
streams?: SyncStreamsReturnValue;
|
|
37
|
+
}>): (store: OptimisticLocalStore, args: {
|
|
5
38
|
threadId: string;
|
|
6
39
|
prompt: string;
|
|
7
40
|
}) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optimisticallySendMessage.d.ts","sourceRoot":"","sources":["../../src/react/optimisticallySendMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"optimisticallySendMessage.d.ts","sourceRoot":"","sources":["../../src/react/optimisticallySendMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,iBAAiB,CACtB,OAAO,EACP,QAAQ,EACR;IACE,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,iBAAiB,CAAC;IAClC,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB,EACD,gBAAgB,CAAC,UAAU,GAAG,SAAS,CAAC,GAAG;IACzC,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC,CACF,GACA,CACD,KAAK,EAAE,oBAAoB,EAC3B,IAAI,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,KACvC,IAAI,CAoCR;AAED,wBAAgB,UAAU,WAWzB"}
|
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
import { insertAtTop } from "convex/react";
|
|
2
|
+
/**
|
|
3
|
+
* Adds a sent message to the end of a list of messages, so it shows up until
|
|
4
|
+
* the message is saved on the server and arrives in the query.
|
|
5
|
+
* It generates a message with fields that match both MessageDoc and UIMessage,
|
|
6
|
+
* for convenience. It will not include any other fields you might have in your
|
|
7
|
+
* regular query, however.
|
|
8
|
+
*
|
|
9
|
+
* @param query The query used to fetch messages, typically with
|
|
10
|
+
* useThreadMessages or useUIMessages.
|
|
11
|
+
* @returns A function that can be used to optimistically send a message.
|
|
12
|
+
* If your mutation takes different arguments than { threadId, prompt }, you can
|
|
13
|
+
* use it as a helper function in your optimistic update:
|
|
14
|
+
* ```ts
|
|
15
|
+
* const sendMessage = useMutation(
|
|
16
|
+
* api.chatStreaming.streamStoryAsynchronously,
|
|
17
|
+
* ).withOptimisticUpdate(
|
|
18
|
+
* (store, args) => {
|
|
19
|
+
* optimisticallySendMessage(api.chatStreaming.listThreadMessages)(store, {
|
|
20
|
+
* threadId:
|
|
21
|
+
* prompt: whatever you would have passed to the mutation,
|
|
22
|
+
* })
|
|
23
|
+
* }
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
2
27
|
export function optimisticallySendMessage(query) {
|
|
3
28
|
return (store, args) => {
|
|
4
29
|
const queries = store.getAllQueries(query);
|
|
@@ -13,19 +38,26 @@ export function optimisticallySendMessage(query) {
|
|
|
13
38
|
}
|
|
14
39
|
}
|
|
15
40
|
const order = maxOrder + 1;
|
|
41
|
+
const stepOrder = 0;
|
|
42
|
+
const id = randomUUID();
|
|
43
|
+
const { prompt, ...rest } = args;
|
|
16
44
|
insertAtTop({
|
|
17
45
|
paginatedQuery: query,
|
|
18
46
|
argsToMatch: { threadId: args.threadId, streamArgs: undefined },
|
|
19
47
|
item: {
|
|
48
|
+
...rest,
|
|
20
49
|
_creationTime: Date.now(),
|
|
21
|
-
_id:
|
|
50
|
+
_id: id,
|
|
51
|
+
id,
|
|
52
|
+
key: `${args.threadId}-${order}-${stepOrder}`,
|
|
22
53
|
order,
|
|
23
|
-
stepOrder
|
|
54
|
+
stepOrder,
|
|
24
55
|
status: "pending",
|
|
25
|
-
threadId: args.threadId,
|
|
26
56
|
tool: false,
|
|
27
|
-
message: { role: "user", content:
|
|
28
|
-
text:
|
|
57
|
+
message: { role: "user", content: prompt },
|
|
58
|
+
parts: [{ type: "text", text: prompt }],
|
|
59
|
+
role: "user",
|
|
60
|
+
text: prompt,
|
|
29
61
|
},
|
|
30
62
|
localQueryStore: store,
|
|
31
63
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optimisticallySendMessage.js","sourceRoot":"","sources":["../../src/react/optimisticallySendMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"optimisticallySendMessage.js","sourceRoot":"","sources":["../../src/react/optimisticallySendMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAW3C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,yBAAyB,CACvC,KAWC;IAKD,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACrB,MAAM,OAAO,GAAG,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CA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ACD,eAAe,EAAE,KAAK;SACvB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,UAAU;IACxB,IACE,OAAO,MAAM,KAAK,WAAW;QAC7B,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU,EACvC,CAAC;QACD,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC;IAC7B,CAAC;IACD,OAAO,CACL,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;QAC3C,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAC5C,CAAC;AACJ,CAAC"}
|
package/dist/react/types.d.ts
CHANGED
|
@@ -1,26 +1,12 @@
|
|
|
1
1
|
import type { BetterOmit, Expand } from "convex-helpers";
|
|
2
|
-
import type { FunctionArgs, FunctionReference
|
|
3
|
-
import type { MessageDoc } from "../client/index.js";
|
|
2
|
+
import type { FunctionArgs, FunctionReference } from "convex/server";
|
|
4
3
|
import type { SyncStreamsReturnValue } from "../client/types.js";
|
|
5
4
|
import type { StreamArgs } from "../validators.js";
|
|
6
|
-
export type
|
|
5
|
+
export type StreamQuery<Args = Record<string, unknown>> = FunctionReference<"query", "public", {
|
|
7
6
|
threadId: string;
|
|
8
|
-
paginationOpts: PaginationOptions;
|
|
9
|
-
/**
|
|
10
|
-
* If { stream: true } is passed, it will also query for stream deltas.
|
|
11
|
-
* In order for this to work, the query must take as an argument streamArgs.
|
|
12
|
-
*/
|
|
13
7
|
streamArgs?: StreamArgs;
|
|
14
|
-
} & Args,
|
|
15
|
-
streams?: SyncStreamsReturnValue;
|
|
16
|
-
}>;
|
|
17
|
-
export type ThreadStreamQuery<Args = Record<string, unknown>, M extends MessageDoc = MessageDoc> = FunctionReference<"query", "public", {
|
|
18
|
-
threadId: string;
|
|
19
|
-
paginationOpts: PaginationOptions;
|
|
20
|
-
streamArgs?: StreamArgs;
|
|
21
|
-
} & Args, PaginationResult<M> & {
|
|
8
|
+
} & Args, {
|
|
22
9
|
streams: SyncStreamsReturnValue;
|
|
23
10
|
}>;
|
|
24
|
-
export type
|
|
25
|
-
export type ThreadMessagesResult<Query extends ThreadQuery<unknown, MessageDoc>> = Query extends ThreadQuery<unknown, infer M> ? M : never;
|
|
11
|
+
export type StreamMessagesArgs<Query extends StreamQuery<unknown>> = Query extends StreamQuery<unknown> ? Expand<BetterOmit<FunctionArgs<Query>, "streamArgs">> : never;
|
|
26
12
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/react/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/react/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACrE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,MAAM,WAAW,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,iBAAiB,CACzE,OAAO,EACP,QAAQ,EACR;IACE,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB,GAAG,IAAI,EACR;IAAE,OAAO,EAAE,sBAAsB,CAAA;CAAE,CACpC,CAAC;AAEF,MAAM,MAAM,kBAAkB,CAAC,KAAK,SAAS,WAAW,CAAC,OAAO,CAAC,IAC/D,KAAK,SAAS,WAAW,CAAC,OAAO,CAAC,GAC9B,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC,CAAC,GACrD,KAAK,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type UIDataTypes, type UITools } from "ai";
|
|
2
|
+
import type { StreamQuery, StreamMessagesArgs } from "./types.js";
|
|
3
|
+
import { type UIMessage } from "../UIMessages.js";
|
|
4
|
+
/**
|
|
5
|
+
* A hook that fetches streaming messages from a thread and converts them to UIMessages
|
|
6
|
+
* using AI SDK's readUIMessageStream.
|
|
7
|
+
* This ONLY returns streaming UIMessages. To get both full and streaming messages,
|
|
8
|
+
* use `useUIMessages`.
|
|
9
|
+
*
|
|
10
|
+
* @param query The query to use to fetch messages.
|
|
11
|
+
* It must take as arguments `{ threadId, paginationOpts, streamArgs }` and
|
|
12
|
+
* return a `streams` object returned from `agent.syncStreams`.
|
|
13
|
+
* @param args The arguments to pass to the query other than `paginationOpts`
|
|
14
|
+
* and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that
|
|
15
|
+
* you want to pass to the query.
|
|
16
|
+
* @returns The streaming UIMessages.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useStreamingUIMessages<METADATA = unknown, DATA_PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, Query extends StreamQuery<any> = StreamQuery<object>>(query: Query, args: StreamMessagesArgs<Query> | "skip", options?: {
|
|
19
|
+
startOrder?: number;
|
|
20
|
+
skipStreamIds?: string[];
|
|
21
|
+
}): UIMessage<METADATA, DATA_PARTS, TOOLS>[] | undefined;
|
|
22
|
+
//# sourceMappingURL=useStreamingUIMessages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStreamingUIMessages.d.ts","sourceRoot":"","sources":["../../src/react/useStreamingUIMessages.ts"],"names":[],"mappings":"AAKA,OAAO,EAGL,KAAK,WAAW,EAEhB,KAAK,OAAO,EACb,MAAM,IAAI,CAAC;AAGZ,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AASlD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,GAAG,OAAO,EAClB,UAAU,SAAS,WAAW,GAAG,WAAW,EAC5C,KAAK,SAAS,OAAO,GAAG,OAAO,EAE/B,KAAK,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,EAEpD,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,kBAAkB,CAAC,KAAK,CAAC,GAAG,MAAM,EACxC,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,GAEA,SAAS,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,GAAG,SAAS,CAiPtD"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { omit } from "convex-helpers";
|
|
3
|
+
import { useQuery } from "convex/react";
|
|
4
|
+
import { useMemo, useState, useEffect, useRef } from "react";
|
|
5
|
+
import { readUIMessageStream, } from "ai";
|
|
6
|
+
import {} from "../UIMessages.js";
|
|
7
|
+
import { blankUIMessage, getParts, deriveUIMessagesFromTextStreamParts, statusFromStreamStatus, } from "../deltas.js";
|
|
8
|
+
import { sorted } from "../shared.js";
|
|
9
|
+
/**
|
|
10
|
+
* A hook that fetches streaming messages from a thread and converts them to UIMessages
|
|
11
|
+
* using AI SDK's readUIMessageStream.
|
|
12
|
+
* This ONLY returns streaming UIMessages. To get both full and streaming messages,
|
|
13
|
+
* use `useUIMessages`.
|
|
14
|
+
*
|
|
15
|
+
* @param query The query to use to fetch messages.
|
|
16
|
+
* It must take as arguments `{ threadId, paginationOpts, streamArgs }` and
|
|
17
|
+
* return a `streams` object returned from `agent.syncStreams`.
|
|
18
|
+
* @param args The arguments to pass to the query other than `paginationOpts`
|
|
19
|
+
* and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that
|
|
20
|
+
* you want to pass to the query.
|
|
21
|
+
* @returns The streaming UIMessages.
|
|
22
|
+
*/
|
|
23
|
+
export function useStreamingUIMessages(query, args, options) {
|
|
24
|
+
const [uiMessages, setUIMessages] = useState({});
|
|
25
|
+
const [streamCursors, setStreamCursors] = useState({});
|
|
26
|
+
const uiMessageStreamControllers = useRef(new Map());
|
|
27
|
+
// Get all the active streams
|
|
28
|
+
const streamList = useQuery(query, args === "skip"
|
|
29
|
+
? args
|
|
30
|
+
: {
|
|
31
|
+
...args,
|
|
32
|
+
streamArgs: {
|
|
33
|
+
kind: "list",
|
|
34
|
+
startOrder: options?.startOrder ?? 0,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
// Get the cursors for all the active streams
|
|
38
|
+
const cursors = useMemo(() => {
|
|
39
|
+
if (!streamList?.streams)
|
|
40
|
+
return [];
|
|
41
|
+
if (streamList.streams.kind !== "list") {
|
|
42
|
+
throw new Error("Expected list streams");
|
|
43
|
+
}
|
|
44
|
+
return streamList.streams.messages
|
|
45
|
+
.filter(({ streamId }) => !options?.skipStreamIds?.includes(streamId))
|
|
46
|
+
.map(({ streamId }) => {
|
|
47
|
+
const cursor = streamCursors[streamId] ?? 0;
|
|
48
|
+
return { streamId, cursor };
|
|
49
|
+
});
|
|
50
|
+
}, [streamList, streamCursors, options?.skipStreamIds]);
|
|
51
|
+
// Get the deltas for all the active streams, if any.
|
|
52
|
+
const cursorQuery = useQuery(query, args === "skip" || !streamList
|
|
53
|
+
? "skip"
|
|
54
|
+
: {
|
|
55
|
+
...args,
|
|
56
|
+
paginationOpts: { cursor: null, numItems: 0 },
|
|
57
|
+
streamArgs: { kind: "deltas", cursors },
|
|
58
|
+
});
|
|
59
|
+
const threadId = args === "skip" ? undefined : args.threadId;
|
|
60
|
+
// Process new deltas and convert to UIMessageChunks, then use readUIMessageStream
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!cursorQuery?.streams?.deltas || !threadId)
|
|
63
|
+
return;
|
|
64
|
+
const deltasByStream = new Map();
|
|
65
|
+
// Group deltas by streamId
|
|
66
|
+
for (const delta of cursorQuery.streams.deltas) {
|
|
67
|
+
if (!deltasByStream.has(delta.streamId)) {
|
|
68
|
+
deltasByStream.set(delta.streamId, []);
|
|
69
|
+
}
|
|
70
|
+
deltasByStream.get(delta.streamId).push(delta);
|
|
71
|
+
}
|
|
72
|
+
// Process each stream's deltas
|
|
73
|
+
for (const [streamId, deltas] of deltasByStream.entries()) {
|
|
74
|
+
const streamMessage = streamList?.streams?.messages.find((m) => m.streamId === streamId);
|
|
75
|
+
if (!streamMessage)
|
|
76
|
+
continue;
|
|
77
|
+
if (streamMessage.format === "UIMessageChunk") {
|
|
78
|
+
setStreamCursors((streamCursors) => {
|
|
79
|
+
const currentCursor = streamCursors[streamId] ?? 0;
|
|
80
|
+
const { parts, cursor } = getParts(deltas, currentCursor);
|
|
81
|
+
const newStreamCursors = cursor === currentCursor
|
|
82
|
+
? streamCursors
|
|
83
|
+
: { ...streamCursors, [streamId]: cursor };
|
|
84
|
+
if (parts.length === 0)
|
|
85
|
+
return newStreamCursors;
|
|
86
|
+
// Get existing message for this stream as starting point
|
|
87
|
+
const existingStream = uiMessageStreamControllers.current.get(streamId);
|
|
88
|
+
if (existingStream) {
|
|
89
|
+
for (const part of parts) {
|
|
90
|
+
existingStream.enqueue(part);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const stream = new ReadableStream({
|
|
95
|
+
start(controller) {
|
|
96
|
+
uiMessageStreamControllers.current.set(streamId, controller);
|
|
97
|
+
for (const part of parts) {
|
|
98
|
+
controller.enqueue(part);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
const initialMessage = blankUIMessage(streamMessage, threadId);
|
|
103
|
+
setUIMessages((prev) => ({
|
|
104
|
+
...prev,
|
|
105
|
+
[streamId]: initialMessage,
|
|
106
|
+
}));
|
|
107
|
+
const messageStream = readUIMessageStream({
|
|
108
|
+
message: initialMessage,
|
|
109
|
+
stream,
|
|
110
|
+
onError: (error) => {
|
|
111
|
+
setUIMessages((prev) => ({
|
|
112
|
+
...prev,
|
|
113
|
+
[streamId]: {
|
|
114
|
+
...prev[streamId],
|
|
115
|
+
status: "failed",
|
|
116
|
+
},
|
|
117
|
+
}));
|
|
118
|
+
console.error(`Error in stream ${streamId}:`, error);
|
|
119
|
+
},
|
|
120
|
+
terminateOnError: false,
|
|
121
|
+
});
|
|
122
|
+
// Process the async iterator
|
|
123
|
+
void (async () => {
|
|
124
|
+
for await (const message of messageStream) {
|
|
125
|
+
message.text = message.parts
|
|
126
|
+
.filter((p) => p.type === "text")
|
|
127
|
+
.map((p) => p.text)
|
|
128
|
+
.join("");
|
|
129
|
+
setUIMessages((prev) =>
|
|
130
|
+
// If we don't have a ui message assume we've aborted.
|
|
131
|
+
prev[streamId]
|
|
132
|
+
? {
|
|
133
|
+
...prev,
|
|
134
|
+
[streamId]: message,
|
|
135
|
+
}
|
|
136
|
+
: prev);
|
|
137
|
+
}
|
|
138
|
+
})().catch((error) => {
|
|
139
|
+
console.error(`Error processing stream ${streamId}:`, error);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return newStreamCursors;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
setUIMessages((uiMessages) => {
|
|
147
|
+
const existingUIMessage = uiMessages[streamId];
|
|
148
|
+
const [[uiMessage], [streamMetadata], changed] = deriveUIMessagesFromTextStreamParts(threadId, [streamMessage], existingUIMessage
|
|
149
|
+
? [
|
|
150
|
+
{
|
|
151
|
+
streamId,
|
|
152
|
+
cursor: streamCursors[streamId] ?? 0,
|
|
153
|
+
message: existingUIMessage,
|
|
154
|
+
},
|
|
155
|
+
]
|
|
156
|
+
: [], deltas);
|
|
157
|
+
if (changed) {
|
|
158
|
+
setStreamCursors((prev) => ({
|
|
159
|
+
...prev,
|
|
160
|
+
[streamId]: streamMetadata.cursor,
|
|
161
|
+
}));
|
|
162
|
+
return {
|
|
163
|
+
...uiMessages,
|
|
164
|
+
[streamId]: uiMessage,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return uiMessages;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}, [cursorQuery, streamCursors, threadId, streamList?.streams?.messages]);
|
|
172
|
+
// Clean up finished streams
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
const activeStreamIds = new Set(streamList?.streams.messages.map((m) => m.streamId) ?? []);
|
|
175
|
+
setUIMessages((prev) => {
|
|
176
|
+
const toRemove = Object.keys(prev).filter((streamId) => !activeStreamIds.has(streamId));
|
|
177
|
+
return toRemove.length ? omit(prev, toRemove) : prev;
|
|
178
|
+
});
|
|
179
|
+
for (const [streamId, controller,] of uiMessageStreamControllers.current.entries()) {
|
|
180
|
+
if (!activeStreamIds.has(streamId)) {
|
|
181
|
+
controller.close();
|
|
182
|
+
uiMessageStreamControllers.current.delete(streamId);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}, [streamList]);
|
|
186
|
+
return useMemo(() => {
|
|
187
|
+
if (!streamList)
|
|
188
|
+
return undefined;
|
|
189
|
+
return sorted(Array.from(Object.entries(uiMessages)
|
|
190
|
+
.map(([streamId, uiMessage]) => {
|
|
191
|
+
const streamMessage = streamList?.streams.messages.find((m) => m.streamId === streamId);
|
|
192
|
+
if (!streamMessage)
|
|
193
|
+
return undefined;
|
|
194
|
+
uiMessage.status = statusFromStreamStatus(streamMessage?.status ?? "finished");
|
|
195
|
+
uiMessage.text = uiMessage.parts
|
|
196
|
+
.filter((p) => p.type === "text")
|
|
197
|
+
.map((p) => p.text)
|
|
198
|
+
.join("");
|
|
199
|
+
return uiMessage;
|
|
200
|
+
})
|
|
201
|
+
.filter((uiMessage) => uiMessage !== undefined)));
|
|
202
|
+
}, [uiMessages, streamList]);
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=useStreamingUIMessages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStreamingUIMessages.js","sourceRoot":"","sources":["../../src/react/useStreamingUIMessages.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC7D,OAAO,EACL,mBAAmB,GAKpB,MAAM,IAAI,CAAC;AAIZ,OAAO,EAAkB,MAAM,kBAAkB,CAAC;AAClD,OAAO,EACL,cAAc,EACd,QAAQ,EACR,mCAAmC,EACnC,sBAAsB,GACvB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,sBAAsB,CAOpC,KAAY,EACZ,IAAwC,EACxC,OAGC;IAGD,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAE1C,EAAE,CAAC,CAAC;IACN,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAChD,EAAE,CACH,CAAC;IACF,MAAM,0BAA0B,GAAG,MAAM,CAEvC,IAAI,GAAG,EAAE,CAAC,CAAC;IAEb,6BAA6B;IAC7B,MAAM,UAAU,GAAG,QAAQ,CACzB,KAAK,EACL,IAAI,KAAK,MAAM;QACb,CAAC,CAAC,IAAI;QACN,CAAC,CAAE;YACC,GAAG,IAAI;YACP,UAAU,EAAE;gBACV,IAAI,EAAE,MAAM;gBACZ,UAAU,EAAE,OAAO,EAAE,UAAU,IAAI,CAAC;aACv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