@effect/ai-openai-compat 4.0.0-beta.66 → 4.0.0-beta.68
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 +181 -42
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +42 -6
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +56 -8
- package/dist/OpenAiConfig.d.ts.map +1 -1
- package/dist/OpenAiConfig.js +35 -4
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +14 -9
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -1
- package/dist/OpenAiEmbeddingModel.js +9 -6
- package/dist/OpenAiEmbeddingModel.js.map +1 -1
- package/dist/OpenAiError.d.ts +85 -3
- package/dist/OpenAiError.d.ts.map +1 -1
- package/dist/OpenAiError.js +14 -1
- package/dist/OpenAiLanguageModel.d.ts +209 -12
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +9 -7
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +17 -14
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +3 -3
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.d.ts +63 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -7
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +205 -43
- package/src/OpenAiConfig.ts +56 -8
- package/src/OpenAiEmbeddingModel.ts +16 -11
- package/src/OpenAiError.ts +85 -3
- package/src/OpenAiLanguageModel.ts +212 -15
- package/src/OpenAiTelemetry.ts +18 -15
- package/src/index.ts +63 -7
package/src/OpenAiClient.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
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"
|
|
@@ -20,8 +43,13 @@ 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
|
+
* Exposes the configured HTTP client plus helpers for non-streaming chat completions, streaming chat completions, and embeddings. Transport and schema decoding failures are mapped to `AiError`.
|
|
50
|
+
*
|
|
24
51
|
* @category models
|
|
52
|
+
* @since 4.0.0
|
|
25
53
|
*/
|
|
26
54
|
export interface Service {
|
|
27
55
|
readonly client: HttpClient.HttpClient
|
|
@@ -46,16 +74,21 @@ export interface Service {
|
|
|
46
74
|
}
|
|
47
75
|
|
|
48
76
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
77
|
+
* Context service tag for accessing an OpenAI-compatible client from the
|
|
78
|
+
* Effect context.
|
|
79
|
+
*
|
|
80
|
+
* @category services
|
|
81
|
+
* @since 4.0.0
|
|
51
82
|
*/
|
|
52
83
|
export class OpenAiClient extends Context.Service<OpenAiClient, Service>()(
|
|
53
84
|
"@effect/ai-openai-compat/OpenAiClient"
|
|
54
85
|
) {}
|
|
55
86
|
|
|
56
87
|
/**
|
|
57
|
-
*
|
|
88
|
+
* Configuration options used to construct an OpenAI-compatible client.
|
|
89
|
+
*
|
|
58
90
|
* @category models
|
|
91
|
+
* @since 4.0.0
|
|
59
92
|
*/
|
|
60
93
|
export type Options = {
|
|
61
94
|
readonly apiKey?: Redacted.Redacted<string> | undefined
|
|
@@ -71,8 +104,13 @@ const RedactedOpenAiHeaders = {
|
|
|
71
104
|
}
|
|
72
105
|
|
|
73
106
|
/**
|
|
74
|
-
*
|
|
107
|
+
* Constructs an OpenAI-compatible client service from explicit options.
|
|
108
|
+
*
|
|
109
|
+
* The returned service applies the configured base URL, authentication, and
|
|
110
|
+
* OpenAI organization/project headers to the underlying HTTP client.
|
|
111
|
+
*
|
|
75
112
|
* @category constructors
|
|
113
|
+
* @since 4.0.0
|
|
76
114
|
*/
|
|
77
115
|
export const make = Effect.fnUntraced(
|
|
78
116
|
function*(options: Options): Effect.fn.Return<Service, never, HttpClient.HttpClient> {
|
|
@@ -212,15 +250,20 @@ export const make = Effect.fnUntraced(
|
|
|
212
250
|
)
|
|
213
251
|
|
|
214
252
|
/**
|
|
215
|
-
*
|
|
253
|
+
* Creates a layer that provides an OpenAI-compatible client from explicit options.
|
|
254
|
+
*
|
|
216
255
|
* @category layers
|
|
256
|
+
* @since 4.0.0
|
|
217
257
|
*/
|
|
218
258
|
export const layer = (options: Options): Layer.Layer<OpenAiClient, never, HttpClient.HttpClient> =>
|
|
219
259
|
Layer.effect(OpenAiClient, make(options))
|
|
220
260
|
|
|
221
261
|
/**
|
|
222
|
-
*
|
|
262
|
+
* Creates a layer that loads OpenAI-compatible client settings from `Config`
|
|
263
|
+
* values before constructing the service.
|
|
264
|
+
*
|
|
223
265
|
* @category layers
|
|
266
|
+
* @since 4.0.0
|
|
224
267
|
*/
|
|
225
268
|
export const layerConfig = (options?: {
|
|
226
269
|
readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
@@ -257,7 +300,10 @@ export const layerConfig = (options?: {
|
|
|
257
300
|
type JsonObject = { readonly [x: string]: Schema.Json }
|
|
258
301
|
|
|
259
302
|
/**
|
|
260
|
-
*
|
|
303
|
+
* Optional response fields that can be requested with the `include` parameter.
|
|
304
|
+
*
|
|
305
|
+
* @category response
|
|
306
|
+
* @since 4.0.0
|
|
261
307
|
*/
|
|
262
308
|
export type IncludeEnum =
|
|
263
309
|
| "message.input_image.image_url"
|
|
@@ -265,7 +311,10 @@ export type IncludeEnum =
|
|
|
265
311
|
| "message.output_text.logprobs"
|
|
266
312
|
|
|
267
313
|
/**
|
|
268
|
-
*
|
|
314
|
+
* Lifecycle status shared by message, reasoning, and tool-call items.
|
|
315
|
+
*
|
|
316
|
+
* @category models
|
|
317
|
+
* @since 4.0.0
|
|
269
318
|
*/
|
|
270
319
|
export type MessageStatus = "in_progress" | "completed" | "incomplete"
|
|
271
320
|
|
|
@@ -290,12 +339,18 @@ type InputFileContent = {
|
|
|
290
339
|
}
|
|
291
340
|
|
|
292
341
|
/**
|
|
293
|
-
*
|
|
342
|
+
* Content blocks accepted in input messages.
|
|
343
|
+
*
|
|
344
|
+
* @category request
|
|
345
|
+
* @since 4.0.0
|
|
294
346
|
*/
|
|
295
347
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
296
348
|
|
|
297
349
|
/**
|
|
298
|
-
*
|
|
350
|
+
* Text content block used for model-provided reasoning summaries.
|
|
351
|
+
*
|
|
352
|
+
* @category response
|
|
353
|
+
* @since 4.0.0
|
|
299
354
|
*/
|
|
300
355
|
export type SummaryTextContent = {
|
|
301
356
|
readonly type: "summary_text"
|
|
@@ -354,7 +409,10 @@ type FilePathAnnotation = {
|
|
|
354
409
|
}
|
|
355
410
|
|
|
356
411
|
/**
|
|
357
|
-
*
|
|
412
|
+
* Citation and file-path annotations attached to output text content.
|
|
413
|
+
*
|
|
414
|
+
* @category response
|
|
415
|
+
* @since 4.0.0
|
|
358
416
|
*/
|
|
359
417
|
export type Annotation =
|
|
360
418
|
| FileCitationAnnotation
|
|
@@ -389,7 +447,11 @@ type OutputMessage = {
|
|
|
389
447
|
}
|
|
390
448
|
|
|
391
449
|
/**
|
|
392
|
-
*
|
|
450
|
+
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
451
|
+
* optional reasoning text.
|
|
452
|
+
*
|
|
453
|
+
* @category response
|
|
454
|
+
* @since 4.0.0
|
|
393
455
|
*/
|
|
394
456
|
export type ReasoningItem = {
|
|
395
457
|
readonly type: "reasoning"
|
|
@@ -438,7 +500,13 @@ type ItemReference = {
|
|
|
438
500
|
}
|
|
439
501
|
|
|
440
502
|
/**
|
|
441
|
-
*
|
|
503
|
+
* Item shapes accepted by a Responses-style `input` field.
|
|
504
|
+
*
|
|
505
|
+
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
506
|
+
* items, custom tool interactions, and item references.
|
|
507
|
+
*
|
|
508
|
+
* @category request
|
|
509
|
+
* @since 4.0.0
|
|
442
510
|
*/
|
|
443
511
|
export type InputItem =
|
|
444
512
|
| {
|
|
@@ -476,7 +544,10 @@ type CustomToolParam = {
|
|
|
476
544
|
}
|
|
477
545
|
|
|
478
546
|
/**
|
|
479
|
-
*
|
|
547
|
+
* Tool definitions that can be supplied to a Responses-style request.
|
|
548
|
+
*
|
|
549
|
+
* @category request
|
|
550
|
+
* @since 4.0.0
|
|
480
551
|
*/
|
|
481
552
|
export type Tool =
|
|
482
553
|
| FunctionTool
|
|
@@ -501,7 +572,11 @@ type ToolChoice =
|
|
|
501
572
|
}
|
|
502
573
|
|
|
503
574
|
/**
|
|
504
|
-
*
|
|
575
|
+
* Text output format configuration for plain text, JSON object, or JSON Schema
|
|
576
|
+
* responses.
|
|
577
|
+
*
|
|
578
|
+
* @category configuration
|
|
579
|
+
* @since 4.0.0
|
|
505
580
|
*/
|
|
506
581
|
export type TextResponseFormatConfiguration =
|
|
507
582
|
| {
|
|
@@ -519,7 +594,11 @@ export type TextResponseFormatConfiguration =
|
|
|
519
594
|
}
|
|
520
595
|
|
|
521
596
|
/**
|
|
522
|
-
*
|
|
597
|
+
* Request options for creating a Responses-style response with an
|
|
598
|
+
* OpenAI-compatible provider.
|
|
599
|
+
*
|
|
600
|
+
* @category request
|
|
601
|
+
* @since 4.0.0
|
|
523
602
|
*/
|
|
524
603
|
export type CreateResponse = {
|
|
525
604
|
readonly metadata?: Readonly<Record<string, string>> | null | undefined
|
|
@@ -556,7 +635,10 @@ export type CreateResponse = {
|
|
|
556
635
|
}
|
|
557
636
|
|
|
558
637
|
/**
|
|
559
|
-
*
|
|
638
|
+
* Token accounting reported on Responses-style response objects.
|
|
639
|
+
*
|
|
640
|
+
* @category response
|
|
641
|
+
* @since 4.0.0
|
|
560
642
|
*/
|
|
561
643
|
export type ResponseUsage = {
|
|
562
644
|
readonly input_tokens: number
|
|
@@ -573,7 +655,11 @@ type OutputItem =
|
|
|
573
655
|
| CustomToolCall
|
|
574
656
|
|
|
575
657
|
/**
|
|
576
|
-
*
|
|
658
|
+
* Responses-style response object returned by compatible providers or embedded
|
|
659
|
+
* in response stream lifecycle events.
|
|
660
|
+
*
|
|
661
|
+
* @category response
|
|
662
|
+
* @since 4.0.0
|
|
577
663
|
*/
|
|
578
664
|
export type Response = {
|
|
579
665
|
readonly id: string
|
|
@@ -699,7 +785,10 @@ type UnknownResponseStreamEvent = {
|
|
|
699
785
|
}
|
|
700
786
|
|
|
701
787
|
/**
|
|
702
|
-
*
|
|
788
|
+
* Server-sent event shapes emitted by Responses-style response streams.
|
|
789
|
+
*
|
|
790
|
+
* @category streaming
|
|
791
|
+
* @since 4.0.0
|
|
703
792
|
*/
|
|
704
793
|
export type ResponseStreamEvent =
|
|
705
794
|
| ResponseCreatedEvent
|
|
@@ -718,7 +807,16 @@ export type ResponseStreamEvent =
|
|
|
718
807
|
| UnknownResponseStreamEvent
|
|
719
808
|
|
|
720
809
|
/**
|
|
721
|
-
*
|
|
810
|
+
* Represents one embedding item returned by an OpenAI-compatible embeddings API.
|
|
811
|
+
*
|
|
812
|
+
* **Details**
|
|
813
|
+
*
|
|
814
|
+
* The embedding can be returned either as a numeric vector or as a base64-encoded
|
|
815
|
+
* string. The `index` field identifies the input item that produced this
|
|
816
|
+
* embedding.
|
|
817
|
+
*
|
|
818
|
+
* @category response
|
|
819
|
+
* @since 4.0.0
|
|
722
820
|
*/
|
|
723
821
|
export type Embedding = {
|
|
724
822
|
readonly embedding: ReadonlyArray<number> | string
|
|
@@ -727,7 +825,10 @@ export type Embedding = {
|
|
|
727
825
|
}
|
|
728
826
|
|
|
729
827
|
/**
|
|
730
|
-
*
|
|
828
|
+
* Request payload for the embeddings endpoint.
|
|
829
|
+
*
|
|
830
|
+
* @category request
|
|
831
|
+
* @since 4.0.0
|
|
731
832
|
*/
|
|
732
833
|
export type CreateEmbeddingRequest = {
|
|
733
834
|
readonly input: string | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<ReadonlyArray<number>>
|
|
@@ -738,7 +839,10 @@ export type CreateEmbeddingRequest = {
|
|
|
738
839
|
}
|
|
739
840
|
|
|
740
841
|
/**
|
|
741
|
-
*
|
|
842
|
+
* Successful response payload returned by the embeddings endpoint.
|
|
843
|
+
*
|
|
844
|
+
* @category response
|
|
845
|
+
* @since 4.0.0
|
|
742
846
|
*/
|
|
743
847
|
export type CreateEmbeddingResponse = {
|
|
744
848
|
readonly data: ReadonlyArray<Embedding>
|
|
@@ -751,15 +855,24 @@ export type CreateEmbeddingResponse = {
|
|
|
751
855
|
}
|
|
752
856
|
|
|
753
857
|
/**
|
|
754
|
-
*
|
|
858
|
+
* JSON request body accepted by the embeddings endpoint.
|
|
859
|
+
*
|
|
860
|
+
* @category request
|
|
861
|
+
* @since 4.0.0
|
|
755
862
|
*/
|
|
756
863
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
757
864
|
/**
|
|
758
|
-
*
|
|
865
|
+
* Decoded successful embeddings response body.
|
|
866
|
+
*
|
|
867
|
+
* @category response
|
|
868
|
+
* @since 4.0.0
|
|
759
869
|
*/
|
|
760
870
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
761
871
|
/**
|
|
762
|
-
*
|
|
872
|
+
* Structured content parts accepted in chat completion messages.
|
|
873
|
+
*
|
|
874
|
+
* @category request
|
|
875
|
+
* @since 4.0.0
|
|
763
876
|
*/
|
|
764
877
|
export type ChatCompletionContentPart =
|
|
765
878
|
| {
|
|
@@ -774,7 +887,10 @@ export type ChatCompletionContentPart =
|
|
|
774
887
|
}
|
|
775
888
|
}
|
|
776
889
|
/**
|
|
777
|
-
*
|
|
890
|
+
* Tool call data attached to an assistant chat completion message.
|
|
891
|
+
*
|
|
892
|
+
* @category request
|
|
893
|
+
* @since 4.0.0
|
|
778
894
|
*/
|
|
779
895
|
export type ChatCompletionRequestToolCall = {
|
|
780
896
|
readonly id: string
|
|
@@ -785,7 +901,10 @@ export type ChatCompletionRequestToolCall = {
|
|
|
785
901
|
}
|
|
786
902
|
}
|
|
787
903
|
/**
|
|
788
|
-
*
|
|
904
|
+
* Message shapes accepted by the chat completions endpoint.
|
|
905
|
+
*
|
|
906
|
+
* @category request
|
|
907
|
+
* @since 4.0.0
|
|
789
908
|
*/
|
|
790
909
|
export type ChatCompletionRequestMessage =
|
|
791
910
|
| {
|
|
@@ -799,7 +918,10 @@ export type ChatCompletionRequestMessage =
|
|
|
799
918
|
readonly content: string
|
|
800
919
|
}
|
|
801
920
|
/**
|
|
802
|
-
*
|
|
921
|
+
* Function tool definition accepted by the chat completions endpoint.
|
|
922
|
+
*
|
|
923
|
+
* @category request
|
|
924
|
+
* @since 4.0.0
|
|
803
925
|
*/
|
|
804
926
|
export type ChatCompletionTool = {
|
|
805
927
|
readonly type: "function"
|
|
@@ -811,7 +933,10 @@ export type ChatCompletionTool = {
|
|
|
811
933
|
}
|
|
812
934
|
}
|
|
813
935
|
/**
|
|
814
|
-
*
|
|
936
|
+
* Controls whether the model may call tools and can force a specific function.
|
|
937
|
+
*
|
|
938
|
+
* @category configuration
|
|
939
|
+
* @since 4.0.0
|
|
815
940
|
*/
|
|
816
941
|
export type ChatCompletionToolChoice =
|
|
817
942
|
| "none"
|
|
@@ -824,7 +949,10 @@ export type ChatCompletionToolChoice =
|
|
|
824
949
|
}
|
|
825
950
|
}
|
|
826
951
|
/**
|
|
827
|
-
*
|
|
952
|
+
* JSON response format configuration for chat completion requests.
|
|
953
|
+
*
|
|
954
|
+
* @category configuration
|
|
955
|
+
* @since 4.0.0
|
|
828
956
|
*/
|
|
829
957
|
export type ChatCompletionResponseFormat =
|
|
830
958
|
| {
|
|
@@ -840,7 +968,10 @@ export type ChatCompletionResponseFormat =
|
|
|
840
968
|
}
|
|
841
969
|
}
|
|
842
970
|
/**
|
|
843
|
-
*
|
|
971
|
+
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
972
|
+
*
|
|
973
|
+
* @category request
|
|
974
|
+
* @since 4.0.0
|
|
844
975
|
*/
|
|
845
976
|
export type ChatCompletionRequest = {
|
|
846
977
|
readonly model: string
|
|
@@ -863,15 +994,24 @@ export type ChatCompletionRequest = {
|
|
|
863
994
|
readonly [x: string]: unknown
|
|
864
995
|
}
|
|
865
996
|
/**
|
|
866
|
-
*
|
|
997
|
+
* JSON request body used by this client when creating a chat completion response.
|
|
998
|
+
*
|
|
999
|
+
* @category request
|
|
1000
|
+
* @since 4.0.0
|
|
867
1001
|
*/
|
|
868
1002
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
869
1003
|
/**
|
|
870
|
-
*
|
|
1004
|
+
* Decoded successful chat completion response body returned by `createResponse`.
|
|
1005
|
+
*
|
|
1006
|
+
* @category response
|
|
1007
|
+
* @since 4.0.0
|
|
871
1008
|
*/
|
|
872
1009
|
export type CreateResponse200 = ChatCompletionResponse
|
|
873
1010
|
/**
|
|
874
|
-
*
|
|
1011
|
+
* Decoded server-sent event payload emitted by `createResponseStream`.
|
|
1012
|
+
*
|
|
1013
|
+
* @category streaming
|
|
1014
|
+
* @since 4.0.0
|
|
875
1015
|
*/
|
|
876
1016
|
export type CreateResponse200Sse = ChatCompletionStreamEvent
|
|
877
1017
|
|
|
@@ -961,31 +1101,53 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
961
1101
|
})
|
|
962
1102
|
|
|
963
1103
|
/**
|
|
964
|
-
*
|
|
1104
|
+
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1105
|
+
*
|
|
1106
|
+
* @category response
|
|
1107
|
+
* @since 4.0.0
|
|
965
1108
|
*/
|
|
966
1109
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
967
1110
|
/**
|
|
968
|
-
*
|
|
1111
|
+
* Decoded message object from a non-streaming chat completion choice.
|
|
1112
|
+
*
|
|
1113
|
+
* @category response
|
|
1114
|
+
* @since 4.0.0
|
|
969
1115
|
*/
|
|
970
1116
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
971
1117
|
/**
|
|
972
|
-
*
|
|
1118
|
+
* Decoded choice object returned by chat completion responses and chunks.
|
|
1119
|
+
*
|
|
1120
|
+
* @category response
|
|
1121
|
+
* @since 4.0.0
|
|
973
1122
|
*/
|
|
974
1123
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
975
1124
|
/**
|
|
976
|
-
*
|
|
1125
|
+
* Decoded token usage summary returned by chat completions.
|
|
1126
|
+
*
|
|
1127
|
+
* @category response
|
|
1128
|
+
* @since 4.0.0
|
|
977
1129
|
*/
|
|
978
1130
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
979
1131
|
/**
|
|
980
|
-
*
|
|
1132
|
+
* Decoded successful response from the chat completions endpoint.
|
|
1133
|
+
*
|
|
1134
|
+
* @category response
|
|
1135
|
+
* @since 4.0.0
|
|
981
1136
|
*/
|
|
982
1137
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
983
1138
|
/**
|
|
984
|
-
*
|
|
1139
|
+
* Decoded streaming chunk emitted by the chat completions endpoint.
|
|
1140
|
+
*
|
|
1141
|
+
* @category streaming
|
|
1142
|
+
* @since 4.0.0
|
|
985
1143
|
*/
|
|
986
1144
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
987
1145
|
/**
|
|
988
|
-
*
|
|
1146
|
+
* Streaming chat completion event, including decoded chunks and the `[DONE]`
|
|
1147
|
+
* sentinel.
|
|
1148
|
+
*
|
|
1149
|
+
* @category streaming
|
|
1150
|
+
* @since 4.0.0
|
|
989
1151
|
*/
|
|
990
1152
|
export type ChatCompletionStreamEvent = ChatCompletionChunk | "[DONE]"
|
|
991
1153
|
|
package/src/OpenAiConfig.ts
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
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
|
*/
|
|
4
24
|
import * as Context from "effect/Context"
|
|
5
25
|
import * as Effect from "effect/Effect"
|
|
@@ -7,15 +27,20 @@ import { dual } from "effect/Function"
|
|
|
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
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
46
|
Effect.context<never>(),
|
|
@@ -24,12 +49,17 @@ export class OpenAiConfig extends Context.Service<
|
|
|
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,36 @@ export declare namespace OpenAiConfig {
|
|
|
37
67
|
}
|
|
38
68
|
|
|
39
69
|
/**
|
|
40
|
-
*
|
|
70
|
+
* Provides an HTTP client transform for the supplied effect.
|
|
71
|
+
*
|
|
72
|
+
* The transform is read by OpenAI-compatible provider services from the
|
|
73
|
+
* `OpenAiConfig` context and can be used to add provider-specific behavior such
|
|
74
|
+
* as headers, retries, instrumentation, or proxy routing.
|
|
75
|
+
*
|
|
41
76
|
* @category configuration
|
|
77
|
+
* @since 4.0.0
|
|
42
78
|
*/
|
|
43
79
|
export const withClientTransform: {
|
|
44
80
|
/**
|
|
45
|
-
*
|
|
81
|
+
* Provides an HTTP client transform for the supplied effect.
|
|
82
|
+
*
|
|
83
|
+
* The transform is read by OpenAI-compatible provider services from the
|
|
84
|
+
* `OpenAiConfig` context and can be used to add provider-specific behavior such
|
|
85
|
+
* as headers, retries, instrumentation, or proxy routing.
|
|
86
|
+
*
|
|
46
87
|
* @category configuration
|
|
88
|
+
* @since 4.0.0
|
|
47
89
|
*/
|
|
48
90
|
(transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
|
|
49
91
|
/**
|
|
50
|
-
*
|
|
92
|
+
* Provides an HTTP client transform for the supplied effect.
|
|
93
|
+
*
|
|
94
|
+
* The transform is read by OpenAI-compatible provider services from the
|
|
95
|
+
* `OpenAiConfig` context and can be used to add provider-specific behavior such
|
|
96
|
+
* as headers, retries, instrumentation, or proxy routing.
|
|
97
|
+
*
|
|
51
98
|
* @category configuration
|
|
99
|
+
* @since 4.0.0
|
|
52
100
|
*/
|
|
53
101
|
<A, E, R>(
|
|
54
102
|
self: Effect.Effect<A, E, R>,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
|
|
5
5
|
*
|
|
6
|
-
* @since
|
|
6
|
+
* @since 4.0.0
|
|
7
7
|
*/
|
|
8
8
|
import * as Context from "effect/Context"
|
|
9
9
|
import * as Effect from "effect/Effect"
|
|
@@ -17,16 +17,18 @@ import type { CreateEmbedding200, CreateEmbeddingRequestJson } from "./OpenAiCli
|
|
|
17
17
|
import { OpenAiClient } from "./OpenAiClient.ts"
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* A model identifier accepted by an OpenAI-compatible embeddings endpoint.
|
|
21
|
+
*
|
|
21
22
|
* @category models
|
|
23
|
+
* @since 4.0.0
|
|
22
24
|
*/
|
|
23
25
|
export type Model = string
|
|
24
26
|
|
|
25
27
|
/**
|
|
26
28
|
* Service definition for OpenAI embedding model configuration.
|
|
27
29
|
*
|
|
28
|
-
* @since 1.0.0
|
|
29
30
|
* @category context
|
|
31
|
+
* @since 4.0.0
|
|
30
32
|
*/
|
|
31
33
|
export class Config extends Context.Service<
|
|
32
34
|
Config,
|
|
@@ -44,8 +46,11 @@ export class Config extends Context.Service<
|
|
|
44
46
|
>()("@effect/ai-openai-compat/OpenAiEmbeddingModel/Config") {}
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
|
-
*
|
|
49
|
+
* Creates an OpenAI-compatible embedding model that can be used with
|
|
50
|
+
* `AiModel.provide`.
|
|
51
|
+
*
|
|
48
52
|
* @category constructors
|
|
53
|
+
* @since 4.0.0
|
|
49
54
|
*/
|
|
50
55
|
export const model = (
|
|
51
56
|
model: string,
|
|
@@ -72,8 +77,8 @@ export const model = (
|
|
|
72
77
|
/**
|
|
73
78
|
* Creates an OpenAI embedding model service.
|
|
74
79
|
*
|
|
75
|
-
* @since 1.0.0
|
|
76
80
|
* @category constructors
|
|
81
|
+
* @since 4.0.0
|
|
77
82
|
*/
|
|
78
83
|
export const make = Effect.fnUntraced(function*({ model, config: providerConfig }: {
|
|
79
84
|
readonly model: string
|
|
@@ -98,8 +103,8 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
98
103
|
/**
|
|
99
104
|
* Creates a layer for the OpenAI embedding model.
|
|
100
105
|
*
|
|
101
|
-
* @since 1.0.0
|
|
102
106
|
* @category layers
|
|
107
|
+
* @since 4.0.0
|
|
103
108
|
*/
|
|
104
109
|
export const layer = (options: {
|
|
105
110
|
readonly model: string
|
|
@@ -110,37 +115,37 @@ export const layer = (options: {
|
|
|
110
115
|
/**
|
|
111
116
|
* Provides config overrides for OpenAI embedding model operations.
|
|
112
117
|
*
|
|
113
|
-
* @since 1.0.0
|
|
114
118
|
* @category configuration
|
|
119
|
+
* @since 4.0.0
|
|
115
120
|
*/
|
|
116
121
|
export const withConfigOverride: {
|
|
117
122
|
/**
|
|
118
123
|
* Provides config overrides for OpenAI embedding model operations.
|
|
119
124
|
*
|
|
120
|
-
* @since 1.0.0
|
|
121
125
|
* @category configuration
|
|
126
|
+
* @since 4.0.0
|
|
122
127
|
*/
|
|
123
128
|
(overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
124
129
|
/**
|
|
125
130
|
* Provides config overrides for OpenAI embedding model operations.
|
|
126
131
|
*
|
|
127
|
-
* @since 1.0.0
|
|
128
132
|
* @category configuration
|
|
133
|
+
* @since 4.0.0
|
|
129
134
|
*/
|
|
130
135
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>
|
|
131
136
|
} = dual<
|
|
132
137
|
/**
|
|
133
138
|
* Provides config overrides for OpenAI embedding model operations.
|
|
134
139
|
*
|
|
135
|
-
* @since 1.0.0
|
|
136
140
|
* @category configuration
|
|
141
|
+
* @since 4.0.0
|
|
137
142
|
*/
|
|
138
143
|
(overrides: typeof Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
|
|
139
144
|
/**
|
|
140
145
|
* Provides config overrides for OpenAI embedding model operations.
|
|
141
146
|
*
|
|
142
|
-
* @since 1.0.0
|
|
143
147
|
* @category configuration
|
|
148
|
+
* @since 4.0.0
|
|
144
149
|
*/
|
|
145
150
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
146
151
|
>(2, (self, overrides) =>
|