@fungi.computer/darkroom 0.11.0 → 0.11.2
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/dist/cloudflare/provider-reason.d.ts +8 -0
- package/dist/cloudflare/provider-reason.js +72 -0
- package/dist/cloudflare/provider.js +17 -5
- package/dist/daily-media-meter.d.ts +5 -0
- package/dist/daily-media-meter.js +29 -9
- package/dist/media-operation-owner.js +5 -0
- package/dist/server.js +6 -1
- package/package.json +2 -2
|
@@ -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
|
|
132
|
-
|
|
133
|
-
|
|
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
|
|
146
|
-
safetyFilterMessage.safeParse(cause.message).success
|
|
158
|
+
return safetyRefused
|
|
147
159
|
? new MediaSafetyFilterRefusal({ cause, provider })
|
|
148
160
|
: new MediaExecutionError("provider", { cause, provider });
|
|
149
161
|
}
|
|
@@ -51,5 +51,10 @@ export declare class DailyMediaMeter {
|
|
|
51
51
|
delivered: boolean;
|
|
52
52
|
}>): void;
|
|
53
53
|
}
|
|
54
|
+
/** Which daily-meter RPC failed; its original cause stays private. */
|
|
55
|
+
export declare class DailyMediaMeterError extends Error {
|
|
56
|
+
readonly operation: "admit" | "settle";
|
|
57
|
+
constructor(operation: "admit" | "settle", options: ErrorOptions);
|
|
58
|
+
}
|
|
54
59
|
/** `MediaMetering` that enforces one exact daily limit through the owning object. */
|
|
55
60
|
export declare function createDailyMediaMetering(meter: DailyMediaMeterPort, limitMilliunits: number): MediaMetering;
|
|
@@ -58,24 +58,44 @@ export class DailyMediaMeter {
|
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
/** Which daily-meter RPC failed; its original cause stays private. */
|
|
62
|
+
export class DailyMediaMeterError extends Error {
|
|
63
|
+
operation;
|
|
64
|
+
constructor(operation, options) {
|
|
65
|
+
super(`Daily media meter ${operation} failed`, options);
|
|
66
|
+
this.operation = operation;
|
|
67
|
+
this.name = "DailyMediaMeterError";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
61
70
|
/** `MediaMetering` that enforces one exact daily limit through the owning object. */
|
|
62
71
|
export function createDailyMediaMetering(meter, limitMilliunits) {
|
|
63
72
|
return {
|
|
64
73
|
admit: async (input) => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
let admitted;
|
|
75
|
+
try {
|
|
76
|
+
admitted = await meter.dailyAdmit({
|
|
77
|
+
operationId: input.operationId,
|
|
78
|
+
costMilliunits: input.providerCostMilliunits,
|
|
79
|
+
limitMilliunits,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (cause) {
|
|
83
|
+
throw new DailyMediaMeterError("admit", { cause });
|
|
84
|
+
}
|
|
70
85
|
if (!admitted)
|
|
71
86
|
throw new MediaBillingRefusal("daily_limit_reached");
|
|
72
87
|
return { priceMilliunits: input.providerCostMilliunits };
|
|
73
88
|
},
|
|
74
89
|
settle: async (input) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
try {
|
|
91
|
+
await meter.dailySettle({
|
|
92
|
+
operationId: input.operationId,
|
|
93
|
+
delivered: input.units > 0,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (cause) {
|
|
97
|
+
throw new DailyMediaMeterError("settle", { cause });
|
|
98
|
+
}
|
|
79
99
|
},
|
|
80
100
|
};
|
|
81
101
|
}
|
|
@@ -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/dist/server.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DailyMediaMeterError } from "./daily-media-meter.js";
|
|
1
2
|
import { mediaPendingSchema } from "./media-binding.js";
|
|
2
3
|
export { estimateMediaModel } from "./cloudflare/provider.js";
|
|
3
4
|
export { mediaPendingSchema, mediaBindingCallSchema, mediaBindingGenerationSchema, mediaImageBindingInputSchema, mediaVideoBindingInputSchema, parseMediaBindingCall, mediaBindingRequest, readMediaBindingReply, } from "./media-binding.js";
|
|
@@ -125,7 +126,11 @@ function failure(error, refusalMessages) {
|
|
|
125
126
|
"destination-authorization": "Media destination permission could not be checked; try again.",
|
|
126
127
|
"destination-write": "Media generated an image but could not save it; retry this request to save the retained result.",
|
|
127
128
|
estimate: "Media could not prepare the image inputs or estimate their cost; check the reference files and retry.",
|
|
128
|
-
billing:
|
|
129
|
+
billing: error.cause instanceof DailyMediaMeterError
|
|
130
|
+
? error.cause.operation === "admit"
|
|
131
|
+
? "Couldn't check today's spending limit. Try again."
|
|
132
|
+
: "Couldn't record this generation's spending. Retry this request."
|
|
133
|
+
: "Media billing is unavailable; retry this request.",
|
|
129
134
|
normalization: "Media provider returned an unusable image; start a new request only after checking provider status.",
|
|
130
135
|
artifact: "Media generated output could not be retained or read; try again.",
|
|
131
136
|
operation: "Media operation is unresolved; try again.",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fungi.computer/darkroom",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
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.
|
|
63
|
+
"@fungi.computer/agar": "0.18.2",
|
|
64
64
|
"@fungi.computer/capnweb": "0.12.0-fungi.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|