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,255 @@
|
|
|
1
|
+
// Static generate_media schema, argument validation, commands and approval text.
|
|
2
|
+
import { IMAGE_ASPECTS, VIDEO_ASPECTS, MAX_REFERENCE_IMAGES } from "./constants.js";
|
|
3
|
+
import { plain } from "./util.js";
|
|
4
|
+
import { validCommandId } from "./session-store.js";
|
|
5
|
+
|
|
6
|
+
// #region Agent tool (generate_media)
|
|
7
|
+
//
|
|
8
|
+
// Pure parts of the optional agent tool: its static definition (no model
|
|
9
|
+
// lists), argument checks, the command line it runs through the slash-command
|
|
10
|
+
// handlers, the run id derived from the tool call id, and the approval text.
|
|
11
|
+
|
|
12
|
+
export const GENERATE_MEDIA = "generate_media";
|
|
13
|
+
|
|
14
|
+
const MEDIA_ASPECTS_ALL = [...new Set([...IMAGE_ASPECTS, ...VIDEO_ASPECTS])];
|
|
15
|
+
|
|
16
|
+
// Responses strict schemas may require every property: null keeps optional and
|
|
17
|
+
// inapplicable arguments expressible without inventing a model, source or mode.
|
|
18
|
+
const optionalMediaArg = (schema, description) => ({
|
|
19
|
+
oneOf: [schema, { type: "null" }],
|
|
20
|
+
...(description === undefined ? {} : { description }),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** The tool's model-facing description and parameter schema (raw JSON Schema subset). */
|
|
24
|
+
export const GENERATE_MEDIA_TOOL = {
|
|
25
|
+
name: GENERATE_MEDIA,
|
|
26
|
+
description:
|
|
27
|
+
"Generate an image or video with the user's CLIProxyAPI media models, or edit / extend a video; saves the files under the working directory and returns their paths. Pass the user's prompt in their own words and level of detail: never expand it into a longer scene description or add camera, lighting, style, character or environment details they did not ask for. Videos are billed, cannot be cancelled and may need the user's approval; never retry one that was declined or over budget.",
|
|
28
|
+
parameters: {
|
|
29
|
+
type: "object",
|
|
30
|
+
properties: {
|
|
31
|
+
kind: { type: "string", enum: ["image", "video"] },
|
|
32
|
+
prompt: { type: "string", description: "The user's prompt, verbatim: their own words and level of detail. Do not rewrite, expand, translate or add style/camera/scene details." },
|
|
33
|
+
mode: optionalMediaArg({ type: "string", enum: ["generate", "edit", "extend"] }, "Video only; null = generate."),
|
|
34
|
+
model: optionalMediaArg({ type: "string" }, "Model id or unique part; null = user's pick."),
|
|
35
|
+
images: optionalMediaArg({ type: "array", items: { type: "string" } }, "Image paths, or null. Image: up to 4 to edit/combine. Video: 1 = first frame, 2+ = references."),
|
|
36
|
+
frames: optionalMediaArg({ type: "string", enum: ["first-frame", "reference", "first-last", "last-frame"] }, "Video images only; otherwise null. Pinned first-last/last-frame need grok-imagine-video-1.5."),
|
|
37
|
+
video: optionalMediaArg({ type: "string" }, "Edit/extend source: MP4 path or public URL; null = session's newest video. Other modes: null."),
|
|
38
|
+
aspect: optionalMediaArg({ type: "string", enum: MEDIA_ASPECTS_ALL }, "Image/video generation ratio, or null for default; edit/extend: null."),
|
|
39
|
+
seconds: optionalMediaArg({ type: "integer" }, "Video length 1–15, extend adds 2–10; null = default. Image/edit: null."),
|
|
40
|
+
resolution: optionalMediaArg({ type: "string", enum: ["480p", "720p"] }, "Video generation only; null = default or unused."),
|
|
41
|
+
count: optionalMediaArg({ type: "integer" }, "Images 1–4; null = 1. Video: null."),
|
|
42
|
+
},
|
|
43
|
+
required: ["kind", "prompt"],
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** The tool's result value schema (validated by DSH's tool registry). */
|
|
48
|
+
export const GENERATE_MEDIA_OUTPUT = {
|
|
49
|
+
type: "object",
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
properties: {
|
|
52
|
+
status: { type: "string", enum: ["done", "running"] },
|
|
53
|
+
kind: { type: "string" },
|
|
54
|
+
mode: { type: "string" },
|
|
55
|
+
model: { type: "string" },
|
|
56
|
+
account: { type: "string" },
|
|
57
|
+
run: { type: "string" },
|
|
58
|
+
files: { type: "array", items: { type: "string" } },
|
|
59
|
+
folder: { type: "string" },
|
|
60
|
+
seconds: { type: "integer" },
|
|
61
|
+
resolution: { type: "string" },
|
|
62
|
+
aspect: { type: "string" },
|
|
63
|
+
revisedPrompt: { type: "string" },
|
|
64
|
+
note: { type: "string" },
|
|
65
|
+
commandRow: { type: "boolean" },
|
|
66
|
+
previews: {
|
|
67
|
+
type: "array",
|
|
68
|
+
items: {
|
|
69
|
+
type: "object",
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
properties: {
|
|
72
|
+
attachmentId: { type: "string" },
|
|
73
|
+
mediaType: { type: "string" },
|
|
74
|
+
bytes: { type: "integer" },
|
|
75
|
+
width: { type: "integer" },
|
|
76
|
+
height: { type: "integer" },
|
|
77
|
+
name: { type: "string" },
|
|
78
|
+
originalDimensions: {
|
|
79
|
+
type: "object",
|
|
80
|
+
additionalProperties: false,
|
|
81
|
+
properties: { width: { type: "integer" }, height: { type: "integer" } },
|
|
82
|
+
required: ["width", "height"],
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
required: ["attachmentId", "mediaType", "bytes", "width", "height"],
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
required: ["status", "kind", "model", "run", "files"],
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Check one `generate_media` call's arguments.
|
|
94
|
+
* @returns the normalized arguments.
|
|
95
|
+
* @throws Error with a model-facing message.
|
|
96
|
+
*/
|
|
97
|
+
export function parseToolArgs(args) {
|
|
98
|
+
const input = plain(args);
|
|
99
|
+
if (input === undefined) throw new Error("arguments must be an object");
|
|
100
|
+
const known = new Set(Object.keys(GENERATE_MEDIA_TOOL.parameters.properties));
|
|
101
|
+
for (const key of Object.keys(input)) if (!known.has(key)) throw new Error(`unknown argument "${key}"`);
|
|
102
|
+
// Canonical strict-schema calls use null for absent optional fields. Also
|
|
103
|
+
// accept empty strings from older all-fields calls, without changing prompt.
|
|
104
|
+
const a = Object.fromEntries(Object.entries(input).filter(([key, value]) =>
|
|
105
|
+
key === "kind" || key === "prompt" || (value !== null && value !== undefined && !(typeof value === "string" && value.trim() === ""))
|
|
106
|
+
));
|
|
107
|
+
if (a.kind !== "image" && a.kind !== "video") throw new Error('kind must be "image" or "video"');
|
|
108
|
+
if (typeof a.prompt !== "string" || a.prompt.trim() === "") throw new Error("prompt must be a non-empty string");
|
|
109
|
+
if (a.prompt.length > 4000) throw new Error("prompt is longer than 4000 characters");
|
|
110
|
+
const str = (key) => {
|
|
111
|
+
if (a[key] === undefined) return undefined;
|
|
112
|
+
if (typeof a[key] !== "string" || a[key].trim() === "") throw new Error(`${key} must be a non-empty string`);
|
|
113
|
+
return a[key].trim();
|
|
114
|
+
};
|
|
115
|
+
const int = (key) => {
|
|
116
|
+
if (a[key] === undefined) return undefined;
|
|
117
|
+
if (!Number.isInteger(a[key])) throw new Error(`${key} must be an integer`);
|
|
118
|
+
return a[key];
|
|
119
|
+
};
|
|
120
|
+
const out = { kind: a.kind, prompt: a.prompt.trim() };
|
|
121
|
+
const model = str("model");
|
|
122
|
+
if (model !== undefined) {
|
|
123
|
+
if (/\s/.test(model)) throw new Error("model must not contain spaces");
|
|
124
|
+
out.model = model;
|
|
125
|
+
}
|
|
126
|
+
if (a.images !== undefined) {
|
|
127
|
+
if (!Array.isArray(a.images) || a.images.some((p) => typeof p !== "string" || p.trim() === "")) throw new Error("images must be an array of paths");
|
|
128
|
+
if (a.images.length > 14) throw new Error("at most 14 images");
|
|
129
|
+
if (a.images.length > 0) out.images = a.images.map((p) => p.trim());
|
|
130
|
+
}
|
|
131
|
+
const aspect = str("aspect");
|
|
132
|
+
if (aspect !== undefined) out.aspect = aspect;
|
|
133
|
+
if (a.kind === "image") {
|
|
134
|
+
// Older strict calls supplied these neutral defaults even for images.
|
|
135
|
+
if (a.mode === "generate") delete a.mode;
|
|
136
|
+
if (a.frames === "first-frame") delete a.frames;
|
|
137
|
+
for (const key of ["mode", "frames", "video", "seconds", "resolution"]) if (a[key] !== undefined) throw new Error(`${key} is for videos only`);
|
|
138
|
+
const count = int("count");
|
|
139
|
+
if (count !== undefined && (count < 1 || count > 4)) throw new Error("count must be 1–4 for images");
|
|
140
|
+
if (count !== undefined) out.count = count;
|
|
141
|
+
if (aspect !== undefined && !IMAGE_ASPECTS.includes(aspect)) throw new Error(`aspect must be one of ${IMAGE_ASPECTS.join(", ")} for images`);
|
|
142
|
+
if ((out.images?.length ?? 0) > MAX_REFERENCE_IMAGES) throw new Error(`at most ${MAX_REFERENCE_IMAGES} images for an image`);
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
// One video is already the only supported behavior: legacy count: 1 is a
|
|
146
|
+
// harmless default, but an actual request for multiple videos is rejected.
|
|
147
|
+
if (a.count !== undefined && a.count !== 1) throw new Error("count is for images only; use null for a video (one video per call)");
|
|
148
|
+
const mode = str("mode") ?? "generate";
|
|
149
|
+
if (!["generate", "edit", "extend"].includes(mode)) throw new Error('mode must be "generate", "edit" or "extend"');
|
|
150
|
+
out.mode = mode;
|
|
151
|
+
const seconds = int("seconds");
|
|
152
|
+
if (seconds !== undefined) out.seconds = seconds;
|
|
153
|
+
if (mode === "generate") {
|
|
154
|
+
if (a.video !== undefined) throw new Error("video is for mode edit / extend");
|
|
155
|
+
if (seconds !== undefined && (seconds < 1 || seconds > 15)) throw new Error("seconds must be 1–15 for video generation");
|
|
156
|
+
if (aspect !== undefined && !VIDEO_ASPECTS.includes(aspect)) throw new Error(`aspect must be one of ${VIDEO_ASPECTS.join(", ")} for videos`);
|
|
157
|
+
const resolution = str("resolution");
|
|
158
|
+
if (resolution !== undefined && !["480p", "720p"].includes(resolution)) throw new Error("resolution must be 480p or 720p");
|
|
159
|
+
if (resolution !== undefined) out.resolution = resolution;
|
|
160
|
+
const frames = str("frames");
|
|
161
|
+
if (frames !== undefined) {
|
|
162
|
+
if (!["first-frame", "reference", "first-last", "last-frame"].includes(frames)) throw new Error("frames must be first-frame, reference, first-last or last-frame");
|
|
163
|
+
const count = out.images?.length ?? 0;
|
|
164
|
+
// A legacy all-fields text-video call supplied first-frame with no images.
|
|
165
|
+
// It is still text-to-video, not an implicit image or a guessed reference.
|
|
166
|
+
if (count === 0 && frames !== "first-frame") throw new Error("frames needs images; use null without images");
|
|
167
|
+
if (count > 0) {
|
|
168
|
+
if ((frames === "first-frame" || frames === "last-frame") && count !== 1) throw new Error(`${frames} needs exactly one image`);
|
|
169
|
+
if (frames === "first-last" && count !== 2) throw new Error("first-last needs exactly two images");
|
|
170
|
+
out.frames = frames;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
if (a.images?.length === 0) delete a.images;
|
|
176
|
+
if (a.frames === "first-frame") delete a.frames;
|
|
177
|
+
for (const key of ["images", "frames", "aspect", "resolution"]) if (a[key] !== undefined) throw new Error(`${key} is not used by mode ${mode} (the output keeps the source's format)`);
|
|
178
|
+
if (mode === "edit" && seconds !== undefined) throw new Error("an edit keeps the source's length; seconds is for extend");
|
|
179
|
+
if (mode === "extend" && seconds !== undefined && (seconds < 2 || seconds > 10)) throw new Error("seconds must be 2–10 for extend");
|
|
180
|
+
const video = str("video");
|
|
181
|
+
if (video !== undefined) out.video = video;
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The slash-command line (after the command name) that runs these arguments, taken literally. */
|
|
186
|
+
export function toolCommandLine(t) {
|
|
187
|
+
const parts = [];
|
|
188
|
+
if (t.model !== undefined) parts.push(`-m ${t.model}`);
|
|
189
|
+
if (t.kind === "image") {
|
|
190
|
+
if (t.aspect !== undefined) parts.push(`--aspect ${t.aspect}`);
|
|
191
|
+
if (t.count !== undefined) parts.push(`-n ${t.count}`);
|
|
192
|
+
} else if (t.mode === "generate") {
|
|
193
|
+
if (t.aspect !== undefined) parts.push(`--aspect ${t.aspect}`);
|
|
194
|
+
if (t.seconds !== undefined) parts.push(`-s ${t.seconds}`);
|
|
195
|
+
if (t.resolution !== undefined) parts.push(`-r ${t.resolution}`);
|
|
196
|
+
const flag = { reference: "--ref", "first-last": "--first-last", "last-frame": "--last" }[t.frames];
|
|
197
|
+
if (flag !== undefined) parts.push(flag);
|
|
198
|
+
} else {
|
|
199
|
+
if (t.video !== undefined) {
|
|
200
|
+
if (/^https?:\/\//i.test(t.video)) parts.push(`--url ${t.video.replace(/\s/g, "%20")}`);
|
|
201
|
+
else {
|
|
202
|
+
if (t.video.includes('"')) throw new Error("video path must not contain a double quote");
|
|
203
|
+
parts.push(`--video "${t.video}"`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (t.mode === "extend" && t.seconds !== undefined) parts.push(`-s ${t.seconds}`);
|
|
207
|
+
}
|
|
208
|
+
parts.push("--", t.prompt);
|
|
209
|
+
return parts.join(" ");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The run id (a chat-row key) for one tool call: the call id with other characters replaced. */
|
|
213
|
+
export function toolRunId(callId) {
|
|
214
|
+
const raw = String(callId ?? "").replace(/[^A-Za-z0-9._:-]/g, "_").replace(/^[^A-Za-z0-9]+/, "").slice(0, 119);
|
|
215
|
+
return validCommandId(raw);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* What the approval prompt says about one generation (both languages).
|
|
220
|
+
* @param plan - from the command's gate: kind, mode, option, prompt, seconds, resolution, aspect, references, count, sourceName.
|
|
221
|
+
* @param policy - `agentPolicy()` of the root session (videos show the budget).
|
|
222
|
+
*/
|
|
223
|
+
export function approvalText(plan, policy) {
|
|
224
|
+
const prompt = plan.prompt.length > 80 ? `${plan.prompt.slice(0, 80)}…` : plan.prompt;
|
|
225
|
+
const model = `${plan.option.modelId} (${plan.option.accountName})`;
|
|
226
|
+
const modelZh = `${plan.option.modelId}(${plan.option.accountName})`;
|
|
227
|
+
if (plan.kind === "image") {
|
|
228
|
+
const n = plan.count ?? 1;
|
|
229
|
+
return {
|
|
230
|
+
reason: `generate ${n} image(s) with ${plan.option.ref}`,
|
|
231
|
+
en: `${plan.references > 0 ? `Edit ${plan.references} image(s) into` : "Generate"} ${n} image(s) with ${model}: "${prompt}".`,
|
|
232
|
+
zh: `用 ${modelZh}${plan.references > 0 ? `改 ${plan.references} 张图,` : ""}生成 ${n} 张图片:「${prompt}」。`,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
const used = policy.limit === undefined ? `${policy.used} s (no limit)` : `${policy.used} of ${policy.limit} s`;
|
|
236
|
+
const usedZh = policy.limit === undefined ? `${policy.used} 秒(不限)` : `${policy.used} / ${policy.limit} 秒`;
|
|
237
|
+
const shape = [plan.resolution, plan.aspect].filter(Boolean).join(", ");
|
|
238
|
+
const what =
|
|
239
|
+
plan.mode === "edit" ? `Edit the video${plan.sourceName ? ` ${plan.sourceName}` : ""}`
|
|
240
|
+
: plan.mode === "extend" ? `Extend the video${plan.sourceName ? ` ${plan.sourceName}` : ""} by ${plan.seconds} s`
|
|
241
|
+
: `Generate a ${plan.seconds} s video${shape ? ` (${shape})` : ""}`;
|
|
242
|
+
const whatZh =
|
|
243
|
+
plan.mode === "edit" ? `编辑视频${plan.sourceName ? ` ${plan.sourceName}` : ""}`
|
|
244
|
+
: plan.mode === "extend" ? `把视频${plan.sourceName ? ` ${plan.sourceName}` : ""}延长 ${plan.seconds} 秒`
|
|
245
|
+
: `生成 ${plan.seconds} 秒视频${shape ? `(${shape})` : ""}`;
|
|
246
|
+
const refs = plan.references > 0 ? ` with ${plan.references} attached image(s)` : "";
|
|
247
|
+
const refsZh = plan.references > 0 ? `,附 ${plan.references} 张图` : "";
|
|
248
|
+
return {
|
|
249
|
+
reason: `${plan.mode ?? "generate"} a ${plan.seconds} s video with ${plan.option.ref}`,
|
|
250
|
+
en: `${what} with ${model}${refs}: "${prompt}". It is billed and cannot be cancelled once submitted. Agent videos in this session so far: ${used}.`,
|
|
251
|
+
zh: `用 ${modelZh}${whatZh}${refsZh}:「${prompt}」。提交后无法取消,会照常计费。本会话 agent 已用视频 ${usedZh}。`,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// #endregion
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Enabled generation options and unambiguous model selection.
|
|
2
|
+
import { mediaModelEnabled } from "./settings.js";
|
|
3
|
+
import { MediaError } from "./util.js";
|
|
4
|
+
|
|
5
|
+
// #region Model choice
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Generation models a user may pick: enabled models of the given kind on
|
|
9
|
+
* enabled accounts. `ref` is `<accountId>/<modelId>` (an account id never
|
|
10
|
+
* contains a slash; a model id may).
|
|
11
|
+
* @param accounts - `controller.mediaAccounts()` entries.
|
|
12
|
+
*/
|
|
13
|
+
export function mediaOptions(accounts, settings, kind) {
|
|
14
|
+
const out = [];
|
|
15
|
+
for (const account of accounts ?? []) {
|
|
16
|
+
if (account.enabled !== true) continue;
|
|
17
|
+
for (const model of account.models ?? []) {
|
|
18
|
+
if (model.kind !== kind || !mediaModelEnabled(settings, account.id, model.id)) continue;
|
|
19
|
+
out.push({
|
|
20
|
+
ref: `${account.id}/${model.id}`,
|
|
21
|
+
accountId: account.id,
|
|
22
|
+
accountName: account.displayName || account.id,
|
|
23
|
+
modelId: model.id,
|
|
24
|
+
...(model.name === undefined ? {} : { name: model.name }),
|
|
25
|
+
...(model.aliasOf === undefined ? {} : { aliasOf: model.aliasOf }),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Split a selection ref at its first slash. */
|
|
33
|
+
export function splitMediaRef(ref) {
|
|
34
|
+
const value = typeof ref === "string" ? ref.trim() : "";
|
|
35
|
+
const slash = value.indexOf("/");
|
|
36
|
+
return slash > 0 && slash < value.length - 1
|
|
37
|
+
? { accountId: value.slice(0, slash), modelId: value.slice(slash + 1) }
|
|
38
|
+
: undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Match what a user typed (`-m`): a full ref, then an exact model id, then a
|
|
43
|
+
* unique case-insensitive substring of the model id. Never guesses between
|
|
44
|
+
* several candidates.
|
|
45
|
+
* @throws MediaError `model-not-found` / `model-ambiguous` (detail lists refs).
|
|
46
|
+
*/
|
|
47
|
+
export function matchMediaModel(options, requested) {
|
|
48
|
+
const wanted = String(requested ?? "").trim();
|
|
49
|
+
const lower = wanted.toLowerCase();
|
|
50
|
+
const byRef = options.find((o) => o.ref === wanted);
|
|
51
|
+
if (byRef !== undefined) return byRef;
|
|
52
|
+
const pick = (list) => {
|
|
53
|
+
if (list.length === 1) return list[0];
|
|
54
|
+
if (list.length > 1) throw new MediaError("model-ambiguous", list.map((o) => o.ref).join(", "));
|
|
55
|
+
return undefined;
|
|
56
|
+
};
|
|
57
|
+
const exact = pick(options.filter((o) => o.modelId.toLowerCase() === lower));
|
|
58
|
+
if (exact !== undefined) return exact;
|
|
59
|
+
const partial = pick(lower === "" ? [] : options.filter((o) => o.modelId.toLowerCase().includes(lower)));
|
|
60
|
+
if (partial !== undefined) return partial;
|
|
61
|
+
throw new MediaError("model-not-found", options.map((o) => o.ref).join(", "));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The model a generation uses: an explicit request, else the session's pick,
|
|
66
|
+
* else the last pick anywhere, else the only option.
|
|
67
|
+
* @returns `{ option, source }` with source `request | session | last | only`.
|
|
68
|
+
* @throws MediaError `no-media-model` or `choose-model` (several, none picked).
|
|
69
|
+
*/
|
|
70
|
+
export function chooseMediaModel({ options, requested, sessionRef, lastRef }) {
|
|
71
|
+
if (typeof requested === "string" && requested.trim() !== "") {
|
|
72
|
+
return { option: matchMediaModel(options, requested), source: "request" };
|
|
73
|
+
}
|
|
74
|
+
const session = options.find((o) => o.ref === sessionRef);
|
|
75
|
+
if (session !== undefined) return { option: session, source: "session" };
|
|
76
|
+
const last = options.find((o) => o.ref === lastRef);
|
|
77
|
+
if (last !== undefined) return { option: last, source: "last" };
|
|
78
|
+
if (options.length === 1) return { option: options[0], source: "only" };
|
|
79
|
+
if (options.length === 0) throw new MediaError("no-media-model");
|
|
80
|
+
throw new MediaError("choose-model", options.map((o) => o.ref).join(", "));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// #endregion
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Catalog media classification and CPA model aliases.
|
|
2
|
+
// #region Classification
|
|
3
|
+
|
|
4
|
+
export const MEDIA_KINDS = ["chat", "image", "video"];
|
|
5
|
+
|
|
6
|
+
// Families CLIProxyAPI routes to its image / video handlers
|
|
7
|
+
// (sdk/api/handlers/openai/openai_{images,videos}_handlers.go), matched by
|
|
8
|
+
// prefix: a catalog may list variants CPA does not accept yet (BCPA lists
|
|
9
|
+
// `grok-imagine-video-1.5-lite`; CPA 9e79552 accepts only the base, `-1.5`
|
|
10
|
+
// and `-1.5-preview`). Such a model still cannot chat, so it is a media
|
|
11
|
+
// model, and CPA's own error explains a refused generation. Compare against
|
|
12
|
+
// those handlers when CPA adds a family.
|
|
13
|
+
const CPA_IMAGE_PREFIXES = ["grok-imagine-image", "gpt-image-"];
|
|
14
|
+
const CPA_VIDEO_PREFIXES = ["grok-imagine-video"];
|
|
15
|
+
const SORA_ALIAS_TARGET = "grok-imagine-video";
|
|
16
|
+
|
|
17
|
+
/** The model id without an account / vendor prefix, lower-cased. */
|
|
18
|
+
export function bareModelId(id) {
|
|
19
|
+
return String(id ?? "").trim().toLowerCase().split("/").pop() ?? "";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isSoraAlias(bare) {
|
|
23
|
+
return bare === "sora-2" || bare.startsWith("sora-2-");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function lowerList(value) {
|
|
27
|
+
return Array.isArray(value)
|
|
28
|
+
? value.filter((v) => typeof v === "string").map((v) => v.trim().toLowerCase()).filter(Boolean)
|
|
29
|
+
: [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Kind of a parsed catalog model, from catalog fields first and CPA's own
|
|
34
|
+
* model families second. `catalogType` is the CPA registry `type`
|
|
35
|
+
* (`openai-image` marks image endpoints); `outputModalities` the declared
|
|
36
|
+
* output modalities.
|
|
37
|
+
* @returns one of {@link MEDIA_KINDS}.
|
|
38
|
+
*/
|
|
39
|
+
export function detectMediaKind(model) {
|
|
40
|
+
const type = String(model?.catalogType ?? "").trim().toLowerCase();
|
|
41
|
+
if (type === "openai-image" || /(^|[-_])image($|[-_])/.test(type)) return "image";
|
|
42
|
+
if (/(^|[-_])video($|[-_])/.test(type)) return "video";
|
|
43
|
+
const out = lowerList(model?.outputModalities);
|
|
44
|
+
if (out.includes("video")) return "video";
|
|
45
|
+
if (out.includes("image") && !out.includes("text")) return "image";
|
|
46
|
+
const bare = bareModelId(model?.id);
|
|
47
|
+
if (CPA_VIDEO_PREFIXES.some((p) => bare.startsWith(p)) || isSoraAlias(bare)) return "video";
|
|
48
|
+
if (CPA_IMAGE_PREFIXES.some((p) => bare.startsWith(p))) return "image";
|
|
49
|
+
return "chat";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The model CPA really calls for an alias id (`sora-2*` → Grok video), or undefined. */
|
|
53
|
+
export function mediaAliasTarget(id) {
|
|
54
|
+
return isSoraAlias(bareModelId(id)) ? SORA_ALIAS_TARGET : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Effective kind: a valid user override wins over detection. */
|
|
58
|
+
export function effectiveKind(model, overrides) {
|
|
59
|
+
const forced = overrides?.[model?.id];
|
|
60
|
+
if (MEDIA_KINDS.includes(forced)) return forced;
|
|
61
|
+
if (MEDIA_KINDS.includes(model?.detectedKind)) return model.detectedKind;
|
|
62
|
+
return detectMediaKind(model);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// #endregion
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Reference-image flattening, area downscaling and JPEG size fitting.
|
|
2
|
+
import { REFERENCE_MAX_EDGE } from "../constants.js";
|
|
3
|
+
import { sniffImageType } from "../images.js";
|
|
4
|
+
import { decodePng } from "./png.js";
|
|
5
|
+
import { encodeJpeg } from "./jpeg.js";
|
|
6
|
+
|
|
7
|
+
// #region Reference images (size fitting without dependencies)
|
|
8
|
+
//
|
|
9
|
+
// Upstreams cap the request body (xAI answers "Failed to read request body:
|
|
10
|
+
// length limit exceeded" near 2 MiB), and references travel base64-encoded
|
|
11
|
+
// inside the JSON. A reference over the byte budget is shrunk here: PNG is
|
|
12
|
+
// decoded with node:zlib, flattened onto white, scaled to fit, and encoded as
|
|
13
|
+
// baseline JPEG. Other formats over the budget are left to the caller (DSH's
|
|
14
|
+
// attachment store can derive a smaller version).
|
|
15
|
+
|
|
16
|
+
/** Flatten onto white and scale down (area average) so the long edge is at most `maxEdge`; returns RGB8. */
|
|
17
|
+
export function fitRgb({ width, height, rgba }, maxEdge) {
|
|
18
|
+
const scale = Math.min(1, maxEdge / Math.max(width, height));
|
|
19
|
+
const w = Math.max(1, Math.round(width * scale));
|
|
20
|
+
const h = Math.max(1, Math.round(height * scale));
|
|
21
|
+
const rgb = new Uint8Array(w * h * 3);
|
|
22
|
+
const fx = width / w;
|
|
23
|
+
const fy = height / h;
|
|
24
|
+
for (let y = 0; y < h; y += 1) {
|
|
25
|
+
const y0 = Math.floor(y * fy);
|
|
26
|
+
const y1 = Math.max(y0 + 1, Math.min(height, Math.floor((y + 1) * fy)));
|
|
27
|
+
for (let x = 0; x < w; x += 1) {
|
|
28
|
+
const x0 = Math.floor(x * fx);
|
|
29
|
+
const x1 = Math.max(x0 + 1, Math.min(width, Math.floor((x + 1) * fx)));
|
|
30
|
+
let r = 0;
|
|
31
|
+
let g = 0;
|
|
32
|
+
let bl = 0;
|
|
33
|
+
let n = 0;
|
|
34
|
+
for (let yy = y0; yy < y1; yy += 1) {
|
|
35
|
+
for (let xx = x0; xx < x1; xx += 1) {
|
|
36
|
+
const o = (yy * width + xx) * 4;
|
|
37
|
+
const a = rgba[o + 3] / 255;
|
|
38
|
+
r += rgba[o] * a + 255 * (1 - a);
|
|
39
|
+
g += rgba[o + 1] * a + 255 * (1 - a);
|
|
40
|
+
bl += rgba[o + 2] * a + 255 * (1 - a);
|
|
41
|
+
n += 1;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const d = (y * w + x) * 3;
|
|
45
|
+
rgb[d] = Math.round(r / n);
|
|
46
|
+
rgb[d + 1] = Math.round(g / n);
|
|
47
|
+
rgb[d + 2] = Math.round(bl / n);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { width: w, height: h, rgb };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A reference image within `maxBytes`: unchanged when it already fits; a PNG
|
|
55
|
+
* over the budget is re-encoded as JPEG (flattened onto white, long edge at
|
|
56
|
+
* most `maxEdge`, lower quality / size until it fits). Returns undefined for
|
|
57
|
+
* other formats over the budget (the caller may ask DSH for a smaller version).
|
|
58
|
+
* @param image - `{ mediaType, data }`.
|
|
59
|
+
* @returns `{ mediaType, data, resized }` or undefined.
|
|
60
|
+
*/
|
|
61
|
+
export function fitReferenceImage(image, maxBytes, maxEdge = REFERENCE_MAX_EDGE) {
|
|
62
|
+
const data = image.data instanceof Uint8Array ? image.data : new Uint8Array(image.data);
|
|
63
|
+
if (data.byteLength <= maxBytes) return { mediaType: image.mediaType ?? sniffImageType(data) ?? "image/png", data, resized: false };
|
|
64
|
+
const decoded = sniffImageType(data) === "image/png" ? decodePng(data) : undefined;
|
|
65
|
+
if (decoded === undefined) return undefined;
|
|
66
|
+
for (const edge of [maxEdge, Math.round(maxEdge * 0.75), Math.round(maxEdge * 0.5), Math.round(maxEdge * 0.375)]) {
|
|
67
|
+
const fitted = fitRgb(decoded, edge);
|
|
68
|
+
for (const quality of [88, 78, 65]) {
|
|
69
|
+
const jpeg = encodeJpeg(fitted, quality);
|
|
70
|
+
if (jpeg.byteLength <= maxBytes) return { mediaType: "image/jpeg", data: jpeg, resized: true };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// #endregion
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Baseline JPEG encoding with standard quantization and Huffman tables.
|
|
2
|
+
const ZIGZAG = [0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63];
|
|
3
|
+
const STD_LUMA_Q = [16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99];
|
|
4
|
+
const STD_CHROMA_Q = [17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99];
|
|
5
|
+
// Annex K standard Huffman tables: code counts per length 1..16, then symbols.
|
|
6
|
+
const HUFF = {
|
|
7
|
+
dcL: [[0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
|
|
8
|
+
dcC: [[0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
|
|
9
|
+
acL: [[0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d], [
|
|
10
|
+
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
|
|
11
|
+
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
|
|
12
|
+
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
|
|
13
|
+
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
|
|
14
|
+
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
|
|
15
|
+
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
|
|
16
|
+
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
|
|
17
|
+
]],
|
|
18
|
+
acC: [[0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77], [
|
|
19
|
+
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
|
|
20
|
+
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
|
|
21
|
+
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
|
22
|
+
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
|
|
23
|
+
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
|
|
24
|
+
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
|
|
25
|
+
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
|
|
26
|
+
]],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function huffmanCodes([counts, symbols]) {
|
|
30
|
+
const codes = new Array(256);
|
|
31
|
+
let code = 0;
|
|
32
|
+
let k = 0;
|
|
33
|
+
for (let length = 1; length <= 16; length += 1) {
|
|
34
|
+
for (let i = 0; i < counts[length - 1]; i += 1) codes[symbols[k++]] = [code++, length];
|
|
35
|
+
code <<= 1;
|
|
36
|
+
}
|
|
37
|
+
return codes;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const COS = Array.from({ length: 8 }, (_, x) => Array.from({ length: 8 }, (_, u) => Math.cos(((2 * x + 1) * u * Math.PI) / 16)));
|
|
41
|
+
const C0 = 1 / Math.SQRT2;
|
|
42
|
+
|
|
43
|
+
/** Baseline JPEG (4:4:4, standard tables) from RGB8. */
|
|
44
|
+
export function encodeJpeg({ width, height, rgb }, quality = 85) {
|
|
45
|
+
const q = Math.min(100, Math.max(1, Math.round(quality)));
|
|
46
|
+
const factor = q < 50 ? 5000 / q : 200 - q * 2;
|
|
47
|
+
const table = (base) => base.map((v) => Math.min(255, Math.max(1, Math.floor((v * factor + 50) / 100))));
|
|
48
|
+
const qL = table(STD_LUMA_Q);
|
|
49
|
+
const qC = table(STD_CHROMA_Q);
|
|
50
|
+
const codes = { dcL: huffmanCodes(HUFF.dcL), dcC: huffmanCodes(HUFF.dcC), acL: huffmanCodes(HUFF.acL), acC: huffmanCodes(HUFF.acC) };
|
|
51
|
+
const out = [];
|
|
52
|
+
const bytes = (...values) => out.push(...values);
|
|
53
|
+
const marker = (code, payload) => {
|
|
54
|
+
bytes(0xff, code, ((payload.length + 2) >> 8) & 0xff, (payload.length + 2) & 0xff);
|
|
55
|
+
for (const v of payload) out.push(v);
|
|
56
|
+
};
|
|
57
|
+
bytes(0xff, 0xd8);
|
|
58
|
+
marker(0xe0, [0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]);
|
|
59
|
+
marker(0xdb, [0, ...ZIGZAG.map((i) => qL[i]), 1, ...ZIGZAG.map((i) => qC[i])]);
|
|
60
|
+
marker(0xc0, [8, (height >> 8) & 0xff, height & 0xff, (width >> 8) & 0xff, width & 0xff, 3, 1, 0x11, 0, 2, 0x11, 1, 3, 0x11, 1]);
|
|
61
|
+
const dht = [];
|
|
62
|
+
for (const [id, key] of [[0x00, "dcL"], [0x10, "acL"], [0x01, "dcC"], [0x11, "acC"]]) dht.push(id, ...HUFF[key][0], ...HUFF[key][1]);
|
|
63
|
+
marker(0xc4, dht);
|
|
64
|
+
marker(0xda, [3, 1, 0x00, 2, 0x11, 3, 0x11, 0, 63, 0]);
|
|
65
|
+
|
|
66
|
+
let buffer = 0;
|
|
67
|
+
let bits = 0;
|
|
68
|
+
const writeBits = (value, length) => {
|
|
69
|
+
buffer = (buffer << length) | (value & ((1 << length) - 1));
|
|
70
|
+
bits += length;
|
|
71
|
+
while (bits >= 8) {
|
|
72
|
+
const byte = (buffer >> (bits - 8)) & 0xff;
|
|
73
|
+
out.push(byte);
|
|
74
|
+
if (byte === 0xff) out.push(0);
|
|
75
|
+
bits -= 8;
|
|
76
|
+
}
|
|
77
|
+
buffer &= (1 << bits) - 1;
|
|
78
|
+
};
|
|
79
|
+
const category = (v) => {
|
|
80
|
+
let a = Math.abs(v);
|
|
81
|
+
let n = 0;
|
|
82
|
+
while (a > 0) {
|
|
83
|
+
n += 1;
|
|
84
|
+
a >>= 1;
|
|
85
|
+
}
|
|
86
|
+
return n;
|
|
87
|
+
};
|
|
88
|
+
const encodeValue = (v, n) => writeBits(v < 0 ? v + (1 << n) - 1 : v, n);
|
|
89
|
+
const block = new Float64Array(64);
|
|
90
|
+
const tmp = new Float64Array(64);
|
|
91
|
+
const coef = new Int32Array(64);
|
|
92
|
+
const prev = [0, 0, 0];
|
|
93
|
+
const encodeBlock = (component, quant, dcCodes, acCodes) => {
|
|
94
|
+
// Separable 2-D DCT-II.
|
|
95
|
+
for (let y = 0; y < 8; y += 1) {
|
|
96
|
+
for (let u = 0; u < 8; u += 1) {
|
|
97
|
+
let sum = 0;
|
|
98
|
+
for (let x = 0; x < 8; x += 1) sum += block[y * 8 + x] * COS[x][u];
|
|
99
|
+
tmp[y * 8 + u] = sum * (u === 0 ? C0 : 1) / 2;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (let u = 0; u < 8; u += 1) {
|
|
103
|
+
for (let v = 0; v < 8; v += 1) {
|
|
104
|
+
let sum = 0;
|
|
105
|
+
for (let y = 0; y < 8; y += 1) sum += tmp[y * 8 + u] * COS[y][v];
|
|
106
|
+
coef[v * 8 + u] = Math.round((sum * (v === 0 ? C0 : 1)) / 2 / quant[v * 8 + u]);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const dc = coef[0] - prev[component];
|
|
110
|
+
prev[component] = coef[0];
|
|
111
|
+
const dn = category(dc);
|
|
112
|
+
writeBits(dcCodes[dn][0], dcCodes[dn][1]);
|
|
113
|
+
if (dn > 0) encodeValue(dc, dn);
|
|
114
|
+
let run = 0;
|
|
115
|
+
for (let k = 1; k < 64; k += 1) {
|
|
116
|
+
const value = coef[ZIGZAG[k]];
|
|
117
|
+
if (value === 0) {
|
|
118
|
+
run += 1;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
while (run > 15) {
|
|
122
|
+
writeBits(acCodes[0xf0][0], acCodes[0xf0][1]);
|
|
123
|
+
run -= 16;
|
|
124
|
+
}
|
|
125
|
+
const n = category(value);
|
|
126
|
+
const symbol = (run << 4) | n;
|
|
127
|
+
writeBits(acCodes[symbol][0], acCodes[symbol][1]);
|
|
128
|
+
encodeValue(value, n);
|
|
129
|
+
run = 0;
|
|
130
|
+
}
|
|
131
|
+
if (run > 0) writeBits(acCodes[0][0], acCodes[0][1]);
|
|
132
|
+
};
|
|
133
|
+
for (let by = 0; by < height; by += 8) {
|
|
134
|
+
for (let bx = 0; bx < width; bx += 8) {
|
|
135
|
+
for (let component = 0; component < 3; component += 1) {
|
|
136
|
+
for (let y = 0; y < 8; y += 1) {
|
|
137
|
+
const sy = Math.min(height - 1, by + y);
|
|
138
|
+
for (let x = 0; x < 8; x += 1) {
|
|
139
|
+
const o = (sy * width + Math.min(width - 1, bx + x)) * 3;
|
|
140
|
+
const r = rgb[o];
|
|
141
|
+
const g = rgb[o + 1];
|
|
142
|
+
const b = rgb[o + 2];
|
|
143
|
+
const value =
|
|
144
|
+
component === 0 ? 0.299 * r + 0.587 * g + 0.114 * b
|
|
145
|
+
: component === 1 ? -0.168736 * r - 0.331264 * g + 0.5 * b + 128
|
|
146
|
+
: 0.5 * r - 0.418688 * g - 0.081312 * b + 128;
|
|
147
|
+
block[y * 8 + x] = value - 128;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (component === 0) encodeBlock(0, qL, codes.dcL, codes.acL);
|
|
151
|
+
else encodeBlock(component, qC, codes.dcC, codes.acC);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (bits > 0) writeBits(0x7f, 8 - bits);
|
|
156
|
+
bytes(0xff, 0xd9);
|
|
157
|
+
return new Uint8Array(out);
|
|
158
|
+
}
|