Skip to content

Commit d07918e

Browse files
authored
fix(ai-node-canvas): 出图超过 30 秒时改走 Agent 通道,不再直接失败 (#111)
宿主对一次连接器调用有 30 秒上限,超时会被中止并报「结果未知」。图片没有任务号可查, provider 那边做出来的那张也就找不回来,所以带参考图的慢速出图过去会直接失败一次。 现在出图超时不再失败:直连被掐断后,把直连本来要发的那份请求原样交给 Agent 跑一轮 (宿主不限制 Agent 回合的时长),图从那条路回来,并落一份本地副本。页面上不弹失败, 运行日志里写明换了路。 只有 CONNECTOR_TIMEOUT 和 CONNECTOR_OUTCOME_UNKNOWN 走这条路;provider 明确报错不降级。 版本 0.86.0 -> 0.87.0。
1 parent 9b27e90 commit d07918e

7 files changed

Lines changed: 167 additions & 17 deletions

File tree

‎plugins/antianqi/ai-node-canvas/.minimax-plugin/plugin.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"schemaVersion": 1,
33
"name": "ai-node-canvas",
44
"displayName": "节点画布",
5-
"version": "0.86.0",
5+
"version": "0.87.0",
66
"description": "无限画布节点编排:把 MiniMax 模型生成的文本、图片、视频串成工作流,连线即数据流,上游产物可直接被下游引用。",
77
"author": "antianqi",
88
"icon": "icon.png",

‎plugins/antianqi/ai-node-canvas/README.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ English | [简体中文](README.zh-CN.md)
55
An infinite-canvas node graph for MiniMax Code, modelled on the canvas of
66
`work.batvid.com`.
77

8-
Author: [antianqi](https://github.com/antianqi) · Version: `0.86.0`
8+
Author: [antianqi](https://github.com/antianqi) · Version: `0.87.0`
99

1010
![The canvas with the 图片复刻 template on it: a reference picture goes in, the prompt is read back out of that picture, and a new picture is made from the prompt](docs/preview.png)
1111

@@ -668,10 +668,15 @@ separate pictures.
668668
- **How long a connector call may take** — the Host puts a timeout on one
669669
connector call (30 seconds by default). A call that runs over it is aborted
670670
and reported as an unknown outcome: it may have been dispatched, and it may
671-
have been charged, so it must never be retried automatically. A slow
672-
generation (an image generation with a reference is the one that hits it
673-
most often) can therefore fail once; the message names the tool and how long
674-
it ran. Video is submitted and then polled, so it is not affected.
671+
have been charged, so it must never be retried automatically. A picture has no
672+
task number to ask about, so the one the provider makes cannot be found
673+
afterwards — and a slow generation (an image generation with a reference is
674+
the one that hits the limit most often) would be lost with it. So a picture
675+
that runs over the limit does not fail: the run goes on by the other road.
676+
The Agent is handed the very request the direct call would have sent and runs
677+
it as a turn, which the Host does not time out, and the picture comes back
678+
from there. The page is shown no failure; the run log says the road changed.
679+
Video is submitted and then polled, so it is not affected.
675680
- **Processes spawned** — `ffmpeg` and `ffprobe`, and nothing else. Both are
676681
looked up on the machine and run with an argument array, never through a
677682
shell, so no part of a path or a parameter can be read as shell syntax.

‎plugins/antianqi/ai-node-canvas/README.zh-CN.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
MiniMax Code 里的无限画布节点图。每个节点是一次模型运行,每条连线是一条数据通路——
66
把文本节点连到图片节点,上游文本会自动注入下游提示词。
77

8-
作者:[antianqi](https://github.com/antianqi) · 版本:`0.86.0`
8+
作者:[antianqi](https://github.com/antianqi) · 版本:`0.87.0`
99

1010
![画布:铺了「图片复刻」模板——参考图进去,提示词从这张图里反推出来,再照着它重新生成一张](docs/preview.png)
1111

@@ -419,8 +419,10 @@ provider 有**几种不同的请求**把素材变成视频,不是同一个请
419419
(回环地址除外),请求由运行时自己发出,**不走**宿主连接器。
420420
- **连接器调用的时限** — 宿主对一次连接器调用有超时上限(默认 30 秒),超时的调用会被
421421
中止并报「结果未知」:调用可能已经发出去了,也可能已经扣费,所以**不能自动重试**。
422-
慢的生成(带参考图的图片生成最容易碰到)因此可能失败一次,失败信息里会写明是哪一个
423-
工具、跑了多少秒。视频走「提交 + 轮询」,不受这个时限影响。
422+
图片没有任务号可查,provider 那边做出来的那张也就找不回来——出图超限**不会失败**:
423+
这次运行改走另一条路,把直连本来要发的那份请求原样交给 Agent 跑一轮(宿主不限制 Agent
424+
回合的时长),图从那条路回来。页面上不弹失败,运行日志里写明换了路。
425+
视频走「提交 + 轮询」,不受这个时限影响。
424426
- **启动的进程** — 只有 `ffmpeg` 与 `ffprobe`。两者都在本机查找,调用时用参数数组、
425427
绝不经过 shell,所以路径或参数里的任何字符都不会被当成 shell 语法。具体命令:
426428
- `ffmpeg -hide_banner -loglevel error -y <输入> …` — 每个媒体工具一次调用:

‎plugins/antianqi/ai-node-canvas/miniapp/node/agent-runner.mjs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const MODES = new Set(['image', 'video', 'audio', 'music', 'text']);
4242
// so it keeps the Agent turn.
4343
const DIRECT_MODES = new Set(['image', 'video', 'audio', 'music']);
4444

45+
// The codes that mean the Host stopped a connector call it had already sent:
46+
// the provider may still be working on it, and a picture has no task number
47+
// to ask about afterwards.
48+
const STOPPED_CODES = new Set(['CONNECTOR_TIMEOUT', 'CONNECTOR_OUTCOME_UNKNOWN']);
49+
4550
const ERROR_TEXT = {
4651
NOT_PERMITTED: '没有 Agent 访问权限(需要 MiniMax Code 3.1.2 或更新版本)',
4752
BUSY: 'Agent 正忙,请稍后重试',
@@ -383,6 +388,35 @@ function buildPrompt({ mode, segments, prompt, references, params, model, path =
383388
return { agentPrompt: sections.join('\n\n'), compiled, providerPrompt, assets };
384389
}
385390

391+
/**
392+
* What the Agent is told when the direct call was stopped.
393+
*
394+
* The request is the one the direct path would have sent, written out as the
395+
* tool call to make. The Agent decides nothing here: it is handed the same
396+
* request and asked to run it by the other road, because the Host does not
397+
* time a turn out the way it times a connector call out.
398+
*/
399+
function agentImageInstruction({ prompt, params, assets, count = 1 }) {
400+
const request = { prompt: String(prompt ?? '') };
401+
if (params?.ratio) request.aspect_ratio = params.ratio;
402+
if (params?.resolution) request.resolution = params.resolution;
403+
const urls = (assets?.images ?? [])
404+
.map((item) => String(item?.url ?? item ?? ''))
405+
.filter(Boolean);
406+
if (urls.length) request.input_urls = urls;
407+
// A batch is one call with one request per asset: the direct path gives
408+
// every asset a window of its own, but a turn has no such window.
409+
const total = Math.max(1, Number(count) || 1);
410+
const requests = [];
411+
for (let index = 0; index < total; index += 1) requests.push({ ...request });
412+
return [
413+
'刚才那条直连生成通道超时了,现在改走另一条路:请调用图片生成工具重新生成,只调用这一次。',
414+
'参数严格照抄下面这一份,不要改动,也不要额外生成别的:',
415+
JSON.stringify({ requests }),
416+
'生成之后,对每一张图片调用 get_asset_url 换成能访问的链接,并把每条链接都写在回复里。',
417+
].join('\n\n');
418+
}
419+
386420
export function createAgentRunner(context) {
387421
const tasks = new Map();
388422
const nodeTasks = new Map();
@@ -696,6 +730,9 @@ export function createAgentRunner(context) {
696730
assets: { images: [], videos: [], audios: [] },
697731
// Which path owns this run: the connector (direct) or an Agent turn.
698732
runner: 'agent',
733+
// Set when the direct call was stopped and the run went on by the
734+
// Agent road instead.
735+
degraded: false,
699736
providerTaskId: null,
700737
providerModel: null,
701738
providerStatus: null,
@@ -836,6 +873,9 @@ export function createAgentRunner(context) {
836873
// plan quota. The Host owns the credentials; no key is handled here.
837874
async function executeConnector(task, { mode, prompt, params, assets, modelId, count = 1, path, frames }) {
838875
const signal = armController(task);
876+
// Set when the direct call was stopped and the run goes on by the Agent
877+
// road instead.
878+
let degrade = false;
839879
try {
840880
const result = await connector.submit({ mode, prompt, params, assets, modelId, signal, count, path, frames });
841881
if (task.cancelled) {
@@ -876,6 +916,15 @@ export function createAgentRunner(context) {
876916
task.status = 'aborted';
877917
task.error = '';
878918
task.log.push('已取消');
919+
} else if (mode === 'image' && STOPPED_CODES.has(error?.code)) {
920+
// The Host stopped the call and the outcome is unknown: the provider
921+
// may still be working on it. A picture has no task number to ask
922+
// about, so the one it makes cannot be found afterwards.
923+
//
924+
// The page is not told the run failed. The run goes on by the other
925+
// road: the Host does not time an Agent turn out, so the same
926+
// request can run there as long as it needs.
927+
degrade = true;
879928
} else {
880929
task.status = 'error';
881930
task.error = describeError(error?.code ?? 'UNKNOWN', error?.message);
@@ -884,6 +933,27 @@ export function createAgentRunner(context) {
884933
} finally {
885934
releaseController(task);
886935
}
936+
if (degrade) {
937+
task.degraded = true;
938+
task.runner = 'agent';
939+
task.log.push('直连超过 30 秒,改走 Agent 通道重新生成');
940+
await execute(task, { text: agentImageInstruction({ prompt, params, assets, count }) });
941+
// The Agent's links are the provider's own and they expire, so the
942+
// canvas keeps a local copy: the node still has its picture later.
943+
if (task.status === 'done' && task.media?.length) {
944+
task.media = await Promise.all(task.media.map(async (item) => {
945+
const local = await store.publishRemote(item.url, item.kind).catch(() => null);
946+
return local ? { ...item, url: local } : item;
947+
}));
948+
}
949+
// A turn that ended without a picture is not a picture: the run says
950+
// so, instead of finishing with nothing to show.
951+
if (task.status === 'done' && !(task.media ?? []).length) {
952+
task.status = 'error';
953+
task.error = 'Agent 通道没有返回图片,请再试一次';
954+
task.log.push(`失败:${task.error}`);
955+
}
956+
}
887957
return task;
888958
}
889959

‎plugins/antianqi/ai-node-canvas/miniapp/node/connector-runner.mjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,23 @@ export function createConnectorRunner(context, options = {}) {
332332
// calls themselves took. The two are told apart, because only one of
333333
// them is the provider's answer.
334334
const timing = { waitedMs: 0, callMs: 0 };
335+
// The first failure keeps its own code: whether the Host stopped the call
336+
// or the provider refused it decides what the run does next, so the code
337+
// is not flattened into one thing.
338+
let firstError = null;
335339
for (let index = 0; index < wanted; index += 1) {
336340
try {
337341
media.push(...(await callOneAsset(tool, argsFor(index), kind, signal, timing)));
338342
} catch (error) {
343+
if (!firstError) firstError = error;
339344
warnings.push(`第 ${index + 1} 个没有生成出来:${error?.message ?? error}`);
340345
}
341346
}
342347
if (!media.length && warnings.length) {
343-
throw Object.assign(new Error(warnings[0].replace(/^第 \d+ 个没有生成出来:/, '')), { code: 'CONNECTOR_ERROR' });
348+
throw Object.assign(
349+
new Error(warnings[0].replace(/^第 \d+ 个没有生成出来:/, '')),
350+
{ code: firstError?.code ?? 'CONNECTOR_ERROR' },
351+
);
344352
}
345353
return { media, warnings, waitedMs: timing.waitedMs, callMs: timing.callMs };
346354
}

‎plugins/antianqi/ai-node-canvas/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ai-node-canvas",
3-
"version": "0.86.0",
3+
"version": "0.87.0",
44
"private": true,
55
"description": "Node-graph canvas MiniApp for MiniMax Code: wire MiniMax model runs into a visual workflow.",
66
"mcode": {

‎plugins/antianqi/ai-node-canvas/tests/smoke.test.mjs‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,30 +2994,30 @@ test('a call that waited for the machine says how long it waited when it fails',
29942994
// call, how long it ran, and how long it stood behind another call.
29952995
const connector = createMockHostConnector(undefined, {
29962996
callDelayMs: 60,
2997-
timeoutCall: { tool: 'generate_image', nth: 2 },
2997+
timeoutCall: { tool: 'synthesize_speech', nth: 2 },
29982998
});
29992999
const h = await createHarness({ hostConnector: connector });
30003000
t.after(() => h.cleanup());
30013001

30023002
const first = await fetch(`${h.base}/api/runs`, {
30033003
method: 'POST',
30043004
headers: { 'content-type': 'application/json' },
3005-
body: JSON.stringify({ nodeId: 'n_ok', mode: 'image', prompt: '第一张' }),
3005+
body: JSON.stringify({ nodeId: 'n_ok', mode: 'audio', prompt: '第一段' }),
30063006
});
30073007
const firstRun = (await first.json()).run;
3008-
await waitForCall(connector.calls, (call) => call.toolRef.endsWith('generate-image'));
3008+
await waitForCall(connector.calls, (call) => call.toolRef.endsWith('synthesize-speech'));
30093009
const second = await fetch(`${h.base}/api/runs`, {
30103010
method: 'POST',
30113011
headers: { 'content-type': 'application/json' },
3012-
body: JSON.stringify({ nodeId: 'n_cut', mode: 'image', prompt: '第二张' }),
3012+
body: JSON.stringify({ nodeId: 'n_cut', mode: 'audio', prompt: '第二段' }),
30133013
});
30143014
const secondRun = (await second.json()).run;
30153015

30163016
const doneFirst = await waitFor(h.base, firstRun.id, (item) => item.status === 'done' || item.status === 'error');
30173017
const failed = await waitFor(h.base, secondRun.id, (item) => item.status === 'done' || item.status === 'error');
30183018
assert.equal(doneFirst.status, 'done');
30193019
assert.equal(failed.status, 'error');
3020-
assert.match(failed.error, /generate_image/, 'the error does not say which call failed');
3020+
assert.match(failed.error, /synthesize_speech/, 'the error does not say which call failed');
30213021
assert.match(failed.error, /前面排队/, 'the error does not say how long the call waited');
30223022
assert.match(failed.error, /耗时/, 'the error does not say how long the call itself ran');
30233023
// A call that the Host stopped leaves nothing behind: the outcome is
@@ -3028,13 +3028,78 @@ test('a call that waited for the machine says how long it waited when it fails',
30283028
const third = await fetch(`${h.base}/api/runs`, {
30293029
method: 'POST',
30303030
headers: { 'content-type': 'application/json' },
3031-
body: JSON.stringify({ nodeId: 'n_after', mode: 'image', prompt: '第三张' }),
3031+
body: JSON.stringify({ nodeId: 'n_after', mode: 'audio', prompt: '第三段' }),
30323032
});
30333033
const thirdRun = (await third.json()).run;
30343034
const doneThird = await waitFor(h.base, thirdRun.id, (item) => item.status === 'done' || item.status === 'error');
30353035
assert.equal(doneThird.status, 'done', 'a failed call blocked the one behind it');
30363036
});
30373037

3038+
test('a stopped image call goes on by the Agent road instead of failing', async (t) => {
3039+
// The Host stops a call that runs over its window, and a picture has no
3040+
// task number to ask about, so the one the provider makes cannot be found
3041+
// afterwards. The page is therefore not told the run failed: the run goes
3042+
// on by the other road, because the Host does not time a turn out.
3043+
const connector = createMockHostConnector(undefined, {
3044+
timeoutCall: { tool: 'generate_image', nth: 1 },
3045+
});
3046+
const h = await createHarness({ hostConnector: connector });
3047+
t.after(() => h.cleanup());
3048+
3049+
const started = await fetch(`${h.base}/api/runs`, {
3050+
method: 'POST',
3051+
headers: { 'content-type': 'application/json' },
3052+
body: JSON.stringify({
3053+
nodeId: 'n_degrade',
3054+
mode: 'image',
3055+
prompt: '把参考图里的字搬到牌匾上',
3056+
params: { ratio: '9:16', resolution: '1K' },
3057+
references: [
3058+
{
3059+
id: 'n_ref',
3060+
title: '门头照',
3061+
kind: 'image',
3062+
text: '',
3063+
media: [{ url: 'https://cdn.example.com/ref.png', kind: 'image' }],
3064+
},
3065+
],
3066+
}),
3067+
});
3068+
assert.equal(started.status, 202);
3069+
const runId = (await started.json()).run.id;
3070+
3071+
// The Agent turn asks for its tool call to be approved before it makes it.
3072+
const waiting = await waitFor(h.base, runId, (run) => Boolean(run.pendingPermission));
3073+
assert.ok(waiting.pendingPermission, 'the run never reached the Agent');
3074+
assert.equal(waiting.degraded, true, 'the run did not say it changed road');
3075+
assert.equal(waiting.status, 'running', 'the page was told the run failed');
3076+
assert.ok(!waiting.error, 'the page was shown a failure');
3077+
assert.ok(
3078+
waiting.log.some((line) => line.includes('改走 Agent 通道')),
3079+
'the run did not say it changed road',
3080+
);
3081+
3082+
// The Agent is handed the very request the direct path would have sent.
3083+
const runCall = h.agent.calls.find((call) => call.kind === 'run');
3084+
assert.ok(runCall, 'the Agent was not asked to run');
3085+
assert.match(runCall.payload.text, /把参考图里的字搬到牌匾上/, 'the request lost the prompt');
3086+
assert.match(runCall.payload.text, /"aspect_ratio":"9:16"/, 'the request lost the ratio');
3087+
assert.match(runCall.payload.text, /"resolution":"1K"/, 'the request lost the resolution');
3088+
assert.match(runCall.payload.text, /cdn\.example\.com\/ref\.png/, 'the request lost the reference');
3089+
3090+
const approved = await getJson(h.base, `/api/runs/${runId}/permission`, {
3091+
method: 'POST',
3092+
headers: { 'content-type': 'application/json' },
3093+
body: JSON.stringify({ decision: 'allowOnce' }),
3094+
});
3095+
assert.equal(approved.status, 200);
3096+
3097+
const done = await waitFor(h.base, runId, (run) => run.status === 'done' || run.status === 'error');
3098+
assert.equal(done.status, 'done', `the degraded run died: ${done.error}`);
3099+
assert.ok(!done.error, 'the degraded run ended with an error');
3100+
assert.equal(done.media.length, 1, 'the degraded run produced no picture');
3101+
});
3102+
30383103
test('a finished video that cannot be fetched says so instead of finishing empty', async (t) => {
30393104
// The task is done, but the answer carries no link and the video has no
30403105
// drive asset. Saying "完成" with nothing behind it is a lie the user would

0 commit comments

Comments
 (0)