@ai-sdk/openai-compatible 3.0.59 → 3.0.60
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/CHANGELOG.md +9 -0
- package/dist/index.d.ts +342 -330
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1419 -1834
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +176 -168
- package/dist/internal/index.d.ts.map +1 -0
- package/dist/internal/index.js +254 -346
- package/dist/internal/index.js.map +1 -1
- package/docs/index.mdx +1 -1
- package/package.json +8 -8
package/dist/internal/index.d.ts
CHANGED
|
@@ -1,216 +1,224 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { JSONValue, LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4FinishReason, LanguageModelV4Prompt, LanguageModelV4Usage, SharedV4ProviderMetadata, SharedV4Warning } from "@ai-sdk/provider";
|
|
2
|
+
import { FetchFunction } from "@ai-sdk/provider-utils";
|
|
3
|
+
import { ZodType, z } from "zod/v4";
|
|
4
|
+
//#region src/chat/openai-compatible-api-types.d.ts
|
|
6
5
|
type OpenAICompatibleChatPrompt = Array<OpenAICompatibleMessage>;
|
|
7
6
|
type OpenAICompatibleMessage = OpenAICompatibleSystemMessage | OpenAICompatibleUserMessage | OpenAICompatibleAssistantMessage | OpenAICompatibleToolMessage;
|
|
8
7
|
type JsonRecord<T = never> = Record<string, JSONValue | JSONValue[] | T | T[] | undefined>;
|
|
9
8
|
interface OpenAICompatibleSystemMessage extends JsonRecord {
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
role: 'system';
|
|
10
|
+
content: string;
|
|
12
11
|
}
|
|
13
12
|
interface OpenAICompatibleUserMessage extends JsonRecord<OpenAICompatibleContentPart> {
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
role: 'user';
|
|
14
|
+
content: string | Array<OpenAICompatibleContentPart>;
|
|
16
15
|
}
|
|
17
16
|
type OpenAICompatibleContentPart = OpenAICompatibleContentPartText | OpenAICompatibleContentPartImage | OpenAICompatibleContentPartVideo | OpenAICompatibleContentPartInputAudio | OpenAICompatibleContentPartFile;
|
|
18
17
|
interface OpenAICompatibleContentPartText extends JsonRecord {
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
type: 'text';
|
|
19
|
+
text: string;
|
|
21
20
|
}
|
|
22
21
|
interface OpenAICompatibleContentPartImage extends JsonRecord {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
type: 'image_url';
|
|
23
|
+
image_url: {
|
|
24
|
+
url: string;
|
|
25
|
+
};
|
|
27
26
|
}
|
|
28
27
|
interface OpenAICompatibleContentPartVideo extends JsonRecord {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
type: 'video_url';
|
|
29
|
+
video_url: {
|
|
30
|
+
url: string;
|
|
31
|
+
};
|
|
33
32
|
}
|
|
34
33
|
interface OpenAICompatibleContentPartInputAudio extends JsonRecord {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
type: 'input_audio';
|
|
35
|
+
input_audio: {
|
|
36
|
+
data: string;
|
|
37
|
+
format: 'wav' | 'mp3';
|
|
38
|
+
};
|
|
40
39
|
}
|
|
41
40
|
interface OpenAICompatibleContentPartFile extends JsonRecord {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
type: 'file';
|
|
42
|
+
file: {
|
|
43
|
+
filename: string;
|
|
44
|
+
file_data: string;
|
|
45
|
+
};
|
|
47
46
|
}
|
|
48
47
|
interface OpenAICompatibleAssistantMessage extends JsonRecord<OpenAICompatibleMessageToolCall> {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
role: 'assistant';
|
|
49
|
+
content?: string | null;
|
|
50
|
+
reasoning_content?: string;
|
|
51
|
+
tool_calls?: Array<OpenAICompatibleMessageToolCall>;
|
|
53
52
|
}
|
|
54
53
|
interface OpenAICompatibleMessageToolCall extends JsonRecord {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
54
|
+
type: 'function';
|
|
55
|
+
id: string;
|
|
56
|
+
function: {
|
|
57
|
+
arguments: string;
|
|
58
|
+
name: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Additional content for provider-specific features.
|
|
62
|
+
* Used by Google Gemini for thought signatures via OpenAI compatibility.
|
|
63
|
+
*/
|
|
64
|
+
extra_content?: {
|
|
65
|
+
google?: {
|
|
66
|
+
thought_signature?: string;
|
|
69
67
|
};
|
|
68
|
+
};
|
|
70
69
|
}
|
|
71
70
|
interface OpenAICompatibleToolMessage extends JsonRecord {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
role: 'tool';
|
|
72
|
+
content: string;
|
|
73
|
+
tool_call_id: string;
|
|
75
74
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/chat/convert-to-openai-compatible-chat-messages.d.ts
|
|
77
|
+
export declare function convertToOpenAICompatibleChatMessages(prompt: LanguageModelV4Prompt, { providerOptionsKey }?: {
|
|
78
|
+
providerOptionsKey?: string;
|
|
79
79
|
}): OpenAICompatibleChatPrompt;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/chat/convert-openai-compatible-chat-usage.d.ts
|
|
82
|
+
export declare function convertOpenAICompatibleChatUsage(usage: {
|
|
83
|
+
prompt_tokens?: number | null;
|
|
84
|
+
completion_tokens?: number | null;
|
|
85
|
+
prompt_tokens_details?: {
|
|
86
|
+
cached_tokens?: number | null;
|
|
87
|
+
} | null;
|
|
88
|
+
completion_tokens_details?: {
|
|
89
|
+
reasoning_tokens?: number | null;
|
|
90
|
+
} | null;
|
|
90
91
|
} | undefined | null): LanguageModelV4Usage;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/chat/map-openai-compatible-finish-reason.d.ts
|
|
94
|
+
export declare function mapOpenAICompatibleFinishReason(finishReason: string | null | undefined): LanguageModelV4FinishReason['unified'];
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/chat/get-response-metadata.d.ts
|
|
97
|
+
export declare function getResponseMetadata({ id, model, created }: {
|
|
98
|
+
id?: string | null;
|
|
99
|
+
model?: string | null;
|
|
100
|
+
created?: number | null;
|
|
101
|
+
}): import("@ai-sdk/provider").LanguageModelV4ResponseMetadata;
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/chat/openai-compatible-prepare-tools.d.ts
|
|
104
|
+
export declare function prepareTools({ tools, toolChoice }: {
|
|
105
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
106
|
+
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
103
107
|
}): {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
108
|
+
tools: undefined | Array<{
|
|
109
|
+
type: 'function';
|
|
110
|
+
function: {
|
|
111
|
+
name: string;
|
|
112
|
+
description: string | undefined;
|
|
113
|
+
parameters: unknown;
|
|
114
|
+
strict?: boolean;
|
|
115
|
+
};
|
|
116
|
+
}>;
|
|
117
|
+
toolChoice: {
|
|
118
|
+
type: 'function';
|
|
119
|
+
function: {
|
|
120
|
+
name: string;
|
|
121
|
+
};
|
|
122
|
+
} | 'auto' | 'none' | 'required' | undefined;
|
|
123
|
+
toolWarnings: SharedV4Warning[];
|
|
120
124
|
};
|
|
121
|
-
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/openai-compatible-error.d.ts
|
|
122
127
|
type ProviderErrorStructure<T> = {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
128
|
+
errorSchema: ZodType<T>;
|
|
129
|
+
errorToMessage: (error: T) => string;
|
|
130
|
+
isRetryable?: (response: Response, error?: T) => boolean;
|
|
126
131
|
};
|
|
127
|
-
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/chat/openai-compatible-metadata-extractor.d.ts
|
|
128
134
|
/**
|
|
129
135
|
* Extracts provider-specific metadata from API responses.
|
|
130
136
|
* Used to standardize metadata handling across different LLM providers while allowing
|
|
131
137
|
* provider-specific metadata to be captured.
|
|
132
138
|
*/
|
|
133
139
|
type MetadataExtractor = {
|
|
140
|
+
/**
|
|
141
|
+
* Extracts provider metadata from a complete, non-streaming response.
|
|
142
|
+
*
|
|
143
|
+
* @param parsedBody - The parsed response JSON body from the provider's API.
|
|
144
|
+
*
|
|
145
|
+
* @returns Provider-specific metadata or undefined if no metadata is available.
|
|
146
|
+
* The metadata should be under a key indicating the provider id.
|
|
147
|
+
*/
|
|
148
|
+
extractMetadata: ({ parsedBody }: {
|
|
149
|
+
parsedBody: unknown;
|
|
150
|
+
}) => Promise<SharedV4ProviderMetadata | undefined>;
|
|
151
|
+
/**
|
|
152
|
+
* Creates an extractor for handling streaming responses. The returned object provides
|
|
153
|
+
* methods to process individual chunks and build the final metadata from the accumulated
|
|
154
|
+
* stream data.
|
|
155
|
+
*
|
|
156
|
+
* @returns An object with methods to process chunks and build metadata from a stream
|
|
157
|
+
*/
|
|
158
|
+
createStreamExtractor: () => {
|
|
134
159
|
/**
|
|
135
|
-
*
|
|
160
|
+
* Process an individual chunk from the stream. Called for each chunk in the response stream
|
|
161
|
+
* to accumulate metadata throughout the streaming process.
|
|
136
162
|
*
|
|
137
|
-
* @param
|
|
138
|
-
*
|
|
139
|
-
* @returns Provider-specific metadata or undefined if no metadata is available.
|
|
140
|
-
* The metadata should be under a key indicating the provider id.
|
|
163
|
+
* @param parsedChunk - The parsed JSON response chunk from the provider's API
|
|
141
164
|
*/
|
|
142
|
-
|
|
143
|
-
parsedBody: unknown;
|
|
144
|
-
}) => Promise<SharedV4ProviderMetadata | undefined>;
|
|
165
|
+
processChunk(parsedChunk: unknown): void;
|
|
145
166
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* stream data.
|
|
167
|
+
* Builds the metadata object after all chunks have been processed.
|
|
168
|
+
* Called at the end of the stream to generate the complete provider metadata.
|
|
149
169
|
*
|
|
150
|
-
* @returns
|
|
170
|
+
* @returns Provider-specific metadata or undefined if no metadata is available.
|
|
171
|
+
* The metadata should be under a key indicating the provider id.
|
|
151
172
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
* Process an individual chunk from the stream. Called for each chunk in the response stream
|
|
155
|
-
* to accumulate metadata throughout the streaming process.
|
|
156
|
-
*
|
|
157
|
-
* @param parsedChunk - The parsed JSON response chunk from the provider's API
|
|
158
|
-
*/
|
|
159
|
-
processChunk(parsedChunk: unknown): void;
|
|
160
|
-
/**
|
|
161
|
-
* Builds the metadata object after all chunks have been processed.
|
|
162
|
-
* Called at the end of the stream to generate the complete provider metadata.
|
|
163
|
-
*
|
|
164
|
-
* @returns Provider-specific metadata or undefined if no metadata is available.
|
|
165
|
-
* The metadata should be under a key indicating the provider id.
|
|
166
|
-
*/
|
|
167
|
-
buildMetadata(): SharedV4ProviderMetadata | undefined;
|
|
168
|
-
};
|
|
173
|
+
buildMetadata(): SharedV4ProviderMetadata | undefined;
|
|
174
|
+
};
|
|
169
175
|
};
|
|
170
|
-
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/chat/openai-compatible-chat-language-model.d.ts
|
|
171
178
|
type OpenAICompatibleChatConfig = {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
179
|
+
provider: string;
|
|
180
|
+
headers?: () => Record<string, string | undefined>;
|
|
181
|
+
url: (options: {
|
|
182
|
+
modelId: string;
|
|
183
|
+
path: string;
|
|
184
|
+
}) => string;
|
|
185
|
+
fetch?: FetchFunction;
|
|
186
|
+
includeUsage?: boolean;
|
|
187
|
+
errorStructure?: ProviderErrorStructure<any>;
|
|
188
|
+
metadataExtractor?: MetadataExtractor;
|
|
189
|
+
/**
|
|
190
|
+
* Whether the model supports structured outputs.
|
|
191
|
+
*/
|
|
192
|
+
supportsStructuredOutputs?: boolean;
|
|
193
|
+
/**
|
|
194
|
+
* The supported URLs for the model.
|
|
195
|
+
*/
|
|
196
|
+
supportedUrls?: () => LanguageModelV4['supportedUrls'];
|
|
197
|
+
/**
|
|
198
|
+
* Optional function to transform the request body before sending it to the API.
|
|
199
|
+
* This is useful for proxy providers that may require a different request format
|
|
200
|
+
* than the official OpenAI API.
|
|
201
|
+
*/
|
|
202
|
+
transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
|
|
203
|
+
/**
|
|
204
|
+
* Optional usage converter for OpenAI-compatible providers with different
|
|
205
|
+
* token accounting semantics.
|
|
206
|
+
*/
|
|
207
|
+
convertUsage?: (usage: z.infer<typeof openaiCompatibleTokenUsageSchema>) => LanguageModelV4Usage;
|
|
201
208
|
};
|
|
202
209
|
declare const openaiCompatibleTokenUsageSchema: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
210
|
+
prompt_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
211
|
+
completion_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
212
|
+
total_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
213
|
+
prompt_tokens_details: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
214
|
+
cached_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
215
|
+
}, z.core.$loose>>>;
|
|
216
|
+
completion_tokens_details: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
217
|
+
reasoning_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
218
|
+
accepted_prediction_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
219
|
+
rejected_prediction_tokens: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
220
|
+
}, z.core.$loose>>>;
|
|
214
221
|
}, z.core.$loose>>>;
|
|
215
|
-
|
|
216
|
-
export {
|
|
222
|
+
//#endregion
|
|
223
|
+
export type { OpenAICompatibleChatConfig };
|
|
224
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/chat/openai-compatible-api-types.ts","../../src/chat/convert-to-openai-compatible-chat-messages.ts","../../src/chat/convert-openai-compatible-chat-usage.ts","../../src/chat/map-openai-compatible-finish-reason.ts","../../src/chat/get-response-metadata.ts","../../src/chat/openai-compatible-prepare-tools.ts","../../src/openai-compatible-error.ts","../../src/chat/openai-compatible-metadata-extractor.ts","../../src/chat/openai-compatible-chat-language-model.ts"],"mappings":";;;;KAEY,6BAA6B,MAAM;KAEnC,0BACR,gCACA,8BACA,mCACA;KAIC,WAAW,aAAa,eAE3B,YAAY,cAAc,IAAI;UAGf,sCAAsC;EACrD;EACA;;UAGe,oCAAoC,WAAW;EAC9D;EACA,kBAAkB,MAAM;;KAGd,8BACR,kCACA,mCACA,mCACA,wCACA;UAEa,wCAAwC;EACvD;EACA;;UAGe,yCAAyC;EACxD;EACA;IAAa;;;UAIE,yCAAyC;EACxD;EACA;IAAa;;;UAIE,8CAA8C;EAC7D;EACA;IAAe;IAAc;;;UAId,wCAAwC;EACvD;EACA;IAAQ;IAAkB;;;UAGX,yCAAyC,WAAW;EACnE;EACA;EACA;EACA,aAAa,MAAM;;UAGJ,wCAAwC;EACvD;EACA;EACA;IACE;IACA;;;;;;EAMF;IACE;MACE;;;;UAKW,oCAAoC;EACnD;EACA;EACA;;;;wBC3Dc,sCACd,QAAQ,yBACN;EAAmC;IACpC;;;wBC/Ba,iCACd;EAEM;EACA;EACA;IACE;;EAEF;IACE;;uBAKP;;;wBCfa,gCACd,0CACC;;;wBCFa,sBACd,IACA,OACA;EAEA;EACA;EACA;+BACD;;;wBCLe,eACd,OACA;EAEA,OAAO;EACP,aAAa;;EAEb,mBAEI;IACE;IACA;MACE;MACA;MACA;MACA;;;EAGR;IACM;IAAkB;MAAY;;;EAKpC,cAAc;;;;KCXJ,uBAAuB;EACjC,aAAa,QAAQ;EACrB,iBAAiB,OAAO;EACxB,eAAe,UAAU,UAAU,QAAQ;;;;;;;;;KCdjC;;;;;;;;;EASV,oBACE;IAEA;QACI,QAAQ;;;;;;;;EASd;;;;;;;IAOE,aAAa;;;;;;;;IASb,iBAAiB;;;;;KCgBT;EACV;EACA,gBAAgB;EAChB,MAAM;IAAW;IAAiB;;EAClC,QAAQ;EACR;EACA,iBAAiB;EACjB,oBAAoB;;;;EAKpB;;;;EAKA,sBAAsB;;;;;;EAOtB,wBAAwB,MAAM,wBAAwB;;;;;EAMtD,gBACE,OAAO,EAAE,aAAa,sCACnB;;cAqqBD,kCAAgC,EAAA,YAAA,EAAA,YAAA,EAAA;;;;;;;;;;;;GAkB1B,EAAA,KAAA"}
|