@effect/ai-openai 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 (58) hide show
  1. package/dist/Generated.d.ts +66734 -37723
  2. package/dist/Generated.d.ts.map +1 -1
  3. package/dist/Generated.js +1 -1
  4. package/dist/Generated.js.map +1 -1
  5. package/dist/OpenAiClient.d.ts +167 -27
  6. package/dist/OpenAiClient.d.ts.map +1 -1
  7. package/dist/OpenAiClient.js +337 -44
  8. package/dist/OpenAiClient.js.map +1 -1
  9. package/dist/OpenAiClientGenerated.d.ts +91 -0
  10. package/dist/OpenAiClientGenerated.d.ts.map +1 -0
  11. package/dist/OpenAiClientGenerated.js +84 -0
  12. package/dist/OpenAiClientGenerated.js.map +1 -0
  13. package/dist/OpenAiConfig.d.ts +114 -10
  14. package/dist/OpenAiConfig.d.ts.map +1 -1
  15. package/dist/OpenAiConfig.js +68 -7
  16. package/dist/OpenAiConfig.js.map +1 -1
  17. package/dist/OpenAiEmbeddingModel.d.ts +213 -0
  18. package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
  19. package/dist/OpenAiEmbeddingModel.js +219 -0
  20. package/dist/OpenAiEmbeddingModel.js.map +1 -0
  21. package/dist/OpenAiError.d.ts +168 -35
  22. package/dist/OpenAiError.d.ts.map +1 -1
  23. package/dist/OpenAiError.js +1 -1
  24. package/dist/OpenAiLanguageModel.d.ts +384 -62
  25. package/dist/OpenAiLanguageModel.d.ts.map +1 -1
  26. package/dist/OpenAiLanguageModel.js +416 -166
  27. package/dist/OpenAiLanguageModel.js.map +1 -1
  28. package/dist/OpenAiSchema.d.ts +2298 -0
  29. package/dist/OpenAiSchema.d.ts.map +1 -0
  30. package/dist/OpenAiSchema.js +814 -0
  31. package/dist/OpenAiSchema.js.map +1 -0
  32. package/dist/OpenAiTelemetry.d.ts +59 -18
  33. package/dist/OpenAiTelemetry.d.ts.map +1 -1
  34. package/dist/OpenAiTelemetry.js +35 -8
  35. package/dist/OpenAiTelemetry.js.map +1 -1
  36. package/dist/OpenAiTool.d.ts +157 -62
  37. package/dist/OpenAiTool.d.ts.map +1 -1
  38. package/dist/OpenAiTool.js +134 -39
  39. package/dist/OpenAiTool.js.map +1 -1
  40. package/dist/index.d.ts +19 -33
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +19 -33
  43. package/dist/index.js.map +1 -1
  44. package/dist/internal/errors.js +4 -4
  45. package/dist/internal/errors.js.map +1 -1
  46. package/package.json +3 -3
  47. package/src/Generated.ts +9858 -5044
  48. package/src/OpenAiClient.ts +513 -95
  49. package/src/OpenAiClientGenerated.ts +202 -0
  50. package/src/OpenAiConfig.ts +115 -11
  51. package/src/OpenAiEmbeddingModel.ts +357 -0
  52. package/src/OpenAiError.ts +170 -35
  53. package/src/OpenAiLanguageModel.ts +802 -167
  54. package/src/OpenAiSchema.ts +1289 -0
  55. package/src/OpenAiTelemetry.ts +81 -23
  56. package/src/OpenAiTool.ts +135 -40
  57. package/src/index.ts +22 -33
  58. package/src/internal/errors.ts +6 -4
