@effect/ai-openai-compat 4.0.0-beta.7 → 4.0.0-beta.70
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 +198 -50
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +57 -9
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +76 -10
- package/dist/OpenAiConfig.d.ts.map +1 -1
- package/dist/OpenAiConfig.js +44 -7
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +90 -0
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
- package/dist/OpenAiEmbeddingModel.js +122 -0
- package/dist/OpenAiEmbeddingModel.js.map +1 -0
- package/dist/OpenAiError.d.ts +109 -35
- package/dist/OpenAiError.d.ts.map +1 -1
- package/dist/OpenAiError.js +14 -1
- package/dist/OpenAiLanguageModel.d.ts +219 -13
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +42 -19
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +35 -23
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +6 -4
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.d.ts +70 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +70 -6
- package/dist/index.js.map +1 -1
- package/dist/internal/errors.js +4 -4
- package/dist/internal/errors.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +232 -49
- package/src/OpenAiConfig.ts +77 -11
- package/src/OpenAiEmbeddingModel.ts +208 -0
- package/src/OpenAiError.ts +111 -35
- package/src/OpenAiLanguageModel.ts +273 -30
- package/src/OpenAiTelemetry.ts +36 -24
- package/src/index.ts +71 -6
- package/src/internal/errors.ts +4 -4
package/src/OpenAiClient.ts
CHANGED
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `OpenAiClient` module provides an Effect service for calling
|
|
3
|
+
* OpenAI-compatible chat completions and embeddings APIs. It builds on the
|
|
4
|
+
* Effect HTTP client, adds authentication and OpenAI header handling, and
|
|
5
|
+
* exposes typed helpers for regular responses, server-sent event streaming, and
|
|
6
|
+
* embedding requests.
|
|
7
|
+
*
|
|
8
|
+
* **Common tasks**
|
|
9
|
+
*
|
|
10
|
+
* - Create a client service directly with {@link make}
|
|
11
|
+
* - Provide the service as a layer with {@link layer} or {@link layerConfig}
|
|
12
|
+
* - Send non-streaming chat completion requests with `createResponse`
|
|
13
|
+
* - Send streaming chat completion requests with `createResponseStream`
|
|
14
|
+
* - Generate embeddings with `createEmbedding`
|
|
15
|
+
* - Reuse the exported request and response types when integrating compatible providers
|
|
16
|
+
*
|
|
17
|
+
* **Gotchas**
|
|
18
|
+
*
|
|
19
|
+
* - The default base URL is `https://api.openai.com/v1`; set `apiUrl` for other
|
|
20
|
+
* OpenAI-compatible providers.
|
|
21
|
+
* - `createResponseStream` forces `stream: true` and requests usage events with
|
|
22
|
+
* `stream_options.include_usage`.
|
|
23
|
+
* - HTTP and schema decoding failures are mapped into `AiError`.
|
|
24
|
+
*
|
|
25
|
+
* @since 4.0.0
|
|
3
26
|
*/
|
|
4
27
|
import * as Array from "effect/Array"
|
|
5
28
|
import type * as Config from "effect/Config"
|
|
29
|
+
import * as Context from "effect/Context"
|
|
6
30
|
import * as Effect from "effect/Effect"
|
|
7
31
|
import { identity, pipe } from "effect/Function"
|
|
8
32
|
import * as Layer from "effect/Layer"
|
|
9
33
|
import * as Redacted from "effect/Redacted"
|
|
10
34
|
import * as Schema from "effect/Schema"
|
|
11
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
12
35
|
import * as Stream from "effect/Stream"
|
|
13
36
|
import type * as AiError from "effect/unstable/ai/AiError"
|
|
14
37
|
import * as Sse from "effect/unstable/encoding/Sse"
|
|
@@ -20,8 +43,16 @@ import * as Errors from "./internal/errors.ts"
|
|
|
20
43
|
import { OpenAiConfig } from "./OpenAiConfig.ts"
|
|
21
44
|
|
|
22
45
|
/**
|
|
23
|
-
*
|
|
46
|
+
* Effect service interface for OpenAI-compatible chat completions and embeddings.
|
|
47
|
+
*
|
|
48
|
+
* **Details**
|
|
49
|
+
*
|
|
50
|
+
* Exposes the configured HTTP client plus helpers for non-streaming chat
|
|
51
|
+
* completions, streaming chat completions, and embeddings. Transport and
|
|
52
|
+
* schema decoding failures are mapped to `AiError`.
|
|
53
|
+
*
|
|
24
54
|
* @category models
|
|
55
|
+
* @since 4.0.0
|
|
25
56
|
*/
|
|
26
57
|
export interface Service {
|
|
27
58
|
readonly client: HttpClient.HttpClient
|
|
@@ -46,16 +77,21 @@ export interface Service {
|
|
|
46
77
|
}
|
|
47
78
|
|
|
48
79
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
80
|
+
* Context service tag for accessing an OpenAI-compatible client from the
|
|
81
|
+
* Effect context.
|
|
82
|
+
*
|
|
83
|
+
* @category services
|
|
84
|
+
* @since 4.0.0
|
|
51
85
|
*/
|
|
52
|
-
export class OpenAiClient extends
|
|
86
|
+
export class OpenAiClient extends Context.Service<OpenAiClient, Service>()(
|
|
53
87
|
"@effect/ai-openai-compat/OpenAiClient"
|
|
54
88
|
) {}
|
|
55
89
|
|
|
56
90
|
/**
|
|
57
|
-
*
|
|
91
|
+
* Configuration options used to construct an OpenAI-compatible client.
|
|
92
|
+
*
|
|
58
93
|
* @category models
|
|
94
|
+
* @since 4.0.0
|
|
59
95
|
*/
|
|
60
96
|
export type Options = {
|
|
61
97
|
readonly apiKey?: Redacted.Redacted<string> | undefined
|
|
@@ -71,8 +107,15 @@ const RedactedOpenAiHeaders = {
|
|
|
71
107
|
}
|
|
72
108
|
|
|
73
109
|
/**
|
|
74
|
-
*
|
|
110
|
+
* Constructs an OpenAI-compatible client service from explicit options.
|
|
111
|
+
*
|
|
112
|
+
* **Details**
|
|
113
|
+
*
|
|
114
|
+
* The returned service applies the configured base URL, authentication, and
|
|
115
|
+
* OpenAI organization/project headers to the underlying HTTP client.
|
|
116
|
+
*
|
|
75
117
|
* @category constructors
|
|
118
|
+
* @since 4.0.0
|
|
76
119
|
*/
|
|
77
120
|
export const make = Effect.fnUntraced(
|
|
78
121
|
function*(options: Options): Effect.fn.Return<Service, never, HttpClient.HttpClient> {
|
|
@@ -212,21 +255,26 @@ export const make = Effect.fnUntraced(
|
|
|
212
255
|
)
|
|
213
256
|
|
|
214
257
|
/**
|
|
215
|
-
*
|
|
258
|
+
* Creates a layer that provides an OpenAI-compatible client from explicit options.
|
|
259
|
+
*
|
|
216
260
|
* @category layers
|
|
261
|
+
* @since 4.0.0
|
|
217
262
|
*/
|
|
218
263
|
export const layer = (options: Options): Layer.Layer<OpenAiClient, never, HttpClient.HttpClient> =>
|
|
219
264
|
Layer.effect(OpenAiClient, make(options))
|
|
220
265
|
|
|
221
266
|
/**
|
|
222
|
-
*
|
|
267
|
+
* Creates a layer that loads OpenAI-compatible client settings from `Config`
|
|
268
|
+
* values before constructing the service.
|
|
269
|
+
*
|
|
223
270
|
* @category layers
|
|
271
|
+
* @since 4.0.0
|
|
224
272
|
*/
|
|
225
273
|
export const layerConfig = (options?: {
|
|
226
|
-
readonly apiKey?: Config.Config<Redacted.Redacted<string
|
|
274
|
+
readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
227
275
|
readonly apiUrl?: Config.Config<string> | undefined
|
|
228
|
-
readonly organizationId?: Config.Config<Redacted.Redacted<string
|
|
229
|
-
readonly projectId?: Config.Config<Redacted.Redacted<string
|
|
276
|
+
readonly organizationId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
277
|
+
readonly projectId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
230
278
|
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
|
|
231
279
|
}): Layer.Layer<OpenAiClient, Config.ConfigError, HttpClient.HttpClient> =>
|
|
232
280
|
Layer.effect(
|
|
@@ -257,7 +305,10 @@ export const layerConfig = (options?: {
|
|
|
257
305
|
type JsonObject = { readonly [x: string]: Schema.Json }
|
|
258
306
|
|
|
259
307
|
/**
|
|
260
|
-
*
|
|
308
|
+
* Optional response fields that can be requested with the `include` parameter.
|
|
309
|
+
*
|
|
310
|
+
* @category response
|
|
311
|
+
* @since 4.0.0
|
|
261
312
|
*/
|
|
262
313
|
export type IncludeEnum =
|
|
263
314
|
| "message.input_image.image_url"
|
|
@@ -265,7 +316,10 @@ export type IncludeEnum =
|
|
|
265
316
|
| "message.output_text.logprobs"
|
|
266
317
|
|
|
267
318
|
/**
|
|
268
|
-
*
|
|
319
|
+
* Lifecycle status shared by message, reasoning, and tool-call items.
|
|
320
|
+
*
|
|
321
|
+
* @category models
|
|
322
|
+
* @since 4.0.0
|
|
269
323
|
*/
|
|
270
324
|
export type MessageStatus = "in_progress" | "completed" | "incomplete"
|
|
271
325
|
|
|
@@ -290,12 +344,18 @@ type InputFileContent = {
|
|
|
290
344
|
}
|
|
291
345
|
|
|
292
346
|
/**
|
|
293
|
-
*
|
|
347
|
+
* Content blocks accepted in input messages.
|
|
348
|
+
*
|
|
349
|
+
* @category request
|
|
350
|
+
* @since 4.0.0
|
|
294
351
|
*/
|
|
295
352
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
296
353
|
|
|
297
354
|
/**
|
|
298
|
-
*
|
|
355
|
+
* Text content block used for model-provided reasoning summaries.
|
|
356
|
+
*
|
|
357
|
+
* @category response
|
|
358
|
+
* @since 4.0.0
|
|
299
359
|
*/
|
|
300
360
|
export type SummaryTextContent = {
|
|
301
361
|
readonly type: "summary_text"
|
|
@@ -354,7 +414,10 @@ type FilePathAnnotation = {
|
|
|
354
414
|
}
|
|
355
415
|
|
|
356
416
|
/**
|
|
357
|
-
*
|
|
417
|
+
* Citation and file-path annotations attached to output text content.
|
|
418
|
+
*
|
|
419
|
+
* @category response
|
|
420
|
+
* @since 4.0.0
|
|
358
421
|
*/
|
|
359
422
|
export type Annotation =
|
|
360
423
|
| FileCitationAnnotation
|
|
@@ -389,7 +452,11 @@ type OutputMessage = {
|
|
|
389
452
|
}
|
|
390
453
|
|
|
391
454
|
/**
|
|
392
|
-
*
|
|
455
|
+
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
456
|
+
* optional reasoning text.
|
|
457
|
+
*
|
|
458
|
+
* @category response
|
|
459
|
+
* @since 4.0.0
|
|
393
460
|
*/
|
|
394
461
|
export type ReasoningItem = {
|
|
395
462
|
readonly type: "reasoning"
|
|
@@ -438,7 +505,15 @@ type ItemReference = {
|
|
|
438
505
|
}
|
|
439
506
|
|
|
440
507
|
/**
|
|
441
|
-
*
|
|
508
|
+
* Item shapes accepted by a Responses-style `input` field.
|
|
509
|
+
*
|
|
510
|
+
* **Details**
|
|
511
|
+
*
|
|
512
|
+
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
513
|
+
* items, custom tool interactions, and item references.
|
|
514
|
+
*
|
|
515
|
+
* @category request
|
|
516
|
+
* @since 4.0.0
|
|
442
517
|
*/
|
|
443
518
|
export type InputItem =
|
|
444
519
|
| {
|
|
@@ -476,7 +551,10 @@ type CustomToolParam = {
|
|
|
476
551
|
}
|
|
477
552
|
|
|
478
553
|
/**
|
|
479
|
-
*
|
|
554
|
+
* Tool definitions that can be supplied to a Responses-style request.
|
|
555
|
+
*
|
|
556
|
+
* @category request
|
|
557
|
+
* @since 4.0.0
|
|
480
558
|
*/
|
|
481
559
|
export type Tool =
|
|
482
560
|
| FunctionTool
|
|
@@ -501,7 +579,11 @@ type ToolChoice =
|
|
|
501
579
|
}
|
|
502
580
|
|
|
503
581
|
/**
|
|
504
|
-
*
|
|
582
|
+
* Text output format configuration for plain text, JSON object, or JSON Schema
|
|
583
|
+
* responses.
|
|
584
|
+
*
|
|
585
|
+
* @category configuration
|
|
586
|
+
* @since 4.0.0
|
|
505
587
|
*/
|
|
506
588
|
export type TextResponseFormatConfiguration =
|
|
507
589
|
| {
|
|
@@ -519,7 +601,11 @@ export type TextResponseFormatConfiguration =
|
|
|
519
601
|
}
|
|
520
602
|
|
|
521
603
|
/**
|
|
522
|
-
*
|
|
604
|
+
* Request options for creating a Responses-style response with an
|
|
605
|
+
* OpenAI-compatible provider.
|
|
606
|
+
*
|
|
607
|
+
* @category request
|
|
608
|
+
* @since 4.0.0
|
|
523
609
|
*/
|
|
524
610
|
export type CreateResponse = {
|
|
525
611
|
readonly metadata?: Readonly<Record<string, string>> | null | undefined
|
|
@@ -556,7 +642,10 @@ export type CreateResponse = {
|
|
|
556
642
|
}
|
|
557
643
|
|
|
558
644
|
/**
|
|
559
|
-
*
|
|
645
|
+
* Token accounting reported on Responses-style response objects.
|
|
646
|
+
*
|
|
647
|
+
* @category response
|
|
648
|
+
* @since 4.0.0
|
|
560
649
|
*/
|
|
561
650
|
export type ResponseUsage = {
|
|
562
651
|
readonly input_tokens: number
|
|
@@ -573,7 +662,11 @@ type OutputItem =
|
|
|
573
662
|
| CustomToolCall
|
|
574
663
|
|
|
575
664
|
/**
|
|
576
|
-
*
|
|
665
|
+
* Responses-style response object returned by compatible providers or embedded
|
|
666
|
+
* in response stream lifecycle events.
|
|
667
|
+
*
|
|
668
|
+
* @category response
|
|
669
|
+
* @since 4.0.0
|
|
577
670
|
*/
|
|
578
671
|
export type Response = {
|
|
579
672
|
readonly id: string
|
|
@@ -699,7 +792,10 @@ type UnknownResponseStreamEvent = {
|
|
|
699
792
|
}
|
|
700
793
|
|
|
701
794
|
/**
|
|
702
|
-
*
|
|
795
|
+
* Server-sent event shapes emitted by Responses-style response streams.
|
|
796
|
+
*
|
|
797
|
+
* @category streaming
|
|
798
|
+
* @since 4.0.0
|
|
703
799
|
*/
|
|
704
800
|
export type ResponseStreamEvent =
|
|
705
801
|
| ResponseCreatedEvent
|
|
@@ -718,7 +814,16 @@ export type ResponseStreamEvent =
|
|
|
718
814
|
| UnknownResponseStreamEvent
|
|
719
815
|
|
|
720
816
|
/**
|
|
721
|
-
*
|
|
817
|
+
* Represents one embedding item returned by an OpenAI-compatible embeddings API.
|
|
818
|
+
*
|
|
819
|
+
* **Details**
|
|
820
|
+
*
|
|
821
|
+
* The embedding can be returned either as a numeric vector or as a base64-encoded
|
|
822
|
+
* string. The `index` field identifies the input item that produced this
|
|
823
|
+
* embedding.
|
|
824
|
+
*
|
|
825
|
+
* @category response
|
|
826
|
+
* @since 4.0.0
|
|
722
827
|
*/
|
|
723
828
|
export type Embedding = {
|
|
724
829
|
readonly embedding: ReadonlyArray<number> | string
|
|
@@ -727,7 +832,10 @@ export type Embedding = {
|
|
|
727
832
|
}
|
|
728
833
|
|
|
729
834
|
/**
|
|
730
|
-
*
|
|
835
|
+
* Request payload for the embeddings endpoint.
|
|
836
|
+
*
|
|
837
|
+
* @category request
|
|
838
|
+
* @since 4.0.0
|
|
731
839
|
*/
|
|
732
840
|
export type CreateEmbeddingRequest = {
|
|
733
841
|
readonly input: string | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<ReadonlyArray<number>>
|
|
@@ -738,7 +846,10 @@ export type CreateEmbeddingRequest = {
|
|
|
738
846
|
}
|
|
739
847
|
|
|
740
848
|
/**
|
|
741
|
-
*
|
|
849
|
+
* Successful response payload returned by the embeddings endpoint.
|
|
850
|
+
*
|
|
851
|
+
* @category response
|
|
852
|
+
* @since 4.0.0
|
|
742
853
|
*/
|
|
743
854
|
export type CreateEmbeddingResponse = {
|
|
744
855
|
readonly data: ReadonlyArray<Embedding>
|
|
@@ -751,15 +862,24 @@ export type CreateEmbeddingResponse = {
|
|
|
751
862
|
}
|
|
752
863
|
|
|
753
864
|
/**
|
|
754
|
-
*
|
|
865
|
+
* JSON request body accepted by the embeddings endpoint.
|
|
866
|
+
*
|
|
867
|
+
* @category request
|
|
868
|
+
* @since 4.0.0
|
|
755
869
|
*/
|
|
756
870
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
757
871
|
/**
|
|
758
|
-
*
|
|
872
|
+
* Decoded successful embeddings response body.
|
|
873
|
+
*
|
|
874
|
+
* @category response
|
|
875
|
+
* @since 4.0.0
|
|
759
876
|
*/
|
|
760
877
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
761
878
|
/**
|
|
762
|
-
*
|
|
879
|
+
* Structured content parts accepted in chat completion messages.
|
|
880
|
+
*
|
|
881
|
+
* @category request
|
|
882
|
+
* @since 4.0.0
|
|
763
883
|
*/
|
|
764
884
|
export type ChatCompletionContentPart =
|
|
765
885
|
| {
|
|
@@ -774,7 +894,10 @@ export type ChatCompletionContentPart =
|
|
|
774
894
|
}
|
|
775
895
|
}
|
|
776
896
|
/**
|
|
777
|
-
*
|
|
897
|
+
* Tool call data attached to an assistant chat completion message.
|
|
898
|
+
*
|
|
899
|
+
* @category request
|
|
900
|
+
* @since 4.0.0
|
|
778
901
|
*/
|
|
779
902
|
export type ChatCompletionRequestToolCall = {
|
|
780
903
|
readonly id: string
|
|
@@ -785,7 +908,10 @@ export type ChatCompletionRequestToolCall = {
|
|
|
785
908
|
}
|
|
786
909
|
}
|
|
787
910
|
/**
|
|
788
|
-
*
|
|
911
|
+
* Message shapes accepted by the chat completions endpoint.
|
|
912
|
+
*
|
|
913
|
+
* @category request
|
|
914
|
+
* @since 4.0.0
|
|
789
915
|
*/
|
|
790
916
|
export type ChatCompletionRequestMessage =
|
|
791
917
|
| {
|
|
@@ -799,7 +925,10 @@ export type ChatCompletionRequestMessage =
|
|
|
799
925
|
readonly content: string
|
|
800
926
|
}
|
|
801
927
|
/**
|
|
802
|
-
*
|
|
928
|
+
* Function tool definition accepted by the chat completions endpoint.
|
|
929
|
+
*
|
|
930
|
+
* @category request
|
|
931
|
+
* @since 4.0.0
|
|
803
932
|
*/
|
|
804
933
|
export type ChatCompletionTool = {
|
|
805
934
|
readonly type: "function"
|
|
@@ -811,7 +940,10 @@ export type ChatCompletionTool = {
|
|
|
811
940
|
}
|
|
812
941
|
}
|
|
813
942
|
/**
|
|
814
|
-
*
|
|
943
|
+
* Controls whether the model may call tools and can force a specific function.
|
|
944
|
+
*
|
|
945
|
+
* @category configuration
|
|
946
|
+
* @since 4.0.0
|
|
815
947
|
*/
|
|
816
948
|
export type ChatCompletionToolChoice =
|
|
817
949
|
| "none"
|
|
@@ -824,7 +956,10 @@ export type ChatCompletionToolChoice =
|
|
|
824
956
|
}
|
|
825
957
|
}
|
|
826
958
|
/**
|
|
827
|
-
*
|
|
959
|
+
* JSON response format configuration for chat completion requests.
|
|
960
|
+
*
|
|
961
|
+
* @category configuration
|
|
962
|
+
* @since 4.0.0
|
|
828
963
|
*/
|
|
829
964
|
export type ChatCompletionResponseFormat =
|
|
830
965
|
| {
|
|
@@ -840,7 +975,10 @@ export type ChatCompletionResponseFormat =
|
|
|
840
975
|
}
|
|
841
976
|
}
|
|
842
977
|
/**
|
|
843
|
-
*
|
|
978
|
+
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
979
|
+
*
|
|
980
|
+
* @category request
|
|
981
|
+
* @since 4.0.0
|
|
844
982
|
*/
|
|
845
983
|
export type ChatCompletionRequest = {
|
|
846
984
|
readonly model: string
|
|
@@ -855,21 +993,32 @@ export type ChatCompletionRequest = {
|
|
|
855
993
|
readonly tools?: ReadonlyArray<ChatCompletionTool> | undefined
|
|
856
994
|
readonly tool_choice?: ChatCompletionToolChoice | undefined
|
|
857
995
|
readonly service_tier?: string | undefined
|
|
996
|
+
readonly reasoning?: unknown
|
|
858
997
|
readonly stream?: boolean | undefined
|
|
859
998
|
readonly stream_options?: {
|
|
860
999
|
readonly include_usage?: boolean | undefined
|
|
861
1000
|
} | undefined
|
|
1001
|
+
readonly [x: string]: unknown
|
|
862
1002
|
}
|
|
863
1003
|
/**
|
|
864
|
-
*
|
|
1004
|
+
* JSON request body used by this client when creating a chat completion response.
|
|
1005
|
+
*
|
|
1006
|
+
* @category request
|
|
1007
|
+
* @since 4.0.0
|
|
865
1008
|
*/
|
|
866
1009
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
867
1010
|
/**
|
|
868
|
-
*
|
|
1011
|
+
* Decoded successful chat completion response body returned by `createResponse`.
|
|
1012
|
+
*
|
|
1013
|
+
* @category response
|
|
1014
|
+
* @since 4.0.0
|
|
869
1015
|
*/
|
|
870
1016
|
export type CreateResponse200 = ChatCompletionResponse
|
|
871
1017
|
/**
|
|
872
|
-
*
|
|
1018
|
+
* Decoded server-sent event payload emitted by `createResponseStream`.
|
|
1019
|
+
*
|
|
1020
|
+
* @category streaming
|
|
1021
|
+
* @since 4.0.0
|
|
873
1022
|
*/
|
|
874
1023
|
export type CreateResponse200Sse = ChatCompletionStreamEvent
|
|
875
1024
|
|
|
@@ -894,6 +1043,11 @@ const ChatCompletionToolFunction = Schema.Struct({
|
|
|
894
1043
|
arguments: Schema.optionalKey(Schema.String)
|
|
895
1044
|
})
|
|
896
1045
|
|
|
1046
|
+
const ChatCompletionToolFunctionDelta = Schema.Struct({
|
|
1047
|
+
name: Schema.optionalKey(Schema.String),
|
|
1048
|
+
arguments: Schema.optionalKey(Schema.String)
|
|
1049
|
+
})
|
|
1050
|
+
|
|
897
1051
|
const ChatCompletionToolCall = Schema.Struct({
|
|
898
1052
|
id: Schema.optionalKey(Schema.String),
|
|
899
1053
|
index: Schema.optionalKey(Schema.Number),
|
|
@@ -901,6 +1055,13 @@ const ChatCompletionToolCall = Schema.Struct({
|
|
|
901
1055
|
function: Schema.optionalKey(ChatCompletionToolFunction)
|
|
902
1056
|
})
|
|
903
1057
|
|
|
1058
|
+
const ChatCompletionToolCallDelta = Schema.Struct({
|
|
1059
|
+
id: Schema.optionalKey(Schema.String),
|
|
1060
|
+
index: Schema.optionalKey(Schema.Number),
|
|
1061
|
+
type: Schema.optionalKey(Schema.String),
|
|
1062
|
+
function: Schema.optionalKey(ChatCompletionToolFunctionDelta)
|
|
1063
|
+
})
|
|
1064
|
+
|
|
904
1065
|
const ChatCompletionMessage = Schema.Struct({
|
|
905
1066
|
role: Schema.optionalKey(Schema.String),
|
|
906
1067
|
content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
@@ -910,7 +1071,7 @@ const ChatCompletionMessage = Schema.Struct({
|
|
|
910
1071
|
const ChatCompletionDelta = Schema.Struct({
|
|
911
1072
|
role: Schema.optionalKey(Schema.String),
|
|
912
1073
|
content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
913
|
-
tool_calls: Schema.optionalKey(Schema.Array(
|
|
1074
|
+
tool_calls: Schema.optionalKey(Schema.Array(ChatCompletionToolCallDelta))
|
|
914
1075
|
})
|
|
915
1076
|
|
|
916
1077
|
const ChatCompletionChoice = Schema.Struct({
|
|
@@ -947,31 +1108,53 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
947
1108
|
})
|
|
948
1109
|
|
|
949
1110
|
/**
|
|
950
|
-
*
|
|
1111
|
+
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1112
|
+
*
|
|
1113
|
+
* @category response
|
|
1114
|
+
* @since 4.0.0
|
|
951
1115
|
*/
|
|
952
1116
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
953
1117
|
/**
|
|
954
|
-
*
|
|
1118
|
+
* Decoded message object from a non-streaming chat completion choice.
|
|
1119
|
+
*
|
|
1120
|
+
* @category response
|
|
1121
|
+
* @since 4.0.0
|
|
955
1122
|
*/
|
|
956
1123
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
957
1124
|
/**
|
|
958
|
-
*
|
|
1125
|
+
* Decoded choice object returned by chat completion responses and chunks.
|
|
1126
|
+
*
|
|
1127
|
+
* @category response
|
|
1128
|
+
* @since 4.0.0
|
|
959
1129
|
*/
|
|
960
1130
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
961
1131
|
/**
|
|
962
|
-
*
|
|
1132
|
+
* Decoded token usage summary returned by chat completions.
|
|
1133
|
+
*
|
|
1134
|
+
* @category response
|
|
1135
|
+
* @since 4.0.0
|
|
963
1136
|
*/
|
|
964
1137
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
965
1138
|
/**
|
|
966
|
-
*
|
|
1139
|
+
* Decoded successful response from the chat completions endpoint.
|
|
1140
|
+
*
|
|
1141
|
+
* @category response
|
|
1142
|
+
* @since 4.0.0
|
|
967
1143
|
*/
|
|
968
1144
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
969
1145
|
/**
|
|
970
|
-
*
|
|
1146
|
+
* Decoded streaming chunk emitted by the chat completions endpoint.
|
|
1147
|
+
*
|
|
1148
|
+
* @category streaming
|
|
1149
|
+
* @since 4.0.0
|
|
971
1150
|
*/
|
|
972
1151
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
973
1152
|
/**
|
|
974
|
-
*
|
|
1153
|
+
* Streaming chat completion event, including decoded chunks and the `[DONE]`
|
|
1154
|
+
* sentinel.
|
|
1155
|
+
*
|
|
1156
|
+
* @category streaming
|
|
1157
|
+
* @since 4.0.0
|
|
975
1158
|
*/
|
|
976
1159
|
export type ChatCompletionStreamEvent = ChatCompletionChunk | "[DONE]"
|
|
977
1160
|
|
package/src/OpenAiConfig.ts
CHANGED
|
@@ -1,35 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `OpenAiConfig` module provides shared configuration for clients that
|
|
3
|
+
* talk to OpenAI-compatible APIs. It is used to customize the HTTP client
|
|
4
|
+
* wiring around a provider without changing the higher-level model,
|
|
5
|
+
* embeddings, or tool-calling APIs that consume the client.
|
|
6
|
+
*
|
|
7
|
+
* **Common tasks**
|
|
8
|
+
*
|
|
9
|
+
* - Install a client transform with {@link withClientTransform}
|
|
10
|
+
* - Add provider-specific HTTP behavior, such as headers, retries, proxies, or
|
|
11
|
+
* instrumentation
|
|
12
|
+
* - Read the active configuration from the Effect context when implementing
|
|
13
|
+
* OpenAI-compatible integrations
|
|
14
|
+
*
|
|
15
|
+
* **Gotchas**
|
|
16
|
+
*
|
|
17
|
+
* - The transform receives and returns an `HttpClient`, so it should preserve
|
|
18
|
+
* the existing client behavior unless it intentionally replaces it
|
|
19
|
+
* - Configuration is provided through Effect context and is scoped to the
|
|
20
|
+
* effect that receives the service
|
|
21
|
+
*
|
|
22
|
+
* @since 4.0.0
|
|
3
23
|
*/
|
|
24
|
+
import * as Context from "effect/Context"
|
|
4
25
|
import * as Effect from "effect/Effect"
|
|
5
26
|
import { dual } from "effect/Function"
|
|
6
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
7
27
|
import type { HttpClient } from "effect/unstable/http/HttpClient"
|
|
8
28
|
|
|
9
29
|
/**
|
|
10
|
-
*
|
|
30
|
+
* Context service used to carry OpenAI-compatible client configuration for the
|
|
31
|
+
* current Effect scope.
|
|
32
|
+
*
|
|
11
33
|
* @category services
|
|
34
|
+
* @since 4.0.0
|
|
12
35
|
*/
|
|
13
|
-
export class OpenAiConfig extends
|
|
36
|
+
export class OpenAiConfig extends Context.Service<
|
|
14
37
|
OpenAiConfig,
|
|
15
38
|
OpenAiConfig.Service
|
|
16
39
|
>()("@effect/ai-openai-compat/OpenAiConfig") {
|
|
17
40
|
/**
|
|
18
|
-
*
|
|
41
|
+
* Gets the configured OpenAI-compatible service from the current context when present.
|
|
42
|
+
*
|
|
43
|
+
* @since 4.0.0
|
|
19
44
|
*/
|
|
20
45
|
static readonly getOrUndefined: Effect.Effect<typeof OpenAiConfig.Service | undefined> = Effect.map(
|
|
21
|
-
Effect.
|
|
46
|
+
Effect.context<never>(),
|
|
22
47
|
(context) => context.mapUnsafe.get(OpenAiConfig.key)
|
|
23
48
|
)
|
|
24
49
|
}
|
|
25
50
|
|
|
26
51
|
/**
|
|
27
|
-
*
|
|
52
|
+
* Types associated with the `OpenAiConfig` context service.
|
|
53
|
+
*
|
|
54
|
+
* @since 4.0.0
|
|
28
55
|
*/
|
|
29
56
|
export declare namespace OpenAiConfig {
|
|
30
57
|
/**
|
|
31
|
-
*
|
|
58
|
+
* Configuration consumed by OpenAI-compatible clients when they build or
|
|
59
|
+
* resolve the underlying HTTP client.
|
|
60
|
+
*
|
|
32
61
|
* @category models
|
|
62
|
+
* @since 4.0.0
|
|
33
63
|
*/
|
|
34
64
|
export interface Service {
|
|
35
65
|
readonly transformClient?: ((client: HttpClient) => HttpClient) | undefined
|
|
@@ -37,18 +67,54 @@ export declare namespace OpenAiConfig {
|
|
|
37
67
|
}
|
|
38
68
|
|
|
39
69
|
/**
|
|
40
|
-
*
|
|
70
|
+
* Provides an HTTP client transform for the supplied effect.
|
|
71
|
+
*
|
|
72
|
+
* **When to use**
|
|
73
|
+
*
|
|
74
|
+
* Use this to add provider-specific OpenAI-compatible HTTP behavior, such as
|
|
75
|
+
* headers, retries, instrumentation, or proxy routing.
|
|
76
|
+
*
|
|
77
|
+
* **Details**
|
|
78
|
+
*
|
|
79
|
+
* OpenAI-compatible provider services read the transform from the
|
|
80
|
+
* `OpenAiConfig` context.
|
|
81
|
+
*
|
|
41
82
|
* @category configuration
|
|
83
|
+
* @since 4.0.0
|
|
42
84
|
*/
|
|
43
85
|
export const withClientTransform: {
|
|
44
86
|
/**
|
|
45
|
-
*
|
|
87
|
+
* Provides an HTTP client transform for the supplied effect.
|
|
88
|
+
*
|
|
89
|
+
* **When to use**
|
|
90
|
+
*
|
|
91
|
+
* Use this to add provider-specific OpenAI-compatible HTTP behavior, such as
|
|
92
|
+
* headers, retries, instrumentation, or proxy routing.
|
|
93
|
+
*
|
|
94
|
+
* **Details**
|
|
95
|
+
*
|
|
96
|
+
* OpenAI-compatible provider services read the transform from the
|
|
97
|
+
* `OpenAiConfig` context.
|
|
98
|
+
*
|
|
46
99
|
* @category configuration
|
|
100
|
+
* @since 4.0.0
|
|
47
101
|
*/
|
|
48
102
|
(transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
|
|
49
103
|
/**
|
|
50
|
-
*
|
|
104
|
+
* Provides an HTTP client transform for the supplied effect.
|
|
105
|
+
*
|
|
106
|
+
* **When to use**
|
|
107
|
+
*
|
|
108
|
+
* Use this to add provider-specific OpenAI-compatible HTTP behavior, such as
|
|
109
|
+
* headers, retries, instrumentation, or proxy routing.
|
|
110
|
+
*
|
|
111
|
+
* **Details**
|
|
112
|
+
*
|
|
113
|
+
* OpenAI-compatible provider services read the transform from the
|
|
114
|
+
* `OpenAiConfig` context.
|
|
115
|
+
*
|
|
51
116
|
* @category configuration
|
|
117
|
+
* @since 4.0.0
|
|
52
118
|
*/
|
|
53
119
|
<A, E, R>(
|
|
54
120
|
self: Effect.Effect<A, E, R>,
|