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