@tothemax/tasking 0.0.0-stage → 0.6.16
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/LICENSE +21 -0
- package/README.md +121 -2
- package/README.zh-CN.md +122 -0
- package/assets/skills/install-glab/SKILL.md +70 -0
- package/assets/skills/land/SKILL.md +63 -0
- package/assets/skills/land/land_watch.py +312 -0
- package/assets/skills/openspec-archive-change/SKILL.md +114 -0
- package/assets/skills/opsx-grill-with-docs/SKILL.md +193 -0
- package/assets/skills/opsx-implement/SKILL.md +258 -0
- package/assets/skills/opsx-propose-with-tdd/SKILL.md +271 -0
- package/assets/workflows/WORKFLOW.code-mr.en.md.example +345 -0
- package/assets/workflows/WORKFLOW.code-mr.md.example +339 -0
- package/assets/workflows/WORKFLOW.feishu-direct.en.md.example +267 -0
- package/assets/workflows/WORKFLOW.feishu-direct.md.example +258 -0
- package/assets/workflows/WORKFLOW.grill-openspec.en.md.example +362 -0
- package/assets/workflows/WORKFLOW.grill-openspec.md.example +356 -0
- package/assets/workflows/WORKFLOW.minimal.en.md.example +296 -0
- package/assets/workflows/WORKFLOW.minimal.md.example +289 -0
- package/dist/adapters/feishu/events.d.ts +55 -0
- package/dist/adapters/feishu/events.d.ts.map +1 -0
- package/dist/adapters/feishu/events.js +184 -0
- package/dist/adapters/feishu/events.js.map +1 -0
- package/dist/adapters/feishu/index.d.ts +2 -0
- package/dist/adapters/feishu/index.d.ts.map +1 -0
- package/dist/adapters/feishu/index.js +2 -0
- package/dist/adapters/feishu/index.js.map +1 -0
- package/dist/adapters/feishu/probe.d.ts +31 -0
- package/dist/adapters/feishu/probe.d.ts.map +1 -0
- package/dist/adapters/feishu/probe.js +179 -0
- package/dist/adapters/feishu/probe.js.map +1 -0
- package/dist/adapters/feishu/source.d.ts +124 -0
- package/dist/adapters/feishu/source.d.ts.map +1 -0
- package/dist/adapters/feishu/source.js +715 -0
- package/dist/adapters/feishu/source.js.map +1 -0
- package/dist/adapters/jira/index.d.ts +2 -0
- package/dist/adapters/jira/index.d.ts.map +1 -0
- package/dist/adapters/jira/index.js +2 -0
- package/dist/adapters/jira/index.js.map +1 -0
- package/dist/adapters/jira/tracker.d.ts +63 -0
- package/dist/adapters/jira/tracker.d.ts.map +1 -0
- package/dist/adapters/jira/tracker.js +402 -0
- package/dist/adapters/jira/tracker.js.map +1 -0
- package/dist/bridge/binding-store.d.ts +93 -0
- package/dist/bridge/binding-store.d.ts.map +1 -0
- package/dist/bridge/binding-store.js +365 -0
- package/dist/bridge/binding-store.js.map +1 -0
- package/dist/bridge/commands.d.ts +45 -0
- package/dist/bridge/commands.d.ts.map +1 -0
- package/dist/bridge/commands.js +197 -0
- package/dist/bridge/commands.js.map +1 -0
- package/dist/bridge/comment-bridge.d.ts +116 -0
- package/dist/bridge/comment-bridge.d.ts.map +1 -0
- package/dist/bridge/comment-bridge.js +1473 -0
- package/dist/bridge/comment-bridge.js.map +1 -0
- package/dist/cli/daemon-info.d.ts +17 -0
- package/dist/cli/daemon-info.d.ts.map +1 -0
- package/dist/cli/daemon-info.js +64 -0
- package/dist/cli/daemon-info.js.map +1 -0
- package/dist/cli/daemon.d.ts +27 -0
- package/dist/cli/daemon.d.ts.map +1 -0
- package/dist/cli/daemon.js +77 -0
- package/dist/cli/daemon.js.map +1 -0
- package/dist/cli/doctor.d.ts +7 -0
- package/dist/cli/doctor.d.ts.map +1 -0
- package/dist/cli/doctor.js +156 -0
- package/dist/cli/doctor.js.map +1 -0
- package/dist/cli/event-channel.d.ts +16 -0
- package/dist/cli/event-channel.d.ts.map +1 -0
- package/dist/cli/event-channel.js +51 -0
- package/dist/cli/event-channel.js.map +1 -0
- package/dist/cli/help.d.ts +3 -0
- package/dist/cli/help.d.ts.map +1 -0
- package/dist/cli/help.js +155 -0
- package/dist/cli/help.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +774 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/init.d.ts +64 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +1296 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/instance-lock.d.ts +13 -0
- package/dist/cli/instance-lock.d.ts.map +1 -0
- package/dist/cli/instance-lock.js +53 -0
- package/dist/cli/instance-lock.js.map +1 -0
- package/dist/cli/prompts.d.ts +13 -0
- package/dist/cli/prompts.d.ts.map +1 -0
- package/dist/cli/prompts.js +26 -0
- package/dist/cli/prompts.js.map +1 -0
- package/dist/cli/style.d.ts +26 -0
- package/dist/cli/style.d.ts.map +1 -0
- package/dist/cli/style.js +58 -0
- package/dist/cli/style.js.map +1 -0
- package/dist/cli/tui-helpers.d.ts +5 -0
- package/dist/cli/tui-helpers.d.ts.map +1 -0
- package/dist/cli/tui-helpers.js +124 -0
- package/dist/cli/tui-helpers.js.map +1 -0
- package/dist/cli/workflow-path.d.ts +12 -0
- package/dist/cli/workflow-path.d.ts.map +1 -0
- package/dist/cli/workflow-path.js +37 -0
- package/dist/cli/workflow-path.js.map +1 -0
- package/dist/config/config-types.d.ts +180 -0
- package/dist/config/config-types.d.ts.map +1 -0
- package/dist/config/config-types.js +71 -0
- package/dist/config/config-types.js.map +1 -0
- package/dist/config/dotenv.d.ts +2 -0
- package/dist/config/dotenv.d.ts.map +1 -0
- package/dist/config/dotenv.js +10 -0
- package/dist/config/dotenv.js.map +1 -0
- package/dist/config/index.d.ts +6 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +6 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/loader.d.ts +5 -0
- package/dist/config/loader.d.ts.map +1 -0
- package/dist/config/loader.js +304 -0
- package/dist/config/loader.js.map +1 -0
- package/dist/config/manager.d.ts +31 -0
- package/dist/config/manager.d.ts.map +1 -0
- package/dist/config/manager.js +110 -0
- package/dist/config/manager.js.map +1 -0
- package/dist/config/resolver.d.ts +6 -0
- package/dist/config/resolver.d.ts.map +1 -0
- package/dist/config/resolver.js +267 -0
- package/dist/config/resolver.js.map +1 -0
- package/dist/config/validator.d.ts +9 -0
- package/dist/config/validator.d.ts.map +1 -0
- package/dist/config/validator.js +70 -0
- package/dist/config/validator.js.map +1 -0
- package/dist/direct/card.d.ts +49 -0
- package/dist/direct/card.d.ts.map +1 -0
- package/dist/direct/card.js +274 -0
- package/dist/direct/card.js.map +1 -0
- package/dist/direct/command.d.ts +12 -0
- package/dist/direct/command.d.ts.map +1 -0
- package/dist/direct/command.js +23 -0
- package/dist/direct/command.js.map +1 -0
- package/dist/direct/manager.d.ts +159 -0
- package/dist/direct/manager.d.ts.map +1 -0
- package/dist/direct/manager.js +1862 -0
- package/dist/direct/manager.js.map +1 -0
- package/dist/mcp/inject.d.ts +5 -0
- package/dist/mcp/inject.d.ts.map +1 -0
- package/dist/mcp/inject.js +51 -0
- package/dist/mcp/inject.js.map +1 -0
- package/dist/mcp/jira-server.d.ts +29 -0
- package/dist/mcp/jira-server.d.ts.map +1 -0
- package/dist/mcp/jira-server.js +288 -0
- package/dist/mcp/jira-server.js.map +1 -0
- package/dist/observability/activity-store.d.ts +29 -0
- package/dist/observability/activity-store.d.ts.map +1 -0
- package/dist/observability/activity-store.js +32 -0
- package/dist/observability/activity-store.js.map +1 -0
- package/dist/observability/config-view.d.ts +19 -0
- package/dist/observability/config-view.d.ts.map +1 -0
- package/dist/observability/config-view.js +61 -0
- package/dist/observability/config-view.js.map +1 -0
- package/dist/observability/http-dashboard.d.ts +36 -0
- package/dist/observability/http-dashboard.d.ts.map +1 -0
- package/dist/observability/http-dashboard.js +297 -0
- package/dist/observability/http-dashboard.js.map +1 -0
- package/dist/observability/log-buffer.d.ts +21 -0
- package/dist/observability/log-buffer.d.ts.map +1 -0
- package/dist/observability/log-buffer.js +69 -0
- package/dist/observability/log-buffer.js.map +1 -0
- package/dist/observability/logger.d.ts +37 -0
- package/dist/observability/logger.d.ts.map +1 -0
- package/dist/observability/logger.js +114 -0
- package/dist/observability/logger.js.map +1 -0
- package/dist/observability/snapshot.d.ts +11 -0
- package/dist/observability/snapshot.d.ts.map +1 -0
- package/dist/observability/snapshot.js +59 -0
- package/dist/observability/snapshot.js.map +1 -0
- package/dist/observability/terminal-dashboard.d.ts +155 -0
- package/dist/observability/terminal-dashboard.d.ts.map +1 -0
- package/dist/observability/terminal-dashboard.js +1598 -0
- package/dist/observability/terminal-dashboard.js.map +1 -0
- package/dist/observability/wrap.d.ts +4 -0
- package/dist/observability/wrap.d.ts.map +1 -0
- package/dist/observability/wrap.js +80 -0
- package/dist/observability/wrap.js.map +1 -0
- package/dist/scheduler/index.d.ts +2 -0
- package/dist/scheduler/index.d.ts.map +1 -0
- package/dist/scheduler/index.js +2 -0
- package/dist/scheduler/index.js.map +1 -0
- package/dist/scheduler/scheduler.d.ts +52 -0
- package/dist/scheduler/scheduler.d.ts.map +1 -0
- package/dist/scheduler/scheduler.js +535 -0
- package/dist/scheduler/scheduler.js.map +1 -0
- package/dist/shared/agent-resolver.d.ts +11 -0
- package/dist/shared/agent-resolver.d.ts.map +1 -0
- package/dist/shared/agent-resolver.js +20 -0
- package/dist/shared/agent-resolver.js.map +1 -0
- package/dist/shared/errors.d.ts +14 -0
- package/dist/shared/errors.d.ts.map +1 -0
- package/dist/shared/errors.js +24 -0
- package/dist/shared/errors.js.map +1 -0
- package/dist/shared/index.d.ts +4 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +4 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/shared/info-card.d.ts +24 -0
- package/dist/shared/info-card.d.ts.map +1 -0
- package/dist/shared/info-card.js +46 -0
- package/dist/shared/info-card.js.map +1 -0
- package/dist/shared/types.d.ts +148 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +15 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/translate/card.d.ts +2 -0
- package/dist/translate/card.d.ts.map +1 -0
- package/dist/translate/card.js +17 -0
- package/dist/translate/card.js.map +1 -0
- package/dist/translate/dict.d.ts +7 -0
- package/dist/translate/dict.d.ts.map +1 -0
- package/dist/translate/dict.js +895 -0
- package/dist/translate/dict.js.map +1 -0
- package/dist/translate/locale.d.ts +8 -0
- package/dist/translate/locale.d.ts.map +1 -0
- package/dist/translate/locale.js +75 -0
- package/dist/translate/locale.js.map +1 -0
- package/dist/translate/normalize.d.ts +4 -0
- package/dist/translate/normalize.d.ts.map +1 -0
- package/dist/translate/normalize.js +38 -0
- package/dist/translate/normalize.js.map +1 -0
- package/dist/translate/output.d.ts +3 -0
- package/dist/translate/output.d.ts.map +1 -0
- package/dist/translate/output.js +16 -0
- package/dist/translate/output.js.map +1 -0
- package/dist/worker/prompt.d.ts +11 -0
- package/dist/worker/prompt.d.ts.map +1 -0
- package/dist/worker/prompt.js +153 -0
- package/dist/worker/prompt.js.map +1 -0
- package/dist/worker/runner.d.ts +101 -0
- package/dist/worker/runner.d.ts.map +1 -0
- package/dist/worker/runner.js +448 -0
- package/dist/worker/runner.js.map +1 -0
- package/dist/workspace/index.d.ts +2 -0
- package/dist/workspace/index.d.ts.map +1 -0
- package/dist/workspace/index.js +2 -0
- package/dist/workspace/index.js.map +1 -0
- package/dist/workspace/manager.d.ts +37 -0
- package/dist/workspace/manager.d.ts.map +1 -0
- package/dist/workspace/manager.js +157 -0
- package/dist/workspace/manager.js.map +1 -0
- package/package.json +70 -4
|
@@ -0,0 +1,1473 @@
|
|
|
1
|
+
import { isReadableText, toSecondsStr, } from "../adapters/feishu/source.js";
|
|
2
|
+
import { isTerminalState, jiraBrowseUrl, wrapDescription } from "../adapters/jira/tracker.js";
|
|
3
|
+
import { resolveAgent } from "../shared/agent-resolver.js";
|
|
4
|
+
import { toTaskingError } from "../shared/errors.js";
|
|
5
|
+
import { buildInfoCard } from "../shared/info-card.js";
|
|
6
|
+
import { translate } from "../translate/locale.js";
|
|
7
|
+
import { THREAD_ACK_FAIL_TEXT, THREAD_ACK_TEXT, buildBridgedCommentBody, hasSentinel, helpLinesFor, helpTextFor, intakeAckTextFor, intakeCommandsFor, isCommand, isHelp, parseAgent, parseChat, parseIssue, parseState, renderForwardedComment, threadCommandsFor, threadHelpLinesFor, threadHelpTextFor, } from "./commands.js";
|
|
8
|
+
const BRIDGE_CARD_ACTION_DEDUP_MS = 2 * 60 * 1000;
|
|
9
|
+
const MEMBER_CACHE_TTL_MS = 60 * 60 * 1000;
|
|
10
|
+
const BRIDGE_WRITE_ACTIONS = new Set(["tr_confirm", "full", "state_summon", "agent_set"]);
|
|
11
|
+
const BRIDGE_JIRA_CARD_ACTIONS = new Set([
|
|
12
|
+
"agent_set",
|
|
13
|
+
"state_summon",
|
|
14
|
+
"state_refresh",
|
|
15
|
+
"state_open",
|
|
16
|
+
"tr",
|
|
17
|
+
"tr_confirm",
|
|
18
|
+
"tr_cancel",
|
|
19
|
+
"full",
|
|
20
|
+
]);
|
|
21
|
+
const COMMENT_CARD_BUDGET_BYTES = 24 * 1024;
|
|
22
|
+
export function isJiraNotFound(e) {
|
|
23
|
+
const err = toTaskingError(e);
|
|
24
|
+
if (err.category !== "jira_api_status")
|
|
25
|
+
return false;
|
|
26
|
+
const status = err.details?.status;
|
|
27
|
+
return status === 404;
|
|
28
|
+
}
|
|
29
|
+
export class CommentBridge {
|
|
30
|
+
deps;
|
|
31
|
+
get jiraTracker() {
|
|
32
|
+
return this.deps.tracker;
|
|
33
|
+
}
|
|
34
|
+
static DEFAULT_RETRY_DELAYS_MS = [30000, 120000, 600000];
|
|
35
|
+
running = false;
|
|
36
|
+
stopped = false;
|
|
37
|
+
processed = new Set();
|
|
38
|
+
queue = [];
|
|
39
|
+
consuming = false;
|
|
40
|
+
forwardTimer = null;
|
|
41
|
+
sweepTimer = null;
|
|
42
|
+
retryTimers = new Set();
|
|
43
|
+
terminalHintedRoots = new Set();
|
|
44
|
+
cardActionSeen = new Map();
|
|
45
|
+
memberCache = new Map();
|
|
46
|
+
constructor(deps) {
|
|
47
|
+
this.deps = deps;
|
|
48
|
+
}
|
|
49
|
+
async handleCardAction(action) {
|
|
50
|
+
const value = (action.value ?? {});
|
|
51
|
+
const name = typeof value.action === "string" ? value.action : "";
|
|
52
|
+
if (name === "")
|
|
53
|
+
return undefined;
|
|
54
|
+
if (BRIDGE_JIRA_CARD_ACTIONS.has(name) && this.jiraTracker === null) {
|
|
55
|
+
return { toast: { type: "warning", content: "该操作需要 Jira 任务源" } };
|
|
56
|
+
}
|
|
57
|
+
if (BRIDGE_WRITE_ACTIONS.has(name) && !this.claimCardAction(action)) {
|
|
58
|
+
return { toast: { type: "info", content: "该操作已处理" } };
|
|
59
|
+
}
|
|
60
|
+
this.deps.logger.debug("bridge card action received", {
|
|
61
|
+
action: name,
|
|
62
|
+
operator: action.operatorOpenId ?? null,
|
|
63
|
+
message_id: action.messageId,
|
|
64
|
+
});
|
|
65
|
+
if (name === "agent_set") {
|
|
66
|
+
const b = await this.bindingFromValue(value);
|
|
67
|
+
const agent = typeof value.agent === "string" ? value.agent : "";
|
|
68
|
+
if (b === null)
|
|
69
|
+
return { toast: { type: "error", content: "任务不存在" } };
|
|
70
|
+
const parsed = agent === "" ? { kind: "reset", name: null } : { kind: "switch", name: agent };
|
|
71
|
+
this.deferAction(async () => {
|
|
72
|
+
await this.executeAgentSwitch(parsed, b, b.root_id, this.deps.config());
|
|
73
|
+
const config = this.deps.config();
|
|
74
|
+
const lines = await this.renderAgentList(b, config);
|
|
75
|
+
try {
|
|
76
|
+
await this.deps.feishu.updateCard(action.messageId, this.agentListCard(b, config, lines));
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
this.deps.logger.debug("agent card refresh failed", {
|
|
80
|
+
issue: b.issue_key,
|
|
81
|
+
error: toTaskingError(e).message,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
toast: {
|
|
87
|
+
type: "info",
|
|
88
|
+
content: agent === "" ? "正在恢复默认 agent…" : `正在切换 ${agent}…`,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
if (name === "state_summon") {
|
|
93
|
+
const b = await this.bindingFromValue(value);
|
|
94
|
+
if (b === null)
|
|
95
|
+
return { toast: { type: "error", content: "任务不存在" } };
|
|
96
|
+
this.deferAction(async () => {
|
|
97
|
+
const info = await this.fetchState(b.issue_id);
|
|
98
|
+
await this.publishStateCard(b, info.state, info.assignee);
|
|
99
|
+
});
|
|
100
|
+
return { toast: { type: "info", content: "已发送状态卡" } };
|
|
101
|
+
}
|
|
102
|
+
if (name === "state_refresh") {
|
|
103
|
+
const b = await this.bindingFromValue(value);
|
|
104
|
+
if (b === null)
|
|
105
|
+
return { toast: { type: "error", content: "任务不存在" } };
|
|
106
|
+
const info = await this.fetchState(b.issue_id);
|
|
107
|
+
return {
|
|
108
|
+
toast: { type: "success", content: "已刷新" },
|
|
109
|
+
card: this.stateCard(b, info.state, info.assignee),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (name === "state_open") {
|
|
113
|
+
const tracker = this.jiraTracker;
|
|
114
|
+
if (tracker === null)
|
|
115
|
+
return undefined;
|
|
116
|
+
const b = await this.bindingFromValue(value);
|
|
117
|
+
if (b === null)
|
|
118
|
+
return { toast: { type: "error", content: "任务不存在" } };
|
|
119
|
+
const info = await this.fetchState(b.issue_id);
|
|
120
|
+
try {
|
|
121
|
+
const transitions = await tracker.fetchTransitions(b.issue_key);
|
|
122
|
+
if (transitions.length === 0) {
|
|
123
|
+
return {
|
|
124
|
+
toast: { type: "info", content: "当前没有可用转换" },
|
|
125
|
+
card: this.stateCard(b, info.state, info.assignee),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
toast: { type: "info", content: "请选择目标状态" },
|
|
130
|
+
card: this.transitionPickCard(b, info, transitions),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
return { toast: { type: "error", content: `查询失败:${toTaskingError(e).message}` } };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (name === "tr_cancel") {
|
|
138
|
+
const b = await this.bindingFromValue(value);
|
|
139
|
+
if (b === null)
|
|
140
|
+
return { toast: { type: "error", content: "任务不存在" } };
|
|
141
|
+
const info = await this.fetchState(b.issue_id);
|
|
142
|
+
return {
|
|
143
|
+
toast: { type: "info", content: "已取消" },
|
|
144
|
+
card: this.stateCard(b, info.state, info.assignee),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (name === "tr") {
|
|
148
|
+
const b = await this.bindingFromValue(value);
|
|
149
|
+
const transitionId = typeof value.transition_id === "string" ? value.transition_id : "";
|
|
150
|
+
const transitionName = typeof value.transition_name === "string" ? value.transition_name : "";
|
|
151
|
+
if (b === null || transitionId === "")
|
|
152
|
+
return undefined;
|
|
153
|
+
const info = await this.fetchState(b.issue_id);
|
|
154
|
+
return {
|
|
155
|
+
toast: { type: "warning", content: "请确认" },
|
|
156
|
+
card: this.transitionConfirmCard(b, info, { id: transitionId, toName: transitionName }),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (name === "tr_confirm") {
|
|
160
|
+
const issueId = typeof value.issue_id === "string" ? value.issue_id : "";
|
|
161
|
+
const transitionId = typeof value.transition_id === "string" ? value.transition_id : "";
|
|
162
|
+
const transitionName = typeof value.transition_name === "string" ? value.transition_name : "";
|
|
163
|
+
if (issueId === "" || transitionId === "")
|
|
164
|
+
return undefined;
|
|
165
|
+
this.deferAction(() => this.applyTransition(issueId, transitionId, transitionName, action));
|
|
166
|
+
return { toast: { type: "info", content: "正在转换…" } };
|
|
167
|
+
}
|
|
168
|
+
if (name === "full") {
|
|
169
|
+
const issueId = typeof value.issue_id === "string" ? value.issue_id : "";
|
|
170
|
+
const commentId = typeof value.comment_id === "string" ? value.comment_id : "";
|
|
171
|
+
if (issueId === "" || commentId === "")
|
|
172
|
+
return undefined;
|
|
173
|
+
this.deferAction(() => this.deliverFullComment(issueId, commentId));
|
|
174
|
+
return { toast: { type: "info", content: "正在整理完整内容…" } };
|
|
175
|
+
}
|
|
176
|
+
return { toast: { type: "warning", content: "未知操作" } };
|
|
177
|
+
}
|
|
178
|
+
async bindingFromValue(value) {
|
|
179
|
+
const issueId = typeof value.issue_id === "string" ? value.issue_id : "";
|
|
180
|
+
if (issueId === "")
|
|
181
|
+
return null;
|
|
182
|
+
return this.deps.bindings.getBinding(issueId);
|
|
183
|
+
}
|
|
184
|
+
async fetchState(issueId) {
|
|
185
|
+
const tracker = this.jiraTracker;
|
|
186
|
+
if (tracker === null)
|
|
187
|
+
return { state: "", assignee: null };
|
|
188
|
+
try {
|
|
189
|
+
const tasks = await tracker.fetchIssueStatesByIds([issueId]);
|
|
190
|
+
const task = tasks[0];
|
|
191
|
+
return { state: task?.state ?? "", assignee: task?.assignee ?? null };
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
this.deps.logger.debug("state card state fetch failed", {
|
|
195
|
+
issue_id: issueId,
|
|
196
|
+
error: toTaskingError(e).message,
|
|
197
|
+
});
|
|
198
|
+
return { state: "", assignee: null };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
stateCardLines(state, assignee) {
|
|
202
|
+
return [
|
|
203
|
+
`状态:${state === "" ? translate("(未知)") : state}`,
|
|
204
|
+
`负责人:${assignee === null || assignee === "" ? translate("(未分配)") : assignee}`,
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
stateButtons(b) {
|
|
208
|
+
const buttons = [
|
|
209
|
+
{ label: "🔀 转状态", action: "state_open", value: { issue_id: b.issue_id } },
|
|
210
|
+
{ label: "🔄 刷新", action: "state_refresh", value: { issue_id: b.issue_id } },
|
|
211
|
+
];
|
|
212
|
+
const url = jiraBrowseUrl(this.deps.config().tracker.endpoint, b.issue_key);
|
|
213
|
+
if (url !== null)
|
|
214
|
+
buttons.push({ label: "打开 Jira", url });
|
|
215
|
+
return buttons;
|
|
216
|
+
}
|
|
217
|
+
stateCard(b, state, assignee) {
|
|
218
|
+
return buildInfoCard({
|
|
219
|
+
title: `📋 ${b.issue_key}`,
|
|
220
|
+
lines: this.stateCardLines(state, assignee),
|
|
221
|
+
buttons: this.stateButtons(b),
|
|
222
|
+
rootId: b.root_id,
|
|
223
|
+
kind: "bridge",
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
transitionPickCard(b, info, transitions) {
|
|
227
|
+
const buttons = transitions.map((t) => ({
|
|
228
|
+
label: t.toName,
|
|
229
|
+
action: "tr",
|
|
230
|
+
value: { issue_id: b.issue_id, transition_id: t.id, transition_name: t.toName },
|
|
231
|
+
}));
|
|
232
|
+
buttons.push({ label: "✖ 取消", action: "tr_cancel", value: { issue_id: b.issue_id } });
|
|
233
|
+
return buildInfoCard({
|
|
234
|
+
title: `📋 ${b.issue_key}`,
|
|
235
|
+
lines: [...this.stateCardLines(info.state, info.assignee), "", "选择目标状态:"],
|
|
236
|
+
buttons,
|
|
237
|
+
rootId: b.root_id,
|
|
238
|
+
kind: "bridge",
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
transitionConfirmCard(b, info, choice) {
|
|
242
|
+
return buildInfoCard({
|
|
243
|
+
title: `📋 ${b.issue_key}`,
|
|
244
|
+
lines: [
|
|
245
|
+
...this.stateCardLines(info.state, info.assignee),
|
|
246
|
+
"",
|
|
247
|
+
`确认转为「${choice.toName}」?`,
|
|
248
|
+
],
|
|
249
|
+
buttons: [
|
|
250
|
+
{
|
|
251
|
+
label: "✅ 确认",
|
|
252
|
+
action: "tr_confirm",
|
|
253
|
+
type: "primary",
|
|
254
|
+
value: {
|
|
255
|
+
issue_id: b.issue_id,
|
|
256
|
+
transition_id: choice.id,
|
|
257
|
+
transition_name: choice.toName,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
{ label: "✖ 取消", action: "tr_cancel", value: { issue_id: b.issue_id } },
|
|
261
|
+
],
|
|
262
|
+
rootId: b.root_id,
|
|
263
|
+
kind: "bridge",
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
async applyTransition(issueId, transitionId, transitionName, action) {
|
|
267
|
+
const tracker = this.jiraTracker;
|
|
268
|
+
if (tracker === null)
|
|
269
|
+
return;
|
|
270
|
+
const b = this.deps.bindings.getBinding(issueId);
|
|
271
|
+
if (b === null)
|
|
272
|
+
return;
|
|
273
|
+
const config = this.deps.config();
|
|
274
|
+
try {
|
|
275
|
+
await tracker.transitionIssue(b.issue_key, transitionId);
|
|
276
|
+
}
|
|
277
|
+
catch (e) {
|
|
278
|
+
const info = await this.fetchState(b.issue_id);
|
|
279
|
+
await this.patchStateCard(b, info.state, info.assignee);
|
|
280
|
+
await this.deps.feishu.replyInThread(b.root_id, `⚠️ 转状态失败:${toTaskingError(e).message}`);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
this.deps.requestRefresh?.();
|
|
284
|
+
const isTerminal = isTerminalState(transitionName, config.tracker.terminal_states);
|
|
285
|
+
if (isTerminal)
|
|
286
|
+
this.deps.bindings.markTerminal(b.issue_id);
|
|
287
|
+
const info = await this.fetchState(b.issue_id);
|
|
288
|
+
const state = info.state === "" ? transitionName : info.state;
|
|
289
|
+
const fresh = this.deps.bindings.getBinding(issueId) ?? b;
|
|
290
|
+
const card = this.stateCard(fresh, state, info.assignee);
|
|
291
|
+
try {
|
|
292
|
+
await this.deps.feishu.updateCard(action.messageId, card);
|
|
293
|
+
}
|
|
294
|
+
catch (e) {
|
|
295
|
+
this.deps.logger.debug("clicked state card refresh failed", {
|
|
296
|
+
issue: b.issue_key,
|
|
297
|
+
error: toTaskingError(e).message,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
if (fresh.card_message_id !== action.messageId) {
|
|
301
|
+
await this.patchStateCard(fresh, state, info.assignee);
|
|
302
|
+
}
|
|
303
|
+
const who = await this.operatorLabel(action);
|
|
304
|
+
await this.deps.feishu.replyInThread(b.root_id, `✅ 已转 ${transitionName}${isTerminal ? translate("(完结)") : ""}(操作人:${who})`);
|
|
305
|
+
}
|
|
306
|
+
async operatorLabel(action) {
|
|
307
|
+
if (action.operatorName !== null && action.operatorName !== "")
|
|
308
|
+
return action.operatorName;
|
|
309
|
+
const openId = action.operatorOpenId;
|
|
310
|
+
if (openId === null || openId === "")
|
|
311
|
+
return "未知";
|
|
312
|
+
const cached = this.memberCache.get(action.chatId);
|
|
313
|
+
if (cached && Date.now() - cached.at < MEMBER_CACHE_TTL_MS) {
|
|
314
|
+
const hit = cached.names.get(openId);
|
|
315
|
+
if (hit !== undefined && hit !== "")
|
|
316
|
+
return hit;
|
|
317
|
+
}
|
|
318
|
+
const names = await this.fetchMemberNames(action.chatId);
|
|
319
|
+
const name = names?.get(openId);
|
|
320
|
+
return name !== undefined && name !== "" ? name : openId;
|
|
321
|
+
}
|
|
322
|
+
async fetchMemberNames(chatId) {
|
|
323
|
+
if (chatId === "")
|
|
324
|
+
return null;
|
|
325
|
+
try {
|
|
326
|
+
const members = await this.deps.feishu.listChatMembers(chatId);
|
|
327
|
+
const names = new Map(members.map((m) => [m.open_id, m.name]));
|
|
328
|
+
this.memberCache.set(chatId, { at: Date.now(), names });
|
|
329
|
+
return names;
|
|
330
|
+
}
|
|
331
|
+
catch (e) {
|
|
332
|
+
this.deps.logger.debug("bridge member lookup failed", {
|
|
333
|
+
chat_id: chatId,
|
|
334
|
+
error: toTaskingError(e).message,
|
|
335
|
+
});
|
|
336
|
+
return this.memberCache.get(chatId)?.names ?? null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async patchStateCard(b, state, assignee) {
|
|
340
|
+
const tracked = this.deps.bindings.getBinding(b.issue_id) ?? b;
|
|
341
|
+
if (tracked.card_message_id === null) {
|
|
342
|
+
await this.publishStateCard(b, state, assignee);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
try {
|
|
346
|
+
await this.deps.feishu.updateCard(tracked.card_message_id, this.stateCard(tracked, state, assignee));
|
|
347
|
+
}
|
|
348
|
+
catch (e) {
|
|
349
|
+
this.deps.logger.warn("state card patch failed; republishing", {
|
|
350
|
+
issue: b.issue_key,
|
|
351
|
+
error: toTaskingError(e).message,
|
|
352
|
+
});
|
|
353
|
+
await this.publishStateCard(b, state, assignee);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
async publishStateCard(b, state, assignee) {
|
|
357
|
+
const tracked = this.deps.bindings.getBinding(b.issue_id) ?? b;
|
|
358
|
+
if (tracked.card_message_id !== null) {
|
|
359
|
+
try {
|
|
360
|
+
await this.deps.feishu.deleteMessage(tracked.card_message_id);
|
|
361
|
+
}
|
|
362
|
+
catch (e) {
|
|
363
|
+
this.deps.logger.debug("old state card delete failed", {
|
|
364
|
+
issue: b.issue_key,
|
|
365
|
+
error: toTaskingError(e).message,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
try {
|
|
370
|
+
const r = await this.deps.feishu.replyCardInThread(b.root_id, this.stateCard(b, state, assignee));
|
|
371
|
+
this.deps.bindings.setBindingCardId(b.issue_id, r.message_id);
|
|
372
|
+
}
|
|
373
|
+
catch (e) {
|
|
374
|
+
this.deps.logger.warn("state card publish failed", {
|
|
375
|
+
issue: b.issue_key,
|
|
376
|
+
error: toTaskingError(e).message,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
deferAction(work) {
|
|
381
|
+
setImmediate(() => {
|
|
382
|
+
void work().catch((e) => this.deps.logger.warn("bridge card action failed", {
|
|
383
|
+
error: toTaskingError(e).message,
|
|
384
|
+
}));
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
truncateCommentForCard(text) {
|
|
388
|
+
const budget = COMMENT_CARD_BUDGET_BYTES - 1024;
|
|
389
|
+
if (Buffer.byteLength(text, "utf8") <= budget)
|
|
390
|
+
return { text, truncated: false };
|
|
391
|
+
let end = text.length;
|
|
392
|
+
while (end > 0 && Buffer.byteLength(text.slice(0, end), "utf8") > budget) {
|
|
393
|
+
end = Math.floor(end * 0.9);
|
|
394
|
+
}
|
|
395
|
+
return { text: text.slice(0, end), truncated: true };
|
|
396
|
+
}
|
|
397
|
+
buildCommentCard(b, comment, isAgent, config, text, truncated) {
|
|
398
|
+
const prefix = isAgent ? "🤖 agent" : `💬 ${comment.authorName ?? "someone"} (Jira)`;
|
|
399
|
+
const body = truncated ? `${text}\n\n…(已截断,点「📄 完整输出」收全文文件)` : text;
|
|
400
|
+
const buttons = [];
|
|
401
|
+
if (truncated) {
|
|
402
|
+
buttons.push({
|
|
403
|
+
label: "📄 完整输出",
|
|
404
|
+
action: "full",
|
|
405
|
+
value: { issue_id: b.issue_id, comment_id: comment.id },
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
buttons.push({
|
|
409
|
+
label: "📋 状态",
|
|
410
|
+
action: "state_summon",
|
|
411
|
+
value: { issue_id: b.issue_id },
|
|
412
|
+
});
|
|
413
|
+
const url = jiraBrowseUrl(config.tracker.endpoint, b.issue_key);
|
|
414
|
+
if (url !== null)
|
|
415
|
+
buttons.push({ label: "打开 Jira", url });
|
|
416
|
+
return buildInfoCard({
|
|
417
|
+
title: `${b.issue_key} · ${prefix}`,
|
|
418
|
+
lines: [body],
|
|
419
|
+
buttons,
|
|
420
|
+
rootId: b.root_id,
|
|
421
|
+
kind: "bridge",
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
async deliverFullComment(issueId, commentId) {
|
|
425
|
+
const tracker = this.jiraTracker;
|
|
426
|
+
if (tracker === null)
|
|
427
|
+
return;
|
|
428
|
+
const b = this.deps.bindings.getBinding(issueId);
|
|
429
|
+
if (b === null)
|
|
430
|
+
return;
|
|
431
|
+
try {
|
|
432
|
+
const comments = await tracker.fetchComments(b.issue_key, null);
|
|
433
|
+
const comment = comments.find((c) => c.id === commentId);
|
|
434
|
+
if (comment === undefined) {
|
|
435
|
+
throw new Error(`comment ${commentId} not found on ${b.issue_key}`);
|
|
436
|
+
}
|
|
437
|
+
const fileName = `jira-${b.issue_key}-comment-${commentId}.md`;
|
|
438
|
+
const key = await this.deps.feishu.uploadFile(Buffer.from(comment.text, "utf8"), fileName);
|
|
439
|
+
await this.deps.feishu.sendFileMessage(b.root_id, key);
|
|
440
|
+
}
|
|
441
|
+
catch (e) {
|
|
442
|
+
this.deps.logger.warn("full comment delivery failed", {
|
|
443
|
+
issue: b.issue_key,
|
|
444
|
+
comment: commentId,
|
|
445
|
+
error: toTaskingError(e).message,
|
|
446
|
+
});
|
|
447
|
+
await this.deps.feishu
|
|
448
|
+
.replyInThread(b.root_id, `⚠️ 完整输出失败:${toTaskingError(e).message}`)
|
|
449
|
+
.catch(() => { });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
claimCardAction(action) {
|
|
453
|
+
const key = `${action.messageId}:${action.operatorOpenId ?? ""}:${JSON.stringify(action.value ?? null)}`;
|
|
454
|
+
const now = Date.now();
|
|
455
|
+
for (const [seen, ts] of this.cardActionSeen) {
|
|
456
|
+
if (now - ts > BRIDGE_CARD_ACTION_DEDUP_MS)
|
|
457
|
+
this.cardActionSeen.delete(seen);
|
|
458
|
+
}
|
|
459
|
+
if (this.cardActionSeen.has(key))
|
|
460
|
+
return false;
|
|
461
|
+
this.cardActionSeen.set(key, now);
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
get isRunning() {
|
|
465
|
+
return this.running;
|
|
466
|
+
}
|
|
467
|
+
start() {
|
|
468
|
+
if (this.running)
|
|
469
|
+
return;
|
|
470
|
+
this.running = true;
|
|
471
|
+
this.stopped = false;
|
|
472
|
+
if (this.deps.scheduler) {
|
|
473
|
+
this.deps.scheduler.on("dispatch", (...args) => {
|
|
474
|
+
const taskId = String(args[0] ?? "");
|
|
475
|
+
void this.onDispatch(taskId).catch((e) => this.deps.logger.error("bridge onDispatch failed", {
|
|
476
|
+
error: toTaskingError(e).message,
|
|
477
|
+
}));
|
|
478
|
+
});
|
|
479
|
+
this.deps.scheduler.on("terminate", (...args) => {
|
|
480
|
+
const taskId = String(args[0] ?? "");
|
|
481
|
+
const reason = String(args[1] ?? "");
|
|
482
|
+
void this.onTerminate(taskId, reason).catch((e) => this.deps.logger.error("bridge onTerminate failed", {
|
|
483
|
+
error: toTaskingError(e).message,
|
|
484
|
+
}));
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
this.deps.logger.info("comment bridge started");
|
|
488
|
+
this.scheduleForward(0);
|
|
489
|
+
this.scheduleSweep(0);
|
|
490
|
+
}
|
|
491
|
+
stop() {
|
|
492
|
+
this.running = false;
|
|
493
|
+
this.stopped = true;
|
|
494
|
+
if (this.forwardTimer) {
|
|
495
|
+
clearTimeout(this.forwardTimer);
|
|
496
|
+
this.forwardTimer = null;
|
|
497
|
+
}
|
|
498
|
+
if (this.sweepTimer) {
|
|
499
|
+
clearTimeout(this.sweepTimer);
|
|
500
|
+
this.sweepTimer = null;
|
|
501
|
+
}
|
|
502
|
+
for (const t of this.retryTimers)
|
|
503
|
+
clearTimeout(t);
|
|
504
|
+
this.retryTimers.clear();
|
|
505
|
+
this.deps.logger.info("comment bridge stopped");
|
|
506
|
+
}
|
|
507
|
+
scheduleForward(delayMs) {
|
|
508
|
+
if (!this.running)
|
|
509
|
+
return;
|
|
510
|
+
this.forwardTimer = setTimeout(() => {
|
|
511
|
+
this.forwardTick()
|
|
512
|
+
.catch((e) => this.deps.logger.error("comment bridge forward tick failed", {
|
|
513
|
+
error: toTaskingError(e).message,
|
|
514
|
+
}))
|
|
515
|
+
.finally(() => {
|
|
516
|
+
if (this.running) {
|
|
517
|
+
this.scheduleForward(this.deps.config().feishu.forward_interval_ms);
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}, delayMs);
|
|
521
|
+
}
|
|
522
|
+
async forwardTick() {
|
|
523
|
+
if (this.jiraTracker === null)
|
|
524
|
+
return;
|
|
525
|
+
const config = this.deps.config();
|
|
526
|
+
if (!config.feishu.enabled)
|
|
527
|
+
return;
|
|
528
|
+
for (const b of this.deps.bindings.listActive()) {
|
|
529
|
+
if (!this.running)
|
|
530
|
+
return;
|
|
531
|
+
await this.forwardComments(b, config);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Sweep (兜底清扫): loss backstop re-pulling bound threads and intake-group
|
|
536
|
+
* messages from the durable cursors. Triggered at start, on event-channel
|
|
537
|
+
* (re)connect, and on the sweep_interval_ms timer — not a processing retry.
|
|
538
|
+
*/
|
|
539
|
+
scheduleSweep(delayMs) {
|
|
540
|
+
if (!this.running)
|
|
541
|
+
return;
|
|
542
|
+
if (this.sweepTimer)
|
|
543
|
+
clearTimeout(this.sweepTimer);
|
|
544
|
+
this.sweepTimer = setTimeout(() => {
|
|
545
|
+
this.sweepTimer = null;
|
|
546
|
+
void this.sweepNow().catch((e) => this.deps.logger.error("comment bridge sweep failed", {
|
|
547
|
+
error: toTaskingError(e).message,
|
|
548
|
+
}));
|
|
549
|
+
}, delayMs);
|
|
550
|
+
}
|
|
551
|
+
async sweepNow() {
|
|
552
|
+
if (this.sweepTimer) {
|
|
553
|
+
clearTimeout(this.sweepTimer);
|
|
554
|
+
this.sweepTimer = null;
|
|
555
|
+
}
|
|
556
|
+
const config = this.deps.config();
|
|
557
|
+
if (config.feishu.enabled && this.running) {
|
|
558
|
+
for (const b of this.deps.bindings.listActive()) {
|
|
559
|
+
if (!this.running)
|
|
560
|
+
return;
|
|
561
|
+
await this.bridgeMessages(b, config);
|
|
562
|
+
}
|
|
563
|
+
if (!this.running)
|
|
564
|
+
return;
|
|
565
|
+
await this.discoverIntake(config);
|
|
566
|
+
if (!this.running)
|
|
567
|
+
return;
|
|
568
|
+
await this.deps.direct?.sweepThreads?.();
|
|
569
|
+
}
|
|
570
|
+
if (this.running) {
|
|
571
|
+
this.scheduleSweep(config.feishu.sweep_interval_ms);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async whenIdle() {
|
|
575
|
+
while (this.consuming || this.queue.length > 0) {
|
|
576
|
+
await new Promise((r) => setImmediate(r));
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
async ingestMessage(msg) {
|
|
580
|
+
const config = this.deps.config();
|
|
581
|
+
if (!config.feishu.enabled)
|
|
582
|
+
return;
|
|
583
|
+
const intakeChat = config.feishu.intake_chat_id ?? "";
|
|
584
|
+
if (msg.chat_id !== "" && intakeChat !== "" && msg.chat_id !== intakeChat) {
|
|
585
|
+
this.deps.logger.debug("ingest dropped: outside intake chat", {
|
|
586
|
+
msg_id: msg.message_id,
|
|
587
|
+
chat_id: msg.chat_id,
|
|
588
|
+
});
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const isIntakeRoot = msg.root_id === null;
|
|
592
|
+
if (this.processed.has(msg.message_id)) {
|
|
593
|
+
await this.whenIdle();
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (isIntakeRoot && this.deps.bindings.isIntakeSeen(msg.message_id)) {
|
|
597
|
+
this.deps.logger.debug("intake root redelivered; suppressed by durable seen-id", {
|
|
598
|
+
msg_id: msg.message_id,
|
|
599
|
+
});
|
|
600
|
+
await this.whenIdle();
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
this.processed.add(msg.message_id);
|
|
604
|
+
await new Promise((resolve) => {
|
|
605
|
+
this.queue.push({ msg, attempt: 0, done: resolve });
|
|
606
|
+
void this.drainQueue();
|
|
607
|
+
});
|
|
608
|
+
if (isIntakeRoot)
|
|
609
|
+
this.deps.bindings.markIntakeSeen(msg.message_id);
|
|
610
|
+
}
|
|
611
|
+
async drainQueue() {
|
|
612
|
+
if (this.consuming)
|
|
613
|
+
return;
|
|
614
|
+
this.consuming = true;
|
|
615
|
+
try {
|
|
616
|
+
while (this.queue.length > 0) {
|
|
617
|
+
const item = this.queue.shift();
|
|
618
|
+
if (!item)
|
|
619
|
+
continue;
|
|
620
|
+
try {
|
|
621
|
+
await this.processItem(item);
|
|
622
|
+
}
|
|
623
|
+
catch (e) {
|
|
624
|
+
this.deps.logger.debug("queue item processing failed", {
|
|
625
|
+
error: toTaskingError(e).message,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
finally {
|
|
629
|
+
item.done?.();
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
finally {
|
|
634
|
+
this.consuming = false;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
async processItem(item) {
|
|
638
|
+
const config = this.deps.config();
|
|
639
|
+
const m = item.msg;
|
|
640
|
+
if (m.root_id === null) {
|
|
641
|
+
await this.processIntakeMessage(m, config);
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
if (this.deps.direct) {
|
|
645
|
+
const handled = await this.deps.direct.handleThreadMessage(m);
|
|
646
|
+
if (handled)
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
const b = this.deps.bindings.getActiveByRootId(m.root_id);
|
|
650
|
+
if (!b) {
|
|
651
|
+
const terminal = this.deps.bindings.getByRootId(m.root_id);
|
|
652
|
+
if (terminal) {
|
|
653
|
+
this.deps.logger.debug("thread message dropped: binding is terminal", {
|
|
654
|
+
msg_id: m.message_id,
|
|
655
|
+
issue: terminal.issue_key,
|
|
656
|
+
});
|
|
657
|
+
if (!this.terminalHintedRoots.has(m.root_id)) {
|
|
658
|
+
this.terminalHintedRoots.add(m.root_id);
|
|
659
|
+
try {
|
|
660
|
+
await this.deps.feishu.replyInThread(m.message_id, `ℹ️ 任务 ${terminal.issue_key} 已完结,此话题已停用。如需继续,请在话题里发 /state 恢复到活跃状态,或重新 /issue 创建新任务。`);
|
|
661
|
+
}
|
|
662
|
+
catch (e) {
|
|
663
|
+
this.deps.logger.debug("terminal hint reply failed", {
|
|
664
|
+
msg_id: m.message_id,
|
|
665
|
+
error: toTaskingError(e).message,
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
this.deps.logger.debug("thread message dropped: no active binding for root", {
|
|
672
|
+
msg_id: m.message_id,
|
|
673
|
+
root_id: m.root_id,
|
|
674
|
+
});
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
try {
|
|
678
|
+
await this.processThreadMessage(m, b, config);
|
|
679
|
+
}
|
|
680
|
+
catch (e) {
|
|
681
|
+
const err = toTaskingError(e);
|
|
682
|
+
if (isJiraNotFound(err)) {
|
|
683
|
+
this.deps.logger.info("binding cleaned: jira issue not found (404)", {
|
|
684
|
+
issue: b.issue_key,
|
|
685
|
+
});
|
|
686
|
+
this.deps.bindings.markTerminal(b.issue_id);
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
const delays = this.deps.retryDelaysMs ?? CommentBridge.DEFAULT_RETRY_DELAYS_MS;
|
|
690
|
+
if (item.attempt < delays.length) {
|
|
691
|
+
const delay = delays[item.attempt] ?? 0;
|
|
692
|
+
this.deps.logger.warn("thread bridge failed; scheduling retry", {
|
|
693
|
+
issue: b.issue_key,
|
|
694
|
+
msg_id: m.message_id,
|
|
695
|
+
attempt: item.attempt + 1,
|
|
696
|
+
delay_ms: delay,
|
|
697
|
+
error: err.message,
|
|
698
|
+
});
|
|
699
|
+
const t = setTimeout(() => {
|
|
700
|
+
this.retryTimers.delete(t);
|
|
701
|
+
this.queue.push({ msg: m, attempt: item.attempt + 1 });
|
|
702
|
+
void this.drainQueue();
|
|
703
|
+
}, delay);
|
|
704
|
+
this.retryTimers.add(t);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
this.deps.logger.warn("thread bridge failed permanently", {
|
|
708
|
+
issue: b.issue_key,
|
|
709
|
+
msg_id: m.message_id,
|
|
710
|
+
error: err.message,
|
|
711
|
+
});
|
|
712
|
+
this.advanceCursor(b, m);
|
|
713
|
+
await this.sendThreadAck(m, THREAD_ACK_FAIL_TEXT);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
async processThreadMessage(m, b, config) {
|
|
717
|
+
if (m.message_id === b.root_id)
|
|
718
|
+
return;
|
|
719
|
+
if (!isReadableText(m.msg_type) || m.sender_type === "app" || isCommand(m.text)) {
|
|
720
|
+
if (isCommand(m.text) && m.sender_type !== "app") {
|
|
721
|
+
await this.handleInThreadCommand(m.text, b, config);
|
|
722
|
+
}
|
|
723
|
+
this.advanceCursor(b, m);
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
if (m.text.trim() === "") {
|
|
727
|
+
this.deps.logger.debug("thread message dropped: empty text", { msg_id: m.message_id });
|
|
728
|
+
this.advanceCursor(b, m);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
const tracker = this.jiraTracker;
|
|
732
|
+
if (tracker === null)
|
|
733
|
+
return;
|
|
734
|
+
const body = buildBridgedCommentBody(m.sender_name ?? "user", m.text);
|
|
735
|
+
await tracker.postComment(b.issue_key, body);
|
|
736
|
+
this.advanceCursor(b, m);
|
|
737
|
+
await this.sendThreadAck(m, THREAD_ACK_TEXT.replace("{issue_key}", b.issue_key));
|
|
738
|
+
}
|
|
739
|
+
advanceCursor(b, m) {
|
|
740
|
+
const cur = this.deps.bindings.getBinding(b.issue_id)?.last_feishu_msg_id;
|
|
741
|
+
const curSec = toSecondsStr(cur ?? "");
|
|
742
|
+
if (curSec !== "" && Number(m.create_time) <= Number(curSec))
|
|
743
|
+
return;
|
|
744
|
+
this.deps.bindings.advanceFeishuCursor(b.issue_id, m.create_time);
|
|
745
|
+
}
|
|
746
|
+
async processIntakeMessage(m, config) {
|
|
747
|
+
if (m.root_id !== null)
|
|
748
|
+
return;
|
|
749
|
+
if (!isReadableText(m.msg_type) || m.sender_type === "app") {
|
|
750
|
+
this.deps.logger.debug("intake message dropped: not readable text or app sender", {
|
|
751
|
+
msg_id: m.message_id,
|
|
752
|
+
msg_type: m.msg_type,
|
|
753
|
+
sender_type: m.sender_type,
|
|
754
|
+
});
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
if (m.text.trim() === "") {
|
|
758
|
+
this.deps.logger.debug("intake message dropped: empty text", { msg_id: m.message_id });
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
try {
|
|
762
|
+
if (isHelp(m.text)) {
|
|
763
|
+
await this.replyFlatCard(m.message_id, this.helpCard("intake"), this.helpText("intake"));
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
if (parseState(m.text)) {
|
|
767
|
+
if (this.jiraTracker === null) {
|
|
768
|
+
await this.replyUnknownCommand(m.message_id);
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
await this.deps.feishu.replyFlat(m.message_id, "/state 是话题命令,请在 issue 话题内发哦~");
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
if (parseChat(m.text)) {
|
|
775
|
+
if (this.deps.direct) {
|
|
776
|
+
const handled = await this.deps.direct.handleIntakeMessage(m);
|
|
777
|
+
if (!handled) {
|
|
778
|
+
await this.replyUnknownCommand(m.message_id);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
else {
|
|
782
|
+
await this.replyUnknownCommand(m.message_id);
|
|
783
|
+
}
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
const agentCmd = parseAgent(m.text);
|
|
787
|
+
if (agentCmd) {
|
|
788
|
+
await this.executeAgent(agentCmd, null, m.message_id, config);
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
const parsed = parseIssue(m.text);
|
|
792
|
+
if (!parsed) {
|
|
793
|
+
if (isCommand(m.text)) {
|
|
794
|
+
await this.replyUnknownCommand(m.message_id);
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
await this.deps.feishu.replyFlat(m.message_id, intakeAckTextFor(this.jiraTracker !== null));
|
|
798
|
+
}
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
if (this.deps.bindings.hasRootId(m.message_id))
|
|
802
|
+
return;
|
|
803
|
+
if (this.jiraTracker === null) {
|
|
804
|
+
await this.replyUnknownCommand(m.message_id);
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
await this.executeIssue(parsed, m.message_id, config);
|
|
808
|
+
}
|
|
809
|
+
catch (e) {
|
|
810
|
+
const err = toTaskingError(e);
|
|
811
|
+
const recalled = err.category === "feishu_api_status" &&
|
|
812
|
+
err.details?.status === 400;
|
|
813
|
+
if (recalled) {
|
|
814
|
+
this.deps.logger.debug("intake message skipped (likely recalled)", {
|
|
815
|
+
msg_id: m.message_id,
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
this.deps.logger.warn("intake message skipped", {
|
|
820
|
+
msg_id: m.message_id,
|
|
821
|
+
error: err.message,
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
isAgentAuthor(authorName, config) {
|
|
827
|
+
if (authorName === null)
|
|
828
|
+
return false;
|
|
829
|
+
const apiUser = config.tracker.api_user;
|
|
830
|
+
return apiUser !== null && authorName === apiUser;
|
|
831
|
+
}
|
|
832
|
+
async forwardComments(b, config) {
|
|
833
|
+
const tracker = this.jiraTracker;
|
|
834
|
+
if (tracker === null)
|
|
835
|
+
return;
|
|
836
|
+
let comments;
|
|
837
|
+
try {
|
|
838
|
+
comments = await tracker.fetchComments(b.issue_key, b.last_jira_comment_id);
|
|
839
|
+
}
|
|
840
|
+
catch (e) {
|
|
841
|
+
const err = toTaskingError(e);
|
|
842
|
+
if (isJiraNotFound(err)) {
|
|
843
|
+
this.deps.logger.info("binding cleaned: jira issue not found (404)", {
|
|
844
|
+
issue: b.issue_key,
|
|
845
|
+
});
|
|
846
|
+
this.deps.bindings.markTerminal(b.issue_id);
|
|
847
|
+
await this.patchStateCard(b, "已完结", null);
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
this.deps.logger.debug(`forwardComments fetch failed: ${err.message}`, {
|
|
851
|
+
issue: b.issue_key,
|
|
852
|
+
});
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
const newComments = comments.filter((c) => !hasSentinel(c.text));
|
|
856
|
+
if (newComments.length > 0) {
|
|
857
|
+
this.deps.logger.info(`jira → feishu: ${newComments.length} comment(s) from ${b.issue_key}`, {});
|
|
858
|
+
}
|
|
859
|
+
for (const c of comments) {
|
|
860
|
+
if (hasSentinel(c.text)) {
|
|
861
|
+
this.deps.bindings.advanceJiraCursor(b.issue_id, c.id);
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
const isAgent = this.isAgentAuthor(c.authorName, config);
|
|
865
|
+
const rendered = renderForwardedComment(c.authorName, c.text, isAgent);
|
|
866
|
+
const { text: mirrorText, truncated } = this.truncateCommentForCard(c.text);
|
|
867
|
+
const card = this.buildCommentCard(b, c, isAgent, config, mirrorText, truncated);
|
|
868
|
+
try {
|
|
869
|
+
const r = await this.sendCommentCard(b.root_id, card, rendered);
|
|
870
|
+
if (r.thread_id && r.thread_id !== b.thread_id) {
|
|
871
|
+
this.deps.bindings.upsertBinding({
|
|
872
|
+
issue_id: b.issue_id,
|
|
873
|
+
issue_key: b.issue_key,
|
|
874
|
+
chat_id: b.chat_id,
|
|
875
|
+
root_id: b.root_id,
|
|
876
|
+
thread_id: r.thread_id,
|
|
877
|
+
});
|
|
878
|
+
b.thread_id = r.thread_id;
|
|
879
|
+
}
|
|
880
|
+
this.deps.bindings.advanceJiraCursor(b.issue_id, c.id);
|
|
881
|
+
}
|
|
882
|
+
catch (e) {
|
|
883
|
+
const err = toTaskingError(e);
|
|
884
|
+
if (err.category === "feishu_cooldown") {
|
|
885
|
+
this.deps.logger.debug("forward skipped: feishu cooling down", {
|
|
886
|
+
issue: b.issue_key,
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
else {
|
|
890
|
+
this.deps.logger.warn("forward reply failed; cursor held for retry", {
|
|
891
|
+
issue: b.issue_key,
|
|
892
|
+
error: err.message,
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
async bridgeMessages(b, config) {
|
|
899
|
+
if (!b.thread_id) {
|
|
900
|
+
this.deps.logger.debug("sweep skipped: no thread_id yet", {
|
|
901
|
+
issue: b.issue_key,
|
|
902
|
+
});
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
const since = toSecondsStr(b.last_feishu_msg_id ?? "");
|
|
906
|
+
let messages;
|
|
907
|
+
try {
|
|
908
|
+
messages = await this.deps.feishu.listThreadReplies(b.thread_id, b.last_feishu_msg_id);
|
|
909
|
+
}
|
|
910
|
+
catch (e) {
|
|
911
|
+
this.deps.logger.debug(`sweep thread fetch failed: ${toTaskingError(e).message}`, {
|
|
912
|
+
issue: b.issue_key,
|
|
913
|
+
});
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
let newCount = 0;
|
|
917
|
+
for (const m of messages) {
|
|
918
|
+
if (since !== "" && Number(m.create_time) <= Number(since))
|
|
919
|
+
continue;
|
|
920
|
+
if (m.root_id === null)
|
|
921
|
+
continue;
|
|
922
|
+
if (m.message_id === b.root_id)
|
|
923
|
+
continue;
|
|
924
|
+
newCount++;
|
|
925
|
+
await this.ingestMessage(m);
|
|
926
|
+
}
|
|
927
|
+
if (newCount > 0) {
|
|
928
|
+
this.deps.logger.info(`feishu → jira: ${newCount} msg(s) from ${b.issue_key} topic`, {});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
async sendThreadAck(m, text) {
|
|
932
|
+
try {
|
|
933
|
+
await this.deps.feishu.replyInThread(m.message_id, text);
|
|
934
|
+
}
|
|
935
|
+
catch (e) {
|
|
936
|
+
this.deps.logger.warn("thread ack reply failed", {
|
|
937
|
+
msg_id: m.message_id,
|
|
938
|
+
error: toTaskingError(e).message,
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
helpCard(context) {
|
|
943
|
+
return buildInfoCard({ title: "📖 tasking 命令", lines: this.helpLines(context) });
|
|
944
|
+
}
|
|
945
|
+
helpLines(context) {
|
|
946
|
+
return context === "thread"
|
|
947
|
+
? threadHelpLinesFor(this.jiraTracker !== null)
|
|
948
|
+
: helpLinesFor(this.jiraTracker !== null);
|
|
949
|
+
}
|
|
950
|
+
helpText(context) {
|
|
951
|
+
return context === "thread"
|
|
952
|
+
? threadHelpTextFor(this.jiraTracker !== null)
|
|
953
|
+
: helpTextFor(this.jiraTracker !== null);
|
|
954
|
+
}
|
|
955
|
+
unknownCommandText() {
|
|
956
|
+
return `未知命令。可用:${intakeCommandsFor(this.jiraTracker !== null).join("、")}`;
|
|
957
|
+
}
|
|
958
|
+
async replyUnknownCommand(msgId) {
|
|
959
|
+
await this.deps.feishu.replyFlat(msgId, this.unknownCommandText());
|
|
960
|
+
}
|
|
961
|
+
async replyFlatCard(rootId, card, fallbackText) {
|
|
962
|
+
try {
|
|
963
|
+
await this.deps.feishu.replyCardFlat(rootId, card);
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
catch (e) {
|
|
967
|
+
this.deps.logger.warn("bridge flat card failed; falling back to text", {
|
|
968
|
+
root_id: rootId,
|
|
969
|
+
error: toTaskingError(e).message,
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
await this.deps.feishu.replyFlat(rootId, fallbackText);
|
|
973
|
+
}
|
|
974
|
+
async sendCommentCard(rootId, card, fallbackText) {
|
|
975
|
+
try {
|
|
976
|
+
return await this.deps.feishu.replyCardInThread(rootId, card);
|
|
977
|
+
}
|
|
978
|
+
catch (e) {
|
|
979
|
+
this.deps.logger.warn("comment card failed; falling back to text", {
|
|
980
|
+
root_id: rootId,
|
|
981
|
+
error: toTaskingError(e).message,
|
|
982
|
+
});
|
|
983
|
+
return this.deps.feishu.replyInThread(rootId, fallbackText);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
async replyThreadCard(rootId, card, fallbackText) {
|
|
987
|
+
try {
|
|
988
|
+
await this.deps.feishu.replyCardInThread(rootId, card);
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
catch (e) {
|
|
992
|
+
this.deps.logger.warn("bridge thread card failed; falling back to text", {
|
|
993
|
+
root_id: rootId,
|
|
994
|
+
error: toTaskingError(e).message,
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
await this.deps.feishu.replyFlat(rootId, fallbackText);
|
|
998
|
+
}
|
|
999
|
+
async handleInThreadCommand(text, b, config) {
|
|
1000
|
+
if (isHelp(text)) {
|
|
1001
|
+
await this.replyThreadCard(b.root_id, this.helpCard("thread"), this.helpText("thread"));
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
if (text.trim().startsWith("/issue")) {
|
|
1005
|
+
await this.deps.feishu.replyFlat(b.root_id, "/issue 是群聊命令,请在群里发哦~");
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
if (parseChat(text)) {
|
|
1009
|
+
await this.deps.feishu.replyFlat(b.root_id, "/chat 是群聊命令,请在群里发哦~");
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
const agent = parseAgent(text);
|
|
1013
|
+
if (agent) {
|
|
1014
|
+
await this.executeAgent(agent, b, b.root_id, config);
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
const state = parseState(text);
|
|
1018
|
+
if (state) {
|
|
1019
|
+
await this.executeState(state, b, config);
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
await this.deps.feishu.replyFlat(b.root_id, `未知命令。话题内可用:${threadCommandsFor(this.jiraTracker !== null).join("、")}`);
|
|
1023
|
+
}
|
|
1024
|
+
async discoverIntake(config) {
|
|
1025
|
+
const intakeChat = config.feishu.intake_chat_id;
|
|
1026
|
+
if (!intakeChat)
|
|
1027
|
+
return;
|
|
1028
|
+
const since = toSecondsStr(this.deps.bindings.getMeta("intake_cursor") ?? "");
|
|
1029
|
+
let messages;
|
|
1030
|
+
if (since === "") {
|
|
1031
|
+
await this.probeIntakeCursor(intakeChat);
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
try {
|
|
1035
|
+
messages = await this.deps.feishu.listMessages(intakeChat, since);
|
|
1036
|
+
}
|
|
1037
|
+
catch (e) {
|
|
1038
|
+
this.deps.logger.debug(`sweep intake fetch failed: ${toTaskingError(e).message}`);
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
if (this.stopped)
|
|
1042
|
+
return;
|
|
1043
|
+
let maxTime = since;
|
|
1044
|
+
let newCount = 0;
|
|
1045
|
+
for (const m of messages) {
|
|
1046
|
+
if (m.root_id !== null)
|
|
1047
|
+
continue;
|
|
1048
|
+
if (!isReadableText(m.msg_type) || m.sender_type === "app")
|
|
1049
|
+
continue;
|
|
1050
|
+
if (m.text.trim() === "")
|
|
1051
|
+
continue;
|
|
1052
|
+
if (since !== "" && Number(m.create_time) <= Number(since))
|
|
1053
|
+
continue;
|
|
1054
|
+
newCount++;
|
|
1055
|
+
if (m.create_time > maxTime)
|
|
1056
|
+
maxTime = m.create_time;
|
|
1057
|
+
await this.ingestMessage(m);
|
|
1058
|
+
}
|
|
1059
|
+
if (newCount > 0) {
|
|
1060
|
+
this.deps.logger.info(`feishu intake: ${newCount} new message(s) in ${intakeChat}`, {});
|
|
1061
|
+
}
|
|
1062
|
+
if (maxTime !== since) {
|
|
1063
|
+
this.deps.bindings.setMeta("intake_cursor", maxTime);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
async probeIntakeCursor(intakeChat) {
|
|
1067
|
+
const nowSec = String(Math.floor(Date.now() / 1000));
|
|
1068
|
+
const windowStart = String(Math.max(0, Number(nowSec) - 120));
|
|
1069
|
+
let messages;
|
|
1070
|
+
try {
|
|
1071
|
+
messages = await this.deps.feishu.listMessages(intakeChat, windowStart);
|
|
1072
|
+
}
|
|
1073
|
+
catch (e) {
|
|
1074
|
+
this.deps.logger.debug(`intake cursor probe failed: ${toTaskingError(e).message}`);
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
let maxTime = windowStart;
|
|
1078
|
+
for (const m of messages) {
|
|
1079
|
+
if (m.create_time > maxTime)
|
|
1080
|
+
maxTime = m.create_time;
|
|
1081
|
+
}
|
|
1082
|
+
this.deps.bindings.setMeta("intake_cursor", maxTime);
|
|
1083
|
+
this.deps.logger.info("intake cursor initialized via cold-start probe", {
|
|
1084
|
+
cursor: maxTime,
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
async freshStartReset() {
|
|
1088
|
+
const config = this.deps.config();
|
|
1089
|
+
this.deps.bindings.deleteMeta("intake_cursor");
|
|
1090
|
+
const active = this.deps.bindings.listActive();
|
|
1091
|
+
if (active.length === 0)
|
|
1092
|
+
return true;
|
|
1093
|
+
const tracker = this.jiraTracker;
|
|
1094
|
+
if (tracker === null)
|
|
1095
|
+
return true;
|
|
1096
|
+
let refreshed;
|
|
1097
|
+
try {
|
|
1098
|
+
refreshed = await tracker.fetchIssueStatesByIds(active.map((b) => b.issue_id));
|
|
1099
|
+
}
|
|
1100
|
+
catch (e) {
|
|
1101
|
+
this.deps.logger.warn("fresh-start reconcile skipped (tracker unreachable), keeping bindings", {
|
|
1102
|
+
error: toTaskingError(e).message,
|
|
1103
|
+
});
|
|
1104
|
+
return false;
|
|
1105
|
+
}
|
|
1106
|
+
const statesById = new Map(refreshed.map((t) => [t.id, t.state]));
|
|
1107
|
+
for (const b of active) {
|
|
1108
|
+
const state = statesById.get(b.issue_id);
|
|
1109
|
+
if (state === undefined || isTerminalState(state, config.tracker.terminal_states)) {
|
|
1110
|
+
this.deps.bindings.markTerminal(b.issue_id);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
return true;
|
|
1114
|
+
}
|
|
1115
|
+
async executeIssue(parsed, msgId, config) {
|
|
1116
|
+
const tracker = this.jiraTracker;
|
|
1117
|
+
if (tracker === null) {
|
|
1118
|
+
await this.replyUnknownCommand(msgId);
|
|
1119
|
+
return;
|
|
1120
|
+
}
|
|
1121
|
+
const intakeChat = config.feishu.intake_chat_id;
|
|
1122
|
+
if (!intakeChat || !config.feishu.create_issue_type)
|
|
1123
|
+
return;
|
|
1124
|
+
if (parsed.kind === "discover") {
|
|
1125
|
+
let types = [];
|
|
1126
|
+
try {
|
|
1127
|
+
types = await tracker.fetchIssueTypes();
|
|
1128
|
+
}
|
|
1129
|
+
catch {
|
|
1130
|
+
types = [];
|
|
1131
|
+
}
|
|
1132
|
+
const list = types.length > 0 ? types.map((t, i) => `${i + 1}) ${t}`).join("、") : "(无)";
|
|
1133
|
+
const fallback = `可用 issueType: ${list}\n默认: ${config.feishu.create_issue_type}`;
|
|
1134
|
+
await this.replyFlatCard(msgId, buildInfoCard({
|
|
1135
|
+
title: "🗂 Issue Types",
|
|
1136
|
+
lines: [`可用:${list}`, `默认:${config.feishu.create_issue_type}`],
|
|
1137
|
+
}), fallback);
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
let type = parsed.typeOverride ?? config.feishu.create_issue_type;
|
|
1141
|
+
if (parsed.typeOverride !== null && /^\d+$/.test(parsed.typeOverride)) {
|
|
1142
|
+
try {
|
|
1143
|
+
const types = await tracker.fetchIssueTypes();
|
|
1144
|
+
const idx = Number(parsed.typeOverride) - 1;
|
|
1145
|
+
const picked = types[idx];
|
|
1146
|
+
if (picked) {
|
|
1147
|
+
type = picked;
|
|
1148
|
+
}
|
|
1149
|
+
else {
|
|
1150
|
+
await this.deps.feishu.replyFlat(msgId, `无此 issueType 编号: ${parsed.typeOverride}`);
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
catch { }
|
|
1155
|
+
}
|
|
1156
|
+
const labels = [...config.tracker.required_labels];
|
|
1157
|
+
if (parsed.agentOverride) {
|
|
1158
|
+
labels.push(`agent:${parsed.agentOverride.toLowerCase()}`);
|
|
1159
|
+
}
|
|
1160
|
+
const fields = {
|
|
1161
|
+
project: { key: config.tracker.project_key },
|
|
1162
|
+
summary: parsed.title,
|
|
1163
|
+
description: wrapDescription(parsed.description, tracker.isApiV3()),
|
|
1164
|
+
issuetype: { name: type },
|
|
1165
|
+
labels,
|
|
1166
|
+
};
|
|
1167
|
+
if (config.tracker.assignee) {
|
|
1168
|
+
try {
|
|
1169
|
+
const isCloud = tracker.isApiV3();
|
|
1170
|
+
if (config.tracker.assignee === "me") {
|
|
1171
|
+
const me = await tracker.fetchMyself();
|
|
1172
|
+
if (isCloud && me.accountId) {
|
|
1173
|
+
fields.assignee = { accountId: me.accountId };
|
|
1174
|
+
}
|
|
1175
|
+
else if (me.name) {
|
|
1176
|
+
fields.assignee = { name: me.name };
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
else if (isCloud) {
|
|
1180
|
+
fields.assignee = { accountId: config.tracker.assignee };
|
|
1181
|
+
}
|
|
1182
|
+
else {
|
|
1183
|
+
fields.assignee = { name: config.tracker.assignee };
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
catch {
|
|
1187
|
+
if (config.tracker.assignee !== "me") {
|
|
1188
|
+
fields.assignee = tracker.isApiV3()
|
|
1189
|
+
? { accountId: config.tracker.assignee }
|
|
1190
|
+
: { name: config.tracker.assignee };
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
let created;
|
|
1195
|
+
try {
|
|
1196
|
+
created = await tracker.createIssue(fields);
|
|
1197
|
+
}
|
|
1198
|
+
catch (e) {
|
|
1199
|
+
await this.deps.feishu.replyFlat(msgId, `创建失败: ${toTaskingError(e).message}`);
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
let ackText = `✅ 已创建 ${created.key}`;
|
|
1203
|
+
if (parsed.agentOverride) {
|
|
1204
|
+
const name = parsed.agentOverride;
|
|
1205
|
+
const warned = config.opencode.agents.length > 0 &&
|
|
1206
|
+
!config.opencode.agents.some((a) => a.toLowerCase() === name.toLowerCase());
|
|
1207
|
+
if (warned) {
|
|
1208
|
+
ackText += `\n⚠️ agent ${name} 不在配置目录 [${config.opencode.agents.join(", ")}] 中,派发时将回退默认`;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
const ack = await this.deps.feishu.replyInThread(msgId, ackText);
|
|
1212
|
+
this.deps.bindings.upsertBinding({
|
|
1213
|
+
issue_id: created.id,
|
|
1214
|
+
issue_key: created.key,
|
|
1215
|
+
chat_id: intakeChat,
|
|
1216
|
+
root_id: ack.root_id ?? msgId,
|
|
1217
|
+
thread_id: ack.thread_id,
|
|
1218
|
+
});
|
|
1219
|
+
await this.ensureActiveState(created.id, created.key, config);
|
|
1220
|
+
const binding = this.deps.bindings.getBinding(created.id);
|
|
1221
|
+
if (binding !== null) {
|
|
1222
|
+
const info = await this.fetchState(created.id);
|
|
1223
|
+
await this.publishStateCard(binding, info.state, info.assignee);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
async ensureActiveState(issueId, issueKey, config) {
|
|
1227
|
+
const tracker = this.jiraTracker;
|
|
1228
|
+
if (tracker === null)
|
|
1229
|
+
return;
|
|
1230
|
+
try {
|
|
1231
|
+
const states = await tracker.fetchIssueStatesByIds([issueId]);
|
|
1232
|
+
const cur = states[0]?.state ?? "";
|
|
1233
|
+
const isActive = config.tracker.active_states.some((s) => s.toLowerCase() === cur.toLowerCase());
|
|
1234
|
+
if (isActive || cur === "")
|
|
1235
|
+
return;
|
|
1236
|
+
const transitions = await tracker.fetchTransitions(issueKey);
|
|
1237
|
+
const target = transitions.find((t) => config.tracker.active_states.some((s) => s.toLowerCase() === t.toName.toLowerCase()));
|
|
1238
|
+
if (target) {
|
|
1239
|
+
await tracker.transitionIssue(issueKey, target.id);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
catch (e) {
|
|
1243
|
+
this.deps.logger.debug("ensure active state failed", {
|
|
1244
|
+
issue: issueKey,
|
|
1245
|
+
error: toTaskingError(e).message,
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
async executeState(parsed, b, config) {
|
|
1250
|
+
const tracker = this.jiraTracker;
|
|
1251
|
+
if (tracker === null) {
|
|
1252
|
+
await this.deps.feishu.replyFlat(b.root_id, "⚠️ /state 需要 Jira 任务源(当前未配置)");
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
let transitions;
|
|
1256
|
+
try {
|
|
1257
|
+
transitions = await tracker.fetchTransitions(b.issue_key);
|
|
1258
|
+
}
|
|
1259
|
+
catch (e) {
|
|
1260
|
+
await this.deps.feishu.replyFlat(b.root_id, `查询失败: ${toTaskingError(e).message}`);
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
if (parsed.kind === "list" || parsed.target === null) {
|
|
1264
|
+
const info = await this.fetchState(b.issue_id);
|
|
1265
|
+
await this.publishStateCard(b, info.state, info.assignee);
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
const target = parsed.target;
|
|
1269
|
+
let chosen = null;
|
|
1270
|
+
if (/^\d+$/.test(target)) {
|
|
1271
|
+
const idx = Number(target) - 1;
|
|
1272
|
+
chosen = transitions[idx] ?? null;
|
|
1273
|
+
}
|
|
1274
|
+
else {
|
|
1275
|
+
chosen = transitions.find((t) => t.toName.toLowerCase() === target.toLowerCase()) ?? null;
|
|
1276
|
+
}
|
|
1277
|
+
if (!chosen) {
|
|
1278
|
+
const lines = transitions.map((t, i) => `${i + 1}) ${t.toName}`);
|
|
1279
|
+
await this.deps.feishu.replyFlat(b.root_id, `未匹配,可用:\n${lines.join("\n")}`);
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
try {
|
|
1283
|
+
await tracker.transitionIssue(b.issue_key, chosen.id);
|
|
1284
|
+
}
|
|
1285
|
+
catch (e) {
|
|
1286
|
+
await this.deps.feishu.replyFlat(b.root_id, `转状态失败: ${toTaskingError(e).message}`);
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
this.deps.requestRefresh?.();
|
|
1290
|
+
const after = await this.fetchState(b.issue_id);
|
|
1291
|
+
await this.patchStateCard(b, after.state === "" ? chosen.toName : after.state, after.assignee);
|
|
1292
|
+
const chosenTransition = chosen;
|
|
1293
|
+
const isTerminal = config.tracker.terminal_states.some((s) => s.toLowerCase() === chosenTransition.toName.toLowerCase());
|
|
1294
|
+
if (isTerminal) {
|
|
1295
|
+
this.deps.bindings.markTerminal(b.issue_id);
|
|
1296
|
+
await this.deps.feishu.replyFlat(b.root_id, `✅ 已转 ${chosenTransition.toName}(完结)`);
|
|
1297
|
+
}
|
|
1298
|
+
else {
|
|
1299
|
+
await this.deps.feishu.replyFlat(b.root_id, `✅ 已转 ${chosenTransition.toName}`);
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
async executeAgent(parsed, b, replyTarget, config) {
|
|
1303
|
+
if (parsed.kind === "list") {
|
|
1304
|
+
const lines = await this.renderAgentList(b, config);
|
|
1305
|
+
await this.replyFlatCard(replyTarget, this.agentListCard(b, config, lines), lines.join("\n"));
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
if (b === null) {
|
|
1309
|
+
await this.deps.feishu.replyFlat(replyTarget, "/agent <name> 需在任务话题内使用哦~");
|
|
1310
|
+
return;
|
|
1311
|
+
}
|
|
1312
|
+
await this.executeAgentSwitch(parsed, b, replyTarget, config);
|
|
1313
|
+
}
|
|
1314
|
+
agentListCard(b, config, lines) {
|
|
1315
|
+
const buttons = [];
|
|
1316
|
+
if (b !== null) {
|
|
1317
|
+
for (const name of config.opencode.agents.slice(0, 8)) {
|
|
1318
|
+
buttons.push({
|
|
1319
|
+
label: name,
|
|
1320
|
+
action: "agent_set",
|
|
1321
|
+
value: { issue_id: b.issue_id, agent: name },
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
buttons.push({
|
|
1325
|
+
label: "↩️ 默认",
|
|
1326
|
+
action: "agent_set",
|
|
1327
|
+
value: { issue_id: b.issue_id, agent: "" },
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
return buildInfoCard({
|
|
1331
|
+
title: "🧠 Agent",
|
|
1332
|
+
lines,
|
|
1333
|
+
buttons,
|
|
1334
|
+
...(b === null ? {} : { rootId: b.root_id, kind: "bridge" }),
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
async renderAgentList(b, config) {
|
|
1338
|
+
const lines = [`默认 agent: ${config.opencode.agent}`];
|
|
1339
|
+
if (config.opencode.agents.length > 0) {
|
|
1340
|
+
lines.push(`目录: ${config.opencode.agents.join(", ")}`);
|
|
1341
|
+
}
|
|
1342
|
+
else {
|
|
1343
|
+
lines.push("目录: 未配置(任意 agent:<name> 标签直通)");
|
|
1344
|
+
}
|
|
1345
|
+
const tracker = this.jiraTracker;
|
|
1346
|
+
if (b !== null && tracker !== null) {
|
|
1347
|
+
try {
|
|
1348
|
+
const refreshed = await tracker.fetchIssueStatesByIds([b.issue_id]);
|
|
1349
|
+
const task = refreshed.find((t) => t.id === b.issue_id);
|
|
1350
|
+
if (task) {
|
|
1351
|
+
const resolved = resolveAgent(task, config);
|
|
1352
|
+
lines.push(`当前 agent: ${resolved.agent}${resolved.warned ? translate("(未在目录中,已回退默认)") : ""}`);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
catch (e) {
|
|
1356
|
+
this.deps.logger.debug("agent list current fetch failed", {
|
|
1357
|
+
issue: b.issue_key,
|
|
1358
|
+
error: toTaskingError(e).message,
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return lines;
|
|
1363
|
+
}
|
|
1364
|
+
async executeAgentSwitch(parsed, b, replyTarget, config) {
|
|
1365
|
+
const tracker = this.jiraTracker;
|
|
1366
|
+
if (tracker === null) {
|
|
1367
|
+
await this.deps.feishu.replyFlat(replyTarget, "⚠️ /agent 切换需要 Jira 任务源(当前未配置)");
|
|
1368
|
+
return;
|
|
1369
|
+
}
|
|
1370
|
+
let labels;
|
|
1371
|
+
try {
|
|
1372
|
+
const refreshed = await tracker.fetchIssueStatesByIds([b.issue_id]);
|
|
1373
|
+
const task = refreshed.find((t) => t.id === b.issue_id);
|
|
1374
|
+
labels = task?.labels ?? [];
|
|
1375
|
+
}
|
|
1376
|
+
catch (e) {
|
|
1377
|
+
await this.deps.feishu.replyFlat(replyTarget, `查询失败: ${toTaskingError(e).message}`);
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
const next = labels.filter((l) => !l.startsWith("agent:"));
|
|
1381
|
+
let ack;
|
|
1382
|
+
if (parsed.kind === "reset") {
|
|
1383
|
+
ack = `✅ 已清除 agent 标签,回退默认 ${config.opencode.agent}`;
|
|
1384
|
+
}
|
|
1385
|
+
else {
|
|
1386
|
+
const name = parsed.name ?? "";
|
|
1387
|
+
const label = `agent:${name.toLowerCase()}`;
|
|
1388
|
+
next.push(label);
|
|
1389
|
+
const warned = config.opencode.agents.length > 0 &&
|
|
1390
|
+
!config.opencode.agents.some((a) => a.toLowerCase() === name.toLowerCase());
|
|
1391
|
+
ack = warned
|
|
1392
|
+
? `✅ 已切 ${label}(下一 turn 生效)\n⚠️ ${name} 不在配置目录中,派发时将回退默认`
|
|
1393
|
+
: `✅ 已切 ${label}(下一 turn 生效)`;
|
|
1394
|
+
}
|
|
1395
|
+
try {
|
|
1396
|
+
await tracker.updateIssue(b.issue_key, { labels: next });
|
|
1397
|
+
}
|
|
1398
|
+
catch (e) {
|
|
1399
|
+
await this.deps.feishu.replyFlat(replyTarget, `切换失败: ${toTaskingError(e).message}`);
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
this.deps.requestRefresh?.();
|
|
1403
|
+
await this.deps.feishu.replyFlat(replyTarget, ack);
|
|
1404
|
+
}
|
|
1405
|
+
async onDispatch(taskId) {
|
|
1406
|
+
const config = this.deps.config();
|
|
1407
|
+
if (!config.feishu.enabled)
|
|
1408
|
+
return;
|
|
1409
|
+
const existing = this.deps.bindings.getBinding(taskId);
|
|
1410
|
+
if (existing) {
|
|
1411
|
+
if (existing.status === "terminal") {
|
|
1412
|
+
this.deps.bindings.markActive(taskId);
|
|
1413
|
+
}
|
|
1414
|
+
return;
|
|
1415
|
+
}
|
|
1416
|
+
const entry = this.deps.scheduler?.state.running.get(taskId);
|
|
1417
|
+
const issueKey = entry?.task.identifier ?? taskId;
|
|
1418
|
+
const intakeChat = config.feishu.intake_chat_id;
|
|
1419
|
+
if (!intakeChat)
|
|
1420
|
+
return;
|
|
1421
|
+
const title = entry?.task.title ?? issueKey;
|
|
1422
|
+
let rootId = "";
|
|
1423
|
+
try {
|
|
1424
|
+
rootId = await this.deps.feishu.postMessage(intakeChat, `📋 ${issueKey} · ${title} 已开始`);
|
|
1425
|
+
}
|
|
1426
|
+
catch (e) {
|
|
1427
|
+
this.deps.logger.warn("onDispatch postMessage failed", {
|
|
1428
|
+
issue: issueKey,
|
|
1429
|
+
error: toTaskingError(e).message,
|
|
1430
|
+
});
|
|
1431
|
+
return;
|
|
1432
|
+
}
|
|
1433
|
+
let threadId = null;
|
|
1434
|
+
try {
|
|
1435
|
+
const r = await this.deps.feishu.replyInThread(rootId, "💬 可在此话题回复");
|
|
1436
|
+
threadId = r.thread_id;
|
|
1437
|
+
}
|
|
1438
|
+
catch (e) {
|
|
1439
|
+
this.deps.logger.warn("onDispatch replyInThread failed", {
|
|
1440
|
+
issue: issueKey,
|
|
1441
|
+
error: toTaskingError(e).message,
|
|
1442
|
+
});
|
|
1443
|
+
}
|
|
1444
|
+
this.deps.bindings.upsertBinding({
|
|
1445
|
+
issue_id: taskId,
|
|
1446
|
+
issue_key: issueKey,
|
|
1447
|
+
chat_id: intakeChat,
|
|
1448
|
+
root_id: rootId,
|
|
1449
|
+
thread_id: threadId,
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
async onTerminate(taskId, reason) {
|
|
1453
|
+
const config = this.deps.config();
|
|
1454
|
+
if (!config.feishu.enabled)
|
|
1455
|
+
return;
|
|
1456
|
+
if (reason !== "terminal")
|
|
1457
|
+
return;
|
|
1458
|
+
const b = this.deps.bindings.getBinding(taskId);
|
|
1459
|
+
if (!b || b.status === "terminal")
|
|
1460
|
+
return;
|
|
1461
|
+
try {
|
|
1462
|
+
await this.deps.feishu.replyFlat(b.root_id, `✅ 已完结 ${b.issue_key}`);
|
|
1463
|
+
}
|
|
1464
|
+
catch (e) {
|
|
1465
|
+
this.deps.logger.warn("onTerminate ack failed", {
|
|
1466
|
+
issue: b.issue_key,
|
|
1467
|
+
error: toTaskingError(e).message,
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
this.deps.bindings.markTerminal(taskId);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
//# sourceMappingURL=comment-bridge.js.map
|