agentworkshop 0.12.0 → 0.13.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 (31) hide show
  1. package/.output/nitro.json +1 -1
  2. package/.output/public/_nuxt/builds/latest.json +1 -1
  3. package/.output/public/_nuxt/builds/meta/2f96705b-cf57-4bde-87a4-1b818b268f0f.json +1 -0
  4. package/.output/server/chunks/_/nitro.mjs +1102 -1088
  5. package/.output/server/package.json +1 -1
  6. package/README-zh.md +18 -6
  7. package/README.md +20 -6
  8. package/config.yml +1 -1
  9. package/docs/cli.en.md +1 -1
  10. package/docs/cli.md +1 -1
  11. package/docs/plugin-idd-rag-contract.md +87 -0
  12. package/docs/plugins.en.md +2 -2
  13. package/docs/plugins.md +2 -2
  14. package/docs/sdk.en.md +1 -1
  15. package/docs/sdk.md +1 -1
  16. package/package.json +2 -1
  17. package/scripts/onboarding/sim-twin.mjs +27 -15
  18. package/server/plugins-builtin/diag-bridge/host/deposit.mjs +232 -0
  19. package/server/plugins-builtin/diag-bridge/host/diagnosis.mjs +121 -38
  20. package/server/plugins-builtin/diag-bridge/host/external-v1.mjs +213 -0
  21. package/server/plugins-builtin/diag-bridge/host/helpers.mjs +4 -2
  22. package/server/plugins-builtin/diag-bridge/host/intents.mjs +65 -62
  23. package/server/plugins-builtin/diag-bridge/host/plugin.mjs +351 -82
  24. package/server/plugins-builtin/diag-bridge/host/receipt.mjs +83 -0
  25. package/server/plugins-builtin/diag-bridge/host/sweep.mjs +70 -91
  26. package/server/plugins-builtin/diag-bridge/i18n.json +4 -0
  27. package/server/plugins-builtin/rag-bridge/i18n.json +18 -0
  28. package/server/plugins-builtin/rag-bridge/index.mjs +556 -68
  29. package/server/services/workshop/agents/industrial/optimization-tools.ts +12 -1
  30. package/server/services/workshop/tools/policy.ts +16 -1
  31. package/.output/public/_nuxt/builds/meta/577f464e-672a-4c4e-a3c8-dc8c1b38930d.json +0 -1
