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