@downcity/agent 1.1.217 → 1.1.224

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/bin/agent/local/Agent.d.ts +1 -34
  2. package/bin/agent/local/Agent.d.ts.map +1 -1
  3. package/bin/agent/local/Agent.js +1 -44
  4. package/bin/agent/local/Agent.js.map +1 -1
  5. package/bin/agent/local/services/AgentAssemblyService.d.ts.map +1 -1
  6. package/bin/agent/local/services/AgentAssemblyService.js +35 -2
  7. package/bin/agent/local/services/AgentAssemblyService.js.map +1 -1
  8. package/bin/agent/local/services/AgentSessions.d.ts +6 -0
  9. package/bin/agent/local/services/AgentSessions.d.ts.map +1 -1
  10. package/bin/agent/local/services/AgentSessions.js +43 -4
  11. package/bin/agent/local/services/AgentSessions.js.map +1 -1
  12. package/bin/agent/remote/RemoteAgent.d.ts +1 -35
  13. package/bin/agent/remote/RemoteAgent.d.ts.map +1 -1
  14. package/bin/agent/remote/RemoteAgent.js +1 -37
  15. package/bin/agent/remote/RemoteAgent.js.map +1 -1
  16. package/bin/agent/remote/RemoteSession.d.ts +26 -41
  17. package/bin/agent/remote/RemoteSession.d.ts.map +1 -1
  18. package/bin/agent/remote/RemoteSession.js +75 -107
  19. package/bin/agent/remote/RemoteSession.js.map +1 -1
  20. package/bin/agent/remote/RemoteTransport.d.ts +15 -27
  21. package/bin/agent/remote/RemoteTransport.d.ts.map +1 -1
  22. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts +10 -21
  23. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -1
  24. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js +24 -46
  25. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js.map +1 -1
  26. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts +10 -20
  27. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -1
  28. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js +10 -16
  29. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js.map +1 -1
  30. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js +2 -2
  31. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js.map +1 -1
  32. package/bin/executor/types/SessionHistoryPaths.d.ts +1 -1
  33. package/bin/executor/types/SessionRecords.d.ts +2 -0
  34. package/bin/executor/types/SessionRecords.d.ts.map +1 -1
  35. package/bin/executor/types/SessionRecords.js.map +1 -1
  36. package/bin/index.d.ts +6 -3
  37. package/bin/index.d.ts.map +1 -1
  38. package/bin/index.js +1 -0
  39. package/bin/index.js.map +1 -1
  40. package/bin/rpc/Client.d.ts +12 -34
  41. package/bin/rpc/Client.d.ts.map +1 -1
  42. package/bin/rpc/Client.js +31 -70
  43. package/bin/rpc/Client.js.map +1 -1
  44. package/bin/session/Session.d.ts +22 -4
  45. package/bin/session/Session.d.ts.map +1 -1
  46. package/bin/session/Session.js +95 -21
  47. package/bin/session/Session.js.map +1 -1
  48. package/bin/session/browse/Browse.d.ts.map +1 -1
  49. package/bin/session/browse/Browse.js +49 -18
  50. package/bin/session/browse/Browse.js.map +1 -1
  51. package/bin/session/recorder/JsonlSessionMessageStore.d.ts +51 -0
  52. package/bin/session/recorder/JsonlSessionMessageStore.d.ts.map +1 -0
  53. package/bin/session/recorder/JsonlSessionMessageStore.js +231 -0
  54. package/bin/session/recorder/JsonlSessionMessageStore.js.map +1 -0
  55. package/bin/session/recorder/SessionMessageCodec.d.ts +20 -0
  56. package/bin/session/recorder/SessionMessageCodec.d.ts.map +1 -0
  57. package/bin/session/recorder/SessionMessageCodec.js +257 -0
  58. package/bin/session/recorder/SessionMessageCodec.js.map +1 -0
  59. package/bin/session/recorder/SessionRecorder.d.ts +186 -0
  60. package/bin/session/recorder/SessionRecorder.d.ts.map +1 -0
  61. package/bin/session/recorder/SessionRecorder.js +655 -0
  62. package/bin/session/recorder/SessionRecorder.js.map +1 -0
  63. package/bin/session/recorder/SessionRecorderCompaction.d.ts +18 -0
  64. package/bin/session/recorder/SessionRecorderCompaction.d.ts.map +1 -0
  65. package/bin/session/recorder/SessionRecorderCompaction.js +142 -0
  66. package/bin/session/recorder/SessionRecorderCompaction.js.map +1 -0
  67. package/bin/session/recorder/SessionRecorderHistoryStore.d.ts +66 -0
  68. package/bin/session/recorder/SessionRecorderHistoryStore.d.ts.map +1 -0
  69. package/bin/session/recorder/SessionRecorderHistoryStore.js +195 -0
  70. package/bin/session/recorder/SessionRecorderHistoryStore.js.map +1 -0
  71. package/bin/session/runtime/SessionEventHub.d.ts +12 -5
  72. package/bin/session/runtime/SessionEventHub.d.ts.map +1 -1
  73. package/bin/session/runtime/SessionEventHub.js +11 -6
  74. package/bin/session/runtime/SessionEventHub.js.map +1 -1
  75. package/bin/session/runtime/SessionPromptRuntime.d.ts +9 -15
  76. package/bin/session/runtime/SessionPromptRuntime.d.ts.map +1 -1
  77. package/bin/session/runtime/SessionPromptRuntime.js +48 -30
  78. package/bin/session/runtime/SessionPromptRuntime.js.map +1 -1
  79. package/bin/session/services/SessionStateService.d.ts +9 -5
  80. package/bin/session/services/SessionStateService.d.ts.map +1 -1
  81. package/bin/session/services/SessionStateService.js +138 -74
  82. package/bin/session/services/SessionStateService.js.map +1 -1
  83. package/bin/session/services/SessionTurnService.d.ts +6 -7
  84. package/bin/session/services/SessionTurnService.d.ts.map +1 -1
  85. package/bin/session/services/SessionTurnService.js +84 -60
  86. package/bin/session/services/SessionTurnService.js.map +1 -1
  87. package/bin/session/services/SessionViewService.d.ts +8 -3
  88. package/bin/session/services/SessionViewService.d.ts.map +1 -1
  89. package/bin/session/services/SessionViewService.js +10 -5
  90. package/bin/session/services/SessionViewService.js.map +1 -1
  91. package/bin/session/storage/Paths.d.ts +1 -1
  92. package/bin/session/storage/Paths.d.ts.map +1 -1
  93. package/bin/session/storage/Paths.js +2 -2
  94. package/bin/session/storage/Paths.js.map +1 -1
  95. package/bin/session/storage/Persistence.d.ts +6 -0
  96. package/bin/session/storage/Persistence.d.ts.map +1 -1
  97. package/bin/session/storage/Persistence.js +7 -0
  98. package/bin/session/storage/Persistence.js.map +1 -1
  99. package/bin/session/storage/RuntimeSessionPort.d.ts +3 -3
  100. package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
  101. package/bin/session/storage/RuntimeSessionPort.js +2 -2
  102. package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
  103. package/bin/types/agent/SessionActor.d.ts +15 -5
  104. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  105. package/bin/types/rpc/RpcProtocol.d.ts +48 -61
  106. package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
  107. package/bin/types/runtime/agent/AgentContext.d.ts +5 -4
  108. package/bin/types/runtime/agent/AgentContext.d.ts.map +1 -1
  109. package/bin/types/runtime/agent/AgentContext.js.map +1 -1
  110. package/bin/types/session/SessionApproval.d.ts +97 -0
  111. package/bin/types/session/SessionApproval.d.ts.map +1 -0
  112. package/bin/types/session/SessionApproval.js +7 -0
  113. package/bin/types/session/SessionApproval.js.map +1 -0
  114. package/bin/types/session/SessionMessage.d.ts +223 -0
  115. package/bin/types/session/SessionMessage.d.ts.map +1 -0
  116. package/bin/types/session/SessionMessage.js +8 -0
  117. package/bin/types/session/SessionMessage.js.map +1 -0
  118. package/bin/types/session/SessionMutation.d.ts +116 -0
  119. package/bin/types/session/SessionMutation.d.ts.map +1 -0
  120. package/bin/types/session/SessionMutation.js +19 -0
  121. package/bin/types/session/SessionMutation.js.map +1 -0
  122. package/bin/types/session/SessionOptions.d.ts +9 -0
  123. package/bin/types/session/SessionOptions.d.ts.map +1 -1
  124. package/package.json +3 -3
  125. package/scripts/agent-env.test.mjs +4 -4
  126. package/scripts/remote-session-reconnect.test.mjs +7 -3
  127. package/scripts/session-action-message.test.mjs +98 -27
  128. package/scripts/session-history-inflight.test.mjs +2 -2
  129. package/scripts/session-list-title.test.mjs +6 -19
  130. package/scripts/session-prompt-runtime.test.mjs +7 -19
  131. package/scripts/session-recorder.test.mjs +199 -0
  132. package/scripts/session-title-event.test.mjs +10 -18
  133. package/src/agent/local/Agent.ts +1 -55
  134. package/src/agent/local/services/AgentAssemblyService.ts +39 -3
  135. package/src/agent/local/services/AgentSessions.ts +48 -3
  136. package/src/agent/remote/RemoteAgent.ts +1 -54
  137. package/src/agent/remote/RemoteSession.ts +106 -136
  138. package/src/agent/remote/RemoteTransport.ts +30 -30
  139. package/src/agent/remote/transports/HttpRemoteAgentTransport.ts +52 -77
  140. package/src/agent/remote/transports/RpcRemoteAgentTransport.ts +30 -34
  141. package/src/executor/store/history/jsonl/JsonlSessionHistoryStore.ts +2 -2
  142. package/src/executor/types/SessionHistoryPaths.ts +1 -1
  143. package/src/executor/types/SessionRecords.ts +2 -0
  144. package/src/index.ts +36 -10
  145. package/src/rpc/Client.ts +58 -95
  146. package/src/session/Session.ts +128 -37
  147. package/src/session/browse/Browse.ts +51 -22
  148. package/src/session/recorder/JsonlSessionMessageStore.ts +246 -0
  149. package/src/session/recorder/SessionMessageCodec.ts +284 -0
  150. package/src/session/recorder/SessionRecorder.ts +871 -0
  151. package/src/session/recorder/SessionRecorderCompaction.ts +188 -0
  152. package/src/session/recorder/SessionRecorderHistoryStore.ts +276 -0
  153. package/src/session/runtime/SessionEventHub.ts +25 -12
  154. package/src/session/runtime/SessionPromptRuntime.ts +60 -43
  155. package/src/session/services/SessionStateService.ts +159 -86
  156. package/src/session/services/SessionTurnService.ts +91 -73
  157. package/src/session/services/SessionViewService.ts +25 -13
  158. package/src/session/storage/Paths.ts +2 -2
  159. package/src/session/storage/Persistence.ts +13 -0
  160. package/src/session/storage/RuntimeSessionPort.ts +8 -9
  161. package/src/types/agent/SessionActor.ts +31 -8
  162. package/src/types/rpc/RpcProtocol.ts +44 -67
  163. package/src/types/runtime/agent/AgentContext.ts +9 -7
  164. package/src/types/session/SessionApproval.ts +108 -0
  165. package/src/types/session/SessionMessage.ts +253 -0
  166. package/src/types/session/SessionMutation.ts +141 -0
  167. package/src/types/session/SessionOptions.ts +25 -0
  168. package/tsconfig.tsbuildinfo +1 -1
  169. package/bin/session/SessionEventMapper.d.ts +0 -32
  170. package/bin/session/SessionEventMapper.d.ts.map +0 -1
  171. package/bin/session/SessionEventMapper.js +0 -131
  172. package/bin/session/SessionEventMapper.js.map +0 -1
  173. package/bin/types/sdk/AgentSessionEvent.d.ts +0 -302
  174. package/bin/types/sdk/AgentSessionEvent.d.ts.map +0 -1
  175. package/bin/types/sdk/AgentSessionEvent.js +0 -9
  176. package/bin/types/sdk/AgentSessionEvent.js.map +0 -1
  177. package/bin/types/sdk/InternalUiChunkEvent.d.ts +0 -100
  178. package/bin/types/sdk/InternalUiChunkEvent.d.ts.map +0 -1
  179. package/bin/types/sdk/InternalUiChunkEvent.js +0 -9
  180. package/bin/types/sdk/InternalUiChunkEvent.js.map +0 -1
  181. package/scripts/session-history-archive.test.mjs +0 -188
  182. package/src/session/SessionEventMapper.ts +0 -148
  183. package/src/types/sdk/AgentSessionEvent.ts +0 -370
  184. package/src/types/sdk/InternalUiChunkEvent.ts +0 -108