@@ -0,0 +1,83 @@
1
+ /**
2
+ * 回执与结果归一(WS-2 I6):sweep / diag_status / wait 循环共用同一套
3
+ * 完成态字段与回执文本,避免三处各自拼装导致形状漂移。
4
+ */
5
+ import { intentLabel } from './intents.mjs'
6
+
7
+ /** 终态集合(v1 六态去掉 awaiting_input —— awaiting_input 单列,不冒充终态) */
8
+ export const TERMINAL_STATUSES = new Set(['completed', 'failed', 'stopped', 'interrupted', 'expired'])
9
+
10
+ /** 该状态是否终态(awaiting_input 不计) */
11
+ export function isTerminal(status) {
12
+ return TERMINAL_STATUSES.has(String(status ?? ''))
13
+ }
14
+
15
+ /** 回执通告(v1/legacy 同构;reportPath 可为绝对路径或 inline reportUrl 指引) */
16
+ export function buildNotice(meta, id) {
17
+ const intent = meta.intent ?? 'deep-diagnosis'
18
+ const label = intentLabel(intent)
19
+ if (meta.status === 'completed') {
20
+ const reportPath = meta.reportPath ?? null
21
+ const reportUrl = meta.reportUrl ?? null
22
+ return {
23
+ kind: 'tool-result',
24
+ tool: 'diag_run',
25
+ jobId: id,
26
+ agentId: meta.agentId,
27
+ ...(meta.channelId ? { channelId: meta.channelId } : {}),
28
+ ok: true,
29
+ title: `${label}完成(产线 ${meta.line})`,
30
+ summary: [
31
+ meta.score != null ? `评分=${meta.score}` : '',
32
+ meta.verdict ? `结论=${meta.verdict}` : '',
33
+ meta.summary ? `总结=${String(meta.summary).slice(0, 400)}` : '',
34
+ reportPath ? `report_md_path=${reportPath}` : '',
35
+ reportUrl ? `report_url=${reportUrl}` : '',
36
+ `入库知识库:调用 diag_deposit(run_id=${id})(一键入 aw-docs 文档库并检索自查);需要结构化经验沉淀时再用 kb_agent(action=ingest,目标 aw-industrial)。`,
37
+ ].filter(Boolean).join(';'),
38
+ }
39
+ }
40
+ const statusText = meta.status === 'stopped' ? '已停止' : meta.status === 'expired' ? '已过期' : '失败'
41
+ return {
42
+ kind: 'tool-result',
43
+ tool: 'diag_run',
44
+ jobId: id,
45
+ agentId: meta.agentId,
46
+ ...(meta.channelId ? { channelId: meta.channelId } : {}),
47
+ ok: false,
48
+ title: `${label}${statusText}(产线 ${meta.line})`,
49
+ summary: meta.error
50
+ ? `原因:${meta.error}`
51
+ : `任务以 ${meta.status} 结束;可用 diag_status(run_id=${id}) 查询详情。`,
52
+ }
53
+ }
54
+
55
+ /** 完成态字段落 kv 的统一形状(legacy/v1 共用) */
56
+ export function completedMeta(cur, normalized, extra = {}) {
57
+ return {
58
+ ...cur,
59
+ ...extra,
60
+ status: 'completed',
61
+ completedAt: cur.completedAt ?? Date.now(),
62
+ score: normalized.score ?? null,
63
+ verdict: normalized.verdict ?? null,
64
+ summary: normalized.summary ?? '',
65
+ reportPath: normalized.reportPath ?? null,
66
+ reportUrl: normalized.reportUrl ?? null,
67
+ pollErrors: 0,
68
+ deliverPending: Boolean(cur.agentId),
69
+ }
70
+ }
71
+
72
+ /** 非完成终态的统一形状 */
73
+ export function terminalMeta(cur, status, error, extra = {}) {
74
+ return {
75
+ ...cur,
76
+ ...extra,
77
+ status,
78
+ completedAt: Date.now(),
79
+ error: error ?? cur.error ?? null,
80
+ pollErrors: 0,
81
+ deliverPending: Boolean(cur.agentId),
82
+ }
83
+ }
@@ -1,16 +1,19 @@
1
1
  /**
2
2
  * 自动巡检:轮询 IDD 后台任务并把终态/回执送达发起 Agent。
3
3
  *
4
- * 生命周期铁律(2026-10-18 修复):
5
- * - 未终态 = queued | running 都参与轮询(此前只看 running,queued 会被写回后永久丢弃);
4
+ * 生命周期铁律(2026-10-18 修复,2026-11-18 WS-2 扩展 v1 契约):
5
+ * - 未终态 = queued | running | awaiting_input 都参与轮询与占锁;
6
+ * - v1 六态:completed/failed/stopped/interrupted/expired 为终态;
7
+ * awaiting_input 单列 —— 不触发终态回执、不释放单飞锁,超过老化上限转 expired 并提示;
8
+ * - 契约按 run meta.contract 路由(提交/轮询/状态/回执全链同契约,消除双状态源);
6
9
  * - 终态先落 kv,再投递;投递失败保留 deliverPending 由下一轮重试(不再静默丢失回执);
7
- * - 远端 404/401/403 连续 3 轮 → 收敛为 failed,避免永久 running 占住产线单飞锁;
10
+ * - v1 404 连续 3 轮 → 收敛为 expired(任务过期/IDD 重启丢失),legacy 仍为 failed;
8
11
  * - 单轮不重入(15s 定时器不等待异步,串行 N×8s 可能超过周期)。
9
12
  */
10
13
  import { baseOf, jget, kvRuns, runKey } from './helpers.mjs'
11
- import { intentLabel } from './intents.mjs'
14
+ import { buildNotice, completedMeta, isTerminal, terminalMeta } from './receipt.mjs'
15
+ import { awaitingMaxMinutesOf, normalizeResult, v1Task } from './external-v1.mjs'
12
16
 
13
- const TERMINAL = new Set(['completed', 'failed', 'stopped'])
14
17
  const MAX_DELIVER_ATTEMPTS = 20
15
18
  let sweeping = false
16
19
 
@@ -24,44 +27,6 @@ async function notifyOf(ctx) {
24
27
  }
25
28
  }
26
29
 
