ai 0.0.0-85f9a635-20240518005312 → 0.0.0-8777c42a-20250115032312

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +2863 -0
  2. package/README.md +99 -22
  3. package/dist/index.d.mts +1925 -1592
  4. package/dist/index.d.ts +1925 -1592
  5. package/dist/index.js +5500 -2961
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +5497 -2916
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +39 -100
  10. package/react/dist/index.d.mts +8 -563
  11. package/react/dist/index.d.ts +8 -580
  12. package/react/dist/index.js +7 -1395
  13. package/react/dist/index.js.map +1 -1
  14. package/react/dist/index.mjs +12 -1383
  15. package/react/dist/index.mjs.map +1 -1
  16. package/rsc/dist/index.d.ts +340 -197
  17. package/rsc/dist/rsc-server.d.mts +339 -197
  18. package/rsc/dist/rsc-server.mjs +1295 -1347
  19. package/rsc/dist/rsc-server.mjs.map +1 -1
  20. package/rsc/dist/rsc-shared.d.mts +30 -23
  21. package/rsc/dist/rsc-shared.mjs +69 -105
  22. package/rsc/dist/rsc-shared.mjs.map +1 -1
  23. package/test/dist/index.d.mts +67 -0
  24. package/test/dist/index.d.ts +67 -0
  25. package/test/dist/index.js +131 -0
  26. package/test/dist/index.js.map +1 -0
  27. package/test/dist/index.mjs +101 -0
  28. package/test/dist/index.mjs.map +1 -0
  29. package/prompts/dist/index.d.mts +0 -324
  30. package/prompts/dist/index.d.ts +0 -324
  31. package/prompts/dist/index.js +0 -178
  32. package/prompts/dist/index.js.map +0 -1
  33. package/prompts/dist/index.mjs +0 -146
  34. package/prompts/dist/index.mjs.map +0 -1
  35. package/react/dist/index.server.d.mts +0 -17
  36. package/react/dist/index.server.d.ts +0 -17
  37. package/react/dist/index.server.js +0 -50
  38. package/react/dist/index.server.js.map +0 -1
  39. package/react/dist/index.server.mjs +0 -23
  40. package/react/dist/index.server.mjs.map +0 -1
  41. package/solid/dist/index.d.mts +0 -408
  42. package/solid/dist/index.d.ts +0 -408
  43. package/solid/dist/index.js +0 -1072
  44. package/solid/dist/index.js.map +0 -1
  45. package/solid/dist/index.mjs +0 -1044
  46. package/solid/dist/index.mjs.map +0 -1
  47. package/svelte/dist/index.d.mts +0 -484
  48. package/svelte/dist/index.d.ts +0 -484
  49. package/svelte/dist/index.js +0 -1778
  50. package/svelte/dist/index.js.map +0 -1
  51. package/svelte/dist/index.mjs +0 -1749
  52. package/svelte/dist/index.mjs.map +0 -1
  53. package/vue/dist/index.d.mts +0 -402
  54. package/vue/dist/index.d.ts +0 -402
  55. package/vue/dist/index.js +0 -1072
  56. package/vue/dist/index.js.map +0 -1
  57. package/vue/dist/index.mjs +0 -1034
  58. package/vue/dist/index.mjs.map +0 -1
