@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
@@ -29,10 +29,10 @@ import type { SessionHistoryStore } from "@/executor/store/history/SessionHistor
29
29
  import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
30
30
  import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
31
31
  import type {
32
- AgentSessionEvent,
33
- AgentSessionSubscriber,
34
- AgentSessionUnsubscribe,
35
- } from "@/types/sdk/AgentSessionEvent.js";
32
+ SessionMutationSubscriber,
33
+ SessionMutationUnsubscribe,
34
+ } from "@/types/session/SessionMutation.js";
35
+ import type { SessionApprovalRuntimeEvent } from "@/types/session/SessionApproval.js";
36
36
  import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
37
37
 
38
38
  /**
@@ -133,9 +133,11 @@ export interface SessionPort {
133
133
  *
134
134
  * 关键点(中文)
135
135
  * - 只广播订阅之后产生的事件。
136
- * - session records 仍通过 `getHistoryStore()` / SDK `records()` 读取。
136
+ * - 当前 Message snapshot 通过公开 SDK `session.messages()` 读取。
137
137
  */
138
- subscribe(subscriber: AgentSessionSubscriber): AgentSessionUnsubscribe;
138
+ subscribe(
139
+ subscriber: SessionMutationSubscriber,
140
+ ): SessionMutationUnsubscribe;
139
141
  /**
140
142
  * 发布一条 session runtime 事件。
141
143
  *
@@ -143,7 +145,7 @@ export interface SessionPort {
143
145
  * - plugin runtime 用它把审批、外部进度等非模型 chunk 事件推送给订阅方。
144
146
  * - 历史消息持久化仍由 append_user_message / append_assistant_message 负责。
145
147
  */
