ai 3.1.29 → 3.1.31
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.mts +4 -563
- package/dist/index.d.ts +4 -563
- package/dist/index.js +38 -509
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -497
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -33
- package/prompts/dist/index.d.mts +2 -129
- package/prompts/dist/index.d.ts +2 -129
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +9 -648
- package/react/dist/index.d.ts +9 -648
- package/react/dist/index.js +4 -1441
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +9 -1429
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +15 -236
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +7 -465
- package/solid/dist/index.d.ts +7 -465
- package/solid/dist/index.js +3 -1057
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +7 -1056
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +12 -422
- package/svelte/dist/index.d.ts +12 -422
- package/svelte/dist/index.js +12 -768
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +10 -762
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +7 -459
- package/vue/dist/index.d.ts +7 -459
- package/vue/dist/index.js +3 -1057
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +7 -1046
- package/vue/dist/index.mjs.map +1 -1
package/svelte/dist/index.mjs
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
// svelte/use-chat.ts
|
2
|
+
import {
|
3
|
+
callChatApi,
|
4
|
+
generateId as generateIdFunc,
|
5
|
+
processChatStream
|
6
|
+
} from "@ai-sdk/ui-utils";
|
7
|
+
|
1
8
|
// ../../node_modules/.pnpm/swrev@4.0.0/node_modules/swrev/dist/swrev.mjs
|
2
9
|
var P = Object.defineProperty;
|
3
10
|
var F = (r, e, t) => e in r ? P(r, e, { enumerable: true, configurable: true, writable: true, value: t }) : r[e] = t;
|
@@ -497,649 +504,6 @@ var F2 = (t, e) => c.useSWR(t, e);
|
|
497
504
|
|
498
505
|
// svelte/use-chat.ts
|
499
506
|
import { derived, get, writable } from "svelte/store";
|
500
|
-
|
501
|
-
// shared/generate-id.ts
|
502
|
-
import { customAlphabet } from "nanoid/non-secure";
|
503
|
-
var generateId = customAlphabet(
|
504
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
505
|
-
7
|
506
|
-
);
|
507
|
-
|
508
|
-
// shared/stream-parts.ts
|
509
|
-
var textStreamPart = {
|
510
|
-
code: "0",
|
511
|
-
name: "text",
|
512
|
-
parse: (value) => {
|
513
|
-
if (typeof value !== "string") {
|
514
|
-
throw new Error('"text" parts expect a string value.');
|
515
|
-
}
|
516
|
-
return { type: "text", value };
|
517
|
-
}
|
518
|
-
};
|
519
|
-
var functionCallStreamPart = {
|
520
|
-
code: "1",
|
521
|
-
name: "function_call",
|
522
|
-
parse: (value) => {
|
523
|
-
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") {
|
524
|
-
throw new Error(
|
525
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
526
|
-
);
|
527
|
-
}
|
528
|
-
return {
|
529
|
-
type: "function_call",
|
530
|
-
value
|
531
|
-
};
|
532
|
-
}
|
533
|
-
};
|
534
|
-
var dataStreamPart = {
|
535
|
-
code: "2",
|
536
|
-
name: "data",
|
537
|
-
parse: (value) => {
|
538
|
-
if (!Array.isArray(value)) {
|
539
|
-
throw new Error('"data" parts expect an array value.');
|
540
|
-
}
|
541
|
-
return { type: "data", value };
|
542
|
-
}
|
543
|
-
};
|
544
|
-
var errorStreamPart = {
|
545
|
-
code: "3",
|
546
|
-
name: "error",
|
547
|
-
parse: (value) => {
|
548
|
-
if (typeof value !== "string") {
|
549
|
-
throw new Error('"error" parts expect a string value.');
|
550
|
-
}
|
551
|
-
return { type: "error", value };
|
552
|
-
}
|
553
|
-
};
|
554
|
-
var assistantMessageStreamPart = {
|
555
|
-
code: "4",
|
556
|
-
name: "assistant_message",
|
557
|
-
parse: (value) => {
|
558
|
-
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(
|
559
|
-
(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"
|
560
|
-
)) {
|
561
|
-
throw new Error(
|
562
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
563
|
-
);
|
564
|
-
}
|
565
|
-
return {
|
566
|
-
type: "assistant_message",
|
567
|
-
value
|
568
|
-
};
|
569
|
-
}
|
570
|
-
};
|
571
|
-
var assistantControlDataStreamPart = {
|
572
|
-
code: "5",
|
573
|
-
name: "assistant_control_data",
|
574
|
-
parse: (value) => {
|
575
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
576
|
-
throw new Error(
|
577
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
578
|
-
);
|
579
|
-
}
|
580
|
-
return {
|
581
|
-
type: "assistant_control_data",
|
582
|
-
value: {
|
583
|
-
threadId: value.threadId,
|
584
|
-
messageId: value.messageId
|
585
|
-
}
|
586
|
-
};
|
587
|
-
}
|
588
|
-
};
|
589
|
-
var dataMessageStreamPart = {
|
590
|
-
code: "6",
|
591
|
-
name: "data_message",
|
592
|
-
parse: (value) => {
|
593
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
594
|
-
throw new Error(
|
595
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
596
|
-
);
|
597
|
-
}
|
598
|
-
return {
|
599
|
-
type: "data_message",
|
600
|
-
value
|
601
|
-
};
|
602
|
-
}
|
603
|
-
};
|
604
|
-
var toolCallsStreamPart = {
|
605
|
-
code: "7",
|
606
|
-
name: "tool_calls",
|
607
|
-
parse: (value) => {
|
608
|
-
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(
|
609
|
-
(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"
|
610
|
-
)) {
|
611
|
-
throw new Error(
|
612
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
613
|
-
);
|
614
|
-
}
|
615
|
-
return {
|
616
|
-
type: "tool_calls",
|
617
|
-
value
|
618
|
-
};
|
619
|
-
}
|
620
|
-
};
|
621
|
-
var messageAnnotationsStreamPart = {
|
622
|
-
code: "8",
|
623
|
-
name: "message_annotations",
|
624
|
-
parse: (value) => {
|
625
|
-
if (!Array.isArray(value)) {
|
626
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
627
|
-
}
|
628
|
-
return { type: "message_annotations", value };
|
629
|
-
}
|
630
|
-
};
|
631
|
-
var toolCallStreamPart = {
|
632
|
-
code: "9",
|
633
|
-
name: "tool_call",
|
634
|
-
parse: (value) => {
|
635
|
-
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") {
|
636
|
-
throw new Error(
|
637
|
-
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
638
|
-
);
|
639
|
-
}
|
640
|
-
return {
|
641
|
-
type: "tool_call",
|
642
|
-
value
|
643
|
-
};
|
644
|
-
}
|
645
|
-
};
|
646
|
-
var toolResultStreamPart = {
|
647
|
-
code: "a",
|
648
|
-
name: "tool_result",
|
649
|
-
parse: (value) => {
|
650
|
-
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)) {
|
651
|
-
throw new Error(
|
652
|
-
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
653
|
-
);
|
654
|
-
}
|
655
|
-
return {
|
656
|
-
type: "tool_result",
|
657
|
-
value
|
658
|
-
};
|
659
|
-
}
|
660
|
-
};
|
661
|
-
var streamParts = [
|
662
|
-
textStreamPart,
|
663
|
-
functionCallStreamPart,
|
664
|
-
dataStreamPart,
|
665
|
-
errorStreamPart,
|
666
|
-
assistantMessageStreamPart,
|
667
|
-
assistantControlDataStreamPart,
|
668
|
-
dataMessageStreamPart,
|
669
|
-
toolCallsStreamPart,
|
670
|
-
messageAnnotationsStreamPart,
|
671
|
-
toolCallStreamPart,
|
672
|
-
toolResultStreamPart
|
673
|
-
];
|
674
|
-
var streamPartsByCode = {
|
675
|
-
[textStreamPart.code]: textStreamPart,
|
676
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
677
|
-
[dataStreamPart.code]: dataStreamPart,
|
678
|
-
[errorStreamPart.code]: errorStreamPart,
|
679
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
680
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
681
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
682
|
-
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
683
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
684
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
685
|
-
[toolResultStreamPart.code]: toolResultStreamPart
|
686
|
-
};
|
687
|
-
var StreamStringPrefixes = {
|
688
|
-
[textStreamPart.name]: textStreamPart.code,
|
689
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
690
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
691
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
692
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
693
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
694
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
695
|
-
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
696
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
697
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
698
|
-
[toolResultStreamPart.name]: toolResultStreamPart.code
|
699
|
-
};
|
700
|
-
var validCodes = streamParts.map((part) => part.code);
|
701
|
-
var parseStreamPart = (line) => {
|
702
|
-
const firstSeparatorIndex = line.indexOf(":");
|
703
|
-
if (firstSeparatorIndex === -1) {
|
704
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
705
|
-
}
|
706
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
707
|
-
if (!validCodes.includes(prefix)) {
|
708
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
709
|
-
}
|
710
|
-
const code = prefix;
|
711
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
712
|
-
const jsonValue = JSON.parse(textValue);
|
713
|
-
return streamPartsByCode[code].parse(jsonValue);
|
714
|
-
};
|
715
|
-
|
716
|
-
// shared/read-data-stream.ts
|
717
|
-
var NEWLINE = "\n".charCodeAt(0);
|
718
|
-
function concatChunks(chunks, totalLength) {
|
719
|
-
const concatenatedChunks = new Uint8Array(totalLength);
|
720
|
-
let offset = 0;
|
721
|
-
for (const chunk of chunks) {
|
722
|
-
concatenatedChunks.set(chunk, offset);
|
723
|
-
offset += chunk.length;
|
724
|
-
}
|
725
|
-
chunks.length = 0;
|
726
|
-
return concatenatedChunks;
|
727
|
-
}
|
728
|
-
async function* readDataStream(reader, {
|
729
|
-
isAborted
|
730
|
-
} = {}) {
|
731
|
-
const decoder = new TextDecoder();
|
732
|
-
const chunks = [];
|
733
|
-
let totalLength = 0;
|
734
|
-
while (true) {
|
735
|
-
const { value } = await reader.read();
|
736
|
-
if (value) {
|
737
|
-
chunks.push(value);
|
738
|
-
totalLength += value.length;
|
739
|
-
if (value[value.length - 1] !== NEWLINE) {
|
740
|
-
continue;
|
741
|
-
}
|
742
|
-
}
|
743
|
-
if (chunks.length === 0) {
|
744
|
-
break;
|
745
|
-
}
|
746
|
-
const concatenatedChunks = concatChunks(chunks, totalLength);
|
747
|
-
totalLength = 0;
|
748
|
-
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
749
|
-
for (const streamPart of streamParts2) {
|
750
|
-
yield streamPart;
|
751
|
-
}
|
752
|
-
if (isAborted == null ? void 0 : isAborted()) {
|
753
|
-
reader.cancel();
|
754
|
-
break;
|
755
|
-
}
|
756
|
-
}
|
757
|
-
}
|
758
|
-
|
759
|
-
// shared/parse-complex-response.ts
|
760
|
-
function assignAnnotationsToMessage(message, annotations) {
|
761
|
-
if (!message || !annotations || !annotations.length)
|
762
|
-
return message;
|
763
|
-
return { ...message, annotations: [...annotations] };
|
764
|
-
}
|
765
|
-
async function parseComplexResponse({
|
766
|
-
reader,
|
767
|
-
abortControllerRef,
|
768
|
-
update,
|
769
|
-
onToolCall,
|
770
|
-
onFinish,
|
771
|
-
generateId: generateId2 = generateId,
|
772
|
-
getCurrentDate = () => /* @__PURE__ */ new Date()
|
773
|
-
}) {
|
774
|
-
const createdAt = getCurrentDate();
|
775
|
-
const prefixMap = {
|
776
|
-
data: []
|
777
|
-
};
|
778
|
-
let message_annotations = void 0;
|
779
|
-
for await (const { type, value } of readDataStream(reader, {
|
780
|
-
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
781
|
-
})) {
|
782
|
-
if (type === "text") {
|
783
|
-
if (prefixMap["text"]) {
|
784
|
-
prefixMap["text"] = {
|
785
|
-
...prefixMap["text"],
|
786
|
-
content: (prefixMap["text"].content || "") + value
|
787
|
-
};
|
788
|
-
} else {
|
789
|
-
prefixMap["text"] = {
|
790
|
-
id: generateId2(),
|
791
|
-
role: "assistant",
|
792
|
-
content: value,
|
793
|
-
createdAt
|
794
|
-
};
|
795
|
-
}
|
796
|
-
}
|
797
|
-
if (type === "tool_call") {
|
798
|
-
if (prefixMap.text == null) {
|
799
|
-
prefixMap.text = {
|
800
|
-
id: generateId2(),
|
801
|
-
role: "assistant",
|
802
|
-
content: "",
|
803
|
-
createdAt
|
804
|
-
};
|
805
|
-
}
|
806
|
-
if (prefixMap.text.toolInvocations == null) {
|
807
|
-
prefixMap.text.toolInvocations = [];
|
808
|
-
}
|
809
|
-
prefixMap.text.toolInvocations.push(value);
|
810
|
-
if (onToolCall) {
|
811
|
-
const result = await onToolCall({ toolCall: value });
|
812
|
-
if (result != null) {
|
813
|
-
prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
|
814
|
-
}
|
815
|
-
}
|
816
|
-
} else if (type === "tool_result") {
|
817
|
-
if (prefixMap.text == null) {
|
818
|
-
prefixMap.text = {
|
819
|
-
id: generateId2(),
|
820
|
-
role: "assistant",
|
821
|
-
content: "",
|
822
|
-
createdAt
|
823
|
-
};
|
824
|
-
}
|
825
|
-
if (prefixMap.text.toolInvocations == null) {
|
826
|
-
prefixMap.text.toolInvocations = [];
|
827
|
-
}
|
828
|
-
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
829
|
-
(invocation) => invocation.toolCallId === value.toolCallId
|
830
|
-
);
|
831
|
-
if (toolInvocationIndex !== -1) {
|
832
|
-
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
833
|
-
} else {
|
834
|
-
prefixMap.text.toolInvocations.push(value);
|
835
|
-
}
|
836
|
-
}
|
837
|
-
let functionCallMessage = null;
|
838
|
-
if (type === "function_call") {
|
839
|
-
prefixMap["function_call"] = {
|
840
|
-
id: generateId2(),
|
841
|
-
role: "assistant",
|
842
|
-
content: "",
|
843
|
-
function_call: value.function_call,
|
844
|
-
name: value.function_call.name,
|
845
|
-
createdAt
|
846
|
-
};
|
847
|
-
functionCallMessage = prefixMap["function_call"];
|
848
|
-
}
|
849
|
-
let toolCallMessage = null;
|
850
|
-
if (type === "tool_calls") {
|
851
|
-
prefixMap["tool_calls"] = {
|
852
|
-
id: generateId2(),
|
853
|
-
role: "assistant",
|
854
|
-
content: "",
|
855
|
-
tool_calls: value.tool_calls,
|
856
|
-
createdAt
|
857
|
-
};
|
858
|
-
toolCallMessage = prefixMap["tool_calls"];
|
859
|
-
}
|
860
|
-
if (type === "data") {
|
861
|
-
prefixMap["data"].push(...value);
|
862
|
-
}
|
863
|
-
let responseMessage = prefixMap["text"];
|
864
|
-
if (type === "message_annotations") {
|
865
|
-
if (!message_annotations) {
|
866
|
-
message_annotations = [...value];
|
867
|
-
} else {
|
868
|
-
message_annotations.push(...value);
|
869
|
-
}
|
870
|
-
functionCallMessage = assignAnnotationsToMessage(
|
871
|
-
prefixMap["function_call"],
|
872
|
-
message_annotations
|
873
|
-
);
|
874
|
-
toolCallMessage = assignAnnotationsToMessage(
|
875
|
-
prefixMap["tool_calls"],
|
876
|
-
message_annotations
|
877
|
-
);
|
878
|
-
responseMessage = assignAnnotationsToMessage(
|
879
|
-
prefixMap["text"],
|
880
|
-
message_annotations
|
881
|
-
);
|
882
|
-
}
|
883
|
-
if (message_annotations == null ? void 0 : message_annotations.length) {
|
884
|
-
const messagePrefixKeys = [
|
885
|
-
"text",
|
886
|
-
"function_call",
|
887
|
-
"tool_calls"
|
888
|
-
];
|
889
|
-
messagePrefixKeys.forEach((key) => {
|
890
|
-
if (prefixMap[key]) {
|
891
|
-
prefixMap[key].annotations = [...message_annotations];
|
892
|
-
}
|
893
|
-
});
|
894
|
-
}
|
895
|
-
const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
|
896
|
-
...assignAnnotationsToMessage(message, message_annotations)
|
897
|
-
}));
|
898
|
-
update(merged, [...prefixMap["data"]]);
|
899
|
-
}
|
900
|
-
onFinish == null ? void 0 : onFinish(prefixMap);
|
901
|
-
return {
|
902
|
-
messages: [
|
903
|
-
prefixMap.text,
|
904
|
-
prefixMap.function_call,
|
905
|
-
prefixMap.tool_calls
|
906
|
-
].filter(Boolean),
|
907
|
-
data: prefixMap.data
|
908
|
-
};
|
909
|
-
}
|
910
|
-
|
911
|
-
// shared/utils.ts
|
912
|
-
function createChunkDecoder(complex) {
|
913
|
-
const decoder = new TextDecoder();
|
914
|
-
if (!complex) {
|
915
|
-
return function(chunk) {
|
916
|
-
if (!chunk)
|
917
|
-
return "";
|
918
|
-
return decoder.decode(chunk, { stream: true });
|
919
|
-
};
|
920
|
-
}
|
921
|
-
return function(chunk) {
|
922
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
923
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
924
|
-
};
|
925
|
-
}
|
926
|
-
|
927
|
-
// shared/call-chat-api.ts
|
928
|
-
async function callChatApi({
|
929
|
-
api,
|
930
|
-
messages,
|
931
|
-
body,
|
932
|
-
streamMode = "stream-data",
|
933
|
-
credentials,
|
934
|
-
headers,
|
935
|
-
abortController,
|
936
|
-
restoreMessagesOnFailure,
|
937
|
-
onResponse,
|
938
|
-
onUpdate,
|
939
|
-
onFinish,
|
940
|
-
onToolCall,
|
941
|
-
generateId: generateId2
|
942
|
-
}) {
|
943
|
-
var _a;
|
944
|
-
const response = await fetch(api, {
|
945
|
-
method: "POST",
|
946
|
-
body: JSON.stringify({
|
947
|
-
messages,
|
948
|
-
...body
|
949
|
-
}),
|
950
|
-
headers: {
|
951
|
-
"Content-Type": "application/json",
|
952
|
-
...headers
|
953
|
-
},
|
954
|
-
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
955
|
-
credentials
|
956
|
-
}).catch((err) => {
|
957
|
-
restoreMessagesOnFailure();
|
958
|
-
throw err;
|
959
|
-
});
|
960
|
-
if (onResponse) {
|
961
|
-
try {
|
962
|
-
await onResponse(response);
|
963
|
-
} catch (err) {
|
964
|
-
throw err;
|
965
|
-
}
|
966
|
-
}
|
967
|
-
if (!response.ok) {
|
968
|
-
restoreMessagesOnFailure();
|
969
|
-
throw new Error(
|
970
|
-
await response.text() || "Failed to fetch the chat response."
|
971
|
-
);
|
972
|
-
}
|
973
|
-
if (!response.body) {
|
974
|
-
throw new Error("The response body is empty.");
|
975
|
-
}
|
976
|
-
const reader = response.body.getReader();
|
977
|
-
switch (streamMode) {
|
978
|
-
case "text": {
|
979
|
-
const decoder = createChunkDecoder();
|
980
|
-
const resultMessage = {
|
981
|
-
id: generateId2(),
|
982
|
-
createdAt: /* @__PURE__ */ new Date(),
|
983
|
-
role: "assistant",
|
984
|
-
content: ""
|
985
|
-
};
|
986
|
-
while (true) {
|
987
|
-
const { done, value } = await reader.read();
|
988
|
-
if (done) {
|
989
|
-
break;
|
990
|
-
}
|
991
|
-
resultMessage.content += decoder(value);
|
992
|
-
resultMessage.id = generateId2();
|
993
|
-
onUpdate([{ ...resultMessage }], []);
|
994
|
-
if ((abortController == null ? void 0 : abortController()) === null) {
|
995
|
-
reader.cancel();
|
996
|
-
break;
|
997
|
-
}
|
998
|
-
}
|
999
|
-
onFinish == null ? void 0 : onFinish(resultMessage);
|
1000
|
-
return {
|
1001
|
-
messages: [resultMessage],
|
1002
|
-
data: []
|
1003
|
-
};
|
1004
|
-
}
|
1005
|
-
case "stream-data": {
|
1006
|
-
return await parseComplexResponse({
|
1007
|
-
reader,
|
1008
|
-
abortControllerRef: abortController != null ? { current: abortController() } : void 0,
|
1009
|
-
update: onUpdate,
|
1010
|
-
onToolCall,
|
1011
|
-
onFinish(prefixMap) {
|
1012
|
-
if (onFinish && prefixMap.text != null) {
|
1013
|
-
onFinish(prefixMap.text);
|
1014
|
-
}
|
1015
|
-
},
|
1016
|
-
generateId: generateId2
|
1017
|
-
});
|
1018
|
-
}
|
1019
|
-
default: {
|
1020
|
-
const exhaustiveCheck = streamMode;
|
1021
|
-
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
1022
|
-
}
|
1023
|
-
}
|
1024
|
-
}
|
1025
|
-
|
1026
|
-
// shared/process-chat-stream.ts
|
1027
|
-
async function processChatStream({
|
1028
|
-
getStreamedResponse: getStreamedResponse2,
|
1029
|
-
experimental_onFunctionCall,
|
1030
|
-
experimental_onToolCall,
|
1031
|
-
updateChatRequest,
|
1032
|
-
getCurrentMessages
|
1033
|
-
}) {
|
1034
|
-
while (true) {
|
1035
|
-
const messagesAndDataOrJustMessage = await getStreamedResponse2();
|
1036
|
-
if ("messages" in messagesAndDataOrJustMessage) {
|
1037
|
-
let hasFollowingResponse = false;
|
1038
|
-
for (const message of messagesAndDataOrJustMessage.messages) {
|
1039
|
-
if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
|
1040
|
-
continue;
|
1041
|
-
}
|
1042
|
-
hasFollowingResponse = true;
|
1043
|
-
if (experimental_onFunctionCall) {
|
1044
|
-
const functionCall = message.function_call;
|
1045
|
-
if (typeof functionCall !== "object") {
|
1046
|
-
console.warn(
|
1047
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
1048
|
-
);
|
1049
|
-
continue;
|
1050
|
-
}
|
1051
|
-
const functionCallResponse = await experimental_onFunctionCall(
|
1052
|
-
getCurrentMessages(),
|
1053
|
-
functionCall
|
1054
|
-
);
|
1055
|
-
if (functionCallResponse === void 0) {
|
1056
|
-
hasFollowingResponse = false;
|
1057
|
-
break;
|
1058
|
-
}
|
1059
|
-
updateChatRequest(functionCallResponse);
|
1060
|
-
}
|
1061
|
-
if (experimental_onToolCall) {
|
1062
|
-
const toolCalls = message.tool_calls;
|
1063
|
-
if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
|
1064
|
-
console.warn(
|
1065
|
-
"experimental_onToolCall should not be defined when using tools"
|
1066
|
-
);
|
1067
|
-
continue;
|
1068
|
-
}
|
1069
|
-
const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
|
1070
|
-
if (toolCallResponse === void 0) {
|
1071
|
-
hasFollowingResponse = false;
|
1072
|
-
break;
|
1073
|
-
}
|
1074
|
-
updateChatRequest(toolCallResponse);
|
1075
|
-
}
|
1076
|
-
}
|
1077
|
-
if (!hasFollowingResponse) {
|
1078
|
-
break;
|
1079
|
-
}
|
1080
|
-
} else {
|
1081
|
-
let fixFunctionCallArguments2 = function(response) {
|
1082
|
-
for (const message of response.messages) {
|
1083
|
-
if (message.tool_calls !== void 0) {
|
1084
|
-
for (const toolCall of message.tool_calls) {
|
1085
|
-
if (typeof toolCall === "object") {
|
1086
|
-
if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
|
1087
|
-
toolCall.function.arguments = JSON.stringify(
|
1088
|
-
toolCall.function.arguments
|
1089
|
-
);
|
1090
|
-
}
|
1091
|
-
}
|
1092
|
-
}
|
1093
|
-
}
|
1094
|
-
if (message.function_call !== void 0) {
|
1095
|
-
if (typeof message.function_call === "object") {
|
1096
|
-
if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
|
1097
|
-
message.function_call.arguments = JSON.stringify(
|
1098
|
-
message.function_call.arguments
|
1099
|
-
);
|
1100
|
-
}
|
1101
|
-
}
|
1102
|
-
}
|
1103
|
-
}
|
1104
|
-
};
|
1105
|
-
var fixFunctionCallArguments = fixFunctionCallArguments2;
|
1106
|
-
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
1107
|
-
if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
|
1108
|
-
break;
|
1109
|
-
}
|
1110
|
-
if (experimental_onFunctionCall) {
|
1111
|
-
const functionCall = streamedResponseMessage.function_call;
|
1112
|
-
if (!(typeof functionCall === "object")) {
|
1113
|
-
console.warn(
|
1114
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
1115
|
-
);
|
1116
|
-
continue;
|
1117
|
-
}
|
1118
|
-
const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
|
1119
|
-
if (functionCallResponse === void 0)
|
1120
|
-
break;
|
1121
|
-
fixFunctionCallArguments2(functionCallResponse);
|
1122
|
-
updateChatRequest(functionCallResponse);
|
1123
|
-
}
|
1124
|
-
if (experimental_onToolCall) {
|
1125
|
-
const toolCalls = streamedResponseMessage.tool_calls;
|
1126
|
-
if (!(typeof toolCalls === "object")) {
|
1127
|
-
console.warn(
|
1128
|
-
"experimental_onToolCall should not be defined when using functions"
|
1129
|
-
);
|
1130
|
-
continue;
|
1131
|
-
}
|
1132
|
-
const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
|
1133
|
-
if (toolCallResponse === void 0)
|
1134
|
-
break;
|
1135
|
-
fixFunctionCallArguments2(toolCallResponse);
|
1136
|
-
updateChatRequest(toolCallResponse);
|
1137
|
-
}
|
1138
|
-
}
|
1139
|
-
}
|
1140
|
-
}
|
1141
|
-
|
1142
|
-
// svelte/use-chat.ts
|
1143
507
|
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
|
1144
508
|
var _a, _b;
|
1145
509
|
mutate(chatRequest.messages);
|
@@ -1220,7 +584,7 @@ function useChat({
|
|
1220
584
|
credentials,
|
1221
585
|
headers,
|
1222
586
|
body,
|
1223
|
-
generateId: generateId2 =
|
587
|
+
generateId: generateId2 = generateIdFunc
|
1224
588
|
} = {}) {
|
1225
589
|
const chatId = id || `chat-${uniqueId++}`;
|
1226
590
|
const key = `${api}|${chatId}`;
|
@@ -1392,125 +756,8 @@ function useChat({
|
|
1392
756
|
}
|
1393
757
|
|
1394
758
|
// svelte/use-completion.ts
|
759
|
+
import { callCompletionApi } from "@ai-sdk/ui-utils";
|
1395
760
|
import { derived as derived2, get as get2, writable as writable2 } from "svelte/store";
|
1396
|
-
|
1397
|
-
// shared/call-completion-api.ts
|
1398
|
-
async function callCompletionApi({
|
1399
|
-
api,
|
1400
|
-
prompt,
|
1401
|
-
credentials,
|
1402
|
-
headers,
|
1403
|
-
body,
|
1404
|
-
streamMode = "stream-data",
|
1405
|
-
setCompletion,
|
1406
|
-
setLoading,
|
1407
|
-
setError,
|
1408
|
-
setAbortController,
|
1409
|
-
onResponse,
|
1410
|
-
onFinish,
|
1411
|
-
onError,
|
1412
|
-
onData
|
1413
|
-
}) {
|
1414
|
-
try {
|
1415
|
-
setLoading(true);
|
1416
|
-
setError(void 0);
|
1417
|
-
const abortController = new AbortController();
|
1418
|
-
setAbortController(abortController);
|
1419
|
-
setCompletion("");
|
1420
|
-
const res = await fetch(api, {
|
1421
|
-
method: "POST",
|
1422
|
-
body: JSON.stringify({
|
1423
|
-
prompt,
|
1424
|
-
...body
|
1425
|
-
}),
|
1426
|
-
credentials,
|
1427
|
-
headers: {
|
1428
|
-
"Content-Type": "application/json",
|
1429
|
-
...headers
|
1430
|
-
},
|
1431
|
-
signal: abortController.signal
|
1432
|
-
}).catch((err) => {
|
1433
|
-
throw err;
|
1434
|
-
});
|
1435
|
-
if (onResponse) {
|
1436
|
-
try {
|
1437
|
-
await onResponse(res);
|
1438
|
-
} catch (err) {
|
1439
|
-
throw err;
|
1440
|
-
}
|
1441
|
-
}
|
1442
|
-
if (!res.ok) {
|
1443
|
-
throw new Error(
|
1444
|
-
await res.text() || "Failed to fetch the chat response."
|
1445
|
-
);
|
1446
|
-
}
|
1447
|
-
if (!res.body) {
|
1448
|
-
throw new Error("The response body is empty.");
|
1449
|
-
}
|
1450
|
-
let result = "";
|
1451
|
-
const reader = res.body.getReader();
|
1452
|
-
switch (streamMode) {
|
1453
|
-
case "text": {
|
1454
|
-
const decoder = createChunkDecoder();
|
1455
|
-
while (true) {
|
1456
|
-
const { done, value } = await reader.read();
|
1457
|
-
if (done) {
|
1458
|
-
break;
|
1459
|
-
}
|
1460
|
-
result += decoder(value);
|
1461
|
-
setCompletion(result);
|
1462
|
-
if (abortController === null) {
|
1463
|
-
reader.cancel();
|
1464
|
-
break;
|
1465
|
-
}
|
1466
|
-
}
|
1467
|
-
break;
|
1468
|
-
}
|
1469
|
-
case "stream-data": {
|
1470
|
-
for await (const { type, value } of readDataStream(reader, {
|
1471
|
-
isAborted: () => abortController === null
|
1472
|
-
})) {
|
1473
|
-
switch (type) {
|
1474
|
-
case "text": {
|
1475
|
-
result += value;
|
1476
|
-
setCompletion(result);
|
1477
|
-
break;
|
1478
|
-
}
|
1479
|
-
case "data": {
|
1480
|
-
onData == null ? void 0 : onData(value);
|
1481
|
-
break;
|
1482
|
-
}
|
1483
|
-
}
|
1484
|
-
}
|
1485
|
-
break;
|
1486
|
-
}
|
1487
|
-
default: {
|
1488
|
-
const exhaustiveCheck = streamMode;
|
1489
|
-
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
1490
|
-
}
|
1491
|
-
}
|
1492
|
-
if (onFinish) {
|
1493
|
-
onFinish(prompt, result);
|
1494
|
-
}
|
1495
|
-
setAbortController(null);
|
1496
|
-
return result;
|
1497
|
-
} catch (err) {
|
1498
|
-
if (err.name === "AbortError") {
|
1499
|
-
setAbortController(null);
|
1500
|
-
return null;
|
1501
|
-
}
|
1502
|
-
if (err instanceof Error) {
|
1503
|
-
if (onError) {
|
1504
|
-
onError(err);
|
1505
|
-
}
|
1506
|
-
}
|
1507
|
-
setError(err);
|
1508
|
-
} finally {
|
1509
|
-
setLoading(false);
|
1510
|
-
}
|
1511
|
-
}
|
1512
|
-
|
1513
|
-
// svelte/use-completion.ts
|
1514
761
|
var uniqueId2 = 0;
|
1515
762
|
var store2 = {};
|
1516
763
|
function useCompletion({
|
@@ -1613,6 +860,7 @@ function useCompletion({
|
|
1613
860
|
|
1614
861
|
// svelte/use-assistant.ts
|
1615
862
|
import { isAbortError } from "@ai-sdk/provider-utils";
|
863
|
+
import { generateId, readDataStream } from "@ai-sdk/ui-utils";
|
1616
864
|
import { get as get3, writable as writable3 } from "svelte/store";
|
1617
865
|
var uniqueId3 = 0;
|
1618
866
|
var store3 = {};
|