@tothemax/tasking 0.6.18 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -6
- package/README.zh-CN.md +14 -6
- package/assets/workflows/WORKFLOW.code-mr.en.md.example +7 -7
- package/assets/workflows/WORKFLOW.code-mr.md.example +7 -7
- package/assets/workflows/WORKFLOW.feishu-direct.en.md.example +7 -7
- package/assets/workflows/WORKFLOW.feishu-direct.md.example +7 -7
- package/assets/workflows/WORKFLOW.grill-openspec.en.md.example +7 -7
- package/assets/workflows/WORKFLOW.grill-openspec.md.example +7 -7
- package/assets/workflows/WORKFLOW.minimal.en.md.example +7 -7
- package/assets/workflows/WORKFLOW.minimal.md.example +7 -7
- package/dist/bridge/comment-bridge.d.ts +4 -0
- package/dist/bridge/comment-bridge.d.ts.map +1 -1
- package/dist/bridge/comment-bridge.js +25 -0
- package/dist/bridge/comment-bridge.js.map +1 -1
- package/dist/cli/daemon.d.ts.map +1 -1
- package/dist/cli/daemon.js +5 -1
- package/dist/cli/daemon.js.map +1 -1
- package/dist/cli/doctor.d.ts +4 -0
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +51 -17
- package/dist/cli/doctor.js.map +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +213 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/config/config-types.d.ts +7 -1
- package/dist/config/config-types.d.ts.map +1 -1
- package/dist/config/config-types.js +4 -1
- package/dist/config/config-types.js.map +1 -1
- package/dist/config/resolver.d.ts.map +1 -1
- package/dist/config/resolver.js +3 -0
- package/dist/config/resolver.js.map +1 -1
- package/dist/direct/card.d.ts +18 -0
- package/dist/direct/card.d.ts.map +1 -1
- package/dist/direct/card.js +81 -5
- package/dist/direct/card.js.map +1 -1
- package/dist/direct/driver.d.ts +36 -0
- package/dist/direct/driver.d.ts.map +1 -0
- package/dist/direct/driver.js +77 -0
- package/dist/direct/driver.js.map +1 -0
- package/dist/direct/manager.d.ts +20 -3
- package/dist/direct/manager.d.ts.map +1 -1
- package/dist/direct/manager.js +438 -69
- package/dist/direct/manager.js.map +1 -1
- package/dist/driver/api-client.d.ts +33 -0
- package/dist/driver/api-client.d.ts.map +1 -0
- package/dist/driver/api-client.js +69 -0
- package/dist/driver/api-client.js.map +1 -0
- package/dist/driver/approval.d.ts +32 -0
- package/dist/driver/approval.d.ts.map +1 -0
- package/dist/driver/approval.js +106 -0
- package/dist/driver/approval.js.map +1 -0
- package/dist/driver/session-ops.d.ts +68 -0
- package/dist/driver/session-ops.d.ts.map +1 -0
- package/dist/driver/session-ops.js +156 -0
- package/dist/driver/session-ops.js.map +1 -0
- package/dist/driver/sweep.d.ts +16 -0
- package/dist/driver/sweep.d.ts.map +1 -0
- package/dist/driver/sweep.js +84 -0
- package/dist/driver/sweep.js.map +1 -0
- package/dist/driver/turn-driver.d.ts +83 -0
- package/dist/driver/turn-driver.d.ts.map +1 -0
- package/dist/driver/turn-driver.js +449 -0
- package/dist/driver/turn-driver.js.map +1 -0
- package/dist/mcp/inject.d.ts +1 -0
- package/dist/mcp/inject.d.ts.map +1 -1
- package/dist/mcp/inject.js +23 -11
- package/dist/mcp/inject.js.map +1 -1
- package/dist/observability/http-dashboard.d.ts +2 -0
- package/dist/observability/http-dashboard.d.ts.map +1 -1
- package/dist/observability/http-dashboard.js +124 -98
- package/dist/observability/http-dashboard.js.map +1 -1
- package/dist/observability/snapshot.d.ts +1 -0
- package/dist/observability/snapshot.d.ts.map +1 -1
- package/dist/observability/snapshot.js +8 -0
- package/dist/observability/snapshot.js.map +1 -1
- package/dist/observability/terminal-dashboard.d.ts +9 -0
- package/dist/observability/terminal-dashboard.d.ts.map +1 -1
- package/dist/observability/terminal-dashboard.js +119 -6
- package/dist/observability/terminal-dashboard.js.map +1 -1
- package/dist/scheduler/scheduler.d.ts +18 -1
- package/dist/scheduler/scheduler.d.ts.map +1 -1
- package/dist/scheduler/scheduler.js +108 -7
- package/dist/scheduler/scheduler.js.map +1 -1
- package/dist/shared/errors.d.ts +1 -1
- package/dist/shared/errors.d.ts.map +1 -1
- package/dist/shared/errors.js.map +1 -1
- package/dist/shared/opencode-version.d.ts +25 -0
- package/dist/shared/opencode-version.d.ts.map +1 -0
- package/dist/shared/opencode-version.js +88 -0
- package/dist/shared/opencode-version.js.map +1 -0
- package/dist/shared/redact.d.ts +2 -0
- package/dist/shared/redact.d.ts.map +1 -0
- package/dist/shared/redact.js +8 -0
- package/dist/shared/redact.js.map +1 -0
- package/dist/shared/types.d.ts +6 -0
- package/dist/shared/types.d.ts.map +1 -1
- package/dist/shared/types.js.map +1 -1
- package/dist/translate/dict.d.ts.map +1 -1
- package/dist/translate/dict.js +85 -3
- package/dist/translate/dict.js.map +1 -1
- package/dist/worker/prompt.d.ts +2 -0
- package/dist/worker/prompt.d.ts.map +1 -1
- package/dist/worker/prompt.js +10 -0
- package/dist/worker/prompt.js.map +1 -1
- package/dist/worker/runner.d.ts +8 -40
- package/dist/worker/runner.d.ts.map +1 -1
- package/dist/worker/runner.js +95 -282
- package/dist/worker/runner.js.map +1 -1
- package/dist/workspace/manager.d.ts +6 -2
- package/dist/workspace/manager.d.ts.map +1 -1
- package/dist/workspace/manager.js +14 -13
- package/dist/workspace/manager.js.map +1 -1
- package/package.json +9 -3
- package/dist/direct/command.d.ts +0 -12
- package/dist/direct/command.d.ts.map +0 -1
- package/dist/direct/command.js +0 -23
- package/dist/direct/command.js.map +0 -1
package/dist/direct/manager.js
CHANGED
|
@@ -2,13 +2,19 @@ import { existsSync, mkdirSync, realpathSync, rmSync, statSync, writeFileSync }
|
|
|
2
2
|
import { basename, extname, join } from "node:path";
|
|
3
3
|
import { toSecondsStr, } from "../adapters/feishu/source.js";
|
|
4
4
|
import { parseChat } from "../bridge/commands.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
5
|
+
import { readTaskingVersion } from "../cli/version.js";
|
|
6
|
+
import { defaultApiExec, describeApiCommand } from "../driver/api-client.js";
|
|
7
|
+
import { ApprovalCoordinator } from "../driver/approval.js";
|
|
8
|
+
import { ALLOW_ALL_RULES, replyPermission, setSessionPermissions } from "../driver/session-ops.js";
|
|
9
|
+
import { TaskingError, toTaskingError } from "../shared/errors.js";
|
|
10
|
+
import { redactSecrets } from "../shared/redact.js";
|
|
11
|
+
import { CARD_BUDGET_BYTES, buildApprovalCard, buildCompletionCard, buildInfoCard, buildTurnCard, formatTokens, isFailed, toolSummary, } from "./card.js";
|
|
12
|
+
import { createDefaultTurnDriver } from "./driver.js";
|
|
9
13
|
const MAX_MESSAGE_CHARS = 3500;
|
|
10
14
|
const TTL_SWEEP_INTERVAL_MS = 6 * 60 * 60 * 1000;
|
|
11
15
|
const CARD_UPDATE_INTERVAL_MS = 1500;
|
|
16
|
+
const CARD_RETRY_DELAY_MS = 2000;
|
|
17
|
+
const CARD_RETRY_JITTER_MS = 1000;
|
|
12
18
|
const CARD_ACTION_DEDUP_MS = 2 * 60 * 1000;
|
|
13
19
|
const MEMBER_CACHE_TTL_MS = 60 * 60 * 1000;
|
|
14
20
|
const CARD_HEARTBEAT_MS = 5000;
|
|
@@ -20,7 +26,14 @@ import { translate } from "../translate/locale.js";
|
|
|
20
26
|
export function isDirectEnabled(config) {
|
|
21
27
|
return config.feishu.enabled && config.feishu.direct.enabled;
|
|
22
28
|
}
|
|
23
|
-
const
|
|
29
|
+
const defaultDriverFor = (exec) => createDefaultTurnDriver(exec);
|
|
30
|
+
function approvalCardResolution(state, decision) {
|
|
31
|
+
if (state === "replied" &&
|
|
32
|
+
(decision === "once" || decision === "always" || decision === "reject")) {
|
|
33
|
+
return { state: "replied", decision };
|
|
34
|
+
}
|
|
35
|
+
return { state: state };
|
|
36
|
+
}
|
|
24
37
|
export class DirectSessionManager {
|
|
25
38
|
deps;
|
|
26
39
|
runtime = new Map();
|
|
@@ -29,6 +42,9 @@ export class DirectSessionManager {
|
|
|
29
42
|
cardActionSeen = new Map();
|
|
30
43
|
memberCache = new Map();
|
|
31
44
|
pendingWork = 0;
|
|
45
|
+
approvalRoots = new Map();
|
|
46
|
+
approvalCards = new Map();
|
|
47
|
+
approvals;
|
|
32
48
|
dirSlots = new Map();
|
|
33
49
|
running = false;
|
|
34
50
|
halted = false;
|
|
@@ -37,6 +53,21 @@ export class DirectSessionManager {
|
|
|
37
53
|
waiters = [];
|
|
38
54
|
constructor(deps) {
|
|
39
55
|
this.deps = deps;
|
|
56
|
+
const exec = () => deps.apiExec ?? defaultApiExec();
|
|
57
|
+
this.approvals = new ApprovalCoordinator({
|
|
58
|
+
replyPermission: async (sessionId, requestID, decision) => {
|
|
59
|
+
const cfg = deps.config();
|
|
60
|
+
await replyPermission(exec(), { command: cfg.opencode.command, serverUrl: cfg.opencode.server_url }, sessionId, requestID, decision);
|
|
61
|
+
},
|
|
62
|
+
onCardRequested: (request) => {
|
|
63
|
+
void this.sendApprovalCard(request);
|
|
64
|
+
},
|
|
65
|
+
onCardResolved: (requestID, state, decision) => {
|
|
66
|
+
if (state === "pending")
|
|
67
|
+
return;
|
|
68
|
+
void this.stampApprovalCard(requestID);
|
|
69
|
+
},
|
|
70
|
+
});
|
|
40
71
|
}
|
|
41
72
|
start() {
|
|
42
73
|
if (this.running)
|
|
@@ -86,8 +117,13 @@ export class DirectSessionManager {
|
|
|
86
117
|
if (this.pendingWork > 0)
|
|
87
118
|
return true;
|
|
88
119
|
for (const rt of this.runtime.values()) {
|
|
89
|
-
if (rt.turnInFlight ||
|
|
120
|
+
if (rt.turnInFlight ||
|
|
121
|
+
rt.queue.length > 0 ||
|
|
122
|
+
rt.flushTimer !== null ||
|
|
123
|
+
rt.flushInFlight ||
|
|
124
|
+
rt.flushQueued) {
|
|
90
125
|
return true;
|
|
126
|
+
}
|
|
91
127
|
}
|
|
92
128
|
return false;
|
|
93
129
|
}
|
|
@@ -122,8 +158,11 @@ export class DirectSessionManager {
|
|
|
122
158
|
if (name === "done" || name === "done_confirm" || name === "cancel") {
|
|
123
159
|
return this.handleLifecycleAction(name, session, rt, action);
|
|
124
160
|
}
|
|
161
|
+
if (name === "approve_once" || name === "approve_always" || name === "approve_reject") {
|
|
162
|
+
return await this.handleApprovalAction(name, value);
|
|
163
|
+
}
|
|
125
164
|
if (name === "perm" || name === "perm_on" || name === "perm_once" || name === "perm_cancel") {
|
|
126
|
-
return this.handlePermissionAction(name, session, rt);
|
|
165
|
+
return await this.handlePermissionAction(name, session, rt);
|
|
127
166
|
}
|
|
128
167
|
if (name.startsWith("auto_") || name.startsWith("collect_")) {
|
|
129
168
|
return this.handleInfoAction(name, session, rt, action);
|
|
@@ -162,7 +201,154 @@ export class DirectSessionManager {
|
|
|
162
201
|
}
|
|
163
202
|
return { toast: { type: "warning", content: "未知操作" } };
|
|
164
203
|
}
|
|
165
|
-
|
|
204
|
+
turnNow(rootId) {
|
|
205
|
+
const session = this.deps.store.getDirectSession(rootId);
|
|
206
|
+
return (session?.turns ?? 0) + 1;
|
|
207
|
+
}
|
|
208
|
+
async handlePendingPermissions(rootId, pending) {
|
|
209
|
+
const bySession = new Map();
|
|
210
|
+
for (const p of pending) {
|
|
211
|
+
this.approvalRoots.set(p.sessionID, rootId);
|
|
212
|
+
const list = bySession.get(p.sessionID) ?? [];
|
|
213
|
+
list.push(p);
|
|
214
|
+
bySession.set(p.sessionID, list);
|
|
215
|
+
}
|
|
216
|
+
for (const [sessionId, items] of bySession) {
|
|
217
|
+
this.approvals.ingest(sessionId, items);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
async sendApprovalCard(request) {
|
|
221
|
+
const rootId = this.sessionRootByApproval(request);
|
|
222
|
+
if (rootId === null)
|
|
223
|
+
return;
|
|
224
|
+
const card = buildApprovalCard({
|
|
225
|
+
rootId,
|
|
226
|
+
turn: this.turnNow(rootId),
|
|
227
|
+
requestID: request.requestID,
|
|
228
|
+
action: request.action,
|
|
229
|
+
resources: request.resources,
|
|
230
|
+
save: request.save,
|
|
231
|
+
kind: "direct",
|
|
232
|
+
});
|
|
233
|
+
const replyCard = this.deps.feishu.replyCardInThread;
|
|
234
|
+
if (!replyCard) {
|
|
235
|
+
await this.replySafe(rootId, `🔐 等待审批:${request.action} ${redactSecrets(request.resources.join("、"))}(当前客户端不支持卡片,30 分钟未处理将自动拒绝)`);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const res = await replyCard.call(this.deps.feishu, rootId, card);
|
|
240
|
+
if (res.message_id) {
|
|
241
|
+
this.approvalCards.set(request.requestID, { messageId: res.message_id, rootId, request });
|
|
242
|
+
const state = this.approvals.state(request.requestID);
|
|
243
|
+
if (state !== undefined && state !== "pending") {
|
|
244
|
+
void this.stampApprovalCard(request.requestID);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
this.deps.logger.warn("approval card send failed", {
|
|
250
|
+
request_id: request.requestID,
|
|
251
|
+
error: toTaskingError(e).message,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
sessionRootByApproval(request) {
|
|
256
|
+
const recorded = this.approvalRoots.get(request.sessionId);
|
|
257
|
+
if (recorded !== undefined)
|
|
258
|
+
return recorded;
|
|
259
|
+
for (const rootId of this.runtime.keys()) {
|
|
260
|
+
const session = this.deps.store.getDirectSession(rootId);
|
|
261
|
+
if (session?.session_id === request.sessionId)
|
|
262
|
+
return rootId;
|
|
263
|
+
}
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
async stampApprovalCard(requestID) {
|
|
267
|
+
const entry = this.approvalCards.get(requestID);
|
|
268
|
+
const resolution = this.approvals.resolutionOf(requestID);
|
|
269
|
+
if (!entry || !resolution || resolution.state === "pending")
|
|
270
|
+
return;
|
|
271
|
+
const update = this.deps.feishu.updateCard;
|
|
272
|
+
if (!update) {
|
|
273
|
+
this.approvalCards.delete(requestID);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const card = buildApprovalCard({
|
|
277
|
+
rootId: entry.rootId,
|
|
278
|
+
turn: this.turnNow(entry.rootId),
|
|
279
|
+
requestID,
|
|
280
|
+
action: entry.request.action,
|
|
281
|
+
resources: entry.request.resources,
|
|
282
|
+
save: entry.request.save,
|
|
283
|
+
}, approvalCardResolution(resolution.state, resolution.decision));
|
|
284
|
+
try {
|
|
285
|
+
await update.call(this.deps.feishu, entry.messageId, card);
|
|
286
|
+
this.approvalCards.delete(requestID);
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
this.deps.logger.warn("approval card stamp failed", {
|
|
290
|
+
request_id: requestID,
|
|
291
|
+
error: toTaskingError(e).message,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async handleApprovalAction(name, value) {
|
|
296
|
+
const requestID = typeof value.request_id === "string" ? value.request_id : null;
|
|
297
|
+
if (!requestID) {
|
|
298
|
+
return { toast: { type: "error", content: "审批单标识缺失,无法处理" } };
|
|
299
|
+
}
|
|
300
|
+
const decision = name === "approve_once" ? "once" : name === "approve_always" ? "always" : "reject";
|
|
301
|
+
const outcome = await this.approvals.reply(requestID, decision);
|
|
302
|
+
const toast = outcome === "replied"
|
|
303
|
+
? { type: "success", content: decision === "reject" ? "已拒绝" : "已允许" }
|
|
304
|
+
: outcome === "already"
|
|
305
|
+
? { type: "info", content: "该审批已处理" }
|
|
306
|
+
: outcome === "unknown"
|
|
307
|
+
? { type: "warning", content: "审批单不存在或已过期" }
|
|
308
|
+
: { type: "error", content: "审批提交失败,请重试" };
|
|
309
|
+
const card = outcome === "replied" || outcome === "already"
|
|
310
|
+
? this.stampedApprovalCard(requestID)
|
|
311
|
+
: undefined;
|
|
312
|
+
return { toast, ...(card !== undefined ? { card } : {}) };
|
|
313
|
+
}
|
|
314
|
+
stampedApprovalCard(requestID) {
|
|
315
|
+
const request = this.approvals.requestOf(requestID);
|
|
316
|
+
if (!request)
|
|
317
|
+
return undefined;
|
|
318
|
+
const resolution = this.approvals.resolutionOf(requestID);
|
|
319
|
+
if (!resolution || resolution.state === "pending")
|
|
320
|
+
return undefined;
|
|
321
|
+
const rootId = this.approvalRoots.get(request.sessionId) ?? "";
|
|
322
|
+
return buildApprovalCard({
|
|
323
|
+
rootId,
|
|
324
|
+
turn: this.turnNow(rootId),
|
|
325
|
+
requestID,
|
|
326
|
+
action: request.action,
|
|
327
|
+
resources: request.resources,
|
|
328
|
+
...(request.save ? { save: request.save } : {}),
|
|
329
|
+
kind: "direct",
|
|
330
|
+
}, approvalCardResolution(resolution.state, resolution.decision));
|
|
331
|
+
}
|
|
332
|
+
async applySessionPosture(session, rt, open) {
|
|
333
|
+
const sessionId = session.session_id;
|
|
334
|
+
if (!sessionId) {
|
|
335
|
+
rt.postureOpen = open;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const cfg = this.deps.config();
|
|
339
|
+
try {
|
|
340
|
+
await setSessionPermissions(this.deps.apiExec ?? defaultApiExec(), { command: cfg.opencode.command, serverUrl: cfg.opencode.server_url }, sessionId, open ? ALLOW_ALL_RULES : null);
|
|
341
|
+
rt.postureOpen = open;
|
|
342
|
+
}
|
|
343
|
+
catch (e) {
|
|
344
|
+
this.deps.logger.warn("session posture update failed", {
|
|
345
|
+
root_id: session.root_id,
|
|
346
|
+
open,
|
|
347
|
+
error: toTaskingError(e).message,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
async handlePermissionAction(name, session, rt) {
|
|
166
352
|
const card = (confirm) => {
|
|
167
353
|
const built = this.permissionCard(session, rt, confirm);
|
|
168
354
|
return built === null ? {} : { card: built };
|
|
@@ -176,6 +362,7 @@ export class DirectSessionManager {
|
|
|
176
362
|
rt.autoOnce = false;
|
|
177
363
|
rt.turnUsedOnce = false;
|
|
178
364
|
this.deps.store.updateDirectSession(session.root_id, { auto_approve_override: null });
|
|
365
|
+
await this.applySessionPosture(fresh, rt, false);
|
|
179
366
|
return { toast: { type: "success", content: "已收回权限放开" }, ...card(false) };
|
|
180
367
|
}
|
|
181
368
|
return { toast: { type: "warning", content: "请确认是否放开权限" }, ...card(true) };
|
|
@@ -185,8 +372,9 @@ export class DirectSessionManager {
|
|
|
185
372
|
return { toast: { type: "success", content: "已设置为仅下一轮放开" }, ...card(false) };
|
|
186
373
|
}
|
|
187
374
|
this.deps.store.updateDirectSession(session.root_id, { auto_approve_override: "on" });
|
|
375
|
+
await this.applySessionPosture(fresh, rt, true);
|
|
188
376
|
return {
|
|
189
|
-
toast: { type: "success", content: "
|
|
377
|
+
toast: { type: "success", content: "已放开本会话权限(立即生效)" },
|
|
190
378
|
...card(false),
|
|
191
379
|
};
|
|
192
380
|
}
|
|
@@ -197,6 +385,14 @@ export class DirectSessionManager {
|
|
|
197
385
|
const state = {
|
|
198
386
|
...rt.turn,
|
|
199
387
|
autoPosture: this.turnAutoPosture(fresh, rt),
|
|
388
|
+
...(rt.waitingApproval
|
|
389
|
+
? {
|
|
390
|
+
waitingRemainingMs: rt.waitingSince !== null
|
|
391
|
+
? Math.max(0, this.deps.config().opencode.permission_timeout_ms -
|
|
392
|
+
(Date.now() - rt.waitingSince))
|
|
393
|
+
: null,
|
|
394
|
+
}
|
|
395
|
+
: {}),
|
|
200
396
|
agentOverride: fresh.agent_override,
|
|
201
397
|
};
|
|
202
398
|
if (confirm)
|
|
@@ -315,7 +511,7 @@ export class DirectSessionManager {
|
|
|
315
511
|
? translate("🔓 本会话放开")
|
|
316
512
|
: current === "once"
|
|
317
513
|
? translate("⚡ 仅下一轮放开")
|
|
318
|
-
: translate("🔒
|
|
514
|
+
: translate("🔒 默认(跟随项目配置)");
|
|
319
515
|
return buildInfoCard({
|
|
320
516
|
title: "🔐 权限姿态",
|
|
321
517
|
rootId: session.root_id,
|
|
@@ -666,9 +862,34 @@ export class DirectSessionManager {
|
|
|
666
862
|
this.enqueueTurn(m.root_id, prompt);
|
|
667
863
|
return true;
|
|
668
864
|
}
|
|
865
|
+
async deleteSessionByRoot(rootId, operator) {
|
|
866
|
+
const session = this.deps.store.getDirectSession(rootId);
|
|
867
|
+
if (!session)
|
|
868
|
+
return false;
|
|
869
|
+
if (session.status === "done" || session.status === "expired")
|
|
870
|
+
return true;
|
|
871
|
+
await this.executeDone(session, operator ?? "TUI");
|
|
872
|
+
return true;
|
|
873
|
+
}
|
|
669
874
|
async recover() {
|
|
670
875
|
const running = this.deps.store.listDirectSessionsByStatus("running");
|
|
671
876
|
for (const s of running) {
|
|
877
|
+
if (s.session_id && this.deps.interruptSession) {
|
|
878
|
+
try {
|
|
879
|
+
await this.deps.interruptSession(s.session_id);
|
|
880
|
+
this.deps.logger.info("direct session interrupted on restart", {
|
|
881
|
+
root_id: s.root_id,
|
|
882
|
+
session_id: s.session_id,
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
catch (e) {
|
|
886
|
+
this.deps.logger.warn("direct session interrupt on restart failed", {
|
|
887
|
+
root_id: s.root_id,
|
|
888
|
+
session_id: s.session_id,
|
|
889
|
+
error: toTaskingError(e).message,
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
}
|
|
672
893
|
this.deps.store.markDirectSessionStatus(s.root_id, "idle");
|
|
673
894
|
await this.finalizeStoredCard(s, "interrupted", "idle", true, "daemon 重启,上一轮已中断;工具明细未保留,可点「重试」重跑上一轮。");
|
|
674
895
|
await this.replySafe(s.root_id, "⚠️ 上次运行被中断(daemon 重启),请重发消息继续;若当时是第一轮对话,此前内容不会保留。");
|
|
@@ -731,6 +952,14 @@ export class DirectSessionManager {
|
|
|
731
952
|
input_tokens: s.input_tokens,
|
|
732
953
|
output_tokens: s.output_tokens,
|
|
733
954
|
total_tokens: s.total_tokens,
|
|
955
|
+
...(rt?.waitingApproval
|
|
956
|
+
? {
|
|
957
|
+
waiting_approval: true,
|
|
958
|
+
waiting_remaining_ms: rt.waitingSince !== null
|
|
959
|
+
? Math.max(0, config.opencode.permission_timeout_ms - (Date.now() - rt.waitingSince))
|
|
960
|
+
: null,
|
|
961
|
+
}
|
|
962
|
+
: {}),
|
|
734
963
|
};
|
|
735
964
|
});
|
|
736
965
|
}
|
|
@@ -867,14 +1096,23 @@ export class DirectSessionManager {
|
|
|
867
1096
|
flushedTools: 0,
|
|
868
1097
|
autoOnce: false,
|
|
869
1098
|
turnUsedOnce: false,
|
|
1099
|
+
postureOpen: false,
|
|
1100
|
+
appliedAgent: null,
|
|
1101
|
+
appliedModel: null,
|
|
1102
|
+
contextTokens: 0,
|
|
1103
|
+
waitingApproval: false,
|
|
1104
|
+
waitingSince: null,
|
|
1105
|
+
seenMessages: new Map(),
|
|
870
1106
|
heartbeatTimer: null,
|
|
871
1107
|
collect: null,
|
|
872
1108
|
collectTimer: null,
|
|
873
1109
|
fullDetail: [],
|
|
874
1110
|
fullDetailBytes: 0,
|
|
875
1111
|
fullDetailDropped: 0,
|
|
876
|
-
cardUpdateFailures: 0,
|
|
877
1112
|
flushTimer: null,
|
|
1113
|
+
flushInFlight: false,
|
|
1114
|
+
flushQueued: false,
|
|
1115
|
+
flushPromise: null,
|
|
878
1116
|
};
|
|
879
1117
|
this.runtime.set(rootId, rt);
|
|
880
1118
|
}
|
|
@@ -949,12 +1187,20 @@ export class DirectSessionManager {
|
|
|
949
1187
|
}
|
|
950
1188
|
async startTurnCard(session, rt, busy, retryFromTurn) {
|
|
951
1189
|
const fresh = this.deps.store.getDirectSession(session.root_id) ?? session;
|
|
1190
|
+
await rt.flushPromise?.catch(() => { });
|
|
1191
|
+
const prev = rt.turn;
|
|
1192
|
+
if (prev !== null && prev.tools.length > rt.flushedTools) {
|
|
1193
|
+
const pending = prev.tools.slice(rt.flushedTools);
|
|
1194
|
+
rt.flushedTools = prev.tools.length;
|
|
1195
|
+
await this.replySafe(prev.rootId, truncate(toolTextLines(pending), 2000));
|
|
1196
|
+
}
|
|
952
1197
|
const state = {
|
|
953
1198
|
rootId: session.root_id,
|
|
954
1199
|
sessionName: fresh.name ?? sessionName(fresh.path),
|
|
955
1200
|
sessionStatus: busy ? "queued" : fresh.status,
|
|
956
1201
|
turns: fresh.turns,
|
|
957
1202
|
totalTokens: fresh.total_tokens,
|
|
1203
|
+
contextTokens: rt.contextTokens,
|
|
958
1204
|
queued: rt.queue.length,
|
|
959
1205
|
turn: fresh.turns + 1,
|
|
960
1206
|
phase: "running",
|
|
@@ -970,12 +1216,15 @@ export class DirectSessionManager {
|
|
|
970
1216
|
rt.turnUsedOnce = false;
|
|
971
1217
|
rt.turnStartedAt = 0;
|
|
972
1218
|
rt.flushedTools = 0;
|
|
1219
|
+
rt.waitingApproval = false;
|
|
1220
|
+
rt.waitingSince = null;
|
|
973
1221
|
rt.fullDetail = [];
|
|
974
1222
|
rt.fullDetailBytes = 0;
|
|
975
1223
|
rt.fullDetailDropped = 0;
|
|
976
1224
|
const detailed = this.deps.config().feishu.direct.verbosity === "detailed";
|
|
977
1225
|
const replyCard = this.deps.feishu.replyCardInThread;
|
|
978
|
-
|
|
1226
|
+
rt.cardsAvailable = true;
|
|
1227
|
+
if (!detailed || !replyCard) {
|
|
979
1228
|
rt.cardsAvailable = false;
|
|
980
1229
|
return;
|
|
981
1230
|
}
|
|
@@ -1001,12 +1250,16 @@ export class DirectSessionManager {
|
|
|
1001
1250
|
return;
|
|
1002
1251
|
const updateCard = this.deps.feishu.updateCard;
|
|
1003
1252
|
const cardId = rt.cardId;
|
|
1253
|
+
const turn = rt.turn;
|
|
1254
|
+
const renderedTools = turn.tools.length;
|
|
1004
1255
|
const sealed = { ...rt.turn, sealed: true };
|
|
1005
1256
|
rt.cardId = null;
|
|
1006
1257
|
if (!updateCard)
|
|
1007
1258
|
return;
|
|
1008
1259
|
try {
|
|
1009
1260
|
await updateCard.call(this.deps.feishu, cardId, buildTurnCard(sealed));
|
|
1261
|
+
if (rt.turn === turn)
|
|
1262
|
+
rt.flushedTools = Math.max(rt.flushedTools, renderedTools);
|
|
1010
1263
|
}
|
|
1011
1264
|
catch (e) {
|
|
1012
1265
|
this.deps.logger.warn("direct card seal failed", {
|
|
@@ -1042,6 +1295,29 @@ export class DirectSessionManager {
|
|
|
1042
1295
|
}));
|
|
1043
1296
|
}
|
|
1044
1297
|
async flushTurnCard(rootId) {
|
|
1298
|
+
const rt = this.runtimeFor(rootId);
|
|
1299
|
+
if (rt.turn === null)
|
|
1300
|
+
return;
|
|
1301
|
+
if (rt.flushInFlight) {
|
|
1302
|
+
rt.flushQueued = true;
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
rt.flushInFlight = true;
|
|
1306
|
+
const flight = this.flushTurnCardInner(rootId);
|
|
1307
|
+
rt.flushPromise = flight;
|
|
1308
|
+
try {
|
|
1309
|
+
await flight;
|
|
1310
|
+
}
|
|
1311
|
+
finally {
|
|
1312
|
+
rt.flushPromise = null;
|
|
1313
|
+
rt.flushInFlight = false;
|
|
1314
|
+
if (rt.flushQueued) {
|
|
1315
|
+
rt.flushQueued = false;
|
|
1316
|
+
this.flushSoon(rootId);
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
async flushTurnCardInner(rootId) {
|
|
1045
1321
|
const rt = this.runtimeFor(rootId);
|
|
1046
1322
|
const turn = rt.turn;
|
|
1047
1323
|
if (!turn)
|
|
@@ -1055,10 +1331,16 @@ export class DirectSessionManager {
|
|
|
1055
1331
|
turn.sessionName = fresh.name ?? sessionName(fresh.path);
|
|
1056
1332
|
turn.autoPosture = this.turnAutoPosture(fresh, rt);
|
|
1057
1333
|
turn.agentOverride = fresh.agent_override;
|
|
1334
|
+
turn.waitingRemainingMs = rt.waitingApproval
|
|
1335
|
+
? rt.waitingSince !== null
|
|
1336
|
+
? Math.max(0, this.deps.config().opencode.permission_timeout_ms - (Date.now() - rt.waitingSince))
|
|
1337
|
+
: null
|
|
1338
|
+
: undefined;
|
|
1058
1339
|
if (rt.turnStartedAt > 0) {
|
|
1059
1340
|
turn.sessionStatus = fresh.status;
|
|
1060
1341
|
turn.turns = fresh.turns;
|
|
1061
1342
|
turn.totalTokens = fresh.total_tokens;
|
|
1343
|
+
turn.contextTokens = rt.contextTokens;
|
|
1062
1344
|
}
|
|
1063
1345
|
}
|
|
1064
1346
|
turn.queued = rt.queue.length;
|
|
@@ -1067,47 +1349,54 @@ export class DirectSessionManager {
|
|
|
1067
1349
|
}
|
|
1068
1350
|
const updateCard = this.deps.feishu.updateCard;
|
|
1069
1351
|
if (rt.cardsAvailable && rt.cardId !== null && updateCard) {
|
|
1352
|
+
const renderedTools = turn.tools.length;
|
|
1353
|
+
const applySuccess = () => {
|
|
1354
|
+
if (rt.turn === turn)
|
|
1355
|
+
rt.flushedTools = Math.max(rt.flushedTools, renderedTools);
|
|
1356
|
+
};
|
|
1070
1357
|
try {
|
|
1071
1358
|
await updateCard.call(this.deps.feishu, rt.cardId, buildTurnCard(turn));
|
|
1072
|
-
|
|
1359
|
+
applySuccess();
|
|
1073
1360
|
return;
|
|
1074
1361
|
}
|
|
1075
1362
|
catch (e) {
|
|
1076
|
-
rt.cardUpdateFailures++;
|
|
1077
1363
|
this.deps.logger.warn("direct card update failed", {
|
|
1078
1364
|
root_id: rootId,
|
|
1079
1365
|
error: toTaskingError(e).message,
|
|
1080
1366
|
});
|
|
1081
|
-
if (
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1367
|
+
if (!isRetryableCardError(e)) {
|
|
1368
|
+
rt.cardsAvailable = false;
|
|
1369
|
+
}
|
|
1370
|
+
else {
|
|
1371
|
+
const base = this.deps.cardRetryDelayMs ?? CARD_RETRY_DELAY_MS;
|
|
1372
|
+
const jitter = base <= 0 ? 0 : Math.floor(Math.random() * CARD_RETRY_JITTER_MS);
|
|
1373
|
+
await sleepUnrefed(base + jitter);
|
|
1374
|
+
if (rt.turn === turn && rt.cardId !== null) {
|
|
1375
|
+
try {
|
|
1376
|
+
await updateCard.call(this.deps.feishu, rt.cardId, buildTurnCard(turn));
|
|
1377
|
+
applySuccess();
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
catch (retryError) {
|
|
1381
|
+
this.deps.logger.warn("direct card update retry failed; falling back to text", {
|
|
1382
|
+
root_id: rootId,
|
|
1383
|
+
error: toTaskingError(retryError).message,
|
|
1384
|
+
});
|
|
1385
|
+
rt.cardsAvailable = false;
|
|
1386
|
+
}
|
|
1093
1387
|
}
|
|
1094
1388
|
}
|
|
1095
|
-
rt.cardsAvailable = false;
|
|
1096
|
-
rt.cardId = null;
|
|
1097
1389
|
}
|
|
1098
1390
|
}
|
|
1391
|
+
if (rt.turn !== turn)
|
|
1392
|
+
return;
|
|
1099
1393
|
if (this.deps.config().feishu.direct.verbosity !== "detailed")
|
|
1100
1394
|
return;
|
|
1101
1395
|
const pending = turn.tools.slice(rt.flushedTools);
|
|
1102
1396
|
rt.flushedTools = turn.tools.length;
|
|
1103
1397
|
if (pending.length === 0)
|
|
1104
1398
|
return;
|
|
1105
|
-
|
|
1106
|
-
const summary = toolSummary(t);
|
|
1107
|
-
const exit = t.exit !== undefined && t.exit !== 0 ? ` (exit ${t.exit})` : "";
|
|
1108
|
-
return `🔧 ${t.tool}${summary === "" ? "" : ` · ${summary}`}${exit}`;
|
|
1109
|
-
});
|
|
1110
|
-
await this.replySafe(rootId, truncate(lines.join("\n"), 2000));
|
|
1399
|
+
await this.replySafe(rootId, truncate(toolTextLines(pending), 2000));
|
|
1111
1400
|
}
|
|
1112
1401
|
accumulateTool(rt, evt) {
|
|
1113
1402
|
const panel = { tool: evt.tool ?? "tool" };
|
|
@@ -1164,6 +1453,10 @@ export class DirectSessionManager {
|
|
|
1164
1453
|
const markdown = `${delivery.answer}\n\n${[delivery.statsLine, delivery.note]
|
|
1165
1454
|
.filter((part) => part !== "")
|
|
1166
1455
|
.join("\n")}`.trim();
|
|
1456
|
+
if (markdown.length <= MAX_MESSAGE_CHARS) {
|
|
1457
|
+
await this.replySafe(session.root_id, markdown);
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1167
1460
|
await this.deliverLongContent(session.root_id, markdown, `direct-answer-${session.turns}.md`, "回答过长,无法作为消息发送;请在 TUI 活动视图查看该会话的完整回答。", "(回答过长,文件上传不可用,已改为文本投递;完整内容亦可在 TUI 活动视图查看)");
|
|
1168
1461
|
}
|
|
1169
1462
|
async deliverLongContent(rootId, markdown, fileName, oversizeNote, unavailableNote) {
|
|
@@ -1269,24 +1562,27 @@ export class DirectSessionManager {
|
|
|
1269
1562
|
const sessionId = session.session_id;
|
|
1270
1563
|
const isFirstTurn = sessionId === null;
|
|
1271
1564
|
const autoApprove = this.resolveTurnAutoApprove(session, rt);
|
|
1272
|
-
const
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
sessionId,
|
|
1277
|
-
autoApprove,
|
|
1278
|
-
agent: session.agent_override ?? config.opencode.agent,
|
|
1279
|
-
model: session.model_override ?? config.opencode.model,
|
|
1280
|
-
variant: config.opencode.variant,
|
|
1281
|
-
});
|
|
1565
|
+
const effectiveAgent = session.agent_override ?? config.opencode.agent;
|
|
1566
|
+
const effectiveModel = session.model_override ?? config.opencode.model;
|
|
1567
|
+
const switchAgent = effectiveAgent !== rt.appliedAgent;
|
|
1568
|
+
const switchModel = effectiveModel !== rt.appliedModel;
|
|
1282
1569
|
this.deps.logger.info("direct turn starting", {
|
|
1283
1570
|
root_id: session.root_id,
|
|
1284
1571
|
dir: session.path,
|
|
1285
|
-
agent:
|
|
1286
|
-
model:
|
|
1572
|
+
agent: effectiveAgent,
|
|
1573
|
+
model: effectiveModel ?? null,
|
|
1287
1574
|
auto_approve: autoApprove,
|
|
1288
1575
|
continuation: !isFirstTurn,
|
|
1289
|
-
|
|
1576
|
+
session_id: sessionId ?? undefined,
|
|
1577
|
+
switch_agent: switchAgent,
|
|
1578
|
+
switch_model: switchModel,
|
|
1579
|
+
poll_interval_ms: config.opencode.poll_interval_ms,
|
|
1580
|
+
tasking_version: readTaskingVersion(),
|
|
1581
|
+
command: describeApiCommand({
|
|
1582
|
+
command: config.opencode.command,
|
|
1583
|
+
operation: "session.prompt",
|
|
1584
|
+
serverUrl: config.opencode.server_url,
|
|
1585
|
+
}),
|
|
1290
1586
|
message: truncate(prompt, 60),
|
|
1291
1587
|
});
|
|
1292
1588
|
this.deps.store.updateDirectSession(session.root_id, { status: "running" });
|
|
@@ -1340,8 +1636,13 @@ export class DirectSessionManager {
|
|
|
1340
1636
|
activity.text = evt.text;
|
|
1341
1637
|
if (evt.tool !== undefined)
|
|
1342
1638
|
activity.tool = evt.tool;
|
|
1343
|
-
if (evt.
|
|
1344
|
-
activity.tokens =
|
|
1639
|
+
if (evt.usage !== undefined) {
|
|
1640
|
+
activity.tokens = {
|
|
1641
|
+
input: evt.usage.input_tokens,
|
|
1642
|
+
output: evt.usage.output_tokens,
|
|
1643
|
+
total: evt.usage.total_tokens,
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1345
1646
|
if (evt.title !== undefined)
|
|
1346
1647
|
activity.title = evt.title;
|
|
1347
1648
|
if (evt.input !== undefined)
|
|
@@ -1361,9 +1662,37 @@ export class DirectSessionManager {
|
|
|
1361
1662
|
this.deps.activity(`direct:${session.root_id}`, activity);
|
|
1362
1663
|
}
|
|
1363
1664
|
};
|
|
1665
|
+
const turnDriver = this.deps.turnDriver ?? defaultDriverFor(this.deps.apiExec ?? defaultApiExec());
|
|
1364
1666
|
let result;
|
|
1365
1667
|
try {
|
|
1366
|
-
result = await (
|
|
1668
|
+
result = await turnDriver({
|
|
1669
|
+
command: config.opencode.command,
|
|
1670
|
+
serverUrl: config.opencode.server_url,
|
|
1671
|
+
directory: session.path,
|
|
1672
|
+
prompt,
|
|
1673
|
+
sessionId,
|
|
1674
|
+
autoApprove,
|
|
1675
|
+
postureOpen: rt.postureOpen,
|
|
1676
|
+
agent: effectiveAgent,
|
|
1677
|
+
model: effectiveModel,
|
|
1678
|
+
variant: config.opencode.variant,
|
|
1679
|
+
switchAgent,
|
|
1680
|
+
switchModel,
|
|
1681
|
+
pollIntervalMs: config.opencode.poll_interval_ms,
|
|
1682
|
+
permissionTimeoutMs: config.opencode.permission_timeout_ms,
|
|
1683
|
+
turnTimeoutMs: config.opencode.turn_timeout_ms,
|
|
1684
|
+
inactivityTimeoutMs: config.opencode.inactivity_timeout_ms,
|
|
1685
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
1686
|
+
onEvent,
|
|
1687
|
+
seenMessages: rt.seenMessages,
|
|
1688
|
+
onPendingPermissions: (pending) => this.handlePendingPermissions(session.root_id, pending),
|
|
1689
|
+
onPermissionReply: (requestID) => this.approvals.markExpired(requestID),
|
|
1690
|
+
onWaitingForApproval: (waiting) => {
|
|
1691
|
+
rt.waitingApproval = waiting;
|
|
1692
|
+
rt.waitingSince = waiting ? Date.now() : null;
|
|
1693
|
+
},
|
|
1694
|
+
signal: abort.signal,
|
|
1695
|
+
});
|
|
1367
1696
|
}
|
|
1368
1697
|
catch (e) {
|
|
1369
1698
|
rt.abort = null;
|
|
@@ -1380,40 +1709,55 @@ export class DirectSessionManager {
|
|
|
1380
1709
|
finally {
|
|
1381
1710
|
rt.abort = null;
|
|
1382
1711
|
this.stopHeartbeat(rt);
|
|
1712
|
+
rt.waitingApproval = false;
|
|
1713
|
+
rt.waitingSince = null;
|
|
1383
1714
|
}
|
|
1384
1715
|
const durationSec = Math.max(1, Math.round((Date.now() - startedAt) / 1000));
|
|
1385
1716
|
const fresh = this.deps.store.getDirectSession(session.root_id);
|
|
1386
1717
|
if (!fresh || fresh.status === "done" || fresh.status === "expired")
|
|
1387
1718
|
return;
|
|
1388
|
-
|
|
1389
|
-
if (
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1719
|
+
rt.postureOpen = result.postureOpened;
|
|
1720
|
+
if (result.contextTokens !== undefined && result.contextTokens > rt.contextTokens) {
|
|
1721
|
+
rt.contextTokens = result.contextTokens;
|
|
1722
|
+
}
|
|
1723
|
+
if (isFirstTurn || result.agentSwitched !== false) {
|
|
1724
|
+
rt.appliedAgent = effectiveAgent;
|
|
1725
|
+
}
|
|
1726
|
+
if (isFirstTurn || result.modelSwitched !== false) {
|
|
1727
|
+
rt.appliedModel = effectiveModel;
|
|
1728
|
+
}
|
|
1729
|
+
const usage = result.usage;
|
|
1730
|
+
if (usage) {
|
|
1731
|
+
stats.inputTokens = usage.input_tokens;
|
|
1732
|
+
stats.outputTokens = usage.output_tokens;
|
|
1733
|
+
stats.totalTokens = usage.total_tokens;
|
|
1734
|
+
this.aggregate.inputTokens += usage.input_tokens;
|
|
1735
|
+
this.aggregate.outputTokens += usage.output_tokens;
|
|
1736
|
+
this.aggregate.totalTokens += usage.total_tokens;
|
|
1737
|
+
this.deps.store.addDirectSessionUsage(session.root_id, usage, 0);
|
|
1738
|
+
}
|
|
1739
|
+
if (result.sessionId) {
|
|
1740
|
+
this.deps.store.updateDirectSession(session.root_id, { session_id: result.sessionId });
|
|
1741
|
+
}
|
|
1742
|
+
this.approvals.cancelAll(result.sessionId);
|
|
1743
|
+
if (result.status === "interrupted" || rt.stopRequested) {
|
|
1402
1744
|
this.deps.store.markDirectSessionStatus(session.root_id, "idle");
|
|
1403
1745
|
await this.finalizeTurnCard(session.root_id, "interrupted", stats, durationSec);
|
|
1404
1746
|
return;
|
|
1405
1747
|
}
|
|
1406
|
-
if (result.
|
|
1407
|
-
const reason = result.
|
|
1748
|
+
if (result.status === "timed_out") {
|
|
1749
|
+
const reason = result.error?.includes("inactivity_timeout")
|
|
1750
|
+
? translate("无输出超时")
|
|
1751
|
+
: translate("整体超时");
|
|
1408
1752
|
this.deps.store.markDirectSessionStatus(session.root_id, "idle");
|
|
1409
1753
|
await this.finalizeTurnCard(session.root_id, "failed", stats, durationSec);
|
|
1410
1754
|
await this.replySafe(session.root_id, `⚠️ 本轮${reason}被中止(${durationSec}s)。可用更小的任务描述重试。`);
|
|
1411
1755
|
return;
|
|
1412
1756
|
}
|
|
1413
|
-
if (result.
|
|
1757
|
+
if (result.status === "failed" || result.status === "error") {
|
|
1414
1758
|
this.deps.store.markDirectSessionStatus(session.root_id, "idle");
|
|
1415
1759
|
await this.finalizeTurnCard(session.root_id, "failed", stats, durationSec, true);
|
|
1416
|
-
await this.replySafe(session.root_id, `⚠️
|
|
1760
|
+
await this.replySafe(session.root_id, `⚠️ 本轮未完成(${result.error ?? result.status},${durationSec}s)。${stats.lastText ? `\n\n${truncate(stats.lastText)}` : ""}`.trim());
|
|
1417
1761
|
return;
|
|
1418
1762
|
}
|
|
1419
1763
|
this.deps.store.markDirectSessionStatus(session.root_id, "idle");
|
|
@@ -1594,7 +1938,7 @@ export class DirectSessionManager {
|
|
|
1594
1938
|
? "🔓 本会话放开"
|
|
1595
1939
|
: postureNow === "once"
|
|
1596
1940
|
? "⚡ 仅下一轮放开"
|
|
1597
|
-
: "🔒
|
|
1941
|
+
: "🔒 默认(跟随项目配置)";
|
|
1598
1942
|
return [
|
|
1599
1943
|
`当前权限姿态:${current}`,
|
|
1600
1944
|
this.autoSourceLabel(session),
|
|
@@ -1776,6 +2120,31 @@ export class DirectSessionManager {
|
|
|
1776
2120
|
}
|
|
1777
2121
|
}
|
|
1778
2122
|
}
|
|
2123
|
+
function isRetryableCardError(e) {
|
|
2124
|
+
if (!(e instanceof TaskingError))
|
|
2125
|
+
return true;
|
|
2126
|
+
if (e.category === "feishu_api_request")
|
|
2127
|
+
return true;
|
|
2128
|
+
if (e.category !== "feishu_api_status")
|
|
2129
|
+
return false;
|
|
2130
|
+
const status = e.details?.status;
|
|
2131
|
+
return status === 429 || (typeof status === "number" && status >= 500);
|
|
2132
|
+
}
|
|
2133
|
+
function toolTextLines(tools) {
|
|
2134
|
+
return tools
|
|
2135
|
+
.map((t) => {
|
|
2136
|
+
const summary = toolSummary(t);
|
|
2137
|
+
const exit = t.exit !== undefined && t.exit !== 0 ? ` (exit ${t.exit})` : "";
|
|
2138
|
+
return `🔧 ${t.tool}${summary === "" ? "" : ` · ${summary}`}${exit}`;
|
|
2139
|
+
})
|
|
2140
|
+
.join("\n");
|
|
2141
|
+
}
|
|
2142
|
+
function sleepUnrefed(ms) {
|
|
2143
|
+
return new Promise((resolve) => {
|
|
2144
|
+
const timer = setTimeout(resolve, ms);
|
|
2145
|
+
timer.unref?.();
|
|
2146
|
+
});
|
|
2147
|
+
}
|
|
1779
2148
|
function fullDetailBytes(detail) {
|
|
1780
2149
|
return (Buffer.byteLength(detail.output ?? "", "utf8") +
|
|
1781
2150
|
Buffer.byteLength(detail.title ?? "", "utf8") +
|