27
- function buildNotice(meta, id) {
28
- const intent = meta.intent ?? 'deep-diagnosis'
29
- const label = intentLabel(intent)
30
- if (meta.status === 'completed') {
31
- const reportPath = meta.reportPath ?? null
32
- return {
33
- kind: 'tool-result',
34
- tool: 'diag_run',
35
- jobId: id,
36
- agentId: meta.agentId,
37
- ...(meta.channelId ? { channelId: meta.channelId } : {}),
38
- ok: true,
39
- title: `${label}完成(产线 ${meta.line})`,
40
- summary: [
41
- meta.score != null ? `评分=${meta.score}` : '',
42
- meta.verdict ? `结论=${meta.verdict}` : '',
43
- meta.summary ? `总结=${String(meta.summary).slice(0, 400)}` : '',
44
- reportPath ? `report_md_path=${reportPath}` : '',
45
- reportPath
46
- ? `入库知识库:调用 rag-bridge 的 kb_agent(mode=async)读取该报告文件全文入库(标签:分析,${label}),完成后用 kb_agent_status 查询入库结果。`
47
- : `报告路径未就绪,可用 diag_status(run_id=${id}) 查询详情。`,
48
- ].filter(Boolean).join(';'),
49
- }
50
- }
51
- return {
52
- kind: 'tool-result',
53
- tool: 'diag_run',
54
- jobId: id,
55
- agentId: meta.agentId,
56
- ...(meta.channelId ? { channelId: meta.channelId } : {}),
57
- ok: false,
58
- title: `${label}${meta.status === 'stopped' ? '已停止' : '失败'}(产线 ${meta.line})`,
59
- summary: meta.error
60
- ? `原因:${meta.error}`
61
- : `任务以 ${meta.status} 结束;可用 diag_status(run_id=${id}) 查询详情。`,
62
- }
63
- }
64
-
65
30
  /** 投递终态回执(幂等:deliveredAt/deliverSkipped 后不再投)。失败保留 pending 下轮重试。 */
66
31
  async function tryDeliver(ctx, notify, id) {
67
32
  const cur = ctx.kv.get(runKey(id))
@@ -91,9 +56,26 @@ async function tryDeliver(ctx, notify, id) {
91
56
  }
92
57
  }
93
58
 
