@core-ai/openai 0.14.0 → 0.16.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/compat.d.ts CHANGED
@@ -1,16 +1,13 @@
1
+ import * as _core_ai_core_ai from '@core-ai/core-ai';
1
2
  import { ChatModel } from '@core-ai/core-ai';
2
- import OpenAI from 'openai';
3
- import { O as OpenAIProvider, a as OpenAIProviderBaseOptions } from './provider-options-DzqvHoId.js';
4
- export { b as OpenAICompatGenerateProviderOptions, c as OpenAICompatRequestOptions, o as openaiCompatGenerateProviderOptionsSchema, d as openaiCompatProviderOptionsSchema } from './provider-options-DzqvHoId.js';
3
+ import { O as OpenAIChatClient, a as OpenAIProvider, b as OpenAIProviderBaseOptions } from './provider-options-CpxtGjm0.js';
4
+ export { c as OpenAICompatGenerateProviderOptions, d as OpenAICompatRequestOptions, o as openaiCompatGenerateProviderOptionsSchema, e as openaiCompatProviderOptionsSchema } from './provider-options-CpxtGjm0.js';
5
+ import 'openai';
5
6
  import 'zod';
6
7
 
7
- type OpenAIChatClient = {
8
- chat: OpenAI['chat'];
9
- };
10
- declare function createOpenAICompatChatModel(client: OpenAIChatClient, modelId: string, providerId?: string): ChatModel;
11
-
12
8
  type OpenAICompatProviderOptions = OpenAIProviderBaseOptions;
13
9
  type OpenAICompatProvider = OpenAIProvider;
10
+ /** @deprecated Use `createOpenAI().chat` or `@core-ai/openai-compat`. */
14
11
  declare function createOpenAICompat(options?: OpenAICompatProviderOptions): OpenAICompatProvider;
