@effect/ai-openai-compat 4.0.0-beta.9 → 4.0.0-beta.90
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 +264 -52
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +97 -9
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +68 -10
- package/dist/OpenAiConfig.d.ts.map +1 -1
- package/dist/OpenAiConfig.js +36 -7
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +186 -0
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
- package/dist/OpenAiEmbeddingModel.js +190 -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 +304 -20
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +157 -27
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +76 -26
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +22 -10
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.d.ts +10 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -17
- package/dist/index.js.map +1 -1
- package/dist/internal/errors.js +4 -4
- package/dist/internal/errors.js.map +1 -1
- package/dist/internal/utilities.js +0 -6
- package/dist/internal/utilities.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +273 -50
- package/src/OpenAiConfig.ts +69 -11
- package/src/OpenAiEmbeddingModel.ts +332 -0
- package/src/OpenAiError.ts +111 -35
- package/src/OpenAiLanguageModel.ts +426 -43
- package/src/OpenAiTelemetry.ts +81 -32
- package/src/index.ts +11 -17
- package/src/internal/errors.ts +4 -4
- package/src/internal/utilities.ts +0 -9
package/src/OpenAiClient.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `OpenAiClient` module provides an Effect service for OpenAI-compatible
|
|
3
|
+
* chat completions and embeddings APIs. It builds on the Effect HTTP client,
|
|
4
|
+
* adds authentication and OpenAI organization or project headers, and exposes
|
|
5
|
+
* typed helpers for non-streaming chat completions, streaming chat completions,
|
|
6
|
+
* and embedding requests.
|
|
7
|
+
*
|
|
8
|
+
* @since 4.0.0
|
|
3
9
|
*/
|
|
4
10
|
import * as Array from "effect/Array"
|
|
5
11
|
import type * as Config from "effect/Config"
|
|
12
|
+
import * as Context from "effect/Context"
|
|
6
13
|
import * as Effect from "effect/Effect"
|
|
7
14
|
import { identity, pipe } from "effect/Function"
|
|
8
15
|
import * as Layer from "effect/Layer"
|
|
9
16
|
import * as Redacted from "effect/Redacted"
|
|
10
17
|
import * as Schema from "effect/Schema"
|
|
11
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
12
18
|
import * as Stream from "effect/Stream"
|
|
13
19
|
import type * as AiError from "effect/unstable/ai/AiError"
|
|
14
20
|
import * as Sse from "effect/unstable/encoding/Sse"
|
|
@@ -20,8 +26,16 @@ import * as Errors from "./internal/errors.ts"
|
|
|
20
26
|
import { OpenAiConfig } from "./OpenAiConfig.ts"
|
|
21
27
|
|
|
22
28
|
/**
|
|
23
|
-
*
|
|
29
|
+
* Effect service interface for OpenAI-compatible chat completions and embeddings.
|
|
30
|
+
*
|
|
31
|
+
* **Details**
|
|
32
|
+
*
|
|
33
|
+
* Exposes the configured HTTP client plus helpers for non-streaming chat
|
|
34
|
+
* completions, streaming chat completions, and embeddings. Transport and
|
|
35
|
+
* schema decoding failures are mapped to `AiError`.
|
|
36
|
+
*
|
|
24
37
|
* @category models
|
|
38
|
+
* @since 4.0.0
|
|
25
39
|
*/
|
|
26
40
|
export interface Service {
|
|
27
41
|
readonly client: HttpClient.HttpClient
|
|
@@ -46,16 +60,35 @@ export interface Service {
|
|
|
46
60
|
}
|
|
47
61
|
|
|
48
62
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
63
|
+
* Service tag for the OpenAI-compatible chat completions and embeddings client.
|
|
64
|
+
*
|
|
65
|
+
* **When to use**
|
|
66
|
+
*
|
|
67
|
+
* Use when building effects that depend on the low-level OpenAI-compatible
|
|
68
|
+
* client through context rather than receiving the client as a value.
|
|
69
|
+
*
|
|
70
|
+
* **Details**
|
|
71
|
+
*
|
|
72
|
+
* The tagged service is the `Service` interface produced by `make` and provided
|
|
73
|
+
* by `layer` or `layerConfig`.
|
|
74
|
+
*
|
|
75
|
+
* @see {@link Service} for the operations provided by the service
|
|
76
|
+
* @see {@link make} for constructing the service from explicit options
|
|
77
|
+
* @see {@link layer} for providing the service from explicit options
|
|
78
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
79
|
+
*
|
|
80
|
+
* @category services
|
|
81
|
+
* @since 4.0.0
|
|
51
82
|
*/
|
|
52
|
-
export class OpenAiClient extends
|
|
83
|
+
export class OpenAiClient extends Context.Service<OpenAiClient, Service>()(
|
|
53
84
|
"@effect/ai-openai-compat/OpenAiClient"
|
|
54
85
|
) {}
|
|
55
86
|
|
|
56
87
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
88
|
+
* Configuration options used to construct an OpenAI-compatible client.
|
|
89
|
+
*
|
|
90
|
+
* @category options
|
|
91
|
+
* @since 4.0.0
|
|
59
92
|
*/
|
|
60
93
|
export type Options = {
|
|
61
94
|
readonly apiKey?: Redacted.Redacted<string> | undefined
|
|
@@ -71,8 +104,29 @@ const RedactedOpenAiHeaders = {
|
|
|
71
104
|
}
|
|
72
105
|
|
|
73
106
|
/**
|
|
74
|
-
*
|
|
107
|
+
* Constructs an OpenAI-compatible client service from explicit options.
|
|
108
|
+
*
|
|
109
|
+
* **When to use**
|
|
110
|
+
*
|
|
111
|
+
* Use when you need the OpenAI-compatible client service value inside an effect.
|
|
112
|
+
*
|
|
113
|
+
* **Details**
|
|
114
|
+
*
|
|
115
|
+
* The returned service uses the current `HttpClient`, prepends `apiUrl` or
|
|
116
|
+
* `https://api.openai.com/v1`, adds authentication and OpenAI
|
|
117
|
+
* organization/project headers, accepts JSON responses, and applies
|
|
118
|
+
* `transformClient` when provided.
|
|
119
|
+
*
|
|
120
|
+
* **Gotchas**
|
|
121
|
+
*
|
|
122
|
+
* A scoped `OpenAiConfig.withClientTransform` is applied when request helpers
|
|
123
|
+
* run, after the `transformClient` option supplied to `make`.
|
|
124
|
+
*
|
|
125
|
+
* @see {@link layer} for providing this client from explicit options
|
|
126
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
127
|
+
*
|
|
75
128
|
* @category constructors
|
|
129
|
+
* @since 4.0.0
|
|
76
130
|
*/
|
|
77
131
|
export const make = Effect.fnUntraced(
|
|
78
132
|
function*(options: Options): Effect.fn.Return<Service, never, HttpClient.HttpClient> {
|
|
@@ -212,21 +266,48 @@ export const make = Effect.fnUntraced(
|
|
|
212
266
|
)
|
|
213
267
|
|
|
214
268
|
/**
|
|
215
|
-
*
|
|
269
|
+
* Creates a layer that provides an OpenAI-compatible client from explicit options.
|
|
270
|
+
*
|
|
271
|
+
* **When to use**
|
|
272
|
+
*
|
|
273
|
+
* Use to install `OpenAiClient` in an application layer when the client options
|
|
274
|
+
* are already available as values rather than loaded from `Config`.
|
|
275
|
+
*
|
|
276
|
+
* @see {@link make} for constructing the client service effectfully
|
|
277
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
278
|
+
*
|
|
216
279
|
* @category layers
|
|
280
|
+
* @since 4.0.0
|
|
217
281
|
*/
|
|
218
282
|
export const layer = (options: Options): Layer.Layer<OpenAiClient, never, HttpClient.HttpClient> =>
|
|
219
283
|
Layer.effect(OpenAiClient, make(options))
|
|
220
284
|
|
|
221
285
|
/**
|
|
222
|
-
*
|
|
286
|
+
* Creates a layer that loads OpenAI-compatible client settings from `Config`
|
|
287
|
+
* values before constructing the service.
|
|
288
|
+
*
|
|
289
|
+
* **When to use**
|
|
290
|
+
*
|
|
291
|
+
* Use when you need client settings for OpenAI-compatible APIs to be read from
|
|
292
|
+
* Effect `Config` values while providing `OpenAiClient` as a layer.
|
|
293
|
+
*
|
|
294
|
+
* **Details**
|
|
295
|
+
*
|
|
296
|
+
* Only config values supplied in `options` are loaded. Omitted fields are
|
|
297
|
+
* passed to `make` as `undefined`, and `transformClient` is forwarded as a
|
|
298
|
+
* plain option.
|
|
299
|
+
*
|
|
300
|
+
* @see {@link make} for constructing the client service effectfully
|
|
301
|
+
* @see {@link layer} for providing the client from already-resolved options
|
|
302
|
+
*
|
|
223
303
|
* @category layers
|
|
304
|
+
* @since 4.0.0
|
|
224
305
|
*/
|
|
225
306
|
export const layerConfig = (options?: {
|
|
226
|
-
readonly apiKey?: Config.Config<Redacted.Redacted<string
|
|
307
|
+
readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
227
308
|
readonly apiUrl?: Config.Config<string> | undefined
|
|
228
|
-
readonly organizationId?: Config.Config<Redacted.Redacted<string
|
|
229
|
-
readonly projectId?: Config.Config<Redacted.Redacted<string
|
|
309
|
+
readonly organizationId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
310
|
+
readonly projectId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
230
311
|
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
|
|
231
312
|
}): Layer.Layer<OpenAiClient, Config.ConfigError, HttpClient.HttpClient> =>
|
|
232
313
|
Layer.effect(
|
|
@@ -257,7 +338,10 @@ export const layerConfig = (options?: {
|
|
|
257
338
|
type JsonObject = { readonly [x: string]: Schema.Json }
|
|
258
339
|
|
|
259
340
|
/**
|
|
260
|
-
*
|
|
341
|
+
* Optional response fields that can be requested with the `include` parameter.
|
|
342
|
+
*
|
|
343
|
+
* @category response
|
|
344
|
+
* @since 4.0.0
|
|
261
345
|
*/
|
|
262
346
|
export type IncludeEnum =
|
|
263
347
|
| "message.input_image.image_url"
|
|
@@ -265,7 +349,10 @@ export type IncludeEnum =
|
|
|
265
349
|
| "message.output_text.logprobs"
|
|
266
350
|
|
|
267
351
|
/**
|
|
268
|
-
*
|
|
352
|
+
* Lifecycle status shared by message, reasoning, and tool-call items.
|
|
353
|
+
*
|
|
354
|
+
* @category models
|
|
355
|
+
* @since 4.0.0
|
|
269
356
|
*/
|
|
270
357
|
export type MessageStatus = "in_progress" | "completed" | "incomplete"
|
|
271
358
|
|
|
@@ -290,12 +377,18 @@ type InputFileContent = {
|
|
|
290
377
|
}
|
|
291
378
|
|
|
292
379
|
/**
|
|
293
|
-
*
|
|
380
|
+
* Content blocks accepted in input messages.
|
|
381
|
+
*
|
|
382
|
+
* @category request
|
|
383
|
+
* @since 4.0.0
|
|
294
384
|
*/
|
|
295
385
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
296
386
|
|
|
297
387
|
/**
|
|
298
|
-
*
|
|
388
|
+
* Text content block used for model-provided reasoning summaries.
|
|
389
|
+
*
|
|
390
|
+
* @category response
|
|
391
|
+
* @since 4.0.0
|
|
299
392
|
*/
|
|
300
393
|
export type SummaryTextContent = {
|
|
301
394
|
readonly type: "summary_text"
|
|
@@ -354,7 +447,10 @@ type FilePathAnnotation = {
|
|
|
354
447
|
}
|
|
355
448
|
|
|
356
449
|
/**
|
|
357
|
-
*
|
|
450
|
+
* Citation and file-path annotations attached to output text content.
|
|
451
|
+
*
|
|
452
|
+
* @category response
|
|
453
|
+
* @since 4.0.0
|
|
358
454
|
*/
|
|
359
455
|
export type Annotation =
|
|
360
456
|
| FileCitationAnnotation
|
|
@@ -389,7 +485,11 @@ type OutputMessage = {
|
|
|
389
485
|
}
|
|
390
486
|
|
|
391
487
|
/**
|
|
392
|
-
*
|
|
488
|
+
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
489
|
+
* optional reasoning text.
|
|
490
|
+
*
|
|
491
|
+
* @category response
|
|
492
|
+
* @since 4.0.0
|
|
393
493
|
*/
|
|
394
494
|
export type ReasoningItem = {
|
|
395
495
|
readonly type: "reasoning"
|
|
@@ -438,7 +538,15 @@ type ItemReference = {
|
|
|
438
538
|
}
|
|
439
539
|
|
|
440
540
|
/**
|
|
441
|
-
*
|
|
541
|
+
* Item shapes accepted by a Responses-style `input` field.
|
|
542
|
+
*
|
|
543
|
+
* **Details**
|
|
544
|
+
*
|
|
545
|
+
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
546
|
+
* items, custom tool interactions, and item references.
|
|
547
|
+
*
|
|
548
|
+
* @category request
|
|
549
|
+
* @since 4.0.0
|
|
442
550
|
*/
|
|
443
551
|
export type InputItem =
|
|
444
552
|
| {
|
|
@@ -476,7 +584,10 @@ type CustomToolParam = {
|
|
|
476
584
|
}
|
|
477
585
|
|
|
478
586
|
/**
|
|
479
|
-
*
|
|
587
|
+
* Tool definitions that can be supplied to a Responses-style request.
|
|
588
|
+
*
|
|
589
|
+
* @category request
|
|
590
|
+
* @since 4.0.0
|
|
480
591
|
*/
|
|
481
592
|
export type Tool =
|
|
482
593
|
| FunctionTool
|
|
@@ -501,7 +612,11 @@ type ToolChoice =
|
|
|
501
612
|
}
|
|
502
613
|
|
|
503
614
|
/**
|
|
504
|
-
*
|
|
615
|
+
* Text output format configuration for plain text, JSON object, or JSON Schema
|
|
616
|
+
* responses.
|
|
617
|
+
*
|
|
618
|
+
* @category configuration
|
|
619
|
+
* @since 4.0.0
|
|
505
620
|
*/
|
|
506
621
|
export type TextResponseFormatConfiguration =
|
|
507
622
|
| {
|
|
@@ -519,7 +634,11 @@ export type TextResponseFormatConfiguration =
|
|
|
519
634
|
}
|
|
520
635
|
|
|
521
636
|
/**
|
|
522
|
-
*
|
|
637
|
+
* Request options for creating a Responses-style response with an
|
|
638
|
+
* OpenAI-compatible provider.
|
|
639
|
+
*
|
|
640
|
+
* @category request
|
|
641
|
+
* @since 4.0.0
|
|
523
642
|
*/
|
|
524
643
|
export type CreateResponse = {
|
|
525
644
|
readonly metadata?: Readonly<Record<string, string>> | null | undefined
|
|
@@ -556,7 +675,10 @@ export type CreateResponse = {
|
|
|
556
675
|
}
|
|
557
676
|
|
|
558
677
|
/**
|
|
559
|
-
*
|
|
678
|
+
* Token accounting reported on Responses-style response objects.
|
|
679
|
+
*
|
|
680
|
+
* @category response
|
|
681
|
+
* @since 4.0.0
|
|
560
682
|
*/
|
|
561
683
|
export type ResponseUsage = {
|
|
562
684
|
readonly input_tokens: number
|
|
@@ -573,7 +695,11 @@ type OutputItem =
|
|
|
573
695
|
| CustomToolCall
|
|
574
696
|
|
|
575
697
|
/**
|
|
576
|
-
*
|
|
698
|
+
* Responses-style response object returned by compatible providers or embedded
|
|
699
|
+
* in response stream lifecycle events.
|
|
700
|
+
*
|
|
701
|
+
* @category response
|
|
702
|
+
* @since 4.0.0
|
|
577
703
|
*/
|
|
578
704
|
export type Response = {
|
|
579
705
|
readonly id: string
|
|
@@ -699,7 +825,10 @@ type UnknownResponseStreamEvent = {
|
|
|
699
825
|
}
|
|
700
826
|
|
|
701
827
|
/**
|
|
702
|
-
*
|
|
828
|
+
* Server-sent event shapes emitted by Responses-style response streams.
|
|
829
|
+
*
|
|
830
|
+
* @category streaming
|
|
831
|
+
* @since 4.0.0
|
|
703
832
|
*/
|
|
704
833
|
export type ResponseStreamEvent =
|
|
705
834
|
| ResponseCreatedEvent
|
|
@@ -718,7 +847,16 @@ export type ResponseStreamEvent =
|
|
|
718
847
|
| UnknownResponseStreamEvent
|
|
719
848
|
|
|
720
849
|
/**
|
|
721
|
-
*
|
|
850
|
+
* Represents one embedding item returned by an OpenAI-compatible embeddings API.
|
|
851
|
+
*
|
|
852
|
+
* **Details**
|
|
853
|
+
*
|
|
854
|
+
* The embedding can be returned either as a numeric vector or as a base64-encoded
|
|
855
|
+
* string. The `index` field identifies the input item that produced this
|
|
856
|
+
* embedding.
|
|
857
|
+
*
|
|
858
|
+
* @category response
|
|
859
|
+
* @since 4.0.0
|
|
722
860
|
*/
|
|
723
861
|
export type Embedding = {
|
|
724
862
|
readonly embedding: ReadonlyArray<number> | string
|
|
@@ -727,7 +865,10 @@ export type Embedding = {
|
|
|
727
865
|
}
|
|
728
866
|
|
|
729
867
|
/**
|
|
730
|
-
*
|
|
868
|
+
* Request payload for the embeddings endpoint.
|
|
869
|
+
*
|
|
870
|
+
* @category request
|
|
871
|
+
* @since 4.0.0
|
|
731
872
|
*/
|
|
732
873
|
export type CreateEmbeddingRequest = {
|
|
733
874
|
readonly input: string | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<ReadonlyArray<number>>
|
|
@@ -738,7 +879,10 @@ export type CreateEmbeddingRequest = {
|
|
|
738
879
|
}
|
|
739
880
|
|
|
740
881
|
/**
|
|
741
|
-
*
|
|
882
|
+
* Successful response payload returned by the embeddings endpoint.
|
|
883
|
+
*
|
|
884
|
+
* @category response
|
|
885
|
+
* @since 4.0.0
|
|
742
886
|
*/
|
|
743
887
|
export type CreateEmbeddingResponse = {
|
|
744
888
|
readonly data: ReadonlyArray<Embedding>
|
|
@@ -751,15 +895,24 @@ export type CreateEmbeddingResponse = {
|
|
|
751
895
|
}
|
|
752
896
|
|
|
753
897
|
/**
|
|
754
|
-
*
|
|
898
|
+
* JSON request body accepted by the embeddings endpoint.
|
|
899
|
+
*
|
|
900
|
+
* @category request
|
|
901
|
+
* @since 4.0.0
|
|
755
902
|
*/
|
|
756
903
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
757
904
|
/**
|
|
758
|
-
*
|
|
905
|
+
* Decoded successful embeddings response body.
|
|
906
|
+
*
|
|
907
|
+
* @category response
|
|
908
|
+
* @since 4.0.0
|
|
759
909
|
*/
|
|
760
910
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
761
911
|
/**
|
|
762
|
-
*
|
|
912
|
+
* Structured content parts accepted in chat completion messages.
|
|
913
|
+
*
|
|
914
|
+
* @category request
|
|
915
|
+
* @since 4.0.0
|
|
763
916
|
*/
|
|
764
917
|
export type ChatCompletionContentPart =
|
|
765
918
|
| {
|
|
@@ -774,7 +927,10 @@ export type ChatCompletionContentPart =
|
|
|
774
927
|
}
|
|
775
928
|
}
|
|
776
929
|
/**
|
|
777
|
-
*
|
|
930
|
+
* Tool call data attached to an assistant chat completion message.
|
|
931
|
+
*
|
|
932
|
+
* @category request
|
|
933
|
+
* @since 4.0.0
|
|
778
934
|
*/
|
|
779
935
|
export type ChatCompletionRequestToolCall = {
|
|
780
936
|
readonly id: string
|
|
@@ -785,7 +941,10 @@ export type ChatCompletionRequestToolCall = {
|
|
|
785
941
|
}
|
|
786
942
|
}
|
|
787
943
|
/**
|
|
788
|
-
*
|
|
944
|
+
* Message shapes accepted by the chat completions endpoint.
|
|
945
|
+
*
|
|
946
|
+
* @category request
|
|
947
|
+
* @since 4.0.0
|
|
789
948
|
*/
|
|
790
949
|
export type ChatCompletionRequestMessage =
|
|
791
950
|
| {
|
|
@@ -799,7 +958,10 @@ export type ChatCompletionRequestMessage =
|
|
|
799
958
|
readonly content: string
|
|
800
959
|
}
|
|
801
960
|
/**
|
|
802
|
-
*
|
|
961
|
+
* Function tool definition accepted by the chat completions endpoint.
|
|
962
|
+
*
|
|
963
|
+
* @category request
|
|
964
|
+
* @since 4.0.0
|
|
803
965
|
*/
|
|
804
966
|
export type ChatCompletionTool = {
|
|
805
967
|
readonly type: "function"
|
|
@@ -811,7 +973,10 @@ export type ChatCompletionTool = {
|
|
|
811
973
|
}
|
|
812
974
|
}
|
|
813
975
|
/**
|
|
814
|
-
*
|
|
976
|
+
* Controls whether the model may call tools and can force a specific function.
|
|
977
|
+
*
|
|
978
|
+
* @category configuration
|
|
979
|
+
* @since 4.0.0
|
|
815
980
|
*/
|
|
816
981
|
export type ChatCompletionToolChoice =
|
|
817
982
|
| "none"
|
|
@@ -824,7 +989,10 @@ export type ChatCompletionToolChoice =
|
|
|
824
989
|
}
|
|
825
990
|
}
|
|
826
991
|
/**
|
|
827
|
-
*
|
|
992
|
+
* JSON response format configuration for chat completion requests.
|
|
993
|
+
*
|
|
994
|
+
* @category configuration
|
|
995
|
+
* @since 4.0.0
|
|
828
996
|
*/
|
|
829
997
|
export type ChatCompletionResponseFormat =
|
|
830
998
|
| {
|
|
@@ -840,7 +1008,10 @@ export type ChatCompletionResponseFormat =
|
|
|
840
1008
|
}
|
|
841
1009
|
}
|
|
842
1010
|
/**
|
|
843
|
-
*
|
|
1011
|
+
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
1012
|
+
*
|
|
1013
|
+
* @category request
|
|
1014
|
+
* @since 4.0.0
|
|
844
1015
|
*/
|
|
845
1016
|
export type ChatCompletionRequest = {
|
|
846
1017
|
readonly model: string
|
|
@@ -855,21 +1026,32 @@ export type ChatCompletionRequest = {
|
|
|
855
1026
|
readonly tools?: ReadonlyArray<ChatCompletionTool> | undefined
|
|
856
1027
|
readonly tool_choice?: ChatCompletionToolChoice | undefined
|
|
857
1028
|
readonly service_tier?: string | undefined
|
|
1029
|
+
readonly reasoning?: unknown
|
|
858
1030
|
readonly stream?: boolean | undefined
|
|
859
1031
|
readonly stream_options?: {
|
|
860
1032
|
readonly include_usage?: boolean | undefined
|
|
861
1033
|
} | undefined
|
|
1034
|
+
readonly [x: string]: unknown
|
|
862
1035
|
}
|
|
863
1036
|
/**
|
|
864
|
-
*
|
|
1037
|
+
* JSON request body used by this client when creating a chat completion response.
|
|
1038
|
+
*
|
|
1039
|
+
* @category request
|
|
1040
|
+
* @since 4.0.0
|
|
865
1041
|
*/
|
|
866
1042
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
867
1043
|
/**
|
|
868
|
-
*
|
|
1044
|
+
* Decoded successful chat completion response body returned by `createResponse`.
|
|
1045
|
+
*
|
|
1046
|
+
* @category response
|
|
1047
|
+
* @since 4.0.0
|
|
869
1048
|
*/
|
|
870
1049
|
export type CreateResponse200 = ChatCompletionResponse
|
|
871
1050
|
/**
|
|
872
|
-
*
|
|
1051
|
+
* Decoded server-sent event payload emitted by `createResponseStream`.
|
|
1052
|
+
*
|
|
1053
|
+
* @category streaming
|
|
1054
|
+
* @since 4.0.0
|
|
873
1055
|
*/
|
|
874
1056
|
export type CreateResponse200Sse = ChatCompletionStreamEvent
|
|
875
1057
|
|
|
@@ -894,6 +1076,14 @@ const ChatCompletionToolFunction = Schema.Struct({
|
|
|
894
1076
|
arguments: Schema.optionalKey(Schema.String)
|
|
895
1077
|
})
|
|
896
1078
|
|
|
1079
|
+
const ChatCompletionToolFunctionDelta = Schema.Struct({
|
|
1080
|
+
// Some OpenAI-compatible providers (e.g. Fireworks) send `name: null` on
|
|
1081
|
+
// streamed tool-call continuation fragments. `name` must be nullable, else
|
|
1082
|
+
// the whole chunk fails validation and its argument delta is dropped.
|
|
1083
|
+
name: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
1084
|
+
arguments: Schema.optionalKey(Schema.String)
|
|
1085
|
+
})
|
|
1086
|
+
|
|
897
1087
|
const ChatCompletionToolCall = Schema.Struct({
|
|
898
1088
|
id: Schema.optionalKey(Schema.String),
|
|
899
1089
|
index: Schema.optionalKey(Schema.Number),
|
|
@@ -901,16 +1091,27 @@ const ChatCompletionToolCall = Schema.Struct({
|
|
|
901
1091
|
function: Schema.optionalKey(ChatCompletionToolFunction)
|
|
902
1092
|
})
|
|
903
1093
|
|
|
1094
|
+
const ChatCompletionToolCallDelta = Schema.Struct({
|
|
1095
|
+
id: Schema.optionalKey(Schema.String),
|
|
1096
|
+
index: Schema.optionalKey(Schema.Number),
|
|
1097
|
+
type: Schema.optionalKey(Schema.String),
|
|
1098
|
+
function: Schema.optionalKey(ChatCompletionToolFunctionDelta)
|
|
1099
|
+
})
|
|
1100
|
+
|
|
904
1101
|
const ChatCompletionMessage = Schema.Struct({
|
|
905
1102
|
role: Schema.optionalKey(Schema.String),
|
|
906
1103
|
content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
1104
|
+
reasoning: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
1105
|
+
reasoning_content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
907
1106
|
tool_calls: Schema.optionalKey(Schema.Array(ChatCompletionToolCall))
|
|
908
1107
|
})
|
|
909
1108
|
|
|
910
1109
|
const ChatCompletionDelta = Schema.Struct({
|
|
911
1110
|
role: Schema.optionalKey(Schema.String),
|
|
912
1111
|
content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
913
|
-
|
|
1112
|
+
reasoning: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
1113
|
+
reasoning_content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
1114
|
+
tool_calls: Schema.optionalKey(Schema.Array(ChatCompletionToolCallDelta))
|
|
914
1115
|
})
|
|
915
1116
|
|
|
916
1117
|
const ChatCompletionChoice = Schema.Struct({
|
|
@@ -947,31 +1148,53 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
947
1148
|
})
|
|
948
1149
|
|
|
949
1150
|
/**
|
|
950
|
-
*
|
|
1151
|
+
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1152
|
+
*
|
|
1153
|
+
* @category response
|
|
1154
|
+
* @since 4.0.0
|
|
951
1155
|
*/
|
|
952
1156
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
953
1157
|
/**
|
|
954
|
-
*
|
|
1158
|
+
* Decoded message object from a non-streaming chat completion choice.
|
|
1159
|
+
*
|
|
1160
|
+
* @category response
|
|
1161
|
+
* @since 4.0.0
|
|
955
1162
|
*/
|
|
956
1163
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
957
1164
|
/**
|
|
958
|
-
*
|
|
1165
|
+
* Decoded choice object returned by chat completion responses and chunks.
|
|
1166
|
+
*
|
|
1167
|
+
* @category response
|
|
1168
|
+
* @since 4.0.0
|
|
959
1169
|
*/
|
|
960
1170
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
961
1171
|
/**
|
|
962
|
-
*
|
|
1172
|
+
* Decoded token usage summary returned by chat completions.
|
|
1173
|
+
*
|
|
1174
|
+
* @category response
|
|
1175
|
+
* @since 4.0.0
|
|
963
1176
|
*/
|
|
964
1177
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
965
1178
|
/**
|
|
966
|
-
*
|
|
1179
|
+
* Decoded successful response from the chat completions endpoint.
|
|
1180
|
+
*
|
|
1181
|
+
* @category response
|
|
1182
|
+
* @since 4.0.0
|
|
967
1183
|
*/
|
|
968
1184
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
969
1185
|
/**
|
|
970
|
-
*
|
|
1186
|
+
* Decoded streaming chunk emitted by the chat completions endpoint.
|
|
1187
|
+
*
|
|
1188
|
+
* @category streaming
|
|
1189
|
+
* @since 4.0.0
|
|
971
1190
|
*/
|
|
972
1191
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
973
1192
|
/**
|
|
974
|
-
*
|
|
1193
|
+
* Streaming chat completion event, including decoded chunks and the `[DONE]`
|
|
1194
|
+
* sentinel.
|
|
1195
|
+
*
|
|
1196
|
+
* @category streaming
|
|
1197
|
+
* @since 4.0.0
|
|
975
1198
|
*/
|
|
976
1199
|
export type ChatCompletionStreamEvent = ChatCompletionChunk | "[DONE]"
|
|
977
1200
|
|