59
+ /** 远端状态查询:按 meta.contract 路由(v1 /tasks/:id vs legacy /diagnosis/tasks/:id) */
60
+ async function queryRemote(ctx, base, cur, id) {
61
+ const contract = String(cur.contract ?? 'legacy')
62
+ if (contract === 'v1') {
63
+ const r = await v1Task(ctx, cur.taskId || id, 8000)
64
+ if (!r.ok) {
65
+ const err = new Error(r.error || 'v1 task query failed')
66
+ err.code = String(r.body?.code ?? '')
67
+ err.status = r.status
68
+ throw err
69
+ }
70
+ return { contract, data: r.body?.data ?? {} }
71
+ }
72
+ const st = (await jget(ctx, `${base}/api/diagnosis/tasks/${encodeURIComponent(id)}`, 8000)).data ?? {}
73
+ return { contract, data: st }
74
+ }
75
+
94
76
  export async function sweepOnce(ctx) {
95
77
  if (sweeping) return
96
- const all = kvRuns(ctx).filter(({ meta }) => meta && (!TERMINAL.has(meta.status) || meta.deliverPending))
78
+ const all = kvRuns(ctx).filter(({ meta }) => meta && (!isTerminal(meta.status) || meta.deliverPending))
97
79
  if (!all.length) return
98
80
  sweeping = true
99
81
  try {
@@ -102,77 +84,76 @@ export async function sweepOnce(ctx) {
102
84
  if (!base) {
103
85
  ctx.logger.warn('diag.base_url 非法,本轮跳过任务轮询(仅重试待投递回执)')
104
86
  for (const { id, meta } of all) {
105
- if (TERMINAL.has(meta.status) || meta.deliverPending) await tryDeliver(ctx, notify, id)
87
+ if (isTerminal(meta.status) || meta.deliverPending) await tryDeliver(ctx, notify, id)
106
88
  }
107
89
  return
108
90
  }
109
91
  for (const { id, meta } of all) {
110
92
  const cur = ctx.kv.get(runKey(id)) ?? meta
111
93
  // 已终态但回执未送达:不重复查询远端,只重试投递
112
- if (TERMINAL.has(cur.status)) {
94
+ if (isTerminal(cur.status)) {
113
95
  await tryDeliver(ctx, notify, id)
114
96
  continue
115
97
  }
116
98
  try {
117
- // v2.3:统一走任务管理面 /tasks/:id(与 diag_status 同源)——
118
- // result.report_md_path 是绝对路径(kb_agent 可直接读),result.summary 是报告结论段;
119
- // 旧 /status 的相对路径会让 kb_agent/Agent 找不到文件。
120
- const st = (await jget(ctx, `${base}/api/diagnosis/tasks/${encodeURIComponent(id)}`, 8000)).data ?? {}
121
- const status = String(st.status || st.run_status || cur.status)
122
- const intent = cur.intent ?? 'deep-diagnosis'
123
- const result = st.result ?? {}
99
+ const { contract, data } = await queryRemote(ctx, base, cur, id)
100
+ const status = String(data.status || data.run_status || cur.status)
101
+ const normalized = normalizeResult(data)
124
102
  if (status === 'completed') {
125
- ctx.kv.set(runKey(id), {
126
- ...cur,
127
- status: 'completed',
128
- completedAt: cur.completedAt ?? Date.now(),
129
- name: st.name ?? cur.name,
130
- score: result.score ?? null,
131
- verdict: result.verdict ?? null,
132
- summary: result.summary ?? '',
133
- reportPath: result.report_md_path ?? null,
134
- pollErrors: 0,
135
- deliverPending: Boolean(cur.agentId),
136
- })
137
- ctx.logger.info(`分析任务 ${id}[${intent}](产线 ${cur.line})完成——待 Channel 空闲时 diag_status 查询,经 kb_agent 入库知识库`)
103
+ ctx.kv.set(runKey(id), completedMeta(cur, normalized, {
104
+ contract,
105
+ name: data.name ?? cur.name,
106
+ taskId: cur.taskId || id,
107
+ runId: data.run_id ?? cur.runId ?? id,
108
+ }))
109
+ ctx.logger.info(`分析任务 ${id}[${cur.intent ?? 'deep-diagnosis'}](产线 ${cur.line})完成[contract=${contract}]`)
138
110
  await tryDeliver(ctx, notify, id)
139
111
  }
140
- else if (status === 'failed' || status === 'stopped') {
141
- ctx.kv.set(runKey(id), {
142
- ...cur,
143
- status,
144
- completedAt: Date.now(),
145
- name: st.name ?? cur.name,
146
- error: result.error ?? st.error_message ?? null,
147
- pollErrors: 0,
148
- deliverPending: Boolean(cur.agentId),
149
- })
150
- ctx.logger.warn(`分析任务 ${id}[${intent}](产线 ${cur.line})结束: ${status}${result.error ? `(${result.error})` : ''}`)
112
+ else if (isTerminal(status)) {
113
+ ctx.kv.set(runKey(id), terminalMeta(cur, status, normalized.error ?? data.error_message ?? null, {
114
+ contract,
115
+ name: data.name ?? cur.name,
116
+ }))
117
+ ctx.logger.warn(`分析任务 ${id}[${cur.intent ?? 'deep-diagnosis'}](产线 ${cur.line})结束: ${status}${normalized.error ? `(${normalized.error})` : ''}`)
151
118
  await tryDeliver(ctx, notify, id)
152
119
  }
153
- else if (status !== cur.status || cur.pollErrors) {
120
+ else if (status === 'awaiting_input') {
121
+ // 等待人工介入:占锁 + 不触发终态回执;老化到期转 expired 并提示(Agent 得知需人工介入)
122
+ const since = Number(cur.awaitingSince ?? 0) || Date.now()
123
+ const maxMs = awaitingMaxMinutesOf(ctx) * 60_000
124
+ if (Date.now() - since >= maxMs) {
125
+ const error = `分析在 awaiting_input(等待人工介入)超过 ${Math.round(maxMs / 60_000)} 分钟,已转为 expired;如需继续请重新发起或在 IDD 控制台处理。`
126
+ ctx.kv.set(runKey(id), terminalMeta(cur, 'expired', error, { contract, awaitingSince: since }))
127
+ ctx.logger.warn(`分析任务 ${id} awaiting_input 老化到期 → expired`)
128
+ await tryDeliver(ctx, notify, id)
129
+ }
130
+ else {
131
+ const patch = { ...cur, status: 'awaiting_input', contract, awaitingSince: since, pollErrors: 0 }
132
+ if (!cur.awaitingNotified) {
133
+ patch.awaitingNotified = true
134
+ ctx.logger.warn(`分析任务 ${id} 进入 awaiting_input(等待人工介入,占锁;${Math.round(maxMs / 60_000)} 分钟内未处理将转 expired)`)
135
+ }
136
+ ctx.kv.set(runKey(id), patch)
137
+ }
138
+ }
139
+ else if (status !== cur.status || cur.pollErrors || cur.contract !== contract) {
154
140
  // queued/running 变化照常落 kv(queued 必须留在轮询集合里)
155
- ctx.kv.set(runKey(id), { ...cur, status, name: st.name ?? cur.name, pollErrors: 0, lastPollError: undefined })
141
+ ctx.kv.set(runKey(id), { ...cur, status, contract, name: data.name ?? cur.name, pollErrors: 0, lastPollError: undefined })
156
142
  }
157
143
  }
158
144
  catch (err) {
159
145
  const msg = String(err?.message ?? err)
160
- const code = /HTTP (\d{3})/.exec(msg)?.[1] ?? ''
146
+ const code = String(err?.code ?? '') || (/HTTP (\d{3})/.exec(msg)?.[1] ?? '')
161
147
  const attempts = Number(cur.pollErrors ?? 0) + 1
162
148
  // 404=任务在 IDD 侧不存在;401/403=token/所有权变化。连续 3 轮仍不可查询 →
163
- // 收敛为 failed 并回执,避免永久 running 占住产线单飞锁(此前每 15s 无限重试)。
149
+ // v1 按 expired(任务过期/重启丢失),legacy 按 failed(历史语义不变)。
164
150
  if (['404', '401', '403'].includes(code) && attempts >= 3) {
151
+ const contract = String(cur.contract ?? 'legacy')
152
+ const status = code === '404' && contract === 'v1' ? 'expired' : 'failed'
165
153
  const error = code === '404'
166
154
  ? `远端分析任务不存在(HTTP 404,可能被 IDD 清理或服务重启):${msg}`
167
155
  : `远端任务查询被拒(HTTP ${code},请检查诊断 Token):${msg}`
168
- ctx.kv.set(runKey(id), {
169
- ...cur,
170
- status: 'failed',
171
- completedAt: Date.now(),
172
- error,
173
- pollErrors: attempts,
174
- deliverPending: Boolean(cur.agentId),
175
- })
156
+ ctx.kv.set(runKey(id), terminalMeta(cur, status, error, { pollErrors: attempts }))
176
157
  ctx.logger.warn(`分析任务 ${id} 无法继续跟踪(第 ${attempts} 轮): ${error}`)
177
158
  await tryDeliver(ctx, notify, id)
178
159
  }
@@ -187,5 +168,3 @@ export async function sweepOnce(ctx) {
187
168
  sweeping = false
188
169
  }
189
170
  }
190
-
191
- // ── 插件入口 ─────────────────────────────────────────────────────────────
@@ -4,6 +4,8 @@
4
4
  "base_url": "诊断服务地址",
5
5
  "token": "诊断 API Token",
6
6
  "harness": "诊断引擎",
7
+ "contract": "IDD 契约",
8
+ "awaiting_max_minutes": "等待人工介入上限(分钟)",
7
9
  "max_turns": "诊断最大轮数",
8
10
  "max_minutes": "诊断超时(分钟)",
9
11
  "auto_enabled": "自动诊断",
@@ -99,6 +101,8 @@
99
101
  "base_url": "Diagnosis service URL",
100
102
  "token": "Diagnosis API token",
101
103
  "harness": "Diagnosis harness",
104
+ "contract": "IDD contract",
105
+ "awaiting_max_minutes": "Awaiting-input timeout (minutes)",
102
106
  "max_turns": "Max turns",
103
107
  "max_minutes": "Timeout (minutes)",
104
108
  "auto_enabled": "Auto diagnosis",
@@ -3,9 +3,18 @@
3
3
  "settings": {
4
4
  "base_url": "rag-knowledge 后端地址",
5
5
  "web_url": "rag-knowledge Web 地址",
6
+ "docs_kb": "文档知识库名",
6
7
  "token": "rag-knowledge API Token",
7
8
  "agent_harness": "知识库 Agent 引擎"
8
9
  },
10
+ "tools": {
11
+ "kb_agent": "知识库Agent",
12
+ "kb_agent_status": "知识库任务状态",
13
+ "kb_doc_create": "创建知识库文档",
14
+ "kb_doc_search": "检索知识库文档",
15
+ "kb_doc_read": "读取知识库文档",
16
+ "kb_catalog": "知识库目录"
17
+ },
9
18
  "panel": {
10
19
  "title": "工业知识库(rag-knowledge)",
11
20
  "backend": "后端",
@@ -45,9 +54,18 @@
45
54
  "settings": {
46
55
  "base_url": "rag-knowledge backend URL",
47
56
  "web_url": "rag-knowledge Web URL",
57
+ "docs_kb": "Documents knowledge base name",
48
58
  "token": "rag-knowledge API token",
49
59
  "agent_harness": "Knowledge-base agent engine"
50
60
  },
61
+ "tools": {
62
+ "kb_agent": "Knowledge base agent",
63
+ "kb_agent_status": "KB task status",
64
+ "kb_doc_create": "Create KB document",
65
+ "kb_doc_search": "Search KB documents",
66
+ "kb_doc_read": "Read KB document",
67
+ "kb_catalog": "KB catalog"
68
+ },
51
69
  "panel": {
52
70
  "title": "Industrial knowledge base (rag-knowledge)",
53
71
  "backend": "Backend",