@effect/ai-openai-compat 4.0.0-beta.7 → 4.0.0-beta.71

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 +250 -51
  2. package/dist/OpenAiClient.d.ts.map +1 -1
  3. package/dist/OpenAiClient.js +108 -9
  4. package/dist/OpenAiClient.js.map +1 -1
  5. package/dist/OpenAiConfig.d.ts +83 -10
  6. package/dist/OpenAiConfig.d.ts.map +1 -1
  7. package/dist/OpenAiConfig.js +51 -7
  8. package/dist/OpenAiConfig.js.map +1 -1
  9. package/dist/OpenAiEmbeddingModel.d.ts +214 -0
  10. package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
  11. package/dist/OpenAiEmbeddingModel.js +218 -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 +326 -18
  17. package/dist/OpenAiLanguageModel.d.ts.map +1 -1
  18. package/dist/OpenAiLanguageModel.js +126 -25
  19. package/dist/OpenAiLanguageModel.js.map +1 -1
  20. package/dist/OpenAiTelemetry.d.ts +72 -22
  21. package/dist/OpenAiTelemetry.d.ts.map +1 -1
  22. package/dist/OpenAiTelemetry.js +47 -8
  23. package/dist/OpenAiTelemetry.js.map +1 -1
  24. package/dist/index.d.ts +10 -17
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +10 -17
  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 +283 -49
  32. package/src/OpenAiConfig.ts +84 -11
  33. package/src/OpenAiEmbeddingModel.ts +360 -0
  34. package/src/OpenAiError.ts +111 -35
  35. package/src/OpenAiLanguageModel.ts +409 -40
  36. package/src/OpenAiTelemetry.ts +103 -27
  37. package/src/index.ts +11 -17
  38. package/src/internal/errors.ts +4 -4
@@ -1,11 +1,37 @@
1
1
  /**
2
- * OpenAI telemetry attributes for OpenTelemetry integration.
2
+ * The `OpenAiTelemetry` module adds OpenAI-compatible provider attributes to
3
+ * the provider-neutral GenAI telemetry model. It keeps the standard
4
+ * `Telemetry.addGenAIAnnotations` attributes and adds OpenAI request and
5
+ * response metadata under the `gen_ai.openai.*` OpenTelemetry namespaces.
3
6
  *
4
- * Provides OpenAI-specific GenAI telemetry attributes following OpenTelemetry
5
- * semantic conventions, extending the base GenAI attributes with OpenAI-specific
6
- * request and response metadata.
7
+ * **Mental model**
7
8
  *
8
- * @since 1.0.0
9
+ * - Standard GenAI attributes come from `effect/unstable/ai/Telemetry`
10
+ * - OpenAI request attributes are written under `gen_ai.openai.request.*`
11
+ * - OpenAI response attributes are written under `gen_ai.openai.response.*`
12
+ * - Attribute option keys are written in camelCase and converted to
13
+ * OpenTelemetry snake_case attribute names
14
+ * - {@link addGenAIAnnotations} mutates the supplied span by adding any
15
+ * non-nullish attributes from the option object
16
+ *
17
+ * **Common tasks**
18
+ *
19
+ * - Use {@link OpenAiTelemetryAttributes} when typing the complete set of
20
+ * standard and OpenAI-specific span attributes
21
+ * - Pass `openai.request` data for requested response format and service tier
22
+ * - Pass `openai.response` data for the service tier actually used and the
23
+ * system fingerprint returned by the provider
24
+ * - Use {@link addGenAIAnnotations} from an OpenAI-compatible model span to keep
25
+ * standard GenAI and provider-specific annotations together
26
+ *
27
+ * **Gotchas**
28
+ *
29
+ * - This module only annotates spans; it does not start spans or export traces
30
+ * - Null and undefined attribute values are skipped instead of being written
31
+ * - OpenAI-compatible providers may not return every OpenAI-specific response
32
+ * field, so only pass fields that are present on the provider response
33
+ *
34
+ * @since 4.0.0
9
35
  */
10
36
  import { dual } from "effect/Function"
11
37
  import * as String from "effect/String"
