@assistant-ui/react-ai-sdk 0.10.16 → 0.11.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/frontendTools.js +1 -1
- package/dist/frontendTools.js.map +1 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -5
- package/dist/index.js.map +1 -1
- package/dist/ui/getVercelAIMessages.d.ts +2 -2
- package/dist/ui/getVercelAIMessages.d.ts.map +1 -1
- package/dist/ui/getVercelAIMessages.js.map +1 -1
- package/dist/ui/index.d.ts +1 -4
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +2 -8
- package/dist/ui/index.js.map +1 -1
- package/dist/ui/use-chat/useAISDKRuntime.d.ts +7 -0
- package/dist/ui/use-chat/useAISDKRuntime.d.ts.map +1 -0
- package/dist/ui/use-chat/useAISDKRuntime.js +53 -0
- package/dist/ui/use-chat/useAISDKRuntime.js.map +1 -0
- package/dist/ui/utils/convertMessage.d.ts +5 -5
- package/dist/ui/utils/convertMessage.d.ts.map +1 -1
- package/dist/ui/utils/convertMessage.js +115 -86
- package/dist/ui/utils/convertMessage.js.map +1 -1
- package/dist/ui/utils/sliceMessagesUntil.d.ts +2 -2
- package/dist/ui/utils/sliceMessagesUntil.d.ts.map +1 -1
- package/dist/ui/utils/sliceMessagesUntil.js.map +1 -1
- package/dist/ui/utils/toCreateMessage.d.ts +2 -2
- package/dist/ui/utils/toCreateMessage.d.ts.map +1 -1
- package/dist/ui/utils/toCreateMessage.js +29 -17
- package/dist/ui/utils/toCreateMessage.js.map +1 -1
- package/dist/ui/utils/vercelAttachmentAdapter.js +1 -1
- package/dist/ui/utils/vercelAttachmentAdapter.js.map +1 -1
- package/package.json +14 -14
- package/src/frontendTools.ts +1 -1
- package/src/index.ts +0 -5
- package/src/ui/getVercelAIMessages.tsx +2 -2
- package/src/ui/index.ts +1 -4
- package/src/ui/use-chat/{useVercelUseChatRuntime.tsx → useAISDKRuntime.tsx} +13 -39
- package/src/ui/utils/convertMessage.ts +162 -117
- package/src/ui/utils/sliceMessagesUntil.tsx +2 -2
- package/src/ui/utils/toCreateMessage.ts +45 -20
- package/src/ui/utils/vercelAttachmentAdapter.ts +1 -1
- package/dist/converters/fromLanguageModelTools.d.ts +0 -7
- package/dist/converters/fromLanguageModelTools.d.ts.map +0 -1
- package/dist/converters/fromLanguageModelTools.js +0 -16
- package/dist/converters/fromLanguageModelTools.js.map +0 -1
- package/dist/converters/index.d.ts +0 -4
- package/dist/converters/index.d.ts.map +0 -1
- package/dist/converters/index.js +0 -10
- package/dist/converters/index.js.map +0 -1
- package/dist/converters/toLanguageModelMessages.d.ts +0 -9
- package/dist/converters/toLanguageModelMessages.d.ts.map +0 -1
- package/dist/converters/toLanguageModelMessages.js +0 -143
- package/dist/converters/toLanguageModelMessages.js.map +0 -1
- package/dist/converters/toLanguageModelTools.d.ts +0 -7
- package/dist/converters/toLanguageModelTools.d.ts.map +0 -1
- package/dist/converters/toLanguageModelTools.js +0 -15
- package/dist/converters/toLanguageModelTools.js.map +0 -1
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.d.ts +0 -9
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.d.ts.map +0 -1
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.js +0 -38
- package/dist/dangerous-in-browser/DangerousInBrowserAdapter.js.map +0 -1
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.d.ts +0 -56
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.d.ts.map +0 -1
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.js +0 -65
- package/dist/dangerous-in-browser/createEdgeRuntimeAPI.js.map +0 -1
- package/dist/dangerous-in-browser/index.d.ts +0 -2
- package/dist/dangerous-in-browser/index.d.ts.map +0 -1
- package/dist/dangerous-in-browser/index.js +0 -8
- package/dist/dangerous-in-browser/index.js.map +0 -1
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.d.ts +0 -5
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.d.ts.map +0 -1
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.js +0 -21
- package/dist/dangerous-in-browser/useDangerousInBrowserRuntime.js.map +0 -1
- package/dist/rsc/RSCDisplay.d.ts +0 -3
- package/dist/rsc/RSCDisplay.d.ts.map +0 -1
- package/dist/rsc/RSCDisplay.js +0 -26
- package/dist/rsc/RSCDisplay.js.map +0 -1
- package/dist/rsc/VercelRSCAdapter.d.ts +0 -17
- package/dist/rsc/VercelRSCAdapter.d.ts.map +0 -1
- package/dist/rsc/VercelRSCAdapter.js +0 -1
- package/dist/rsc/VercelRSCAdapter.js.map +0 -1
- package/dist/rsc/VercelRSCMessage.d.ts +0 -8
- package/dist/rsc/VercelRSCMessage.d.ts.map +0 -1
- package/dist/rsc/VercelRSCMessage.js +0 -2
- package/dist/rsc/VercelRSCMessage.js.map +0 -1
- package/dist/rsc/index.d.ts +0 -5
- package/dist/rsc/index.d.ts.map +0 -1
- package/dist/rsc/index.js +0 -8
- package/dist/rsc/index.js.map +0 -1
- package/dist/rsc/useVercelRSCRuntime.d.ts +0 -3
- package/dist/rsc/useVercelRSCRuntime.d.ts.map +0 -1
- package/dist/rsc/useVercelRSCRuntime.js +0 -57
- package/dist/rsc/useVercelRSCRuntime.js.map +0 -1
- package/dist/rsc/utils/RSCThreadExtras.d.ts +0 -8
- package/dist/rsc/utils/RSCThreadExtras.d.ts.map +0 -1
- package/dist/rsc/utils/RSCThreadExtras.js +0 -8
- package/dist/rsc/utils/RSCThreadExtras.js.map +0 -1
- package/dist/ui/use-assistant/useVercelUseAssistantRuntime.d.ts +0 -7
- package/dist/ui/use-assistant/useVercelUseAssistantRuntime.d.ts.map +0 -1
- package/dist/ui/use-assistant/useVercelUseAssistantRuntime.js +0 -48
- package/dist/ui/use-assistant/useVercelUseAssistantRuntime.js.map +0 -1
- package/dist/ui/use-chat/useVercelUseChatRuntime.d.ts +0 -8
- package/dist/ui/use-chat/useVercelUseChatRuntime.d.ts.map +0 -1
- package/dist/ui/use-chat/useVercelUseChatRuntime.js +0 -75
- package/dist/ui/use-chat/useVercelUseChatRuntime.js.map +0 -1
- package/dist/ui/utils/useInputSync.d.ts +0 -6
- package/dist/ui/utils/useInputSync.d.ts.map +0 -1
- package/dist/ui/utils/useInputSync.js +0 -18
- package/dist/ui/utils/useInputSync.js.map +0 -1
- package/dist/useChatRuntime.d.ts +0 -16
- package/dist/useChatRuntime.d.ts.map +0 -1
- package/dist/useChatRuntime.js +0 -106
- package/dist/useChatRuntime.js.map +0 -1
- package/dist/useCloudRuntime.d.ts +0 -12
- package/dist/useCloudRuntime.d.ts.map +0 -1
- package/dist/useCloudRuntime.js +0 -15
- package/dist/useCloudRuntime.js.map +0 -1
- package/src/converters/fromLanguageModelTools.ts +0 -19
- package/src/converters/index.ts +0 -3
- package/src/converters/toLanguageModelMessages.ts +0 -185
- package/src/converters/toLanguageModelTools.ts +0 -21
- package/src/dangerous-in-browser/DangerousInBrowserAdapter.ts +0 -41
- package/src/dangerous-in-browser/createEdgeRuntimeAPI.ts +0 -145
- package/src/dangerous-in-browser/index.ts +0 -4
- package/src/dangerous-in-browser/useDangerousInBrowserRuntime.ts +0 -26
- package/src/rsc/RSCDisplay.tsx +0 -23
- package/src/rsc/VercelRSCAdapter.tsx +0 -21
- package/src/rsc/VercelRSCMessage.tsx +0 -9
- package/src/rsc/index.ts +0 -4
- package/src/rsc/useVercelRSCRuntime.tsx +0 -70
- package/src/rsc/utils/RSCThreadExtras.tsx +0 -11
- package/src/ui/use-assistant/useVercelUseAssistantRuntime.tsx +0 -60
- package/src/ui/utils/useInputSync.tsx +0 -26
- package/src/useChatRuntime.ts +0 -157
- package/src/useCloudRuntime.ts +0 -21
package/src/useChatRuntime.ts
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
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
|
-
);
|
|
157
|
-
};
|
package/src/useCloudRuntime.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { AssistantCloud } from "@assistant-ui/react";
|
|
2
|
-
import { useChatRuntime, UseChatRuntimeOptions } from "./useChatRuntime";
|
|
3
|
-
|
|
4
|
-
type UseCloudRuntimeOptions = Omit<UseChatRuntimeOptions, "api"> & {
|
|
5
|
-
cloud: AssistantCloud;
|
|
6
|
-
assistantId: string;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated This is under active development and not yet ready for prod use.
|
|
11
|
-
*/
|
|
12
|
-
export const useCloudRuntime = (options: UseCloudRuntimeOptions) => {
|
|
13
|
-
const opts = options.cloud.runs.__internal_getAssistantOptions(
|
|
14
|
-
options.assistantId,
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
return useChatRuntime({
|
|
18
|
-
...options,
|
|
19
|
-
...opts,
|
|
20
|
-
});
|
|
21
|
-
};
|