@effect/ai-openrouter 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.
@@ -1,35 +1,83 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * The `OpenRouterConfig` module provides scoped contextual configuration for
3
+ * OpenRouter request execution. It lets a workflow customize the HTTP client
4
+ * used by generated OpenRouter request methods without rebuilding the
5
+ * `OpenRouterClient` layer.
6
+ *
7
+ * **Mental model**
8
+ *
9
+ * - {@link OpenRouterConfig} is a context service carrying optional
10
+ * OpenRouter-specific request configuration
11
+ * - {@link withClientTransform} provides that service around one effect
12
+ * - Generated request methods read the current context and apply the transform
13
+ * to their `HttpClient`
14
+ * - The scoped configuration only applies while the returned effect is run
15
+ *
16
+ * **Common tasks**
17
+ *
18
+ * - Add request logging, retries, proxy routing, headers, or test doubles with
19
+ * {@link withClientTransform}
20
+ * - Scope OpenRouter client customization to one workflow without changing the
21
+ * shared client layer
22
+ *
23
+ * **Gotchas**
24
+ *
25
+ * - Each {@link withClientTransform} call replaces the current scoped
26
+ * transform for the supplied effect; compose transforms manually when both
27
+ * behaviors should apply
28
+ * - The transform receives and returns an `HttpClient`, so it should preserve
29
+ * the OpenRouter request contract while adding behavior around it
30
+ * - Streaming chat completion requests are sent directly by `OpenRouterClient`
31
+ * and do not read this scoped transform
32
+ *
33
+ * @since 4.0.0
3
34
  */
35
+ import * as Context from "effect/Context"
4
36
  import * as Effect from "effect/Effect"
5
37
  import { dual } from "effect/Function"
6
- import * as ServiceMap from "effect/ServiceMap"
7
38
  import type { HttpClient } from "effect/unstable/http/HttpClient"
8
39
 
9
40
  /**
10
- * @since 1.0.0
41
+ * Context service carrying scoped OpenRouter provider configuration for client
42
+ * operations.
43
+ *
44
+ * **When to use**
45
+ *
46
+ * Use as the context service tag when manually providing or reading scoped
47
+ * OpenRouter provider configuration in an Effect context.
48
+ *
49
+ * @see {@link withClientTransform} for scoping an HTTP client transformation
50
+ *
11
51
  * @category services
52
+ * @since 4.0.0
12
53
  */
13
- export class OpenRouterConfig extends ServiceMap.Service<
54
+ export class OpenRouterConfig extends Context.Service<
14
55
  OpenRouterConfig,
15
56
  OpenRouterConfig.Service
16
57
  >()("@effect/ai-openrouter/OpenRouterConfig") {
17
58
  /**
18
- * @since 1.0.0
59
+ * Gets the configured OpenRouter service from the current context when present.
60
+ *
61
+ * @since 4.0.0
19
62
  */
20
63
  static readonly getOrUndefined: Effect.Effect<typeof OpenRouterConfig.Service | undefined> = Effect.map(
21
- Effect.services<never>(),
64
+ Effect.context<never>(),
22
65
  (services) => services.mapUnsafe.get(OpenRouterConfig.key)
23
66
  )
24
67
  }
25
68
 
26
69
  /**
27
- * @since 1.0.0
70
+ * Types associated with the `OpenRouterConfig` context service.
71
+ *
72
+ * @since 4.0.0
28
73
  */