@@ -17,10 +43,14 @@ import * as Telemetry from "effect/unstable/ai/Telemetry"
17
43
  * The attributes used to describe telemetry in the context of Generative
18
44
  * Artificial Intelligence (GenAI) Models requests and responses.
19
45
  *
20
- * {@see https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/}
46
+ * **Details**
47
+ *
48
+ * These attributes follow the OpenTelemetry generative AI semantic
49
+ * conventions:
50
+ * https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
21
51
  *
22
- * @since 1.0.0
23
52
  * @category models
53
+ * @since 4.0.0
24
54
  */
25
55
  export type OpenAiTelemetryAttributes = Simplify<
26
56
  & Telemetry.GenAITelemetryAttributes
@@ -32,8 +62,8 @@ export type OpenAiTelemetryAttributes = Simplify<
32
62
  * All telemetry attributes which are part of the GenAI specification,
33
63
  * including the OpenAi-specific attributes.
34
64
  *
35
- * @since 1.0.0
36
65
  * @category models
66
+ * @since 4.0.0
37
67
  */
38
68
  export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
39
69
 
@@ -41,8 +71,8 @@ export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & Respon
41
71
  * Telemetry attributes which are part of the GenAI specification and are
42
72
  * namespaced by `gen_ai.openai.request`.
43
73
  *
44
- * @since 1.0.0
45
74
  * @category models
75
+ * @since 4.0.0
46
76
  */
