@jterrazz/intelligence 4.2.0 → 6.0.0
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 +233 -217
- package/dist/formatting.cjs +2845 -0
- package/dist/formatting.cjs.map +1 -0
- package/dist/formatting.d.cts +105 -0
- package/dist/formatting.d.ts +105 -0
- package/dist/formatting.js +2819 -0
- package/dist/formatting.js.map +1 -0
- package/dist/index.cjs +683 -476
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +183 -1396
- package/dist/index.d.ts +183 -1396
- package/dist/index.js +680 -459
- package/dist/index.js.map +1 -1
- package/dist/oxlint.cjs +629 -0
- package/dist/oxlint.cjs.map +1 -0
- package/dist/oxlint.d.cts +132 -0
- package/dist/oxlint.d.ts +132 -0
- package/dist/oxlint.js +623 -0
- package/dist/oxlint.js.map +1 -0
- package/package.json +28 -22
- package/dist/parse-text.cjs +0 -57
- package/dist/parse-text.cjs.map +0 -1
- package/dist/parse-text.d.cts +0 -18
- package/dist/parse-text.d.ts +0 -18
- package/dist/parse-text.js +0 -52
- package/dist/parse-text.js.map +0 -1
- package/dist/text.cjs +0 -3
- package/dist/text.d.cts +0 -2
- package/dist/text.d.ts +0 -2
- package/dist/text.js +0 -2
package/dist/index.js
CHANGED
|
@@ -1,11 +1,94 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { jsonrepair } from "jsonrepair";
|
|
5
|
-
import { z } from "zod/v4";
|
|
6
|
-
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
1
|
+
import { OpenTelemetry } from "@ai-sdk/otel";
|
|
2
|
+
import { extractJsonMiddleware, registerTelemetry, wrapLanguageModel } from "ai";
|
|
3
|
+
import { trace } from "@opentelemetry/api";
|
|
7
4
|
import { createOpenAI } from "@ai-sdk/openai";
|
|
8
|
-
|
|
5
|
+
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
6
|
+
//#region src/middleware/cost.middleware.ts
|
|
7
|
+
const COST_ATTRIBUTE = "gen_ai.usage.cost";
|
|
8
|
+
const MODEL_ATTRIBUTE = "gen_ai.request.model";
|
|
9
|
+
function resolveCost(providerMetadata, usage, pricing) {
|
|
10
|
+
const actualCost = providerMetadata?.openrouter?.usage?.cost;
|
|
11
|
+
if (typeof actualCost === "number" && actualCost > 0) return actualCost;
|
|
12
|
+
if (pricing) {
|
|
13
|
+
const inputTokens = usage?.inputTokens?.total ?? 0;
|
|
14
|
+
const outputTokens = usage?.outputTokens?.total ?? 0;
|
|
15
|
+
return inputTokens / 1e6 * pricing.input + outputTokens / 1e6 * pricing.output;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function recordGeneration(modelRef, cost) {
|
|
19
|
+
const span = trace.getActiveSpan();
|
|
20
|
+
if (!span) return;
|
|
21
|
+
span.setAttribute(MODEL_ATTRIBUTE, modelRef);
|
|
22
|
+
if (cost !== void 0) span.setAttribute(COST_ATTRIBUTE, cost);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates middleware that enriches the active OpenTelemetry span with the
|
|
26
|
+
* model reference (`gen_ai.request.model`) and the USD cost
|
|
27
|
+
* (`gen_ai.usage.cost`) of a generation.
|
|
28
|
+
*
|
|
29
|
+
* Resolution order:
|
|
30
|
+
* 1. Actual cost reported by the provider (currently: OpenRouter's
|
|
31
|
+
* `providerMetadata.openrouter.usage.cost`).
|
|
32
|
+
* 2. Estimated cost from `pricing` (USD per million input/output tokens),
|
|
33
|
+
* computed from the reported token usage.
|
|
34
|
+
*
|
|
35
|
+
* The `gen_ai.usage.cost` attribute is set on `trace.getActiveSpan()` because
|
|
36
|
+
* that's the attribute Langfuse's OTel ingestion prioritizes over its own
|
|
37
|
+
* cost inference (`langfuse.observation.cost_details` is buggy on ingestion).
|
|
38
|
+
*
|
|
39
|
+
* Never throws: all enrichment is best-effort.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const model = wrapLanguageModel({
|
|
44
|
+
* model: provider.model('google/gemini-2.5-flash-lite'),
|
|
45
|
+
* middleware: [
|
|
46
|
+
* createCostMiddleware({
|
|
47
|
+
* modelRef: 'openrouter/google/gemini-2.5-flash-lite',
|
|
48
|
+
* pricing: { input: 0.1, output: 0.4 },
|
|
49
|
+
* }),
|
|
50
|
+
* ],
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
function createCostMiddleware(options) {
|
|
55
|
+
const { modelRef, pricing } = options;
|
|
56
|
+
return {
|
|
57
|
+
specificationVersion: "v4",
|
|
58
|
+
wrapGenerate: async ({ doGenerate }) => {
|
|
59
|
+
const result = await doGenerate();
|
|
60
|
+
try {
|
|
61
|
+
recordGeneration(modelRef, resolveCost(result.providerMetadata, result.usage, pricing));
|
|
62
|
+
} catch {}
|
|
63
|
+
return result;
|
|
64
|
+
},
|
|
65
|
+
wrapStream: async ({ doStream }) => {
|
|
66
|
+
const result = await doStream();
|
|
67
|
+
let finishUsage;
|
|
68
|
+
let finishProviderMetadata;
|
|
69
|
+
const transformStream = new TransformStream({
|
|
70
|
+
transform(chunk, controller) {
|
|
71
|
+
if (chunk.type === "finish") {
|
|
72
|
+
finishUsage = chunk.usage;
|
|
73
|
+
finishProviderMetadata = chunk.providerMetadata;
|
|
74
|
+
}
|
|
75
|
+
controller.enqueue(chunk);
|
|
76
|
+
},
|
|
77
|
+
flush() {
|
|
78
|
+
try {
|
|
79
|
+
recordGeneration(modelRef, resolveCost(finishProviderMetadata, finishUsage, pricing));
|
|
80
|
+
} catch {}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
...result,
|
|
85
|
+
stream: result.stream.pipeThrough(transformStream)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/middleware/logging.middleware.ts
|
|
9
92
|
/**
|
|
10
93
|
* Creates middleware that logs AI SDK requests and responses.
|
|
11
94
|
*/
|
|
@@ -13,7 +96,7 @@ function createLoggingMiddleware(options) {
|
|
|
13
96
|
const { logger, include = {} } = options;
|
|
14
97
|
const { params: includeParams, content: includeContent, usage: includeUsage = true } = include;
|
|
15
98
|
return {
|
|
16
|
-
specificationVersion: "
|
|
99
|
+
specificationVersion: "v4",
|
|
17
100
|
wrapGenerate: async ({ doGenerate, params, model }) => {
|
|
18
101
|
const startTime = Date.now();
|
|
19
102
|
logger.debug("ai.generate.start", {
|
|
@@ -63,7 +146,7 @@ function createLoggingMiddleware(options) {
|
|
|
63
146
|
}
|
|
64
147
|
});
|
|
65
148
|
return {
|
|
66
|
-
specificationVersion: "
|
|
149
|
+
specificationVersion: "v4",
|
|
67
150
|
...result,
|
|
68
151
|
stream: result.stream.pipeThrough(transformStream)
|
|
69
152
|
};
|
|
@@ -79,521 +162,659 @@ function createLoggingMiddleware(options) {
|
|
|
79
162
|
};
|
|
80
163
|
}
|
|
81
164
|
//#endregion
|
|
82
|
-
//#region
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
*
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (meta?.traceId) {
|
|
102
|
-
const extracted = providerMetadata?.extract(result.providerMetadata);
|
|
103
|
-
const outputText = result.content.filter((c) => c.type === "text").map((c) => c.text).join("");
|
|
104
|
-
observability.generation({
|
|
105
|
-
traceId: meta.traceId,
|
|
106
|
-
name: meta.name ?? "generation",
|
|
107
|
-
model: model.modelId,
|
|
108
|
-
input: params.prompt,
|
|
109
|
-
output: outputText,
|
|
110
|
-
startTime,
|
|
111
|
-
endTime,
|
|
112
|
-
usage: extracted?.usage,
|
|
113
|
-
cost: extracted?.cost,
|
|
114
|
-
metadata: meta.metadata
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
return result;
|
|
118
|
-
},
|
|
119
|
-
wrapStream: async ({ doStream, params, model }) => {
|
|
120
|
-
const startTime = /* @__PURE__ */ new Date();
|
|
121
|
-
const meta = params.providerOptions?.observability;
|
|
122
|
-
const result = await doStream();
|
|
123
|
-
if (!meta?.traceId) return result;
|
|
124
|
-
const chunks = [];
|
|
125
|
-
const transformStream = new TransformStream({
|
|
126
|
-
transform(chunk, controller) {
|
|
127
|
-
if (chunk.type === "text-delta") chunks.push(chunk.delta);
|
|
128
|
-
controller.enqueue(chunk);
|
|
129
|
-
},
|
|
130
|
-
flush() {
|
|
131
|
-
const endTime = /* @__PURE__ */ new Date();
|
|
132
|
-
observability.generation({
|
|
133
|
-
traceId: meta.traceId,
|
|
134
|
-
name: meta.name ?? "generation",
|
|
135
|
-
model: model.modelId,
|
|
136
|
-
input: params.prompt,
|
|
137
|
-
output: chunks.join(""),
|
|
138
|
-
startTime,
|
|
139
|
-
endTime,
|
|
140
|
-
metadata: meta.metadata
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
return {
|
|
145
|
-
specificationVersion: "v3",
|
|
146
|
-
...result,
|
|
147
|
-
stream: result.stream.pipeThrough(transformStream)
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
//#endregion
|
|
153
|
-
//#region src/observability/langfuse.adapter.ts
|
|
154
|
-
/**
|
|
155
|
-
* Langfuse adapter implementing ObservabilityPort
|
|
156
|
-
*/
|
|
157
|
-
var LangfuseAdapter = class {
|
|
158
|
-
client;
|
|
159
|
-
constructor(config) {
|
|
160
|
-
this.client = new Langfuse({
|
|
161
|
-
secretKey: config.secretKey,
|
|
162
|
-
publicKey: config.publicKey,
|
|
163
|
-
baseUrl: config.baseUrl,
|
|
164
|
-
environment: config.environment,
|
|
165
|
-
release: config.release
|
|
166
|
-
});
|
|
165
|
+
//#region node_modules/@ai-sdk/provider/dist/index.js
|
|
166
|
+
var marker = "vercel.ai.error";
|
|
167
|
+
var symbol = Symbol.for(marker);
|
|
168
|
+
var _a;
|
|
169
|
+
var _b;
|
|
170
|
+
var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
171
|
+
/**
|
|
172
|
+
* Creates an AI SDK Error.
|
|
173
|
+
*
|
|
174
|
+
* @param {Object} params - The parameters for creating the error.
|
|
175
|
+
* @param {string} params.name - The name of the error.
|
|
176
|
+
* @param {string} params.message - The error message.
|
|
177
|
+
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
178
|
+
*/
|
|
179
|
+
constructor({ name: name15, message, cause }) {
|
|
180
|
+
super(message);
|
|
181
|
+
this[_a] = true;
|
|
182
|
+
this.name = name15;
|
|
183
|
+
this.cause = cause;
|
|
167
184
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
total: params.usage.total ?? params.usage.input + params.usage.output,
|
|
176
|
-
...params.usage.reasoning !== void 0 && { reasoning: params.usage.reasoning },
|
|
177
|
-
...params.usage.cacheRead !== void 0 && { cache_read: params.usage.cacheRead },
|
|
178
|
-
...params.usage.cacheWrite !== void 0 && { cache_write: params.usage.cacheWrite }
|
|
179
|
-
} : void 0;
|
|
180
|
-
const costDetails = params.cost ? {
|
|
181
|
-
total: params.cost.total,
|
|
182
|
-
...params.cost.input !== void 0 && { input: params.cost.input },
|
|
183
|
-
...params.cost.output !== void 0 && { output: params.cost.output }
|
|
184
|
-
} : void 0;
|
|
185
|
-
this.client.generation({
|
|
186
|
-
traceId: params.traceId,
|
|
187
|
-
name: params.name,
|
|
188
|
-
model: params.model,
|
|
189
|
-
input: params.input,
|
|
190
|
-
output: params.output,
|
|
191
|
-
startTime: params.startTime,
|
|
192
|
-
endTime: params.endTime,
|
|
193
|
-
usageDetails,
|
|
194
|
-
costDetails,
|
|
195
|
-
metadata: params.metadata
|
|
196
|
-
});
|
|
185
|
+
/**
|
|
186
|
+
* Checks if the given error is an AI SDK Error.
|
|
187
|
+
* @param {unknown} error - The error to check.
|
|
188
|
+
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
189
|
+
*/
|
|
190
|
+
static isInstance(error) {
|
|
191
|
+
return _AISDKError.hasMarker(error, marker);
|
|
197
192
|
}
|
|
198
|
-
|
|
199
|
-
|
|
193
|
+
static hasMarker(error, marker16) {
|
|
194
|
+
const markerSymbol = Symbol.for(marker16);
|
|
195
|
+
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
200
196
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
197
|
+
};
|
|
198
|
+
var name = "AI_APICallError";
|
|
199
|
+
var marker2 = `vercel.ai.error.${name}`;
|
|
200
|
+
var symbol2 = Symbol.for(marker2);
|
|
201
|
+
var _a2;
|
|
202
|
+
var _b2;
|
|
203
|
+
var APICallError = class extends (_b2 = AISDKError, _a2 = symbol2, _b2) {
|
|
204
|
+
constructor({ message, url, requestBodyValues, statusCode, responseHeaders, responseBody, cause, isRetryable = statusCode != null && (statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500), data }) {
|
|
205
|
+
super({
|
|
206
|
+
name,
|
|
207
|
+
message,
|
|
208
|
+
cause
|
|
206
209
|
});
|
|
210
|
+
this[_a2] = true;
|
|
211
|
+
this.url = url;
|
|
212
|
+
this.requestBodyValues = requestBodyValues;
|
|
213
|
+
this.statusCode = statusCode;
|
|
214
|
+
this.responseHeaders = responseHeaders;
|
|
215
|
+
this.responseBody = responseBody;
|
|
216
|
+
this.isRetryable = isRetryable;
|
|
217
|
+
this.data = data;
|
|
218
|
+
}
|
|
219
|
+
static isInstance(error) {
|
|
220
|
+
return AISDKError.hasMarker(error, marker2);
|
|
207
221
|
}
|
|
208
222
|
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
function
|
|
227
|
-
return
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
223
|
+
var name2 = "AI_EmptyResponseBodyError";
|
|
224
|
+
var marker3 = `vercel.ai.error.${name2}`;
|
|
225
|
+
var symbol3 = Symbol.for(marker3);
|
|
226
|
+
var _a3;
|
|
227
|
+
var _b3;
|
|
228
|
+
(class extends (_b3 = AISDKError, _a3 = symbol3, _b3) {
|
|
229
|
+
constructor({ message = "Empty response body" } = {}) {
|
|
230
|
+
super({
|
|
231
|
+
name: name2,
|
|
232
|
+
message
|
|
233
|
+
});
|
|
234
|
+
this[_a3] = true;
|
|
235
|
+
}
|
|
236
|
+
static isInstance(error) {
|
|
237
|
+
return AISDKError.hasMarker(error, marker3);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
function getErrorMessage(error) {
|
|
241
|
+
if (error == null) return "unknown error";
|
|
242
|
+
if (typeof error === "string") return error;
|
|
243
|
+
if (error instanceof Error) return error.toString();
|
|
244
|
+
return JSON.stringify(error);
|
|
231
245
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
246
|
+
var name3 = "AI_InvalidArgumentError";
|
|
247
|
+
var marker4 = `vercel.ai.error.${name3}`;
|
|
248
|
+
var symbol4 = Symbol.for(marker4);
|
|
249
|
+
var _a4;
|
|
250
|
+
var _b4;
|
|
251
|
+
(class extends (_b4 = AISDKError, _a4 = symbol4, _b4) {
|
|
252
|
+
constructor({ message, cause, argument }) {
|
|
253
|
+
super({
|
|
254
|
+
name: name3,
|
|
240
255
|
message,
|
|
241
256
|
cause
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
var
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
text
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
257
|
+
});
|
|
258
|
+
this[_a4] = true;
|
|
259
|
+
this.argument = argument;
|
|
260
|
+
}
|
|
261
|
+
static isInstance(error) {
|
|
262
|
+
return AISDKError.hasMarker(error, marker4);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
var name4 = "AI_InvalidPromptError";
|
|
266
|
+
var marker5 = `vercel.ai.error.${name4}`;
|
|
267
|
+
var symbol5 = Symbol.for(marker5);
|
|
268
|
+
var _a5;
|
|
269
|
+
var _b5;
|
|
270
|
+
(class extends (_b5 = AISDKError, _a5 = symbol5, _b5) {
|
|
271
|
+
constructor({ prompt, message, cause }) {
|
|
272
|
+
super({
|
|
273
|
+
name: name4,
|
|
274
|
+
message: `Invalid prompt: ${message}`,
|
|
275
|
+
cause
|
|
276
|
+
});
|
|
277
|
+
this[_a5] = true;
|
|
278
|
+
this.prompt = prompt;
|
|
279
|
+
}
|
|
280
|
+
static isInstance(error) {
|
|
281
|
+
return AISDKError.hasMarker(error, marker5);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
var name5 = "AI_InvalidResponseDataError";
|
|
285
|
+
var marker6 = `vercel.ai.error.${name5}`;
|
|
286
|
+
var symbol6 = Symbol.for(marker6);
|
|
287
|
+
var _a6;
|
|
288
|
+
var _b6;
|
|
289
|
+
(class extends (_b6 = AISDKError, _a6 = symbol6, _b6) {
|
|
290
|
+
constructor({ data, message = `Invalid response data: ${JSON.stringify(data)}.` }) {
|
|
291
|
+
super({
|
|
292
|
+
name: name5,
|
|
293
|
+
message
|
|
294
|
+
});
|
|
295
|
+
this[_a6] = true;
|
|
296
|
+
this.data = data;
|
|
297
|
+
}
|
|
298
|
+
static isInstance(error) {
|
|
299
|
+
return AISDKError.hasMarker(error, marker6);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
var name6 = "AI_JSONParseError";
|
|
303
|
+
var marker7 = `vercel.ai.error.${name6}`;
|
|
304
|
+
var symbol7 = Symbol.for(marker7);
|
|
305
|
+
var _a7;
|
|
306
|
+
var _b7;
|
|
307
|
+
(class extends (_b7 = AISDKError, _a7 = symbol7, _b7) {
|
|
308
|
+
constructor({ text, cause }) {
|
|
309
|
+
super({
|
|
310
|
+
name: name6,
|
|
311
|
+
message: `JSON parsing failed: Text: ${text}.
|
|
312
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
313
|
+
cause
|
|
314
|
+
});
|
|
315
|
+
this[_a7] = true;
|
|
297
316
|
this.text = text;
|
|
298
317
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
if (schema instanceof z.ZodBoolean) return Boolean(value);
|
|
302
|
-
if (schema instanceof z.ZodNull) return null;
|
|
303
|
-
if (schema instanceof z.ZodNumber) return Number(value);
|
|
304
|
-
if (schema instanceof z.ZodString) return String(value);
|
|
305
|
-
return value;
|
|
306
|
-
}
|
|
307
|
-
function extractArray(text, originalText) {
|
|
308
|
-
const start = text.indexOf("[");
|
|
309
|
-
const end = text.lastIndexOf("]");
|
|
310
|
-
if (start === -1 || end === -1) throw new ParseObjectError("No array found in response", void 0, originalText);
|
|
311
|
-
try {
|
|
312
|
-
const raw = text.slice(start, end + 1);
|
|
313
|
-
return JSON.parse(jsonrepair(raw));
|
|
314
|
-
} catch (error) {
|
|
315
|
-
throw new ParseObjectError("Failed to parse array JSON", error, originalText);
|
|
318
|
+
static isInstance(error) {
|
|
319
|
+
return AISDKError.hasMarker(error, marker7);
|
|
316
320
|
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
321
|
+
});
|
|
322
|
+
var name7 = "AI_LoadAPIKeyError";
|
|
323
|
+
var marker8 = `vercel.ai.error.${name7}`;
|
|
324
|
+
var symbol8 = Symbol.for(marker8);
|
|
325
|
+
var _a8;
|
|
326
|
+
var _b8;
|
|
327
|
+
(class extends (_b8 = AISDKError, _a8 = symbol8, _b8) {
|
|
328
|
+
constructor({ message }) {
|
|
329
|
+
super({
|
|
330
|
+
name: name7,
|
|
331
|
+
message
|
|
332
|
+
});
|
|
333
|
+
this[_a8] = true;
|
|
327
334
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const trimmed = text.trim();
|
|
331
|
-
try {
|
|
332
|
-
return convertToPrimitive(JSON.parse(trimmed), schema);
|
|
333
|
-
} catch {
|
|
334
|
-
return convertToPrimitive(trimmed, schema);
|
|
335
|
+
static isInstance(error) {
|
|
336
|
+
return AISDKError.hasMarker(error, marker8);
|
|
335
337
|
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
throw new ParseObjectError("Unsupported schema type", void 0, originalText);
|
|
350
|
-
}
|
|
351
|
-
function extractJsonFromCodeBlock(block) {
|
|
352
|
-
const content = block.replace(/```(?:json)?\r?\n([^`]*?)\r?\n```/, "$1").trim();
|
|
353
|
-
try {
|
|
354
|
-
JSON.parse(content);
|
|
355
|
-
return content;
|
|
356
|
-
} catch {
|
|
357
|
-
return null;
|
|
338
|
+
});
|
|
339
|
+
var name8 = "AI_LoadSettingError";
|
|
340
|
+
var marker9 = `vercel.ai.error.${name8}`;
|
|
341
|
+
var symbol9 = Symbol.for(marker9);
|
|
342
|
+
var _a9;
|
|
343
|
+
var _b9;
|
|
344
|
+
(class extends (_b9 = AISDKError, _a9 = symbol9, _b9) {
|
|
345
|
+
constructor({ message }) {
|
|
346
|
+
super({
|
|
347
|
+
name: name8,
|
|
348
|
+
message
|
|
349
|
+
});
|
|
350
|
+
this[_a9] = true;
|
|
358
351
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
352
|
+
static isInstance(error) {
|
|
353
|
+
return AISDKError.hasMarker(error, marker9);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
var name9 = "AI_NoContentGeneratedError";
|
|
357
|
+
var marker10 = `vercel.ai.error.${name9}`;
|
|
358
|
+
var symbol10 = Symbol.for(marker10);
|
|
359
|
+
var _a10;
|
|
360
|
+
var _b10;
|
|
361
|
+
(class extends (_b10 = AISDKError, _a10 = symbol10, _b10) {
|
|
362
|
+
constructor({ message = "No content generated." } = {}) {
|
|
363
|
+
super({
|
|
364
|
+
name: name9,
|
|
365
|
+
message
|
|
366
|
+
});
|
|
367
|
+
this[_a10] = true;
|
|
368
|
+
}
|
|
369
|
+
static isInstance(error) {
|
|
370
|
+
return AISDKError.hasMarker(error, marker10);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
var name10 = "AI_NoSuchModelError";
|
|
374
|
+
var marker11 = `vercel.ai.error.${name10}`;
|
|
375
|
+
var symbol11 = Symbol.for(marker11);
|
|
376
|
+
var _a11;
|
|
377
|
+
var _b11;
|
|
378
|
+
(class extends (_b11 = AISDKError, _a11 = symbol11, _b11) {
|
|
379
|
+
constructor({ errorName = name10, modelId, modelType, message = `No such ${modelType}: ${modelId}` }) {
|
|
380
|
+
super({
|
|
381
|
+
name: errorName,
|
|
382
|
+
message
|
|
383
|
+
});
|
|
384
|
+
this[_a11] = true;
|
|
385
|
+
this.modelId = modelId;
|
|
386
|
+
this.modelType = modelType;
|
|
387
|
+
}
|
|
388
|
+
static isInstance(error) {
|
|
389
|
+
return AISDKError.hasMarker(error, marker11);
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
var name11 = "AI_NoSuchProviderReferenceError";
|
|
393
|
+
var marker12 = `vercel.ai.error.${name11}`;
|
|
394
|
+
var symbol12 = Symbol.for(marker12);
|
|
395
|
+
var _a12;
|
|
396
|
+
var _b12;
|
|
397
|
+
(class extends (_b12 = AISDKError, _a12 = symbol12, _b12) {
|
|
398
|
+
constructor({ provider, reference, message = `No provider reference found for provider '${provider}'. Available providers: ${Object.keys(reference).join(", ")}` }) {
|
|
399
|
+
super({
|
|
400
|
+
name: name11,
|
|
401
|
+
message
|
|
402
|
+
});
|
|
403
|
+
this[_a12] = true;
|
|
404
|
+
this.provider = provider;
|
|
405
|
+
this.reference = reference;
|
|
406
|
+
}
|
|
407
|
+
static isInstance(error) {
|
|
408
|
+
return AISDKError.hasMarker(error, marker12);
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
var name12 = "AI_TooManyEmbeddingValuesForCallError";
|
|
412
|
+
var marker13 = `vercel.ai.error.${name12}`;
|
|
413
|
+
var symbol13 = Symbol.for(marker13);
|
|
414
|
+
var _a13;
|
|
415
|
+
var _b13;
|
|
416
|
+
(class extends (_b13 = AISDKError, _a13 = symbol13, _b13) {
|
|
417
|
+
constructor(options) {
|
|
418
|
+
super({
|
|
419
|
+
name: name12,
|
|
420
|
+
message: `Too many values for a single embedding call. The ${options.provider} model "${options.modelId}" can only embed up to ${options.maxEmbeddingsPerCall} values per call, but ${options.values.length} values were provided.`
|
|
421
|
+
});
|
|
422
|
+
this[_a13] = true;
|
|
423
|
+
this.provider = options.provider;
|
|
424
|
+
this.modelId = options.modelId;
|
|
425
|
+
this.maxEmbeddingsPerCall = options.maxEmbeddingsPerCall;
|
|
426
|
+
this.values = options.values;
|
|
427
|
+
}
|
|
428
|
+
static isInstance(error) {
|
|
429
|
+
return AISDKError.hasMarker(error, marker13);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
var name13 = "AI_TypeValidationError";
|
|
433
|
+
var marker14 = `vercel.ai.error.${name13}`;
|
|
434
|
+
var symbol14 = Symbol.for(marker14);
|
|
435
|
+
var _a14;
|
|
436
|
+
var _b14;
|
|
437
|
+
(class _TypeValidationError extends (_b14 = AISDKError, _a14 = symbol14, _b14) {
|
|
438
|
+
constructor({ value, cause, context }) {
|
|
439
|
+
let contextPrefix = "Type validation failed";
|
|
440
|
+
if (context == null ? void 0 : context.field) contextPrefix += ` for ${context.field}`;
|
|
441
|
+
if ((context == null ? void 0 : context.entityName) || (context == null ? void 0 : context.entityId)) {
|
|
442
|
+
contextPrefix += " (";
|
|
443
|
+
const parts = [];
|
|
444
|
+
if (context.entityName) parts.push(context.entityName);
|
|
445
|
+
if (context.entityId) parts.push(`id: "${context.entityId}"`);
|
|
446
|
+
contextPrefix += parts.join(", ");
|
|
447
|
+
contextPrefix += ")";
|
|
381
448
|
}
|
|
449
|
+
super({
|
|
450
|
+
name: name13,
|
|
451
|
+
message: `${contextPrefix}: Value: ${JSON.stringify(value)}.
|
|
452
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
453
|
+
cause
|
|
454
|
+
});
|
|
455
|
+
this[_a14] = true;
|
|
456
|
+
this.value = value;
|
|
457
|
+
this.context = context;
|
|
382
458
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
459
|
+
static isInstance(error) {
|
|
460
|
+
return AISDKError.hasMarker(error, marker14);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Wraps an error into a TypeValidationError.
|
|
464
|
+
* If the cause is already a TypeValidationError with the same value and context, it returns the cause.
|
|
465
|
+
* Otherwise, it creates a new TypeValidationError.
|
|
466
|
+
*
|
|
467
|
+
* @param {Object} params - The parameters for wrapping the error.
|
|
468
|
+
* @param {unknown} params.value - The value that failed validation.
|
|
469
|
+
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
470
|
+
* @param {TypeValidationContext} params.context - Optional context about what is being validated.
|
|
471
|
+
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
472
|
+
*/
|
|
473
|
+
static wrap({ value, cause, context }) {
|
|
474
|
+
var _a16, _b16, _c;
|
|
475
|
+
if (_TypeValidationError.isInstance(cause) && cause.value === value && ((_a16 = cause.context) == null ? void 0 : _a16.field) === (context == null ? void 0 : context.field) && ((_b16 = cause.context) == null ? void 0 : _b16.entityName) === (context == null ? void 0 : context.entityName) && ((_c = cause.context) == null ? void 0 : _c.entityId) === (context == null ? void 0 : context.entityId)) return cause;
|
|
476
|
+
return new _TypeValidationError({
|
|
477
|
+
value,
|
|
478
|
+
cause,
|
|
479
|
+
context
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
var name14 = "AI_UnsupportedFunctionalityError";
|
|
484
|
+
var marker15 = `vercel.ai.error.${name14}`;
|
|
485
|
+
var symbol15 = Symbol.for(marker15);
|
|
486
|
+
var _a15;
|
|
487
|
+
var _b15;
|
|
488
|
+
(class extends (_b15 = AISDKError, _a15 = symbol15, _b15) {
|
|
489
|
+
constructor({ functionality, message = `'${functionality}' functionality not supported.` }) {
|
|
490
|
+
super({
|
|
491
|
+
name: name14,
|
|
492
|
+
message
|
|
493
|
+
});
|
|
494
|
+
this[_a15] = true;
|
|
495
|
+
this.functionality = functionality;
|
|
496
|
+
}
|
|
497
|
+
static isInstance(error) {
|
|
498
|
+
return AISDKError.hasMarker(error, marker15);
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
//#endregion
|
|
502
|
+
//#region src/model/fallback-model.ts
|
|
503
|
+
const RETRYABLE_MESSAGE_PATTERNS = [
|
|
504
|
+
/ECONNREFUSED/i,
|
|
505
|
+
/ECONNRESET/i,
|
|
506
|
+
/ETIMEDOUT/i,
|
|
507
|
+
/EAI_AGAIN/i,
|
|
508
|
+
/ENOTFOUND/i,
|
|
509
|
+
/timed?\s*out/i,
|
|
510
|
+
/network/i,
|
|
511
|
+
/fetch failed/i
|
|
512
|
+
];
|
|
513
|
+
function isRetryableError(error) {
|
|
514
|
+
if (APICallError.isInstance(error)) {
|
|
515
|
+
if (typeof error.statusCode === "number") return error.statusCode === 429 || error.statusCode >= 500;
|
|
516
|
+
return error.isRetryable;
|
|
517
|
+
}
|
|
518
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
519
|
+
return RETRYABLE_MESSAGE_PATTERNS.some((pattern) => pattern.test(message));
|
|
403
520
|
}
|
|
404
521
|
/**
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
* - JSON embedded in prose text
|
|
410
|
-
* - Malformed JSON (auto-repaired)
|
|
411
|
-
* - Escaped unicode and special characters
|
|
522
|
+
* Creates a `LanguageModelV4` that transparently falls back to a secondary
|
|
523
|
+
* model when the primary model fails with a retryable error (HTTP 429, 5xx,
|
|
524
|
+
* network errors/timeouts). Non-retryable errors (400s, validation, abort)
|
|
525
|
+
* propagate unchanged.
|
|
412
526
|
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* @returns The parsed and validated data
|
|
416
|
-
* @throws {ParseObjectError} When parsing or validation fails
|
|
527
|
+
* This is a model, not a middleware — middleware cannot switch the
|
|
528
|
+
* underlying model, only transform a single model's behavior.
|
|
417
529
|
*
|
|
418
530
|
* @example
|
|
419
531
|
* ```ts
|
|
420
|
-
* const
|
|
421
|
-
*
|
|
422
|
-
*
|
|
532
|
+
* const model = createFallbackModel({
|
|
533
|
+
* primary: provider.model('anthropic/claude-sonnet-4'),
|
|
534
|
+
* fallback: provider.model('openai/gpt-4o-mini'),
|
|
535
|
+
* logger,
|
|
536
|
+
* });
|
|
423
537
|
* ```
|
|
424
538
|
*/
|
|
425
|
-
function
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
539
|
+
function createFallbackModel(options) {
|
|
540
|
+
const { primary, fallback, logger } = options;
|
|
541
|
+
const primaryModel = primary;
|
|
542
|
+
const fallbackModel = fallback;
|
|
543
|
+
function logFallback(error) {
|
|
544
|
+
logger?.warn("ai.fallback.triggered", {
|
|
545
|
+
modelIds: [primaryModel.modelId, fallbackModel.modelId],
|
|
546
|
+
error: error instanceof Error ? error.message : String(error)
|
|
547
|
+
});
|
|
433
548
|
}
|
|
549
|
+
return {
|
|
550
|
+
specificationVersion: "v4",
|
|
551
|
+
provider: primaryModel.provider,
|
|
552
|
+
modelId: primaryModel.modelId,
|
|
553
|
+
supportedUrls: primaryModel.supportedUrls,
|
|
554
|
+
async doGenerate(callOptions) {
|
|
555
|
+
try {
|
|
556
|
+
return await primaryModel.doGenerate(callOptions);
|
|
557
|
+
} catch (error) {
|
|
558
|
+
if (!isRetryableError(error)) throw error;
|
|
559
|
+
logFallback(error);
|
|
560
|
+
return fallbackModel.doGenerate(callOptions);
|
|
561
|
+
}
|
|
562
|
+
},
|
|
563
|
+
async doStream(callOptions) {
|
|
564
|
+
try {
|
|
565
|
+
return await primaryModel.doStream(callOptions);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
if (!isRetryableError(error)) throw error;
|
|
568
|
+
logFallback(error);
|
|
569
|
+
return fallbackModel.doStream(callOptions);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
};
|
|
434
573
|
}
|
|
435
574
|
//#endregion
|
|
436
|
-
//#region src/
|
|
575
|
+
//#region src/middleware/schema-instruction.middleware.ts
|
|
576
|
+
function buildInstruction(responseFormat) {
|
|
577
|
+
if (responseFormat?.type !== "json") return "";
|
|
578
|
+
const lines = ["You must respond with valid JSON only — no prose, no markdown code fences."];
|
|
579
|
+
if (responseFormat.schema) lines.push("The JSON must strictly conform to this JSON schema:", JSON.stringify(responseFormat.schema));
|
|
580
|
+
return lines.join("\n");
|
|
581
|
+
}
|
|
582
|
+
function appendToLastUserMessage(prompt, instruction) {
|
|
583
|
+
const lastUserIndex = prompt.findLastIndex((message) => message.role === "user");
|
|
584
|
+
if (lastUserIndex === -1) return [...prompt, {
|
|
585
|
+
content: [{
|
|
586
|
+
text: instruction,
|
|
587
|
+
type: "text"
|
|
588
|
+
}],
|
|
589
|
+
role: "user"
|
|
590
|
+
}];
|
|
591
|
+
return prompt.map((message, index) => {
|
|
592
|
+
if (index !== lastUserIndex || message.role !== "user") return message;
|
|
593
|
+
const lastTextIndex = message.content.findLastIndex((part) => part.type === "text");
|
|
594
|
+
if (lastTextIndex === -1) return {
|
|
595
|
+
...message,
|
|
596
|
+
content: [...message.content, {
|
|
597
|
+
text: instruction,
|
|
598
|
+
type: "text"
|
|
599
|
+
}]
|
|
600
|
+
};
|
|
601
|
+
return {
|
|
602
|
+
...message,
|
|
603
|
+
content: message.content.map((part, partIndex) => {
|
|
604
|
+
if (partIndex !== lastTextIndex || part.type !== "text") return part;
|
|
605
|
+
return {
|
|
606
|
+
...part,
|
|
607
|
+
text: `${part.text}\n\n${instruction}`
|
|
608
|
+
};
|
|
609
|
+
})
|
|
610
|
+
};
|
|
611
|
+
});
|
|
612
|
+
}
|
|
437
613
|
/**
|
|
438
|
-
*
|
|
439
|
-
*
|
|
614
|
+
* Creates middleware that injects the JSON schema of a structured-output
|
|
615
|
+
* request into the last user message.
|
|
616
|
+
*
|
|
617
|
+
* Some gateways silently drop the native structured-output field when
|
|
618
|
+
* translating to their backend, so the model never sees the schema and
|
|
619
|
+
* answers in free prose. This middleware re-states the schema as part of the
|
|
620
|
+
* user message so structured output works regardless. It targets the user
|
|
621
|
+
* message rather than a system message because gateways backed by cloaked
|
|
622
|
+
* CLI agents bury injected system messages under their own persona prompt and
|
|
623
|
+
* ignore them. The original `responseFormat` is left untouched: backends that
|
|
624
|
+
* honor it get the native signal too.
|
|
625
|
+
*
|
|
626
|
+
* No-op for text generations (no `responseFormat`, or `type: 'text'`).
|
|
440
627
|
*/
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
temperature
|
|
452
|
-
});
|
|
453
|
-
if (!response.text || response.text.trim() === "") return generationFailure("EMPTY_RESULT", "AI returned empty response");
|
|
454
|
-
try {
|
|
455
|
-
return generationSuccess(parseObject(response.text, schema));
|
|
456
|
-
} catch (error) {
|
|
457
|
-
if (error instanceof ParseObjectError) return generationFailure("PARSING_FAILED", error.message, error);
|
|
458
|
-
return generationFailure("VALIDATION_FAILED", "Schema validation failed", error);
|
|
628
|
+
function createSchemaInstructionMiddleware() {
|
|
629
|
+
return {
|
|
630
|
+
specificationVersion: "v4",
|
|
631
|
+
transformParams: ({ params }) => {
|
|
632
|
+
const instruction = buildInstruction(params.responseFormat);
|
|
633
|
+
if (!instruction) return Promise.resolve(params);
|
|
634
|
+
return Promise.resolve({
|
|
635
|
+
...params,
|
|
636
|
+
prompt: appendToLastUserMessage(params.prompt, instruction)
|
|
637
|
+
});
|
|
459
638
|
}
|
|
460
|
-
}
|
|
461
|
-
return generationFailure(classifyError(error), error instanceof Error ? error.message : "Unknown error", error);
|
|
462
|
-
}
|
|
639
|
+
};
|
|
463
640
|
}
|
|
464
641
|
//#endregion
|
|
465
|
-
//#region src/
|
|
642
|
+
//#region src/provider/gateway.provider.ts
|
|
466
643
|
/**
|
|
467
|
-
* Creates a
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
* Use this with `generateText` when the provider doesn't support native
|
|
471
|
-
* structured outputs, then parse the response with `parseObject`.
|
|
644
|
+
* Creates a provider for gateways exposing a chat-completions API — any API
|
|
645
|
+
* implementing the OpenAI chat completions spec.
|
|
472
646
|
*
|
|
473
|
-
*
|
|
474
|
-
*
|
|
647
|
+
* Every model returned is automatically wrapped with two safety nets for
|
|
648
|
+
* gateways that don't support native structured output:
|
|
649
|
+
* - `createSchemaInstructionMiddleware` injects the JSON schema into the
|
|
650
|
+
* system prompt (some gateways silently drop `response_format`);
|
|
651
|
+
* - `extractJsonMiddleware` strips markdown code fences from JSON responses.
|
|
475
652
|
*
|
|
476
653
|
* @example
|
|
477
654
|
* ```ts
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
* const schema = z.object({ title: z.string(), tags: z.array(z.string()) });
|
|
482
|
-
*
|
|
483
|
-
* const { text } = await generateText({
|
|
484
|
-
* model,
|
|
485
|
-
* prompt: 'Generate an article about TypeScript',
|
|
486
|
-
* system: createSchemaPrompt(schema),
|
|
487
|
-
* });
|
|
655
|
+
* const provider = createGatewayProvider({ baseURL: 'https://gateway.example.com/v1' });
|
|
656
|
+
* const model = provider.model('gpt-4o-mini');
|
|
488
657
|
*
|
|
489
|
-
* const
|
|
658
|
+
* const { text } = await generateText({ model, prompt: 'Hello!' });
|
|
490
659
|
* ```
|
|
491
660
|
*/
|
|
492
|
-
function
|
|
493
|
-
const
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
\`\`\`json
|
|
504
|
-
${schemaJson}
|
|
505
|
-
\`\`\`
|
|
506
|
-
|
|
507
|
-
Your response should be only the ${jsonSchema.type} value, without any JSON wrapping or additional text.
|
|
508
|
-
</OUTPUT_FORMAT>`;
|
|
509
|
-
return `<OUTPUT_FORMAT>
|
|
510
|
-
You must respond with valid JSON that matches this JSON schema:
|
|
511
|
-
|
|
512
|
-
\`\`\`json
|
|
513
|
-
${schemaJson}
|
|
514
|
-
\`\`\`
|
|
515
|
-
|
|
516
|
-
Your response must be parseable JSON that validates against this schema. Do not include any text outside the JSON.
|
|
517
|
-
</OUTPUT_FORMAT>`;
|
|
661
|
+
function createGatewayProvider(config) {
|
|
662
|
+
const openai = createOpenAI({
|
|
663
|
+
apiKey: config.apiKey,
|
|
664
|
+
baseURL: config.baseURL
|
|
665
|
+
});
|
|
666
|
+
return { model(id) {
|
|
667
|
+
return wrapLanguageModel({
|
|
668
|
+
model: openai.chat(id),
|
|
669
|
+
middleware: [createSchemaInstructionMiddleware(), extractJsonMiddleware()]
|
|
670
|
+
});
|
|
671
|
+
} };
|
|
518
672
|
}
|
|
519
673
|
//#endregion
|
|
520
674
|
//#region src/provider/openrouter.provider.ts
|
|
521
675
|
/**
|
|
522
676
|
* Creates an OpenRouter provider for AI SDK models.
|
|
523
677
|
*
|
|
678
|
+
* Per-call options (reasoning effort, max tokens, etc.) are no longer
|
|
679
|
+
* configured here — pass them at the call site via `providerOptions.openrouter`
|
|
680
|
+
* on `generateText`/`streamText`.
|
|
681
|
+
*
|
|
524
682
|
* @example
|
|
525
683
|
* ```ts
|
|
526
684
|
* const provider = createOpenRouterProvider({ apiKey: process.env.OPENROUTER_API_KEY });
|
|
527
685
|
* const model = provider.model('anthropic/claude-sonnet-4-20250514');
|
|
528
686
|
*
|
|
529
|
-
* const { text } = await generateText({
|
|
687
|
+
* const { text } = await generateText({
|
|
688
|
+
* model,
|
|
689
|
+
* prompt: 'Hello!',
|
|
690
|
+
* providerOptions: { openrouter: { reasoning: { effort: 'high' } } },
|
|
691
|
+
* });
|
|
530
692
|
* ```
|
|
531
693
|
*/
|
|
532
694
|
function createOpenRouterProvider(config) {
|
|
533
|
-
const openrouter = createOpenRouter({
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
695
|
+
const openrouter = createOpenRouter({
|
|
696
|
+
apiKey: config.apiKey,
|
|
697
|
+
appName: config.metadata?.application,
|
|
698
|
+
appUrl: config.metadata?.website
|
|
699
|
+
});
|
|
700
|
+
return { model(id) {
|
|
701
|
+
return openrouter(id);
|
|
539
702
|
} };
|
|
540
703
|
}
|
|
541
704
|
//#endregion
|
|
542
|
-
//#region src/
|
|
705
|
+
//#region src/factory/create-intelligence.ts
|
|
706
|
+
let telemetryRegistered = false;
|
|
543
707
|
/**
|
|
544
|
-
*
|
|
708
|
+
* Registers the AI SDK OpenTelemetry integration, once per process. Best
|
|
709
|
+
* effort — if the host app has no OTel SDK configured, `@ai-sdk/otel` spans
|
|
710
|
+
* are simply dropped rather than throwing.
|
|
545
711
|
*/
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
cacheRead: usage.promptTokensDetails?.cachedTokens
|
|
558
|
-
},
|
|
559
|
-
cost: usage.cost !== void 0 ? { total: usage.cost } : void 0
|
|
560
|
-
};
|
|
712
|
+
function ensureTelemetryRegistered() {
|
|
713
|
+
if (telemetryRegistered) return;
|
|
714
|
+
telemetryRegistered = true;
|
|
715
|
+
try {
|
|
716
|
+
registerTelemetry(new OpenTelemetry());
|
|
717
|
+
} catch {}
|
|
718
|
+
}
|
|
719
|
+
function createProvider(config) {
|
|
720
|
+
switch (config.type) {
|
|
721
|
+
case "gateway": return createGatewayProvider(config);
|
|
722
|
+
case "openrouter": return createOpenRouterProvider(config);
|
|
561
723
|
}
|
|
562
|
-
};
|
|
563
|
-
//#endregion
|
|
564
|
-
//#region src/provider/openai-compatible.provider.ts
|
|
565
|
-
/**
|
|
566
|
-
* Creates a provider for OpenAI-compatible APIs.
|
|
567
|
-
* Works with any API implementing the OpenAI chat completions spec.
|
|
568
|
-
*/
|
|
569
|
-
function createOpenAICompatibleProvider(config) {
|
|
570
|
-
const openai = createOpenAI({
|
|
571
|
-
apiKey: config.apiKey,
|
|
572
|
-
baseURL: config.baseURL
|
|
573
|
-
});
|
|
574
|
-
return { model(name, _options = {}) {
|
|
575
|
-
return openai(config.modelMapping?.[name] ?? name);
|
|
576
|
-
} };
|
|
577
724
|
}
|
|
578
|
-
|
|
579
|
-
|
|
725
|
+
function assertProviderExists(providerKey, providers) {
|
|
726
|
+
if (providers[providerKey]) return;
|
|
727
|
+
const available = Object.keys(providers).join(", ") || "(none configured)";
|
|
728
|
+
throw new Error(`Unknown provider "${providerKey}". Available providers: ${available}.`);
|
|
729
|
+
}
|
|
580
730
|
/**
|
|
581
|
-
*
|
|
582
|
-
*
|
|
731
|
+
* Creates a composition root over AI SDK v7: resolves each agent's
|
|
732
|
+
* `provider`/`model` pair into a fully instrumented `LanguageModel` — cost
|
|
733
|
+
* tracking, optional fallback, and optional logging — cached per agent name.
|
|
734
|
+
*
|
|
735
|
+
* Registers the `@ai-sdk/otel` telemetry integration on first use (idempotent,
|
|
736
|
+
* best-effort). The host app is expected to have already registered an
|
|
737
|
+
* OpenTelemetry Node SDK (e.g. via `@jterrazz/telemetry`).
|
|
738
|
+
*
|
|
739
|
+
* @example
|
|
740
|
+
* ```ts
|
|
741
|
+
* const intelligence = createIntelligence({
|
|
742
|
+
* providers: {
|
|
743
|
+
* openrouter: { type: 'openrouter', apiKey: process.env.OPENROUTER_API_KEY },
|
|
744
|
+
* },
|
|
745
|
+
* agents: {
|
|
746
|
+
* summarizer: {
|
|
747
|
+
* provider: 'openrouter',
|
|
748
|
+
* model: 'google/gemini-2.5-flash-lite',
|
|
749
|
+
* fallback: { provider: 'openrouter', model: 'openai/gpt-4o-mini' },
|
|
750
|
+
* },
|
|
751
|
+
* },
|
|
752
|
+
* pricing: {
|
|
753
|
+
* 'openrouter/google/gemini-2.5-flash-lite': { input: 0.1, output: 0.4 },
|
|
754
|
+
* },
|
|
755
|
+
* logger,
|
|
756
|
+
* });
|
|
757
|
+
*
|
|
758
|
+
* const model = intelligence.model('summarizer');
|
|
759
|
+
* const { text } = await generateText({ model, prompt: 'Hello!' });
|
|
760
|
+
* ```
|
|
583
761
|
*/
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
762
|
+
function createIntelligence(config) {
|
|
763
|
+
ensureTelemetryRegistered();
|
|
764
|
+
const { agents, logger, pricing, providers } = config;
|
|
765
|
+
const providerCache = /* @__PURE__ */ new Map();
|
|
766
|
+
const modelCache = /* @__PURE__ */ new Map();
|
|
767
|
+
function resolveProvider(providerKey) {
|
|
768
|
+
let provider = providerCache.get(providerKey);
|
|
769
|
+
if (!provider) {
|
|
770
|
+
provider = createProvider(providers[providerKey]);
|
|
771
|
+
providerCache.set(providerKey, provider);
|
|
772
|
+
}
|
|
773
|
+
return provider;
|
|
594
774
|
}
|
|
595
|
-
|
|
775
|
+
function buildModel(ref) {
|
|
776
|
+
assertProviderExists(ref.provider, providers);
|
|
777
|
+
const baseModel = resolveProvider(ref.provider).model(ref.model);
|
|
778
|
+
const pricingKey = `${ref.provider}/${ref.model}`;
|
|
779
|
+
return wrapLanguageModel({
|
|
780
|
+
model: baseModel,
|
|
781
|
+
middleware: [createCostMiddleware({
|
|
782
|
+
modelRef: pricingKey,
|
|
783
|
+
pricing: pricing?.[pricingKey]
|
|
784
|
+
})]
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
function buildAgentModel(agentName) {
|
|
788
|
+
const agentConfig = agents[agentName];
|
|
789
|
+
if (!agentConfig) {
|
|
790
|
+
const available = Object.keys(agents).join(", ") || "(none configured)";
|
|
791
|
+
throw new Error(`Unknown agent "${agentName}". Available agents: ${available}.`);
|
|
792
|
+
}
|
|
793
|
+
const primary = buildModel({
|
|
794
|
+
model: agentConfig.model,
|
|
795
|
+
provider: agentConfig.provider
|
|
796
|
+
});
|
|
797
|
+
const composed = agentConfig.fallback ? createFallbackModel({
|
|
798
|
+
fallback: buildModel(agentConfig.fallback),
|
|
799
|
+
logger,
|
|
800
|
+
primary
|
|
801
|
+
}) : primary;
|
|
802
|
+
if (!logger) return composed;
|
|
803
|
+
return wrapLanguageModel({
|
|
804
|
+
model: composed,
|
|
805
|
+
middleware: [createLoggingMiddleware({ logger })]
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
return { model(agentName) {
|
|
809
|
+
let model = modelCache.get(agentName);
|
|
810
|
+
if (!model) {
|
|
811
|
+
model = buildAgentModel(agentName);
|
|
812
|
+
modelCache.set(agentName, model);
|
|
813
|
+
}
|
|
814
|
+
return model;
|
|
815
|
+
} };
|
|
816
|
+
}
|
|
596
817
|
//#endregion
|
|
597
|
-
export {
|
|
818
|
+
export { createCostMiddleware, createFallbackModel, createGatewayProvider, createIntelligence, createLoggingMiddleware, createOpenRouterProvider, createSchemaInstructionMiddleware };
|
|
598
819
|
|
|
599
820
|
//# sourceMappingURL=index.js.map
|