@effect/ai-openai-compat 4.0.2 → 4.0.4
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 +111 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +116 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +122 -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 +19 -0
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +2 -0
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiLanguageModel.d.ts +26 -5
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +110 -288
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +13 -0
- package/src/OpenAiLanguageModel.ts +160 -367
|
@@ -115,17 +115,14 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
115
115
|
options,
|
|
116
116
|
toolNameMapper
|
|
117
117
|
}) {
|
|
118
|
-
const include = new Set();
|
|
119
|
-
const capabilities = getModelCapabilities(config.model);
|
|
120
118
|
const messages = yield* prepareMessages({
|
|
121
119
|
config,
|
|
122
120
|
options,
|
|
123
|
-
capabilities,
|
|
124
|
-
include,
|
|
125
121
|
toolNameMapper
|
|
126
122
|
});
|
|
127
123
|
const {
|
|
128
124
|
toolChoice,
|
|
125
|
+
toolCodecs,
|
|
129
126
|
tools
|
|
130
127
|
} = yield* prepareTools({
|
|
131
128
|
config,
|
|
@@ -143,8 +140,6 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
143
140
|
} = config;
|
|
144
141
|
const request = {
|
|
145
142
|
...apiConfig,
|
|
146
|
-
input: messages,
|
|
147
|
-
include: include.size > 0 ? Array.from(include) : null,
|
|
148
143
|
text: {
|
|
149
144
|
verbosity: config.text?.verbosity ?? null,
|
|
150
145
|
format: responseFormat
|
|
@@ -156,14 +151,20 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
156
151
|
tool_choice: toolChoice
|
|
157
152
|
} : undefined)
|
|
158
153
|
};
|
|
159
|
-
return
|
|
154
|
+
return {
|
|
155
|
+
request: toChatCompletionsRequest(request, messages),
|
|
156
|
+
toolCodecs
|
|
157
|
+
};
|
|
160
158
|
});
|
|
161
159
|
return yield* LanguageModel.make({
|
|
162
160
|
codecTransformer: toCodecOpenAI,
|
|
163
161
|
generateText: Effect.fnUntraced(function* (options) {
|
|
164
162
|
const config = yield* makeConfig;
|
|
165
163
|
const toolNameMapper = new Tool.NameMapper(options.tools);
|
|
166
|
-
const
|
|
164
|
+
const {
|
|
165
|
+
request,
|
|
166
|
+
toolCodecs
|
|
167
|
+
} = yield* makeRequest({
|
|
167
168
|
config,
|
|
168
169
|
options,
|
|
169
170
|
toolNameMapper
|
|
@@ -175,13 +176,17 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
175
176
|
options,
|
|
176
177
|
rawResponse,
|
|
177
178
|
response,
|
|
178
|
-
toolNameMapper
|
|
179
|
+
toolNameMapper,
|
|
180
|
+
toolCodecs
|
|
179
181
|
});
|
|
180
182
|
}),
|
|
181
183
|
streamText: Effect.fnUntraced(function* (options) {
|
|
182
184
|
const config = yield* makeConfig;
|
|
183
185
|
const toolNameMapper = new Tool.NameMapper(options.tools);
|
|
184
|
-
const
|
|
186
|
+
const {
|
|
187
|
+
request,
|
|
188
|
+
toolCodecs
|
|
189
|
+
} = yield* makeRequest({
|
|
185
190
|
config,
|
|
186
191
|
options,
|
|
187
192
|
toolNameMapper
|
|
@@ -192,7 +197,8 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
192
197
|
options,
|
|
193
198
|
stream,
|
|
194
199
|
response,
|
|
195
|
-
toolNameMapper
|
|
200
|
+
toolNameMapper,
|
|
201
|
+
toolCodecs
|
|
196
202
|
});
|
|
197
203
|
}, (effect, options) => effect.pipe(Stream.unwrap, Stream.map(response => {
|
|
198
204
|
annotateStreamResponse(options.span, response);
|
|
@@ -247,18 +253,8 @@ const getSystemMessageMode = model => model.startsWith("o") || model.startsWith(
|
|
|
247
253
|
const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
248
254
|
config,
|
|
249
255
|
options,
|
|
250
|
-
capabilities,
|
|
251
|
-
include,
|
|
252
256
|
toolNameMapper
|
|
253
257
|
}) {
|
|
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
258
|
const messages = [];
|
|
263
259
|
for (const message of options.prompt.content) {
|
|
264
260
|
switch (message.role) {
|
|
@@ -340,149 +336,64 @@ const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
340
336
|
}
|
|
341
337
|
messages.push({
|
|
342
338
|
role: "user",
|
|
343
|
-
content
|
|
339
|
+
content: toChatMessageContent(content)
|
|
344
340
|
});
|
|
345
341
|
break;
|
|
346
342
|
}
|
|
347
343
|
case "assistant":
|
|
348
344
|
{
|
|
349
|
-
|
|
345
|
+
let content = null;
|
|
346
|
+
let reasoning = "";
|
|
347
|
+
let reasoningField;
|
|
348
|
+
const toolCalls = [];
|
|
350
349
|
for (const part of message.content) {
|
|
351
350
|
switch (part.type) {
|
|
352
351
|
case "text":
|
|
353
352
|
{
|
|
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
|
-
});
|
|
353
|
+
content = (content ?? "") + part.text;
|
|
379
354
|
break;
|
|
380
355
|
}
|
|
381
356
|
case "reasoning":
|
|
382
357
|
{
|
|
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
|
-
}
|
|
358
|
+
reasoning += part.text;
|
|
359
|
+
reasoningField ??= part.options.openai?.reasoningField ?? undefined;
|
|
431
360
|
break;
|
|
432
361
|
}
|
|
433
362
|
case "tool-call":
|
|
434
363
|
{
|
|
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
364
|
if (part.providerExecuted) {
|
|
448
365
|
break;
|
|
449
366
|
}
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
} : {})
|
|
367
|
+
const extraContent = getExtraContent(part);
|
|
368
|
+
toolCalls.push({
|
|
369
|
+
id: part.id,
|
|
370
|
+
type: "function",
|
|
371
|
+
function: {
|
|
372
|
+
name: toolNameMapper.getProviderName(part.name),
|
|
373
|
+
arguments: JSON.stringify(part.params)
|
|
374
|
+
},
|
|
375
|
+
...(Predicate.isNotNull(extraContent) ? {
|
|
376
|
+
extra_content: extraContent
|
|
377
|
+
} : undefined)
|
|
462
378
|
});
|
|
463
379
|
break;
|
|
464
380
|
}
|
|
465
|
-
// Assistant tool-result parts are always provider executed
|
|
466
|
-
case "tool-result":
|
|
467
|
-
{
|
|
468
|
-
// Skip execution denied results - these have no corresponding
|
|
469
|
-
// item in OpenAI's store
|
|
470
|
-
if (Predicate.hasProperty(part.result, "type") && part.result.type === "execution-denied") {
|
|
471
|
-
break;
|
|
472
|
-
}
|
|
473
|
-
if (hasConversation) {
|
|
474
|
-
break;
|
|
475
|
-
}
|
|
476
|
-
if (config.store === true) {
|
|
477
|
-
const id = getItemId(part) ?? part.id;
|
|
478
|
-
messages.push({
|
|
479
|
-
type: "item_reference",
|
|
480
|
-
id
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
381
|
}
|
|
485
382
|
}
|
|
383
|
+
// Text, reasoning and tool calls from one prompt message are sent as a
|
|
384
|
+
// single chat message so reasoning stays attached to its tool calls
|
|
385
|
+
if (content !== null || toolCalls.length > 0) {
|
|
386
|
+
messages.push({
|
|
387
|
+
role: "assistant",
|
|
388
|
+
content,
|
|
389
|
+
...(reasoning.length > 0 ? {
|
|
390
|
+
[reasoningField ?? "reasoning_content"]: reasoning
|
|
391
|
+
} : undefined),
|
|
392
|
+
...(toolCalls.length > 0 ? {
|
|
393
|
+
tool_calls: toolCalls
|
|
394
|
+
} : undefined)
|
|
395
|
+
});
|
|
396
|
+
}
|
|
486
397
|
break;
|
|
487
398
|
}
|
|
488
399
|
case "tool":
|
|
@@ -491,14 +402,10 @@ const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
491
402
|
if (part.type === "tool-approval-response") {
|
|
492
403
|
continue;
|
|
493
404
|
}
|
|
494
|
-
const status = getStatus(part);
|
|
495
405
|
messages.push({
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
...(Predicate.isNotNull(status) ? {
|
|
500
|
-
status
|
|
501
|
-
} : {})
|
|
406
|
+
role: "tool",
|
|
407
|
+
tool_call_id: part.id,
|
|
408
|
+
content: typeof part.result === "string" ? part.result : JSON.stringify(part.result)
|
|
502
409
|
});
|
|
503
410
|
}
|
|
504
411
|
break;
|
|
@@ -526,7 +433,8 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
526
433
|
options,
|
|
527
434
|
rawResponse,
|
|
528
435
|
response,
|
|
529
|
-
toolNameMapper
|
|
436
|
+
toolNameMapper,
|
|
437
|
+
toolCodecs
|
|
530
438
|
}) {
|
|
531
439
|
let hasToolCalls = false;
|
|
532
440
|
const parts = [];
|
|
@@ -541,11 +449,17 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
541
449
|
const choice = rawResponse.choices[0];
|
|
542
450
|
const message = choice?.message;
|
|
543
451
|
if (message !== undefined) {
|
|
544
|
-
const
|
|
545
|
-
|
|
452
|
+
const reasoningField = getResponseReasoningField(message);
|
|
453
|
+
const reasoning = reasoningField !== undefined ? message[reasoningField] : undefined;
|
|
454
|
+
if (reasoningField !== undefined && Predicate.isNotNullish(reasoning) && reasoning.length > 0) {
|
|
546
455
|
parts.push({
|
|
547
456
|
type: "reasoning",
|
|
548
|
-
text: reasoning
|
|
457
|
+
text: reasoning,
|
|
458
|
+
metadata: {
|
|
459
|
+
openai: {
|
|
460
|
+
reasoningField
|
|
461
|
+
}
|
|
462
|
+
}
|
|
549
463
|
});
|
|
550
464
|
}
|
|
551
465
|
if (message.content !== undefined && Predicate.isNotNull(message.content) && message.content.length > 0) {
|
|
@@ -570,7 +484,7 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
570
484
|
})
|
|
571
485
|
})
|
|
572
486
|
});
|
|
573
|
-
const params = yield* transformToolCallParams(options
|
|
487
|
+
const params = yield* transformToolCallParams(options, toolName, toolParams, toolCodecs);
|
|
574
488
|
hasToolCalls = true;
|
|
575
489
|
parts.push({
|
|
576
490
|
type: "tool-call",
|
|
@@ -579,7 +493,8 @@ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
579
493
|
params,
|
|
580
494
|
metadata: {
|
|
581
495
|
openai: {
|
|
582
|
-
...makeItemIdMetadata(toolCall.id)
|
|
496
|
+
...makeItemIdMetadata(toolCall.id),
|
|
497
|
+
...makeExtraContentMetadata(toolCall.extra_content ?? undefined)
|
|
583
498
|
}
|
|
584
499
|
}
|
|
585
500
|
});
|
|
@@ -607,7 +522,8 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
607
522
|
options,
|
|
608
523
|
stream,
|
|
609
524
|
response,
|
|
610
|
-
toolNameMapper
|
|
525
|
+
toolNameMapper,
|
|
526
|
+
toolCodecs
|
|
611
527
|
}) {
|
|
612
528
|
let serviceTier = undefined;
|
|
613
529
|
let usage = undefined;
|
|
@@ -657,7 +573,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
657
573
|
})
|
|
658
574
|
})
|
|
659
575
|
});
|
|
660
|
-
const params = yield* transformToolCallParams(options
|
|
576
|
+
const params = yield* transformToolCallParams(options, toolCall.name, parsedParams, toolCodecs);
|
|
661
577
|
parts.push({
|
|
662
578
|
type: "tool-params-end",
|
|
663
579
|
id: toolCall.id
|
|
@@ -669,7 +585,8 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
669
585
|
params,
|
|
670
586
|
metadata: {
|
|
671
587
|
openai: {
|
|
672
|
-
...makeItemIdMetadata(toolCall.id)
|
|
588
|
+
...makeItemIdMetadata(toolCall.id),
|
|
589
|
+
...makeExtraContentMetadata(toolCall.extraContent)
|
|
673
590
|
}
|
|
674
591
|
}
|
|
675
592
|
});
|
|
@@ -718,8 +635,9 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
718
635
|
if (Predicate.isUndefined(choice)) {
|
|
719
636
|
return parts;
|
|
720
637
|
}
|
|
721
|
-
const
|
|
722
|
-
|
|
638
|
+
const reasoningField = Predicate.isNotNullish(choice.delta) ? getResponseReasoningField(choice.delta) : undefined;
|
|
639
|
+
const reasoningDelta = reasoningField !== undefined ? choice.delta?.[reasoningField] : undefined;
|
|
640
|
+
if (reasoningField !== undefined && Predicate.isNotNullish(reasoningDelta) && reasoningDelta.length > 0) {
|
|
723
641
|
if (!reasoningStarted) {
|
|
724
642
|
reasoningStarted = true;
|
|
725
643
|
parts.push({
|
|
@@ -727,7 +645,8 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
727
645
|
id: reasoningId,
|
|
728
646
|
metadata: {
|
|
729
647
|
openai: {
|
|
730
|
-
...makeItemIdMetadata(reasoningId)
|
|
648
|
+
...makeItemIdMetadata(reasoningId),
|
|
649
|
+
reasoningField
|
|
731
650
|
}
|
|
732
651
|
}
|
|
733
652
|
});
|
|
@@ -738,7 +657,7 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
738
657
|
delta: reasoningDelta
|
|
739
658
|
});
|
|
740
659
|
}
|
|
741
|
-
if (choice.delta?.content
|
|
660
|
+
if (Predicate.isNotNullish(choice.delta?.content) && choice.delta.content.length > 0) {
|
|
742
661
|
if (reasoningStarted) {
|
|
743
662
|
reasoningStarted = false;
|
|
744
663
|
parts.push({
|
|
@@ -782,7 +701,8 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
782
701
|
activeToolCalls[toolIndex] = {
|
|
783
702
|
id: toolId,
|
|
784
703
|
name: toolName,
|
|
785
|
-
arguments: argumentsDelta
|
|
704
|
+
arguments: argumentsDelta,
|
|
705
|
+
extraContent: deltaTool.extra_content ?? undefined
|
|
786
706
|
};
|
|
787
707
|
parts.push({
|
|
788
708
|
type: "tool-params-start",
|
|
@@ -792,6 +712,9 @@ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
792
712
|
} else {
|
|
793
713
|
activeToolCall.name = toolName;
|
|
794
714
|
activeToolCall.arguments = `${activeToolCall.arguments}${argumentsDelta}`;
|
|
715
|
+
if (Predicate.isNotNullish(deltaTool.extra_content)) {
|
|
716
|
+
activeToolCall.extraContent = deltaTool.extra_content;
|
|
717
|
+
}
|
|
795
718
|
}
|
|
796
719
|
if (argumentsDelta.length > 0) {
|
|
797
720
|
parts.push({
|
|
@@ -895,30 +818,25 @@ const tryJsonSchema = (schema, method) => Effect.try({
|
|
|
895
818
|
}),
|
|
896
819
|
catch: error => unsupportedSchemaError(error, method)
|
|
897
820
|
});
|
|
898
|
-
const
|
|
899
|
-
|
|
900
|
-
transformer: toCodecOpenAI
|
|
901
|
-
}),
|
|
902
|
-
catch: error => unsupportedSchemaError(error, method)
|
|
903
|
-
});
|
|
904
|
-
const transformToolCallParams = /*#__PURE__*/Effect.fnUntraced(function* (tools, toolName, toolParams) {
|
|
905
|
-
const tool = tools.find(tool => tool.name === toolName);
|
|
821
|
+
const transformToolCallParams = /*#__PURE__*/Effect.fnUntraced(function* (options, toolName, toolParams, toolCodecs) {
|
|
822
|
+
const tool = options.tools.find(tool => tool.name === toolName);
|
|
906
823
|
if (Predicate.isUndefined(tool)) {
|
|
824
|
+
if (options.toolCallValidation === "deferred") {
|
|
825
|
+
return toolParams;
|
|
826
|
+
}
|
|
907
827
|
return yield* AiError.make({
|
|
908
828
|
module: "OpenAiLanguageModel",
|
|
909
829
|
method: "makeResponse",
|
|
910
830
|
reason: new AiError.ToolNotFoundError({
|
|
911
831
|
toolName,
|
|
912
|
-
availableTools: tools.map(tool => tool.name)
|
|
832
|
+
availableTools: options.tools.map(tool => tool.name)
|
|
913
833
|
})
|
|
914
834
|
});
|
|
915
835
|
}
|
|
916
836
|
if (Tool.isDynamic(tool) && tool.jsonSchema !== undefined) {
|
|
917
837
|
return toolParams;
|
|
918
838
|
}
|
|
919
|
-
const
|
|
920
|
-
codec
|
|
921
|
-
} = yield* tryCodecTransform(tool.parametersSchema, "makeResponse");
|
|
839
|
+
const codec = toolCodecs.get(tool) ?? (yield* tryCodecTransform(tool.parametersSchema, "makeResponse")).codec;
|
|
922
840
|
// Normalize valid parameters; leave invalid ones for Toolkit.
|
|
923
841
|
return yield* Schema.decodeEffect(codec)(toolParams).pipe(Effect.flatMap(decoded => Schema.encodeUnknownEffect(tool.parametersSchema)(decoded)), Effect.orElseSucceed(() => toolParams));
|
|
924
842
|
});
|
|
@@ -927,11 +845,13 @@ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
927
845
|
options,
|
|
928
846
|
toolNameMapper
|
|
929
847
|
}) {
|
|
848
|
+
const toolCodecs = new Map();
|
|
930
849
|
// Return immediately if no tools are in the toolkit
|
|
931
850
|
if (options.tools.length === 0) {
|
|
932
851
|
return {
|
|
933
852
|
tools: undefined,
|
|
934
|
-
toolChoice: undefined
|
|
853
|
+
toolChoice: undefined,
|
|
854
|
+
toolCodecs
|
|
935
855
|
};
|
|
936
856
|
}
|
|
937
857
|
const tools = [];
|
|
@@ -950,7 +870,15 @@ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
950
870
|
for (const tool of allowedTools) {
|
|
951
871
|
if (Tool.isUserDefined(tool) || Tool.isDynamic(tool)) {
|
|
952
872
|
const strict = Tool.getStrictMode(tool) ?? config.strictJsonSchema ?? true;
|
|
953
|
-
|
|
873
|
+
let parameters;
|
|
874
|
+
if (Tool.isDynamic(tool) && tool.jsonSchema !== undefined) {
|
|
875
|
+
parameters = tool.jsonSchema;
|
|
876
|
+
} else {
|
|
877
|
+
const prepared = yield* tryCodecTransform(tool.parametersSchema, "prepareTools");
|
|
878
|
+
parameters = prepared.jsonSchema;
|
|
879
|
+
// Reused to normalize this tool's calls in the response.
|
|
880
|
+
toolCodecs.set(tool, prepared.codec);
|
|
881
|
+
}
|
|
954
882
|
tools.push({
|
|
955
883
|
type: "function",
|
|
956
884
|
name: tool.name,
|
|
@@ -989,11 +917,11 @@ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
989
917
|
}
|
|
990
918
|
return {
|
|
991
919
|
tools,
|
|
992
|
-
toolChoice
|
|
920
|
+
toolChoice,
|
|
921
|
+
toolCodecs
|
|
993
922
|
};
|
|
994
923
|
});
|
|
995
|
-
const toChatCompletionsRequest = payload => {
|
|
996
|
-
const messages = toChatMessages(payload.input);
|
|
924
|
+
const toChatCompletionsRequest = (payload, messages) => {
|
|
997
925
|
const responseFormat = toChatResponseFormat(payload.text?.format);
|
|
998
926
|
const tools = payload.tools !== undefined ? payload.tools.map(toChatTool).filter(tool => tool !== undefined) : [];
|
|
999
927
|
const toolChoice = toChatToolChoice(payload.tool_choice);
|
|
@@ -1140,95 +1068,6 @@ const toChatTool = tool => {
|
|
|
1140
1068
|
}
|
|
1141
1069
|
return undefined;
|
|
1142
1070
|
};
|
|
1143
|
-
const toChatMessages = input => {
|
|
1144
|
-
if (Predicate.isUndefined(input)) {
|
|
1145
|
-
return [];
|
|
1146
|
-
}
|
|
1147
|
-
if (typeof input === "string") {
|
|
1148
|
-
return [{
|
|
1149
|
-
role: "user",
|
|
1150
|
-
content: input
|
|
1151
|
-
}];
|
|
1152
|
-
}
|
|
1153
|
-
const messages = [];
|
|
1154
|
-
for (const item of input) {
|
|
1155
|
-
if (Predicate.hasProperty(item, "type") && item.type === "function_call") {
|
|
1156
|
-
const previous = messages.at(-1);
|
|
1157
|
-
const toolCall = toChatToolCall(item);
|
|
1158
|
-
if (previous?.role === "assistant" && previous.tool_calls !== undefined) {
|
|
1159
|
-
messages[messages.length - 1] = {
|
|
1160
|
-
...previous,
|
|
1161
|
-
tool_calls: [...previous.tool_calls, toolCall]
|
|
1162
|
-
};
|
|
1163
|
-
} else {
|
|
1164
|
-
messages.push({
|
|
1165
|
-
role: "assistant",
|
|
1166
|
-
content: null,
|
|
1167
|
-
tool_calls: [toolCall]
|
|
1168
|
-
});
|
|
1169
|
-
}
|
|
1170
|
-
} else {
|
|
1171
|
-
messages.push(...toChatMessagesFromItem(item));
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
return messages;
|
|
1175
|
-
};
|
|
1176
|
-
const toChatMessagesFromItem = item => {
|
|
1177
|
-
if (Predicate.hasProperty(item, "type") && item.type === "message") {
|
|
1178
|
-
return [{
|
|
1179
|
-
role: item.role,
|
|
1180
|
-
content: toAssistantChatMessageContent(item.content)
|
|
1181
|
-
}];
|
|
1182
|
-
}
|
|
1183
|
-
if (Predicate.hasProperty(item, "role")) {
|
|
1184
|
-
return [{
|
|
1185
|
-
role: item.role,
|
|
1186
|
-
content: toChatMessageContent(item.content)
|
|
1187
|
-
}];
|
|
1188
|
-
}
|
|
1189
|
-
switch (item.type) {
|
|
1190
|
-
case "function_call":
|
|
1191
|
-
{
|
|
1192
|
-
return [{
|
|
1193
|
-
role: "assistant",
|
|
1194
|
-
content: null,
|
|
1195
|
-
tool_calls: [toChatToolCall(item)]
|
|
1196
|
-
}];
|
|
1197
|
-
}
|
|
1198
|
-
case "function_call_output":
|
|
1199
|
-
{
|
|
1200
|
-
return [{
|
|
1201
|
-
role: "tool",
|
|
1202
|
-
tool_call_id: item.call_id,
|
|
1203
|
-
content: stringifyJson(item.output)
|
|
1204
|
-
}];
|
|
1205
|
-
}
|
|
1206
|
-
default:
|
|
1207
|
-
{
|
|
1208
|
-
return [];
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
1211
|
-
};
|
|
1212
|
-
const toChatToolCall = item => ({
|
|
1213
|
-
id: item.call_id,
|
|
1214
|
-
type: "function",
|
|
1215
|
-
function: {
|
|
1216
|
-
name: item.name,
|
|
1217
|
-
arguments: item.arguments
|
|
1218
|
-
}
|
|
1219
|
-
});
|
|
1220
|
-
const toAssistantChatMessageContent = content => {
|
|
1221
|
-
let text = "";
|
|
1222
|
-
for (const part of content) {
|
|
1223
|
-
if (part.type === "output_text" && typeof part.text === "string") {
|
|
1224
|
-
text += part.text;
|
|
1225
|
-
}
|
|
1226
|
-
if (part.type === "refusal" && typeof part.refusal === "string") {
|
|
1227
|
-
text += part.refusal;
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
return text;
|
|
1231
|
-
};
|
|
1232
1071
|
const toChatMessageContent = content => {
|
|
1233
1072
|
if (typeof content === "string") {
|
|
1234
1073
|
return content;
|
|
@@ -1290,18 +1129,19 @@ const toChatMessageContent = content => {
|
|
|
1290
1129
|
}
|
|
1291
1130
|
return parts;
|
|
1292
1131
|
};
|
|
1293
|
-
const stringifyJson = value => typeof value === "string" ? value : JSON.stringify(value);
|
|
1294
1132
|
// =============================================================================
|
|
1295
1133
|
// Utilities
|
|
1296
1134
|
// =============================================================================
|
|
1297
1135
|
const isFileId = (data, config) => config.fileIdPrefixes != null && config.fileIdPrefixes.some(prefix => data.startsWith(prefix));
|
|
1298
|
-
const
|
|
1299
|
-
const
|
|
1300
|
-
const getEncryptedContent = part => part.options.openai?.encryptedContent ?? null;
|
|
1136
|
+
const getExtraContent = part => part.options.openai?.extraContent ?? null;
|
|
1137
|
+
const getResponseReasoningField = message => Predicate.isNotNullish(message.reasoning) ? "reasoning" : Predicate.isNotNullish(message.reasoning_content) ? "reasoning_content" : undefined;
|
|
1301
1138
|
const getImageDetail = part => part.options.openai?.imageDetail ?? "auto";
|
|
1302
1139
|
const makeItemIdMetadata = itemId => itemId !== undefined ? {
|
|
1303
1140
|
itemId
|
|
1304
1141
|
} : undefined;
|
|
1142
|
+
const makeExtraContentMetadata = extraContent => extraContent !== undefined ? {
|
|
1143
|
+
extraContent
|
|
1144
|
+
} : undefined;
|
|
1305
1145
|
const normalizeServiceTier = serviceTier => {
|
|
1306
1146
|
switch (serviceTier) {
|
|
1307
1147
|
case undefined:
|
|
@@ -1336,24 +1176,6 @@ const prepareResponseFormat = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
|
1336
1176
|
type: "text"
|
|
1337
1177
|
};
|
|
1338
1178
|
});
|
|
1339
|
-
const getModelCapabilities = modelId => {
|
|
1340
|
-
const supportsFlexProcessing = modelId.startsWith("o3") || modelId.startsWith("o4-mini") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat");
|
|
1341
|
-
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");
|
|
1342
|
-
// Use allowlist approach: only known reasoning models should use 'developer' role
|
|
1343
|
-
// This prevents issues with fine-tuned models, third-party models, and custom models
|
|
1344
|
-
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");
|
|
1345
|
-
// https://platform.openai.com/docs/guides/latest-model#gpt-5-1-parameter-compatibility
|
|
1346
|
-
// GPT-5.1 and GPT-5.2 support temperature, topP, logProbs when reasoningEffort is none
|
|
1347
|
-
const supportsNonReasoningParameters = modelId.startsWith("gpt-5.1") || modelId.startsWith("gpt-5.2");
|
|
1348
|
-
const systemMessageMode = isReasoningModel ? "developer" : "system";
|
|
1349
|
-
return {
|
|
1350
|
-
supportsFlexProcessing,
|
|
1351
|
-
supportsPriorityProcessing,
|
|
1352
|
-
isReasoningModel,
|
|
1353
|
-
systemMessageMode,
|
|
1354
|
-
supportsNonReasoningParameters
|
|
1355
|
-
};
|
|
1356
|
-
};
|
|
1357
1179
|
const getUsage = usage => {
|
|
1358
1180
|
if (Predicate.isNullish(usage)) {
|
|
1359
1181
|
return {
|