29
74
  export declare namespace OpenRouterConfig {
30
75
  /**
31
- * @since 1.0.0
76
+ * Configuration values read by OpenRouter provider operations when resolving
77
+ * the generated HTTP client.
78
+ *
32
79
  * @category models
80
+ * @since 4.0.0
33
81
  */
34
82
  export interface Service {
35
83
  readonly transformClient?: ((client: HttpClient) => HttpClient) | undefined
@@ -37,18 +85,81 @@ export declare namespace OpenRouterConfig {
37
85
  }
38
86
 
39
87
  /**
40
- * @since 1.0.0
88
+ * Provides a scoped transform for the OpenRouter HTTP client used by provider
89
+ * operations.
90
+ *
91
+ * **When to use**
92
+ *
93
+ * Use when a single effect or workflow needs temporary OpenRouter HTTP client
94
+ * customization without rebuilding the client layer.
95
+ *
96
+ * **Details**
97
+ *
98
+ * Supports both data-first and data-last forms. The transform is stored in the
99
+ * scoped `OpenRouterConfig` service and read by generated OpenRouter request
100
+ * operations while running the supplied effect.
101
+ *
102
+ * **Gotchas**
103
+ *
104
+ * If a transform is already present in the scoped config, this helper replaces
105
+ * it. Compose transforms manually when both should apply. Streaming chat
106
+ * completion requests are sent directly by `OpenRouterClient.make` and do not
107
+ * read this scoped transform.
108
+ *
41
109
  * @category configuration
110
+ * @since 4.0.0
42
111
  */
43
112
  export const withClientTransform: {
44
113
  /**
45
- * @since 1.0.0
114
+ * Provides a scoped transform for the OpenRouter HTTP client used by provider
115
+ * operations.
116
+ *
117
+ * **When to use**
118
+ *
119
+ * Use when a single effect or workflow needs temporary OpenRouter HTTP client
120
+ * customization without rebuilding the client layer.
121
+ *
122
+ * **Details**
123
+ *
124
+ * Supports both data-first and data-last forms. The transform is stored in the
125
+ * scoped `OpenRouterConfig` service and read by generated OpenRouter request
126
+ * operations while running the supplied effect.
127
+ *
128
+ * **Gotchas**
129
+ *
130
+ * If a transform is already present in the scoped config, this helper replaces
131
+ * it. Compose transforms manually when both should apply. Streaming chat
132
+ * completion requests are sent directly by `OpenRouterClient.make` and do not
133
+ * read this scoped transform.
134
+ *
46
135
  * @category configuration
136
+ * @since 4.0.0
47
137
  */
48
138
  (transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
49
139
  /**
50
- * @since 1.0.0
140
+ * Provides a scoped transform for the OpenRouter HTTP client used by provider
141
+ * operations.
142
+ *
143
+ * **When to use**
144
+ *
145
+ * Use when a single effect or workflow needs temporary OpenRouter HTTP client
146
+ * customization without rebuilding the client layer.
147
+ *
148
+ * **Details**
149
+ *
150
+ * Supports both data-first and data-last forms. The transform is stored in the
151
+ * scoped `OpenRouterConfig` service and read by generated OpenRouter request
152
+ * operations while running the supplied effect.
153
+ *
154
+ * **Gotchas**
155
+ *
156
+ * If a transform is already present in the scoped config, this helper replaces
157
+ * it. Compose transforms manually when both should apply. Streaming chat
158
+ * completion requests are sent directly by `OpenRouterClient.make` and do not
159
+ * read this scoped transform.
160
+ *
51
161
  * @category configuration
162
+ * @since 4.0.0
52
163
  */
53
164
  <A, E, R>(
54
165
  self: Effect.Effect<A, E, R>,
@@ -56,13 +167,55 @@ export const withClientTransform: {
56
167
  ): Effect.Effect<A, E, R>
57
168
  } = dual<
58
169
  /**
59
- * @since 1.0.0
170
+ * Provides a scoped transform for the OpenRouter HTTP client used by provider
171
+ * operations.
172
+ *
173
+ * **When to use**
174
+ *
175
+ * Use when a single effect or workflow needs temporary OpenRouter HTTP client
176
+ * customization without rebuilding the client layer.
177
+ *
178
+ * **Details**
179
+ *
180
+ * Supports both data-first and data-last forms. The transform is stored in the
181
+ * scoped `OpenRouterConfig` service and read by generated OpenRouter request
182
+ * operations while running the supplied effect.
183
+ *
184
+ * **Gotchas**
185
+ *
186
+ * If a transform is already present in the scoped config, this helper replaces
187
+ * it. Compose transforms manually when both should apply. Streaming chat
188
+ * completion requests are sent directly by `OpenRouterClient.make` and do not
189
+ * read this scoped transform.
190
+ *
60
191
  * @category configuration
192
+ * @since 4.0.0
61
193
  */
62
194
  (transform: (client: HttpClient) => HttpClient) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
63
195
  /**
64
- * @since 1.0.0
196
+ * Provides a scoped transform for the OpenRouter HTTP client used by provider
197
+ * operations.
198
+ *
199
+ * **When to use**
200
+ *
201
+ * Use when a single effect or workflow needs temporary OpenRouter HTTP client
202
+ * customization without rebuilding the client layer.
203
+ *
204
+ * **Details**
205
+ *
206
+ * Supports both data-first and data-last forms. The transform is stored in the
207
+ * scoped `OpenRouterConfig` service and read by generated OpenRouter request
208
+ * operations while running the supplied effect.
209
+ *
210
+ * **Gotchas**
211
+ *
212
+ * If a transform is already present in the scoped config, this helper replaces
213
+ * it. Compose transforms manually when both should apply. Streaming chat
214
+ * completion requests are sent directly by `OpenRouterClient.make` and do not
215
+ * read this scoped transform.
216
+ *
65
217
  * @category configuration
218
+ * @since 4.0.0
66
219
  */
67
220
  <A, E, R>(
68
221
  self: Effect.Effect<A, E, R>,
@@ -4,14 +4,14 @@
4
4
  * Provides OpenRouter-specific metadata fields for AI error types through
5
5
  * module augmentation, enabling typed access to OpenRouter error details.
6
6
  *
7
- * @since 1.0.0
7
+ * @since 4.0.0
8
8
  */
9
9
 
10
10
  /**
11
11
  * OpenRouter-specific error metadata fields.
12
12
  *
13
- * @since 1.0.0
14
13
  * @category models
14
+ * @since 4.0.0
15
15
  */
16
16
  export type OpenRouterErrorMetadata = {
17
17
  /**
@@ -31,8 +31,8 @@ export type OpenRouterErrorMetadata = {
31
31
  /**
32
32
  * OpenRouter-specific rate limit metadata fields.
33
33
  *
34
- * @since 1.0.0
35
34
  * @category models
35
+ * @since 4.0.0
36
36
  */
37
37
  export type OpenRouterRateLimitMetadata = OpenRouterErrorMetadata & {
38
38
  readonly limit: string | null
@@ -42,51 +42,184 @@ export type OpenRouterRateLimitMetadata = OpenRouterErrorMetadata & {
42
42
  }
43
43
 
44
44
  declare module "effect/unstable/ai/AiError" {
45
- export interface RateLimitError {
46
- readonly metadata: {
47
- readonly openrouter?: OpenRouterRateLimitMetadata | null
48
- }
45
+ /**
46
+ * OpenRouter metadata attached to `RateLimitError` values.
47
+ *
48
+ * **Details**
49
+ *
50
+ * Captures OpenRouter error details together with rate limit header
51
+ * information from responses where the provider rejected the request because
52
+ * a limit was reached.
53
+ *
54
+ * @category configuration
55
+ * @since 4.0.0
56
+ */
57
+ export interface RateLimitErrorMetadata {
58
+ /**
59
+ * OpenRouter-specific details for the rate limit response.
60
+ */
61
+ readonly openrouter?: OpenRouterRateLimitMetadata | null
62
+ }
63
+
64
+ /**
65
+ * OpenRouter metadata attached to `QuotaExhaustedError` values.
66
+ *
67
+ * **Details**
68
+ *
69
+ * Preserves provider error details for failures caused by exhausted account,
70
+ * billing, or usage quota.
71
+ *
72
+ * @category configuration
73
+ * @since 4.0.0
74
+ */
75
+ export interface QuotaExhaustedErrorMetadata {
76
+ /**
77
+ * OpenRouter-specific details for the quota exhaustion response.
78
+ */
79
+ readonly openrouter?: OpenRouterErrorMetadata | null
49
80
  }
50
81
 
51
- export interface QuotaExhaustedError {
52
- readonly metadata: {
53
- readonly openrouter?: OpenRouterErrorMetadata | null
54
- }
82
+ /**
83
+ * OpenRouter metadata attached to `AuthenticationError` values.
84
+ *
85
+ * **Details**
86
+ *
87
+ * Preserves provider error details for failed API key, authorization, or
88
+ * permission checks.
89
+ *
90
+ * @category configuration
91
+ * @since 4.0.0
92
+ */
93
+ export interface AuthenticationErrorMetadata {
94
+ /**
95
+ * OpenRouter-specific details for the authentication failure.
96
+ */
97
+ readonly openrouter?: OpenRouterErrorMetadata | null
98
+ }
99
+
100
+ /**
101
+ * OpenRouter metadata attached to `ContentPolicyError` values.
102
+ *
103
+ * **Details**
104
+ *
105
+ * Preserves provider error details when OpenRouter rejects input or output
106
+ * because it violates a content policy.
107
+ *
108
+ * @category configuration
109
+ * @since 4.0.0
110
+ */
111
+ export interface ContentPolicyErrorMetadata {
112
+ /**
113
+ * OpenRouter-specific details for the content policy response.
114
+ */
115
+ readonly openrouter?: OpenRouterErrorMetadata | null
55
116
  }
56
117
 
57
- export interface AuthenticationError {
58
- readonly metadata: {
59
- readonly openrouter?: OpenRouterErrorMetadata | null
60
- }
118
+ /**
119
+ * OpenRouter metadata attached to `InvalidRequestError` values.
120
+ *
121
+ * **Details**
122
+ *
123
+ * Preserves provider error details for malformed requests, unsupported
124
+ * parameters, or other request validation failures reported by OpenRouter.
125
+ *
126
+ * @category configuration
127
+ * @since 4.0.0
128
+ */
129
+ export interface InvalidRequestErrorMetadata {
130
+ /**
131
+ * OpenRouter-specific details for the invalid request response.
132
+ */
133
+ readonly openrouter?: OpenRouterErrorMetadata | null
61
134
  }
62
135
 
63
- export interface ContentPolicyError {
64
- readonly metadata: {
65
- readonly openrouter?: OpenRouterErrorMetadata | null
66
- }
136
+ /**
137
+ * OpenRouter metadata attached to `InternalProviderError` values.
138
+ *
139
+ * **Details**
140
+ *
141
+ * Preserves provider error details for OpenRouter-side failures such as
142
+ * transient server errors or overload responses.
143
+ *
144
+ * @category configuration
145
+ * @since 4.0.0
146
+ */
147
+ export interface InternalProviderErrorMetadata {
148
+ /**
149
+ * OpenRouter-specific details for the internal provider response.
150
+ */
151
+ readonly openrouter?: OpenRouterErrorMetadata | null
67
152
  }
68
153
 
69
- export interface InvalidRequestError {
70
- readonly metadata: {
71
- readonly openrouter?: OpenRouterErrorMetadata | null
72
- }
154
+ /**
155
+ * OpenRouter metadata attached to `InvalidOutputError` values.
156
+ *
157
+ * **Details**
158
+ *
159
+ * Preserves provider error details when an OpenRouter response cannot be
160
+ * parsed or validated as the expected output.
161
+ *
162
+ * @category configuration
163
+ * @since 4.0.0
164
+ */
165
+ export interface InvalidOutputErrorMetadata {
166
+ /**
167
+ * OpenRouter-specific details for the invalid output response.
168
+ */
169
+ readonly openrouter?: OpenRouterErrorMetadata | null
73
170
  }
74
171
 
75
- export interface InternalProviderError {
76
- readonly metadata: {
77
- readonly openrouter?: OpenRouterErrorMetadata | null
78
- }
172
+ /**
173
+ * OpenRouter metadata attached to `StructuredOutputError` values.
174
+ *
175
+ * **Details**
176
+ *
177
+ * Preserves provider error details when OpenRouter returns content that does
178
+ * not satisfy the requested structured output schema.
179
+ *
180
+ * @category configuration
181
+ * @since 4.0.0
182
+ */
183
+ export interface StructuredOutputErrorMetadata {
184
+ /**
185
+ * OpenRouter-specific details for the structured output failure.
186
+ */
187
+ readonly openrouter?: OpenRouterErrorMetadata | null
79
188
  }
80
189
 
81
- export interface InvalidOutputError {
82
- readonly metadata: {
83
- readonly openrouter?: OpenRouterErrorMetadata | null
84
- }
190
+ /**
191
+ * OpenRouter metadata attached to `UnsupportedSchemaError` values.
192
+ *
193
+ * **Details**
194
+ *
195
+ * Preserves provider error details when an unsupported schema failure is
196
+ * associated with an OpenRouter response.
197
+ *
198
+ * @category configuration
199
+ * @since 4.0.0
200
+ */
201
+ export interface UnsupportedSchemaErrorMetadata {
202
+ /**
203
+ * OpenRouter-specific details for the unsupported schema failure.
204
+ */
205
+ readonly openrouter?: OpenRouterErrorMetadata | null
85
206
  }
86
207
 
87
- export interface UnknownError {
88
- readonly metadata: {
89
- readonly openrouter?: OpenRouterErrorMetadata | null
90
- }
208
+ /**
209
+ * OpenRouter metadata attached to `UnknownError` values.
210
+ *
211
+ * **Details**
212
+ *
213
+ * Preserves provider error details for OpenRouter failures that do not map
214
+ * cleanly to a more specific AI error category.
215
+ *
216
+ * @category configuration
217
+ * @since 4.0.0
218
+ */
219
+ export interface UnknownErrorMetadata {
220
+ /**
221
+ * OpenRouter-specific details for the unclassified provider failure.
222
+ */
223
+ readonly openrouter?: OpenRouterErrorMetadata | null
91
224
  }
92
225
  }