dsh-cliproxyapi-pro 1.9.0 → 1.9.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.
Files changed (81) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.en.md +23 -407
  3. package/README.md +23 -407
  4. package/lib/client.js +46 -20
  5. package/lib/host/accounts.js +176 -0
  6. package/lib/host/apply/agent-tool.js +406 -0
  7. package/lib/host/apply/commands.js +122 -0
  8. package/lib/host/apply/controller.js +105 -0
  9. package/lib/host/apply/http-routes.js +273 -0
  10. package/lib/host/apply/image-jobs.js +171 -0
  11. package/lib/host/apply/index.js +41 -0
  12. package/lib/host/apply/interpret.js +72 -0
  13. package/lib/host/apply/lifecycle.js +34 -0
  14. package/lib/host/apply/media-failures.js +68 -0
  15. package/lib/host/apply/media-service.js +202 -0
  16. package/lib/host/apply/quota-service.js +42 -0
  17. package/lib/host/apply/references.js +43 -0
  18. package/lib/host/apply/session-status.js +104 -0
  19. package/lib/host/apply/state.js +78 -0
  20. package/lib/host/apply/updates.js +55 -0
  21. package/lib/host/apply/video-command.js +152 -0
  22. package/lib/host/apply/video-edit.js +253 -0
  23. package/lib/host/apply/video-jobs.js +206 -0
  24. package/lib/host/apply-utils.js +39 -0
  25. package/lib/host/config.js +205 -0
  26. package/lib/host/controller.js +435 -0
  27. package/lib/host/discovery.js +295 -0
  28. package/lib/host/http-gate.js +144 -0
  29. package/lib/host/models.js +102 -0
  30. package/lib/host/mount-guard.js +56 -0
  31. package/lib/host/routes.js +165 -0
  32. package/lib/host/runtime.js +445 -0
  33. package/lib/host/state-store.js +39 -0
  34. package/lib/host/status-text.js +93 -0
  35. package/lib/index.js +17 -4417
  36. package/lib/media/agent-tool.js +255 -0
  37. package/lib/media/choice.js +83 -0
  38. package/lib/media/classify.js +65 -0
  39. package/lib/media/codec/fit.js +76 -0
  40. package/lib/media/codec/jpeg.js +158 -0
  41. package/lib/media/codec/png.js +102 -0
  42. package/lib/media/commands/parsing.js +269 -0
  43. package/lib/media/constants.js +46 -0
  44. package/lib/media/diagnose.js +134 -0
  45. package/lib/media/images.js +165 -0
  46. package/lib/media/index.js +29 -0
  47. package/lib/media/interpret.js +126 -0
  48. package/lib/media/job-store.js +99 -0
  49. package/lib/media/mp4.js +44 -0
  50. package/lib/media/output-files.js +59 -0
  51. package/lib/media/session-store.js +293 -0
  52. package/lib/media/settings.js +255 -0
  53. package/lib/media/util.js +45 -0
  54. package/lib/media/video.js +326 -0
  55. package/lib/quota/auth-state.js +33 -0
  56. package/lib/quota/constants.js +86 -0
  57. package/lib/quota/controller.js +467 -0
  58. package/lib/quota/helpers.js +325 -0
  59. package/lib/quota/index.js +19 -0
  60. package/lib/quota/management.js +216 -0
  61. package/lib/quota/matching.js +143 -0
  62. package/lib/quota/parsers.js +352 -0
  63. package/lib/quota/probes/billing.js +80 -0
  64. package/lib/quota/probes/google.js +120 -0
  65. package/lib/quota/probes/oauth.js +27 -0
  66. package/lib/quota/probes/plugin.js +18 -0
  67. package/lib/updater/constants.js +23 -0
  68. package/lib/updater/errors.js +8 -0
  69. package/lib/updater/index.js +11 -0
  70. package/lib/updater/registry.js +83 -0
  71. package/lib/updater/settings.js +45 -0
  72. package/lib/updater/store.js +23 -0
  73. package/lib/updater/updater.js +148 -0
  74. package/lib/updater/verify.js +110 -0
  75. package/lib/updater/version.js +31 -0
  76. package/locale/en.json +1 -1
  77. package/locale/zh.json +1 -1
  78. package/package.json +6 -9
  79. package/lib/media.js +0 -2542
  80. package/lib/quota.js +0 -1748
  81. package/lib/updater.js +0 -443
