@effect/ai-openai-compat 4.0.0-beta.8 → 4.0.0-beta.81

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 +264 -52
  2. package/dist/OpenAiClient.d.ts.map +1 -1
  3. package/dist/OpenAiClient.js +97 -9
  4. package/dist/OpenAiClient.js.map +1 -1
  5. package/dist/OpenAiConfig.d.ts +68 -10
  6. package/dist/OpenAiConfig.d.ts.map +1 -1
  7. package/dist/OpenAiConfig.js +36 -7
  8. package/dist/OpenAiConfig.js.map +1 -1
  9. package/dist/OpenAiEmbeddingModel.d.ts +186 -0
  10. package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
  11. package/dist/OpenAiEmbeddingModel.js +190 -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 +304 -20
  17. package/dist/OpenAiLanguageModel.d.ts.map +1 -1
  18. package/dist/OpenAiLanguageModel.js +157 -27
  19. package/dist/OpenAiLanguageModel.js.map +1 -1
  20. package/dist/OpenAiTelemetry.d.ts +76 -26
  21. package/dist/OpenAiTelemetry.d.ts.map +1 -1
  22. package/dist/OpenAiTelemetry.js +22 -10
  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 +273 -50
  32. package/src/OpenAiConfig.ts +69 -11
  33. package/src/OpenAiEmbeddingModel.ts +332 -0
  34. package/src/OpenAiError.ts +111 -35
  35. package/src/OpenAiLanguageModel.ts +425 -42
  36. package/src/OpenAiTelemetry.ts +81 -32
  37. package/src/index.ts +11 -17
  38. package/src/internal/errors.ts +4 -4
@@ -1,11 +1,10 @@
1
1
  /**
2
- * OpenAI telemetry attributes for OpenTelemetry integration.
2
+ * The `OpenAiTelemetry` module defines OpenAI-compatible telemetry attributes
3
+ * and a helper for adding them to a tracing span. It keeps the standard GenAI
4
+ * telemetry attributes and adds request and response metadata under the
5
+ * `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
- *
8
- * @since 1.0.0
7
+ * @since 4.0.0
9
8
  */
10
9
  import { dual } from "effect/Function"
11
10
  import * as String from "effect/String"
@@ -17,10 +16,14 @@ import * as Telemetry from "effect/unstable/ai/Telemetry"
17
16
  * The attributes used to describe telemetry in the context of Generative
18
17
  * Artificial Intelligence (GenAI) Models requests and responses.
19
18
  *
20
- * {@see https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/}
19
+ * **Details**
20
+ *
21
+ * These attributes follow the OpenTelemetry generative AI semantic
22
+ * conventions:
23
+ * https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
21
24
  *
22
- * @since 1.0.0
23
25
  * @category models
26
+ * @since 4.0.0
24
27
  */
25
28
  export type OpenAiTelemetryAttributes = Simplify<
26
29
  & Telemetry.GenAITelemetryAttributes
@@ -30,10 +33,10 @@ export type OpenAiTelemetryAttributes = Simplify<
30
33
 
31
34
  /**
32
35
  * All telemetry attributes which are part of the GenAI specification,
33
- * including the OpenAi-specific attributes.
36
+ * including the OpenAI-specific attributes.
34
37
  *
35
- * @since 1.0.0
36
38
  * @category models
39
+ * @since 4.0.0
37
40
  */
38
41
  export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
39
42
 
@@ -41,8 +44,8 @@ export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & Respon
41
44
  * Telemetry attributes which are part of the GenAI specification and are
42
45
  * namespaced by `gen_ai.openai.request`.
43
46
  *
44
- * @since 1.0.0
45
47
  * @category models
48
+ * @since 4.0.0
46
49
  */
