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