@effect/ai-typesafe 4.0.5 → 4.0.7
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.
- package/ai-docs/package.json +1 -1
- package/ai-docs/src/06_schedule/10_schedules.ts +5 -4
- package/ai-docs/tsconfig.json +3 -1
- package/dist/internal/errors.d.ts.map +1 -1
- package/dist/internal/errors.js +11 -8
- package/dist/internal/errors.js.map +1 -1
- package/package.json +3 -3
- package/src/internal/errors.ts +11 -8
package/ai-docs/package.json
CHANGED
|
@@ -5,14 +5,15 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { Duration, Effect, Random, Schedule, Schema } from "effect"
|
|
7
7
|
|
|
8
|
-
// Production pattern: capped exponential backoff with jitter and
|
|
9
|
-
//
|
|
8
|
+
// Production pattern: capped exponential backoff with jitter and at most 5 retries (6 attempts).
|
|
9
|
+
// Base delays start at 250ms and grow exponentially, capped at 10s before jitter.
|
|
10
10
|
export const productionRetrySchedule = Schedule.min([
|
|
11
11
|
Schedule.exponential("250 millis"),
|
|
12
12
|
// Cap the delay at 10 seconds to avoid excessively long waits.
|
|
13
13
|
Schedule.spaced("10 seconds")
|
|
14
14
|
]).pipe(
|
|
15
15
|
Schedule.jittered,
|
|
16
|
+
Schedule.upTo({ times: 5 }),
|
|
16
17
|
Schedule.setInputType<HttpError>(),
|
|
17
18
|
Schedule.while(({ input }) => input.retryable)
|
|
18
19
|
)
|
|
@@ -44,8 +45,8 @@ export const fetchUserProfile = Effect.fn("fetchUserProfile")(
|
|
|
44
45
|
// Use the schedule with `Effect.retry` to retry failures.
|
|
45
46
|
export const loadUserWithRetry = fetchUserProfile("user-123").pipe(
|
|
46
47
|
Effect.retry(productionRetrySchedule),
|
|
47
|
-
//
|
|
48
|
-
//
|
|
48
|
+
// Turn the final error into a defect, either after exhausting the retries
|
|
49
|
+
// or immediately for a non-retryable error.
|
|
49
50
|
Effect.orDie
|
|
50
51
|
)
|
|
51
52
|
|
package/ai-docs/tsconfig.json
CHANGED
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
"plugins": [
|
|
14
14
|
{
|
|
15
15
|
"name": "@effect/language-service",
|
|
16
|
+
"effectFn": ["untraced"],
|
|
16
17
|
"namespaceImportPackages": [],
|
|
17
18
|
"diagnosticSeverity": {
|
|
18
19
|
"floatingEffect": "off",
|
|
19
20
|
"preferSchemaOverJson": "off",
|
|
20
|
-
"unstableApiUsage": "off"
|
|
21
|
+
"unstableApiUsage": "off",
|
|
22
|
+
"effectFnOpportunity": "error"
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/internal/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/internal/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,mBAAmB,CAAA;AAG5C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,eAAe,MAAM,6BAA6B,CAAA;AAMnE,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAmCvC,eAAO,MAAM,cAAc,UAAW,MAAM,CAAC,WAAW,UAAU,MAAM,KAAG,OAAO,CAAC,OAK/E,CAAA;AAEJ,eAAO,MAAM,kBAAkB,0GA8D9B,CAAA"}
|
package/dist/internal/errors.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import * as AiError from "effect/ai/AiError";
|
|
2
|
+
import * as Clock from "effect/Clock";
|
|
2
3
|
import * as Duration from "effect/Duration";
|
|
3
4
|
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Headers from "effect/http/Headers";
|
|
4
6
|
import * as Num from "effect/Number";
|
|
5
7
|
import * as Option from "effect/Option";
|
|
6
8
|
import * as Redactable from "effect/Redactable";
|
|
9
|
+
import * as Result from "effect/Result";
|
|
7
10
|
import * as Schema from "effect/Schema";
|
|
8
11
|
const requestDetails = request => ({
|
|
9
12
|
method: request.method,
|
|
@@ -23,20 +26,20 @@ const nonNegative = value => {
|
|
|
23
26
|
const parsed = Option.getOrUndefined(Num.parse(value));
|
|
24
27
|
return parsed !== undefined && Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
25
28
|
};
|
|
26
|
-
|
|
29
|
+
// `retry-after-ms` is TypeSafe-specific and wins over the standard header;
|
|
30
|
+
// an invalid Retry-After is treated as absent
|
|
31
|
+
const retryAfter = (headers, now) => {
|
|
27
32
|
const ms = nonNegative(headers["retry-after-ms"]);
|
|
28
33
|
if (ms !== undefined) return Duration.millis(ms);
|
|
29
34
|
const raw = headers["retry-after"];
|
|
30
|
-
if (raw === undefined
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
const date = Date.parse(raw);
|
|
34
|
-
return Number.isNaN(date) ? undefined : Duration.millis(Math.max(0, date - Date.now()));
|
|
35
|
+
if (raw === undefined) return undefined;
|
|
36
|
+
const retryAfter = Headers.parseRetryAfter(raw);
|
|
37
|
+
return Result.isSuccess(retryAfter) ? Headers.retryAfterDelay(retryAfter.success, now) : undefined;
|
|
35
38
|
};
|
|
36
39
|
export const mapSchemaError = (error, method) => AiError.make({
|
|
37
40
|
module: "TypeSafeClient",
|
|
38
41
|
method,
|
|
39
|
-
reason: AiError.
|
|
42
|
+
reason: AiError.InvalidResponseError.fromSchemaError(error)
|
|
40
43
|
});
|
|
41
44
|
export const mapHttpClientError = /*#__PURE__*/Effect.fnUntraced(function* (error, method) {
|
|
42
45
|
const source = error.reason;
|
|
@@ -86,7 +89,7 @@ export const mapHttpClientError = /*#__PURE__*/Effect.fnUntraced(function* (erro
|
|
|
86
89
|
});
|
|
87
90
|
if (response.status === 429) {
|
|
88
91
|
reason = new AiError.RateLimitError({
|
|
89
|
-
retryAfter: retryAfter(response.headers),
|
|
92
|
+
retryAfter: retryAfter(response.headers, yield* Clock.currentTimeMillis),
|
|
90
93
|
http,
|
|
91
94
|
metadata: {
|
|
92
95
|
typesafe: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","names":[],"sources":["../../src/internal/errors.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,mBAAmB;AAC5C,OAAO,KAAK,QAAQ,MAAM,iBAAiB;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe;
|
|
1
|
+
{"version":3,"file":"errors.js","names":[],"sources":["../../src/internal/errors.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,mBAAmB;AAC5C,OAAO,KAAK,KAAK,MAAM,cAAc;AACrC,OAAO,KAAK,QAAQ,MAAM,iBAAiB;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe;AACvC,OAAO,KAAK,OAAO,MAAM,qBAAqB;AAG9C,OAAO,KAAK,GAAG,MAAM,eAAe;AACpC,OAAO,KAAK,MAAM,MAAM,eAAe;AACvC,OAAO,KAAK,UAAU,MAAM,mBAAmB;AAC/C,OAAO,KAAK,MAAM,MAAM,eAAe;AACvC,OAAO,KAAK,MAAM,MAAM,eAAe;AAEvC,MAAM,cAAc,GAAI,OAA4C,KAAM;EACxE,MAAM,EAAE,OAAO,CAAC,MAAM;EACtB,GAAG,EAAE,OAAO,CAAC,GAAG;EAChB,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;EACxC,IAAI,EAAE,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC;EACzC,OAAO,EAAE,UAAU,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO;CAC3C,CAAC;AAEF,MAAM,SAAS,gBAAG,MAAM,CAAC,MAAM,CAAC;EAC9B,OAAO,eAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC;EACvC,IAAI,eAAE,MAAM,CAAC,QAAQ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}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/ai-typesafe",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A TypeSafe DecisionModel provider for Effect",
|
|
@@ -47,10 +47,10 @@
|
|
|
47
47
|
"provenance": true
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"effect": "^4.0.
|
|
50
|
+
"effect": "^4.0.7"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"effect": "^4.0.
|
|
53
|
+
"effect": "^4.0.7"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"codegen": "effect-utils codegen",
|
package/src/internal/errors.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import * as AiError from "effect/ai/AiError"
|
|
2
|
+
import * as Clock from "effect/Clock"
|
|
2
3
|
import * as Duration from "effect/Duration"
|
|
3
4
|
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Headers from "effect/http/Headers"
|
|
4
6
|
import type * as HttpClientError from "effect/http/HttpClientError"
|
|
5
7
|
import type * as HttpClientRequest from "effect/http/HttpClientRequest"
|
|
6
8
|
import * as Num from "effect/Number"
|
|
7
9
|
import * as Option from "effect/Option"
|
|
8
10
|
import * as Redactable from "effect/Redactable"
|
|
11
|
+
import * as Result from "effect/Result"
|
|
9
12
|
import * as Schema from "effect/Schema"
|
|
10
13
|
|
|
11
14
|
const requestDetails = (request: HttpClientRequest.HttpClientRequest) => ({
|
|
@@ -30,22 +33,22 @@ const nonNegative = (value: string | undefined): number | undefined => {
|
|
|
30
33
|
return parsed !== undefined && Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined
|
|
31
34
|
}
|
|
32
35
|
|
|
33
|
-
|
|
36
|
+
// `retry-after-ms` is TypeSafe-specific and wins over the standard header;
|
|
37
|
+
// an invalid Retry-After is treated as absent
|
|
38
|
+
const retryAfter = (headers: Readonly<Record<string, string>>, now: number) => {
|
|
34
39
|
const ms = nonNegative(headers["retry-after-ms"])
|
|
35
40
|
if (ms !== undefined) return Duration.millis(ms)
|
|
36
41
|
const raw = headers["retry-after"]
|
|
37
|
-
if (raw === undefined
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
const date = Date.parse(raw)
|
|
41
|
-
return Number.isNaN(date) ? undefined : Duration.millis(Math.max(0, date - Date.now()))
|
|
42
|
+
if (raw === undefined) return undefined
|
|
43
|
+
const retryAfter = Headers.parseRetryAfter(raw)
|
|
44
|
+
return Result.isSuccess(retryAfter) ? Headers.retryAfterDelay(retryAfter.success, now) : undefined
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
export const mapSchemaError = (error: Schema.SchemaError, method: string): AiError.AiError =>
|
|
45
48
|
AiError.make({
|
|
46
49
|
module: "TypeSafeClient",
|
|
47
50
|
method,
|
|
48
|
-
reason: AiError.
|
|
51
|
+
reason: AiError.InvalidResponseError.fromSchemaError(error)
|
|
49
52
|
})
|
|
50
53
|
|
|
51
54
|
export const mapHttpClientError = Effect.fnUntraced(
|
|
@@ -90,7 +93,7 @@ export const mapHttpClientError = Effect.fnUntraced(
|
|
|
90
93
|
})
|
|
91
94
|
if (response.status === 429) {
|
|
92
95
|
reason = new AiError.RateLimitError({
|
|
93
|
-
retryAfter: retryAfter(response.headers),
|
|
96
|
+
retryAfter: retryAfter(response.headers, yield* Clock.currentTimeMillis),
|
|
94
97
|
http,
|
|
95
98
|
metadata: {
|
|
96
99
|
typesafe: {
|