@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.
Files changed (163) hide show
  1. package/dist/reglow/common/errors.js +1 -1
  2. package/dist/reglow/common/errors.js.map +1 -1
  3. package/dist/reglow/core/config.js +1 -1
  4. package/dist/reglow/core/config.js.map +1 -1
  5. package/dist/reglow/core/entities.d.ts +113 -1
  6. package/dist/reglow/core/models.d.ts +16 -0
  7. package/dist/reglow/core/models.js +9 -0
  8. package/dist/reglow/core/models.js.map +1 -1
  9. package/dist/reglow/modules/ai/controller.d.ts +15 -0
  10. package/dist/reglow/modules/ai/controller.js +21 -16
  11. package/dist/reglow/modules/ai/controller.js.map +1 -1
  12. package/dist/reglow/modules/ai/service.d.ts +4 -0
  13. package/dist/reglow/modules/ai/service.js +31 -1
  14. package/dist/reglow/modules/ai/service.js.map +1 -1
  15. package/dist/reglow/modules/analytics/collect.d.ts +13 -0
  16. package/dist/reglow/modules/analytics/collect.js +237 -0
  17. package/dist/reglow/modules/analytics/collect.js.map +1 -0
  18. package/dist/reglow/modules/analytics/controller.d.ts +6 -0
  19. package/dist/reglow/modules/analytics/controller.js +338 -0
  20. package/dist/reglow/modules/analytics/controller.js.map +1 -0
  21. package/dist/reglow/modules/analytics/controller_client.d.ts +6 -0
  22. package/dist/reglow/modules/analytics/controller_client.js +34 -0
  23. package/dist/reglow/modules/analytics/controller_client.js.map +1 -0
  24. package/dist/reglow/modules/analytics/gov.d.ts +30 -0
  25. package/dist/reglow/modules/analytics/gov.js +140 -0
  26. package/dist/reglow/modules/analytics/gov.js.map +1 -0
  27. package/dist/reglow/modules/analytics/model.d.ts +190 -0
  28. package/dist/reglow/modules/analytics/model.js +14 -0
  29. package/dist/reglow/modules/analytics/model.js.map +1 -0
  30. package/dist/reglow/modules/analytics/repository.d.ts +66 -0
  31. package/dist/reglow/modules/analytics/repository.js +451 -0
  32. package/dist/reglow/modules/analytics/repository.js.map +1 -0
  33. package/dist/reglow/modules/analytics/rollup.d.ts +17 -0
  34. package/dist/reglow/modules/analytics/rollup.js +299 -0
  35. package/dist/reglow/modules/analytics/rollup.js.map +1 -0
  36. package/dist/reglow/modules/analytics/router.d.ts +16 -0
  37. package/dist/reglow/modules/analytics/router.js +11 -0
  38. package/dist/reglow/modules/analytics/router.js.map +1 -0
  39. package/dist/reglow/modules/analytics/schema.d.ts +275 -0
  40. package/dist/reglow/modules/analytics/schema.js +7 -0
  41. package/dist/reglow/modules/analytics/schema.js.map +1 -0
  42. package/dist/reglow/modules/analytics/service.d.ts +51 -0
  43. package/dist/reglow/modules/analytics/service.js +653 -0
  44. package/dist/reglow/modules/analytics/service.js.map +1 -0
  45. package/dist/reglow/modules/analytics/snapshot.d.ts +10 -0
  46. package/dist/reglow/modules/analytics/snapshot.js +63 -0
  47. package/dist/reglow/modules/analytics/snapshot.js.map +1 -0
  48. package/dist/reglow/modules/article/controller.js +1 -0
  49. package/dist/reglow/modules/article/controller.js.map +1 -1
  50. package/dist/reglow/modules/article/schema.d.ts +2 -0
  51. package/dist/reglow/modules/article/schema.js +2 -0
  52. package/dist/reglow/modules/article/schema.js.map +1 -1
  53. package/dist/reglow/modules/article/service.d.ts +5 -0
  54. package/dist/reglow/modules/article/service.js +26 -1
  55. package/dist/reglow/modules/article/service.js.map +1 -1
  56. package/dist/reglow/modules/config/controller.js +3 -1
  57. package/dist/reglow/modules/config/controller.js.map +1 -1
  58. package/dist/reglow/modules/customer_service/repository.js +4 -2
  59. package/dist/reglow/modules/customer_service/repository.js.map +1 -1
  60. package/dist/reglow/modules/customer_service/sdk_template.html +4 -3
  61. package/dist/reglow/modules/dashboard/controller.js +7 -1
  62. package/dist/reglow/modules/dashboard/controller.js.map +1 -1
  63. package/dist/reglow/modules/dashboard/service.d.ts +1 -0
  64. package/dist/reglow/modules/dashboard/service.js +152 -0
  65. package/dist/reglow/modules/dashboard/service.js.map +1 -1
  66. package/dist/reglow/modules/material/controller.js +16 -0
  67. package/dist/reglow/modules/material/controller.js.map +1 -1
  68. package/dist/reglow/modules/material/repository.js +2 -2
  69. package/dist/reglow/modules/material/repository.js.map +1 -1
  70. package/dist/reglow/modules/material/service.d.ts +2 -0
  71. package/dist/reglow/modules/material/service.js +46 -4
  72. package/dist/reglow/modules/material/service.js.map +1 -1
  73. package/dist/reglow/modules/payment/service.js +7 -1
  74. package/dist/reglow/modules/payment/service.js.map +1 -1
  75. package/dist/reglow/modules/photo/controller.js +81 -55
  76. package/dist/reglow/modules/photo/controller.js.map +1 -1
  77. package/dist/reglow/modules/photo/repository.js +1 -1
  78. package/dist/reglow/modules/photo/repository.js.map +1 -1
  79. package/dist/reglow/modules/photo/service.d.ts +11 -1
  80. package/dist/reglow/modules/photo/service.js +65 -3
  81. package/dist/reglow/modules/photo/service.js.map +1 -1
  82. package/dist/reglow/modules/skill/controller.d.ts +8 -0
  83. package/dist/reglow/modules/skill/controller.js +93 -0
  84. package/dist/reglow/modules/skill/controller.js.map +1 -0
  85. package/dist/reglow/modules/skill/model.d.ts +19 -0
  86. package/dist/reglow/modules/skill/model.js +49 -0
  87. package/dist/reglow/modules/skill/model.js.map +1 -0
  88. package/dist/reglow/modules/skill/repository.d.ts +8 -0
  89. package/dist/reglow/modules/skill/repository.js +101 -0
  90. package/dist/reglow/modules/skill/repository.js.map +1 -0
  91. package/dist/reglow/modules/skill/router.d.ts +10 -0
  92. package/dist/reglow/modules/skill/router.js +8 -0
  93. package/dist/reglow/modules/skill/router.js.map +1 -0
  94. package/dist/reglow/modules/skill/schema.d.ts +39 -0
  95. package/dist/reglow/modules/skill/schema.js +38 -0
  96. package/dist/reglow/modules/skill/schema.js.map +1 -0
  97. package/dist/reglow/modules/skill/service.d.ts +62 -0
  98. package/dist/reglow/modules/skill/service.js +285 -0
  99. package/dist/reglow/modules/skill/service.js.map +1 -0
  100. package/dist/reglow/modules/studio/controller.d.ts +9 -0
  101. package/dist/reglow/modules/studio/controller.js +303 -0
  102. package/dist/reglow/modules/studio/controller.js.map +1 -0
  103. package/dist/reglow/modules/studio/deps.d.ts +47 -0
  104. package/dist/reglow/modules/studio/deps.js +175 -0
  105. package/dist/reglow/modules/studio/deps.js.map +1 -0
  106. package/dist/reglow/modules/studio/engine.d.ts +85 -0
  107. package/dist/reglow/modules/studio/engine.js +881 -0
  108. package/dist/reglow/modules/studio/engine.js.map +1 -0
  109. package/dist/reglow/modules/studio/llm.d.ts +30 -0
  110. package/dist/reglow/modules/studio/llm.js +151 -0
  111. package/dist/reglow/modules/studio/llm.js.map +1 -0
  112. package/dist/reglow/modules/studio/mcp/client.d.ts +22 -0
  113. package/dist/reglow/modules/studio/mcp/client.js +99 -0
  114. package/dist/reglow/modules/studio/mcp/client.js.map +1 -0
  115. package/dist/reglow/modules/studio/mcp/transport.d.ts +59 -0
  116. package/dist/reglow/modules/studio/mcp/transport.js +259 -0
  117. package/dist/reglow/modules/studio/mcp/transport.js.map +1 -0
  118. package/dist/reglow/modules/studio/media.d.ts +52 -0
  119. package/dist/reglow/modules/studio/media.js +484 -0
  120. package/dist/reglow/modules/studio/media.js.map +1 -0
  121. package/dist/reglow/modules/studio/model.d.ts +23 -0
  122. package/dist/reglow/modules/studio/model.js +35 -0
  123. package/dist/reglow/modules/studio/model.js.map +1 -0
  124. package/dist/reglow/modules/studio/repository.d.ts +31 -0
  125. package/dist/reglow/modules/studio/repository.js +300 -0
  126. package/dist/reglow/modules/studio/repository.js.map +1 -0
  127. package/dist/reglow/modules/studio/router.d.ts +10 -0
  128. package/dist/reglow/modules/studio/router.js +8 -0
  129. package/dist/reglow/modules/studio/router.js.map +1 -0
  130. package/dist/reglow/modules/studio/schema.d.ts +194 -0
  131. package/dist/reglow/modules/studio/schema.js +739 -0
  132. package/dist/reglow/modules/studio/schema.js.map +1 -0
  133. package/dist/reglow/modules/studio/service.d.ts +168 -0
  134. package/dist/reglow/modules/studio/service.js +673 -0
  135. package/dist/reglow/modules/studio/service.js.map +1 -0
  136. package/dist/reglow/modules/studio/tools/builtins.d.ts +9 -0
  137. package/dist/reglow/modules/studio/tools/builtins.js +162 -0
  138. package/dist/reglow/modules/studio/tools/builtins.js.map +1 -0
  139. package/dist/reglow/modules/studio/tools/media_tools.d.ts +31 -0
  140. package/dist/reglow/modules/studio/tools/media_tools.js +412 -0
  141. package/dist/reglow/modules/studio/tools/media_tools.js.map +1 -0
  142. package/dist/reglow/modules/studio/tools/registry.d.ts +46 -0
  143. package/dist/reglow/modules/studio/tools/registry.js +160 -0
  144. package/dist/reglow/modules/studio/tools/registry.js.map +1 -0
  145. package/dist/reglow/modules/studio/tools/runner.d.ts +18 -0
  146. package/dist/reglow/modules/studio/tools/runner.js +89 -0
  147. package/dist/reglow/modules/studio/tools/runner.js.map +1 -0
  148. package/dist/reglow/modules/user/controller_client.js +1 -1
  149. package/dist/reglow/modules/user/controller_client.js.map +1 -1
  150. package/dist/reglow/modules/video/controller.js +1 -0
  151. package/dist/reglow/modules/video/controller.js.map +1 -1
  152. package/dist/reglow/modules/video/schema.d.ts +2 -0
  153. package/dist/reglow/modules/video/schema.js +2 -0
  154. package/dist/reglow/modules/video/schema.js.map +1 -1
  155. package/dist/reglow/modules/video/service.d.ts +5 -0
  156. package/dist/reglow/modules/video/service.js +26 -1
  157. package/dist/reglow/modules/video/service.js.map +1 -1
  158. package/dist/reglow/scripts/init-data.js +423 -2
  159. package/dist/reglow/scripts/init-data.js.map +1 -1
  160. package/dist/reglow/scripts/schema.d.ts +9 -0
  161. package/dist/reglow/scripts/schema.js +448 -1
  162. package/dist/reglow/scripts/schema.js.map +1 -1
  163. package/package.json +83 -83
