@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.
Files changed (38) hide show
  1. package/dist/OpenAiClient.d.ts +198 -50
  2. package/dist/OpenAiClient.d.ts.map +1 -1
  3. package/dist/OpenAiClient.js +57 -9
  4. package/dist/OpenAiClient.js.map +1 -1
  5. package/dist/OpenAiConfig.d.ts +76 -10
  6. package/dist/OpenAiConfig.d.ts.map +1 -1
  7. package/dist/OpenAiConfig.js +44 -7
  8. package/dist/OpenAiConfig.js.map +1 -1
  9. package/dist/OpenAiEmbeddingModel.d.ts +90 -0
  10. package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
  11. package/dist/OpenAiEmbeddingModel.js +122 -0
  12. package/dist/OpenAiEmbeddingModel.js.map +1 -0
  13. package/dist/OpenAiError.d.ts +109 -35
  14. package/dist/OpenAiError.d.ts.map +1 -1
  15. package/dist/OpenAiError.js +14 -1
  16. package/dist/OpenAiLanguageModel.d.ts +219 -13
  17. package/dist/OpenAiLanguageModel.d.ts.map +1 -1
  18. package/dist/OpenAiLanguageModel.js +42 -19
  19. package/dist/OpenAiLanguageModel.js.map +1 -1
  20. package/dist/OpenAiTelemetry.d.ts +35 -23
  21. package/dist/OpenAiTelemetry.d.ts.map +1 -1
  22. package/dist/OpenAiTelemetry.js +6 -4
  23. package/dist/OpenAiTelemetry.js.map +1 -1
  24. package/dist/index.d.ts +70 -6
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +70 -6
  27. package/dist/index.js.map +1 -1
  28. package/dist/internal/errors.js +4 -4
  29. package/dist/internal/errors.js.map +1 -1
  30. package/package.json +3 -3
  31. package/src/OpenAiClient.ts +232 -49
  32. package/src/OpenAiConfig.ts +77 -11
  33. package/src/OpenAiEmbeddingModel.ts +208 -0
  34. package/src/OpenAiError.ts +111 -35
  35. package/src/OpenAiLanguageModel.ts +273 -30
  36. package/src/OpenAiTelemetry.ts +36 -24
  37. package/src/index.ts +71 -6
  38. package/src/internal/errors.ts +4 -4
@@ -5,7 +5,7 @@
5
5
  * semantic conventions, extending the base GenAI attributes with OpenAI-specific
6
6
  * request and response metadata.
7
7
  *
8
- * @since 1.0.0
8
+ * @since 4.0.0
9
9
  */
10
10
  import { dual } from "effect/Function"
11
11
  import * as String from "effect/String"
@@ -17,10 +17,9 @@ import * as Telemetry from "effect/unstable/ai/Telemetry"
17
17
  * The attributes used to describe telemetry in the context of Generative
18
18
  * Artificial Intelligence (GenAI) Models requests and responses.
19
19
  *
20
- * {@see https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/}
21
- *
22
- * @since 1.0.0
20
+ * @see https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
23
21
  * @category models
22
+ * @since 4.0.0
24
23
  */
25
24
  export type OpenAiTelemetryAttributes = Simplify<
26
25
  & Telemetry.GenAITelemetryAttributes
@@ -32,8 +31,8 @@ export type OpenAiTelemetryAttributes = Simplify<
32
31
  * All telemetry attributes which are part of the GenAI specification,
33
32
  * including the OpenAi-specific attributes.
34
33
  *
35
- * @since 1.0.0
36
34
  * @category models
35
+ * @since 4.0.0
37
36
  */
38
37
  export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
39
38
 
@@ -41,8 +40,8 @@ export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & Respon
41
40
  * Telemetry attributes which are part of the GenAI specification and are
42
41
  * namespaced by `gen_ai.openai.request`.
43
42
  *
44
- * @since 1.0.0
45
43
  * @category models
44
+ * @since 4.0.0
46
45
  */
