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.
Files changed (84) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.en.md +24 -408
  3. package/README.md +24 -408
  4. package/lib/client.js +265 -27
  5. package/lib/host/accounts.js +178 -0
  6. package/lib/host/apply/agent-tool.js +406 -0
  7. package/lib/host/apply/commands.js +122 -0
  8. package/lib/host/apply/controller.js +121 -0
  9. package/lib/host/apply/http-routes.js +273 -0
  10. package/lib/host/apply/image-jobs.js +171 -0
  11. package/lib/host/apply/index.js +41 -0
  12. package/lib/host/apply/interpret.js +72 -0
  13. package/lib/host/apply/lifecycle.js +50 -0
  14. package/lib/host/apply/media-failures.js +68 -0
  15. package/lib/host/apply/media-service.js +202 -0
  16. package/lib/host/apply/quota-service.js +42 -0
  17. package/lib/host/apply/references.js +43 -0
  18. package/lib/host/apply/session-status.js +104 -0
  19. package/lib/host/apply/state.js +78 -0
  20. package/lib/host/apply/updates.js +55 -0
  21. package/lib/host/apply/video-command.js +152 -0
  22. package/lib/host/apply/video-edit.js +253 -0
  23. package/lib/host/apply/video-jobs.js +206 -0
  24. package/lib/host/apply-utils.js +39 -0
  25. package/lib/host/config.js +210 -0
  26. package/lib/host/controller.js +464 -0
  27. package/lib/host/discovery.js +301 -0
  28. package/lib/host/http-gate.js +144 -0
  29. package/lib/host/model-info.js +363 -0
  30. package/lib/host/model-names.js +152 -0
  31. package/lib/host/models.js +75 -0
  32. package/lib/host/mount-guard.js +56 -0
  33. package/lib/host/recommend.js +367 -0
  34. package/lib/host/routes.js +165 -0
  35. package/lib/host/runtime.js +471 -0
  36. package/lib/host/state-store.js +39 -0
  37. package/lib/host/status-text.js +93 -0
  38. package/lib/index.js +18 -4417
  39. package/lib/media/agent-tool.js +255 -0
  40. package/lib/media/choice.js +83 -0
  41. package/lib/media/classify.js +65 -0
  42. package/lib/media/codec/fit.js +76 -0
  43. package/lib/media/codec/jpeg.js +158 -0
  44. package/lib/media/codec/png.js +102 -0
  45. package/lib/media/commands/parsing.js +269 -0
  46. package/lib/media/constants.js +46 -0
  47. package/lib/media/diagnose.js +134 -0
  48. package/lib/media/images.js +165 -0
  49. package/lib/media/index.js +29 -0
  50. package/lib/media/interpret.js +126 -0
  51. package/lib/media/job-store.js +99 -0
  52. package/lib/media/mp4.js +44 -0
  53. package/lib/media/output-files.js +59 -0
  54. package/lib/media/session-store.js +293 -0
  55. package/lib/media/settings.js +255 -0
  56. package/lib/media/util.js +45 -0
  57. package/lib/media/video.js +326 -0
  58. package/lib/quota/auth-state.js +33 -0
  59. package/lib/quota/constants.js +86 -0
  60. package/lib/quota/controller.js +467 -0
  61. package/lib/quota/helpers.js +325 -0
  62. package/lib/quota/index.js +19 -0
  63. package/lib/quota/management.js +216 -0
  64. package/lib/quota/matching.js +143 -0
  65. package/lib/quota/parsers.js +352 -0
  66. package/lib/quota/probes/billing.js +80 -0
  67. package/lib/quota/probes/google.js +120 -0
  68. package/lib/quota/probes/oauth.js +27 -0
  69. package/lib/quota/probes/plugin.js +18 -0
  70. package/lib/updater/constants.js +23 -0
  71. package/lib/updater/errors.js +8 -0
  72. package/lib/updater/index.js +11 -0
  73. package/lib/updater/registry.js +83 -0
  74. package/lib/updater/settings.js +45 -0
  75. package/lib/updater/store.js +23 -0
  76. package/lib/updater/updater.js +148 -0
  77. package/lib/updater/verify.js +110 -0
  78. package/lib/updater/version.js +31 -0
  79. package/locale/en.json +1 -1
  80. package/locale/zh.json +1 -1
  81. package/package.json +6 -9
  82. package/lib/media.js +0 -2542
  83. package/lib/quota.js +0 -1748
  84. package/lib/updater.js +0 -443
