@downcity/agent 1.1.199 → 1.1.202

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 (247) hide show
  1. package/bin/agent/local/Agent.d.ts +3 -7
  2. package/bin/agent/local/Agent.d.ts.map +1 -1
  3. package/bin/agent/local/Agent.js +14 -19
  4. package/bin/agent/local/Agent.js.map +1 -1
  5. package/bin/agent/local/services/{AgentSessionManager.d.ts → AgentSessions.d.ts} +12 -17
  6. package/bin/agent/local/services/AgentSessions.d.ts.map +1 -0
  7. package/bin/agent/local/services/{AgentSessionManager.js → AgentSessions.js} +11 -26
  8. package/bin/agent/local/services/AgentSessions.js.map +1 -0
  9. package/bin/agent/remote/RemoteAgent.d.ts +2 -9
  10. package/bin/agent/remote/RemoteAgent.d.ts.map +1 -1
  11. package/bin/agent/remote/RemoteAgent.js +40 -22
  12. package/bin/agent/remote/RemoteAgent.js.map +1 -1
  13. package/bin/agent/remote/RemoteSession.d.ts +5 -5
  14. package/bin/agent/remote/RemoteSession.d.ts.map +1 -1
  15. package/bin/agent/remote/RemoteSession.js +5 -3
  16. package/bin/agent/remote/RemoteSession.js.map +1 -1
  17. package/bin/agent/remote/RemoteTransport.d.ts +2 -2
  18. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts +2 -2
  19. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js +5 -5
  20. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts +2 -2
  21. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js +2 -2
  22. package/bin/executor/Executor.d.ts +7 -7
  23. package/bin/executor/Executor.d.ts.map +1 -1
  24. package/bin/executor/Executor.js +41 -33
  25. package/bin/executor/Executor.js.map +1 -1
  26. package/bin/executor/SessionRunScope.d.ts +1 -1
  27. package/bin/executor/SessionRunScope.d.ts.map +1 -1
  28. package/bin/executor/composer/compaction/SessionCompactionComposer.d.ts +5 -5
  29. package/bin/executor/composer/compaction/SessionCompactionComposer.d.ts.map +1 -1
  30. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.js +1 -1
  31. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.js.map +1 -1
  32. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.d.ts +4 -5
  33. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.d.ts.map +1 -1
  34. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.js +73 -40
  35. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.js.map +1 -1
  36. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.d.ts +27 -0
  37. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.d.ts.map +1 -0
  38. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js +181 -0
  39. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js.map +1 -0
  40. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts +45 -0
  41. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts.map +1 -0
  42. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js +144 -0
  43. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js.map +1 -0
  44. package/bin/executor/composer/context/LocalSessionContextComposer.d.ts +3 -3
  45. package/bin/executor/composer/context/LocalSessionContextComposer.d.ts.map +1 -1
  46. package/bin/executor/composer/context/SessionContextComposer.d.ts +3 -3
  47. package/bin/executor/composer/context/SessionContextComposer.d.ts.map +1 -1
  48. package/bin/executor/composer/history/SessionHistoryComposer.d.ts +2 -2
  49. package/bin/executor/composer/history/SessionHistoryComposer.d.ts.map +1 -1
  50. package/bin/executor/composer/history/SessionHistoryWriter.d.ts +5 -5
  51. package/bin/executor/composer/history/SessionHistoryWriter.d.ts.map +1 -1
  52. package/bin/executor/composer/history/SessionHistoryWriter.js +6 -6
  53. package/bin/executor/composer/history/SessionHistoryWriter.js.map +1 -1
  54. package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.d.ts +2 -2
  55. package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.d.ts.map +1 -1
  56. package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.js +5 -4
  57. package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.js.map +1 -1
  58. package/bin/executor/core-engine/CoreEngineMessageState.d.ts +5 -5
  59. package/bin/executor/core-engine/CoreEngineMessageState.d.ts.map +1 -1
  60. package/bin/executor/core-engine/CoreEngineMessageState.js.map +1 -1
  61. package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
  62. package/bin/executor/core-engine/CoreEngineSignals.d.ts +4 -4
  63. package/bin/executor/core-engine/CoreEngineSignals.d.ts.map +1 -1
  64. package/bin/executor/core-engine/CoreEngineSignals.js.map +1 -1
  65. package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts +3 -3
  66. package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts.map +1 -1
  67. package/bin/executor/core-engine/CoreEngineUiStreamCollector.js.map +1 -1
  68. package/bin/executor/messages/AssistantResultPersistence.d.ts +5 -5
  69. package/bin/executor/messages/AssistantResultPersistence.d.ts.map +1 -1
  70. package/bin/executor/messages/AssistantResultPersistence.js +1 -1
  71. package/bin/executor/messages/AssistantResultPersistence.js.map +1 -1
  72. package/bin/executor/messages/SessionAttachmentMapper.d.ts +3 -3
  73. package/bin/executor/messages/SessionAttachmentMapper.d.ts.map +1 -1
  74. package/bin/executor/messages/SessionAttachmentMapper.js.map +1 -1
  75. package/bin/executor/messages/SessionMessageCodec.d.ts +4 -4
  76. package/bin/executor/messages/SessionMessageCodec.d.ts.map +1 -1
  77. package/bin/executor/messages/SessionMessageCodec.js +5 -5
  78. package/bin/executor/messages/SessionMessageCodec.js.map +1 -1
  79. package/bin/executor/messages/SessionMessageLog.d.ts +3 -3
  80. package/bin/executor/messages/SessionMessageLog.d.ts.map +1 -1
  81. package/bin/executor/messages/SessionMessageLog.js.map +1 -1
  82. package/bin/executor/messages/SessionStepEventMapper.d.ts +2 -2
  83. package/bin/executor/messages/SessionStepEventMapper.d.ts.map +1 -1
  84. package/bin/executor/messages/SessionStepEventMapper.js.map +1 -1
  85. package/bin/executor/messages/UserVisibleText.d.ts +3 -3
  86. package/bin/executor/messages/UserVisibleText.d.ts.map +1 -1
  87. package/bin/executor/messages/UserVisibleText.js +3 -3
  88. package/bin/executor/messages/UserVisibleText.js.map +1 -1
  89. package/bin/executor/services/ExecutorInflightService.d.ts +2 -2
  90. package/bin/executor/services/ExecutorInflightService.d.ts.map +1 -1
  91. package/bin/executor/services/ExecutorInflightService.js +11 -7
  92. package/bin/executor/services/ExecutorInflightService.js.map +1 -1
  93. package/bin/executor/store/history/SessionHistoryStore.d.ts +30 -27
  94. package/bin/executor/store/history/SessionHistoryStore.d.ts.map +1 -1
  95. package/bin/executor/store/history/SessionHistoryStore.js +3 -3
  96. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts +21 -20
  97. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts.map +1 -1
  98. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js +114 -72
  99. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js.map +1 -1
  100. package/bin/executor/tools/plugin/PluginToolDefinition.d.ts +1 -7
  101. package/bin/executor/tools/plugin/PluginToolDefinition.d.ts.map +1 -1
  102. package/bin/executor/tools/plugin/PluginToolSchemas.d.ts +4 -6
  103. package/bin/executor/tools/plugin/PluginToolSchemas.d.ts.map +1 -1
  104. package/bin/executor/tools/plugin/PluginToolSchemas.js +24 -10
  105. package/bin/executor/tools/plugin/PluginToolSchemas.js.map +1 -1
  106. package/bin/executor/types/SessionRecords.d.ts +162 -0
  107. package/bin/executor/types/SessionRecords.d.ts.map +1 -0
  108. package/bin/executor/types/SessionRecords.js +52 -0
  109. package/bin/executor/types/SessionRecords.js.map +1 -0
  110. package/bin/executor/types/SessionRun.d.ts +3 -3
  111. package/bin/executor/types/SessionRun.d.ts.map +1 -1
  112. package/bin/index.d.ts +2 -2
  113. package/bin/index.d.ts.map +1 -1
  114. package/bin/index.js.map +1 -1
  115. package/bin/rpc/Client.d.ts +5 -5
  116. package/bin/rpc/Client.js +4 -4
  117. package/bin/session/Session.d.ts +6 -6
  118. package/bin/session/Session.d.ts.map +1 -1
  119. package/bin/session/Session.js +8 -8
  120. package/bin/session/Session.js.map +1 -1
  121. package/bin/session/SessionTitle.d.ts +2 -2
  122. package/bin/session/SessionTitle.d.ts.map +1 -1
  123. package/bin/session/SessionTitle.js +5 -0
  124. package/bin/session/SessionTitle.js.map +1 -1
  125. package/bin/session/browse/Browse.d.ts +12 -12
  126. package/bin/session/browse/Browse.d.ts.map +1 -1
  127. package/bin/session/browse/Browse.js +37 -34
  128. package/bin/session/browse/Browse.js.map +1 -1
  129. package/bin/session/index.d.ts +1 -1
  130. package/bin/session/index.js +1 -1
  131. package/bin/session/runtime/SessionPromptRuntime.d.ts +3 -3
  132. package/bin/session/runtime/SessionPromptRuntime.d.ts.map +1 -1
  133. package/bin/session/services/SessionStateService.d.ts +11 -12
  134. package/bin/session/services/SessionStateService.d.ts.map +1 -1
  135. package/bin/session/services/SessionStateService.js +43 -69
  136. package/bin/session/services/SessionStateService.js.map +1 -1
  137. package/bin/session/services/SessionTurnService.js +3 -3
  138. package/bin/session/services/SessionTurnService.js.map +1 -1
  139. package/bin/session/services/SessionViewService.d.ts +4 -4
  140. package/bin/session/services/SessionViewService.d.ts.map +1 -1
  141. package/bin/session/services/SessionViewService.js +26 -36
  142. package/bin/session/services/SessionViewService.js.map +1 -1
  143. package/bin/session/storage/Persistence.d.ts +4 -4
  144. package/bin/session/storage/Persistence.d.ts.map +1 -1
  145. package/bin/session/storage/RuntimeSessionPort.d.ts +2 -2
  146. package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
  147. package/bin/session/storage/RuntimeSessionPort.js +4 -4
  148. package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
  149. package/bin/types/agent/AgentTypes.d.ts +2 -2
  150. package/bin/types/agent/AgentTypes.d.ts.map +1 -1
  151. package/bin/types/agent/SessionActor.d.ts +11 -11
  152. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  153. package/bin/types/agent/SessionTypes.d.ts +22 -22
  154. package/bin/types/agent/SessionTypes.d.ts.map +1 -1
  155. package/bin/types/executor/SessionRunContext.d.ts +7 -7
  156. package/bin/types/executor/SessionRunContext.d.ts.map +1 -1
  157. package/bin/types/rpc/RpcProtocol.d.ts +6 -6
  158. package/bin/types/runtime/agent/AgentContext.d.ts +7 -7
  159. package/bin/types/runtime/agent/AgentContext.d.ts.map +1 -1
  160. package/bin/types/sdk/AgentSessionAction.d.ts +26 -0
  161. package/bin/types/sdk/AgentSessionAction.d.ts.map +1 -0
  162. package/bin/types/sdk/AgentSessionAction.js +10 -0
  163. package/bin/types/sdk/AgentSessionAction.js.map +1 -0
  164. package/bin/types/sdk/AgentSessionEvent.d.ts +2 -2
  165. package/bin/types/sdk/AgentSessionEvent.d.ts.map +1 -1
  166. package/bin/types/sdk/AgentSessionTurn.d.ts +2 -2
  167. package/bin/types/sdk/AgentSessionTurn.d.ts.map +1 -1
  168. package/package.json +3 -3
  169. package/scripts/agent-ready.test.mjs +1 -1
  170. package/scripts/city-model-tool-loop.test.mjs +36 -4
  171. package/scripts/plugin-tool-bridge.test.mjs +13 -0
  172. package/scripts/session-action-message.test.mjs +199 -0
  173. package/scripts/session-compaction-prompts.test.mjs +97 -0
  174. package/scripts/session-history-archive.test.mjs +8 -8
  175. package/scripts/session-history-inflight.test.mjs +10 -10
  176. package/scripts/session-list-title.test.mjs +9 -9
  177. package/scripts/session-title-event.test.mjs +12 -10
  178. package/src/agent/local/Agent.ts +16 -22
  179. package/src/agent/local/services/{AgentSessionManager.ts → AgentSessions.ts} +16 -32
  180. package/src/agent/remote/RemoteAgent.ts +58 -23
  181. package/src/agent/remote/RemoteSession.ts +10 -8
  182. package/src/agent/remote/RemoteTransport.ts +5 -5
  183. package/src/agent/remote/transports/HttpRemoteAgentTransport.ts +10 -10
  184. package/src/agent/remote/transports/RpcRemoteAgentTransport.ts +6 -6
  185. package/src/executor/Executor.ts +51 -41
  186. package/src/executor/SessionRunScope.ts +1 -1
  187. package/src/executor/composer/compaction/SessionCompactionComposer.ts +5 -5
  188. package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.ts +1 -1
  189. package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.ts +91 -52
  190. package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.ts +197 -0
  191. package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.ts +162 -0
  192. package/src/executor/composer/context/LocalSessionContextComposer.ts +5 -5
  193. package/src/executor/composer/context/SessionContextComposer.ts +5 -5
  194. package/src/executor/composer/history/SessionHistoryComposer.ts +2 -2
  195. package/src/executor/composer/history/SessionHistoryWriter.ts +10 -10
  196. package/src/executor/composer/history/jsonl/JsonlSessionHistoryComposer.ts +13 -10
  197. package/src/executor/core-engine/CoreEngineMessageState.ts +8 -8
  198. package/src/executor/core-engine/CoreEngineRunner.ts +7 -7
  199. package/src/executor/core-engine/CoreEngineSignals.ts +7 -7
  200. package/src/executor/core-engine/CoreEngineUiStreamCollector.ts +5 -5
  201. package/src/executor/messages/AssistantResultPersistence.ts +8 -8
  202. package/src/executor/messages/SessionAttachmentMapper.ts +11 -11
  203. package/src/executor/messages/SessionMessageCodec.ts +10 -10
  204. package/src/executor/messages/SessionMessageLog.ts +3 -3
  205. package/src/executor/messages/SessionStepEventMapper.ts +5 -5
  206. package/src/executor/messages/UserVisibleText.ts +9 -9
  207. package/src/executor/services/ExecutorInflightService.ts +13 -9
  208. package/src/executor/store/history/SessionHistoryStore.ts +32 -28
  209. package/src/executor/store/history/jsonl/JsonlSessionHistoryStore.ts +146 -95
  210. package/src/executor/tools/plugin/PluginToolSchemas.ts +24 -10
  211. package/src/executor/types/SessionRecords.ts +217 -0
  212. package/src/executor/types/SessionRun.ts +5 -5
  213. package/src/index.ts +9 -10
  214. package/src/rpc/Client.ts +9 -9
  215. package/src/session/Session.ts +10 -10
  216. package/src/session/SessionTitle.ts +7 -4
  217. package/src/session/browse/Browse.ts +66 -58
  218. package/src/session/index.ts +1 -1
  219. package/src/session/runtime/SessionPromptRuntime.ts +6 -6
  220. package/src/session/services/SessionStateService.ts +61 -86
  221. package/src/session/services/SessionTurnService.ts +7 -7
  222. package/src/session/services/SessionViewService.ts +35 -45
  223. package/src/session/storage/Persistence.ts +6 -6
  224. package/src/session/storage/RuntimeSessionPort.ts +6 -6
  225. package/src/types/agent/AgentTypes.ts +4 -4
  226. package/src/types/agent/SessionActor.ts +12 -12
  227. package/src/types/agent/SessionTypes.ts +22 -22
  228. package/src/types/executor/SessionRunContext.ts +12 -12
  229. package/src/types/rpc/RpcProtocol.ts +6 -6
  230. package/src/types/runtime/agent/AgentContext.ts +8 -8
  231. package/src/types/sdk/AgentSessionAction.ts +35 -0
  232. package/src/types/sdk/AgentSessionEvent.ts +2 -2
  233. package/src/types/sdk/AgentSessionTurn.ts +2 -2
  234. package/tsconfig.tsbuildinfo +1 -1
  235. package/bin/agent/local/services/AgentSessionManager.d.ts.map +0 -1
  236. package/bin/agent/local/services/AgentSessionManager.js.map +0 -1
  237. package/bin/executor/types/SessionMessages.d.ts +0 -118
  238. package/bin/executor/types/SessionMessages.d.ts.map +0 -1
  239. package/bin/executor/types/SessionMessages.js +0 -28
  240. package/bin/executor/types/SessionMessages.js.map +0 -1
  241. package/bin/types/sdk/AgentSessionOperation.d.ts +0 -99
  242. package/bin/types/sdk/AgentSessionOperation.d.ts.map +0 -1
  243. package/bin/types/sdk/AgentSessionOperation.js +0 -10
  244. package/bin/types/sdk/AgentSessionOperation.js.map +0 -1
  245. package/scripts/session-operation-message.test.mjs +0 -197
  246. package/src/executor/types/SessionMessages.ts +0 -148
  247. package/src/types/sdk/AgentSessionOperation.ts +0 -125
