ycodium-acp-executor 0.2.5634 → 0.2.5751
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 +2 -1
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.d.ts +47 -0
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.js +172 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.d.ts +63 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.js +220 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.d.ts +41 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.js +197 -0
- package/dist/_private/ycodium-jsonl-stream/history.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/history.js +389 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.js +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.d.ts +19 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.js +117 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.d.ts +31 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.js +144 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.d.ts +8 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.js +142 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.js +18 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.d.ts +79 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.js +603 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.d.ts +69 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.js +106 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.js +32 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
- package/dist/adapter-support.d.ts.map +1 -1
- package/dist/adapter-support.js.map +1 -1
- package/dist/apply-effort.d.ts.map +1 -1
- package/dist/apply-effort.js.map +1 -1
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js.map +1 -1
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js.map +1 -1
- package/dist/card-defaults.d.ts.map +1 -1
- package/dist/card-defaults.js.map +1 -1
- package/dist/config-option-wiring.d.ts.map +1 -1
- package/dist/config-option-wiring.js.map +1 -1
- package/dist/config-options.d.ts.map +1 -1
- package/dist/config-options.js.map +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/event-stream.d.ts.map +1 -1
- package/dist/event-stream.js.map +1 -1
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/executor/child-sessions.d.ts.map +1 -1
- package/dist/executor/child-sessions.js.map +1 -1
- package/dist/executor/lifecycle.d.ts.map +1 -1
- package/dist/executor/lifecycle.js.map +1 -1
- package/dist/executor/prompt-results.d.ts.map +1 -1
- package/dist/executor/prompt-results.js.map +1 -1
- package/dist/executor/prompt.d.ts.map +1 -1
- package/dist/executor/prompt.js.map +1 -1
- package/dist/executor/requests.d.ts.map +1 -1
- package/dist/executor/requests.js.map +1 -1
- package/dist/executor/runtime.d.ts.map +1 -1
- package/dist/executor/runtime.js.map +1 -1
- package/dist/executor/session.d.ts.map +1 -1
- package/dist/executor/session.js +17 -1
- package/dist/executor/session.js.map +1 -1
- package/dist/executor/settlement.d.ts.map +1 -1
- package/dist/executor/settlement.js.map +1 -1
- package/dist/executor/subagent-sessions.d.ts.map +1 -1
- package/dist/executor/subagent-sessions.js.map +1 -1
- package/dist/executor/support.d.ts.map +1 -1
- package/dist/executor/support.js.map +1 -1
- package/dist/executor/updates.d.ts.map +1 -1
- package/dist/executor/updates.js.map +1 -1
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js.map +1 -1
- package/dist/failureCategory.d.ts.map +1 -1
- package/dist/failureCategory.js.map +1 -1
- package/dist/handshake.d.ts.map +1 -1
- package/dist/handshake.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/instance-kit.d.ts.map +1 -1
- package/dist/instance-kit.js.map +1 -1
- package/dist/jsonrpc.d.ts.map +1 -1
- package/dist/jsonrpc.js.map +1 -1
- package/dist/line-buffer.d.ts.map +1 -1
- package/dist/line-buffer.js.map +1 -1
- package/dist/mcp-servers.d.ts +26 -4
- package/dist/mcp-servers.d.ts.map +1 -1
- package/dist/mcp-servers.js +25 -4
- package/dist/mcp-servers.js.map +1 -1
- package/dist/model-selection.d.ts.map +1 -1
- package/dist/model-selection.js.map +1 -1
- package/dist/model-send.d.ts.map +1 -1
- package/dist/model-send.js.map +1 -1
- package/dist/mutex.d.ts.map +1 -1
- package/dist/mutex.js.map +1 -1
- package/dist/native-session-transcript.d.ts.map +1 -1
- package/dist/native-session-transcript.js.map +1 -1
- package/dist/native-sessions.d.ts.map +1 -1
- package/dist/native-sessions.js.map +1 -1
- package/dist/openCommands.d.ts.map +1 -1
- package/dist/openCommands.js.map +1 -1
- package/dist/permission-statement.d.ts.map +1 -1
- package/dist/permission-statement.js.map +1 -1
- package/dist/probe-process.d.ts.map +1 -1
- package/dist/probe-process.js.map +1 -1
- package/dist/probe-session.d.ts.map +1 -1
- package/dist/probe-session.js.map +1 -1
- package/dist/probe.d.ts.map +1 -1
- package/dist/probe.js.map +1 -1
- package/dist/prompt-dispatch.d.ts.map +1 -1
- package/dist/prompt-dispatch.js.map +1 -1
- package/dist/protocol-log.d.ts.map +1 -1
- package/dist/protocol-log.js.map +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/refreshDeadline.d.ts.map +1 -1
- package/dist/refreshDeadline.js.map +1 -1
- package/dist/runtime-mode.d.ts.map +1 -1
- package/dist/runtime-mode.js.map +1 -1
- package/dist/runtime-model.d.ts.map +1 -1
- package/dist/runtime-model.js.map +1 -1
- package/dist/session-cleanup.d.ts.map +1 -1
- package/dist/session-cleanup.js.map +1 -1
- package/dist/session-registry.d.ts.map +1 -1
- package/dist/session-registry.js.map +1 -1
- package/dist/session-update.d.ts.map +1 -1
- package/dist/session-update.js.map +1 -1
- package/dist/sessionProcess.d.ts.map +1 -1
- package/dist/sessionProcess.js.map +1 -1
- package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
- package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
- package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
- package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
- package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
- package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
- package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
- package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
- package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
- package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
- package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
- package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
- package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
- package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
- package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
- package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
- package/dist/source/adapter-support.ts +115 -0
- package/dist/source/apply-effort.ts +66 -0
- package/dist/source/approvals.ts +94 -0
- package/dist/source/artifacts.ts +88 -0
- package/dist/source/card-defaults.ts +25 -0
- package/dist/source/config-option-wiring.ts +119 -0
- package/dist/source/config-options.ts +270 -0
- package/dist/source/connection.ts +553 -0
- package/dist/source/errors.ts +207 -0
- package/dist/source/event-stream.ts +91 -0
- package/dist/source/events.ts +341 -0
- package/dist/source/executor/child-sessions.ts +87 -0
- package/dist/source/executor/lifecycle.ts +282 -0
- package/dist/source/executor/prompt-results.ts +123 -0
- package/dist/source/executor/prompt.ts +606 -0
- package/dist/source/executor/requests.ts +323 -0
- package/dist/source/executor/runtime.ts +137 -0
- package/dist/source/executor/session.ts +553 -0
- package/dist/source/executor/settlement.ts +92 -0
- package/dist/source/executor/subagent-sessions.ts +183 -0
- package/dist/source/executor/support.ts +129 -0
- package/dist/source/executor/updates.ts +427 -0
- package/dist/source/executor.ts +537 -0
- package/dist/source/failureCategory.ts +74 -0
- package/dist/source/handshake.ts +66 -0
- package/dist/source/index.ts +210 -0
- package/dist/source/instance-kit.ts +478 -0
- package/dist/source/jsonrpc.ts +59 -0
- package/dist/source/line-buffer.ts +54 -0
- package/dist/source/mcp-servers.ts +83 -0
- package/dist/source/model-selection.ts +138 -0
- package/dist/source/model-send.ts +80 -0
- package/dist/source/mutex.ts +47 -0
- package/dist/source/native-session-transcript.ts +167 -0
- package/dist/source/native-sessions.ts +271 -0
- package/dist/source/openCommands.ts +39 -0
- package/dist/source/permission-statement.ts +49 -0
- package/dist/source/probe-process.ts +95 -0
- package/dist/source/probe-session.ts +81 -0
- package/dist/source/probe.ts +261 -0
- package/dist/source/prompt-dispatch.ts +42 -0
- package/dist/source/protocol-log.ts +143 -0
- package/dist/source/protocol.ts +36 -0
- package/dist/source/refreshDeadline.ts +2 -0
- package/dist/source/runtime-mode.ts +125 -0
- package/dist/source/runtime-model.ts +598 -0
- package/dist/source/session-cleanup.ts +82 -0
- package/dist/source/session-registry.ts +62 -0
- package/dist/source/session-update.ts +88 -0
- package/dist/source/sessionProcess.ts +29 -0
- package/dist/source/start-abort.ts +19 -0
- package/dist/source/thread-operations.ts +145 -0
- package/dist/source/tool-call-state.ts +322 -0
- package/dist/source/tool-presentation.ts +20 -0
- package/dist/source/tool-subagents.ts +491 -0
- package/dist/source/turn-usage.ts +78 -0
- package/dist/source/types.ts +146 -0
- package/dist/source/user-input-pending.ts +31 -0
- package/dist/source/util.ts +38 -0
- package/dist/source/vendor-hooks.ts +394 -0
- package/dist/start-abort.d.ts.map +1 -1
- package/dist/start-abort.js.map +1 -1
- package/dist/thread-operations.d.ts.map +1 -1
- package/dist/thread-operations.js.map +1 -1
- package/dist/tool-call-state.d.ts.map +1 -1
- package/dist/tool-call-state.js.map +1 -1
- package/dist/tool-presentation.d.ts.map +1 -1
- package/dist/tool-presentation.js.map +1 -1
- package/dist/tool-subagents.d.ts +1 -0
- package/dist/tool-subagents.d.ts.map +1 -1
- package/dist/tool-subagents.js +2 -0
- package/dist/tool-subagents.js.map +1 -1
- package/dist/turn-usage.d.ts.map +1 -1
- package/dist/turn-usage.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/user-input-pending.d.ts.map +1 -1
- package/dist/user-input-pending.js.map +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js.map +1 -1
- package/dist/vendor-hooks.d.ts +9 -0
- package/dist/vendor-hooks.d.ts.map +1 -1
- package/dist/vendor-hooks.js.map +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把「子 agent 就是一次工具调用」的 agent 翻成 `task.*`。
|
|
3
|
+
*
|
|
4
|
+
* ACP 上这类 agent 看不到子 agent 的内部进度,只有一条工具调用。状态机只有这一份:
|
|
5
|
+
* 厂商用 `AcpToolSubagentSpec` 说明怎么认人、身份字段从哪取、怎么读结果;开着、一轮结束、
|
|
6
|
+
* 进程没了怎么收尾由这里定,事件一律走执行器自己的发布入口(身份、时间戳、代次)。
|
|
7
|
+
*
|
|
8
|
+
* 状态:开着(pending / running)→ 一轮结束报 `idle` 并带 `outcomeUnknown`,保留身份;
|
|
9
|
+
* 拿到真实结果按结果发终态并封死;进程没了(含传输失败)开着的和 idle 的都补一次 `cancelled`。
|
|
10
|
+
*/
|
|
11
|
+
import type { AcpTaskEvent, AcpToolCallHookResult, AcpToolCallInput } from "./vendor-hooks.ts";
|
|
12
|
+
|
|
13
|
+
/** `AcpToolSubagentSpec.facts` 返回的、会写进每条事件的身份字段。 */
|
|
14
|
+
export type AcpToolSubagentFacts = {
|
|
15
|
+
readonly title: string;
|
|
16
|
+
readonly description: string;
|
|
17
|
+
/** 进度事件里的 `summary`;缺省不带。 */
|
|
18
|
+
readonly summary?: string;
|
|
19
|
+
readonly role?: string;
|
|
20
|
+
readonly model?: string;
|
|
21
|
+
readonly effort?: string;
|
|
22
|
+
readonly nativeSessionId?: string;
|
|
23
|
+
readonly rosterKind?: "subagent";
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** 厂商从工具结果里读出的结局。 */
|
|
27
|
+
export type AcpToolSubagentResolution =
|
|
28
|
+
/** 先静默记下,不发任何事件(历史回放里还没到的结果)。 */
|
|
29
|
+
| { readonly type: "hold" }
|
|
30
|
+
/** 工具已转入后台,之后 ACP 不再报它。 */
|
|
31
|
+
| {
|
|
32
|
+
readonly type: "background";
|
|
33
|
+
readonly agentRef?: string;
|
|
34
|
+
/** 比标题里更准的角色。 */
|
|
35
|
+
readonly role?: string;
|
|
36
|
+
}
|
|
37
|
+
| {
|
|
38
|
+
readonly type: "final";
|
|
39
|
+
/** `idle` = 结局未知;其余是真实终态。 */
|
|
40
|
+
readonly status: "completed" | "failed" | "cancelled" | "idle";
|
|
41
|
+
readonly summary?: string;
|
|
42
|
+
readonly description?: string;
|
|
43
|
+
readonly timelineBypass?: boolean;
|
|
44
|
+
readonly role?: string;
|
|
45
|
+
/** 厂商自己的 agent 号,续接调用靠它认回同一个任务。 */
|
|
46
|
+
readonly agentRef?: string;
|
|
47
|
+
/** 结果就是用户要看的回答:这次调用保留工具行。 */
|
|
48
|
+
readonly keepToolRow?: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type AcpToolSubagentSettlement = "turn-ended" | "turn-interrupted" | "session-ended";
|
|
52
|
+
|
|
53
|
+
/** `Kind` 是厂商自己区分调用种类的字符串(比如单个与成批)。 */
|
|
54
|
+
export type AcpToolSubagentSpec = {
|
|
55
|
+
/** 认出子 agent 调用;`exclude` = 同名但不是(记住,之后不再判),返回 undefined = 不相关。 */
|
|
56
|
+
readonly recognize: (call: AcpToolCallInput) => string | "exclude" | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* 参数到齐了吗。认出是子 agent 调用但参数还没到(首帧常常没有入参)时返回 false:
|
|
59
|
+
* 先只记下、不发任何事件,带参数的那一帧到了再判断是新 agent 还是续接。缺省 = 立刻发布。
|
|
60
|
+
*/
|
|
61
|
+
readonly ready?: (call: AcpToolCallInput, kind: string) => boolean;
|
|
62
|
+
/** 身份字段。`previous` 是同一任务上次的结果,想固定身份就原样返回它。 */
|
|
63
|
+
readonly facts: (
|
|
64
|
+
call: AcpToolCallInput,
|
|
65
|
+
kind: string,
|
|
66
|
+
previous: AcpToolSubagentFacts | undefined,
|
|
67
|
+
) => AcpToolSubagentFacts;
|
|
68
|
+
/** 首次见到时发 `task.started`,还是直接发 `task.progress`。 */
|
|
69
|
+
readonly announce: "started" | "progress";
|
|
70
|
+
/** 读结果;每条更新都会问一次,没有结局返回 undefined。 */
|
|
71
|
+
readonly resolve?: (
|
|
72
|
+
call: AcpToolCallInput,
|
|
73
|
+
kind: string,
|
|
74
|
+
seen: { readonly known: boolean; readonly background: boolean },
|
|
75
|
+
) => AcpToolSubagentResolution | undefined;
|
|
76
|
+
/** 这次调用续接的是哪个 agent 号;认得回就沿用旧任务 id 并明说重开。 */
|
|
77
|
+
readonly resumes?: (call: AcpToolCallInput, kind: string) => string | undefined;
|
|
78
|
+
/** 被打断不等于子 agent 停了(它在后台继续跑):打断的一轮报 idle,不报 cancelled。 */
|
|
79
|
+
readonly interruptedMayStillRun?: boolean;
|
|
80
|
+
/** 一轮结束但没有结果时,`idle` 事件带的说明。 */
|
|
81
|
+
readonly endNote: (info: { readonly interrupted: boolean; readonly background: boolean }) => {
|
|
82
|
+
readonly description: string;
|
|
83
|
+
readonly timelineBypass?: boolean;
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
type Entry = {
|
|
88
|
+
readonly kind: string;
|
|
89
|
+
readonly taskId: string;
|
|
90
|
+
turnId: string | undefined;
|
|
91
|
+
facts: AcpToolSubagentFacts;
|
|
92
|
+
/** open = 在跑;idle = 一轮结束无结果,等真实结果或进程退出。 */
|
|
93
|
+
phase: "open" | "idle";
|
|
94
|
+
/** hold 的条目还没有状态。 */
|
|
95
|
+
status: "pending" | "running" | undefined;
|
|
96
|
+
background: boolean;
|
|
97
|
+
/** 已经向外发布过身份;没有的只是记下了调用,参数还没到。 */
|
|
98
|
+
announced: boolean;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** 封存后只留抑制迟到更新所需的信息;续接资料归 `agentTasks`。 */
|
|
102
|
+
type Sealed = {
|
|
103
|
+
readonly sealed: true;
|
|
104
|
+
readonly keepToolRow: boolean;
|
|
105
|
+
/** 保留工具行的真实终态与已报的结果;重复的终态只用来补上当时缺的结果。 */
|
|
106
|
+
readonly final?: { readonly status: "completed" | "failed"; summary: string | undefined };
|
|
107
|
+
readonly turnId: string | undefined;
|
|
108
|
+
/** 只为晚到的结果还能记下厂商 agent 号 → 任务 id。 */
|
|
109
|
+
readonly kind: string;
|
|
110
|
+
readonly taskId: string;
|
|
111
|
+
readonly facts: AcpToolSubagentFacts;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const EXCLUDED = Symbol("excluded");
|
|
115
|
+
const sealedHidden = (entry: Entry): Sealed => ({
|
|
116
|
+
sealed: true,
|
|
117
|
+
keepToolRow: false,
|
|
118
|
+
turnId: entry.turnId,
|
|
119
|
+
kind: entry.kind,
|
|
120
|
+
taskId: entry.taskId,
|
|
121
|
+
facts: entry.facts,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
export type AcpToolSubagentTracker = {
|
|
125
|
+
/** 不是子 agent 的调用返回 undefined,交还给厂商自己的钩子。 */
|
|
126
|
+
readonly onToolCallUpdated: (call: AcpToolCallInput) => AcpToolCallHookResult | undefined;
|
|
127
|
+
/** 收尾还没有结果的调用,返回要发的事件。 */
|
|
128
|
+
readonly settle: (
|
|
129
|
+
settlement: AcpToolSubagentSettlement,
|
|
130
|
+
turnId?: string,
|
|
131
|
+
) => ReadonlyArray<AcpTaskEvent>;
|
|
132
|
+
/** 还有没拿到结局的子 agent(在跑的,或一轮结束后状态未知的):它们可能还在后台跑。 */
|
|
133
|
+
readonly mayStillRun: () => boolean;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** `turn.completed` 对应哪种收尾:被打断、进程退出(进程已经没了)、其余算正常结束。 */
|
|
137
|
+
export function toolSubagentSettlementOfTurn(
|
|
138
|
+
state: string,
|
|
139
|
+
failureCategory: string | undefined,
|
|
140
|
+
): AcpToolSubagentSettlement {
|
|
141
|
+
if (state === "cancelled") return "turn-interrupted";
|
|
142
|
+
return state === "failed" && failureCategory === "process-exit" ? "session-ended" : "turn-ended";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const sameFacts = (a: AcpToolSubagentFacts, b: AcpToolSubagentFacts): boolean =>
|
|
146
|
+
a.title === b.title &&
|
|
147
|
+
a.description === b.description &&
|
|
148
|
+
a.summary === b.summary &&
|
|
149
|
+
a.role === b.role &&
|
|
150
|
+
a.model === b.model &&
|
|
151
|
+
a.effort === b.effort &&
|
|
152
|
+
a.nativeSessionId === b.nativeSessionId &&
|
|
153
|
+
a.rosterKind === b.rosterKind;
|
|
154
|
+
|
|
155
|
+
function linkage(taskId: string, toolCallId: string, facts: AcpToolSubagentFacts) {
|
|
156
|
+
return {
|
|
157
|
+
taskId,
|
|
158
|
+
taskType: "subagent",
|
|
159
|
+
toolUseId: toolCallId,
|
|
160
|
+
title: facts.title,
|
|
161
|
+
...(facts.role === undefined ? {} : { role: facts.role }),
|
|
162
|
+
...(facts.model === undefined ? {} : { model: facts.model }),
|
|
163
|
+
...(facts.effort === undefined ? {} : { effort: facts.effort }),
|
|
164
|
+
...(facts.nativeSessionId === undefined ? {} : { nativeSessionId: facts.nativeSessionId }),
|
|
165
|
+
...(facts.rosterKind === undefined ? {} : { rosterKind: facts.rosterKind }),
|
|
166
|
+
} as const;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const isEntry = (value: Entry | Sealed | typeof EXCLUDED): value is Entry =>
|
|
170
|
+
value !== EXCLUDED && !("sealed" in value);
|
|
171
|
+
|
|
172
|
+
export function createToolSubagentTracker(spec: AcpToolSubagentSpec): AcpToolSubagentTracker {
|
|
173
|
+
const calls = new Map<string, Entry | Sealed | typeof EXCLUDED>();
|
|
174
|
+
/**
|
|
175
|
+
* 厂商 agent 号 → 任务 id 与上次身份,只在本执行器内存里,不淘汰(只是短字符串,淘汰会让
|
|
176
|
+
* 仍可续接的 agent 变成另一条任务)。
|
|
177
|
+
* 约束:执行器重启(会话恢复)后映射丢失,续接会成为新的一行,不做持久化。
|
|
178
|
+
*/
|
|
179
|
+
const agentTasks = new Map<
|
|
180
|
+
string,
|
|
181
|
+
{ readonly taskId: string; readonly facts: AcpToolSubagentFacts }
|
|
182
|
+
>();
|
|
183
|
+
|
|
184
|
+
const rememberAgent = (ref: string, taskId: string, facts: AcpToolSubagentFacts): void => {
|
|
185
|
+
agentTasks.set(ref, { taskId, facts });
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const turnOf = (turnId: string | undefined) => (turnId === undefined ? {} : { turnId });
|
|
189
|
+
|
|
190
|
+
const onToolCallUpdated: AcpToolSubagentTracker["onToolCallUpdated"] = (call) => {
|
|
191
|
+
const tracked = calls.get(call.toolCallId);
|
|
192
|
+
if (tracked === EXCLUDED) return undefined;
|
|
193
|
+
if (tracked !== undefined && !isEntry(tracked)) {
|
|
194
|
+
// 状态照旧抑制,但晚到的结果里的 agent 号仍要记下,之后的续接才认得回。
|
|
195
|
+
const late = spec.resolve?.(call, tracked.kind, { known: true, background: false });
|
|
196
|
+
if (late !== undefined && late.type !== "hold" && late.agentRef !== undefined) {
|
|
197
|
+
rememberAgent(late.agentRef, tracked.taskId, tracked.facts);
|
|
198
|
+
}
|
|
199
|
+
// 保留的工具行在封存那一帧已经出过终态;重复的终态再放行,客户端会多出一行。
|
|
200
|
+
// 重复的终态补来了当时缺的结果(比如先空后补的失败原因):同一终态再报一次带上它,
|
|
201
|
+
// 宿主与客户端对已收尾的条目只补缺的结果。
|
|
202
|
+
const repeatedTerminal = call.status === "completed" || call.status === "failed";
|
|
203
|
+
const events: AcpTaskEvent[] = [];
|
|
204
|
+
const final = tracked.final;
|
|
205
|
+
if (
|
|
206
|
+
repeatedTerminal &&
|
|
207
|
+
final !== undefined &&
|
|
208
|
+
final.summary === undefined &&
|
|
209
|
+
late?.type === "final" &&
|
|
210
|
+
late.status === final.status &&
|
|
211
|
+
late.summary !== undefined
|
|
212
|
+
) {
|
|
213
|
+
final.summary = late.summary;
|
|
214
|
+
events.push({
|
|
215
|
+
type: "task.completed",
|
|
216
|
+
...turnOf(tracked.turnId),
|
|
217
|
+
payload: {
|
|
218
|
+
...linkage(tracked.taskId, call.toolCallId, tracked.facts),
|
|
219
|
+
status: final.status,
|
|
220
|
+
summary: late.summary,
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
events,
|
|
226
|
+
handled: !tracked.keepToolRow || repeatedTerminal,
|
|
227
|
+
...turnOf(tracked.turnId),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let known: Entry | undefined = tracked;
|
|
232
|
+
let kind: string;
|
|
233
|
+
if (known === undefined) {
|
|
234
|
+
const recognized = spec.recognize(call);
|
|
235
|
+
if (recognized === undefined) return undefined;
|
|
236
|
+
if (recognized === "exclude") {
|
|
237
|
+
calls.set(call.toolCallId, EXCLUDED);
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
kind = recognized;
|
|
241
|
+
} else {
|
|
242
|
+
kind = known.kind;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const resolution = spec.resolve?.(call, kind, {
|
|
246
|
+
known: known?.announced === true,
|
|
247
|
+
background: known?.background ?? false,
|
|
248
|
+
});
|
|
249
|
+
// 一轮结束后留下的 idle 只等真实终态:进度类更新不重开它,也不画成工具行。
|
|
250
|
+
if (known?.phase === "idle" && resolution?.type !== "final") {
|
|
251
|
+
if (resolution?.type === "background" && resolution.agentRef !== undefined) {
|
|
252
|
+
rememberAgent(resolution.agentRef, known.taskId, known.facts);
|
|
253
|
+
}
|
|
254
|
+
return { events: [], handled: true };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
let carriedTurn: string | undefined;
|
|
258
|
+
if (known === undefined || !known.announced) {
|
|
259
|
+
// 参数还没到:只记下,等带参数的那一帧再判断是新 agent 还是续接。
|
|
260
|
+
if (resolution === undefined && !(spec.ready?.(call, kind) ?? true)) {
|
|
261
|
+
if (known === undefined) {
|
|
262
|
+
calls.set(call.toolCallId, {
|
|
263
|
+
kind,
|
|
264
|
+
taskId: call.toolCallId,
|
|
265
|
+
turnId: call.turnId,
|
|
266
|
+
facts: spec.facts(call, kind, undefined),
|
|
267
|
+
phase: "open",
|
|
268
|
+
status: undefined,
|
|
269
|
+
background: false,
|
|
270
|
+
announced: false,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return { events: [], handled: true };
|
|
274
|
+
}
|
|
275
|
+
carriedTurn = known?.turnId;
|
|
276
|
+
calls.delete(call.toolCallId);
|
|
277
|
+
known = undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const events: AcpTaskEvent[] = [];
|
|
281
|
+
const ref = known === undefined ? spec.resumes?.(call, kind) : undefined;
|
|
282
|
+
const prior = ref === undefined ? undefined : agentTasks.get(ref);
|
|
283
|
+
const facts = spec.facts(call, kind, prior?.facts ?? known?.facts);
|
|
284
|
+
const turnId = known?.turnId ?? carriedTurn ?? call.turnId;
|
|
285
|
+
const turn = turnOf(turnId);
|
|
286
|
+
|
|
287
|
+
let entry: Entry;
|
|
288
|
+
if (known !== undefined) {
|
|
289
|
+
entry = known;
|
|
290
|
+
} else if (prior !== undefined) {
|
|
291
|
+
// 续接:沿用旧任务 id 并明说重开;旧调用不再收尾,已封存的也不再补报结果(那会收掉这一轮)。
|
|
292
|
+
for (const [id, other] of calls) {
|
|
293
|
+
if (other === EXCLUDED || other.taskId !== prior.taskId) continue;
|
|
294
|
+
if (isEntry(other)) calls.set(id, sealedHidden(other));
|
|
295
|
+
else if (other.final !== undefined) {
|
|
296
|
+
const { final: _superseded, ...rest } = other;
|
|
297
|
+
calls.set(id, rest);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
entry = {
|
|
301
|
+
kind,
|
|
302
|
+
taskId: prior.taskId,
|
|
303
|
+
turnId,
|
|
304
|
+
facts,
|
|
305
|
+
phase: "open",
|
|
306
|
+
status: "running",
|
|
307
|
+
background: false,
|
|
308
|
+
announced: true,
|
|
309
|
+
};
|
|
310
|
+
calls.set(call.toolCallId, entry);
|
|
311
|
+
events.push({
|
|
312
|
+
type: "task.updated",
|
|
313
|
+
...turn,
|
|
314
|
+
payload: {
|
|
315
|
+
...linkage(entry.taskId, call.toolCallId, facts),
|
|
316
|
+
description: facts.description,
|
|
317
|
+
status: "running",
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
} else {
|
|
321
|
+
entry = {
|
|
322
|
+
kind,
|
|
323
|
+
taskId: call.toolCallId,
|
|
324
|
+
turnId,
|
|
325
|
+
facts,
|
|
326
|
+
phase: "open",
|
|
327
|
+
status: undefined,
|
|
328
|
+
background: false,
|
|
329
|
+
announced: true,
|
|
330
|
+
};
|
|
331
|
+
calls.set(call.toolCallId, entry);
|
|
332
|
+
if (spec.announce === "started") {
|
|
333
|
+
events.push({
|
|
334
|
+
type: "task.started",
|
|
335
|
+
...turn,
|
|
336
|
+
payload: {
|
|
337
|
+
...linkage(entry.taskId, call.toolCallId, facts),
|
|
338
|
+
description: facts.description,
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const seen = known !== undefined || prior !== undefined;
|
|
344
|
+
const wasStatus = entry.status;
|
|
345
|
+
entry.turnId = turnId;
|
|
346
|
+
|
|
347
|
+
if (resolution?.type === "hold") {
|
|
348
|
+
entry.facts = facts;
|
|
349
|
+
return { events, handled: true };
|
|
350
|
+
}
|
|
351
|
+
if (resolution?.type === "background") {
|
|
352
|
+
entry.facts = resolution.role === undefined ? facts : { ...facts, role: resolution.role };
|
|
353
|
+
if (!entry.background) {
|
|
354
|
+
events.push({
|
|
355
|
+
type: "task.updated",
|
|
356
|
+
...turn,
|
|
357
|
+
payload: {
|
|
358
|
+
...linkage(entry.taskId, call.toolCallId, entry.facts),
|
|
359
|
+
status: "running",
|
|
360
|
+
isBackgrounded: true,
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
entry.background = true;
|
|
365
|
+
entry.status = "running";
|
|
366
|
+
if (resolution.agentRef !== undefined)
|
|
367
|
+
rememberAgent(resolution.agentRef, entry.taskId, entry.facts);
|
|
368
|
+
return { events, handled: true };
|
|
369
|
+
}
|
|
370
|
+
if (resolution?.type === "final") {
|
|
371
|
+
const finalFacts =
|
|
372
|
+
resolution.role === undefined ? facts : { ...facts, role: resolution.role };
|
|
373
|
+
const keepToolRow = resolution.keepToolRow === true;
|
|
374
|
+
const base = linkage(entry.taskId, call.toolCallId, finalFacts);
|
|
375
|
+
if (resolution.agentRef !== undefined)
|
|
376
|
+
rememberAgent(resolution.agentRef, entry.taskId, finalFacts);
|
|
377
|
+
calls.set(call.toolCallId, {
|
|
378
|
+
sealed: true,
|
|
379
|
+
keepToolRow,
|
|
380
|
+
...(keepToolRow && (resolution.status === "completed" || resolution.status === "failed")
|
|
381
|
+
? { final: { status: resolution.status, summary: resolution.summary } }
|
|
382
|
+
: {}),
|
|
383
|
+
turnId,
|
|
384
|
+
kind,
|
|
385
|
+
taskId: entry.taskId,
|
|
386
|
+
facts: finalFacts,
|
|
387
|
+
});
|
|
388
|
+
if (resolution.status === "completed" || resolution.status === "failed") {
|
|
389
|
+
events.push({
|
|
390
|
+
type: "task.completed",
|
|
391
|
+
...turn,
|
|
392
|
+
payload: {
|
|
393
|
+
...base,
|
|
394
|
+
status: resolution.status,
|
|
395
|
+
...(resolution.summary === undefined ? {} : { summary: resolution.summary }),
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
} else {
|
|
399
|
+
events.push({
|
|
400
|
+
type: "task.updated",
|
|
401
|
+
...turn,
|
|
402
|
+
payload: {
|
|
403
|
+
...base,
|
|
404
|
+
status: resolution.status,
|
|
405
|
+
...(resolution.status === "idle" ? { outcomeUnknown: true } : {}),
|
|
406
|
+
...(resolution.description === undefined
|
|
407
|
+
? {}
|
|
408
|
+
: { description: resolution.description }),
|
|
409
|
+
...(resolution.timelineBypass === true ? { timelineBypass: true } : {}),
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return { events, handled: !keepToolRow, ...turn };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const status = call.status === "pending" && wasStatus !== "running" ? "pending" : "running";
|
|
417
|
+
const changed =
|
|
418
|
+
!seen && spec.announce === "progress"
|
|
419
|
+
? true
|
|
420
|
+
: seen && (wasStatus !== status || !sameFacts(entry.facts, facts));
|
|
421
|
+
entry.facts = facts;
|
|
422
|
+
entry.status = status;
|
|
423
|
+
if (changed) {
|
|
424
|
+
events.push({
|
|
425
|
+
type: "task.progress",
|
|
426
|
+
...turn,
|
|
427
|
+
payload: {
|
|
428
|
+
...linkage(entry.taskId, call.toolCallId, facts),
|
|
429
|
+
description: facts.description,
|
|
430
|
+
...(facts.summary === undefined ? {} : { summary: facts.summary }),
|
|
431
|
+
status,
|
|
432
|
+
},
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
return { events, handled: true };
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const settle: AcpToolSubagentTracker["settle"] = (settlement, turnId) => {
|
|
439
|
+
const events: AcpTaskEvent[] = [];
|
|
440
|
+
for (const [toolCallId, entry] of calls) {
|
|
441
|
+
if (!isEntry(entry)) continue;
|
|
442
|
+
// 轮次级收尾只处理这一轮发起的任务;会话退出才处理全部。
|
|
443
|
+
if (
|
|
444
|
+
settlement !== "session-ended" &&
|
|
445
|
+
turnId !== undefined &&
|
|
446
|
+
entry.turnId !== undefined &&
|
|
447
|
+
entry.turnId !== turnId
|
|
448
|
+
) {
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
const interrupted = settlement === "turn-interrupted";
|
|
452
|
+
const ended =
|
|
453
|
+
settlement === "session-ended" || (interrupted && spec.interruptedMayStillRun !== true);
|
|
454
|
+
if (!entry.announced) {
|
|
455
|
+
// 没发布过身份,没有东西要收;但收尾要记下,晚到的参数帧不能让已结束的轮次复活。
|
|
456
|
+
if (ended) calls.set(toolCallId, sealedHidden(entry));
|
|
457
|
+
else entry.phase = "idle";
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
const base = linkage(entry.taskId, toolCallId, entry.facts);
|
|
461
|
+
const turn = turnOf(entry.turnId);
|
|
462
|
+
if (ended) {
|
|
463
|
+
events.push({ type: "task.updated", ...turn, payload: { ...base, status: "cancelled" } });
|
|
464
|
+
calls.set(toolCallId, sealedHidden(entry));
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (entry.phase === "idle") continue;
|
|
468
|
+
const note = spec.endNote({ interrupted, background: entry.background });
|
|
469
|
+
events.push({
|
|
470
|
+
type: "task.updated",
|
|
471
|
+
...turn,
|
|
472
|
+
payload: {
|
|
473
|
+
...base,
|
|
474
|
+
status: "idle",
|
|
475
|
+
outcomeUnknown: true,
|
|
476
|
+
description: note.description,
|
|
477
|
+
...(note.timelineBypass === true ? { timelineBypass: true } : {}),
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
entry.phase = "idle";
|
|
481
|
+
}
|
|
482
|
+
return events;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
const mayStillRun: AcpToolSubagentTracker["mayStillRun"] = () => {
|
|
486
|
+
for (const entry of calls.values()) if (isEntry(entry)) return true;
|
|
487
|
+
return false;
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
return { onToolCallUpdated, settle, mayStillRun };
|
|
491
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从 `AcpTurnUsage.ts` 搬来。ACP 的计数是整个会话的累计值,记账要的是每个 turn 的增量。
|
|
3
|
+
*/
|
|
4
|
+
import type { AcpUsageDelta, AcpUsageSnapshot, AcpUsageTokenTotals } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
const KNOWN_USAGE_PROVIDERS = new Set(["claude", "codex", "grok", "opencode", "dsh", "acp"]);
|
|
7
|
+
|
|
8
|
+
export function acpUsageProviderKind(driver: string): string {
|
|
9
|
+
return KNOWN_USAGE_PROVIDERS.has(driver) ? driver : "acp";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const ACP_USAGE_BASELINE_NEW_SESSION: AcpUsageSnapshot = {
|
|
13
|
+
uncachedInputTokens: 0,
|
|
14
|
+
cachedInputTokens: 0,
|
|
15
|
+
cacheCreationTokens: 0,
|
|
16
|
+
outputTokens: 0,
|
|
17
|
+
reasoningTokens: 0,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function toCount(value: number | null | undefined): number {
|
|
21
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type AcpUsageReport = {
|
|
25
|
+
readonly inputTokens?: number | null;
|
|
26
|
+
readonly outputTokens?: number | null;
|
|
27
|
+
readonly thoughtTokens?: number | null;
|
|
28
|
+
readonly cachedReadTokens?: number | null;
|
|
29
|
+
readonly cachedWriteTokens?: number | null;
|
|
30
|
+
readonly totalTokens?: number | null;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function readAcpUsageSnapshot(
|
|
34
|
+
usage: AcpUsageReport | null | undefined,
|
|
35
|
+
): AcpUsageSnapshot | undefined {
|
|
36
|
+
if (!usage) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
uncachedInputTokens: toCount(usage.inputTokens),
|
|
41
|
+
cachedInputTokens: toCount(usage.cachedReadTokens),
|
|
42
|
+
cacheCreationTokens: toCount(usage.cachedWriteTokens),
|
|
43
|
+
outputTokens: toCount(usage.outputTokens),
|
|
44
|
+
reasoningTokens: toCount(usage.thoughtTokens),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function diffSnapshots(previous: AcpUsageSnapshot, current: AcpUsageSnapshot): AcpUsageTokenTotals {
|
|
49
|
+
return {
|
|
50
|
+
uncachedInputTokens: Math.max(0, current.uncachedInputTokens - previous.uncachedInputTokens),
|
|
51
|
+
cachedInputTokens: Math.max(0, current.cachedInputTokens - previous.cachedInputTokens),
|
|
52
|
+
cacheCreationTokens: Math.max(0, current.cacheCreationTokens - previous.cacheCreationTokens),
|
|
53
|
+
outputTokens: Math.max(0, current.outputTokens - previous.outputTokens),
|
|
54
|
+
reasoningTokens: Math.max(0, current.reasoningTokens - previous.reasoningTokens),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function advanceAcpUsage(
|
|
59
|
+
previous: AcpUsageSnapshot | undefined,
|
|
60
|
+
usage: AcpUsageReport | null | undefined,
|
|
61
|
+
): AcpUsageDelta | undefined {
|
|
62
|
+
const snapshot = readAcpUsageSnapshot(usage);
|
|
63
|
+
if (!snapshot) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
return previous === undefined
|
|
67
|
+
? { snapshot, totals: undefined }
|
|
68
|
+
: { snapshot, totals: diffSnapshots(previous, snapshot) };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function hasAnyTokens(totals: AcpUsageTokenTotals): boolean {
|
|
72
|
+
return (
|
|
73
|
+
totals.uncachedInputTokens > 0 ||
|
|
74
|
+
totals.cachedInputTokens > 0 ||
|
|
75
|
+
totals.cacheCreationTokens > 0 ||
|
|
76
|
+
totals.outputTokens > 0
|
|
77
|
+
);
|
|
78
|
+
}
|