dsh-cliproxyapi-pro 1.8.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.
- package/CHANGELOG.md +23 -0
- package/README.en.md +38 -409
- package/README.md +38 -409
- package/lib/client.js +131 -28
- package/lib/host/accounts.js +176 -0
- package/lib/host/apply/agent-tool.js +406 -0
- package/lib/host/apply/commands.js +122 -0
- package/lib/host/apply/controller.js +105 -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 +34 -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 +205 -0
- package/lib/host/controller.js +435 -0
- package/lib/host/discovery.js +295 -0
- package/lib/host/http-gate.js +144 -0
- package/lib/host/models.js +102 -0
- package/lib/host/mount-guard.js +56 -0
- package/lib/host/routes.js +165 -0
- package/lib/host/runtime.js +445 -0
- package/lib/host/state-store.js +39 -0
- package/lib/host/status-text.js +93 -0
- package/lib/index.js +17 -4330
- 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 -2528
- package/lib/quota.js +0 -1646
- package/lib/updater.js +0 -443
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// Video generation admission, frame modes and interpreted submission parameters.
|
|
2
|
+
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { isAbsolute } from "node:path";
|
|
5
|
+
import { chooseMediaModel, MediaError, outputDirectory, validCommandId, validSessionId, parseVideoCommand, submitVideo, videoImageBudget } from "../../media/index.js";
|
|
6
|
+
|
|
7
|
+
export function createVideoCommand(shared) {
|
|
8
|
+
const { ctx, controller, mediaSessions, mediaChoicesFor, mediaAccountsSummary, FIRST_FRAME_MAX_BYTES, readReference, interpretParams, interpretedRecord, mediaFailureText, recordRun, videoLive, startVideoJob } = shared;
|
|
9
|
+
/** Grok's 1.5 video family takes more references and pinned first / last frames. */
|
|
10
|
+
const isVideo15 = (modelId) => /(^|\/)grok-imagine-video-1\.5/i.test(String(modelId ?? ""));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* How attached images are used: none = text only; one = first frame
|
|
14
|
+
* (`--ref`: a reference, `--last`: the last frame); two = references
|
|
15
|
+
* (`--first-last` / `--last`: first and last frame); more = references.
|
|
16
|
+
* @throws MediaError `frames-need-images` / `too-many-references` / `needs-video-15`.
|
|
17
|
+
*/
|
|
18
|
+
const videoMode = (count, frames, modelId) => {
|
|
19
|
+
if (count === 0) {
|
|
20
|
+
if (frames !== undefined) throw new MediaError("frames-need-images");
|
|
21
|
+
return "text";
|
|
22
|
+
}
|
|
23
|
+
let mode;
|
|
24
|
+
if (count === 1) mode = frames === "reference" ? "reference" : frames === "last" ? "last-frame" : frames === "first-last" ? undefined : "first-frame";
|
|
25
|
+
else if (count === 2) mode = frames === "first-last" || frames === "last" ? "first-last" : "reference";
|
|
26
|
+
else mode = frames === undefined || frames === "reference" ? "reference" : undefined;
|
|
27
|
+
if (mode === undefined) throw new MediaError(count === 1 ? "frames-need-images" : "too-many-references", count === 1 ? "2" : "2");
|
|
28
|
+
const maxRefs = isVideo15(modelId) ? 14 : 7;
|
|
29
|
+
if (mode === "reference" && count > maxRefs) throw new MediaError("too-many-references", String(maxRefs));
|
|
30
|
+
if ((mode === "first-last" || mode === "last-frame") && !isVideo15(modelId)) throw new MediaError("needs-video-15");
|
|
31
|
+
return mode;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* `/cpa-video`: submit a video — text only, from a first frame, from
|
|
36
|
+
* reference images, or between pinned first / last frames — and return at
|
|
37
|
+
* once; the chat row follows the background job.
|
|
38
|
+
*/
|
|
39
|
+
const runVideoCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
|
|
40
|
+
try {
|
|
41
|
+
const parsed = parseVideoCommand(rawInput, { literal });
|
|
42
|
+
const blocks = Array.isArray(attachments) ? attachments : [];
|
|
43
|
+
if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
|
|
44
|
+
if (blocks.length > 14) throw new MediaError("too-many-references", "14");
|
|
45
|
+
const { settings, options, accounts } = await mediaChoicesFor("video");
|
|
46
|
+
const sessionId = validSessionId(agent?.id);
|
|
47
|
+
const pickId = validSessionId(pickSession) ?? sessionId;
|
|
48
|
+
const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
|
|
49
|
+
let option;
|
|
50
|
+
try {
|
|
51
|
+
({ option } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
const mode = videoMode(blocks.length, parsed.frames, option.modelId);
|
|
57
|
+
const workingDirectory = ctx.get?.("workingDirectory");
|
|
58
|
+
const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
|
|
59
|
+
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
|
|
60
|
+
const images = [];
|
|
61
|
+
if (blocks.length > 0) {
|
|
62
|
+
const store = ctx.get?.("attachments");
|
|
63
|
+
if (store === undefined) throw new MediaError("unsupported-attachment");
|
|
64
|
+
// The generation route reads about 1 MiB of body (measured), so all images share that.
|
|
65
|
+
const each = Math.min(FIRST_FRAME_MAX_BYTES, videoImageBudget(parsed.prompt, blocks.length));
|
|
66
|
+
for (const block of blocks) images.push(await readReference(store, block, each, signal));
|
|
67
|
+
}
|
|
68
|
+
const framesFor = (m) =>
|
|
69
|
+
m === "first-frame" ? { image: images[0] }
|
|
70
|
+
: m === "last-frame" ? { lastFrame: images[0] }
|
|
71
|
+
: m === "first-last" ? { image: images[0], lastFrame: images[1] }
|
|
72
|
+
: m === "reference" ? { references: images }
|
|
73
|
+
: {};
|
|
74
|
+
const endpoint = await controller.mediaEndpoint(option.accountId);
|
|
75
|
+
const args = { ...parsed, seconds: parsed.seconds ?? settings.defaultVideoSeconds, resolution: parsed.resolution ?? settings.defaultVideoResolution };
|
|
76
|
+
const id = validCommandId(commandId);
|
|
77
|
+
if (gate !== undefined) {
|
|
78
|
+
await gate({ kind: "video", mode, option, prompt: args.prompt, seconds: args.seconds, resolution: args.resolution, aspect: args.aspect, references: images.length, cwd, outputDir: settings.outputDir });
|
|
79
|
+
}
|
|
80
|
+
recordRun(sessionId, id, {
|
|
81
|
+
kind: "video",
|
|
82
|
+
status: "running",
|
|
83
|
+
mode,
|
|
84
|
+
model: option.ref,
|
|
85
|
+
modelName: option.name || option.modelId,
|
|
86
|
+
accountName: option.accountName,
|
|
87
|
+
prompt: args.prompt,
|
|
88
|
+
...(args.aspect === undefined ? {} : { aspect: args.aspect }),
|
|
89
|
+
seconds: args.seconds,
|
|
90
|
+
resolution: args.resolution,
|
|
91
|
+
count: 1,
|
|
92
|
+
references: images.length,
|
|
93
|
+
files: 0,
|
|
94
|
+
startedAt: new Date().toISOString(),
|
|
95
|
+
});
|
|
96
|
+
void startVideoJob({
|
|
97
|
+
sessionId,
|
|
98
|
+
commandId: id,
|
|
99
|
+
accountId: option.accountId,
|
|
100
|
+
modelId: option.modelId,
|
|
101
|
+
ref: option.ref,
|
|
102
|
+
prompt: args.prompt,
|
|
103
|
+
seconds: args.seconds,
|
|
104
|
+
cwd,
|
|
105
|
+
outputDir: settings.outputDir,
|
|
106
|
+
label: "/cpa-video",
|
|
107
|
+
submit: async (jobSignal) => {
|
|
108
|
+
// The session's chat model reads what the rules did not (explicit options and recognized words win).
|
|
109
|
+
if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
|
|
110
|
+
const fixed = ["seconds", "resolution", "aspect", "frames"].filter((k) => parsed[k] !== undefined);
|
|
111
|
+
const result = literal ? undefined : await interpretParams({ agent, command: "video", text: parsed.prompt, images: images.length, fixed, signal: jobSignal });
|
|
112
|
+
let final = args;
|
|
113
|
+
let finalMode = mode;
|
|
114
|
+
if (result !== undefined) {
|
|
115
|
+
final = { ...args, ...result.values, prompt: result.prompt };
|
|
116
|
+
if (result.values.frames !== undefined) finalMode = videoMode(images.length, result.values.frames, option.modelId);
|
|
117
|
+
recordRun(sessionId, id, {
|
|
118
|
+
mode: finalMode,
|
|
119
|
+
prompt: final.prompt,
|
|
120
|
+
...(final.aspect === undefined ? {} : { aspect: final.aspect }),
|
|
121
|
+
seconds: final.seconds,
|
|
122
|
+
resolution: final.resolution,
|
|
123
|
+
...interpretedRecord(result, parsed.prompt),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
if (id !== undefined) videoLive.set(id, { phase: "submitting" });
|
|
127
|
+
const submitted = await submitVideo({
|
|
128
|
+
rootURL: endpoint.rootURL,
|
|
129
|
+
apiKey: endpoint.apiKey,
|
|
130
|
+
model: option.modelId,
|
|
131
|
+
prompt: final.prompt,
|
|
132
|
+
seconds: final.seconds,
|
|
133
|
+
...(final.aspect === undefined ? {} : { aspect: final.aspect }),
|
|
134
|
+
resolution: final.resolution,
|
|
135
|
+
...framesFor(finalMode),
|
|
136
|
+
signal: jobSignal,
|
|
137
|
+
});
|
|
138
|
+
return { ...submitted, prompt: final.prompt, seconds: final.seconds };
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
kind: "success",
|
|
143
|
+
text: `Submitting a ${args.seconds} s ${args.resolution} video (${mode}) to ${option.modelId} (${option.accountName}); a submitted video cannot be cancelled. Files go to ${outputDirectory(cwd, settings.outputDir)}`,
|
|
144
|
+
};
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (signal?.aborted || error?.gateError === true) throw error;
|
|
147
|
+
return { kind: "error", text: `/cpa-video: ${mediaFailureText(error, "video")}` };
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
return { runVideoCommand };
|
|
152
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// Video edit/extend admission, local/attached sources and session video status.
|
|
2
|
+
|
|
3
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
4
|
+
import { basename, isAbsolute, join } from "node:path";
|
|
5
|
+
import { chooseMediaModel, MediaError, outputDirectory, validCommandId, validSessionId, EDIT_SOURCE_MAX_SECONDS, EXTEND_SOURCE_SECONDS, parseVideoEditCommand, submitVideoEdit, VIDEO_EDIT_MODEL, isMp4, mp4Duration, VIDEO_UPLOAD_MAX_BYTES } from "../../media/index.js";
|
|
6
|
+
|
|
7
|
+
export function createVideoEdit(shared) {
|
|
8
|
+
const { ctx, controller, mediaSessions, mediaChoicesFor, mediaAccountsSummary, interpretParams, interpretedRecord, mediaFailureText, recordRun, mediaJobStore, videoLive, videoSources, usableSource, startVideoJob } = shared;
|
|
9
|
+
// `${accountId}/${route}` the upstream answered 405 for in this activation:
|
|
10
|
+
// asked again only after a reload (CPA pauses the credential after a 405).
|
|
11
|
+
const unsupportedRoutes = new Set();
|
|
12
|
+
const editRoute = (mode) => (mode === "extend" ? "extensions" : "edits");
|
|
13
|
+
|
|
14
|
+
/** The newest existing video file the session recorded (for one command, or any). */
|
|
15
|
+
const sessionVideoFile = (sessionId, commandId) => {
|
|
16
|
+
if (sessionId === undefined) return undefined;
|
|
17
|
+
return mediaSessions.get(sessionId).recent.find((r) => r.kind === "video" && (commandId === undefined || r.commandId === commandId) && existsSync(r.file));
|
|
18
|
+
};
|
|
19
|
+
/** The account a recorded run used (`accountId/modelId`), if it still exists. */
|
|
20
|
+
const runAccountId = (sessionId, commandId, accounts) => {
|
|
21
|
+
if (sessionId === undefined || commandId === undefined) return undefined;
|
|
22
|
+
const run = mediaSessions.get(sessionId).runs.find((r) => r.commandId === commandId);
|
|
23
|
+
const id = typeof run?.model === "string" ? run.model.split("/", 1)[0] : undefined;
|
|
24
|
+
return accounts.some((a) => a.id === id) ? id : undefined;
|
|
25
|
+
};
|
|
26
|
+
const checkVideoBytes = (data) => {
|
|
27
|
+
if (!isMp4(data)) throw new MediaError("video-not-mp4");
|
|
28
|
+
return mp4Duration(data);
|
|
29
|
+
};
|
|
30
|
+
const tooLarge = (bytes) => new MediaError("source-too-large", (bytes / 1024 / 1024).toFixed(1));
|
|
31
|
+
/** A local MP4 read whole (at most {@link VIDEO_UPLOAD_MAX_BYTES}) for an inline upload. */
|
|
32
|
+
const readLocalVideo = (path, name = basename(path)) => {
|
|
33
|
+
let info;
|
|
34
|
+
try {
|
|
35
|
+
info = statSync(path);
|
|
36
|
+
} catch {
|
|
37
|
+
info = undefined;
|
|
38
|
+
}
|
|
39
|
+
if (info === undefined || !info.isFile()) throw new MediaError("video-not-found", name);
|
|
40
|
+
if (info.size > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(info.size);
|
|
41
|
+
const data = new Uint8Array(readFileSync(path));
|
|
42
|
+
return { data, seconds: checkVideoBytes(data), name };
|
|
43
|
+
};
|
|
44
|
+
/** An MP4 attached to the command (a DSH file attachment `{ attachmentId, name, bytes }`). */
|
|
45
|
+
const readAttachedVideo = async (store, ref, signal) => {
|
|
46
|
+
const name = basename(String(ref?.name ?? "video.mp4"));
|
|
47
|
+
if (Number(ref?.bytes) > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(Number(ref.bytes));
|
|
48
|
+
let hostPath;
|
|
49
|
+
try {
|
|
50
|
+
hostPath = store.fileHostPath?.(ref);
|
|
51
|
+
} catch {
|
|
52
|
+
hostPath = undefined;
|
|
53
|
+
}
|
|
54
|
+
if (typeof hostPath === "string" && existsSync(hostPath)) return readLocalVideo(hostPath, name);
|
|
55
|
+
if (typeof store.readFileStream !== "function") throw new MediaError("unsupported-attachment", "edit");
|
|
56
|
+
const chunks = [];
|
|
57
|
+
let size = 0;
|
|
58
|
+
for await (const chunk of store.readFileStream(ref, signal)) {
|
|
59
|
+
size += chunk.byteLength;
|
|
60
|
+
if (size > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(size);
|
|
61
|
+
chunks.push(chunk);
|
|
62
|
+
}
|
|
63
|
+
const data = new Uint8Array(Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))));
|
|
64
|
+
return { data, seconds: checkVideoBytes(data), name };
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* `/cpa-video-edit` and `/cpa-video-extend`: change or continue any MP4.
|
|
69
|
+
* Sources: an attached MP4, `--video <path>`, `--url <public URL>`,
|
|
70
|
+
* `--from <command id>`, or by default the session's newest video. A video
|
|
71
|
+
* this plugin made goes by xAI's own short-lived link while it is
|
|
72
|
+
* remembered (about an hour, this activation), otherwise its file is sent
|
|
73
|
+
* inline; local files go inline as a base64 data URI (CPA has no file
|
|
74
|
+
* upload route; the edit routes read bodies of 20 MiB, measured), a URL is
|
|
75
|
+
* downloaded by xAI itself.
|
|
76
|
+
*/
|
|
77
|
+
const runVideoEditCommand = (mode) => async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
|
|
78
|
+
const name = mode === "extend" ? "/cpa-video-extend" : "/cpa-video-edit";
|
|
79
|
+
try {
|
|
80
|
+
const parsed = parseVideoEditCommand(rawInput, mode, { literal });
|
|
81
|
+
const blocks = Array.isArray(attachments) ? attachments : [];
|
|
82
|
+
if (blocks.some((block) => block?.type !== "file")) throw new MediaError("unsupported-attachment", "edit");
|
|
83
|
+
const named = [parsed.from, parsed.video, parsed.url].filter((v) => v !== undefined).length;
|
|
84
|
+
if (blocks.length > 1 || (blocks.length === 1 && named > 0)) throw new MediaError("one-video-source");
|
|
85
|
+
const sessionId = validSessionId(agent?.id);
|
|
86
|
+
const workingDirectory = ctx.get?.("workingDirectory");
|
|
87
|
+
const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
|
|
88
|
+
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
|
|
89
|
+
const { settings, options, accounts } = await mediaChoicesFor("video");
|
|
90
|
+
|
|
91
|
+
// source: { url? | data?, seconds?, accountId?, commandId?, name? }
|
|
92
|
+
let source;
|
|
93
|
+
const linkOf = (id) => {
|
|
94
|
+
const link = usableSource(id);
|
|
95
|
+
return link !== undefined && link.sessionId === sessionId ? link : undefined;
|
|
96
|
+
};
|
|
97
|
+
const fromLink = (link) => ({ url: link.url, seconds: link.seconds, accountId: link.accountId, commandId: link.commandId });
|
|
98
|
+
const fromFile = (record) => ({ ...readLocalVideo(record.file), commandId: record.commandId, accountId: runAccountId(sessionId, record.commandId, accounts) });
|
|
99
|
+
if (blocks.length === 1) {
|
|
100
|
+
const store = ctx.get?.("attachments");
|
|
101
|
+
if (store === undefined) throw new MediaError("unsupported-attachment", "edit");
|
|
102
|
+
source = await readAttachedVideo(store, blocks[0].attachment, signal);
|
|
103
|
+
} else if (parsed.video !== undefined) {
|
|
104
|
+
source = readLocalVideo(isAbsolute(parsed.video) ? parsed.video : join(cwd, parsed.video));
|
|
105
|
+
} else if (parsed.url !== undefined) {
|
|
106
|
+
source = { url: parsed.url, name: new URL(parsed.url).hostname };
|
|
107
|
+
} else if (parsed.from !== undefined) {
|
|
108
|
+
const link = linkOf(parsed.from);
|
|
109
|
+
const record = link === undefined ? sessionVideoFile(sessionId, parsed.from) : undefined;
|
|
110
|
+
if (link !== undefined) source = fromLink(link);
|
|
111
|
+
else if (record !== undefined) source = fromFile(record);
|
|
112
|
+
else throw new MediaError(videoSources.has(parsed.from) ? "video-source-expired" : "no-video-source");
|
|
113
|
+
} else {
|
|
114
|
+
const record = sessionVideoFile(sessionId);
|
|
115
|
+
const link = record !== undefined ? linkOf(record.commandId) : [...videoSources.values()].reverse().map((s) => linkOf(s.commandId)).find(Boolean);
|
|
116
|
+
if (link !== undefined) source = fromLink(link);
|
|
117
|
+
else if (record !== undefined) source = fromFile(record);
|
|
118
|
+
else throw new MediaError("no-video-source");
|
|
119
|
+
}
|
|
120
|
+
if (source.seconds !== undefined) {
|
|
121
|
+
if (mode === "edit" && source.seconds > EDIT_SOURCE_MAX_SECONDS) throw new MediaError("edit-source-too-long", String(source.seconds));
|
|
122
|
+
if (mode === "extend" && (source.seconds < EXTEND_SOURCE_SECONDS.min || source.seconds > EXTEND_SOURCE_SECONDS.max)) {
|
|
123
|
+
throw new MediaError("extend-source-length", String(source.seconds));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// The account: the source video's own, unless -m names a model; else the session's video model.
|
|
128
|
+
let accountId = parsed.model === undefined ? source.accountId : undefined;
|
|
129
|
+
if (accountId === undefined) {
|
|
130
|
+
const pickId = validSessionId(pickSession) ?? sessionId;
|
|
131
|
+
const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
|
|
132
|
+
try {
|
|
133
|
+
({ option: { accountId } } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const account = accounts.find((a) => a.id === accountId);
|
|
140
|
+
if (account === undefined) throw new MediaError("invalid-account");
|
|
141
|
+
const route = editRoute(mode);
|
|
142
|
+
if (unsupportedRoutes.has(`${accountId}/${route}`)) throw new MediaError("route-unsupported", route);
|
|
143
|
+
const endpoint = await controller.mediaEndpoint(accountId);
|
|
144
|
+
const known = source.seconds === undefined ? undefined : Math.max(1, Math.min(15, Math.round(source.seconds)));
|
|
145
|
+
const seconds = mode === "extend" ? parsed.seconds ?? 6 : known;
|
|
146
|
+
const id = validCommandId(commandId);
|
|
147
|
+
const accountName = account.displayName || account.id;
|
|
148
|
+
if (gate !== undefined) {
|
|
149
|
+
await gate({
|
|
150
|
+
kind: "video",
|
|
151
|
+
mode,
|
|
152
|
+
option: { ref: `${accountId}/${VIDEO_EDIT_MODEL}`, modelId: VIDEO_EDIT_MODEL, accountId, accountName },
|
|
153
|
+
prompt: parsed.prompt,
|
|
154
|
+
// Billed as the new footage: an edit re-renders the source, an extension adds `seconds`.
|
|
155
|
+
seconds: seconds ?? 6,
|
|
156
|
+
sourceName: source.name ?? (source.commandId !== undefined ? source.commandId : undefined),
|
|
157
|
+
references: 0,
|
|
158
|
+
cwd,
|
|
159
|
+
outputDir: settings.outputDir,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
recordRun(sessionId, id, {
|
|
163
|
+
kind: "video",
|
|
164
|
+
status: "running",
|
|
165
|
+
mode,
|
|
166
|
+
...(source.commandId === undefined ? {} : { sourceCommandId: source.commandId }),
|
|
167
|
+
...(source.name === undefined ? {} : { sourceName: source.name }),
|
|
168
|
+
sourceKind: source.data !== undefined ? "upload" : source.commandId !== undefined ? "link" : "url",
|
|
169
|
+
...(Array.isArray(parsed.ignored) ? { ignored: parsed.ignored } : {}),
|
|
170
|
+
model: `${accountId}/${VIDEO_EDIT_MODEL}`,
|
|
171
|
+
modelName: VIDEO_EDIT_MODEL,
|
|
172
|
+
accountName,
|
|
173
|
+
prompt: parsed.prompt,
|
|
174
|
+
...(seconds === undefined ? {} : { seconds }),
|
|
175
|
+
count: 1,
|
|
176
|
+
references: 0,
|
|
177
|
+
files: 0,
|
|
178
|
+
startedAt: new Date().toISOString(),
|
|
179
|
+
});
|
|
180
|
+
const { url: sourceUrl, data: sourceData } = source;
|
|
181
|
+
void startVideoJob({
|
|
182
|
+
sessionId,
|
|
183
|
+
commandId: id,
|
|
184
|
+
accountId,
|
|
185
|
+
modelId: VIDEO_EDIT_MODEL,
|
|
186
|
+
ref: `${accountId}/${VIDEO_EDIT_MODEL}`,
|
|
187
|
+
prompt: parsed.prompt,
|
|
188
|
+
// An extension returns the source plus the new part.
|
|
189
|
+
seconds: mode === "extend" ? Math.min(15, (known ?? 0) + seconds) : seconds ?? 6,
|
|
190
|
+
cwd,
|
|
191
|
+
outputDir: settings.outputDir,
|
|
192
|
+
label: name,
|
|
193
|
+
submit: async (jobSignal) => {
|
|
194
|
+
// The session's chat model reads an extension length or other wording the rules missed.
|
|
195
|
+
if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
|
|
196
|
+
const fixed = mode === "extend" && parsed.seconds !== undefined ? ["seconds"] : [];
|
|
197
|
+
const result = literal ? undefined : await interpretParams({ agent, command: mode, text: parsed.prompt, fixed, signal: jobSignal });
|
|
198
|
+
let prompt = parsed.prompt;
|
|
199
|
+
let added = seconds;
|
|
200
|
+
if (result !== undefined) {
|
|
201
|
+
prompt = result.prompt;
|
|
202
|
+
if (mode === "extend" && result.values.seconds !== undefined) added = result.values.seconds;
|
|
203
|
+
recordRun(sessionId, id, { prompt, ...(added === undefined ? {} : { seconds: added }), ...interpretedRecord(result, parsed.prompt) });
|
|
204
|
+
}
|
|
205
|
+
if (id !== undefined) videoLive.set(id, { phase: "submitting" });
|
|
206
|
+
try {
|
|
207
|
+
const submitted = await submitVideoEdit({
|
|
208
|
+
rootURL: endpoint.rootURL,
|
|
209
|
+
apiKey: endpoint.apiKey,
|
|
210
|
+
mode,
|
|
211
|
+
...(sourceData !== undefined ? { sourceData } : { sourceUrl }),
|
|
212
|
+
prompt,
|
|
213
|
+
...(mode === "extend" ? { seconds: added } : {}),
|
|
214
|
+
signal: jobSignal,
|
|
215
|
+
});
|
|
216
|
+
return { ...submitted, prompt, ...(mode === "extend" ? { seconds: Math.min(15, (known ?? 0) + added) } : {}) };
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (error instanceof MediaError && error.code === "route-unsupported") unsupportedRoutes.add(`${accountId}/${route}`);
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
const skipped = Array.isArray(parsed.ignored) && parsed.ignored.length > 0
|
|
224
|
+
? ` Ignored ${parsed.ignored.join(", ")}: ${mode === "extend" ? "an extension keeps the source's resolution and ratio" : "an edit keeps the source's length, resolution and ratio"}.`
|
|
225
|
+
: "";
|
|
226
|
+
const how = sourceData !== undefined ? `uploading ${source.name} (${(sourceData.byteLength / 1024 / 1024).toFixed(1)} MiB)` : source.name !== undefined ? `from ${source.name}` : "from xAI's link";
|
|
227
|
+
return {
|
|
228
|
+
kind: "success",
|
|
229
|
+
text: `${mode === "extend" ? `Extending the video by ${seconds} s` : "Editing the video"} with ${VIDEO_EDIT_MODEL} (${accountName}), ${how}; a submitted video cannot be cancelled. Files go to ${outputDirectory(cwd, settings.outputDir)}.${skipped}`,
|
|
230
|
+
};
|
|
231
|
+
} catch (error) {
|
|
232
|
+
if (signal?.aborted || error?.gateError === true) throw error;
|
|
233
|
+
return { kind: "error", text: `${name}: ${mediaFailureText(error, "video")}` };
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
/** `/cpa-video-status`: this session's video jobs, newest first (text). */
|
|
237
|
+
const runVideoStatusCommand = async ({ agent }) => {
|
|
238
|
+
const sessionId = validSessionId(agent?.id);
|
|
239
|
+
const jobs = mediaJobStore.list().filter((job) => job.sessionId !== undefined && job.sessionId === sessionId).slice(0, 20);
|
|
240
|
+
if (jobs.length === 0) return { kind: "success", text: "No video jobs in this session." };
|
|
241
|
+
const lines = jobs.map((job) => {
|
|
242
|
+
const live = job.commandId === undefined ? undefined : videoLive.get(job.commandId);
|
|
243
|
+
const state = job.state === "submitted" ? (live?.phase ?? "waiting") + (live?.progress === undefined ? "" : ` ${live.progress}%`) : job.state;
|
|
244
|
+
const prompt = job.prompt.length > 40 ? `${job.prompt.slice(0, 40)}…` : job.prompt;
|
|
245
|
+
return `${job.submittedAt.slice(0, 19).replace("T", " ")} ${state.padEnd(14)} ${job.modelId}, ${job.seconds} s ${prompt} (${job.id})`;
|
|
246
|
+
});
|
|
247
|
+
return { kind: "success", text: lines.join("\n") };
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// #endregion
|
|
251
|
+
|
|
252
|
+
return { unsupportedRoutes, runVideoEditCommand, runVideoStatusCommand };
|
|
253
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Persistent video submission/poll/download/resume and in-memory source links.
|
|
2
|
+
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { MediaError, outputDirectory, outputStem, recentEntry, createMediaJobStore, downloadVideo, retrieveVideo, VIDEO_JOB_TTL_MS } from "../../media/index.js";
|
|
5
|
+
|
|
6
|
+
export function createVideoJobs(shared) {
|
|
7
|
+
const { ctx, updateDirectory, controller, mediaFailureText, mediaJobs, settleWith, publishSessionStatus, settleSessionStatus, syncHeartbeat, recordRun, folderLabel } = shared;
|
|
8
|
+
// #region Video jobs (/cpa-video)
|
|
9
|
+
//
|
|
10
|
+
// A video is submitted once and cannot be cancelled (CPA and xAI have no
|
|
11
|
+
// cancel route). The job is stored in media-jobs.json as soon as CPA
|
|
12
|
+
// returns its request id, polled (5 s, then 10 s, then 15 s) and downloaded
|
|
13
|
+
// through CPA into the session's folder. A reload or restart only pauses
|
|
14
|
+
// the polling: the next activation resumes every stored job younger than
|
|
15
|
+
// CPA's 3-hour request binding, and marks older ones expired.
|
|
16
|
+
|
|
17
|
+
const mediaJobStore = createMediaJobStore(join(updateDirectory, "media-jobs.json"));
|
|
18
|
+
// commandId → { phase: "submitting" | "queued" | "running" | "downloading", progress? } for the chat row.
|
|
19
|
+
const videoLive = new Map();
|
|
20
|
+
// commandId → { url, accountId, accountName, seconds, sessionId, commandId, at }: the short-lived
|
|
21
|
+
// result URL of a finished video, for /cpa-video-edit and /cpa-video-extend.
|
|
22
|
+
// In memory only (never stored, logged or sent to the page); a reload forgets it.
|
|
23
|
+
const videoSources = new Map();
|
|
24
|
+
// xAI calls its result URLs ephemeral without a number; offer them for an hour.
|
|
25
|
+
const VIDEO_SOURCE_TTL_MS = 60 * 60 * 1000;
|
|
26
|
+
const MAX_VIDEO_SOURCES = 100;
|
|
27
|
+
const rememberSource = (job, status) => {
|
|
28
|
+
if (job.commandId === undefined || status.sourceUrl === undefined) return;
|
|
29
|
+
videoSources.delete(job.commandId);
|
|
30
|
+
videoSources.set(job.commandId, {
|
|
31
|
+
url: status.sourceUrl,
|
|
32
|
+
accountId: job.accountId,
|
|
33
|
+
seconds: status.seconds ?? job.seconds,
|
|
34
|
+
sessionId: job.sessionId,
|
|
35
|
+
commandId: job.commandId,
|
|
36
|
+
at: Date.now(),
|
|
37
|
+
});
|
|
38
|
+
while (videoSources.size > MAX_VIDEO_SOURCES) videoSources.delete(videoSources.keys().next().value);
|
|
39
|
+
};
|
|
40
|
+
const usableSource = (commandId) => {
|
|
41
|
+
const source = videoSources.get(commandId);
|
|
42
|
+
return source !== undefined && Date.now() - source.at <= VIDEO_SOURCE_TTL_MS ? source : undefined;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const sleep = (ms, signal) =>
|
|
46
|
+
new Promise((resolve, reject) => {
|
|
47
|
+
if (signal.aborted) return reject(signal.reason);
|
|
48
|
+
const timer = setTimeout(() => {
|
|
49
|
+
signal.removeEventListener("abort", onAbort);
|
|
50
|
+
resolve();
|
|
51
|
+
}, ms);
|
|
52
|
+
timer.unref?.();
|
|
53
|
+
const onAbort = () => {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
reject(signal.reason);
|
|
56
|
+
};
|
|
57
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const pollDelay = (elapsedMs) => (elapsedMs < 60_000 ? 5000 : elapsedMs < 180_000 ? 10_000 : 15_000);
|
|
61
|
+
const MAX_POLL_FAILURES = 12;
|
|
62
|
+
|
|
63
|
+
/** Poll, download and record one submitted video; a dispose abort leaves it stored for the next activation. */
|
|
64
|
+
const followVideoJob = async (job, abort) => {
|
|
65
|
+
const live = (patch) => {
|
|
66
|
+
if (job.commandId !== undefined) videoLive.set(job.commandId, { ...(videoLive.get(job.commandId) ?? {}), ...patch });
|
|
67
|
+
};
|
|
68
|
+
const finish = (state, runPatch, recent = []) => {
|
|
69
|
+
mediaJobStore.put({ ...job, state, finishedAt: new Date().toISOString() });
|
|
70
|
+
recordRun(job.sessionId, job.commandId, { ...runPatch, finishedAt: new Date().toISOString() }, recent);
|
|
71
|
+
};
|
|
72
|
+
const fail = (error) => {
|
|
73
|
+
const code = error instanceof MediaError ? error.code : "failed";
|
|
74
|
+
finish(code === "expired" ? "expired" : "failed", { status: "error", errorCode: code, error: mediaFailureText(error, "video") });
|
|
75
|
+
ctx.logger?.warn?.(`cpa: /cpa-video ${job.id} failed: ${mediaFailureText(error, "video")}`);
|
|
76
|
+
};
|
|
77
|
+
const submitted = Date.parse(job.submittedAt);
|
|
78
|
+
let failures = 0;
|
|
79
|
+
live({ phase: "queued" });
|
|
80
|
+
try {
|
|
81
|
+
for (;;) {
|
|
82
|
+
if (Date.now() - submitted > VIDEO_JOB_TTL_MS) throw new MediaError("expired");
|
|
83
|
+
let endpoint;
|
|
84
|
+
let status;
|
|
85
|
+
try {
|
|
86
|
+
endpoint = await controller.mediaEndpoint(job.accountId);
|
|
87
|
+
status = await retrieveVideo({ rootURL: endpoint.rootURL, apiKey: endpoint.apiKey, requestId: job.id, signal: abort.signal });
|
|
88
|
+
failures = 0;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (abort.signal.aborted) throw error;
|
|
91
|
+
// A missing account or a refusal that keeps coming back ends the job;
|
|
92
|
+
// network trouble and short CPA hiccups are retried.
|
|
93
|
+
if (error instanceof MediaError && error.code === "invalid-account") throw error;
|
|
94
|
+
failures += 1;
|
|
95
|
+
if (failures >= MAX_POLL_FAILURES) throw error;
|
|
96
|
+
await sleep(pollDelay(Date.now() - submitted), abort.signal);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (status.state === "failed") throw new MediaError("video-failed", status.error);
|
|
100
|
+
if (status.state === "done") {
|
|
101
|
+
live({ phase: "downloading", progress: 100 });
|
|
102
|
+
const dir = outputDirectory(job.cwd, job.outputDir);
|
|
103
|
+
const path = await downloadVideo({
|
|
104
|
+
rootURL: endpoint.rootURL,
|
|
105
|
+
apiKey: endpoint.apiKey,
|
|
106
|
+
requestId: job.id,
|
|
107
|
+
dir,
|
|
108
|
+
stem: outputStem(job.modelId, job.prompt),
|
|
109
|
+
signal: abort.signal,
|
|
110
|
+
});
|
|
111
|
+
finish("done", { status: "done", files: 1, folder: folderLabel(job.cwd, dir) }, [recentEntry("video", path, job.ref, new Date(), job.commandId)]);
|
|
112
|
+
rememberSource(job, status);
|
|
113
|
+
ctx.logger?.info?.(`cpa: /cpa-video ${job.id} saved after ${Math.round((Date.now() - submitted) / 1000)} s`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
live({ phase: status.state, ...(status.progress === undefined ? {} : { progress: status.progress }) });
|
|
117
|
+
await sleep(pollDelay(Date.now() - submitted), abort.signal);
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (abort.signal.aborted) return; // paused by a reload; resumed by the next activation
|
|
121
|
+
fail(error);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** Track one job in the session's running state until it settles. */
|
|
126
|
+
const trackVideoJob = (key, sessionId, run) => {
|
|
127
|
+
const abort = new AbortController();
|
|
128
|
+
mediaJobs.set(key, { sessionId, abort });
|
|
129
|
+
syncHeartbeat();
|
|
130
|
+
if (sessionId !== undefined) publishSessionStatus(sessionId, true);
|
|
131
|
+
return settleWith(key, (async () => {
|
|
132
|
+
try {
|
|
133
|
+
await run(abort);
|
|
134
|
+
} finally {
|
|
135
|
+
if (mediaJobs.get(key)?.abort === abort) mediaJobs.delete(key);
|
|
136
|
+
videoLive.delete(key);
|
|
137
|
+
syncHeartbeat();
|
|
138
|
+
settleSessionStatus(sessionId);
|
|
139
|
+
}
|
|
140
|
+
})());
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Submit one admitted video through `submit(signal)` (generation, edit or
|
|
145
|
+
* extension), store the job, then follow it.
|
|
146
|
+
*/
|
|
147
|
+
const startVideoJob = ({ sessionId, commandId, accountId, modelId, ref, prompt, seconds, cwd, outputDir, submit, label }) => {
|
|
148
|
+
const key = commandId ?? `job-${++shared.mediaJobSeq}`;
|
|
149
|
+
if (commandId !== undefined) videoLive.set(commandId, { phase: "submitting" });
|
|
150
|
+
return trackVideoJob(key, sessionId, async (abort) => {
|
|
151
|
+
let requestId;
|
|
152
|
+
try {
|
|
153
|
+
// `submit` may settle the final prompt / length first (parameter interpretation).
|
|
154
|
+
const submitted = await submit(abort.signal);
|
|
155
|
+
requestId = submitted.requestId;
|
|
156
|
+
if (typeof submitted.prompt === "string") prompt = submitted.prompt;
|
|
157
|
+
if (Number.isInteger(submitted.seconds)) seconds = submitted.seconds;
|
|
158
|
+
} catch (thrown) {
|
|
159
|
+
// Cut off before CPA answered: whether it was submitted is unknown.
|
|
160
|
+
const error = abort.signal.aborted ? new MediaError("interrupted") : thrown;
|
|
161
|
+
recordRun(sessionId, commandId, {
|
|
162
|
+
status: "error",
|
|
163
|
+
errorCode: error instanceof MediaError ? error.code : "failed",
|
|
164
|
+
error: mediaFailureText(error, "video"),
|
|
165
|
+
finishedAt: new Date().toISOString(),
|
|
166
|
+
});
|
|
167
|
+
ctx.logger?.warn?.(`cpa: ${label} was not submitted: ${mediaFailureText(error, "video")}`);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const job = mediaJobStore.put({
|
|
171
|
+
id: requestId,
|
|
172
|
+
state: "submitted",
|
|
173
|
+
accountId,
|
|
174
|
+
modelId,
|
|
175
|
+
ref,
|
|
176
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
177
|
+
...(commandId === undefined ? {} : { commandId }),
|
|
178
|
+
cwd,
|
|
179
|
+
outputDir,
|
|
180
|
+
prompt,
|
|
181
|
+
seconds,
|
|
182
|
+
submittedAt: new Date().toISOString(),
|
|
183
|
+
});
|
|
184
|
+
recordRun(sessionId, commandId, { videoId: requestId });
|
|
185
|
+
ctx.logger?.info?.(`cpa: ${label} submitted ${requestId} (${modelId}, ${seconds} s)`);
|
|
186
|
+
await followVideoJob(job, abort);
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
/** Resume every stored job that is still running (after a reload or restart). */
|
|
190
|
+
const resumeVideoJobs = () => {
|
|
191
|
+
let jobs;
|
|
192
|
+
try {
|
|
193
|
+
jobs = mediaJobStore.list().filter((job) => job.state === "submitted");
|
|
194
|
+
} catch (error) {
|
|
195
|
+
ctx.logger?.warn?.(`cpa: could not read media-jobs.json: ${error instanceof Error ? error.message : String(error)}`);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
for (const job of jobs) {
|
|
199
|
+
const key = job.commandId ?? `video-${job.id}`;
|
|
200
|
+
if (mediaJobs.has(key)) continue;
|
|
201
|
+
void trackVideoJob(key, job.sessionId, (abort) => followVideoJob(job, abort));
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
return { mediaJobStore, videoLive, videoSources, VIDEO_SOURCE_TTL_MS, usableSource, startVideoJob, resumeVideoJobs };
|
|
206
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// HMR-safe callback execution and accepted account HTTP input fields.
|
|
2
|
+
|
|
3
|
+
import { GLOBAL_FIELDS } from "./config.js";
|
|
4
|
+
import { plainObject } from "./accounts.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Run `fn` outside the HMR transaction's async context.
|
|
8
|
+
*
|
|
9
|
+
* dsh-hmr marks a running transaction with an AsyncLocalStorage store, and
|
|
10
|
+
* this plugin's apply() runs inside the install/enable transaction. Timers
|
|
11
|
+
* created there inherit that store forever, so every later settings write
|
|
12
|
+
* from them fails with "HMR transactions cannot be nested". Creating the
|
|
13
|
+
* timers under `executing.exit()` — what dsh-hmr does for its own watchers —
|
|
14
|
+
* gives them a clean context.
|
|
15
|
+
*/
|
|
16
|
+
export function outsideHmrTransaction(ctx, fn) {
|
|
17
|
+
try {
|
|
18
|
+
const executing = ctx.get?.("hmr")?.executing;
|
|
19
|
+
if (typeof executing?.exit === "function") return executing.exit(fn);
|
|
20
|
+
} catch {}
|
|
21
|
+
return fn();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Account fields the save and add routes accept, besides key handling. */
|
|
25
|
+
export function accountPatchFromInput(input) {
|
|
26
|
+
const patch = {};
|
|
27
|
+
for (const field of ["baseURL", "provider", "displayName", "protocol", "protocolMode", "includePrefix", "excludePrefix", "apiKeyEnv"]) {
|
|
28
|
+
if (typeof input[field] === "string") patch[field] = input[field];
|
|
29
|
+
}
|
|
30
|
+
if (typeof input.enabled === "boolean") patch.enabled = input.enabled;
|
|
31
|
+
if (Array.isArray(input.selectedModels)) patch.selectedModels = input.selectedModels;
|
|
32
|
+
if (plainObject(input.modelOverrides) !== undefined) patch.modelOverrides = input.modelOverrides;
|
|
33
|
+
if (typeof input.apiKey === "string" && input.apiKey.trim() !== "") patch.apiKey = input.apiKey.trim();
|
|
34
|
+
if (input.clearApiKey === true) patch.clearApiKey = true;
|
|
35
|
+
for (const field of GLOBAL_FIELDS) {
|
|
36
|
+
if (input[field] !== undefined) patch[field] = input[field];
|
|
37
|
+
}
|
|
38
|
+
return patch;
|
|
39
|
+
}
|