47
50
  export interface RequestAttributes {
48
51
  /**
@@ -59,8 +62,8 @@ export interface RequestAttributes {
59
62
  * Telemetry attributes which are part of the GenAI specification and are
60
63
  * namespaced by `gen_ai.openai.response`.
61
64
  *
62
- * @since 1.0.0
63
65
  * @category models
66
+ * @since 4.0.0
64
67
  */
65
68
  export interface ResponseAttributes {
66
69
  /**
@@ -75,32 +78,39 @@ export interface ResponseAttributes {
75
78
  }
76
79
 
77
80
  /**
78
- * The `gen_ai.openai.request.response_format` attribute has the following
79
- * list of well-known values.
81
+ * The `gen_ai.openai.request.response_format` attribute has a list of
82
+ * well-known values.
83
+ *
84
+ * **Details**
80
85
  *
81
86
  * If one of them applies, then the respective value **MUST** be used;
82
87
  * otherwise, a custom value **MAY** be used.
83
88
  *
84
- * @since 1.0.0
85
89
  * @category models
90
+ * @since 4.0.0
86
91
  */
87
92
  export type WellKnownResponseFormat = "json_object" | "json_schema" | "text"
88
93
 
89
94
  /**
90
- * The `gen_ai.openai.request.service_tier` attribute has the following
91
- * list of well-known values.
95
+ * The `gen_ai.openai.request.service_tier` attribute has a list of
96
+ * well-known values.
97
+ *
98
+ * **Details**
92
99
  *
93
100
  * If one of them applies, then the respective value **MUST** be used;
94
101
  * otherwise, a custom value **MAY** be used.
95
102
  *
96
- * @since 1.0.0
97
103
  * @category models
104
+ * @since 4.0.0
98
105
  */
99
106
  export type WellKnownServiceTier = "auto" | "default"
100
107
 
101
108
  /**
102
- * @since 1.0.0
103
- * @since models
109
+ * Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry
110
+ * attributes with optional OpenAI-compatible request and response attributes.
111
+ *
112
+ * @category options
113
+ * @since 4.0.0
104
114
  */
105
115
  export type OpenAiTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
106
116
  openai?: {
@@ -117,33 +127,72 @@ const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("gen_ai.openai.r
117
127
  >
118
128
 
119
129
  /**
120
- * Applies the specified OpenAi GenAI telemetry attributes to the provided
130
+ * Applies the specified OpenAI GenAI telemetry attributes to the provided
121
131
  * `Span`.
122
132
  *
123
- * **NOTE**: This method will mutate the `Span` **in-place**.
133
+ * **When to use**
134
+ *
135
+ * Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
136
+ * attributes and OpenAI-specific request or response metadata.
137
+ *
138
+ * **Details**
139
+ *
140
+ * Standard GenAI attributes are applied first. When OpenAI request or response
141
+ * metadata is present, it is written under `gen_ai.openai.request.*` and
142
+ * `gen_ai.openai.response.*` attributes.
143
+ *
144
+ * **Gotchas**
145
+ *
146
+ * Mutates the supplied `Span` in place.
124
147
  *
125
- * @since 1.0.0
126
- * @since utilities
148
+ * @category tracing
149
+ * @since 4.0.0
127
150
  */
128
151
  export const addGenAIAnnotations: {
129
152
  /**
130
- * Applies the specified OpenAi GenAI telemetry attributes to the provided
153
+ * Applies the specified OpenAI GenAI telemetry attributes to the provided
131
154
  * `Span`.
132
155
  *
133
- * **NOTE**: This method will mutate the `Span` **in-place**.
156
+ * **When to use**
134
157
  *
135
- * @since 1.0.0
136
- * @since utilities
158
+ * Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
159
+ * attributes and OpenAI-specific request or response metadata.
160
+ *
161
+ * **Details**
162
+ *
163
+ * Standard GenAI attributes are applied first. When OpenAI request or response
164
+ * metadata is present, it is written under `gen_ai.openai.request.*` and
165
+ * `gen_ai.openai.response.*` attributes.
166
+ *
167
+ * **Gotchas**
168
+ *
169
+ * Mutates the supplied `Span` in place.
170
+ *
171
+ * @category tracing
172
+ * @since 4.0.0
137
173
  */
138
174
  (options: OpenAiTelemetryAttributeOptions): (span: Span) => void
139
175
  /**
140
- * Applies the specified OpenAi GenAI telemetry attributes to the provided
176
+ * Applies the specified OpenAI GenAI telemetry attributes to the provided
141
177
  * `Span`.
142
178
  *
143
- * **NOTE**: This method will mutate the `Span` **in-place**.
179
+ * **When to use**
180
+ *
181
+ * Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
182
+ * attributes and OpenAI-specific request or response metadata.
183
+ *
184
+ * **Details**
185
+ *
186
+ * Standard GenAI attributes are applied first. When OpenAI request or response
187
+ * metadata is present, it is written under `gen_ai.openai.request.*` and
188
+ * `gen_ai.openai.response.*` attributes.
189
+ *
190
+ * **Gotchas**
191
+ *
192
+ * Mutates the supplied `Span` in place.
144
193
  *
145
- * @since 1.0.0
146
- * @since utilities
194
+ * @category tracing
195
+ * @since 4.0.0
147
196
  */
148
197
  (span: Span, options: OpenAiTelemetryAttributeOptions): void
149
198
  } = 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