@@ -0,0 +1,673 @@
1
+ /**
2
+ * 智能工坊 Studio - Service 业务逻辑层
3
+ * 对应 backend-python 的 reglow/modules/studio/service.py
4
+ */
5
+ import { logger } from "../../core/logging.js";
6
+ import { StudioRepository } from "./repository.js";
7
+ import * as engine from "./engine.js";
8
+ import * as deps from "./deps.js";
9
+ import { AppException, ErrorCode } from "../../common/errors.js";
10
+ import { DEFAULT_ROLES } from "./schema.js";
11
+ import { MCP_TRANSPORTS, MCP_AUTH_TYPES } from "./model.js";
12
+ function toAssetDto(a) {
13
+ return {
14
+ id: a.id,
15
+ run_id: a.run_id ?? null,
16
+ step_key: a.step_key ?? null,
17
+ tool_key: a.tool_key ?? null,
18
+ kind: a.kind ?? "file",
19
+ name: a.name ?? null,
20
+ material_id: a.material_id ?? null,
21
+ url: a.url ?? null,
22
+ path: a.path ?? null,
23
+ meta: a.meta ?? null,
24
+ source_tool: a.source_tool ?? null,
25
+ created_at: a.created_at ?? null,
26
+ updated_at: a.updated_at ?? null,
27
+ };
28
+ }
29
+ function toJobDto(j) {
30
+ return {
31
+ id: j.id,
32
+ run_id: j.run_id ?? null,
33
+ step_key: j.step_key ?? null,
34
+ tool_key: j.tool_key ?? null,
35
+ provider: j.provider ?? null,
36
+ external_task_id: j.external_task_id ?? null,
37
+ status: j.status ?? "pending",
38
+ progress: j.progress ?? 0,
39
+ result: j.result ?? null,
40
+ error: j.error ?? null,
41
+ created_at: j.created_at ?? null,
42
+ updated_at: j.updated_at ?? null,
43
+ };
44
+ }
45
+ function toMcpServerDto(s) {
46
+ return {
47
+ id: s.id,
48
+ name: s.name,
49
+ transport: s.transport ?? "http",
50
+ url: s.url ?? null,
51
+ command: s.command ?? null,
52
+ auth_type: s.auth_type ?? "none",
53
+ secret_ref: s.secret_ref ?? null,
54
+ enabled: s.enabled ?? 0,
55
+ status: s.status ?? "unknown",
56
+ last_sync_at: s.last_sync_at ?? null,
57
+ created_at: s.created_at ?? null,
58
+ updated_at: s.updated_at ?? null,
59
+ };
60
+ }
61
+ function toMcpToolDto(t) {
62
+ return {
63
+ id: t.id,
64
+ server_id: t.server_id,
65
+ name: t.name,
66
+ description: t.description ?? null,
67
+ input_schema: t.input_schema ?? null,
68
+ risk_level: t.risk_level ?? "low",
69
+ enabled: t.enabled ?? 1,
70
+ synced_at: t.synced_at ?? null,
71
+ };
72
+ }
73
+ function toRoleDto(r) {
74
+ return {
75
+ id: r.id,
76
+ key: r.key,
77
+ name: r.name,
78
+ avatar: r.avatar ?? null,
79
+ category: r.category ?? "内容",
80
+ description: r.description ?? null,
81
+ system_prompt: r.system_prompt,
82
+ skill_ids: Array.isArray(r.skill_ids) ? r.skill_ids : null,
83
+ model: r.model ?? null,
84
+ enabled: r.enabled ?? 1,
85
+ builtin: r.builtin ?? 0,
86
+ sort: r.sort ?? 0,
87
+ created_at: r.created_at ?? null,
88
+ updated_at: r.updated_at ?? null,
89
+ };
90
+ }
91
+ function toWorkflowDto(w) {
92
+ return {
93
+ id: w.id,
94
+ name: w.name,
95
+ description: w.description ?? null,
96
+ goal: w.goal ?? null,
97
+ steps: w.steps ?? [],
98
+ source: w.source ?? "auto",
99
+ status: w.status ?? "active",
100
+ run_count: w.run_count ?? 0,
101
+ created_at: w.created_at ?? null,
102
+ updated_at: w.updated_at ?? null,
103
+ };
104
+ }
105
+ function toRunDto(r) {
106
+ return {
107
+ id: r.id,
108
+ workflow_id: r.workflow_id ?? null,
109
+ workflow_name: r.workflow_name ?? null,
110
+ title: r.title,
111
+ intent: r.intent ?? null,
112
+ status: r.status ?? "pending",
113
+ step_states: r.step_states ?? {},
114
+ result: r.result ?? null,
115
+ error: r.error ?? null,
116
+ source: r.source ?? "compose",
117
+ created_at: r.created_at ?? null,
118
+ updated_at: r.updated_at ?? null,
119
+ };
120
+ }
121
+ function normalizeStep(s) {
122
+ return {
123
+ key: s.key,
124
+ name: s.name,
125
+ role_key: s.role_key,
126
+ task: s.task ?? "",
127
+ depends_on: s.depends_on ?? [],
128
+ approval: s.approval ?? false,
129
+ tool_key: s.tool_key ?? null,
130
+ args: s.args ?? null,
131
+ };
132
+ }
133
+ function buildStates(steps) {
134
+ const states = {};
135
+ for (const s of steps) {
136
+ states[s.key] = {
137
+ name: s.name ?? null,
138
+ role_key: s.role_key ?? null,
139
+ approval: Boolean(s.approval),
140
+ status: "pending",
141
+ };
142
+ }
143
+ return states;
144
+ }
145
+ export class StudioService {
146
+ repo = new StudioRepository();
147
+ // ── 种子 ──
148
+ /**
149
+ * 技能库 + 内置专家角色。
150
+ * - 缺失的内置角色:新增
151
+ * - 已存在但人设版本落后的内置角色:刷新人设与技能绑定(保留 id 与 enabled 开关)
152
+ * - 用户自建角色(builtin=0):永不改动
153
+ */
154
+ async ensureSeed() {
155
+ await deps.ensureSkills();
156
+ const nameToId = await deps.skillIdsByName();
157
+ const existing = new Map((await this.repo.listRoles()).map((r) => [r.key, r]));
158
+ let changed = false;
159
+ for (const d of DEFAULT_ROLES) {
160
+ const payload = {
161
+ key: d.key,
162
+ name: d.name,
163
+ avatar: d.avatar,
164
+ category: d.category,
165
+ description: d.description,
166
+ system_prompt: d.system_prompt,
167
+ skill_ids: d.skill_names
168
+ .map((n) => nameToId[n])
169
+ .filter((id) => id !== undefined),
170
+ sort: d.sort,
171
+ };
172
+ const cur = existing.get(d.key);
173
+ if (!cur) {
174
+ payload.builtin = 1;
175
+ payload.enabled = 1;
176
+ await this.repo.createRole(payload);
177
+ changed = true;
178
+ }
179
+ else if (cur.builtin &&
180
+ (cur.system_prompt !== payload.system_prompt ||
181
+ cur.description !== payload.description ||
182
+ JSON.stringify(cur.skill_ids ?? []) !==
183
+ JSON.stringify(payload.skill_ids))) {
184
+ // 刷新内置人设(保留 id 与 enabled 开关)
185
+ await this.repo.updateRole(cur.id, {
186
+ name: payload.name,
187
+ avatar: payload.avatar,
188
+ category: payload.category,
189
+ description: payload.description,
190
+ system_prompt: payload.system_prompt,
191
+ skill_ids: payload.skill_ids,
192
+ sort: payload.sort,
193
+ });
194
+ changed = true;
195
+ }
196
+ }
197
+ if (changed) {
198
+ logger.debug("[studio] 内置角色种子已刷新");
199
+ }
200
+ }
201
+ // ── 角色 ──
202
+ async listRoles(enabled, category, keyword) {
203
+ await this.ensureSeed();
204
+ const items = await this.repo.listRoles(enabled, category, keyword);
205
+ return items.map(toRoleDto);
206
+ }
207
+ async createRole(data, createdBy) {
208
+ if (await this.repo.findRoleByKey(data.key)) {
209
+ throw new AppException(ErrorCode.PARAM_ERROR, `角色标识已存在: ${data.key}`, 400);
210
+ }
211
+ const payload = {
212
+ key: data.key,
213
+ name: data.name,
214
+ avatar: data.avatar ?? null,
215
+ category: data.category ?? "内容",
216
+ description: data.description ?? null,
217
+ system_prompt: data.system_prompt,
218
+ skill_ids: data.skill_ids ?? null,
219
+ model: data.model ?? null,
220
+ enabled: data.enabled ?? 1,
221
+ sort: data.sort ?? 0,
222
+ builtin: 0,
223
+ created_by: createdBy ?? null,
224
+ };
225
+ return toRoleDto(await this.repo.createRole(payload));
226
+ }
227
+ async updateRole(roleId, data) {
228
+ const role = await this.repo.findRole(roleId);
229
+ if (!role) {
230
+ throw new AppException(ErrorCode.NOT_FOUND, "角色不存在", 404);
231
+ }
232
+ const patch = {};
233
+ for (const [k, v] of Object.entries(data)) {
234
+ if (v !== undefined)
235
+ patch[k] = v;
236
+ }
237
+ const updated = await this.repo.updateRole(roleId, patch);
238
+ return toRoleDto(updated);
239
+ }
240
+ async toggleRole(roleId) {
241
+ const role = await this.repo.findRole(roleId);
242
+ if (!role) {
243
+ throw new AppException(ErrorCode.NOT_FOUND, "角色不存在", 404);
244
+ }
245
+ const updated = await this.repo.updateRole(roleId, {
246
+ enabled: role.enabled ? 0 : 1,
247
+ });
248
+ return toRoleDto(updated);
249
+ }
250
+ async deleteRole(roleId) {
251
+ const role = await this.repo.findRole(roleId);
252
+ if (!role) {
253
+ throw new AppException(ErrorCode.NOT_FOUND, "角色不存在", 404);
254
+ }
255
+ if (role.builtin) {
256
+ throw new AppException(ErrorCode.PARAM_ERROR, "内置角色不可删除,可停用", 400);
257
+ }
258
+ await this.repo.deleteRole(roleId);
259
+ }
260
+ // ── 工作流 ──
261
+ async listWorkflows(keyword) {
262
+ const items = await this.repo.listWorkflows(keyword);
263
+ return items.map(toWorkflowDto);
264
+ }
265
+ async getWorkflow(workflowId) {
266
+ const w = await this.repo.findWorkflow(workflowId);
267
+ if (!w) {
268
+ throw new AppException(ErrorCode.NOT_FOUND, "工作流不存在", 404);
269
+ }
270
+ return toWorkflowDto(w);
271
+ }
272
+ async createWorkflow(data, createdBy) {
273
+ const payload = {
274
+ name: data.name,
275
+ description: data.description ?? null,
276
+ goal: data.goal ?? null,
277
+ steps: data.steps.map(normalizeStep),
278
+ source: data.source ?? "manual",
279
+ status: data.status ?? "active",
280
+ created_by: createdBy ?? null,
281
+ };
282
+ return toWorkflowDto(await this.repo.createWorkflow(payload));
283
+ }
284
+ async updateWorkflow(workflowId, data) {
285
+ const w = await this.repo.findWorkflow(workflowId);
286
+ if (!w) {
287
+ throw new AppException(ErrorCode.NOT_FOUND, "工作流不存在", 404);
288
+ }
289
+ const patch = {};
290
+ for (const [k, v] of Object.entries(data)) {
291
+ if (v !== undefined)
292
+ patch[k] = v;
293
+ }
294
+ if (Array.isArray(patch.steps)) {
295
+ patch.steps = patch.steps.map(normalizeStep);
296
+ }
297
+ const updated = await this.repo.updateWorkflow(workflowId, patch);
298
+ return toWorkflowDto(updated);
299
+ }
300
+ async deleteWorkflow(workflowId) {
301
+ const w = await this.repo.findWorkflow(workflowId);
302
+ if (!w) {
303
+ throw new AppException(ErrorCode.NOT_FOUND, "工作流不存在", 404);
304
+ }
305
+ await this.repo.deleteWorkflow(workflowId);
306
+ }
307
+ // ── 计划 / 运行 ──
308
+ async composePlan(data) {
309
+ await this.ensureSeed();
310
+ const roles = await this.repo.listRoles(1);
311
+ const plan = await engine.composePlan(data.intent, roles, data.attachments ?? null);
312
+ const teamKeys = new Set(plan.steps.map((s) => s.role_key));
313
+ const roleMap = new Map(roles.map((r) => [r.key, r]));
314
+ const team = [];
315
+ for (const k of teamKeys) {
316
+ const r = roleMap.get(k);
317
+ if (r)
318
+ team.push(toRoleDto(r));
319
+ }
320
+ return { ...plan, team };
321
+ }
322
+ async composeRun(data, createdBy) {
323
+ await this.ensureSeed();
324
+ const attachText = engine.attachmentsBrief(data.attachments ?? null);
325
+ const attachSuffix = attachText
326
+ ? `\n\n【用户附件】\n${attachText}`
327
+ : "";
328
+ let wf;
329
+ let steps;
330
+ let name;
331
+ let goal;
332
+ if (data.plan && data.plan.steps && data.plan.steps.length) {
333
+ // 执行用户确认过的计划:不再重新规划
334
+ steps = data.plan.steps.map(normalizeStep);
335
+ name = data.plan.name;
336
+ goal = (data.goal || data.plan.goal || data.intent) + attachSuffix;
337
+ wf = await this.repo.createWorkflow({
338
+ name,
339
+ goal,
340
+ description: "执行一句话任务已生成的计划",
341
+ steps,
342
+ source: "auto",
343
+ status: "active",
344
+ created_by: createdBy ?? null,
345
+ });
346
+ }
347
+ else if (data.workflow_id) {
348
+ const found = await this.repo.findWorkflow(data.workflow_id);
349
+ if (!found) {
350
+ throw new AppException(ErrorCode.NOT_FOUND, "工作流不存在", 404);
351
+ }
352
+ wf = found;
353
+ steps = Array.isArray(wf.steps)
354
+ ? wf.steps
355
+ : [];
356
+ name = wf.name;
357
+ goal = (data.goal || wf.goal || data.intent) + attachSuffix;
358
+ }
359
+ else {
360
+ const roles = await this.repo.listRoles(1);
361
+ const plan = await engine.composePlan(data.intent, roles, data.attachments ?? null);
362
+ steps = plan.steps;
363
+ if (!steps.length) {
364
+ throw new AppException(ErrorCode.PARAM_ERROR, "未能生成有效步骤(角色库为空?)", 400);
365
+ }
366
+ name = plan.name;
367
+ goal = (data.goal || plan.goal) + attachSuffix;
368
+ wf = await this.repo.createWorkflow({
369
+ name,
370
+ goal,
371
+ description: `由一句话任务自动生成(${plan.mode})`,
372
+ steps,
373
+ source: "auto",
374
+ status: "active",
375
+ created_by: createdBy ?? null,
376
+ });
377
+ }
378
+ const states = buildStates(steps);
379
+ const run = await this.repo.createRun({
380
+ workflow_id: wf.id,
381
+ workflow_name: name,
382
+ title: name,
383
+ intent: data.intent,
384
+ goal,
385
+ status: "pending",
386
+ steps,
387
+ step_states: states,
388
+ source: "compose",
389
+ created_by: createdBy ?? null,
390
+ });
391
+ await this.repo.updateWorkflow(wf.id, {
392
+ run_count: (wf.run_count ?? 0) + 1,
393
+ });
394
+ return toRunDto(run);
395
+ }
396
+ async runWorkflow(workflowId, createdBy) {
397
+ const wf = await this.repo.findWorkflow(workflowId);
398
+ if (!wf) {
399
+ throw new AppException(ErrorCode.NOT_FOUND, "工作流不存在", 404);
400
+ }
401
+ const steps = Array.isArray(wf.steps)
402
+ ? wf.steps
403
+ : [];
404
+ const states = buildStates(steps);
405
+ const run = await this.repo.createRun({
406
+ workflow_id: wf.id,
407
+ workflow_name: wf.name,
408
+ title: wf.name,
409
+ intent: wf.goal,
410
+ goal: wf.goal || wf.name,
411
+ status: "pending",
412
+ steps,
413
+ step_states: states,
414
+ source: "manual",
415
+ created_by: createdBy ?? null,
416
+ });
417
+ await this.repo.updateWorkflow(wf.id, {
418
+ run_count: (wf.run_count ?? 0) + 1,
419
+ });
420
+ return toRunDto(run);
421
+ }
422
+ /** 润色一句话任务描述(AI 改写;未配置 AI 时原样返回)。 */
423
+ async polishIntent(intent) {
424
+ return engine.polishIntent(intent);
425
+ }
426
+ async listRuns(status, page = 1, size = 20, keyword) {
427
+ const [items, total] = await this.repo.listRuns(status, page, size, keyword);
428
+ return [items.map(toRunDto), total];
429
+ }
430
+ async getRun(runId) {
431
+ const run = await this.repo.findRun(runId);
432
+ if (!run) {
433
+ throw new AppException(ErrorCode.NOT_FOUND, "运行记录不存在", 404);
434
+ }
435
+ return toRunDto(run);
436
+ }
437
+ async deleteRun(runId) {
438
+ const run = await this.repo.findRun(runId);
439
+ if (!run) {
440
+ throw new AppException(ErrorCode.NOT_FOUND, "运行记录不存在", 404);
441
+ }
442
+ await this.repo.deleteRun(runId);
443
+ }
444
+ async approveRun(runId, stepKey, decision, comment, editedOutput) {
445
+ const run = await this.repo.findRun(runId);
446
+ if (!run) {
447
+ throw new AppException(ErrorCode.NOT_FOUND, "运行记录不存在", 404);
448
+ }
449
+ const updated = await engine.applyApproval(this.repo, run, stepKey, decision, comment, editedOutput);
450
+ return toRunDto(updated);
451
+ }
452
+ async reworkRun(runId, stepKey, comment) {
453
+ const run = await this.repo.findRun(runId);
454
+ if (!run) {
455
+ throw new AppException(ErrorCode.NOT_FOUND, "运行记录不存在", 404);
456
+ }
457
+ const updated = await engine.applyRework(this.repo, run, stepKey, comment);
458
+ return toRunDto(updated);
459
+ }
460
+ async retryRun(runId, stepKey) {
461
+ const run = await this.repo.findRun(runId);
462
+ if (!run) {
463
+ throw new AppException(ErrorCode.NOT_FOUND, "运行记录不存在", 404);
464
+ }
465
+ const updated = await engine.applyRetry(this.repo, run, stepKey);
466
+ return toRunDto(updated);
467
+ }
468
+ async overview() {
469
+ await this.ensureSeed();
470
+ const roles = await this.repo.listRoles();
471
+ const workflows = await this.repo.listWorkflows();
472
+ const [, total] = await this.repo.listRuns(null, 1, 1);
473
+ const [runningItems] = await this.repo.listRuns("running", 1, 100);
474
+ const [reviewItems] = await this.repo.listRuns("review", 1, 100);
475
+ return {
476
+ roles: roles.length,
477
+ workflows: workflows.length,
478
+ runs: total,
479
+ running: runningItems.length + reviewItems.length,
480
+ };
481
+ }
482
+ // ── 产物 / 长任务 ──
483
+ async listAssetsByRun(runId) {
484
+ const run = await this.repo.findRun(runId);
485
+ if (!run) {
486
+ throw new AppException(ErrorCode.NOT_FOUND, "运行记录不存在", 404);
487
+ }
488
+ const [items] = await this.repo.listAssets(runId, null, 1, 1000);
489
+ return items.map(toAssetDto);
490
+ }
491
+ async listAssets(runId, kind, page = 1, size = 20, keyword, publishedOnly = false) {
492
+ const [items, total] = await this.repo.listAssets(runId, kind, page, size, keyword, publishedOnly);
493
+ return [items.map(toAssetDto), total];
494
+ }
495
+ /** 删除产物记录(只删工坊语义记录,系统素材与已归档内容不受影响)。 */
496
+ async deleteAsset(assetId) {
497
+ const asset = await this.repo.findAsset(assetId);
498
+ if (!asset) {
499
+ throw new AppException(ErrorCode.NOT_FOUND, "产物不存在", 404);
500
+ }
501
+ await this.repo.deleteAssets([assetId]);
502
+ }
503
+ /** 批量删除产物记录(只删工坊语义记录,系统素材与已归档内容不受影响)。 */
504
+ async batchDeleteAssets(ids) {
505
+ return this.repo.deleteAssets(ids);
506
+ }
507
+ /** 把产物文件登记为系统素材(复用原文件,不重复上传)。 */
508
+ async saveAssetToMaterial(assetId) {
509
+ const asset = await this.repo.findAsset(assetId);
510
+ if (!asset) {
511
+ throw new AppException(ErrorCode.NOT_FOUND, "产物不存在", 404);
512
+ }
513
+ try {
514
+ return await deps.saveAssetToMaterial(asset);
515
+ }
516
+ catch (e) {
517
+ throw new AppException(ErrorCode.PARAM_ERROR, String(e.message ?? e), 400);
518
+ }
519
+ }
520
+ async getJob(jobId) {
521
+ const job = await this.repo.findJob(jobId);
522
+ if (!job) {
523
+ throw new AppException(ErrorCode.NOT_FOUND, "任务不存在", 404);
524
+ }
525
+ return toJobDto(job);
526
+ }
527
+ // ── 工具目录(工作流步骤选工具) ──
528
+ async listTools() {
529
+ const { getRegistry } = await import("./tools/registry.js");
530
+ const registry = getRegistry();
531
+ try {
532
+ await registry.loadFromSkills();
533
+ await registry.loadMcp();
534
+ }
535
+ catch (e) {
536
+ logger.error("工具目录加载失败,忽略", e.message);
537
+ }
538
+ return registry.list();
539
+ }
540
+ // ── MCP 服务 ──
541
+ static validateMcp(transport, url, command) {
542
+ if (!MCP_TRANSPORTS.includes(transport)) {
543
+ throw new AppException(ErrorCode.PARAM_ERROR, `未知传输方式: ${transport}`, 400);
544
+ }
545
+ if (transport === "http" && !url) {
546
+ throw new AppException(ErrorCode.PARAM_ERROR, "http 传输必须提供 url", 400);
547
+ }
548
+ if (transport === "stdio" && !command) {
549
+ throw new AppException(ErrorCode.PARAM_ERROR, "stdio 传输必须提供 command", 400);
550
+ }
551
+ }
552
+ async getMcpServer(serverId) {
553
+ const server = await this.repo.findMcpServer(serverId);
554
+ if (!server) {
555
+ throw new AppException(ErrorCode.NOT_FOUND, "MCP 服务不存在", 404);
556
+ }
557
+ return server;
558
+ }
559
+ async listMcpServers() {
560
+ const items = await this.repo.listMcpServers();
561
+ return items.map(toMcpServerDto);
562
+ }
563
+ async createMcpServer(data) {
564
+ const transport = data.transport ?? "http";
565
+ const authType = data.auth_type ?? "none";
566
+ StudioService.validateMcp(transport, data.url, data.command);
567
+ if (!MCP_AUTH_TYPES.includes(authType)) {
568
+ throw new AppException(ErrorCode.PARAM_ERROR, `未知鉴权方式: ${authType}`, 400);
569
+ }
570
+ const payload = {
571
+ name: data.name,
572
+ transport,
573
+ url: data.url ?? null,
574
+ command: data.command ?? null,
575
+ auth_type: authType,
576
+ secret_ref: data.secret_ref ?? null,
577
+ enabled: data.enabled ?? 0,
578
+ status: "unknown",
579
+ };
580
+ return toMcpServerDto(await this.repo.createMcpServer(payload));
581
+ }
582
+ async updateMcpServer(serverId, data) {
583
+ const server = await this.getMcpServer(serverId);
584
+ const patch = {};
585
+ for (const [k, v] of Object.entries(data)) {
586
+ if (v !== undefined)
587
+ patch[k] = v;
588
+ }
589
+ StudioService.validateMcp(patch.transport ?? server.transport ?? "http", patch.url ?? server.url, patch.command ?? server.command);
590
+ if (patch.auth_type !== undefined &&
591
+ !MCP_AUTH_TYPES.includes(patch.auth_type)) {
592
+ throw new AppException(ErrorCode.PARAM_ERROR, `未知鉴权方式: ${patch.auth_type}`, 400);
593
+ }
594
+ const updated = await this.repo.updateMcpServer(serverId, patch);
595
+ return toMcpServerDto(updated);
596
+ }
597
+ async deleteMcpServer(serverId) {
598
+ await this.getMcpServer(serverId);
599
+ await this.repo.deleteMcpServer(serverId);
600
+ }
601
+ async listMcpTools(serverId) {
602
+ await this.getMcpServer(serverId);
603
+ const items = await this.repo.listMcpTools(serverId);
604
+ return items.map(toMcpToolDto);
605
+ }
606
+ /** 连通性测试:拉取工具清单,不落库。 */
607
+ async testMcpServer(serverId) {
608
+ const server = await this.getMcpServer(serverId);
609
+ try {
610
+ const { fetchTools } = await import("./mcp/client.js");
611
+ const tools = await fetchTools(server);
612
+ await this.repo.updateMcpServer(serverId, { status: "ok" });
613
+ return { ok: true, tool_count: tools.length, error: null };
614
+ }
615
+ catch (e) {
616
+ await this.repo.updateMcpServer(serverId, { status: "error" });
617
+ return {
618
+ ok: false,
619
+ tool_count: 0,
620
+ error: String(e.message ?? e).slice(0, 300),
621
+ };
622
+ }
623
+ }
624
+ /** 同步工具:拉取工具清单并整体覆盖缓存。 */
625
+ async syncMcpServer(serverId) {
626
+ const server = await this.getMcpServer(serverId);
627
+ let tools;
628
+ try {
629
+ const { fetchTools } = await import("./mcp/client.js");
630
+ tools = await fetchTools(server);
631
+ }
632
+ catch (e) {
633
+ await this.repo.updateMcpServer(serverId, { status: "error" });
634
+ throw new AppException(ErrorCode.PARAM_ERROR, `同步失败: ${String(e.message ?? e).slice(0, 200)}`, 400);
635
+ }
636
+ await this.repo.replaceMcpTools(serverId, tools);
637
+ const updated = await this.repo.updateMcpServer(serverId, {
638
+ status: "ok",
639
+ last_sync_at: new Date(),
640
+ });
641
+ return toMcpServerDto(updated);
642
+ }
643
+ }
644
+ /**
645
+ * 后台执行运行(长任务:多步 LLM 不阻塞请求)。
646
+ * 前端通过轮询 GET /studio/runs/{id} 查看进度。
647
+ */
648
+ export async function executeRunBackground(runId) {
649
+ try {
650
+ const repo = new StudioRepository();
651
+ const run = await repo.findRun(runId);
652
+ if (!run || run.status === "done" || run.status === "failed")
653
+ return;
654
+ await engine.executeRun(repo, run);
655
+ }
656
+ catch (e) {
657
+ logger.error(`后台执行运行 #${runId} 失败`, e.message);
658
+ try {
659
+ const repo = new StudioRepository();
660
+ const run = await repo.findRun(runId);
661
+ if (run && run.status !== "done" && run.status !== "failed") {
662
+ await repo.updateRun(run.id, {
663
+ status: "failed",
664
+ error: "后台执行异常",
665
+ });
666
+ }
667
+ }
668
+ catch (e2) {
669
+ logger.error(`标记运行 #${runId} 失败状态时出错`, e2.message);
670
+ }
671
+ }
672
+ }
673
+ //# sourceMappingURL=service.js.map