15
12
  type OpenAICompatChatProviderOptions = {
16
13
  apiKey?: string;
@@ -23,7 +20,12 @@ type OpenAICompatChatProvider = {
23
20
  /**
24
21
  * Creates a chat-only OpenAI-compatible provider. Handles client construction
25
22
  * internally so consumers do not need a direct dependency on the `openai` package.
23
+ *
24
+ * @deprecated Use the provider composition exports from `@core-ai/openai`.
26
25
  */
27
26
  declare function createOpenAICompatChatProvider(options?: OpenAICompatChatProviderOptions, providerId?: string): OpenAICompatChatProvider;
28
27
 
29
- export { type OpenAIChatClient, type OpenAICompatChatProvider, type OpenAICompatChatProviderOptions, type OpenAICompatProvider, type OpenAICompatProviderOptions, createOpenAICompat, createOpenAICompatChatModel, createOpenAICompatChatProvider };
28
+ /** @deprecated Use `createOpenAI().chat.chatModel()` instead. */
29
+ declare function createOpenAICompatChatModel(client: OpenAIChatClient, modelId: string, providerId?: string): _core_ai_core_ai.ChatModel;
30
+
31
+ export { OpenAIChatClient, type OpenAICompatChatProvider, type OpenAICompatChatProviderOptions, type OpenAICompatProvider, type OpenAICompatProviderOptions, createOpenAICompat, createOpenAICompatChatModel, createOpenAICompatChatProvider };
package/dist/compat.js CHANGED
@@ -1,465 +1,17 @@
1
1
  import {
2
- convertToolChoice,
3
- convertTools,
2
+ createOpenAIChatCompletionsModel,
4
3
  createOpenAIProvider,
5
- createStructuredOutputOptions,
6
- extractStructuredObject,
7
- getOpenAIModelCapabilities,
8
- getStructuredOutputToolName,
9
4
  openaiCompatGenerateProviderOptionsSchema,
10
- openaiCompatProviderOptionsSchema,
11
- parseOpenAICompatGenerateProviderOptions,
12
- safeParseJsonObject,
13
- toOpenAIReasoningEffort,
14
- transformStructuredOutputStream,
15
- validateOpenAIReasoningConfig,
16
- wrapOpenAIError
17
- } from "./chunk-7MAEB5C7.js";
5
+ openaiCompatProviderOptionsSchema
6
+ } from "./chunk-YORO2XQ3.js";
18
7
 
19
8
  // src/compat/provider.ts
20
9
  import OpenAI from "openai";
21
-
22
- // src/compat/chat-model.ts
23
- import { createObjectStream, createChatStream } from "@core-ai/core-ai";
24
-
25
- // src/compat/chat-adapter.ts
26
- import { clampReasoningEffort } from "@core-ai/core-ai";
27
- function convertMessages(messages) {
28
- return messages.map(convertMessage);
29
- }
30
- function convertMessage(message) {
31
- if (message.role === "system") {
32
- return {
33
- role: "system",
34
- content: message.content
35
- };
36
- }
37
- if (message.role === "user") {
38
- return {
39
- role: "user",
40
- content: typeof message.content === "string" ? message.content : message.content.map(convertUserContentPart)
41
- };
42
- }
43
- if (message.role === "assistant") {
44
- const text = message.parts.flatMap((part) => {
45
- if (part.type === "text") return [part.text];
46
- if (part.type === "reasoning" && part.text.length > 0) {
47
- return [`<thinking>${part.text}</thinking>`];
48
- }
49
- return [];
50
- }).join("\n\n");
51
- const toolCalls = message.parts.flatMap(
52
- (part) => part.type === "tool-call" ? [part.toolCall] : []
53
- );
54
- return {
55
- role: "assistant",
56
- content: text.length > 0 ? text : null,
57
- ...toolCalls.length > 0 ? {
58
- tool_calls: toolCalls.map((toolCall) => ({
59
- id: toolCall.id,
60
- type: "function",
61
- function: {
62
- name: toolCall.name,
63
- arguments: JSON.stringify(toolCall.arguments)
64
- }
65
- }))
66
- } : {}
67
- };
68
- }
69
- return {
70
- role: "tool",
71
- tool_call_id: message.toolCallId,
72
- content: message.content
73
- };
74
- }
75
- function convertUserContentPart(part) {
76
- if (part.type === "text") {
77
- return {
78
- type: "text",
79
- text: part.text
80
- };
81
- }
82
- if (part.type === "image") {
83
- const url = part.source.type === "url" ? part.source.url : `data:${part.source.mediaType};base64,${part.source.data}`;
84
- return {
85
- type: "image_url",
86
- image_url: {
87
- url
88
- }
89
- };
90
- }
91
- return {
92
- type: "file",
93
- file: {
94
- file_data: part.data,
95
- ...part.filename ? { filename: part.filename } : {}
96
- }
97
- };
98
- }
99
- function createGenerateRequest(modelId, options) {
100
- return createRequest(modelId, options, false);
101
- }
102
- function createStreamRequest(modelId, options) {
103
- return createRequest(modelId, options, true);
104
- }
105
- function createRequest(modelId, options, stream) {
106
- const openaiOptions = parseOpenAICompatGenerateProviderOptions(
107
- options.providerOptions
108
- );
109
- return {
110
- ...createRequestBase(modelId, options),
111
- ...stream ? {
112
- stream: true,
113
- stream_options: {
114
- include_usage: true
115
- }
116
- } : {},
117
- ...mapOpenAIProviderOptionsToRequestFields(openaiOptions)
118
- };
119
- }
120
- function createRequestBase(modelId, options) {
121
- validateOpenAIReasoningConfig(modelId, options);
122
- const reasoningFields = mapReasoningToRequestFields(modelId, options);
123
- return {
124
- model: modelId,
125
- messages: convertMessages(options.messages),
126
- ...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
127
- ...options.toolChoice ? { tool_choice: convertToolChoice(options.toolChoice) } : {},
128
- ...reasoningFields,
129
- ...mapSamplingToRequestFields(options)
130
- };
131
- }
132
- function mapSamplingToRequestFields(options) {
133
- return {
134
- ...options.temperature !== void 0 ? { temperature: options.temperature } : {},
135
- ...options.maxTokens !== void 0 ? { max_tokens: options.maxTokens } : {},
136
- ...options.topP !== void 0 ? { top_p: options.topP } : {}
137
- };
138
- }
139
- function mapOpenAIProviderOptionsToRequestFields(options) {
140
- return {
141
- ...options?.store !== void 0 ? { store: options.store } : {},
142
- ...options?.serviceTier !== void 0 ? { service_tier: options.serviceTier } : {},
143
- ...options?.parallelToolCalls !== void 0 ? { parallel_tool_calls: options.parallelToolCalls } : {},
144
- ...options?.user !== void 0 ? { user: options.user } : {},
145
- ...options?.stopSequences ? { stop: options.stopSequences } : {},
146
- ...options?.frequencyPenalty !== void 0 ? { frequency_penalty: options.frequencyPenalty } : {},
147
- ...options?.presencePenalty !== void 0 ? { presence_penalty: options.presencePenalty } : {},
148
- ...options?.seed !== void 0 ? { seed: options.seed } : {}
149
- };
150
- }
151
- function mapGenerateResponse(response) {
152
- const firstChoice = response.choices[0];
153
- if (!firstChoice) {
154
- return {
155
- parts: [],
156
- content: null,
157
- reasoning: null,
158
- toolCalls: [],
159
- finishReason: "unknown",
160
- usage: {
161
- inputTokens: 0,
162
- outputTokens: 0,
163
- inputTokenDetails: {
164
- cacheReadTokens: 0,
165
- cacheWriteTokens: 0
166
- },
167
- outputTokenDetails: {}
168
- }
169
- };
170
- }
171
- const reasoningTokens = response.usage?.completion_tokens_details?.reasoning_tokens;
172
- const content = extractTextContent(firstChoice.message.content);
173
- const toolCalls = parseToolCalls(firstChoice.message.tool_calls);
174
- const parts = createAssistantParts(content, toolCalls);
175
- return {
176
- parts,
177
- content,
178
- reasoning: null,
179
- toolCalls,
180
- finishReason: mapFinishReason(firstChoice.finish_reason),
181
- usage: {
182
- inputTokens: response.usage?.prompt_tokens ?? 0,
183
- outputTokens: response.usage?.completion_tokens ?? 0,
184
- inputTokenDetails: {
185
- cacheReadTokens: response.usage?.prompt_tokens_details?.cached_tokens ?? 0,
186
- cacheWriteTokens: 0
187
- },
188
- outputTokenDetails: {
189
- ...reasoningTokens !== void 0 ? { reasoningTokens } : {}
190
- }
191
- }
192
- };
193
- }
194
- function parseToolCalls(calls) {
195
- if (!calls) {
196
- return [];
197
- }
198
- return calls.flatMap((toolCall) => {
199
- if (toolCall.type !== "function") {
200
- return [];
201
- }
202
- return [mapFunctionToolCall(toolCall)];
203
- });
204
- }
205
- function mapFunctionToolCall(toolCall) {
206
- return {
207
- id: toolCall.id,
208
- name: toolCall.function.name,
209
- arguments: safeParseJsonObject(toolCall.function.arguments)
210
- };
211
- }
212
- function mapFinishReason(reason) {
213
- if (reason === "stop") {
214
- return "stop";
215
- }
216
- if (reason === "length") {
217
- return "length";
218
- }
219
- if (reason === "tool_calls" || reason === "function_call") {
220
- return "tool-calls";
221
- }
222
- if (reason === "content_filter") {
223
- return "content-filter";
224
- }
225
- return "unknown";
226
- }
227
- async function* transformStream(stream) {
228
- const bufferedToolCalls = /* @__PURE__ */ new Map();
229
- const emittedToolCalls = /* @__PURE__ */ new Set();
230
- let finishReason = "unknown";
231
- let textOpen = false;
232
- let usage = {
233
- inputTokens: 0,
234
- outputTokens: 0,
235
- inputTokenDetails: {
236
- cacheReadTokens: 0,
237
- cacheWriteTokens: 0
238
- },
239
- outputTokenDetails: {}
240
- };
241
- const startText = function* () {
242
- if (textOpen) {
243
- return;
244
- }
245
- textOpen = true;
246
- yield { type: "text-start" };
247
- };
248
- const closeText = function* () {
249
- if (!textOpen) {
250
- return;
251
- }
252
- textOpen = false;
253
- yield { type: "text-end" };
254
- };
255
- for await (const chunk of stream) {
256
- if (chunk.usage) {
257
- const reasoningTokens = chunk.usage.completion_tokens_details?.reasoning_tokens;
258
- usage = {
259
- inputTokens: chunk.usage.prompt_tokens ?? 0,
260
- outputTokens: chunk.usage.completion_tokens ?? 0,
261
- inputTokenDetails: {
262
- cacheReadTokens: chunk.usage.prompt_tokens_details?.cached_tokens ?? 0,
263
- cacheWriteTokens: 0
264
- },
265
- outputTokenDetails: {
266
- ...reasoningTokens !== void 0 ? { reasoningTokens } : {}
267
- }
268
- };
269
- }
270
- const choice = chunk.choices[0];
271
- if (!choice) {
272
- continue;
273
- }
274
- if (choice.delta.content) {
275
- yield* startText();
276
- yield {
277
- type: "text-delta",
278
- text: choice.delta.content
279
- };
280
- }
281
- if (choice.delta.tool_calls) {
282
- yield* closeText();
283
- for (const partialToolCall of choice.delta.tool_calls) {
284
- const current = bufferedToolCalls.get(
285
- partialToolCall.index
286
- ) ?? {
287
- id: partialToolCall.id ?? `tool-${partialToolCall.index}`,
288
- name: partialToolCall.function?.name ?? "",
289
- arguments: ""
290
- };
291
- const wasNew = !bufferedToolCalls.has(partialToolCall.index);
292
- if (partialToolCall.id) {
293
- current.id = partialToolCall.id;
294
- }
295
- if (partialToolCall.function?.name) {
296
- current.name = partialToolCall.function.name;
297
- }
298
- if (partialToolCall.function?.arguments) {
299
- current.arguments += partialToolCall.function.arguments;
300
- yield {
301
- type: "tool-call-delta",
302
- toolCallId: current.id,
303
- argumentsDelta: partialToolCall.function.arguments
304
- };
305
- }
306
- bufferedToolCalls.set(partialToolCall.index, current);
307
- if (wasNew) {
308
- yield {
309
- type: "tool-call-start",
310
- toolCallId: current.id,
311
- toolName: current.name
312
- };
313
- }
314
- }
315
- }
316
- if (choice.finish_reason) {
317
- finishReason = mapFinishReason(choice.finish_reason);
318
- }
319
- if (finishReason === "tool-calls") {
320
- yield* closeText();
321
- for (const toolCall of bufferedToolCalls.values()) {
322
- if (emittedToolCalls.has(toolCall.id)) {
323
- continue;
324
- }
325
- emittedToolCalls.add(toolCall.id);
326
- yield {
327
- type: "tool-call-end",
328
- toolCall: {
329
- id: toolCall.id,
330
- name: toolCall.name,
331
- arguments: safeParseJsonObject(toolCall.arguments)
332
- }
333
- };
334
- }
335
- }
336
- }
337
- yield* closeText();
338
- yield {
339
- type: "finish",
340
- finishReason,
341
- usage
342
- };
343
- }
344
- function mapReasoningToRequestFields(modelId, options) {
345
- if (!options.reasoning) {
346
- return {};
347
- }
348
- const capabilities = getOpenAIModelCapabilities(modelId);
349
- if (!capabilities.reasoning.supported) {
350
- return {};
351
- }
352
- const clampedEffort = clampReasoningEffort(
353
- options.reasoning.effort,
354
- capabilities.reasoning.supportedEfforts
355
- );
356
- return {
357
- reasoning_effort: toOpenAIReasoningEffort(clampedEffort)
358
- };
359
- }
360
- function createAssistantParts(content, toolCalls) {
361
- const parts = [];
362
- if (content) {
363
- parts.push({
364
- type: "text",
365
- text: content
366
- });
367
- }
368
- for (const toolCall of toolCalls) {
369
- parts.push({
370
- type: "tool-call",
371
- toolCall
372
- });
373
- }
374
- return parts;
375
- }
376
- function extractTextContent(content) {
377
- if (typeof content === "string") {
378
- return content;
379
- }
380
- if (!Array.isArray(content)) {
381
- return null;
382
- }
383
- const text = content.flatMap((item) => {
384
- if (!item || typeof item !== "object") {
385
- return [];
386
- }
387
- const textValue = item.text;
388
- return typeof textValue === "string" ? [textValue] : [];
389
- }).join("");
390
- return text.length > 0 ? text : null;
391
- }
392
-
393
- // src/compat/chat-model.ts
394
- function createOpenAICompatChatModel(client, modelId, providerId = "openai") {
395
- const provider = providerId;
396
- async function callOpenAIChatCompletionsApi(request, signal) {
397
- try {
398
- return await client.chat.completions.create(request, {
399
- signal
400
- });
401
- } catch (error) {
402
- throw wrapOpenAIError(error, provider);
403
- }
404
- }
405
- async function generateChat(options) {
406
- const request = createGenerateRequest(modelId, options);
407
- const response = await callOpenAIChatCompletionsApi(request, options.signal);
408
- return mapGenerateResponse(response);
409
- }
410
- async function streamChat(options) {
411
- const request = createStreamRequest(modelId, options);
412
- return createChatStream(
413
- async () => transformStream(
414
- await callOpenAIChatCompletionsApi(request, options.signal)
415
- ),
416
- { signal: options.signal }
417
- );
418
- }
419
- return {
420
- provider,
421
- modelId,
422
- capabilities: getOpenAIModelCapabilities(modelId),
423
- generate: generateChat,
424
- stream: streamChat,
425
- async generateObject(options) {
426
- const structuredOptions = createStructuredOutputOptions(options);
427
- const result = await generateChat(structuredOptions);
428
- const toolName = getStructuredOutputToolName(options);
429
- const object = extractStructuredObject(
430
- result,
431
- options.schema,
432
- provider,
433
- toolName
434
- );
435
- return {
436
- object,
437
- finishReason: result.finishReason,
438
- usage: result.usage
439
- };
440
- },
441
- async streamObject(options) {
442
- const structuredOptions = createStructuredOutputOptions(options);
443
- const stream = await streamChat(structuredOptions);
444
- const toolName = getStructuredOutputToolName(options);
445
- return createObjectStream(
446
- transformStructuredOutputStream(
447
- stream,
448
- options.schema,
449
- provider,
450
- toolName
451
- ),
452
- {
453
- signal: options.signal
454
- }
455
- );
456
- }
457
- };
458
- }
459
-
460
- // src/compat/provider.ts
461
10
  function createOpenAICompat(options = {}) {
462
- return createOpenAIProvider(options, createOpenAICompatChatModel);
11
+ return createOpenAIProvider(options, {
12
+ defaultApi: "chat-completions",
13
+ compatibility: true
14
+ });
463
15
  }
464
16
  function createOpenAICompatChatProvider(options = {}, providerId = "openai") {
465
17
  const client = options.client ?? new OpenAI({
@@ -467,9 +19,20 @@ function createOpenAICompatChatProvider(options = {}, providerId = "openai") {
467
19
  baseURL: options.baseURL
468
20
  });
469
21
  return {
470
- chatModel: (modelId) => createOpenAICompatChatModel(client, modelId, providerId)
22
+ chatModel: (modelId) => createOpenAIChatCompletionsModel(client, modelId, {
23
+ providerId,
24
+ compatibility: true
25
+ })
471
26
  };
472
27
  }
28
+
29
+ // src/compat/chat-model.ts
30
+ function createOpenAICompatChatModel(client, modelId, providerId = "openai") {
31
+ return createOpenAIChatCompletionsModel(client, modelId, {
32
+ providerId,
33
+ compatibility: true
34
+ });
35
+ }
473
36
  export {
474
37
  createOpenAICompat,
475
38
  createOpenAICompatChatModel,
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { O as OpenAIProvider$1, a as OpenAIProviderBaseOptions } from './provider-options-DzqvHoId.js';
2
- export { b as OpenAICompatGenerateProviderOptions, c as OpenAICompatRequestOptions, e as OpenAIEmbedProviderOptions, f as OpenAIImageProviderOptions, g as OpenAIResponsesGenerateProviderOptions, h as OpenAIResponsesProviderOptions, o as openaiCompatGenerateProviderOptionsSchema, d as openaiCompatProviderOptionsSchema, i as openaiEmbedProviderOptionsSchema, j as openaiImageProviderOptionsSchema, k as openaiResponsesGenerateProviderOptionsSchema, l as openaiResponsesProviderOptionsSchema } from './provider-options-DzqvHoId.js';
3
- import { ModelCapabilities } from '@core-ai/core-ai';
1
+ import { a as OpenAIProvider$1, b as OpenAIProviderBaseOptions } from './provider-options-CpxtGjm0.js';
2
+ export { O as OpenAIChatClient, f as OpenAIChatCompletionsModelOptions, g as OpenAIChatGenerateProviderOptions, h as OpenAIChatProvider, c as OpenAICompatGenerateProviderOptions, d as OpenAICompatRequestOptions, i as OpenAICompatibility, j as OpenAICompatibilityOptions, k as OpenAIEmbedProviderOptions, l as OpenAIImageProviderOptions, m as OpenAIModelCapabilities, n as OpenAIProviderFactoryOptions, p as OpenAIResponsesGenerateProviderOptions, q as OpenAIResponsesProviderOptions, r as OpenAIStructuredOutputMode, s as createOpenAIChatCompletionsModel, t as createOpenAIProvider, u as getOpenAIModelCapabilities, v as openaiChatGenerateProviderOptionsSchema, o as openaiCompatGenerateProviderOptionsSchema, e as openaiCompatProviderOptionsSchema, w as openaiEmbedProviderOptionsSchema, x as openaiImageProviderOptionsSchema, y as openaiResponsesGenerateProviderOptionsSchema, z as openaiResponsesProviderOptionsSchema } from './provider-options-CpxtGjm0.js';
4
3
  import 'openai';
4
+ import '@core-ai/core-ai';
5
5
  import 'zod';
6
6
 
7
7
  type OpenAIProviderOptions = OpenAIProviderBaseOptions;
@@ -12,7 +12,4 @@ type OpenAIReasoningMetadata = {
12
12
  encryptedContent?: string;
13
13
  };
14
14
 
15
- type OpenAIModelCapabilities = ModelCapabilities;
16
- declare function getOpenAIModelCapabilities(modelId: string): OpenAIModelCapabilities;
17
-
18
- export { type OpenAIModelCapabilities, type OpenAIProvider, type OpenAIProviderOptions, type OpenAIReasoningMetadata, createOpenAI, getOpenAIModelCapabilities };
15
+ export { type OpenAIProvider, OpenAIProviderBaseOptions, type OpenAIProviderOptions, type OpenAIReasoningMetadata, createOpenAI };