146
- publishEvent(event: AgentSessionEvent): void;
148
+ publishEvent(event: SessionApprovalRuntimeEvent): Promise<void>;
147
149
  /**
148
150
  * 追加一条 user 消息。
149
151
  */
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Session 级工具审批类型。
3
+ *
4
+ * 审批由具体工具运行时执行,但归属、查询和决策能力统一通过 Session 暴露。
5
+ */
6
+
7
+ /** Session 工具审批的最终人工决策。 */
8
+ export type SessionApprovalDecision = "approved" | "denied";
9
+
10
+ /** Session 工具审批模式。 */
11
+ export type SessionApprovalMode = "ask" | "always-allow";
12
+
13
+ /** 当前 Session 的待审批工具请求。 */
14
+ export interface SessionApproval {
15
+ /** 当前审批请求的稳定唯一标识。 */
16
+ approval_id: string;
17
+ /** 当前审批所属 Session 标识。 */
18
+ session_id: string;
19
+ /** 当前审批所属 Turn 标识。 */
20
+ turn_id?: string;
21
+ /** 当前审批对应的工具调用标识。 */
22
+ tool_call_id?: string;
23
+ /** 当前审批对应的工具注册名称。 */
24
+ tool_name: string;
25
+ /** 请求执行的命令或输入文本。 */
26
+ command: string;
27
+ /** 当前工具执行工作目录。 */
28
+ cwd: string;
29
+ /** 工具申请 unrestricted 权限的原因。 */
30
+ reason: string;
31
+ /** 当前审批对应的工具操作类型。 */
32
+ operation: "exec" | "start" | "write";
33
+ /** 当前审批创建时间戳(ms)。 */
34
+ created_at: number;
35
+ }
36
+
37
+ /** 提交 Session 工具审批决策的参数。 */
38
+ export interface ResolveSessionApprovalInput {
39
+ /** 需要处理的审批请求标识。 */
40
+ approval_id: string;
41
+ /** 对当前审批请求作出的最终决定。 */
42
+ decision: SessionApprovalDecision;
43
+ }
44
+
45
+ /** Session 工具审批决策结果。 */
46
+ export interface SessionApprovalResult {
47
+ /** 是否成功命中并处理 pending 审批。 */
48
+ success: boolean;
49
+ /** 已尝试处理的审批请求标识。 */
50
+ approval_id: string;
51
+ /** 已提交的最终审批决定。 */
52
+ decision: SessionApprovalDecision;
53
+ }
54
+
55
+ /** 当前 Session 的审批模式快照。 */
56
+ export interface SessionApprovalModeSnapshot {
57
+ /** 当前审批模式所属 Session 标识。 */
58
+ session_id: string;
59
+ /** 当前 Session 生效的审批模式。 */
60
+ mode: SessionApprovalMode;
61
+ }
62
+
63
+ /** 更新当前 Session 审批模式的参数。 */
64
+ export interface SetSessionApprovalModeInput {
65
+ /** 需要设置的新审批模式。 */
66
+ mode: SessionApprovalMode;
67
+ }
68
+
69
+ /** Shell Runtime 发给 Session 的工具审批请求。 */
70
+ export interface SessionToolApprovalRequest {
71
+ /** Runtime 事件类型固定为工具审批请求。 */
72
+ type: "tool-approval-request";
73
+ /** 当前审批所属 Turn 标识。 */
74
+ turn_id: string;
75
+ /** 当前审批对应的工具调用标识。 */
76
+ tool_call_id: string;
77
+ /** 当前审批对应的工具注册名称。 */
78
+ tool_name: string;
79
+ /** 当前审批请求的稳定唯一标识。 */
80
+ approval_id: string;
81
+ /** 请求执行的命令或输入文本。 */
82
+ command: string;
83
+ /** 当前工具执行工作目录。 */
84
+ cwd: string;
85
+ /** 工具申请 unrestricted 权限的原因。 */
86
+ reason: string;
87
+ /** 当前审批对应的工具操作类型。 */
88
+ operation: "exec" | "start" | "write";
89
+ }
90
+
91
+ /** Shell Runtime 发给 Session 的工具审批结果。 */
92
+ export interface SessionToolApprovalResult {
93
+ /** Runtime 事件类型固定为工具审批结果。 */
94
+ type: "tool-approval-result";
95
+ /** 当前审批所属 Turn 标识。 */
96
+ turn_id: string;
97
+ /** 当前审批对应的工具调用标识。 */
98
+ tool_call_id: string;
99
+ /** 当前审批请求的稳定唯一标识。 */
100
+ approval_id: string;
101
+ /** 当前审批的最终结果,超时由 Runtime 标记为 expired。 */
102
+ decision: SessionApprovalDecision | "expired";
103
+ }
104
+
105
+ /** Session 内部接收的工具审批 Runtime 事件。 */
106
+ export type SessionApprovalRuntimeEvent =
107
+ | SessionToolApprovalRequest
108
+ | SessionToolApprovalResult;
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Session canonical 消息类型。
3
+ *
4
+ * Session 只维护一条由 sequence 排序的消息序列;assistant 的 text、reasoning、tool、file
5
+ * 均为内部 part,不提升为顶层消息。
6
+ */
7
+
8
+ import type { JsonObject, JsonValue } from "@/types/common/Json.js";
9
+
10
+ /** Message 默认展示范围。 */
11
+ export type SessionMessageVisibility = "visible" | "internal";
12
+
13
+ /** 从其他 Session 导入时保留的来源身份。 */
14
+ export interface SessionMessageOrigin {
15
+ /** 来源 Session 标识。 */
16
+ session_id: string;
17
+ /** 来源 Message 标识。 */
18
+ message_id: string;
19
+ /** 来源 turn 标识。 */
20
+ turn_id?: string;
21
+ }
22
+
23
+ /** Session Message 公共字段。 */
24
+ export interface SessionMessageBase {
25
+ /** 当前 Message 在 Session 内的稳定唯一标识。 */
26
+ message_id: string;
27
+ /** 当前 Message 所属 Session 标识。 */
28
+ session_id: string;
29
+ /** 当前 Message 所属 turn;独立 action 可以省略。 */
30
+ turn_id?: string;
31
+ /** Message 的线性位置;创建后永远不变。 */
32
+ sequence: number;
33
+ /** Message 版本号;每次对外发布的消息变化后递增。 */
34
+ revision: number;
35
+ /** 当前 Message 是否默认对用户可见。 */
36
+ visibility: SessionMessageVisibility;
37
+ /** Message 首次创建时间戳(ms)。 */
38
+ created_at: number;
39
+ /** Message 最近更新时间戳(ms)。 */
40
+ updated_at: number;
41
+ /** 从其他 Session 导入时的来源信息。 */
42
+ origin?: SessionMessageOrigin;
43
+ }
44
+
45
+ /** User 文本 part。 */
46
+ export interface SessionUserTextPart {
47
+ /** User Message 内稳定的 part 标识。 */
48
+ part_id: string;
49
+ /** part 类型固定为 text。 */
50
+ type: "text";
51
+ /** 用户输入文本。 */
52
+ text: string;
53
+ /** User 文本已经完整,不参与流式更新。 */
54
+ state: "done";
55
+ }
56
+
57
+ /** User 文件 part。 */
58
+ export interface SessionUserFilePart {
59
+ /** User Message 内稳定的 part 标识。 */
60
+ part_id: string;
61
+ /** part 类型固定为 file。 */
62
+ type: "file";
63
+ /** 文件可读取地址或 data URL。 */
64
+ url: string;
65
+ /** 文件 MIME 类型。 */
66
+ media_type: string;
67
+ /** 可选原始文件名。 */
68
+ filename?: string;
69
+ }
70
+
71
+ /** User 结构化数据 part。 */
72
+ export interface SessionUserDataPart {
73
+ /** User Message 内稳定的 part 标识。 */
74
+ part_id: string;
75
+ /** part 类型固定为 data。 */
76
+ type: "data";
77
+ /** 对应 AI SDK 的 data-* 类型名称。 */
78
+ data_type: `data-${string}` | string;
79
+ /** 可 JSON 序列化的数据。 */
80
+ data: JsonValue;
81
+ }
82
+
83
+ /** User Message part。 */
84
+ export type SessionUserMessagePart =
85
+ | SessionUserTextPart
86
+ | SessionUserFilePart
87
+ | SessionUserDataPart;
88
+
89
+ /** User 顶层 Message。 */
90
+ export interface SessionUserMessage extends SessionMessageBase {
91
+ /** Message 类型固定为 user。 */
92
+ type: "user";
93
+ /** 普通 prompt 或当前 turn 中的 steering 输入。 */
94
+ input_type: "prompt" | "steer";
95
+ /** 用户消息的结构化 parts。 */
96
+ parts: SessionUserMessagePart[];
97
+ }
98
+
99
+ /** Assistant 文本或推理 part。 */
100
+ export interface SessionAssistantTextPart {
101
+ /** Assistant Message 内稳定的 part 标识。 */
102
+ part_id: string;
103
+ /** Assistant Part 在当前 Message 中的不可变线性顺序,从 1 开始。 */
104
+ sequence: number;
105
+ /** part 是可见文本或推理文本。 */
106
+ type: "text" | "reasoning";
107
+ /** 当前已经累计的完整文本。 */
108
+ text: string;
109
+ /** 文本 part 是否已经结束。 */
110
+ state: "streaming" | "done";
111
+ }
112
+
113
+ /** Assistant 工具 part。 */
114
+ export interface SessionAssistantToolPart {
115
+ /** Assistant Message 内稳定的 part 标识。 */
116
+ part_id: string;
117
+ /** Assistant Part 在当前 Message 中的不可变线性顺序,从 1 开始。 */
118
+ sequence: number;
119
+ /** part 类型固定为 tool。 */
120
+ type: "tool";
121
+ /** 模型工具调用稳定标识。 */
122
+ tool_call_id: string;
123
+ /** 工具注册名称。 */
124
+ tool_name: string;
125
+ /** 工具当前生命周期状态。 */
126
+ state: "input-streaming" | "ready" | "approval-required" | "running" | "completed" | "failed";
127
+ /** 流式接收中的参数原文。 */
128
+ input_text?: string;
129
+ /** 收敛后的结构化输入。 */
130
+ input?: JsonValue;
131
+ /** 工具成功输出。 */
132
+ output?: JsonValue;
133
+ /** 工具失败信息。 */
134
+ error?: string;
135
+ /** 工具审批请求标识。 */
136
+ approval_id?: string;
137
+ }
138
+
139
+ /** Assistant 文件 part。 */
140
+ export interface SessionAssistantFilePart {
141
+ /** Assistant Message 内稳定的 part 标识。 */
142
+ part_id: string;
143
+ /** Assistant Part 在当前 Message 中的不可变线性顺序,从 1 开始。 */
144
+ sequence: number;
145
+ /** part 类型固定为 file。 */
146
+ type: "file";
147
+ /** 文件 MIME 类型。 */
148
+ media_type: string;
149
+ /** 文件可读取地址或 data URL。 */
150
+ url: string;
151
+ /** 可选原始文件名。 */
152
+ filename?: string;
153
+ }
154
+
155
+ /** Assistant 结构化数据 part。 */
156
+ export interface SessionAssistantDataPart {
157
+ /** Assistant Message 内稳定的 part 标识。 */
158
+ part_id: string;
159
+ /** Assistant Part 在当前 Message 中的不可变线性顺序,从 1 开始。 */
160
+ sequence: number;
161
+ /** part 类型固定为 data。 */
162
+ type: "data";
163
+ /** 对应 AI SDK 的 data-* 类型名称。 */
164
+ data_type: `data-${string}` | string;
165
+ /** 可 JSON 序列化的数据。 */
166
+ data: JsonValue;
167
+ }
168
+
169
+ /** Assistant Message part。 */
170
+ export type SessionAssistantMessagePart =
171
+ | SessionAssistantTextPart
172
+ | SessionAssistantToolPart
173
+ | SessionAssistantFilePart
174
+ | SessionAssistantDataPart;
175
+
176
+ /** Assistant 顶层 Message。 */
177
+ export interface SessionAssistantMessage extends SessionMessageBase {
178
+ /** Message 类型固定为 assistant。 */
179
+ type: "assistant";
180
+ /** 普通 assistant segment 或内部 compact summary。 */
181
+ kind: "normal" | "summary";
182
+ /** Assistant 在所属 turn 内的 segment 序号,从一开始。 */
183
+ segment_index: number;
184
+ /** Assistant 当前执行状态。 */
185
+ status: "streaming" | "completed" | "stopped" | "failed";
186
+ /** Assistant 内按真实生成顺序保存的 parts。 */
187
+ parts: SessionAssistantMessagePart[];
188
+ /** Summary 已覆盖到的来源 Message 标识。 */
189
+ summary_through_message_id?: string;
190
+ }
191
+
192
+ /** Action 顶层 Message。 */
193
+ export interface SessionActionMessage extends SessionMessageBase {
194
+ /** Message 类型固定为 action。 */
195
+ type: "action";
196
+ /** Action 业务类型。 */
197
+ action_type: string;
198
+ /** Action 当前状态。 */
199
+ status: "running" | "completed" | "failed";
200
+ /** Action 展示标题。 */
201
+ title: string;
202
+ /** Action 展示描述。 */
203
+ description?: string;
204
+ /** Action 附加结构化信息。 */
205
+ data?: JsonObject;
206
+ }
207
+
208
+ /** Error 顶层 Message。 */
209
+ export interface SessionErrorMessage extends SessionMessageBase {
210
+ /** Message 类型固定为 error。 */
211
+ type: "error";
212
+ /** 错误影响范围。 */
213
+ scope: "session" | "turn";
214
+ /** 稳定错误码。 */
215
+ code: string;
216
+ /** 用户可见错误信息。 */
217
+ message: string;
218
+ /** 当前错误是否允许重试恢复。 */
219
+ recoverable: boolean;
220
+ }
221
+
222
+ /** Session 唯一顶层 Message 联合类型。 */
223
+ export type SessionMessage =
224
+ | SessionUserMessage
225
+ | SessionAssistantMessage
226
+ | SessionActionMessage
227
+ | SessionErrorMessage;
228
+
229
+ /** 读取 Session Message snapshot 的分页输入。 */
230
+ export interface ListSessionMessagesInput {
231
+ /** 单页数量上限。 */
232
+ limit?: number;
233
+ /** 上一页返回的透明游标。 */
234
+ cursor?: string;
235
+ /** 只返回 sequence 严格小于该值的 Message,供向前翻页。 */
236
+ before_sequence?: number;
237
+ /** 是否包含 internal Message。 */
238
+ include_internal?: boolean;
239
+ /** 只返回不大于该 sequence 的 Message。 */
240
+ through_sequence?: number;
241
+ }
242
+
243
+ /** Session Message snapshot 分页结果。 */
244
+ export interface SessionMessagePage {
245
+ /** 当前页按 sequence 升序排列的 Message。 */
246
+ items: SessionMessage[];
247
+ /** 当前过滤条件下的 Message 总数。 */
248
+ total: number;
249
+ /** 下一页透明游标。 */
250
+ next_cursor?: string;
251
+ /** 是否仍有更多 Message。 */
252
+ has_more: boolean;
253
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Session 的统一实时 Mutation 协议。
3
+ *
4
+ * Mutation 描述订阅之后发生的状态变化,不等同于 messages.jsonl 持久化格式。
5
+ */
6
+
7
+ import type { SessionAssistantMessagePart, SessionMessage } from "@/types/session/SessionMessage.js";
8
+ import type { ResolveSessionApprovalInput, SessionApprovalResult } from "@/types/session/SessionApproval.js";
9
+
10
+ /** 所有 Session Mutation 的公共字段。 */
11
+ export interface SessionMutationBase {
12
+ /** 当前 Mutation 的稳定唯一标识。 */
13
+ mutation_id: string;
14
+ /** 当前 Mutation 所属 Session 标识。 */
15
+ session_id: string;
16
+ /** 当前 Mutation 创建时间戳(ms)。 */
17
+ created_at: number;
18
+ }
19
+ /** Message 创建或完整快照更新 Mutation。 */
20
+ export type SessionMessageMutation = {
21
+ [TType in SessionMessage["type"]]: SessionMutationBase & {
22
+ /** Mutation 层级固定为 message。 */
23
+ variant: "message";
24
+ /** 当前完整 Message 类型。 */
25
+ type: TType;
26
+ /** 当前 Mutation 目标 Message 标识。 */
27
+ message_id: string;
28
+ /** 当前 Mutation 所属 Turn 标识。 */
29
+ turn_id?: string;
30
+ /** 当前 Message 在线性 Session 中的不可变顺序。 */
31
+ sequence: number;
32
+ /** 应用当前 Mutation 后的 Message revision。 */
33
+ revision: number;
34
+ /** 创建或更新后的完整 Message 快照。 */
35
+ message: Extract<SessionMessage, { type: TType }>;
36
+ };
37
+ }[SessionMessage["type"]];
38
+
39
+ /** Assistant Part 创建或完整快照更新 Mutation。 */
40
+ export type SessionPartMutation = {
41
+ [TType in SessionAssistantMessagePart["type"]]: SessionMutationBase & {
42
+ /** Mutation 层级固定为 part。 */
43
+ variant: "part";
44
+ /** 当前完整 Part 类型。 */
45
+ type: TType;
46
+ /** 当前 Part 所属 Message 标识。 */
47
+ message_id: string;
48
+ /** 当前 Mutation 所属 Turn 标识。 */
49
+ turn_id?: string;
50
+ /** 应用当前 Mutation 后的 Message revision。 */
51
+ revision: number;
52
+ /** 当前被创建或更新的 Part 标识。 */
53
+ part_id: string;
54
+ /** 创建或更新后的完整 Part 快照。 */
55
+ part: SessionAssistantMessagePart & { type: TType };
56
+ };
57
+ }[SessionAssistantMessagePart["type"]];
58
+
59
+ /** Assistant 文本或推理的原始增量 Mutation。 */
60
+ export interface SessionDeltaMutation extends SessionMutationBase {
61
+ /** Mutation 层级固定为 delta。 */
62
+ variant: "delta";
63
+ /** Delta 只允许可见文本或推理文本。 */
64
+ type: "text" | "reasoning";
65
+ /** 当前 Delta 所属 Message 标识。 */
66
+ message_id: string;
67
+ /** 当前 Mutation 所属 Turn 标识。 */
68
+ turn_id?: string;
69
+ /** 应用当前 Mutation 后的 Message revision。 */
70
+ revision: number;
71
+ /** 当前 Delta 所属 Part 标识。 */
72
+ part_id: string;
73
+ /** 本次新增的原始文本,不是累计全文。 */
74
+ delta: string;
75
+ }
76
+
77
+ /** Turn 生命周期 Mutation。 */
78
+ export type SessionTurnMutation = SessionMutationBase & {
79
+ /** Mutation 层级固定为 turn。 */
80
+ variant: "turn";
81
+ /** 当前 Turn 是开始还是完成。 */
82
+ type: "start" | "finish";
83
+ /** 当前 Turn 的稳定唯一标识。 */
84
+ turn_id: string;
85
+ /** 当前 Turn 的执行状态。 */
86
+ status: "running" | "completed" | "failed" | "stopped";
87
+ /** Turn 完成时的最终可见文本。 */
88
+ text?: string;
89
+ /** Turn 失败或停止时的错误文本。 */
90
+ error?: string;
91
+ };
92
+
93
+ /** Session 自身属性变化 Mutation。 */
94
+ export type SessionStateMutation = SessionMutationBase & {
95
+ /** Mutation 层级固定为 session。 */
96
+ variant: "session";
97
+ /** 当前 Session 属性变化类型。 */
98
+ type: "title";
99
+ /** 当前 Session 最新标题。 */
100
+ title: string;
101
+ };
102
+
103
+ /** Session 对外唯一实时 Mutation 联合类型。 */
104
+ export type SessionMutation =
105
+ | SessionMessageMutation
106
+ | SessionPartMutation
107
+ | SessionDeltaMutation
108
+ | SessionTurnMutation
109
+ | SessionStateMutation;
110
+
111
+ /** 当前 Mutation 可用的命令回复入口。 */
112
+ export interface SessionMutationReply {
113
+ /**
114
+ * 回复当前 Tool Part 的审批请求。
115
+ * 只有 approval-required Tool Part Mutation 可以成功调用。
116
+ */
117
+ approval(input: Pick<ResolveSessionApprovalInput, "decision">): Promise<SessionApprovalResult>;
118
+ }
119
+
120
+ /** Session Mutation 订阅回调。 */
121
+ export type SessionMutationSubscriber = (
122
+ mutation: SessionMutation,
123
+ reply: SessionMutationReply,
124
+ ) => void | Promise<void>;
125
+
126
+ /** 取消 Session Mutation 订阅的函数。 */
127
+ export type SessionMutationUnsubscribe = () => void;
128
+
129
+ /** 判断未知事件是否为 Session Mutation。 */
130
+ export function is_session_mutation(input: unknown): input is SessionMutation {
131
+ if (!input || typeof input !== "object") return false;
132
+ const candidate = input as { mutation_id?: unknown; variant?: unknown; type?: unknown };
133
+ if (typeof candidate.mutation_id !== "string" || typeof candidate.type !== "string") return false;
134
+ return (
135
+ candidate.variant === "message" ||
136
+ candidate.variant === "part" ||
137
+ candidate.variant === "delta" ||
138
+ candidate.variant === "turn" ||
139
+ candidate.variant === "session"
140
+ );
141
+ }
@@ -14,6 +14,13 @@ import type { AgentSessionSystemBlock } from "@/types/agent/SessionTypes.js";
14
14
  import type { SessionComposerOptions } from "@/types/session/SessionComposerOptions.js";
15
15
  import type { Logger } from "@/utils/logger/Logger.js";
16
16
  import type { AgentModel } from "@/model/CityModelAdapter.js";
17
+ import type {
18
+ ResolveSessionApprovalInput,
19
+ SessionApproval,
20
+ SessionApprovalModeSnapshot,
21
+ SessionApprovalResult,
22
+ SetSessionApprovalModeInput,
23
+ } from "@/types/session/SessionApproval.js";
17
24
 
18
25
  /**
19
26
  * Agent 可管理的本地 Session 实例。
@@ -87,6 +94,24 @@ export interface SessionOptions {
87
94
  /** 按稳定模型 ID 解析当前 Session 的运行时模型。 */
88
95
  resolve_model?: (model_id: string) => Promise<AgentModel>;
89
96
 
97
+ /** 列出当前 Session 的 pending 工具审批。 */
98
+ list_approvals: (session_id: string) => Promise<SessionApproval[]>;
99
+
100
+ /** 读取当前 Session 的工具审批模式。 */
101
+ get_approval_mode: (session_id: string) => Promise<SessionApprovalModeSnapshot>;
102
+
103
+ /** 更新当前 Session 的工具审批模式。 */
104
+ set_approval_mode: (
105
+ session_id: string,
106
+ input: SetSessionApprovalModeInput,
107
+ ) => Promise<SessionApprovalModeSnapshot>;
108
+
109
+ /** 处理当前 Session 的 pending 工具审批。 */
110
+ resolve_approval: (
111
+ session_id: string,
112
+ input: ResolveSessionApprovalInput,
113
+ ) => Promise<SessionApprovalResult>;
114
+
90
115
  /**
91
116
  * 当前 session 的 Composer 覆盖项。
92
117
  */