@@ -1,1044 +0,0 @@
1
- // solid/use-chat.ts
2
- import { createSignal } from "solid-js";
3
- import { useSWRStore } from "solid-swr-store";
4
- import { createSWRStore } from "swr-store";
5
-
6
- // shared/generate-id.ts
7
- import { customAlphabet } from "nanoid/non-secure";
8
- var generateId = customAlphabet(
9
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
10
- 7
11
- );
12
-
13
- // shared/stream-parts.ts
14
- var textStreamPart = {
15
- code: "0",
16
- name: "text",
17
- parse: (value) => {
18
- if (typeof value !== "string") {
19
- throw new Error('"text" parts expect a string value.');
20
- }
21
- return { type: "text", value };
22
- }
23
- };
24
- var functionCallStreamPart = {
25
- code: "1",
26
- name: "function_call",
27
- parse: (value) => {
28
- if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
29
- throw new Error(
30
- '"function_call" parts expect an object with a "function_call" property.'
31
- );
32
- }
33
- return {
34
- type: "function_call",
35
- value
36
- };
37
- }
38
- };
39
- var dataStreamPart = {
40
- code: "2",
41
- name: "data",
42
- parse: (value) => {
43
- if (!Array.isArray(value)) {
44
- throw new Error('"data" parts expect an array value.');
45
- }
46
- return { type: "data", value };
47
- }
48
- };
49
- var errorStreamPart = {
50
- code: "3",
51
- name: "error",
52
- parse: (value) => {
53
- if (typeof value !== "string") {
54
- throw new Error('"error" parts expect a string value.');
55
- }
56
- return { type: "error", value };
57
- }
58
- };
59
- var assistantMessageStreamPart = {
60
- code: "4",
61
- name: "assistant_message",
62
- parse: (value) => {
63
- if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
64
- (item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
65
- )) {
66
- throw new Error(
67
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
68
- );
69
- }
70
- return {
71
- type: "assistant_message",
72
- value
73
- };
74
- }
75
- };
76
- var assistantControlDataStreamPart = {
77
- code: "5",
78
- name: "assistant_control_data",
79
- parse: (value) => {
80
- if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
81
- throw new Error(
82
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
83
- );
84
- }
85
- return {
86
- type: "assistant_control_data",
87
- value: {
88
- threadId: value.threadId,
89
- messageId: value.messageId
90
- }
91
- };
92
- }
93
- };
94
- var dataMessageStreamPart = {
95
- code: "6",
96
- name: "data_message",
97
- parse: (value) => {
98
- if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
99
- throw new Error(
100
- '"data_message" parts expect an object with a "role" and "data" property.'
101
- );
102
- }
103
- return {
104
- type: "data_message",
105
- value
106
- };
107
- }
108
- };
109
- var toolCallsStreamPart = {
110
- code: "7",
111
- name: "tool_calls",
112
- parse: (value) => {
113
- if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
114
- (tc) => tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
115
- )) {
116
- throw new Error(
117
- '"tool_calls" parts expect an object with a ToolCallPayload.'
118
- );
119
- }
120
- return {
121
- type: "tool_calls",
122
- value
123
- };
124
- }
125
- };
126
- var messageAnnotationsStreamPart = {
127
- code: "8",
128
- name: "message_annotations",
129
- parse: (value) => {
130
- if (!Array.isArray(value)) {
131
- throw new Error('"message_annotations" parts expect an array value.');
132
- }
133
- return { type: "message_annotations", value };
134
- }
135
- };
136
- var toolCallStreamPart = {
137
- code: "9",
138
- name: "tool_call",
139
- parse: (value) => {
140
- if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
141
- throw new Error(
142
- '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
143
- );
144
- }
145
- return {
146
- type: "tool_call",
147
- value
148
- };
149
- }
150
- };
151
- var toolResultStreamPart = {
152
- code: "a",
153
- name: "tool_result",
154
- parse: (value) => {
155
- if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
156
- throw new Error(
157
- '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
158
- );
159
- }
160
- return {
161
- type: "tool_result",
162
- value
163
- };
164
- }
165
- };
166
- var streamParts = [
167
- textStreamPart,
168
- functionCallStreamPart,
169
- dataStreamPart,
170
- errorStreamPart,
171
- assistantMessageStreamPart,
172
- assistantControlDataStreamPart,
173
- dataMessageStreamPart,
174
- toolCallsStreamPart,
175
- messageAnnotationsStreamPart,
176
- toolCallStreamPart,
177
- toolResultStreamPart
178
- ];
179
- var streamPartsByCode = {
180
- [textStreamPart.code]: textStreamPart,
181
- [functionCallStreamPart.code]: functionCallStreamPart,
182
- [dataStreamPart.code]: dataStreamPart,
183
- [errorStreamPart.code]: errorStreamPart,
184
- [assistantMessageStreamPart.code]: assistantMessageStreamPart,
185
- [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
186
- [dataMessageStreamPart.code]: dataMessageStreamPart,
187
- [toolCallsStreamPart.code]: toolCallsStreamPart,
188
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
189
- [toolCallStreamPart.code]: toolCallStreamPart,
190
- [toolResultStreamPart.code]: toolResultStreamPart
191
- };
192
- var StreamStringPrefixes = {
193
- [textStreamPart.name]: textStreamPart.code,
194
- [functionCallStreamPart.name]: functionCallStreamPart.code,
195
- [dataStreamPart.name]: dataStreamPart.code,
196
- [errorStreamPart.name]: errorStreamPart.code,
197
- [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
198
- [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
199
- [dataMessageStreamPart.name]: dataMessageStreamPart.code,
200
- [toolCallsStreamPart.name]: toolCallsStreamPart.code,
201
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
202
- [toolCallStreamPart.name]: toolCallStreamPart.code,
203
- [toolResultStreamPart.name]: toolResultStreamPart.code
204
- };
205
- var validCodes = streamParts.map((part) => part.code);
206
- var parseStreamPart = (line) => {
207
- const firstSeparatorIndex = line.indexOf(":");
208
- if (firstSeparatorIndex === -1) {
209
- throw new Error("Failed to parse stream string. No separator found.");
210
- }
211
- const prefix = line.slice(0, firstSeparatorIndex);
212
- if (!validCodes.includes(prefix)) {
213
- throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
214
- }
215
- const code = prefix;
216
- const textValue = line.slice(firstSeparatorIndex + 1);
217
- const jsonValue = JSON.parse(textValue);
218
- return streamPartsByCode[code].parse(jsonValue);
219
- };
220
-
221
- // shared/read-data-stream.ts
222
- var NEWLINE = "\n".charCodeAt(0);
223
- function concatChunks(chunks, totalLength) {
224
- const concatenatedChunks = new Uint8Array(totalLength);
225
- let offset = 0;
226
- for (const chunk of chunks) {
227
- concatenatedChunks.set(chunk, offset);
228
- offset += chunk.length;
229
- }
230
- chunks.length = 0;
231
- return concatenatedChunks;
232
- }
233
- async function* readDataStream(reader, {
234
- isAborted
235
- } = {}) {
236
- const decoder = new TextDecoder();
237
- const chunks = [];
238
- let totalLength = 0;
239
- while (true) {
240
- const { value } = await reader.read();
241
- if (value) {
242
- chunks.push(value);
243
- totalLength += value.length;
244
- if (value[value.length - 1] !== NEWLINE) {
245
- continue;
246
- }
247
- }
248
- if (chunks.length === 0) {
249
- break;
250
- }
251
- const concatenatedChunks = concatChunks(chunks, totalLength);
252
- totalLength = 0;
253
- const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
254
- for (const streamPart of streamParts2) {
255
- yield streamPart;
256
- }
257
- if (isAborted == null ? void 0 : isAborted()) {
258
- reader.cancel();
259
- break;
260
- }
261
- }
262
- }
263
-
264
- // shared/parse-complex-response.ts
265
- function assignAnnotationsToMessage(message, annotations) {
266
- if (!message || !annotations || !annotations.length)
267
- return message;
268
- return { ...message, annotations: [...annotations] };
269
- }
270
- async function parseComplexResponse({
271
- reader,
272
- abortControllerRef,
273
- update,
274
- onFinish,
275
- generateId: generateId2 = generateId,
276
- getCurrentDate = () => /* @__PURE__ */ new Date()
277
- }) {
278
- const createdAt = getCurrentDate();
279
- const prefixMap = {
280
- data: []
281
- };
282
- let message_annotations = void 0;
283
- for await (const { type, value } of readDataStream(reader, {
284
- isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
285
- })) {
286
- if (type === "text") {
287
- if (prefixMap["text"]) {
288
- prefixMap["text"] = {
289
- ...prefixMap["text"],
290
- content: (prefixMap["text"].content || "") + value
291
- };
292
- } else {
293
- prefixMap["text"] = {
294
- id: generateId2(),
295
- role: "assistant",
296
- content: value,
297
- createdAt
298
- };
299
- }
300
- }
301
- if (type === "tool_call") {
302
- if (prefixMap.text == null) {
303
- prefixMap.text = {
304
- id: generateId2(),
305
- role: "assistant",
306
- content: "",
307
- createdAt
308
- };
309
- }
310
- if (prefixMap.text.toolInvocations == null) {
311
- prefixMap.text.toolInvocations = [];
312
- }
313
- prefixMap.text.toolInvocations.push(value);
314
- } else if (type === "tool_result") {
315
- if (prefixMap.text == null) {
316
- prefixMap.text = {
317
- id: generateId2(),
318
- role: "assistant",
319
- content: "",
320
- createdAt
321
- };
322
- }
323
- if (prefixMap.text.toolInvocations == null) {
324
- prefixMap.text.toolInvocations = [];
325
- }
326
- const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
327
- (invocation) => invocation.toolCallId === value.toolCallId
328
- );
329
- if (toolInvocationIndex !== -1) {
330
- prefixMap.text.toolInvocations[toolInvocationIndex] = value;
331
- } else {
332
- prefixMap.text.toolInvocations.push(value);
333
- }
334
- }
335
- let functionCallMessage = null;
336
- if (type === "function_call") {
337
- prefixMap["function_call"] = {
338
- id: generateId2(),
339
- role: "assistant",
340
- content: "",
341
- function_call: value.function_call,
342
- name: value.function_call.name,
343
- createdAt
344
- };
345
- functionCallMessage = prefixMap["function_call"];
346
- }
347
- let toolCallMessage = null;
348
- if (type === "tool_calls") {
349
- prefixMap["tool_calls"] = {
350
- id: generateId2(),
351
- role: "assistant",
352
- content: "",
353
- tool_calls: value.tool_calls,
354
- createdAt
355
- };
356
- toolCallMessage = prefixMap["tool_calls"];
357
- }
358
- if (type === "data") {
359
- prefixMap["data"].push(...value);
360
- }
361
- let responseMessage = prefixMap["text"];
362
- if (type === "message_annotations") {
363
- if (!message_annotations) {
364
- message_annotations = [...value];
365
- } else {
366
- message_annotations.push(...value);
367
- }
368
- functionCallMessage = assignAnnotationsToMessage(
369
- prefixMap["function_call"],
370
- message_annotations
371
- );
372
- toolCallMessage = assignAnnotationsToMessage(
373
- prefixMap["tool_calls"],
374
- message_annotations
375
- );
376
- responseMessage = assignAnnotationsToMessage(
377
- prefixMap["text"],
378
- message_annotations
379
- );
380
- }
381
- if (message_annotations == null ? void 0 : message_annotations.length) {
382
- const messagePrefixKeys = [
383
- "text",
384
- "function_call",
385
- "tool_calls"
386
- ];
387
- messagePrefixKeys.forEach((key) => {
388
- if (prefixMap[key]) {
389
- prefixMap[key].annotations = [...message_annotations];
390
- }
391
- });
392
- }
393
- const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
394
- ...assignAnnotationsToMessage(message, message_annotations)
395
- }));
396
- update(merged, [...prefixMap["data"]]);
397
- }
398
- onFinish == null ? void 0 : onFinish(prefixMap);
399
- return {
400
- messages: [
401
- prefixMap.text,
402
- prefixMap.function_call,
403
- prefixMap.tool_calls
404
- ].filter(Boolean),
405
- data: prefixMap.data
406
- };
407
- }
408
-
409
- // shared/utils.ts
410
- function createChunkDecoder(complex) {
411
- const decoder = new TextDecoder();
412
- if (!complex) {
413
- return function(chunk) {
414
- if (!chunk)
415
- return "";
416
- return decoder.decode(chunk, { stream: true });
417
- };
418
- }
419
- return function(chunk) {
420
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
421
- return decoded.map(parseStreamPart).filter(Boolean);
422
- };
423
- }
424
-
425
- // shared/call-chat-api.ts
426
- async function callChatApi({
427
- api,
428
- messages,
429
- body,
430
- streamMode = "stream-data",
431
- credentials,
432
- headers,
433
- abortController,
434
- restoreMessagesOnFailure,
435
- onResponse,
436
- onUpdate,
437
- onFinish,
438
- generateId: generateId2
439
- }) {
440
- var _a;
441
- const response = await fetch(api, {
442
- method: "POST",
443
- body: JSON.stringify({
444
- messages,
445
- ...body
446
- }),
447
- headers: {
448
- "Content-Type": "application/json",
449
- ...headers
450
- },
451
- signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
452
- credentials
453
- }).catch((err) => {
454
- restoreMessagesOnFailure();
455
- throw err;
456
- });
457
- if (onResponse) {
458
- try {
459
- await onResponse(response);
460
- } catch (err) {
461
- throw err;
462
- }
463
- }
464
- if (!response.ok) {
465
- restoreMessagesOnFailure();
466
- throw new Error(
467
- await response.text() || "Failed to fetch the chat response."
468
- );
469
- }
470
- if (!response.body) {
471
- throw new Error("The response body is empty.");
472
- }
473
- const reader = response.body.getReader();
474
- switch (streamMode) {
475
- case "text": {
476
- const decoder = createChunkDecoder();
477
- const resultMessage = {
478
- id: generateId2(),
479
- createdAt: /* @__PURE__ */ new Date(),
480
- role: "assistant",
481
- content: ""
482
- };
483
- while (true) {
484
- const { done, value } = await reader.read();
485
- if (done) {
486
- break;
487
- }
488
- resultMessage.content += decoder(value);
489
- resultMessage.id = generateId2();
490
- onUpdate([{ ...resultMessage }], []);
491
- if ((abortController == null ? void 0 : abortController()) === null) {
492
- reader.cancel();
493
- break;
494
- }
495
- }
496
- onFinish == null ? void 0 : onFinish(resultMessage);
497
- return {
498
- messages: [resultMessage],
499
- data: []
500
- };
501
- }
502
- case "stream-data": {
503
- return await parseComplexResponse({
504
- reader,
505
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
506
- update: onUpdate,
507
- onFinish(prefixMap) {
508
- if (onFinish && prefixMap.text != null) {
509
- onFinish(prefixMap.text);
510
- }
511
- },
512
- generateId: generateId2
513
- });
514
- }
515
- default: {
516
- const exhaustiveCheck = streamMode;
517
- throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
518
- }
519
- }
520
- }
521
-
522
- // shared/process-chat-stream.ts
523
- async function processChatStream({
524
- getStreamedResponse,
525
- experimental_onFunctionCall,
526
- experimental_onToolCall,
527
- updateChatRequest,
528
- getCurrentMessages
529
- }) {
530
- while (true) {
531
- const messagesAndDataOrJustMessage = await getStreamedResponse();
532
- if ("messages" in messagesAndDataOrJustMessage) {
533
- let hasFollowingResponse = false;
534
- for (const message of messagesAndDataOrJustMessage.messages) {
535
- if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
536
- continue;
537
- }
538
- hasFollowingResponse = true;
539
- if (experimental_onFunctionCall) {
540
- const functionCall = message.function_call;
541
- if (typeof functionCall !== "object") {
542
- console.warn(
543
- "experimental_onFunctionCall should not be defined when using tools"
544
- );
545
- continue;
546
- }
547
- const functionCallResponse = await experimental_onFunctionCall(
548
- getCurrentMessages(),
549
- functionCall
550
- );
551
- if (functionCallResponse === void 0) {
552
- hasFollowingResponse = false;
553
- break;
554
- }
555
- updateChatRequest(functionCallResponse);
556
- }
557
- if (experimental_onToolCall) {
558
- const toolCalls = message.tool_calls;
559
- if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
560
- console.warn(
561
- "experimental_onToolCall should not be defined when using tools"
562
- );
563
- continue;
564
- }
565
- const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
566
- if (toolCallResponse === void 0) {
567
- hasFollowingResponse = false;
568
- break;
569
- }
570
- updateChatRequest(toolCallResponse);
571
- }
572
- }
573
- if (!hasFollowingResponse) {
574
- break;
575
- }
576
- } else {
577
- let fixFunctionCallArguments2 = function(response) {
578
- for (const message of response.messages) {
579
- if (message.tool_calls !== void 0) {
580
- for (const toolCall of message.tool_calls) {
581
- if (typeof toolCall === "object") {
582
- if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
583
- toolCall.function.arguments = JSON.stringify(
584
- toolCall.function.arguments
585
- );
586
- }
587
- }
588
- }
589
- }
590
- if (message.function_call !== void 0) {
591
- if (typeof message.function_call === "object") {
592
- if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
593
- message.function_call.arguments = JSON.stringify(
594
- message.function_call.arguments
595
- );
596
- }
597
- }
598
- }
599
- }
600
- };
601
- var fixFunctionCallArguments = fixFunctionCallArguments2;
602
- const streamedResponseMessage = messagesAndDataOrJustMessage;
603
- if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
604
- break;
605
- }
606
- if (experimental_onFunctionCall) {
607
- const functionCall = streamedResponseMessage.function_call;
608
- if (!(typeof functionCall === "object")) {
609
- console.warn(
610
- "experimental_onFunctionCall should not be defined when using tools"
611
- );
612
- continue;
613
- }
614
- const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
615
- if (functionCallResponse === void 0)
616
- break;
617
- fixFunctionCallArguments2(functionCallResponse);
618
- updateChatRequest(functionCallResponse);
619
- }
620
- if (experimental_onToolCall) {
621
- const toolCalls = streamedResponseMessage.tool_calls;
622
- if (!(typeof toolCalls === "object")) {
623
- console.warn(
624
- "experimental_onToolCall should not be defined when using functions"
625
- );
626
- continue;
627
- }
628
- const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
629
- if (toolCallResponse === void 0)
630
- break;
631
- fixFunctionCallArguments2(toolCallResponse);
632
- updateChatRequest(toolCallResponse);
633
- }
634
- }
635
- }
636
- }
637
-
638
- // solid/use-chat.ts
639
- var uniqueId = 0;
640
- var store = {};
641
- var chatApiStore = createSWRStore({
642
- get: async (key) => {
643
- var _a;
644
- return (_a = store[key]) != null ? _a : [];
645
- }
646
- });
647
- function useChat({
648
- api = "/api/chat",
649
- id,
650
- initialMessages = [],
651
- initialInput = "",
652
- sendExtraMessageFields,
653
- experimental_onFunctionCall,
654
- onResponse,
655
- onFinish,
656
- onError,
657
- credentials,
658
- headers,
659
- body,
660
- streamMode,
661
- generateId: generateId2 = generateId
662
- } = {}) {
663
- const chatId = id || `chat-${uniqueId++}`;
664
- const key = `${api}|${chatId}`;
665
- const messages = useSWRStore(chatApiStore, () => [key], {
666
- initialData: initialMessages
667
- });
668
- const mutate = (data) => {
669
- store[key] = data;
670
- return chatApiStore.mutate([key], {
671
- status: "success",
672
- data
673
- });
674
- };
675
- const [error, setError] = createSignal(void 0);
676
- const [streamData, setStreamData] = createSignal(
677
- void 0
678
- );
679
- const [isLoading, setIsLoading] = createSignal(false);
680
- let abortController = null;
681
- async function triggerRequest(messagesSnapshot, { options, data } = {}) {
682
- try {
683
- setError(void 0);
684
- setIsLoading(true);
685
- abortController = new AbortController();
686
- const getCurrentMessages = () => chatApiStore.get([key], {
687
- shouldRevalidate: false
688
- });
689
- const previousMessages = getCurrentMessages();
690
- mutate(messagesSnapshot);
691
- let chatRequest = {
692
- messages: messagesSnapshot,
693
- options,
694
- data
695
- };
696
- await processChatStream({
697
- getStreamedResponse: async () => {
698
- var _a;
699
- const existingData = (_a = streamData()) != null ? _a : [];
700
- return await callChatApi({
701
- api,
702
- messages: sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
703
- ({ role, content, name, function_call }) => ({
704
- role,
705
- content,
706
- ...name !== void 0 && { name },
707
- ...function_call !== void 0 && {
708
- function_call
709
- }
710
- })
711
- ),
712
- body: {
713
- data: chatRequest.data,
714
- ...body,
715
- ...options == null ? void 0 : options.body
716
- },
717
- streamMode,
718
- headers: {
719
- ...headers,
720
- ...options == null ? void 0 : options.headers
721
- },
722
- abortController: () => abortController,
723
- credentials,
724
- onResponse,
725
- onUpdate(merged, data2) {
726
- mutate([...chatRequest.messages, ...merged]);
727
- setStreamData([...existingData, ...data2 != null ? data2 : []]);
728
- },
729
- onFinish,
730
- restoreMessagesOnFailure() {
731
- if (previousMessages.status === "success") {
732
- mutate(previousMessages.data);
733
- }
734
- },
735
- generateId: generateId2
736
- });
737
- },
738
- experimental_onFunctionCall,
739
- updateChatRequest(newChatRequest) {
740
- chatRequest = newChatRequest;
741
- },
742
- getCurrentMessages: () => getCurrentMessages().data
743
- });
744
- abortController = null;
745
- } catch (err) {
746
- if (err.name === "AbortError") {
747
- abortController = null;
748
- return null;
749
- }
750
- if (onError && err instanceof Error) {
751
- onError(err);
752
- }
753
- setError(err);
754
- } finally {
755
- setIsLoading(false);
756
- }
757
- }
758
- const append = async (message, options) => {
759
- var _a;
760
- if (!message.id) {
761
- message.id = generateId2();
762
- }
763
- return triggerRequest(
764
- ((_a = messages()) != null ? _a : []).concat(message),
765
- options
766
- );
767
- };
768
- const reload = async (options) => {
769
- const messagesSnapshot = messages();
770
- if (!messagesSnapshot || messagesSnapshot.length === 0)
771
- return null;
772
- const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
773
- if (lastMessage.role === "assistant") {
774
- return triggerRequest(messagesSnapshot.slice(0, -1), options);
775
- }
776
- return triggerRequest(messagesSnapshot, options);
777
- };
778
- const stop = () => {
779
- if (abortController) {
780
- abortController.abort();
781
- abortController = null;
782
- }
783
- };
784
- const setMessages = (messages2) => {
785
- mutate(messages2);
786
- };
787
- const [input, setInput] = createSignal(initialInput);
788
- const handleSubmit = (e, options = {}) => {
789
- e.preventDefault();
790
- const inputValue = input();
791
- if (!inputValue)
792
- return;
793
- append(
794
- {
795
- content: inputValue,
796
- role: "user",
797
- createdAt: /* @__PURE__ */ new Date()
798
- },
799
- options
800
- );
801
- setInput("");
802
- };
803
- return {
804
- messages,
805
- append,
806
- error,
807
- reload,
808
- stop,
809
- setMessages,
810
- input,
811
- setInput,
812
- handleSubmit,
813
- isLoading,
814
- data: streamData
815
- };
816
- }
817
-
818
- // solid/use-completion.ts
819
- import { createSignal as createSignal2 } from "solid-js";
820
- import { useSWRStore as useSWRStore2 } from "solid-swr-store";
821
- import { createSWRStore as createSWRStore2 } from "swr-store";
822
-
823
- // shared/call-completion-api.ts
824
- async function callCompletionApi({
825
- api,
826
- prompt,
827
- credentials,
828
- headers,
829
- body,
830
- streamMode = "stream-data",
831
- setCompletion,
832
- setLoading,
833
- setError,
834
- setAbortController,
835
- onResponse,
836
- onFinish,
837
- onError,
838
- onData
839
- }) {
840
- try {
841
- setLoading(true);
842
- setError(void 0);
843
- const abortController = new AbortController();
844
- setAbortController(abortController);
845
- setCompletion("");
846
- const res = await fetch(api, {
847
- method: "POST",
848
- body: JSON.stringify({
849
- prompt,
850
- ...body
851
- }),
852
- credentials,
853
- headers: {
854
- "Content-Type": "application/json",
855
- ...headers
856
- },
857
- signal: abortController.signal
858
- }).catch((err) => {
859
- throw err;
860
- });
861
- if (onResponse) {
862
- try {
863
- await onResponse(res);
864
- } catch (err) {
865
- throw err;
866
- }
867
- }
868
- if (!res.ok) {
869
- throw new Error(
870
- await res.text() || "Failed to fetch the chat response."
871
- );
872
- }
873
- if (!res.body) {
874
- throw new Error("The response body is empty.");
875
- }
876
- let result = "";
877
- const reader = res.body.getReader();
878
- switch (streamMode) {
879
- case "text": {
880
- const decoder = createChunkDecoder();
881
- while (true) {
882
- const { done, value } = await reader.read();
883
- if (done) {
884
- break;
885
- }
886
- result += decoder(value);
887
- setCompletion(result);
888
- if (abortController === null) {
889
- reader.cancel();
890
- break;
891
- }
892
- }
893
- break;
894
- }
895
- case "stream-data": {
896
- for await (const { type, value } of readDataStream(reader, {
897
- isAborted: () => abortController === null
898
- })) {
899
- switch (type) {
900
- case "text": {
901
- result += value;
902
- setCompletion(result);
903
- break;
904
- }
905
- case "data": {
906
- onData == null ? void 0 : onData(value);
907
- break;
908
- }
909
- }
910
- }
911
- break;
912
- }
913
- default: {
914
- const exhaustiveCheck = streamMode;
915
- throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
916
- }
917
- }
918
- if (onFinish) {
919
- onFinish(prompt, result);
920
- }
921
- setAbortController(null);
922
- return result;
923
- } catch (err) {
924
- if (err.name === "AbortError") {
925
- setAbortController(null);
926
- return null;
927
- }
928
- if (err instanceof Error) {
929
- if (onError) {
930
- onError(err);
931
- }
932
- }
933
- setError(err);
934
- } finally {
935
- setLoading(false);
936
- }
937
- }
938
-
939
- // solid/use-completion.ts
940
- var uniqueId2 = 0;
941
- var store2 = {};
942
- var completionApiStore = createSWRStore2({
943
- get: async (key) => {
944
- var _a;
945
- return (_a = store2[key]) != null ? _a : [];
946
- }
947
- });
948
- function useCompletion({
949
- api = "/api/completion",
950
- id,
951
- initialCompletion = "",
952
- initialInput = "",
953
- credentials,
954
- headers,
955
- body,
956
- streamMode,
957
- onResponse,
958
- onFinish,
959
- onError
960
- } = {}) {
961
- const completionId = id || `completion-${uniqueId2++}`;
962
- const key = `${api}|${completionId}`;
963
- const data = useSWRStore2(completionApiStore, () => [key], {
964
- initialData: initialCompletion
965
- });
966
- const mutate = (data2) => {
967
- store2[key] = data2;
968
- return completionApiStore.mutate([key], {
969
- data: data2,
970
- status: "success"
971
- });
972
- };
973
- const completion = data;
974
- const [error, setError] = createSignal2(void 0);
975
- const [streamData, setStreamData] = createSignal2(
976
- void 0
977
- );
978
- const [isLoading, setIsLoading] = createSignal2(false);
979
- let abortController = null;
980
- const complete = async (prompt, options) => {
981
- var _a;
982
- const existingData = (_a = streamData()) != null ? _a : [];
983
- return callCompletionApi({
984
- api,
985
- prompt,
986
- credentials,
987
- headers: {
988
- ...headers,
989
- ...options == null ? void 0 : options.headers
990
- },
991
- body: {
992
- ...body,
993
- ...options == null ? void 0 : options.body
994
- },
995
- streamMode,
996
- setCompletion: mutate,
997
- setLoading: setIsLoading,
998
- setError,
999
- setAbortController: (controller) => {
1000
- abortController = controller;
1001
- },
1002
- onResponse,
1003
- onFinish,
1004
- onError,
1005
- onData: (data2) => {
1006
- setStreamData([...existingData, ...data2 != null ? data2 : []]);
1007
- }
1008
- });
1009
- };
1010
- const stop = () => {
1011
- if (abortController) {
1012
- abortController.abort();
1013
- abortController = null;
1014
- }
1015
- };
1016
- const setCompletion = (completion2) => {
1017
- mutate(completion2);
1018
- };
1019
- const [input, setInput] = createSignal2(initialInput);
1020
- const handleSubmit = (e) => {
1021
- e.preventDefault();
1022
- const inputValue = input();
1023
- if (!inputValue)
1024
- return;
1025
- return complete(inputValue);
1026
- };
1027
- return {
1028
- completion,
1029
- complete,
1030
- error,
1031
- stop,
1032
- setCompletion,
1033
- input,
1034
- setInput,
1035
- handleSubmit,
1036
- isLoading,
1037
- data: streamData
1038
- };
1039
- }
1040
- export {
1041
- useChat,
1042
- useCompletion
1043
- };
1044
- //# sourceMappingURL=index.mjs.map