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,406 @@
|
|
|
1
|
+
// Optional generate_media tool, cost consent, session budgets and output presentation.
|
|
2
|
+
|
|
3
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { basename, isAbsolute, join } from "node:path";
|
|
6
|
+
import { MediaError, outputDirectory, validSessionId, agentPolicy, approvalText, chargeSeconds, GENERATE_MEDIA, GENERATE_MEDIA_OUTPUT, GENERATE_MEDIA_TOOL, parseToolArgs, sniffImageType, toolCommandLine, toolRunId } from "../../media/index.js";
|
|
7
|
+
|
|
8
|
+
export function createAgentTool(shared) {
|
|
9
|
+
const { ctx, mediaSessions, mediaChoices, sessionChatTarget, mediaFailureText, jobPromises, runImageCommand, runVideoCommand, runVideoEditCommand } = shared;
|
|
10
|
+
// #region Agent tool generate_media (off by default; design §5, §7, §8)
|
|
11
|
+
//
|
|
12
|
+
// One optional tool, registered only while the Generation tab's switch is
|
|
13
|
+
// on and some enabled account offers a generation model. A call runs the
|
|
14
|
+
// same admission as the slash commands (literally: no plain-language
|
|
15
|
+
// parsing or interpretation), with a gate between admission and the
|
|
16
|
+
// request that asks for approval and enforces the video seconds budget of
|
|
17
|
+
// the root session, then waits up to 10 minutes for the background job.
|
|
18
|
+
const TOOL_WAIT_MS = 10 * 60_000;
|
|
19
|
+
const TOOL_IMAGE_MAX_BYTES = 32 * 1024 * 1024;
|
|
20
|
+
const TOOL_PREVIEWS = 4;
|
|
21
|
+
let toolsCtx;
|
|
22
|
+
let toolOff;
|
|
23
|
+
|
|
24
|
+
const gateError = (message) => Object.assign(new Error(message), { gateError: true });
|
|
25
|
+
|
|
26
|
+
/** The top-level agent a (sub-)agent was created under: its session holds picks and the budget. */
|
|
27
|
+
const rootAgentOf = (agent) => {
|
|
28
|
+
const agents = ctx.get?.("agents");
|
|
29
|
+
let current = agent;
|
|
30
|
+
if (typeof agents?.list !== "function" || typeof agents.isOwnedBy !== "function") return current;
|
|
31
|
+
for (let depth = 0; depth < 16; depth += 1) {
|
|
32
|
+
const owner = agents.list().find((candidate) => candidate !== current && agents.isOwnedBy(current.id, candidate));
|
|
33
|
+
if (owner === undefined) break;
|
|
34
|
+
current = owner;
|
|
35
|
+
}
|
|
36
|
+
return current;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Legacy fallback only when the native userQuestions service is absent. Cost
|
|
40
|
+
// confirmation is independent of DSH's execution-approval permission policy.
|
|
41
|
+
// runId → { sessionId, kind, en, zh, expiresAt, settle(outcome) }, in memory only.
|
|
42
|
+
const pendingConfirms = new Map();
|
|
43
|
+
const pendingNativeConfirms = new Set();
|
|
44
|
+
const CONFIRM_WAIT_MS = 10 * 60_000;
|
|
45
|
+
const CONFIRM_ALLOW = "Allow / 允许";
|
|
46
|
+
const CONFIRM_DECLINE = "Decline / 拒绝";
|
|
47
|
+
const cancelConfirms = () => {
|
|
48
|
+
for (const cancel of [...pendingNativeConfirms]) cancel();
|
|
49
|
+
for (const waiting of [...pendingConfirms.values()]) waiting.settle("cancelled");
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Compatibility composer/row consent without userQuestions (`allowed` / `rejected` / `timeout` / `cancelled`). */
|
|
53
|
+
const askInRow = (exec, runId, sessionId, text, kind) =>
|
|
54
|
+
new Promise((resolve) => {
|
|
55
|
+
if (exec.signal?.aborted || toolOff === undefined || toolsCtx === undefined) return resolve("cancelled");
|
|
56
|
+
const timer = setTimeout(() => settle("timeout"), CONFIRM_WAIT_MS);
|
|
57
|
+
timer.unref?.();
|
|
58
|
+
const onAbort = () => settle("cancelled");
|
|
59
|
+
const settle = (outcome) => {
|
|
60
|
+
if (pendingConfirms.get(runId)?.settle !== settle) return;
|
|
61
|
+
pendingConfirms.delete(runId);
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
exec.signal?.removeEventListener("abort", onAbort);
|
|
64
|
+
resolve(outcome);
|
|
65
|
+
};
|
|
66
|
+
exec.signal?.addEventListener("abort", onAbort, { once: true });
|
|
67
|
+
pendingConfirms.set(runId, { sessionId, kind, en: text.en, zh: text.zh, expiresAt: new Date(Date.now() + CONFIRM_WAIT_MS).toISOString(), settle });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/** Anonymous questions block in the native composer, not in a folded tool call. */
|
|
71
|
+
const askNativeConfirm = (questions, exec, text, kind, runId) =>
|
|
72
|
+
new Promise((resolve) => {
|
|
73
|
+
if (exec.signal?.aborted || toolOff === undefined || toolsCtx === undefined) return resolve("cancelled");
|
|
74
|
+
const controller = new AbortController();
|
|
75
|
+
const questionId = `media-confirm-${runId}`;
|
|
76
|
+
let settled = false;
|
|
77
|
+
const onAbort = () => finish("cancelled");
|
|
78
|
+
const cancel = () => finish("cancelled");
|
|
79
|
+
const timer = setTimeout(() => finish("timeout"), CONFIRM_WAIT_MS);
|
|
80
|
+
timer.unref?.();
|
|
81
|
+
const finish = (outcome) => {
|
|
82
|
+
if (settled) return;
|
|
83
|
+
settled = true;
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
exec.signal?.removeEventListener("abort", onAbort);
|
|
86
|
+
pendingNativeConfirms.delete(cancel);
|
|
87
|
+
// Remove the composer's pending request on every settlement, even if
|
|
88
|
+
// an answerer ignores cancellation or rejects after the local deadline.
|
|
89
|
+
controller.abort();
|
|
90
|
+
resolve(outcome);
|
|
91
|
+
};
|
|
92
|
+
pendingNativeConfirms.add(cancel);
|
|
93
|
+
exec.signal?.addEventListener("abort", onAbort, { once: true });
|
|
94
|
+
Promise.resolve().then(() => {
|
|
95
|
+
if (settled) return;
|
|
96
|
+
return questions.ask({
|
|
97
|
+
agent: rootAgentOf(exec.agent),
|
|
98
|
+
signal: controller.signal,
|
|
99
|
+
questions: [{
|
|
100
|
+
id: questionId,
|
|
101
|
+
header: kind === "video" ? "Video confirmation / 视频确认" : "Image confirmation / 图像确认",
|
|
102
|
+
question: `${text.zh}\n\n${text.en}`,
|
|
103
|
+
options: [{ label: CONFIRM_ALLOW }, { label: CONFIRM_DECLINE }],
|
|
104
|
+
multiSelect: false,
|
|
105
|
+
}],
|
|
106
|
+
// Do not supply wait/callId: anonymous requests publish the native
|
|
107
|
+
// blocking PendingQuestion instead of a continued tool-call card.
|
|
108
|
+
});
|
|
109
|
+
}).then((answer) => {
|
|
110
|
+
const item = Array.isArray(answer?.answers) && answer.answers.length === 1 ? answer.answers[0] : undefined;
|
|
111
|
+
const allowed = item?.id === questionId && item.custom === undefined &&
|
|
112
|
+
Array.isArray(item.selected) && item.selected.length === 1 && item.selected[0] === CONFIRM_ALLOW;
|
|
113
|
+
finish(allowed ? "allowed" : "rejected");
|
|
114
|
+
}, () => finish("unavailable"));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/** Cost consent is not an execution approval and never falls back after a native error. */
|
|
118
|
+
const askApproval = async (exec, text, kind, runId, sessionId) => {
|
|
119
|
+
const questions = ctx.get?.("userQuestions");
|
|
120
|
+
const what = kind === "video" ? "this video" : "these images";
|
|
121
|
+
let answer;
|
|
122
|
+
if (questions === undefined || questions === null) {
|
|
123
|
+
const ownerId = validSessionId(rootAgentOf(exec.agent)?.id) ?? sessionId;
|
|
124
|
+
if (ownerId === undefined) throw gateError(`${what} needs the user's confirmation, but this call has no session to ask in`);
|
|
125
|
+
answer = await askInRow(exec, runId, ownerId, text, kind);
|
|
126
|
+
} else {
|
|
127
|
+
answer = await askNativeConfirm(questions, exec, text, kind, runId);
|
|
128
|
+
}
|
|
129
|
+
if (answer === "allowed") return;
|
|
130
|
+
if (answer === "rejected") throw gateError(`the user declined ${what}; do not retry unless the user asks`);
|
|
131
|
+
if (answer === "timeout") throw gateError(`the user did not answer the confirmation for ${what} within 10 minutes; ask the user before trying again`);
|
|
132
|
+
if (answer === "cancelled") throw gateError(`the confirmation for ${what} was cancelled`);
|
|
133
|
+
throw gateError(`the confirmation for ${what} could not be shown; ask the user before trying again`);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** Attached-image blocks for local image paths (saved in DSH's attachment store, like composer images). */
|
|
137
|
+
const toolImageBlocks = async (paths, cwd, signal) => {
|
|
138
|
+
if (paths === undefined) return [];
|
|
139
|
+
const store = ctx.get?.("attachments");
|
|
140
|
+
if (typeof store?.saveImage !== "function") throw new Error("images cannot be read: DSH's attachment store is not available");
|
|
141
|
+
const blocks = [];
|
|
142
|
+
for (const raw of paths) {
|
|
143
|
+
const path = isAbsolute(raw) ? raw : join(cwd, raw);
|
|
144
|
+
let info;
|
|
145
|
+
try {
|
|
146
|
+
info = statSync(path);
|
|
147
|
+
} catch {
|
|
148
|
+
info = undefined;
|
|
149
|
+
}
|
|
150
|
+
if (info === undefined || !info.isFile()) throw new Error(`image not found: ${raw}`);
|
|
151
|
+
if (info.size > TOOL_IMAGE_MAX_BYTES) throw new Error(`image is larger than 32 MiB: ${raw}`);
|
|
152
|
+
const data = new Uint8Array(readFileSync(path));
|
|
153
|
+
const mediaType = sniffImageType(data);
|
|
154
|
+
if (mediaType === undefined) throw new Error(`not a PNG, JPEG, WebP or GIF image: ${raw}`);
|
|
155
|
+
signal?.throwIfAborted();
|
|
156
|
+
const ref = await store.saveImage({ data, mediaType, name: basename(path) });
|
|
157
|
+
blocks.push({ type: "image", attachment: ref });
|
|
158
|
+
}
|
|
159
|
+
return blocks;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/** Whether the calling route declares image input (as DSH's own read_image checks). */
|
|
163
|
+
const routeSeesImages = async (agent, signal) => {
|
|
164
|
+
const target = sessionChatTarget(agent);
|
|
165
|
+
const llm = ctx.get?.("llm");
|
|
166
|
+
if (target === undefined || typeof llm?.resolveModelInfo !== "function") return false;
|
|
167
|
+
try {
|
|
168
|
+
const info = await llm.resolveModelInfo(target.provider, target.model, signal);
|
|
169
|
+
return Array.isArray(info?.inputModalities) && info.inputModalities.includes("image");
|
|
170
|
+
} catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/** Smaller copies of generated images for the model (DSH's attachment store normalizes them). */
|
|
176
|
+
const imagePreviews = async (paths, agent, signal) => {
|
|
177
|
+
const store = ctx.get?.("attachments");
|
|
178
|
+
if (paths.length === 0 || typeof store?.saveImage !== "function" || !(await routeSeesImages(agent, signal))) return [];
|
|
179
|
+
const previews = [];
|
|
180
|
+
for (const path of paths.slice(0, TOOL_PREVIEWS)) {
|
|
181
|
+
try {
|
|
182
|
+
const data = new Uint8Array(readFileSync(path));
|
|
183
|
+
const mediaType = sniffImageType(data);
|
|
184
|
+
if (mediaType === undefined) continue;
|
|
185
|
+
const ref = await store.saveImage({ data, mediaType, name: basename(path) });
|
|
186
|
+
previews.push({
|
|
187
|
+
attachmentId: String(ref.attachmentId),
|
|
188
|
+
mediaType: ref.mediaType,
|
|
189
|
+
bytes: ref.bytes,
|
|
190
|
+
width: ref.width,
|
|
191
|
+
height: ref.height,
|
|
192
|
+
...(ref.name === undefined ? {} : { name: ref.name }),
|
|
193
|
+
...(ref.originalDimensions === undefined ? {} : { originalDimensions: { width: ref.originalDimensions.width, height: ref.originalDimensions.height } }),
|
|
194
|
+
});
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (signal?.aborted) throw error;
|
|
197
|
+
ctx.logger?.warn?.(`cpa: could not attach a preview of ${basename(path)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return previews;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/** Wait for a job of this activation: "settled", "timeout" or "aborted" (the job keeps running). */
|
|
204
|
+
const waitForJob = (key, signal, ms) => {
|
|
205
|
+
const promise = jobPromises.get(key);
|
|
206
|
+
if (promise === undefined) return Promise.resolve("settled");
|
|
207
|
+
return new Promise((resolve) => {
|
|
208
|
+
const timer = setTimeout(() => finish("timeout"), ms);
|
|
209
|
+
timer.unref?.();
|
|
210
|
+
const onAbort = () => finish("aborted");
|
|
211
|
+
const finish = (how) => {
|
|
212
|
+
clearTimeout(timer);
|
|
213
|
+
signal?.removeEventListener("abort", onAbort);
|
|
214
|
+
resolve(how);
|
|
215
|
+
};
|
|
216
|
+
if (signal?.aborted) return finish("aborted");
|
|
217
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
218
|
+
promise.then(() => finish("settled"), () => finish("settled"));
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// Not submitted (CPA refused it at once): the seconds go back to the budget.
|
|
223
|
+
const NOT_SUBMITTED = new Set(["upstream-error", "request-too-large", "route-unsupported", "no-endpoint", "invalid-account"]);
|
|
224
|
+
|
|
225
|
+
const executeGenerateMedia = async (args, exec) => {
|
|
226
|
+
const t = parseToolArgs(args);
|
|
227
|
+
if (exec.agent === undefined) throw new Error("generate_media needs an agent session");
|
|
228
|
+
const agent = exec.agent;
|
|
229
|
+
const sessionId = validSessionId(agent.id);
|
|
230
|
+
const root = rootAgentOf(agent);
|
|
231
|
+
const rootId = validSessionId(root?.id) ?? sessionId;
|
|
232
|
+
const runId = toolRunId(exec.callId) ?? `tool-${randomUUID()}`;
|
|
233
|
+
const workingDirectory = ctx.get?.("workingDirectory");
|
|
234
|
+
const cwd = workingDirectory === undefined ? undefined : await workingDirectory.ensure(agent, exec.signal);
|
|
235
|
+
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new Error(mediaFailureText(new MediaError("workspace-not-local"), t.kind));
|
|
236
|
+
const blocks = await toolImageBlocks(t.images, cwd, exec.signal);
|
|
237
|
+
const line = toolCommandLine(t);
|
|
238
|
+
const handler = t.kind === "image" ? runImageCommand : t.mode === "generate" ? runVideoCommand : runVideoEditCommand(t.mode);
|
|
239
|
+
// The client groups native tool events at turn level; never impersonate
|
|
240
|
+
// a user slash command. Retain the deprecated output flag for old clients.
|
|
241
|
+
const commandRow = false;
|
|
242
|
+
let charged = 0;
|
|
243
|
+
let planned;
|
|
244
|
+
const gate = async (plan) => {
|
|
245
|
+
planned = plan;
|
|
246
|
+
const { settings } = mediaChoices();
|
|
247
|
+
const policy = agentPolicy(settings, rootId === undefined ? {} : mediaSessions.get(rootId));
|
|
248
|
+
if (plan.kind === "video") {
|
|
249
|
+
if (policy.videoConfirm === "auto" && policy.limit !== undefined && policy.used + plan.seconds > policy.limit) {
|
|
250
|
+
throw gateError(`this session's agent video budget is ${policy.limit} s and ${policy.used} s are used; this video needs ${plan.seconds} s. Ask the user to raise the limit (the media button next to the composer) or to confirm videos one by one`);
|
|
251
|
+
}
|
|
252
|
+
if (policy.videoConfirm === "ask") await askApproval(exec, approvalText(plan, policy), "video", runId, sessionId);
|
|
253
|
+
if (rootId !== undefined) {
|
|
254
|
+
mediaSessions.update(rootId, (entry) => chargeSeconds(entry, agentPolicy(settings, entry), plan.seconds));
|
|
255
|
+
charged = plan.seconds;
|
|
256
|
+
}
|
|
257
|
+
} else if (policy.imageConfirm === "ask") {
|
|
258
|
+
await askApproval(exec, approvalText(plan, policy), "image", runId, sessionId);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
const refund = () => {
|
|
262
|
+
if (charged === 0 || rootId === undefined) return;
|
|
263
|
+
const { settings } = mediaChoices();
|
|
264
|
+
mediaSessions.update(rootId, (entry) => chargeSeconds(entry, agentPolicy(settings, entry), -charged));
|
|
265
|
+
charged = 0;
|
|
266
|
+
};
|
|
267
|
+
const outcome = await handler({ agent, rawInput: line, attachments: blocks, signal: exec.signal, commandId: runId, literal: true, pickSession: rootId, gate });
|
|
268
|
+
if (outcome?.kind !== "success") {
|
|
269
|
+
refund();
|
|
270
|
+
throw new Error(String(outcome?.text ?? "the request was refused").replace(/^\/cpa-[a-z-]+:\s*/, ""));
|
|
271
|
+
}
|
|
272
|
+
const waited = await waitForJob(runId, exec.signal, TOOL_WAIT_MS);
|
|
273
|
+
const entry = sessionId === undefined ? { runs: [], recent: [] } : mediaSessions.get(sessionId);
|
|
274
|
+
const run = entry.runs.find((r) => r.commandId === runId);
|
|
275
|
+
const files = entry.recent.filter((r) => r.commandId === runId && existsSync(r.file)).map((r) => r.file);
|
|
276
|
+
const option = planned?.option;
|
|
277
|
+
const base = {
|
|
278
|
+
kind: t.kind,
|
|
279
|
+
...(t.kind === "video" ? { mode: t.mode } : {}),
|
|
280
|
+
model: option?.modelId ?? run?.modelName ?? "",
|
|
281
|
+
...(option?.accountName ? { account: option.accountName } : {}),
|
|
282
|
+
run: runId,
|
|
283
|
+
commandRow,
|
|
284
|
+
files,
|
|
285
|
+
folder: outputDirectory(cwd, planned?.outputDir ?? mediaChoices().settings.outputDir),
|
|
286
|
+
...(Number.isInteger(run?.seconds) ? { seconds: run.seconds } : {}),
|
|
287
|
+
...(typeof run?.resolution === "string" ? { resolution: run.resolution } : {}),
|
|
288
|
+
...(typeof run?.aspect === "string" ? { aspect: run.aspect } : {}),
|
|
289
|
+
};
|
|
290
|
+
if (waited !== "settled" || run?.status === "running") {
|
|
291
|
+
return {
|
|
292
|
+
status: "running",
|
|
293
|
+
...base,
|
|
294
|
+
note:
|
|
295
|
+
waited === "aborted"
|
|
296
|
+
? "Stopped waiting. The generation keeps running in the background (and is billed); the file will be saved in the folder above and shown in this call's row."
|
|
297
|
+
: "Still generating after 10 minutes. It keeps running in the background; the file will be saved in the folder above and shown in this call's row.",
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
if (run === undefined || run.status === "error") {
|
|
301
|
+
if (run === undefined || (run.videoId === undefined && NOT_SUBMITTED.has(run.errorCode))) refund();
|
|
302
|
+
throw new Error(run?.error ?? "the generation failed");
|
|
303
|
+
}
|
|
304
|
+
const previews = t.kind === "image" ? await imagePreviews(files, agent, exec.signal) : [];
|
|
305
|
+
return {
|
|
306
|
+
status: "done",
|
|
307
|
+
...base,
|
|
308
|
+
...(typeof run.revisedPrompt === "string" ? { revisedPrompt: run.revisedPrompt } : {}),
|
|
309
|
+
...(previews.length > 0 ? { previews } : {}),
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
/** The model-facing content: a short summary, then any image previews. */
|
|
314
|
+
const renderGenerateMedia = (_args, value) => {
|
|
315
|
+
const lines = [
|
|
316
|
+
`${value.status === "done" ? "Saved" : "Running"}: ${value.kind}${value.mode && value.mode !== "generate" ? ` (${value.mode})` : ""} with ${value.model}${value.account ? ` (${value.account})` : ""}${value.seconds ? `, ${value.seconds} s` : ""}${value.resolution ? `, ${value.resolution}` : ""}${value.aspect ? `, ${value.aspect}` : ""}`,
|
|
317
|
+
...value.files.map((file) => file),
|
|
318
|
+
...(value.files.length === 0 ? [`Folder: ${value.folder}`] : []),
|
|
319
|
+
...(value.revisedPrompt ? [`Revised prompt: ${value.revisedPrompt}`] : []),
|
|
320
|
+
...(value.note ? [value.note] : []),
|
|
321
|
+
];
|
|
322
|
+
return [
|
|
323
|
+
{ type: "text", text: lines.join("\n") },
|
|
324
|
+
...(value.previews ?? []).map((p) => ({
|
|
325
|
+
type: "image",
|
|
326
|
+
attachment: {
|
|
327
|
+
attachmentId: p.attachmentId,
|
|
328
|
+
mediaType: p.mediaType,
|
|
329
|
+
bytes: p.bytes,
|
|
330
|
+
width: p.width,
|
|
331
|
+
height: p.height,
|
|
332
|
+
...(p.name === undefined ? {} : { name: p.name }),
|
|
333
|
+
...(p.originalDimensions === undefined ? {} : { originalDimensions: { ...p.originalDimensions } }),
|
|
334
|
+
},
|
|
335
|
+
})),
|
|
336
|
+
];
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const generateMediaTool = {
|
|
340
|
+
name: GENERATE_MEDIA,
|
|
341
|
+
description: GENERATE_MEDIA_TOOL.description,
|
|
342
|
+
parameters: GENERATE_MEDIA_TOOL.parameters,
|
|
343
|
+
output: {
|
|
344
|
+
schema: GENERATE_MEDIA_OUTPUT,
|
|
345
|
+
render: renderGenerateMedia,
|
|
346
|
+
presentationMeta: (_args, value) => ({ run: value.run, status: value.status, commandRow: value.commandRow === true }),
|
|
347
|
+
},
|
|
348
|
+
execute: executeGenerateMedia,
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/** Register or unregister the tool to match the switch and the offered models. */
|
|
352
|
+
const syncAgentTool = () => {
|
|
353
|
+
let wanted = false;
|
|
354
|
+
try {
|
|
355
|
+
const { settings, options } = mediaChoices();
|
|
356
|
+
wanted = settings.agentTool === true && options.image.length + options.video.length > 0;
|
|
357
|
+
} catch {
|
|
358
|
+
wanted = false;
|
|
359
|
+
}
|
|
360
|
+
if (wanted && toolOff === undefined && toolsCtx !== undefined) {
|
|
361
|
+
try {
|
|
362
|
+
const off = toolsCtx.tools.register(generateMediaTool);
|
|
363
|
+
toolOff = typeof off === "function" ? off : () => {};
|
|
364
|
+
ctx.logger?.info?.("cpa: generate_media tool registered");
|
|
365
|
+
} catch (error) {
|
|
366
|
+
ctx.logger?.warn?.(`cpa: could not register generate_media: ${error instanceof Error ? error.message : String(error)}`);
|
|
367
|
+
}
|
|
368
|
+
} else if (!wanted && toolOff !== undefined) {
|
|
369
|
+
cancelConfirms();
|
|
370
|
+
const off = toolOff;
|
|
371
|
+
toolOff = undefined;
|
|
372
|
+
try {
|
|
373
|
+
off();
|
|
374
|
+
} catch {
|
|
375
|
+
// already gone with its scope
|
|
376
|
+
}
|
|
377
|
+
ctx.logger?.info?.("cpa: generate_media tool unregistered");
|
|
378
|
+
}
|
|
379
|
+
// The generation commands follow the same switch (see commands.js).
|
|
380
|
+
shared.syncMediaCommands?.();
|
|
381
|
+
};
|
|
382
|
+
// #endregion
|
|
383
|
+
|
|
384
|
+
const registerAgentTool = () => {
|
|
385
|
+
// The optional agent tool (registered only while switched on; see syncAgentTool).
|
|
386
|
+
ctx.inject(["tools"], (scope) => {
|
|
387
|
+
scope.effect(() => {
|
|
388
|
+
toolsCtx = scope;
|
|
389
|
+
syncAgentTool();
|
|
390
|
+
return () => {
|
|
391
|
+
cancelConfirms();
|
|
392
|
+
const off = toolOff;
|
|
393
|
+
toolOff = undefined;
|
|
394
|
+
toolsCtx = undefined;
|
|
395
|
+
try {
|
|
396
|
+
off?.();
|
|
397
|
+
} catch {
|
|
398
|
+
// disposed with its scope
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
}, "cpa: agent tool");
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
return { pendingConfirms, syncAgentTool, registerAgentTool };
|
|
406
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Slash-command registration and ordered command disposal; the generation
|
|
2
|
+
// commands follow the Generation tab's master switch (syncMediaCommands).
|
|
3
|
+
|
|
4
|
+
import { renderQuotaText, renderStatusText } from "../status-text.js";
|
|
5
|
+
|
|
6
|
+
export function createCommands(shared) {
|
|
7
|
+
const { ctx, controller, quota, readMedia, runImageCommand, runVideoCommand, runVideoEditCommand, runVideoStatusCommand, syncAgentTool } = shared;
|
|
8
|
+
// The generation slash commands (registered only while generation is on).
|
|
9
|
+
let commandsCtx;
|
|
10
|
+
let mediaCommandsOff;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Register or unregister /cpa-image and the /cpa-video* commands to match
|
|
14
|
+
* the Generation tab's master switch. Jobs already submitted keep running.
|
|
15
|
+
*/
|
|
16
|
+
const syncMediaCommands = () => {
|
|
17
|
+
let wanted = false;
|
|
18
|
+
try {
|
|
19
|
+
wanted = readMedia().settings.enabled === true;
|
|
20
|
+
} catch {
|
|
21
|
+
wanted = false;
|
|
22
|
+
}
|
|
23
|
+
if (wanted && mediaCommandsOff === undefined && commandsCtx !== undefined) {
|
|
24
|
+
try {
|
|
25
|
+
mediaCommandsOff = registerMediaCommands(commandsCtx);
|
|
26
|
+
ctx.logger?.info?.("cpa: generation commands registered");
|
|
27
|
+
} catch (error) {
|
|
28
|
+
ctx.logger?.warn?.(`cpa: could not register the generation commands: ${error instanceof Error ? error.message : String(error)}`);
|
|
29
|
+
}
|
|
30
|
+
} else if (!wanted && mediaCommandsOff !== undefined) {
|
|
31
|
+
const off = mediaCommandsOff;
|
|
32
|
+
mediaCommandsOff = undefined;
|
|
33
|
+
try {
|
|
34
|
+
off();
|
|
35
|
+
} catch {
|
|
36
|
+
// already gone with its scope
|
|
37
|
+
}
|
|
38
|
+
ctx.logger?.info?.("cpa: generation commands unregistered");
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Register slash commands (/cpa-status, /cpa-refresh)
|
|
43
|
+
ctx.inject(["commands"], (cmdCtx) => {
|
|
44
|
+
cmdCtx.effect(() => {
|
|
45
|
+
const offStatus = cmdCtx.commands.register({
|
|
46
|
+
name: "cpa-status",
|
|
47
|
+
description: "Show CLIProxyAPI discovery and model synchronization status of every account.",
|
|
48
|
+
handler: async () => {
|
|
49
|
+
const payload = await controller.statusPayload();
|
|
50
|
+
const quotaText = renderQuotaText(await quota.snapshot());
|
|
51
|
+
return { kind: "success", text: [renderStatusText(payload), quotaText].filter(Boolean).join("\n\n") };
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const offRefresh = cmdCtx.commands.register({
|
|
55
|
+
name: "cpa-refresh",
|
|
56
|
+
description: "Probe every CLIProxyAPI account now and refresh the synchronized DSH model catalog.",
|
|
57
|
+
handler: async () => {
|
|
58
|
+
const payload = await controller.refresh(undefined);
|
|
59
|
+
syncAgentTool();
|
|
60
|
+
const failed = payload.accounts.some((a) => a.status.state === "error");
|
|
61
|
+
return { kind: failed ? "error" : "success", text: renderStatusText(payload) };
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
commandsCtx = cmdCtx;
|
|
65
|
+
syncMediaCommands();
|
|
66
|
+
return () => {
|
|
67
|
+
offStatus?.();
|
|
68
|
+
offRefresh?.();
|
|
69
|
+
commandsCtx = undefined;
|
|
70
|
+
const off = mediaCommandsOff;
|
|
71
|
+
mediaCommandsOff = undefined;
|
|
72
|
+
try {
|
|
73
|
+
off?.();
|
|
74
|
+
} catch {
|
|
75
|
+
// disposed with its scope
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}, "cpa: commands");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/** The generation commands, registered only while generation is switched on (see syncMediaCommands). */
|
|
82
|
+
function registerMediaCommands(cmdCtx) {
|
|
83
|
+
const offVideo = cmdCtx.commands.register({
|
|
84
|
+
name: "cpa-video",
|
|
85
|
+
description: "Generate a video with a CLIProxyAPI video model: text only, one image as the first frame, reference images (--ref, or 2+ images), or pinned first / last frames (--first-last, --last). A submitted video cannot be cancelled.",
|
|
86
|
+
input: { hint: "[-m <model>] [--aspect 16:9] [-s 1-15] [-r 480p|720p] [--ref|--first-last|--last] <prompt>", attachments: true },
|
|
87
|
+
handler: runVideoCommand,
|
|
88
|
+
});
|
|
89
|
+
const offVideoEdit = cmdCtx.commands.register({
|
|
90
|
+
name: "cpa-video-edit",
|
|
91
|
+
description: "Edit any MP4 of up to 8.7 s with grok-imagine-video: attach it, or pass --video <path>, --url <public URL> or --from <command id> (default: this session's newest video). A submitted video cannot be cancelled.",
|
|
92
|
+
input: { hint: "[--video <path> | --url <URL> | --from <command id>] [-m <model>] <what to change>", attachments: true },
|
|
93
|
+
handler: runVideoEditCommand("edit"),
|
|
94
|
+
});
|
|
95
|
+
const offVideoExtend = cmdCtx.commands.register({
|
|
96
|
+
name: "cpa-video-extend",
|
|
97
|
+
description: "Continue any MP4 of 2-15 s by 2-10 seconds: attach it, or pass --video <path>, --url <public URL> or --from <command id> (default: this session's newest video). A submitted video cannot be cancelled.",
|
|
98
|
+
input: { hint: "[--video <path> | --url <URL> | --from <command id>] [-m <model>] [-s 2-10] <what happens next>", attachments: true },
|
|
99
|
+
handler: runVideoEditCommand("extend"),
|
|
100
|
+
});
|
|
101
|
+
const offVideoStatus = cmdCtx.commands.register({
|
|
102
|
+
name: "cpa-video-status",
|
|
103
|
+
description: "List this session's CLIProxyAPI video jobs and their state.",
|
|
104
|
+
handler: runVideoStatusCommand,
|
|
105
|
+
});
|
|
106
|
+
const offImage = cmdCtx.commands.register({
|
|
107
|
+
name: "cpa-image",
|
|
108
|
+
description: "Generate an image with a CLIProxyAPI image model; attach images to edit them. Files go to the session's working directory.",
|
|
109
|
+
input: { hint: "[-m <model>] [--aspect 16:9] [-n 1-4] <prompt>", attachments: true },
|
|
110
|
+
handler: runImageCommand,
|
|
111
|
+
});
|
|
112
|
+
return () => {
|
|
113
|
+
offImage?.();
|
|
114
|
+
offVideo?.();
|
|
115
|
+
offVideoStatus?.();
|
|
116
|
+
offVideoEdit?.();
|
|
117
|
+
offVideoExtend?.();
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return { syncMediaCommands };
|
|
122
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Credentials, media preferences, account controller and refresh scheduling.
|
|
2
|
+
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { accountKindOverrides, loadMediaSettings } from "../../media/index.js";
|
|
6
|
+
import { outsideHmrTransaction } from "../apply-utils.js";
|
|
7
|
+
import { toCredentialRef, getEnvValue } from "../config.js";
|
|
8
|
+
import { createCpaController } from "../controller.js";
|
|
9
|
+
|
|
10
|
+
export function createController(shared) {
|
|
11
|
+
const { ctx, readExistingPiAiRoute, readDefaultModel, readState, writeState, updateDirectory } = shared;
|
|
12
|
+
shared.timer = undefined;
|
|
13
|
+
shared.startupTimeout = undefined;
|
|
14
|
+
|
|
15
|
+
const resolveApiKey = async (refName) => {
|
|
16
|
+
try {
|
|
17
|
+
const ref = toCredentialRef(refName);
|
|
18
|
+
const credentials = ctx.get("credentials");
|
|
19
|
+
const stored = credentials === undefined ? undefined : await credentials.resolve(ref);
|
|
20
|
+
const raw = stored?.value ?? getEnvValue(ctx, ref);
|
|
21
|
+
const value = raw?.trim();
|
|
22
|
+
return value === undefined || value === "" ? undefined : value;
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const describeApiKey = async (refName) => {
|
|
29
|
+
try {
|
|
30
|
+
const ref = toCredentialRef(refName);
|
|
31
|
+
const credentials = ctx.get("credentials");
|
|
32
|
+
if (credentials !== undefined) {
|
|
33
|
+
return await credentials.describe(ref);
|
|
34
|
+
}
|
|
35
|
+
const envVal = getEnvValue(ctx, ref);
|
|
36
|
+
return {
|
|
37
|
+
configured: envVal !== undefined && envVal.length > 0,
|
|
38
|
+
writable: false,
|
|
39
|
+
};
|
|
40
|
+
} catch {
|
|
41
|
+
return { configured: false, writable: true };
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const reschedule = () => {
|
|
46
|
+
if (shared.timer !== undefined) clearInterval(shared.timer);
|
|
47
|
+
shared.timer = outsideHmrTransaction(ctx, () =>
|
|
48
|
+
setInterval(() => {
|
|
49
|
+
void controller.refreshAll().finally(() => shared.syncAgentTool());
|
|
50
|
+
}, readState().refreshIntervalSeconds * 1000)
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Media preferences (lib/media/settings.js) live in their own file next to the
|
|
55
|
+
// account state, never inside it, so no state migration is involved.
|
|
56
|
+
const mediaSettingsPath = join(updateDirectory, "media-settings.json");
|
|
57
|
+
let mediaWarned = false;
|
|
58
|
+
// Generation is off for a new profile; one that already picked a model or
|
|
59
|
+
// generated something before the switch existed (session or job file, no
|
|
60
|
+
// readable settings file) keeps it on.
|
|
61
|
+
const mediaUsedBefore = () =>
|
|
62
|
+
existsSync(join(updateDirectory, "media-sessions.json")) || existsSync(join(updateDirectory, "media-jobs.json"));
|
|
63
|
+
const readMedia = () => {
|
|
64
|
+
const loaded = loadMediaSettings(mediaSettingsPath);
|
|
65
|
+
if ((loaded.error !== undefined || !existsSync(mediaSettingsPath)) && mediaUsedBefore()) {
|
|
66
|
+
loaded.settings = { ...loaded.settings, enabled: true };
|
|
67
|
+
}
|
|
68
|
+
if (loaded.error === undefined) {
|
|
69
|
+
mediaWarned = false;
|
|
70
|
+
} else if (!mediaWarned) {
|
|
71
|
+
mediaWarned = true;
|
|
72
|
+
ctx.logger?.warn?.(
|
|
73
|
+
`dsh-cliproxyapi: ${mediaSettingsPath} is unreadable; default media settings apply until it is saved again (the damaged file is then kept as *.corrupt-*.json)`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return loaded;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const controller = createCpaController({
|
|
80
|
+
settings: ctx.settings,
|
|
81
|
+
readState,
|
|
82
|
+
writeState,
|
|
83
|
+
mediaKindOverrides: (id) => accountKindOverrides(readMedia().settings, id),
|
|
84
|
+
resolveApiKey,
|
|
85
|
+
describeApiKey,
|
|
86
|
+
setCredential: async (ref, value) => {
|
|
87
|
+
const credentials = ctx.get("credentials");
|
|
88
|
+
if (credentials === undefined) {
|
|
89
|
+
throw new Error("Credentials service is not available on this host");
|
|
90
|
+
}
|
|
91
|
+
await credentials.set(ref, value);
|
|
92
|
+
},
|
|
93
|
+
unsetCredential: async (ref) => {
|
|
94
|
+
await ctx.get("credentials")?.unset(ref);
|
|
95
|
+
},
|
|
96
|
+
readExistingPiAiRoute,
|
|
97
|
+
readDefaultModel,
|
|
98
|
+
setTimeout: (fn, ms) => outsideHmrTransaction(ctx, () => setTimeout(fn, ms)),
|
|
99
|
+
environmentBaseURL: () => getEnvValue(ctx, "CLIPROXYAPI_BASE_URL"),
|
|
100
|
+
logger: ctx.logger,
|
|
101
|
+
onGlobalsChanged: () => reschedule(),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return { resolveApiKey, describeApiKey, reschedule, mediaSettingsPath, readMedia, controller };
|
|
105
|
+
}
|