@@ -0,0 +1,217 @@
1
+ /**
2
+ * SessionRecord 类型定义。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这里统一描述 session JSONL 中的持久化 record 结构。
6
+ * - record 是 session 内部事实源;其中 message record 继续使用 AI SDK `UIMessage`。
7
+ * - action record 是 UI record,不属于 LLM 输入。
8
+ * - 这些类型会被 Store、Composer、compact、control UI、task runtime 共同复用。
9
+ */
10
+
11
+ import type { UIMessage } from "ai";
12
+ import type { JsonObject } from "@/types/common/Json.js";
13
+
14
+ /**
15
+ * Session message record 类别。
16
+ *
17
+ * 关键点(中文)
18
+ * - 持久化存储在 `.downcity/agents/<encodedAgentId>/sessions/<encodedSessionId>/messages/messages.jsonl`
19
+ * - record 文件默认包含 message record,或 `type=action` 的 UI 状态记录。
20
+ * - compact 会把更早 message record 压缩为一条 `assistant` 摘要 record。
21
+ * - action 只用于 UI timeline,不进入 LLM 输入
22
+ */
23
+ export type SessionMessageKind = "normal" | "summary";
24
+
25
+ /**
26
+ * Session 消息来源类型。
27
+ *
28
+ * 说明(中文)
29
+ * - `ingress`:外部输入写入的 user 消息。
30
+ * - `egress`:模型或 agent 输出写入的 assistant 消息。
31
+ * - `compact`:由 compact 过程生成的摘要消息。
32
+ */
33
+ export type SessionMessageSource =
34
+ | "ingress"
35
+ | "egress"
36
+ | "compact";
37
+
38
+ /**
39
+ * 入站消息细分类型。
40
+ *
41
+ * 说明(中文)
42
+ * - 当前仅保留 `exec`,表示这条入站消息可触发一次 session 执行。
43
+ */
44
+ export type SessionIngressKind = "exec";
45
+
46
+ /**
47
+ * 摘要消息对应的原始消息范围。
48
+ */
49
+ export type SessionMessageSourceRangeV1 = {
50
+ /** 起始消息 ID。 */
51
+ fromId: string;
52
+ /** 结束消息 ID。 */
53
+ toId: string;
54
+ /** 被覆盖的原始消息数量。 */
55
+ count: number;
56
+ };
57
+
58
+ /**
59
+ * Session 消息元信息。
60
+ */
61
+ export type SessionMetadataV1 = {
62
+ /** 元信息 schema 版本号。 */
63
+ v: 1;
64
+ /** 当前消息写入时的毫秒时间戳。 */
65
+ ts: number;
66
+ /** 当前消息所属的 session ID。 */
67
+ sessionId: string;
68
+ /** 当前消息是普通消息还是摘要消息。 */
69
+ kind?: SessionMessageKind;
70
+ /** 当前消息来自入站、出站还是 compact。 */
71
+ source?: SessionMessageSource;
72
+ /** compact 摘要对应的 archive 文件 ID,用于用户历史按层读取更早消息。 */
73
+ archiveId?: string;
74
+ /** compact 摘要所覆盖的原始消息范围。 */
75
+ sourceRange?: SessionMessageSourceRangeV1;
76
+ /**
77
+ * 扩展元信息。
78
+ *
79
+ * 约定(中文)
80
+ * - 对 `source=ingress` 的 user 消息,可选写入 `extra.ingressKind`:
81
+ * - `exec`:可触发 Session 执行的输入
82
+ */
83
+ extra?: JsonObject;
84
+ };
85
+
86
+ /**
87
+ * 大模型可消费的 Session message record。
88
+ */
89
+ export type SessionMessageRecordV1 = UIMessage<SessionMetadataV1>;
90
+
91
+ /**
92
+ * Session action 当前状态。
93
+ */
94
+ export type SessionActionStateV1 = "running" | "completed" | "failed";
95
+
96
+ /**
97
+ * action record 元信息。
98
+ */
99
+ export type SessionActionMetadataV1 = {
100
+ /** 元信息 schema 版本号。 */
101
+ v: 1;
102
+ /** 当前 action 写入时的毫秒时间戳。 */
103
+ ts: number;
104
+ /** 当前 action 所属的 session ID。 */
105
+ sessionId: string;
106
+ /** 当前 action 关联的 turn 标识。 */
107
+ turnId?: string;
108
+ };
109
+
110
+ /**
111
+ * action 类型的 Session record 结构。
112
+ *
113
+ * 关键点(中文)
114
+ * - `action` 不是 AI SDK 原生 message record。
115
+ * - 它只存在于 session records 与前端 timeline,进入 LLM 前必须过滤。
116
+ */
117
+ export type SessionActionRecordV1 = {
118
+ /** record 类型固定为 `action`。 */
119
+ type: "action";
120
+ /** 同一个 action 生命周期内稳定复用的 ID。 */
121
+ id: string;
122
+ /** 当前 action 标题。 */
123
+ title: string;
124
+ /** 当前 action 描述。 */
125
+ description?: string;
126
+ /** 当前 action 状态。 */
127
+ state: SessionActionStateV1;
128
+ /** action 元信息。 */
129
+ metadata: SessionActionMetadataV1;
130
+ };
131
+
132
+ /**
133
+ * 构造 action record 的输入结构。
134
+ */
135
+ export type SessionActionRecordInputV1 = {
136
+ /** 同一个 action 生命周期内稳定复用的 ID。 */
137
+ id?: string;
138
+ /** 当前 action 标题。 */
139
+ title: string;
140
+ /** 当前 action 描述。 */
141
+ description?: string;
142
+ /** 当前 action 状态。 */
143
+ state: SessionActionStateV1;
144
+ /** 当前 action 关联的 turn 标识。 */
145
+ turnId?: string;
146
+ /** 可选 action 元信息覆盖。 */
147
+ metadata?: Partial<SessionActionMetadataV1>;
148
+ };
149
+
150
+ /**
151
+ * Session 持久化 record 结构。
152
+ *
153
+ * 说明(中文)
154
+ * - message record 是可进入 LLM 的 record。
155
+ * - action record 是只给前端展示的 record,组装模型输入前必须过滤。
156
+ */
157
+ export type SessionRecordV1 = SessionMessageRecordV1 | SessionActionRecordV1;
158
+
159
+ /**
160
+ * user 角色的 Session message record。
161
+ */
162
+ export type SessionUserMessageV1 = SessionMessageRecordV1 & {
163
+ /** 消息角色固定为 `user`。 */
164
+ role: "user";
165
+ };
166
+
167
+ /**
168
+ * 判断一条 record 是否为 action record。
169
+ */
170
+ export function is_session_action_record(
171
+ message: SessionRecordV1 | null | undefined,
172
+ ): message is SessionActionRecordV1 {
173
+ if (!message || typeof message !== "object") return false;
174
+ return (message as { type?: unknown }).type === "action";
175
+ }
176
+
177
+ /**
178
+ * 从订阅事件构造 action record。
179
+ */
180
+ export function to_session_action_record(
181
+ action: SessionActionRecordInputV1 | SessionActionRecordV1,
182
+ session_id: string,
183
+ ): SessionActionRecordV1 {
184
+ const title = String(action.title || "").trim() || "Action";
185
+ const description = String(action.description || "").trim();
186
+ const id = String(action.id || "").trim() || `action:${session_id}:${Date.now()}`;
187
+ const metadata =
188
+ "metadata" in action && action.metadata && typeof action.metadata === "object"
189
+ ? action.metadata
190
+ : {};
191
+ const turn_id =
192
+ String(metadata.turnId || "").trim() ||
193
+ ("turnId" in action ? String(action.turnId || "").trim() : "");
194
+ return {
195
+ type: "action",
196
+ id,
197
+ title,
198
+ ...(description ? { description } : {}),
199
+ state: action.state,
200
+ metadata: {
201
+ v: 1,
202
+ ts: typeof metadata.ts === "number" ? metadata.ts : Date.now(),
203
+ sessionId: String(metadata.sessionId || "").trim() || session_id,
204
+ ...(turn_id ? { turnId: turn_id } : {}),
205
+ },
206
+ };
207
+ }
208
+
209
+ /**
210
+ * 判断一条 record 是否为大模型可消费的 message record。
211
+ */
212
+ export function is_session_message_record(
213
+ message: SessionRecordV1 | null | undefined,
214
+ ): message is SessionMessageRecordV1 {
215
+ if (!message || typeof message !== "object") return false;
216
+ return !is_session_action_record(message);
217
+ }
@@ -9,10 +9,10 @@
9
9
 