@@ -0,0 +1,165 @@
1
+ // Image generation, reference requests and bounded result downloads.
2
+ import { MAX_REFERENCE_IMAGES } from "./constants.js";
3
+ import { MediaError, readBoundedText, upstreamMessage } from "./util.js";
4
+
5
+ // #region Image generation
6
+
7
+ const IMAGE_TIMEOUT_MS = 180_000;
8
+ const MAX_IMAGE_RESPONSE_BYTES = 96 * 1024 * 1024;
9
+ const MAX_IMAGE_DOWNLOAD_BYTES = 32 * 1024 * 1024;
10
+ // gpt-image accepts only these sizes; xAI reads `aspect_ratio` instead.
11
+ const GPT_IMAGE_SIZES = { "1:1": "1024x1024", "3:2": "1536x1024", "2:3": "1024x1536" };
12
+ const IMAGE_EXTENSIONS = { "image/png": "png", "image/jpeg": "jpg", "image/webp": "webp", "image/gif": "gif" };
13
+
14
+ /** Media type from magic bytes (PNG, JPEG, WebP, GIF), or undefined. */
15
+ export function sniffImageType(bytes) {
16
+ const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes ?? []);
17
+ if (b.length >= 8 && b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47) return "image/png";
18
+ if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return "image/jpeg";
19
+ if (b.length >= 12 && String.fromCharCode(...b.subarray(0, 4)) === "RIFF" && String.fromCharCode(...b.subarray(8, 12)) === "WEBP") return "image/webp";
20
+ if (b.length >= 6 && String.fromCharCode(...b.subarray(0, 4)) === "GIF8") return "image/gif";
21
+ return undefined;
22
+ }
23
+
24
+ async function readBoundedBytes(response, max) {
25
+ const declared = Number(response.headers?.get?.("content-length"));
26
+ if (Number.isFinite(declared) && declared > max) throw new MediaError("image-too-large");
27
+ if (typeof response.body?.getReader !== "function") {
28
+ const buffer = new Uint8Array(await response.arrayBuffer());
29
+ if (buffer.byteLength > max) throw new MediaError("image-too-large");
30
+ return buffer;
31
+ }
32
+ const reader = response.body.getReader();
33
+ const chunks = [];
34
+ let size = 0;
35
+ for (;;) {
36
+ const { done, value } = await reader.read();
37
+ if (done) break;
38
+ size += value.byteLength;
39
+ if (size > max) {
40
+ await reader.cancel().catch(() => {});
41
+ throw new MediaError("image-too-large");
42
+ }
43
+ chunks.push(value);
44
+ }
45
+ return new Uint8Array(Buffer.concat(chunks));
46
+ }
47
+
48
+ /** Bytes behind a result `url`: a data URL, or an HTTPS download without credentials. */
49
+ async function imageFromUrl(fetchImpl, url, signal) {
50
+ const dataMatch = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(url);
51
+ if (dataMatch !== null) {
52
+ if (dataMatch[2] === undefined) throw new MediaError("invalid-image");
53
+ const bytes = new Uint8Array(Buffer.from(dataMatch[3], "base64"));
54
+ if (bytes.byteLength > MAX_IMAGE_DOWNLOAD_BYTES) throw new MediaError("image-too-large");
55
+ return bytes;
56
+ }
57
+ let parsed;
58
+ try {
59
+ parsed = new URL(url);
60
+ } catch {
61
+ throw new MediaError("invalid-image");
62
+ }
63
+ if (parsed.protocol !== "https:" || parsed.username || parsed.password) throw new MediaError("invalid-image");
64
+ const response = await fetchImpl(parsed.toString(), { method: "GET", headers: { Accept: "image/*" }, signal });
65
+ if (!response.ok) throw new MediaError("download-failed", `HTTP ${response.status}`);
66
+ return readBoundedBytes(response, MAX_IMAGE_DOWNLOAD_BYTES);
67
+ }
68
+
69
+ /**
70
+ * Generate (no references) or edit (with references) images through CPA's
71
+ * OpenAI-compatible endpoints. Asks for `b64_json`; a result that carries a
72
+ * URL instead is fetched (data URL or HTTPS only, size-bounded, no key).
73
+ * @param references - `[{ mediaType, data: Uint8Array }]`, at most 4.
74
+ * @returns `[{ bytes, mediaType, ext, revisedPrompt? }]`.
75
+ * @throws MediaError `upstream-error` (detail: short message), `invalid-response`,
76
+ * `no-image`, `invalid-image`, `image-too-large`, `download-failed`, `timeout`.
77
+ */
78
+ export async function generateImages({
79
+ fetch: fetchImpl = fetch,
80
+ rootURL,
81
+ apiKey,
82
+ model,
83
+ prompt,
84
+ aspect,
85
+ count = 1,
86
+ references = [],
87
+ signal,
88
+ timeoutMs = IMAGE_TIMEOUT_MS,
89
+ }) {
90
+ if (references.length > MAX_REFERENCE_IMAGES) throw new MediaError("too-many-references");
91
+ const timeout = AbortSignal.timeout(timeoutMs);
92
+ const combined = signal === undefined ? timeout : AbortSignal.any([signal, timeout]);
93
+ const edit = references.length > 0;
94
+ const body = { model, prompt, n: count, response_format: "b64_json" };
95
+ if (aspect !== undefined) {
96
+ body.aspect_ratio = aspect;
97
+ if (GPT_IMAGE_SIZES[aspect] !== undefined) body.size = GPT_IMAGE_SIZES[aspect];
98
+ }
99
+ if (edit) {
100
+ body.images = references.map((ref) => ({
101
+ image_url: `data:${ref.mediaType};base64,${Buffer.from(ref.data).toString("base64")}`,
102
+ }));
103
+ }
104
+ let response;
105
+ let text;
106
+ try {
107
+ response = await fetchImpl(`${rootURL}${edit ? "/v1/images/edits" : "/v1/images/generations"}`, {
108
+ method: "POST",
109
+ headers: {
110
+ "Content-Type": "application/json",
111
+ Accept: "application/json",
112
+ ...(typeof apiKey === "string" && apiKey !== "" ? { Authorization: `Bearer ${apiKey}` } : {}),
113
+ },
114
+ body: JSON.stringify(body),
115
+ signal: combined,
116
+ });
117
+ text = await readBoundedText(response, MAX_IMAGE_RESPONSE_BYTES);
118
+ } catch (error) {
119
+ if (signal?.aborted) throw signal.reason;
120
+ if (timeout.aborted) throw new MediaError("timeout");
121
+ if (error instanceof MediaError) throw error;
122
+ throw new MediaError("network", error instanceof Error ? error.message.slice(0, 200) : undefined);
123
+ }
124
+ if (!response.ok) throw new MediaError("upstream-error", upstreamMessage(text, response.status));
125
+ let payload;
126
+ try {
127
+ payload = JSON.parse(text);
128
+ } catch {
129
+ throw new MediaError("invalid-response");
130
+ }
131
+ const items = Array.isArray(payload?.data) ? payload.data : [];
132
+ const out = [];
133
+ for (const item of items) {
134
+ let bytes;
135
+ if (typeof item?.b64_json === "string" && item.b64_json !== "") {
136
+ bytes = new Uint8Array(Buffer.from(item.b64_json, "base64"));
137
+ if (bytes.byteLength > MAX_IMAGE_DOWNLOAD_BYTES) throw new MediaError("image-too-large");
138
+ } else if (typeof item?.url === "string" && item.url !== "") {
139
+ try {
140
+ bytes = await imageFromUrl(fetchImpl, item.url, combined);
141
+ } catch (error) {
142
+ if (signal?.aborted) throw signal.reason;
143
+ if (timeout.aborted) throw new MediaError("timeout");
144
+ if (error instanceof MediaError) throw error;
145
+ throw new MediaError("download-failed");
146
+ }
147
+ } else {
148
+ continue;
149
+ }
150
+ const mediaType = sniffImageType(bytes);
151
+ if (mediaType === undefined) throw new MediaError("invalid-image");
152
+ out.push({
153
+ bytes,
154
+ mediaType,
155
+ ext: IMAGE_EXTENSIONS[mediaType],
156
+ ...(typeof item.revised_prompt === "string" && item.revised_prompt.trim() !== ""
157
+ ? { revisedPrompt: item.revised_prompt.trim().slice(0, 500) }
158
+ : {}),
159
+ });
160
+ }
161
+ if (out.length === 0) throw new MediaError("no-image");
162
+ return out;
163
+ }
164
+
165
+ // #endregion
@@ -0,0 +1,29 @@
1
+ // Barrel preserving the internal media engine's public surface.
2
+
3
+ // Internal module: CPA image / video generation support.
4
+ //
5
+ // Classifies catalog models as chat / image / video, owns the independent
6
+ // `media-settings.json` preferences, `media-sessions.json` (per-session picks
7
+ // and run records) and `media-jobs.json` (video jobs that outlive a reload),
8
+ // runs a read-only diagnostic, and talks to CPA's image and video routes.
9
+ // Generation happens only when the host calls it for an explicit user action.
10
+ // Keys are passed in per call and never stored or returned.
11
+
12
+ export { MEDIA_SETTINGS_VERSION, VIDEO_SECONDS, VIDEO_RESOLUTIONS, BUDGET_SECONDS, CONFIRM_MODES, MAX_RUNS, VIDEO_MODES, IMAGE_ASPECTS, IMAGE_COUNT, MAX_REFERENCE_IMAGES, VIDEO_ASPECTS, MAX_VIDEO_BYTES, VIDEO_JOB_TTL_MS, VIDEO_GENERATION_BODY_MAX_BYTES, VIDEO_UPLOAD_BODY_MAX_BYTES, VIDEO_UPLOAD_MAX_BYTES, EXTEND_SECONDS, EDIT_SOURCE_MAX_SECONDS, EXTEND_SOURCE_SECONDS, VIDEO_EDIT_MODEL, REFERENCE_MAX_EDGE } from "./constants.js";
13
+ export { MediaError, upstreamMessage } from "./util.js";
14
+ export { MEDIA_KINDS, bareModelId, detectMediaKind, mediaAliasTarget, effectiveKind } from "./classify.js";
15
+ export { defaultMediaSettings, normalizeMediaSettings, loadMediaSettings, saveMediaSettings, applyMediaPatch, accountKindOverrides, mediaModelEnabled } from "./settings.js";
16
+ export { summarizeCatalogForMedia, diagnoseMedia } from "./diagnose.js";
17
+ export { mediaOptions, splitMediaRef, matchMediaModel, chooseMediaModel } from "./choice.js";
18
+ export { MEDIA_SESSIONS_VERSION, MAX_RECENT, validSessionId, validCommandId, normalizeRun, budgetKey, agentPolicy, applySessionPolicy, chargeSeconds, normalizeMediaSessions, createMediaSessionStore, recentEntry } from "./session-store.js";
19
+ export { parseImageCommand, parseVideoCommand, parseVideoEditCommand } from "./commands/parsing.js";
20
+ export { sniffImageType, generateImages } from "./images.js";
21
+ export { outputDirectory, outputStem, writeOutputFiles } from "./output-files.js";
22
+ export { isMp4, mp4Duration } from "./mp4.js";
23
+ export { videoImageBudget, videoRequestModel, submitVideo, submitVideoEdit, videoState, retrieveVideo, sniffVideoType, downloadVideo } from "./video.js";
24
+ export { interpretationRequest, interpretedPrompt, parseInterpretation, interpretationLabels } from "./interpret.js";
25
+ export { MEDIA_JOBS_VERSION, normalizeJob, normalizeMediaJobs, createMediaJobStore } from "./job-store.js";
26
+ export { decodePng } from "./codec/png.js";
27
+ export { encodeJpeg } from "./codec/jpeg.js";
28
+ export { fitRgb, fitReferenceImage } from "./codec/fit.js";
29
+ export { GENERATE_MEDIA, GENERATE_MEDIA_TOOL, GENERATE_MEDIA_OUTPUT, parseToolArgs, toolCommandLine, toolRunId, approvalText } from "./agent-tool.js";
@@ -0,0 +1,126 @@
1
+ // Chat-model parameter questions, validated answers and deletion-only prompts.
2
+ import { IMAGE_ASPECTS, VIDEO_ASPECTS, IMAGE_COUNT, VIDEO_SECONDS, VIDEO_RESOLUTIONS, EXTEND_SECONDS } from "./constants.js";
3
+ import { intIn } from "./util.js";
4
+
5
+ // #region Parameter interpretation by the session's chat model
6
+ //
7
+ // The rule parsers read flags and a few whole words; anything else written in
8
+ // plain language ("十秒钟", "竖版的", "第二张当结尾") is handed to the session's
9
+ // own chat model, which returns the parameters as JSON. Explicit flags always
10
+ // win, every value is validated here, and the prompt may only lose words
11
+ // (never be rewritten): `interpretedPrompt()` keeps the original otherwise.
12
+
13
+ const INTERPRET_FIELDS = {
14
+ image: ["aspect", "count"],
15
+ video: ["seconds", "resolution", "aspect", "frames"],
16
+ edit: [],
17
+ extend: ["seconds"],
18
+ };
19
+
20
+ /**
21
+ * The system and user text asking a chat model to read parameters out of a
22
+ * generation request.
23
+ * @param command - `image` / `video` / `edit` / `extend`.
24
+ * @param text - the prompt as the rule parser left it.
25
+ * @param images - attached image count.
26
+ * @param fixed - field names already set by explicit options (not to be read again).
27
+ */
28
+ export function interpretationRequest({ command, text, images = 0, fixed = [] }) {
29
+ const fields = INTERPRET_FIELDS[command] ?? [];
30
+ const lines = {
31
+ seconds: command === "extend" ? "seconds: integer 2-10, how many seconds to ADD to the video" : "seconds: integer 1-15, video length",
32
+ resolution: 'resolution: "480p" or "720p" (high definition / 高清 = "720p")',
33
+ aspect: `aspect: one of ${(command === "image" ? IMAGE_ASPECTS : VIDEO_ASPECTS).join(", ")} (portrait / 竖屏 = "9:16", landscape / 横屏 = "16:9", square = "1:1")`,
34
+ count: "count: integer 1-4, number of images",
35
+ frames:
36
+ 'frames: how the attached images are used — "first-frame" (one image starts the video), "reference" (images show people / objects to include, no fixed frame), "first-last" (image 1 starts, image 2 ends the video), "last-frame" (the video ends on the image)',
37
+ };
38
+ const wanted = fields.filter((f) => !fixed.includes(f) && (f !== "frames" || images > 0));
39
+ const system = [
40
+ "You read generation parameters out of a user's request to an image / video generator.",
41
+ "Reply with exactly one JSON object and nothing else.",
42
+ wanted.length > 0 ? `Fields (omit a field the request does not state):\n- ${wanted.map((f) => lines[f]).join("\n- ")}` : "There are no parameter fields to read.",
43
+ '- prompt: the request text with ONLY the words that state these parameters removed. Copy every other character exactly as written: do not translate, rephrase, fix, shorten or add anything. If no such words exist, return the text unchanged.',
44
+ ].join("\n");
45
+ const user = `${images > 0 ? `Attached images: ${images}.\n` : ""}Request:\n${text}`;
46
+ return { system, user, fields: wanted };
47
+ }
48
+
49
+ /** Whether `short` can be made from `long` by deleting characters (order kept). */
50
+ function isSubsequence(short, long) {
51
+ let at = 0;
52
+ for (const ch of long) if (at < short.length && ch === short[at]) at += 1;
53
+ return at === short.length;
54
+ }
55
+
56
+ /**
57
+ * The model's prompt when it only removed characters and kept a fair part of
58
+ * the text (at least 30 %, unless it removed 12 characters or fewer),
59
+ * otherwise the original.
60
+ */
61
+ export function interpretedPrompt(candidate, original) {
62
+ if (typeof candidate !== "string") return original;
63
+ const prompt = candidate.replace(/\s+/g, " ").trim();
64
+ const base = original.replace(/\s+/g, " ").trim();
65
+ if (prompt === "" || !isSubsequence(prompt, base)) return original;
66
+ if (prompt.length < base.length * 0.3 && base.length - prompt.length > 12) return original;
67
+ return prompt;
68
+ }
69
+
70
+ /**
71
+ * Validate a chat model's JSON answer against the command's fields.
72
+ * @returns `{ values, prompt }`: only valid, wanted fields; the prompt checked by
73
+ * {@link interpretedPrompt}. Undefined when the answer holds no JSON object.
74
+ */
75
+ export function parseInterpretation(answer, { command, text, fields, images = 0 }) {
76
+ const raw = String(answer ?? "");
77
+ const start = raw.indexOf("{");
78
+ const end = raw.lastIndexOf("}");
79
+ if (start < 0 || end <= start) return undefined;
80
+ let data;
81
+ try {
82
+ data = JSON.parse(raw.slice(start, end + 1));
83
+ } catch {
84
+ return undefined;
85
+ }
86
+ if (data === null || typeof data !== "object" || Array.isArray(data)) return undefined;
87
+ const values = {};
88
+ const allowed = new Set(fields ?? INTERPRET_FIELDS[command] ?? []);
89
+ const int = (v) => (typeof v === "number" ? v : typeof v === "string" && /^\s*\d+\s*$/.test(v) ? Number(v) : NaN);
90
+ if (allowed.has("seconds")) {
91
+ const range = command === "extend" ? EXTEND_SECONDS : VIDEO_SECONDS;
92
+ const seconds = intIn(int(data.seconds), range);
93
+ if (seconds !== undefined) values.seconds = seconds;
94
+ }
95
+ if (allowed.has("resolution") && typeof data.resolution === "string") {
96
+ const resolution = data.resolution.trim().toLowerCase();
97
+ if (VIDEO_RESOLUTIONS.includes(resolution)) values.resolution = resolution;
98
+ }
99
+ if (allowed.has("aspect") && typeof data.aspect === "string") {
100
+ const aspect = data.aspect.replace(/[:\s]/g, (c) => (c === ":" ? ":" : ""));
101
+ if ((command === "image" ? IMAGE_ASPECTS : VIDEO_ASPECTS).includes(aspect)) values.aspect = aspect;
102
+ }
103
+ if (allowed.has("count")) {
104
+ const count = intIn(int(data.count), IMAGE_COUNT);
105
+ if (count !== undefined) values.count = count;
106
+ }
107
+ if (allowed.has("frames") && images > 0) {
108
+ // The parser's words: undefined = default (first frame for one image, references for more).
109
+ const frames = { "first-frame": images === 1 ? "first" : undefined, reference: "reference", "first-last": "first-last", "last-frame": "last" }[data.frames];
110
+ if (frames !== undefined) values.frames = frames;
111
+ }
112
+ return { values, prompt: interpretedPrompt(data.prompt, text) };
113
+ }
114
+
115
+ /** Short labels of interpreted values for the chat row ("10s", "720p", "9:16", "2 images", "reference"). */
116
+ export function interpretationLabels(values) {
117
+ return [
118
+ ...(values.seconds !== undefined ? [`${values.seconds}s`] : []),
119
+ ...(values.resolution !== undefined ? [values.resolution] : []),
120
+ ...(values.aspect !== undefined ? [values.aspect] : []),
121
+ ...(values.count !== undefined ? [`×${values.count}`] : []),
122
+ ...(values.frames !== undefined ? [values.frames] : []),
123
+ ];
124
+ }
125
+
126
+ // #endregion
@@ -0,0 +1,99 @@
1
+ // Durable, URL-free video jobs and finished-job retention.
2
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
3
+ import { dirname, isAbsolute } from "node:path";
4
+ import { REQUEST_ID_PATTERN, ACCOUNT_ID_PATTERN, MAX_OUTPUT_DIR, VIDEO_SECONDS } from "./constants.js";
5
+ import { validSessionId, validCommandId } from "./session-store.js";
6
+ import { plain, clip, intIn } from "./util.js";
7
+
8
+ // #region Video job store (media-jobs.json)
9
+
10
+ export const MEDIA_JOBS_VERSION = 1;
11
+ const JOB_STATES = ["submitted", "done", "failed", "expired"];
12
+ const MAX_JOBS = 200;
13
+ const FINISHED_JOB_KEEP_MS = 7 * 24 * 60 * 60 * 1000;
14
+
15
+ /**
16
+ * One submitted video, enough to resume polling and saving after a reload or
17
+ * restart: CPA's request id, the account and model, where to save, and the
18
+ * chat row it belongs to. Never keys or upstream URLs.
19
+ */
20
+ export function normalizeJob(value) {
21
+ const job = plain(value);
22
+ if (job === undefined || typeof job.id !== "string" || !REQUEST_ID_PATTERN.test(job.id)) return undefined;
23
+ if (!JOB_STATES.includes(job.state) || typeof job.accountId !== "string" || !ACCOUNT_ID_PATTERN.test(job.accountId)) return undefined;
24
+ if (typeof job.modelId !== "string" || job.modelId === "" || typeof job.cwd !== "string" || !isAbsolute(job.cwd)) return undefined;
25
+ if (typeof job.submittedAt !== "string" || !Number.isFinite(Date.parse(job.submittedAt))) return undefined;
26
+ return {
27
+ id: job.id,
28
+ state: job.state,
29
+ accountId: job.accountId,
30
+ modelId: job.modelId.slice(0, 300),
31
+ ref: clip(job.ref, 400),
32
+ ...(validSessionId(job.sessionId) === undefined ? {} : { sessionId: job.sessionId }),
33
+ ...(validCommandId(job.commandId) === undefined ? {} : { commandId: job.commandId }),
34
+ cwd: job.cwd,
35
+ outputDir: typeof job.outputDir === "string" && job.outputDir !== "" ? job.outputDir.slice(0, MAX_OUTPUT_DIR) : "generated",
36
+ prompt: clip(job.prompt, 4000),
37
+ seconds: intIn(job.seconds, VIDEO_SECONDS) ?? 6,
38
+ submittedAt: job.submittedAt.slice(0, 40),
39
+ ...(typeof job.finishedAt === "string" ? { finishedAt: job.finishedAt.slice(0, 40) } : {}),
40
+ };
41
+ }
42
+
43
+ export function normalizeMediaJobs(raw, now = Date.now()) {
44
+ const jobs = [];
45
+ const seen = new Set();
46
+ for (const item of Array.isArray(plain(raw)?.jobs) ? raw.jobs : []) {
47
+ const job = normalizeJob(item);
48
+ if (job === undefined || seen.has(job.id)) continue;
49
+ if (job.state !== "submitted" && now - Date.parse(job.finishedAt ?? job.submittedAt) > FINISHED_JOB_KEEP_MS) continue;
50
+ seen.add(job.id);
51
+ jobs.push(job);
52
+ if (jobs.length >= MAX_JOBS) break;
53
+ }
54
+ return { version: MEDIA_JOBS_VERSION, jobs };
55
+ }
56
+
57
+ /**
58
+ * Video jobs in their own file, re-read on every access; atomic writes; a
59
+ * damaged file loads as empty and is kept as `*.corrupt-*.json` by the next write.
60
+ */
61
+ export function createMediaJobStore(filePath, { now = () => Date.now() } = {}) {
62
+ const read = () => {
63
+ if (!existsSync(filePath)) return { data: normalizeMediaJobs({}, now()) };
64
+ try {
65
+ const raw = JSON.parse(readFileSync(filePath, "utf8"));
66
+ if (plain(raw) === undefined) return { data: normalizeMediaJobs({}, now()), damaged: true };
67
+ return { data: normalizeMediaJobs(raw, now()) };
68
+ } catch {
69
+ return { data: normalizeMediaJobs({}, now()), damaged: true };
70
+ }
71
+ };
72
+ const write = (data, damaged) => {
73
+ const next = normalizeMediaJobs(data, now());
74
+ mkdirSync(dirname(filePath), { recursive: true });
75
+ if (damaged && existsSync(filePath)) renameSync(filePath, filePath.replace(/\.json$/, `.corrupt-${Date.now().toString(36)}.json`));
76
+ const tmp = `${filePath}.${Date.now().toString(36)}.tmp`;
77
+ writeFileSync(tmp, `${JSON.stringify(next, null, 2)}\n`, "utf8");
78
+ renameSync(tmp, filePath);
79
+ return next;
80
+ };
81
+ return {
82
+ list() {
83
+ return read().data.jobs;
84
+ },
85
+ get(id) {
86
+ return read().data.jobs.find((job) => job.id === id);
87
+ },
88
+ /** Insert or merge one job (newest first). */
89
+ put(job) {
90
+ const { data, damaged } = read();
91
+ const index = data.jobs.findIndex((j) => j.id === job.id);
92
+ if (index >= 0) data.jobs[index] = { ...data.jobs[index], ...job };
93
+ else data.jobs.unshift(job);
94
+ return write(data, damaged).jobs.find((j) => j.id === job.id);
95
+ },
96
+ };
97
+ }
98
+
99
+ // #endregion
@@ -0,0 +1,44 @@
1
+ // Local ISO media identification and MP4 duration inspection.
2
+ const asBuffer = (bytes) => (Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
3
+
4
+ /** Whether the bytes start like an ISO base media file (MP4 / MOV: an `ftyp` box first). */
5
+ export function isMp4(bytes) {
6
+ return bytes instanceof Uint8Array && bytes.byteLength >= 12 && asBuffer(bytes).toString("latin1", 4, 8) === "ftyp";
7
+ }
8
+
9
+ /**
10
+ * A video's length from its `moov/mvhd` box, in seconds (two decimals), or
11
+ * undefined when the file does not carry one this parser understands.
12
+ */
13
+ export function mp4Duration(bytes) {
14
+ if (!isMp4(bytes)) return undefined;
15
+ const buf = asBuffer(bytes);
16
+ const find = (start, end, type) => {
17
+ let at = start;
18
+ while (at + 8 <= end) {
19
+ let size = buf.readUInt32BE(at);
20
+ let header = 8;
21
+ if (size === 1) {
22
+ if (at + 16 > end) return undefined;
23
+ const large = buf.readBigUInt64BE(at + 8);
24
+ if (large > BigInt(end - at)) return undefined;
25
+ size = Number(large);
26
+ header = 16;
27
+ } else if (size === 0) size = end - at;
28
+ if (size < header || at + size > end) return undefined;
29
+ if (buf.toString("latin1", at + 4, at + 8) === type) return [at + header, at + size];
30
+ at += size;
31
+ }
32
+ return undefined;
33
+ };
34
+ const moov = find(0, buf.length, "moov");
35
+ const mvhd = moov === undefined ? undefined : find(moov[0], moov[1], "mvhd");
36
+ if (mvhd === undefined) return undefined;
37
+ const [c, end] = mvhd;
38
+ const v1 = buf[c] === 1;
39
+ if (c + (v1 ? 32 : 20) > end) return undefined;
40
+ const timescale = buf.readUInt32BE(c + (v1 ? 20 : 12));
41
+ const duration = v1 ? Number(buf.readBigUInt64BE(c + 24)) : buf.readUInt32BE(c + 16);
42
+ if (timescale === 0 || !Number.isFinite(duration) || duration <= 0) return undefined;
43
+ return Math.round((duration / timescale) * 100) / 100;
44
+ }
@@ -0,0 +1,59 @@
1
+ // Portable output names and exclusive, non-overwriting file writes.
2
+ import { mkdirSync, writeFileSync } from "node:fs";
3
+ import { isAbsolute, join } from "node:path";
4
+ import { bareModelId } from "./classify.js";
5
+ import { MediaError } from "./util.js";
6
+
7
+ // #region Output files
8
+
9
+ /** The output directory for a session: absolute settings are used as is, relative ones under `cwd`. */
10
+ export function outputDirectory(cwd, outputDir) {
11
+ return isAbsolute(outputDir) ? outputDir : join(cwd, outputDir);
12
+ }
13
+
14
+ function stamp(date) {
15
+ const p = (n) => String(n).padStart(2, "0");
16
+ return `${date.getFullYear()}${p(date.getMonth() + 1)}${p(date.getDate())}-${p(date.getHours())}${p(date.getMinutes())}${p(date.getSeconds())}`;
17
+ }
18
+
19
+ /** File stem `YYYYMMDD-HHmmss-<model>-<prompt slug>`; safe on Windows, macOS and Linux. */
20
+ export function outputStem(model, prompt, date = new Date()) {
21
+ const modelPart = bareModelId(model).replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "") || "model";
22
+ const slug =
23
+ String(prompt ?? "")
24
+ .normalize("NFKC")
25
+ .replace(/[^\p{L}\p{N}]+/gu, "-")
26
+ .replace(/^-+|-+$/g, "")
27
+ .slice(0, 24)
28
+ .replace(/-+$/g, "") || "media";
29
+ return `${stamp(date)}-${modelPart}-${slug}`;
30
+ }
31
+
32
+ /**
33
+ * Write each file next to the others without ever replacing an existing
34
+ * file (`wx`); a taken name gets the next free `-N` suffix.
35
+ * @param files - `[{ bytes, ext }]`.
36
+ * @returns absolute paths, in order.
37
+ */
38
+ export function writeOutputFiles(dir, stem, files) {
39
+ mkdirSync(dir, { recursive: true });
40
+ const paths = [];
41
+ files.forEach((file, index) => {
42
+ const base = files.length > 1 ? `${stem}-${index + 1}` : stem;
43
+ for (let attempt = 0; attempt < 100; attempt += 1) {
44
+ const name = attempt === 0 ? `${base}.${file.ext}` : `${base}-${attempt + 1}.${file.ext}`;
45
+ const path = join(dir, name);
46
+ try {
47
+ writeFileSync(path, file.bytes, { flag: "wx" });
48
+ paths.push(path);
49
+ return;
50
+ } catch (error) {
51
+ if (error?.code !== "EEXIST") throw new MediaError("write-failed", error?.code ?? undefined);
52
+ }
53
+ }
54
+ throw new MediaError("write-failed", "EEXIST");
55
+ });
56
+ return paths;
57
+ }
58
+
59
+ // #endregion