@@ -1,188 +0,0 @@
1
- /**
2
- * @file 验证 session.records 的 compact archive 分层读取语义。
3
- *
4
- * 关键点(中文)
5
- * - compact summary 是模型上下文消息,但不应作为用户可见 history item 返回。
6
- * - 调用方通过 previous_archive_id 再传入 archive_id,读取上一层被覆盖历史。
7
- * - history 分页字段使用 snake_case,保持 SDK 对外数据命名一致。
8
- */
9
-
10
- import test from "node:test";
11
- import assert from "node:assert/strict";
12
- import os from "node:os";
13
- import path from "node:path";
14
- import fs from "node:fs/promises";
15
-
16
- import { Agent } from "../bin/index.js";
17
-
18
- function user_message(session_id, index) {
19
- return {
20
- id: `u:${index}`,
21
- role: "user",
22
- metadata: {
23
- v: 1,
24
- ts: index,
25
- sessionId: session_id,
26
- source: "ingress",
27
- },
28
- parts: [{ type: "text", text: `user ${index}` }],
29
- };
30
- }
31
-
32
- function assistant_message(session_id, index) {
33
- return {
34
- id: `a:${index}`,
35
- role: "assistant",
36
- metadata: {
37
- v: 1,
38
- ts: index,
39
- sessionId: session_id,
40
- source: "egress",
41
- },
42
- parts: [{ type: "text", text: `assistant ${index}` }],
43
- };
44
- }
45
-
46
- function compact_summary(session_id, archive_id, text) {
47
- return {
48
- id: `summary:${archive_id}`,
49
- role: "assistant",
50
- metadata: {
51
- v: 1,
52
- ts: 1000,
53
- sessionId: session_id,
54
- source: "compact",
55
- kind: "summary",
56
- archiveId: archive_id,
57
- sourceRange: {
58
- fromId: "start",
59
- toId: "end",
60
- count: 2,
61
- },
62
- },
63
- parts: [{ type: "text", text }],
64
- };
65
- }
66
-
67
- async function write_jsonl(file_path, messages) {
68
- await fs.mkdir(path.dirname(file_path), { recursive: true });
69
- await fs.writeFile(
70
- file_path,
71
- messages.map((message) => JSON.stringify(message)).join("\n") + "\n",
72
- "utf8",
73
- );
74
- }
75
-
76
- async function write_archive(file_path, session_id, messages) {
77
- await fs.mkdir(path.dirname(file_path), { recursive: true });
78
- await fs.writeFile(
79
- file_path,
80
- JSON.stringify(
81
- {
82
- v: 1,
83
- sessionId: session_id,
84
- archivedAt: Date.now(),
85
- messages,
86
- },
87
- null,
88
- 2,
89
- ),
90
- "utf8",
91
- );
92
- }
93
-
94
- test("session.records hides compact summary and reads archive layers by archive_id", async () => {
95
- const agent_path = await fs.mkdtemp(
96
- path.join(os.tmpdir(), "downcity-agent-history-archive-"),
97
- );
98
- const agent = new Agent({
99
- id: "history_archive_agent",
100
- path: agent_path,
101
- });
102
- const session = await agent.sessions.create({
103
- sessionId: "history_archive_session",
104
- });
105
-
106
- const messages_dir = path.join(
107
- agent_path,
108
- ".downcity",
109
- "agents",
110
- encodeURIComponent("history_archive_agent"),
111
- "sessions",
112
- encodeURIComponent(session.id),
113
- "messages",
114
- );
115
- const messages_path = path.join(messages_dir, "messages.jsonl");
116
- const archive_dir = path.join(messages_dir, "archive");
117
-
118
- try {
119
- await write_jsonl(messages_path, [
120
- compact_summary(session.id, "compact-B", "summary B"),
121
- user_message(session.id, 31),
122
- assistant_message(session.id, 32),
123
- ]);
124
- await write_archive(path.join(archive_dir, "compact-B.json"), session.id, [
125
- compact_summary(session.id, "compact-A", "summary A"),
126
- user_message(session.id, 16),
127
- assistant_message(session.id, 17),
128
- ]);
129
- await write_archive(path.join(archive_dir, "compact-A.json"), session.id, [
130
- user_message(session.id, 1),
131
- assistant_message(session.id, 2),
132
- ]);
133
-
134
- const current_page = await session.records({
135
- limit: 1,
136
- });
137
- assert.equal(current_page.total, 2);
138
- assert.equal(current_page.has_more, true);
139
- assert.equal(current_page.next_cursor, "1");
140
- assert.equal(current_page.archive_id, undefined);
141
- assert.equal(current_page.previous_archive_id, "compact-B");
142
- assert.deepEqual(
143
- current_page.items.map((message) => message.id),
144
- ["u:31"],
145
- );
146
-
147
- const next_current_page = await session.records({
148
- cursor: current_page.next_cursor,
149
- });
150
- assert.equal(next_current_page.has_more, false);
151
- assert.deepEqual(
152
- next_current_page.items.map((message) => message.id),
153
- ["a:32"],
154
- );
155
-
156
- const previous_page = await session.records({
157
- archive_id: current_page.previous_archive_id,
158
- });
159
- assert.equal(previous_page.archive_id, "compact-B");
160
- assert.equal(previous_page.previous_archive_id, "compact-A");
161
- assert.equal(previous_page.total, 2);
162
- assert.deepEqual(
163
- previous_page.items.map((message) => message.id),
164
- ["u:16", "a:17"],
165
- );
166
-
167
- const oldest_page = await session.records({
168
- archive_id: previous_page.previous_archive_id,
169
- });
170
- assert.equal(oldest_page.archive_id, "compact-A");
171
- assert.equal(oldest_page.previous_archive_id, undefined);
172
- assert.deepEqual(
173
- oldest_page.items.map((message) => message.id),
174
- ["u:1", "a:2"],
175
- );
176
-
177
- const timeline_page = await session.records({
178
- view: "timeline",
179
- archive_id: "compact-B",
180
- });
181
- assert.deepEqual(
182
- timeline_page.items.map((event) => event.text),
183
- ["user 16", "assistant 17"],
184
- );
185
- } finally {
186
- await agent.dispose();
187
- }
188
- });
@@ -1,148 +0,0 @@
1
- /**
2
- * Session 事件映射辅助。
3
- *
4
- * 关键点(中文)
5
- * - 把底层 AI SDK `UIMessageChunk` 归一到内部 `InternalUiChunkEvent`。
6
- * - 再把内部 chunk 事件转换为 `session.subscribe()` 可见的 Session 事件。
7
- */
8
-
9
- import type { UIMessageChunk } from "ai";
10
- import type { JsonValue } from "@/types/common/Json.js";
11
- import type { AgentSessionEvent } from "@/types/sdk/AgentSessionEvent.js";
12
- import type { InternalUiChunkEvent } from "@/types/sdk/InternalUiChunkEvent.js";
13
-
14
- function toJsonValue(value: unknown): JsonValue {
15
- if (value === undefined) return null;
16
- if (value === null) return null;
17
- if (
18
- typeof value === "string" ||
19
- typeof value === "number" ||
20
- typeof value === "boolean"
21
- ) {
22
- return value;
23
- }
24
- try {
25
- return JSON.parse(JSON.stringify(value)) as JsonValue;
26
- } catch {
27
- return String(value);
28
- }
29
- }
30
-
31
- /**
32
- * 把单个 UI chunk 映射为内部 chunk 事件。
33
- */
34
- export function mapUiMessageChunkToAgentEvent(
35
- chunk: UIMessageChunk,
36
- ): InternalUiChunkEvent | null {
37
- switch (chunk.type) {
38
- case "text-delta":
39
- return {
40
- type: "text-delta",
41
- text: chunk.delta,
42
- };
43
- case "reasoning-delta":
44
- return {
45
- type: "reasoning-delta",
46
- text: chunk.delta,
47
- };
48
- case "tool-input-available":
49
- return {
50
- type: "tool-call",
51
- toolCallId: chunk.toolCallId,
52
- toolName: chunk.toolName,
53
- args: toJsonValue(chunk.input),
54
- };
55
- case "tool-output-available":
56
- return {
57
- type: "tool-result",
58
- toolCallId: chunk.toolCallId,
59
- toolName: "unknown",
60
- result: toJsonValue(chunk.output),
61
- };
62
- case "tool-output-error":
63
- return {
64
- type: "tool-error",
65
- toolCallId: chunk.toolCallId,
66
- toolName: "unknown",
67
- error: chunk.errorText,
68
- };
69
- case "tool-input-error":
70
- return {
71
- type: "tool-error",
72
- toolCallId: chunk.toolCallId,
73
- toolName: chunk.toolName,
74
- error: chunk.errorText,
75
- };
76
- case "finish":
77
- return {
78
- type: "finish",
79
- ...(typeof chunk.finishReason === "string"
80
- ? { finishReason: chunk.finishReason }
81
- : {}),
82
- };
83
- case "error":
84
- return {
85
- type: "error",
86
- error: chunk.errorText,
87
- };
88
- case "abort":
89
- return {
90
- type: "error",
91
- error: String(chunk.reason || "execution aborted"),
92
- };
93
- default:
94
- return null;
95
- }
96
- }
97
-
98
- /**
99
- * 把内部 chunk 事件映射为 Session actor 事件。
100
- *
101
- * 关键点(中文)
102
- * - Session 级事件只保留 turn 维度所需的最小字段。
103
- * - `finish` 与通用 `error` 由 turn 生命周期统一表达,不在这里直接透出。
104
- */
105
- export function mapAgentEventToSessionEvent(params: {
106
- /**
107
- * 当前内部 chunk 事件。
108
- */
109
- event: InternalUiChunkEvent;
110
- /**
111
- * 当前 turn 标识。
112
- */
113
- turnId: string;
114
- }): AgentSessionEvent | null {
115
- const { event, turnId } = params;
116
- switch (event.type) {
117
- case "text-delta":
118
- return {
119
- type: "text-delta",
120
- turnId,
121
- text: event.text,
122
- };
123
- case "reasoning-delta":
124
- return {
125
- type: "reasoning-delta",
126
- turnId,
127
- text: event.text,
128
- };
129
- case "tool-call":
130
- return {
131
- type: "tool-call",
132
- turnId,
133
- toolCallId: event.toolCallId,
134
- toolName: event.toolName,
135
- args: event.args,
136
- };
137
- case "tool-result":
138
- return {
139
- type: "tool-result",
140
- turnId,
141
- toolCallId: event.toolCallId,
142
- toolName: event.toolName,
143
- result: event.result,
144
- };
145
- default:
146
- return null;
147
- }
148
- }
@@ -1,370 +0,0 @@
1
- /**
2
- * Agent Session 事件类型定义。
3
- *
4
- * 关键点(中文)
5
- * - `subscribe()` 暴露的是 session 级长期事件序列,而不是单次执行的局部结果。
6
- * - 事件只表达“当前 turn 正在发生什么”,不负责历史回放。
7
- */
8
-
9
- import type { JsonValue } from "@/types/common/Json.js";
10
- import type { SessionAssistantStepVisibility } from "@/executor/types/SessionRun.js";
11
- import type { AgentSessionActionEvent } from "@/types/sdk/AgentSessionAction.js";
12
-
13
- /**
14
- * 单个 turn 开始事件。
15
- */
16
- export interface AgentSessionTurnStartEvent {
17
- /**
18
- * 当前事件类型。
19
- */
20
- type: "turn-start";
21
-
22
- /**
23
- * 当前 turn 的稳定标识。
24
- *
25
- * 说明(中文)
26
- * - 同一个 turn 内的全部增量事件都会复用这个 turnId。
27
- * - 当 Session 排队进入下一轮时,会生成新的 turnId。
28
- */
29
- turnId: string;
30
- }
31
-
32
- /**
33
- * 普通文本增量事件。
34
- */
35
- export interface AgentSessionTextDeltaEvent {
36
- /**
37
- * 当前事件类型。
38
- */
39
- type: "text-delta";
40
-
41
- /**
42
- * 当前文本所属 turn。
43
- */
44
- turnId: string;
45
-
46
- /**
47
- * 当前这次新增的可见文本片段。
48
- */
49
- text: string;
50
- }
51
-
52
- /**
53
- * reasoning 文本增量事件。
54
- */
55
- export interface AgentSessionReasoningDeltaEvent {
56
- /**
57
- * 当前事件类型。
58
- */
59
- type: "reasoning-delta";
60
-
61
- /**
62
- * 当前 reasoning 所属 turn。
63
- */
64
- turnId: string;
65
-
66
- /**
67
- * 当前这次新增的 reasoning 文本片段。
68
- */
69
- text: string;
70
- }
71
-
72
- /**
73
- * 工具调用事件。
74
- */
75
- export interface AgentSessionToolCallEvent {
76
- /**
77
- * 当前事件类型。
78
- */
79
- type: "tool-call";
80
-
81
- /**
82
- * 当前工具调用所属 turn。
83
- */
84
- turnId: string;
85
-
86
- /**
87
- * 当前工具调用唯一标识。
88
- */
89
- toolCallId: string;
90
-
91
- /**
92
- * 当前工具名称。
93
- */
94
- toolName: "shell_exec" | "shell_session" | "shell_write" | string;
95
-
96
- /**
97
- * 当前工具输入参数。
98
- */
99
- args: JsonValue;
100
- }
101
-
102
- /**
103
- * 工具结果事件。
104
- */
105
- export interface AgentSessionToolResultEvent {
106
- /**
107
- * 当前事件类型。
108
- */
109
- type: "tool-result";
110
-
111
- /**
112
- * 当前工具结果所属 turn。
113
- */
114
- turnId: string;
115
-
116
- /**
117
- * 当前工具调用唯一标识。
118
- */
119
- toolCallId: string;
120
-
121
- /**
122
- * 当前工具名称。
123
- */
124
- toolName: "shell_exec" | "shell_session" | "shell_write" | string;
125
-
126
- /**
127
- * 当前工具输出结果。
128
- */
129
- result: JsonValue;
130
- }
131
-
132
- /**
133
- * 工具审批请求事件。
134
- */
135
- export interface AgentSessionToolApprovalRequestEvent {
136
- /**
137
- * 当前事件类型。
138
- */
139
- type: "tool-approval-request";
140
-
141
- /**
142
- * 当前审批所属 turn。
143
- */
144
- turnId: string;
145
-
146
- /**
147
- * 当前工具调用唯一标识。
148
- */
149
- toolCallId: string;
150
-
151
- /**
152
- * 当前工具名称。
153
- */
154
- toolName: "shell_exec" | "shell_session" | "shell_write" | string;
155
-
156
- /**
157
- * 当前审批请求唯一标识。
158
- */
159
- approvalId: string;
160
-
161
- /**
162
- * 请求执行的 sandbox 模式。
163
- */
164
- sandbox: "unrestricted";
165
-
166
- /**
167
- * 请求执行的命令文本。
168
- *
169
- * 说明(中文)
170
- * - `shell_exec` / `shell_session` start 中是命令文本。
171
- * - `shell_write` 中是 stdin 写入内容,用于兼容内部审批展示。
172
- */
173
- cmd: string;
174
-
175
- /**
176
- * 当前审批动作类型。
177
- */
178
- operation?: "exec" | "start" | "write";
179
-
180
- /**
181
- * 关联 shell_id;session 输入审批时用于标识目标会话。
182
- */
183
- shellId?: string;
184
-
185
- /**
186
- * stdin 写入内容预览;仅 session 输入审批存在。
187
- */
188
- inputPreview?: string;
189
-
190
- /**
191
- * stdin 写入字符数;仅 session 输入审批存在。
192
- */
193
- inputChars?: number;
194
-
195
- /**
196
- * 命令执行目录。
197
- */
198
- cwd: string;
199
-
200
- /**
201
- * Agent 给用户展示的申请原因。
202
- */
203
- reason: string;
204
-
205
- /**
206
- * 当前审批状态。
207
- */
208
- status: "pending";
209
- }
210
-
211
- /**
212
- * 工具审批结果事件。
213
- */
214
- export interface AgentSessionToolApprovalResultEvent {
215
- /**
216
- * 当前事件类型。
217
- */
218
- type: "tool-approval-result";
219
-
220
- /**
221
- * 当前审批所属 turn。
222
- */
223
- turnId: string;
224
-
225
- /**
226
- * 当前工具调用唯一标识。
227
- */
228
- toolCallId: string;
229
-
230
- /**
231
- * 当前工具名称。
232
- */
233
- toolName: "shell_exec" | "shell_session" | "shell_write" | string;
234
-
235
- /**
236
- * 当前审批请求唯一标识。
237
- */
238
- approvalId: string;
239
-
240
- /**
241
- * 用户最终决策。
242
- */
243
- decision: "approved" | "denied" | "expired";
244
- }
245
-
246
- /**
247
- * assistant step 完成事件。
248
- */
249
- export interface AgentSessionAssistantStepEvent {
250
- /**
251
- * 当前事件类型。
252
- */
253
- type: "assistant-step";
254
-
255
- /**
256
- * 当前 assistant step 所属 turn。
257
- */
258
- turnId: string;
259
-
260
- /**
261
- * 当前 step 文本。
262
- */
263
- text: string;
264
-
265
- /**
266
- * 当前 step 序号(从 1 开始)。
267
- */
268
- stepIndex: number;
269
-
270
- /**
271
- * 当前 step 可见性。
272
- */
273
- visibility?: SessionAssistantStepVisibility;
274
- }
275
-
276
- /**
277
- * Session 标题更新事件。
278
- */
279
- export interface AgentSessionTitleEvent {
280
- /**
281
- * 当前事件类型。
282
- */
283
- type: "session-title";
284
-
285
- /**
286
- * 当前 session 唯一标识。
287
- */
288
- sessionId: string;
289
-
290
- /**
291
- * 当前 session 最新标题。
292
- *
293
- * 说明(中文)
294
- * - 标题已持久化到 session meta。
295
- * - 新 session 通常在首条 user message 落盘后生成标题。
296
- */
297
- title: string;
298
- }
299
-
300
- /**
301
- * 单个 turn 完成事件。
302
- */
303
- export interface AgentSessionTurnFinishEvent {
304
- /**
305
- * 当前事件类型。
306
- */
307
- type: "turn-finish";
308
-
309
- /**
310
- * 当前已完成 turn 的稳定标识。
311
- */
312
- turnId: string;
313
-
314
- /**
315
- * 当前 turn 最终可见文本。
316
- */
317
- text: string;
318
-
319
- /**
320
- * 当前 turn 是否成功结束。
321
- */
322
- success: boolean;
323
-
324
- /**
325
- * 当前 turn 失败时的错误文本。
326
- */
327
- error?: string;
328
- }
329
-
330
- /**
331
- * Session 级错误事件。
332
- */
333
- export interface AgentSessionErrorEvent {
334
- /**
335
- * 当前事件类型。
336
- */
337
- type: "error";
338
-
339
- /**
340
- * 当前错误文本。
341
- */
342
- message: string;
343
- }
344
-
345
- /**
346
- * Session 订阅可见事件联合类型。
347
- */
348
- export type AgentSessionEvent =
349
- | AgentSessionTurnStartEvent
350
- | AgentSessionTextDeltaEvent
351
- | AgentSessionReasoningDeltaEvent
352
- | AgentSessionToolCallEvent
353
- | AgentSessionToolResultEvent
354
- | AgentSessionToolApprovalRequestEvent
355
- | AgentSessionToolApprovalResultEvent
356
- | AgentSessionAssistantStepEvent
357
- | AgentSessionTitleEvent
358
- | AgentSessionActionEvent
359
- | AgentSessionTurnFinishEvent
360
- | AgentSessionErrorEvent;
361
-
362
- /**
363
- * Session 事件订阅回调。
364
- */
365
- export type AgentSessionSubscriber = (event: AgentSessionEvent) => void;
366
-
367
- /**
368
- * 取消 Session 订阅的返回函数。
369
- */
370
- export type AgentSessionUnsubscribe = () => void;