47
77
  export interface RequestAttributes {
48
78
  /**
@@ -59,8 +89,8 @@ export interface RequestAttributes {
59
89
  * Telemetry attributes which are part of the GenAI specification and are
60
90
  * namespaced by `gen_ai.openai.response`.
61
91
  *
62
- * @since 1.0.0
63
92
  * @category models
93
+ * @since 4.0.0
64
94
  */
65
95
  export interface ResponseAttributes {
66
96
  /**
@@ -75,32 +105,39 @@ export interface ResponseAttributes {
75
105
  }
76
106
 
77
107
  /**
78
- * The `gen_ai.openai.request.response_format` attribute has the following
79
- * list of well-known values.
108
+ * The `gen_ai.openai.request.response_format` attribute has a list of
109
+ * well-known values.
110
+ *
111
+ * **Details**
80
112
  *
81
113
  * If one of them applies, then the respective value **MUST** be used;
82
114
  * otherwise, a custom value **MAY** be used.
83
115
  *
84
- * @since 1.0.0
85
116
  * @category models
117
+ * @since 4.0.0
86
118
  */
87
119
  export type WellKnownResponseFormat = "json_object" | "json_schema" | "text"
88
120
 
89
121
  /**
90
- * The `gen_ai.openai.request.service_tier` attribute has the following
91
- * list of well-known values.
122
+ * The `gen_ai.openai.request.service_tier` attribute has a list of
123
+ * well-known values.
124
+ *
125
+ * **Details**
92
126
  *
93
127
  * If one of them applies, then the respective value **MUST** be used;
94
128
  * otherwise, a custom value **MAY** be used.
95
129
  *
96
- * @since 1.0.0
97
130
  * @category models
131
+ * @since 4.0.0
98
132
  */
99
133
  export type WellKnownServiceTier = "auto" | "default"
100
134
 
101
135
  /**
102
- * @since 1.0.0
103
- * @since models
136
+ * Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry
137
+ * attributes with optional OpenAI-compatible request and response attributes.
138
+ *
139
+ * @category models
140
+ * @since 4.0.0
104
141
  */
105
142
  export type OpenAiTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
106
143
  openai?: {
@@ -120,30 +157,69 @@ const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("gen_ai.openai.r
120
157
  * Applies the specified OpenAi GenAI telemetry attributes to the provided
121
158
  * `Span`.
122
159
  *
123
- * **NOTE**: This method will mutate the `Span` **in-place**.
160
+ * **When to use**
161
+ *
162
+ * Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
163
+ * attributes and OpenAI-specific request or response metadata.
164
+ *
165
+ * **Details**
166
+ *
167
+ * Standard GenAI attributes are applied first. When OpenAI request or response
168
+ * metadata is present, it is written under `gen_ai.openai.request.*` and
169
+ * `gen_ai.openai.response.*` attributes.
124
170
  *
125
- * @since 1.0.0
126
- * @since utilities
171
+ * **Gotchas**
172
+ *
173
+ * This method will mutate the `Span` **in-place**.
174
+ *
175
+ * @category tracing
176
+ * @since 4.0.0
127
177
  */
128
178
  export const addGenAIAnnotations: {
129
179
  /**
130
180
  * Applies the specified OpenAi GenAI telemetry attributes to the provided
131
181
  * `Span`.
132
182
  *
133
- * **NOTE**: This method will mutate the `Span` **in-place**.
183
+ * **When to use**
184
+ *
185
+ * Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
186
+ * attributes and OpenAI-specific request or response metadata.
187
+ *
188
+ * **Details**
189
+ *
190
+ * Standard GenAI attributes are applied first. When OpenAI request or response
191
+ * metadata is present, it is written under `gen_ai.openai.request.*` and
192
+ * `gen_ai.openai.response.*` attributes.
193
+ *
194
+ * **Gotchas**
134
195
  *
135
- * @since 1.0.0
136
- * @since utilities
196
+ * This method will mutate the `Span` **in-place**.
197
+ *
198
+ * @category tracing
199
+ * @since 4.0.0
137
200
  */
138
201
  (options: OpenAiTelemetryAttributeOptions): (span: Span) => void
139
202
  /**
140
203
  * Applies the specified OpenAi GenAI telemetry attributes to the provided
141
204
  * `Span`.
142
205
  *
143
- * **NOTE**: This method will mutate the `Span` **in-place**.
206
+ * **When to use**
207
+ *
208
+ * Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
209
+ * attributes and OpenAI-specific request or response metadata.
210
+ *
211
+ * **Details**
212
+ *
213
+ * Standard GenAI attributes are applied first. When OpenAI request or response
214
+ * metadata is present, it is written under `gen_ai.openai.request.*` and
215
+ * `gen_ai.openai.response.*` attributes.
216
+ *
217
+ * **Gotchas**
218
+ *
219
+ * This method will mutate the `Span` **in-place**.
144
220
  *
145
- * @since 1.0.0
146
- * @since utilities
221
+ * @category tracing
222
+ * @since 4.0.0
147
223
  */
148
224
  (span: Span, options: OpenAiTelemetryAttributeOptions): void
149
225
  } = dual(2, (span: Span, options: OpenAiTelemetryAttributeOptions) => {
package/src/index.ts CHANGED
@@ -1,41 +1,35 @@
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
+ * @since 4.0.0
9
9
  */
10
10
  export * as OpenAiClient from "./OpenAiClient.ts"
11
11
 
12
12
  /**
13
- * @since 1.0.0
13
+ * @since 4.0.0
14
14
  */
15
15
  export * as OpenAiConfig from "./OpenAiConfig.ts"
16
16
 
17
17
  /**
18
- * @since 1.0.0
18
+ * @since 4.0.0
19
+ */
20
+ export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.ts"
21
+
22
+ /**
23
+ * @since 4.0.0
19
24
  */
20
25
  export * as OpenAiError from "./OpenAiError.ts"
21
26
 
22
27
  /**
23
- * OpenAI Language Model implementation.
24
- *
25
- * Provides a LanguageModel implementation for OpenAI's chat completions API,
26
- * supporting text generation, structured output, tool calling, and streaming.
27
- *
28
- * @since 1.0.0
28
+ * @since 4.0.0
29
29
  */
30
30
  export * as OpenAiLanguageModel from "./OpenAiLanguageModel.ts"
31
31
 
32
32
  /**
33
- * OpenAI telemetry attributes for OpenTelemetry integration.
34
- *
35
- * Provides OpenAI-specific GenAI telemetry attributes following OpenTelemetry
36
- * semantic conventions, extending the base GenAI attributes with OpenAI-specific
37
- * request and response metadata.
38
- *
39
- * @since 1.0.0
33
+ * @since 4.0.0
40
34
  */
41
35
  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