@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 90 lines 3.52 KB
Raw
1 import * as AiError from "effect/ai/AiError"
2 import * as Clock from "effect/Clock"
3 import type * as Duration from "effect/Duration"
4 import * as Effect from "effect/Effect"
5 import * as Headers from "effect/http/Headers"
6 import type * as HttpClientError from "effect/http/HttpClientError"
7 import type * as HttpClientResponse from "effect/http/HttpClientResponse"
8 import * as Option from "effect/Option"
9 import * as Redactable from "effect/Redactable"
10 import * as Result from "effect/Result"
11 import * as Schema from "effect/Schema"
12
13 const ErrorBody = Schema.Struct({
14 errors: Schema.Array(Schema.Struct({ code: Schema.Number, message: Schema.String }))
15 })
16
17 const decodeErrorBody = Schema.decodeUnknownOption(ErrorBody)
18
19 // an invalid Retry-After is treated as absent
20 const retryAfter = (raw: string | undefined, now: number): Duration.Duration | undefined => {
21 if (raw === undefined) return undefined
22 const retryAfter = Headers.parseRetryAfter(raw)
23 return Result.isSuccess(retryAfter) ? Headers.retryAfterDelay(retryAfter.success, now) : undefined
24 }
25
26 const fail = (reason: AiError.AiErrorReason) =>
27 Effect.fail(AiError.make({ module: "CloudflareClient", method: "createDecisions", reason }))
28
29 export const mapResponseError = Effect.fnUntraced(
30 function*(response: HttpClientResponse.HttpClientResponse): Effect.fn.Return<never, AiError.AiError> {
31 const { request, status } = response
32 const body = yield* response.text.pipe(Effect.orElseSucceed(() => undefined))
33 const parsed = yield* Effect.try(() => JSON.parse(body ?? "")).pipe(Effect.orElseSucceed(() => undefined))
34 const details = Option.getOrUndefined(decodeErrorBody(parsed))
35 const http = {
36 response: { status, headers: Redactable.redact(response.headers) as Record<string, string> },
37 body
38 }
39 const description = AiError.buildErrorDescription({
40 status,
41 method: request.method,
42 url: request.url,
43 body,
44 message: details?.errors.map((error) => error.message).join("; "),
45 errorCode: details?.errors[0]?.code,
46 requestId: response.headers["cf-ray"]
47 })
48 if (status === 429) {
49 return yield* fail(
50 new AiError.RateLimitError({
51 retryAfter: retryAfter(response.headers["retry-after"], yield* Clock.currentTimeMillis),
52 http,
53 metadata: { cloudflare: { rayId: response.headers["cf-ray"] ?? null, errors: details?.errors ?? [] } }
54 })
55 )
56 }
57 if (status === 404 || status === 422) {
58 return yield* fail(new AiError.InvalidRequestError({ description, http }))
59 }
60 if (status >= 200 && status < 300) {
61 return yield* fail(new AiError.InternalProviderError({ description, http }))
62 }
63 return yield* fail(AiError.reasonFromHttpStatus({ status, description, http }))
64 }
65 )
66
67 export const mapHttpClientError = Effect.fnUntraced(
68 function*(error: HttpClientError.HttpClientError): Effect.fn.Return<never, AiError.AiError> {
69 const source = error.reason
70 switch (source._tag) {
71 case "TransportError":
72 case "EncodeError":
73 case "InvalidUrlError":
74 return yield* fail(
75 AiError.NetworkError.fromHttpClientError(
76 error as HttpClientError.HttpClientError & { readonly reason: typeof source }
77 )
78 )
79 case "DecodeError":
80 case "EmptyBodyError":
81 return yield* fail(
82 new AiError.InvalidOutputError({
83 description: source.description ?? "Failed to decode Cloudflare response"
84 })
85 )
86 case "StatusCodeError":
87 return yield* mapResponseError(source.response)
88 }
89 }
90 )