@downcity/agent 1.1.129 → 1.1.148

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 (228) hide show
  1. package/README.md +2 -2
  2. package/bin/agent/local/Agent.d.ts +40 -26
  3. package/bin/agent/local/Agent.d.ts.map +1 -1
  4. package/bin/agent/local/Agent.js +65 -41
  5. package/bin/agent/local/Agent.js.map +1 -1
  6. package/bin/agent/local/ProjectSetup.js +2 -2
  7. package/bin/agent/local/ProjectSetup.js.map +1 -1
  8. package/bin/agent/local/services/AgentAssemblyService.d.ts +2 -7
  9. package/bin/agent/local/services/AgentAssemblyService.d.ts.map +1 -1
  10. package/bin/agent/local/services/AgentAssemblyService.js +34 -22
  11. package/bin/agent/local/services/AgentAssemblyService.js.map +1 -1
  12. package/bin/agent/local/services/AgentBackgroundService.d.ts +55 -0
  13. package/bin/agent/local/services/AgentBackgroundService.d.ts.map +1 -0
  14. package/bin/agent/local/services/AgentBackgroundService.js +86 -0
  15. package/bin/agent/local/services/AgentBackgroundService.js.map +1 -0
  16. package/bin/agent/local/services/AgentSessionManager.d.ts +16 -7
  17. package/bin/agent/local/services/AgentSessionManager.d.ts.map +1 -1
  18. package/bin/agent/local/services/AgentSessionManager.js +83 -8
  19. package/bin/agent/local/services/AgentSessionManager.js.map +1 -1
  20. package/bin/agent/remote/RemoteAgent.d.ts +9 -13
  21. package/bin/agent/remote/RemoteAgent.d.ts.map +1 -1
  22. package/bin/agent/remote/RemoteAgent.js +23 -24
  23. package/bin/agent/remote/RemoteAgent.js.map +1 -1
  24. package/bin/agent/remote/RemoteSession.d.ts +11 -4
  25. package/bin/agent/remote/RemoteSession.d.ts.map +1 -1
  26. package/bin/agent/remote/RemoteSession.js +15 -3
  27. package/bin/agent/remote/RemoteSession.js.map +1 -1
  28. package/bin/agent/remote/RemoteTransport.d.ts +9 -1
  29. package/bin/agent/remote/RemoteTransport.d.ts.map +1 -1
  30. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts +7 -3
  31. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -1
  32. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js +65 -2
  33. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js.map +1 -1
  34. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts +5 -1
  35. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -1
  36. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js +12 -0
  37. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js.map +1 -1
  38. package/bin/config/AgentInitializer.d.ts +2 -2
  39. package/bin/config/AgentInitializer.js +2 -2
  40. package/bin/executor/Executor.d.ts +5 -0
  41. package/bin/executor/Executor.d.ts.map +1 -1
  42. package/bin/executor/Executor.js +34 -0
  43. package/bin/executor/Executor.js.map +1 -1
  44. package/bin/executor/composer/system/default/InitPrompts.d.ts +1 -1
  45. package/bin/executor/composer/system/default/InitPrompts.js +1 -1
  46. package/bin/executor/composer/system/default/SystemDomain.js +2 -2
  47. package/bin/executor/composer/system/default/SystemDomain.js.map +1 -1
  48. package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -1
  49. package/bin/executor/core-engine/CoreEngineRunner.js +16 -0
  50. package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
  51. package/bin/executor/messages/ChatMessageMarkupTypes.d.ts +1 -1
  52. package/bin/executor/messages/ChatMessageMarkupTypes.js +1 -1
  53. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts +8 -0
  54. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts.map +1 -1
  55. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js +22 -0
  56. package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js.map +1 -1
  57. package/bin/executor/types/SessionExecutor.d.ts +8 -0
  58. package/bin/executor/types/SessionExecutor.d.ts.map +1 -1
  59. package/bin/index.d.ts +4 -4
  60. package/bin/index.d.ts.map +1 -1
  61. package/bin/index.js +1 -2
  62. package/bin/index.js.map +1 -1
  63. package/bin/plugin/core/Activation.d.ts +0 -2
  64. package/bin/plugin/core/Activation.d.ts.map +1 -1
  65. package/bin/plugin/core/Activation.js +1 -1
  66. package/bin/plugin/core/Activation.js.map +1 -1
  67. package/bin/plugin/core/PluginCommandRequest.d.ts +1 -1
  68. package/bin/plugin/core/PluginCommandRequest.js +1 -1
  69. package/bin/plugin/core/PluginStateController.d.ts +0 -5
  70. package/bin/plugin/core/PluginStateController.d.ts.map +1 -1
  71. package/bin/plugin/core/PluginStateController.js +1 -5
  72. package/bin/plugin/core/PluginStateController.js.map +1 -1
  73. package/bin/plugin/types/PluginApi.d.ts +1 -1
  74. package/bin/plugin/types/PluginApi.js +1 -1
  75. package/bin/rpc/Client.d.ts +17 -1
  76. package/bin/rpc/Client.d.ts.map +1 -1
  77. package/bin/rpc/Client.js +47 -0
  78. package/bin/rpc/Client.js.map +1 -1
  79. package/bin/session/Session.d.ts +5 -0
  80. package/bin/session/Session.d.ts.map +1 -1
  81. package/bin/session/Session.js +7 -0
  82. package/bin/session/Session.js.map +1 -1
  83. package/bin/session/browse/Browse.d.ts +9 -1
  84. package/bin/session/browse/Browse.d.ts.map +1 -1
  85. package/bin/session/browse/Browse.js +81 -3
  86. package/bin/session/browse/Browse.js.map +1 -1
  87. package/bin/session/index.d.ts +3 -3
  88. package/bin/session/index.d.ts.map +1 -1
  89. package/bin/session/index.js +3 -3
  90. package/bin/session/index.js.map +1 -1
  91. package/bin/session/runtime/SessionPromptRuntime.d.ts +13 -0
  92. package/bin/session/runtime/SessionPromptRuntime.d.ts.map +1 -1
  93. package/bin/session/runtime/SessionPromptRuntime.js +82 -5
  94. package/bin/session/runtime/SessionPromptRuntime.js.map +1 -1
  95. package/bin/session/services/SessionTurnService.d.ts +5 -0
  96. package/bin/session/services/SessionTurnService.d.ts.map +1 -1
  97. package/bin/session/services/SessionTurnService.js +10 -1
  98. package/bin/session/services/SessionTurnService.js.map +1 -1
  99. package/bin/session/services/SessionViewService.d.ts.map +1 -1
  100. package/bin/session/services/SessionViewService.js +1 -7
  101. package/bin/session/services/SessionViewService.js.map +1 -1
  102. package/bin/session/storage/Metadata.d.ts +15 -0
  103. package/bin/session/storage/Metadata.d.ts.map +1 -1
  104. package/bin/session/storage/Metadata.js +15 -1
  105. package/bin/session/storage/Metadata.js.map +1 -1
  106. package/bin/session/storage/Paths.d.ts +24 -0
  107. package/bin/session/storage/Paths.d.ts.map +1 -1
  108. package/bin/session/storage/Paths.js +34 -0
  109. package/bin/session/storage/Paths.js.map +1 -1
  110. package/bin/session/storage/RuntimeSessionPort.d.ts +5 -0
  111. package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
  112. package/bin/session/storage/RuntimeSessionPort.js +4 -0
  113. package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
  114. package/bin/types/agent/AgentOptions.d.ts +0 -63
  115. package/bin/types/agent/AgentOptions.d.ts.map +1 -1
  116. package/bin/types/agent/AgentTypes.d.ts +3 -2
  117. package/bin/types/agent/AgentTypes.d.ts.map +1 -1
  118. package/bin/types/agent/SessionActor.d.ts +13 -4
  119. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  120. package/bin/types/agent/SessionTypes.d.ts +62 -0
  121. package/bin/types/agent/SessionTypes.d.ts.map +1 -1
  122. package/bin/types/config/DowncityConfig.d.ts +1 -1
  123. package/bin/types/executor/SessionRunContext.d.ts +8 -0
  124. package/bin/types/executor/SessionRunContext.d.ts.map +1 -1
  125. package/bin/types/platform/Store.d.ts +2 -2
  126. package/bin/types/platform/Store.js +2 -2
  127. package/bin/types/rpc/RpcProtocol.d.ts +37 -5
  128. package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
  129. package/bin/types/rpc/RpcProtocol.js +1 -1
  130. package/bin/types/runtime/agent/AgentContext.d.ts +92 -43
  131. package/bin/types/runtime/agent/AgentContext.d.ts.map +1 -1
  132. package/bin/types/runtime/agent/AgentContext.js +97 -5
  133. package/bin/types/runtime/agent/AgentContext.js.map +1 -1
  134. package/bin/types/runtime/platform/Platform.d.ts +8 -8
  135. package/bin/types/runtime/platform/PlatformGateway.d.ts +2 -2
  136. package/bin/types/sdk/AgentSessionStop.d.ts +36 -0
  137. package/bin/types/sdk/AgentSessionStop.d.ts.map +1 -0
  138. package/bin/types/sdk/AgentSessionStop.js +9 -0
  139. package/bin/types/sdk/AgentSessionStop.js.map +1 -0
  140. package/package.json +3 -3
  141. package/scripts/city-model-tool-loop.test.mjs +2 -2
  142. package/scripts/session-prompt-runtime.test.mjs +59 -0
  143. package/src/agent/local/Agent.ts +66 -55
  144. package/src/agent/local/ProjectSetup.ts +2 -2
  145. package/src/agent/local/services/AgentAssemblyService.ts +41 -35
  146. package/src/agent/local/services/AgentBackgroundService.ts +116 -0
  147. package/src/agent/local/services/AgentSessionManager.ts +127 -13
  148. package/src/agent/remote/RemoteAgent.ts +25 -34
  149. package/src/agent/remote/RemoteSession.ts +21 -6
  150. package/src/agent/remote/RemoteTransport.ts +14 -0
  151. package/src/agent/remote/transports/HttpRemoteAgentTransport.ts +100 -2
  152. package/src/agent/remote/transports/RpcRemoteAgentTransport.ts +26 -0
  153. package/src/config/AgentInitializer.ts +2 -2
  154. package/src/executor/Executor.ts +33 -0
  155. package/src/executor/composer/system/default/InitPrompts.ts +1 -1
  156. package/src/executor/composer/system/default/SystemDomain.ts +2 -2
  157. package/src/executor/core-engine/CoreEngineRunner.ts +21 -0
  158. package/src/executor/messages/ChatMessageMarkupTypes.ts +1 -1
  159. package/src/executor/store/history/jsonl/JsonlSessionHistoryStore.ts +22 -0
  160. package/src/executor/types/SessionExecutor.ts +2 -1
  161. package/src/index.ts +8 -10
  162. package/src/plugin/core/Activation.ts +1 -3
  163. package/src/plugin/core/PluginCommandRequest.ts +1 -1
  164. package/src/plugin/core/PluginStateController.ts +1 -10
  165. package/src/plugin/types/PluginApi.ts +1 -1
  166. package/src/rpc/Client.ts +61 -0
  167. package/src/session/Session.ts +9 -0
  168. package/src/session/browse/Browse.ts +103 -3
  169. package/src/session/index.ts +7 -0
  170. package/src/session/runtime/SessionPromptRuntime.ts +100 -5
  171. package/src/session/services/SessionTurnService.ts +13 -1
  172. package/src/session/services/SessionViewService.ts +1 -7
  173. package/src/session/storage/Metadata.ts +23 -1
  174. package/src/session/storage/Paths.ts +70 -0
  175. package/src/session/storage/RuntimeSessionPort.ts +9 -0
  176. package/src/types/agent/AgentOptions.ts +0 -69
  177. package/src/types/agent/AgentTypes.ts +6 -5
  178. package/src/types/agent/SessionActor.ts +21 -3
  179. package/src/types/agent/SessionTypes.ts +68 -0
  180. package/src/types/config/DowncityConfig.ts +1 -1
  181. package/src/types/executor/SessionRunContext.ts +9 -0
  182. package/src/types/platform/Store.ts +2 -2
  183. package/src/types/rpc/RpcProtocol.ts +46 -4
  184. package/src/types/runtime/agent/AgentContext.ts +132 -45
  185. package/src/types/runtime/platform/Platform.ts +8 -8
  186. package/src/types/runtime/platform/PlatformGateway.ts +2 -2
  187. package/src/types/sdk/AgentSessionStop.ts +39 -0
  188. package/tsconfig.tsbuildinfo +1 -1
  189. package/bin/agent/local/AgentRuntimeFactory.d.ts +0 -72
  190. package/bin/agent/local/AgentRuntimeFactory.d.ts.map +0 -1
  191. package/bin/agent/local/AgentRuntimeFactory.js +0 -73
  192. package/bin/agent/local/AgentRuntimeFactory.js.map +0 -1
  193. package/bin/agent/local/services/AgentLifecycleService.d.ts +0 -65
  194. package/bin/agent/local/services/AgentLifecycleService.d.ts.map +0 -1
  195. package/bin/agent/local/services/AgentLifecycleService.js +0 -140
  196. package/bin/agent/local/services/AgentLifecycleService.js.map +0 -1
  197. package/bin/rpc/Server.d.ts +0 -31
  198. package/bin/rpc/Server.d.ts.map +0 -1
  199. package/bin/rpc/Server.js +0 -106
  200. package/bin/rpc/Server.js.map +0 -1
  201. package/bin/rpc/server/InternalHandlers.d.ts +0 -21
  202. package/bin/rpc/server/InternalHandlers.d.ts.map +0 -1
  203. package/bin/rpc/server/InternalHandlers.js +0 -244
  204. package/bin/rpc/server/InternalHandlers.js.map +0 -1
  205. package/bin/rpc/server/RequestDispatcher.d.ts +0 -27
  206. package/bin/rpc/server/RequestDispatcher.d.ts.map +0 -1
  207. package/bin/rpc/server/RequestDispatcher.js +0 -38
  208. package/bin/rpc/server/RequestDispatcher.js.map +0 -1
  209. package/bin/rpc/server/SdkSessionHandlers.d.ts +0 -25
  210. package/bin/rpc/server/SdkSessionHandlers.d.ts.map +0 -1
  211. package/bin/rpc/server/SdkSessionHandlers.js +0 -86
  212. package/bin/rpc/server/SdkSessionHandlers.js.map +0 -1
  213. package/bin/rpc/server/ServerTypes.d.ts +0 -64
  214. package/bin/rpc/server/ServerTypes.d.ts.map +0 -1
  215. package/bin/rpc/server/ServerTypes.js +0 -9
  216. package/bin/rpc/server/ServerTypes.js.map +0 -1
  217. package/bin/types/runtime/agent/AgentRuntime.d.ts +0 -79
  218. package/bin/types/runtime/agent/AgentRuntime.d.ts.map +0 -1
  219. package/bin/types/runtime/agent/AgentRuntime.js +0 -13
  220. package/bin/types/runtime/agent/AgentRuntime.js.map +0 -1
  221. package/src/agent/local/AgentRuntimeFactory.ts +0 -152
  222. package/src/agent/local/services/AgentLifecycleService.ts +0 -198
  223. package/src/rpc/Server.ts +0 -143
  224. package/src/rpc/server/InternalHandlers.ts +0 -285
  225. package/src/rpc/server/RequestDispatcher.ts +0 -67
  226. package/src/rpc/server/SdkSessionHandlers.ts +0 -106
  227. package/src/rpc/server/ServerTypes.ts +0 -70
  228. package/src/types/runtime/agent/AgentRuntime.ts +0 -84
