@assistant-ui/react 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.mts +37 -29
- package/dist/index.d.ts +37 -29
- package/dist/index.js +204 -149
- package/dist/index.mjs +207 -152
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -35,7 +35,7 @@ var useAssistantContext = () => {
|
|
35
35
|
const context = useContext(AssistantContext);
|
36
36
|
if (!context)
|
37
37
|
throw new Error(
|
38
|
-
"
|
38
|
+
"This component must be used within a AssistantProvider"
|
39
39
|
);
|
40
40
|
return context;
|
41
41
|
};
|
@@ -187,7 +187,7 @@ var MessageContext = createContext2(null);
|
|
187
187
|
var useMessageContext = () => {
|
188
188
|
const context = useContext2(MessageContext);
|
189
189
|
if (!context)
|
190
|
-
throw new Error("
|
190
|
+
throw new Error("This component must be used within a MessageProvider");
|
191
191
|
return context;
|
192
192
|
};
|
193
193
|
|
@@ -294,6 +294,7 @@ var useMessageContext2 = () => {
|
|
294
294
|
const [context] = useState(() => {
|
295
295
|
const useMessage = create2(() => ({
|
296
296
|
message: null,
|
297
|
+
parentId: null,
|
297
298
|
branches: [],
|
298
299
|
isLast: false,
|
299
300
|
isCopied: false,
|
@@ -308,15 +309,19 @@ var useMessageContext2 = () => {
|
|
308
309
|
const message = useMessage.getState().message;
|
309
310
|
if (message.role !== "user")
|
310
311
|
throw new Error("Editing is only supported for user messages");
|
311
|
-
|
312
|
-
|
313
|
-
return message.content[0].text;
|
312
|
+
const text = message.content.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
313
|
+
return text;
|
314
314
|
},
|
315
315
|
onSend: (text) => {
|
316
|
-
const message = useMessage.getState()
|
316
|
+
const { message, parentId } = useMessage.getState();
|
317
|
+
if (message.role !== "user")
|
318
|
+
throw new Error("Editing is only supported for user messages");
|
319
|
+
const nonTextParts = message.content.filter(
|
320
|
+
(part) => part.type !== "text" && part.type !== "ui"
|
321
|
+
);
|
317
322
|
useThread.getState().append({
|
318
|
-
parentId
|
319
|
-
content: [{ type: "text", text }]
|
323
|
+
parentId,
|
324
|
+
content: [{ type: "text", text }, ...nonTextParts]
|
320
325
|
});
|
321
326
|
}
|
322
327
|
});
|
@@ -326,6 +331,7 @@ var useMessageContext2 = () => {
|
|
326
331
|
};
|
327
332
|
var MessageProvider = ({
|
328
333
|
message,
|
334
|
+
parentId,
|
329
335
|
children
|
330
336
|
}) => {
|
331
337
|
const { useThread } = useAssistantContext();
|
@@ -338,6 +344,7 @@ var MessageProvider = ({
|
|
338
344
|
context.useMessage.setState(
|
339
345
|
{
|
340
346
|
message,
|
347
|
+
parentId,
|
341
348
|
branches,
|
342
349
|
isLast,
|
343
350
|
isCopied,
|
@@ -347,7 +354,7 @@ var MessageProvider = ({
|
|
347
354
|
},
|
348
355
|
true
|
349
356
|
);
|
350
|
-
}, [context, message, branches, isLast, isCopied, isHovering]);
|
357
|
+
}, [context, message, parentId, branches, isLast, isCopied, isHovering]);
|
351
358
|
return /* @__PURE__ */ jsx4(MessageContext.Provider, { value: context, children });
|
352
359
|
};
|
353
360
|
|
@@ -460,15 +467,21 @@ var ThreadMessages = ({ components }) => {
|
|
460
467
|
if (messages.length === 0)
|
461
468
|
return null;
|
462
469
|
return /* @__PURE__ */ jsx7(Fragment2, { children: messages.map((message, idx) => {
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
470
|
+
const parentId = messages[idx - 1]?.id ?? null;
|
471
|
+
return /* @__PURE__ */ jsxs2(
|
472
|
+
MessageProvider,
|
473
|
+
{
|
474
|
+
message,
|
475
|
+
parentId,
|
476
|
+
children: [
|
477
|
+
/* @__PURE__ */ jsxs2(MessageIf, { user: true, children: [
|
478
|
+
/* @__PURE__ */ jsx7(ComposerIf, { editing: false, children: /* @__PURE__ */ jsx7(UserMessage, {}) }),
|
479
|
+
/* @__PURE__ */ jsx7(ComposerIf, { editing: true, children: /* @__PURE__ */ jsx7(EditComposer, {}) })
|
480
|
+
] }),
|
481
|
+
/* @__PURE__ */ jsx7(MessageIf, { assistant: true, children: /* @__PURE__ */ jsx7(AssistantMessage, {}) })
|
482
|
+
]
|
483
|
+
},
|
484
|
+
parentId ?? "__ROOT__"
|
472
485
|
);
|
473
486
|
}) });
|
474
487
|
};
|
@@ -582,7 +595,7 @@ import {
|
|
582
595
|
import TextareaAutosize from "react-textarea-autosize";
|
583
596
|
import { jsx as jsx11 } from "react/jsx-runtime";
|
584
597
|
var ComposerInput = forwardRef7(
|
585
|
-
({ autoFocus, asChild, disabled, onChange, onKeyDown, ...rest }, forwardedRef) => {
|
598
|
+
({ autoFocus = false, asChild, disabled, onChange, onKeyDown, ...rest }, forwardedRef) => {
|
586
599
|
const { useThread, useViewport } = useAssistantContext();
|
587
600
|
const { useComposer, type } = useComposerContext();
|
588
601
|
const value = useComposer((c) => {
|
@@ -610,7 +623,7 @@ var ComposerInput = forwardRef7(
|
|
610
623
|
};
|
611
624
|
const textareaRef = useRef5(null);
|
612
625
|
const ref = useComposedRefs3(forwardedRef, textareaRef);
|
613
|
-
const autoFocusEnabled = autoFocus
|
626
|
+
const autoFocusEnabled = autoFocus && !disabled;
|
614
627
|
const focus = useCallback(() => {
|
615
628
|
const textarea = textareaRef.current;
|
616
629
|
if (!textarea || !autoFocusEnabled)
|
@@ -894,10 +907,10 @@ var useReloadMessage = () => {
|
|
894
907
|
if (disabled)
|
895
908
|
return null;
|
896
909
|
return () => {
|
897
|
-
const message = useMessage.getState()
|
910
|
+
const { message, parentId } = useMessage.getState();
|
898
911
|
if (message.role !== "assistant")
|
899
912
|
throw new Error("Reloading is only supported on assistant messages");
|
900
|
-
useThread.getState().startRun(
|
913
|
+
useThread.getState().startRun(parentId);
|
901
914
|
useViewport.getState().scrollToBottom();
|
902
915
|
};
|
903
916
|
};
|
@@ -924,7 +937,7 @@ var useBeginMessageEdit = () => {
|
|
924
937
|
var ActionBarEdit = createActionButton(useBeginMessageEdit);
|
925
938
|
|
926
939
|
// src/adapters/vercel/VercelAIAssistantProvider.tsx
|
927
|
-
import {
|
940
|
+
import { useMemo as useMemo4 } from "react";
|
928
941
|
|
929
942
|
// src/adapters/vercel/useDummyAIAssistantContext.tsx
|
930
943
|
import { useState as useState2 } from "react";
|
@@ -982,7 +995,7 @@ var useDummyAIAssistantContext = () => {
|
|
982
995
|
return context;
|
983
996
|
};
|
984
997
|
|
985
|
-
// src/adapters/vercel/
|
998
|
+
// src/adapters/vercel/useVercelAIThreadState.tsx
|
986
999
|
import { useCallback as useCallback2, useMemo as useMemo3, useRef as useRef6, useState as useState3 } from "react";
|
987
1000
|
|
988
1001
|
// src/adapters/MessageRepository.tsx
|
@@ -1011,17 +1024,26 @@ var MessageRepository = class {
|
|
1011
1024
|
}
|
1012
1025
|
return messages;
|
1013
1026
|
}
|
1014
|
-
|
1027
|
+
// TODO previousId is confusing
|
1028
|
+
// TODO previousId does not fix children
|
1029
|
+
// TODO cut / link operations
|
1030
|
+
addOrUpdateMessage(parentId, message, previousId = message.id) {
|
1015
1031
|
const item = this.messages.get(message.id);
|
1016
1032
|
if (item) {
|
1017
|
-
if (item.current.
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1033
|
+
if (item.prev?.current.id !== parentId) {
|
1034
|
+
if ((item.prev?.current.id ?? null) !== parentId) {
|
1035
|
+
this.deleteMessage(message.id);
|
1036
|
+
} else {
|
1037
|
+
item.current = message;
|
1038
|
+
if (previousId !== message.id) {
|
1039
|
+
this.messages.delete(previousId);
|
1040
|
+
this.messages.set(message.id, item);
|
1041
|
+
}
|
1042
|
+
return;
|
1043
|
+
}
|
1022
1044
|
}
|
1023
1045
|
}
|
1024
|
-
const prev =
|
1046
|
+
const prev = parentId ? this.messages.get(parentId) : null;
|
1025
1047
|
if (prev === void 0)
|
1026
1048
|
throw new Error("Unexpected: Parent message not found");
|
1027
1049
|
const newItem = {
|
@@ -1070,16 +1092,27 @@ var MessageRepository = class {
|
|
1070
1092
|
this.head = message.prev ? findHead(message.prev) : null;
|
1071
1093
|
}
|
1072
1094
|
}
|
1073
|
-
getOptimisticId
|
1095
|
+
getOptimisticId() {
|
1074
1096
|
let optimisticId;
|
1075
1097
|
do {
|
1076
1098
|
optimisticId = generateOptimisticId();
|
1077
1099
|
} while (this.messages.has(optimisticId));
|
1078
1100
|
return optimisticId;
|
1079
|
-
}
|
1101
|
+
}
|
1102
|
+
commitOptimisticAppend(message) {
|
1103
|
+
const optimisticIdUser = this.getOptimisticId();
|
1104
|
+
this.addOrUpdateMessage(message.parentId, {
|
1105
|
+
id: optimisticIdUser,
|
1106
|
+
role: "user",
|
1107
|
+
content: message.content,
|
1108
|
+
createdAt: /* @__PURE__ */ new Date()
|
1109
|
+
});
|
1110
|
+
const optimisticIdAssistant = this.commitOptimisticRun(optimisticIdUser);
|
1111
|
+
return [optimisticIdUser, optimisticIdAssistant];
|
1112
|
+
}
|
1080
1113
|
commitOptimisticRun(parentId) {
|
1081
1114
|
const optimisticId = this.getOptimisticId();
|
1082
|
-
this.addOrUpdateMessage({
|
1115
|
+
this.addOrUpdateMessage(parentId, {
|
1083
1116
|
id: optimisticId,
|
1084
1117
|
role: "assistant",
|
1085
1118
|
content: [
|
@@ -1088,7 +1121,6 @@ var MessageRepository = class {
|
|
1088
1121
|
text: ""
|
1089
1122
|
}
|
1090
1123
|
],
|
1091
|
-
parentId,
|
1092
1124
|
createdAt: /* @__PURE__ */ new Date()
|
1093
1125
|
});
|
1094
1126
|
return optimisticId;
|
@@ -1128,7 +1160,37 @@ var MessageRepository = class {
|
|
1128
1160
|
}
|
1129
1161
|
};
|
1130
1162
|
|
1131
|
-
// src/adapters/
|
1163
|
+
// src/adapters/ThreadMessageConverter.tsx
|
1164
|
+
var ThreadMessageConverter = class {
|
1165
|
+
constructor(converter2) {
|
1166
|
+
this.converter = converter2;
|
1167
|
+
}
|
1168
|
+
cache = /* @__PURE__ */ new WeakMap();
|
1169
|
+
convertMessages(messages) {
|
1170
|
+
return messages.map((m) => {
|
1171
|
+
const cached = this.cache.get(m);
|
1172
|
+
if (cached)
|
1173
|
+
return cached;
|
1174
|
+
const newMessage = this.converter(m);
|
1175
|
+
this.cache.set(m, newMessage);
|
1176
|
+
return newMessage;
|
1177
|
+
});
|
1178
|
+
}
|
1179
|
+
};
|
1180
|
+
|
1181
|
+
// src/adapters/vercel/useVercelAIThreadState.tsx
|
1182
|
+
var vercelToThreadMessage = (message) => {
|
1183
|
+
if (message.role !== "user" && message.role !== "assistant")
|
1184
|
+
throw new Error("Unsupported role");
|
1185
|
+
return {
|
1186
|
+
id: message.id,
|
1187
|
+
role: message.role,
|
1188
|
+
content: [{ type: "text", text: message.content }],
|
1189
|
+
createdAt: message.createdAt ?? /* @__PURE__ */ new Date(),
|
1190
|
+
innerMessage: message
|
1191
|
+
};
|
1192
|
+
};
|
1193
|
+
var converter = new ThreadMessageConverter(vercelToThreadMessage);
|
1132
1194
|
var sliceMessagesUntil = (messages, messageId) => {
|
1133
1195
|
if (messageId == null)
|
1134
1196
|
return [];
|
@@ -1142,28 +1204,36 @@ var sliceMessagesUntil = (messages, messageId) => {
|
|
1142
1204
|
var hasUpcomingMessage = (isRunning, messages) => {
|
1143
1205
|
return isRunning && messages[messages.length - 1]?.role !== "assistant";
|
1144
1206
|
};
|
1145
|
-
var
|
1207
|
+
var getIsRunning = (vercel) => {
|
1208
|
+
if ("isLoading" in vercel)
|
1209
|
+
return vercel.isLoading;
|
1210
|
+
return vercel.status === "in_progress";
|
1211
|
+
};
|
1212
|
+
var useVercelAIThreadState = (vercel) => {
|
1146
1213
|
const [data] = useState3(() => new MessageRepository());
|
1147
|
-
const
|
1214
|
+
const vercelRef = useRef6(vercel);
|
1215
|
+
vercelRef.current = vercel;
|
1216
|
+
const isRunning = getIsRunning(vercelRef.current);
|
1148
1217
|
const assistantOptimisticIdRef = useRef6(null);
|
1149
|
-
const
|
1150
|
-
|
1151
|
-
|
1218
|
+
const messages = useMemo3(() => {
|
1219
|
+
const vm = converter.convertMessages(vercel.messages);
|
1220
|
+
for (let i = 0; i < vm.length; i++) {
|
1221
|
+
const message = vm[i];
|
1222
|
+
const parent = vm[i - 1];
|
1223
|
+
data.addOrUpdateMessage(parent?.id ?? null, message);
|
1152
1224
|
}
|
1153
1225
|
if (assistantOptimisticIdRef.current) {
|
1154
1226
|
data.deleteMessage(assistantOptimisticIdRef.current);
|
1155
1227
|
assistantOptimisticIdRef.current = null;
|
1156
1228
|
}
|
1157
|
-
if (hasUpcomingMessage(isRunning,
|
1229
|
+
if (hasUpcomingMessage(isRunning, vm)) {
|
1158
1230
|
assistantOptimisticIdRef.current = data.commitOptimisticRun(
|
1159
|
-
|
1231
|
+
vm.at(-1)?.id ?? null
|
1160
1232
|
);
|
1161
1233
|
}
|
1162
|
-
data.resetHead(
|
1163
|
-
assistantOptimisticIdRef.current ?? messages.at(-1)?.id ?? null
|
1164
|
-
);
|
1234
|
+
data.resetHead(assistantOptimisticIdRef.current ?? vm.at(-1)?.id ?? null);
|
1165
1235
|
return data.getMessages();
|
1166
|
-
}, [data, isRunning, messages]);
|
1236
|
+
}, [data, isRunning, vercel.messages]);
|
1167
1237
|
const getBranches = useCallback2(
|
1168
1238
|
(messageId) => {
|
1169
1239
|
return data.getBranches(messageId);
|
@@ -1173,106 +1243,77 @@ var useVercelAIBranches = (chat, messages) => {
|
|
1173
1243
|
const switchToBranch = useCallback2(
|
1174
1244
|
(messageId) => {
|
1175
1245
|
data.switchToBranch(messageId);
|
1176
|
-
|
1246
|
+
vercelRef.current.setMessages(
|
1177
1247
|
data.getMessages().filter((m) => !isOptimisticId(m.id)).map((m) => m.innerMessage)
|
1178
1248
|
);
|
1179
1249
|
},
|
1180
|
-
[data
|
1181
|
-
);
|
1182
|
-
const reloadMaybe = "reload" in chat ? chat.reload : void 0;
|
1183
|
-
const startRun = useCallback2(
|
1184
|
-
async (parentId) => {
|
1185
|
-
if (!reloadMaybe)
|
1186
|
-
throw new Error("Reload not supported by Vercel AI SDK's useAssistant");
|
1187
|
-
const newMessages = sliceMessagesUntil(chat.messages, parentId);
|
1188
|
-
chat.setMessages(newMessages);
|
1189
|
-
await reloadMaybe();
|
1190
|
-
},
|
1191
|
-
[chat.messages, chat.setMessages, reloadMaybe]
|
1192
|
-
);
|
1193
|
-
const append = useCallback2(
|
1194
|
-
async (message) => {
|
1195
|
-
if (message.content.length !== 1 || message.content[0]?.type !== "text")
|
1196
|
-
throw new Error("Only text content is supported by Vercel AI SDK");
|
1197
|
-
const newMessages = sliceMessagesUntil(chat.messages, message.parentId);
|
1198
|
-
chat.setMessages(newMessages);
|
1199
|
-
await chat.append({
|
1200
|
-
role: "user",
|
1201
|
-
content: message.content[0].text
|
1202
|
-
});
|
1203
|
-
},
|
1204
|
-
[chat.messages, chat.setMessages, chat.append]
|
1250
|
+
[data]
|
1205
1251
|
);
|
1252
|
+
const startRun = useCallback2(async (parentId) => {
|
1253
|
+
const reloadMaybe = "reload" in vercelRef.current ? vercelRef.current.reload : void 0;
|
1254
|
+
if (!reloadMaybe)
|
1255
|
+
throw new Error("Reload not supported by Vercel AI SDK's useAssistant");
|
1256
|
+
const newMessages = sliceMessagesUntil(
|
1257
|
+
vercelRef.current.messages,
|
1258
|
+
parentId
|
1259
|
+
);
|
1260
|
+
vercelRef.current.setMessages(newMessages);
|
1261
|
+
await reloadMaybe();
|
1262
|
+
}, []);
|
1263
|
+
const append = useCallback2(async (message) => {
|
1264
|
+
if (message.content.length !== 1 || message.content[0]?.type !== "text")
|
1265
|
+
throw new Error("Only text content is supported by Vercel AI SDK");
|
1266
|
+
const newMessages = sliceMessagesUntil(
|
1267
|
+
vercelRef.current.messages,
|
1268
|
+
message.parentId
|
1269
|
+
);
|
1270
|
+
vercelRef.current.setMessages(newMessages);
|
1271
|
+
await vercelRef.current.append({
|
1272
|
+
role: "user",
|
1273
|
+
content: message.content[0].text
|
1274
|
+
});
|
1275
|
+
}, []);
|
1276
|
+
const cancelRun = useCallback2(() => {
|
1277
|
+
const lastMessage = vercelRef.current.messages.at(-1);
|
1278
|
+
vercelRef.current.stop();
|
1279
|
+
if (lastMessage?.role === "user") {
|
1280
|
+
vercelRef.current.setInput(lastMessage.content);
|
1281
|
+
}
|
1282
|
+
}, []);
|
1206
1283
|
return useMemo3(
|
1207
1284
|
() => ({
|
1208
|
-
|
1285
|
+
isRunning,
|
1286
|
+
messages,
|
1209
1287
|
getBranches,
|
1210
1288
|
switchToBranch,
|
1211
1289
|
append,
|
1212
|
-
startRun
|
1290
|
+
startRun,
|
1291
|
+
cancelRun
|
1213
1292
|
}),
|
1214
|
-
[
|
1293
|
+
[
|
1294
|
+
isRunning,
|
1295
|
+
messages,
|
1296
|
+
getBranches,
|
1297
|
+
switchToBranch,
|
1298
|
+
append,
|
1299
|
+
startRun,
|
1300
|
+
cancelRun
|
1301
|
+
]
|
1215
1302
|
);
|
1216
1303
|
};
|
1217
1304
|
|
1218
1305
|
// src/adapters/vercel/VercelAIAssistantProvider.tsx
|
1219
1306
|
import { jsx as jsx19 } from "react/jsx-runtime";
|
1220
|
-
var ThreadMessageCache = /* @__PURE__ */ new WeakMap();
|
1221
|
-
var vercelToThreadMessage = (message, parentId) => {
|
1222
|
-
if (message.role !== "user" && message.role !== "assistant")
|
1223
|
-
throw new Error("Unsupported role");
|
1224
|
-
return {
|
1225
|
-
parentId,
|
1226
|
-
id: message.id,
|
1227
|
-
role: message.role,
|
1228
|
-
content: [{ type: "text", text: message.content }],
|
1229
|
-
createdAt: message.createdAt ?? /* @__PURE__ */ new Date(),
|
1230
|
-
innerMessage: message
|
1231
|
-
};
|
1232
|
-
};
|
1233
|
-
var vercelToCachedThreadMessages = (messages) => {
|
1234
|
-
return messages.map((m, idx) => {
|
1235
|
-
const cached = ThreadMessageCache.get(m);
|
1236
|
-
const parentId = messages[idx - 1]?.id ?? null;
|
1237
|
-
if (cached && cached.parentId === parentId)
|
1238
|
-
return cached;
|
1239
|
-
const newMessage = vercelToThreadMessage(m, parentId);
|
1240
|
-
ThreadMessageCache.set(m, newMessage);
|
1241
|
-
return newMessage;
|
1242
|
-
});
|
1243
|
-
};
|
1244
1307
|
var VercelAIAssistantProvider = ({
|
1245
1308
|
children,
|
1246
1309
|
...rest
|
1247
1310
|
}) => {
|
1248
1311
|
const context = useDummyAIAssistantContext();
|
1249
1312
|
const vercel = "chat" in rest ? rest.chat : rest.assistant;
|
1250
|
-
const
|
1251
|
-
return vercelToCachedThreadMessages(vercel.messages);
|
1252
|
-
}, [vercel.messages]);
|
1253
|
-
const branches = useVercelAIBranches(vercel, messages);
|
1254
|
-
const cancelRun = useCallback3(() => {
|
1255
|
-
const lastMessage = vercel.messages.at(-1);
|
1256
|
-
vercel.stop();
|
1257
|
-
if (lastMessage?.role === "user") {
|
1258
|
-
vercel.setInput(lastMessage.content);
|
1259
|
-
}
|
1260
|
-
}, [vercel.messages, vercel.stop, vercel.setInput]);
|
1261
|
-
const isRunning = "isLoading" in vercel ? vercel.isLoading : vercel.status === "in_progress";
|
1313
|
+
const threadState = useVercelAIThreadState(vercel);
|
1262
1314
|
useMemo4(() => {
|
1263
|
-
context.useThread.setState(
|
1264
|
-
|
1265
|
-
messages: branches.messages,
|
1266
|
-
isRunning,
|
1267
|
-
getBranches: branches.getBranches,
|
1268
|
-
switchToBranch: branches.switchToBranch,
|
1269
|
-
append: branches.append,
|
1270
|
-
startRun: branches.startRun,
|
1271
|
-
cancelRun
|
1272
|
-
},
|
1273
|
-
true
|
1274
|
-
);
|
1275
|
-
}, [context, isRunning, cancelRun, branches]);
|
1315
|
+
context.useThread.setState(threadState, true);
|
1316
|
+
}, [context, threadState]);
|
1276
1317
|
useMemo4(() => {
|
1277
1318
|
context.useComposer.setState({
|
1278
1319
|
value: vercel.input,
|
@@ -1284,55 +1325,69 @@ var VercelAIAssistantProvider = ({
|
|
1284
1325
|
|
1285
1326
|
// src/adapters/vercel/VercelRSCAssistantProvider.tsx
|
1286
1327
|
import {
|
1287
|
-
useCallback as
|
1328
|
+
useCallback as useCallback3,
|
1288
1329
|
useMemo as useMemo5
|
1289
1330
|
} from "react";
|
1290
1331
|
import { jsx as jsx20 } from "react/jsx-runtime";
|
1291
|
-
var
|
1292
|
-
var vercelToThreadMessage2 = (parentId, message) => {
|
1332
|
+
var vercelToThreadMessage2 = (message) => {
|
1293
1333
|
if (message.role !== "user" && message.role !== "assistant")
|
1294
1334
|
throw new Error("Unsupported role");
|
1295
1335
|
return {
|
1296
|
-
parentId,
|
1297
1336
|
id: message.id,
|
1298
1337
|
role: message.role,
|
1299
1338
|
content: [{ type: "ui", display: message.display }],
|
1300
1339
|
createdAt: message.createdAt ?? /* @__PURE__ */ new Date()
|
1301
1340
|
};
|
1302
1341
|
};
|
1303
|
-
var
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
return newMessage;
|
1312
|
-
});
|
1313
|
-
};
|
1314
|
-
var VercelRSCAssistantProvider = ({ children, messages: vercelMessages, append: vercelAppend }) => {
|
1342
|
+
var VercelRSCAssistantProvider = ({
|
1343
|
+
children,
|
1344
|
+
convertMessage,
|
1345
|
+
messages: vercelMessages,
|
1346
|
+
append: appendCallback,
|
1347
|
+
edit,
|
1348
|
+
reload
|
1349
|
+
}) => {
|
1315
1350
|
const context = useDummyAIAssistantContext();
|
1351
|
+
const converter2 = useMemo5(() => {
|
1352
|
+
const rscConverter = convertMessage ?? ((m) => m);
|
1353
|
+
return new ThreadMessageConverter((m) => {
|
1354
|
+
return vercelToThreadMessage2(rscConverter(m));
|
1355
|
+
});
|
1356
|
+
}, [convertMessage]);
|
1316
1357
|
const messages = useMemo5(() => {
|
1317
|
-
return
|
1318
|
-
}, [vercelMessages]);
|
1319
|
-
const append =
|
1358
|
+
return converter2.convertMessages(vercelMessages);
|
1359
|
+
}, [converter2, vercelMessages]);
|
1360
|
+
const append = useCallback3(
|
1320
1361
|
async (message) => {
|
1321
|
-
if (message.parentId !== (context.useThread.getState().messages.at(-1)?.id ?? null))
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1362
|
+
if (message.parentId !== (context.useThread.getState().messages.at(-1)?.id ?? null)) {
|
1363
|
+
if (!edit)
|
1364
|
+
throw new Error(
|
1365
|
+
"Unexpected: Message editing is not supported, no edit callback was provided to VercelRSCAssistantProvider."
|
1366
|
+
);
|
1367
|
+
await edit(message);
|
1368
|
+
} else {
|
1369
|
+
await appendCallback(message);
|
1325
1370
|
}
|
1326
|
-
await vercelAppend(message);
|
1327
1371
|
},
|
1328
|
-
[context,
|
1372
|
+
[context, appendCallback, edit]
|
1373
|
+
);
|
1374
|
+
const startRun = useCallback3(
|
1375
|
+
async (parentId) => {
|
1376
|
+
if (!reload)
|
1377
|
+
throw new Error(
|
1378
|
+
"Unexpected: Message reloading is not supported, no reload callback was provided to VercelRSCAssistantProvider."
|
1379
|
+
);
|
1380
|
+
await reload(parentId);
|
1381
|
+
},
|
1382
|
+
[reload]
|
1329
1383
|
);
|
1330
1384
|
useMemo5(() => {
|
1331
1385
|
context.useThread.setState({
|
1332
1386
|
messages,
|
1333
|
-
append
|
1387
|
+
append,
|
1388
|
+
startRun
|
1334
1389
|
});
|
1335
|
-
}, [context, messages, append]);
|
1390
|
+
}, [context, messages, append, startRun]);
|
1336
1391
|
return /* @__PURE__ */ jsx20(AssistantContext.Provider, { value: context, children });
|
1337
1392
|
};
|
1338
1393
|
export {
|