@chainfuse/ai-tools 0.3.1 → 0.3.3
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.
|
@@ -105,46 +105,72 @@ export class AiCustomProviders extends AiBase {
|
|
|
105
105
|
// @ts-expect-error override for types
|
|
106
106
|
acc[model] = wrapLanguageModel({
|
|
107
107
|
model: (await raw.restWorkersAi(args))(model),
|
|
108
|
-
middleware:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
if (chunk.error
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
108
|
+
middleware: [
|
|
109
|
+
{
|
|
110
|
+
wrapStream: async ({ doStream }) => {
|
|
111
|
+
const { stream, ...rest } = await doStream();
|
|
112
|
+
const transformStream = new TransformStream({
|
|
113
|
+
transform(chunk, controller) {
|
|
114
|
+
if (chunk.type === 'error') {
|
|
115
|
+
if (TypeValidationError.isInstance(chunk.error) && chunk.error.cause instanceof ZodError) {
|
|
116
|
+
if (chunk.error.cause.issues.filter((issues) => issues.code === 'invalid_union')) {
|
|
117
|
+
// Verify the specific error instead of assuming all errors
|
|
118
|
+
const missingIndexPropertyError = chunk.error.cause.issues
|
|
119
|
+
.filter((issues) => issues.code === 'invalid_union')
|
|
120
|
+
.flatMap((issue) => issue.unionErrors)
|
|
121
|
+
.flatMap((issue) => issue.issues)
|
|
122
|
+
.filter((issue) => issue.code === 'invalid_type' && Helpers.areArraysEqual(issue.path, ['choices', 0, 'index']));
|
|
123
|
+
if (missingIndexPropertyError.length > 0) {
|
|
124
|
+
const newChunk = chunk.error.value;
|
|
125
|
+
newChunk.choices
|
|
126
|
+
.filter((choice) => choice.delta.content)
|
|
127
|
+
.forEach((choice) => {
|
|
128
|
+
controller.enqueue({
|
|
129
|
+
type: 'text-delta',
|
|
130
|
+
textDelta: choice.delta.content,
|
|
131
|
+
});
|
|
130
132
|
});
|
|
131
|
-
}
|
|
133
|
+
}
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
136
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
137
|
+
else {
|
|
138
|
+
// Passthrough untouched
|
|
139
|
+
controller.enqueue(chunk);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
return {
|
|
144
|
+
stream: stream.pipeThrough(transformStream),
|
|
145
|
+
...rest,
|
|
146
|
+
};
|
|
147
|
+
},
|
|
146
148
|
},
|
|
147
|
-
|
|
149
|
+
// Fix output generation where it's correct, but encapsulated in a code fence
|
|
150
|
+
{
|
|
151
|
+
wrapGenerate: async ({ doGenerate, model }) => {
|
|
152
|
+
const result = await doGenerate();
|
|
153
|
+
/**
|
|
154
|
+
* `chunkSchema` is undocumented but always present in `model` regardless of model
|
|
155
|
+
* Can't use `responseFormat` (in `params`) because it isn't always present because some models don't support that part of openai api spec.
|
|
156
|
+
*/
|
|
157
|
+
if ('chunkSchema' in model) {
|
|
158
|
+
const codeFenceStart = new RegExp(/^`{1,3}\w*\s*(?=[\[{])/i);
|
|
159
|
+
const codefenceEnd = new RegExp(/(?![\]}])\s*`{1,3}$/i);
|
|
160
|
+
return {
|
|
161
|
+
...result,
|
|
162
|
+
/**
|
|
163
|
+
* 1. trim initially to remove any leading/trailing whitespace
|
|
164
|
+
* 2. Remove start and end
|
|
165
|
+
* 3. Trim again to remove any leading/trailing whitespace
|
|
166
|
+
*/
|
|
167
|
+
text: result.text?.trim().replace(codeFenceStart, '').replace(codefenceEnd, '').trim(),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
],
|
|
148
174
|
});
|
|
149
175
|
return acc;
|
|
150
176
|
}, Promise.resolve({})),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/ai-tools",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/ai-tools#readme",
|
|
@@ -50,19 +50,19 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@ai-sdk/anthropic": "^1.1.6",
|
|
52
52
|
"@ai-sdk/azure": "^1.1.9",
|
|
53
|
-
"@ai-sdk/google": "^1.1.
|
|
53
|
+
"@ai-sdk/google": "^1.1.9",
|
|
54
54
|
"@ai-sdk/openai": "^1.0.5",
|
|
55
55
|
"@ai-sdk/openai-compatible": "^0.1.8",
|
|
56
|
-
"@chainfuse/helpers": "^1.1.
|
|
57
|
-
"@chainfuse/types": "^1.6.
|
|
58
|
-
"ai": "^4.1.
|
|
56
|
+
"@chainfuse/helpers": "^1.1.3",
|
|
57
|
+
"@chainfuse/types": "^1.6.3",
|
|
58
|
+
"ai": "^4.1.21",
|
|
59
59
|
"chalk": "^5.4.1",
|
|
60
60
|
"haversine-distance": "^1.2.3",
|
|
61
61
|
"workers-ai-provider": "^0.0.10"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@cloudflare/workers-types": "^4.
|
|
65
|
-
"openai": "^4.
|
|
64
|
+
"@cloudflare/workers-types": "^4.20250204.0",
|
|
65
|
+
"openai": "^4.83.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "ff192abfdf16d12135b06254bed03c7453824e08"
|
|
68
68
|
}
|