@effect/ai-openai-compat 4.0.3 → 4.0.5
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/AGENTS.md +24 -0
- package/CLAUDE.md +24 -0
- package/ai-docs/package.json +2 -2
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/dist/OpenAiClient.d.ts +9 -0
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +3 -2
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiError.d.ts +10 -10
- package/dist/OpenAiError.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.d.ts +33 -21
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +110 -301
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/internal/errors.js +5 -5
- package/dist/internal/errors.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +12 -2
- package/src/OpenAiError.ts +10 -10
- package/src/OpenAiLanguageModel.ts +160 -393
- package/src/internal/errors.ts +5 -5
|
@@ -14,6 +14,7 @@ import * as LanguageModel from "effect/ai/LanguageModel";
|
|
|
14
14
|
import * as AiModel from "effect/ai/Model";
|
|
15
15
|
import { toCodecOpenAI } from "effect/ai/OpenAiStructuredOutput";
|
|
16
16
|
import * as Tool from "effect/ai/Tool";
|
|
17
|
+
import * as Arr from "effect/Array";
|
|
17
18
|
import * as Context from "effect/Context";
|
|
18
19
|
import * as DateTime from "effect/DateTime";
|
|
19
20
|
import * as Effect from "effect/Effect";
|
|
@@ -115,17 +116,14 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
115
116
|
options,
|
|
116
117
|
toolNameMapper
|
|
117
118
|
}) {
|
|
118
|
-
const include = new Set();
|
|
119
|
-
const capabilities = getModelCapabilities(config.model);
|
|
120
119
|
const messages = yield* prepareMessages({
|
|
121
120
|
config,
|
|
122
121
|
options,
|
|
123
|
-
capabilities,
|
|
124
|
-
include,
|
|
125
122
|
toolNameMapper
|
|
126
123
|
});
|
|
127
124
|
const {
|
|
128
125
|
toolChoice,
|
|
126
|
+
toolCodecs,
|
|
129
127
|
tools
|
|
130
128
|
} = yield* prepareTools({
|
|
131
129
|
config,
|
|
@@ -143,8 +141,6 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
143
141
|
} = config;
|
|
144
142
|
const request = {
|
|
145
143
|
...apiConfig,
|
|
146
|
-
input: messages,
|
|
147
|
-
include: include.size > 0 ? Array.from(include) : null,
|
|
148
144
|
text: {
|
|
149
145
|
verbosity: config.text?.verbosity ?? null,
|
|
150
146
|
format: responseFormat
|
|
@@ -156,14 +152,20 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
156
152
|
tool_choice: toolChoice
|
|
157
153
|
} : undefined)
|
|
158
154
|
};
|
|
159
|
-
return
|
|
155
|
+
return {
|
|
156
|
+
request: toChatCompletionsRequest(request, messages),
|
|
157
|
+
toolCodecs
|
|
158
|
+
};
|
|
160
159
|
});
|
|
161
160
|
return yield* LanguageModel.make({
|
|
162
161
|
codecTransformer: toCodecOpenAI,
|
|
163
162
|
generateText: Effect.fnUntraced(function* (options) {
|
|
164
163
|
const config = yield* makeConfig;
|
|
165
164
|
const toolNameMapper = new Tool.NameMapper(options.tools);
|
|
166
|
-
const
|
|
165
|
+
const {
|
|
166
|
+
request,
|
|
167
|
+
toolCodecs
|
|
168
|
+
} = yield* makeRequest({
|
|
167
169
|
config,
|
|
168
170
|
options,
|
|
169
171
|
toolNameMapper
|
|
@@ -175,13 +177,17 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
175
177
|
options,
|
|
176
178
|
rawResponse,
|
|
177
179
|
response,
|
|
178
|
-
toolNameMapper
|
|
180
|
+
toolNameMapper,
|
|
181
|
+
toolCodecs
|
|
179
182
|
});
|
|
180
183
|
}),
|
|
181
184
|
streamText: Effect.fnUntraced(function* (options) {
|
|
182
185
|
const config = yield* makeConfig;
|
|
183
186
|
const toolNameMapper = new Tool.NameMapper(options.tools);
|
|
184
|
-
const
|
|
187
|
+
const {
|
|
188
|
+
request,
|
|
189
|
+
toolCodecs
|
|
190
|
+
} = yield* makeRequest({
|
|
185
191
|
config,
|
|
186
192
|
options,
|
|
187
193
|
toolNameMapper
|
|
@@ -192,7 +198,8 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
192
198
|
options,
|
|
193
199
|
stream,
|
|
194
200
|
response,
|
|
195
|
-
toolNameMapper
|
|
201
|
+
toolNameMapper,
|
|
202
|
+
toolCodecs
|
|
196
203
|
});
|
|
197
204
|
}, (effect, options) => effect.pipe(Stream.unwrap, Stream.map(response => {
|
|
198
205
|
annotateStreamResponse(options.span, response);
|
|
@@ -247,18 +254,8 @@ const getSystemMessageMode = model => model.startsWith("o") || model.startsWith(
|
|
|
247
254
|
const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
248
255
|
config,
|
|
249
256
|
options,
|
|
250
|
-
capabilities,
|
|
251
|
-
include,
|
|
252
257
|
toolNameMapper
|
|
253
258
|
}) {
|
|
254
|
-
const hasConversation = Predicate.isNotNullish(config.conversation);
|
|
255
|
-
// Handle Included Features
|
|
256
|
-
if (config.top_logprobs !== undefined) {
|
|
257
|
-
include.add("message.output_text.logprobs");
|
|
258
|
-
}
|
|
259
|
-
if (config.store === false && capabilities.isReasoningModel) {
|
|
260
|
-
include.add("reasoning.encrypted_content");
|
|
261
|
-
}
|
|
262
259
|
const messages = [];
|
|
263
260
|
for (const message of options.prompt.content) {
|
|
264
261
|
switch (message.role) {
|
|
@@ -340,153 +337,64 @@ const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
340
337
|
}
|
|
341
338
|
messages.push({
|
|
342
339
|
role: "user",
|
|
343
|
-
content
|
|
340
|
+
content: toChatMessageContent(content)
|
|
344
341
|
});
|
|
345
342
|
break;
|
|
346
343
|
}
|
|
347
344
|
case "assistant":
|
|
348
345
|
{
|
|
349
|
-
|
|
346
|
+
let content = null;
|
|
347
|
+
let reasoning = "";
|
|
348
|
+
let reasoningField;
|
|
349
|
+
const toolCalls = [];
|
|
350
350
|
for (const part of message.content) {
|
|
351
351
|
switch (part.type) {
|
|
352
352
|
case "text":
|
|
353
353
|
{
|
|
354
|
-
|
|
355
|
-
// When in conversation mode, skip items that already exist in the
|
|
356
|
-
// conversation context to avoid "Duplicate item found" errors
|
|
357
|
-
if (hasConversation && Predicate.isNotNull(id)) {
|
|
358
|
-
break;
|
|
359
|
-
}
|
|
360
|
-
if (config.store === true && Predicate.isNotNull(id)) {
|
|
361
|
-
messages.push({
|
|
362
|
-
type: "item_reference",
|
|
363
|
-
id
|
|
364
|
-
});
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
messages.push({
|
|
368
|
-
id: id,
|
|
369
|
-
type: "message",
|
|
370
|
-
role: "assistant",
|
|
371
|
-
status: part.options.openai?.status ?? "completed",
|
|
372
|
-
content: [{
|
|
373
|
-
type: "output_text",
|
|
374
|
-
text: part.text,
|
|
375
|
-
annotations: part.options.openai?.annotations ?? [],
|
|
376
|
-
logprobs: []
|
|
377
|
-
}]
|
|
378
|
-
});
|
|
354
|
+
content = (content ?? "") + part.text;
|
|
379
355
|
break;
|
|
380
356
|
}
|
|
381
357
|
case "reasoning":
|
|
382
358
|
{
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (hasConversation && Predicate.isNotNull(id)) {
|
|
386
|
-
break;
|
|
387
|
-
}
|
|
388
|
-
if (Predicate.isNotNull(id)) {
|
|
389
|
-
const message = reasoningMessages[id];
|
|
390
|
-
if (config.store === true) {
|
|
391
|
-
// Use item references to refer to reasoning (single reference)
|
|
392
|
-
// when the first part is encountered
|
|
393
|
-
if (Predicate.isUndefined(message)) {
|
|
394
|
-
messages.push({
|
|
395
|
-
type: "item_reference",
|
|
396
|
-
id
|
|
397
|
-
});
|
|
398
|
-
// Store unused reasoning message to mark its id as used
|
|
399
|
-
reasoningMessages[id] = {
|
|
400
|
-
type: "reasoning",
|
|
401
|
-
id,
|
|
402
|
-
summary: []
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
} else {
|
|
406
|
-
const summaryParts = [];
|
|
407
|
-
if (part.text.length > 0) {
|
|
408
|
-
summaryParts.push({
|
|
409
|
-
type: "summary_text",
|
|
410
|
-
text: part.text
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
if (Predicate.isUndefined(message)) {
|
|
414
|
-
reasoningMessages[id] = {
|
|
415
|
-
type: "reasoning",
|
|
416
|
-
id,
|
|
417
|
-
summary: summaryParts,
|
|
418
|
-
encrypted_content: encryptedContent ?? null
|
|
419
|
-
};
|
|
420
|
-
messages.push(reasoningMessages[id]);
|
|
421
|
-
} else {
|
|
422
|
-
message.summary.push(...summaryParts);
|
|
423
|
-
// Update encrypted content to enable setting it in the
|
|
424
|
-
// last summary part
|
|
425
|
-
if (Predicate.isNotNull(encryptedContent)) {
|
|
426
|
-
message.encrypted_content = encryptedContent;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
359
|
+
reasoning += part.text;
|
|
360
|
+
reasoningField ??= part.options["openai-compat"]?.reasoningField ?? undefined;
|
|
431
361
|
break;
|
|
432
362
|
}
|
|
433
363
|
case "tool-call":
|
|
434
364
|
{
|
|
435
|
-
const id = getItemId(part);
|
|
436
|
-
const status = getStatus(part);
|
|
437
|
-
if (hasConversation && Predicate.isNotNull(id)) {
|
|
438
|
-
break;
|
|
439
|
-
}
|
|
440
|
-
if (config.store && Predicate.isNotNull(id)) {
|
|
441
|
-
messages.push({
|
|
442
|
-
type: "item_reference",
|
|
443
|
-
id
|
|
444
|
-
});
|
|
445
|
-
break;
|
|
446
|
-
}
|
|
447
365
|
if (part.providerExecuted) {
|
|
448
366
|
break;
|
|
449
367
|
}
|
|
450
|
-
const toolName = toolNameMapper.getProviderName(part.name);
|
|
451
368
|
const extraContent = getExtraContent(part);
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
369
|
+
toolCalls.push({
|
|
370
|
+
id: part.id,
|
|
371
|
+
type: "function",
|
|
372
|
+
function: {
|
|
373
|
+
name: toolNameMapper.getProviderName(part.name),
|
|
374
|
+
arguments: JSON.stringify(part.params)
|
|
375
|
+
},
|
|
457
376
|
...(Predicate.isNotNull(extraContent) ? {
|
|
458
377
|
extra_content: extraContent
|
|
459
|
-
} :
|
|
460
|
-
...(Predicate.isNotNull(id) ? {
|
|
461
|
-
id
|
|
462
|
-
} : {}),
|
|
463
|
-
...(Predicate.isNotNull(status) ? {
|
|
464
|
-
status
|
|
465
|
-
} : {})
|
|
378
|
+
} : undefined)
|
|
466
379
|
});
|
|
467
380
|
break;
|
|
468
381
|
}
|
|
469
|
-
// Assistant tool-result parts are always provider executed
|
|
470
|
-
case "tool-result":
|
|
471
|
-
{
|
|
472
|
-
// Skip execution denied results - these have no corresponding
|
|
473
|
-
// item in OpenAI's store
|
|
474
|
-
if (Predicate.hasProperty(part.result, "type") && part.result.type === "execution-denied") {
|
|
475
|
-
break;
|
|
476
|
-
}
|
|
477
|
-
if (hasConversation) {
|
|
478
|
-
break;
|
|
479
|
-
}
|
|
480
|
-
if (config.store === true) {
|
|
481
|
-
const id = getItemId(part) ?? part.id;
|
|
482
|
-
messages.push({
|
|
483
|
-
type: "item_reference",
|
|
484
|
-
id
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
382
|
}
|
|
489
383
|
}
|
|
384
|
+
// Text, reasoning and tool calls from one prompt message are sent as a
|
|
385
|
+
// single chat message so reasoning stays attached to its tool calls
|
|
386
|
+
if (content !== null || toolCalls.length > 0) {
|
|
387
|
+
messages.push({
|
|
388
|
+
role: "assistant",
|
|
389
|
+
content,
|
|
390
|
+
...(reasoning.length > 0 ? {
|
|
391
|
+
[reasoningField ?? "reasoning_content"]: reasoning
|
|
392
|
+
} : undefined),
|
|
393
|
+
...(toolCalls.length > 0 ? {
|
|
394
|
+
tool_calls: toolCalls
|
|
395
|
+
} : undefined)
|
|
396
|
+
});
|
|
397
|
+
}
|
|
490
398
|
break;
|
|
491
399
|
}
|
|
492
400
|
case "tool":
|
|
@@ -495,14 +403,10 @@ const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
495
403
|
if (part.type === "tool-approval-response") {
|
|
496
404
|
continue;
|
|
497
405
|
}
|
|
498
|
-
const status = getStatus(part);
|
|
499
406
|
messages.push({
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
...(Predicate.isNotNull(status) ? {
|
|
504
|
-
status
|
|
505
|
-
} : {})
|
|
407
|
+
role: "tool",
|
|
408
|
+
tool_call_id: part.id,
|
|
409
|
+
content: typeof part.result === "string" ? part.result : JSON.stringify(part.result)
|
|
506
410
|
});
|
|
507
411
|
}
|
|
508
412
|
break;
|
|
@@ -530,7 +434,8 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
530
434
|
options,
|
|
531
435
|
rawResponse,
|
|
532
436
|
response,
|
|
533
|
-
toolNameMapper
|
|
437
|
+
toolNameMapper,
|
|
438
|
+
toolCodecs
|
|
534
439
|
}) {
|
|
535
440
|
let hasToolCalls = false;
|
|
536
441
|
const parts = [];
|
|
@@ -545,11 +450,17 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
545
450
|
const choice = rawResponse.choices[0];
|
|
546
451
|
const message = choice?.message;
|
|
547
452
|
if (message !== undefined) {
|
|
548
|
-
const
|
|
549
|
-
|
|
453
|
+
const reasoningField = getResponseReasoningField(message);
|
|
454
|
+
const reasoning = reasoningField !== undefined ? message[reasoningField] : undefined;
|
|
455
|
+
if (reasoningField !== undefined && Predicate.isNotNullish(reasoning) && reasoning.length > 0) {
|
|
550
456
|
parts.push({
|
|
551
457
|
type: "reasoning",
|
|
552
|
-
text: reasoning
|
|
458
|
+
text: reasoning,
|
|
459
|
+
metadata: {
|
|
460
|
+
"openai-compat": {
|
|
461
|
+
reasoningField
|
|
462
|
+
}
|
|
463
|
+
}
|
|
553
464
|
});
|
|
554
465
|
}
|
|
555
466
|
if (message.content !== undefined && Predicate.isNotNull(message.content) && message.content.length > 0) {
|
|
@@ -574,7 +485,7 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
574
485
|
})
|
|
575
486
|
})
|
|
576
487
|
});
|
|
577
|
-
const params = yield* transformToolCallParams(options
|
|
488
|
+
const params = yield* transformToolCallParams(options, toolName, toolParams, toolCodecs);
|
|
578
489
|
hasToolCalls = true;
|
|
579
490
|
parts.push({
|
|
580
491
|
type: "tool-call",
|
|
@@ -582,7 +493,7 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
582
493
|
name: toolName,
|
|
583
494
|
params,
|
|
584
495
|
metadata: {
|
|
585
|
-
openai: {
|
|
496
|
+
"openai-compat": {
|
|
586
497
|
...makeItemIdMetadata(toolCall.id),
|
|
587
498
|
...makeExtraContentMetadata(toolCall.extra_content ?? undefined)
|
|
588
499
|
}
|
|
@@ -600,7 +511,7 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
600
511
|
response: buildHttpResponseDetails(response),
|
|
601
512
|
...(serviceTier !== undefined && {
|
|
602
513
|
metadata: {
|
|
603
|
-
openai: {
|
|
514
|
+
"openai-compat": {
|
|
604
515
|
serviceTier
|
|
605
516
|
}
|
|
606
517
|
}
|
|
@@ -612,7 +523,8 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
612
523
|
options,
|
|
613
524
|
stream,
|
|
614
525
|
response,
|
|
615
|
-
toolNameMapper
|
|
526
|
+
toolNameMapper,
|
|
527
|
+
toolCodecs
|
|
616
528
|
}) {
|
|
617
529
|
let serviceTier = undefined;
|
|
618
530
|
let usage = undefined;
|
|
@@ -632,7 +544,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
632
544
|
type: "reasoning-end",
|
|
633
545
|
id: reasoningId,
|
|
634
546
|
metadata: {
|
|
635
|
-
openai: {
|
|
547
|
+
"openai-compat": {
|
|
636
548
|
...makeItemIdMetadata(reasoningId)
|
|
637
549
|
}
|
|
638
550
|
}
|
|
@@ -643,7 +555,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
643
555
|
type: "text-end",
|
|
644
556
|
id: textId,
|
|
645
557
|
metadata: {
|
|
646
|
-
openai: {
|
|
558
|
+
"openai-compat": {
|
|
647
559
|
...makeItemIdMetadata(textId)
|
|
648
560
|
}
|
|
649
561
|
}
|
|
@@ -662,7 +574,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
662
574
|
})
|
|
663
575
|
})
|
|
664
576
|
});
|
|
665
|
-
const params = yield* transformToolCallParams(options
|
|
577
|
+
const params = yield* transformToolCallParams(options, toolCall.name, parsedParams, toolCodecs);
|
|
666
578
|
parts.push({
|
|
667
579
|
type: "tool-params-end",
|
|
668
580
|
id: toolCall.id
|
|
@@ -673,7 +585,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
673
585
|
name: toolCall.name,
|
|
674
586
|
params,
|
|
675
587
|
metadata: {
|
|
676
|
-
openai: {
|
|
588
|
+
"openai-compat": {
|
|
677
589
|
...makeItemIdMetadata(toolCall.id),
|
|
678
590
|
...makeExtraContentMetadata(toolCall.extraContent)
|
|
679
591
|
}
|
|
@@ -689,7 +601,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
689
601
|
response: buildHttpResponseDetails(response),
|
|
690
602
|
...(normalizedServiceTier !== undefined ? {
|
|
691
603
|
metadata: {
|
|
692
|
-
openai: {
|
|
604
|
+
"openai-compat": {
|
|
693
605
|
serviceTier: normalizedServiceTier
|
|
694
606
|
}
|
|
695
607
|
}
|
|
@@ -724,16 +636,18 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
724
636
|
if (Predicate.isUndefined(choice)) {
|
|
725
637
|
return parts;
|
|
726
638
|
}
|
|
727
|
-
const
|
|
728
|
-
|
|
639
|
+
const reasoningField = Predicate.isNotNullish(choice.delta) ? getResponseReasoningField(choice.delta) : undefined;
|
|
640
|
+
const reasoningDelta = reasoningField !== undefined ? choice.delta?.[reasoningField] : undefined;
|
|
641
|
+
if (reasoningField !== undefined && Predicate.isNotNullish(reasoningDelta) && reasoningDelta.length > 0) {
|
|
729
642
|
if (!reasoningStarted) {
|
|
730
643
|
reasoningStarted = true;
|
|
731
644
|
parts.push({
|
|
732
645
|
type: "reasoning-start",
|
|
733
646
|
id: reasoningId,
|
|
734
647
|
metadata: {
|
|
735
|
-
openai: {
|
|
736
|
-
...makeItemIdMetadata(reasoningId)
|
|
648
|
+
"openai-compat": {
|
|
649
|
+
...makeItemIdMetadata(reasoningId),
|
|
650
|
+
reasoningField
|
|
737
651
|
}
|
|
738
652
|
}
|
|
739
653
|
});
|
|
@@ -751,7 +665,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
751
665
|
type: "reasoning-end",
|
|
752
666
|
id: reasoningId,
|
|
753
667
|
metadata: {
|
|
754
|
-
openai: {
|
|
668
|
+
"openai-compat": {
|
|
755
669
|
...makeItemIdMetadata(reasoningId)
|
|
756
670
|
}
|
|
757
671
|
}
|
|
@@ -763,7 +677,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
763
677
|
type: "text-start",
|
|
764
678
|
id: textId,
|
|
765
679
|
metadata: {
|
|
766
|
-
openai: {
|
|
680
|
+
"openai-compat": {
|
|
767
681
|
...makeItemIdMetadata(textId)
|
|
768
682
|
}
|
|
769
683
|
}
|
|
@@ -816,7 +730,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
816
730
|
finishReason = choice.finish_reason;
|
|
817
731
|
}
|
|
818
732
|
return parts;
|
|
819
|
-
})), Stream.
|
|
733
|
+
})), Stream.filter(Arr.isArrayNonEmpty), Stream.flattenArray);
|
|
820
734
|
});
|
|
821
735
|
// =============================================================================
|
|
822
736
|
// Telemetry
|
|
@@ -871,7 +785,7 @@ const annotateStreamResponse = (span, part) => {
|
|
|
871
785
|
});
|
|
872
786
|
}
|
|
873
787
|
if (part.type === "finish") {
|
|
874
|
-
const serviceTier = part.metadata?.openai?.serviceTier;
|
|
788
|
+
const serviceTier = part.metadata?.["openai-compat"]?.serviceTier;
|
|
875
789
|
addGenAIAnnotations(span, {
|
|
876
790
|
response: {
|
|
877
791
|
finishReasons: [part.reason]
|
|
@@ -905,30 +819,25 @@ const tryJsonSchema = (schema, method) => Effect.try({
|
|
|
905
819
|
}),
|
|
906
820
|
catch: error => unsupportedSchemaError(error, method)
|
|
907
821
|
});
|
|
908
|
-
const
|
|
909
|
-
|
|
910
|
-
transformer: toCodecOpenAI
|
|
911
|
-
}),
|
|
912
|
-
catch: error => unsupportedSchemaError(error, method)
|
|
913
|
-
});
|
|
914
|
-
const transformToolCallParams = /*#__PURE__*/Effect.fnUntraced(function* (tools, toolName, toolParams) {
|
|
915
|
-
const tool = tools.find(tool => tool.name === toolName);
|
|
822
|
+
const transformToolCallParams = /*#__PURE__*/Effect.fnUntraced(function* (options, toolName, toolParams, toolCodecs) {
|
|
823
|
+
const tool = options.tools.find(tool => tool.name === toolName);
|
|
916
824
|
if (Predicate.isUndefined(tool)) {
|
|
825
|
+
if (options.toolCallValidation === "deferred") {
|
|
826
|
+
return toolParams;
|
|
827
|
+
}
|
|
917
828
|
return yield* AiError.make({
|
|
918
829
|
module: "OpenAiLanguageModel",
|
|
919
830
|
method: "makeResponse",
|
|
920
831
|
reason: new AiError.ToolNotFoundError({
|
|
921
832
|
toolName,
|
|
922
|
-
availableTools: tools.map(tool => tool.name)
|
|
833
|
+
availableTools: options.tools.map(tool => tool.name)
|
|
923
834
|
})
|
|
924
835
|
});
|
|
925
836
|
}
|
|
926
837
|
if (Tool.isDynamic(tool) && tool.jsonSchema !== undefined) {
|
|
927
838
|
return toolParams;
|
|
928
839
|
}
|
|
929
|
-
const
|
|
930
|
-
codec
|
|
931
|
-
} = yield* tryCodecTransform(tool.parametersSchema, "makeResponse");
|
|
840
|
+
const codec = toolCodecs.get(tool) ?? (yield* tryCodecTransform(tool.parametersSchema, "makeResponse")).codec;
|
|
932
841
|
// Normalize valid parameters; leave invalid ones for Toolkit.
|
|
933
842
|
return yield* Schema.decodeEffect(codec)(toolParams).pipe(Effect.flatMap(decoded => Schema.encodeUnknownEffect(tool.parametersSchema)(decoded)), Effect.orElseSucceed(() => toolParams));
|
|
934
843
|
});
|
|
@@ -937,11 +846,13 @@ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
937
846
|
options,
|
|
938
847
|
toolNameMapper
|
|
939
848
|
}) {
|
|
849
|
+
const toolCodecs = new Map();
|
|
940
850
|
// Return immediately if no tools are in the toolkit
|
|
941
851
|
if (options.tools.length === 0) {
|
|
942
852
|
return {
|
|
943
853
|
tools: undefined,
|
|
944
|
-
toolChoice: undefined
|
|
854
|
+
toolChoice: undefined,
|
|
855
|
+
toolCodecs
|
|
945
856
|
};
|
|
946
857
|
}
|
|
947
858
|
const tools = [];
|
|
@@ -960,7 +871,15 @@ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
960
871
|
for (const tool of allowedTools) {
|
|
961
872
|
if (Tool.isUserDefined(tool) || Tool.isDynamic(tool)) {
|
|
962
873
|
const strict = Tool.getStrictMode(tool) ?? config.strictJsonSchema ?? true;
|
|
963
|
-
|
|
874
|
+
let parameters;
|
|
875
|
+
if (Tool.isDynamic(tool) && tool.jsonSchema !== undefined) {
|
|
876
|
+
parameters = tool.jsonSchema;
|
|
877
|
+
} else {
|
|
878
|
+
const prepared = yield* tryCodecTransform(tool.parametersSchema, "prepareTools");
|
|
879
|
+
parameters = prepared.jsonSchema;
|
|
880
|
+
// Reused to normalize this tool's calls in the response.
|
|
881
|
+
toolCodecs.set(tool, prepared.codec);
|
|
882
|
+
}
|
|
964
883
|
tools.push({
|
|
965
884
|
type: "function",
|
|
966
885
|
name: tool.name,
|
|
@@ -999,11 +918,11 @@ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
999
918
|
}
|
|
1000
919
|
return {
|
|
1001
920
|
tools,
|
|
1002
|
-
toolChoice
|
|
921
|
+
toolChoice,
|
|
922
|
+
toolCodecs
|
|
1003
923
|
};
|
|
1004
924
|
});
|
|
1005
|
-
const toChatCompletionsRequest = payload => {
|
|
1006
|
-
const messages = toChatMessages(payload.input);
|
|
925
|
+
const toChatCompletionsRequest = (payload, messages) => {
|
|
1007
926
|
const responseFormat = toChatResponseFormat(payload.text?.format);
|
|
1008
927
|
const tools = payload.tools !== undefined ? payload.tools.map(toChatTool).filter(tool => tool !== undefined) : [];
|
|
1009
928
|
const toolChoice = toChatToolChoice(payload.tool_choice);
|
|
@@ -1150,102 +1069,13 @@ const toChatTool = tool => {
|
|
|
1150
1069
|
}
|
|
1151
1070
|
return undefined;
|
|
1152
1071
|
};
|
|
1153
|
-
const toChatMessages = input => {
|
|
1154
|
-
if (Predicate.isUndefined(input)) {
|
|
1155
|
-
return [];
|
|
1156
|
-
}
|
|
1157
|
-
if (typeof input === "string") {
|
|
1158
|
-
return [{
|
|
1159
|
-
role: "user",
|
|
1160
|
-
content: input
|
|
1161
|
-
}];
|
|
1162
|
-
}
|
|
1163
|
-
const messages = [];
|
|
1164
|
-
for (const item of input) {
|
|
1165
|
-
if (Predicate.hasProperty(item, "type") && item.type === "function_call") {
|
|
1166
|
-
const previous = messages.at(-1);
|
|
1167
|
-
const toolCall = toChatToolCall(item);
|
|
1168
|
-
if (previous?.role === "assistant" && previous.tool_calls !== undefined) {
|
|
1169
|
-
messages[messages.length - 1] = {
|
|
1170
|
-
...previous,
|
|
1171
|
-
tool_calls: [...previous.tool_calls, toolCall]
|
|
1172
|
-
};
|
|
1173
|
-
} else {
|
|
1174
|
-
messages.push({
|
|
1175
|
-
role: "assistant",
|
|
1176
|
-
content: null,
|
|
1177
|
-
tool_calls: [toolCall]
|
|
1178
|
-
});
|
|
1179
|
-
}
|
|
1180
|
-
} else {
|
|
1181
|
-
messages.push(...toChatMessagesFromItem(item));
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
return messages;
|
|
1185
|
-
};
|
|
1186
|
-
const toChatMessagesFromItem = item => {
|
|
1187
|
-
if (Predicate.hasProperty(item, "type") && item.type === "message") {
|
|
1188
|
-
return [{
|
|
1189
|
-
role: item.role,
|
|
1190
|
-
content: toAssistantChatMessageContent(item.content)
|
|
1191
|
-
}];
|
|
1192
|
-
}
|
|
1193
|
-
if (Predicate.hasProperty(item, "role")) {
|
|
1194
|
-
return [{
|
|
1195
|
-
role: item.role,
|
|
1196
|
-
content: toChatMessageContent(item.content)
|
|
1197
|
-
}];
|
|
1198
|
-
}
|
|
1199
|
-
switch (item.type) {
|
|
1200
|
-
case "function_call":
|
|
1201
|
-
{
|
|
1202
|
-
return [{
|
|
1203
|
-
role: "assistant",
|
|
1204
|
-
content: null,
|
|
1205
|
-
tool_calls: [toChatToolCall(item)]
|
|
1206
|
-
}];
|
|
1207
|
-
}
|
|
1208
|
-
case "function_call_output":
|
|
1209
|
-
{
|
|
1210
|
-
return [{
|
|
1211
|
-
role: "tool",
|
|
1212
|
-
tool_call_id: item.call_id,
|
|
1213
|
-
content: stringifyJson(item.output)
|
|
1214
|
-
}];
|
|
1215
|
-
}
|
|
1216
|
-
default:
|
|
1217
|
-
{
|
|
1218
|
-
return [];
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
};
|
|
1222
|
-
const toChatToolCall = item => ({
|
|
1223
|
-
id: item.call_id,
|
|
1224
|
-
type: "function",
|
|
1225
|
-
function: {
|
|
1226
|
-
name: item.name,
|
|
1227
|
-
arguments: item.arguments
|
|
1228
|
-
},
|
|
1229
|
-
...(item.extra_content !== undefined ? {
|
|
1230
|
-
extra_content: item.extra_content
|
|
1231
|
-
} : undefined)
|
|
1232
|
-
});
|
|
1233
|
-
const toAssistantChatMessageContent = content => {
|
|
1234
|
-
let text = "";
|
|
1235
|
-
for (const part of content) {
|
|
1236
|
-
if (part.type === "output_text" && typeof part.text === "string") {
|
|
1237
|
-
text += part.text;
|
|
1238
|
-
}
|
|
1239
|
-
if (part.type === "refusal" && typeof part.refusal === "string") {
|
|
1240
|
-
text += part.refusal;
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1243
|
-
return text;
|
|
1244
|
-
};
|
|
1245
1072
|
const toChatMessageContent = content => {
|
|
1246
1073
|
if (typeof content === "string") {
|
|
1247
1074
|
return content;
|
|
1248
1075
|
}
|
|
1076
|
+
if (content.length === 1 && content[0].type === "input_text") {
|
|
1077
|
+
return content[0].text;
|
|
1078
|
+
}
|
|
1249
1079
|
const parts = [];
|
|
1250
1080
|
for (const part of content) {
|
|
1251
1081
|
switch (part.type) {
|
|
@@ -1303,16 +1133,13 @@ const toChatMessageContent = content => {
|
|
|
1303
1133
|
}
|
|
1304
1134
|
return parts;
|
|
1305
1135
|
};
|
|
1306
|
-
const stringifyJson = value => typeof value === "string" ? value : JSON.stringify(value);
|
|
1307
1136
|
// =============================================================================
|
|
1308
1137
|
// Utilities
|
|
1309
1138
|
// =============================================================================
|
|
1310
1139
|
const isFileId = (data, config) => config.fileIdPrefixes != null && config.fileIdPrefixes.some(prefix => data.startsWith(prefix));
|
|
1311
|
-
const
|
|
1312
|
-
const
|
|
1313
|
-
const
|
|
1314
|
-
const getExtraContent = part => part.options.openai?.extraContent ?? null;
|
|
1315
|
-
const getImageDetail = part => part.options.openai?.imageDetail ?? "auto";
|
|
1140
|
+
const getExtraContent = part => part.options["openai-compat"]?.extraContent ?? null;
|
|
1141
|
+
const getResponseReasoningField = message => Predicate.isNotNullish(message.reasoning) ? "reasoning" : Predicate.isNotNullish(message.reasoning_content) ? "reasoning_content" : undefined;
|
|
1142
|
+
const getImageDetail = part => part.options["openai-compat"]?.imageDetail ?? "auto";
|
|
1316
1143
|
const makeItemIdMetadata = itemId => itemId !== undefined ? {
|
|
1317
1144
|
itemId
|
|
1318
1145
|
} : undefined;
|
|
@@ -1353,24 +1180,6 @@ const prepareResponseFormat = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
1353
1180
|
type: "text"
|
|
1354
1181
|
};
|
|
1355
1182
|
});
|
|
1356
|
-
const getModelCapabilities = modelId => {
|
|
1357
|
-
const supportsFlexProcessing = modelId.startsWith("o3") || modelId.startsWith("o4-mini") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat");
|
|
1358
|
-
const supportsPriorityProcessing = modelId.startsWith("gpt-4") || modelId.startsWith("gpt-5-mini") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-nano") && !modelId.startsWith("gpt-5-chat") || modelId.startsWith("o3") || modelId.startsWith("o4-mini");
|
|
1359
|
-
// Use allowlist approach: only known reasoning models should use 'developer' role
|
|
1360
|
-
// This prevents issues with fine-tuned models, third-party models, and custom models
|
|
1361
|
-
const isReasoningModel = modelId.startsWith("o1") || modelId.startsWith("o3") || modelId.startsWith("o4-mini") || modelId.startsWith("codex-mini") || modelId.startsWith("computer-use-preview") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat");
|
|
1362
|
-
// https://platform.openai.com/docs/guides/latest-model#gpt-5-1-parameter-compatibility
|
|
1363
|
-
// GPT-5.1 and GPT-5.2 support temperature, topP, logProbs when reasoningEffort is none
|
|
1364
|
-
const supportsNonReasoningParameters = modelId.startsWith("gpt-5.1") || modelId.startsWith("gpt-5.2");
|
|
1365
|
-
const systemMessageMode = isReasoningModel ? "developer" : "system";
|
|
1366
|
-
return {
|
|
1367
|
-
supportsFlexProcessing,
|
|
1368
|
-
supportsPriorityProcessing,
|
|
1369
|
-
isReasoningModel,
|
|
1370
|
-
systemMessageMode,
|
|
1371
|
-
supportsNonReasoningParameters
|
|
1372
|
-
};
|
|
1373
|
-
};
|
|
1374
1183
|
const getUsage = usage => {
|
|
1375
1184
|
if (Predicate.isNullish(usage)) {
|
|
1376
1185
|
return {
|