@effect/ai-openai-compat 4.0.0-beta.102 → 4.0.0-beta.104
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/OpenAiClient.d.ts +41 -31
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +8 -1
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +1 -1
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +1 -1
- package/dist/OpenAiEmbeddingModel.js +1 -1
- package/dist/OpenAiEmbeddingModel.js.map +1 -1
- package/dist/OpenAiLanguageModel.d.ts +17 -17
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +75 -14
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/errors.js +8 -0
- package/dist/internal/errors.js.map +1 -1
- package/dist/internal/utilities.js.map +1 -1
- package/package.json +5 -6
- package/src/OpenAiClient.ts +49 -34
- package/src/OpenAiConfig.ts +1 -1
- package/src/OpenAiEmbeddingModel.ts +1 -1
- package/src/OpenAiLanguageModel.ts +116 -32
- package/src/internal/errors.ts +12 -0
package/src/OpenAiClient.ts
CHANGED
|
@@ -34,7 +34,7 @@ import { OpenAiConfig } from "./OpenAiConfig.ts"
|
|
|
34
34
|
* completions, streaming chat completions, and embeddings. Transport and
|
|
35
35
|
* schema decoding failures are mapped to `AiError`.
|
|
36
36
|
*
|
|
37
|
-
* @category
|
|
37
|
+
* @category services
|
|
38
38
|
* @since 4.0.0
|
|
39
39
|
*/
|
|
40
40
|
export interface Service {
|
|
@@ -218,6 +218,7 @@ export const make = Effect.fnUntraced(
|
|
|
218
218
|
Stream.takeUntil((event) => event === "[DONE]"),
|
|
219
219
|
Stream.catchTags({
|
|
220
220
|
Retry: (error) => Stream.die(error),
|
|
221
|
+
SseError: (error) => Stream.fail(Errors.mapSseError(error, "createResponseStream")),
|
|
221
222
|
HttpClientError: (error) => Stream.fromEffect(Errors.mapHttpClientError(error, "createResponseStream"))
|
|
222
223
|
})
|
|
223
224
|
) as any
|
|
@@ -351,7 +352,7 @@ type JsonObject = { readonly [x: string]: Schema.Json }
|
|
|
351
352
|
/**
|
|
352
353
|
* Optional response fields that can be requested with the `include` parameter.
|
|
353
354
|
*
|
|
354
|
-
* @category
|
|
355
|
+
* @category models
|
|
355
356
|
* @since 4.0.0
|
|
356
357
|
*/
|
|
357
358
|
export type IncludeEnum =
|
|
@@ -390,7 +391,7 @@ type InputFileContent = {
|
|
|
390
391
|
/**
|
|
391
392
|
* Content blocks accepted in input messages.
|
|
392
393
|
*
|
|
393
|
-
* @category
|
|
394
|
+
* @category models
|
|
394
395
|
* @since 4.0.0
|
|
395
396
|
*/
|
|
396
397
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
@@ -398,7 +399,7 @@ export type InputContent = InputTextContent | InputImageContent | InputFileConte
|
|
|
398
399
|
/**
|
|
399
400
|
* Text content block used for model-provided reasoning summaries.
|
|
400
401
|
*
|
|
401
|
-
* @category
|
|
402
|
+
* @category models
|
|
402
403
|
* @since 4.0.0
|
|
403
404
|
*/
|
|
404
405
|
export type SummaryTextContent = {
|
|
@@ -460,7 +461,7 @@ type FilePathAnnotation = {
|
|
|
460
461
|
/**
|
|
461
462
|
* Citation and file-path annotations attached to output text content.
|
|
462
463
|
*
|
|
463
|
-
* @category
|
|
464
|
+
* @category models
|
|
464
465
|
* @since 4.0.0
|
|
465
466
|
*/
|
|
466
467
|
export type Annotation =
|
|
@@ -499,7 +500,7 @@ type OutputMessage = {
|
|
|
499
500
|
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
500
501
|
* optional reasoning text.
|
|
501
502
|
*
|
|
502
|
-
* @category
|
|
503
|
+
* @category models
|
|
503
504
|
* @since 4.0.0
|
|
504
505
|
*/
|
|
505
506
|
export type ReasoningItem = {
|
|
@@ -556,7 +557,7 @@ type ItemReference = {
|
|
|
556
557
|
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
557
558
|
* items, custom tool interactions, and item references.
|
|
558
559
|
*
|
|
559
|
-
* @category
|
|
560
|
+
* @category models
|
|
560
561
|
* @since 4.0.0
|
|
561
562
|
*/
|
|
562
563
|
export type InputItem =
|
|
@@ -597,7 +598,7 @@ type CustomToolParam = {
|
|
|
597
598
|
/**
|
|
598
599
|
* Tool definitions that can be supplied to a Responses-style request.
|
|
599
600
|
*
|
|
600
|
-
* @category
|
|
601
|
+
* @category models
|
|
601
602
|
* @since 4.0.0
|
|
602
603
|
*/
|
|
603
604
|
export type Tool =
|
|
@@ -648,7 +649,7 @@ export type TextResponseFormatConfiguration =
|
|
|
648
649
|
* Request options for creating a Responses-style response with an
|
|
649
650
|
* OpenAI-compatible provider.
|
|
650
651
|
*
|
|
651
|
-
* @category
|
|
652
|
+
* @category models
|
|
652
653
|
* @since 4.0.0
|
|
653
654
|
*/
|
|
654
655
|
export type CreateResponse = {
|
|
@@ -688,7 +689,7 @@ export type CreateResponse = {
|
|
|
688
689
|
/**
|
|
689
690
|
* Token accounting reported on Responses-style response objects.
|
|
690
691
|
*
|
|
691
|
-
* @category
|
|
692
|
+
* @category models
|
|
692
693
|
* @since 4.0.0
|
|
693
694
|
*/
|
|
694
695
|
export type ResponseUsage = {
|
|
@@ -709,7 +710,7 @@ type OutputItem =
|
|
|
709
710
|
* Responses-style response object returned by compatible providers or embedded
|
|
710
711
|
* in response stream lifecycle events.
|
|
711
712
|
*
|
|
712
|
-
* @category
|
|
713
|
+
* @category models
|
|
713
714
|
* @since 4.0.0
|
|
714
715
|
*/
|
|
715
716
|
export type Response = {
|
|
@@ -866,7 +867,7 @@ export type ResponseStreamEvent =
|
|
|
866
867
|
* string. The `index` field identifies the input item that produced this
|
|
867
868
|
* embedding.
|
|
868
869
|
*
|
|
869
|
-
* @category
|
|
870
|
+
* @category models
|
|
870
871
|
* @since 4.0.0
|
|
871
872
|
*/
|
|
872
873
|
export type Embedding = {
|
|
@@ -878,7 +879,7 @@ export type Embedding = {
|
|
|
878
879
|
/**
|
|
879
880
|
* Request payload for the embeddings endpoint.
|
|
880
881
|
*
|
|
881
|
-
* @category
|
|
882
|
+
* @category models
|
|
882
883
|
* @since 4.0.0
|
|
883
884
|
*/
|
|
884
885
|
export type CreateEmbeddingRequest = {
|
|
@@ -892,7 +893,7 @@ export type CreateEmbeddingRequest = {
|
|
|
892
893
|
/**
|
|
893
894
|
* Successful response payload returned by the embeddings endpoint.
|
|
894
895
|
*
|
|
895
|
-
* @category
|
|
896
|
+
* @category models
|
|
896
897
|
* @since 4.0.0
|
|
897
898
|
*/
|
|
898
899
|
export type CreateEmbeddingResponse = {
|
|
@@ -908,21 +909,21 @@ export type CreateEmbeddingResponse = {
|
|
|
908
909
|
/**
|
|
909
910
|
* JSON request body accepted by the embeddings endpoint.
|
|
910
911
|
*
|
|
911
|
-
* @category
|
|
912
|
+
* @category models
|
|
912
913
|
* @since 4.0.0
|
|
913
914
|
*/
|
|
914
915
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
915
916
|
/**
|
|
916
917
|
* Decoded successful embeddings response body.
|
|
917
918
|
*
|
|
918
|
-
* @category
|
|
919
|
+
* @category models
|
|
919
920
|
* @since 4.0.0
|
|
920
921
|
*/
|
|
921
922
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
922
923
|
/**
|
|
923
924
|
* Structured content parts accepted in chat completion messages.
|
|
924
925
|
*
|
|
925
|
-
* @category
|
|
926
|
+
* @category models
|
|
926
927
|
* @since 4.0.0
|
|
927
928
|
*/
|
|
928
929
|
export type ChatCompletionContentPart =
|
|
@@ -940,7 +941,7 @@ export type ChatCompletionContentPart =
|
|
|
940
941
|
/**
|
|
941
942
|
* Tool call data attached to an assistant chat completion message.
|
|
942
943
|
*
|
|
943
|
-
* @category
|
|
944
|
+
* @category models
|
|
944
945
|
* @since 4.0.0
|
|
945
946
|
*/
|
|
946
947
|
export type ChatCompletionRequestToolCall = {
|
|
@@ -954,7 +955,7 @@ export type ChatCompletionRequestToolCall = {
|
|
|
954
955
|
/**
|
|
955
956
|
* Message shapes accepted by the chat completions endpoint.
|
|
956
957
|
*
|
|
957
|
-
* @category
|
|
958
|
+
* @category models
|
|
958
959
|
* @since 4.0.0
|
|
959
960
|
*/
|
|
960
961
|
export type ChatCompletionRequestMessage =
|
|
@@ -971,7 +972,7 @@ export type ChatCompletionRequestMessage =
|
|
|
971
972
|
/**
|
|
972
973
|
* Function tool definition accepted by the chat completions endpoint.
|
|
973
974
|
*
|
|
974
|
-
* @category
|
|
975
|
+
* @category models
|
|
975
976
|
* @since 4.0.0
|
|
976
977
|
*/
|
|
977
978
|
export type ChatCompletionTool = {
|
|
@@ -1021,7 +1022,7 @@ export type ChatCompletionResponseFormat =
|
|
|
1021
1022
|
/**
|
|
1022
1023
|
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
1023
1024
|
*
|
|
1024
|
-
* @category
|
|
1025
|
+
* @category models
|
|
1025
1026
|
* @since 4.0.0
|
|
1026
1027
|
*/
|
|
1027
1028
|
export type ChatCompletionRequest = {
|
|
@@ -1047,14 +1048,14 @@ export type ChatCompletionRequest = {
|
|
|
1047
1048
|
/**
|
|
1048
1049
|
* JSON request body used by this client when creating a chat completion response.
|
|
1049
1050
|
*
|
|
1050
|
-
* @category
|
|
1051
|
+
* @category models
|
|
1051
1052
|
* @since 4.0.0
|
|
1052
1053
|
*/
|
|
1053
1054
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
1054
1055
|
/**
|
|
1055
1056
|
* Decoded successful chat completion response body returned by `createResponse`.
|
|
1056
1057
|
*
|
|
1057
|
-
* @category
|
|
1058
|
+
* @category models
|
|
1058
1059
|
* @since 4.0.0
|
|
1059
1060
|
*/
|
|
1060
1061
|
export type CreateResponse200 = ChatCompletionResponse
|
|
@@ -1161,35 +1162,35 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
1161
1162
|
/**
|
|
1162
1163
|
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1163
1164
|
*
|
|
1164
|
-
* @category
|
|
1165
|
+
* @category models
|
|
1165
1166
|
* @since 4.0.0
|
|
1166
1167
|
*/
|
|
1167
1168
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
1168
1169
|
/**
|
|
1169
1170
|
* Decoded message object from a non-streaming chat completion choice.
|
|
1170
1171
|
*
|
|
1171
|
-
* @category
|
|
1172
|
+
* @category models
|
|
1172
1173
|
* @since 4.0.0
|
|
1173
1174
|
*/
|
|
1174
1175
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
1175
1176
|
/**
|
|
1176
1177
|
* Decoded choice object returned by chat completion responses and chunks.
|
|
1177
1178
|
*
|
|
1178
|
-
* @category
|
|
1179
|
+
* @category models
|
|
1179
1180
|
* @since 4.0.0
|
|
1180
1181
|
*/
|
|
1181
1182
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
1182
1183
|
/**
|
|
1183
1184
|
* Decoded token usage summary returned by chat completions.
|
|
1184
1185
|
*
|
|
1185
|
-
* @category
|
|
1186
|
+
* @category models
|
|
1186
1187
|
* @since 4.0.0
|
|
1187
1188
|
*/
|
|
1188
1189
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
1189
1190
|
/**
|
|
1190
1191
|
* Decoded successful response from the chat completions endpoint.
|
|
1191
1192
|
*
|
|
1192
|
-
* @category
|
|
1193
|
+
* @category models
|
|
1193
1194
|
* @since 4.0.0
|
|
1194
1195
|
*/
|
|
1195
1196
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
@@ -1201,13 +1202,23 @@ export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
|
1201
1202
|
*/
|
|
1202
1203
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
1203
1204
|
/**
|
|
1204
|
-
*
|
|
1205
|
-
* sentinel.
|
|
1205
|
+
* A parsed chat completion event that does not match the expected chunk schema.
|
|
1206
1206
|
*
|
|
1207
1207
|
* @category streaming
|
|
1208
1208
|
* @since 4.0.0
|
|
1209
1209
|
*/
|
|
1210
|
-
export
|
|
1210
|
+
export interface UnknownChatCompletionEvent {
|
|
1211
|
+
readonly _tag: "UnknownChatCompletionEvent"
|
|
1212
|
+
readonly data: unknown
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Streaming chat completion event, including decoded chunks, unknown parsed
|
|
1216
|
+
* events, and the `[DONE]` sentinel.
|
|
1217
|
+
*
|
|
1218
|
+
* @category streaming
|
|
1219
|
+
* @since 4.0.0
|
|
1220
|
+
*/
|
|
1221
|
+
export type ChatCompletionStreamEvent = ChatCompletionChunk | UnknownChatCompletionEvent | "[DONE]"
|
|
1211
1222
|
|
|
1212
1223
|
const parseJson = (value: string): unknown => {
|
|
1213
1224
|
try {
|
|
@@ -1226,7 +1237,11 @@ const decodeChatCompletionSseData = (
|
|
|
1226
1237
|
return data
|
|
1227
1238
|
}
|
|
1228
1239
|
const parsed = parseJson(data)
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1240
|
+
if (parsed === undefined) {
|
|
1241
|
+
return undefined
|
|
1242
|
+
}
|
|
1243
|
+
return isChatCompletionChunk(parsed) ? parsed : {
|
|
1244
|
+
_tag: "UnknownChatCompletionEvent",
|
|
1245
|
+
data: parsed
|
|
1246
|
+
}
|
|
1232
1247
|
}
|
package/src/OpenAiConfig.ts
CHANGED
|
@@ -50,7 +50,7 @@ export declare namespace OpenAiConfig {
|
|
|
50
50
|
* Configuration consumed by OpenAI-compatible clients when they build or
|
|
51
51
|
* resolve the underlying HTTP client.
|
|
52
52
|
*
|
|
53
|
-
* @category
|
|
53
|
+
* @category services
|
|
54
54
|
* @since 4.0.0
|
|
55
55
|
*/
|
|
56
56
|
export interface Service {
|
|
@@ -46,7 +46,7 @@ type ModelConfig = Omit<ConfigOptions, "model"> & { readonly [x: string]: unknow
|
|
|
46
46
|
*
|
|
47
47
|
* @see {@link withConfigOverride} for scoping embedding request overrides
|
|
48
48
|
*
|
|
49
|
-
* @category
|
|
49
|
+
* @category services
|
|
50
50
|
* @since 4.0.0
|
|
51
51
|
*/
|
|
52
52
|
export class Config extends Context.Service<
|
|
@@ -18,7 +18,7 @@ import * as Option from "effect/Option"
|
|
|
18
18
|
import * as Predicate from "effect/Predicate"
|
|
19
19
|
import * as Rec from "effect/Record"
|
|
20
20
|
import * as Redactable from "effect/Redactable"
|
|
21
|
-
import
|
|
21
|
+
import * as Schema from "effect/Schema"
|
|
22
22
|
import * as AST from "effect/SchemaAST"
|
|
23
23
|
import * as Stream from "effect/Stream"
|
|
24
24
|
import type { Span } from "effect/Tracer"
|
|
@@ -36,6 +36,7 @@ import * as InternalUtilities from "./internal/utilities.ts"
|
|
|
36
36
|
import {
|
|
37
37
|
type Annotation,
|
|
38
38
|
type ChatCompletionContentPart,
|
|
39
|
+
type ChatCompletionRequestToolCall,
|
|
39
40
|
type CreateResponse,
|
|
40
41
|
type CreateResponse200,
|
|
41
42
|
type CreateResponse200Sse,
|
|
@@ -48,7 +49,8 @@ import {
|
|
|
48
49
|
type ReasoningItem,
|
|
49
50
|
type SummaryTextContent,
|
|
50
51
|
type TextResponseFormatConfiguration,
|
|
51
|
-
type Tool as OpenAiClientTool
|
|
52
|
+
type Tool as OpenAiClientTool,
|
|
53
|
+
type UnknownChatCompletionEvent
|
|
52
54
|
} from "./OpenAiClient.ts"
|
|
53
55
|
import { addGenAIAnnotations } from "./OpenAiTelemetry.ts"
|
|
54
56
|
|
|
@@ -109,7 +111,7 @@ type ModelConfig = Omit<ConfigOptions, "model"> & { readonly [x: string]: unknow
|
|
|
109
111
|
*
|
|
110
112
|
* @see {@link withConfigOverride} for scoping language model request overrides
|
|
111
113
|
*
|
|
112
|
-
* @category
|
|
114
|
+
* @category services
|
|
113
115
|
* @since 4.0.0
|
|
114
116
|
*/
|
|
115
117
|
export class Config extends Context.Service<
|
|
@@ -125,7 +127,7 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
125
127
|
/**
|
|
126
128
|
* OpenAI-compatible options for file prompt parts.
|
|
127
129
|
*
|
|
128
|
-
* @category
|
|
130
|
+
* @category models
|
|
129
131
|
* @since 4.0.0
|
|
130
132
|
*/
|
|
131
133
|
export interface FilePartOptions extends ProviderOptions {
|
|
@@ -143,7 +145,7 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
143
145
|
/**
|
|
144
146
|
* OpenAI-compatible options for reasoning prompt parts.
|
|
145
147
|
*
|
|
146
|
-
* @category
|
|
148
|
+
* @category models
|
|
147
149
|
* @since 4.0.0
|
|
148
150
|
*/
|
|
149
151
|
export interface ReasoningPartOptions extends ProviderOptions {
|
|
@@ -167,7 +169,7 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
167
169
|
/**
|
|
168
170
|
* OpenAI-compatible options for assistant tool-call prompt parts.
|
|
169
171
|
*
|
|
170
|
-
* @category
|
|
172
|
+
* @category models
|
|
171
173
|
* @since 4.0.0
|
|
172
174
|
*/
|
|
173
175
|
export interface ToolCallPartOptions extends ProviderOptions {
|
|
@@ -189,7 +191,7 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
189
191
|
/**
|
|
190
192
|
* OpenAI-compatible options for tool-result prompt parts.
|
|
191
193
|
*
|
|
192
|
-
* @category
|
|
194
|
+
* @category models
|
|
193
195
|
* @since 4.0.0
|
|
194
196
|
*/
|
|
195
197
|
export interface ToolResultPartOptions extends ProviderOptions {
|
|
@@ -211,7 +213,7 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
211
213
|
/**
|
|
212
214
|
* OpenAI-compatible options for text prompt parts.
|
|
213
215
|
*
|
|
214
|
-
* @category
|
|
216
|
+
* @category models
|
|
215
217
|
* @since 4.0.0
|
|
216
218
|
*/
|
|
217
219
|
export interface TextPartOptions extends ProviderOptions {
|
|
@@ -239,7 +241,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
239
241
|
/**
|
|
240
242
|
* OpenAI-compatible metadata attached to a complete text response part.
|
|
241
243
|
*
|
|
242
|
-
* @category
|
|
244
|
+
* @category models
|
|
243
245
|
* @since 4.0.0
|
|
244
246
|
*/
|
|
245
247
|
export interface TextPartMetadata extends ProviderMetadata {
|
|
@@ -271,7 +273,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
271
273
|
/**
|
|
272
274
|
* OpenAI-compatible metadata emitted when a streamed text part starts.
|
|
273
275
|
*
|
|
274
|
-
* @category
|
|
276
|
+
* @category models
|
|
275
277
|
* @since 4.0.0
|
|
276
278
|
*/
|
|
277
279
|
export interface TextStartPartMetadata extends ProviderMetadata {
|
|
@@ -289,7 +291,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
289
291
|
/**
|
|
290
292
|
* OpenAI-compatible metadata emitted when a streamed text part ends.
|
|
291
293
|
*
|
|
292
|
-
* @category
|
|
294
|
+
* @category models
|
|
293
295
|
* @since 4.0.0
|
|
294
296
|
*/
|
|
295
297
|
export interface TextEndPartMetadata extends ProviderMetadata {
|
|
@@ -311,7 +313,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
311
313
|
/**
|
|
312
314
|
* OpenAI-compatible metadata attached to a complete reasoning response part.
|
|
313
315
|
*
|
|
314
|
-
* @category
|
|
316
|
+
* @category models
|
|
315
317
|
* @since 4.0.0
|
|
316
318
|
*/
|
|
317
319
|
export interface ReasoningPartMetadata extends ProviderMetadata {
|
|
@@ -333,7 +335,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
333
335
|
/**
|
|
334
336
|
* OpenAI-compatible metadata emitted when a streamed reasoning part starts.
|
|
335
337
|
*
|
|
336
|
-
* @category
|
|
338
|
+
* @category models
|
|
337
339
|
* @since 4.0.0
|
|
338
340
|
*/
|
|
339
341
|
export interface ReasoningStartPartMetadata extends ProviderMetadata {
|
|
@@ -355,7 +357,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
355
357
|
/**
|
|
356
358
|
* OpenAI-compatible metadata emitted for a streamed reasoning delta.
|
|
357
359
|
*
|
|
358
|
-
* @category
|
|
360
|
+
* @category models
|
|
359
361
|
* @since 4.0.0
|
|
360
362
|
*/
|
|
361
363
|
export interface ReasoningDeltaPartMetadata extends ProviderMetadata {
|
|
@@ -373,7 +375,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
373
375
|
/**
|
|
374
376
|
* OpenAI-compatible metadata emitted when a streamed reasoning part ends.
|
|
375
377
|
*
|
|
376
|
-
* @category
|
|
378
|
+
* @category models
|
|
377
379
|
* @since 4.0.0
|
|
378
380
|
*/
|
|
379
381
|
export interface ReasoningEndPartMetadata extends ProviderMetadata {
|
|
@@ -395,7 +397,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
395
397
|
/**
|
|
396
398
|
* OpenAI-compatible metadata attached to tool-call response parts.
|
|
397
399
|
*
|
|
398
|
-
* @category
|
|
400
|
+
* @category models
|
|
399
401
|
* @since 4.0.0
|
|
400
402
|
*/
|
|
401
403
|
export interface ToolCallPartMetadata extends ProviderMetadata {
|
|
@@ -413,7 +415,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
413
415
|
/**
|
|
414
416
|
* OpenAI-compatible metadata attached to document source citations.
|
|
415
417
|
*
|
|
416
|
-
* @category
|
|
418
|
+
* @category models
|
|
417
419
|
* @since 4.0.0
|
|
418
420
|
*/
|
|
419
421
|
export interface DocumentSourcePartMetadata extends ProviderMetadata {
|
|
@@ -469,7 +471,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
469
471
|
/**
|
|
470
472
|
* OpenAI-compatible metadata attached to URL source citations.
|
|
471
473
|
*
|
|
472
|
-
* @category
|
|
474
|
+
* @category models
|
|
473
475
|
* @since 4.0.0
|
|
474
476
|
*/
|
|
475
477
|
export interface UrlSourcePartMetadata extends ProviderMetadata {
|
|
@@ -495,7 +497,7 @@ declare module "effect/unstable/ai/Response" {
|
|
|
495
497
|
/**
|
|
496
498
|
* OpenAI-compatible metadata attached to finish response parts.
|
|
497
499
|
*
|
|
498
|
-
* @category
|
|
500
|
+
* @category models
|
|
499
501
|
* @since 4.0.0
|
|
500
502
|
*/
|
|
501
503
|
export interface FinishPartMetadata extends ProviderMetadata {
|
|
@@ -629,6 +631,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
629
631
|
const [rawResponse, response] = yield* client.createResponse(request)
|
|
630
632
|
annotateResponse(options.span, rawResponse)
|
|
631
633
|
return yield* makeResponse({
|
|
634
|
+
options,
|
|
632
635
|
rawResponse,
|
|
633
636
|
response,
|
|
634
637
|
toolNameMapper
|
|
@@ -643,6 +646,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
643
646
|
annotateRequest(options.span, request)
|
|
644
647
|
const [response, stream] = yield* client.createResponseStream(request)
|
|
645
648
|
return yield* makeStreamResponse({
|
|
649
|
+
options,
|
|
646
650
|
stream,
|
|
647
651
|
response,
|
|
648
652
|
toolNameMapper
|
|
@@ -1097,6 +1101,11 @@ const buildHttpResponseDetails = (
|
|
|
1097
1101
|
|
|
1098
1102
|
type ResponseStreamEvent = CreateResponse200Sse
|
|
1099
1103
|
|
|
1104
|
+
const isUnknownChatCompletionEvent = (
|
|
1105
|
+
event: ResponseStreamEvent
|
|
1106
|
+
): event is UnknownChatCompletionEvent =>
|
|
1107
|
+
typeof event !== "string" && "_tag" in event && event._tag === "UnknownChatCompletionEvent"
|
|
1108
|
+
|
|
1100
1109
|
type ActiveToolCall = {
|
|
1101
1110
|
readonly id: string
|
|
1102
1111
|
name: string
|
|
@@ -1105,10 +1114,12 @@ type ActiveToolCall = {
|
|
|
1105
1114
|
|
|
1106
1115
|
const makeResponse = Effect.fnUntraced(
|
|
1107
1116
|
function*<Tools extends ReadonlyArray<Tool.Any>>({
|
|
1117
|
+
options,
|
|
1108
1118
|
rawResponse,
|
|
1109
1119
|
response,
|
|
1110
1120
|
toolNameMapper
|
|
1111
1121
|
}: {
|
|
1122
|
+
readonly options: LanguageModel.ProviderOptions
|
|
1112
1123
|
readonly rawResponse: CreateResponse200
|
|
1113
1124
|
readonly response: HttpClientResponse.HttpClientResponse
|
|
1114
1125
|
readonly toolNameMapper: Tool.NameMapper<Tools>
|
|
@@ -1147,9 +1158,9 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1147
1158
|
for (const [index, toolCall] of message.tool_calls.entries()) {
|
|
1148
1159
|
const toolId = toolCall.id ?? `${rawResponse.id}_tool_${index}`
|
|
1149
1160
|
const toolName = toolNameMapper.getCustomName(toolCall.function?.name ?? "unknown_tool")
|
|
1150
|
-
const
|
|
1151
|
-
const
|
|
1152
|
-
try: () => Tool.unsafeSecureJsonParse(
|
|
1161
|
+
const toolParamsJson = toolCall.function?.arguments ?? "{}"
|
|
1162
|
+
const toolParams = yield* Effect.try({
|
|
1163
|
+
try: () => Tool.unsafeSecureJsonParse(toolParamsJson),
|
|
1153
1164
|
catch: (cause) =>
|
|
1154
1165
|
AiError.make({
|
|
1155
1166
|
module: "OpenAiLanguageModel",
|
|
@@ -1161,6 +1172,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1161
1172
|
})
|
|
1162
1173
|
})
|
|
1163
1174
|
})
|
|
1175
|
+
const params = yield* transformToolCallParams(options.tools, toolName, toolParams)
|
|
1164
1176
|
hasToolCalls = true
|
|
1165
1177
|
parts.push({
|
|
1166
1178
|
type: "tool-call",
|
|
@@ -1193,10 +1205,12 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1193
1205
|
|
|
1194
1206
|
const makeStreamResponse = Effect.fnUntraced(
|
|
1195
1207
|
function*<Tools extends ReadonlyArray<Tool.Any>>({
|
|
1208
|
+
options,
|
|
1196
1209
|
stream,
|
|
1197
1210
|
response,
|
|
1198
1211
|
toolNameMapper
|
|
1199
1212
|
}: {
|
|
1213
|
+
readonly options: LanguageModel.ProviderOptions
|
|
1200
1214
|
readonly stream: Stream.Stream<ResponseStreamEvent, AiError.AiError>
|
|
1201
1215
|
readonly response: HttpClientResponse.HttpClientResponse
|
|
1202
1216
|
readonly toolNameMapper: Tool.NameMapper<Tools>
|
|
@@ -1238,7 +1252,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1238
1252
|
|
|
1239
1253
|
for (const toolCall of Object.values(activeToolCalls)) {
|
|
1240
1254
|
const toolParams = toolCall.arguments.length > 0 ? toolCall.arguments : "{}"
|
|
1241
|
-
const
|
|
1255
|
+
const parsedParams = yield* Effect.try({
|
|
1242
1256
|
try: () => Tool.unsafeSecureJsonParse(toolParams),
|
|
1243
1257
|
catch: (cause) =>
|
|
1244
1258
|
AiError.make({
|
|
@@ -1251,6 +1265,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1251
1265
|
})
|
|
1252
1266
|
})
|
|
1253
1267
|
})
|
|
1268
|
+
const params = yield* transformToolCallParams(options.tools, toolCall.name, parsedParams)
|
|
1254
1269
|
parts.push({ type: "tool-params-end", id: toolCall.id })
|
|
1255
1270
|
parts.push({
|
|
1256
1271
|
type: "tool-call",
|
|
@@ -1275,6 +1290,12 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1275
1290
|
return parts
|
|
1276
1291
|
}
|
|
1277
1292
|
|
|
1293
|
+
// Keep unknown events available to direct client consumers; this layer
|
|
1294
|
+
// cannot translate provider-specific data into portable stream parts.
|
|
1295
|
+
if (isUnknownChatCompletionEvent(event)) {
|
|
1296
|
+
return parts
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1278
1299
|
if (event.service_tier !== undefined) {
|
|
1279
1300
|
serviceTier = event.service_tier
|
|
1280
1301
|
}
|
|
@@ -1462,6 +1483,12 @@ const unsupportedSchemaError = (error: unknown, method: string): AiError.AiError
|
|
|
1462
1483
|
})
|
|
1463
1484
|
})
|
|
1464
1485
|
|
|
1486
|
+
const tryCodecTransform = <S extends Schema.Constraint>(schema: S, method: string) =>
|
|
1487
|
+
Effect.try({
|
|
1488
|
+
try: () => toCodecOpenAI(schema),
|
|
1489
|
+
catch: (error) => unsupportedSchemaError(error, method)
|
|
1490
|
+
})
|
|
1491
|
+
|
|
1465
1492
|
const tryJsonSchema = <S extends Schema.Constraint>(schema: S, method: string) =>
|
|
1466
1493
|
Effect.try({
|
|
1467
1494
|
try: () => Tool.getJsonSchemaFromSchema(schema, { transformer: toCodecOpenAI }),
|
|
@@ -1474,6 +1501,42 @@ const tryToolJsonSchema = <T extends Tool.Any>(tool: T, method: string) =>
|
|
|
1474
1501
|
catch: (error) => unsupportedSchemaError(error, method)
|
|
1475
1502
|
})
|
|
1476
1503
|
|
|
1504
|
+
const transformToolCallParams = Effect.fnUntraced(function*<Tools extends ReadonlyArray<Tool.Any>>(
|
|
1505
|
+
tools: Tools,
|
|
1506
|
+
toolName: string,
|
|
1507
|
+
toolParams: unknown
|
|
1508
|
+
): Effect.fn.Return<unknown, AiError.AiError> {
|
|
1509
|
+
const tool = tools.find((tool) => tool.name === toolName)
|
|
1510
|
+
|
|
1511
|
+
if (Predicate.isUndefined(tool)) {
|
|
1512
|
+
return yield* AiError.make({
|
|
1513
|
+
module: "OpenAiLanguageModel",
|
|
1514
|
+
method: "makeResponse",
|
|
1515
|
+
reason: new AiError.ToolNotFoundError({
|
|
1516
|
+
toolName,
|
|
1517
|
+
availableTools: tools.map((tool) => tool.name)
|
|
1518
|
+
})
|
|
1519
|
+
})
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
const { codec } = yield* tryCodecTransform(tool.parametersSchema, "makeResponse")
|
|
1523
|
+
const transform = Schema.decodeEffect(codec)
|
|
1524
|
+
|
|
1525
|
+
return yield* (
|
|
1526
|
+
transform(toolParams) as Effect.Effect<unknown, Schema.SchemaError>
|
|
1527
|
+
).pipe(Effect.mapError((error) =>
|
|
1528
|
+
AiError.make({
|
|
1529
|
+
module: "OpenAiLanguageModel",
|
|
1530
|
+
method: "makeResponse",
|
|
1531
|
+
reason: new AiError.ToolParameterValidationError({
|
|
1532
|
+
toolName,
|
|
1533
|
+
toolParams,
|
|
1534
|
+
description: error.issue.toString()
|
|
1535
|
+
})
|
|
1536
|
+
})
|
|
1537
|
+
))
|
|
1538
|
+
})
|
|
1539
|
+
|
|
1477
1540
|
const prepareTools = Effect.fnUntraced(function*<Tools extends ReadonlyArray<Tool.Any>>({
|
|
1478
1541
|
config,
|
|
1479
1542
|
options,
|
|
@@ -1724,7 +1787,24 @@ const toChatMessages = (
|
|
|
1724
1787
|
const messages: Array<CreateResponseRequestJson["messages"][number]> = []
|
|
1725
1788
|
|
|
1726
1789
|
for (const item of input) {
|
|
1727
|
-
|
|
1790
|
+
if (Predicate.hasProperty(item, "type") && item.type === "function_call") {
|
|
1791
|
+
const previous = messages.at(-1)
|
|
1792
|
+
const toolCall = toChatToolCall(item)
|
|
1793
|
+
if (previous?.role === "assistant" && previous.tool_calls !== undefined) {
|
|
1794
|
+
messages[messages.length - 1] = {
|
|
1795
|
+
...previous,
|
|
1796
|
+
tool_calls: [...previous.tool_calls, toolCall]
|
|
1797
|
+
}
|
|
1798
|
+
} else {
|
|
1799
|
+
messages.push({
|
|
1800
|
+
role: "assistant",
|
|
1801
|
+
content: null,
|
|
1802
|
+
tool_calls: [toolCall]
|
|
1803
|
+
})
|
|
1804
|
+
}
|
|
1805
|
+
} else {
|
|
1806
|
+
messages.push(...toChatMessagesFromItem(item))
|
|
1807
|
+
}
|
|
1728
1808
|
}
|
|
1729
1809
|
|
|
1730
1810
|
return messages
|
|
@@ -1752,14 +1832,7 @@ const toChatMessagesFromItem = (
|
|
|
1752
1832
|
return [{
|
|
1753
1833
|
role: "assistant",
|
|
1754
1834
|
content: null,
|
|
1755
|
-
tool_calls: [
|
|
1756
|
-
id: item.call_id,
|
|
1757
|
-
type: "function",
|
|
1758
|
-
function: {
|
|
1759
|
-
name: item.name,
|
|
1760
|
-
arguments: item.arguments
|
|
1761
|
-
}
|
|
1762
|
-
}]
|
|
1835
|
+
tool_calls: [toChatToolCall(item)]
|
|
1763
1836
|
}]
|
|
1764
1837
|
}
|
|
1765
1838
|
|
|
@@ -1777,6 +1850,17 @@ const toChatMessagesFromItem = (
|
|
|
1777
1850
|
}
|
|
1778
1851
|
}
|
|
1779
1852
|
|
|
1853
|
+
const toChatToolCall = (
|
|
1854
|
+
item: Extract<InputItem, { readonly type: "function_call" }>
|
|
1855
|
+
): ChatCompletionRequestToolCall => ({
|
|
1856
|
+
id: item.call_id,
|
|
1857
|
+
type: "function",
|
|
1858
|
+
function: {
|
|
1859
|
+
name: item.name,
|
|
1860
|
+
arguments: item.arguments
|
|
1861
|
+
}
|
|
1862
|
+
})
|
|
1863
|
+
|
|
1780
1864
|
const toAssistantChatMessageContent = (
|
|
1781
1865
|
content: ReadonlyArray<{
|
|
1782
1866
|
readonly type: string
|