@@ -0,0 +1,202 @@
1
+ /**
2
+ * @since 4.0.0
3
+ */
4
+ import * as Array from "effect/Array"
5
+ import type * as Config from "effect/Config"
6
+ import * as Context from "effect/Context"
7
+ import * as Effect from "effect/Effect"
8
+ import { identity } from "effect/Function"
9
+ import * as Function from "effect/Function"
10
+ import * as Layer from "effect/Layer"
11
+ import * as Predicate from "effect/Predicate"
12
+ import * as Redacted from "effect/Redacted"
13
+ import * as Headers from "effect/unstable/http/Headers"
14
+ import * as HttpClient from "effect/unstable/http/HttpClient"
15
+ import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest"
16
+ import * as Generated from "./Generated.ts"
17
+ import { OpenAiConfig } from "./OpenAiConfig.ts"
18
+
19
+ // =============================================================================
20
+ // Service Identifier
21
+ // =============================================================================
22
+
23
+ /**
24
+ * Service identifier for the generated OpenAI client.
25
+ *
26
+ * @since 4.0.0
27
+ * @category service
28
+ */
29
+ export class OpenAiClientGenerated extends Context.Service<OpenAiClientGenerated, Generated.OpenAiClient>()(
30
+ "@effect/ai-openai/OpenAiClientGenerated"
31
+ ) {}
32
+
33
+ // =============================================================================
34
+ // Options
35
+ // =============================================================================
36
+
37
+ /**
38
+ * Options for configuring the generated OpenAI client.
39
+ *
40
+ * @since 4.0.0
41
+ * @category models
42
+ */
43
+ export type Options = {
44
+ /**
45
+ * The OpenAI API key.
46
+ */
47
+ readonly apiKey?: Redacted.Redacted<string> | undefined
48
+
49
+ /**
50
+ * The base URL for the OpenAI API.
51
+ *
52
+ * @default "https://api.openai.com/v1"
53
+ */
54
+ readonly apiUrl?: string | undefined
55
+
56
+ /**
57
+ * Optional organization ID for multi-org accounts.
58
+ */
59
+ readonly organizationId?: Redacted.Redacted<string> | undefined
60
+
61
+ /**
62
+ * Optional project ID for project-scoped requests.
63
+ */
64
+ readonly projectId?: Redacted.Redacted<string> | undefined
65
+
66
+ /**
67
+ * Optional transformer for the HTTP client.
68
+ */
69
+ readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
70
+ }
71
+
72
+ const RedactedOpenAiHeaders = {
73
+ OpenAiOrganization: "OpenAI-Organization",
74
+ OpenAiProject: "OpenAI-Project"
75
+ }
76
+
77
+ // =============================================================================
78
+ // Constructor
79
+ // =============================================================================
80
+
81
+ /**
82
+ * Creates a generated OpenAI client service with the given options.
83
+ *
84
+ * @since 4.0.0
85
+ * @category constructors
86
+ */
87
+ export const make = Effect.fnUntraced(
88
+ function*(options: Options): Effect.fn.Return<Generated.OpenAiClient, never, HttpClient.HttpClient> {
89
+ const baseClient = yield* HttpClient.HttpClient
90
+ const apiUrl = options.apiUrl ?? "https://api.openai.com/v1"
91
+
92
+ const httpClient = baseClient.pipe(
93
+ HttpClient.mapRequest(Function.flow(
94
+ HttpClientRequest.prependUrl(apiUrl),
95
+ options.apiKey
96
+ ? HttpClientRequest.bearerToken(Redacted.value(options.apiKey))
97
+ : identity,
98
+ options.organizationId
99
+ ? HttpClientRequest.setHeader(
100
+ RedactedOpenAiHeaders.OpenAiOrganization,
101
+ Redacted.value(options.organizationId)
102
+ )
103
+ : identity,
104
+ options.projectId
105
+ ? HttpClientRequest.setHeader(
106
+ RedactedOpenAiHeaders.OpenAiProject,
107
+ Redacted.value(options.projectId)
108
+ )
109
+ : identity,
110
+ HttpClientRequest.acceptJson
111
+ )),
112
+ options.transformClient
113
+ ? options.transformClient
114
+ : identity
115
+ )
116
+
117
+ return Generated.make(httpClient, {
118
+ transformClient: Effect.fnUntraced(function*(client) {
119
+ const config = yield* OpenAiConfig.getOrUndefined
120
+ if (Predicate.isNotUndefined(config?.transformClient)) {
121
+ return config.transformClient(client)
122
+ }
123
+ return client
124
+ })
125
+ })
126
+ },
127
+ Effect.updateService(
128
+ Headers.CurrentRedactedNames,
129
+ Array.appendAll(Object.values(RedactedOpenAiHeaders))
130
+ )
131
+ )
132
+
133
+ // =============================================================================
134
+ // Layers
135
+ // =============================================================================
136
+
137
+ /**
138
+ * Creates a layer for the generated OpenAI client with the given options.
139
+ *
140
+ * @since 4.0.0
141
+ * @category layers
142
+ */
143
+ export const layer = (options: Options): Layer.Layer<OpenAiClientGenerated, never, HttpClient.HttpClient> =>
144
+ Layer.effect(OpenAiClientGenerated, make(options))
145
+
146
+ /**
147
+ * Creates a layer for the generated OpenAI client, loading the requisite
148
+ * configuration via Effect's `Config` module.
149
+ *
150
+ * @since 4.0.0
151
+ * @category layers
152
+ */
153
+ export const layerConfig = (options?: {
154
+ /**
155
+ * The config value to load for the API key.
156
+ */
157
+ readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
158
+
159
+ /**
160
+ * The config value to load for the API URL.
161
+ */
162
+ readonly apiUrl?: Config.Config<string> | undefined
163
+
164
+ /**
165
+ * The config value to load for the organization ID.
166
+ */
167
+ readonly organizationId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
168
+
169
+ /**
170
+ * The config value to load for the project ID.
171
+ */
172
+ readonly projectId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
173
+
174
+ /**
175
+ * Optional transformer for the HTTP client.
176
+ */
177
+ readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
178
+ }): Layer.Layer<OpenAiClientGenerated, Config.ConfigError, HttpClient.HttpClient> =>
179
+ Layer.effect(
180
+ OpenAiClientGenerated,
181
+ Effect.gen(function*() {
182
+ const apiKey = Predicate.isNotUndefined(options?.apiKey)
183
+ ? yield* options.apiKey :
184
+ undefined
185
+ const apiUrl = Predicate.isNotUndefined(options?.apiUrl)
186
+ ? yield* options.apiUrl :
187
+ undefined
188
+ const organizationId = Predicate.isNotUndefined(options?.organizationId)
189
+ ? yield* options.organizationId
190
+ : undefined
191
+ const projectId = Predicate.isNotUndefined(options?.projectId)
192
+ ? yield* options.projectId :
193
+ undefined
194
+ return yield* make({
195
+ apiKey,
196
+ apiUrl,
197
+ organizationId,
198
+ projectId,
199
+ transformClient: options?.transformClient
200
+ })
201
+ })
202
+ )
@@ -1,35 +1,82 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * The `OpenAiConfig` module carries request-time configuration for the
3
+ * `@effect/ai-openai` package through Effect's context. It currently exposes a
4
+ * scoped HTTP client transform used by OpenAI request helpers when they execute
5
+ * provider calls.
6
+ *
7
+ * **Mental model**
8
+ *
9
+ * Client constructors set the baseline HTTP client for an OpenAI service.
10
+ * `OpenAiConfig` is for narrower dynamic scopes: wrap an effect with
11
+ * {@link withClientTransform} and OpenAI requests evaluated inside that effect
12
+ * see the transformed `HttpClient`. Leaving the scope restores the previous
13
+ * configuration.
14
+ *
15
+ * **Common tasks**
16
+ *
17
+ * - Add request middleware, tracing, retry policy, proxy routing, or test
18
+ * interception around a group of OpenAI calls.
19
+ * - Apply a temporary HTTP client transform without rebuilding the OpenAI
20
+ * service layer.
21
+ * - Share one transform across all OpenAI helpers executed inside the same
22
+ * Effect scope.
23
+ *
24
+ * **Gotchas**
25
+ *
26
+ * - Only one `transformClient` value is stored in the scoped config. Compose
27
+ * transforms into a single `HttpClient => HttpClient` function when multiple
28
+ * behaviors should apply.
29
+ * - The transform affects OpenAI requests that read this contextual config; it
30
+ * does not mutate an already constructed HTTP client globally.
31
+ *
32
+ * @since 4.0.0
3
33
  */
