modelfusion 0.47.0 → 0.47.2
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/README.md +37 -36
- package/core/structure/Schema.d.ts +5 -5
- package/core/structure/UncheckedSchema.cjs +2 -2
- package/core/structure/UncheckedSchema.d.ts +2 -2
- package/core/structure/UncheckedSchema.js +2 -2
- package/core/structure/ZodSchema.cjs +2 -5
- package/core/structure/ZodSchema.d.ts +2 -2
- package/core/structure/ZodSchema.js +2 -5
- package/event-source/readEventSource.cjs +5 -8
- package/event-source/readEventSource.d.ts +1 -1
- package/event-source/readEventSource.js +5 -5
- package/event-source/readEventSourceStream.cjs +3 -6
- package/event-source/readEventSourceStream.js +3 -3
- package/guard/fixStructure.cjs +50 -0
- package/guard/fixStructure.d.ts +50 -0
- package/guard/fixStructure.js +50 -0
- package/guard/guard.d.ts +1 -1
- package/model-function/generate-structure/fixJson.test.cjs +183 -181
- package/model-function/generate-structure/fixJson.test.js +181 -182
- package/model-function/generate-structure/generateStructure.cjs +1 -1
- package/model-function/generate-structure/generateStructure.js +1 -1
- package/model-function/generate-structure/generateStructureOrText.cjs +1 -1
- package/model-function/generate-structure/generateStructureOrText.js +1 -1
- package/model-function/generate-structure/streamStructure.cjs +3 -8
- package/model-function/generate-structure/streamStructure.js +3 -5
- package/model-provider/anthropic/AnthropicError.cjs +7 -14
- package/model-provider/anthropic/AnthropicError.js +7 -11
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +2 -15
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +2 -12
- package/model-provider/automatic1111/Automatic1111Error.cjs +2 -5
- package/model-provider/automatic1111/Automatic1111Error.d.ts +1 -1
- package/model-provider/automatic1111/Automatic1111Error.js +2 -2
- package/model-provider/cohere/CohereError.cjs +2 -5
- package/model-provider/cohere/CohereError.js +2 -2
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +8 -11
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.js +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +22 -21
- package/model-provider/cohere/CohereTextGenerationModel.js +22 -18
- package/model-provider/cohere/CohereTokenizer.cjs +12 -15
- package/model-provider/cohere/CohereTokenizer.d.ts +1 -1
- package/model-provider/cohere/CohereTokenizer.js +1 -1
- package/model-provider/huggingface/HuggingFaceError.cjs +7 -31
- package/model-provider/huggingface/HuggingFaceError.js +7 -28
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.cjs +3 -6
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +2 -5
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +3 -6
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +1 -1
- package/model-provider/llamacpp/LlamaCppError.cjs +7 -30
- package/model-provider/llamacpp/LlamaCppError.js +7 -27
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +3 -6
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +1 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +53 -66
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +3 -13
- package/model-provider/llamacpp/LlamaCppTokenizer.cjs +3 -6
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTokenizer.js +1 -1
- package/model-provider/openai/OpenAIError.cjs +2 -5
- package/model-provider/openai/OpenAIError.js +2 -2
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +11 -14
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.js +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.cjs +26 -39
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.js +3 -13
- package/model-provider/openai/OpenAITranscriptionModel.cjs +20 -23
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/OpenAITranscriptionModel.js +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.cjs +21 -21
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.js +2 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +2 -6
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +2 -3
- package/model-provider/stability/StabilityError.cjs +2 -5
- package/model-provider/stability/StabilityError.js +2 -2
- package/package.json +4 -4
- package/util/JSONParseError.cjs +33 -0
- package/util/JSONParseError.d.ts +9 -0
- package/util/JSONParseError.js +29 -0
- package/util/index.cjs +2 -0
- package/util/index.d.ts +2 -0
- package/util/index.js +2 -0
- package/util/isDeepEqualData.cjs +53 -0
- package/util/isDeepEqualData.d.ts +8 -0
- package/util/isDeepEqualData.js +49 -0
- package/util/isDeepEqualData.test.cjs +108 -0
- package/util/isDeepEqualData.test.d.ts +1 -0
- package/util/isDeepEqualData.test.js +103 -0
- package/util/parseJSON.cjs +67 -0
- package/util/parseJSON.d.ts +18 -0
- package/util/parseJSON.js +58 -0
- package/vector-index/memory/MemoryVectorIndex.cjs +7 -10
- package/vector-index/memory/MemoryVectorIndex.js +3 -3
@@ -1,184 +1,183 @@
|
|
1
|
-
import
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
});
|
1
|
+
import assert from "node:assert";
|
2
|
+
import { test, describe } from "vitest";
|
3
|
+
import { fixJson } from "./fixJson.js";
|
4
|
+
test("should handle empty input", () => {
|
5
|
+
assert.strictEqual(fixJson(""), "");
|
6
|
+
});
|
7
|
+
describe("literals", () => {
|
8
|
+
test("should handle incomplete null", () => {
|
9
|
+
assert.strictEqual(fixJson("nul"), "null");
|
10
|
+
});
|
11
|
+
test("should handle incomplete true", () => {
|
12
|
+
assert.strictEqual(fixJson("t"), "true");
|
13
|
+
});
|
14
|
+
test("should handle incomplete false", () => {
|
15
|
+
assert.strictEqual(fixJson("fals"), "false");
|
16
|
+
});
|
17
|
+
});
|
18
|
+
describe("number", () => {
|
19
|
+
test("should handle incomplete numbers", () => {
|
20
|
+
assert.strictEqual(fixJson("12."), "12");
|
21
|
+
});
|
22
|
+
test("should handle numbers with dot", () => {
|
23
|
+
assert.strictEqual(fixJson("12.2"), "12.2");
|
24
|
+
});
|
25
|
+
test("should handle negative numbers", () => {
|
26
|
+
assert.strictEqual(fixJson("-12"), "-12");
|
27
|
+
});
|
28
|
+
test("should handle incomplete negative numbers", () => {
|
29
|
+
assert.strictEqual(fixJson("-"), "");
|
30
|
+
});
|
31
|
+
test("should handle e-notation numbers", () => {
|
32
|
+
assert.strictEqual(fixJson("2.5e"), "2.5");
|
33
|
+
assert.strictEqual(fixJson("2.5e-"), "2.5");
|
34
|
+
assert.strictEqual(fixJson("2.5e3"), "2.5e3");
|
35
|
+
assert.strictEqual(fixJson("-2.5e3"), "-2.5e3");
|
36
|
+
});
|
37
|
+
test("should handle uppercase e-notation numbers", () => {
|
38
|
+
assert.strictEqual(fixJson("2.5E"), "2.5");
|
39
|
+
assert.strictEqual(fixJson("2.5E-"), "2.5");
|
40
|
+
assert.strictEqual(fixJson("2.5E3"), "2.5E3");
|
41
|
+
assert.strictEqual(fixJson("-2.5E3"), "-2.5E3");
|
42
|
+
});
|
43
|
+
test("should handle incomplete numbers", () => {
|
44
|
+
assert.strictEqual(fixJson("12.e"), "12");
|
45
|
+
assert.strictEqual(fixJson("12.34e"), "12.34");
|
46
|
+
assert.strictEqual(fixJson("5e"), "5");
|
47
|
+
});
|
48
|
+
});
|
49
|
+
describe("string", () => {
|
50
|
+
test("should handle incomplete strings", () => {
|
51
|
+
assert.strictEqual(fixJson('"abc'), '"abc"');
|
52
|
+
});
|
53
|
+
test("should handle escape sequences", () => {
|
54
|
+
assert.strictEqual(fixJson('"value with \\"quoted\\" text and \\\\ escape'), '"value with \\"quoted\\" text and \\\\ escape"');
|
55
|
+
});
|
56
|
+
test("should handle incomplete escape sequences", () => {
|
57
|
+
assert.strictEqual(fixJson('"value with \\'), '"value with "');
|
58
|
+
});
|
59
|
+
test("should handle unicode characters", () => {
|
60
|
+
assert.strictEqual(fixJson('"value with unicode \u003C"'), '"value with unicode \u003C"');
|
61
|
+
});
|
62
|
+
});
|
63
|
+
describe("array", () => {
|
64
|
+
test("should handle incomplete array", () => {
|
65
|
+
assert.strictEqual(fixJson("["), "[]");
|
66
|
+
});
|
67
|
+
test("should handle closing bracket after number in array", () => {
|
68
|
+
assert.strictEqual(fixJson("[[1], [2"), "[[1], [2]]");
|
69
|
+
});
|
70
|
+
test("should handle closing bracket after string in array", () => {
|
71
|
+
assert.strictEqual(fixJson(`[["1"], ["2`), `[["1"], ["2"]]`);
|
72
|
+
});
|
73
|
+
test("should handle closing bracket after literal in array", () => {
|
74
|
+
assert.strictEqual(fixJson("[[false], [nu"), "[[false], [null]]");
|
75
|
+
});
|
76
|
+
test("should handle closing bracket after array in array", () => {
|
77
|
+
assert.strictEqual(fixJson("[[[]], [[]"), "[[[]], [[]]]");
|
78
|
+
});
|
79
|
+
test("should handle closing bracket after object in array", () => {
|
80
|
+
assert.strictEqual(fixJson("[[{}], [{"), "[[{}], [{}]]");
|
81
|
+
});
|
82
|
+
test("should handle trailing comma", () => {
|
83
|
+
assert.strictEqual(fixJson("[1, "), "[1]");
|
84
|
+
});
|
85
|
+
test("should handle closing array", () => {
|
86
|
+
assert.strictEqual(fixJson("[[], 123"), "[[], 123]");
|
87
|
+
});
|
88
|
+
});
|
89
|
+
describe("object", () => {
|
90
|
+
test("should handle keys without values", () => {
|
91
|
+
assert.strictEqual(fixJson('{"key":'), "{}");
|
92
|
+
});
|
93
|
+
test("should handle closing brace after number in object", () => {
|
94
|
+
assert.strictEqual(fixJson('{"a": {"b": 1}, "c": {"d": 2'), '{"a": {"b": 1}, "c": {"d": 2}}');
|
95
|
+
});
|
96
|
+
test("should handle closing brace after string in object", () => {
|
97
|
+
assert.strictEqual(fixJson('{"a": {"b": "1"}, "c": {"d": 2'), '{"a": {"b": "1"}, "c": {"d": 2}}');
|
98
|
+
});
|
99
|
+
test("should handle closing brace after literal in object", () => {
|
100
|
+
assert.strictEqual(fixJson('{"a": {"b": false}, "c": {"d": 2'), '{"a": {"b": false}, "c": {"d": 2}}');
|
101
|
+
});
|
102
|
+
test("should handle closing brace after array in object", () => {
|
103
|
+
assert.strictEqual(fixJson('{"a": {"b": []}, "c": {"d": 2'), '{"a": {"b": []}, "c": {"d": 2}}');
|
104
|
+
});
|
105
|
+
test("should handle closing brace after object in object", () => {
|
106
|
+
assert.strictEqual(fixJson('{"a": {"b": {}}, "c": {"d": 2'), '{"a": {"b": {}}, "c": {"d": 2}}');
|
107
|
+
});
|
108
|
+
test("should handle partial keys (first key)", () => {
|
109
|
+
assert.strictEqual(fixJson('{"ke'), "{}");
|
110
|
+
});
|
111
|
+
test("should handle partial keys (second key)", () => {
|
112
|
+
assert.strictEqual(fixJson('{"k1": 1, "k2'), '{"k1": 1}');
|
113
|
+
});
|
114
|
+
test("should handle partial keys with colon (second key)", () => {
|
115
|
+
assert.strictEqual(fixJson('{"k1": 1, "k2":'), '{"k1": 1}');
|
116
|
+
});
|
117
|
+
test("should handle trailing whitespaces", () => {
|
118
|
+
assert.strictEqual(fixJson('{"key": "value" '), '{"key": "value"}');
|
119
|
+
});
|
120
|
+
});
|
121
|
+
describe("nesting", () => {
|
122
|
+
test("should handle nested arrays with numbers", () => {
|
123
|
+
assert.strictEqual(fixJson("[1, [2, 3, ["), "[1, [2, 3, []]]");
|
124
|
+
});
|
125
|
+
test("should handle nested arrays with literals", () => {
|
126
|
+
assert.strictEqual(fixJson("[false, [true, ["), "[false, [true, []]]");
|
127
|
+
});
|
128
|
+
test("should handle nested objects", () => {
|
129
|
+
assert.strictEqual(fixJson('{"key": {"subKey":'), '{"key": {}}');
|
130
|
+
});
|
131
|
+
test("should handle nested objects with numbers", () => {
|
132
|
+
assert.strictEqual(fixJson('{"key": 123, "key2": {"subKey":'), '{"key": 123, "key2": {}}');
|
133
|
+
});
|
134
|
+
test("should handle nested objects with literals", () => {
|
135
|
+
assert.strictEqual(fixJson('{"key": null, "key2": {"subKey":'), '{"key": null, "key2": {}}');
|
136
|
+
});
|
137
|
+
test("should handle arrays within objects", () => {
|
138
|
+
assert.strictEqual(fixJson('{"key": [1, 2, {'), '{"key": [1, 2, {}]}');
|
139
|
+
});
|
140
|
+
test("should handle objects within arrays", () => {
|
141
|
+
assert.strictEqual(fixJson('[1, 2, {"key": "value",'), '[1, 2, {"key": "value"}]');
|
142
|
+
});
|
143
|
+
test("should handle nested arrays and objects", () => {
|
144
|
+
assert.strictEqual(fixJson('{"a": {"b": ["c", {"d": "e",'), '{"a": {"b": ["c", {"d": "e"}]}}');
|
145
|
+
});
|
146
|
+
test("should handle deeply nested structures", () => {
|
147
|
+
assert.strictEqual(fixJson('{"a": {"b": {"c": {"d":'), '{"a": {"b": {"c": {}}}}');
|
148
|
+
});
|
149
|
+
test("should handle potential nested arrays or objects", () => {
|
150
|
+
assert.strictEqual(fixJson('{"a": 1, "b": ['), '{"a": 1, "b": []}');
|
151
|
+
assert.strictEqual(fixJson('{"a": 1, "b": {'), '{"a": 1, "b": {}}');
|
152
|
+
assert.strictEqual(fixJson('{"a": 1, "b": "'), '{"a": 1, "b": ""}');
|
153
|
+
});
|
154
|
+
});
|
155
|
+
describe("regression", () => {
|
156
|
+
test("should handle complex nesting 1", () => {
|
157
|
+
assert.strictEqual(fixJson([
|
158
|
+
"{",
|
159
|
+
' "a": [',
|
160
|
+
" {",
|
161
|
+
' "a1": "v1",',
|
162
|
+
' "a2": "v2",',
|
163
|
+
` "a3": "v3"`,
|
164
|
+
" }",
|
165
|
+
" ],",
|
166
|
+
' "b": [',
|
167
|
+
" {",
|
168
|
+
' "b1": "n',
|
169
|
+
].join("\n")), [
|
170
|
+
"{",
|
171
|
+
' "a": [',
|
172
|
+
" {",
|
173
|
+
' "a1": "v1",',
|
174
|
+
' "a2": "v2",',
|
175
|
+
` "a3": "v3"`,
|
176
|
+
" }",
|
177
|
+
" ],",
|
178
|
+
' "b": [',
|
179
|
+
" {",
|
180
|
+
' "b1": "n"}]}',
|
181
|
+
].join("\n"));
|
183
182
|
});
|
184
183
|
});
|
@@ -23,7 +23,7 @@ export function generateStructure(model, structureDefinition, prompt, options) {
|
|
23
23
|
cause: parseResult.error,
|
24
24
|
});
|
25
25
|
}
|
26
|
-
const value = parseResult.
|
26
|
+
const value = parseResult.data;
|
27
27
|
return {
|
28
28
|
response: result.response,
|
29
29
|
extractedValue: value,
|
@@ -43,7 +43,7 @@ function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
|
43
43
|
response: result.response,
|
44
44
|
extractedValue: {
|
45
45
|
structure: structure,
|
46
|
-
value: parseResult.
|
46
|
+
value: parseResult.data,
|
47
47
|
text: text, // text is string | null, which is part of the response for schema values
|
48
48
|
},
|
49
49
|
usage: result.usage,
|
@@ -40,7 +40,7 @@ export function generateStructureOrText(model, structureDefinitions, prompt, opt
|
|
40
40
|
response: result.response,
|
41
41
|
extractedValue: {
|
42
42
|
structure: structure,
|
43
|
-
value: parseResult.
|
43
|
+
value: parseResult.data,
|
44
44
|
text: text, // text is string | null, which is part of the response for schema values
|
45
45
|
},
|
46
46
|
usage: result.usage,
|
@@ -1,10 +1,6 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.streamStructure = void 0;
|
7
|
-
const deep_equal_1 = __importDefault(require("deep-equal"));
|
8
4
|
const nanoid_1 = require("nanoid");
|
9
5
|
const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
|
10
6
|
const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
|
@@ -13,6 +9,7 @@ const AbortError_js_1 = require("../../core/api/AbortError.cjs");
|
|
13
9
|
const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
|
14
10
|
const getRun_js_1 = require("../../core/getRun.cjs");
|
15
11
|
const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
|
12
|
+
const isDeepEqualData_js_1 = require("../../util/isDeepEqualData.cjs");
|
16
13
|
const runSafe_js_1 = require("../../util/runSafe.cjs");
|
17
14
|
const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
|
18
15
|
function streamStructure(model, structureDefinition, prompt, options) {
|
@@ -91,9 +88,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
91
88
|
const latestFullDelta = event.fullDelta;
|
92
89
|
const latestStructure = event.valueDelta;
|
93
90
|
// only send a new part into the stream when the partial structure has changed:
|
94
|
-
if (!(0,
|
95
|
-
strict: true,
|
96
|
-
})) {
|
91
|
+
if (!(0, isDeepEqualData_js_1.isDeepEqualData)(lastStructure, latestStructure)) {
|
97
92
|
lastFullDelta = latestFullDelta;
|
98
93
|
lastStructure = latestStructure;
|
99
94
|
yield {
|
@@ -111,7 +106,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
111
106
|
}
|
112
107
|
yield {
|
113
108
|
isComplete: true,
|
114
|
-
value: parseResult.
|
109
|
+
value: parseResult.data,
|
115
110
|
};
|
116
111
|
const finishMetadata = {
|
117
112
|
eventType: "finished",
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import deepEqual from "deep-equal";
|
2
1
|
import { nanoid as createId } from "nanoid";
|
3
2
|
import { FunctionEventSource } from "../../core/FunctionEventSource.js";
|
4
3
|
import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
|
@@ -7,6 +6,7 @@ import { AbortError } from "../../core/api/AbortError.js";
|
|
7
6
|
import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
|
8
7
|
import { getRun } from "../../core/getRun.js";
|
9
8
|
import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
|
9
|
+
import { isDeepEqualData } from "../../util/isDeepEqualData.js";
|
10
10
|
import { runSafe } from "../../util/runSafe.js";
|
11
11
|
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
12
12
|
export function streamStructure(model, structureDefinition, prompt, options) {
|
@@ -84,9 +84,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
84
84
|
const latestFullDelta = event.fullDelta;
|
85
85
|
const latestStructure = event.valueDelta;
|
86
86
|
// only send a new part into the stream when the partial structure has changed:
|
87
|
-
if (!
|
88
|
-
strict: true,
|
89
|
-
})) {
|
87
|
+
if (!isDeepEqualData(lastStructure, latestStructure)) {
|
90
88
|
lastFullDelta = latestFullDelta;
|
91
89
|
lastStructure = latestStructure;
|
92
90
|
yield {
|
@@ -104,7 +102,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
104
102
|
}
|
105
103
|
yield {
|
106
104
|
isComplete: true,
|
107
|
-
value: parseResult.
|
105
|
+
value: parseResult.data,
|
108
106
|
};
|
109
107
|
const finishMetadata = {
|
110
108
|
eventType: "finished",
|
@@ -1,12 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.failedAnthropicCallResponseHandler = exports.AnthropicError = exports.anthropicErrorDataSchema = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
4
|
const zod_1 = require("zod");
|
9
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
|
+
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
10
7
|
exports.anthropicErrorDataSchema = zod_1.z.object({
|
11
8
|
error: zod_1.z.object({
|
12
9
|
type: zod_1.z.string(),
|
@@ -26,14 +23,10 @@ class AnthropicError extends ApiCallError_js_1.ApiCallError {
|
|
26
23
|
}
|
27
24
|
}
|
28
25
|
exports.AnthropicError = AnthropicError;
|
29
|
-
const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
statusCode: response.status,
|
36
|
-
data: parsedError,
|
37
|
-
});
|
38
|
-
};
|
26
|
+
const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyValues }) => new AnthropicError({
|
27
|
+
url,
|
28
|
+
requestBodyValues,
|
29
|
+
statusCode: response.status,
|
30
|
+
data: (0, parseJSON_js_1.parseJsonWithZod)(await response.text(), exports.anthropicErrorDataSchema),
|
31
|
+
});
|
39
32
|
exports.failedAnthropicCallResponseHandler = failedAnthropicCallResponseHandler;
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import SecureJSON from "secure-json-parse";
|
2
1
|
import { z } from "zod";
|
3
2
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
+
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
4
4
|
export const anthropicErrorDataSchema = z.object({
|
5
5
|
error: z.object({
|
6
6
|
type: z.string(),
|
@@ -19,13 +19,9 @@ export class AnthropicError extends ApiCallError {
|
|
19
19
|
this.data = data;
|
20
20
|
}
|
21
21
|
}
|
22
|
-
export const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
statusCode: response.status,
|
29
|
-
data: parsedError,
|
30
|
-
});
|
31
|
-
};
|
22
|
+
export const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyValues }) => new AnthropicError({
|
23
|
+
url,
|
24
|
+
requestBodyValues,
|
25
|
+
statusCode: response.status,
|
26
|
+
data: parseJsonWithZod(await response.text(), anthropicErrorDataSchema),
|
27
|
+
});
|
@@ -1,10 +1,6 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.AnthropicTextGenerationResponseFormat = exports.AnthropicTextGenerationModel = exports.ANTHROPIC_TEXT_GENERATION_MODELS = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
4
|
const zod_1 = require("zod");
|
9
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
10
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
@@ -12,6 +8,7 @@ const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
|
|
12
8
|
const parseEventSourceStream_js_1 = require("../../event-source/parseEventSourceStream.cjs");
|
13
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
14
10
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
11
|
+
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
15
12
|
const AnthropicApiConfiguration_js_1 = require("./AnthropicApiConfiguration.cjs");
|
16
13
|
const AnthropicError_js_1 = require("./AnthropicError.cjs");
|
17
14
|
const AnthropicPromptFormat_js_1 = require("./AnthropicPromptFormat.cjs");
|
@@ -183,17 +180,7 @@ async function createAnthropicFullDeltaIterableQueue(stream) {
|
|
183
180
|
continue;
|
184
181
|
}
|
185
182
|
const data = event.data;
|
186
|
-
const
|
187
|
-
const parseResult = anthropicTextStreamingResponseSchema.safeParse(json);
|
188
|
-
if (!parseResult.success) {
|
189
|
-
queue.push({
|
190
|
-
type: "error",
|
191
|
-
error: parseResult.error,
|
192
|
-
});
|
193
|
-
queue.close();
|
194
|
-
return;
|
195
|
-
}
|
196
|
-
const eventData = parseResult.data;
|
183
|
+
const eventData = (0, parseJSON_js_1.parseJsonWithZod)(data, anthropicTextStreamingResponseSchema);
|
197
184
|
content += eventData.completion;
|
198
185
|
queue.push({
|
199
186
|
type: "delta",
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import SecureJSON from "secure-json-parse";
|
2
1
|
import { z } from "zod";
|
3
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
@@ -6,6 +5,7 @@ import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
|
6
5
|
import { parseEventSourceStream } from "../../event-source/parseEventSourceStream.js";
|
7
6
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
7
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
8
|
+
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
9
9
|
import { AnthropicApiConfiguration } from "./AnthropicApiConfiguration.js";
|
10
10
|
import { failedAnthropicCallResponseHandler } from "./AnthropicError.js";
|
11
11
|
import { mapChatPromptToAnthropicFormat, mapInstructionPromptToAnthropicFormat, } from "./AnthropicPromptFormat.js";
|
@@ -176,17 +176,7 @@ async function createAnthropicFullDeltaIterableQueue(stream) {
|
|
176
176
|
continue;
|
177
177
|
}
|
178
178
|
const data = event.data;
|
179
|
-
const
|
180
|
-
const parseResult = anthropicTextStreamingResponseSchema.safeParse(json);
|
181
|
-
if (!parseResult.success) {
|
182
|
-
queue.push({
|
183
|
-
type: "error",
|
184
|
-
error: parseResult.error,
|
185
|
-
});
|
186
|
-
queue.close();
|
187
|
-
return;
|
188
|
-
}
|
189
|
-
const eventData = parseResult.data;
|
179
|
+
const eventData = parseJsonWithZod(data, anthropicTextStreamingResponseSchema);
|
190
180
|
content += eventData.completion;
|
191
181
|
queue.push({
|
192
182
|
type: "delta",
|