@assistant-ui/react 0.12.15 → 0.12.17
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/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +1 -1
- package/dist/internal.js.map +1 -1
- package/dist/legacy-runtime/cloud/AssistantCloudThreadHistoryAdapter.d.ts +1 -4
- package/dist/legacy-runtime/cloud/AssistantCloudThreadHistoryAdapter.d.ts.map +1 -1
- package/dist/legacy-runtime/cloud/AssistantCloudThreadHistoryAdapter.js +2 -527
- package/dist/legacy-runtime/cloud/AssistantCloudThreadHistoryAdapter.js.map +1 -1
- package/dist/legacy-runtime/hooks/AttachmentContext.d.ts +96 -96
- package/dist/legacy-runtime/runtime-cores/adapters/RuntimeAdapterProvider.d.ts +1 -16
- package/dist/legacy-runtime/runtime-cores/adapters/RuntimeAdapterProvider.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/adapters/RuntimeAdapterProvider.js +1 -14
- package/dist/legacy-runtime/runtime-cores/adapters/RuntimeAdapterProvider.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/adapters/attachment/CloudFileAttachmentAdapter.d.ts +1 -13
- package/dist/legacy-runtime/runtime-cores/adapters/attachment/CloudFileAttachmentAdapter.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/adapters/attachment/CloudFileAttachmentAdapter.js +2 -82
- package/dist/legacy-runtime/runtime-cores/adapters/attachment/CloudFileAttachmentAdapter.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.d.ts +1 -23
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.js +1 -410
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/createMessageConverter.d.ts +1 -16
- package/dist/legacy-runtime/runtime-cores/external-store/createMessageConverter.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/createMessageConverter.js +1 -48
- package/dist/legacy-runtime/runtime-cores/external-store/createMessageConverter.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.d.ts +1 -33
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.js +1 -307
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/useExternalStoreRuntime.d.ts +1 -3
- package/dist/legacy-runtime/runtime-cores/external-store/useExternalStoreRuntime.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/useExternalStoreRuntime.js +1 -17
- package/dist/legacy-runtime/runtime-cores/external-store/useExternalStoreRuntime.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/local/LocalRuntimeOptions.d.ts +2 -26
- package/dist/legacy-runtime/runtime-cores/local/LocalRuntimeOptions.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/local/LocalRuntimeOptions.js +1 -13
- package/dist/legacy-runtime/runtime-cores/local/LocalRuntimeOptions.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/local/useLocalRuntime.d.ts +1 -3
- package/dist/legacy-runtime/runtime-cores/local/useLocalRuntime.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/local/useLocalRuntime.js +1 -51
- package/dist/legacy-runtime/runtime-cores/local/useLocalRuntime.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts +1 -96
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.js +1 -110
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts +1 -112
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.js +1 -439
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/adapter/cloud.d.ts +1 -12
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/adapter/cloud.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/adapter/cloud.js +1 -102
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/adapter/cloud.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/useRemoteThreadListRuntime.d.ts +1 -3
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/useRemoteThreadListRuntime.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/useRemoteThreadListRuntime.js +1 -46
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/useRemoteThreadListRuntime.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarCopy.d.ts.map +1 -1
- package/dist/primitives/actionBar/ActionBarCopy.js +7 -20
- package/dist/primitives/actionBar/ActionBarCopy.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarEdit.d.ts.map +1 -1
- package/dist/primitives/actionBar/ActionBarEdit.js +3 -8
- package/dist/primitives/actionBar/ActionBarEdit.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarFeedbackNegative.d.ts.map +1 -1
- package/dist/primitives/actionBar/ActionBarFeedbackNegative.js +4 -7
- package/dist/primitives/actionBar/ActionBarFeedbackNegative.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarFeedbackPositive.d.ts.map +1 -1
- package/dist/primitives/actionBar/ActionBarFeedbackPositive.js +5 -7
- package/dist/primitives/actionBar/ActionBarFeedbackPositive.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarReload.d.ts.map +1 -1
- package/dist/primitives/actionBar/ActionBarReload.js +3 -10
- package/dist/primitives/actionBar/ActionBarReload.js.map +1 -1
- package/dist/primitives/composer/ComposerAddAttachment.d.ts.map +1 -1
- package/dist/primitives/composer/ComposerAddAttachment.js +5 -4
- package/dist/primitives/composer/ComposerAddAttachment.js.map +1 -1
- package/dist/primitives/composer/ComposerCancel.d.ts.map +1 -1
- package/dist/primitives/composer/ComposerCancel.js +3 -8
- package/dist/primitives/composer/ComposerCancel.js.map +1 -1
- package/dist/primitives/composer/ComposerSend.d.ts.map +1 -1
- package/dist/primitives/composer/ComposerSend.js +3 -8
- package/dist/primitives/composer/ComposerSend.js.map +1 -1
- package/dist/utils/createActionButton.js +1 -1
- package/dist/utils/createActionButton.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +1 -1
- package/src/internal.ts +1 -1
- package/src/legacy-runtime/cloud/AssistantCloudThreadHistoryAdapter.ts +2 -784
- package/src/legacy-runtime/runtime-cores/adapters/RuntimeAdapterProvider.tsx +5 -43
- package/src/legacy-runtime/runtime-cores/adapters/attachment/CloudFileAttachmentAdapter.ts +2 -100
- package/src/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.ts +4 -580
- package/src/legacy-runtime/runtime-cores/external-store/createMessageConverter.ts +1 -76
- package/src/legacy-runtime/runtime-cores/external-store/external-message-converter.ts +4 -483
- package/src/legacy-runtime/runtime-cores/external-store/useExternalStoreRuntime.ts +1 -27
- package/src/legacy-runtime/runtime-cores/local/LocalRuntimeOptions.ts +2 -35
- package/src/legacy-runtime/runtime-cores/local/useLocalRuntime.ts +1 -67
- package/src/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.tsx +1 -178
- package/src/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.tsx +1 -529
- package/src/legacy-runtime/runtime-cores/remote-thread-list/adapter/cloud.tsx +1 -152
- package/src/legacy-runtime/runtime-cores/remote-thread-list/useRemoteThreadListRuntime.ts +1 -80
- package/src/primitives/actionBar/ActionBarCopy.tsx +7 -26
- package/src/primitives/actionBar/ActionBarEdit.ts +3 -10
- package/src/primitives/actionBar/ActionBarFeedbackNegative.tsx +4 -9
- package/src/primitives/actionBar/ActionBarFeedbackPositive.tsx +5 -9
- package/src/primitives/actionBar/ActionBarReload.ts +3 -16
- package/src/primitives/composer/ComposerAddAttachment.ts +5 -4
- package/src/primitives/composer/ComposerCancel.ts +3 -10
- package/src/primitives/composer/ComposerSend.ts +3 -13
- package/src/tests/BaseComposerRuntimeCore.test.ts +2 -3
- package/src/utils/createActionButton.tsx +1 -1
|
@@ -1,484 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
getExternalStoreMessages,
|
|
7
|
-
symbolInnerMessage,
|
|
8
|
-
bindExternalStoreMessage,
|
|
9
|
-
} from "./getExternalStoreMessage";
|
|
10
|
-
import { fromThreadMessageLike, ThreadMessageLike } from "./ThreadMessageLike";
|
|
11
|
-
import { getAutoStatus, isAutoStatus } from "./auto-status";
|
|
12
|
-
import { ToolExecutionStatus } from "../assistant-transport/useToolInvocations";
|
|
13
|
-
import { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
14
|
-
import { generateErrorMessageId } from "@assistant-ui/core/internal";
|
|
15
|
-
import type {
|
|
16
|
-
MessageTiming,
|
|
17
|
-
ThreadAssistantMessage,
|
|
18
|
-
ThreadMessage,
|
|
19
|
-
ToolCallMessagePart,
|
|
20
|
-
} from "@assistant-ui/core";
|
|
21
|
-
|
|
22
|
-
export namespace useExternalMessageConverter {
|
|
23
|
-
export type Message =
|
|
24
|
-
| (ThreadMessageLike & {
|
|
25
|
-
readonly convertConfig?: {
|
|
26
|
-
readonly joinStrategy?: "concat-content" | "none";
|
|
27
|
-
};
|
|
28
|
-
})
|
|
29
|
-
| {
|
|
30
|
-
role: "tool";
|
|
31
|
-
toolCallId: string;
|
|
32
|
-
toolName?: string | undefined;
|
|
33
|
-
result: any;
|
|
34
|
-
artifact?: any;
|
|
35
|
-
isError?: boolean;
|
|
36
|
-
messages?: readonly ThreadMessage[];
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type Metadata = {
|
|
40
|
-
readonly toolStatuses?: Record<string, ToolExecutionStatus>;
|
|
41
|
-
readonly error?: ReadonlyJSONValue;
|
|
42
|
-
readonly messageTiming?: Record<string, MessageTiming>;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export type Callback<T> = (
|
|
46
|
-
message: T,
|
|
47
|
-
metadata: Metadata,
|
|
48
|
-
) => Message | Message[];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
type CallbackResult<T> = {
|
|
52
|
-
input: T;
|
|
53
|
-
outputs: useExternalMessageConverter.Message[];
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
type ChunkResult<T> = {
|
|
57
|
-
inputs: T[];
|
|
58
|
-
outputs: useExternalMessageConverter.Message[];
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
type Mutable<T> = {
|
|
62
|
-
-readonly [P in keyof T]: T[P];
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const mergeInnerMessages = (existing: object, incoming: object) => ({
|
|
66
|
-
[symbolInnerMessage]: [
|
|
67
|
-
...((existing as any)[symbolInnerMessage] ?? []),
|
|
68
|
-
...((incoming as any)[symbolInnerMessage] ?? []),
|
|
69
|
-
],
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const joinExternalMessages = (
|
|
73
|
-
messages: readonly useExternalMessageConverter.Message[],
|
|
74
|
-
): ThreadMessageLike => {
|
|
75
|
-
const assistantMessage: Mutable<Omit<ThreadMessageLike, "metadata">> & {
|
|
76
|
-
content: Exclude<ThreadMessageLike["content"][0], string>[];
|
|
77
|
-
metadata?: Mutable<ThreadMessageLike["metadata"]>;
|
|
78
|
-
} = {
|
|
79
|
-
role: "assistant",
|
|
80
|
-
content: [],
|
|
81
|
-
};
|
|
82
|
-
for (const output of messages) {
|
|
83
|
-
if (output.role === "tool") {
|
|
84
|
-
const toolCallIdx = assistantMessage.content.findIndex(
|
|
85
|
-
(c) => c.type === "tool-call" && c.toolCallId === output.toolCallId,
|
|
86
|
-
);
|
|
87
|
-
// Ignore orphaned tool results so one bad tool message does not
|
|
88
|
-
// prevent rendering the rest of the conversation.
|
|
89
|
-
if (toolCallIdx !== -1) {
|
|
90
|
-
const toolCall = assistantMessage.content[
|
|
91
|
-
toolCallIdx
|
|
92
|
-
]! as ToolCallMessagePart;
|
|
93
|
-
if (output.toolName !== undefined) {
|
|
94
|
-
if (toolCall.toolName !== output.toolName)
|
|
95
|
-
throw new Error(
|
|
96
|
-
`Tool call name ${output.toolCallId} ${output.toolName} does not match existing tool call ${toolCall.toolName}`,
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
assistantMessage.content[toolCallIdx] = {
|
|
100
|
-
...toolCall,
|
|
101
|
-
...{
|
|
102
|
-
[symbolInnerMessage]: [
|
|
103
|
-
...((toolCall as any)[symbolInnerMessage] ?? []),
|
|
104
|
-
output,
|
|
105
|
-
],
|
|
106
|
-
},
|
|
107
|
-
result: output.result,
|
|
108
|
-
artifact: output.artifact,
|
|
109
|
-
isError: output.isError,
|
|
110
|
-
messages: output.messages,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
const role = output.role;
|
|
115
|
-
const content = (
|
|
116
|
-
typeof output.content === "string"
|
|
117
|
-
? [{ type: "text" as const, text: output.content }]
|
|
118
|
-
: output.content
|
|
119
|
-
).map((c) => ({
|
|
120
|
-
...c,
|
|
121
|
-
...{ [symbolInnerMessage]: [output] },
|
|
122
|
-
}));
|
|
123
|
-
switch (role) {
|
|
124
|
-
case "system":
|
|
125
|
-
case "user":
|
|
126
|
-
return {
|
|
127
|
-
...output,
|
|
128
|
-
content,
|
|
129
|
-
};
|
|
130
|
-
case "assistant":
|
|
131
|
-
if (assistantMessage.content.length === 0) {
|
|
132
|
-
assistantMessage.id = output.id;
|
|
133
|
-
assistantMessage.createdAt ??= output.createdAt;
|
|
134
|
-
assistantMessage.status ??= output.status;
|
|
135
|
-
|
|
136
|
-
if (output.attachments) {
|
|
137
|
-
assistantMessage.attachments = [
|
|
138
|
-
...(assistantMessage.attachments ?? []),
|
|
139
|
-
...output.attachments,
|
|
140
|
-
];
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (output.metadata) {
|
|
144
|
-
assistantMessage.metadata ??= {};
|
|
145
|
-
if (output.metadata.unstable_state) {
|
|
146
|
-
assistantMessage.metadata.unstable_state =
|
|
147
|
-
output.metadata.unstable_state;
|
|
148
|
-
}
|
|
149
|
-
if (output.metadata.unstable_annotations) {
|
|
150
|
-
assistantMessage.metadata.unstable_annotations = [
|
|
151
|
-
...(assistantMessage.metadata.unstable_annotations ?? []),
|
|
152
|
-
...output.metadata.unstable_annotations,
|
|
153
|
-
];
|
|
154
|
-
}
|
|
155
|
-
if (output.metadata.unstable_data) {
|
|
156
|
-
assistantMessage.metadata.unstable_data = [
|
|
157
|
-
...(assistantMessage.metadata.unstable_data ?? []),
|
|
158
|
-
...output.metadata.unstable_data,
|
|
159
|
-
];
|
|
160
|
-
}
|
|
161
|
-
if (output.metadata.steps) {
|
|
162
|
-
assistantMessage.metadata.steps = [
|
|
163
|
-
...(assistantMessage.metadata.steps ?? []),
|
|
164
|
-
...output.metadata.steps,
|
|
165
|
-
];
|
|
166
|
-
}
|
|
167
|
-
if (output.metadata.custom) {
|
|
168
|
-
assistantMessage.metadata.custom = {
|
|
169
|
-
...(assistantMessage.metadata.custom ?? {}),
|
|
170
|
-
...output.metadata.custom,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (output.metadata.timing) {
|
|
175
|
-
assistantMessage.metadata.timing = output.metadata.timing;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (output.metadata.submittedFeedback) {
|
|
179
|
-
assistantMessage.metadata.submittedFeedback =
|
|
180
|
-
output.metadata.submittedFeedback;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
// TODO keep this in sync
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Add content parts, merging reasoning parts with same parentId
|
|
187
|
-
for (const part of content) {
|
|
188
|
-
if (part.type === "tool-call") {
|
|
189
|
-
const existingIdx = assistantMessage.content.findIndex(
|
|
190
|
-
(c) =>
|
|
191
|
-
c.type === "tool-call" && c.toolCallId === part.toolCallId,
|
|
192
|
-
);
|
|
193
|
-
if (existingIdx !== -1) {
|
|
194
|
-
const existing = assistantMessage.content[
|
|
195
|
-
existingIdx
|
|
196
|
-
] as typeof part;
|
|
197
|
-
assistantMessage.content[existingIdx] = {
|
|
198
|
-
...existing,
|
|
199
|
-
...part,
|
|
200
|
-
...mergeInnerMessages(existing, part),
|
|
201
|
-
};
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (
|
|
207
|
-
part.type === "reasoning" &&
|
|
208
|
-
"parentId" in part &&
|
|
209
|
-
part.parentId
|
|
210
|
-
) {
|
|
211
|
-
const existingIdx = assistantMessage.content.findIndex(
|
|
212
|
-
(c) =>
|
|
213
|
-
c.type === "reasoning" &&
|
|
214
|
-
"parentId" in c &&
|
|
215
|
-
c.parentId === part.parentId,
|
|
216
|
-
);
|
|
217
|
-
if (existingIdx !== -1) {
|
|
218
|
-
const existing = assistantMessage.content[
|
|
219
|
-
existingIdx
|
|
220
|
-
] as typeof part;
|
|
221
|
-
assistantMessage.content[existingIdx] = {
|
|
222
|
-
...existing,
|
|
223
|
-
text: `${existing.text}\n\n${part.text}`,
|
|
224
|
-
...mergeInnerMessages(existing, part),
|
|
225
|
-
};
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
assistantMessage.content.push(part);
|
|
230
|
-
}
|
|
231
|
-
break;
|
|
232
|
-
default: {
|
|
233
|
-
const unsupportedRole: never = role;
|
|
234
|
-
throw new Error(`Unknown message role: ${unsupportedRole}`);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return assistantMessage;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
const chunkExternalMessages = <T>(
|
|
243
|
-
callbackResults: CallbackResult<T>[],
|
|
244
|
-
joinStrategy?: "concat-content" | "none",
|
|
245
|
-
) => {
|
|
246
|
-
const results: ChunkResult<T>[] = [];
|
|
247
|
-
let isAssistant = false;
|
|
248
|
-
let pendingNone = false; // true if the previous assistant message had joinStrategy "none"
|
|
249
|
-
let inputs: T[] = [];
|
|
250
|
-
let outputs: useExternalMessageConverter.Message[] = [];
|
|
251
|
-
|
|
252
|
-
const flush = () => {
|
|
253
|
-
if (outputs.length) {
|
|
254
|
-
results.push({
|
|
255
|
-
inputs,
|
|
256
|
-
outputs,
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
inputs = [];
|
|
260
|
-
outputs = [];
|
|
261
|
-
isAssistant = false;
|
|
262
|
-
pendingNone = false;
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
for (const callbackResult of callbackResults) {
|
|
266
|
-
for (const output of callbackResult.outputs) {
|
|
267
|
-
if (
|
|
268
|
-
(pendingNone && output.role !== "tool") ||
|
|
269
|
-
!isAssistant ||
|
|
270
|
-
output.role === "user" ||
|
|
271
|
-
output.role === "system"
|
|
272
|
-
) {
|
|
273
|
-
flush();
|
|
274
|
-
}
|
|
275
|
-
isAssistant = output.role === "assistant" || output.role === "tool";
|
|
276
|
-
|
|
277
|
-
if (inputs.at(-1) !== callbackResult.input) {
|
|
278
|
-
inputs.push(callbackResult.input);
|
|
279
|
-
}
|
|
280
|
-
outputs.push(output);
|
|
281
|
-
|
|
282
|
-
if (
|
|
283
|
-
output.role === "assistant" &&
|
|
284
|
-
(output.convertConfig?.joinStrategy === "none" ||
|
|
285
|
-
joinStrategy === "none")
|
|
286
|
-
) {
|
|
287
|
-
pendingNone = true;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
flush();
|
|
292
|
-
return results;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
function createErrorAssistantMessage(
|
|
296
|
-
error: ReadonlyJSONValue,
|
|
297
|
-
): ThreadAssistantMessage {
|
|
298
|
-
const msg: ThreadAssistantMessage = {
|
|
299
|
-
id: generateErrorMessageId(),
|
|
300
|
-
role: "assistant",
|
|
301
|
-
content: [],
|
|
302
|
-
status: { type: "incomplete", reason: "error", error },
|
|
303
|
-
createdAt: new Date(),
|
|
304
|
-
metadata: {
|
|
305
|
-
unstable_state: null,
|
|
306
|
-
unstable_annotations: [],
|
|
307
|
-
unstable_data: [],
|
|
308
|
-
custom: {},
|
|
309
|
-
steps: [],
|
|
310
|
-
},
|
|
311
|
-
};
|
|
312
|
-
bindExternalStoreMessage(msg, []);
|
|
313
|
-
return msg;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
export const convertExternalMessages = <T extends WeakKey>(
|
|
317
|
-
messages: T[],
|
|
318
|
-
callback: useExternalMessageConverter.Callback<T>,
|
|
319
|
-
isRunning: boolean,
|
|
320
|
-
metadata: useExternalMessageConverter.Metadata,
|
|
321
|
-
) => {
|
|
322
|
-
const callbackResults: CallbackResult<T>[] = [];
|
|
323
|
-
for (const message of messages) {
|
|
324
|
-
const output = callback(message, metadata);
|
|
325
|
-
const outputs = Array.isArray(output) ? output : [output];
|
|
326
|
-
const result = { input: message, outputs };
|
|
327
|
-
callbackResults.push(result);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const chunks = chunkExternalMessages(callbackResults);
|
|
331
|
-
|
|
332
|
-
const result = chunks.map((message, idx) => {
|
|
333
|
-
const isLast = idx === chunks.length - 1;
|
|
334
|
-
const joined = joinExternalMessages(message.outputs);
|
|
335
|
-
const hasPendingToolCalls =
|
|
336
|
-
typeof joined.content === "object" &&
|
|
337
|
-
joined.content.some(
|
|
338
|
-
(c) => c.type === "tool-call" && c.result === undefined,
|
|
339
|
-
);
|
|
340
|
-
const autoStatus = getAutoStatus(
|
|
341
|
-
isLast,
|
|
342
|
-
isRunning,
|
|
343
|
-
hasPendingToolCalls,
|
|
344
|
-
hasPendingToolCalls,
|
|
345
|
-
isLast ? metadata.error : undefined,
|
|
346
|
-
);
|
|
347
|
-
const newMessage = fromThreadMessageLike(
|
|
348
|
-
joined,
|
|
349
|
-
idx.toString(),
|
|
350
|
-
autoStatus,
|
|
351
|
-
);
|
|
352
|
-
bindExternalStoreMessage(newMessage, message.inputs);
|
|
353
|
-
return newMessage;
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
if (metadata.error) {
|
|
357
|
-
const lastMessage = result.at(-1);
|
|
358
|
-
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
359
|
-
result.push(createErrorAssistantMessage(metadata.error));
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
return result;
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
export const useExternalMessageConverter = <T extends WeakKey>({
|
|
367
|
-
callback,
|
|
368
|
-
messages,
|
|
369
|
-
isRunning,
|
|
370
|
-
joinStrategy,
|
|
371
|
-
metadata,
|
|
372
|
-
}: {
|
|
373
|
-
callback: useExternalMessageConverter.Callback<T>;
|
|
374
|
-
messages: T[];
|
|
375
|
-
isRunning: boolean;
|
|
376
|
-
joinStrategy?: "concat-content" | "none" | undefined;
|
|
377
|
-
metadata?: useExternalMessageConverter.Metadata | undefined;
|
|
378
|
-
}) => {
|
|
379
|
-
const state = useMemo(
|
|
380
|
-
() => ({
|
|
381
|
-
metadata: metadata ?? {},
|
|
382
|
-
callback,
|
|
383
|
-
callbackCache: new WeakMap<T, CallbackResult<T>>(),
|
|
384
|
-
chunkCache: new WeakMap<
|
|
385
|
-
useExternalMessageConverter.Message,
|
|
386
|
-
ChunkResult<T>
|
|
387
|
-
>(),
|
|
388
|
-
converterCache: new ThreadMessageConverter(),
|
|
389
|
-
}),
|
|
390
|
-
[callback, metadata],
|
|
391
|
-
);
|
|
392
|
-
|
|
393
|
-
return useMemo(() => {
|
|
394
|
-
const callbackResults: CallbackResult<T>[] = [];
|
|
395
|
-
for (const message of messages) {
|
|
396
|
-
let result = state.callbackCache.get(message);
|
|
397
|
-
if (!result) {
|
|
398
|
-
const output = state.callback(message, state.metadata);
|
|
399
|
-
const outputs = Array.isArray(output) ? output : [output];
|
|
400
|
-
result = { input: message, outputs };
|
|
401
|
-
state.callbackCache.set(message, result);
|
|
402
|
-
}
|
|
403
|
-
callbackResults.push(result);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
const chunks = chunkExternalMessages(callbackResults, joinStrategy).map(
|
|
407
|
-
(m) => {
|
|
408
|
-
const key = m.outputs[0];
|
|
409
|
-
if (!key) return m;
|
|
410
|
-
|
|
411
|
-
const cached = state.chunkCache.get(key);
|
|
412
|
-
if (cached && shallowArrayEqual(cached.outputs, m.outputs))
|
|
413
|
-
return cached;
|
|
414
|
-
state.chunkCache.set(key, m);
|
|
415
|
-
return m;
|
|
416
|
-
},
|
|
417
|
-
);
|
|
418
|
-
|
|
419
|
-
const threadMessages = state.converterCache.convertMessages(
|
|
420
|
-
chunks,
|
|
421
|
-
(cache, message, idx) => {
|
|
422
|
-
const isLast = idx === chunks.length - 1;
|
|
423
|
-
|
|
424
|
-
const joined = joinExternalMessages(message.outputs);
|
|
425
|
-
const hasSuspendedToolCalls =
|
|
426
|
-
typeof joined.content === "object" &&
|
|
427
|
-
joined.content.some(
|
|
428
|
-
(c) => c.type === "tool-call" && c.result === undefined,
|
|
429
|
-
);
|
|
430
|
-
const hasPendingToolCalls =
|
|
431
|
-
typeof joined.content === "object" &&
|
|
432
|
-
joined.content.some(
|
|
433
|
-
(c) => c.type === "tool-call" && c.result === undefined,
|
|
434
|
-
);
|
|
435
|
-
const autoStatus = getAutoStatus(
|
|
436
|
-
isLast,
|
|
437
|
-
isRunning,
|
|
438
|
-
hasSuspendedToolCalls,
|
|
439
|
-
hasPendingToolCalls,
|
|
440
|
-
isLast ? state.metadata.error : undefined,
|
|
441
|
-
);
|
|
442
|
-
|
|
443
|
-
if (
|
|
444
|
-
cache &&
|
|
445
|
-
(cache.role !== "assistant" ||
|
|
446
|
-
!isAutoStatus(cache.status) ||
|
|
447
|
-
cache.status === autoStatus)
|
|
448
|
-
) {
|
|
449
|
-
const inputs = getExternalStoreMessages<T>(cache);
|
|
450
|
-
if (shallowArrayEqual(inputs, message.inputs)) {
|
|
451
|
-
return cache;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
const newMessage = fromThreadMessageLike(
|
|
456
|
-
joined,
|
|
457
|
-
idx.toString(),
|
|
458
|
-
autoStatus,
|
|
459
|
-
);
|
|
460
|
-
bindExternalStoreMessage(newMessage, message.inputs);
|
|
461
|
-
return newMessage;
|
|
462
|
-
},
|
|
463
|
-
);
|
|
464
|
-
|
|
465
|
-
bindExternalStoreMessage(threadMessages, messages);
|
|
466
|
-
|
|
467
|
-
if (state.metadata.error) {
|
|
468
|
-
const lastMessage = threadMessages.at(-1);
|
|
469
|
-
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
470
|
-
threadMessages.push(createErrorAssistantMessage(state.metadata.error));
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return threadMessages;
|
|
475
|
-
}, [state, messages, isRunning, joinStrategy]);
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
const shallowArrayEqual = (a: unknown[], b: unknown[]) => {
|
|
479
|
-
if (a.length !== b.length) return false;
|
|
480
|
-
for (let i = 0; i < a.length; i++) {
|
|
481
|
-
if (a[i] !== b[i]) return false;
|
|
482
|
-
}
|
|
483
|
-
return true;
|
|
484
|
-
};
|
|
2
|
+
export {
|
|
3
|
+
useExternalMessageConverter,
|
|
4
|
+
convertExternalMessages,
|
|
5
|
+
} from "@assistant-ui/core/react";
|
|
@@ -1,29 +1,3 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
import { ExternalStoreRuntimeCore } from "./ExternalStoreRuntimeCore";
|
|
5
|
-
import { ExternalStoreAdapter } from "./ExternalStoreAdapter";
|
|
6
|
-
import {
|
|
7
|
-
AssistantRuntime,
|
|
8
|
-
AssistantRuntimeImpl,
|
|
9
|
-
} from "../../runtime/AssistantRuntime";
|
|
10
|
-
import { useRuntimeAdapters } from "../adapters/RuntimeAdapterProvider";
|
|
11
|
-
|
|
12
|
-
export const useExternalStoreRuntime = <T>(
|
|
13
|
-
store: ExternalStoreAdapter<T>,
|
|
14
|
-
): AssistantRuntime => {
|
|
15
|
-
const [runtime] = useState(() => new ExternalStoreRuntimeCore(store));
|
|
16
|
-
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
runtime.setAdapter(store);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const { modelContext } = useRuntimeAdapters() ?? {};
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (!modelContext) return undefined;
|
|
25
|
-
return runtime.registerModelContextProvider(modelContext);
|
|
26
|
-
}, [modelContext, runtime]);
|
|
27
|
-
|
|
28
|
-
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
|
29
|
-
};
|
|
3
|
+
export { useExternalStoreRuntime } from "@assistant-ui/core/react";
|
|
@@ -1,36 +1,3 @@
|
|
|
1
1
|
export type { LocalRuntimeOptionsBase } from "@assistant-ui/core";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import type { ThreadMessageLike } from "@assistant-ui/core";
|
|
5
|
-
import type { AssistantCloud } from "assistant-cloud";
|
|
6
|
-
|
|
7
|
-
// TODO align LocalRuntimeOptions with LocalRuntimeOptionsBase
|
|
8
|
-
export type LocalRuntimeOptions = Omit<LocalRuntimeOptionsBase, "adapters"> & {
|
|
9
|
-
cloud?: AssistantCloud | undefined;
|
|
10
|
-
initialMessages?: readonly ThreadMessageLike[] | undefined;
|
|
11
|
-
adapters?: Omit<LocalRuntimeOptionsBase["adapters"], "chatModel"> | undefined;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const splitLocalRuntimeOptions = <T extends LocalRuntimeOptions>(
|
|
15
|
-
options: T,
|
|
16
|
-
) => {
|
|
17
|
-
const {
|
|
18
|
-
cloud,
|
|
19
|
-
initialMessages,
|
|
20
|
-
maxSteps,
|
|
21
|
-
adapters,
|
|
22
|
-
unstable_humanToolNames,
|
|
23
|
-
...rest
|
|
24
|
-
} = options;
|
|
25
|
-
|
|
26
|
-
return {
|
|
27
|
-
localRuntimeOptions: {
|
|
28
|
-
cloud,
|
|
29
|
-
initialMessages,
|
|
30
|
-
maxSteps,
|
|
31
|
-
adapters,
|
|
32
|
-
unstable_humanToolNames,
|
|
33
|
-
},
|
|
34
|
-
otherOptions: rest,
|
|
35
|
-
};
|
|
36
|
-
};
|
|
2
|
+
export type { LocalRuntimeOptions } from "@assistant-ui/core/react";
|
|
3
|
+
export { splitLocalRuntimeOptions } from "@assistant-ui/core/react";
|
|
@@ -1,69 +1,3 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
import type { ChatModelAdapter } from "./ChatModelAdapter";
|
|
5
|
-
import { LocalRuntimeCore } from "./LocalRuntimeCore";
|
|
6
|
-
import type { LocalRuntimeOptions } from "./LocalRuntimeOptions";
|
|
7
|
-
import { useRuntimeAdapters } from "../adapters/RuntimeAdapterProvider";
|
|
8
|
-
import { useRemoteThreadListRuntime } from "../remote-thread-list/useRemoteThreadListRuntime";
|
|
9
|
-
import { useCloudThreadListAdapter } from "../remote-thread-list/adapter/cloud";
|
|
10
|
-
import { AssistantRuntimeImpl } from "../../../internal";
|
|
11
|
-
import { useAuiState } from "@assistant-ui/store";
|
|
12
|
-
|
|
13
|
-
const useLocalThreadRuntime = (
|
|
14
|
-
adapter: ChatModelAdapter,
|
|
15
|
-
{ initialMessages, ...options }: LocalRuntimeOptions,
|
|
16
|
-
) => {
|
|
17
|
-
const { modelContext, ...threadListAdapters } = useRuntimeAdapters() ?? {};
|
|
18
|
-
const opt = {
|
|
19
|
-
...options,
|
|
20
|
-
adapters: {
|
|
21
|
-
...threadListAdapters,
|
|
22
|
-
...options.adapters,
|
|
23
|
-
chatModel: adapter,
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const [runtime] = useState(() => new LocalRuntimeCore(opt, initialMessages));
|
|
28
|
-
|
|
29
|
-
const threadIdRef = useRef<string | undefined>(undefined);
|
|
30
|
-
threadIdRef.current = useAuiState((s) => s.threadListItem.remoteId);
|
|
31
|
-
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
runtime.threads
|
|
34
|
-
.getMainThreadRuntimeCore()
|
|
35
|
-
.__internal_setGetThreadId(() => threadIdRef.current);
|
|
36
|
-
}, [runtime]);
|
|
37
|
-
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
return () => {
|
|
40
|
-
runtime.threads.getMainThreadRuntimeCore().detach();
|
|
41
|
-
};
|
|
42
|
-
}, [runtime]);
|
|
43
|
-
|
|
44
|
-
useEffect(() => {
|
|
45
|
-
runtime.threads.getMainThreadRuntimeCore().__internal_setOptions(opt);
|
|
46
|
-
runtime.threads.getMainThreadRuntimeCore().__internal_load();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
if (!modelContext) return undefined;
|
|
51
|
-
return runtime.registerModelContextProvider(modelContext);
|
|
52
|
-
}, [modelContext, runtime]);
|
|
53
|
-
|
|
54
|
-
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export const useLocalRuntime = (
|
|
58
|
-
adapter: ChatModelAdapter,
|
|
59
|
-
{ cloud, ...options }: LocalRuntimeOptions = {},
|
|
60
|
-
) => {
|
|
61
|
-
const cloudAdapter = useCloudThreadListAdapter({ cloud });
|
|
62
|
-
return useRemoteThreadListRuntime({
|
|
63
|
-
runtimeHook: function RuntimeHook() {
|
|
64
|
-
return useLocalThreadRuntime(adapter, options);
|
|
65
|
-
},
|
|
66
|
-
adapter: cloudAdapter,
|
|
67
|
-
allowNesting: true,
|
|
68
|
-
});
|
|
69
|
-
};
|
|
3
|
+
export { useLocalRuntime } from "@assistant-ui/core/react";
|