34
+ import * as Context from "effect/Context"
4
35
  import * as Effect from "effect/Effect"
5
36
  import { dual } from "effect/Function"
6
- import * as ServiceMap from "effect/ServiceMap"
7
37
  import type { HttpClient } from "effect/unstable/http/HttpClient"
8
38
 
9
39
  /**
10
- * @since 1.0.0
40
+ * Context service carrying scoped OpenAI configuration for provider
41
+ * operations.
42
+ *
43
+ * **When to use**
44
+ *
45
+ * Use to provide scoped OpenAI client configuration, such as an HTTP client
46
+ * transform, to OpenAI provider operations without passing it through each call.
47
+ *
48
+ * @see {@link withClientTransform} for scoping an HTTP client transformation
49
+ *
11
50
  * @category services
51
+ * @since 4.0.0
12
52
  */
13
- export class OpenAiConfig extends ServiceMap.Service<
53
+ export class OpenAiConfig extends Context.Service<
14
54
  OpenAiConfig,
15
55
  OpenAiConfig.Service
16
56
  >()("@effect/ai-openai/OpenAiConfig") {
17
57
  /**
18
- * @since 1.0.0
58
+ * Gets the configured OpenAI service from the current context when present.
59
+ *
60
+ * @since 4.0.0
19
61
  */
20
62
  static readonly getOrUndefined: Effect.Effect<typeof OpenAiConfig.Service | undefined> = Effect.map(
21
- Effect.services<never>(),
63
+ Effect.context<never>(),
22
64
  (context) => context.mapUnsafe.get(OpenAiConfig.key)
23
65
  )
24
66
  }
