@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
|
@@ -16,6 +16,7 @@ import { toCodecOpenAI } from "effect/ai/OpenAiStructuredOutput"
|
|
|
16
16
|
import type * as Prompt from "effect/ai/Prompt"
|
|
17
17
|
import type * as Response from "effect/ai/Response"
|
|
18
18
|
import * as Tool from "effect/ai/Tool"
|
|
19
|
+
import * as Arr from "effect/Array"
|
|
19
20
|
import * as Context from "effect/Context"
|
|
20
21
|
import * as DateTime from "effect/DateTime"
|
|
21
22
|
import * as Effect from "effect/Effect"
|
|
@@ -32,23 +33,20 @@ import * as Schema from "effect/Schema"
|
|
|
32
33
|
import * as AST from "effect/SchemaAST"
|
|
33
34
|
import * as Stream from "effect/Stream"
|
|
34
35
|
import type { Span } from "effect/Tracer"
|
|
35
|
-
import type {
|
|
36
|
+
import type { Simplify } from "effect/Types"
|
|
36
37
|
import * as InternalUtilities from "./internal/utilities.ts"
|
|
37
38
|
import {
|
|
38
39
|
type Annotation,
|
|
39
40
|
type ChatCompletionContentPart,
|
|
41
|
+
type ChatCompletionRequestMessage,
|
|
40
42
|
type ChatCompletionRequestToolCall,
|
|
41
43
|
type CreateResponse,
|
|
42
44
|
type CreateResponse200,
|
|
43
45
|
type CreateResponse200Sse,
|
|
44
46
|
type CreateResponseRequestJson,
|
|
45
|
-
type IncludeEnum,
|
|
46
47
|
type InputContent,
|
|
47
|
-
type InputItem,
|
|
48
48
|
type MessageStatus,
|
|
49
49
|
OpenAiClient,
|
|
50
|
-
type ReasoningItem,
|
|
51
|
-
type SummaryTextContent,
|
|
52
50
|
type TextResponseFormatConfiguration,
|
|
53
51
|
type Tool as OpenAiClientTool,
|
|
54
52
|
type UnknownChatCompletionEvent
|
|
@@ -66,7 +64,7 @@ type ImageDetail = "auto" | "low" | "high"
|
|
|
66
64
|
|
|
67
65
|
type ConfigOptions = Simplify<
|
|
68
66
|
& Partial<
|
|
69
|
-
Omit<CreateResponse, "input" | "tools" | "tool_choice" | "stream" | "text">
|
|
67
|
+
Omit<CreateResponse, "input" | "tools" | "tool_choice" | "stream" | "text" | "store" | "conversation">
|
|
70
68
|
>
|
|
71
69
|
& {
|
|
72
70
|
/**
|
|
@@ -137,7 +135,7 @@ declare module "effect/ai/Prompt" {
|
|
|
137
135
|
/**
|
|
138
136
|
* Provider-specific file options for OpenAI-compatible APIs.
|
|
139
137
|
*/
|
|
140
|
-
readonly openai?: {
|
|
138
|
+
readonly "openai-compat"?: {
|
|
141
139
|
/**
|
|
142
140
|
* The detail level of the image to be sent to the model. One of `high`, `low`, or `auto`. Defaults to `auto`.
|
|
143
141
|
*/
|
|
@@ -156,7 +154,7 @@ declare module "effect/ai/Prompt" {
|
|
|
156
154
|
/**
|
|
157
155
|
* Provider-specific reasoning options for OpenAI-compatible APIs.
|
|
158
156
|
*/
|
|
159
|
-
readonly openai?: {
|
|
157
|
+
readonly "openai-compat"?: {
|
|
160
158
|
/**
|
|
161
159
|
* The ID of the item to reference.
|
|
162
160
|
*/
|
|
@@ -167,6 +165,12 @@ declare module "effect/ai/Prompt" {
|
|
|
167
165
|
* parameter.
|
|
168
166
|
*/
|
|
169
167
|
readonly encryptedContent?: string | null
|
|
168
|
+
/**
|
|
169
|
+
* The chat message field the reasoning was returned under. Replayed
|
|
170
|
+
* reasoning is sent back under the same field, defaulting to
|
|
171
|
+
* `reasoning_content`.
|
|
172
|
+
*/
|
|
173
|
+
readonly reasoningField?: "reasoning" | "reasoning_content" | null
|
|
170
174
|
} | null
|
|
171
175
|
}
|
|
172
176
|
|
|
@@ -181,7 +185,7 @@ declare module "effect/ai/Prompt" {
|
|
|
181
185
|
/**
|
|
182
186
|
* Provider-specific tool-call options for OpenAI-compatible APIs.
|
|
183
187
|
*/
|
|
184
|
-
readonly openai?: {
|
|
188
|
+
readonly "openai-compat"?: {
|
|
185
189
|
/**
|
|
186
190
|
* The ID of the item to reference.
|
|
187
191
|
*/
|
|
@@ -208,7 +212,7 @@ declare module "effect/ai/Prompt" {
|
|
|
208
212
|
/**
|
|
209
213
|
* Provider-specific tool-result options for OpenAI-compatible APIs.
|
|
210
214
|
*/
|
|
211
|
-
readonly openai?: {
|
|
215
|
+
readonly "openai-compat"?: {
|
|
212
216
|
/**
|
|
213
217
|
* The ID of the item to reference.
|
|
214
218
|
*/
|
|
@@ -231,7 +235,7 @@ declare module "effect/ai/Prompt" {
|
|
|
231
235
|
/**
|
|
232
236
|
* Provider-specific text options for OpenAI-compatible APIs.
|
|
233
237
|
*/
|
|
234
|
-
readonly openai?: {
|
|
238
|
+
readonly "openai-compat"?: {
|
|
235
239
|
/**
|
|
236
240
|
* The ID of the item to reference.
|
|
237
241
|
*/
|
|
@@ -260,7 +264,7 @@ declare module "effect/ai/Response" {
|
|
|
260
264
|
/**
|
|
261
265
|
* Provider-specific metadata returned for the text part.
|
|
262
266
|
*/
|
|
263
|
-
readonly openai?: {
|
|
267
|
+
readonly "openai-compat"?: {
|
|
264
268
|
/**
|
|
265
269
|
* The OpenAI item ID associated with the text part.
|
|
266
270
|
*/
|
|
@@ -293,7 +297,7 @@ declare module "effect/ai/Response" {
|
|
|
293
297
|
/**
|
|
294
298
|
* Provider-specific metadata returned for the streamed text start.
|
|
295
299
|
*/
|
|
296
|
-
readonly openai?: {
|
|
300
|
+
readonly "openai-compat"?: {
|
|
297
301
|
/**
|
|
298
302
|
* The OpenAI item ID associated with the streamed text part.
|
|
299
303
|
*/
|
|
@@ -312,7 +316,7 @@ declare module "effect/ai/Response" {
|
|
|
312
316
|
/**
|
|
313
317
|
* Provider-specific metadata returned for the streamed text end.
|
|
314
318
|
*/
|
|
315
|
-
readonly openai?: {
|
|
319
|
+
readonly "openai-compat"?: {
|
|
316
320
|
/**
|
|
317
321
|
* The OpenAI item ID associated with the streamed text part.
|
|
318
322
|
*/
|
|
@@ -335,7 +339,7 @@ declare module "effect/ai/Response" {
|
|
|
335
339
|
/**
|
|
336
340
|
* Provider-specific metadata returned for the reasoning part.
|
|
337
341
|
*/
|
|
338
|
-
readonly openai?: {
|
|
342
|
+
readonly "openai-compat"?: {
|
|
339
343
|
/**
|
|
340
344
|
* The OpenAI item ID associated with the reasoning part.
|
|
341
345
|
*/
|
|
@@ -344,6 +348,10 @@ declare module "effect/ai/Response" {
|
|
|
344
348
|
* Encrypted reasoning content that can be sent back in later requests.
|
|
345
349
|
*/
|
|
346
350
|
readonly encryptedContent?: string | null
|
|
351
|
+
/**
|
|
352
|
+
* The chat message field the provider returned the reasoning under.
|
|
353
|
+
*/
|
|
354
|
+
readonly reasoningField?: "reasoning" | "reasoning_content" | null
|
|
347
355
|
} | null
|
|
348
356
|
}
|
|
349
357
|
|
|
@@ -358,7 +366,7 @@ declare module "effect/ai/Response" {
|
|
|
358
366
|
/**
|
|
359
367
|
* Provider-specific metadata returned for the streamed reasoning start.
|
|
360
368
|
*/
|
|
361
|
-
readonly openai?: {
|
|
369
|
+
readonly "openai-compat"?: {
|
|
362
370
|
/**
|
|
363
371
|
* The OpenAI item ID associated with the reasoning part.
|
|
364
372
|
*/
|
|
@@ -367,6 +375,10 @@ declare module "effect/ai/Response" {
|
|
|
367
375
|
* Encrypted reasoning content that can be sent back in later requests.
|
|
368
376
|
*/
|
|
369
377
|
readonly encryptedContent?: string | null
|
|
378
|
+
/**
|
|
379
|
+
* The chat message field the provider returned the reasoning under.
|
|
380
|
+
*/
|
|
381
|
+
readonly reasoningField?: "reasoning" | "reasoning_content" | null
|
|
370
382
|
} | null
|
|
371
383
|
}
|
|
372
384
|
|
|
@@ -381,7 +393,7 @@ declare module "effect/ai/Response" {
|
|
|
381
393
|
/**
|
|
382
394
|
* Provider-specific metadata returned for the streamed reasoning delta.
|
|
383
395
|
*/
|
|
384
|
-
readonly openai?: {
|
|
396
|
+
readonly "openai-compat"?: {
|
|
385
397
|
/**
|
|
386
398
|
* The OpenAI item ID associated with the reasoning part.
|
|
387
399
|
*/
|
|
@@ -400,7 +412,7 @@ declare module "effect/ai/Response" {
|
|
|
400
412
|
/**
|
|
401
413
|
* Provider-specific metadata returned for the streamed reasoning end.
|
|
402
414
|
*/
|
|
403
|
-
readonly openai?: {
|
|
415
|
+
readonly "openai-compat"?: {
|
|
404
416
|
/**
|
|
405
417
|
* The OpenAI item ID associated with the reasoning part.
|
|
406
418
|
*/
|
|
@@ -423,7 +435,7 @@ declare module "effect/ai/Response" {
|
|
|
423
435
|
/**
|
|
424
436
|
* Provider-specific metadata returned for the tool call.
|
|
425
437
|
*/
|
|
426
|
-
readonly openai?: {
|
|
438
|
+
readonly "openai-compat"?: {
|
|
427
439
|
/**
|
|
428
440
|
* The OpenAI item ID associated with the tool call.
|
|
429
441
|
*/
|
|
@@ -446,7 +458,7 @@ declare module "effect/ai/Response" {
|
|
|
446
458
|
/**
|
|
447
459
|
* Provider-specific citation metadata for OpenAI-compatible APIs.
|
|
448
460
|
*/
|
|
449
|
-
readonly openai?:
|
|
461
|
+
readonly "openai-compat"?:
|
|
450
462
|
| {
|
|
451
463
|
/**
|
|
452
464
|
* Identifies a citation to an uploaded file.
|
|
@@ -503,7 +515,7 @@ declare module "effect/ai/Response" {
|
|
|
503
515
|
/**
|
|
504
516
|
* Provider-specific URL citation metadata for OpenAI-compatible APIs.
|
|
505
517
|
*/
|
|
506
|
-
readonly openai?: {
|
|
518
|
+
readonly "openai-compat"?: {
|
|
507
519
|
/**
|
|
508
520
|
* Identifies a citation to a URL.
|
|
509
521
|
*/
|
|
@@ -530,7 +542,7 @@ declare module "effect/ai/Response" {
|
|
|
530
542
|
/**
|
|
531
543
|
* Provider-specific metadata returned when generation finishes.
|
|
532
544
|
*/
|
|
533
|
-
readonly openai?: {
|
|
545
|
+
readonly "openai-compat"?: {
|
|
534
546
|
/**
|
|
535
547
|
* The service tier reported by the OpenAI-compatible provider.
|
|
536
548
|
*/
|
|
@@ -612,17 +624,16 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
612
624
|
readonly config: typeof Config.Service
|
|
613
625
|
readonly options: LanguageModel.ProviderOptions
|
|
614
626
|
readonly toolNameMapper: Tool.NameMapper<Tools>
|
|
615
|
-
}): Effect.fn.Return<
|
|
616
|
-
|
|
617
|
-
|
|
627
|
+
}): Effect.fn.Return<{
|
|
628
|
+
readonly request: CreateResponseRequestJson
|
|
629
|
+
readonly toolCodecs: ToolCodecs
|
|
630
|
+
}, AiError.AiError> {
|
|
618
631
|
const messages = yield* prepareMessages({
|
|
619
632
|
config,
|
|
620
633
|
options,
|
|
621
|
-
capabilities,
|
|
622
|
-
include,
|
|
623
634
|
toolNameMapper
|
|
624
635
|
})
|
|
625
|
-
const { toolChoice, tools } = yield* prepareTools({
|
|
636
|
+
const { toolChoice, toolCodecs, tools } = yield* prepareTools({
|
|
626
637
|
config,
|
|
627
638
|
options,
|
|
628
639
|
toolNameMapper
|
|
@@ -634,8 +645,6 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
634
645
|
const { fileIdPrefixes: _fip, strictJsonSchema: _sjs, ...apiConfig } = config
|
|
635
646
|
const request: CreateResponse = {
|
|
636
647
|
...apiConfig,
|
|
637
|
-
input: messages,
|
|
638
|
-
include: include.size > 0 ? Array.from(include) : null,
|
|
639
648
|
text: {
|
|
640
649
|
verbosity: config.text?.verbosity ?? null,
|
|
641
650
|
format: responseFormat
|
|
@@ -643,7 +652,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
643
652
|
...(tools !== undefined ? { tools } : undefined),
|
|
644
653
|
...(toolChoice !== undefined ? { tool_choice: toolChoice } : undefined)
|
|
645
654
|
}
|
|
646
|
-
return toChatCompletionsRequest(request)
|
|
655
|
+
return { request: toChatCompletionsRequest(request, messages), toolCodecs }
|
|
647
656
|
}
|
|
648
657
|
)
|
|
649
658
|
|
|
@@ -653,7 +662,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
653
662
|
function*(options) {
|
|
654
663
|
const config = yield* makeConfig
|
|
655
664
|
const toolNameMapper = new Tool.NameMapper(options.tools)
|
|
656
|
-
const request = yield* makeRequest({ config, options, toolNameMapper })
|
|
665
|
+
const { request, toolCodecs } = yield* makeRequest({ config, options, toolNameMapper })
|
|
657
666
|
annotateRequest(options.span, request)
|
|
658
667
|
const [rawResponse, response] = yield* client.createResponse(request)
|
|
659
668
|
annotateResponse(options.span, rawResponse)
|
|
@@ -661,7 +670,8 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
661
670
|
options,
|
|
662
671
|
rawResponse,
|
|
663
672
|
response,
|
|
664
|
-
toolNameMapper
|
|
673
|
+
toolNameMapper,
|
|
674
|
+
toolCodecs
|
|
665
675
|
})
|
|
666
676
|
}
|
|
667
677
|
),
|
|
@@ -669,14 +679,15 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
669
679
|
function*(options) {
|
|
670
680
|
const config = yield* makeConfig
|
|
671
681
|
const toolNameMapper = new Tool.NameMapper(options.tools)
|
|
672
|
-
const request = yield* makeRequest({ config, options, toolNameMapper })
|
|
682
|
+
const { request, toolCodecs } = yield* makeRequest({ config, options, toolNameMapper })
|
|
673
683
|
annotateRequest(options.span, request)
|
|
674
684
|
const [response, stream] = yield* client.createResponseStream(request)
|
|
675
685
|
return yield* makeStreamResponse({
|
|
676
686
|
options,
|
|
677
687
|
stream,
|
|
678
688
|
response,
|
|
679
|
-
toolNameMapper
|
|
689
|
+
toolNameMapper,
|
|
690
|
+
toolCodecs
|
|
680
691
|
})
|
|
681
692
|
},
|
|
682
693
|
(effect, options) =>
|
|
@@ -840,27 +851,13 @@ const prepareMessages = Effect.fnUntraced(
|
|
|
840
851
|
function*<Tools extends ReadonlyArray<Tool.Any>>({
|
|
841
852
|
config,
|
|
842
853
|
options,
|
|
843
|
-
capabilities,
|
|
844
|
-
include,
|
|
845
854
|
toolNameMapper
|
|
846
855
|
}: {
|
|
847
856
|
readonly config: typeof Config.Service
|
|
848
857
|
readonly options: LanguageModel.ProviderOptions
|
|
849
|
-
readonly include: Set<IncludeEnum>
|
|
850
|
-
readonly capabilities: ModelCapabilities
|
|
851
858
|
readonly toolNameMapper: Tool.NameMapper<Tools>
|
|
852
|
-
}): Effect.fn.Return<ReadonlyArray<
|
|
853
|
-
const
|
|
854
|
-
|
|
855
|
-
// Handle Included Features
|
|
856
|
-
if (config.top_logprobs !== undefined) {
|
|
857
|
-
include.add("message.output_text.logprobs")
|
|
858
|
-
}
|
|
859
|
-
if (config.store === false && capabilities.isReasoningModel) {
|
|
860
|
-
include.add("reasoning.encrypted_content")
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
const messages: Array<InputItem> = []
|
|
859
|
+
}): Effect.fn.Return<ReadonlyArray<ChatCompletionRequestMessage>, AiError.AiError> {
|
|
860
|
+
const messages: Array<ChatCompletionRequestMessage> = []
|
|
864
861
|
|
|
865
862
|
for (const message of options.prompt.content) {
|
|
866
863
|
switch (message.role) {
|
|
@@ -929,157 +926,62 @@ const prepareMessages = Effect.fnUntraced(
|
|
|
929
926
|
}
|
|
930
927
|
}
|
|
931
928
|
|
|
932
|
-
messages.push({ role: "user", content })
|
|
929
|
+
messages.push({ role: "user", content: toChatMessageContent(content) })
|
|
933
930
|
|
|
934
931
|
break
|
|
935
932
|
}
|
|
936
933
|
|
|
937
934
|
case "assistant": {
|
|
938
|
-
|
|
935
|
+
let content: string | null = null
|
|
936
|
+
let reasoning = ""
|
|
937
|
+
let reasoningField: ReasoningField | undefined
|
|
938
|
+
const toolCalls: Array<ChatCompletionRequestToolCall> = []
|
|
939
939
|
|
|
940
940
|
for (const part of message.content) {
|
|
941
941
|
switch (part.type) {
|
|
942
942
|
case "text": {
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
// When in conversation mode, skip items that already exist in the
|
|
946
|
-
// conversation context to avoid "Duplicate item found" errors
|
|
947
|
-
if (hasConversation && Predicate.isNotNull(id)) {
|
|
948
|
-
break
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
if (config.store === true && Predicate.isNotNull(id)) {
|
|
952
|
-
messages.push({ type: "item_reference", id })
|
|
953
|
-
break
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
messages.push({
|
|
957
|
-
id: id!,
|
|
958
|
-
type: "message",
|
|
959
|
-
role: "assistant",
|
|
960
|
-
status: part.options.openai?.status ?? "completed",
|
|
961
|
-
content: [{
|
|
962
|
-
type: "output_text",
|
|
963
|
-
text: part.text,
|
|
964
|
-
annotations: part.options.openai?.annotations ?? [],
|
|
965
|
-
logprobs: []
|
|
966
|
-
}]
|
|
967
|
-
})
|
|
968
|
-
|
|
943
|
+
content = (content ?? "") + part.text
|
|
969
944
|
break
|
|
970
945
|
}
|
|
971
946
|
|
|
972
947
|
case "reasoning": {
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
if (hasConversation && Predicate.isNotNull(id)) {
|
|
977
|
-
break
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
if (Predicate.isNotNull(id)) {
|
|
981
|
-
const message = reasoningMessages[id]
|
|
982
|
-
|
|
983
|
-
if (config.store === true) {
|
|
984
|
-
// Use item references to refer to reasoning (single reference)
|
|
985
|
-
// when the first part is encountered
|
|
986
|
-
if (Predicate.isUndefined(message)) {
|
|
987
|
-
messages.push({ type: "item_reference", id })
|
|
988
|
-
|
|
989
|
-
// Store unused reasoning message to mark its id as used
|
|
990
|
-
reasoningMessages[id] = {
|
|
991
|
-
type: "reasoning",
|
|
992
|
-
id,
|
|
993
|
-
summary: []
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
} else {
|
|
997
|
-
const summaryParts: Array<SummaryTextContent> = []
|
|
998
|
-
|
|
999
|
-
if (part.text.length > 0) {
|
|
1000
|
-
summaryParts.push({ type: "summary_text", text: part.text })
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
if (Predicate.isUndefined(message)) {
|
|
1004
|
-
reasoningMessages[id] = {
|
|
1005
|
-
type: "reasoning",
|
|
1006
|
-
id,
|
|
1007
|
-
summary: summaryParts,
|
|
1008
|
-
encrypted_content: encryptedContent ?? null
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
messages.push(reasoningMessages[id])
|
|
1012
|
-
} else {
|
|
1013
|
-
message.summary.push(...summaryParts)
|
|
1014
|
-
|
|
1015
|
-
// Update encrypted content to enable setting it in the
|
|
1016
|
-
// last summary part
|
|
1017
|
-
if (Predicate.isNotNull(encryptedContent)) {
|
|
1018
|
-
message.encrypted_content = encryptedContent
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
|
|
948
|
+
reasoning += part.text
|
|
949
|
+
reasoningField ??= part.options["openai-compat"]?.reasoningField ?? undefined
|
|
1024
950
|
break
|
|
1025
951
|
}
|
|
1026
952
|
|
|
1027
953
|
case "tool-call": {
|
|
1028
|
-
const id = getItemId(part)
|
|
1029
|
-
const status = getStatus(part)
|
|
1030
|
-
|
|
1031
|
-
if (hasConversation && Predicate.isNotNull(id)) {
|
|
1032
|
-
break
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
if (config.store && Predicate.isNotNull(id)) {
|
|
1036
|
-
messages.push({ type: "item_reference", id })
|
|
1037
|
-
break
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
954
|
if (part.providerExecuted) {
|
|
1041
955
|
break
|
|
1042
956
|
}
|
|
1043
957
|
|
|
1044
|
-
const toolName = toolNameMapper.getProviderName(part.name)
|
|
1045
958
|
const extraContent = getExtraContent(part)
|
|
1046
959
|
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
...(Predicate.isNotNull(
|
|
960
|
+
toolCalls.push({
|
|
961
|
+
id: part.id,
|
|
962
|
+
type: "function",
|
|
963
|
+
function: {
|
|
964
|
+
name: toolNameMapper.getProviderName(part.name),
|
|
965
|
+
arguments: JSON.stringify(part.params)
|
|
966
|
+
},
|
|
967
|
+
...(Predicate.isNotNull(extraContent) ? { extra_content: extraContent } : undefined)
|
|
1055
968
|
})
|
|
1056
|
-
|
|
1057
969
|
break
|
|
1058
970
|
}
|
|
1059
|
-
|
|
1060
|
-
// Assistant tool-result parts are always provider executed
|
|
1061
|
-
case "tool-result": {
|
|
1062
|
-
// Skip execution denied results - these have no corresponding
|
|
1063
|
-
// item in OpenAI's store
|
|
1064
|
-
if (
|
|
1065
|
-
Predicate.hasProperty(part.result, "type") &&
|
|
1066
|
-
part.result.type === "execution-denied"
|
|
1067
|
-
) {
|
|
1068
|
-
break
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
if (hasConversation) {
|
|
1072
|
-
break
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
if (config.store === true) {
|
|
1076
|
-
const id = getItemId(part) ?? part.id
|
|
1077
|
-
messages.push({ type: "item_reference", id })
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
971
|
}
|
|
1081
972
|
}
|
|
1082
973
|
|
|
974
|
+
// Text, reasoning and tool calls from one prompt message are sent as a
|
|
975
|
+
// single chat message so reasoning stays attached to its tool calls
|
|
976
|
+
if (content !== null || toolCalls.length > 0) {
|
|
977
|
+
messages.push({
|
|
978
|
+
role: "assistant",
|
|
979
|
+
content,
|
|
980
|
+
...(reasoning.length > 0 ? { [reasoningField ?? "reasoning_content"]: reasoning } : undefined),
|
|
981
|
+
...(toolCalls.length > 0 ? { tool_calls: toolCalls } : undefined)
|
|
982
|
+
})
|
|
983
|
+
}
|
|
984
|
+
|
|
1083
985
|
break
|
|
1084
986
|
}
|
|
1085
987
|
|
|
@@ -1089,13 +991,10 @@ const prepareMessages = Effect.fnUntraced(
|
|
|
1089
991
|
continue
|
|
1090
992
|
}
|
|
1091
993
|
|
|
1092
|
-
const status = getStatus(part)
|
|
1093
|
-
|
|
1094
994
|
messages.push({
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
...(Predicate.isNotNull(status) ? { status } : {})
|
|
995
|
+
role: "tool",
|
|
996
|
+
tool_call_id: part.id,
|
|
997
|
+
content: typeof part.result === "string" ? part.result : JSON.stringify(part.result)
|
|
1099
998
|
})
|
|
1100
999
|
}
|
|
1101
1000
|
|
|
@@ -1152,12 +1051,14 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1152
1051
|
options,
|
|
1153
1052
|
rawResponse,
|
|
1154
1053
|
response,
|
|
1155
|
-
toolNameMapper
|
|
1054
|
+
toolNameMapper,
|
|
1055
|
+
toolCodecs
|
|
1156
1056
|
}: {
|
|
1157
1057
|
readonly options: LanguageModel.ProviderOptions
|
|
1158
1058
|
readonly rawResponse: CreateResponse200
|
|
1159
1059
|
readonly response: HttpClientResponse.HttpClientResponse
|
|
1160
1060
|
readonly toolNameMapper: Tool.NameMapper<Tools>
|
|
1061
|
+
readonly toolCodecs: ToolCodecs
|
|
1161
1062
|
}): Effect.fn.Return<
|
|
1162
1063
|
Array<Response.PartEncoded>,
|
|
1163
1064
|
AiError.AiError
|
|
@@ -1178,9 +1079,10 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1178
1079
|
const message = choice?.message
|
|
1179
1080
|
|
|
1180
1081
|
if (message !== undefined) {
|
|
1181
|
-
const
|
|
1182
|
-
|
|
1183
|
-
|
|
1082
|
+
const reasoningField = getResponseReasoningField(message)
|
|
1083
|
+
const reasoning = reasoningField !== undefined ? message[reasoningField] : undefined
|
|
1084
|
+
if (reasoningField !== undefined && Predicate.isNotNullish(reasoning) && reasoning.length > 0) {
|
|
1085
|
+
parts.push({ type: "reasoning", text: reasoning, metadata: { "openai-compat": { reasoningField } } })
|
|
1184
1086
|
}
|
|
1185
1087
|
|
|
1186
1088
|
if (
|
|
@@ -1206,7 +1108,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1206
1108
|
})
|
|
1207
1109
|
})
|
|
1208
1110
|
})
|
|
1209
|
-
const params = yield* transformToolCallParams(options
|
|
1111
|
+
const params = yield* transformToolCallParams(options, toolName, toolParams, toolCodecs)
|
|
1210
1112
|
hasToolCalls = true
|
|
1211
1113
|
parts.push({
|
|
1212
1114
|
type: "tool-call",
|
|
@@ -1214,7 +1116,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1214
1116
|
name: toolName,
|
|
1215
1117
|
params,
|
|
1216
1118
|
metadata: {
|
|
1217
|
-
openai: {
|
|
1119
|
+
"openai-compat": {
|
|
1218
1120
|
...makeItemIdMetadata(toolCall.id),
|
|
1219
1121
|
...makeExtraContentMetadata(toolCall.extra_content ?? undefined)
|
|
1220
1122
|
}
|
|
@@ -1235,7 +1137,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1235
1137
|
reason: finishReason,
|
|
1236
1138
|
usage: getUsage(rawResponse.usage),
|
|
1237
1139
|
response: buildHttpResponseDetails(response),
|
|
1238
|
-
...(serviceTier !== undefined && { metadata: { openai: { serviceTier } } })
|
|
1140
|
+
...(serviceTier !== undefined && { metadata: { "openai-compat": { serviceTier } } })
|
|
1239
1141
|
})
|
|
1240
1142
|
|
|
1241
1143
|
return parts
|
|
@@ -1247,12 +1149,14 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1247
1149
|
options,
|
|
1248
1150
|
stream,
|
|
1249
1151
|
response,
|
|
1250
|
-
toolNameMapper
|
|
1152
|
+
toolNameMapper,
|
|
1153
|
+
toolCodecs
|
|
1251
1154
|
}: {
|
|
1252
1155
|
readonly options: LanguageModel.ProviderOptions
|
|
1253
1156
|
readonly stream: Stream.Stream<ResponseStreamEvent, AiError.AiError>
|
|
1254
1157
|
readonly response: HttpClientResponse.HttpClientResponse
|
|
1255
1158
|
readonly toolNameMapper: Tool.NameMapper<Tools>
|
|
1159
|
+
readonly toolCodecs: ToolCodecs
|
|
1256
1160
|
}): Effect.fn.Return<
|
|
1257
1161
|
Stream.Stream<Response.StreamPartEncoded, AiError.AiError>,
|
|
1258
1162
|
AiError.AiError
|
|
@@ -1277,7 +1181,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1277
1181
|
parts.push({
|
|
1278
1182
|
type: "reasoning-end",
|
|
1279
1183
|
id: reasoningId,
|
|
1280
|
-
metadata: { openai: { ...makeItemIdMetadata(reasoningId) } }
|
|
1184
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(reasoningId) } }
|
|
1281
1185
|
})
|
|
1282
1186
|
}
|
|
1283
1187
|
|
|
@@ -1285,7 +1189,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1285
1189
|
parts.push({
|
|
1286
1190
|
type: "text-end",
|
|
1287
1191
|
id: textId,
|
|
1288
|
-
metadata: { openai: { ...makeItemIdMetadata(textId) } }
|
|
1192
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(textId) } }
|
|
1289
1193
|
})
|
|
1290
1194
|
}
|
|
1291
1195
|
|
|
@@ -1303,7 +1207,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1303
1207
|
})
|
|
1304
1208
|
})
|
|
1305
1209
|
})
|
|
1306
|
-
const params = yield* transformToolCallParams(options
|
|
1210
|
+
const params = yield* transformToolCallParams(options, toolCall.name, parsedParams, toolCodecs)
|
|
1307
1211
|
parts.push({ type: "tool-params-end", id: toolCall.id })
|
|
1308
1212
|
parts.push({
|
|
1309
1213
|
type: "tool-call",
|
|
@@ -1311,7 +1215,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1311
1215
|
name: toolCall.name,
|
|
1312
1216
|
params,
|
|
1313
1217
|
metadata: {
|
|
1314
|
-
openai: {
|
|
1218
|
+
"openai-compat": {
|
|
1315
1219
|
...makeItemIdMetadata(toolCall.id),
|
|
1316
1220
|
...makeExtraContentMetadata(toolCall.extraContent)
|
|
1317
1221
|
}
|
|
@@ -1327,7 +1231,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1327
1231
|
usage: getUsage(usage),
|
|
1328
1232
|
response: buildHttpResponseDetails(response),
|
|
1329
1233
|
...(normalizedServiceTier !== undefined
|
|
1330
|
-
? { metadata: { openai: { serviceTier: normalizedServiceTier } } }
|
|
1234
|
+
? { metadata: { "openai-compat": { serviceTier: normalizedServiceTier } } }
|
|
1331
1235
|
: undefined)
|
|
1332
1236
|
})
|
|
1333
1237
|
return parts
|
|
@@ -1364,14 +1268,17 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1364
1268
|
return parts
|
|
1365
1269
|
}
|
|
1366
1270
|
|
|
1367
|
-
const
|
|
1368
|
-
|
|
1271
|
+
const reasoningField = Predicate.isNotNullish(choice.delta)
|
|
1272
|
+
? getResponseReasoningField(choice.delta)
|
|
1273
|
+
: undefined
|
|
1274
|
+
const reasoningDelta = reasoningField !== undefined ? choice.delta?.[reasoningField] : undefined
|
|
1275
|
+
if (reasoningField !== undefined && Predicate.isNotNullish(reasoningDelta) && reasoningDelta.length > 0) {
|
|
1369
1276
|
if (!reasoningStarted) {
|
|
1370
1277
|
reasoningStarted = true
|
|
1371
1278
|
parts.push({
|
|
1372
1279
|
type: "reasoning-start",
|
|
1373
1280
|
id: reasoningId,
|
|
1374
|
-
metadata: { openai: { ...makeItemIdMetadata(reasoningId) } }
|
|
1281
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(reasoningId), reasoningField } }
|
|
1375
1282
|
})
|
|
1376
1283
|
}
|
|
1377
1284
|
parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningDelta })
|
|
@@ -1383,7 +1290,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1383
1290
|
parts.push({
|
|
1384
1291
|
type: "reasoning-end",
|
|
1385
1292
|
id: reasoningId,
|
|
1386
|
-
metadata: { openai: { ...makeItemIdMetadata(reasoningId) } }
|
|
1293
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(reasoningId) } }
|
|
1387
1294
|
})
|
|
1388
1295
|
}
|
|
1389
1296
|
|
|
@@ -1392,7 +1299,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1392
1299
|
parts.push({
|
|
1393
1300
|
type: "text-start",
|
|
1394
1301
|
id: textId,
|
|
1395
|
-
metadata: { openai: { ...makeItemIdMetadata(textId) } }
|
|
1302
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(textId) } }
|
|
1396
1303
|
})
|
|
1397
1304
|
}
|
|
1398
1305
|
parts.push({ type: "text-delta", id: textId, delta: choice.delta.content })
|
|
@@ -1438,7 +1345,8 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1438
1345
|
|
|
1439
1346
|
return parts
|
|
1440
1347
|
})),
|
|
1441
|
-
Stream.
|
|
1348
|
+
Stream.filter(Arr.isArrayNonEmpty),
|
|
1349
|
+
Stream.flattenArray
|
|
1442
1350
|
)
|
|
1443
1351
|
}
|
|
1444
1352
|
)
|
|
@@ -1498,7 +1406,7 @@ const annotateStreamResponse = (span: Span, part: Response.StreamPartEncoded) =>
|
|
|
1498
1406
|
})
|
|
1499
1407
|
}
|
|
1500
1408
|
if (part.type === "finish") {
|
|
1501
|
-
const serviceTier = (part.metadata as any)?.openai?.serviceTier as string | undefined
|
|
1409
|
+
const serviceTier = (part.metadata as any)?.["openai-compat"]?.serviceTier as string | undefined
|
|
1502
1410
|
addGenAIAnnotations(span, {
|
|
1503
1411
|
response: {
|
|
1504
1412
|
finishReasons: [part.reason]
|
|
@@ -1541,26 +1449,27 @@ const tryJsonSchema = <S extends Schema.Constraint>(schema: S, method: string) =
|
|
|
1541
1449
|
catch: (error) => unsupportedSchemaError(error, method)
|
|
1542
1450
|
})
|
|
1543
1451
|
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
try: () => Tool.getJsonSchema(tool, { transformer: toCodecOpenAI }),
|
|
1547
|
-
catch: (error) => unsupportedSchemaError(error, method)
|
|
1548
|
-
})
|
|
1452
|
+
type ToolCodec = ReturnType<typeof toCodecOpenAI>["codec"]
|
|
1453
|
+
type ToolCodecs = ReadonlyMap<Tool.Any, ToolCodec>
|
|
1549
1454
|
|
|
1550
|
-
const transformToolCallParams = Effect.fnUntraced(function
|
|
1551
|
-
|
|
1455
|
+
const transformToolCallParams = Effect.fnUntraced(function*(
|
|
1456
|
+
options: LanguageModel.ProviderOptions,
|
|
1552
1457
|
toolName: string,
|
|
1553
|
-
toolParams: unknown
|
|
1458
|
+
toolParams: unknown,
|
|
1459
|
+
toolCodecs: ToolCodecs
|
|
1554
1460
|
): Effect.fn.Return<unknown, AiError.AiError> {
|
|
1555
|
-
const tool = tools.find((tool) => tool.name === toolName)
|
|
1461
|
+
const tool = options.tools.find((tool) => tool.name === toolName)
|
|
1556
1462
|
|
|
1557
1463
|
if (Predicate.isUndefined(tool)) {
|
|
1464
|
+
if (options.toolCallValidation === "deferred") {
|
|
1465
|
+
return toolParams
|
|
1466
|
+
}
|
|
1558
1467
|
return yield* AiError.make({
|
|
1559
1468
|
module: "OpenAiLanguageModel",
|
|
1560
1469
|
method: "makeResponse",
|
|
1561
1470
|
reason: new AiError.ToolNotFoundError({
|
|
1562
1471
|
toolName,
|
|
1563
|
-
availableTools: tools.map((tool) => tool.name)
|
|
1472
|
+
availableTools: options.tools.map((tool) => tool.name)
|
|
1564
1473
|
})
|
|
1565
1474
|
})
|
|
1566
1475
|
}
|
|
@@ -1569,7 +1478,7 @@ const transformToolCallParams = Effect.fnUntraced(function*<Tools extends Readon
|
|
|
1569
1478
|
return toolParams
|
|
1570
1479
|
}
|
|
1571
1480
|
|
|
1572
|
-
const
|
|
1481
|
+
const codec = toolCodecs.get(tool) ?? (yield* tryCodecTransform(tool.parametersSchema, "makeResponse")).codec
|
|
1573
1482
|
|
|
1574
1483
|
// Normalize valid parameters; leave invalid ones for Toolkit.
|
|
1575
1484
|
return yield* (
|
|
@@ -1593,10 +1502,13 @@ const prepareTools = Effect.fnUntraced(function*<Tools extends ReadonlyArray<Too
|
|
|
1593
1502
|
}): Effect.fn.Return<{
|
|
1594
1503
|
readonly tools: ReadonlyArray<OpenAiClientTool> | undefined
|
|
1595
1504
|
readonly toolChoice: OpenAiToolChoice | undefined
|
|
1505
|
+
readonly toolCodecs: ToolCodecs
|
|
1596
1506
|
}, AiError.AiError> {
|
|
1507
|
+
const toolCodecs = new Map<Tool.Any, ToolCodec>()
|
|
1508
|
+
|
|
1597
1509
|
// Return immediately if no tools are in the toolkit
|
|
1598
1510
|
if (options.tools.length === 0) {
|
|
1599
|
-
return { tools: undefined, toolChoice: undefined }
|
|
1511
|
+
return { tools: undefined, toolChoice: undefined, toolCodecs }
|
|
1600
1512
|
}
|
|
1601
1513
|
|
|
1602
1514
|
const tools: Array<OpenAiClientTool> = []
|
|
@@ -1617,7 +1529,15 @@ const prepareTools = Effect.fnUntraced(function*<Tools extends ReadonlyArray<Too
|
|
|
1617
1529
|
for (const tool of allowedTools) {
|
|
1618
1530
|
if (Tool.isUserDefined(tool) || Tool.isDynamic(tool)) {
|
|
1619
1531
|
const strict = Tool.getStrictMode(tool) ?? config.strictJsonSchema ?? true
|
|
1620
|
-
|
|
1532
|
+
let parameters: ReturnType<typeof Tool.getJsonSchema>
|
|
1533
|
+
if (Tool.isDynamic(tool) && tool.jsonSchema !== undefined) {
|
|
1534
|
+
parameters = tool.jsonSchema
|
|
1535
|
+
} else {
|
|
1536
|
+
const prepared = yield* tryCodecTransform(tool.parametersSchema, "prepareTools")
|
|
1537
|
+
parameters = prepared.jsonSchema
|
|
1538
|
+
// Reused to normalize this tool's calls in the response.
|
|
1539
|
+
toolCodecs.set(tool, prepared.codec)
|
|
1540
|
+
}
|
|
1621
1541
|
tools.push({
|
|
1622
1542
|
type: "function",
|
|
1623
1543
|
name: tool.name,
|
|
@@ -1652,11 +1572,13 @@ const prepareTools = Effect.fnUntraced(function*<Tools extends ReadonlyArray<Too
|
|
|
1652
1572
|
}
|
|
1653
1573
|
}
|
|
1654
1574
|
|
|
1655
|
-
return { tools, toolChoice }
|
|
1575
|
+
return { tools, toolChoice, toolCodecs }
|
|
1656
1576
|
})
|
|
1657
1577
|
|
|
1658
|
-
const toChatCompletionsRequest = (
|
|
1659
|
-
|
|
1578
|
+
const toChatCompletionsRequest = (
|
|
1579
|
+
payload: CreateResponse,
|
|
1580
|
+
messages: ReadonlyArray<ChatCompletionRequestMessage>
|
|
1581
|
+
): CreateResponseRequestJson => {
|
|
1660
1582
|
const responseFormat = toChatResponseFormat(payload.text?.format)
|
|
1661
1583
|
const tools = payload.tools !== undefined
|
|
1662
1584
|
? payload.tools.map(toChatTool).filter((tool): tool is NonNullable<ReturnType<typeof toChatTool>> =>
|
|
@@ -1818,113 +1740,6 @@ const toChatTool = (
|
|
|
1818
1740
|
return undefined
|
|
1819
1741
|
}
|
|
1820
1742
|
|
|
1821
|
-
const toChatMessages = (
|
|
1822
|
-
input: CreateResponse["input"]
|
|
1823
|
-
): Array<CreateResponseRequestJson["messages"][number]> => {
|
|
1824
|
-
if (Predicate.isUndefined(input)) {
|
|
1825
|
-
return []
|
|
1826
|
-
}
|
|
1827
|
-
|
|
1828
|
-
if (typeof input === "string") {
|
|
1829
|
-
return [{ role: "user", content: input }]
|
|
1830
|
-
}
|
|
1831
|
-
|
|
1832
|
-
const messages: Array<CreateResponseRequestJson["messages"][number]> = []
|
|
1833
|
-
|
|
1834
|
-
for (const item of input) {
|
|
1835
|
-
if (Predicate.hasProperty(item, "type") && item.type === "function_call") {
|
|
1836
|
-
const previous = messages.at(-1)
|
|
1837
|
-
const toolCall = toChatToolCall(item)
|
|
1838
|
-
if (previous?.role === "assistant" && previous.tool_calls !== undefined) {
|
|
1839
|
-
messages[messages.length - 1] = {
|
|
1840
|
-
...previous,
|
|
1841
|
-
tool_calls: [...previous.tool_calls, toolCall]
|
|
1842
|
-
}
|
|
1843
|
-
} else {
|
|
1844
|
-
messages.push({
|
|
1845
|
-
role: "assistant",
|
|
1846
|
-
content: null,
|
|
1847
|
-
tool_calls: [toolCall]
|
|
1848
|
-
})
|
|
1849
|
-
}
|
|
1850
|
-
} else {
|
|
1851
|
-
messages.push(...toChatMessagesFromItem(item))
|
|
1852
|
-
}
|
|
1853
|
-
}
|
|
1854
|
-
|
|
1855
|
-
return messages
|
|
1856
|
-
}
|
|
1857
|
-
|
|
1858
|
-
const toChatMessagesFromItem = (
|
|
1859
|
-
item: InputItem
|
|
1860
|
-
): Array<CreateResponseRequestJson["messages"][number]> => {
|
|
1861
|
-
if (Predicate.hasProperty(item, "type") && item.type === "message") {
|
|
1862
|
-
return [{
|
|
1863
|
-
role: item.role,
|
|
1864
|
-
content: toAssistantChatMessageContent(item.content)
|
|
1865
|
-
}]
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
if (Predicate.hasProperty(item, "role")) {
|
|
1869
|
-
return [{
|
|
1870
|
-
role: item.role,
|
|
1871
|
-
content: toChatMessageContent(item.content)
|
|
1872
|
-
}]
|
|
1873
|
-
}
|
|
1874
|
-
|
|
1875
|
-
switch (item.type) {
|
|
1876
|
-
case "function_call": {
|
|
1877
|
-
return [{
|
|
1878
|
-
role: "assistant",
|
|
1879
|
-
content: null,
|
|
1880
|
-
tool_calls: [toChatToolCall(item)]
|
|
1881
|
-
}]
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
case "function_call_output": {
|
|
1885
|
-
return [{
|
|
1886
|
-
role: "tool",
|
|
1887
|
-
tool_call_id: item.call_id,
|
|
1888
|
-
content: stringifyJson(item.output)
|
|
1889
|
-
}]
|
|
1890
|
-
}
|
|
1891
|
-
|
|
1892
|
-
default: {
|
|
1893
|
-
return []
|
|
1894
|
-
}
|
|
1895
|
-
}
|
|
1896
|
-
}
|
|
1897
|
-
|
|
1898
|
-
const toChatToolCall = (
|
|
1899
|
-
item: Extract<InputItem, { readonly type: "function_call" }>
|
|
1900
|
-
): ChatCompletionRequestToolCall => ({
|
|
1901
|
-
id: item.call_id,
|
|
1902
|
-
type: "function",
|
|
1903
|
-
function: {
|
|
1904
|
-
name: item.name,
|
|
1905
|
-
arguments: item.arguments
|
|
1906
|
-
},
|
|
1907
|
-
...(item.extra_content !== undefined ? { extra_content: item.extra_content } : undefined)
|
|
1908
|
-
})
|
|
1909
|
-
|
|
1910
|
-
const toAssistantChatMessageContent = (
|
|
1911
|
-
content: ReadonlyArray<{
|
|
1912
|
-
readonly type: string
|
|
1913
|
-
readonly [x: string]: unknown
|
|
1914
|
-
}>
|
|
1915
|
-
): string => {
|
|
1916
|
-
let text = ""
|
|
1917
|
-
for (const part of content) {
|
|
1918
|
-
if (part.type === "output_text" && typeof part.text === "string") {
|
|
1919
|
-
text += part.text
|
|
1920
|
-
}
|
|
1921
|
-
if (part.type === "refusal" && typeof part.refusal === "string") {
|
|
1922
|
-
text += part.refusal
|
|
1923
|
-
}
|
|
1924
|
-
}
|
|
1925
|
-
return text
|
|
1926
|
-
}
|
|
1927
|
-
|
|
1928
1743
|
const toChatMessageContent = (
|
|
1929
1744
|
content: string | ReadonlyArray<InputContent>
|
|
1930
1745
|
): string | ReadonlyArray<ChatCompletionContentPart> => {
|
|
@@ -1932,6 +1747,10 @@ const toChatMessageContent = (
|
|
|
1932
1747
|
return content
|
|
1933
1748
|
}
|
|
1934
1749
|
|
|
1750
|
+
if (content.length === 1 && content[0].type === "input_text") {
|
|
1751
|
+
return content[0].text
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1935
1754
|
const parts: Array<ChatCompletionContentPart> = []
|
|
1936
1755
|
|
|
1937
1756
|
for (const part of content) {
|
|
@@ -1982,11 +1801,6 @@ const toChatMessageContent = (
|
|
|
1982
1801
|
return parts
|
|
1983
1802
|
}
|
|
1984
1803
|
|
|
1985
|
-
const stringifyJson = (value: unknown): string =>
|
|
1986
|
-
typeof value === "string"
|
|
1987
|
-
? value
|
|
1988
|
-
: JSON.stringify(value)
|
|
1989
|
-
|
|
1990
1804
|
// =============================================================================
|
|
1991
1805
|
// Utilities
|
|
1992
1806
|
// =============================================================================
|
|
@@ -1994,27 +1808,28 @@ const stringifyJson = (value: unknown): string =>
|
|
|
1994
1808
|
const isFileId = (data: string, config: typeof Config.Service): boolean =>
|
|
1995
1809
|
config.fileIdPrefixes != null && config.fileIdPrefixes.some((prefix) => data.startsWith(prefix))
|
|
1996
1810
|
|
|
1997
|
-
const getItemId = (
|
|
1998
|
-
part:
|
|
1999
|
-
| Prompt.TextPart
|
|
2000
|
-
| Prompt.ReasoningPart
|
|
2001
|
-
| Prompt.ToolCallPart
|
|
2002
|
-
| Prompt.ToolResultPart
|
|
2003
|
-
): string | null => part.options.openai?.itemId ?? null
|
|
2004
|
-
const getStatus = (
|
|
2005
|
-
part:
|
|
2006
|
-
| Prompt.TextPart
|
|
2007
|
-
| Prompt.ToolCallPart
|
|
2008
|
-
| Prompt.ToolResultPart
|
|
2009
|
-
): MessageStatus | null => part.options.openai?.status ?? null
|
|
2010
|
-
const getEncryptedContent = (
|
|
2011
|
-
part: Prompt.ReasoningPart
|
|
2012
|
-
): string | null => part.options.openai?.encryptedContent ?? null
|
|
2013
1811
|
const getExtraContent = (
|
|
2014
1812
|
part: Prompt.ToolCallPart
|
|
2015
|
-
): Schema.JsonObject | null => part.options
|
|
1813
|
+
): Schema.JsonObject | null => part.options["openai-compat"]?.extraContent ?? null
|
|
2016
1814
|
|
|
2017
|
-
|
|
1815
|
+
/**
|
|
1816
|
+
* The chat message field carrying reasoning text. Providers differ: DeepSeek,
|
|
1817
|
+
* Kimi and GLM use `reasoning_content`, while Groq uses `reasoning` and
|
|
1818
|
+
* rejects `reasoning_content` on request messages.
|
|
1819
|
+
*/
|
|
1820
|
+
type ReasoningField = "reasoning" | "reasoning_content"
|
|
1821
|
+
|
|
1822
|
+
const getResponseReasoningField = (message: {
|
|
1823
|
+
readonly reasoning?: string | null | undefined
|
|
1824
|
+
readonly reasoning_content?: string | null | undefined
|
|
1825
|
+
}): ReasoningField | undefined =>
|
|
1826
|
+
Predicate.isNotNullish(message.reasoning)
|
|
1827
|
+
? "reasoning"
|
|
1828
|
+
: Predicate.isNotNullish(message.reasoning_content)
|
|
1829
|
+
? "reasoning_content"
|
|
1830
|
+
: undefined
|
|
1831
|
+
|
|
1832
|
+
const getImageDetail = (part: Prompt.FilePart): ImageDetail => part.options["openai-compat"]?.imageDetail ?? "auto"
|
|
2018
1833
|
|
|
2019
1834
|
const makeItemIdMetadata = (itemId: string | undefined) => itemId !== undefined ? { itemId } : undefined
|
|
2020
1835
|
|
|
@@ -2057,54 +1872,6 @@ const prepareResponseFormat = Effect.fnUntraced(function*({ config, options }: {
|
|
|
2057
1872
|
return { type: "text" }
|
|
2058
1873
|
})
|
|
2059
1874
|
|
|
2060
|
-
interface ModelCapabilities {
|
|
2061
|
-
readonly isReasoningModel: boolean
|
|
2062
|
-
readonly systemMessageMode: "remove" | "system" | "developer"
|
|
2063
|
-
readonly supportsFlexProcessing: boolean
|
|
2064
|
-
readonly supportsPriorityProcessing: boolean
|
|
2065
|
-
/**
|
|
2066
|
-
* Allow temperature, topP, logProbs when reasoningEffort is none.
|
|
2067
|
-
*/
|
|
2068
|
-
readonly supportsNonReasoningParameters: boolean
|
|
2069
|
-
}
|
|
2070
|
-
|
|
2071
|
-
const getModelCapabilities = (modelId: string): ModelCapabilities => {
|
|
2072
|
-
const supportsFlexProcessing = modelId.startsWith("o3") ||
|
|
2073
|
-
modelId.startsWith("o4-mini") ||
|
|
2074
|
-
(modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat"))
|
|
2075
|
-
|
|
2076
|
-
const supportsPriorityProcessing = modelId.startsWith("gpt-4") ||
|
|
2077
|
-
modelId.startsWith("gpt-5-mini") ||
|
|
2078
|
-
(modelId.startsWith("gpt-5") &&
|
|
2079
|
-
!modelId.startsWith("gpt-5-nano") &&
|
|
2080
|
-
!modelId.startsWith("gpt-5-chat")) ||
|
|
2081
|
-
modelId.startsWith("o3") ||
|
|
2082
|
-
modelId.startsWith("o4-mini")
|
|
2083
|
-
|
|
2084
|
-
// Use allowlist approach: only known reasoning models should use 'developer' role
|
|
2085
|
-
// This prevents issues with fine-tuned models, third-party models, and custom models
|
|
2086
|
-
const isReasoningModel = modelId.startsWith("o1") ||
|
|
2087
|
-
modelId.startsWith("o3") ||
|
|
2088
|
-
modelId.startsWith("o4-mini") ||
|
|
2089
|
-
modelId.startsWith("codex-mini") ||
|
|
2090
|
-
modelId.startsWith("computer-use-preview") ||
|
|
2091
|
-
(modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat"))
|
|
2092
|
-
|
|
2093
|
-
// https://platform.openai.com/docs/guides/latest-model#gpt-5-1-parameter-compatibility
|
|
2094
|
-
// GPT-5.1 and GPT-5.2 support temperature, topP, logProbs when reasoningEffort is none
|
|
2095
|
-
const supportsNonReasoningParameters = modelId.startsWith("gpt-5.1") || modelId.startsWith("gpt-5.2")
|
|
2096
|
-
|
|
2097
|
-
const systemMessageMode = isReasoningModel ? "developer" : "system"
|
|
2098
|
-
|
|
2099
|
-
return {
|
|
2100
|
-
supportsFlexProcessing,
|
|
2101
|
-
supportsPriorityProcessing,
|
|
2102
|
-
isReasoningModel,
|
|
2103
|
-
systemMessageMode,
|
|
2104
|
-
supportsNonReasoningParameters
|
|
2105
|
-
}
|
|
2106
|
-
}
|
|
2107
|
-
|
|
2108
1875
|
const getUsage = (usage: CreateResponse200["usage"]): Response.Usage => {
|
|
2109
1876
|
if (Predicate.isNullish(usage)) {
|
|
2110
1877
|
return {
|