47
46
  export interface RequestAttributes {
48
47
  /**
@@ -59,8 +58,8 @@ export interface RequestAttributes {
59
58
  * Telemetry attributes which are part of the GenAI specification and are
60
59
  * namespaced by `gen_ai.openai.response`.
61
60
  *
62
- * @since 1.0.0
63
61
  * @category models
62
+ * @since 4.0.0
64
63
  */
65
64
  export interface ResponseAttributes {
66
65
  /**
@@ -75,32 +74,39 @@ export interface ResponseAttributes {
75
74
  }
76
75
 
77
76
  /**
78
- * The `gen_ai.openai.request.response_format` attribute has the following
79
- * list of well-known values.
77
+ * The `gen_ai.openai.request.response_format` attribute has a list of
78
+ * well-known values.
79
+ *
80
+ * **Details**
80
81
  *
81
82
  * If one of them applies, then the respective value **MUST** be used;
82
83
  * otherwise, a custom value **MAY** be used.
83
84
  *
84
- * @since 1.0.0
85
85
  * @category models
86
+ * @since 4.0.0
86
87
  */
87
88
  export type WellKnownResponseFormat = "json_object" | "json_schema" | "text"
88
89
 
89
90
  /**
90
- * The `gen_ai.openai.request.service_tier` attribute has the following
91
- * list of well-known values.
91
+ * The `gen_ai.openai.request.service_tier` attribute has a list of
92
+ * well-known values.
93
+ *
94
+ * **Details**
92
95
  *
93
96
  * If one of them applies, then the respective value **MUST** be used;
94
97
  * otherwise, a custom value **MAY** be used.
95
98
  *
96
- * @since 1.0.0
97
99
  * @category models
100
+ * @since 4.0.0
98
101
  */
99
102
  export type WellKnownServiceTier = "auto" | "default"
100
103
 
101
104
  /**
102
- * @since 1.0.0
103
- * @since models
105
+ * Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry
106
+ * attributes with optional OpenAI-compatible request and response attributes.
107
+ *
108
+ * @category models
109
+ * @since 4.0.0
104
110
  */
105
111
  export type OpenAiTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
106
112
  openai?: {
@@ -120,30 +126,36 @@ const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("gen_ai.openai.r
120
126
  * Applies the specified OpenAi GenAI telemetry attributes to the provided
121
127
  * `Span`.
122
128
  *
123
- * **NOTE**: This method will mutate the `Span` **in-place**.
129
+ * **Gotchas**
130
+ *
131
+ * This method will mutate the `Span` **in-place**.
124
132
  *
125
- * @since 1.0.0
126
- * @since utilities
133
+ * @category tracing
134
+ * @since 4.0.0
127
135
  */
128
136
  export const addGenAIAnnotations: {
129
137
  /**
130
138
  * Applies the specified OpenAi GenAI telemetry attributes to the provided
131
139
  * `Span`.
132
140
  *
133
- * **NOTE**: This method will mutate the `Span` **in-place**.
141
+ * **Gotchas**
134
142
  *
135
- * @since 1.0.0
136
- * @since utilities
143
+ * This method will mutate the `Span` **in-place**.
144
+ *
145
+ * @category tracing
146
+ * @since 4.0.0
137
147
  */
138
148
  (options: OpenAiTelemetryAttributeOptions): (span: Span) => void
139
149
  /**
140
150
  * Applies the specified OpenAi GenAI telemetry attributes to the provided
141
151
  * `Span`.
142
152
  *
143
- * **NOTE**: This method will mutate the `Span` **in-place**.
153
+ * **Gotchas**
154
+ *
155
+ * This method will mutate the `Span` **in-place**.
144
156
  *
145
- * @since 1.0.0
146
- * @since utilities
157
+ * @category tracing
158
+ * @since 4.0.0
147
159
  */
148
160
  (span: Span, options: OpenAiTelemetryAttributeOptions): void
149
161
  } = dual(2, (span: Span, options: OpenAiTelemetryAttributeOptions) => {
package/src/index.ts CHANGED
@@ -1,21 +1,86 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
 
5
5
  // @barrel: Auto-generated exports. Do not edit manually.
6
6
 
7
7
  /**
8
- * @since 1.0.0
8
+ * The `OpenAiClient` module provides an Effect service for calling
9
+ * OpenAI-compatible chat completions and embeddings APIs. It builds on the
10
+ * Effect HTTP client, adds authentication and OpenAI header handling, and
11
+ * exposes typed helpers for regular responses, server-sent event streaming, and
12
+ * embedding requests.
13
+ *
14
+ * **Common tasks**
15
+ *
16
+ * - Create a client service directly with {@link make}
17
+ * - Provide the service as a layer with {@link layer} or {@link layerConfig}
18
+ * - Send non-streaming chat completion requests with `createResponse`
19
+ * - Send streaming chat completion requests with `createResponseStream`
20
+ * - Generate embeddings with `createEmbedding`
21
+ * - Reuse the exported request and response types when integrating compatible providers
22
+ *
23
+ * **Gotchas**
24
+ *
25
+ * - The default base URL is `https://api.openai.com/v1`; set `apiUrl` for other
26
+ * OpenAI-compatible providers.
27
+ * - `createResponseStream` forces `stream: true` and requests usage events with
28
+ * `stream_options.include_usage`.
29
+ * - HTTP and schema decoding failures are mapped into `AiError`.
30
+ *
31
+ * @since 4.0.0
9
32
  */
10
33
  export * as OpenAiClient from "./OpenAiClient.ts"
11
34
 
12
35
  /**
13
- * @since 1.0.0
36
+ * The `OpenAiConfig` module provides shared configuration for clients that
37
+ * talk to OpenAI-compatible APIs. It is used to customize the HTTP client
38
+ * wiring around a provider without changing the higher-level model,
39
+ * embeddings, or tool-calling APIs that consume the client.
40
+ *
41
+ * **Common tasks**
42
+ *
43
+ * - Install a client transform with {@link withClientTransform}
44
+ * - Add provider-specific HTTP behavior, such as headers, retries, proxies, or
45
+ * instrumentation
46
+ * - Read the active configuration from the Effect context when implementing
47
+ * OpenAI-compatible integrations
48
+ *
49
+ * **Gotchas**
50
+ *
51
+ * - The transform receives and returns an `HttpClient`, so it should preserve
52
+ * the existing client behavior unless it intentionally replaces it
53
+ * - Configuration is provided through Effect context and is scoped to the
54
+ * effect that receives the service
55
+ *
56
+ * @since 4.0.0
14
57
  */
15
58
  export * as OpenAiConfig from "./OpenAiConfig.ts"
16
59
 
17
60
  /**
18
- * @since 1.0.0
61
+ * OpenAI Embedding Model implementation.
62
+ *
63
+ * Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
64
+ *
65
+ * @since 4.0.0
66
+ */
67
+ export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.ts"
68
+
69
+ /**
70
+ * The `OpenAiError` module defines OpenAI-specific metadata that can be
71
+ * attached to the shared `AiError` error types used by the AI packages. It is
72
+ * primarily used by OpenAI-compatible clients to preserve provider details
73
+ * such as error codes, error types, request IDs, and rate limit headers while
74
+ * still exposing errors through the provider-neutral Effect AI error model.
75
+ *
76
+ * Use this module when mapping OpenAI API failures into `AiError` values and
77
+ * when consumers need enough structured metadata to debug failed requests,
78
+ * inspect quota or rate limit responses, or correlate an error with OpenAI
79
+ * support. The exported types are metadata shapes only; the module augmentation
80
+ * makes those shapes available on the corresponding shared AI error metadata
81
+ * interfaces without defining new runtime error classes.
82
+ *
83
+ * @since 4.0.0
19
84
  */
20
85
  export * as OpenAiError from "./OpenAiError.ts"
21
86
 
@@ -25,7 +90,7 @@ export * as OpenAiError from "./OpenAiError.ts"
25
90
  * Provides a LanguageModel implementation for OpenAI's chat completions API,
26
91
  * supporting text generation, structured output, tool calling, and streaming.
27
92
  *
28
- * @since 1.0.0
93
+ * @since 4.0.0
29
94
  */
30
95
  export * as OpenAiLanguageModel from "./OpenAiLanguageModel.ts"
31
96
 
@@ -36,6 +101,6 @@ export * as OpenAiLanguageModel from "./OpenAiLanguageModel.ts"
36
101
  * semantic conventions, extending the base GenAI attributes with OpenAI-specific
37
102
  * request and response metadata.
38
103
  *
39
- * @since 1.0.0
104
+ * @since 4.0.0
40
105
  */
41
106
  export * as OpenAiTelemetry from "./OpenAiTelemetry.ts"
@@ -153,12 +153,12 @@ export const parseRateLimitHeaders = (headers: Record<string, string>) => {
153
153
  let retryAfter: Duration.Duration | undefined
154
154
  if (retryAfterRaw !== undefined) {
155
155
  const parsed = Number.parse(retryAfterRaw)
156
- if (parsed !== undefined) {
157
- retryAfter = Duration.seconds(parsed)
156
+ if (Option.isSome(parsed)) {
157
+ retryAfter = Duration.seconds(parsed.value)
158
158
  }
159
159
  }
160
160
  const remainingRaw = headers["x-ratelimit-remaining-requests"]
161
- const remaining = remainingRaw !== undefined ? Number.parse(remainingRaw) ?? null : null
161
+ const remaining = remainingRaw !== undefined ? Option.getOrNull(Number.parse(remainingRaw)) : null
162
162
  return {
163
163
  retryAfter,
164
164
  limit: headers["x-ratelimit-limit-requests"] ?? null,
@@ -175,7 +175,7 @@ export const buildHttpRequestDetails = (
175
175
  method: request.method,
176
176
  url: request.url,
177
177
  urlParams: Array.from(request.urlParams),
178
- hash: request.hash,
178
+ hash: Option.getOrUndefined(request.hash),
179
179
  headers: Redactable.redact(request.headers) as Record<string, string>
180
180
  })
181
181