@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/dist/index.d.cts CHANGED
@@ -1,1455 +1,242 @@
1
- import { n as parseText, t as ParseTextOptions } from "./parse-text.cjs";
2
1
  import { LoggerPort } from "@jterrazz/telemetry";
3
2
  import { LanguageModel, LanguageModelMiddleware } from "ai";
4
- import { JSONSchema7 } from "json-schema";
5
- import { Schema } from "zod";
6
- import { z } from "zod/v4";
7
-
8
- //#region src/logging/logging.middleware.d.ts
9
- interface LoggingMiddlewareOptions {
10
- logger: LoggerPort;
11
- include?: {
12
- params?: boolean;
13
- content?: boolean;
14
- usage?: boolean;
15
- };
3
+ //#region src/provider/gateway.provider.d.ts
4
+ interface GatewayConfig {
5
+ /** Base URL of the gateway's chat-completions endpoint */
6
+ baseURL: string;
7
+ /** API key for authentication, if required by the endpoint */
8
+ apiKey?: string;
9
+ }
10
+ interface GatewayProvider {
11
+ /** Get a language model instance for the given model id */
12
+ model: (id: string) => LanguageModel;
16
13
  }
17
14
  /**
18
- * Creates middleware that logs AI SDK requests and responses.
19
- */
20
- declare function createLoggingMiddleware(options: LoggingMiddlewareOptions): LanguageModelMiddleware;
21
- //#endregion
22
- //#region node_modules/@ai-sdk/provider/dist/index.d.ts
23
- type SharedV3Headers = Record<string, string>;
24
- /**
25
- * A JSON value can be a string, number, boolean, object, array, or null.
26
- * JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods.
27
- */
28
- type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
29
- type JSONObject = {
30
- [key: string]: JSONValue | undefined;
31
- };
32
- type JSONArray = JSONValue[];
33
- /**
34
- * Additional provider-specific metadata.
35
- * Metadata are additional outputs from the provider.
36
- * They are passed through to the provider from the AI SDK
37
- * and enable provider-specific functionality
38
- * that can be fully encapsulated in the provider.
39
- *
40
- * This enables us to quickly ship provider-specific functionality
41
- * without affecting the core AI SDK.
15
+ * Creates a provider for gateways exposing a chat-completions API — any API
16
+ * implementing the OpenAI chat completions spec.
42
17
  *
43
- * The outer record is keyed by the provider name, and the inner
44
- * record is keyed by the provider-specific metadata key.
18
+ * Every model returned is automatically wrapped with two safety nets for
19
+ * gateways that don't support native structured output:
20
+ * - `createSchemaInstructionMiddleware` injects the JSON schema into the
21
+ * system prompt (some gateways silently drop `response_format`);
22
+ * - `extractJsonMiddleware` strips markdown code fences from JSON responses.
45
23
  *
24
+ * @example
46
25
  * ```ts
47
- * {
48
- * "anthropic": {
49
- * "cacheControl": { "type": "ephemeral" }
50
- * }
51
- * }
52
- * ```
53
- */
54
- type SharedV3ProviderMetadata = Record<string, JSONObject>;
55
- /**
56
- * Additional provider-specific options.
57
- * Options are additional input to the provider.
58
- * They are passed through to the provider from the AI SDK
59
- * and enable provider-specific functionality
60
- * that can be fully encapsulated in the provider.
61
- *
62
- * This enables us to quickly ship provider-specific functionality
63
- * without affecting the core AI SDK.
64
- *
65
- * The outer record is keyed by the provider name, and the inner
66
- * record is keyed by the provider-specific metadata key.
26
+ * const provider = createGatewayProvider({ baseURL: 'https://gateway.example.com/v1' });
27
+ * const model = provider.model('gpt-4o-mini');
67
28
  *
68
- * ```ts
69
- * {
70
- * "anthropic": {
71
- * "cacheControl": { "type": "ephemeral" }
72
- * }
73
- * }
29
+ * const { text } = await generateText({ model, prompt: 'Hello!' });
74
30
  * ```
75
31
  */
76
- type SharedV3ProviderOptions = Record<string, JSONObject>;
77
- /**
78
- * Warning from the model.
79
- *
80
- * For example, that certain features are unsupported or compatibility
81
- * functionality is used (which might lead to suboptimal results).
82
- */
83
- type SharedV3Warning = {
84
- /**
85
- * A feature is not supported by the model.
86
- */
87
- type: 'unsupported';
88
- /**
89
- * The feature that is not supported.
90
- */
91
- feature: string;
92
- /**
93
- * Additional details about the warning.
94
- */
95
- details?: string;
96
- } | {
97
- /**
98
- * A compatibility feature is used that might lead to suboptimal results.
99
- */
100
- type: 'compatibility';
101
- /**
102
- * The feature that is used in a compatibility mode.
103
- */
104
- feature: string;
105
- /**
106
- * Additional details about the warning.
107
- */
108
- details?: string;
109
- } | {
110
- /**
111
- * Other warning.
112
- */
113
- type: 'other';
114
- /**
115
- * The message of the warning.
116
- */
117
- message: string;
118
- };
119
- /**
120
- * A tool has a name, a description, and a set of parameters.
121
- *
122
- * Note: this is **not** the user-facing tool definition. The AI SDK methods will
123
- * map the user-facing tool definitions to this format.
124
- */
125
- type LanguageModelV3FunctionTool = {
126
- /**
127
- * The type of the tool (always 'function').
128
- */
129
- type: 'function';
130
- /**
131
- * The name of the tool. Unique within this model call.
132
- */
133
- name: string;
134
- /**
135
- * A description of the tool. The language model uses this to understand the
136
- * tool's purpose and to provide better completion suggestions.
137
- */
138
- description?: string;
139
- /**
140
- * The parameters that the tool expects. The language model uses this to
141
- * understand the tool's input requirements and to provide matching suggestions.
142
- */
143
- inputSchema: JSONSchema7;
144
- /**
145
- * An optional list of input examples that show the language
146
- * model what the input should look like.
147
- */
148
- inputExamples?: Array<{
149
- input: JSONObject;
150
- }>;
151
- /**
152
- * Strict mode setting for the tool.
153
- *
154
- * Providers that support strict mode will use this setting to determine
155
- * how the input should be generated. Strict mode will always produce
156
- * valid inputs, but it might limit what input schemas are supported.
157
- */
158
- strict?: boolean;
159
- /**
160
- * The provider-specific options for the tool.
161
- */
162
- providerOptions?: SharedV3ProviderOptions;
163
- };
164
- /**
165
- * Data content. Can be a Uint8Array, base64 encoded data as a string or a URL.
166
- */
167
- type LanguageModelV3DataContent = Uint8Array | string | URL;
168
- /**
169
- * A prompt is a list of messages.
170
- *
171
- * Note: Not all models and prompt formats support multi-modal inputs and
172
- * tool calls. The validation happens at runtime.
173
- *
174
- * Note: This is not a user-facing prompt. The AI SDK methods will map the
175
- * user-facing prompt types such as chat or instruction prompts to this format.
176
- */
177
- type LanguageModelV3Prompt = Array<LanguageModelV3Message>;
178
- type LanguageModelV3Message = ({
179
- role: 'system';
180
- content: string;
181
- } | {
182
- role: 'user';
183
- content: Array<LanguageModelV3TextPart | LanguageModelV3FilePart>;
184
- } | {
185
- role: 'assistant';
186
- content: Array<LanguageModelV3TextPart | LanguageModelV3FilePart | LanguageModelV3ReasoningPart | LanguageModelV3ToolCallPart | LanguageModelV3ToolResultPart>;
187
- } | {
188
- role: 'tool';
189
- content: Array<LanguageModelV3ToolResultPart | LanguageModelV3ToolApprovalResponsePart>;
190
- }) & {
191
- /**
192
- * Additional provider-specific options. They are passed through
193
- * to the provider from the AI SDK and enable provider-specific
194
- * functionality that can be fully encapsulated in the provider.
195
- */
196
- providerOptions?: SharedV3ProviderOptions;
197
- };
198
- /**
199
- * Text content part of a prompt. It contains a string of text.
200
- */
201
- interface LanguageModelV3TextPart {
202
- type: 'text';
203
- /**
204
- * The text content.
205
- */
206
- text: string;
207
- /**
208
- * Additional provider-specific options. They are passed through
209
- * to the provider from the AI SDK and enable provider-specific
210
- * functionality that can be fully encapsulated in the provider.
211
- */
212
- providerOptions?: SharedV3ProviderOptions;
213
- }
214
- /**
215
- * Reasoning content part of a prompt. It contains a string of reasoning text.
216
- */
217
- interface LanguageModelV3ReasoningPart {
218
- type: 'reasoning';
219
- /**
220
- * The reasoning text.
221
- */
222
- text: string;
223
- /**
224
- * Additional provider-specific options. They are passed through
225
- * to the provider from the AI SDK and enable provider-specific
226
- * functionality that can be fully encapsulated in the provider.
227
- */
228
- providerOptions?: SharedV3ProviderOptions;
229
- }
230
- /**
231
- * File content part of a prompt. It contains a file.
232
- */
233
- interface LanguageModelV3FilePart {
234
- type: 'file';
235
- /**
236
- * Optional filename of the file.
237
- */
238
- filename?: string;
239
- /**
240
- * File data. Can be a Uint8Array, base64 encoded data as a string or a URL.
241
- */
242
- data: LanguageModelV3DataContent;
243
- /**
244
- * IANA media type of the file.
245
- *
246
- * Can support wildcards, e.g. `image/*` (in which case the provider needs to take appropriate action).
247
- *
248
- * @see https://www.iana.org/assignments/media-types/media-types.xhtml
249
- */
250
- mediaType: string;
251
- /**
252
- * Additional provider-specific options. They are passed through
253
- * to the provider from the AI SDK and enable provider-specific
254
- * functionality that can be fully encapsulated in the provider.
255
- */
256
- providerOptions?: SharedV3ProviderOptions;
257
- }
258
- /**
259
- * Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
260
- */
261
- interface LanguageModelV3ToolCallPart {
262
- type: 'tool-call';
263
- /**
264
- * ID of the tool call. This ID is used to match the tool call with the tool result.
265
- */
266
- toolCallId: string;
267
- /**
268
- * Name of the tool that is being called.
269
- */
270
- toolName: string;
271
- /**
272
- * Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
273
- */
274
- input: unknown;
275
- /**
276
- * Whether the tool call will be executed by the provider.
277
- * If this flag is not set or is false, the tool call will be executed by the client.
278
- */
279
- providerExecuted?: boolean;
280
- /**
281
- * Additional provider-specific options. They are passed through
282
- * to the provider from the AI SDK and enable provider-specific
283
- * functionality that can be fully encapsulated in the provider.
284
- */
285
- providerOptions?: SharedV3ProviderOptions;
32
+ declare function createGatewayProvider(config: GatewayConfig): GatewayProvider;
33
+ //#endregion
34
+ //#region src/provider/openrouter.provider.d.ts
35
+ interface OpenRouterMetadata {
36
+ /** Application name, sent as the `X-OpenRouter-Title` header for dashboard attribution */
37
+ application?: string;
38
+ /** Application URL, sent as the `HTTP-Referer` header for dashboard attribution */
39
+ website?: string;
286
40
  }
287
- /**
288
- * Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
289
- */
290
- interface LanguageModelV3ToolResultPart {
291
- type: 'tool-result';
292
- /**
293
- * ID of the tool call that this result is associated with.
294
- */
295
- toolCallId: string;
296
- /**
297
- * Name of the tool that generated this result.
298
- */
299
- toolName: string;
300
- /**
301
- * Result of the tool call.
302
- */
303
- output: LanguageModelV3ToolResultOutput;
304
- /**
305
- * Additional provider-specific options. They are passed through
306
- * to the provider from the AI SDK and enable provider-specific
307
- * functionality that can be fully encapsulated in the provider.
308
- */
309
- providerOptions?: SharedV3ProviderOptions;
41
+ interface OpenRouterConfig {
42
+ apiKey: string;
43
+ metadata?: OpenRouterMetadata;
310
44
  }
311
- /**
312
- * Tool approval response content part of a prompt. It contains the user's
313
- * decision to approve or deny a provider-executed tool call.
314
- */
315
- interface LanguageModelV3ToolApprovalResponsePart {
316
- type: 'tool-approval-response';
317
- /**
318
- * ID of the approval request that this response refers to.
319
- */
320
- approvalId: string;
321
- /**
322
- * Whether the approval was granted (true) or denied (false).
323
- */
324
- approved: boolean;
325
- /**
326
- * Optional reason for approval or denial.
327
- */
328
- reason?: string;
329
- /**
330
- * Additional provider-specific options. They are passed through
331
- * to the provider from the AI SDK and enable provider-specific
332
- * functionality that can be fully encapsulated in the provider.
333
- */
334
- providerOptions?: SharedV3ProviderOptions;
45
+ interface OpenRouterProvider {
46
+ /** Get a language model instance for the given OpenRouter model id */
47
+ model: (id: string) => LanguageModel;
335
48
  }
336
49
  /**
337
- * Result of a tool call.
338
- */
339
- type LanguageModelV3ToolResultOutput = {
340
- /**
341
- * Text tool output that should be directly sent to the API.
342
- */
343
- type: 'text';
344
- value: string;
345
- /**
346
- * Provider-specific options.
347
- */
348
- providerOptions?: SharedV3ProviderOptions;
349
- } | {
350
- type: 'json';
351
- value: JSONValue;
352
- /**
353
- * Provider-specific options.
354
- */
355
- providerOptions?: SharedV3ProviderOptions;
356
- } | {
357
- /**
358
- * Type when the user has denied the execution of the tool call.
359
- */
360
- type: 'execution-denied';
361
- /**
362
- * Optional reason for the execution denial.
363
- */
364
- reason?: string;
365
- /**
366
- * Provider-specific options.
367
- */
368
- providerOptions?: SharedV3ProviderOptions;
369
- } | {
370
- type: 'error-text';
371
- value: string;
372
- /**
373
- * Provider-specific options.
374
- */
375
- providerOptions?: SharedV3ProviderOptions;
376
- } | {
377
- type: 'error-json';
378
- value: JSONValue;
379
- /**
380
- * Provider-specific options.
381
- */
382
- providerOptions?: SharedV3ProviderOptions;
383
- } | {
384
- type: 'content';
385
- value: Array<{
386
- type: 'text';
387
- /**
388
- * Text content.
389
- */
390
- text: string;
391
- /**
392
- * Provider-specific options.
393
- */
394
- providerOptions?: SharedV3ProviderOptions;
395
- } | {
396
- type: 'file-data';
397
- /**
398
- * Base-64 encoded media data.
399
- */
400
- data: string;
401
- /**
402
- * IANA media type.
403
- * @see https://www.iana.org/assignments/media-types/media-types.xhtml
404
- */
405
- mediaType: string;
406
- /**
407
- * Optional filename of the file.
408
- */
409
- filename?: string;
410
- /**
411
- * Provider-specific options.
412
- */
413
- providerOptions?: SharedV3ProviderOptions;
414
- } | {
415
- type: 'file-url';
416
- /**
417
- * URL of the file.
418
- */
419
- url: string;
420
- /**
421
- * Provider-specific options.
422
- */
423
- providerOptions?: SharedV3ProviderOptions;
424
- } | {
425
- type: 'file-id';
426
- /**
427
- * ID of the file.
428
- *
429
- * If you use multiple providers, you need to
430
- * specify the provider specific ids using
431
- * the Record option. The key is the provider
432
- * name, e.g. 'openai' or 'anthropic'.
433
- */
434
- fileId: string | Record<string, string>;
435
- /**
436
- * Provider-specific options.
437
- */
438
- providerOptions?: SharedV3ProviderOptions;
439
- } | {
440
- /**
441
- * Images that are referenced using base64 encoded data.
442
- */
443
- type: 'image-data';
444
- /**
445
- * Base-64 encoded image data.
446
- */
447
- data: string;
448
- /**
449
- * IANA media type.
450
- * @see https://www.iana.org/assignments/media-types/media-types.xhtml
451
- */
452
- mediaType: string;
453
- /**
454
- * Provider-specific options.
455
- */
456
- providerOptions?: SharedV3ProviderOptions;
457
- } | {
458
- /**
459
- * Images that are referenced using a URL.
460
- */
461
- type: 'image-url';
462
- /**
463
- * URL of the image.
464
- */
465
- url: string;
466
- /**
467
- * Provider-specific options.
468
- */
469
- providerOptions?: SharedV3ProviderOptions;
470
- } | {
471
- /**
472
- * Images that are referenced using a provider file id.
473
- */
474
- type: 'image-file-id';
475
- /**
476
- * Image that is referenced using a provider file id.
477
- *
478
- * If you use multiple providers, you need to
479
- * specify the provider specific ids using
480
- * the Record option. The key is the provider
481
- * name, e.g. 'openai' or 'anthropic'.
482
- */
483
- fileId: string | Record<string, string>;
484
- /**
485
- * Provider-specific options.
486
- */
487
- providerOptions?: SharedV3ProviderOptions;
488
- } | {
489
- /**
490
- * Custom content part. This can be used to implement
491
- * provider-specific content parts.
492
- */
493
- type: 'custom';
494
- /**
495
- * Provider-specific options.
496
- */
497
- providerOptions?: SharedV3ProviderOptions;
498
- }>;
499
- };
500
- /**
501
- * The configuration of a provider tool.
50
+ * Creates an OpenRouter provider for AI SDK models.
502
51
  *
503
- * Provider tools are tools that are specific to a certain provider.
504
- * The input and output schemas are defined be the provider, and
505
- * some of the tools are also executed on the provider systems.
506
- */
507
- type LanguageModelV3ProviderTool = {
508
- /**
509
- * The type of the tool (always 'provider').
510
- */
511
- type: 'provider';
512
- /**
513
- * The ID of the tool. Should follow the format `<provider-id>.<unique-tool-name>`.
514
- */
515
- id: `${string}.${string}`;
516
- /**
517
- * The name of the tool. Unique within this model call.
518
- */
519
- name: string;
520
- /**
521
- * The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
522
- */
523
- args: Record<string, unknown>;
524
- };
525
- type LanguageModelV3ToolChoice = {
526
- type: 'auto';
527
- } | {
528
- type: 'none';
529
- } | {
530
- type: 'required';
531
- } | {
532
- type: 'tool';
533
- toolName: string;
534
- };
535
- type LanguageModelV3CallOptions = {
536
- /**
537
- * A language mode prompt is a standardized prompt type.
538
- *
539
- * Note: This is **not** the user-facing prompt. The AI SDK methods will map the
540
- * user-facing prompt types such as chat or instruction prompts to this format.
541
- * That approach allows us to evolve the user facing prompts without breaking
542
- * the language model interface.
543
- */
544
- prompt: LanguageModelV3Prompt;
545
- /**
546
- * Maximum number of tokens to generate.
547
- */
548
- maxOutputTokens?: number;
549
- /**
550
- * Temperature setting. The range depends on the provider and model.
551
- */
552
- temperature?: number;
553
- /**
554
- * Stop sequences.
555
- * If set, the model will stop generating text when one of the stop sequences is generated.
556
- * Providers may have limits on the number of stop sequences.
557
- */
558
- stopSequences?: string[];
559
- /**
560
- * Nucleus sampling.
561
- */
562
- topP?: number;
563
- /**
564
- * Only sample from the top K options for each subsequent token.
565
- *
566
- * Used to remove "long tail" low probability responses.
567
- * Recommended for advanced use cases only. You usually only need to use temperature.
568
- */
569
- topK?: number;
570
- /**
571
- * Presence penalty setting. It affects the likelihood of the model to
572
- * repeat information that is already in the prompt.
573
- */
574
- presencePenalty?: number;
575
- /**
576
- * Frequency penalty setting. It affects the likelihood of the model
577
- * to repeatedly use the same words or phrases.
578
- */
579
- frequencyPenalty?: number;
580
- /**
581
- * Response format. The output can either be text or JSON. Default is text.
582
- *
583
- * If JSON is selected, a schema can optionally be provided to guide the LLM.
584
- */
585
- responseFormat?: {
586
- type: 'text';
587
- } | {
588
- type: 'json';
589
- /**
590
- * JSON schema that the generated output should conform to.
591
- */
592
- schema?: JSONSchema7;
593
- /**
594
- * Name of output that should be generated. Used by some providers for additional LLM guidance.
595
- */
596
- name?: string;
597
- /**
598
- * Description of the output that should be generated. Used by some providers for additional LLM guidance.
599
- */
600
- description?: string;
601
- };
602
- /**
603
- * The seed (integer) to use for random sampling. If set and supported
604
- * by the model, calls will generate deterministic results.
605
- */
606
- seed?: number;
607
- /**
608
- * The tools that are available for the model.
609
- */
610
- tools?: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderTool>;
611
- /**
612
- * Specifies how the tool should be selected. Defaults to 'auto'.
613
- */
614
- toolChoice?: LanguageModelV3ToolChoice;
615
- /**
616
- * Include raw chunks in the stream. Only applicable for streaming calls.
617
- */
618
- includeRawChunks?: boolean;
619
- /**
620
- * Abort signal for cancelling the operation.
621
- */
622
- abortSignal?: AbortSignal;
623
- /**
624
- * Additional HTTP headers to be sent with the request.
625
- * Only applicable for HTTP-based providers.
626
- */
627
- headers?: Record<string, string | undefined>;
628
- /**
629
- * Additional provider-specific options. They are passed through
630
- * to the provider from the AI SDK and enable provider-specific
631
- * functionality that can be fully encapsulated in the provider.
632
- */
633
- providerOptions?: SharedV3ProviderOptions;
634
- };
635
- /**
636
- * A file that has been generated by the model.
637
- * Generated files as base64 encoded strings or binary data.
638
- * The files should be returned without any unnecessary conversion.
639
- */
640
- type LanguageModelV3File = {
641
- type: 'file';
642
- /**
643
- * The IANA media type of the file, e.g. `image/png` or `audio/mp3`.
644
- *
645
- * @see https://www.iana.org/assignments/media-types/media-types.xhtml
646
- */
647
- mediaType: string;
648
- /**
649
- * Generated file data as base64 encoded strings or binary data.
650
- *
651
- * The file data should be returned without any unnecessary conversion.
652
- * If the API returns base64 encoded strings, the file data should be returned
653
- * as base64 encoded strings. If the API returns binary data, the file data should
654
- * be returned as binary data.
655
- */
656
- data: string | Uint8Array;
657
- /**
658
- * Optional provider-specific metadata for the file part.
659
- */
660
- providerMetadata?: SharedV3ProviderMetadata;
661
- };
662
- /**
663
- * Reasoning that the model has generated.
664
- */
665
- type LanguageModelV3Reasoning = {
666
- type: 'reasoning';
667
- text: string;
668
- /**
669
- * Optional provider-specific metadata for the reasoning part.
670
- */
671
- providerMetadata?: SharedV3ProviderMetadata;
672
- };
673
- /**
674
- * A source that has been used as input to generate the response.
675
- */
676
- type LanguageModelV3Source = {
677
- type: 'source';
678
- /**
679
- * The type of source - URL sources reference web content.
680
- */
681
- sourceType: 'url';
682
- /**
683
- * The ID of the source.
684
- */
685
- id: string;
686
- /**
687
- * The URL of the source.
688
- */
689
- url: string;
690
- /**
691
- * The title of the source.
692
- */
693
- title?: string;
694
- /**
695
- * Additional provider metadata for the source.
696
- */
697
- providerMetadata?: SharedV3ProviderMetadata;
698
- } | {
699
- type: 'source';
700
- /**
701
- * The type of source - document sources reference files/documents.
702
- */
703
- sourceType: 'document';
704
- /**
705
- * The ID of the source.
706
- */
707
- id: string;
708
- /**
709
- * IANA media type of the document (e.g., 'application/pdf').
710
- */
711
- mediaType: string;
712
- /**
713
- * The title of the document.
714
- */
715
- title: string;
716
- /**
717
- * Optional filename of the document.
718
- */
719
- filename?: string;
720
- /**
721
- * Additional provider metadata for the source.
722
- */
723
- providerMetadata?: SharedV3ProviderMetadata;
724
- };
725
- /**
726
- * Text that the model has generated.
727
- */
728
- type LanguageModelV3Text = {
729
- type: 'text';
730
- /**
731
- * The text content.
732
- */
733
- text: string;
734
- providerMetadata?: SharedV3ProviderMetadata;
735
- };
736
- /**
737
- * Tool approval request emitted by a provider for a provider-executed tool call.
52
+ * Per-call options (reasoning effort, max tokens, etc.) are no longer
53
+ * configured here pass them at the call site via `providerOptions.openrouter`
54
+ * on `generateText`/`streamText`.
738
55
  *
739
- * This is used for flows where the provider executes the tool (e.g. MCP tools)
740
- * but requires an explicit user approval before continuing.
741
- */
742
- type LanguageModelV3ToolApprovalRequest = {
743
- type: 'tool-approval-request';
744
- /**
745
- * ID of the approval request. This ID is referenced by the subsequent
746
- * tool-approval-response (tool message) to approve or deny execution.
747
- */
748
- approvalId: string;
749
- /**
750
- * The tool call ID that this approval request is for.
751
- */
752
- toolCallId: string;
753
- /**
754
- * Additional provider-specific metadata for the approval request.
755
- */
756
- providerMetadata?: SharedV3ProviderMetadata;
757
- };
758
- /**
759
- * Tool calls that the model has generated.
760
- */
761
- type LanguageModelV3ToolCall = {
762
- type: 'tool-call';
763
- /**
764
- * The identifier of the tool call. It must be unique across all tool calls.
765
- */
766
- toolCallId: string;
767
- /**
768
- * The name of the tool that should be called.
769
- */
770
- toolName: string;
771
- /**
772
- * Stringified JSON object with the tool call arguments. Must match the
773
- * parameters schema of the tool.
774
- */
775
- input: string;
776
- /**
777
- * Whether the tool call will be executed by the provider.
778
- * If this flag is not set or is false, the tool call will be executed by the client.
779
- */
780
- providerExecuted?: boolean;
781
- /**
782
- * Whether the tool is dynamic, i.e. defined at runtime.
783
- * For example, MCP (Model Context Protocol) tools that are executed by the provider.
784
- */
785
- dynamic?: boolean;
786
- /**
787
- * Additional provider-specific metadata for the tool call.
788
- */
789
- providerMetadata?: SharedV3ProviderMetadata;
790
- };
791
- /**
792
- * Result of a tool call that has been executed by the provider.
793
- */
794
- type LanguageModelV3ToolResult = {
795
- type: 'tool-result';
796
- /**
797
- * The ID of the tool call that this result is associated with.
798
- */
799
- toolCallId: string;
800
- /**
801
- * Name of the tool that generated this result.
802
- */
803
- toolName: string;
804
- /**
805
- * Result of the tool call. This is a JSON-serializable object.
806
- */
807
- result: NonNullable<JSONValue>;
808
- /**
809
- * Optional flag if the result is an error or an error message.
810
- */
811
- isError?: boolean;
812
- /**
813
- * Whether the tool result is preliminary.
814
- *
815
- * Preliminary tool results replace each other, e.g. image previews.
816
- * There always has to be a final, non-preliminary tool result.
817
- *
818
- * If this flag is set to true, the tool result is preliminary.
819
- * If this flag is not set or is false, the tool result is not preliminary.
820
- */
821
- preliminary?: boolean;
822
- /**
823
- * Whether the tool is dynamic, i.e. defined at runtime.
824
- * For example, MCP (Model Context Protocol) tools that are executed by the provider.
825
- */
826
- dynamic?: boolean;
827
- /**
828
- * Additional provider-specific metadata for the tool result.
829
- */
830
- providerMetadata?: SharedV3ProviderMetadata;
831
- };
832
- type LanguageModelV3Content = LanguageModelV3Text | LanguageModelV3Reasoning | LanguageModelV3File | LanguageModelV3ToolApprovalRequest | LanguageModelV3Source | LanguageModelV3ToolCall | LanguageModelV3ToolResult;
833
- /**
834
- * Reason why a language model finished generating a response.
56
+ * @example
57
+ * ```ts
58
+ * const provider = createOpenRouterProvider({ apiKey: process.env.OPENROUTER_API_KEY });
59
+ * const model = provider.model('anthropic/claude-sonnet-4-20250514');
835
60
  *
836
- * Contains both a unified finish reason and a raw finish reason from the provider.
837
- * The unified finish reason is used to provide a consistent finish reason across different providers.
838
- * The raw finish reason is used to provide the original finish reason from the provider.
839
- */
840
- type LanguageModelV3FinishReason = {
841
- /**
842
- * Unified finish reason. This enables using the same finish reason across different providers.
843
- *
844
- * Can be one of the following:
845
- * - `stop`: model generated stop sequence
846
- * - `length`: model generated maximum number of tokens
847
- * - `content-filter`: content filter violation stopped the model
848
- * - `tool-calls`: model triggered tool calls
849
- * - `error`: model stopped because of an error
850
- * - `other`: model stopped for other reasons
851
- */
852
- unified: 'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'other';
853
- /**
854
- * Raw finish reason from the provider.
855
- * This is the original finish reason from the provider.
856
- */
857
- raw: string | undefined;
858
- };
859
- interface LanguageModelV3ResponseMetadata {
860
- /**
861
- * ID for the generated response, if the provider sends one.
862
- */
863
- id?: string;
864
- /**
865
- * Timestamp for the start of the generated response, if the provider sends one.
866
- */
867
- timestamp?: Date;
868
- /**
869
- * The ID of the response model that was used to generate the response, if the provider sends one.
870
- */
871
- modelId?: string;
872
- }
873
- /**
874
- * Usage information for a language model call.
875
- */
876
- type LanguageModelV3Usage = {
877
- /**
878
- * Information about the input tokens.
879
- */
880
- inputTokens: {
881
- /**
882
- * The total number of input (prompt) tokens used.
883
- */
884
- total: number | undefined;
885
- /**
886
- * The number of non-cached input (prompt) tokens used.
887
- */
888
- noCache: number | undefined;
889
- /**
890
- * The number of cached input (prompt) tokens read.
891
- */
892
- cacheRead: number | undefined;
893
- /**
894
- * The number of cached input (prompt) tokens written.
895
- */
896
- cacheWrite: number | undefined;
897
- };
898
- /**
899
- * Information about the output tokens.
900
- */
901
- outputTokens: {
902
- /**
903
- * The total number of output (completion) tokens used.
904
- */
905
- total: number | undefined;
906
- /**
907
- * The number of text tokens used.
908
- */
909
- text: number | undefined;
910
- /**
911
- * The number of reasoning tokens used.
912
- */
913
- reasoning: number | undefined;
914
- };
915
- /**
916
- * Raw usage information from the provider.
917
- *
918
- * This is the usage information in the shape that the provider returns.
919
- * It can include additional information that is not part of the standard usage information.
920
- */
921
- raw?: JSONObject;
922
- };
923
- /**
924
- * The result of a language model doGenerate call.
925
- */
926
- type LanguageModelV3GenerateResult = {
927
- /**
928
- * Ordered content that the model has generated.
929
- */
930
- content: Array<LanguageModelV3Content>;
931
- /**
932
- * The finish reason.
933
- */
934
- finishReason: LanguageModelV3FinishReason;
935
- /**
936
- * The usage information.
937
- */
938
- usage: LanguageModelV3Usage;
939
- /**
940
- * Additional provider-specific metadata. They are passed through
941
- * from the provider to the AI SDK and enable provider-specific
942
- * results that can be fully encapsulated in the provider.
943
- */
944
- providerMetadata?: SharedV3ProviderMetadata;
945
- /**
946
- * Optional request information for telemetry and debugging purposes.
947
- */
948
- request?: {
949
- /**
950
- * Request HTTP body that was sent to the provider API.
951
- */
952
- body?: unknown;
953
- };
954
- /**
955
- * Optional response information for telemetry and debugging purposes.
956
- */
957
- response?: LanguageModelV3ResponseMetadata & {
958
- /**
959
- * Response headers.
960
- */
961
- headers?: SharedV3Headers;
962
- /**
963
- * Response HTTP body.
964
- */
965
- body?: unknown;
966
- };
967
- /**
968
- * Warnings for the call, e.g. unsupported settings.
969
- */
970
- warnings: Array<SharedV3Warning>;
971
- };
972
- type LanguageModelV3StreamPart = {
973
- type: 'text-start';
974
- providerMetadata?: SharedV3ProviderMetadata;
975
- id: string;
976
- } | {
977
- type: 'text-delta';
978
- id: string;
979
- providerMetadata?: SharedV3ProviderMetadata;
980
- delta: string;
981
- } | {
982
- type: 'text-end';
983
- providerMetadata?: SharedV3ProviderMetadata;
984
- id: string;
985
- } | {
986
- type: 'reasoning-start';
987
- providerMetadata?: SharedV3ProviderMetadata;
988
- id: string;
989
- } | {
990
- type: 'reasoning-delta';
991
- id: string;
992
- providerMetadata?: SharedV3ProviderMetadata;
993
- delta: string;
994
- } | {
995
- type: 'reasoning-end';
996
- id: string;
997
- providerMetadata?: SharedV3ProviderMetadata;
998
- } | {
999
- type: 'tool-input-start';
1000
- id: string;
1001
- toolName: string;
1002
- providerMetadata?: SharedV3ProviderMetadata;
1003
- providerExecuted?: boolean;
1004
- dynamic?: boolean;
1005
- title?: string;
1006
- } | {
1007
- type: 'tool-input-delta';
1008
- id: string;
1009
- delta: string;
1010
- providerMetadata?: SharedV3ProviderMetadata;
1011
- } | {
1012
- type: 'tool-input-end';
1013
- id: string;
1014
- providerMetadata?: SharedV3ProviderMetadata;
1015
- } | LanguageModelV3ToolApprovalRequest | LanguageModelV3ToolCall | LanguageModelV3ToolResult | LanguageModelV3File | LanguageModelV3Source | {
1016
- type: 'stream-start';
1017
- warnings: Array<SharedV3Warning>;
1018
- } | ({
1019
- type: 'response-metadata';
1020
- } & LanguageModelV3ResponseMetadata) | {
1021
- type: 'finish';
1022
- usage: LanguageModelV3Usage;
1023
- finishReason: LanguageModelV3FinishReason;
1024
- providerMetadata?: SharedV3ProviderMetadata;
1025
- } | {
1026
- type: 'raw';
1027
- rawValue: unknown;
1028
- } | {
1029
- type: 'error';
1030
- error: unknown;
1031
- };
1032
- /**
1033
- * The result of a language model doStream call.
1034
- */
1035
- type LanguageModelV3StreamResult = {
1036
- /**
1037
- * The stream.
1038
- */
1039
- stream: ReadableStream<LanguageModelV3StreamPart>;
1040
- /**
1041
- * Optional request information for telemetry and debugging purposes.
1042
- */
1043
- request?: {
1044
- /**
1045
- * Request HTTP body that was sent to the provider API.
1046
- */
1047
- body?: unknown;
1048
- };
1049
- /**
1050
- * Optional response data.
1051
- */
1052
- response?: {
1053
- /**
1054
- * Response headers.
1055
- */
1056
- headers?: SharedV3Headers;
1057
- };
1058
- };
1059
- /**
1060
- * Specification for a language model that implements the language model interface version 3.
1061
- */
1062
- type LanguageModelV3 = {
1063
- /**
1064
- * The language model must specify which language model interface version it implements.
1065
- */
1066
- readonly specificationVersion: 'v3';
1067
- /**
1068
- * Provider ID.
1069
- */
1070
- readonly provider: string;
1071
- /**
1072
- * Provider-specific model ID.
1073
- */
1074
- readonly modelId: string;
1075
- /**
1076
- * Supported URL patterns by media type for the provider.
1077
- *
1078
- * The keys are media type patterns or full media types (e.g. `*\/*` for everything, `audio/*`, `video/*`, or `application/pdf`).
1079
- * and the values are arrays of regular expressions that match the URL paths.
1080
- *
1081
- * The matching should be against lower-case URLs.
1082
- *
1083
- * Matched URLs are supported natively by the model and are not downloaded.
1084
- *
1085
- * @returns A map of supported URL patterns by media type (as a promise or a plain object).
1086
- */
1087
- supportedUrls: PromiseLike<Record<string, RegExp[]>> | Record<string, RegExp[]>;
1088
- /**
1089
- * Generates a language model output (non-streaming).
1090
- * Naming: "do" prefix to prevent accidental direct usage of the method
1091
- * by the user.
1092
- */
1093
- doGenerate(options: LanguageModelV3CallOptions): PromiseLike<LanguageModelV3GenerateResult>;
1094
- /**
1095
- * Generates a language model output (streaming).
1096
- *
1097
- * Naming: "do" prefix to prevent accidental direct usage of the method
1098
- * by the user.
1099
- *
1100
- * @return A stream of higher-level language model output parts.
1101
- */
1102
- doStream(options: LanguageModelV3CallOptions): PromiseLike<LanguageModelV3StreamResult>;
1103
- };
1104
- /**
1105
- * Experimental middleware for LanguageModelV3.
1106
- * This type defines the structure for middleware that can be used to modify
1107
- * the behavior of LanguageModelV3 operations.
61
+ * const { text } = await generateText({
62
+ * model,
63
+ * prompt: 'Hello!',
64
+ * providerOptions: { openrouter: { reasoning: { effort: 'high' } } },
65
+ * });
66
+ * ```
1108
67
  */
68
+ declare function createOpenRouterProvider(config: OpenRouterConfig): OpenRouterProvider;
1109
69
  //#endregion
1110
- //#region src/ports/observability.port.d.ts
1111
- /**
1112
- * Usage details for LLM generations
1113
- */
1114
- interface UsageDetails {
1115
- input: number;
1116
- output: number;
1117
- total?: number;
1118
- reasoning?: number;
1119
- cacheRead?: number;
1120
- cacheWrite?: number;
1121
- }
1122
- /**
1123
- * Cost details for LLM generations
1124
- */
1125
- interface CostDetails {
1126
- total: number;
1127
- input?: number;
1128
- output?: number;
1129
- }
1130
- /**
1131
- * Parameters for creating a trace
1132
- */
1133
- interface TraceParams {
1134
- id: string;
1135
- name: string;
1136
- metadata?: Record<string, unknown>;
1137
- }
1138
- /**
1139
- * Parameters for recording a generation
1140
- */
1141
- interface GenerationParams {
1142
- traceId: string;
1143
- name: string;
70
+ //#region src/factory/create-intelligence.d.ts
71
+ type ProviderConfig = (GatewayConfig & {
72
+ type: 'gateway';
73
+ }) | (OpenRouterConfig & {
74
+ type: 'openrouter';
75
+ });
76
+ interface ModelRef {
77
+ /** Key into `providers` */
78
+ provider: string;
79
+ /** Technical model id passed through to the provider as-is */
1144
80
  model: string;
1145
- input: unknown;
1146
- output: string;
1147
- startTime: Date;
1148
- endTime: Date;
1149
- usage?: UsageDetails;
1150
- cost?: CostDetails;
1151
- metadata?: Record<string, unknown>;
1152
81
  }
1153
- /**
1154
- * Port for observability integrations (Langfuse, Datadog, etc.)
1155
- */
1156
- interface ObservabilityPort {
1157
- trace(params: TraceParams): void;
1158
- generation(params: GenerationParams): void;
1159
- flush(): Promise<void>;
1160
- shutdown(): Promise<void>;
82
+ interface AgentConfig extends ModelRef {
83
+ /** Model used when the primary `provider`/`model` fails with a retryable error */
84
+ fallback?: ModelRef;
1161
85
  }
1162
- //#endregion
1163
- //#region src/ports/provider-metadata.port.d.ts
1164
- /**
1165
- * Extracted metadata from a provider response
1166
- */
1167
- interface ExtractedProviderMetadata {
1168
- usage?: UsageDetails;
1169
- cost?: CostDetails;
1170
- }
1171
- /**
1172
- * Port for extracting usage and cost data from provider-specific metadata.
1173
- * Implement this interface for each AI provider (OpenRouter, Anthropic, etc.)
1174
- */
1175
- interface ProviderMetadataPort {
86
+ interface IntelligenceConfig {
87
+ providers: Record<string, ProviderConfig>;
88
+ agents: Record<string, AgentConfig>;
1176
89
  /**
1177
- * Extract usage and cost data from provider metadata
1178
- * @param metadata - The raw provider metadata from AI SDK response
1179
- * @returns Extracted usage and cost details, or undefined values if not available
90
+ * USD-per-million-token pricing, keyed by `"<provider>/<model>"` (the
91
+ * agent's `provider` and `model` joined with `/`, not a provider-side
92
+ * identifier).
1180
93
  */
1181
- extract(metadata: Record<string, unknown> | undefined): ExtractedProviderMetadata;
1182
- }
1183
- //#endregion
1184
- //#region src/observability/observability.middleware.d.ts
1185
- /**
1186
- * Metadata passed per-call via providerOptions
1187
- */
1188
- interface ObservabilityMetadata {
1189
- traceId: string;
1190
- name?: string;
1191
- metadata?: Record<string, unknown>;
1192
- }
1193
- interface ObservabilityMiddlewareOptions {
1194
- observability: ObservabilityPort;
1195
- providerMetadata?: ProviderMetadataPort;
1196
- }
1197
- /**
1198
- * Helper to create type-safe observability metadata for providerOptions
1199
- */
1200
- declare function withObservability(meta: ObservabilityMetadata): SharedV3ProviderOptions;
1201
- /**
1202
- * Creates middleware that sends generation data to an observability platform.
1203
- */
1204
- declare function createObservabilityMiddleware(options: ObservabilityMiddlewareOptions): LanguageModelMiddleware;
1205
- //#endregion
1206
- //#region src/observability/langfuse.adapter.d.ts
1207
- interface LangfuseConfig {
1208
- secretKey: string;
1209
- publicKey: string;
1210
- baseUrl?: string;
1211
- environment?: string;
1212
- release?: string;
1213
- }
1214
- /**
1215
- * Langfuse adapter implementing ObservabilityPort
1216
- */
1217
- declare class LangfuseAdapter implements ObservabilityPort {
1218
- private readonly client;
1219
- constructor(config: LangfuseConfig);
1220
- flush(): Promise<void>;
1221
- generation(params: GenerationParams): void;
1222
- shutdown(): Promise<void>;
1223
- trace(params: TraceParams): void;
1224
- }
1225
- //#endregion
1226
- //#region src/observability/noop.adapter.d.ts
1227
- /**
1228
- * No-op adapter that silently discards all observability data.
1229
- * Useful for testing, development, or when observability is disabled.
1230
- */
1231
- declare class NoopObservabilityAdapter implements ObservabilityPort {
1232
- flush(): Promise<void>;
1233
- generation(_params: GenerationParams): void;
1234
- shutdown(): Promise<void>;
1235
- trace(_params: TraceParams): void;
1236
- }
1237
- //#endregion
1238
- //#region src/result/result.d.ts
1239
- /**
1240
- * Error codes for AI generation failures
1241
- */
1242
- type GenerationErrorCode = 'AI_GENERATION_FAILED' | 'EMPTY_RESULT' | 'PARSING_FAILED' | 'RATE_LIMITED' | 'TIMEOUT' | 'VALIDATION_FAILED';
1243
- /**
1244
- * Structured error information from AI generation
1245
- */
1246
- interface GenerationError {
1247
- code: GenerationErrorCode;
1248
- message: string;
1249
- cause?: unknown;
94
+ pricing?: Record<string, {
95
+ input: number;
96
+ output: number;
97
+ }>;
98
+ logger?: LoggerPort;
1250
99
  }
1251
- /**
1252
- * Discriminated union result type for AI operations.
1253
- * Forces explicit handling of both success and failure cases.
1254
- */
1255
- type GenerationResult<T> = {
1256
- success: false;
1257
- error: GenerationError;
1258
- } | {
1259
- success: true;
1260
- data: T;
1261
- };
1262
- /**
1263
- * Create a successful result
1264
- */
1265
- declare function generationSuccess<T>(data: T): GenerationResult<T>;
1266
- /**
1267
- * Create a failed result
1268
- */
1269
- declare function generationFailure<T>(code: GenerationErrorCode, message: string, cause?: unknown): GenerationResult<T>;
1270
- /**
1271
- * Classify an error into a GenerationErrorCode
1272
- */
1273
- declare function classifyError(error: unknown): GenerationErrorCode;
1274
- /**
1275
- * Check if a result is successful (type guard)
1276
- */
1277
- declare function isSuccess<T>(result: GenerationResult<T>): result is {
1278
- success: true;
1279
- data: T;
1280
- };
1281
- /**
1282
- * Check if a result is a failure (type guard)
1283
- */
1284
- declare function isFailure<T>(result: GenerationResult<T>): result is {
1285
- success: false;
1286
- error: GenerationError;
1287
- };
1288
- /**
1289
- * Unwrap a result, throwing if it fails
1290
- */
1291
- declare function unwrap<T>(result: GenerationResult<T>): T;
1292
- /**
1293
- * Unwrap a result with a default value for failures
1294
- */
1295
- declare function unwrapOr<T>(result: GenerationResult<T>, defaultValue: T): T;
1296
- //#endregion
1297
- //#region src/generation/generate-structured.d.ts
1298
- interface GenerateStructuredOptions<T> {
1299
- model: LanguageModelV3;
1300
- prompt: string;
1301
- system?: string;
1302
- schema: Schema<T>;
1303
- providerOptions?: SharedV3ProviderOptions;
1304
- abortSignal?: AbortSignal;
1305
- maxOutputTokens?: number;
1306
- temperature?: number;
100
+ interface Intelligence {
101
+ /** Get the composed language model for the given agent name */
102
+ model: (agentName: string) => LanguageModel;
1307
103
  }
1308
104
  /**
1309
- * Generate structured data from an AI model with automatic parsing and error handling.
1310
- * Observability is handled by middleware - no metadata exposed to caller.
1311
- */
1312
- declare function generateStructured<T>(options: GenerateStructuredOptions<T>): Promise<GenerationResult<T>>;
1313
- //#endregion
1314
- //#region src/parsing/create-schema-prompt.d.ts
1315
- /**
1316
- * Creates a system prompt that instructs the model to output structured data
1317
- * matching the provided Zod schema.
105
+ * Creates a composition root over AI SDK v7: resolves each agent's
106
+ * `provider`/`model` pair into a fully instrumented `LanguageModel` cost
107
+ * tracking, optional fallback, and optional logging — cached per agent name.
1318
108
  *
1319
- * Use this with `generateText` when the provider doesn't support native
1320
- * structured outputs, then parse the response with `parseObject`.
1321
- *
1322
- * @param schema - A Zod schema defining the expected output structure
1323
- * @returns A system prompt string with JSON schema instructions
109
+ * Registers the `@ai-sdk/otel` telemetry integration on first use (idempotent,
110
+ * best-effort). The host app is expected to have already registered an
111
+ * OpenTelemetry Node SDK (e.g. via `@jterrazz/telemetry`).
1324
112
  *
1325
113
  * @example
1326
114
  * ```ts
1327
- * import { generateText } from 'ai';
1328
- * import { createSchemaPrompt, parseObject } from '@jterrazz/intelligence';
1329
- *
1330
- * const schema = z.object({ title: z.string(), tags: z.array(z.string()) });
1331
- *
1332
- * const { text } = await generateText({
1333
- * model,
1334
- * prompt: 'Generate an article about TypeScript',
1335
- * system: createSchemaPrompt(schema),
115
+ * const intelligence = createIntelligence({
116
+ * providers: {
117
+ * openrouter: { type: 'openrouter', apiKey: process.env.OPENROUTER_API_KEY },
118
+ * },
119
+ * agents: {
120
+ * summarizer: {
121
+ * provider: 'openrouter',
122
+ * model: 'google/gemini-2.5-flash-lite',
123
+ * fallback: { provider: 'openrouter', model: 'openai/gpt-4o-mini' },
124
+ * },
125
+ * },
126
+ * pricing: {
127
+ * 'openrouter/google/gemini-2.5-flash-lite': { input: 0.1, output: 0.4 },
128
+ * },
129
+ * logger,
1336
130
  * });
1337
131
  *
1338
- * const result = parseObject(text, schema);
132
+ * const model = intelligence.model('summarizer');
133
+ * const { text } = await generateText({ model, prompt: 'Hello!' });
1339
134
  * ```
1340
135
  */
1341
- declare function createSchemaPrompt<T>(schema: z.ZodType<T>): string;
136
+ declare function createIntelligence(config: IntelligenceConfig): Intelligence;
1342
137
  //#endregion
1343
- //#region src/parsing/parse-object.d.ts
1344
- /**
1345
- * Error thrown when object parsing fails.
1346
- * Contains the original text for debugging purposes.
1347
- */
1348
- declare class ParseObjectError extends Error {
1349
- readonly name = "ParseObjectError";
1350
- readonly cause?: unknown;
1351
- readonly text?: string;
1352
- constructor(message: string, cause?: unknown, text?: string);
138
+ //#region src/middleware/cost.middleware.d.ts
139
+ interface CostPricing {
140
+ /** USD per million input tokens */
141
+ input: number;
142
+ /** USD per million output tokens */
143
+ output: number;
144
+ }
145
+ interface CostMiddlewareOptions {
146
+ /** Full model reference, e.g. `'openrouter/google/gemini-2.5-flash-lite'` */
147
+ modelRef: string;
148
+ /** Fallback USD-per-million-token pricing, used when the provider doesn't report actual cost */
149
+ pricing?: CostPricing;
1353
150
  }
1354
151
  /**
1355
- * Parses AI-generated text into structured data validated against a Zod schema.
152
+ * Creates middleware that enriches the active OpenTelemetry span with the
153
+ * model reference (`gen_ai.request.model`) and the USD cost
154
+ * (`gen_ai.usage.cost`) of a generation.
155
+ *
156
+ * Resolution order:
157
+ * 1. Actual cost reported by the provider (currently: OpenRouter's
158
+ * `providerMetadata.openrouter.usage.cost`).
159
+ * 2. Estimated cost from `pricing` (USD per million input/output tokens),
160
+ * computed from the reported token usage.
1356
161
  *
1357
- * Handles common AI response formats:
1358
- * - JSON wrapped in markdown code blocks
1359
- * - JSON embedded in prose text
1360
- * - Malformed JSON (auto-repaired)
1361
- * - Escaped unicode and special characters
162
+ * The `gen_ai.usage.cost` attribute is set on `trace.getActiveSpan()` because
163
+ * that's the attribute Langfuse's OTel ingestion prioritizes over its own
164
+ * cost inference (`langfuse.observation.cost_details` is buggy on ingestion).
1362
165
  *
1363
- * @param text - The raw AI response text
1364
- * @param schema - A Zod schema to validate and type the result
1365
- * @returns The parsed and validated data
1366
- * @throws {ParseObjectError} When parsing or validation fails
166
+ * Never throws: all enrichment is best-effort.
1367
167
  *
1368
168
  * @example
1369
169
  * ```ts
1370
- * const schema = z.object({ title: z.string(), tags: z.array(z.string()) });
1371
- * const result = parseObject(aiResponse, schema);
1372
- * // result is typed as { title: string; tags: string[] }
170
+ * const model = wrapLanguageModel({
171
+ * model: provider.model('google/gemini-2.5-flash-lite'),
172
+ * middleware: [
173
+ * createCostMiddleware({
174
+ * modelRef: 'openrouter/google/gemini-2.5-flash-lite',
175
+ * pricing: { input: 0.1, output: 0.4 },
176
+ * }),
177
+ * ],
178
+ * });
1373
179
  * ```
1374
180
  */
1375
- declare function parseObject<T>(text: string, schema: z.ZodSchema<T>): T;
181
+ declare function createCostMiddleware(options: CostMiddlewareOptions): LanguageModelMiddleware;
1376
182
  //#endregion
1377
- //#region src/provider/openrouter.provider.d.ts
1378
- interface ModelOptions {
1379
- /** Maximum tokens to generate */
1380
- maxTokens?: number;
1381
- /** Reasoning configuration for supported models */
1382
- reasoning?: {
1383
- effort?: 'high' | 'low' | 'medium';
1384
- exclude?: boolean;
183
+ //#region src/middleware/logging.middleware.d.ts
184
+ interface LoggingMiddlewareOptions {
185
+ logger: LoggerPort;
186
+ include?: {
187
+ params?: boolean;
188
+ content?: boolean;
189
+ usage?: boolean;
1385
190
  };
1386
191
  }
1387
- interface OpenRouterConfig {
1388
- apiKey: string;
1389
- metadata?: OpenRouterMetadata;
1390
- }
1391
- interface OpenRouterMetadata {
1392
- /** Application name for X-Title header */
1393
- application?: string;
1394
- /** Website URL for HTTP-Referer header */
1395
- website?: string;
1396
- }
1397
- interface OpenRouterProvider {
1398
- /** Get a language model instance */
1399
- model: (name: string, options?: ModelOptions) => LanguageModel;
1400
- }
1401
192
  /**
1402
- * Creates an OpenRouter provider for AI SDK models.
1403
- *
1404
- * @example
1405
- * ```ts
1406
- * const provider = createOpenRouterProvider({ apiKey: process.env.OPENROUTER_API_KEY });
1407
- * const model = provider.model('anthropic/claude-sonnet-4-20250514');
1408
- *
1409
- * const { text } = await generateText({ model, prompt: 'Hello!' });
1410
- * ```
193
+ * Creates middleware that logs AI SDK requests and responses.
1411
194
  */
1412
- declare function createOpenRouterProvider(config: OpenRouterConfig): OpenRouterProvider;
195
+ declare function createLoggingMiddleware(options: LoggingMiddlewareOptions): LanguageModelMiddleware;
1413
196
  //#endregion
1414
- //#region src/provider/openrouter-metadata.adapter.d.ts
197
+ //#region src/middleware/schema-instruction.middleware.d.ts
1415
198
  /**
1416
- * OpenRouter adapter for extracting usage and cost from provider metadata
199
+ * Creates middleware that injects the JSON schema of a structured-output
200
+ * request into the last user message.
201
+ *
202
+ * Some gateways silently drop the native structured-output field when
203
+ * translating to their backend, so the model never sees the schema and
204
+ * answers in free prose. This middleware re-states the schema as part of the
205
+ * user message so structured output works regardless. It targets the user
206
+ * message rather than a system message because gateways backed by cloaked
207
+ * CLI agents bury injected system messages under their own persona prompt and
208
+ * ignore them. The original `responseFormat` is left untouched: backends that
209
+ * honor it get the native signal too.
210
+ *
211
+ * No-op for text generations (no `responseFormat`, or `type: 'text'`).
1417
212
  */
1418
- declare class OpenRouterMetadataAdapter implements ProviderMetadataPort {
1419
- extract(providerMetadata: Record<string, unknown> | undefined): ExtractedProviderMetadata;
1420
- }
213
+ declare function createSchemaInstructionMiddleware(): LanguageModelMiddleware;
1421
214
  //#endregion
1422
- //#region src/provider/openai-compatible.provider.d.ts
1423
- interface OpenAICompatibleModelOptions {
1424
- /** Maximum tokens to generate */
1425
- maxTokens?: number;
215
+ //#region src/model/fallback-model.d.ts
216
+ interface FallbackModelOptions {
217
+ primary: LanguageModel;
218
+ fallback: LanguageModel;
219
+ logger?: LoggerPort;
1426
220
  }
1427
- interface OpenAICompatibleConfig {
1428
- /** API key for authentication */
1429
- apiKey: string;
1430
- /** Base URL of the OpenAI-compatible API */
1431
- baseURL: string;
1432
- /** Optional model name mapping */
1433
- modelMapping?: Record<string, string>;
1434
- }
1435
- interface OpenAICompatibleProvider {
1436
- /** Get a language model instance */
1437
- model: (name: string, options?: OpenAICompatibleModelOptions) => LanguageModel;
1438
- }
1439
- /**
1440
- * Creates a provider for OpenAI-compatible APIs.
1441
- * Works with any API implementing the OpenAI chat completions spec.
1442
- */
1443
- declare function createOpenAICompatibleProvider(config: OpenAICompatibleConfig): OpenAICompatibleProvider;
1444
- //#endregion
1445
- //#region src/provider/openai-compatible-metadata.adapter.d.ts
1446
221
  /**
1447
- * Metadata adapter for OpenAI-compatible APIs (including gateway-intelligence).
1448
- * Extracts usage data from the standardized OpenAI response format.
222
+ * Creates a `LanguageModelV4` that transparently falls back to a secondary
223
+ * model when the primary model fails with a retryable error (HTTP 429, 5xx,
224
+ * network errors/timeouts). Non-retryable errors (400s, validation, abort)
225
+ * propagate unchanged.
226
+ *
227
+ * This is a model, not a middleware — middleware cannot switch the
228
+ * underlying model, only transform a single model's behavior.
229
+ *
230
+ * @example
231
+ * ```ts
232
+ * const model = createFallbackModel({
233
+ * primary: provider.model('anthropic/claude-sonnet-4'),
234
+ * fallback: provider.model('openai/gpt-4o-mini'),
235
+ * logger,
236
+ * });
237
+ * ```
1449
238
  */
1450
- declare class OpenAICompatibleMetadataAdapter implements ProviderMetadataPort {
1451
- extract(providerMetadata: Record<string, unknown> | undefined): ExtractedProviderMetadata;
1452
- }
239
+ declare function createFallbackModel(options: FallbackModelOptions): LanguageModel;
1453
240
  //#endregion
1454
- export { type CostDetails, type ExtractedProviderMetadata, type GenerateStructuredOptions, type GenerationError, type GenerationErrorCode, type GenerationParams, type GenerationResult, LangfuseAdapter, type LangfuseConfig, type LoggingMiddlewareOptions, type ModelOptions, NoopObservabilityAdapter, type ObservabilityMetadata, type ObservabilityMiddlewareOptions, type ObservabilityPort, type OpenAICompatibleConfig, OpenAICompatibleMetadataAdapter, type OpenAICompatibleModelOptions, type OpenAICompatibleProvider, type OpenRouterConfig, type OpenRouterMetadata, OpenRouterMetadataAdapter, type OpenRouterProvider, ParseObjectError, type ParseTextOptions, type ProviderMetadataPort, type TraceParams, type UsageDetails, classifyError, createLoggingMiddleware, createObservabilityMiddleware, createOpenAICompatibleProvider, createOpenRouterProvider, createSchemaPrompt, generateStructured, generationFailure, generationSuccess, isFailure, isSuccess, parseObject, parseText, unwrap, unwrapOr, withObservability };
241
+ export { type AgentConfig, type CostMiddlewareOptions, type CostPricing, type FallbackModelOptions, type GatewayConfig, type GatewayProvider, type Intelligence, type IntelligenceConfig, type LoggingMiddlewareOptions, type OpenRouterConfig, type OpenRouterMetadata, type OpenRouterProvider, type ProviderConfig, createCostMiddleware, createFallbackModel, createGatewayProvider, createIntelligence, createLoggingMiddleware, createOpenRouterProvider, createSchemaInstructionMiddleware };
1455
242
  //# sourceMappingURL=index.d.cts.map