10
10
  import type { Tool, UIMessageChunk } from "ai";
11
11
  import type {
12
- SessionMessageV1,
13
- SessionModelMessageV1,
12
+ SessionRecordV1,
13
+ SessionMessageRecordV1,
14
14
  SessionUserMessageV1,
15
- } from "@/executor/types/SessionMessages.js";
15
+ } from "@/executor/types/SessionRecords.js";
16
16
  import type { SessionSystemMessage } from "@/executor/types/SessionPrompts.js";
17
17
  import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
18
18
 
@@ -102,7 +102,7 @@ export interface SessionRunResult {
102
102
  * - stop/abort 且没有任何 assistant 内容时可以为空。
103
103
  * - turn 状态通过 `success` / `error` 表达,不应伪造成 assistant 正文。
104
104
  */
105
- assistantMessage?: SessionModelMessageV1 | null;
105
+ assistantMessage?: SessionMessageRecordV1 | null;
106
106
 
107
107
  /**
108
108
  * 本轮执行结束后待写入长期历史的 user 消息。
@@ -150,7 +150,7 @@ export interface SessionExecuteInput {
150
150
  /**
151
151
  * 当前轮 context 语义消息历史。
152
152
  */
153
- messages: SessionMessageV1[];
153
+ messages: SessionRecordV1[];
154
154
 
155
155
  /**
156
156
  * 当前轮可用工具集合。
package/src/index.ts CHANGED
@@ -22,7 +22,7 @@ export type {
22
22
  AgentArchiveSessionResult,
23
23
  AgentArchiveSessionsResult,
24
24
  AgentCleanArchiveResult,
25
- AgentSessionCollection,
25
+ AgentSessions,
26
26
  AgentModel,
27
27
  AgentManagedSession,
28
28
  AgentSessionActor,
@@ -34,9 +34,9 @@ export type {
34
34
  RemoteAgentOptions,
35
35
  AgentSessionConfigSnapshot,
36
36
  AgentSessionForkInput,
37
- AgentSessionHistoryInput,
38
- AgentSessionHistoryPage,
39
- AgentSessionHistoryView,
37
+ AgentSessionRecordsInput,
38
+ AgentSessionRecordsPage,
39
+ AgentSessionRecordsView,
40
40
  AgentSessionInfo,
41
41
  AgentSessionSetInput,
42
42
  AgentSessionSummary,
@@ -56,12 +56,11 @@ export type {
56
56
  AgentSessionUnsubscribe,
57
57
  } from "./types/sdk/AgentSessionEvent.js";
58
58
  export type {
59
- AgentSessionOperationCallback,
60
- AgentSessionOperationEvent,
61
- AgentSessionOperationName,
62
- AgentSessionOperationRecord,
63
- AgentSessionOperationStatus,
64
- } from "./types/sdk/AgentSessionOperation.js";
59
+ AgentSessionActionCallback,
60
+ AgentSessionActionEvent,
61
+ AgentSessionActionRecord,
62
+ AgentSessionActionState,
63
+ } from "./types/sdk/AgentSessionAction.js";
65
64
  export type { AgentSessionPromptInput, SessionUserMessagePart } from "./types/sdk/AgentSessionPrompt.js";
66
65
  export type { AgentSessionStopResult } from "./types/sdk/AgentSessionStop.js";
67
66
  export type {
package/src/rpc/Client.ts CHANGED
@@ -16,8 +16,8 @@ import type {
16
16
  AgentArchiveSessionResult,
17
17
  AgentArchiveSessionsResult,
18
18
  AgentCleanArchiveResult,
19
- AgentSessionHistoryInput,
20
- AgentSessionHistoryPage,
19
+ AgentSessionRecordsInput,
20
+ AgentSessionRecordsPage,
21
21
  AgentSessionInfo,
22
22
  AgentSessionStopResult,
23
23
  AgentSessionSummaryPage,
@@ -196,20 +196,20 @@ export class RpcClient {
196
196
  }
197
197
 
198
198
  /**
199
- * 读取 history
199
+ * 读取 session records
200
200
  */
201
- async get_session_history(params: {
201
+ async get_session_records(params: {
202
202
  session_id: string;
203
- input?: AgentSessionHistoryInput;
204
- }): Promise<AgentSessionHistoryPage> {
205
- const data = await this.request<{ history: AgentSessionHistoryPage }>({
206
- method: "sdk.sessions.history",
203
+ input?: AgentSessionRecordsInput;
204
+ }): Promise<AgentSessionRecordsPage> {
205
+ const data = await this.request<{ records: AgentSessionRecordsPage }>({
206
+ method: "sdk.sessions.records",
207
207
  params: {
208
208
  sessionId: params.session_id,
209
209
  input: params.input,
210
210
  },
211
211
  });
212
- return data.history;
212
+ return data.records;
213
213
  }
214
214
 
215
215
  /**
@@ -15,8 +15,8 @@ import type {
15
15
  AgentSession,
16
16
  AgentSessionConfigSnapshot,
17
17
  AgentSessionForkInput,
18
- AgentSessionHistoryInput,
19
- AgentSessionHistoryPage,
18
+ AgentSessionRecordsInput,
19
+ AgentSessionRecordsPage,
20
20
  AgentSessionInfo,
21
21
  AgentSessionSetInput,
22
22
  AgentSessionSystemBlock,
@@ -207,7 +207,7 @@ export class Session implements AgentSession {
207
207
  /**
208
208
  * 追加一条 user 文本消息。
209
209
  */
210
- async appendUserMessage(input: {
210
+ async append_user_message(input: {
211
211
  text: string;
212
212
  }): Promise<void> {
213
213
  await this.stateService.append_user_message({
@@ -218,7 +218,7 @@ export class Session implements AgentSession {
218
218
  /**
219
219
  * 追加一条 assistant 文本消息。
220
220
  */
221
- async appendAssistantMessage(input: {
221
+ async append_assistant_message(input: {
222
222
  text: string;
223
223
  }): Promise<void> {
224
224
  await this.stateService.append_assistant_message({
@@ -229,15 +229,15 @@ export class Session implements AgentSession {
229
229
  /**
230
230
  * 读取当前 session 详情。
231
231
  */
232
- async getInfo(): Promise<AgentSessionInfo> {
232
+ async get_info(): Promise<AgentSessionInfo> {
233
233
  return await this.viewService.get_info();
234
234
  }
235
235
 
236
236
  /**
237
- * 读取当前 session 历史分页。
237
+ * 读取当前 session records 分页。
238
238
  */
239
- async history(input?: AgentSessionHistoryInput): Promise<AgentSessionHistoryPage> {
240
- return await this.viewService.history(input);
239
+ async records(input?: AgentSessionRecordsInput): Promise<AgentSessionRecordsPage> {
240
+ return await this.viewService.records(input);
241
241
  }
242
242
 
243
243
  /**
@@ -288,10 +288,10 @@ export class Session implements AgentSession {
288
288
  afterSessionUpdatedAsync: async () => {
289
289
  await this.executor.afterSessionUpdatedAsync();
290
290
  },
291
- appendUserMessage: async (message_params) => {
291
+ append_user_message: async (message_params) => {
292
292
  await this.stateService.append_user_message(message_params);
293
293
  },
294
- appendAssistantMessage: async (message_params) => {
294
+ append_assistant_message: async (message_params) => {
295
295
  await this.stateService.append_assistant_message(message_params);
296
296
  },
297
297
  isExecuting: () => this.isExecuting(),
@@ -9,7 +9,8 @@
9
9
 
10
10
  import { generateText, type LanguageModel } from "ai";
11
11
  import type { SessionHistoryMetaV1 } from "@/executor/types/SessionHistoryMeta.js";
12
- import type { SessionMessageV1 } from "@/executor/types/SessionMessages.js";
12
+ import type { SessionRecordV1 } from "@/executor/types/SessionRecords.js";
13
+ import { is_session_message_record } from "@/executor/types/SessionRecords.js";
13
14
  import {
14
15
  normalizeSessionTitle,
15
16
  readSessionMetadata,
@@ -40,7 +41,7 @@ export interface EnsureSessionTitleParams {
40
41
  /**
41
42
  * 当前 session 已落盘消息。
42
43
  */
43
- messages: SessionMessageV1[];
44
+ messages: SessionRecordV1[];
44
45
 
45
46
  /**
46
47
  * 可选模型实例;传入时会尝试生成更短标题。
@@ -60,7 +61,8 @@ function truncateTitle(input: string, maxChars: number): string {
60
61
  return title.slice(0, maxChars).trimEnd();
61
62
  }
62
63
 
63
- function extractTextFromMessage(message: SessionMessageV1): string {
64
+ function extractTextFromMessage(message: SessionRecordV1): string {
65
+ if (!is_session_message_record(message)) return "";
64
66
  if (!Array.isArray(message.parts)) return "";
65
67
  const texts: string[] = [];
66
68
  for (const part of message.parts) {
@@ -74,8 +76,9 @@ function extractTextFromMessage(message: SessionMessageV1): string {
74
76
  return texts.join("\n").trim();
75
77
  }
76
78
 
77
- function resolveFirstUserText(messages: SessionMessageV1[]): string {
79
+ function resolveFirstUserText(messages: SessionRecordV1[]): string {
78
80
  for (const message of messages) {
81
+ if (!is_session_message_record(message)) continue;
79
82
  if (message.role !== "user") continue;
80
83
  const text = extractTextFromMessage(message);
81
84
  if (text) return text;
@@ -17,19 +17,24 @@ import {
17
17
  } from "ai";
18
18
  import type {
19
19
  AgentListSessionsInput,
20
- AgentSessionHistoryInput,
21
- AgentSessionHistoryPage,
22
- AgentSessionHistoryView,
20
+ AgentSessionRecordsInput,
21
+ AgentSessionRecordsPage,
22
+ AgentSessionRecordsView,
23
23
  AgentSessionInfo,
24
24
  AgentSessionSummary,
25
25
  AgentSessionSummaryPage,
26
26
  AgentSessionTimelineEvent,
27
27
  } from "@/types/agent/AgentTypes.js";
28
28
  import type {
29
- SessionMessageV1,
29
+ SessionActionRecordV1,
30
+ SessionRecordV1,
30
31
  SessionMetadataV1,
31
- } from "@/executor/types/SessionMessages.js";
32
- import { isSessionOperationMessage } from "@/executor/types/SessionMessages.js";
32
+ SessionMessageRecordV1,
33
+ } from "@/executor/types/SessionRecords.js";
34
+ import {
35
+ is_session_action_record,
36
+ is_session_message_record,
37
+ } from "@/executor/types/SessionRecords.js";
33
38
  import type { SessionHistoryMetaV1 } from "@/executor/types/SessionHistoryMeta.js";
34
39
  import { pickLastSuccessfulChatSendText } from "@/executor/messages/UserVisibleText.js";
35
40
  import { getSdkAgentSessionMessagesPath } from "@/session/storage/Paths.js";
@@ -76,7 +81,7 @@ type SessionBrowseBaseInput = {
76
81
  /**
77
82
  * 当前 session 已读取到的完整消息。
78
83
  */
79
- messages: SessionMessageV1[];
84
+ messages: SessionRecordV1[];
80
85
 
81
86
  /**
82
87
  * 当前 session 是否正在执行。
@@ -172,7 +177,7 @@ function extractMessageText(parts: unknown): string {
172
177
  return texts.join("\n").trim();
173
178
  }
174
179
 
175
- function extractAssistantToolSummary(message: SessionMessageV1): string {
180
+ function extractAssistantToolSummary(message: SessionMessageRecordV1): string {
176
181
  if (!Array.isArray(message.parts)) return "";
177
182
  const toolNames = new Set<string>();
178
183
  for (const part of message.parts as AnyUiPart[]) {
@@ -188,7 +193,13 @@ function extractAssistantToolSummary(message: SessionMessageV1): string {
188
193
  /**
189
194
  * 解析单条 session 消息的用户可见预览文本。
190
195
  */
191
- export function resolveSessionMessagePreview(message: SessionMessageV1): string {
196
+ export function resolveSessionMessagePreview(message: SessionRecordV1): string {
197
+ if (is_session_action_record(message)) {
198
+ return message.description
199
+ ? `${message.title}\n${message.description}`
200
+ : message.title;
201
+ }
202
+ if (!is_session_message_record(message)) return "";
192
203
  const plainText = extractMessageText(message.parts);
193
204
  if (plainText) return plainText;
194
205
  if (message.role !== "assistant") return "";
@@ -226,7 +237,7 @@ function extractToolResultOutput(part: ToolPartCompatShape): unknown {
226
237
  }
227
238
 
228
239
  function toTimelineEvent(params: {
229
- message: SessionMessageV1;
240
+ message: SessionMessageRecordV1;
230
241
  role: AgentSessionTimelineEvent["role"];
231
242
  text: string;
232
243
  sequence: number;
@@ -241,15 +252,21 @@ function toTimelineEvent(params: {
241
252
  ...(typeof metadata?.source === "string" ? { source: metadata.source } : {}),
242
253
  text: params.text,
243
254
  ...(params.toolName ? { toolName: params.toolName } : {}),
244
- ...(params.role === "operation" && metadata?.operation?.operationId
245
- ? { operationId: metadata.operation.operationId }
246
- : {}),
247
- ...(params.role === "operation" && metadata?.operation?.name
248
- ? { operationName: metadata.operation.name }
249
- : {}),
250
- ...(params.role === "operation" && metadata?.operation?.status
251
- ? { operationStatus: metadata.operation.status }
252
- : {}),
255
+ };
256
+ }
257
+
258
+ function toActionTimelineEvent(
259
+ message: SessionActionRecordV1,
260
+ ): AgentSessionTimelineEvent {
261
+ const metadata = message.metadata || null;
262
+ return {
263
+ id: `${String(message.id || "")}:0`,
264
+ role: "action",
265
+ ...(typeof metadata?.ts === "number" ? { ts: metadata.ts } : {}),
266
+ text: resolveSessionMessagePreview(message),
267
+ actionTitle: message.title,
268
+ ...(message.description ? { actionDescription: message.description } : {}),
269
+ actionState: message.state,
253
270
  };
254
271
  }
255
272
 
@@ -257,23 +274,13 @@ function toTimelineEvent(params: {
257
274
  * 把单条 session message 展平成时间线事件。
258
275
  */
259
276
  export function toSessionTimelineEvents(
260
- message: SessionMessageV1,
277
+ message: SessionRecordV1,
261
278
  ): AgentSessionTimelineEvent[] {
262
- if (isSessionOperationMessage(message)) {
263
- const metadata = (message.metadata || null) as SessionMetadataV1 | null;
264
- if (metadata?.operation?.status !== "finished") return [];
265
- return [
266
- toTimelineEvent({
267
- message,
268
- role: "operation",
269
- text:
270
- String(metadata?.operation?.label || "").trim() ||
271
- resolveSessionMessagePreview(message),
272
- sequence: 0,
273
- }),
274
- ];
279
+ if (is_session_action_record(message)) {
280
+ return [toActionTimelineEvent(message)];
275
281
  }
276
282
 
283
+ if (!is_session_message_record(message)) return [];
277
284
  if (message.role !== "assistant") {
278
285
  return [
279
286
  toTimelineEvent({
@@ -359,19 +366,20 @@ export function toSessionTimelineEvents(
359
366
  */
360
367
  export async function loadSessionMessagesFromPath(
361
368
  filePath: string,
362
- ): Promise<SessionMessageV1[]> {
363
- const messages: SessionMessageV1[] = [];
369
+ ): Promise<SessionRecordV1[]> {
370
+ const messages: SessionRecordV1[] = [];
364
371
  if (await fs.pathExists(filePath)) {
365
372
  const raw = await fs.readFile(filePath, "utf-8");
366
373
  const lines = raw.split("\n").filter(Boolean);
367
374
  for (const line of lines) {
368
375
  try {
369
- const parsed = JSON.parse(line) as SessionMessageV1;
376
+ const parsed = JSON.parse(line) as SessionRecordV1;
370
377
  if (!parsed || typeof parsed !== "object") continue;
378
+ const candidate = parsed as { type?: unknown; role?: unknown };
371
379
  if (
372
- parsed.role !== "user" &&
373
- parsed.role !== "assistant" &&
374
- parsed.role !== "operation"
380
+ candidate.type !== "action" &&
381
+ candidate.role !== "user" &&
382
+ candidate.role !== "assistant"
375
383
  ) {
376
384
  continue;
377
385
  }
@@ -385,10 +393,9 @@ export async function loadSessionMessagesFromPath(
385
393
  const inflight_path = filePath.replace(/messages\.jsonl$/, "inflight.json");
386
394
  if (await fs.pathExists(inflight_path)) {
387
395
  try {
388
- const parsed = (await fs.readJson(inflight_path)) as SessionMessageV1;
396
+ const parsed = (await fs.readJson(inflight_path)) as SessionRecordV1;
389
397
  if (
390
- parsed &&
391
- typeof parsed === "object" &&
398
+ is_session_message_record(parsed) &&
392
399
  parsed.role === "assistant" &&
393
400
  Array.isArray(parsed.parts)
394
401
  ) {
@@ -411,20 +418,20 @@ export async function loadSessionMessagesFromPath(
411
418
  */
412
419
  export async function loadSessionArchiveMessagesFromPath(
413
420
  filePath: string,
414
- ): Promise<SessionMessageV1[]> {
421
+ ): Promise<SessionRecordV1[]> {
415
422
  try {
416
423
  const raw = (await fs.readJson(filePath)) as SessionArchiveFileV1 | null;
417
424
  const items = Array.isArray(raw?.messages) ? raw.messages : [];
418
- return items.filter((item): item is SessionMessageV1 => {
425
+ return items.filter((item): item is SessionRecordV1 => {
419
426
  if (!item || typeof item !== "object") return false;
420
- const candidate = item as Partial<SessionMessageV1>;
427
+ const candidate = item as { type?: unknown; role?: unknown; parts?: unknown };
421
428
  return (
422
429
  (
430
+ candidate.type === "action" ||
423
431
  candidate.role === "user" ||
424
- candidate.role === "assistant" ||
425
- candidate.role === "operation"
432
+ candidate.role === "assistant"
426
433
  ) &&
427
- Array.isArray(candidate.parts)
434
+ (candidate.type === "action" || Array.isArray(candidate.parts))
428
435
  );
429
436
  });
430
437
  } catch {
@@ -432,12 +439,13 @@ export async function loadSessionArchiveMessagesFromPath(
432
439
  }
433
440
  }
434
441
 
435
- function isCompactSummaryMessage(message: SessionMessageV1): boolean {
442
+ function isCompactSummaryMessage(message: SessionRecordV1): boolean {
443
+ if (!is_session_message_record(message)) return false;
436
444
  const metadata = (message.metadata || null) as SessionMetadataV1 | null;
437
445
  return metadata?.source === "compact" || metadata?.kind === "summary";
438
446
  }
439
447
 
440
- function resolvePreviousArchiveId(messages: SessionMessageV1[]): string | undefined {
448
+ function resolvePreviousArchiveId(messages: SessionRecordV1[]): string | undefined {
441
449
  for (const message of messages) {
442
450
  if (!isCompactSummaryMessage(message)) continue;
443
451
  const metadata = (message.metadata || null) as SessionMetadataV1 | null;
@@ -448,8 +456,8 @@ function resolvePreviousArchiveId(messages: SessionMessageV1[]): string | undefi
448
456
  }
449
457
 
450
458
  function filterUserVisibleHistoryMessages(
451
- messages: SessionMessageV1[],
452
- ): SessionMessageV1[] {
459
+ messages: SessionRecordV1[],
460
+ ): SessionRecordV1[] {
453
461
  return messages.filter((message) => !isCompactSummaryMessage(message));
454
462
  }
455
463
 
@@ -492,14 +500,14 @@ export function buildSessionInfo(
492
500
  }
493
501
 
494
502
  /**
495
- * 基于完整消息列表构建 session history 分页结果。
503
+ * 基于完整消息列表构建 session records 分页结果。
496
504
  */
497
- export function buildSessionHistoryPage(params: {
505
+ export function buildSessionRecordsPage(params: {
498
506
  session: AgentSessionInfo;
499
- messages: SessionMessageV1[];
500
- input?: AgentSessionHistoryInput;
501
- }): AgentSessionHistoryPage {
502
- const view: AgentSessionHistoryView = params.input?.view || "message";
507
+ messages: SessionRecordV1[];
508
+ input?: AgentSessionRecordsInput;
509
+ }): AgentSessionRecordsPage {
510
+ const view: AgentSessionRecordsView = params.input?.view || "message";
503
511
  const order = params.input?.order || "asc";
504
512
  const limit = normalizeLimit(params.input?.limit, 50, 500);
505
513
  const cursor = normalizeCursor(params.input?.cursor);
@@ -46,7 +46,7 @@ export {
46
46
  createRuntimeSessionPort,
47
47
  } from "./storage/RuntimeSessionPort.js";
48
48
  export {
49
- buildSessionHistoryPage,
49
+ buildSessionRecordsPage,
50
50
  buildSessionInfo,
51
51
  listArchivedAgentSessionSummaryPage,
52
52
  listAgentSessionSummaryPage,