@@ -8,6 +8,11 @@
8
8
 
9
9
  import type {
10
10
  AgentCreateSessionInput,
11
+ AgentArchiveSessionInput,
12
+ AgentArchiveSessionsInput,
13
+ AgentArchiveSessionResult,
14
+ AgentArchiveSessionsResult,
15
+ AgentCleanArchiveResult,
11
16
  AgentListSessionsInput,
12
17
  AgentSessionConfigSnapshot,
13
18
  AgentSessionForkInput,
@@ -23,6 +28,7 @@ import type {
23
28
  AgentSessionUnsubscribe,
24
29
  } from "@/types/sdk/AgentSessionEvent.js";
25
30
  import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
31
+ import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
26
32
  import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
27
33
 
28
34
  /**
@@ -30,13 +36,22 @@ import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
30
36
  */
31
37
  export interface AgentSessionCollection {
32
38
  /** 新建一个 session。 */
33
- createSession(input?: AgentCreateSessionInput): Promise<AgentSession>;
39
+ create_session(input?: AgentCreateSessionInput): Promise<AgentSession>;
34
40
 
35
41
  /** 获取一个已存在的 session。 */
36
- getSession(sessionId: string): Promise<AgentSession>;
42
+ get_session(sessionId: string): Promise<AgentSession>;
37
43
 
38
44
  /** 列出当前 agent 的 session 摘要页。 */
39
- listSessions(input?: AgentListSessionsInput): Promise<AgentSessionSummaryPage>;
45
+ list_sessions(input?: AgentListSessionsInput): Promise<AgentSessionSummaryPage>;
46
+
47
+ /** 归档单个 session。 */
48
+ archive_session(input: AgentArchiveSessionInput): Promise<AgentArchiveSessionResult>;
49
+
50
+ /** 列出已归档的 session 摘要页。 */
51
+ archive_sessions(input?: AgentArchiveSessionsInput): Promise<AgentArchiveSessionsResult>;
52
+
53
+ /** 永久清空已归档 session。 */
54
+ clean_archive(): Promise<AgentCleanArchiveResult>;
40
55
  }
41
56
 
42
57
  /**
@@ -52,6 +67,9 @@ export interface AgentSessionActor {
52
67
  /** 追加一条新的 prompt。 */
53
68
  prompt(input: AgentSessionPromptInput): Promise<AgentSessionTurnHandle>;
54
69
 
70
+ /** 停止当前 turn,并取消尚未被吸收的排队 prompt。 */
71
+ stop(): Promise<AgentSessionStopResult>;
72
+
55
73
  /** 订阅当前 session 的未来事件。 */
56
74
  subscribe(subscriber: AgentSessionSubscriber): AgentSessionUnsubscribe;
57
75
 
@@ -333,3 +333,71 @@ export interface AgentSessionForkInput {
333
333
  */
334
334
  messageId?: string;
335
335
  }
336
+
337
+ /**
338
+ * 归档单个 session 的输入参数。
339
+ */
340
+ export interface AgentArchiveSessionInput {
341
+ /**
342
+ * 要归档的 session id。
343
+ *
344
+ * 关键点(中文)
345
+ * - 必须指向当前 agent 下已存在的未归档 session。
346
+ * - 正在执行中的 session 不允许归档。
347
+ */
348
+ id: string;
349
+ }
350
+
351
+ /**
352
+ * 列出已归档 session 的输入参数。
353
+ */
354
+ export interface AgentArchiveSessionsInput {
355
+ /**
356
+ * 当前页返回上限。
357
+ *
358
+ * 说明(中文)
359
+ * - 省略时由 SDK 使用默认值。
360
+ */
361
+ limit?: number;
362
+
363
+ /**
364
+ * 分页游标。
365
+ *
366
+ * 说明(中文)
367
+ * - 当前使用 SDK 自身生成的透明字符串游标。
368
+ * - 调用方只负责透传,不应自行解析其内部格式。
369
+ */
370
+ cursor?: string;
371
+
372
+ /**
373
+ * 关键词过滤。
374
+ *
375
+ * 说明(中文)
376
+ * - 推荐用于匹配 `sessionId`、标题与预览文本。
377
+ * - 属于轻量包含匹配,不承诺复杂搜索语义。
378
+ */
379
+ query?: string;
380
+ }
381
+
382
+ /**
383
+ * 归档单个 session 的结果。
384
+ */
385
+ export interface AgentArchiveSessionResult {
386
+ /** 被归档的 session id。 */
387
+ sessionId: string;
388
+ /** 归档时间戳(ms)。 */
389
+ archivedAt: number;
390
+ }
391
+
392
+ /**
393
+ * 列出已归档 session 的结果。
394
+ */
395
+ export interface AgentArchiveSessionsResult extends AgentSessionSummaryPage {}
396
+
397
+ /**
398
+ * 清空归档目录的结果。
399
+ */
400
+ export interface AgentCleanArchiveResult {
401
+ /** 被永久删除的归档 session id 列表。 */
402
+ removedSessionIds: string[];
403
+ }
@@ -135,7 +135,7 @@ export interface DowncityConfig {
135
135
  id: string;
136
136
  version: string;
137
137
  /**
138
- * Runtime startup configuration used by `town agent start`.
138
+ * Runtime startup configuration used by `downcity agent start`.
139
139
  * CLI flags (if provided) take precedence over this config.
140
140
  */
141
141
  start?: {
@@ -66,6 +66,15 @@ export interface SessionRunContext {
66
66
  */
67
67
  onUiMessageChunkCallback?: SessionUiMessageChunkCallback;
68
68
 
69
+ /**
70
+ * 当前 turn 的取消信号。
71
+ *
72
+ * 关键点(中文)
73
+ * - `session.stop()` 会触发该 signal。
74
+ * - 模型流、tool-loop 与长耗时 composer 应优先监听它,尽快结束当前 turn。
75
+ */
76
+ abortSignal?: AbortSignal;
77
+
69
78
  /**
70
79
  * 本轮运行中待并入下一 step 的 user 消息。
71
80
  *
@@ -2,8 +2,8 @@
2
2
  * 平台存储类型定义。
3
3
  *
4
4
  * 关键点(中文)
5
- * - Town 只保存平台级 env、channel account 与安全配置。
6
- * - 模型能力由 City AIService 暴露,Town 不再声明 provider/model 存储类型。
5
+ * - downcity 只保存平台级 env、channel account 与安全配置。
6
+ * - 模型能力由 City AIService 暴露,downcity 不再声明 provider/model 存储类型。
7
7
  */
8
8
 
9
9
  /**
@@ -4,13 +4,19 @@
4
4
  * 关键点(中文)
5
5
  * - 该文件只描述本机 RPC 的线协议,不包含 socket 或业务执行逻辑。
6
6
  * - Client 与 Server 共享同一份 request/frame 类型,避免协议两边漂移。
7
- * - 字段名保持现有协议格式,兼容已发布的 Town 托管 runtime 调用。
7
+ * - 字段名保持现有协议格式,兼容已发布的 downcity 托管 runtime 调用。
8
8
  */
9
9
 
10
10
  import type {
11
11
  AgentCreateSessionInput,
12
12
  AgentListSessionsInput,
13
+ AgentArchiveSessionInput,
14
+ AgentArchiveSessionsInput,
15
+ AgentArchiveSessionResult,
16
+ AgentArchiveSessionsResult,
17
+ AgentCleanArchiveResult,
13
18
  AgentSessionHistoryInput,
19
+ AgentSessionStopResult,
14
20
  AgentSessionSystemSnapshot,
15
21
  } from "@/types/agent/AgentTypes.js";
16
22
  import type { AgentSessionEvent } from "@/types/sdk/AgentSessionEvent.js";
@@ -24,13 +30,13 @@ import type { ShellApprovalMode } from "@downcity/shell";
24
30
  *
25
31
  * 关键点(中文)
26
32
  * - `sdk.*` 面向 RemoteAgent 的稳定会话 SDK。
27
- * - `internal.*` 面向 Town 本机管理通道。
33
+ * - `internal.*` 面向 downcity 本机管理通道。
28
34
  */
29
35
  export type RpcRequest =
30
36
  | {
31
37
  /** 请求 id,用于匹配响应。 */
32
38
  id: string;
33
- /** 列出 session。 */
39
+ /** 列出 sessions。 */
34
40
  method: "sdk.sessions.list";
35
41
  /** 列表过滤与分页参数。 */
36
42
  params?: AgentListSessionsInput;
@@ -46,7 +52,7 @@ export type RpcRequest =
46
52
  | {
47
53
  /** 请求 id,用于匹配响应。 */
48
54
  id: string;
49
- /** 读取 session 信息。 */
55
+ /** 获取 session */
50
56
  method: "sdk.sessions.get";
51
57
  /** 目标 session 参数。 */
52
58
  params: {
@@ -54,6 +60,31 @@ export type RpcRequest =
54
60
  sessionId: string;
55
61
  };
56
62
  }
63
+ | {
64
+ /** 请求 id,用于匹配响应。 */
65
+ id: string;
66
+ /** 归档 session。 */
67
+ method: "sdk.sessions.archive";
68
+ /** 归档参数。 */
69
+ params: {
70
+ /** 目标 session id。 */
71
+ sessionId: string;
72
+ };
73
+ }
74
+ | {
75
+ /** 请求 id,用于匹配响应。 */
76
+ id: string;
77
+ /** 列出已归档 sessions。 */
78
+ method: "sdk.sessions.archived.list";
79
+ /** 列表过滤与分页参数。 */
80
+ params?: AgentArchiveSessionsInput;
81
+ }
82
+ | {
83
+ /** 请求 id,用于匹配响应。 */
84
+ id: string;
85
+ /** 清空已归档 sessions。 */
86
+ method: "sdk.sessions.archived.clean";
87
+ }
57
88
  | {
58
89
  /** 请求 id,用于匹配响应。 */
59
90
  id: string;
@@ -67,6 +98,17 @@ export type RpcRequest =
67
98
  input: AgentSessionPromptInput;
68
99
  };
69
100
  }
101
+ | {
102
+ /** 请求 id,用于匹配响应。 */
103
+ id: string;
104
+ /** 停止当前 session turn。 */
105
+ method: "sdk.sessions.stop";
106
+ /** 停止参数。 */
107
+ params: {
108
+ /** 目标 session id。 */
109
+ sessionId: string;
110
+ };
111
+ }
70
112
  | {
71
113
  /** 请求 id,用于匹配响应。 */
72
114
  id: string;
@@ -3,23 +3,20 @@
3
3
  *
4
4
  * 职责说明(中文)
5
5
  * - 这里定义 plugin runtime、plugin、prompt system 共用的统一执行上下文。
6
- * - `AgentContext` 表达的是“当前一次执行可见的能力面”,不是宿主状态本体。
7
- *
8
- * 边界说明(中文)
9
- * - `AgentRuntime` 负责保存长期状态;`AgentContext` 负责把这些状态暴露成执行接口。
10
- * - 这里只定义协议,不负责任何上下文装配逻辑。
6
+ * - `AgentContext` 是一个 class,表达"当前一次执行可见的能力面",不是宿主状态本体。
7
+ * - 同一 agent 实例全程共享同一个 context;plugin、session、executor 都基于它读写状态。
11
8
  */
12
9
 
13
10
  import type { LanguageModel } from "ai";
14
11
  import type { Logger } from "@/utils/logger/Logger.js";
15
- import type { AgentRuntime } from "@/types/runtime/agent/AgentRuntime.js";
16
12
  import type {
17
13
  AgentPathRuntime,
18
14
  AgentPluginConfigRuntime,
19
15
  } from "@/types/agent/AgentRuntimeAssembly.js";
20
16
  import type { DowncityConfig } from "@/types/config/DowncityConfig.js";
21
- import type { JsonObject, JsonValue } from "@/types/common/Json.js";
17
+ import type { JsonValue } from "@/types/common/Json.js";
22
18
  import type { AgentPlugins } from "@/plugin/types/Plugin.js";
19
+ import type { BasePlugin } from "@/plugin/core/BasePlugin.js";
23
20
  import type {
24
21
  SessionMetadataV1,
25
22
  SessionMessageV1,
@@ -30,6 +27,7 @@ import type {
30
27
  } from "@/executor/types/SessionRun.js";
31
28
  import type { SessionHistoryStore } from "@/executor/store/history/SessionHistoryStore.js";
32
29
  import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
30
+ import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
33
31
  import type {
34
32
  AgentSessionEvent,
35
33
  AgentSessionSubscriber,
@@ -96,9 +94,10 @@ export interface SessionExecutorPort {
96
94
  * 请求取消当前正在执行的 turn。
97
95
  *
98
96
  * 关键点(中文)
99
- * - 对普通本地 runtime 可不实现。
100
- * - ACP runtime 可借此向远端发送 `session/cancel`。
97
+ * - 返回 `true` 表示已发出取消请求。
98
+ * - 返回 `false` 表示当前没有可取消的执行。
101
99
  */
100
+ stop(): boolean;
102
101
  }
103
102
 
104
103
  /**
@@ -125,6 +124,10 @@ export interface SessionPort {
125
124
  * - 返回值只有在当前输入被绑定到某个 turn 后才会兑现。
126
125
  */
127
126
  prompt(input: AgentSessionPromptInput): Promise<AgentSessionTurnHandle>;
127
+ /**
128
+ * 停止当前 turn,并取消尚未被吸收的排队 prompt。
129
+ */
130
+ stop(): Promise<AgentSessionStopResult>;
128
131
  /**
129
132
  * 订阅当前 session 后续产生的 future 事件。
130
133
  *
@@ -216,61 +219,145 @@ export interface SessionCollectionPort {
216
219
  }
217
220
 
218
221
  /**
219
- * 统一执行上下文。
222
+ * AgentContext 构造参数。
223
+ *
224
+ * 关键点(中文)
225
+ * - 装配方负责把 session / plugins 等上层依赖注入进来。
226
+ * - `env` 必须传入 agent 持有的 mutable 共享对象引用,不要在这里克隆。
220
227
  */
221
- export interface AgentContext {
222
- /**
223
- * 当前执行上下文对应的 agent 状态。
224
- */
225
- agent: AgentRuntime;
226
- /**
227
- * 当前命令工作目录。
228
- */
228
+ export interface AgentContextOptions {
229
+ /** 当前命令工作目录。 */
229
230
  cwd: string;
230
- /**
231
- * 当前项目根目录。
232
- */
231
+ /** 当前项目根目录。 */
233
232
  rootPath: string;
234
- /**
235
- * 统一日志器。
236
- */
233
+ /** 统一日志器。 */
237
234
  logger: Logger;
238
- /**
239
- * 当前运行时已解析配置。
240
- */
235
+ /** 当前运行时已解析配置。 */
241
236
  config: DowncityConfig;
242
237
  /**
243
- * 当前项目环境变量快照。
238
+ * 当前 agent env 共享对象引用。
239
+ *
240
+ * 关键点(中文)
241
+ * - 必须是 Agent 持有的同一个 mutable 引用,不要克隆后再传入。
242
+ * - 通过 `agent.setEnv` / `agent.patchEnv` 原地更新会被本 context 立即感知。
244
243
  */
245
244
  env: Record<string, string>;
246
- /**
247
- * 当前生效的 system 文本集合。
248
- */
245
+ /** 当前生效的 system 文本集合。 */
249
246
  systems: string[];
250
- /**
251
- * 当前可见的路径能力集合。
252
- */
247
+ /** 当前可见的路径能力集合。 */
253
248
  paths: AgentPathRuntime;
254
- /**
255
- * 当前可见的 plugin 配置持久化能力集合。
256
- */
249
+ /** 当前可见的 plugin 配置持久化能力集合。 */
257
250
  pluginConfig: AgentPluginConfigRuntime;
251
+ /** 当前 agent 持有的插件实例集合。 */
252
+ pluginInstances: Map<string, BasePlugin>;
253
+ /** Session 能力入口。 */
254
+ session: SessionCollectionPort;
255
+ /** Plugin 调用入口。 */
256
+ plugins: AgentPlugins;
257
+ }
258
+
259
+ /**
260
+ * 统一执行上下文。
261
+ *
262
+ * 关键点(中文)
263
+ * - 字段全部 readonly,构造一次后语义稳定,避免 plugin 误改。
264
+ * - `env` 引用 agent 级共享 mutable 对象,`...ctx.env` / `ctx.env.FOO` 直接可用。
265
+ * - `invoke` 是构造期组装的 plugin 调用端口,对外仍以 `InvokePluginPort` 形态暴露。
266
+ */
267
+ export class AgentContext {
268
+ /** 当前命令工作目录。 */
269
+ readonly cwd: string;
270
+ /** 当前项目根目录。 */
271
+ readonly rootPath: string;
272
+ /** 统一日志器。 */
273
+ readonly logger: Logger;
274
+ /** 当前运行时已解析配置。 */
275
+ readonly config: DowncityConfig;
276
+ /** 当前项目环境变量共享视图。 */
277
+ readonly env: Record<string, string>;
278
+ /** 当前生效的 system 文本集合。 */
279
+ readonly systems: string[];
280
+ /** 当前可见的路径能力集合。 */
281
+ readonly paths: AgentPathRuntime;
282
+ /** 当前可见的 plugin 配置持久化能力集合。 */
283
+ readonly pluginConfig: AgentPluginConfigRuntime;
284
+ /** 当前 agent 持有的插件实例集合。 */
285
+ readonly pluginInstances: Map<string, BasePlugin>;
286
+ /** Session 能力入口。 */
287
+ readonly session: SessionCollectionPort;
288
+ /** Plugin 调用入口。 */
289
+ readonly plugins: AgentPlugins;
290
+ /** 跨 plugin runtime 调用主入口。 */
291
+ readonly invoke: InvokePluginPort;
292
+
293
+ constructor(options: AgentContextOptions) {
294
+ this.cwd = options.cwd;
295
+ this.rootPath = options.rootPath;
296
+ this.logger = options.logger;
297
+ this.config = options.config;
298
+ this.env = options.env;
299
+ this.systems = options.systems;
300
+ this.paths = options.paths;
301
+ this.pluginConfig = options.pluginConfig;
302
+ this.pluginInstances = options.pluginInstances;
303
+ this.session = options.session;
304
+ this.plugins = options.plugins;
305
+ this.invoke = {
306
+ invoke: (params) => this.invoke_plugin_action(params),
307
+ };
308
+ }
309
+
258
310
  /**
259
- * Session 能力入口。
311
+ * 读取指定 sessionId 对应的 session 端口。
260
312
  *
261
313
  * 关键点(中文)
262
- * - plugin runtime 与 plugin 都通过这里访问 session 执行与持久化能力。
263
- * - 内外统一使用 `sessionId` 语义。
314
+ * - 返回值是统一的 `SessionPort`,而不是裸 `Executor`。
315
+ * - 这样 HTTP / plugin runtime / chat queue / contact 等入口都能复用同一层会话装配与执行兜底。
264
316
  */
265
- session: SessionCollectionPort;
317
+ getSession(sessionId: string): SessionPort {
318
+ return this.session.get(sessionId);
319
+ }
320
+
266
321
  /**
267
- * plugin runtime 调用主入口。
322
+ * 返回当前执行中的 sessionId 列表。
268
323
  */
269
- invoke: InvokePluginPort;
324
+ listExecutingSessionIds(): string[] {
325
+ return this.session.listExecutingSessionIds();
326
+ }
327
+
270
328
  /**
271
- * Plugin 调用入口。
329
+ * 返回当前执行中的 session 数量。
272
330
  */
273
- plugins: AgentPlugins;
331
+ getExecutingSessionCount(): number {
332
+ return this.session.getExecutingSessionCount();
333
+ }
334
+
335
+ /**
336
+ * 跨 plugin runtime 调用 action 的内部实现。
337
+ *
338
+ * 关键点(中文)
339
+ * - 统一把 `runAction` 的成功/失败结果归一化为 `InvokePluginResult`。
340
+ * - 这里替代了原 `createAgentContext` 工厂里的胶水匿名函数。
341
+ */
342
+ private async invoke_plugin_action(
343
+ params: InvokePluginParams,
344
+ ): Promise<InvokePluginResult> {
345
+ const result = await this.plugins.runAction({
346
+ plugin: params.plugin,
347
+ action: params.action,
348
+ ...(params.payload !== undefined ? { payload: params.payload } : {}),
349
+ });
350
+ if (!result.success) {
351
+ return {
352
+ success: false,
353
+ error: result.error || result.message || "plugin action failed",
354
+ };
355
+ }
356
+ return {
357
+ success: true,
358
+ ...(result.data !== undefined ? { data: result.data } : {}),
359
+ };
360
+ }
274
361
  }
275
362
 
276
363
  /**
@@ -27,7 +27,7 @@ export interface PlatformAgentOption {
27
27
  projectRoot: string;
28
28
 
29
29
  /**
30
- * 当前 Town 托管的 Agent 进程是否存活。
30
+ * 当前 downcity 托管的 Agent 进程是否存活。
31
31
  */
32
32
  running: boolean;
33
33
 
@@ -62,12 +62,12 @@ export interface PlatformAgentOption {
62
62
  stoppedAt?: string;
63
63
 
64
64
  /**
65
- * 当前读取到的 Town 托管进程 pid(仅 running=true 时有值)。
65
+ * 当前读取到的 downcity 托管进程 pid(仅 running=true 时有值)。
66
66
  */
67
67
  daemonPid?: number;
68
68
 
69
69
  /**
70
- * Town 托管进程日志路径(用于排障展示)。
70
+ * downcity 托管进程日志路径(用于排障展示)。
71
71
  */
72
72
  logPath?: string;
73
73
 
@@ -168,7 +168,7 @@ export interface PlatformAgentDirectoryInspection {
168
168
  knownAgent: boolean;
169
169
 
170
170
  /**
171
- * 若已登记,当前 Town 托管进程是否运行中。
171
+ * 若已登记,当前 downcity 托管进程是否运行中。
172
172
  */
173
173
  running: boolean;
174
174
 
@@ -356,7 +356,7 @@ export interface ManagedAgentRegistryEntry {
356
356
  projectRoot: string;
357
357
 
358
358
  /**
359
- * registry 最近一次记录的 Town 托管进程 pid。
359
+ * registry 最近一次记录的 downcity 托管进程 pid。
360
360
  */
361
361
  pid: number;
362
362
 
@@ -402,7 +402,7 @@ export interface ManagedAgentRegistryV1 {
402
402
  }
403
403
 
404
404
  /**
405
- * `town agent list --running` 输出可复用的受管 agent 运行态视图。
405
+ * `downcity agent list --running` 输出可复用的受管 agent 运行态视图。
406
406
  */
407
407
  export interface ManagedAgentProcessView {
408
408
  /**
@@ -416,7 +416,7 @@ export interface ManagedAgentProcessView {
416
416
  registeredPid: number;
417
417
 
418
418
  /**
419
- * 当前实际存活的 Town 托管进程 pid。
419
+ * 当前实际存活的 downcity 托管进程 pid。
420
420
  */
421
421
  daemonPid: number;
422
422
 
@@ -436,7 +436,7 @@ export interface ManagedAgentProcessView {
436
436
  updatedAt: string;
437
437
 
438
438
  /**
439
- * Town 托管进程日志文件路径。
439
+ * downcity 托管进程日志文件路径。
440
440
  */
441
441
  logPath: string;
442
442
  }
@@ -106,12 +106,12 @@ export interface PlatformAgentShipPluginsConfig {
106
106
  */
107
107
  export interface PlatformAgentShipStartConfig {
108
108
  /**
109
- * Town Agent HTTP gateway host。
109
+ * downcity Agent HTTP gateway host。
110
110
  */
111
111
  host?: unknown;
112
112
 
113
113
  /**
114
- * Town Agent HTTP gateway port。
114
+ * downcity Agent HTTP gateway port。
115
115
  */
116
116
  port?: unknown;
117
117
  }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Agent Session stop 类型定义。
3
+ *
4
+ * 关键点(中文)
5
+ * - `session.stop()` 用于停止当前正在执行的 turn。
6
+ * - 尚未被当前 turn 吸收的排队 prompt 会被取消,不会自动开启下一轮。
7
+ */
8
+
9
+ /**
10
+ * Session stop 的最终结果。
11
+ */
12
+ export interface AgentSessionStopResult {
13
+ /**
14
+ * 本次调用是否实际停止了活跃 turn 或取消了排队 prompt。
15
+ */
16
+ stopped: boolean;
17
+
18
+ /**
19
+ * 被停止的当前 turn 标识。
20
+ *
21
+ * 说明(中文)
22
+ * - 当前没有活跃 turn 时不会返回该字段。
23
+ * - 如果只是清空尚未启动的队列,也不会返回该字段。
24
+ */
25
+ turnId?: string;
26
+
27
+ /**
28
+ * 本次 stop 取消的未绑定 prompt 数量。
29
+ *
30
+ * 说明(中文)
31
+ * - 已经被当前 turn 吸收的 prompt 不计入这里,它们会随当前 turn 一起结束。
32
+ */
33
+ cancelledQueuedPrompts: number;
34
+
35
+ /**
36
+ * 本次 stop 的归因。
37
+ */
38
+ reason: "stopped" | "idle";
39
+ }