@reglow/reglow 0.9.0 → 0.12.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.
- package/dist/reglow/common/errors.js +1 -1
- package/dist/reglow/common/errors.js.map +1 -1
- package/dist/reglow/core/config.js +1 -1
- package/dist/reglow/core/config.js.map +1 -1
- package/dist/reglow/core/entities.d.ts +113 -1
- package/dist/reglow/core/models.d.ts +16 -0
- package/dist/reglow/core/models.js +9 -0
- package/dist/reglow/core/models.js.map +1 -1
- package/dist/reglow/modules/ai/controller.d.ts +15 -0
- package/dist/reglow/modules/ai/controller.js +21 -16
- package/dist/reglow/modules/ai/controller.js.map +1 -1
- package/dist/reglow/modules/ai/service.d.ts +4 -0
- package/dist/reglow/modules/ai/service.js +31 -1
- package/dist/reglow/modules/ai/service.js.map +1 -1
- package/dist/reglow/modules/analytics/collect.d.ts +13 -0
- package/dist/reglow/modules/analytics/collect.js +237 -0
- package/dist/reglow/modules/analytics/collect.js.map +1 -0
- package/dist/reglow/modules/analytics/controller.d.ts +6 -0
- package/dist/reglow/modules/analytics/controller.js +338 -0
- package/dist/reglow/modules/analytics/controller.js.map +1 -0
- package/dist/reglow/modules/analytics/controller_client.d.ts +6 -0
- package/dist/reglow/modules/analytics/controller_client.js +34 -0
- package/dist/reglow/modules/analytics/controller_client.js.map +1 -0
- package/dist/reglow/modules/analytics/gov.d.ts +30 -0
- package/dist/reglow/modules/analytics/gov.js +140 -0
- package/dist/reglow/modules/analytics/gov.js.map +1 -0
- package/dist/reglow/modules/analytics/model.d.ts +190 -0
- package/dist/reglow/modules/analytics/model.js +14 -0
- package/dist/reglow/modules/analytics/model.js.map +1 -0
- package/dist/reglow/modules/analytics/repository.d.ts +66 -0
- package/dist/reglow/modules/analytics/repository.js +451 -0
- package/dist/reglow/modules/analytics/repository.js.map +1 -0
- package/dist/reglow/modules/analytics/rollup.d.ts +17 -0
- package/dist/reglow/modules/analytics/rollup.js +299 -0
- package/dist/reglow/modules/analytics/rollup.js.map +1 -0
- package/dist/reglow/modules/analytics/router.d.ts +16 -0
- package/dist/reglow/modules/analytics/router.js +11 -0
- package/dist/reglow/modules/analytics/router.js.map +1 -0
- package/dist/reglow/modules/analytics/schema.d.ts +275 -0
- package/dist/reglow/modules/analytics/schema.js +7 -0
- package/dist/reglow/modules/analytics/schema.js.map +1 -0
- package/dist/reglow/modules/analytics/service.d.ts +51 -0
- package/dist/reglow/modules/analytics/service.js +653 -0
- package/dist/reglow/modules/analytics/service.js.map +1 -0
- package/dist/reglow/modules/analytics/snapshot.d.ts +10 -0
- package/dist/reglow/modules/analytics/snapshot.js +63 -0
- package/dist/reglow/modules/analytics/snapshot.js.map +1 -0
- package/dist/reglow/modules/article/controller.js +1 -0
- package/dist/reglow/modules/article/controller.js.map +1 -1
- package/dist/reglow/modules/article/schema.d.ts +2 -0
- package/dist/reglow/modules/article/schema.js +2 -0
- package/dist/reglow/modules/article/schema.js.map +1 -1
- package/dist/reglow/modules/article/service.d.ts +5 -0
- package/dist/reglow/modules/article/service.js +26 -1
- package/dist/reglow/modules/article/service.js.map +1 -1
- package/dist/reglow/modules/config/controller.js +3 -1
- package/dist/reglow/modules/config/controller.js.map +1 -1
- package/dist/reglow/modules/customer_service/repository.js +4 -2
- package/dist/reglow/modules/customer_service/repository.js.map +1 -1
- package/dist/reglow/modules/customer_service/sdk_template.html +4 -3
- package/dist/reglow/modules/dashboard/controller.js +7 -1
- package/dist/reglow/modules/dashboard/controller.js.map +1 -1
- package/dist/reglow/modules/dashboard/service.d.ts +1 -0
- package/dist/reglow/modules/dashboard/service.js +152 -0
- package/dist/reglow/modules/dashboard/service.js.map +1 -1
- package/dist/reglow/modules/material/controller.js +16 -0
- package/dist/reglow/modules/material/controller.js.map +1 -1
- package/dist/reglow/modules/material/repository.js +2 -2
- package/dist/reglow/modules/material/repository.js.map +1 -1
- package/dist/reglow/modules/material/service.d.ts +2 -0
- package/dist/reglow/modules/material/service.js +46 -4
- package/dist/reglow/modules/material/service.js.map +1 -1
- package/dist/reglow/modules/payment/service.js +7 -1
- package/dist/reglow/modules/payment/service.js.map +1 -1
- package/dist/reglow/modules/photo/controller.js +81 -55
- package/dist/reglow/modules/photo/controller.js.map +1 -1
- package/dist/reglow/modules/photo/repository.js +1 -1
- package/dist/reglow/modules/photo/repository.js.map +1 -1
- package/dist/reglow/modules/photo/service.d.ts +11 -1
- package/dist/reglow/modules/photo/service.js +65 -3
- package/dist/reglow/modules/photo/service.js.map +1 -1
- package/dist/reglow/modules/skill/controller.d.ts +8 -0
- package/dist/reglow/modules/skill/controller.js +93 -0
- package/dist/reglow/modules/skill/controller.js.map +1 -0
- package/dist/reglow/modules/skill/model.d.ts +19 -0
- package/dist/reglow/modules/skill/model.js +49 -0
- package/dist/reglow/modules/skill/model.js.map +1 -0
- package/dist/reglow/modules/skill/repository.d.ts +8 -0
- package/dist/reglow/modules/skill/repository.js +101 -0
- package/dist/reglow/modules/skill/repository.js.map +1 -0
- package/dist/reglow/modules/skill/router.d.ts +10 -0
- package/dist/reglow/modules/skill/router.js +8 -0
- package/dist/reglow/modules/skill/router.js.map +1 -0
- package/dist/reglow/modules/skill/schema.d.ts +39 -0
- package/dist/reglow/modules/skill/schema.js +38 -0
- package/dist/reglow/modules/skill/schema.js.map +1 -0
- package/dist/reglow/modules/skill/service.d.ts +62 -0
- package/dist/reglow/modules/skill/service.js +285 -0
- package/dist/reglow/modules/skill/service.js.map +1 -0
- package/dist/reglow/modules/studio/controller.d.ts +9 -0
- package/dist/reglow/modules/studio/controller.js +303 -0
- package/dist/reglow/modules/studio/controller.js.map +1 -0
- package/dist/reglow/modules/studio/deps.d.ts +47 -0
- package/dist/reglow/modules/studio/deps.js +175 -0
- package/dist/reglow/modules/studio/deps.js.map +1 -0
- package/dist/reglow/modules/studio/engine.d.ts +85 -0
- package/dist/reglow/modules/studio/engine.js +881 -0
- package/dist/reglow/modules/studio/engine.js.map +1 -0
- package/dist/reglow/modules/studio/llm.d.ts +30 -0
- package/dist/reglow/modules/studio/llm.js +151 -0
- package/dist/reglow/modules/studio/llm.js.map +1 -0
- package/dist/reglow/modules/studio/mcp/client.d.ts +22 -0
- package/dist/reglow/modules/studio/mcp/client.js +99 -0
- package/dist/reglow/modules/studio/mcp/client.js.map +1 -0
- package/dist/reglow/modules/studio/mcp/transport.d.ts +59 -0
- package/dist/reglow/modules/studio/mcp/transport.js +259 -0
- package/dist/reglow/modules/studio/mcp/transport.js.map +1 -0
- package/dist/reglow/modules/studio/media.d.ts +52 -0
- package/dist/reglow/modules/studio/media.js +484 -0
- package/dist/reglow/modules/studio/media.js.map +1 -0
- package/dist/reglow/modules/studio/model.d.ts +23 -0
- package/dist/reglow/modules/studio/model.js +35 -0
- package/dist/reglow/modules/studio/model.js.map +1 -0
- package/dist/reglow/modules/studio/repository.d.ts +31 -0
- package/dist/reglow/modules/studio/repository.js +300 -0
- package/dist/reglow/modules/studio/repository.js.map +1 -0
- package/dist/reglow/modules/studio/router.d.ts +10 -0
- package/dist/reglow/modules/studio/router.js +8 -0
- package/dist/reglow/modules/studio/router.js.map +1 -0
- package/dist/reglow/modules/studio/schema.d.ts +194 -0
- package/dist/reglow/modules/studio/schema.js +739 -0
- package/dist/reglow/modules/studio/schema.js.map +1 -0
- package/dist/reglow/modules/studio/service.d.ts +168 -0
- package/dist/reglow/modules/studio/service.js +673 -0
- package/dist/reglow/modules/studio/service.js.map +1 -0
- package/dist/reglow/modules/studio/tools/builtins.d.ts +9 -0
- package/dist/reglow/modules/studio/tools/builtins.js +162 -0
- package/dist/reglow/modules/studio/tools/builtins.js.map +1 -0
- package/dist/reglow/modules/studio/tools/media_tools.d.ts +31 -0
- package/dist/reglow/modules/studio/tools/media_tools.js +412 -0
- package/dist/reglow/modules/studio/tools/media_tools.js.map +1 -0
- package/dist/reglow/modules/studio/tools/registry.d.ts +46 -0
- package/dist/reglow/modules/studio/tools/registry.js +160 -0
- package/dist/reglow/modules/studio/tools/registry.js.map +1 -0
- package/dist/reglow/modules/studio/tools/runner.d.ts +18 -0
- package/dist/reglow/modules/studio/tools/runner.js +89 -0
- package/dist/reglow/modules/studio/tools/runner.js.map +1 -0
- package/dist/reglow/modules/user/controller_client.js +1 -1
- package/dist/reglow/modules/user/controller_client.js.map +1 -1
- package/dist/reglow/modules/video/controller.js +1 -0
- package/dist/reglow/modules/video/controller.js.map +1 -1
- package/dist/reglow/modules/video/schema.d.ts +2 -0
- package/dist/reglow/modules/video/schema.js +2 -0
- package/dist/reglow/modules/video/schema.js.map +1 -1
- package/dist/reglow/modules/video/service.d.ts +5 -0
- package/dist/reglow/modules/video/service.js +26 -1
- package/dist/reglow/modules/video/service.js.map +1 -1
- package/dist/reglow/scripts/init-data.js +423 -2
- package/dist/reglow/scripts/init-data.js.map +1 -1
- package/dist/reglow/scripts/schema.d.ts +9 -0
- package/dist/reglow/scripts/schema.js +448 -1
- package/dist/reglow/scripts/schema.js.map +1 -1
- package/package.json +83 -83
|
@@ -0,0 +1,881 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智能工坊 Studio - 编排引擎
|
|
3
|
+
* 对应 backend-python 的 reglow/modules/studio/engine.py
|
|
4
|
+
*
|
|
5
|
+
* 两件事:
|
|
6
|
+
* 1. compose:一句话意图 → 调用 LLM 从角色库自动挑人组队 → 输出带依赖的 DAG 步骤;
|
|
7
|
+
* 2. execute:轻量 DAG 执行器 —— 依赖满足即并发执行,遇「人工审批」节点暂停,可恢复、可返工。
|
|
8
|
+
*/
|
|
9
|
+
import { logger } from "../../core/logging.js";
|
|
10
|
+
import * as llm from "./llm.js";
|
|
11
|
+
import * as deps from "./deps.js";
|
|
12
|
+
import * as media from "./media.js";
|
|
13
|
+
const MAX_ATTACHMENT_CHARS = 20000;
|
|
14
|
+
/** 把用户附件整理为提示词上下文(图片给地址,文本给正文,其余给地址)。 */
|
|
15
|
+
export function attachmentsBrief(attachments) {
|
|
16
|
+
if (!attachments || !attachments.length)
|
|
17
|
+
return "";
|
|
18
|
+
const parts = [];
|
|
19
|
+
attachments.forEach((item, index) => {
|
|
20
|
+
const i = index + 1;
|
|
21
|
+
const name = item.name || `附件${i}`;
|
|
22
|
+
const kind = item.kind || "file";
|
|
23
|
+
const url = item.url || "";
|
|
24
|
+
let content = (item.content || "").trim();
|
|
25
|
+
if (kind === "image") {
|
|
26
|
+
parts.push(`- 图片 ${i}:${name}\n 地址:${url || "(上传中/不可用)"}`);
|
|
27
|
+
}
|
|
28
|
+
else if (content) {
|
|
29
|
+
if (content.length > MAX_ATTACHMENT_CHARS) {
|
|
30
|
+
content = `${content.slice(0, MAX_ATTACHMENT_CHARS)}\n…(内容已截断)`;
|
|
31
|
+
}
|
|
32
|
+
parts.push(`- 文件 ${i}:${name}\n\`\`\`\n${content}\n\`\`\``);
|
|
33
|
+
}
|
|
34
|
+
else if (url) {
|
|
35
|
+
parts.push(`- 文件 ${i}:${name}\n 地址:${url}`);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
parts.push(`- 文件 ${i}:${name}(未提供内容)`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return parts.join("\n\n");
|
|
42
|
+
}
|
|
43
|
+
/** 工具目录文本(供 compose 提示词使用)。 */
|
|
44
|
+
function toolsBrief(specs) {
|
|
45
|
+
if (!specs.length)
|
|
46
|
+
return "(当前无可用工具,全部步骤只产出文本)";
|
|
47
|
+
return specs
|
|
48
|
+
.map((s) => `- ${s.key}:${s.name}(产物 ${s.output_kind})— ${s.description || ""}`)
|
|
49
|
+
.join("\n");
|
|
50
|
+
}
|
|
51
|
+
/** 一句话任务允许 AI 自动编排的工具:只开放「产物型」内建工具。 */
|
|
52
|
+
export const COMPOSE_TOOL_KEYS = [
|
|
53
|
+
"image.generate",
|
|
54
|
+
"video.generate",
|
|
55
|
+
"audio.tts",
|
|
56
|
+
"file.write",
|
|
57
|
+
];
|
|
58
|
+
/** 加载并过滤可用工具:未配置服务商的媒体工具不交给 AI 规划。 */
|
|
59
|
+
async function availableToolSpecs() {
|
|
60
|
+
const { getRegistry } = await import("./tools/registry.js");
|
|
61
|
+
const { mediaAvailable } = await import("./tools/media_tools.js");
|
|
62
|
+
let specs;
|
|
63
|
+
try {
|
|
64
|
+
specs = getRegistry()
|
|
65
|
+
.list()
|
|
66
|
+
.filter((s) => COMPOSE_TOOL_KEYS.includes(s.key));
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
logger.error("工具目录加载失败,忽略", e.message);
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
const available = [];
|
|
73
|
+
for (const spec of specs) {
|
|
74
|
+
if (spec.key === "image.generate" && !(await mediaAvailable("image"))) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (spec.key === "video.generate" && !(await mediaAvailable("video"))) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (spec.key === "audio.tts" && !(await mediaAvailable("audio"))) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
available.push(spec);
|
|
84
|
+
}
|
|
85
|
+
return available;
|
|
86
|
+
}
|
|
87
|
+
/** 保证一次运行至少产出一个文件产物:缺 file.write 步骤时补「交付文档」。 */
|
|
88
|
+
export function ensureDeliveryStep(steps, validTools, roles, intent) {
|
|
89
|
+
if (!steps.length || !validTools.has("file.write"))
|
|
90
|
+
return steps;
|
|
91
|
+
if (steps.some((s) => s.tool_key === "file.write"))
|
|
92
|
+
return steps;
|
|
93
|
+
const rolesByKey = new Set(roles.map((r) => r.key));
|
|
94
|
+
const contentKeys = steps
|
|
95
|
+
.filter((s) => !s.approval && s.role_key !== "reviewer")
|
|
96
|
+
.map((s) => s.key);
|
|
97
|
+
if (!contentKeys.length)
|
|
98
|
+
return steps;
|
|
99
|
+
const roleKey = rolesByKey.has("copywriter")
|
|
100
|
+
? "copywriter"
|
|
101
|
+
: steps.find((s) => rolesByKey.has(s.role_key) && s.role_key !== "reviewer")?.role_key || "";
|
|
102
|
+
if (!roleKey)
|
|
103
|
+
return steps;
|
|
104
|
+
let key = "s_deliver";
|
|
105
|
+
while (steps.some((s) => s.key === key))
|
|
106
|
+
key = `${key}_1`;
|
|
107
|
+
const deliver = {
|
|
108
|
+
key,
|
|
109
|
+
name: "交付文档",
|
|
110
|
+
role_key: roleKey,
|
|
111
|
+
task: "汇总全部步骤产出,生成可交付文档。",
|
|
112
|
+
depends_on: contentKeys,
|
|
113
|
+
approval: false,
|
|
114
|
+
tool_key: "file.write",
|
|
115
|
+
args: { name: `${intent.slice(0, 20) || "智能工坊"}_交付文档.md` },
|
|
116
|
+
};
|
|
117
|
+
const reviewer = steps.find((s) => s.approval);
|
|
118
|
+
if (reviewer) {
|
|
119
|
+
reviewer.depends_on = [key];
|
|
120
|
+
steps.splice(steps.indexOf(reviewer), 0, deliver);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
steps.push(deliver);
|
|
124
|
+
}
|
|
125
|
+
return steps;
|
|
126
|
+
}
|
|
127
|
+
function now() {
|
|
128
|
+
return new Date().toISOString().slice(0, 19);
|
|
129
|
+
}
|
|
130
|
+
// ==================== JSON / 步骤工具 ====================
|
|
131
|
+
/** 从 LLM 输出中宽松提取 JSON 对象 */
|
|
132
|
+
export function parseJson(text) {
|
|
133
|
+
if (!text)
|
|
134
|
+
return null;
|
|
135
|
+
let t = text.trim();
|
|
136
|
+
t = t.replace(/^```(?:json)?/, "").trim();
|
|
137
|
+
t = t.replace(/```$/, "").trim();
|
|
138
|
+
const start = t.indexOf("{");
|
|
139
|
+
const end = t.lastIndexOf("}");
|
|
140
|
+
if (start >= 0 && end > start) {
|
|
141
|
+
try {
|
|
142
|
+
return JSON.parse(t.slice(start, end + 1));
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
function rolesBrief(roles) {
|
|
151
|
+
return roles
|
|
152
|
+
.map((r) => `- ${r.key}: ${r.name}(${r.category ?? ""})— ${r.description ?? ""}`)
|
|
153
|
+
.join("\n");
|
|
154
|
+
}
|
|
155
|
+
function toSteps(v) {
|
|
156
|
+
return Array.isArray(v) ? structuredClone(v) : [];
|
|
157
|
+
}
|
|
158
|
+
function toStates(v) {
|
|
159
|
+
return v && typeof v === "object" ? structuredClone(v) : {};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 校验并规整步骤:role_key 合法、key 唯一、依赖存在、工具存在、最后补人工终审。
|
|
163
|
+
*
|
|
164
|
+
* `validTools` 为 undefined 表示不校验工具(保持旧行为);否则未知 tool_key 会被清空,
|
|
165
|
+
* 该步骤退化为普通 LLM 文本步骤,避免规划出必然失败的步骤。
|
|
166
|
+
*/
|
|
167
|
+
export function normalizeSteps(rawSteps, validKeys, validTools) {
|
|
168
|
+
const steps = [];
|
|
169
|
+
const seen = new Set();
|
|
170
|
+
const raw = Array.isArray(rawSteps) ? rawSteps : [];
|
|
171
|
+
raw.forEach((s, i) => {
|
|
172
|
+
if (!s || typeof s !== "object")
|
|
173
|
+
return;
|
|
174
|
+
const rec = s;
|
|
175
|
+
let key = String(rec.key || `s${i + 1}`).trim();
|
|
176
|
+
if (seen.has(key))
|
|
177
|
+
key = `${key}_${i}`;
|
|
178
|
+
const roleKey = String(rec.role_key || "").trim();
|
|
179
|
+
if (!validKeys.has(roleKey))
|
|
180
|
+
return;
|
|
181
|
+
seen.add(key);
|
|
182
|
+
const deps = Array.isArray(rec.depends_on)
|
|
183
|
+
? rec.depends_on.filter((d) => typeof d === "string")
|
|
184
|
+
: [];
|
|
185
|
+
let toolKey = typeof rec.tool_key === "string" && rec.tool_key.trim()
|
|
186
|
+
? rec.tool_key.trim()
|
|
187
|
+
: null;
|
|
188
|
+
if (toolKey && validTools !== undefined && validTools !== null) {
|
|
189
|
+
if (!validTools.has(toolKey))
|
|
190
|
+
toolKey = null;
|
|
191
|
+
}
|
|
192
|
+
const args = rec.args && typeof rec.args === "object" && !Array.isArray(rec.args)
|
|
193
|
+
? rec.args
|
|
194
|
+
: null;
|
|
195
|
+
steps.push({
|
|
196
|
+
key,
|
|
197
|
+
name: String(rec.name || `步骤${i + 1}`).slice(0, 60),
|
|
198
|
+
role_key: roleKey,
|
|
199
|
+
task: String(rec.task || "").slice(0, 500),
|
|
200
|
+
depends_on: deps,
|
|
201
|
+
approval: Boolean(rec.approval) && !toolKey,
|
|
202
|
+
tool_key: toolKey,
|
|
203
|
+
args,
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
// 依赖清理:去掉不存在/自依赖
|
|
207
|
+
const keys = new Set(steps.map((s) => s.key));
|
|
208
|
+
for (const s of steps) {
|
|
209
|
+
s.depends_on = s.depends_on.filter((d) => keys.has(d) && d !== s.key);
|
|
210
|
+
}
|
|
211
|
+
// 保证有终审节点
|
|
212
|
+
if (validKeys.has("reviewer") &&
|
|
213
|
+
!steps.some((s) => s.role_key === "reviewer")) {
|
|
214
|
+
let lastKeys = steps
|
|
215
|
+
.filter((s) => !steps.some((x) => x.depends_on.includes(s.key)))
|
|
216
|
+
.map((s) => s.key);
|
|
217
|
+
if (!lastKeys.length && steps.length)
|
|
218
|
+
lastKeys = [steps[steps.length - 1].key];
|
|
219
|
+
steps.push({
|
|
220
|
+
key: "final_review",
|
|
221
|
+
name: "主编终审",
|
|
222
|
+
role_key: "reviewer",
|
|
223
|
+
task: "汇总并审阅全部产出,给出终审意见与交付清单。",
|
|
224
|
+
depends_on: lastKeys,
|
|
225
|
+
approval: true,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
else if (steps.length && !steps.some((s) => s.approval)) {
|
|
229
|
+
steps[steps.length - 1].approval = true;
|
|
230
|
+
}
|
|
231
|
+
return steps;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 未配置 AI 或解析失败时的规则化保底计划(演示可跑通)。
|
|
235
|
+
*
|
|
236
|
+
* `toolKeys` 为当前可用工具集合:有生图能力时补「生成配图」,
|
|
237
|
+
* 有 file.write 时补「交付文档」,让一句话任务在无 AI 编排时也能产出文件级产物。
|
|
238
|
+
*/
|
|
239
|
+
export function fallbackPlan(intent, roles, toolKeys = new Set()) {
|
|
240
|
+
const have = new Set(roles.map((r) => r.key));
|
|
241
|
+
const pick = (...keys) => {
|
|
242
|
+
for (const k of keys)
|
|
243
|
+
if (have.has(k))
|
|
244
|
+
return k;
|
|
245
|
+
return roles.length ? roles[0].key : "copywriter";
|
|
246
|
+
};
|
|
247
|
+
const steps = [
|
|
248
|
+
{
|
|
249
|
+
key: "s1",
|
|
250
|
+
name: "趋势与机会分析",
|
|
251
|
+
role_key: pick("trend", "analyst"),
|
|
252
|
+
task: `围绕「${intent}」做趋势与机会分析。`,
|
|
253
|
+
depends_on: [],
|
|
254
|
+
approval: false,
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
key: "s2",
|
|
258
|
+
name: "内容策略与选题",
|
|
259
|
+
role_key: pick("strategist", "platform"),
|
|
260
|
+
task: "基于上一步洞察,给出内容主线与选题清单。",
|
|
261
|
+
depends_on: ["s1"],
|
|
262
|
+
approval: false,
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
key: "s3",
|
|
266
|
+
name: "多平台文案",
|
|
267
|
+
role_key: pick("copywriter", "xhs", "douyin"),
|
|
268
|
+
task: "围绕选题产出多平台文案初稿。",
|
|
269
|
+
depends_on: ["s2"],
|
|
270
|
+
approval: false,
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
key: "s4",
|
|
274
|
+
name: "合规预检",
|
|
275
|
+
role_key: pick("compliance"),
|
|
276
|
+
task: "对文案做合规预检并给出修改建议。",
|
|
277
|
+
depends_on: ["s3"],
|
|
278
|
+
approval: false,
|
|
279
|
+
},
|
|
280
|
+
];
|
|
281
|
+
const deliverDeps = ["s4"];
|
|
282
|
+
if (toolKeys.has("image.generate")) {
|
|
283
|
+
steps.push({
|
|
284
|
+
key: "s_img",
|
|
285
|
+
name: "生成配图",
|
|
286
|
+
role_key: pick("designer", "image_prompt", "copywriter"),
|
|
287
|
+
task: `为「${intent}」生成一张营销配图。`,
|
|
288
|
+
depends_on: ["s3"],
|
|
289
|
+
approval: false,
|
|
290
|
+
tool_key: "image.generate",
|
|
291
|
+
args: {
|
|
292
|
+
prompt: `为「${intent}」生成一张营销宣传配图,构图清晰、主体突出`,
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
deliverDeps.push("s_img");
|
|
296
|
+
}
|
|
297
|
+
if (toolKeys.has("file.write")) {
|
|
298
|
+
steps.push({
|
|
299
|
+
key: "s_deliver",
|
|
300
|
+
name: "交付文档",
|
|
301
|
+
role_key: pick("copywriter", "strategist"),
|
|
302
|
+
task: "汇总全部步骤产出,生成可交付文档。",
|
|
303
|
+
depends_on: deliverDeps,
|
|
304
|
+
approval: false,
|
|
305
|
+
tool_key: "file.write",
|
|
306
|
+
args: { name: `${intent.slice(0, 20) || "智能工坊"}_交付文档.md` },
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
if (have.has("reviewer")) {
|
|
310
|
+
steps.push({
|
|
311
|
+
key: "s5",
|
|
312
|
+
name: "主编终审",
|
|
313
|
+
role_key: "reviewer",
|
|
314
|
+
task: "汇总并终审。",
|
|
315
|
+
depends_on: toolKeys.has("file.write") ? ["s_deliver"] : deliverDeps,
|
|
316
|
+
approval: true,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
return {
|
|
320
|
+
name: intent.slice(0, 30) || "自动组队任务",
|
|
321
|
+
goal: intent,
|
|
322
|
+
steps,
|
|
323
|
+
mode: "mock",
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
/** 一句话 → 计划:AI 自动从角色库挑人组队并拆解 DAG(可含工具步骤/附件) */
|
|
327
|
+
export async function composePlan(intent, roles, attachments) {
|
|
328
|
+
if (!roles.length) {
|
|
329
|
+
return { name: intent.slice(0, 30), goal: intent, steps: [], mode: "mock" };
|
|
330
|
+
}
|
|
331
|
+
const attachText = attachmentsBrief(attachments);
|
|
332
|
+
const toolSpecs = await availableToolSpecs();
|
|
333
|
+
const validTools = new Set(toolSpecs.map((s) => s.key));
|
|
334
|
+
const ai = await llm.isConfigured();
|
|
335
|
+
if (ai) {
|
|
336
|
+
const system = "你是营销团队的任务编排器。根据用户意图,从给定角色库中挑选 2-5 个角色组成团队," +
|
|
337
|
+
"并把任务拆解成带依赖关系的步骤(DAG)。\n" +
|
|
338
|
+
"严格只输出 JSON,不要多余解释,结构:\n" +
|
|
339
|
+
'{"name":"工作流名称","goal":"目标","steps":' +
|
|
340
|
+
'[{"key":"s1","name":"步骤名","role_key":"角色key","task":"该角色要做的具体任务",' +
|
|
341
|
+
'"depends_on":[],"approval":false,"tool_key":null,"args":{}}]}\n' +
|
|
342
|
+
"规则:\n" +
|
|
343
|
+
"1) role_key 必须来自给定角色库;步骤 2-6 个;并行步骤可依赖同一前置;\n" +
|
|
344
|
+
"2) 最后一步必须是 role_key=reviewer 的人工终审(approval=true);\n" +
|
|
345
|
+
"3) 需要图片/视频/音频/文件交付时,把该步骤的 tool_key 设为下方可用工具," +
|
|
346
|
+
"并在 args 中给出工具参数(如 image.generate 的 args.prompt、file.write 的 args.name);\n" +
|
|
347
|
+
"4) tool_key 必须来自可用工具清单,不要编造;纯文本步骤 tool_key 留空。\n\n" +
|
|
348
|
+
`可用工具:\n${toolsBrief(toolSpecs)}`;
|
|
349
|
+
let user = `角色库:\n${rolesBrief(roles)}\n\n用户意图:${intent}`;
|
|
350
|
+
if (attachText) {
|
|
351
|
+
user += `\n\n【用户附件(作为任务背景,必要时在步骤任务中引用)】\n${attachText}`;
|
|
352
|
+
}
|
|
353
|
+
try {
|
|
354
|
+
const text = await llm.chat("deepseek", null, system, user);
|
|
355
|
+
const spec = parseJson(text);
|
|
356
|
+
if (spec) {
|
|
357
|
+
const valid = new Set(roles.map((r) => r.key));
|
|
358
|
+
let steps = normalizeSteps(spec.steps, valid, validTools);
|
|
359
|
+
if (steps.length) {
|
|
360
|
+
steps = ensureDeliveryStep(steps, validTools, roles, intent);
|
|
361
|
+
return {
|
|
362
|
+
name: String(spec.name || intent.slice(0, 30)).slice(0, 60),
|
|
363
|
+
goal: String(spec.goal || intent),
|
|
364
|
+
steps,
|
|
365
|
+
mode: "ai",
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
catch (e) {
|
|
371
|
+
logger.error("compose 计划生成失败,回退规则化计划", e.message);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const plan = fallbackPlan(intent, roles, validTools);
|
|
375
|
+
const valid = new Set(roles.map((r) => r.key));
|
|
376
|
+
plan.steps = normalizeSteps(plan.steps, valid, validTools);
|
|
377
|
+
plan.steps = ensureDeliveryStep(plan.steps, validTools, roles, intent);
|
|
378
|
+
return plan;
|
|
379
|
+
}
|
|
380
|
+
/** 润色一句话任务:更清晰、具体、可执行(未配置 AI 时原样返回)。 */
|
|
381
|
+
export async function polishIntent(intent) {
|
|
382
|
+
const text = (intent || "").trim();
|
|
383
|
+
if (!text || !(await llm.isConfigured()))
|
|
384
|
+
return text;
|
|
385
|
+
const system = "你是营销任务的提示词优化助手。把用户的一句话任务改写得更清晰、具体、可执行:" +
|
|
386
|
+
"补全目标与交付物,明确受众 / 平台 / 风格等关键信息,但不要臆造具体数据或作出承诺。" +
|
|
387
|
+
"只输出改写后的任务描述本身,不要解释、不要加引号、不要加任何前缀。";
|
|
388
|
+
return (await llm.chat("deepseek", null, system, text)).trim() || text;
|
|
389
|
+
}
|
|
390
|
+
// ==================== 执行(轻量 DAG) ====================
|
|
391
|
+
export function buildSystemPrompt(role, skillsMap) {
|
|
392
|
+
let base = role ? role.system_prompt : "你是营销团队中的一名专家。";
|
|
393
|
+
const skills = [];
|
|
394
|
+
const ids = role && Array.isArray(role.skill_ids) ? role.skill_ids : [];
|
|
395
|
+
for (const sid of ids) {
|
|
396
|
+
const content = skillsMap[Number(sid)];
|
|
397
|
+
if (content)
|
|
398
|
+
skills.push(content);
|
|
399
|
+
}
|
|
400
|
+
if (skills.length) {
|
|
401
|
+
base +=
|
|
402
|
+
"\n\n【你需要遵守的技能要求】\n" + skills.map((s) => `- ${s}`).join("\n");
|
|
403
|
+
}
|
|
404
|
+
return base;
|
|
405
|
+
}
|
|
406
|
+
export const EXPRESSION_GUIDE = "【写作要求(务必遵守)】\n" +
|
|
407
|
+
"- 先结论后展开:开头一句话说清结论或建议,再讲原因,不要用背景铺垫开头。\n" +
|
|
408
|
+
"- 说人话:假想读者聪明但不懂这个行业;少用术语和抽象名词,必须用时立刻用大白话解释。\n" +
|
|
409
|
+
"- 讲清为什么:每个判断和建议都要交代原因和推理链(因为……所以……),必要时给反例,不要只堆结论。\n" +
|
|
410
|
+
"- 提炼规律:把具体做法总结成一两句可复用的原则或判断公式,让读者下次能自己用。\n" +
|
|
411
|
+
"- 生动具体:抽象观点配一个场景、例子或类比(“就像……”),让人读的时候脑子里有画面。\n" +
|
|
412
|
+
"- 好读:用 Markdown 小标题分段,段落控制在 2-4 句,关键句加粗;多用对比、转折和具体数字,不要写成制度文件或教科书。";
|
|
413
|
+
export function buildUserPrompt(goal, step, _steps, states) {
|
|
414
|
+
const upstream = [];
|
|
415
|
+
for (const dep of step.depends_on ?? []) {
|
|
416
|
+
const st = states[dep] ?? {};
|
|
417
|
+
if (st.output) {
|
|
418
|
+
upstream.push(`【${st.name ?? dep} 的产出】\n${st.output}`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const parts = [
|
|
422
|
+
`【团队总目标】${goal || ""}`,
|
|
423
|
+
`【你负责的步骤】${step.name}`,
|
|
424
|
+
`【具体任务】${step.task || ""}`,
|
|
425
|
+
];
|
|
426
|
+
if (upstream.length) {
|
|
427
|
+
parts.push("【上游产出(请在此基础上继续)】\n" + upstream.join("\n\n"));
|
|
428
|
+
}
|
|
429
|
+
parts.push(EXPRESSION_GUIDE);
|
|
430
|
+
parts.push("请直接输出你的专业产出:按上面的写作要求,先给结论,再讲清为什么," +
|
|
431
|
+
"最后提炼可复用的规律;不要复述本说明。");
|
|
432
|
+
return parts.join("\n\n");
|
|
433
|
+
}
|
|
434
|
+
export function mockStepOutput(role, step, goal) {
|
|
435
|
+
const name = role ? role.name : step.role_key;
|
|
436
|
+
return (`【${name} · ${step.name}】\n` +
|
|
437
|
+
`(演示模式:未配置 AI 服务商)围绕目标「${goal}」,本步骤将由 ${name} 完成。\n` +
|
|
438
|
+
`任务:${step.task || "—"}\n` +
|
|
439
|
+
"配置 AI Key 后此处会产出真实内容。");
|
|
440
|
+
}
|
|
441
|
+
function jsonDumps(data) {
|
|
442
|
+
try {
|
|
443
|
+
return JSON.stringify(data);
|
|
444
|
+
}
|
|
445
|
+
catch {
|
|
446
|
+
return String(data);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
/** 把文本产物登记为 studio_asset(可溯源到运行与工具)。 */
|
|
450
|
+
async function registerTextAsset(run, step, st, text, extra) {
|
|
451
|
+
if (!text)
|
|
452
|
+
return;
|
|
453
|
+
const meta = {
|
|
454
|
+
text,
|
|
455
|
+
size: Buffer.byteLength(text, "utf-8"),
|
|
456
|
+
run_id: run.id,
|
|
457
|
+
step_key: step.key,
|
|
458
|
+
};
|
|
459
|
+
if (extra)
|
|
460
|
+
Object.assign(meta, extra);
|
|
461
|
+
try {
|
|
462
|
+
const asset = await deps.createAsset({
|
|
463
|
+
run_id: run.id,
|
|
464
|
+
step_key: step.key,
|
|
465
|
+
tool_key: "llm.chat",
|
|
466
|
+
kind: "text",
|
|
467
|
+
name: step.name,
|
|
468
|
+
meta,
|
|
469
|
+
created_by: run.created_by ?? null,
|
|
470
|
+
});
|
|
471
|
+
st.assets = st.assets ?? [];
|
|
472
|
+
st.assets.push(asset.id);
|
|
473
|
+
}
|
|
474
|
+
catch (e) {
|
|
475
|
+
logger.error(`文本产物登记失败 run=${run.id} step=${step.key}`, e.message);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/** 把工具结果转成可写入 JSON 列的结构(剔除 bytes 等不可序列化内容,循环引用安全)。 */
|
|
479
|
+
function jsonSafe(value, seen) {
|
|
480
|
+
if (value === null || typeof value !== "object") {
|
|
481
|
+
if (typeof value === "string" ||
|
|
482
|
+
typeof value === "number" ||
|
|
483
|
+
typeof value === "boolean") {
|
|
484
|
+
return value;
|
|
485
|
+
}
|
|
486
|
+
return String(value);
|
|
487
|
+
}
|
|
488
|
+
if (seen === undefined)
|
|
489
|
+
seen = new WeakSet();
|
|
490
|
+
if (seen.has(value))
|
|
491
|
+
return "[Circular]";
|
|
492
|
+
seen.add(value);
|
|
493
|
+
let out;
|
|
494
|
+
if (Array.isArray(value)) {
|
|
495
|
+
out = value.map((v) => jsonSafe(v, seen));
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
const record = {};
|
|
499
|
+
for (const [k, v] of Object.entries(value)) {
|
|
500
|
+
if (k === "content")
|
|
501
|
+
continue;
|
|
502
|
+
record[k] = jsonSafe(v, seen);
|
|
503
|
+
}
|
|
504
|
+
out = record;
|
|
505
|
+
}
|
|
506
|
+
seen.delete(value);
|
|
507
|
+
return out;
|
|
508
|
+
}
|
|
509
|
+
function guessMime(url) {
|
|
510
|
+
const ext = pathExt(url);
|
|
511
|
+
return ({
|
|
512
|
+
".png": "image/png",
|
|
513
|
+
".jpg": "image/jpeg",
|
|
514
|
+
".jpeg": "image/jpeg",
|
|
515
|
+
".gif": "image/gif",
|
|
516
|
+
".webp": "image/webp",
|
|
517
|
+
".mp4": "video/mp4",
|
|
518
|
+
".mov": "video/quicktime",
|
|
519
|
+
".mp3": "audio/mpeg",
|
|
520
|
+
".wav": "audio/wav",
|
|
521
|
+
".flac": "audio/flac",
|
|
522
|
+
".pdf": "application/pdf",
|
|
523
|
+
}[ext] ?? null);
|
|
524
|
+
}
|
|
525
|
+
function pathExt(url) {
|
|
526
|
+
try {
|
|
527
|
+
const pathname = new URL(url, "http://localhost").pathname;
|
|
528
|
+
const idx = pathname.lastIndexOf(".");
|
|
529
|
+
return idx >= 0 ? pathname.slice(idx).toLowerCase() : "";
|
|
530
|
+
}
|
|
531
|
+
catch {
|
|
532
|
+
return "";
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function extOfName(name) {
|
|
536
|
+
if (!name)
|
|
537
|
+
return "";
|
|
538
|
+
const idx = name.lastIndexOf(".");
|
|
539
|
+
return idx >= 0 ? name.slice(idx).toLowerCase() : "";
|
|
540
|
+
}
|
|
541
|
+
const EXT_BY_MIME = {
|
|
542
|
+
"image/png": ".png",
|
|
543
|
+
"image/jpeg": ".jpg",
|
|
544
|
+
"image/webp": ".webp",
|
|
545
|
+
"video/mp4": ".mp4",
|
|
546
|
+
"audio/mpeg": ".mp3",
|
|
547
|
+
"audio/wav": ".wav",
|
|
548
|
+
"audio/flac": ".flac",
|
|
549
|
+
"text/markdown": ".md",
|
|
550
|
+
"text/plain": ".txt",
|
|
551
|
+
"application/pdf": ".pdf",
|
|
552
|
+
};
|
|
553
|
+
/** 给无扩展名的产物补扩展名(本地存储靠扩展名决定 Content-Type)。 */
|
|
554
|
+
function withExt(name, url, mime) {
|
|
555
|
+
if (!name)
|
|
556
|
+
return name ?? null;
|
|
557
|
+
if (extOfName(name))
|
|
558
|
+
return name;
|
|
559
|
+
const baseMime = (mime || "").split(";")[0].trim();
|
|
560
|
+
const ext = EXT_BY_MIME[baseMime] || (url ? pathExt(url) : "");
|
|
561
|
+
return ext ? `${name}${ext}` : name;
|
|
562
|
+
}
|
|
563
|
+
/** 下载媒体产物到本地,避免外部 URL 过期导致产物 404。 */
|
|
564
|
+
async function downloadMedia(url) {
|
|
565
|
+
if (!/^https?:\/\//.test(url))
|
|
566
|
+
return null;
|
|
567
|
+
try {
|
|
568
|
+
return await media.download(url, 120);
|
|
569
|
+
}
|
|
570
|
+
catch (e) {
|
|
571
|
+
logger.warning(`媒体产物下载失败,保留外链快照: ${url}`, e.message);
|
|
572
|
+
return null;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/** 文本步骤:走 LLM(未配置服务商时降级为演示输出),产出登记为文本资产。 */
|
|
576
|
+
async function runLlmStep(run, step, st, states, role, skillsMap) {
|
|
577
|
+
const goal = run.goal || run.intent || "";
|
|
578
|
+
let modelName = "mock";
|
|
579
|
+
if (await llm.isConfigured()) {
|
|
580
|
+
const system = buildSystemPrompt(role, skillsMap);
|
|
581
|
+
const user = buildUserPrompt(goal, step, toSteps(run.steps), states);
|
|
582
|
+
const model = role && role.model ? role.model : null;
|
|
583
|
+
modelName = model || "default";
|
|
584
|
+
st.output = await llm.chat("deepseek", model, system, user);
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
st.output = mockStepOutput(role, step, goal);
|
|
588
|
+
}
|
|
589
|
+
st.output_kind = "text";
|
|
590
|
+
await registerTextAsset(run, step, st, st.output, {
|
|
591
|
+
role_key: step.role_key,
|
|
592
|
+
model: modelName,
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
/** 工具步骤:按 tool_key 分派执行,产出落 studio_asset,长任务落 studio_job。 */
|
|
596
|
+
async function runToolStep(run, step, st, toolKey, states) {
|
|
597
|
+
const { runTool } = await import("./tools/runner.js");
|
|
598
|
+
const args = {
|
|
599
|
+
...(step.args ?? {
|
|
600
|
+
task: step.task || "",
|
|
601
|
+
goal: run.goal || run.intent || "",
|
|
602
|
+
}),
|
|
603
|
+
};
|
|
604
|
+
// file.write / audio.tts 等工具需要上游产出:把直接前置的文本汇总注入
|
|
605
|
+
const upstream = (step.depends_on ?? [])
|
|
606
|
+
.map((d) => states[d]?.output ?? "")
|
|
607
|
+
.filter(Boolean);
|
|
608
|
+
if (upstream.length && !args.upstream) {
|
|
609
|
+
args.upstream = upstream.join("\n\n");
|
|
610
|
+
}
|
|
611
|
+
const res = await runTool(toolKey, args, {
|
|
612
|
+
runId: run.id,
|
|
613
|
+
stepKey: step.key,
|
|
614
|
+
});
|
|
615
|
+
if (!res.ok) {
|
|
616
|
+
throw new Error(res.error || "工具执行失败");
|
|
617
|
+
}
|
|
618
|
+
st.output_kind = res.output_kind;
|
|
619
|
+
st.output =
|
|
620
|
+
res.text !== null && res.text !== undefined
|
|
621
|
+
? res.text
|
|
622
|
+
: jsonDumps(res.data);
|
|
623
|
+
const data = res.data && typeof res.data === "object" ? res.data : {};
|
|
624
|
+
let content = null;
|
|
625
|
+
const raw = data.content;
|
|
626
|
+
if (raw instanceof Uint8Array)
|
|
627
|
+
content = Buffer.from(raw);
|
|
628
|
+
let mime = typeof data.mime === "string"
|
|
629
|
+
? data.mime
|
|
630
|
+
: null;
|
|
631
|
+
let name = (typeof data.filename === "string"
|
|
632
|
+
? data.filename
|
|
633
|
+
: null) || step.name;
|
|
634
|
+
// 外部媒体 URL 尽量落到本地存储,产物才不会随厂商链接过期而失效
|
|
635
|
+
if (res.url &&
|
|
636
|
+
content === null &&
|
|
637
|
+
["image", "audio", "video", "file"].includes(res.output_kind)) {
|
|
638
|
+
content = await downloadMedia(res.url);
|
|
639
|
+
if (content !== null && !mime)
|
|
640
|
+
mime = guessMime(res.url);
|
|
641
|
+
}
|
|
642
|
+
// 有二进制内容时补扩展名:本地存储靠扩展名决定 Content-Type(音频/文件下载需要)
|
|
643
|
+
if (content !== null)
|
|
644
|
+
name = withExt(name, res.url, mime) ?? name;
|
|
645
|
+
const meta = jsonSafe(data);
|
|
646
|
+
if (res.text !== null && res.text !== undefined) {
|
|
647
|
+
if (meta.text === undefined)
|
|
648
|
+
meta.text = res.text;
|
|
649
|
+
}
|
|
650
|
+
if (content !== null && meta.size === undefined)
|
|
651
|
+
meta.size = content.length;
|
|
652
|
+
if (res.url && meta.url === undefined)
|
|
653
|
+
meta.url = res.url;
|
|
654
|
+
if (meta.run_id === undefined)
|
|
655
|
+
meta.run_id = run.id;
|
|
656
|
+
if (meta.step_key === undefined)
|
|
657
|
+
meta.step_key = step.key;
|
|
658
|
+
if (res.url ||
|
|
659
|
+
content !== null ||
|
|
660
|
+
res.text !== null ||
|
|
661
|
+
Object.keys(data).length) {
|
|
662
|
+
const asset = await deps.createAsset({
|
|
663
|
+
run_id: run.id,
|
|
664
|
+
step_key: step.key,
|
|
665
|
+
tool_key: toolKey,
|
|
666
|
+
kind: res.output_kind,
|
|
667
|
+
name,
|
|
668
|
+
url: res.url,
|
|
669
|
+
content,
|
|
670
|
+
mime,
|
|
671
|
+
meta,
|
|
672
|
+
created_by: run.created_by ?? null,
|
|
673
|
+
});
|
|
674
|
+
st.assets = st.assets ?? [];
|
|
675
|
+
st.assets.push(asset.id);
|
|
676
|
+
}
|
|
677
|
+
if (res.job) {
|
|
678
|
+
const job = await deps.createJob({
|
|
679
|
+
run_id: run.id,
|
|
680
|
+
step_key: step.key,
|
|
681
|
+
tool_key: toolKey,
|
|
682
|
+
provider: res.job.provider ?? null,
|
|
683
|
+
external_task_id: res.job.external_task_id ?? null,
|
|
684
|
+
status: res.job.status || "pending",
|
|
685
|
+
progress: Number(res.job.progress) || 0,
|
|
686
|
+
});
|
|
687
|
+
st.job_id = job.id;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
async function runStep(run, step, states, rolesByKey, skillsMap) {
|
|
691
|
+
const st = states[step.key];
|
|
692
|
+
st.status = "running";
|
|
693
|
+
st.started_at = now();
|
|
694
|
+
const role = rolesByKey.get(step.role_key);
|
|
695
|
+
try {
|
|
696
|
+
if (step.tool_key) {
|
|
697
|
+
await runToolStep(run, step, st, step.tool_key, states);
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
await runLlmStep(run, step, st, states, role, skillsMap);
|
|
701
|
+
}
|
|
702
|
+
st.status = "done";
|
|
703
|
+
}
|
|
704
|
+
catch (exc) {
|
|
705
|
+
logger.error(`步骤 ${step.key} 执行失败`, exc.message);
|
|
706
|
+
st.status = "failed";
|
|
707
|
+
st.error = String(exc.message ?? exc).slice(0, 300);
|
|
708
|
+
}
|
|
709
|
+
st.finished_at = now();
|
|
710
|
+
}
|
|
711
|
+
/** 执行/恢复运行:依赖满足即并发执行,遇审批暂停。返回最新 run。 */
|
|
712
|
+
export async function executeRun(repo, run) {
|
|
713
|
+
if (run.status === "done" || run.status === "failed")
|
|
714
|
+
return run;
|
|
715
|
+
const steps = toSteps(run.steps);
|
|
716
|
+
// 深拷贝:JSON 列需整体回写
|
|
717
|
+
const states = toStates(run.step_states);
|
|
718
|
+
for (const s of steps) {
|
|
719
|
+
if (!states[s.key]) {
|
|
720
|
+
states[s.key] = {
|
|
721
|
+
name: s.name,
|
|
722
|
+
role_key: s.role_key,
|
|
723
|
+
approval: Boolean(s.approval),
|
|
724
|
+
status: "pending",
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
const roles = await repo.listRoles();
|
|
729
|
+
const rolesByKey = new Map(roles.map((r) => [r.key, r]));
|
|
730
|
+
const skillsMap = await deps.loadSkills();
|
|
731
|
+
// 加载工具注册表(技能工具目录 + MCP 工具),失败不影响运行
|
|
732
|
+
try {
|
|
733
|
+
const { getRegistry } = await import("./tools/registry.js");
|
|
734
|
+
const registry = getRegistry();
|
|
735
|
+
await registry.loadFromSkills();
|
|
736
|
+
await registry.loadMcp();
|
|
737
|
+
}
|
|
738
|
+
catch (e) {
|
|
739
|
+
logger.error("工具注册表加载失败,忽略", e.message);
|
|
740
|
+
}
|
|
741
|
+
let current = (await repo.updateRun(run.id, {
|
|
742
|
+
status: "running",
|
|
743
|
+
step_states: states,
|
|
744
|
+
error: null,
|
|
745
|
+
})) ?? run;
|
|
746
|
+
while (true) {
|
|
747
|
+
const ready = steps.filter((s) => states[s.key].status === "pending" &&
|
|
748
|
+
(s.depends_on ?? []).every((d) => (states[d] ?? {}).status === "done"));
|
|
749
|
+
if (!ready.length)
|
|
750
|
+
break;
|
|
751
|
+
const approval = ready.filter((s) => s.approval);
|
|
752
|
+
if (approval.length) {
|
|
753
|
+
for (const s of approval)
|
|
754
|
+
states[s.key].status = "review";
|
|
755
|
+
return await repo.updateRun(run.id, {
|
|
756
|
+
status: "review",
|
|
757
|
+
step_states: states,
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
// 串行执行就绪步骤:同一次运行共用连接与产物写入,
|
|
761
|
+
// 并发提交产物 / 用量易产生竞争(并行 DAG 仍按依赖顺序择机执行)
|
|
762
|
+
for (const s of ready) {
|
|
763
|
+
await runStep(current, s, states, rolesByKey, skillsMap);
|
|
764
|
+
}
|
|
765
|
+
current =
|
|
766
|
+
(await repo.updateRun(run.id, { step_states: states })) ?? current;
|
|
767
|
+
if (ready.some((s) => states[s.key].status === "failed")) {
|
|
768
|
+
return await repo.updateRun(run.id, {
|
|
769
|
+
status: "failed",
|
|
770
|
+
step_states: states,
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
// 收尾:仍有 pending 说明依赖无法满足
|
|
775
|
+
const pending = steps.filter((s) => states[s.key].status === "pending");
|
|
776
|
+
if (pending.length) {
|
|
777
|
+
return await repo.updateRun(run.id, {
|
|
778
|
+
status: "failed",
|
|
779
|
+
step_states: states,
|
|
780
|
+
error: "存在无法满足的依赖,执行中止",
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
const result = {
|
|
784
|
+
outputs: steps.map((s) => ({
|
|
785
|
+
key: s.key,
|
|
786
|
+
name: s.name,
|
|
787
|
+
role_key: s.role_key,
|
|
788
|
+
output: states[s.key].output ?? null,
|
|
789
|
+
})),
|
|
790
|
+
summary: `共 ${steps.length} 个步骤全部完成`,
|
|
791
|
+
};
|
|
792
|
+
return await repo.updateRun(run.id, {
|
|
793
|
+
status: "done",
|
|
794
|
+
step_states: states,
|
|
795
|
+
result,
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
/** 应用人工审批结果(仅落库,不执行):通过则标记完成,驳回则终止。 */
|
|
799
|
+
export async function applyApproval(repo, run, stepKey, decision, comment, editedOutput) {
|
|
800
|
+
const states = toStates(run.step_states);
|
|
801
|
+
const st = states[stepKey];
|
|
802
|
+
if (!st)
|
|
803
|
+
return run;
|
|
804
|
+
if (decision === "reject") {
|
|
805
|
+
st.status = "failed";
|
|
806
|
+
st.error = comment || "人工驳回";
|
|
807
|
+
return await repo.updateRun(run.id, {
|
|
808
|
+
status: "failed",
|
|
809
|
+
step_states: states,
|
|
810
|
+
error: comment || "人工驳回",
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
if (editedOutput)
|
|
814
|
+
st.output = editedOutput;
|
|
815
|
+
st.status = "done";
|
|
816
|
+
st.approved_by = "human";
|
|
817
|
+
st.finished_at = now();
|
|
818
|
+
return await repo.updateRun(run.id, { step_states: states });
|
|
819
|
+
}
|
|
820
|
+
/** 收集需重置的步骤:目标步及其全部下游(依赖闭包)。 */
|
|
821
|
+
export function collectResetTargets(steps, stepKey) {
|
|
822
|
+
const affected = new Set([stepKey]);
|
|
823
|
+
let changed = true;
|
|
824
|
+
while (changed) {
|
|
825
|
+
changed = false;
|
|
826
|
+
for (const s of steps) {
|
|
827
|
+
if ((s.depends_on ?? []).some((d) => affected.has(d)) &&
|
|
828
|
+
!affected.has(s.key)) {
|
|
829
|
+
affected.add(s.key);
|
|
830
|
+
changed = true;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return affected;
|
|
835
|
+
}
|
|
836
|
+
/** 把指定步骤重置为待执行,并清空产出/错误/审批记录。 */
|
|
837
|
+
export function resetSteps(states, steps, affected) {
|
|
838
|
+
for (const s of steps) {
|
|
839
|
+
if (!affected.has(s.key))
|
|
840
|
+
continue;
|
|
841
|
+
const st = states[s.key];
|
|
842
|
+
if (!st)
|
|
843
|
+
continue;
|
|
844
|
+
st.status = "pending";
|
|
845
|
+
delete st.output;
|
|
846
|
+
delete st.error;
|
|
847
|
+
delete st.approved_by;
|
|
848
|
+
delete st.assets;
|
|
849
|
+
delete st.job_id;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
/** 应用返工:把该步及所有下游重置为待执行,返工意见并入任务,并重置运行状态触发重跑。 */
|
|
853
|
+
export async function applyRework(repo, run, stepKey, comment) {
|
|
854
|
+
const steps = toSteps(run.steps);
|
|
855
|
+
const states = toStates(run.step_states);
|
|
856
|
+
resetSteps(states, steps, collectResetTargets(steps, stepKey));
|
|
857
|
+
for (const s of steps) {
|
|
858
|
+
if (s.key === stepKey) {
|
|
859
|
+
s.task = `${s.task || ""}\n【返工意见】${comment}`;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return await repo.updateRun(run.id, {
|
|
863
|
+
status: "running",
|
|
864
|
+
steps,
|
|
865
|
+
step_states: states,
|
|
866
|
+
error: null,
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
/** 重试某步:把该步及下游重置为待执行(不改任务),并重置运行状态触发重跑。 */
|
|
870
|
+
export async function applyRetry(repo, run, stepKey) {
|
|
871
|
+
const steps = toSteps(run.steps);
|
|
872
|
+
const states = toStates(run.step_states);
|
|
873
|
+
resetSteps(states, steps, collectResetTargets(steps, stepKey));
|
|
874
|
+
return await repo.updateRun(run.id, {
|
|
875
|
+
status: "running",
|
|
876
|
+
steps,
|
|
877
|
+
step_states: states,
|
|
878
|
+
error: null,
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
//# sourceMappingURL=engine.js.map
|