@@ -0,0 +1,178 @@
1
+ // Persisted account normalization, migration, conflicts and account patches.
2
+
3
+ import { DEFAULT_ACCOUNT_ID, DEFAULT_PROVIDER, DEFAULT_DISPLAY_NAME, DEFAULT_API_KEY_ENV, STATE_VERSION, ACCOUNT_FIELDS, GLOBAL_FIELDS, normalizeStringList, normalizeModelOverrides, resolveConfig, normalizeAccountId } from "./config.js";
4
+ import { managedRouteKeys } from "./routes.js";
5
+
6
+ /** The persisted shape of one account: its settings without the globals. */
7
+ export function pickAccount(resolved) {
8
+ return {
9
+ id: resolved.id,
10
+ enabled: resolved.enabled,
11
+ provider: resolved.provider,
12
+ displayName: resolved.displayName,
13
+ baseURL: resolved.baseURL,
14
+ apiKeyEnv: resolved.apiKeyEnv,
15
+ protocol: resolved.protocol,
16
+ protocolMode: resolved.protocolMode,
17
+ includePrefix: resolved.includePrefix,
18
+ excludePrefix: resolved.excludePrefix,
19
+ ...(resolved.selectedModels === undefined ? {} : { selectedModels: resolved.selectedModels }),
20
+ modelOverrides: resolved.modelOverrides,
21
+ };
22
+ }
23
+
24
+ export function pickFields(source, fields) {
25
+ const out = {};
26
+ if (typeof source !== "object" || source === null) return out;
27
+ for (const field of fields) if (source[field] !== undefined) out[field] = source[field];
28
+ return out;
29
+ }
30
+
31
+ export function plainObject(value) {
32
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
33
+ }
34
+
35
+ /**
36
+ * Normalize the state file into `{ version, refreshIntervalSeconds,
37
+ * probeTimeoutMs, onlineModelInfo, accounts[] }` (a file without
38
+ * `onlineModelInfo` reads as on). A legacy single-account file (flat fields,
39
+ * no `accounts` array) becomes one account with id `default`, so its
40
+ * provider route, key reference and model selection carry over unchanged.
41
+ * Invalid or duplicate account ids are replaced with generated ones.
42
+ */
43
+ export function normalizeState(raw = {}) {
44
+ const source = plainObject(raw) ?? {};
45
+ const globals = resolveConfig(pickFields(source, GLOBAL_FIELDS));
46
+ const rawAccounts = Array.isArray(source.accounts)
47
+ ? source.accounts
48
+ : [{ ...pickFields(source, ACCOUNT_FIELDS), id: DEFAULT_ACCOUNT_ID }];
49
+ const accounts = [];
50
+ const ids = new Set();
51
+ for (const entry of rawAccounts) {
52
+ if (plainObject(entry) === undefined) continue;
53
+ let id = normalizeAccountId(entry.id);
54
+ if (id === undefined || ids.has(id)) {
55
+ let n = accounts.length + 1;
56
+ while (ids.has(`account-${n}`)) n += 1;
57
+ id = `account-${n}`;
58
+ }
59
+ ids.add(id);
60
+ accounts.push(pickAccount(resolveConfig({ ...pickFields(entry, ACCOUNT_FIELDS), id })));
61
+ }
62
+ return {
63
+ version: STATE_VERSION,
64
+ refreshIntervalSeconds: globals.refreshIntervalSeconds,
65
+ probeTimeoutMs: globals.probeTimeoutMs,
66
+ onlineModelInfo: globals.onlineModelInfo,
67
+ accounts,
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Overlay the host-supplied plugin config on the persisted state. A config
73
+ * with `accounts` replaces the account list; flat legacy fields apply to the
74
+ * first account, as they did when only one account existed.
75
+ */
76
+ export function mergeHostConfig(persisted, config) {
77
+ const host = plainObject(config) ?? {};
78
+ const base = plainObject(persisted) ?? {};
79
+ if (!Array.isArray(base.accounts) || Array.isArray(host.accounts)) return { ...base, ...host };
80
+ const flat = pickFields(host, ACCOUNT_FIELDS);
81
+ const accounts = [...base.accounts];
82
+ if (Object.keys(flat).length > 0 && accounts.length > 0) accounts[0] = { ...accounts[0], ...flat };
83
+ return { ...base, ...pickFields(host, GLOBAL_FIELDS), accounts };
84
+ }
85
+
86
+ /**
87
+ * Accounts whose managed route keys overlap an earlier account's. Each one
88
+ * maps to the id of the account that already owns the keys.
89
+ */
90
+ export function accountConflicts(accounts) {
91
+ const owners = new Map();
92
+ const conflicts = new Map();
93
+ for (const account of accounts) {
94
+ const keys = managedRouteKeys(account.provider);
95
+ const clash = keys.map((key) => owners.get(key)).find((owner) => owner !== undefined);
96
+ if (clash !== undefined) {
97
+ conflicts.set(account.id, clash);
98
+ continue;
99
+ }
100
+ for (const key of keys) owners.set(key, account.id);
101
+ }
102
+ return conflicts;
103
+ }
104
+
105
+ export function assertNoConflicts(accounts) {
106
+ const conflicts = accountConflicts(accounts);
107
+ for (const [id, ownerId] of conflicts) {
108
+ const account = accounts.find((a) => a.id === id);
109
+ const owner = accounts.find((a) => a.id === ownerId);
110
+ throw new Error(
111
+ `dsh-cliproxyapi: provider route "${account?.provider}" of account "${account?.displayName ?? id}" overlaps the routes of account "${owner?.displayName ?? ownerId}". Every account needs its own Provider Route ID.`
112
+ );
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Defaults for a newly added account: the first free `cli-proxy-N` route,
118
+ * its own credential reference, and disabled until the user fills it in.
119
+ * When no account owns `cli-proxy`, the new account takes the classic names.
120
+ */
121
+ export function newAccountDefaults(accounts) {
122
+ const usedIds = new Set(accounts.map((a) => a.id));
123
+ const usedKeys = new Set(accounts.flatMap((a) => managedRouteKeys(a.provider)));
124
+ const usedRefs = new Set(accounts.map((a) => a.apiKeyEnv));
125
+ const free = (provider) => managedRouteKeys(provider).every((key) => !usedKeys.has(key));
126
+ const base = {
127
+ enabled: false,
128
+ baseURL: "",
129
+ protocol: "openai-completions",
130
+ protocolMode: "auto",
131
+ includePrefix: "",
132
+ excludePrefix: "",
133
+ // No selectedModels: the first successful probe seeds recommendModels().
134
+ modelOverrides: {},
135
+ };
136
+ if (!usedIds.has(DEFAULT_ACCOUNT_ID) && free(DEFAULT_PROVIDER) && !usedRefs.has(DEFAULT_API_KEY_ENV)) {
137
+ return {
138
+ ...base,
139
+ id: DEFAULT_ACCOUNT_ID,
140
+ provider: DEFAULT_PROVIDER,
141
+ displayName: DEFAULT_DISPLAY_NAME,
142
+ apiKeyEnv: DEFAULT_API_KEY_ENV,
143
+ };
144
+ }
145
+ let n = Math.max(2, accounts.length + 1);
146
+ while (
147
+ usedIds.has(`account-${n}`) ||
148
+ !free(`${DEFAULT_PROVIDER}-${n}`) ||
149
+ usedRefs.has(`${DEFAULT_API_KEY_ENV}_${n}`)
150
+ ) {
151
+ n += 1;
152
+ }
153
+ return {
154
+ ...base,
155
+ id: `account-${n}`,
156
+ provider: `${DEFAULT_PROVIDER}-${n}`,
157
+ displayName: `${DEFAULT_DISPLAY_NAME} ${n}`,
158
+ apiKeyEnv: `${DEFAULT_API_KEY_ENV}_${n}`,
159
+ };
160
+ }
161
+
162
+ /**
163
+ * Apply an account patch from the UI. `selectedModels` and
164
+ * `modelOverrides` replace the stored value when present; the id never changes.
165
+ */
166
+ export function patchAccount(account, patch) {
167
+ const fields = pickFields(patch, ACCOUNT_FIELDS);
168
+ const next = { ...account, ...fields, id: account.id };
169
+ if (patch !== null && typeof patch === "object" && "selectedModels" in patch) {
170
+ next.selectedModels = Array.isArray(patch.selectedModels)
171
+ ? normalizeStringList(patch.selectedModels) ?? []
172
+ : [];
173
+ }
174
+ if (patch !== null && typeof patch === "object" && "modelOverrides" in patch) {
175
+ next.modelOverrides = normalizeModelOverrides(patch.modelOverrides);
176
+ }
177
+ return pickAccount(resolveConfig(next));
178
+ }
@@ -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
+ }