@wwp9298/canvas-agent 0.3.0 → 0.3.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/README.md +7 -1
- package/package.json +1 -1
- package/remote.mjs +471 -3
package/README.md
CHANGED
|
@@ -60,7 +60,13 @@ node /path/to/infinite-canvas/canvas-agent/index.mjs mcp --remote --base-url htt
|
|
|
60
60
|
- `create_canvas`:凭空新建画布,返回画布 ID 与 `/canvas/<id>` 链接;
|
|
61
61
|
- `list_canvases` / `get_canvas_summary` / `get_node` / `query_canvas_nodes`:读画布;
|
|
62
62
|
- `create_text_node` / `update_text_node` / `update_node` / `delete_node` / `create_connection` / `delete_connection` / `create_group` / `arrange_nodes`:写画布,走服务端 ops 增量合并;
|
|
63
|
-
- `generate_image` / `generate_video` / `generate_audio` + `get_media_task_status
|
|
63
|
+
- `generate_image` / `generate_video` / `generate_audio` + `get_media_task_status`:生成素材(需要密钥带「触发生成」权限,按密钥所属账号扣点);
|
|
64
|
+
- `upload_media`:把本机文件、远程 URL 或 base64 传成 `server:<id>` 引用,再交给节点或生成参考(站点需为服务端对象存储,浏览器本地存储模式会明确拒绝);
|
|
65
|
+
- `create_media_node`:把已有素材直接放成图片/视频/音频节点,不触发生成;
|
|
66
|
+
- `list_media_tasks`:以画布节点为准汇总任务,直接给出失败项、错误原因与卡住的节点,定位「哪一格挂了」;
|
|
67
|
+
- `retry_media_task` / `cancel_media_task`:按节点重跑或取消任务,不必重新描述一遍;
|
|
68
|
+
- `get_capabilities`:运行时自检(站点连通、账号、模型、存储模式、工具清单),开新会话先跑它;
|
|
69
|
+
- `open_canvas`:返回可直接打开的画布链接,用于交付。
|
|
64
70
|
|
|
65
71
|
写画布不需要浏览器,也不需要打开任何页面;如果同一个画布正在浏览器里打开,网页会通过增量通道实时看到 Agent 的改动。远程模式不继承网页内 Agent 的会话记忆与技能文件,只提供上述工具。
|
|
66
72
|
|
package/package.json
CHANGED
package/remote.mjs
CHANGED
|
@@ -77,6 +77,7 @@ export class CanvasRemoteClient {
|
|
|
77
77
|
this.fetchImpl = fetchImpl || globalThis.fetch;
|
|
78
78
|
this.timeout = timeout;
|
|
79
79
|
this.modelsCache = null;
|
|
80
|
+
this.storageCache = null;
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
async request(path, { method = "GET", body, timeout } = {}) {
|
|
@@ -193,6 +194,19 @@ export class CanvasRemoteClient {
|
|
|
193
194
|
return this.modelsCache;
|
|
194
195
|
}
|
|
195
196
|
|
|
197
|
+
// server_* 才能由 Agent 直传;local_indexeddb 只有浏览器本地库。
|
|
198
|
+
async loadStorageConfig() {
|
|
199
|
+
if (this.storageCache) return this.storageCache;
|
|
200
|
+
try {
|
|
201
|
+
const response = await this.fetchImpl(this.baseUrl + "/api/storage/config");
|
|
202
|
+
const payload = await response.json();
|
|
203
|
+
this.storageCache = payload?.data || { mode: "" };
|
|
204
|
+
} catch {
|
|
205
|
+
this.storageCache = { mode: "" };
|
|
206
|
+
}
|
|
207
|
+
return this.storageCache;
|
|
208
|
+
}
|
|
209
|
+
|
|
196
210
|
async resolveModel(kind, model, channelId) {
|
|
197
211
|
const catalog = await this.loadModelCatalog();
|
|
198
212
|
const picked = model || catalog.defaults[kind] || "";
|
|
@@ -203,7 +217,7 @@ export class CanvasRemoteClient {
|
|
|
203
217
|
return { model: picked, channelId: owners[0].id };
|
|
204
218
|
}
|
|
205
219
|
|
|
206
|
-
async createMediaTask(kind, { nodeId, prompt, model, channelId, extra }) {
|
|
220
|
+
async createMediaTask(kind, { nodeId, prompt, model, channelId, extra, projectId }) {
|
|
207
221
|
const resolved = await this.resolveModel(kind, model, channelId);
|
|
208
222
|
if (kind === "image") {
|
|
209
223
|
return this.request("/api/v1/canvas/image-tasks", {
|
|
@@ -231,15 +245,141 @@ export class CanvasRemoteClient {
|
|
|
231
245
|
}
|
|
232
246
|
return this.request("/api/v1/videos", {
|
|
233
247
|
method: "POST",
|
|
234
|
-
body: {
|
|
248
|
+
body: {
|
|
249
|
+
model: resolved.model,
|
|
250
|
+
prompt,
|
|
251
|
+
channelId: resolved.channelId,
|
|
252
|
+
// 让视频任务能回溯到画布:列表接口只认 source/source_id。
|
|
253
|
+
...(projectId ? { source: "canvas", sourceId: projectId } : {}),
|
|
254
|
+
...extra,
|
|
255
|
+
},
|
|
235
256
|
});
|
|
236
257
|
}
|
|
237
|
-
|
|
238
258
|
async getMediaTask(kind, taskId) {
|
|
239
259
|
if (kind === "audio") return this.request("/api/v1/canvas/audio-tasks/" + encodeURIComponent(taskId));
|
|
240
260
|
if (kind === "video") return this.request("/api/v1/videos/" + encodeURIComponent(taskId));
|
|
241
261
|
return this.request("/api/v1/canvas/image-tasks/" + encodeURIComponent(taskId));
|
|
242
262
|
}
|
|
263
|
+
|
|
264
|
+
// ---------- 素材上传 ----------
|
|
265
|
+
|
|
266
|
+
// multipart 走独立通道:不能带 content-type,boundary 由 fetch 生成。
|
|
267
|
+
async requestMultipart(path, formData) {
|
|
268
|
+
const controller = new AbortController();
|
|
269
|
+
const timer = setTimeout(() => controller.abort(), Math.max(this.timeout, 180_000));
|
|
270
|
+
let response;
|
|
271
|
+
try {
|
|
272
|
+
response = await this.fetchImpl(this.baseUrl + path, {
|
|
273
|
+
method: "POST",
|
|
274
|
+
headers: { authorization: "Bearer " + this.key, accept: "application/json" },
|
|
275
|
+
body: formData,
|
|
276
|
+
signal: controller.signal,
|
|
277
|
+
});
|
|
278
|
+
} catch (error) {
|
|
279
|
+
const reason = error?.name === "AbortError" ? "上传超时" : error?.message || "网络异常";
|
|
280
|
+
throw new Error("上传到 " + this.baseUrl + " 失败:" + reason);
|
|
281
|
+
} finally {
|
|
282
|
+
clearTimeout(timer);
|
|
283
|
+
}
|
|
284
|
+
const text = await response.text();
|
|
285
|
+
let payload;
|
|
286
|
+
try {
|
|
287
|
+
payload = text ? JSON.parse(text) : {};
|
|
288
|
+
} catch {
|
|
289
|
+
throw new Error("服务端返回非 JSON(HTTP " + response.status + "):" + text.slice(0, 200));
|
|
290
|
+
}
|
|
291
|
+
if (!response.ok) throw new Error(payload?.msg || "HTTP " + response.status);
|
|
292
|
+
if (payload && typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "上传失败");
|
|
293
|
+
return payload?.data;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// 三种来源:本地文件路径、远程 URL、显式 base64。
|
|
297
|
+
async uploadMedia({ filePath, url, base64, filename, canvasId, mimeType } = {}) {
|
|
298
|
+
// 存储模式决定能不能直传:local_indexeddb 只有浏览器侧 IndexedDB,服务端没有对象存储。
|
|
299
|
+
const storage = await this.loadStorageConfig();
|
|
300
|
+
if (storage.mode && storage.mode !== "server_sqlite_s3" && !String(storage.mode).startsWith("server")) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
"该站点存储模式为 " + storage.mode + "(浏览器本地),Agent 无法直接上传素材。请让管理员在后台配置服务端对象存储后再试。",
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
const { readFile } = await import("node:fs/promises");
|
|
306
|
+
const { basename } = await import("node:path");
|
|
307
|
+
let bytes;
|
|
308
|
+
let name = filename || "";
|
|
309
|
+
let type = mimeType || "";
|
|
310
|
+
|
|
311
|
+
if (filePath) {
|
|
312
|
+
bytes = await readFile(filePath);
|
|
313
|
+
if (!name) name = basename(filePath);
|
|
314
|
+
} else if (base64) {
|
|
315
|
+
const raw = String(base64).replace(/^data:[^;]+;base64,/, "");
|
|
316
|
+
bytes = Buffer.from(raw, "base64");
|
|
317
|
+
if (!name) name = "upload.bin";
|
|
318
|
+
const match = /^data:([^;]+);base64,/.exec(String(base64));
|
|
319
|
+
if (!type && match) type = match[1];
|
|
320
|
+
} else if (url) {
|
|
321
|
+
const response = await this.fetchImpl(url);
|
|
322
|
+
if (!response.ok) throw new Error("下载素材失败:HTTP " + response.status);
|
|
323
|
+
bytes = Buffer.from(await response.arrayBuffer());
|
|
324
|
+
if (!type) type = response.headers.get("content-type") || "";
|
|
325
|
+
if (!name) {
|
|
326
|
+
const pathname = (() => { try { return new URL(url).pathname; } catch { return ""; } })();
|
|
327
|
+
name = basename(pathname) || "download.bin";
|
|
328
|
+
}
|
|
329
|
+
} else {
|
|
330
|
+
throw new Error("请提供 filePath、url 或 base64 之一");
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (!bytes?.length) throw new Error("素材为空,未上传");
|
|
334
|
+
const blob = new Blob([bytes], type ? { type } : undefined);
|
|
335
|
+
const form = new FormData();
|
|
336
|
+
form.append("file", blob, name);
|
|
337
|
+
if (canvasId) form.append("canvasId", canvasId);
|
|
338
|
+
const object = await this.requestMultipart("/api/v1/files", form);
|
|
339
|
+
if (!object?.id) throw new Error("上传成功但服务端没有返回文件 ID");
|
|
340
|
+
return {
|
|
341
|
+
fileId: object.id,
|
|
342
|
+
ref: "server:" + object.id,
|
|
343
|
+
url: object.publicUrl || "",
|
|
344
|
+
contentUrl: "/api/files/" + object.id + "/content",
|
|
345
|
+
mimeType: object.mimeType || type || "",
|
|
346
|
+
bytes: object.bytes ?? bytes.length,
|
|
347
|
+
width: object.width || 0,
|
|
348
|
+
height: object.height || 0,
|
|
349
|
+
filename: name,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// ---------- 任务总览 / 重试 / 取消 ----------
|
|
354
|
+
|
|
355
|
+
// audio 没有列表接口,只能按 taskId 单查;这里只覆盖 image / video。
|
|
356
|
+
async listTasks(kind) {
|
|
357
|
+
if (kind === "video") return (await this.request("/api/v1/video-tasks")) || [];
|
|
358
|
+
if (kind === "audio") return [];
|
|
359
|
+
return (await this.request("/api/v1/canvas/image-tasks")) || [];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async deleteTask(kind, taskId) {
|
|
363
|
+
// 音频任务没有删除接口,服务端只提供查询。
|
|
364
|
+
if (kind === "audio") throw new Error("音频任务暂不支持取消,服务端未开放删除接口");
|
|
365
|
+
const path = kind === "video" ? "/api/v1/video-tasks/" : "/api/v1/canvas/image-tasks/";
|
|
366
|
+
return this.request(path + encodeURIComponent(taskId), { method: "DELETE" });
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// ---------- 运行时自检 ----------
|
|
370
|
+
|
|
371
|
+
async health() {
|
|
372
|
+
const response = await this.fetchImpl(this.baseUrl + "/api/health");
|
|
373
|
+
return { ok: response.ok, status: response.status };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
async currentUser() {
|
|
377
|
+
const response = await this.fetchImpl(this.baseUrl + "/api/auth/me", {
|
|
378
|
+
headers: { authorization: "Bearer " + this.key, accept: "application/json" },
|
|
379
|
+
});
|
|
380
|
+
const payload = await response.json();
|
|
381
|
+
return payload?.data || null;
|
|
382
|
+
}
|
|
243
383
|
}
|
|
244
384
|
|
|
245
385
|
// ---------- 工具定义 ----------
|
|
@@ -611,6 +751,333 @@ export function createRemoteTools(client) {
|
|
|
611
751
|
return { ok: true, kind, task };
|
|
612
752
|
},
|
|
613
753
|
},
|
|
754
|
+
{
|
|
755
|
+
name: "upload_media",
|
|
756
|
+
description: "上传素材到站点,返回 server:<id> 引用,可直接写进节点 content 或作为生成参考。三种来源选一种:filePath(本机绝对路径)、url(远程地址)、base64。",
|
|
757
|
+
inputSchema: schema({
|
|
758
|
+
projectId: PROJECT_ID,
|
|
759
|
+
filePath: { type: "string", description: "本机文件绝对路径" },
|
|
760
|
+
url: { type: "string", description: "远程素材地址,由客户端下载后上传" },
|
|
761
|
+
base64: { type: "string", description: "data URL 或纯 base64" },
|
|
762
|
+
filename: { type: "string", description: "覆盖文件名" },
|
|
763
|
+
mimeType: { type: "string", description: "覆盖 MIME 类型" },
|
|
764
|
+
}),
|
|
765
|
+
handler: async (args) => {
|
|
766
|
+
const projectId = args.projectId || activeProjectId || "";
|
|
767
|
+
const asset = await client.uploadMedia({
|
|
768
|
+
filePath: args.filePath,
|
|
769
|
+
url: args.url,
|
|
770
|
+
base64: args.base64,
|
|
771
|
+
filename: args.filename,
|
|
772
|
+
mimeType: args.mimeType,
|
|
773
|
+
canvasId: projectId,
|
|
774
|
+
});
|
|
775
|
+
return { ok: true, projectId, ...asset };
|
|
776
|
+
},
|
|
777
|
+
},
|
|
778
|
+
{
|
|
779
|
+
name: "create_media_node",
|
|
780
|
+
description: "在画布上放一个已有素材节点(图片/视频/音频),不触发生成。content 可传 server:<id>、上传返回的 ref,或完整 URL。",
|
|
781
|
+
inputSchema: schema({
|
|
782
|
+
projectId: PROJECT_ID,
|
|
783
|
+
type: { type: "string", description: "image | video | audio | text" },
|
|
784
|
+
title: { type: "string" },
|
|
785
|
+
content: { type: "string", description: "素材引用:server:<id> 或 https URL" },
|
|
786
|
+
prompt: { type: "string", description: "可选,记录提示词" },
|
|
787
|
+
sourceNodeIds: { type: "array", items: { type: "string" }, description: "上游节点,会同时连线" },
|
|
788
|
+
width: { type: "number" },
|
|
789
|
+
height: { type: "number" },
|
|
790
|
+
}, ["type", "content"]),
|
|
791
|
+
handler: async (args) => {
|
|
792
|
+
const type = ["image", "video", "audio", "text"].includes(args.type) ? args.type : "image";
|
|
793
|
+
const { id, detail } = await canvas(args.projectId);
|
|
794
|
+
const nodes = detail.project.nodes;
|
|
795
|
+
const sources = nodes.filter((node) => (args.sourceNodeIds || []).includes(node.id));
|
|
796
|
+
const size = { width: args.width || NODE_SPECS[type].width, height: args.height || NODE_SPECS[type].height };
|
|
797
|
+
const node = makeNode(type, {
|
|
798
|
+
title: args.title || type,
|
|
799
|
+
content: args.content,
|
|
800
|
+
prompt: args.prompt,
|
|
801
|
+
position: nextPosition(nodes, sources, size),
|
|
802
|
+
width: size.width,
|
|
803
|
+
height: size.height,
|
|
804
|
+
metadata: { status: "success" },
|
|
805
|
+
});
|
|
806
|
+
const connections = sources.map((source) => ({ id: shortId("conn-"), fromNodeId: source.id, toNodeId: node.id }));
|
|
807
|
+
const result = await client.submitOps(id, [
|
|
808
|
+
{ type: "upsertNode", node },
|
|
809
|
+
...connections.map((connection) => ({ type: "upsertConnection", connection })),
|
|
810
|
+
]);
|
|
811
|
+
return { ok: true, projectId: id, nodeId: node.id, connectionIds: connections.map((item) => item.id), revision: result.revision };
|
|
812
|
+
},
|
|
813
|
+
},
|
|
814
|
+
{
|
|
815
|
+
name: "list_media_tasks",
|
|
816
|
+
description: "汇总画布内的生成任务并定位失败项:以画布节点为准(读节点上的 taskId 与状态),再合并服务端进行中的任务。返回状态、进度、错误原因与对应节点。排查「哪个节点卡住/失败」先调它。",
|
|
817
|
+
inputSchema: schema({
|
|
818
|
+
projectId: PROJECT_ID,
|
|
819
|
+
kind: { type: "string", description: "image | video | audio | all(默认 all)" },
|
|
820
|
+
status: { type: "string", description: "只看某个状态:queued/running/success/error 等" },
|
|
821
|
+
limit: { type: "number", description: "最多返回多少条,默认 50" },
|
|
822
|
+
}),
|
|
823
|
+
handler: async (args) => {
|
|
824
|
+
const { id, detail } = await canvas(args.projectId);
|
|
825
|
+
const nodes = detail.project.nodes;
|
|
826
|
+
const kindArg = String(args.kind || "all").toLowerCase();
|
|
827
|
+
const wantKind = (type) => kindArg === "all" || kindArg === type;
|
|
828
|
+
const limit = Math.max(1, Math.min(200, Math.floor(Number(args.limit) || 50)));
|
|
829
|
+
const pick = (obj, ...keys) => {
|
|
830
|
+
for (const key of keys) {
|
|
831
|
+
if (obj && obj[key] !== undefined && obj[key] !== null && obj[key] !== "") return obj[key];
|
|
832
|
+
}
|
|
833
|
+
return "";
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
const collected = new Map();
|
|
837
|
+
const push = (item) => {
|
|
838
|
+
const key = item.taskId || "node:" + item.nodeId;
|
|
839
|
+
const existing = collected.get(key);
|
|
840
|
+
// 同一任务以线上状态为准,节点侧信息补齐标题
|
|
841
|
+
if (!existing || (existing.status !== item.status && item.fromTask)) collected.set(key, item);
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
// 1) 节点侧:画布上所有待跟踪的媒体节点
|
|
845
|
+
const tracked = nodes
|
|
846
|
+
.filter((node) => ["image", "video", "audio"].includes(node.type) && wantKind(node.type))
|
|
847
|
+
.filter((node) => node.metadata?.taskId || (node.metadata?.status && !["success", "idle"].includes(node.metadata.status)))
|
|
848
|
+
.slice(0, limit);
|
|
849
|
+
|
|
850
|
+
for (let i = 0; i < tracked.length; i += 8) {
|
|
851
|
+
const chunk = tracked.slice(i, i + 8);
|
|
852
|
+
await Promise.all(chunk.map(async (node) => {
|
|
853
|
+
const taskId = String(node.metadata?.taskId || "");
|
|
854
|
+
let task = null;
|
|
855
|
+
if (taskId) {
|
|
856
|
+
try {
|
|
857
|
+
task = await client.getMediaTask(node.type, taskId);
|
|
858
|
+
} catch {
|
|
859
|
+
task = null;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
push({
|
|
863
|
+
kind: node.type,
|
|
864
|
+
taskId,
|
|
865
|
+
nodeId: node.id,
|
|
866
|
+
nodeTitle: node.title || "",
|
|
867
|
+
status: String(pick(task, "status") || node.metadata?.status || "unknown"),
|
|
868
|
+
progress: Number(pick(task, "progress") ?? 0),
|
|
869
|
+
model: String(pick(task, "model") || node.metadata?.model || ""),
|
|
870
|
+
prompt: String(pick(task, "prompt") || node.metadata?.prompt || "").slice(0, 160),
|
|
871
|
+
error: String(pick(task, "error", "errorDetail", "error_detail") || node.metadata?.errorDetails || ""),
|
|
872
|
+
errorKind: String(pick(task, "errorKind", "error_kind") || ""),
|
|
873
|
+
errorHint: String(pick(task, "errorHint", "error_hint") || ""),
|
|
874
|
+
resultUrl: String(pick(task, "url", "image_url", "videoUrl", "video_url") || ""),
|
|
875
|
+
createdAt: pick(task, "createdAt", "created_at"),
|
|
876
|
+
fromTask: Boolean(task),
|
|
877
|
+
});
|
|
878
|
+
}));
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// 2) 服务端侧:进行中任务(接口只返回未完成项),用于发现节点没记 taskId 的孤儿任务
|
|
882
|
+
const nodeIds = new Set(nodes.map((node) => node.id));
|
|
883
|
+
const [imageTasks, videoTasks] = await Promise.all([
|
|
884
|
+
kindArg === "all" || kindArg === "image" ? client.listTasks("image").catch(() => []) : [],
|
|
885
|
+
kindArg === "all" || kindArg === "video" ? client.listTasks("video").catch(() => []) : [],
|
|
886
|
+
]);
|
|
887
|
+
const adopt = (kind, task) => {
|
|
888
|
+
const nodeId = String(pick(task, "node_id", "nodeId") || "");
|
|
889
|
+
const sourceId = String(pick(task, "source_id", "sourceId") || "");
|
|
890
|
+
if (!nodeIds.has(nodeId) && sourceId !== id) return;
|
|
891
|
+
const node = nodes.find((item) => item.id === nodeId);
|
|
892
|
+
push({
|
|
893
|
+
kind,
|
|
894
|
+
taskId: String(pick(task, "id") || ""),
|
|
895
|
+
nodeId,
|
|
896
|
+
nodeTitle: node?.title || "",
|
|
897
|
+
status: String(pick(task, "status") || "unknown"),
|
|
898
|
+
progress: Number(pick(task, "progress") ?? 0),
|
|
899
|
+
model: String(pick(task, "model") || ""),
|
|
900
|
+
prompt: String(pick(task, "prompt") || "").slice(0, 160),
|
|
901
|
+
error: String(pick(task, "error", "errorDetail", "error_detail") || ""),
|
|
902
|
+
errorKind: String(pick(task, "errorKind", "error_kind") || ""),
|
|
903
|
+
errorHint: String(pick(task, "errorHint", "error_hint") || ""),
|
|
904
|
+
resultUrl: String(pick(task, "url", "image_url", "videoUrl", "video_url") || ""),
|
|
905
|
+
createdAt: pick(task, "createdAt", "created_at"),
|
|
906
|
+
fromTask: true,
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
imageTasks.forEach((task) => adopt("image", task));
|
|
910
|
+
videoTasks.forEach((task) => adopt("video", task));
|
|
911
|
+
|
|
912
|
+
let items = Array.from(collected.values());
|
|
913
|
+
if (args.status) items = items.filter((item) => item.status === args.status);
|
|
914
|
+
items.sort((a, b) => String(b.createdAt || "").localeCompare(String(a.createdAt || "")));
|
|
915
|
+
|
|
916
|
+
const failed = items.filter((item) => ["error", "failed", "failure"].includes(item.status));
|
|
917
|
+
const running = items.filter((item) => ["queued", "pending", "processing", "running", "in_progress", "loading"].includes(item.status));
|
|
918
|
+
return {
|
|
919
|
+
ok: true,
|
|
920
|
+
projectId: id,
|
|
921
|
+
total: items.length,
|
|
922
|
+
failedCount: failed.length,
|
|
923
|
+
runningCount: running.length,
|
|
924
|
+
failed,
|
|
925
|
+
running,
|
|
926
|
+
items: items.slice(0, limit),
|
|
927
|
+
};
|
|
928
|
+
},
|
|
929
|
+
},
|
|
930
|
+
{
|
|
931
|
+
name: "retry_media_task",
|
|
932
|
+
description: "重跑某个节点上的生成任务(复用节点已有 prompt/model,可覆盖)。不新建节点,直接刷新它的状态与任务 ID。",
|
|
933
|
+
inputSchema: schema({
|
|
934
|
+
projectId: PROJECT_ID,
|
|
935
|
+
nodeId: NODE_ID,
|
|
936
|
+
prompt: { type: "string", description: "覆盖原提示词" },
|
|
937
|
+
model: { type: "string", description: "覆盖原模型" },
|
|
938
|
+
channelId: { type: "string" },
|
|
939
|
+
}, ["nodeId"]),
|
|
940
|
+
handler: async (args) => {
|
|
941
|
+
const { id, detail } = await canvas(args.projectId);
|
|
942
|
+
const node = detail.project.nodes.find((item) => item.id === args.nodeId);
|
|
943
|
+
if (!node) return { ok: false, code: "node_not_found", message: "找不到节点 " + args.nodeId };
|
|
944
|
+
const kind = node.type === "video" ? "video" : node.type === "audio" ? "audio" : "image";
|
|
945
|
+
if (!["image", "video", "audio"].includes(node.type)) {
|
|
946
|
+
return { ok: false, code: "invalid_node_type", message: "只有 image/video/audio 节点可以重跑" };
|
|
947
|
+
}
|
|
948
|
+
const prompt = args.prompt || node.metadata?.prompt || node.metadata?.content || "";
|
|
949
|
+
if (!prompt) return { ok: false, code: "missing_prompt", message: "节点没有提示词,请显式传 prompt" };
|
|
950
|
+
const upstreamIds = detail.project.connections
|
|
951
|
+
.filter((conn) => conn.toNodeId === node.id)
|
|
952
|
+
.map((conn) => conn.fromNodeId);
|
|
953
|
+
const extra = {};
|
|
954
|
+
if (node.metadata?.size) extra.size = node.metadata.size;
|
|
955
|
+
if (node.metadata?.seconds) extra.seconds = node.metadata.seconds;
|
|
956
|
+
if (node.metadata?.voice) extra.voice = node.metadata.voice;
|
|
957
|
+
|
|
958
|
+
await client.submitOps(id, [{
|
|
959
|
+
type: "upsertNode",
|
|
960
|
+
node: { ...node, metadata: { ...node.metadata, status: "loading", errorDetails: "", errorDetail: "" } },
|
|
961
|
+
}]);
|
|
962
|
+
let task;
|
|
963
|
+
try {
|
|
964
|
+
task = await client.createMediaTask(kind, {
|
|
965
|
+
nodeId: node.id,
|
|
966
|
+
prompt,
|
|
967
|
+
model: args.model || node.metadata?.model,
|
|
968
|
+
channelId: args.channelId,
|
|
969
|
+
extra,
|
|
970
|
+
projectId: id,
|
|
971
|
+
});
|
|
972
|
+
} catch (error) {
|
|
973
|
+
await client.submitOps(id, [{
|
|
974
|
+
type: "upsertNode",
|
|
975
|
+
node: { ...node, metadata: { ...node.metadata, status: "error", errorDetails: String(error.message || error) } },
|
|
976
|
+
}]).catch(() => {});
|
|
977
|
+
throw error;
|
|
978
|
+
}
|
|
979
|
+
const taskId = task?.id || task?.task_id || "";
|
|
980
|
+
await client.submitOps(id, [{
|
|
981
|
+
type: "upsertNode",
|
|
982
|
+
node: { ...node, metadata: { ...node.metadata, status: task?.status || "loading", taskId, model: task?.model || node.metadata?.model } },
|
|
983
|
+
}]).catch(() => {});
|
|
984
|
+
return { ok: true, projectId: id, nodeId: node.id, taskId, status: task?.status || "queued", kind, reuseSources: upstreamIds };
|
|
985
|
+
},
|
|
986
|
+
},
|
|
987
|
+
{
|
|
988
|
+
name: "cancel_media_task",
|
|
989
|
+
description: "取消/删除一个生成任务(图片、视频)。可同时传 nodeId 把节点状态标记为 cancelled。音频任务服务端未开放删除。",
|
|
990
|
+
inputSchema: schema({
|
|
991
|
+
projectId: PROJECT_ID,
|
|
992
|
+
taskId: { type: "string" },
|
|
993
|
+
kind: { type: "string", description: "image(默认)| video" },
|
|
994
|
+
nodeId: { type: "string", description: "可选,同步更新该节点状态" },
|
|
995
|
+
}, ["taskId"]),
|
|
996
|
+
handler: async (args) => {
|
|
997
|
+
const kind = args.kind === "video" ? "video" : "image";
|
|
998
|
+
const result = await client.deleteTask(kind, args.taskId);
|
|
999
|
+
let nodeUpdated = false;
|
|
1000
|
+
if (args.nodeId) {
|
|
1001
|
+
try {
|
|
1002
|
+
const { id, detail } = await canvas(args.projectId);
|
|
1003
|
+
const node = detail.project.nodes.find((item) => item.id === args.nodeId);
|
|
1004
|
+
if (node) {
|
|
1005
|
+
await client.submitOps(id, [{
|
|
1006
|
+
type: "upsertNode",
|
|
1007
|
+
node: { ...node, metadata: { ...node.metadata, status: "cancelled", taskId: "" } },
|
|
1008
|
+
}]);
|
|
1009
|
+
nodeUpdated = true;
|
|
1010
|
+
}
|
|
1011
|
+
} catch {
|
|
1012
|
+
nodeUpdated = false;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
return { ok: true, kind, taskId: args.taskId, deleted: true, nodeUpdated, result };
|
|
1016
|
+
},
|
|
1017
|
+
},
|
|
1018
|
+
{
|
|
1019
|
+
name: "get_capabilities",
|
|
1020
|
+
description: "运行时自检:站点连通性、当前账号、可用模型、画布数量、支持的节点类型与工具清单。开新会话或怀疑配置不对时先跑它。",
|
|
1021
|
+
inputSchema: schema({}),
|
|
1022
|
+
handler: async () => {
|
|
1023
|
+
const checks = {};
|
|
1024
|
+
try {
|
|
1025
|
+
checks.health = await client.health();
|
|
1026
|
+
} catch (error) {
|
|
1027
|
+
checks.health = { ok: false, error: String(error.message || error) };
|
|
1028
|
+
}
|
|
1029
|
+
try {
|
|
1030
|
+
const user = await client.currentUser();
|
|
1031
|
+
checks.account = user ? { id: user.id, username: user.username, displayName: user.displayName, role: user.role, credits: user.credits } : null;
|
|
1032
|
+
} catch (error) {
|
|
1033
|
+
checks.account = { error: String(error.message || error) };
|
|
1034
|
+
}
|
|
1035
|
+
let catalog = null;
|
|
1036
|
+
try {
|
|
1037
|
+
catalog = await client.loadModelCatalog();
|
|
1038
|
+
} catch (error) {
|
|
1039
|
+
checks.models = { error: String(error.message || error) };
|
|
1040
|
+
}
|
|
1041
|
+
let canvases = [];
|
|
1042
|
+
try {
|
|
1043
|
+
canvases = await client.listCanvases();
|
|
1044
|
+
} catch (error) {
|
|
1045
|
+
checks.canvases = { error: String(error.message || error) };
|
|
1046
|
+
}
|
|
1047
|
+
const storage = await client.loadStorageConfig();
|
|
1048
|
+
const canUpload = Boolean(storage.mode) && (storage.mode === "server_sqlite_s3" || String(storage.mode).startsWith("server"));
|
|
1049
|
+
return {
|
|
1050
|
+
ok: Boolean(checks.health?.ok),
|
|
1051
|
+
baseUrl: client.baseUrl,
|
|
1052
|
+
checks,
|
|
1053
|
+
storage: { mode: storage.mode || "", canUpload },
|
|
1054
|
+
defaults: catalog?.defaults || {},
|
|
1055
|
+
modelCount: catalog?.availableModels?.length || 0,
|
|
1056
|
+
channelCount: catalog?.channels?.length || 0,
|
|
1057
|
+
canvasCount: canvases.length,
|
|
1058
|
+
nodeTypes: Object.keys(NODE_SPECS),
|
|
1059
|
+
tools: tools.map((tool) => tool.name),
|
|
1060
|
+
activeProjectId,
|
|
1061
|
+
};
|
|
1062
|
+
},
|
|
1063
|
+
},
|
|
1064
|
+
{
|
|
1065
|
+
name: "open_canvas",
|
|
1066
|
+
description: "返回画布的可直接打开链接(浏览器里可继续编辑),用于交付给用户。",
|
|
1067
|
+
inputSchema: schema({ projectId: PROJECT_ID }),
|
|
1068
|
+
handler: async (args) => {
|
|
1069
|
+
const { id, detail } = await canvas(args.projectId);
|
|
1070
|
+
return {
|
|
1071
|
+
ok: true,
|
|
1072
|
+
projectId: id,
|
|
1073
|
+
title: detail.project.title || "未命名画布",
|
|
1074
|
+
url: client.baseUrl + "/canvas/" + encodeURIComponent(id),
|
|
1075
|
+
path: "/canvas/" + encodeURIComponent(id),
|
|
1076
|
+
nodeCount: detail.project.nodes.length,
|
|
1077
|
+
connectionCount: detail.project.connections.length,
|
|
1078
|
+
};
|
|
1079
|
+
},
|
|
1080
|
+
},
|
|
614
1081
|
];
|
|
615
1082
|
|
|
616
1083
|
async function createMedia(kind, args) {
|
|
@@ -644,6 +1111,7 @@ export function createRemoteTools(client) {
|
|
|
644
1111
|
model: args.model,
|
|
645
1112
|
channelId: args.channelId,
|
|
646
1113
|
extra,
|
|
1114
|
+
projectId: id,
|
|
647
1115
|
});
|
|
648
1116
|
} catch (error) {
|
|
649
1117
|
// 提交失败:把节点标成 error,避免画布上永远转圈。
|