@effect/ai-openai-compat 4.0.0-beta.101 → 4.0.0-beta.103

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.
@@ -11,7 +11,7 @@ import * as Array from "effect/Array"
11
11
  import type * as Config from "effect/Config"
12
12
  import * as Context from "effect/Context"
13
13
  import * as Effect from "effect/Effect"
14
- import { identity, pipe } from "effect/Function"
14
+ import { identity } from "effect/Function"
15
15
  import * as Layer from "effect/Layer"
16
16
  import * as Redacted from "effect/Redacted"
17
17
  import * as Schema from "effect/Schema"
@@ -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 models
37
+ * @category services
38
38
  * @since 4.0.0
39
39
  */
40
40
  export interface Service {
@@ -99,10 +99,15 @@ export type Options = {
99
99
  }
100
100
 
101
101
  const RedactedOpenAiHeaders = {
102
- OpenAiOrganization: "OpenAI-Organization",
103
- OpenAiProject: "OpenAI-Project"
102
+ OpenAiOrganization: "openai-organization",
103
+ OpenAiProject: "openai-project"
104
104
  }
105
105
 
106
+ const withRedactedHeaders = Effect.updateService(
107
+ Headers.CurrentRedactedNames,
108
+ Array.appendAll(Object.values(RedactedOpenAiHeaders))
109
+ )
110
+
106
111
  /**
107
112
  * Constructs an OpenAI-compatible client service from explicit options.
108
113
  *
@@ -175,24 +180,27 @@ export const make = Effect.fnUntraced(
175
180
  [body: CreateResponse200, response: HttpClientResponse.HttpClientResponse],
176
181
  AiError.AiError
177
182
  > =>
178
- Effect.flatMap(resolveHttpClient, (client) =>
179
- pipe(
180
- HttpClientRequest.post("/chat/completions"),
181
- HttpClientRequest.bodyJsonUnsafe(payload),
182
- HttpClient.filterStatusOk(client).execute,
183
- Effect.flatMap((response) =>
184
- Effect.map(decodeResponse(response), (
185
- body
186
- ): [CreateResponse200, HttpClientResponse.HttpClientResponse] => [
187
- body,
188
- response
189
- ])
190
- ),
191
- Effect.catchTags({
192
- HttpClientError: (error) => Errors.mapHttpClientError(error, "createResponse"),
193
- SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "createResponse"))
194
- })
195
- ))
183
+ resolveHttpClient.pipe(
184
+ Effect.flatMap((client) =>
185
+ HttpClientRequest.post("/chat/completions").pipe(
186
+ HttpClientRequest.bodyJsonUnsafe(payload),
187
+ HttpClient.filterStatusOk(client).execute,
188
+ Effect.flatMap((response) =>
189
+ Effect.map(decodeResponse(response), (
190
+ body
191
+ ): [CreateResponse200, HttpClientResponse.HttpClientResponse] => [
192
+ body,
193
+ response
194
+ ])
195
+ ),
196
+ Effect.catchTags({
197
+ HttpClientError: (error) => Errors.mapHttpClientError(error, "createResponse"),
198
+ SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "createResponse"))
199
+ })
200
+ )
201
+ ),
202
+ withRedactedHeaders
203
+ )
196
204
 
197
205
  const buildResponseStream = (
198
206
  response: HttpClientResponse.HttpClientResponse
@@ -210,6 +218,7 @@ export const make = Effect.fnUntraced(
210
218
  Stream.takeUntil((event) => event === "[DONE]"),
211
219
  Stream.catchTags({
212
220
  Retry: (error) => Stream.die(error),
221
+ SseError: (error) => Stream.fail(Errors.mapSseError(error, "createResponseStream")),
213
222
  HttpClientError: (error) => Stream.fromEffect(Errors.mapHttpClientError(error, "createResponseStream"))
214
223
  })
215
224
  ) as any
@@ -217,40 +226,46 @@ export const make = Effect.fnUntraced(
217
226
  }
218
227
 
219
228
  const createResponseStream: Service["createResponseStream"] = (payload) =>
220
- Effect.flatMap(resolveHttpClient, (client) =>
221
- pipe(
222
- HttpClientRequest.post("/chat/completions"),
223
- HttpClientRequest.bodyJsonUnsafe({
224
- ...payload,
225
- stream: true,
226
- stream_options: {
227
- include_usage: true
228
- }
229
- }),
230
- HttpClient.filterStatusOk(client).execute,
231
- Effect.map(buildResponseStream),
232
- Effect.catchTag(
233
- "HttpClientError",
234
- (error) => Errors.mapHttpClientError(error, "createResponseStream")
229
+ resolveHttpClient.pipe(
230
+ Effect.flatMap((client) =>
231
+ HttpClientRequest.post("/chat/completions").pipe(
232
+ HttpClientRequest.bodyJsonUnsafe({
233
+ ...payload,
234
+ stream: true,
235
+ stream_options: {
236
+ include_usage: true
237
+ }
238
+ }),
239
+ HttpClient.filterStatusOk(client).execute,
240
+ Effect.map(buildResponseStream),
241
+ Effect.catchTag(
242
+ "HttpClientError",
243
+ (error) => Errors.mapHttpClientError(error, "createResponseStream")
244
+ )
235
245
  )
236
- ))
246
+ ),
247
+ withRedactedHeaders
248
+ )
237
249
 
238
250
  const decodeEmbedding = HttpClientResponse.schemaBodyJson(CreateEmbeddingResponseSchema)
239
251
 
240
252
  const createEmbedding = (
241
253
  payload: CreateEmbeddingRequestJson
242
254
  ): Effect.Effect<CreateEmbedding200, AiError.AiError> =>
243
- Effect.flatMap(resolveHttpClient, (client) =>
244
- pipe(
245
- HttpClientRequest.post("/embeddings"),
246
- HttpClientRequest.bodyJsonUnsafe(payload),
247
- HttpClient.filterStatusOk(client).execute,
248
- Effect.flatMap(decodeEmbedding),
249
- Effect.catchTags({
250
- HttpClientError: (error) => Errors.mapHttpClientError(error, "createEmbedding"),
251
- SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "createEmbedding"))
252
- })
253
- ))
255
+ resolveHttpClient.pipe(
256
+ Effect.flatMap((client) =>
257
+ HttpClientRequest.post("/embeddings").pipe(
258
+ HttpClientRequest.bodyJsonUnsafe(payload),
259
+ HttpClient.filterStatusOk(client).execute,
260
+ Effect.flatMap(decodeEmbedding),
261
+ Effect.catchTags({
262
+ HttpClientError: (error) => Errors.mapHttpClientError(error, "createEmbedding"),
263
+ SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "createEmbedding"))
264
+ })
265
+ )
266
+ ),
267
+ withRedactedHeaders
268
+ )
254
269
 
255
270
  return OpenAiClient.of({
256
271
  client: httpClient,
@@ -259,10 +274,7 @@ export const make = Effect.fnUntraced(
259
274
  createEmbedding
260
275
  })
261
276
  },
262
- Effect.updateService(
263
- Headers.CurrentRedactedNames,
264
- Array.appendAll(Object.values(RedactedOpenAiHeaders))
265
- )
277
+ withRedactedHeaders
266
278
  )
267
279
 
268
280
  /**
@@ -340,7 +352,7 @@ type JsonObject = { readonly [x: string]: Schema.Json }
340
352
  /**
341
353
  * Optional response fields that can be requested with the `include` parameter.
342
354
  *
343
- * @category response
355
+ * @category models
344
356
  * @since 4.0.0
345
357
  */
346
358
  export type IncludeEnum =
@@ -379,7 +391,7 @@ type InputFileContent = {
379
391
  /**
380
392
  * Content blocks accepted in input messages.
381
393
  *
382
- * @category request
394
+ * @category models
383
395
  * @since 4.0.0
384
396
  */
385
397
  export type InputContent = InputTextContent | InputImageContent | InputFileContent
@@ -387,7 +399,7 @@ export type InputContent = InputTextContent | InputImageContent | InputFileConte
387
399
  /**
388
400
  * Text content block used for model-provided reasoning summaries.
389
401
  *
390
- * @category response
402
+ * @category models
391
403
  * @since 4.0.0
392
404
  */
393
405
  export type SummaryTextContent = {
@@ -449,7 +461,7 @@ type FilePathAnnotation = {
449
461
  /**
450
462
  * Citation and file-path annotations attached to output text content.
451
463
  *
452
- * @category response
464
+ * @category models
453
465
  * @since 4.0.0
454
466
  */
455
467
  export type Annotation =
@@ -488,7 +500,7 @@ type OutputMessage = {
488
500
  * Reasoning output item containing encrypted reasoning content, summaries, and
489
501
  * optional reasoning text.
490
502
  *
491
- * @category response
503
+ * @category models
492
504
  * @since 4.0.0
493
505
  */
494
506
  export type ReasoningItem = {
@@ -545,7 +557,7 @@ type ItemReference = {
545
557
  * Supports input messages, output messages, tool calls, tool outputs, reasoning
546
558
  * items, custom tool interactions, and item references.
547
559
  *
548
- * @category request
560
+ * @category models
549
561
  * @since 4.0.0
550
562
  */
551
563
  export type InputItem =
@@ -586,7 +598,7 @@ type CustomToolParam = {
586
598
  /**
587
599
  * Tool definitions that can be supplied to a Responses-style request.
588
600
  *
589
- * @category request
601
+ * @category models
590
602
  * @since 4.0.0
591
603
  */
592
604
  export type Tool =
@@ -637,7 +649,7 @@ export type TextResponseFormatConfiguration =
637
649
  * Request options for creating a Responses-style response with an
638
650
  * OpenAI-compatible provider.
639
651
  *
640
- * @category request
652
+ * @category models
641
653
  * @since 4.0.0
642
654
  */
643
655
  export type CreateResponse = {
@@ -677,7 +689,7 @@ export type CreateResponse = {
677
689
  /**
678
690
  * Token accounting reported on Responses-style response objects.
679
691
  *
680
- * @category response
692
+ * @category models
681
693
  * @since 4.0.0
682
694
  */
683
695
  export type ResponseUsage = {
@@ -698,7 +710,7 @@ type OutputItem =
698
710
  * Responses-style response object returned by compatible providers or embedded
699
711
  * in response stream lifecycle events.
700
712
  *
701
- * @category response
713
+ * @category models
702
714
  * @since 4.0.0
703
715
  */
704
716
  export type Response = {
@@ -855,7 +867,7 @@ export type ResponseStreamEvent =
855
867
  * string. The `index` field identifies the input item that produced this
856
868
  * embedding.
857
869
  *
858
- * @category response
870
+ * @category models
859
871
  * @since 4.0.0
860
872
  */
861
873
  export type Embedding = {
@@ -867,7 +879,7 @@ export type Embedding = {
867
879
  /**
868
880
  * Request payload for the embeddings endpoint.
869
881
  *
870
- * @category request
882
+ * @category models
871
883
  * @since 4.0.0
872
884
  */
873
885
  export type CreateEmbeddingRequest = {
@@ -881,7 +893,7 @@ export type CreateEmbeddingRequest = {
881
893
  /**
882
894
  * Successful response payload returned by the embeddings endpoint.
883
895
  *
884
- * @category response
896
+ * @category models
885
897
  * @since 4.0.0
886
898
  */
887
899
  export type CreateEmbeddingResponse = {
@@ -897,21 +909,21 @@ export type CreateEmbeddingResponse = {
897
909
  /**
898
910
  * JSON request body accepted by the embeddings endpoint.
899
911
  *
900
- * @category request
912
+ * @category models
901
913
  * @since 4.0.0
902
914
  */
903
915
  export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
904
916
  /**
905
917
  * Decoded successful embeddings response body.
906
918
  *
907
- * @category response
919
+ * @category models
908
920
  * @since 4.0.0
909
921
  */
910
922
  export type CreateEmbedding200 = CreateEmbeddingResponse
911
923
  /**
912
924
  * Structured content parts accepted in chat completion messages.
913
925
  *
914
- * @category request
926
+ * @category models
915
927
  * @since 4.0.0
916
928
  */
917
929
  export type ChatCompletionContentPart =
@@ -929,7 +941,7 @@ export type ChatCompletionContentPart =
929
941
  /**
930
942
  * Tool call data attached to an assistant chat completion message.
931
943
  *
932
- * @category request
944
+ * @category models
933
945
  * @since 4.0.0
934
946
  */
935
947
  export type ChatCompletionRequestToolCall = {
@@ -943,7 +955,7 @@ export type ChatCompletionRequestToolCall = {
943
955
  /**
944
956
  * Message shapes accepted by the chat completions endpoint.
945
957
  *
946
- * @category request
958
+ * @category models
947
959
  * @since 4.0.0
948
960
  */
949
961
  export type ChatCompletionRequestMessage =
@@ -960,7 +972,7 @@ export type ChatCompletionRequestMessage =
960
972
  /**
961
973
  * Function tool definition accepted by the chat completions endpoint.
962
974
  *
963
- * @category request
975
+ * @category models
964
976
  * @since 4.0.0
965
977
  */
966
978
  export type ChatCompletionTool = {
@@ -1010,7 +1022,7 @@ export type ChatCompletionResponseFormat =
1010
1022
  /**
1011
1023
  * Request payload for the OpenAI-compatible chat completions endpoint.
1012
1024
  *
1013
- * @category request
1025
+ * @category models
1014
1026
  * @since 4.0.0
1015
1027
  */
1016
1028
  export type ChatCompletionRequest = {
@@ -1036,14 +1048,14 @@ export type ChatCompletionRequest = {
1036
1048
  /**
1037
1049
  * JSON request body used by this client when creating a chat completion response.
1038
1050
  *
1039
- * @category request
1051
+ * @category models
1040
1052
  * @since 4.0.0
1041
1053
  */
1042
1054
  export type CreateResponseRequestJson = ChatCompletionRequest
1043
1055
  /**
1044
1056
  * Decoded successful chat completion response body returned by `createResponse`.
1045
1057
  *
1046
- * @category response
1058
+ * @category models
1047
1059
  * @since 4.0.0
1048
1060
  */
1049
1061
  export type CreateResponse200 = ChatCompletionResponse
@@ -1056,8 +1068,8 @@ export type CreateResponse200 = ChatCompletionResponse
1056
1068
  export type CreateResponse200Sse = ChatCompletionStreamEvent
1057
1069
 
1058
1070
  const EmbeddingSchema = Schema.Struct({
1059
- embedding: Schema.Union([Schema.Array(Schema.Number), Schema.String]),
1060
- index: Schema.Number,
1071
+ embedding: Schema.Union([Schema.Array(Schema.Finite), Schema.String]),
1072
+ index: Schema.Int,
1061
1073
  object: Schema.optionalKey(Schema.String)
1062
1074
  })
1063
1075
 
@@ -1066,8 +1078,8 @@ const CreateEmbeddingResponseSchema = Schema.Struct({
1066
1078
  model: Schema.String,
1067
1079
  object: Schema.optionalKey(Schema.Literal("list")),
1068
1080
  usage: Schema.optionalKey(Schema.Struct({
1069
- prompt_tokens: Schema.Number,
1070
- total_tokens: Schema.Number
1081
+ prompt_tokens: Schema.Int,
1082
+ total_tokens: Schema.Int
1071
1083
  }))
1072
1084
  })
1073
1085
 
@@ -1086,14 +1098,14 @@ const ChatCompletionToolFunctionDelta = Schema.Struct({
1086
1098
 
1087
1099
  const ChatCompletionToolCall = Schema.Struct({
1088
1100
  id: Schema.optionalKey(Schema.String),
1089
- index: Schema.optionalKey(Schema.Number),
1101
+ index: Schema.optionalKey(Schema.Int),
1090
1102
  type: Schema.optionalKey(Schema.String),
1091
1103
  function: Schema.optionalKey(ChatCompletionToolFunction)
1092
1104
  })
1093
1105
 
1094
1106
  const ChatCompletionToolCallDelta = Schema.Struct({
1095
1107
  id: Schema.optionalKey(Schema.String),
1096
- index: Schema.optionalKey(Schema.Number),
1108
+ index: Schema.optionalKey(Schema.Int),
1097
1109
  type: Schema.optionalKey(Schema.String),
1098
1110
  function: Schema.optionalKey(ChatCompletionToolFunctionDelta)
1099
1111
  })
@@ -1115,16 +1127,16 @@ const ChatCompletionDelta = Schema.Struct({
1115
1127
  })
1116
1128
 
1117
1129
  const ChatCompletionChoice = Schema.Struct({
1118
- index: Schema.Number,
1130
+ index: Schema.Int,
1119
1131
  finish_reason: Schema.optionalKey(Schema.NullOr(Schema.String)),
1120
1132
  message: Schema.optionalKey(ChatCompletionMessage),
1121
1133
  delta: Schema.optionalKey(ChatCompletionDelta)
1122
1134
  })
1123
1135
 
1124
1136
  const ChatCompletionUsage = Schema.Struct({
1125
- prompt_tokens: Schema.Number,
1126
- completion_tokens: Schema.Number,
1127
- total_tokens: Schema.Number,
1137
+ prompt_tokens: Schema.Int,
1138
+ completion_tokens: Schema.Int,
1139
+ total_tokens: Schema.Int,
1128
1140
  prompt_tokens_details: Schema.optionalKey(Schema.Any),
1129
1141
  completion_tokens_details: Schema.optionalKey(Schema.Any)
1130
1142
  })
@@ -1132,7 +1144,7 @@ const ChatCompletionUsage = Schema.Struct({
1132
1144
  const ChatCompletionResponse = Schema.Struct({
1133
1145
  id: Schema.String,
1134
1146
  model: Schema.String,
1135
- created: Schema.Number,
1147
+ created: Schema.Int,
1136
1148
  choices: Schema.Array(ChatCompletionChoice),
1137
1149
  usage: Schema.optionalKey(Schema.NullOr(ChatCompletionUsage)),
1138
1150
  service_tier: Schema.optionalKey(Schema.String)
@@ -1141,7 +1153,7 @@ const ChatCompletionResponse = Schema.Struct({
1141
1153
  const ChatCompletionChunk = Schema.Struct({
1142
1154
  id: Schema.String,
1143
1155
  model: Schema.String,
1144
- created: Schema.Number,
1156
+ created: Schema.Int,
1145
1157
  choices: Schema.Array(ChatCompletionChoice),
1146
1158
  usage: Schema.optionalKey(Schema.NullOr(ChatCompletionUsage)),
1147
1159
  service_tier: Schema.optionalKey(Schema.String)
@@ -1150,35 +1162,35 @@ const ChatCompletionChunk = Schema.Struct({
1150
1162
  /**
1151
1163
  * Decoded tool-call object from a chat completion response or streaming chunk.
1152
1164
  *
1153
- * @category response
1165
+ * @category models
1154
1166
  * @since 4.0.0
1155
1167
  */
1156
1168
  export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
1157
1169
  /**
1158
1170
  * Decoded message object from a non-streaming chat completion choice.
1159
1171
  *
1160
- * @category response
1172
+ * @category models
1161
1173
  * @since 4.0.0
1162
1174
  */
1163
1175
  export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
1164
1176
  /**
1165
1177
  * Decoded choice object returned by chat completion responses and chunks.
1166
1178
  *
1167
- * @category response
1179
+ * @category models
1168
1180
  * @since 4.0.0
1169
1181
  */
1170
1182
  export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
1171
1183
  /**
1172
1184
  * Decoded token usage summary returned by chat completions.
1173
1185
  *
1174
- * @category response
1186
+ * @category models
1175
1187
  * @since 4.0.0
1176
1188
  */
1177
1189
  export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
1178
1190
  /**
1179
1191
  * Decoded successful response from the chat completions endpoint.
1180
1192
  *
1181
- * @category response
1193
+ * @category models
1182
1194
  * @since 4.0.0
1183
1195
  */
1184
1196
  export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
@@ -1190,13 +1202,23 @@ export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
1190
1202
  */
1191
1203
  export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
1192
1204
  /**
1193
- * Streaming chat completion event, including decoded chunks and the `[DONE]`
1194
- * sentinel.
1205
+ * A parsed chat completion event that does not match the expected chunk schema.
1195
1206
  *
1196
1207
  * @category streaming
1197
1208
  * @since 4.0.0
1198
1209
  */
1199
- export type ChatCompletionStreamEvent = ChatCompletionChunk | "[DONE]"
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]"
1200
1222
 
1201
1223
  const parseJson = (value: string): unknown => {
1202
1224
  try {
@@ -1215,7 +1237,11 @@ const decodeChatCompletionSseData = (
1215
1237
  return data
1216
1238
  }
1217
1239
  const parsed = parseJson(data)
1218
- return isChatCompletionChunk(parsed)
1219
- ? parsed
1220
- : undefined
1240
+ if (parsed === undefined) {
1241
+ return undefined
1242
+ }
1243
+ return isChatCompletionChunk(parsed) ? parsed : {
1244
+ _tag: "UnknownChatCompletionEvent",
1245
+ data: parsed
1246
+ }
1221
1247
  }
@@ -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 models
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 context
49
+ * @category services
50
50
  * @since 4.0.0
51
51
  */
52
52
  export class Config extends Context.Service<