@convex-dev/agent 0.6.0-alpha.0 → 0.6.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/MIGRATION.md +153 -0
- package/package.json +4 -2
- package/src/UIMessages.ts +135 -0
- package/src/client/approval.test.ts +350 -0
- package/src/client/createTool.ts +50 -52
- package/src/client/index.ts +162 -1
- package/src/client/mockModel.ts +9 -2
- package/src/client/search.test.ts +4 -5
- package/src/client/search.ts +47 -2
- package/src/client/start.ts +41 -24
- package/src/client/streamText.ts +27 -3
- package/src/client/streaming.integration.test.ts +1206 -0
- package/src/client/streaming.ts +41 -12
- package/src/client/types.ts +14 -12
- package/src/component/_generated/component.ts +53 -64
- package/src/component/messages.ts +12 -2
- package/src/deltas.ts +0 -1
- package/src/mapping.test.ts +143 -1
- package/src/mapping.ts +115 -6
- package/src/react/useDeltaStreams.ts +6 -0
- package/dist/UIMessages.d.ts +0 -46
- package/dist/UIMessages.d.ts.map +0 -1
- package/dist/UIMessages.js +0 -546
- package/dist/UIMessages.js.map +0 -1
- package/dist/client/createTool.d.ts +0 -170
- package/dist/client/createTool.d.ts.map +0 -1
- package/dist/client/createTool.js +0 -115
- package/dist/client/createTool.js.map +0 -1
- package/dist/client/defaultComponent.d.ts +0 -11
- package/dist/client/defaultComponent.d.ts.map +0 -1
- package/dist/client/defaultComponent.js +0 -7
- package/dist/client/defaultComponent.js.map +0 -1
- package/dist/client/definePlaygroundAPI.d.ts +0 -1725
- package/dist/client/definePlaygroundAPI.d.ts.map +0 -1
- package/dist/client/definePlaygroundAPI.js +0 -271
- package/dist/client/definePlaygroundAPI.js.map +0 -1
- package/dist/client/files.d.ts +0 -69
- package/dist/client/files.d.ts.map +0 -1
- package/dist/client/files.js +0 -181
- package/dist/client/files.js.map +0 -1
- package/dist/client/index.d.ts +0 -2044
- package/dist/client/index.d.ts.map +0 -1
- package/dist/client/index.js +0 -804
- package/dist/client/index.js.map +0 -1
- package/dist/client/messages.d.ts +0 -461
- package/dist/client/messages.d.ts.map +0 -1
- package/dist/client/messages.js +0 -106
- package/dist/client/messages.js.map +0 -1
- package/dist/client/mockModel.d.ts +0 -42
- package/dist/client/mockModel.d.ts.map +0 -1
- package/dist/client/mockModel.js +0 -175
- package/dist/client/mockModel.js.map +0 -1
- package/dist/client/saveInputMessages.d.ts +0 -20
- package/dist/client/saveInputMessages.d.ts.map +0 -1
- package/dist/client/saveInputMessages.js +0 -58
- package/dist/client/saveInputMessages.js.map +0 -1
- package/dist/client/search.d.ts +0 -493
- package/dist/client/search.d.ts.map +0 -1
- package/dist/client/search.js +0 -425
- package/dist/client/search.js.map +0 -1
- package/dist/client/start.d.ts +0 -84
- package/dist/client/start.d.ts.map +0 -1
- package/dist/client/start.js +0 -171
- package/dist/client/start.js.map +0 -1
- package/dist/client/streamText.d.ts +0 -46
- package/dist/client/streamText.d.ts.map +0 -1
- package/dist/client/streamText.js +0 -93
- package/dist/client/streamText.js.map +0 -1
- package/dist/client/streaming.d.ts +0 -3767
- package/dist/client/streaming.d.ts.map +0 -1
- package/dist/client/streaming.js +0 -294
- package/dist/client/streaming.js.map +0 -1
- package/dist/client/threads.d.ts +0 -46
- package/dist/client/threads.d.ts.map +0 -1
- package/dist/client/threads.js +0 -49
- package/dist/client/threads.js.map +0 -1
- package/dist/client/types.d.ts +0 -460
- package/dist/client/types.d.ts.map +0 -1
- package/dist/client/types.js +0 -2
- package/dist/client/types.js.map +0 -1
- package/dist/client/utils.d.ts +0 -4
- package/dist/client/utils.d.ts.map +0 -1
- package/dist/client/utils.js +0 -21
- package/dist/client/utils.js.map +0 -1
- package/dist/component/_generated/api.d.ts +0 -48
- package/dist/component/_generated/api.d.ts.map +0 -1
- package/dist/component/_generated/api.js +0 -31
- package/dist/component/_generated/api.js.map +0 -1
- package/dist/component/_generated/component.d.ts +0 -3119
- package/dist/component/_generated/component.d.ts.map +0 -1
- package/dist/component/_generated/component.js +0 -11
- package/dist/component/_generated/component.js.map +0 -1
- package/dist/component/_generated/dataModel.d.ts +0 -46
- package/dist/component/_generated/dataModel.d.ts.map +0 -1
- package/dist/component/_generated/dataModel.js +0 -11
- package/dist/component/_generated/dataModel.js.map +0 -1
- package/dist/component/_generated/server.d.ts +0 -121
- package/dist/component/_generated/server.d.ts.map +0 -1
- package/dist/component/_generated/server.js +0 -78
- package/dist/component/_generated/server.js.map +0 -1
- package/dist/component/apiKeys.d.ts +0 -11
- package/dist/component/apiKeys.d.ts.map +0 -1
- package/dist/component/apiKeys.js +0 -69
- package/dist/component/apiKeys.js.map +0 -1
- package/dist/component/convex.config.d.ts +0 -3
- package/dist/component/convex.config.d.ts.map +0 -1
- package/dist/component/convex.config.js +0 -3
- package/dist/component/convex.config.js.map +0 -1
- package/dist/component/files.d.ts +0 -97
- package/dist/component/files.d.ts.map +0 -1
- package/dist/component/files.js +0 -190
- package/dist/component/files.js.map +0 -1
- package/dist/component/messages.d.ts +0 -3850
- package/dist/component/messages.d.ts.map +0 -1
- package/dist/component/messages.js +0 -747
- package/dist/component/messages.js.map +0 -1
- package/dist/component/schema.d.ts +0 -8026
- package/dist/component/schema.d.ts.map +0 -1
- package/dist/component/schema.js +0 -147
- package/dist/component/schema.js.map +0 -1
- package/dist/component/streams.d.ts +0 -128
- package/dist/component/streams.d.ts.map +0 -1
- package/dist/component/streams.js +0 -413
- package/dist/component/streams.js.map +0 -1
- package/dist/component/threads.d.ts +0 -115
- package/dist/component/threads.d.ts.map +0 -1
- package/dist/component/threads.js +0 -208
- package/dist/component/threads.js.map +0 -1
- package/dist/component/users.d.ts +0 -52
- package/dist/component/users.d.ts.map +0 -1
- package/dist/component/users.js +0 -229
- package/dist/component/users.js.map +0 -1
- package/dist/component/vector/index.d.ts +0 -61
- package/dist/component/vector/index.d.ts.map +0 -1
- package/dist/component/vector/index.js +0 -146
- package/dist/component/vector/index.js.map +0 -1
- package/dist/component/vector/tables.d.ts +0 -58
- package/dist/component/vector/tables.d.ts.map +0 -1
- package/dist/component/vector/tables.js +0 -56
- package/dist/component/vector/tables.js.map +0 -1
- package/dist/deltas.d.ts +0 -43
- package/dist/deltas.d.ts.map +0 -1
- package/dist/deltas.js +0 -447
- package/dist/deltas.js.map +0 -1
- package/dist/mapping.d.ts +0 -54
- package/dist/mapping.d.ts.map +0 -1
- package/dist/mapping.js +0 -626
- package/dist/mapping.js.map +0 -1
- package/dist/react/SmoothText.d.ts +0 -5
- package/dist/react/SmoothText.d.ts.map +0 -1
- package/dist/react/SmoothText.js +0 -6
- package/dist/react/SmoothText.js.map +0 -1
- package/dist/react/index.d.ts +0 -25
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js +0 -70
- package/dist/react/index.js.map +0 -1
- package/dist/react/optimisticallySendMessage.d.ts +0 -42
- package/dist/react/optimisticallySendMessage.d.ts.map +0 -1
- package/dist/react/optimisticallySendMessage.js +0 -74
- package/dist/react/optimisticallySendMessage.js.map +0 -1
- package/dist/react/types.d.ts +0 -12
- package/dist/react/types.d.ts.map +0 -1
- package/dist/react/types.js +0 -2
- package/dist/react/types.js.map +0 -1
- package/dist/react/useDeltaStreams.d.ts +0 -10
- package/dist/react/useDeltaStreams.d.ts.map +0 -1
- package/dist/react/useDeltaStreams.js +0 -101
- package/dist/react/useDeltaStreams.js.map +0 -1
- package/dist/react/useSmoothText.d.ts +0 -27
- package/dist/react/useSmoothText.d.ts.map +0 -1
- package/dist/react/useSmoothText.js +0 -68
- package/dist/react/useSmoothText.js.map +0 -1
- package/dist/react/useStreamingUIMessages.d.ts +0 -22
- package/dist/react/useStreamingUIMessages.d.ts.map +0 -1
- package/dist/react/useStreamingUIMessages.js +0 -92
- package/dist/react/useStreamingUIMessages.js.map +0 -1
- package/dist/react/useThreadMessages.d.ts +0 -104
- package/dist/react/useThreadMessages.d.ts.map +0 -1
- package/dist/react/useThreadMessages.js +0 -148
- package/dist/react/useThreadMessages.js.map +0 -1
- package/dist/react/useUIMessages.d.ts +0 -96
- package/dist/react/useUIMessages.d.ts.map +0 -1
- package/dist/react/useUIMessages.js +0 -108
- package/dist/react/useUIMessages.js.map +0 -1
- package/dist/shared.d.ts +0 -26
- package/dist/shared.d.ts.map +0 -1
- package/dist/shared.js +0 -67
- package/dist/shared.js.map +0 -1
- package/dist/validators.d.ts +0 -24516
- package/dist/validators.d.ts.map +0 -1
- package/dist/validators.js +0 -475
- package/dist/validators.js.map +0 -1
package/dist/react/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export { toUIMessages, type UIMessage } from "../UIMessages.js";
|
|
2
|
-
export { optimisticallySendMessage } from "./optimisticallySendMessage.js";
|
|
3
|
-
export { useSmoothText } from "./useSmoothText.js";
|
|
4
|
-
export { SmoothText } from "./SmoothText.js";
|
|
5
|
-
export { type ThreadMessagesQuery, useThreadMessages, useStreamingThreadMessages, } from "./useThreadMessages.js";
|
|
6
|
-
export { type UIMessagesQuery, useUIMessages } from "./useUIMessages.js";
|
|
7
|
-
export { useStreamingUIMessages } from "./useStreamingUIMessages.js";
|
|
8
|
-
/**
|
|
9
|
-
* @deprecated use useThreadMessages or useStreamingThreadMessages instead
|
|
10
|
-
* Use this hook to stream text from a server action, using the
|
|
11
|
-
* toTextStreamResponse or equivalent HTTP streaming endpoint returning text.
|
|
12
|
-
* @param url The URL of the server action to stream text from.
|
|
13
|
-
* e.g. https://....convex.site/yourendpoint
|
|
14
|
-
* @param threadId The ID of the thread to stream text from.
|
|
15
|
-
* @param token The auth token to use for the request.
|
|
16
|
-
* e.g. useAuthToken() from @convex-dev/auth/react
|
|
17
|
-
* @returns A tuple containing the {text, loading, error} and a function to call the endpoint
|
|
18
|
-
* with a given prompt, passing up { prompt, threadId } as the body in JSON.
|
|
19
|
-
*/
|
|
20
|
-
export declare function useStreamingText(url: string, threadId: string | null, token?: string): readonly [{
|
|
21
|
-
readonly text: string;
|
|
22
|
-
readonly loading: boolean;
|
|
23
|
-
readonly error: Error | null;
|
|
24
|
-
}, (prompt: string) => Promise<void>];
|
|
25
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,KAAK,SAAS,EAAE,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,EACL,KAAK,mBAAmB,EACxB,iBAAiB,EACjB,0BAA0B,GAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,KAAK,eAAe,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAErE;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,KAAK,CAAC,EAAE,MAAM;;;;YAOS,MAAM,oBA2C9B"}
|
package/dist/react/index.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useMemo, useState } from "react";
|
|
3
|
-
export { toUIMessages } from "../UIMessages.js";
|
|
4
|
-
export { optimisticallySendMessage } from "./optimisticallySendMessage.js";
|
|
5
|
-
export { useSmoothText } from "./useSmoothText.js";
|
|
6
|
-
export { SmoothText } from "./SmoothText.js";
|
|
7
|
-
export { useThreadMessages, useStreamingThreadMessages, } from "./useThreadMessages.js";
|
|
8
|
-
export { useUIMessages } from "./useUIMessages.js";
|
|
9
|
-
export { useStreamingUIMessages } from "./useStreamingUIMessages.js";
|
|
10
|
-
/**
|
|
11
|
-
* @deprecated use useThreadMessages or useStreamingThreadMessages instead
|
|
12
|
-
* Use this hook to stream text from a server action, using the
|
|
13
|
-
* toTextStreamResponse or equivalent HTTP streaming endpoint returning text.
|
|
14
|
-
* @param url The URL of the server action to stream text from.
|
|
15
|
-
* e.g. https://....convex.site/yourendpoint
|
|
16
|
-
* @param threadId The ID of the thread to stream text from.
|
|
17
|
-
* @param token The auth token to use for the request.
|
|
18
|
-
* e.g. useAuthToken() from @convex-dev/auth/react
|
|
19
|
-
* @returns A tuple containing the {text, loading, error} and a function to call the endpoint
|
|
20
|
-
* with a given prompt, passing up { prompt, threadId } as the body in JSON.
|
|
21
|
-
*/
|
|
22
|
-
export function useStreamingText(url, threadId, token) {
|
|
23
|
-
const [text, setText] = useState("");
|
|
24
|
-
const [loading, setLoading] = useState(false);
|
|
25
|
-
const [error, setError] = useState(null);
|
|
26
|
-
const readStream = useMemo(() => async (prompt) => {
|
|
27
|
-
if (!threadId)
|
|
28
|
-
return;
|
|
29
|
-
try {
|
|
30
|
-
setText("");
|
|
31
|
-
setLoading(true);
|
|
32
|
-
setError(null);
|
|
33
|
-
const response = await fetch(url, {
|
|
34
|
-
method: "POST",
|
|
35
|
-
headers: {
|
|
36
|
-
"Content-Type": "application/json",
|
|
37
|
-
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
38
|
-
},
|
|
39
|
-
body: JSON.stringify({ prompt, threadId }),
|
|
40
|
-
});
|
|
41
|
-
if (!response.ok) {
|
|
42
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
43
|
-
}
|
|
44
|
-
if (!response.body) {
|
|
45
|
-
throw new Error("No body");
|
|
46
|
-
}
|
|
47
|
-
const reader = response.body.getReader();
|
|
48
|
-
const decoder = new TextDecoder();
|
|
49
|
-
let accumulatedText = "";
|
|
50
|
-
while (true) {
|
|
51
|
-
const { done, value } = await reader.read();
|
|
52
|
-
if (done) {
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
accumulatedText += decoder.decode(value);
|
|
56
|
-
setText(accumulatedText);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
if (e instanceof Error && e.name !== "AbortError") {
|
|
61
|
-
setError(e);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
finally {
|
|
65
|
-
setLoading(false);
|
|
66
|
-
}
|
|
67
|
-
}, [threadId, token, url]);
|
|
68
|
-
return [{ text, loading, error }, readStream];
|
|
69
|
-
}
|
|
70
|
-
//# sourceMappingURL=index.js.map
|
package/dist/react/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,CACxB,GAAG,EAAE,CAAC,KAAK,EAAE,MAAc,EAAE,EAAE;QAC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ACX,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,42 +0,0 @@
|
|
|
1
|
-
import type { MessageDoc, StreamArgs } from "../validators.js";
|
|
2
|
-
import type { OptimisticLocalStore } from "convex/browser";
|
|
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: {
|
|
38
|
-
threadId: string;
|
|
39
|
-
prompt: string;
|
|
40
|
-
}) => void;
|
|
41
|
-
export declare function randomUUID(): string;
|
|
42
|
-
//# sourceMappingURL=optimisticallySendMessage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,74 +0,0 @@
|
|
|
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
|
-
*/
|
|
27
|
-
export function optimisticallySendMessage(query) {
|
|
28
|
-
return (store, args) => {
|
|
29
|
-
const queries = store.getAllQueries(query);
|
|
30
|
-
let maxOrder = -1;
|
|
31
|
-
for (const q of queries) {
|
|
32
|
-
if (q.args?.threadId !== args.threadId)
|
|
33
|
-
continue;
|
|
34
|
-
if (q.args.streamArgs)
|
|
35
|
-
continue;
|
|
36
|
-
for (const m of q.value?.page ?? []) {
|
|
37
|
-
maxOrder = Math.max(maxOrder, m.order);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const order = maxOrder + 1;
|
|
41
|
-
const stepOrder = 0;
|
|
42
|
-
const id = randomUUID();
|
|
43
|
-
const { prompt, ...rest } = args;
|
|
44
|
-
insertAtTop({
|
|
45
|
-
paginatedQuery: query,
|
|
46
|
-
argsToMatch: { threadId: args.threadId, streamArgs: undefined },
|
|
47
|
-
item: {
|
|
48
|
-
...rest,
|
|
49
|
-
_creationTime: Date.now(),
|
|
50
|
-
_id: id,
|
|
51
|
-
id,
|
|
52
|
-
key: `${args.threadId}-${order}-${stepOrder}`,
|
|
53
|
-
order,
|
|
54
|
-
stepOrder,
|
|
55
|
-
status: "pending",
|
|
56
|
-
tool: false,
|
|
57
|
-
message: { role: "user", content: prompt },
|
|
58
|
-
parts: [{ type: "text", text: prompt }],
|
|
59
|
-
role: "user",
|
|
60
|
-
text: prompt,
|
|
61
|
-
},
|
|
62
|
-
localQueryStore: store,
|
|
63
|
-
});
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
export function randomUUID() {
|
|
67
|
-
if (typeof crypto !== "undefined" &&
|
|
68
|
-
typeof crypto.randomUUID === "function") {
|
|
69
|
-
return crypto.randomUUID();
|
|
70
|
-
}
|
|
71
|
-
return (Math.random().toString(36).substring(2, 15) +
|
|
72
|
-
Math.random().toString(36).substring(2, 15));
|
|
73
|
-
}
|
|
74
|
-
//# sourceMappingURL=optimisticallySendMessage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,GA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AAE,MAAM;gBACZ,IAAI,EAAE,MAAM;aACb;YACD,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
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { BetterOmit, Expand } from "convex-helpers";
|
|
2
|
-
import type { FunctionArgs, FunctionReference } from "convex/server";
|
|
3
|
-
import type { SyncStreamsReturnValue } from "../client/types.js";
|
|
4
|
-
import type { StreamArgs } from "../validators.js";
|
|
5
|
-
export type StreamQuery<Args = Record<string, unknown>> = FunctionReference<"query", "public", {
|
|
6
|
-
threadId: string;
|
|
7
|
-
streamArgs?: StreamArgs;
|
|
8
|
-
} & Args, {
|
|
9
|
-
streams: SyncStreamsReturnValue;
|
|
10
|
-
}>;
|
|
11
|
-
export type StreamQueryArgs<Query extends StreamQuery<unknown>> = Query extends StreamQuery<unknown> ? Expand<BetterOmit<FunctionArgs<Query>, "streamArgs">> : never;
|
|
12
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,eAAe,CAAC,KAAK,SAAS,WAAW,CAAC,OAAO,CAAC,IAC5D,KAAK,SAAS,WAAW,CAAC,OAAO,CAAC,GAC9B,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC,CAAC,GACrD,KAAK,CAAC"}
|
package/dist/react/types.js
DELETED
package/dist/react/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/react/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { StreamQuery, StreamQueryArgs } from "./types.js";
|
|
2
|
-
import type { StreamDelta, StreamMessage } from "../validators.js";
|
|
3
|
-
export declare function useDeltaStreams<Query extends StreamQuery<any> = StreamQuery<object>>(query: Query, args: StreamQueryArgs<Query> | "skip", options?: {
|
|
4
|
-
startOrder?: number;
|
|
5
|
-
skipStreamIds?: string[];
|
|
6
|
-
}): {
|
|
7
|
-
streamMessage: StreamMessage;
|
|
8
|
-
deltas: StreamDelta[];
|
|
9
|
-
}[] | undefined;
|
|
10
|
-
//# sourceMappingURL=useDeltaStreams.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDeltaStreams.d.ts","sourceRoot":"","sources":["../../src/react/useDeltaStreams.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAG/D,OAAO,KAAK,EAAc,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAM/E,wBAAgB,eAAe,CAC7B,KAAK,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,EAEpD,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,eAAe,CAAC,KAAK,CAAC,GAAG,MAAM,EACrC,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,GACA;IAAE,aAAa,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,WAAW,EAAE,CAAA;CAAE,EAAE,GAAG,SAAS,CAqIvE"}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { sorted } from "../shared.js";
|
|
3
|
-
import { useQuery } from "convex/react";
|
|
4
|
-
import { useState } from "react";
|
|
5
|
-
import { assert } from "convex-helpers";
|
|
6
|
-
export function useDeltaStreams(query, args, options) {
|
|
7
|
-
// We hold onto and modify state directly to avoid re-running unnecessarily.
|
|
8
|
-
const [state] = useState({
|
|
9
|
-
startOrder: options?.startOrder ?? 0,
|
|
10
|
-
deltaStreams: undefined,
|
|
11
|
-
threadId: args === "skip" ? undefined : args.threadId,
|
|
12
|
-
});
|
|
13
|
-
const [cursors, setCursors] = useState({});
|
|
14
|
-
if (args !== "skip" && state.threadId !== args.threadId) {
|
|
15
|
-
state.threadId = args.threadId;
|
|
16
|
-
state.deltaStreams = undefined;
|
|
17
|
-
state.startOrder = options?.startOrder ?? 0;
|
|
18
|
-
setCursors({});
|
|
19
|
-
}
|
|
20
|
-
if (state.deltaStreams?.length ||
|
|
21
|
-
(options?.startOrder && options.startOrder < state.startOrder)) {
|
|
22
|
-
const cacheFriendlyStartOrder = options?.startOrder
|
|
23
|
-
? // round down to the nearest 10 for some cache benefits
|
|
24
|
-
options.startOrder - (options.startOrder % 10)
|
|
25
|
-
: 0;
|
|
26
|
-
if (cacheFriendlyStartOrder !== state.startOrder) {
|
|
27
|
-
state.startOrder = cacheFriendlyStartOrder;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
// Get all the active streams
|
|
31
|
-
const streamList = useQuery(query, args === "skip"
|
|
32
|
-
? args
|
|
33
|
-
: {
|
|
34
|
-
...args,
|
|
35
|
-
streamArgs: {
|
|
36
|
-
kind: "list",
|
|
37
|
-
startOrder: state.startOrder,
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
const streamMessages = args === "skip"
|
|
41
|
-
? undefined
|
|
42
|
-
: !streamList
|
|
43
|
-
? state.deltaStreams?.map(({ streamMessage }) => streamMessage)
|
|
44
|
-
: sorted(streamList.streams.messages.filter(({ streamId, order }) => !options?.skipStreamIds?.includes(streamId) &&
|
|
45
|
-
(!options?.startOrder || order >= options.startOrder)));
|
|
46
|
-
// Get the deltas for all the active streams, if any.
|
|
47
|
-
const cursorQuery = useQuery(query, args === "skip" || !streamMessages?.length
|
|
48
|
-
? "skip"
|
|
49
|
-
: {
|
|
50
|
-
...args,
|
|
51
|
-
streamArgs: {
|
|
52
|
-
kind: "deltas",
|
|
53
|
-
cursors: streamMessages.map(({ streamId }) => ({
|
|
54
|
-
streamId,
|
|
55
|
-
cursor: cursors[streamId] ?? 0,
|
|
56
|
-
})),
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
const newDeltas = cursorQuery?.streams.deltas;
|
|
60
|
-
if (newDeltas?.length && streamMessages) {
|
|
61
|
-
const newDeltasByStreamId = new Map();
|
|
62
|
-
for (const delta of newDeltas) {
|
|
63
|
-
const oldCursor = cursors[delta.streamId];
|
|
64
|
-
if (oldCursor && delta.start < oldCursor)
|
|
65
|
-
continue;
|
|
66
|
-
const existing = newDeltasByStreamId.get(delta.streamId);
|
|
67
|
-
if (existing) {
|
|
68
|
-
const previousEnd = existing.at(-1).end;
|
|
69
|
-
assert(previousEnd === delta.start, `Gap found in deltas for ${delta.streamId} jumping to ${delta.start} from ${previousEnd}`);
|
|
70
|
-
existing.push(delta);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
assert(!oldCursor || oldCursor === delta.start, `Gap found - first delta after ${oldCursor} is ${delta.start} for stream ${delta.streamId}`);
|
|
74
|
-
newDeltasByStreamId.set(delta.streamId, [delta]);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const newCursors = {};
|
|
78
|
-
for (const { streamId } of streamMessages) {
|
|
79
|
-
const cursor = newDeltasByStreamId.get(streamId)?.at(-1)?.end ?? cursors[streamId];
|
|
80
|
-
if (cursor !== undefined) {
|
|
81
|
-
newCursors[streamId] = cursor;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
setCursors(newCursors);
|
|
85
|
-
// we defensively create a new object so object identity matches contents
|
|
86
|
-
state.deltaStreams = streamMessages.map((streamMessage) => {
|
|
87
|
-
const streamId = streamMessage.streamId;
|
|
88
|
-
const old = state.deltaStreams?.find((ds) => ds.streamMessage.streamId === streamId);
|
|
89
|
-
const newDeltas = newDeltasByStreamId.get(streamId);
|
|
90
|
-
if (!newDeltas && streamMessage === old?.streamMessage) {
|
|
91
|
-
return old;
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
streamMessage,
|
|
95
|
-
deltas: [...(old?.deltas ?? []), ...(newDeltas ?? [])],
|
|
96
|
-
};
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
return state.deltaStreams;
|
|
100
|
-
}
|
|
101
|
-
//# sourceMappingURL=useDeltaStreams.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDeltaStreams.js","sourceRoot":"","sources":["../../src/react/useDeltaStreams.ts"],"names":[],"mappings":"AAA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,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC;YACtE,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,UAAU,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC;YAChC,CAAC;QACH,CAAC;QACD,UAAU,CAAC,UAAU,CAAC,CAAC;QAEvB,yEAAyE;QACzE,KAAK,CAAC,YAAY,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,aAAa,EAAE,EAAE;YACxD,MAAM,QAAQ,GAAG,aAAa,CAAC,QAAQ,CAAC;YACxC,MAAM,GAAG,GAAG,KAAK,CAAC,YAAY,EAAE,IAAI,CAClC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,aAAa,CAAC,QAAQ,KAAK,QAAQ,CAC/C,CAAC;YACF,MAAM,SAAS,GAAG,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YACpD,IAAI,CAAC,SAAS,IAAI,aAAa,KAAK,GAAG,EAAE,aAAa,EAAE,CAAC;gBACvD,OAAO,GAAG,CAAC;YACb,CAAC;YACD,OAAO;gBACL,aAAa;gBACb,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC;aACvD,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC,YAAY,CAAC;AAC5B,CAAC"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export type SmoothTextOptions = {
|
|
2
|
-
/**
|
|
3
|
-
* The number of characters to display per second.
|
|
4
|
-
*/
|
|
5
|
-
charsPerSec?: number;
|
|
6
|
-
/**
|
|
7
|
-
* Whether to initially start streaming.
|
|
8
|
-
* If this later turns to false, it'll continue streaming.
|
|
9
|
-
* This will start streaming the first value it sees.
|
|
10
|
-
*/
|
|
11
|
-
startStreaming?: boolean;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* A hook that smoothly displays text as it is streamed.
|
|
15
|
-
*
|
|
16
|
-
* @param text The text to display. Pass in the full text each time.
|
|
17
|
-
* @param charsPerSec The number of characters to display per second.
|
|
18
|
-
* @returns A tuple of the visible text and the state of the smooth text,
|
|
19
|
-
* including the current cursor position and whether it's still streaming.
|
|
20
|
-
* This allows you to decide if it's too far behind and you want to adjust
|
|
21
|
-
* the charsPerSec or just prefer the full text.
|
|
22
|
-
*/
|
|
23
|
-
export declare function useSmoothText(text: string, { charsPerSec, startStreaming, }?: SmoothTextOptions): [string, {
|
|
24
|
-
cursor: number;
|
|
25
|
-
isStreaming: boolean;
|
|
26
|
-
}];
|
|
27
|
-
//# sourceMappingURL=useSmoothText.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSmoothText.d.ts","sourceRoot":"","sources":["../../src/react/useSmoothText.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AACF;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,EACE,WAAmC,EACnC,cAAsB,GACvB,GAAE,iBAAsB,GACxB,CAAC,MAAM,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,CAAC,CAwEpD"}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from "react";
|
|
2
|
-
const FPS = 20;
|
|
3
|
-
const MS_PER_FRAME = 1000 / FPS;
|
|
4
|
-
const INITIAL_CHARS_PER_SEC = 128;
|
|
5
|
-
/**
|
|
6
|
-
* A hook that smoothly displays text as it is streamed.
|
|
7
|
-
*
|
|
8
|
-
* @param text The text to display. Pass in the full text each time.
|
|
9
|
-
* @param charsPerSec The number of characters to display per second.
|
|
10
|
-
* @returns A tuple of the visible text and the state of the smooth text,
|
|
11
|
-
* including the current cursor position and whether it's still streaming.
|
|
12
|
-
* This allows you to decide if it's too far behind and you want to adjust
|
|
13
|
-
* the charsPerSec or just prefer the full text.
|
|
14
|
-
*/
|
|
15
|
-
export function useSmoothText(text, { charsPerSec = INITIAL_CHARS_PER_SEC, startStreaming = false, } = {}) {
|
|
16
|
-
const [visibleText, setVisibleText] = useState(startStreaming ? "" : text || "");
|
|
17
|
-
const smoothState = useRef({
|
|
18
|
-
tick: Date.now(),
|
|
19
|
-
cursor: visibleText.length,
|
|
20
|
-
lastUpdate: Date.now(),
|
|
21
|
-
lastUpdateLength: text.length,
|
|
22
|
-
charsPerMs: charsPerSec / 1000,
|
|
23
|
-
initial: true,
|
|
24
|
-
});
|
|
25
|
-
const isStreaming = smoothState.current.cursor < text.length;
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
if (!isStreaming) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (smoothState.current.lastUpdateLength !== text.length) {
|
|
31
|
-
const timeSinceLastUpdate = Date.now() - smoothState.current.lastUpdate;
|
|
32
|
-
const latestCharsPerMs = (text.length - smoothState.current.lastUpdateLength) /
|
|
33
|
-
timeSinceLastUpdate;
|
|
34
|
-
// Is the rate increasing?
|
|
35
|
-
const rateError = latestCharsPerMs - smoothState.current.charsPerMs;
|
|
36
|
-
// Is our visible text falling behind what it could show?
|
|
37
|
-
const charLag = smoothState.current.lastUpdateLength - smoothState.current.cursor;
|
|
38
|
-
const lagRate = charLag / timeSinceLastUpdate;
|
|
39
|
-
const charsPerMs = latestCharsPerMs +
|
|
40
|
-
(smoothState.current.initial
|
|
41
|
-
? 0
|
|
42
|
-
: Math.max(0, (rateError + lagRate) / 2));
|
|
43
|
-
smoothState.current.initial = false;
|
|
44
|
-
// Smooth out the charsPerSec by weighting it with the previous value.
|
|
45
|
-
smoothState.current.charsPerMs = Math.min((2 * charsPerMs + smoothState.current.charsPerMs) / 3, smoothState.current.charsPerMs * 2);
|
|
46
|
-
}
|
|
47
|
-
smoothState.current.tick = Math.max(smoothState.current.tick, Date.now() - MS_PER_FRAME);
|
|
48
|
-
smoothState.current.lastUpdate = Date.now();
|
|
49
|
-
smoothState.current.lastUpdateLength = text.length;
|
|
50
|
-
function update() {
|
|
51
|
-
if (smoothState.current.cursor >= text.length) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const now = Date.now();
|
|
55
|
-
const timeSinceLastUpdate = now - smoothState.current.tick;
|
|
56
|
-
const charsSinceLastUpdate = Math.floor(timeSinceLastUpdate * smoothState.current.charsPerMs);
|
|
57
|
-
const chars = Math.min(charsSinceLastUpdate, text.length - smoothState.current.cursor);
|
|
58
|
-
smoothState.current.cursor += chars;
|
|
59
|
-
smoothState.current.tick += chars / smoothState.current.charsPerMs;
|
|
60
|
-
setVisibleText(text.slice(0, smoothState.current.cursor));
|
|
61
|
-
}
|
|
62
|
-
update();
|
|
63
|
-
const interval = setInterval(update, MS_PER_FRAME);
|
|
64
|
-
return () => clearInterval(interval);
|
|
65
|
-
}, [text, isStreaming, charsPerSec]);
|
|
66
|
-
return [visibleText, { cursor: smoothState.current.cursor, isStreaming }];
|
|
67
|
-
}
|
|
68
|
-
//# sourceMappingURL=useSmoothText.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSmoothText.js","sourceRoot":"","sources":["../../src/react/useSmoothText.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEpD,MAAM,GAAG,GAAG,EAAE,CAAC;AACf,MAAM,YAAY,GAAG,IAAI,GAAG,GAAG,CAAC;AAChC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAclC;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,EACE,WAAW,GA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AW,CAAC,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC;YACpC,WAAW,CAAC,OAAO,CAAC,IAAI,IAAI,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC;YACnE,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;QAC5D,CAAC;QACD,MAAM,EAAE,CAAC;QACT,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACnD,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IACvC,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC;IAErC,OAAO,CAAC,WAAW,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC;AAC5E,CAAC"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { type UIDataTypes, type UITools } from "ai";
|
|
2
|
-
import type { StreamQuery, StreamQueryArgs } 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: StreamQueryArgs<Query> | "skip", options?: {
|
|
19
|
-
startOrder?: number;
|
|
20
|
-
skipStreamIds?: string[];
|
|
21
|
-
}): UIMessage<METADATA, DATA_PARTS, TOOLS>[] | undefined;
|
|
22
|
-
//# sourceMappingURL=useStreamingUIMessages.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useStreamingUIMessages.d.ts","sourceRoot":"","sources":["../../src/react/useStreamingUIMessages.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,WAAW,EAAuB,KAAK,OAAO,EAAE,MAAM,IAAI,CAAC;AACzE,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/D,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAiBlD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,GAAG,OAAO,EAClB,UAAU,SAAS,WAAW,GAAG,WAAW,EAC5C,KAAK,SAAS,OAAO,GAAG,OAAO,EAC/B,KAAK,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,EAEpD,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,eAAe,CAAC,KAAK,CAAC,GAAG,MAAM,EACrC,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,CA8FtD"}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useMemo, useState, useEffect } from "react";
|
|
3
|
-
import {} from "ai";
|
|
4
|
-
import {} from "../UIMessages.js";
|
|
5
|
-
import { blankUIMessage, getParts, updateFromUIMessageChunks, deriveUIMessagesFromTextStreamParts, } from "../deltas.js";
|
|
6
|
-
import { useDeltaStreams } from "./useDeltaStreams.js";
|
|
7
|
-
// Polyfill structuredClone to support readUIMessageStream on ReactNative
|
|
8
|
-
if (!("structuredClone" in globalThis)) {
|
|
9
|
-
void import("@ungap/structured-clone").then(({ default: structuredClone }) => (globalThis.structuredClone = structuredClone));
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* A hook that fetches streaming messages from a thread and converts them to UIMessages
|
|
13
|
-
* using AI SDK's readUIMessageStream.
|
|
14
|
-
* This ONLY returns streaming UIMessages. To get both full and streaming messages,
|
|
15
|
-
* use `useUIMessages`.
|
|
16
|
-
*
|
|
17
|
-
* @param query The query to use to fetch messages.
|
|
18
|
-
* It must take as arguments `{ threadId, paginationOpts, streamArgs }` and
|
|
19
|
-
* return a `streams` object returned from `agent.syncStreams`.
|
|
20
|
-
* @param args The arguments to pass to the query other than `paginationOpts`
|
|
21
|
-
* and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that
|
|
22
|
-
* you want to pass to the query.
|
|
23
|
-
* @returns The streaming UIMessages.
|
|
24
|
-
*/
|
|
25
|
-
export function useStreamingUIMessages(query, args, options) {
|
|
26
|
-
const [messageState, setMessageState] = useState({});
|
|
27
|
-
const streams = useDeltaStreams(query, args, options);
|
|
28
|
-
const threadId = args === "skip" ? undefined : args.threadId;
|
|
29
|
-
useEffect(() => {
|
|
30
|
-
if (!streams)
|
|
31
|
-
return;
|
|
32
|
-
// return if there are no new deltas beyond the cursors
|
|
33
|
-
let noNewDeltas = true;
|
|
34
|
-
for (const stream of streams) {
|
|
35
|
-
const lastDelta = stream.deltas.at(-1);
|
|
36
|
-
const cursor = messageState[stream.streamMessage.streamId]?.cursor;
|
|
37
|
-
if (!cursor) {
|
|
38
|
-
noNewDeltas = false;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
if (lastDelta && lastDelta.start >= cursor) {
|
|
42
|
-
noNewDeltas = false;
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (noNewDeltas) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const abortController = new AbortController();
|
|
50
|
-
void (async () => {
|
|
51
|
-
const newMessageState = Object.fromEntries(await Promise.all(streams.map(async ({ deltas, streamMessage }) => {
|
|
52
|
-
const { parts, cursor } = getParts(deltas, 0);
|
|
53
|
-
if (streamMessage.format === "UIMessageChunk") {
|
|
54
|
-
// Unfortunately this can't handle resuming from a UIMessage and
|
|
55
|
-
// adding more chunks, so we re-create it from scratch each time.
|
|
56
|
-
const uiMessage = await updateFromUIMessageChunks(blankUIMessage(streamMessage, threadId), parts);
|
|
57
|
-
return [
|
|
58
|
-
streamMessage.streamId,
|
|
59
|
-
{
|
|
60
|
-
uiMessage,
|
|
61
|
-
cursor,
|
|
62
|
-
},
|
|
63
|
-
];
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
const [uiMessages] = deriveUIMessagesFromTextStreamParts(threadId, [streamMessage], [], deltas);
|
|
67
|
-
return [
|
|
68
|
-
streamMessage.streamId,
|
|
69
|
-
{
|
|
70
|
-
uiMessage: uiMessages[0],
|
|
71
|
-
cursor,
|
|
72
|
-
},
|
|
73
|
-
];
|
|
74
|
-
}
|
|
75
|
-
})));
|
|
76
|
-
if (abortController.signal.aborted)
|
|
77
|
-
return;
|
|
78
|
-
setMessageState(newMessageState);
|
|
79
|
-
})();
|
|
80
|
-
return () => {
|
|
81
|
-
abortController.abort();
|
|
82
|
-
};
|
|
83
|
-
}, [messageState, streams, threadId]);
|
|
84
|
-
return useMemo(() => {
|
|
85
|
-
if (!streams)
|
|
86
|
-
return undefined;
|
|
87
|
-
return streams
|
|
88
|
-
.map(({ streamMessage }) => messageState[streamMessage.streamId]?.uiMessage)
|
|
89
|
-
.filter((uiMessage) => uiMessage !== undefined);
|
|
90
|
-
}, [messageState, streams]);
|
|
91
|
-
}
|
|
92
|
-
//# sourceMappingURL=useStreamingUIMessages.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useStreamingUIMessages.js","sourceRoot":"","sources":["../../src/react/useStreamingUIMessages.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACrD,OAAO,EAAuD,MAAM,IAAI,CAAC;AAEzE,OAAO,EAAkB,MAAM,kBAAkB,CAAC;AAClD,OAAO,EACL,cAAc,EACd,QAAQ,EACR,yBAAyB,EACzB,mCAAmC,GACpC,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,yEAAyE;AACzE,IAAI,CAAC,CAAC,iBAAiB,IAAI,UAAU,CAAC,EAAE,CAAC;IACvC,KAAK,MAAM,CAAC,yBAAgC,CAAC,CAAC,IAAI,CAChD,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,EAAE,EAAE,CAC/B,CAAC,UAAU,CAAC,eAAe,GAAG,eAAe,CAAC,CACjD,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,sBAAsB,CAMpC,KAAY,EACZ,IAAqC,EACrC,OAGC;IAGD,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,CAQ9C,EAAE,CAAC,CAAC;IAEN,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IAEtD,MAAM,QAAQ,GAAG,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC;IAE7D,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,uDAAuD;QACvD,IAAI,WAAW,GAAG,IAAI,CAAC;QACvB,K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