@fungi.computer/darkroom 0.11.0 → 0.11.1

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.
@@ -0,0 +1,8 @@
1
+ import { type MediaProviderReason } from "@fungi.computer/agar/errors";
2
+ /** The reason a provider failure is classified under, from its documented code or wording. */
3
+ export declare function providerReason(name: string | undefined, message: string | undefined): MediaProviderReason | undefined;
4
+ /**
5
+ * The provider's own short sentence, when it is plain words. For 7003 it is the
6
+ * <REASON> after "Model execution failed (User Input Error): ".
7
+ */
8
+ export declare function providerDetail(message: string | undefined): string | undefined;
@@ -0,0 +1,72 @@
1
+ import { z } from "zod";
2
+ import { mediaProviderDetailMax, } from "@fungi.computer/agar/errors";
3
+ /**
4
+ * Documented numeric codes, by failure class.
5
+ * Workers AI: https://developers.cloudflare.com/workers-ai/platform/errors/
6
+ * AI Gateway: https://developers.cloudflare.com/ai-gateway/reference/troubleshooting/
7
+ * 2021 is also observed (receipts/prints-e2e: "AiGatewayError: 2021: Insufficient
8
+ * AI Gateway credits"). Codes the docs do not state stay unclassified.
9
+ */
10
+ const reasonByCode = new Map([
11
+ // Gateway or provider rejected the request as invalid ("7003: Model execution
12
+ // failed (User Input Error): <REASON>"), or the body/size/base64 was unusable.
13
+ [7003, "invalid_input"],
14
+ [3003, "invalid_input"],
15
+ [3006, "invalid_input"],
16
+ [5004, "invalid_input"],
17
+ // Request rate or Workers AI capacity.
18
+ [2018, "rate_limited"],
19
+ [3040, "rate_limited"],
20
+ // Prepaid gateway balance.
21
+ [2021, "credits_exhausted"],
22
+ [2052, "credits_exhausted"],
23
+ // Daily allocation used up.
24
+ [3036, "spending_limit"],
25
+ // Timeout.
26
+ [3007, "timeout"],
27
+ // Provider or gateway could not answer.
28
+ [7005, "provider_unavailable"],
29
+ [2002, "provider_unavailable"],
30
+ [2005, "provider_unavailable"],
31
+ ]);
32
+ // "<code>: <rest>", optionally led by the gateway's own "AiGatewayError: ".
33
+ const providerMessage = z
34
+ .string()
35
+ .regex(/^(?:Ai(?:Gateway)?Error: )*\d{4,5}: /u)
36
+ .transform((message) => {
37
+ // The regex above guarantees "<code>: " follows the optional prefixes.
38
+ const body = message.replace(/^(?:Ai(?:Gateway)?Error: )*/u, "");
39
+ const cut = body.indexOf(": ");
40
+ return { code: Number(body.slice(0, cut)), rest: body.slice(cut + 2) };
41
+ });
42
+ // Never print keys, URLs, ids or structured bodies: plain words only.
43
+ const safeSentence = z
44
+ .string()
45
+ .trim()
46
+ .min(1)
47
+ .max(mediaProviderDetailMax)
48
+ .regex(/^[\p{L}\p{N} .,'’:;()-]+$/u)
49
+ .refine((text) => !/[A-Za-z0-9_-]{20,}/u.test(text))
50
+ .refine((text) => !/key|token|secret|bearer|password|authorization/iu.test(text));
51
+ /** The reason a provider failure is classified under, from its documented code or wording. */
52
+ export function providerReason(name, message) {
53
+ if (name === "TimeoutError")
54
+ return "timeout";
55
+ const parsed = providerMessage.safeParse(message);
56
+ if (!parsed.success)
57
+ return undefined;
58
+ const byCode = reasonByCode.get(parsed.data.code);
59
+ return byCode;
60
+ }
61
+ /**
62
+ * The provider's own short sentence, when it is plain words. For 7003 it is the
63
+ * <REASON> after "Model execution failed (User Input Error): ".
64
+ */
65
+ export function providerDetail(message) {
66
+ const parsed = providerMessage.safeParse(message);
67
+ if (!parsed.success)
68
+ return undefined;
69
+ const reason = /^Model execution failed \(User Input Error\): ([\s\S]*)$/u.exec(parsed.data.rest);
70
+ const sentence = safeSentence.safeParse((reason?.[1] ?? parsed.data.rest).replace(/^(?:AiError: )+/u, ""));
71
+ return sentence.success ? sentence.data : undefined;
72
+ }
@@ -8,6 +8,7 @@ import { GATEWAY_VIDEO_MAX_BYTES, gatewayOutputSchema, isMp4, } from "../gateway
8
8
  import { videoProviderAdapters } from "../video-providers/index.js";
9
9
  import { decodeElevenLabsResultUrl, ELEVENLABS_MAX_BYTES, } from "../audio-providers/elevenlabs.js";
10
10
  import { readResponseBytes } from "../index.js";
11
+ import { providerDetail, providerReason } from "./provider-reason.js";
11
12
  /** Quote the catalog defaults through the exact estimator used before dispatch. */
12
13
  export function estimateMediaModel(model) {
13
14
  const checked = validateMediaGenerationSettings(model.kind === "audio"
@@ -128,9 +129,21 @@ function providerFailure(model, cause) {
128
129
  // call at 2026-10-27T01:01:04.296Z). The error had only name and message;
129
130
  // no HTTP status was recorded. This is observed wording, not a general
130
131
  // Cloudflare code guarantee. Keep numeric classification at this owner.
131
- const provider = code === 2021
132
- ? { category, code, reason: "credits_exhausted" }
133
- : { category, code };
132
+ const message = cause instanceof Error ? cause.message : undefined;
133
+ const safetyRefused = cause instanceof Error && safetyFilterMessage.safeParse(message).success;
134
+ const reason = safetyRefused
135
+ ? "content_refused"
136
+ : providerReason(cause instanceof Error ? cause.name : undefined, message);
137
+ // Plain provider sentences help only where the class alone does not explain.
138
+ const detail = reason === undefined || reason === "invalid_input"
139
+ ? providerDetail(message)
140
+ : undefined;
141
+ const provider = {
142
+ category,
143
+ code,
144
+ ...(reason !== undefined && { reason }),
145
+ ...(detail !== undefined && { detail }),
146
+ };
134
147
  const safeMessage = outputFlaggedMessage.safeParse(cause instanceof Error ? cause.message : undefined);
135
148
  // The maintained Workers AI errors expose name/message, not a Response or
136
149
  // cause chain. Only the complete, bounded refusal grammar above is safe to
@@ -142,8 +155,7 @@ function providerFailure(model, cause) {
142
155
  category: provider.category,
143
156
  providerMessage: safeMessage.success ? safeMessage.data : undefined,
144
157
  }, Redacted.make(cause));
145
- return cause instanceof Error &&
146
- safetyFilterMessage.safeParse(cause.message).success
158
+ return safetyRefused
147
159
  ? new MediaSafetyFilterRefusal({ cause, provider })
148
160
  : new MediaExecutionError("provider", { cause, provider });
149
161
  }
@@ -951,6 +951,11 @@ export class MediaOperationOwner {
951
951
  timer = setTimeout(() => {
952
952
  const timeout = new MediaExecutionError("provider", {
953
953
  cause: new Error("Background media provider timed out"),
954
+ provider: {
955
+ category: "TimeoutError",
956
+ code: null,
957
+ reason: "timeout",
958
+ },
954
959
  });
955
960
  controller.abort(timeout);
956
961
  reject(timeout);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/darkroom",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Media generation, durable output retention and provenance with host-owned authority and billing",
6
6
  "type": "module",
@@ -60,7 +60,7 @@
60
60
  "exifreader": "4.46.0",
61
61
  "@fungi.computer/ts-webp": "0.1.6",
62
62
  "@xmldom/xmldom": "0.9.12",
63
- "@fungi.computer/agar": "0.18.0",
63
+ "@fungi.computer/agar": "0.18.1",
64
64
  "@fungi.computer/capnweb": "0.12.0-fungi.0"
65
65
  },
66
66
  "devDependencies": {