@assistant-ui/react-ai-sdk 0.10.15 → 0.10.16
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/converters/fromLanguageModelTools.d.ts +7 -0
- package/dist/converters/fromLanguageModelTools.d.ts.map +1 -0
- package/dist/converters/fromLanguageModelTools.js +16 -0
- package/dist/converters/fromLanguageModelTools.js.map +1 -0
- package/dist/converters/index.d.ts +4 -0
- package/dist/converters/index.d.ts.map +1 -0
- package/dist/converters/index.js +10 -0
- package/dist/converters/index.js.map +1 -0
- package/dist/converters/toLanguageModelMessages.d.ts +9 -0
- package/dist/converters/toLanguageModelMessages.d.ts.map +1 -0
- package/dist/converters/toLanguageModelMessages.js +143 -0
- package/dist/converters/toLanguageModelMessages.js.map +1 -0
- package/dist/converters/toLanguageModelTools.d.ts +7 -0
- package/dist/converters/toLanguageModelTools.d.ts.map +1 -0
- package/dist/converters/toLanguageModelTools.js +15 -0
- package/dist/converters/toLanguageModelTools.js.map +1 -0
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.d.ts +9 -0
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.d.ts.map +1 -0
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.js +38 -0
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.js.map +1 -0
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.d.ts +56 -0
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.d.ts.map +1 -0
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.js +65 -0
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.js.map +1 -0
- package/dist/dangerous-in-browser/index.d.ts +2 -0
- package/dist/dangerous-in-browser/index.d.ts.map +1 -0
- package/dist/dangerous-in-browser/index.js +8 -0
- package/dist/dangerous-in-browser/index.js.map +1 -0
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.d.ts +5 -0
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.d.ts.map +1 -0
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.js +21 -0
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.js.map +1 -0
- package/dist/frontendTools.d.ts +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -13
- package/dist/index.js.map +1 -1
- package/dist/useChatRuntime.d.ts +15 -3
- package/dist/useChatRuntime.d.ts.map +1 -1
- package/dist/useChatRuntime.js +99 -5
- package/dist/useChatRuntime.js.map +1 -1
- package/package.json +8 -4
- package/src/converters/fromLanguageModelTools.ts +19 -0
- package/src/converters/index.ts +3 -0
- package/src/converters/toLanguageModelMessages.ts +185 -0
- package/src/converters/toLanguageModelTools.ts +21 -0
- package/src/dangerous-in-browser/DangerousInBrowserAdapter.ts +41 -0
- package/src/dangerous-in-browser/createEdgeRuntimeAPI.ts +145 -0
- package/src/dangerous-in-browser/index.ts +4 -0
- package/src/dangerous-in-browser/useDangerousInBrowserRuntime.ts +26 -0
- package/src/index.ts +2 -8
- package/src/useChatRuntime.ts +156 -12
package/src/useChatRuntime.ts
CHANGED
|
@@ -1,13 +1,157 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { toLanguageModelMessages } from "./converters";
|
|
4
|
+
import {
|
|
5
|
+
AssistantRuntime,
|
|
6
|
+
ChatModelAdapter,
|
|
7
|
+
ChatModelRunOptions,
|
|
8
|
+
INTERNAL,
|
|
9
|
+
LocalRuntimeOptions,
|
|
10
|
+
ThreadMessage,
|
|
11
|
+
Tool,
|
|
12
|
+
useLocalRuntime,
|
|
13
|
+
} from "@assistant-ui/react";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
|
16
|
+
import { JSONSchema7 } from "json-schema";
|
|
17
|
+
import {
|
|
18
|
+
AssistantMessageAccumulator,
|
|
19
|
+
DataStreamDecoder,
|
|
20
|
+
unstable_toolResultStream,
|
|
21
|
+
} from "assistant-stream";
|
|
22
|
+
import { asAsyncIterableStream } from "assistant-stream/utils";
|
|
23
|
+
|
|
24
|
+
const { splitLocalRuntimeOptions } = INTERNAL;
|
|
25
|
+
|
|
26
|
+
type HeadersValue = Record<string, string> | Headers;
|
|
27
|
+
|
|
28
|
+
export type UseChatRuntimeOptions = {
|
|
29
|
+
api: string;
|
|
30
|
+
onResponse?: (response: Response) => void | Promise<void>;
|
|
31
|
+
onFinish?: (message: ThreadMessage) => void;
|
|
32
|
+
onError?: (error: Error) => void;
|
|
33
|
+
onCancel?: () => void;
|
|
34
|
+
credentials?: RequestCredentials;
|
|
35
|
+
headers?: HeadersValue | (() => Promise<HeadersValue>);
|
|
36
|
+
body?: object;
|
|
37
|
+
sendExtraMessageFields?: boolean;
|
|
38
|
+
} & LocalRuntimeOptions;
|
|
39
|
+
|
|
40
|
+
type ChatRuntimeRequestOptions = {
|
|
41
|
+
messages: any[];
|
|
42
|
+
tools: any;
|
|
43
|
+
system?: string | undefined;
|
|
44
|
+
runConfig?: any;
|
|
45
|
+
unstable_assistantMessageId?: string;
|
|
46
|
+
state?: any;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const toAISDKTools = (tools: Record<string, Tool>) => {
|
|
50
|
+
return Object.fromEntries(
|
|
51
|
+
Object.entries(tools).map(([name, tool]) => [
|
|
52
|
+
name,
|
|
53
|
+
{
|
|
54
|
+
...(tool.description ? { description: tool.description } : undefined),
|
|
55
|
+
parameters: (tool.parameters instanceof z.ZodType
|
|
56
|
+
? zodToJsonSchema(tool.parameters)
|
|
57
|
+
: tool.parameters) as JSONSchema7,
|
|
58
|
+
},
|
|
59
|
+
]),
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const getEnabledTools = (tools: Record<string, Tool>) => {
|
|
64
|
+
return Object.fromEntries(
|
|
65
|
+
Object.entries(tools).filter(
|
|
66
|
+
([, tool]) => !tool.disabled && tool.type !== "backend",
|
|
67
|
+
),
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
class ChatRuntimeAdapter implements ChatModelAdapter {
|
|
72
|
+
constructor(
|
|
73
|
+
private options: Omit<UseChatRuntimeOptions, keyof LocalRuntimeOptions>,
|
|
74
|
+
) {}
|
|
75
|
+
|
|
76
|
+
async *run({
|
|
77
|
+
messages,
|
|
78
|
+
runConfig,
|
|
79
|
+
abortSignal,
|
|
80
|
+
context,
|
|
81
|
+
unstable_assistantMessageId,
|
|
82
|
+
unstable_getMessage,
|
|
83
|
+
}: ChatModelRunOptions) {
|
|
84
|
+
const headersValue =
|
|
85
|
+
typeof this.options.headers === "function"
|
|
86
|
+
? await this.options.headers()
|
|
87
|
+
: this.options.headers;
|
|
88
|
+
|
|
89
|
+
abortSignal.addEventListener(
|
|
90
|
+
"abort",
|
|
91
|
+
() => {
|
|
92
|
+
if (!abortSignal.reason?.detach) this.options.onCancel?.();
|
|
93
|
+
},
|
|
94
|
+
{ once: true },
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const headers = new Headers(headersValue);
|
|
98
|
+
headers.set("Content-Type", "application/json");
|
|
99
|
+
|
|
100
|
+
const result = await fetch(this.options.api, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers,
|
|
103
|
+
credentials: this.options.credentials ?? "same-origin",
|
|
104
|
+
body: JSON.stringify({
|
|
105
|
+
system: context.system,
|
|
106
|
+
messages: toLanguageModelMessages(messages, {
|
|
107
|
+
unstable_includeId: this.options.sendExtraMessageFields,
|
|
108
|
+
}) as ChatRuntimeRequestOptions["messages"],
|
|
109
|
+
tools: toAISDKTools(
|
|
110
|
+
getEnabledTools(context.tools ?? {}),
|
|
111
|
+
) as unknown as ChatRuntimeRequestOptions["tools"],
|
|
112
|
+
...(unstable_assistantMessageId ? { unstable_assistantMessageId } : {}),
|
|
113
|
+
runConfig,
|
|
114
|
+
state: unstable_getMessage().metadata.unstable_state || undefined,
|
|
115
|
+
...context.callSettings,
|
|
116
|
+
...context.config,
|
|
117
|
+
...this.options.body,
|
|
118
|
+
} satisfies ChatRuntimeRequestOptions),
|
|
119
|
+
signal: abortSignal,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
await this.options.onResponse?.(result);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
if (!result.ok) {
|
|
126
|
+
throw new Error(`Status ${result.status}: ${await result.text()}`);
|
|
127
|
+
}
|
|
128
|
+
if (!result.body) {
|
|
129
|
+
throw new Error("Response body is null");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const stream = result.body
|
|
133
|
+
.pipeThrough(new DataStreamDecoder())
|
|
134
|
+
.pipeThrough(unstable_toolResultStream(context.tools, abortSignal))
|
|
135
|
+
.pipeThrough(new AssistantMessageAccumulator());
|
|
136
|
+
|
|
137
|
+
yield* asAsyncIterableStream(stream);
|
|
138
|
+
|
|
139
|
+
this.options.onFinish?.(unstable_getMessage());
|
|
140
|
+
} catch (error: unknown) {
|
|
141
|
+
this.options.onError?.(error as Error);
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const useChatRuntime = (
|
|
148
|
+
options: UseChatRuntimeOptions,
|
|
149
|
+
): AssistantRuntime => {
|
|
150
|
+
const { localRuntimeOptions, otherOptions } =
|
|
151
|
+
splitLocalRuntimeOptions(options);
|
|
152
|
+
|
|
153
|
+
return useLocalRuntime(
|
|
154
|
+
new ChatRuntimeAdapter(otherOptions),
|
|
155
|
+
localRuntimeOptions,
|
|
156
|
+
);
|
|
13
157
|
};
|