dsh-cliproxyapi-pro 1.9.0 → 1.10.0
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/CHANGELOG.md +23 -0
- package/README.en.md +24 -408
- package/README.md +24 -408
- package/lib/client.js +265 -27
- package/lib/host/accounts.js +178 -0
- package/lib/host/apply/agent-tool.js +406 -0
- package/lib/host/apply/commands.js +122 -0
- package/lib/host/apply/controller.js +121 -0
- package/lib/host/apply/http-routes.js +273 -0
- package/lib/host/apply/image-jobs.js +171 -0
- package/lib/host/apply/index.js +41 -0
- package/lib/host/apply/interpret.js +72 -0
- package/lib/host/apply/lifecycle.js +50 -0
- package/lib/host/apply/media-failures.js +68 -0
- package/lib/host/apply/media-service.js +202 -0
- package/lib/host/apply/quota-service.js +42 -0
- package/lib/host/apply/references.js +43 -0
- package/lib/host/apply/session-status.js +104 -0
- package/lib/host/apply/state.js +78 -0
- package/lib/host/apply/updates.js +55 -0
- package/lib/host/apply/video-command.js +152 -0
- package/lib/host/apply/video-edit.js +253 -0
- package/lib/host/apply/video-jobs.js +206 -0
- package/lib/host/apply-utils.js +39 -0
- package/lib/host/config.js +210 -0
- package/lib/host/controller.js +464 -0
- package/lib/host/discovery.js +301 -0
- package/lib/host/http-gate.js +144 -0
- package/lib/host/model-info.js +363 -0
- package/lib/host/model-names.js +152 -0
- package/lib/host/models.js +75 -0
- package/lib/host/mount-guard.js +56 -0
- package/lib/host/recommend.js +367 -0
- package/lib/host/routes.js +165 -0
- package/lib/host/runtime.js +471 -0
- package/lib/host/state-store.js +39 -0
- package/lib/host/status-text.js +93 -0
- package/lib/index.js +18 -4417
- package/lib/media/agent-tool.js +255 -0
- package/lib/media/choice.js +83 -0
- package/lib/media/classify.js +65 -0
- package/lib/media/codec/fit.js +76 -0
- package/lib/media/codec/jpeg.js +158 -0
- package/lib/media/codec/png.js +102 -0
- package/lib/media/commands/parsing.js +269 -0
- package/lib/media/constants.js +46 -0
- package/lib/media/diagnose.js +134 -0
- package/lib/media/images.js +165 -0
- package/lib/media/index.js +29 -0
- package/lib/media/interpret.js +126 -0
- package/lib/media/job-store.js +99 -0
- package/lib/media/mp4.js +44 -0
- package/lib/media/output-files.js +59 -0
- package/lib/media/session-store.js +293 -0
- package/lib/media/settings.js +255 -0
- package/lib/media/util.js +45 -0
- package/lib/media/video.js +326 -0
- package/lib/quota/auth-state.js +33 -0
- package/lib/quota/constants.js +86 -0
- package/lib/quota/controller.js +467 -0
- package/lib/quota/helpers.js +325 -0
- package/lib/quota/index.js +19 -0
- package/lib/quota/management.js +216 -0
- package/lib/quota/matching.js +143 -0
- package/lib/quota/parsers.js +352 -0
- package/lib/quota/probes/billing.js +80 -0
- package/lib/quota/probes/google.js +120 -0
- package/lib/quota/probes/oauth.js +27 -0
- package/lib/quota/probes/plugin.js +18 -0
- package/lib/updater/constants.js +23 -0
- package/lib/updater/errors.js +8 -0
- package/lib/updater/index.js +11 -0
- package/lib/updater/registry.js +83 -0
- package/lib/updater/settings.js +45 -0
- package/lib/updater/store.js +23 -0
- package/lib/updater/updater.js +148 -0
- package/lib/updater/verify.js +110 -0
- package/lib/updater/version.js +31 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +6 -9
- package/lib/media.js +0 -2542
- package/lib/quota.js +0 -1748
- package/lib/updater.js +0 -443
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Dependency-free non-interlaced PNG decoding.
|
|
2
|
+
import { inflateSync } from "node:zlib";
|
|
3
|
+
import { sniffImageType } from "../images.js";
|
|
4
|
+
|
|
5
|
+
/** Decode a non-interlaced PNG (any color type, 1–16-bit) to RGBA8, or undefined when unsupported. */
|
|
6
|
+
export function decodePng(bytes) {
|
|
7
|
+
const b = Buffer.from(bytes);
|
|
8
|
+
if (b.length < 33 || sniffImageType(b) !== "image/png") return undefined;
|
|
9
|
+
let offset = 8;
|
|
10
|
+
let width = 0;
|
|
11
|
+
let height = 0;
|
|
12
|
+
let depth = 0;
|
|
13
|
+
let type = 0;
|
|
14
|
+
let interlace = 0;
|
|
15
|
+
let palette;
|
|
16
|
+
let trns;
|
|
17
|
+
const idat = [];
|
|
18
|
+
while (offset + 8 <= b.length) {
|
|
19
|
+
const length = b.readUInt32BE(offset);
|
|
20
|
+
const name = b.toString("latin1", offset + 4, offset + 8);
|
|
21
|
+
const data = b.subarray(offset + 8, offset + 8 + length);
|
|
22
|
+
offset += 12 + length;
|
|
23
|
+
if (name === "IHDR") {
|
|
24
|
+
width = data.readUInt32BE(0);
|
|
25
|
+
height = data.readUInt32BE(4);
|
|
26
|
+
depth = data[8];
|
|
27
|
+
type = data[9];
|
|
28
|
+
interlace = data[12];
|
|
29
|
+
} else if (name === "PLTE") palette = data;
|
|
30
|
+
else if (name === "tRNS") trns = data;
|
|
31
|
+
else if (name === "IDAT") idat.push(data);
|
|
32
|
+
else if (name === "IEND") break;
|
|
33
|
+
}
|
|
34
|
+
if (width === 0 || height === 0 || interlace !== 0 || width * height > 40_000_000) return undefined;
|
|
35
|
+
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[type];
|
|
36
|
+
if (channels === undefined || ![1, 2, 4, 8, 16].includes(depth) || (type === 3 && palette === undefined)) return undefined;
|
|
37
|
+
let raw;
|
|
38
|
+
try {
|
|
39
|
+
raw = inflateSync(Buffer.concat(idat));
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const bpp = Math.max(1, (channels * depth) / 8);
|
|
44
|
+
const stride = Math.ceil((width * channels * depth) / 8);
|
|
45
|
+
if (raw.length < height * (stride + 1)) return undefined;
|
|
46
|
+
const lines = Buffer.alloc(height * stride);
|
|
47
|
+
for (let y = 0; y < height; y += 1) {
|
|
48
|
+
const filter = raw[y * (stride + 1)];
|
|
49
|
+
const src = y * (stride + 1) + 1;
|
|
50
|
+
const dst = y * stride;
|
|
51
|
+
for (let x = 0; x < stride; x += 1) {
|
|
52
|
+
const a = x >= bpp ? lines[dst + x - bpp] : 0;
|
|
53
|
+
const up = y > 0 ? lines[dst - stride + x] : 0;
|
|
54
|
+
const c = x >= bpp && y > 0 ? lines[dst - stride + x - bpp] : 0;
|
|
55
|
+
let value = raw[src + x];
|
|
56
|
+
if (filter === 1) value += a;
|
|
57
|
+
else if (filter === 2) value += up;
|
|
58
|
+
else if (filter === 3) value += (a + up) >> 1;
|
|
59
|
+
else if (filter === 4) {
|
|
60
|
+
const p = a + up - c;
|
|
61
|
+
const pa = Math.abs(p - a);
|
|
62
|
+
const pb = Math.abs(p - up);
|
|
63
|
+
const pc = Math.abs(p - c);
|
|
64
|
+
value += pa <= pb && pa <= pc ? a : pb <= pc ? up : c;
|
|
65
|
+
} else if (filter !== 0) return undefined;
|
|
66
|
+
lines[dst + x] = value & 0xff;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
70
|
+
const sample = (row, index) => {
|
|
71
|
+
if (depth === 8) return lines[row + index];
|
|
72
|
+
if (depth === 16) return lines[row + index * 2];
|
|
73
|
+
const perByte = 8 / depth;
|
|
74
|
+
const byte = lines[row + Math.floor(index / perByte)];
|
|
75
|
+
const shift = 8 - depth * ((index % perByte) + 1);
|
|
76
|
+
return (byte >> shift) & ((1 << depth) - 1);
|
|
77
|
+
};
|
|
78
|
+
const scale = depth < 8 && type !== 3 ? 255 / ((1 << depth) - 1) : 1;
|
|
79
|
+
for (let y = 0; y < height; y += 1) {
|
|
80
|
+
const row = y * stride;
|
|
81
|
+
for (let x = 0; x < width; x += 1) {
|
|
82
|
+
const o = (y * width + x) * 4;
|
|
83
|
+
if (type === 3) {
|
|
84
|
+
const i = sample(row, x);
|
|
85
|
+
rgba[o] = palette[i * 3] ?? 0;
|
|
86
|
+
rgba[o + 1] = palette[i * 3 + 1] ?? 0;
|
|
87
|
+
rgba[o + 2] = palette[i * 3 + 2] ?? 0;
|
|
88
|
+
rgba[o + 3] = trns !== undefined && i < trns.length ? trns[i] : 255;
|
|
89
|
+
} else if (type === 0 || type === 4) {
|
|
90
|
+
const g = Math.round(sample(row, x * channels) * scale);
|
|
91
|
+
rgba[o] = rgba[o + 1] = rgba[o + 2] = g;
|
|
92
|
+
rgba[o + 3] = type === 4 ? sample(row, x * channels + 1) : 255;
|
|
93
|
+
} else {
|
|
94
|
+
rgba[o] = sample(row, x * channels);
|
|
95
|
+
rgba[o + 1] = sample(row, x * channels + 1);
|
|
96
|
+
rgba[o + 2] = sample(row, x * channels + 2);
|
|
97
|
+
rgba[o + 3] = type === 6 ? sample(row, x * channels + 3) : 255;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { width, height, rgba };
|
|
102
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Natural option extraction and image, video and edit command parsing.
|
|
2
|
+
import { MediaError, intIn } from "../util.js";
|
|
3
|
+
import { IMAGE_ASPECTS, IMAGE_COUNT, VIDEO_ASPECTS, VIDEO_SECONDS, VIDEO_RESOLUTIONS, EXTEND_SECONDS } from "../constants.js";
|
|
4
|
+
import { validCommandId } from "../session-store.js";
|
|
5
|
+
|
|
6
|
+
// Plain-language options at the start or end of a prompt, written as their
|
|
7
|
+
// own words ("10 秒 480p 竖屏 …", "… 16:9 2张"): a token counts only when a
|
|
8
|
+
// space, comma or the end follows it, so "3 秒后爆炸" stays prompt text.
|
|
9
|
+
const WORD_END = "(?=[\\s,,、;;]|$)";
|
|
10
|
+
const NATURAL_TOKENS = [
|
|
11
|
+
{ key: "seconds", kinds: ["video"], re: new RegExp(`^(\\d{1,3})\\s*(?:秒钟?|s|secs?|seconds?)${WORD_END}`, "i"), value: (m) => Number(m[1]) },
|
|
12
|
+
// "edit" / "extend": words xAI's edit and extension routes cannot use (the
|
|
13
|
+
// output keeps the source's resolution and ratio; an edit keeps its length).
|
|
14
|
+
{ key: "seconds", kinds: ["edit"], re: new RegExp(`^(\\d{1,3})\\s*(?:秒钟?|s|secs?|seconds?)${WORD_END}`, "i"), value: (m) => Number(m[1]) },
|
|
15
|
+
{ key: "resolution", kinds: ["video", "edit", "extend"], re: new RegExp(`^(480|720|1080)\\s*p${WORD_END}`, "i"), value: (m) => `${m[1]}p` },
|
|
16
|
+
{ key: "count", kinds: ["image"], re: new RegExp(`^(\\d{1,2})\\s*(?:张|幅|images?|pics?)${WORD_END}`, "i"), value: (m) => Number(m[1]) },
|
|
17
|
+
{ key: "aspect", kinds: ["image", "video", "edit", "extend"], re: new RegExp(`^(\\d{1,2})\\s*[::]\\s*(\\d{1,2})${WORD_END}`), value: (m) => `${m[1]}:${m[2]}` },
|
|
18
|
+
{ key: "aspect", kinds: ["image", "video", "edit", "extend"], re: new RegExp(`^(?:竖屏|竖版|portrait)${WORD_END}`, "i"), value: () => "9:16" },
|
|
19
|
+
{ key: "aspect", kinds: ["image", "video", "edit", "extend"], re: new RegExp(`^(?:横屏|横版|landscape)${WORD_END}`, "i"), value: () => "16:9" },
|
|
20
|
+
{ key: "aspect", kinds: ["image", "video", "edit", "extend"], re: new RegExp(`^(?:方形|正方形|square)${WORD_END}`, "i"), value: () => "1:1" },
|
|
21
|
+
{ key: "frames", kinds: ["video"], re: new RegExp(`^(?:参考图|参考图模式|references?)${WORD_END}`, "i"), value: () => "reference" },
|
|
22
|
+
{ key: "frames", kinds: ["video"], re: new RegExp(`^(?:首尾帧|首帧尾帧|first-last)${WORD_END}`, "i"), value: () => "first-last" },
|
|
23
|
+
{ key: "frames", kinds: ["video"], re: new RegExp(`^(?:尾帧|last-frame)${WORD_END}`, "i"), value: () => "last" },
|
|
24
|
+
{ key: "seconds", kinds: ["extend"], re: new RegExp(`^(\\d{1,3})\\s*(?:秒钟?|s|secs?|seconds?)${WORD_END}`, "i"), value: (m) => Number(m[1]) },
|
|
25
|
+
];
|
|
26
|
+
const SEPARATORS = /^[\s,,、;;]+/;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Take plain-language options from the start and the end of `text`.
|
|
30
|
+
* Explicit flags already in `out` win; a recognized token is removed either way.
|
|
31
|
+
* @returns the remaining prompt; values land in `out` (validated by the caller).
|
|
32
|
+
*/
|
|
33
|
+
function takeNaturalOptions(text, kind, out) {
|
|
34
|
+
const tokens = NATURAL_TOKENS.filter((t) => t.kinds.includes(kind));
|
|
35
|
+
const take = (match, token) => {
|
|
36
|
+
if (out[token.key] === undefined) out[token.key] = token.value(match);
|
|
37
|
+
};
|
|
38
|
+
let rest = text;
|
|
39
|
+
for (let guard = 0; guard < 8; guard += 1) {
|
|
40
|
+
const hit = tokens.map((token) => [token, token.re.exec(rest)]).find(([, m]) => m !== null);
|
|
41
|
+
if (hit === undefined) break;
|
|
42
|
+
take(hit[1], hit[0]);
|
|
43
|
+
rest = rest.slice(hit[1][0].length).replace(SEPARATORS, "");
|
|
44
|
+
}
|
|
45
|
+
// The end: peel whole words off the tail while they are options.
|
|
46
|
+
for (let guard = 0; guard < 8; guard += 1) {
|
|
47
|
+
const words = rest.split(/([\s,,、;;]+)/);
|
|
48
|
+
let matched = false;
|
|
49
|
+
for (let n = Math.min(3, words.length); n >= 1 && !matched; n -= 1) {
|
|
50
|
+
const tail = words.slice(-(2 * n - 1)).join("");
|
|
51
|
+
for (const token of tokens) {
|
|
52
|
+
const m = token.re.exec(tail);
|
|
53
|
+
if (m !== null && m[0].length === tail.length) {
|
|
54
|
+
take(m, token);
|
|
55
|
+
rest = words.slice(0, words.length - (2 * n - 1)).join("").replace(/[\s,,、;;]+$/, "");
|
|
56
|
+
matched = true;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!matched) break;
|
|
62
|
+
}
|
|
63
|
+
return rest.trim();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Parse `/cpa-image` input: leading options, then the prompt verbatim.
|
|
68
|
+
* Options: `-m|--model <id>`, `--aspect <ratio>`, `-n|--count <1-4>`; `--`
|
|
69
|
+
* ends options.
|
|
70
|
+
* @throws MediaError `missing-prompt` / `invalid-option` / `invalid-aspect` / `invalid-count`.
|
|
71
|
+
*/
|
|
72
|
+
export function parseImageCommand(rawInput, { literal = false } = {}) {
|
|
73
|
+
let rest = String(rawInput ?? "").trim();
|
|
74
|
+
const out = {};
|
|
75
|
+
for (;;) {
|
|
76
|
+
if (rest.startsWith("-- ") || rest === "--") {
|
|
77
|
+
rest = rest.slice(2).trim();
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
const match = rest.match(/^(-m|--model|--aspect|-n|--count)(?:=|\s+)(\S+)(?:\s+|$)/);
|
|
81
|
+
if (match === null) {
|
|
82
|
+
if (/^--?[A-Za-z]/.test(rest)) throw new MediaError("invalid-option", rest.split(/\s/, 1)[0]);
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
const [whole, flag, value] = match;
|
|
86
|
+
if (flag === "-m" || flag === "--model") out.model = value;
|
|
87
|
+
else if (flag === "--aspect") {
|
|
88
|
+
if (!IMAGE_ASPECTS.includes(value)) throw new MediaError("invalid-aspect", value);
|
|
89
|
+
out.aspect = value;
|
|
90
|
+
} else {
|
|
91
|
+
const count = /^\d+$/.test(value) ? Number(value) : NaN;
|
|
92
|
+
if (intIn(count, IMAGE_COUNT) === undefined) throw new MediaError("invalid-count", value);
|
|
93
|
+
out.count = count;
|
|
94
|
+
}
|
|
95
|
+
rest = rest.slice(whole.length).trim();
|
|
96
|
+
}
|
|
97
|
+
// `literal` (agent tool calls): the prompt is taken as written.
|
|
98
|
+
const natural = {};
|
|
99
|
+
if (!literal) rest = takeNaturalOptions(rest, "image", natural);
|
|
100
|
+
if (out.aspect === undefined && natural.aspect !== undefined) {
|
|
101
|
+
if (!IMAGE_ASPECTS.includes(natural.aspect)) throw new MediaError("invalid-aspect", natural.aspect);
|
|
102
|
+
out.aspect = natural.aspect;
|
|
103
|
+
}
|
|
104
|
+
if (out.count === undefined && natural.count !== undefined) {
|
|
105
|
+
if (intIn(natural.count, IMAGE_COUNT) === undefined) throw new MediaError("invalid-count", String(natural.count));
|
|
106
|
+
out.count = natural.count;
|
|
107
|
+
}
|
|
108
|
+
if (rest === "") throw new MediaError("missing-prompt");
|
|
109
|
+
return { ...out, prompt: rest };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Parse `/cpa-video` input: leading options, then the prompt verbatim.
|
|
114
|
+
* Options: `-m|--model <id>`, `--aspect <ratio>`, `-s|--seconds <1-15>`,
|
|
115
|
+
* `-r|--resolution <480p|720p>`; `--` ends options.
|
|
116
|
+
* @throws MediaError `missing-prompt` / `invalid-option` / `invalid-aspect` /
|
|
117
|
+
* `invalid-video-seconds` / `invalid-video-resolution`.
|
|
118
|
+
*/
|
|
119
|
+
export function parseVideoCommand(rawInput, { literal = false } = {}) {
|
|
120
|
+
let rest = String(rawInput ?? "").trim();
|
|
121
|
+
const out = {};
|
|
122
|
+
for (;;) {
|
|
123
|
+
if (rest.startsWith("-- ") || rest === "--") {
|
|
124
|
+
rest = rest.slice(2).trim();
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const toggle = rest.match(/^(--ref|--first-last|--last)(?:\s+|$)/);
|
|
128
|
+
if (toggle !== null) {
|
|
129
|
+
out.frames = { "--ref": "reference", "--first-last": "first-last", "--last": "last" }[toggle[1]];
|
|
130
|
+
rest = rest.slice(toggle[0].length).trim();
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const match = rest.match(/^(-m|--model|--aspect|-s|--seconds|-r|--resolution)(?:=|\s+)(\S+)(?:\s+|$)/);
|
|
134
|
+
if (match === null) {
|
|
135
|
+
if (/^--?[A-Za-z]/.test(rest)) throw new MediaError("invalid-option", rest.split(/\s/, 1)[0]);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
const [whole, flag, value] = match;
|
|
139
|
+
if (flag === "-m" || flag === "--model") out.model = value;
|
|
140
|
+
else if (flag === "--aspect") {
|
|
141
|
+
if (!VIDEO_ASPECTS.includes(value)) throw new MediaError("invalid-aspect", value);
|
|
142
|
+
out.aspect = value;
|
|
143
|
+
} else if (flag === "-s" || flag === "--seconds") {
|
|
144
|
+
const seconds = /^\d+$/.test(value) ? Number(value) : NaN;
|
|
145
|
+
if (intIn(seconds, VIDEO_SECONDS) === undefined) throw new MediaError("invalid-video-seconds", value);
|
|
146
|
+
out.seconds = seconds;
|
|
147
|
+
} else {
|
|
148
|
+
const resolution = value.toLowerCase();
|
|
149
|
+
if (!VIDEO_RESOLUTIONS.includes(resolution)) throw new MediaError("invalid-video-resolution", value);
|
|
150
|
+
out.resolution = resolution;
|
|
151
|
+
}
|
|
152
|
+
rest = rest.slice(whole.length).trim();
|
|
153
|
+
}
|
|
154
|
+
const natural = {};
|
|
155
|
+
if (!literal) rest = takeNaturalOptions(rest, "video", natural);
|
|
156
|
+
if (out.seconds === undefined && natural.seconds !== undefined) {
|
|
157
|
+
if (intIn(natural.seconds, VIDEO_SECONDS) === undefined) throw new MediaError("invalid-video-seconds", String(natural.seconds));
|
|
158
|
+
out.seconds = natural.seconds;
|
|
159
|
+
}
|
|
160
|
+
if (out.resolution === undefined && natural.resolution !== undefined) {
|
|
161
|
+
if (!VIDEO_RESOLUTIONS.includes(natural.resolution)) throw new MediaError("invalid-video-resolution", natural.resolution);
|
|
162
|
+
out.resolution = natural.resolution;
|
|
163
|
+
}
|
|
164
|
+
if (out.aspect === undefined && natural.aspect !== undefined) {
|
|
165
|
+
if (!VIDEO_ASPECTS.includes(natural.aspect)) throw new MediaError("invalid-aspect", natural.aspect);
|
|
166
|
+
out.aspect = natural.aspect;
|
|
167
|
+
}
|
|
168
|
+
if (out.frames === undefined && natural.frames !== undefined) out.frames = natural.frames;
|
|
169
|
+
if (rest === "") throw new MediaError("missing-prompt");
|
|
170
|
+
return { ...out, prompt: rest };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Parse `/cpa-video-edit` and `/cpa-video-extend` input. One source at most:
|
|
175
|
+
* `--from <command id>` (a video this plugin made in the session),
|
|
176
|
+
* `--video <path>` (a local MP4; quote a path with spaces), `--url <http(s) URL>`
|
|
177
|
+
* (a public video xAI downloads itself) — or an attached MP4, or by default the
|
|
178
|
+
* session's newest video. `-m|--model <id>` picks the account for a local or
|
|
179
|
+
* URL source; extensions take `-s|--seconds <2-10>` (also as a plain word).
|
|
180
|
+
* @throws MediaError `missing-prompt` / `invalid-option` / `invalid-extend-seconds` /
|
|
181
|
+
* `invalid-video-url` / `one-video-source`.
|
|
182
|
+
*/
|
|
183
|
+
export function parseVideoEditCommand(rawInput, mode, { literal = false } = {}) {
|
|
184
|
+
let rest = String(rawInput ?? "").trim();
|
|
185
|
+
const out = {};
|
|
186
|
+
for (;;) {
|
|
187
|
+
if (rest.startsWith("-- ") || rest === "--") {
|
|
188
|
+
rest = rest.slice(2).trim();
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
const match = rest.match(/^(--from|--video|--url|-m|--model|-s|--seconds)(?:=|\s+)(?:"([^"]+)"|(\S+))(?:\s+|$)/);
|
|
192
|
+
if (match === null) {
|
|
193
|
+
if (/^--?[A-Za-z]/.test(rest)) throw new MediaError("invalid-option", rest.split(/\s/, 1)[0]);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
const [whole, flag, quoted, bare] = match;
|
|
197
|
+
const value = quoted ?? bare;
|
|
198
|
+
if (flag === "--from" || flag === "--video" || flag === "--url") {
|
|
199
|
+
if (out.from !== undefined || out.video !== undefined || out.url !== undefined) throw new MediaError("one-video-source");
|
|
200
|
+
}
|
|
201
|
+
if (flag === "--from") {
|
|
202
|
+
if (validCommandId(value) === undefined) throw new MediaError("invalid-option", `--from ${value}`);
|
|
203
|
+
out.from = value;
|
|
204
|
+
} else if (flag === "--video") {
|
|
205
|
+
out.video = value;
|
|
206
|
+
} else if (flag === "--url") {
|
|
207
|
+
let url;
|
|
208
|
+
try {
|
|
209
|
+
url = new URL(value);
|
|
210
|
+
} catch {
|
|
211
|
+
throw new MediaError("invalid-video-url");
|
|
212
|
+
}
|
|
213
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username !== "" || url.password !== "") throw new MediaError("invalid-video-url");
|
|
214
|
+
out.url = url.href;
|
|
215
|
+
} else if (flag === "-m" || flag === "--model") {
|
|
216
|
+
out.model = value;
|
|
217
|
+
} else {
|
|
218
|
+
if (mode !== "extend") throw new MediaError("invalid-option", flag);
|
|
219
|
+
const seconds = /^\d+$/.test(value) ? Number(value) : NaN;
|
|
220
|
+
if (intIn(seconds, EXTEND_SECONDS) === undefined) throw new MediaError("invalid-extend-seconds", value);
|
|
221
|
+
out.seconds = seconds;
|
|
222
|
+
}
|
|
223
|
+
rest = rest.slice(whole.length).trim();
|
|
224
|
+
}
|
|
225
|
+
// A video path written straight into the prompt (with or without quotes or
|
|
226
|
+
// DSH's `@` file mention) is the source, as if passed with --video.
|
|
227
|
+
const named = () => out.from !== undefined || out.video !== undefined || out.url !== undefined;
|
|
228
|
+
const takePath = (re) => {
|
|
229
|
+
if (named()) return;
|
|
230
|
+
const m = re.exec(rest);
|
|
231
|
+
if (m === null) return;
|
|
232
|
+
out.video = m[1] ?? m[2] ?? m[3];
|
|
233
|
+
rest = (rest.slice(0, m.index) + rest.slice(m.index + m[0].length)).replace(SEPARATORS, "").replace(/[\s,,、;;]+$/, "");
|
|
234
|
+
};
|
|
235
|
+
const natural = {};
|
|
236
|
+
const kind = mode === "extend" ? "extend" : "edit";
|
|
237
|
+
if (!literal) {
|
|
238
|
+
takePath(VIDEO_PATH_HEAD);
|
|
239
|
+
rest = takeNaturalOptions(rest, kind, natural);
|
|
240
|
+
takePath(VIDEO_PATH_HEAD);
|
|
241
|
+
takePath(VIDEO_PATH_TAIL);
|
|
242
|
+
rest = takeNaturalOptions(rest, kind, natural);
|
|
243
|
+
}
|
|
244
|
+
if (mode === "extend" && out.seconds === undefined && natural.seconds !== undefined) {
|
|
245
|
+
if (intIn(natural.seconds, EXTEND_SECONDS) === undefined) throw new MediaError("invalid-extend-seconds", String(natural.seconds));
|
|
246
|
+
out.seconds = natural.seconds;
|
|
247
|
+
}
|
|
248
|
+
// Words the edit / extension routes cannot use are taken out of the prompt and reported.
|
|
249
|
+
const ignored = [
|
|
250
|
+
...(mode === "edit" && natural.seconds !== undefined ? [`${natural.seconds}s`] : []),
|
|
251
|
+
...(natural.resolution !== undefined ? [natural.resolution] : []),
|
|
252
|
+
...(natural.aspect !== undefined ? [natural.aspect] : []),
|
|
253
|
+
];
|
|
254
|
+
if (ignored.length > 0) out.ignored = ignored;
|
|
255
|
+
if (rest === "") throw new MediaError("missing-prompt");
|
|
256
|
+
return { ...out, prompt: rest };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const VIDEO_EXT = "(?:mp4|m4v|mov)";
|
|
260
|
+
// Quoted path; unquoted absolute path (may hold spaces, up to the first `.mp4`
|
|
261
|
+
// followed by a space); or one unquoted word ending in a video extension.
|
|
262
|
+
const VIDEO_PATH_HEAD = new RegExp(
|
|
263
|
+
`^@?(?:"([^"]+\\.${VIDEO_EXT})"|((?:[A-Za-z]:[\\\\/]|\\\\\\\\|/)[^\\n]*?\\.${VIDEO_EXT})|([^\\s"]+\\.${VIDEO_EXT}))(?=[\\s,,、;;]|$)`,
|
|
264
|
+
"i"
|
|
265
|
+
);
|
|
266
|
+
const VIDEO_PATH_TAIL = new RegExp(
|
|
267
|
+
`(?:^|[\\s,,、;;])@?(?:"([^"]+\\.${VIDEO_EXT})"|((?:[A-Za-z]:[\\\\/]|\\\\\\\\)[^\\n"]*?\\.${VIDEO_EXT})|([^\\s"]+\\.${VIDEO_EXT}))$`,
|
|
268
|
+
"i"
|
|
269
|
+
);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Shared media limits, option domains and identifier patterns.
|
|
2
|
+
export const MEDIA_SETTINGS_VERSION = 1;
|
|
3
|
+
export const VIDEO_SECONDS = { min: 1, max: 15 };
|
|
4
|
+
export const VIDEO_RESOLUTIONS = ["480p", "720p"];
|
|
5
|
+
export const BUDGET_SECONDS = { min: 1, max: 3600 };
|
|
6
|
+
export const CONFIRM_MODES = ["ask", "auto"];
|
|
7
|
+
const MAX_OUTPUT_DIR = 260;
|
|
8
|
+
const MAX_LIST = 500;
|
|
9
|
+
const ACCOUNT_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
10
|
+
|
|
11
|
+
export const MAX_RUNS = 48;
|
|
12
|
+
export const VIDEO_MODES = ["text", "first-frame", "reference", "first-last", "last-frame", "edit", "extend"];
|
|
13
|
+
|
|
14
|
+
export const IMAGE_ASPECTS = ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "9:20", "20:9"];
|
|
15
|
+
export const IMAGE_COUNT = { min: 1, max: 4 };
|
|
16
|
+
export const MAX_REFERENCE_IMAGES = 4;
|
|
17
|
+
|
|
18
|
+
export const VIDEO_ASPECTS = ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3"];
|
|
19
|
+
export const MAX_VIDEO_BYTES = 200 * 1024 * 1024;
|
|
20
|
+
const VIDEO_REQUEST_TIMEOUT_MS = 60_000;
|
|
21
|
+
const VIDEO_DOWNLOAD_TIMEOUT_MS = 10 * 60_000;
|
|
22
|
+
const MAX_VIDEO_JSON_BYTES = 1024 * 1024;
|
|
23
|
+
/** CPA keeps the request → credential binding this long (`defaultVideoAuthBindingTTL`). */
|
|
24
|
+
export const VIDEO_JOB_TTL_MS = 3 * 60 * 60 * 1000;
|
|
25
|
+
const REQUEST_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,199}$/;
|
|
26
|
+
/**
|
|
27
|
+
* Request body limits measured on a live CPA → xAI path (Oct 2026, with
|
|
28
|
+
* requests that fail validation, so nothing was generated):
|
|
29
|
+
* `POST /v1/videos/generations` read 1 MiB but refused 1.2 MiB ("Failed to
|
|
30
|
+
* read request body: length limit exceeded"); `/v1/videos/edits` read 20 MiB.
|
|
31
|
+
*/
|
|
32
|
+
export const VIDEO_GENERATION_BODY_MAX_BYTES = 1024 * 1024;
|
|
33
|
+
export const VIDEO_UPLOAD_BODY_MAX_BYTES = 20 * 1024 * 1024;
|
|
34
|
+
/** Largest local video sent inline as a data URI (base64 adds a third). */
|
|
35
|
+
export const VIDEO_UPLOAD_MAX_BYTES = 14 * 1024 * 1024;
|
|
36
|
+
|
|
37
|
+
export const EXTEND_SECONDS = { min: 2, max: 10 };
|
|
38
|
+
/** xAI's limits on a source video: edits take at most 8.7 s, extensions 2–15 s. */
|
|
39
|
+
export const EDIT_SOURCE_MAX_SECONDS = 8.7;
|
|
40
|
+
export const EXTEND_SOURCE_SECONDS = { min: 2, max: 15 };
|
|
41
|
+
/** xAI runs edits and extensions only on this model. */
|
|
42
|
+
export const VIDEO_EDIT_MODEL = "grok-imagine-video";
|
|
43
|
+
|
|
44
|
+
export const REFERENCE_MAX_EDGE = 1280;
|
|
45
|
+
|
|
46
|
+
export { MAX_LIST, MAX_OUTPUT_DIR, ACCOUNT_ID_PATTERN, REQUEST_ID_PATTERN, VIDEO_REQUEST_TIMEOUT_MS, VIDEO_DOWNLOAD_TIMEOUT_MS, MAX_VIDEO_JSON_BYTES };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Read-only catalog summaries and unauthenticated route probes.
|
|
2
|
+
import { detectMediaKind } from "./classify.js";
|
|
3
|
+
import { plain, readBoundedText } from "./util.js";
|
|
4
|
+
|
|
5
|
+
// #region Read-only diagnostic
|
|
6
|
+
|
|
7
|
+
const DIAG_FIELDS = [
|
|
8
|
+
"type",
|
|
9
|
+
"object",
|
|
10
|
+
"owned_by",
|
|
11
|
+
"supportedOutputModalities",
|
|
12
|
+
"output_modalities",
|
|
13
|
+
"outputModalities",
|
|
14
|
+
"input_modalities",
|
|
15
|
+
"supportedInputModalities",
|
|
16
|
+
"display_name",
|
|
17
|
+
];
|
|
18
|
+
const MEDIA_HINT = /(image|imagine|video|sora|veo|dall-?e)/i;
|
|
19
|
+
const DIAG_PROBE_ID = "dsh-diagnostic-probe";
|
|
20
|
+
|
|
21
|
+
function short(value) {
|
|
22
|
+
if (typeof value === "string") return value.slice(0, 120);
|
|
23
|
+
if (typeof value === "number" || typeof value === "boolean") return value;
|
|
24
|
+
if (Array.isArray(value)) return value.filter((v) => typeof v === "string").slice(0, 12).map((v) => v.slice(0, 40));
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function catalogEntries(payload) {
|
|
29
|
+
if (Array.isArray(payload?.models)) return payload.models;
|
|
30
|
+
if (Array.isArray(payload?.data)) return payload.data;
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Media-looking catalog entries with a fixed set of descriptive fields (no other values). */
|
|
35
|
+
export function summarizeCatalogForMedia(payload) {
|
|
36
|
+
const entries = catalogEntries(payload);
|
|
37
|
+
if (entries === undefined) return undefined;
|
|
38
|
+
const media = [];
|
|
39
|
+
for (const item of entries) {
|
|
40
|
+
if (plain(item) === undefined) continue;
|
|
41
|
+
const id = typeof (item.slug ?? item.id) === "string" ? String(item.slug ?? item.id).trim() : "";
|
|
42
|
+
if (id === "") continue;
|
|
43
|
+
const kind = detectMediaKind({
|
|
44
|
+
id,
|
|
45
|
+
catalogType: item.type,
|
|
46
|
+
outputModalities: item.supportedOutputModalities ?? item.output_modalities ?? item.outputModalities,
|
|
47
|
+
});
|
|
48
|
+
if (kind === "chat" && !MEDIA_HINT.test(id)) continue;
|
|
49
|
+
const fields = {};
|
|
50
|
+
for (const field of DIAG_FIELDS) {
|
|
51
|
+
const value = short(item[field]);
|
|
52
|
+
if (value !== undefined) fields[field] = value;
|
|
53
|
+
}
|
|
54
|
+
media.push({ id, kind, keys: Object.keys(item).sort().slice(0, 40), fields });
|
|
55
|
+
if (media.length >= 50) break;
|
|
56
|
+
}
|
|
57
|
+
return { count: entries.length, media };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Read-only CPA check for media support. Sends only GET requests:
|
|
62
|
+
* - the enriched (`?client_version=pi`) and plain `/v1/models` catalogs,
|
|
63
|
+
* with the account key, reporting media-looking entries and their fields;
|
|
64
|
+
* - two route probes **without** the key, so CPA answers 401 for a route
|
|
65
|
+
* that exists and its router's plain-text 404 for one that does not,
|
|
66
|
+
* without selecting a credential or contacting an upstream.
|
|
67
|
+
* Never returns the key, response bodies or upstream URLs.
|
|
68
|
+
*/
|
|
69
|
+
export async function diagnoseMedia({ fetch: fetchImpl = fetch, rootURL, apiKey, timeoutMs = 8000, signal }) {
|
|
70
|
+
const timed = () => {
|
|
71
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
72
|
+
return signal === undefined ? timeout : AbortSignal.any([signal, timeout]);
|
|
73
|
+
};
|
|
74
|
+
const authHeaders = {
|
|
75
|
+
Accept: "application/json",
|
|
76
|
+
...(typeof apiKey === "string" && apiKey !== "" ? { Authorization: `Bearer ${apiKey}` } : {}),
|
|
77
|
+
};
|
|
78
|
+
const catalog = async (path) => {
|
|
79
|
+
try {
|
|
80
|
+
const response = await fetchImpl(`${rootURL}${path}`, { method: "GET", headers: authHeaders, signal: timed() });
|
|
81
|
+
if (!response.ok) return { ok: false, status: response.status };
|
|
82
|
+
let payload;
|
|
83
|
+
try {
|
|
84
|
+
payload = JSON.parse(await readBoundedText(response));
|
|
85
|
+
} catch {
|
|
86
|
+
return { ok: false, status: response.status, error: "invalid-json" };
|
|
87
|
+
}
|
|
88
|
+
const summary = summarizeCatalogForMedia(payload);
|
|
89
|
+
return summary === undefined
|
|
90
|
+
? { ok: false, status: response.status, error: "no-model-list" }
|
|
91
|
+
: { ok: true, status: response.status, ...summary };
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (signal?.aborted) throw signal.reason;
|
|
94
|
+
return { ok: false, error: error?.name === "TimeoutError" ? "timeout" : "network" };
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const probe = async (label, path) => {
|
|
98
|
+
try {
|
|
99
|
+
const response = await fetchImpl(`${rootURL}${path}`, {
|
|
100
|
+
method: "GET",
|
|
101
|
+
headers: { Accept: "application/json" },
|
|
102
|
+
signal: timed(),
|
|
103
|
+
});
|
|
104
|
+
let text = "";
|
|
105
|
+
try {
|
|
106
|
+
text = (await readBoundedText(response)).trim();
|
|
107
|
+
} catch {}
|
|
108
|
+
const verdict =
|
|
109
|
+
response.status === 401 || response.status === 403
|
|
110
|
+
? "present"
|
|
111
|
+
: response.status === 404 && /^404 page not found$/i.test(text)
|
|
112
|
+
? "missing"
|
|
113
|
+
: "unknown";
|
|
114
|
+
return { route: label, status: response.status, verdict };
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (signal?.aborted) throw signal.reason;
|
|
117
|
+
return { route: label, verdict: "unreachable", error: error?.name === "TimeoutError" ? "timeout" : "network" };
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const [enriched, listing, nativeVideo, videoContent] = await Promise.all([
|
|
121
|
+
catalog("/v1/models?client_version=pi"),
|
|
122
|
+
catalog("/v1/models"),
|
|
123
|
+
probe("GET /v1/videos/{id}", `/v1/videos/${DIAG_PROBE_ID}`),
|
|
124
|
+
probe("GET /openai/v1/videos/{id}/content", `/openai/v1/videos/${DIAG_PROBE_ID}/content`),
|
|
125
|
+
]);
|
|
126
|
+
return {
|
|
127
|
+
endpoint: rootURL,
|
|
128
|
+
checkedAt: new Date().toISOString(),
|
|
129
|
+
catalogs: { enriched, listing },
|
|
130
|
+
routes: [nativeVideo, videoContent],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// #endregion
|