25
67
 
26
68
  /**
27
- * @since 1.0.0
69
+ * Types used by the `OpenAiConfig` context service.
70
+ *
71
+ * @since 4.0.0
28
72
  */
29
73
  export declare namespace OpenAiConfig {
30
74
  /**
31
- * @since 1.0.
75
+ * Configuration values read by OpenAI provider operations when executing
76
+ * requests.
77
+ *
32
78
  * @category models
79
+ * @since 4.0.0
33
80
  */
34
81
  export interface Service {
35
82
  readonly transformClient?: ((client: HttpClient) => HttpClient) | undefined
@@ -37,18 +84,75 @@ export declare namespace OpenAiConfig {
37
84
  }
38
85
 
39
86
  /**
40
- * @since 1.0.0
87
+ * Provides a scoped transform for the OpenAI HTTP client used by provider
88
+ * operations.
89
+ *
90
+ * **When to use**
91
+ *
92
+ * Use when a single effect or workflow needs temporary OpenAI HTTP client
93
+ * customization without rebuilding the client layer.
94
+ *
95
+ * **Details**
96
+ *
97
+ * Supports both data-first and data-last forms. The transform is stored in the
98
+ * scoped `OpenAiConfig` service and read by OpenAI provider operations while
99
+ * running the supplied effect.
100
+ *
101
+ * **Gotchas**
102
+ *
103
+ * If a transform is already present in the scoped config, this helper replaces
104
+ * it. Compose transforms manually when both should apply.
105
+ *
41
106
  * @category configuration
107
+ * @since 4.0.0
42
108
  */
43
109
  export const withClientTransform: {
44
110
  /**
45
- * @since 1.0.0
111
+ * Provides a scoped transform for the OpenAI HTTP client used by provider
112
+ * operations.
113
+ *
114
+ * **When to use**
115
+ *
116
+ * Use when a single effect or workflow needs temporary OpenAI HTTP client
117
+ * customization without rebuilding the client layer.
118
+ *
119
+ * **Details**
120
+ *
121
+ * Supports both data-first and data-last forms. The transform is stored in the
122
+ * scoped `OpenAiConfig` service and read by OpenAI provider operations while
123
+ * running the supplied effect.
124
+ *
125
+ * **Gotchas**
126
+ *
127
+ * If a transform is already present in the scoped config, this helper replaces
128
+ * it. Compose transforms manually when both should apply.
129
+ *
46
130
  * @category configuration
131
+ * @since 4.0.0
47
132
  */
48
133
  (transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
49
134
  /**
50
- * @since 1.0.0
135
+ * Provides a scoped transform for the OpenAI HTTP client used by provider
136
+ * operations.
137
+ *
138
+ * **When to use**
139
+ *
140
+ * Use when a single effect or workflow needs temporary OpenAI HTTP client
141
+ * customization without rebuilding the client layer.
142
+ *
143
+ * **Details**
144
+ *
145
+ * Supports both data-first and data-last forms. The transform is stored in the
146
+ * scoped `OpenAiConfig` service and read by OpenAI provider operations while
147
+ * running the supplied effect.
148
+ *
149
+ * **Gotchas**
150
+ *
151
+ * If a transform is already present in the scoped config, this helper replaces
152
+ * it. Compose transforms manually when both should apply.
153
+ *
51
154
  * @category configuration
155
+ * @since 4.0.0
52
156
  */
53
157
  <A, E, R>(
54
158
  self: Effect.Effect<A, E, R>,