@effect/ai-openai-compat 4.0.0-beta.66 → 4.0.0-beta.67
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 +145 -41
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +41 -5
- 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 +161 -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 +13 -10
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +2 -2
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +7 -7
- package/package.json +3 -3
- package/src/OpenAiClient.ts +169 -42
- package/src/OpenAiConfig.ts +56 -8
- package/src/OpenAiEmbeddingModel.ts +16 -11
- package/src/OpenAiError.ts +85 -3
- package/src/OpenAiLanguageModel.ts +164 -15
- package/src/OpenAiTelemetry.ts +14 -11
- package/src/index.ts +7 -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
|
-
*
|
|
77
|
+
* Context service tag for accessing an OpenAI-compatible client from the
|
|
78
|
+
* Effect context.
|
|
79
|
+
*
|
|
50
80
|
* @category service
|
|
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,9 @@ 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
|
+
* @since 4.0.0
|
|
261
306
|
*/
|
|
262
307
|
export type IncludeEnum =
|
|
263
308
|
| "message.input_image.image_url"
|
|
@@ -265,7 +310,9 @@ export type IncludeEnum =
|
|
|
265
310
|
| "message.output_text.logprobs"
|
|
266
311
|
|
|
267
312
|
/**
|
|
268
|
-
*
|
|
313
|
+
* Lifecycle status shared by message, reasoning, and tool-call items.
|
|
314
|
+
*
|
|
315
|
+
* @since 4.0.0
|
|
269
316
|
*/
|
|
270
317
|
export type MessageStatus = "in_progress" | "completed" | "incomplete"
|
|
271
318
|
|
|
@@ -290,12 +337,16 @@ type InputFileContent = {
|
|
|
290
337
|
}
|
|
291
338
|
|
|
292
339
|
/**
|
|
293
|
-
*
|
|
340
|
+
* Content blocks accepted in input messages.
|
|
341
|
+
*
|
|
342
|
+
* @since 4.0.0
|
|
294
343
|
*/
|
|
295
344
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
296
345
|
|
|
297
346
|
/**
|
|
298
|
-
*
|
|
347
|
+
* Text content block used for model-provided reasoning summaries.
|
|
348
|
+
*
|
|
349
|
+
* @since 4.0.0
|
|
299
350
|
*/
|
|
300
351
|
export type SummaryTextContent = {
|
|
301
352
|
readonly type: "summary_text"
|
|
@@ -354,7 +405,9 @@ type FilePathAnnotation = {
|
|
|
354
405
|
}
|
|
355
406
|
|
|
356
407
|
/**
|
|
357
|
-
*
|
|
408
|
+
* Citation and file-path annotations attached to output text content.
|
|
409
|
+
*
|
|
410
|
+
* @since 4.0.0
|
|
358
411
|
*/
|
|
359
412
|
export type Annotation =
|
|
360
413
|
| FileCitationAnnotation
|
|
@@ -389,7 +442,10 @@ type OutputMessage = {
|
|
|
389
442
|
}
|
|
390
443
|
|
|
391
444
|
/**
|
|
392
|
-
*
|
|
445
|
+
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
446
|
+
* optional reasoning text.
|
|
447
|
+
*
|
|
448
|
+
* @since 4.0.0
|
|
393
449
|
*/
|
|
394
450
|
export type ReasoningItem = {
|
|
395
451
|
readonly type: "reasoning"
|
|
@@ -438,7 +494,12 @@ type ItemReference = {
|
|
|
438
494
|
}
|
|
439
495
|
|
|
440
496
|
/**
|
|
441
|
-
*
|
|
497
|
+
* Item shapes accepted by a Responses-style `input` field.
|
|
498
|
+
*
|
|
499
|
+
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
500
|
+
* items, custom tool interactions, and item references.
|
|
501
|
+
*
|
|
502
|
+
* @since 4.0.0
|
|
442
503
|
*/
|
|
443
504
|
export type InputItem =
|
|
444
505
|
| {
|
|
@@ -476,7 +537,9 @@ type CustomToolParam = {
|
|
|
476
537
|
}
|
|
477
538
|
|
|
478
539
|
/**
|
|
479
|
-
*
|
|
540
|
+
* Tool definitions that can be supplied to a Responses-style request.
|
|
541
|
+
*
|
|
542
|
+
* @since 4.0.0
|
|
480
543
|
*/
|
|
481
544
|
export type Tool =
|
|
482
545
|
| FunctionTool
|
|
@@ -501,7 +564,10 @@ type ToolChoice =
|
|
|
501
564
|
}
|
|
502
565
|
|
|
503
566
|
/**
|
|
504
|
-
*
|
|
567
|
+
* Text output format configuration for plain text, JSON object, or JSON Schema
|
|
568
|
+
* responses.
|
|
569
|
+
*
|
|
570
|
+
* @since 4.0.0
|
|
505
571
|
*/
|
|
506
572
|
export type TextResponseFormatConfiguration =
|
|
507
573
|
| {
|
|
@@ -519,7 +585,10 @@ export type TextResponseFormatConfiguration =
|
|
|
519
585
|
}
|
|
520
586
|
|
|
521
587
|
/**
|
|
522
|
-
*
|
|
588
|
+
* Request options for creating a Responses-style response with an
|
|
589
|
+
* OpenAI-compatible provider.
|
|
590
|
+
*
|
|
591
|
+
* @since 4.0.0
|
|
523
592
|
*/
|
|
524
593
|
export type CreateResponse = {
|
|
525
594
|
readonly metadata?: Readonly<Record<string, string>> | null | undefined
|
|
@@ -556,7 +625,9 @@ export type CreateResponse = {
|
|
|
556
625
|
}
|
|
557
626
|
|
|
558
627
|
/**
|
|
559
|
-
*
|
|
628
|
+
* Token accounting reported on Responses-style response objects.
|
|
629
|
+
*
|
|
630
|
+
* @since 4.0.0
|
|
560
631
|
*/
|
|
561
632
|
export type ResponseUsage = {
|
|
562
633
|
readonly input_tokens: number
|
|
@@ -573,7 +644,10 @@ type OutputItem =
|
|
|
573
644
|
| CustomToolCall
|
|
574
645
|
|
|
575
646
|
/**
|
|
576
|
-
*
|
|
647
|
+
* Responses-style response object returned by compatible providers or embedded
|
|
648
|
+
* in response stream lifecycle events.
|
|
649
|
+
*
|
|
650
|
+
* @since 4.0.0
|
|
577
651
|
*/
|
|
578
652
|
export type Response = {
|
|
579
653
|
readonly id: string
|
|
@@ -699,7 +773,9 @@ type UnknownResponseStreamEvent = {
|
|
|
699
773
|
}
|
|
700
774
|
|
|
701
775
|
/**
|
|
702
|
-
*
|
|
776
|
+
* Server-sent event shapes emitted by Responses-style response streams.
|
|
777
|
+
*
|
|
778
|
+
* @since 4.0.0
|
|
703
779
|
*/
|
|
704
780
|
export type ResponseStreamEvent =
|
|
705
781
|
| ResponseCreatedEvent
|
|
@@ -718,7 +794,15 @@ export type ResponseStreamEvent =
|
|
|
718
794
|
| UnknownResponseStreamEvent
|
|
719
795
|
|
|
720
796
|
/**
|
|
721
|
-
*
|
|
797
|
+
* Represents one embedding item returned by an OpenAI-compatible embeddings API.
|
|
798
|
+
*
|
|
799
|
+
* **Details**
|
|
800
|
+
*
|
|
801
|
+
* The embedding can be returned either as a numeric vector or as a base64-encoded
|
|
802
|
+
* string. The `index` field identifies the input item that produced this
|
|
803
|
+
* embedding.
|
|
804
|
+
*
|
|
805
|
+
* @since 4.0.0
|
|
722
806
|
*/
|
|
723
807
|
export type Embedding = {
|
|
724
808
|
readonly embedding: ReadonlyArray<number> | string
|
|
@@ -727,7 +811,9 @@ export type Embedding = {
|
|
|
727
811
|
}
|
|
728
812
|
|
|
729
813
|
/**
|
|
730
|
-
*
|
|
814
|
+
* Request payload for the embeddings endpoint.
|
|
815
|
+
*
|
|
816
|
+
* @since 4.0.0
|
|
731
817
|
*/
|
|
732
818
|
export type CreateEmbeddingRequest = {
|
|
733
819
|
readonly input: string | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<ReadonlyArray<number>>
|
|
@@ -738,7 +824,9 @@ export type CreateEmbeddingRequest = {
|
|
|
738
824
|
}
|
|
739
825
|
|
|
740
826
|
/**
|
|
741
|
-
*
|
|
827
|
+
* Successful response payload returned by the embeddings endpoint.
|
|
828
|
+
*
|
|
829
|
+
* @since 4.0.0
|
|
742
830
|
*/
|
|
743
831
|
export type CreateEmbeddingResponse = {
|
|
744
832
|
readonly data: ReadonlyArray<Embedding>
|
|
@@ -751,15 +839,21 @@ export type CreateEmbeddingResponse = {
|
|
|
751
839
|
}
|
|
752
840
|
|
|
753
841
|
/**
|
|
754
|
-
*
|
|
842
|
+
* JSON request body accepted by the embeddings endpoint.
|
|
843
|
+
*
|
|
844
|
+
* @since 4.0.0
|
|
755
845
|
*/
|
|
756
846
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
757
847
|
/**
|
|
758
|
-
*
|
|
848
|
+
* Decoded successful embeddings response body.
|
|
849
|
+
*
|
|
850
|
+
* @since 4.0.0
|
|
759
851
|
*/
|
|
760
852
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
761
853
|
/**
|
|
762
|
-
*
|
|
854
|
+
* Structured content parts accepted in chat completion messages.
|
|
855
|
+
*
|
|
856
|
+
* @since 4.0.0
|
|
763
857
|
*/
|
|
764
858
|
export type ChatCompletionContentPart =
|
|
765
859
|
| {
|
|
@@ -774,7 +868,9 @@ export type ChatCompletionContentPart =
|
|
|
774
868
|
}
|
|
775
869
|
}
|
|
776
870
|
/**
|
|
777
|
-
*
|
|
871
|
+
* Tool call data attached to an assistant chat completion message.
|
|
872
|
+
*
|
|
873
|
+
* @since 4.0.0
|
|
778
874
|
*/
|
|
779
875
|
export type ChatCompletionRequestToolCall = {
|
|
780
876
|
readonly id: string
|
|
@@ -785,7 +881,9 @@ export type ChatCompletionRequestToolCall = {
|
|
|
785
881
|
}
|
|
786
882
|
}
|
|
787
883
|
/**
|
|
788
|
-
*
|
|
884
|
+
* Message shapes accepted by the chat completions endpoint.
|
|
885
|
+
*
|
|
886
|
+
* @since 4.0.0
|
|
789
887
|
*/
|
|
790
888
|
export type ChatCompletionRequestMessage =
|
|
791
889
|
| {
|
|
@@ -799,7 +897,9 @@ export type ChatCompletionRequestMessage =
|
|
|
799
897
|
readonly content: string
|
|
800
898
|
}
|
|
801
899
|
/**
|
|
802
|
-
*
|
|
900
|
+
* Function tool definition accepted by the chat completions endpoint.
|
|
901
|
+
*
|
|
902
|
+
* @since 4.0.0
|
|
803
903
|
*/
|
|
804
904
|
export type ChatCompletionTool = {
|
|
805
905
|
readonly type: "function"
|
|
@@ -811,7 +911,9 @@ export type ChatCompletionTool = {
|
|
|
811
911
|
}
|
|
812
912
|
}
|
|
813
913
|
/**
|
|
814
|
-
*
|
|
914
|
+
* Controls whether the model may call tools and can force a specific function.
|
|
915
|
+
*
|
|
916
|
+
* @since 4.0.0
|
|
815
917
|
*/
|
|
816
918
|
export type ChatCompletionToolChoice =
|
|
817
919
|
| "none"
|
|
@@ -824,7 +926,9 @@ export type ChatCompletionToolChoice =
|
|
|
824
926
|
}
|
|
825
927
|
}
|
|
826
928
|
/**
|
|
827
|
-
*
|
|
929
|
+
* JSON response format configuration for chat completion requests.
|
|
930
|
+
*
|
|
931
|
+
* @since 4.0.0
|
|
828
932
|
*/
|
|
829
933
|
export type ChatCompletionResponseFormat =
|
|
830
934
|
| {
|
|
@@ -840,7 +944,9 @@ export type ChatCompletionResponseFormat =
|
|
|
840
944
|
}
|
|
841
945
|
}
|
|
842
946
|
/**
|
|
843
|
-
*
|
|
947
|
+
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
948
|
+
*
|
|
949
|
+
* @since 4.0.0
|
|
844
950
|
*/
|
|
845
951
|
export type ChatCompletionRequest = {
|
|
846
952
|
readonly model: string
|
|
@@ -863,15 +969,21 @@ export type ChatCompletionRequest = {
|
|
|
863
969
|
readonly [x: string]: unknown
|
|
864
970
|
}
|
|
865
971
|
/**
|
|
866
|
-
*
|
|
972
|
+
* JSON request body used by this client when creating a chat completion response.
|
|
973
|
+
*
|
|
974
|
+
* @since 4.0.0
|
|
867
975
|
*/
|
|
868
976
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
869
977
|
/**
|
|
870
|
-
*
|
|
978
|
+
* Decoded successful chat completion response body returned by `createResponse`.
|
|
979
|
+
*
|
|
980
|
+
* @since 4.0.0
|
|
871
981
|
*/
|
|
872
982
|
export type CreateResponse200 = ChatCompletionResponse
|
|
873
983
|
/**
|
|
874
|
-
*
|
|
984
|
+
* Decoded server-sent event payload emitted by `createResponseStream`.
|
|
985
|
+
*
|
|
986
|
+
* @since 4.0.0
|
|
875
987
|
*/
|
|
876
988
|
export type CreateResponse200Sse = ChatCompletionStreamEvent
|
|
877
989
|
|
|
@@ -961,31 +1073,46 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
961
1073
|
})
|
|
962
1074
|
|
|
963
1075
|
/**
|
|
964
|
-
*
|
|
1076
|
+
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1077
|
+
*
|
|
1078
|
+
* @since 4.0.0
|
|
965
1079
|
*/
|
|
966
1080
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
967
1081
|
/**
|
|
968
|
-
*
|
|
1082
|
+
* Decoded message object from a non-streaming chat completion choice.
|
|
1083
|
+
*
|
|
1084
|
+
* @since 4.0.0
|
|
969
1085
|
*/
|
|
970
1086
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
971
1087
|
/**
|
|
972
|
-
*
|
|
1088
|
+
* Decoded choice object returned by chat completion responses and chunks.
|
|
1089
|
+
*
|
|
1090
|
+
* @since 4.0.0
|
|
973
1091
|
*/
|
|
974
1092
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
975
1093
|
/**
|
|
976
|
-
*
|
|
1094
|
+
* Decoded token usage summary returned by chat completions.
|
|
1095
|
+
*
|
|
1096
|
+
* @since 4.0.0
|
|
977
1097
|
*/
|
|
978
1098
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
979
1099
|
/**
|
|
980
|
-
*
|
|
1100
|
+
* Decoded successful response from the chat completions endpoint.
|
|
1101
|
+
*
|
|
1102
|
+
* @since 4.0.0
|
|
981
1103
|
*/
|
|
982
1104
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
983
1105
|
/**
|
|
984
|
-
*
|
|
1106
|
+
* Decoded streaming chunk emitted by the chat completions endpoint.
|
|
1107
|
+
*
|
|
1108
|
+
* @since 4.0.0
|
|
985
1109
|
*/
|
|
986
1110
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
987
1111
|
/**
|
|
988
|
-
*
|
|
1112
|
+
* Streaming chat completion event, including decoded chunks and the `[DONE]`
|
|
1113
|
+
* sentinel.
|
|
1114
|
+
*
|
|
1115
|
+
* @since 4.0.0
|
|
989
1116
|
*/
|
|
990
1117
|
export type ChatCompletionStreamEvent = ChatCompletionChunk | "[DONE]"
|
|
991
1118
|
|
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 1.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) =>
|