@downcity/agent 1.1.338 → 1.1.400

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 (987) hide show
  1. package/README.md +31 -15
  2. package/bin/agent/Agent.d.ts +42 -91
  3. package/bin/agent/Agent.d.ts.map +1 -1
  4. package/bin/agent/Agent.js +115 -200
  5. package/bin/agent/Agent.js.map +1 -1
  6. package/bin/agent/AgentModel.d.ts +8 -8
  7. package/bin/agent/AgentModel.d.ts.map +1 -1
  8. package/bin/agent/AgentModel.js +5 -5
  9. package/bin/agent/AgentModel.js.map +1 -1
  10. package/bin/agent/AgentSessions.d.ts +42 -28
  11. package/bin/agent/AgentSessions.d.ts.map +1 -1
  12. package/bin/agent/AgentSessions.js +80 -44
  13. package/bin/agent/AgentSessions.js.map +1 -1
  14. package/bin/agent/WorkspaceEntry.d.ts +82 -0
  15. package/bin/agent/WorkspaceEntry.d.ts.map +1 -0
  16. package/bin/agent/WorkspaceEntry.js +246 -0
  17. package/bin/agent/WorkspaceEntry.js.map +1 -0
  18. package/bin/agent/index.d.ts +5 -0
  19. package/bin/agent/index.d.ts.map +1 -0
  20. package/bin/agent/index.js +5 -0
  21. package/bin/agent/index.js.map +1 -0
  22. package/bin/city/host/CityHostRegistry.d.ts +44 -0
  23. package/bin/city/host/CityHostRegistry.d.ts.map +1 -0
  24. package/bin/city/host/CityHostRegistry.js +225 -0
  25. package/bin/city/host/CityHostRegistry.js.map +1 -0
  26. package/bin/city/index.d.ts +22 -0
  27. package/bin/city/index.d.ts.map +1 -0
  28. package/bin/city/index.js +10 -0
  29. package/bin/city/index.js.map +1 -0
  30. package/bin/city/runtime/City.d.ts +99 -0
  31. package/bin/city/runtime/City.d.ts.map +1 -0
  32. package/bin/city/runtime/City.js +376 -0
  33. package/bin/city/runtime/City.js.map +1 -0
  34. package/bin/city/transport/SerializedTransport.d.ts +30 -0
  35. package/bin/city/transport/SerializedTransport.d.ts.map +1 -0
  36. package/bin/city/transport/SerializedTransport.js +57 -0
  37. package/bin/city/transport/SerializedTransport.js.map +1 -0
  38. package/bin/city/transport/http/AgentHTTP.d.ts +66 -0
  39. package/bin/city/transport/http/AgentHTTP.d.ts.map +1 -0
  40. package/bin/city/transport/http/AgentHTTP.js +142 -0
  41. package/bin/city/transport/http/AgentHTTP.js.map +1 -0
  42. package/bin/city/transport/http/CityHTTP.d.ts +44 -0
  43. package/bin/city/transport/http/CityHTTP.d.ts.map +1 -0
  44. package/bin/city/transport/http/CityHTTP.js +171 -0
  45. package/bin/city/transport/http/CityHTTP.js.map +1 -0
  46. package/bin/city/transport/http/NodeHttpAdapter.d.ts +27 -0
  47. package/bin/city/transport/http/NodeHttpAdapter.d.ts.map +1 -0
  48. package/bin/city/transport/http/NodeHttpAdapter.js +57 -0
  49. package/bin/city/transport/http/NodeHttpAdapter.js.map +1 -0
  50. package/bin/city/transport/http/routes/RuntimeRoutes.d.ts +15 -0
  51. package/bin/city/transport/http/routes/RuntimeRoutes.d.ts.map +1 -0
  52. package/bin/city/transport/http/routes/RuntimeRoutes.js +38 -0
  53. package/bin/city/transport/http/routes/RuntimeRoutes.js.map +1 -0
  54. package/bin/city/transport/http/routes/SessionRoutes.d.ts +17 -0
  55. package/bin/city/transport/http/routes/SessionRoutes.d.ts.map +1 -0
  56. package/bin/city/transport/http/routes/SessionRoutes.js +417 -0
  57. package/bin/city/transport/http/routes/SessionRoutes.js.map +1 -0
  58. package/bin/city/transport/rpc/AgentRPC.d.ts +47 -0
  59. package/bin/city/transport/rpc/AgentRPC.d.ts.map +1 -0
  60. package/bin/city/transport/rpc/AgentRPC.js +89 -0
  61. package/bin/city/transport/rpc/AgentRPC.js.map +1 -0
  62. package/bin/city/transport/rpc/CityRPC.d.ts +26 -0
  63. package/bin/city/transport/rpc/CityRPC.d.ts.map +1 -0
  64. package/bin/city/transport/rpc/CityRPC.js +110 -0
  65. package/bin/city/transport/rpc/CityRPC.js.map +1 -0
  66. package/bin/city/transport/rpc/RpcServer.d.ts +30 -0
  67. package/bin/city/transport/rpc/RpcServer.d.ts.map +1 -0
  68. package/bin/city/transport/rpc/RpcServer.js +125 -0
  69. package/bin/city/transport/rpc/RpcServer.js.map +1 -0
  70. package/bin/city/transport/rpc/server/InternalHandlers.d.ts +22 -0
  71. package/bin/city/transport/rpc/server/InternalHandlers.d.ts.map +1 -0
  72. package/bin/city/transport/rpc/server/InternalHandlers.js +196 -0
  73. package/bin/city/transport/rpc/server/InternalHandlers.js.map +1 -0
  74. package/bin/city/transport/rpc/server/RequestDispatcher.d.ts +27 -0
  75. package/bin/city/transport/rpc/server/RequestDispatcher.d.ts.map +1 -0
  76. package/bin/city/transport/rpc/server/RequestDispatcher.js +38 -0
  77. package/bin/city/transport/rpc/server/RequestDispatcher.js.map +1 -0
  78. package/bin/city/transport/rpc/server/SdkSessionHandlers.d.ts +25 -0
  79. package/bin/city/transport/rpc/server/SdkSessionHandlers.d.ts.map +1 -0
  80. package/bin/city/transport/rpc/server/SdkSessionHandlers.js +146 -0
  81. package/bin/city/transport/rpc/server/SdkSessionHandlers.js.map +1 -0
  82. package/bin/city/transport/rpc/server/ServerTypes.d.ts +94 -0
  83. package/bin/city/transport/rpc/server/ServerTypes.d.ts.map +1 -0
  84. package/bin/city/transport/rpc/server/ServerTypes.js +9 -0
  85. package/bin/city/transport/rpc/server/ServerTypes.js.map +1 -0
  86. package/bin/city/transport/session/RemoteSessionConfig.d.ts +15 -0
  87. package/bin/city/transport/session/RemoteSessionConfig.d.ts.map +1 -0
  88. package/bin/city/transport/session/RemoteSessionConfig.js +23 -0
  89. package/bin/city/transport/session/RemoteSessionConfig.js.map +1 -0
  90. package/bin/city/transport/types/AgentHttpBinding.d.ts +24 -0
  91. package/bin/city/transport/types/AgentHttpBinding.d.ts.map +1 -0
  92. package/bin/city/transport/types/AgentHttpBinding.js +5 -0
  93. package/bin/city/transport/types/AgentHttpBinding.js.map +1 -0
  94. package/bin/city/transport/types/AgentHttpRuntime.d.ts +12 -0
  95. package/bin/city/transport/types/AgentHttpRuntime.d.ts.map +1 -0
  96. package/bin/city/transport/types/AgentHttpRuntime.js +7 -0
  97. package/bin/city/transport/types/AgentHttpRuntime.js.map +1 -0
  98. package/bin/city/transport/types/AgentRpcBinding.d.ts +27 -0
  99. package/bin/city/transport/types/AgentRpcBinding.d.ts.map +1 -0
  100. package/bin/city/transport/types/AgentRpcBinding.js +8 -0
  101. package/bin/city/transport/types/AgentRpcBinding.js.map +1 -0
  102. package/bin/city/transport/types/AgentRpcRuntime.d.ts +18 -0
  103. package/bin/city/transport/types/AgentRpcRuntime.d.ts.map +1 -0
  104. package/bin/city/transport/types/AgentRpcRuntime.js +7 -0
  105. package/bin/city/transport/types/AgentRpcRuntime.js.map +1 -0
  106. package/bin/city/transport/types/AgentSessionModelResolver.d.ts +11 -0
  107. package/bin/city/transport/types/AgentSessionModelResolver.d.ts.map +1 -0
  108. package/bin/city/transport/types/AgentSessionModelResolver.js +7 -0
  109. package/bin/city/transport/types/AgentSessionModelResolver.js.map +1 -0
  110. package/bin/city/transport/types/CityHttpRuntime.d.ts +36 -0
  111. package/bin/city/transport/types/CityHttpRuntime.d.ts.map +1 -0
  112. package/bin/city/transport/types/CityHttpRuntime.js +8 -0
  113. package/bin/city/transport/types/CityHttpRuntime.js.map +1 -0
  114. package/bin/city/transport/types/CityRpcRuntime.d.ts +17 -0
  115. package/bin/city/transport/types/CityRpcRuntime.d.ts.map +1 -0
  116. package/bin/city/transport/types/CityRpcRuntime.js +8 -0
  117. package/bin/city/transport/types/CityRpcRuntime.js.map +1 -0
  118. package/bin/city/transport/types/RpcProtocol.d.ts +24 -0
  119. package/bin/city/transport/types/RpcProtocol.d.ts.map +1 -0
  120. package/bin/city/transport/types/RpcProtocol.js +9 -0
  121. package/bin/city/transport/types/RpcProtocol.js.map +1 -0
  122. package/bin/city/types/City.d.ts +84 -0
  123. package/bin/city/types/City.d.ts.map +1 -0
  124. package/bin/city/types/City.js +7 -0
  125. package/bin/city/types/City.js.map +1 -0
  126. package/bin/city/types/PluginHostContext.d.ts +31 -0
  127. package/bin/city/types/PluginHostContext.d.ts.map +1 -0
  128. package/bin/city/types/PluginHostContext.js +11 -0
  129. package/bin/city/types/PluginHostContext.js.map +1 -0
  130. package/bin/city/types/SerializedTransport.d.ts +16 -0
  131. package/bin/city/types/SerializedTransport.d.ts.map +1 -0
  132. package/bin/city/types/SerializedTransport.js +3 -0
  133. package/bin/city/types/SerializedTransport.js.map +1 -0
  134. package/bin/executor/Executor.d.ts +4 -4
  135. package/bin/executor/Executor.d.ts.map +1 -1
  136. package/bin/executor/Executor.js +90 -34
  137. package/bin/executor/Executor.js.map +1 -1
  138. package/bin/executor/composer/system/SessionSystemComposer.d.ts +2 -2
  139. package/bin/executor/composer/system/SessionSystemComposer.d.ts.map +1 -1
  140. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts +3 -3
  141. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js +1 -1
  142. package/bin/executor/composer/system/default/PromptRenderer.d.ts +3 -3
  143. package/bin/executor/composer/system/default/PromptRenderer.d.ts.map +1 -1
  144. package/bin/executor/composer/system/default/PromptRenderer.js +1 -1
  145. package/bin/executor/composer/system/default/SystemDomain.d.ts +3 -3
  146. package/bin/executor/composer/system/default/SystemDomain.d.ts.map +1 -1
  147. package/bin/executor/composer/system/default/SystemDomain.js +3 -3
  148. package/bin/executor/composer/system/default/SystemDomain.js.map +1 -1
  149. package/bin/executor/composer/system/default/SystemPromptAssets.js +2 -2
  150. package/bin/executor/composer/system/default/assets/core.prompt.d.ts +1 -1
  151. package/bin/executor/composer/system/default/assets/core.prompt.d.ts.map +1 -1
  152. package/bin/executor/composer/system/default/assets/core.prompt.js +1 -1
  153. package/bin/executor/composer/system/default/assets/core.prompt.js.map +1 -1
  154. package/bin/executor/composer/system/default/variables/GeoContext.d.ts +1 -1
  155. package/bin/executor/composer/system/default/variables/VariableReplacer.js +1 -1
  156. package/bin/executor/core-engine/CoreEngineContextCompaction.d.ts +6 -23
  157. package/bin/executor/core-engine/CoreEngineContextCompaction.d.ts.map +1 -1
  158. package/bin/executor/core-engine/CoreEngineContextCompaction.js +112 -344
  159. package/bin/executor/core-engine/CoreEngineContextCompaction.js.map +1 -1
  160. package/bin/executor/core-engine/CoreEngineError.d.ts +1 -1
  161. package/bin/executor/core-engine/CoreEngineError.js +1 -1
  162. package/bin/executor/core-engine/CoreEngineLoopDecision.d.ts +2 -3
  163. package/bin/executor/core-engine/CoreEngineLoopDecision.d.ts.map +1 -1
  164. package/bin/executor/core-engine/CoreEngineLoopDecision.js +1 -15
  165. package/bin/executor/core-engine/CoreEngineLoopDecision.js.map +1 -1
  166. package/bin/executor/core-engine/CoreEngineMessageState.d.ts +16 -39
  167. package/bin/executor/core-engine/CoreEngineMessageState.d.ts.map +1 -1
  168. package/bin/executor/core-engine/CoreEngineMessageState.js +25 -60
  169. package/bin/executor/core-engine/CoreEngineMessageState.js.map +1 -1
  170. package/bin/executor/core-engine/CoreEngineRunner.d.ts +10 -6
  171. package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -1
  172. package/bin/executor/core-engine/CoreEngineRunner.js +146 -234
  173. package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
  174. package/bin/executor/core-engine/CoreEngineSignals.d.ts +20 -58
  175. package/bin/executor/core-engine/CoreEngineSignals.d.ts.map +1 -1
  176. package/bin/executor/core-engine/CoreEngineSignals.js +84 -304
  177. package/bin/executor/core-engine/CoreEngineSignals.js.map +1 -1
  178. package/bin/executor/messages/SessionAttachmentMapper.d.ts +6 -37
  179. package/bin/executor/messages/SessionAttachmentMapper.d.ts.map +1 -1
  180. package/bin/executor/messages/SessionAttachmentMapper.js +12 -243
  181. package/bin/executor/messages/SessionAttachmentMapper.js.map +1 -1
  182. package/bin/executor/messages/SessionMessageLog.d.ts +7 -14
  183. package/bin/executor/messages/SessionMessageLog.d.ts.map +1 -1
  184. package/bin/executor/messages/SessionMessageLog.js +11 -26
  185. package/bin/executor/messages/SessionMessageLog.js.map +1 -1
  186. package/bin/executor/messages/SessionModelMessages.d.ts +14 -0
  187. package/bin/executor/messages/SessionModelMessages.d.ts.map +1 -0
  188. package/bin/executor/messages/SessionModelMessages.js +187 -0
  189. package/bin/executor/messages/SessionModelMessages.js.map +1 -0
  190. package/bin/executor/model/ModelGenerate.d.ts +22 -0
  191. package/bin/executor/model/ModelGenerate.d.ts.map +1 -0
  192. package/bin/executor/model/ModelGenerate.js +54 -0
  193. package/bin/executor/model/ModelGenerate.js.map +1 -0
  194. package/bin/executor/model/ModelStepRunner.d.ts +70 -0
  195. package/bin/executor/model/ModelStepRunner.d.ts.map +1 -0
  196. package/bin/executor/model/ModelStepRunner.js +284 -0
  197. package/bin/executor/model/ModelStepRunner.js.map +1 -0
  198. package/bin/executor/services/ExecutorRecoveryPolicy.d.ts +4 -4
  199. package/bin/executor/services/ExecutorRecoveryPolicy.d.ts.map +1 -1
  200. package/bin/executor/types/SessionHistoryMeta.d.ts +7 -2
  201. package/bin/executor/types/SessionHistoryMeta.d.ts.map +1 -1
  202. package/bin/executor/types/SessionHistoryMeta.js +1 -1
  203. package/bin/executor/types/SessionLoop.d.ts +1 -21
  204. package/bin/executor/types/SessionLoop.d.ts.map +1 -1
  205. package/bin/executor/types/SessionPrompts.d.ts +8 -7
  206. package/bin/executor/types/SessionPrompts.d.ts.map +1 -1
  207. package/bin/executor/types/SessionPrompts.js +1 -2
  208. package/bin/executor/types/SessionPrompts.js.map +1 -1
  209. package/bin/group/Group.d.ts +20 -0
  210. package/bin/group/Group.d.ts.map +1 -0
  211. package/bin/group/Group.js +42 -0
  212. package/bin/group/Group.js.map +1 -0
  213. package/bin/group/GroupSession.d.ts +103 -0
  214. package/bin/group/GroupSession.d.ts.map +1 -0
  215. package/bin/group/GroupSession.js +666 -0
  216. package/bin/group/GroupSession.js.map +1 -0
  217. package/bin/group/GroupSessions.d.ts +21 -0
  218. package/bin/group/GroupSessions.d.ts.map +1 -0
  219. package/bin/group/GroupSessions.js +89 -0
  220. package/bin/group/GroupSessions.js.map +1 -0
  221. package/bin/group/index.d.ts +9 -0
  222. package/bin/group/index.d.ts.map +1 -0
  223. package/bin/group/index.js +6 -0
  224. package/bin/group/index.js.map +1 -0
  225. package/bin/index.d.ts +28 -20
  226. package/bin/index.d.ts.map +1 -1
  227. package/bin/index.js +7 -9
  228. package/bin/index.js.map +1 -1
  229. package/bin/internal/AgentRuntime.d.ts +43 -0
  230. package/bin/internal/AgentRuntime.d.ts.map +1 -0
  231. package/bin/internal/AgentRuntime.js +179 -0
  232. package/bin/internal/AgentRuntime.js.map +1 -0
  233. package/bin/internal/GroupRuntime.d.ts +17 -0
  234. package/bin/internal/GroupRuntime.d.ts.map +1 -0
  235. package/bin/internal/GroupRuntime.js +66 -0
  236. package/bin/internal/GroupRuntime.js.map +1 -0
  237. package/bin/internal/index.d.ts +8 -0
  238. package/bin/internal/index.d.ts.map +1 -0
  239. package/bin/internal/index.js +8 -0
  240. package/bin/internal/index.js.map +1 -0
  241. package/bin/plugin/core/ActionScheduleExecutor.d.ts +3 -2
  242. package/bin/plugin/core/ActionScheduleExecutor.d.ts.map +1 -1
  243. package/bin/plugin/core/ActionScheduleExecutor.js +14 -3
  244. package/bin/plugin/core/ActionScheduleExecutor.js.map +1 -1
  245. package/bin/plugin/core/ActionScheduleRuntime.d.ts +3 -1
  246. package/bin/plugin/core/ActionScheduleRuntime.d.ts.map +1 -1
  247. package/bin/plugin/core/ActionScheduleRuntime.js +9 -8
  248. package/bin/plugin/core/ActionScheduleRuntime.js.map +1 -1
  249. package/bin/plugin/core/ActionScheduleStore.d.ts +7 -3
  250. package/bin/plugin/core/ActionScheduleStore.d.ts.map +1 -1
  251. package/bin/plugin/core/ActionScheduleStore.js +32 -9
  252. package/bin/plugin/core/ActionScheduleStore.js.map +1 -1
  253. package/bin/plugin/core/HookRegistry.d.ts +4 -6
  254. package/bin/plugin/core/HookRegistry.d.ts.map +1 -1
  255. package/bin/plugin/core/HookRegistry.js +8 -14
  256. package/bin/plugin/core/HookRegistry.js.map +1 -1
  257. package/bin/plugin/core/PluginActionExecution.d.ts +33 -0
  258. package/bin/plugin/core/PluginActionExecution.d.ts.map +1 -0
  259. package/bin/plugin/core/PluginActionExecution.js +168 -0
  260. package/bin/plugin/core/PluginActionExecution.js.map +1 -0
  261. package/bin/plugin/core/PluginActionFactory.d.ts +6 -3
  262. package/bin/plugin/core/PluginActionFactory.d.ts.map +1 -1
  263. package/bin/plugin/core/PluginActionFactory.js +3 -0
  264. package/bin/plugin/core/PluginActionFactory.js.map +1 -1
  265. package/bin/plugin/core/PluginContext.d.ts +14 -4
  266. package/bin/plugin/core/PluginContext.d.ts.map +1 -1
  267. package/bin/plugin/core/PluginContext.js +11 -2
  268. package/bin/plugin/core/PluginContext.js.map +1 -1
  269. package/bin/plugin/core/PluginHttpRoutes.d.ts +1 -1
  270. package/bin/plugin/core/PluginHttpRoutes.d.ts.map +1 -1
  271. package/bin/plugin/core/PluginHttpRoutes.js +1 -1
  272. package/bin/plugin/core/PluginHttpRoutes.js.map +1 -1
  273. package/bin/plugin/core/PluginRegistry.d.ts +41 -26
  274. package/bin/plugin/core/PluginRegistry.d.ts.map +1 -1
  275. package/bin/plugin/core/PluginRegistry.js +125 -107
  276. package/bin/plugin/core/PluginRegistry.js.map +1 -1
  277. package/bin/plugin/tool/PluginToolRuntime.d.ts.map +1 -1
  278. package/bin/plugin/tool/PluginToolRuntime.js +4 -1
  279. package/bin/plugin/tool/PluginToolRuntime.js.map +1 -1
  280. package/bin/plugin/tool/PluginToolSchemas.d.ts +22 -2
  281. package/bin/plugin/tool/PluginToolSchemas.d.ts.map +1 -1
  282. package/bin/plugin/tool/PluginToolSchemas.js +3 -4
  283. package/bin/plugin/tool/PluginToolSchemas.js.map +1 -1
  284. package/bin/plugin/tool/PluginTools.d.ts +5 -8
  285. package/bin/plugin/tool/PluginTools.d.ts.map +1 -1
  286. package/bin/plugin/tool/PluginTools.js +9 -8
  287. package/bin/plugin/tool/PluginTools.js.map +1 -1
  288. package/bin/plugin/types/ActionSchedule.d.ts +6 -0
  289. package/bin/plugin/types/ActionSchedule.d.ts.map +1 -1
  290. package/bin/remote/RemoteAgent.js +2 -2
  291. package/bin/remote/RemoteSession.d.ts +1 -1
  292. package/bin/remote/TransportFactory.d.ts +1 -1
  293. package/bin/remote/TransportFactory.js +2 -2
  294. package/bin/remote/transports/HttpRemoteAgentTransport.d.ts +1 -1
  295. package/bin/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -1
  296. package/bin/remote/transports/HttpRemoteAgentTransport.js +3 -3
  297. package/bin/remote/transports/HttpRemoteAgentTransport.js.map +1 -1
  298. package/bin/remote/transports/RpcClient.d.ts +1 -0
  299. package/bin/remote/transports/RpcClient.d.ts.map +1 -1
  300. package/bin/remote/transports/RpcClient.js +16 -0
  301. package/bin/remote/transports/RpcClient.js.map +1 -1
  302. package/bin/remote/transports/RpcRemoteAgentTransport.d.ts +1 -1
  303. package/bin/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -1
  304. package/bin/remote/transports/RpcRemoteAgentTransport.js +2 -1
  305. package/bin/remote/transports/RpcRemoteAgentTransport.js.map +1 -1
  306. package/bin/session/DefaultSessionComposer.js +4 -4
  307. package/bin/session/DefaultSessionComposer.js.map +1 -1
  308. package/bin/session/Session.d.ts +9 -3
  309. package/bin/session/Session.d.ts.map +1 -1
  310. package/bin/session/Session.js +62 -53
  311. package/bin/session/Session.js.map +1 -1
  312. package/bin/session/SessionLoop.d.ts.map +1 -1
  313. package/bin/session/SessionLoop.js +23 -25
  314. package/bin/session/SessionLoop.js.map +1 -1
  315. package/bin/session/SessionMessages.d.ts +9 -11
  316. package/bin/session/SessionMessages.d.ts.map +1 -1
  317. package/bin/session/SessionMessages.js +56 -104
  318. package/bin/session/SessionMessages.js.map +1 -1
  319. package/bin/session/SessionQueue.d.ts +1 -1
  320. package/bin/session/SessionState.d.ts +3 -0
  321. package/bin/session/SessionState.d.ts.map +1 -1
  322. package/bin/session/SessionState.js +42 -12
  323. package/bin/session/SessionState.js.map +1 -1
  324. package/bin/session/SessionTitle.d.ts +6 -6
  325. package/bin/session/SessionTitle.d.ts.map +1 -1
  326. package/bin/session/SessionTitle.js +11 -39
  327. package/bin/session/SessionTitle.js.map +1 -1
  328. package/bin/session/browse/Browse.d.ts +6 -4
  329. package/bin/session/browse/Browse.d.ts.map +1 -1
  330. package/bin/session/browse/Browse.js +25 -43
  331. package/bin/session/browse/Browse.js.map +1 -1
  332. package/bin/session/control/SessionInteractions.d.ts +2 -2
  333. package/bin/session/control/SessionInteractions.d.ts.map +1 -1
  334. package/bin/session/control/SessionInteractions.js +68 -32
  335. package/bin/session/control/SessionInteractions.js.map +1 -1
  336. package/bin/session/execution/SessionAssistantOutputAdapter.d.ts +20 -17
  337. package/bin/session/execution/SessionAssistantOutputAdapter.d.ts.map +1 -1
  338. package/bin/session/execution/SessionAssistantOutputAdapter.js +35 -35
  339. package/bin/session/execution/SessionAssistantOutputAdapter.js.map +1 -1
  340. package/bin/session/execution/tools/SessionShellApprovalAdapter.d.ts +2 -2
  341. package/bin/session/execution/tools/SessionShellApprovalAdapter.d.ts.map +1 -1
  342. package/bin/session/execution/tools/SessionShellApprovalAdapter.js +20 -8
  343. package/bin/session/execution/tools/SessionShellApprovalAdapter.js.map +1 -1
  344. package/bin/session/messages/SessionAssistantMessageWriter.d.ts +42 -48
  345. package/bin/session/messages/SessionAssistantMessageWriter.d.ts.map +1 -1
  346. package/bin/session/messages/SessionAssistantMessageWriter.js +225 -543
  347. package/bin/session/messages/SessionAssistantMessageWriter.js.map +1 -1
  348. package/bin/session/messages/SessionJsonValue.d.ts +0 -10
  349. package/bin/session/messages/SessionJsonValue.d.ts.map +1 -1
  350. package/bin/session/messages/SessionJsonValue.js +0 -23
  351. package/bin/session/messages/SessionJsonValue.js.map +1 -1
  352. package/bin/session/messages/SessionMessageCompaction.d.ts +7 -3
  353. package/bin/session/messages/SessionMessageCompaction.d.ts.map +1 -1
  354. package/bin/session/messages/SessionMessageCompaction.js +69 -91
  355. package/bin/session/messages/SessionMessageCompaction.js.map +1 -1
  356. package/bin/session/messages/SessionMessageInteractionWriter.d.ts.map +1 -1
  357. package/bin/session/messages/SessionMessageInteractionWriter.js +22 -21
  358. package/bin/session/messages/SessionMessageInteractionWriter.js.map +1 -1
  359. package/bin/session/messages/SessionMessageText.d.ts +25 -0
  360. package/bin/session/messages/SessionMessageText.d.ts.map +1 -0
  361. package/bin/session/messages/SessionMessageText.js +52 -0
  362. package/bin/session/messages/SessionMessageText.js.map +1 -0
  363. package/bin/session/preview/SessionMessagePreview.d.ts +3 -3
  364. package/bin/session/preview/SessionMessagePreview.d.ts.map +1 -1
  365. package/bin/session/preview/SessionMessagePreview.js +10 -30
  366. package/bin/session/preview/SessionMessagePreview.js.map +1 -1
  367. package/bin/session/runtime/SessionTurnContext.d.ts.map +1 -1
  368. package/bin/session/runtime/SessionTurnContext.js +4 -2
  369. package/bin/session/runtime/SessionTurnContext.js.map +1 -1
  370. package/bin/session/storage/Metadata.d.ts +5 -3
  371. package/bin/session/storage/Metadata.d.ts.map +1 -1
  372. package/bin/session/storage/Metadata.js +26 -7
  373. package/bin/session/storage/Metadata.js.map +1 -1
  374. package/bin/tools/ask/AskQuestionsTool.d.ts +2 -16
  375. package/bin/tools/ask/AskQuestionsTool.d.ts.map +1 -1
  376. package/bin/tools/ask/AskQuestionsTool.js +21 -17
  377. package/bin/tools/ask/AskQuestionsTool.js.map +1 -1
  378. package/bin/types/action/ActionResult.d.ts +15 -10
  379. package/bin/types/action/ActionResult.d.ts.map +1 -1
  380. package/bin/types/action/ActionResult.js +2 -2
  381. package/bin/types/action/ActionResult.js.map +1 -1
  382. package/bin/types/agent/AgentOptions.d.ts +7 -14
  383. package/bin/types/agent/AgentOptions.d.ts.map +1 -1
  384. package/bin/types/agent/AgentSessionCollection.d.ts +42 -0
  385. package/bin/types/agent/AgentSessionCollection.d.ts.map +1 -0
  386. package/bin/types/agent/AgentSessionCollection.js +7 -0
  387. package/bin/types/agent/AgentSessionCollection.js.map +1 -0
  388. package/bin/types/agent/AgentStorage.d.ts +18 -0
  389. package/bin/types/agent/AgentStorage.d.ts.map +1 -0
  390. package/bin/types/agent/AgentStorage.js +8 -0
  391. package/bin/types/agent/AgentStorage.js.map +1 -0
  392. package/bin/types/agent/RemoteAgentOptions.d.ts +1 -1
  393. package/bin/types/agent/RemoteAgentPluginAction.d.ts +2 -2
  394. package/bin/types/agent/SessionActor.d.ts +15 -8
  395. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  396. package/bin/types/agent/SessionTypes.d.ts +30 -11
  397. package/bin/types/agent/SessionTypes.d.ts.map +1 -1
  398. package/bin/types/agent/WorkspaceEntryOptions.d.ts +16 -0
  399. package/bin/types/agent/WorkspaceEntryOptions.d.ts.map +1 -0
  400. package/bin/types/agent/WorkspaceEntryOptions.js +8 -0
  401. package/bin/types/agent/WorkspaceEntryOptions.js.map +1 -0
  402. package/bin/types/executor/SessionAssistantOutput.d.ts +19 -17
  403. package/bin/types/executor/SessionAssistantOutput.d.ts.map +1 -1
  404. package/bin/types/executor/SessionAssistantOutput.js +2 -3
  405. package/bin/types/executor/SessionAssistantOutput.js.map +1 -1
  406. package/bin/types/executor/SessionToolExecutionContext.d.ts +8 -5
  407. package/bin/types/executor/SessionToolExecutionContext.d.ts.map +1 -1
  408. package/bin/types/executor/SessionToolExecutionContext.js +1 -1
  409. package/bin/types/executor/SessionTurnContext.d.ts +16 -16
  410. package/bin/types/executor/SessionTurnContext.d.ts.map +1 -1
  411. package/bin/types/group/DispatchStrategy.d.ts +62 -0
  412. package/bin/types/group/DispatchStrategy.d.ts.map +1 -0
  413. package/bin/types/group/DispatchStrategy.js +93 -0
  414. package/bin/types/group/DispatchStrategy.js.map +1 -0
  415. package/bin/types/group/Group.d.ts +61 -0
  416. package/bin/types/group/Group.d.ts.map +1 -0
  417. package/bin/types/group/Group.js +3 -0
  418. package/bin/types/group/Group.js.map +1 -0
  419. package/bin/types/group/GroupSession.d.ts +149 -0
  420. package/bin/types/group/GroupSession.d.ts.map +1 -0
  421. package/bin/types/group/GroupSession.js +3 -0
  422. package/bin/types/group/GroupSession.js.map +1 -0
  423. package/bin/types/group/GroupSessionStore.d.ts +77 -0
  424. package/bin/types/group/GroupSessionStore.d.ts.map +1 -0
  425. package/bin/types/group/GroupSessionStore.js +3 -0
  426. package/bin/types/group/GroupSessionStore.js.map +1 -0
  427. package/bin/types/plugin/AgentPluginContext.d.ts +25 -0
  428. package/bin/types/plugin/AgentPluginContext.d.ts.map +1 -0
  429. package/bin/types/plugin/AgentPluginContext.js +10 -0
  430. package/bin/types/plugin/AgentPluginContext.js.map +1 -0
  431. package/bin/types/plugin/PluginAction.d.ts +10 -8
  432. package/bin/types/plugin/PluginAction.d.ts.map +1 -1
  433. package/bin/types/plugin/PluginActionExecution.d.ts +31 -0
  434. package/bin/types/plugin/PluginActionExecution.d.ts.map +1 -0
  435. package/bin/types/plugin/PluginActionExecution.js +10 -0
  436. package/bin/types/plugin/PluginActionExecution.js.map +1 -0
  437. package/bin/types/plugin/PluginCommand.d.ts +5 -5
  438. package/bin/types/plugin/PluginCommand.d.ts.map +1 -1
  439. package/bin/types/plugin/PluginContext.d.ts +19 -8
  440. package/bin/types/plugin/PluginContext.d.ts.map +1 -1
  441. package/bin/types/plugin/PluginContext.js +1 -1
  442. package/bin/types/plugin/PluginDefinition.d.ts +6 -6
  443. package/bin/types/plugin/PluginExecutionContext.d.ts +4 -10
  444. package/bin/types/plugin/PluginExecutionContext.d.ts.map +1 -1
  445. package/bin/types/plugin/PluginExecutionContext.js +3 -5
  446. package/bin/types/plugin/PluginExecutionContext.js.map +1 -1
  447. package/bin/types/plugin/PluginHttp.d.ts +1 -1
  448. package/bin/types/plugin/PluginRuntime.d.ts +13 -8
  449. package/bin/types/plugin/PluginRuntime.d.ts.map +1 -1
  450. package/bin/types/plugin/PluginServices.d.ts +10 -0
  451. package/bin/types/plugin/PluginServices.d.ts.map +1 -0
  452. package/bin/types/plugin/PluginServices.js +3 -0
  453. package/bin/types/plugin/PluginServices.js.map +1 -0
  454. package/bin/types/plugin/PluginState.d.ts +3 -3
  455. package/bin/types/plugin/PluginState.d.ts.map +1 -1
  456. package/bin/types/plugin/PluginState.js +1 -1
  457. package/bin/types/plugin/PluginTool.d.ts +6 -4
  458. package/bin/types/plugin/PluginTool.d.ts.map +1 -1
  459. package/bin/types/rpc/RpcProtocol.d.ts +30 -8
  460. package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
  461. package/bin/types/sdk/AgentSessionAction.d.ts +6 -7
  462. package/bin/types/sdk/AgentSessionAction.d.ts.map +1 -1
  463. package/bin/types/sdk/AgentSessionAction.js +2 -3
  464. package/bin/types/sdk/AgentSessionAction.js.map +1 -1
  465. package/bin/types/sdk/AgentSessionPrompt.d.ts +7 -7
  466. package/bin/types/sdk/AgentSessionPrompt.d.ts.map +1 -1
  467. package/bin/types/sdk/AgentSessionPrompt.js +1 -4
  468. package/bin/types/sdk/AgentSessionPrompt.js.map +1 -1
  469. package/bin/types/session/SessionAction.d.ts +39 -0
  470. package/bin/types/session/SessionAction.d.ts.map +1 -0
  471. package/bin/types/session/SessionAction.js +8 -0
  472. package/bin/types/session/SessionAction.js.map +1 -0
  473. package/bin/types/session/SessionComposer.d.ts +10 -11
  474. package/bin/types/session/SessionComposer.d.ts.map +1 -1
  475. package/bin/types/session/SessionContent.d.ts +41 -0
  476. package/bin/types/session/SessionContent.d.ts.map +1 -0
  477. package/bin/types/session/SessionContent.js +8 -0
  478. package/bin/types/session/SessionContent.js.map +1 -0
  479. package/bin/types/session/SessionExecution.d.ts +9 -68
  480. package/bin/types/session/SessionExecution.d.ts.map +1 -1
  481. package/bin/types/session/SessionInteraction.d.ts +92 -58
  482. package/bin/types/session/SessionInteraction.d.ts.map +1 -1
  483. package/bin/types/session/SessionInteraction.js +11 -4
  484. package/bin/types/session/SessionInteraction.js.map +1 -1
  485. package/bin/types/session/SessionInteractions.d.ts +1 -1
  486. package/bin/types/session/SessionLocalState.d.ts +2 -2
  487. package/bin/types/session/SessionLoop.d.ts +5 -5
  488. package/bin/types/session/SessionLoop.d.ts.map +1 -1
  489. package/bin/types/session/SessionMessage.d.ts +27 -87
  490. package/bin/types/session/SessionMessage.d.ts.map +1 -1
  491. package/bin/types/session/SessionMessages.d.ts +14 -14
  492. package/bin/types/session/SessionMessages.d.ts.map +1 -1
  493. package/bin/types/session/SessionMutation.d.ts +2 -2
  494. package/bin/types/session/SessionOptions.d.ts +14 -9
  495. package/bin/types/session/SessionOptions.d.ts.map +1 -1
  496. package/bin/types/session/SessionOrigin.d.ts +19 -0
  497. package/bin/types/session/SessionOrigin.d.ts.map +1 -0
  498. package/bin/types/session/SessionOrigin.js +8 -0
  499. package/bin/types/session/SessionOrigin.js.map +1 -0
  500. package/bin/types/session/SessionPort.d.ts +16 -20
  501. package/bin/types/session/SessionPort.d.ts.map +1 -1
  502. package/bin/types/session/SessionSegment.d.ts +1 -1
  503. package/bin/types/session/SessionState.d.ts +9 -6
  504. package/bin/types/session/SessionState.d.ts.map +1 -1
  505. package/bin/types/session/SessionSystem.d.ts +1 -1
  506. package/bin/types/session/SessionTool.d.ts +12 -1
  507. package/bin/types/session/SessionTool.d.ts.map +1 -1
  508. package/bin/types/store/LocalStore.d.ts +19 -11
  509. package/bin/types/store/LocalStore.d.ts.map +1 -1
  510. package/bin/types/store/LocalStore.js +2 -2
  511. package/bin/types/store/SessionAttachmentStore.d.ts +4 -4
  512. package/bin/types/store/SessionAttachmentStore.js +3 -3
  513. package/bin/types/store/SessionDataStore.d.ts +85 -0
  514. package/bin/types/store/SessionDataStore.d.ts.map +1 -0
  515. package/bin/types/store/SessionDataStore.js +9 -0
  516. package/bin/types/store/SessionDataStore.js.map +1 -0
  517. package/bin/types/store/SessionStore.d.ts +24 -78
  518. package/bin/types/store/SessionStore.d.ts.map +1 -1
  519. package/bin/types/store/SessionStore.js +3 -3
  520. package/bin/types/tools/ToolActionExecutionContext.d.ts +30 -0
  521. package/bin/types/tools/ToolActionExecutionContext.d.ts.map +1 -0
  522. package/bin/types/tools/ToolActionExecutionContext.js +8 -0
  523. package/bin/types/tools/ToolActionExecutionContext.js.map +1 -0
  524. package/bin/types/tools/ask/AskQuestionsTool.d.ts +1 -1
  525. package/bin/utils/logger/Logger.d.ts +21 -9
  526. package/bin/utils/logger/Logger.d.ts.map +1 -1
  527. package/bin/utils/logger/Logger.js +18 -9
  528. package/bin/utils/logger/Logger.js.map +1 -1
  529. package/bin/workspace/WorkspacePaths.d.ts +3 -32
  530. package/bin/workspace/WorkspacePaths.d.ts.map +1 -1
  531. package/bin/workspace/WorkspacePaths.js +7 -46
  532. package/bin/workspace/WorkspacePaths.js.map +1 -1
  533. package/bin/workspace/store/JsonlSessionMessageStore.d.ts +1 -1
  534. package/bin/workspace/store/JsonlSessionMessageStore.d.ts.map +1 -1
  535. package/bin/workspace/store/LocalGroupSessionDataStore.d.ts +46 -0
  536. package/bin/workspace/store/LocalGroupSessionDataStore.d.ts.map +1 -0
  537. package/bin/workspace/store/LocalGroupSessionDataStore.js +197 -0
  538. package/bin/workspace/store/LocalGroupSessionDataStore.js.map +1 -0
  539. package/bin/workspace/store/LocalGroupSessionStore.d.ts +22 -0
  540. package/bin/workspace/store/LocalGroupSessionStore.d.ts.map +1 -0
  541. package/bin/workspace/store/LocalGroupSessionStore.js +75 -0
  542. package/bin/workspace/store/LocalGroupSessionStore.js.map +1 -0
  543. package/bin/workspace/store/LocalSessionAttachmentStore.d.ts +2 -2
  544. package/bin/workspace/store/LocalSessionAttachmentStore.d.ts.map +1 -1
  545. package/bin/workspace/store/LocalSessionAttachmentStore.js +2 -2
  546. package/bin/workspace/store/LocalSessionAttachmentStore.js.map +1 -1
  547. package/bin/workspace/store/LocalSessionDataStore.d.ts +45 -0
  548. package/bin/workspace/store/LocalSessionDataStore.d.ts.map +1 -0
  549. package/bin/workspace/store/LocalSessionDataStore.js +100 -0
  550. package/bin/workspace/store/LocalSessionDataStore.js.map +1 -0
  551. package/bin/workspace/store/LocalSessionStore.d.ts +38 -28
  552. package/bin/workspace/store/LocalSessionStore.d.ts.map +1 -1
  553. package/bin/workspace/store/LocalSessionStore.js +151 -51
  554. package/bin/workspace/store/LocalSessionStore.js.map +1 -1
  555. package/bin/workspace/store/LocalStorePaths.d.ts +31 -83
  556. package/bin/workspace/store/LocalStorePaths.d.ts.map +1 -1
  557. package/bin/workspace/store/LocalStorePaths.js +43 -101
  558. package/bin/workspace/store/LocalStorePaths.js.map +1 -1
  559. package/bin/workspace/store/SessionMessageStoreFactory.d.ts +1 -1
  560. package/bin/workspace/store/SessionMessageStoreFactory.d.ts.map +1 -1
  561. package/bin/workspace/store/SessionMessageStoreFactory.js +1 -1
  562. package/city-test/agent-http.test.mjs +326 -0
  563. package/city-test/city-container.test.mjs +156 -0
  564. package/city-test/city-host-registry.test.mjs +68 -0
  565. package/city-test/city-transport.test.mjs +282 -0
  566. package/city-test/group.test.mjs +739 -0
  567. package/city-test/session-model.test.mjs +169 -0
  568. package/city-test/transport-lifecycle.test.mjs +164 -0
  569. package/package.json +30 -17
  570. package/scripts/ModelClientMock.mjs +165 -0
  571. package/scripts/action-schedule-store.test.mjs +81 -11
  572. package/scripts/agent-compiler.mjs +10 -6
  573. package/scripts/agent-env-shell-sandbox.test.mjs +10 -10
  574. package/scripts/agent-env.test.mjs +6 -8
  575. package/scripts/agent-multi-workspace.test.mjs +213 -0
  576. package/scripts/agent-ready.test.mjs +71 -16
  577. package/scripts/agent-session-maintenance.test.mjs +15 -28
  578. package/scripts/agent-store.test.mjs +121 -22
  579. package/scripts/city-model-tool-loop.test.mjs +66 -195
  580. package/scripts/core-engine-context-compaction.test.mjs +65 -113
  581. package/scripts/core-engine-model-stream.test.mjs +87 -0
  582. package/scripts/executor-failure-result.test.mjs +13 -29
  583. package/scripts/file-tools.test.mjs +6 -6
  584. package/scripts/image-plugin-job.test.mjs +77 -40
  585. package/scripts/logger-isolation.test.mjs +2 -2
  586. package/scripts/multi-agent-plugin-isolation.test.mjs +11 -9
  587. package/scripts/plugin-tools.test.mjs +153 -18
  588. package/scripts/public-api-naming.test.mjs +0 -1
  589. package/scripts/rewrite-build-aliases.mjs +49 -0
  590. package/scripts/rpc-url-routing.test.mjs +29 -0
  591. package/scripts/search-tools.test.mjs +4 -4
  592. package/scripts/session-ask-question.test.mjs +23 -21
  593. package/scripts/session-attachments.test.mjs +11 -9
  594. package/scripts/session-compact-handle.test.mjs +6 -5
  595. package/scripts/session-composer.test.mjs +61 -18
  596. package/scripts/session-config-turn-boundary.test.mjs +123 -92
  597. package/scripts/session-dynamic-interaction.test.mjs +75 -0
  598. package/scripts/session-fork.test.mjs +35 -0
  599. package/scripts/session-list-title.test.mjs +14 -56
  600. package/scripts/session-messages.test.mjs +306 -1681
  601. package/scripts/session-shell-approval.test.mjs +16 -13
  602. package/scripts/session-system-blocks.test.mjs +1 -1
  603. package/scripts/session-title-event.test.mjs +38 -24
  604. package/scripts/session-tool-approval.test.mjs +17 -15
  605. package/scripts/session-turn-context.test.mjs +31 -28
  606. package/scripts/session-turn-failure.test.mjs +112 -110
  607. package/scripts/verify-build-aliases.mjs +32 -0
  608. package/scripts/workspace.test.mjs +146 -32
  609. package/src/agent/Agent.ts +138 -238
  610. package/src/agent/AgentModel.ts +9 -9
  611. package/src/agent/AgentSessions.ts +116 -65
  612. package/src/agent/WorkspaceEntry.ts +309 -0
  613. package/src/agent/index.ts +4 -0
  614. package/src/city/host/CityHostRegistry.ts +260 -0
  615. package/src/city/index.ts +49 -0
  616. package/src/city/runtime/City.ts +425 -0
  617. package/src/city/transport/SerializedTransport.ts +65 -0
  618. package/src/city/transport/http/AgentHTTP.ts +176 -0
  619. package/src/city/transport/http/CityHTTP.ts +196 -0
  620. package/src/city/transport/http/NodeHttpAdapter.ts +75 -0
  621. package/src/city/transport/http/routes/RuntimeRoutes.ts +51 -0
  622. package/src/city/transport/http/routes/SessionRoutes.ts +497 -0
  623. package/src/city/transport/rpc/AgentRPC.ts +111 -0
  624. package/src/city/transport/rpc/CityRPC.ts +138 -0
  625. package/src/city/transport/rpc/RpcServer.ts +158 -0
  626. package/src/city/transport/rpc/server/InternalHandlers.ts +223 -0
  627. package/src/city/transport/rpc/server/RequestDispatcher.ts +67 -0
  628. package/src/city/transport/rpc/server/SdkSessionHandlers.ts +166 -0
  629. package/src/city/transport/rpc/server/ServerTypes.ts +103 -0
  630. package/src/city/transport/session/RemoteSessionConfig.ts +34 -0
  631. package/src/city/transport/types/AgentHttpBinding.ts +25 -0
  632. package/src/city/transport/types/AgentHttpRuntime.ts +13 -0
  633. package/src/city/transport/types/AgentRpcBinding.ts +28 -0
  634. package/src/city/transport/types/AgentRpcRuntime.ts +20 -0
  635. package/src/city/transport/types/AgentSessionModelResolver.ts +13 -0
  636. package/src/city/transport/types/CityHttpRuntime.ts +45 -0
  637. package/src/city/transport/types/CityRpcRuntime.ts +27 -0
  638. package/src/city/transport/types/RpcProtocol.ts +32 -0
  639. package/src/city/types/City.ts +99 -0
  640. package/src/city/types/PluginHostContext.ts +40 -0
  641. package/src/city/types/SerializedTransport.ts +19 -0
  642. package/src/executor/Executor.ts +101 -43
  643. package/src/executor/README.md +11 -8
  644. package/src/executor/composer/system/SessionSystemComposer.ts +2 -2
  645. package/src/executor/composer/system/default/PromptRenderer.ts +2 -2
  646. package/src/executor/composer/system/default/SystemDomain.ts +5 -5
  647. package/src/executor/composer/system/default/assets/core.prompt.ts +1 -1
  648. package/src/executor/composer/system/default/assets/core.prompt.ts.txt +4 -12
  649. package/src/executor/core-engine/CoreEngineContextCompaction.ts +113 -427
  650. package/src/executor/core-engine/CoreEngineError.ts +1 -1
  651. package/src/executor/core-engine/CoreEngineLoopDecision.ts +1 -18
  652. package/src/executor/core-engine/CoreEngineMessageState.ts +32 -101
  653. package/src/executor/core-engine/CoreEngineRunner.ts +179 -281
  654. package/src/executor/core-engine/CoreEngineSignals.ts +98 -346
  655. package/src/executor/messages/SessionAttachmentMapper.ts +15 -273
  656. package/src/executor/messages/SessionMessageLog.ts +12 -25
  657. package/src/executor/messages/SessionModelMessages.ts +223 -0
  658. package/src/executor/model/ModelGenerate.ts +71 -0
  659. package/src/executor/model/ModelStepRunner.ts +381 -0
  660. package/src/executor/services/ExecutorRecoveryPolicy.ts +4 -4
  661. package/src/executor/types/SessionHistoryMeta.ts +8 -2
  662. package/src/executor/types/SessionLoop.ts +0 -26
  663. package/src/executor/types/SessionPrompts.ts +8 -8
  664. package/src/group/Group.ts +45 -0
  665. package/src/group/GroupSession.ts +728 -0
  666. package/src/group/GroupSessions.ts +104 -0
  667. package/src/group/index.ts +29 -0
  668. package/src/index.ts +108 -60
  669. package/src/internal/AgentRuntime.ts +229 -0
  670. package/src/internal/GroupRuntime.ts +89 -0
  671. package/src/internal/index.ts +33 -0
  672. package/src/plugin/core/ActionScheduleExecutor.ts +17 -5
  673. package/src/plugin/core/ActionScheduleRuntime.ts +16 -6
  674. package/src/plugin/core/ActionScheduleStore.ts +43 -12
  675. package/src/plugin/core/HookRegistry.ts +24 -14
  676. package/src/plugin/core/PluginActionExecution.ts +230 -0
  677. package/src/plugin/core/PluginActionFactory.ts +9 -3
  678. package/src/plugin/core/PluginContext.ts +25 -6
  679. package/src/plugin/core/PluginHttpRoutes.ts +2 -2
  680. package/src/plugin/core/PluginRegistry.ts +193 -107
  681. package/src/plugin/tool/PluginToolRuntime.ts +4 -1
  682. package/src/plugin/tool/PluginToolSchemas.ts +3 -4
  683. package/src/plugin/tool/PluginTools.ts +12 -8
  684. package/src/plugin/types/ActionSchedule.ts +6 -0
  685. package/src/remote/transports/HttpRemoteAgentTransport.ts +3 -3
  686. package/src/remote/transports/RpcClient.ts +17 -0
  687. package/src/remote/transports/RpcRemoteAgentTransport.ts +1 -0
  688. package/src/session/DefaultSessionComposer.ts +3 -3
  689. package/src/session/Session.ts +54 -42
  690. package/src/session/SessionLoop.ts +27 -34
  691. package/src/session/SessionMessages.ts +63 -121
  692. package/src/session/SessionState.ts +46 -21
  693. package/src/session/SessionTitle.ts +23 -40
  694. package/src/session/browse/Browse.ts +35 -63
  695. package/src/session/control/SessionInteractions.ts +72 -51
  696. package/src/session/execution/SessionAssistantOutputAdapter.ts +44 -45
  697. package/src/session/execution/tools/SessionShellApprovalAdapter.ts +20 -9
  698. package/src/session/messages/SessionAssistantMessageWriter.ts +259 -637
  699. package/src/session/messages/SessionJsonValue.ts +0 -23
  700. package/src/session/messages/SessionMessageCompaction.ts +78 -103
  701. package/src/session/messages/SessionMessageInteractionWriter.ts +22 -22
  702. package/src/session/messages/SessionMessageText.ts +73 -0
  703. package/src/session/preview/SessionMessagePreview.ts +15 -45
  704. package/src/session/runtime/SessionTurnContext.ts +11 -9
  705. package/src/session/storage/Metadata.ts +38 -9
  706. package/src/tools/ask/AskQuestionsTool.ts +28 -17
  707. package/src/types/action/ActionResult.ts +20 -11
  708. package/src/types/agent/AgentOptions.ts +6 -13
  709. package/src/types/agent/AgentSessionCollection.ts +61 -0
  710. package/src/types/agent/AgentStorage.ts +19 -0
  711. package/src/types/agent/RemoteAgentOptions.ts +1 -1
  712. package/src/types/agent/SessionActor.ts +9 -0
  713. package/src/types/agent/SessionTypes.ts +31 -11
  714. package/src/types/agent/WorkspaceEntryOptions.ts +18 -0
  715. package/src/types/executor/SessionAssistantOutput.ts +22 -17
  716. package/src/types/executor/SessionToolExecutionContext.ts +9 -4
  717. package/src/types/executor/SessionTurnContext.ts +11 -11
  718. package/src/types/group/DispatchStrategy.ts +165 -0
  719. package/src/types/group/Group.ts +64 -0
  720. package/src/types/group/GroupSession.ts +154 -0
  721. package/src/types/group/GroupSessionStore.ts +82 -0
  722. package/src/types/plugin/AgentPluginContext.ts +30 -0
  723. package/src/types/plugin/PluginAction.ts +7 -5
  724. package/src/types/plugin/PluginActionExecution.ts +36 -0
  725. package/src/types/plugin/PluginCommand.ts +5 -5
  726. package/src/types/plugin/PluginContext.ts +23 -7
  727. package/src/types/plugin/PluginExecutionContext.ts +6 -11
  728. package/src/types/plugin/PluginRuntime.ts +5 -0
  729. package/src/types/plugin/PluginServices.ts +11 -0
  730. package/src/types/plugin/PluginState.ts +2 -2
  731. package/src/types/plugin/PluginTool.ts +4 -1
  732. package/src/types/rpc/RpcProtocol.ts +24 -2
  733. package/src/types/sdk/AgentSessionAction.ts +8 -9
  734. package/src/types/sdk/AgentSessionPrompt.ts +8 -11
  735. package/src/types/session/SessionAction.ts +41 -0
  736. package/src/types/session/SessionComposer.ts +8 -13
  737. package/src/types/session/SessionContent.ts +52 -0
  738. package/src/types/session/SessionExecution.ts +9 -88
  739. package/src/types/session/SessionInteraction.ts +106 -73
  740. package/src/types/session/SessionLoop.ts +1 -1
  741. package/src/types/session/SessionMessage.ts +27 -94
  742. package/src/types/session/SessionMessages.ts +12 -9
  743. package/src/types/session/SessionOptions.ts +11 -4
  744. package/src/types/session/SessionOrigin.ts +21 -0
  745. package/src/types/session/SessionPort.ts +12 -20
  746. package/src/types/session/SessionState.ts +7 -4
  747. package/src/types/session/SessionTool.ts +12 -0
  748. package/src/types/store/LocalStore.ts +19 -11
  749. package/src/types/store/SessionAttachmentStore.ts +4 -4
  750. package/src/types/store/SessionDataStore.ts +109 -0
  751. package/src/types/store/SessionStore.ts +37 -94
  752. package/src/types/tools/ToolActionExecutionContext.ts +32 -0
  753. package/src/utils/logger/Logger.ts +33 -12
  754. package/src/workspace/WorkspacePaths.ts +7 -51
  755. package/src/workspace/store/JsonlSessionMessageStore.ts +2 -2
  756. package/src/workspace/store/LocalGroupSessionDataStore.ts +232 -0
  757. package/src/workspace/store/LocalGroupSessionStore.ts +90 -0
  758. package/src/workspace/store/LocalSessionAttachmentStore.ts +3 -3
  759. package/src/workspace/store/LocalSessionDataStore.ts +169 -0
  760. package/src/workspace/store/LocalSessionStore.ts +202 -86
  761. package/src/workspace/store/LocalStorePaths.ts +65 -147
  762. package/src/workspace/store/SessionMessageStoreFactory.ts +1 -1
  763. package/tsconfig.json +2 -1
  764. package/tsconfig.tsbuildinfo +1 -1
  765. package/LICENSE +0 -183
  766. package/bin/agent/AgentState.d.ts +0 -45
  767. package/bin/agent/AgentState.d.ts.map +0 -1
  768. package/bin/agent/AgentState.js +0 -74
  769. package/bin/agent/AgentState.js.map +0 -1
  770. package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts +0 -48
  771. package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts.map +0 -1
  772. package/bin/executor/core-engine/CoreEngineUiStreamCollector.js +0 -92
  773. package/bin/executor/core-engine/CoreEngineUiStreamCollector.js.map +0 -1
  774. package/bin/executor/messages/SessionMessageCodec.d.ts +0 -29
  775. package/bin/executor/messages/SessionMessageCodec.d.ts.map +0 -1
  776. package/bin/executor/messages/SessionMessageCodec.js +0 -75
  777. package/bin/executor/messages/SessionMessageCodec.js.map +0 -1
  778. package/bin/executor/messages/SessionStepEventMapper.d.ts +0 -19
  779. package/bin/executor/messages/SessionStepEventMapper.d.ts.map +0 -1
  780. package/bin/executor/messages/SessionStepEventMapper.js +0 -177
  781. package/bin/executor/messages/SessionStepEventMapper.js.map +0 -1
  782. package/bin/executor/messages/UIMessageTransformer.d.ts +0 -29
  783. package/bin/executor/messages/UIMessageTransformer.d.ts.map +0 -1
  784. package/bin/executor/messages/UIMessageTransformer.js +0 -114
  785. package/bin/executor/messages/UIMessageTransformer.js.map +0 -1
  786. package/bin/executor/messages/UserVisibleText.d.ts +0 -26
  787. package/bin/executor/messages/UserVisibleText.d.ts.map +0 -1
  788. package/bin/executor/messages/UserVisibleText.js +0 -65
  789. package/bin/executor/messages/UserVisibleText.js.map +0 -1
  790. package/bin/executor/types/SessionRecords.d.ts +0 -162
  791. package/bin/executor/types/SessionRecords.d.ts.map +0 -1
  792. package/bin/executor/types/SessionRecords.js +0 -51
  793. package/bin/executor/types/SessionRecords.js.map +0 -1
  794. package/bin/plugin/core/PluginActionRunner.d.ts +0 -24
  795. package/bin/plugin/core/PluginActionRunner.d.ts.map +0 -1
  796. package/bin/plugin/core/PluginActionRunner.js +0 -19
  797. package/bin/plugin/core/PluginActionRunner.js.map +0 -1
  798. package/bin/session/messages/SessionMessageCodec.d.ts +0 -23
  799. package/bin/session/messages/SessionMessageCodec.d.ts.map +0 -1
  800. package/bin/session/messages/SessionMessageCodec.js +0 -455
  801. package/bin/session/messages/SessionMessageCodec.js.map +0 -1
  802. package/bin/types/agent/AgentState.d.ts +0 -19
  803. package/bin/types/agent/AgentState.d.ts.map +0 -1
  804. package/bin/types/agent/AgentState.js +0 -9
  805. package/bin/types/agent/AgentState.js.map +0 -1
  806. package/bin/types/store/AgentStore.d.ts +0 -31
  807. package/bin/types/store/AgentStore.d.ts.map +0 -1
  808. package/bin/types/store/AgentStore.js +0 -9
  809. package/bin/types/store/AgentStore.js.map +0 -1
  810. package/bin/types/workspace/FileSystem.d.ts +0 -55
  811. package/bin/types/workspace/FileSystem.d.ts.map +0 -1
  812. package/bin/types/workspace/FileSystem.js +0 -9
  813. package/bin/types/workspace/FileSystem.js.map +0 -1
  814. package/bin/types/workspace/FileTool.d.ts +0 -181
  815. package/bin/types/workspace/FileTool.d.ts.map +0 -1
  816. package/bin/types/workspace/FileTool.js +0 -10
  817. package/bin/types/workspace/FileTool.js.map +0 -1
  818. package/bin/types/workspace/SearchTool.d.ts +0 -131
  819. package/bin/types/workspace/SearchTool.d.ts.map +0 -1
  820. package/bin/types/workspace/SearchTool.js +0 -10
  821. package/bin/types/workspace/SearchTool.js.map +0 -1
  822. package/bin/types/workspace/Workspace.d.ts +0 -24
  823. package/bin/types/workspace/Workspace.d.ts.map +0 -1
  824. package/bin/types/workspace/Workspace.js +0 -9
  825. package/bin/types/workspace/Workspace.js.map +0 -1
  826. package/bin/types/workspace/WorkspaceEnv.d.ts +0 -16
  827. package/bin/types/workspace/WorkspaceEnv.d.ts.map +0 -1
  828. package/bin/types/workspace/WorkspaceEnv.js +0 -9
  829. package/bin/types/workspace/WorkspaceEnv.js.map +0 -1
  830. package/bin/types/workspace/WorkspaceTools.d.ts +0 -20
  831. package/bin/types/workspace/WorkspaceTools.d.ts.map +0 -1
  832. package/bin/types/workspace/WorkspaceTools.js +0 -9
  833. package/bin/types/workspace/WorkspaceTools.js.map +0 -1
  834. package/bin/utils/cli/CliOutput.d.ts +0 -14
  835. package/bin/utils/cli/CliOutput.d.ts.map +0 -1
  836. package/bin/utils/cli/CliOutput.js +0 -98
  837. package/bin/utils/cli/CliOutput.js.map +0 -1
  838. package/bin/utils/cli/PrettyStatus.d.ts +0 -25
  839. package/bin/utils/cli/PrettyStatus.d.ts.map +0 -1
  840. package/bin/utils/cli/PrettyStatus.js +0 -77
  841. package/bin/utils/cli/PrettyStatus.js.map +0 -1
  842. package/bin/utils/logger/FormatRequest.d.ts +0 -29
  843. package/bin/utils/logger/FormatRequest.d.ts.map +0 -1
  844. package/bin/utils/logger/FormatRequest.js +0 -309
  845. package/bin/utils/logger/FormatRequest.js.map +0 -1
  846. package/bin/utils/logger/FormatResponse.d.ts +0 -46
  847. package/bin/utils/logger/FormatResponse.d.ts.map +0 -1
  848. package/bin/utils/logger/FormatResponse.js +0 -186
  849. package/bin/utils/logger/FormatResponse.js.map +0 -1
  850. package/bin/utils/logger/FormatShared.d.ts +0 -33
  851. package/bin/utils/logger/FormatShared.d.ts.map +0 -1
  852. package/bin/utils/logger/FormatShared.js +0 -215
  853. package/bin/utils/logger/FormatShared.js.map +0 -1
  854. package/bin/utils/object/DeepMerge.d.ts +0 -18
  855. package/bin/utils/object/DeepMerge.d.ts.map +0 -1
  856. package/bin/utils/object/DeepMerge.js +0 -37
  857. package/bin/utils/object/DeepMerge.js.map +0 -1
  858. package/bin/utils/object/ObjectGuards.d.ts +0 -17
  859. package/bin/utils/object/ObjectGuards.d.ts.map +0 -1
  860. package/bin/utils/object/ObjectGuards.js +0 -19
  861. package/bin/utils/object/ObjectGuards.js.map +0 -1
  862. package/bin/utils/path/AncestorFiles.d.ts +0 -17
  863. package/bin/utils/path/AncestorFiles.d.ts.map +0 -1
  864. package/bin/utils/path/AncestorFiles.js +0 -36
  865. package/bin/utils/path/AncestorFiles.js.map +0 -1
  866. package/bin/utils/storage/index.d.ts +0 -4
  867. package/bin/utils/storage/index.d.ts.map +0 -1
  868. package/bin/utils/storage/index.js +0 -17
  869. package/bin/utils/storage/index.js.map +0 -1
  870. package/bin/utils/string/EscapeRegExp.d.ts +0 -17
  871. package/bin/utils/string/EscapeRegExp.d.ts.map +0 -1
  872. package/bin/utils/string/EscapeRegExp.js +0 -19
  873. package/bin/utils/string/EscapeRegExp.js.map +0 -1
  874. package/bin/workspace/LocalFileSystem.d.ts +0 -44
  875. package/bin/workspace/LocalFileSystem.d.ts.map +0 -1
  876. package/bin/workspace/LocalFileSystem.js +0 -153
  877. package/bin/workspace/LocalFileSystem.js.map +0 -1
  878. package/bin/workspace/Workspace.d.ts +0 -53
  879. package/bin/workspace/Workspace.d.ts.map +0 -1
  880. package/bin/workspace/Workspace.js +0 -151
  881. package/bin/workspace/Workspace.js.map +0 -1
  882. package/bin/workspace/WorkspaceBase.d.ts +0 -41
  883. package/bin/workspace/WorkspaceBase.d.ts.map +0 -1
  884. package/bin/workspace/WorkspaceBase.js +0 -19
  885. package/bin/workspace/WorkspaceBase.js.map +0 -1
  886. package/bin/workspace/WorkspaceEnv.d.ts +0 -19
  887. package/bin/workspace/WorkspaceEnv.d.ts.map +0 -1
  888. package/bin/workspace/WorkspaceEnv.js +0 -50
  889. package/bin/workspace/WorkspaceEnv.js.map +0 -1
  890. package/bin/workspace/file/FileActionRuntime.d.ts +0 -14
  891. package/bin/workspace/file/FileActionRuntime.d.ts.map +0 -1
  892. package/bin/workspace/file/FileActionRuntime.js +0 -386
  893. package/bin/workspace/file/FileActionRuntime.js.map +0 -1
  894. package/bin/workspace/file/FileAtomicWriter.d.ts +0 -20
  895. package/bin/workspace/file/FileAtomicWriter.d.ts.map +0 -1
  896. package/bin/workspace/file/FileAtomicWriter.js +0 -50
  897. package/bin/workspace/file/FileAtomicWriter.js.map +0 -1
  898. package/bin/workspace/file/FileEncoding.d.ts +0 -34
  899. package/bin/workspace/file/FileEncoding.d.ts.map +0 -1
  900. package/bin/workspace/file/FileEncoding.js +0 -152
  901. package/bin/workspace/file/FileEncoding.js.map +0 -1
  902. package/bin/workspace/file/FilePathPolicy.d.ts +0 -26
  903. package/bin/workspace/file/FilePathPolicy.d.ts.map +0 -1
  904. package/bin/workspace/file/FilePathPolicy.js +0 -134
  905. package/bin/workspace/file/FilePathPolicy.js.map +0 -1
  906. package/bin/workspace/file/FileToolError.d.ts +0 -30
  907. package/bin/workspace/file/FileToolError.d.ts.map +0 -1
  908. package/bin/workspace/file/FileToolError.js +0 -78
  909. package/bin/workspace/file/FileToolError.js.map +0 -1
  910. package/bin/workspace/search/SearchActionRuntime.d.ts +0 -14
  911. package/bin/workspace/search/SearchActionRuntime.d.ts.map +0 -1
  912. package/bin/workspace/search/SearchActionRuntime.js +0 -359
  913. package/bin/workspace/search/SearchActionRuntime.js.map +0 -1
  914. package/bin/workspace/search/SearchToolError.d.ts +0 -26
  915. package/bin/workspace/search/SearchToolError.d.ts.map +0 -1
  916. package/bin/workspace/search/SearchToolError.js +0 -76
  917. package/bin/workspace/search/SearchToolError.js.map +0 -1
  918. package/bin/workspace/store/LocalAgentStore.d.ts +0 -43
  919. package/bin/workspace/store/LocalAgentStore.d.ts.map +0 -1
  920. package/bin/workspace/store/LocalAgentStore.js +0 -135
  921. package/bin/workspace/store/LocalAgentStore.js.map +0 -1
  922. package/bin/workspace/tool/FileToolSchemas.d.ts +0 -35
  923. package/bin/workspace/tool/FileToolSchemas.d.ts.map +0 -1
  924. package/bin/workspace/tool/FileToolSchemas.js +0 -73
  925. package/bin/workspace/tool/FileToolSchemas.js.map +0 -1
  926. package/bin/workspace/tool/FileTools.d.ts +0 -11
  927. package/bin/workspace/tool/FileTools.d.ts.map +0 -1
  928. package/bin/workspace/tool/FileTools.js +0 -61
  929. package/bin/workspace/tool/FileTools.js.map +0 -1
  930. package/bin/workspace/tool/SearchToolSchemas.d.ts +0 -24
  931. package/bin/workspace/tool/SearchToolSchemas.d.ts.map +0 -1
  932. package/bin/workspace/tool/SearchToolSchemas.js +0 -60
  933. package/bin/workspace/tool/SearchToolSchemas.js.map +0 -1
  934. package/bin/workspace/tool/SearchTools.d.ts +0 -11
  935. package/bin/workspace/tool/SearchTools.d.ts.map +0 -1
  936. package/bin/workspace/tool/SearchTools.js +0 -38
  937. package/bin/workspace/tool/SearchTools.js.map +0 -1
  938. package/bin/workspace/tool/WorkspaceTools.d.ts +0 -11
  939. package/bin/workspace/tool/WorkspaceTools.d.ts.map +0 -1
  940. package/bin/workspace/tool/WorkspaceTools.js +0 -22
  941. package/bin/workspace/tool/WorkspaceTools.js.map +0 -1
  942. package/scripts/core-engine-ui-stream-collector.test.mjs +0 -38
  943. package/scripts/openai-responses-continuation.test.mjs +0 -78
  944. package/src/agent/AgentState.ts +0 -96
  945. package/src/executor/core-engine/CoreEngineUiStreamCollector.ts +0 -136
  946. package/src/executor/messages/SessionMessageCodec.ts +0 -106
  947. package/src/executor/messages/SessionStepEventMapper.ts +0 -191
  948. package/src/executor/messages/UIMessageTransformer.ts +0 -152
  949. package/src/executor/messages/UserVisibleText.ts +0 -75
  950. package/src/executor/types/SessionRecords.ts +0 -217
  951. package/src/plugin/core/PluginActionRunner.ts +0 -36
  952. package/src/session/messages/SessionMessageCodec.ts +0 -495
  953. package/src/types/agent/AgentState.ts +0 -22
  954. package/src/types/store/AgentStore.ts +0 -52
  955. package/src/types/workspace/FileSystem.ts +0 -76
  956. package/src/types/workspace/FileTool.ts +0 -225
  957. package/src/types/workspace/SearchTool.ts +0 -156
  958. package/src/types/workspace/Workspace.ts +0 -27
  959. package/src/types/workspace/WorkspaceEnv.ts +0 -19
  960. package/src/types/workspace/WorkspaceTools.ts +0 -23
  961. package/src/utils/cli/CliOutput.ts +0 -128
  962. package/src/utils/cli/PrettyStatus.ts +0 -94
  963. package/src/utils/logger/FormatRequest.ts +0 -455
  964. package/src/utils/logger/FormatResponse.ts +0 -265
  965. package/src/utils/logger/FormatShared.ts +0 -258
  966. package/src/utils/object/DeepMerge.ts +0 -40
  967. package/src/utils/object/ObjectGuards.ts +0 -19
  968. package/src/utils/path/AncestorFiles.ts +0 -40
  969. package/src/utils/storage/index.ts +0 -19
  970. package/src/utils/string/EscapeRegExp.ts +0 -19
  971. package/src/workspace/LocalFileSystem.ts +0 -209
  972. package/src/workspace/Workspace.ts +0 -178
  973. package/src/workspace/WorkspaceBase.ts +0 -62
  974. package/src/workspace/WorkspaceEnv.ts +0 -54
  975. package/src/workspace/file/FileActionRuntime.ts +0 -433
  976. package/src/workspace/file/FileAtomicWriter.ts +0 -61
  977. package/src/workspace/file/FileEncoding.ts +0 -175
  978. package/src/workspace/file/FilePathPolicy.ts +0 -162
  979. package/src/workspace/file/FileToolError.ts +0 -99
  980. package/src/workspace/search/SearchActionRuntime.ts +0 -438
  981. package/src/workspace/search/SearchToolError.ts +0 -94
  982. package/src/workspace/store/LocalAgentStore.ts +0 -191
  983. package/src/workspace/tool/FileToolSchemas.ts +0 -77
  984. package/src/workspace/tool/FileTools.ts +0 -91
  985. package/src/workspace/tool/SearchToolSchemas.ts +0 -62
  986. package/src/workspace/tool/SearchTools.ts +0 -61
  987. package/src/workspace/tool/WorkspaceTools.ts +0 -31
@@ -0,0 +1,728 @@
1
+ /** GroupSession:一次 Group 群聊上下文的运行时实现。 */
2
+
3
+ import { nanoid } from "nanoid";
4
+ import type { Agent } from "@/agent/Agent.js";
5
+ import type { AgentSession } from "@/types/agent/SessionActor.js";
6
+ import type { GroupMessage } from "@/types/group/Group.js";
7
+ import type {
8
+ GroupMemberRuntime,
9
+ GroupEventSubscriber,
10
+ GroupEventUnsubscribe,
11
+ GroupPromptInput,
12
+ GroupPromptResult,
13
+ GroupTurnRuntime,
14
+ GroupSessionContract,
15
+ } from "@/types/group/GroupSession.js";
16
+ import type { DispatchDecision, DispatchNode, DispatchStrategy } from "@/types/group/DispatchStrategy.js";
17
+ import type { WorkspaceBase } from "@downcity/workspace";
18
+ import type { GroupSessionDataStore } from "@/types/group/GroupSessionStore.js";
19
+ import type { RespondSessionInteractionInput } from "@/types/session/SessionInteraction.js";
20
+
21
+ const max_auto_dispatch_count = 32;
22
+
23
+ /** GroupSession 的创建依赖。 */
24
+ export interface GroupSessionOptions {
25
+ /** 当前群聊上下文的稳定标识。 */
26
+ readonly id: string;
27
+ /** 所属 Group 的稳定标识。 */
28
+ readonly group_id: string;
29
+ /** Group 展示名称。 */
30
+ readonly group_name: string;
31
+ /** Group 协作说明。 */
32
+ readonly instruction?: string;
33
+ /** Group 成员。 */
34
+ readonly members: readonly Agent[];
35
+ /** Group 消息调度策略。 */
36
+ readonly dispatch_strategy: DispatchStrategy;
37
+ /** Group 为成员执行提供的共享 Workspace。 */
38
+ readonly workspace?: WorkspaceBase;
39
+ }
40
+
41
+ /** Group 的一个独立群聊上下文。 */
42
+ export class GroupSession implements GroupSessionContract {
43
+ readonly id: string;
44
+ readonly group_id: string;
45
+ readonly workspace_id?: string;
46
+
47
+ private readonly group_name: string;
48
+ private readonly instruction?: string;
49
+ private readonly members: readonly Agent[];
50
+ private readonly dispatch_strategy: DispatchStrategy;
51
+ private readonly workspace?: WorkspaceBase;
52
+ private readonly messages_by_id: GroupMessage[] = [];
53
+ private readonly subscribers = new Set<GroupEventSubscriber>();
54
+ private readonly member_sessions = new Map<string, AgentSession>();
55
+ private readonly member_session_ids = new Map<string, string>();
56
+ private readonly member_session_promises = new Map<string, Promise<AgentSession>>();
57
+ private readonly member_session_unsubscribes = new Map<string, () => void>();
58
+ private readonly interaction_sessions = new Map<string, AgentSession>();
59
+ private readonly member_running_counts = new Map<string, number>();
60
+ private readonly pending_deliveries = new Set<Promise<void>>();
61
+ private readonly active_member_deliveries = new Set<Promise<void>>();
62
+ private readonly group_turns_by_id = new Map<string, GroupTurnRuntime>();
63
+ private readonly consumed_auto_message_ids = new Set<string>();
64
+ private readonly auto_dispatch_path_keys = new Set<string>();
65
+ private auto_frontier_messages: readonly GroupMessage[] = [];
66
+ private store?: GroupSessionDataStore;
67
+ private auto_dispatch_promise: Promise<void> | null = null;
68
+ private auto_dispatch_requested = false;
69
+ private stop_requested = false;
70
+ private disposed = false;
71
+
72
+ constructor(options: GroupSessionOptions) {
73
+ this.id = String(options.id || "").trim();
74
+ this.group_id = String(options.group_id || "").trim();
75
+ if (!this.id) throw new Error("GroupSession requires a non-empty id");
76
+ if (!this.group_id) throw new Error("GroupSession requires a non-empty group_id");
77
+ this.group_name = options.group_name;
78
+ this.instruction = options.instruction;
79
+ this.members = options.members;
80
+ this.dispatch_strategy = options.dispatch_strategy;
81
+ this.workspace = options.workspace;
82
+ this.workspace_id = options.workspace?.id;
83
+ }
84
+
85
+ /** 从持久化 Store 初始化当前 GroupSession 的消息历史。 */
86
+ async initialize(store: GroupSessionDataStore): Promise<this> {
87
+ if (this.store && this.store !== store) {
88
+ throw new Error(`GroupSession "${this.id}" is already initialized with another Store`);
89
+ }
90
+ if (this.store === store) return this;
91
+ this.store = store;
92
+ await store.initialize();
93
+ const metadata = await store.read_metadata();
94
+ if (metadata.group_id !== this.group_id) {
95
+ throw new Error(`GroupSession "${this.id}" belongs to another Group`);
96
+ }
97
+ const requested_workspace_id = this.workspace?.id;
98
+ if (metadata.workspace_id && metadata.workspace_id !== requested_workspace_id) {
99
+ throw new Error(
100
+ `GroupSession "${this.id}" requires Workspace "${metadata.workspace_id}"`,
101
+ );
102
+ }
103
+ if (!metadata.workspace_id && requested_workspace_id) {
104
+ await store.update_metadata({ workspace_id: requested_workspace_id });
105
+ }
106
+ this.messages_by_id.push(...await store.list_messages());
107
+ for (const message of this.messages_by_id) this.consumed_auto_message_ids.add(message.id);
108
+ const pending_turns = metadata.pending_turns || [];
109
+ const pending_turn_ids = new Set(pending_turns.map((item) => item.turn_id));
110
+ for (const checkpoint of pending_turns) {
111
+ this.group_turns_by_id.set(checkpoint.turn_id, {
112
+ turn_id: checkpoint.turn_id,
113
+ root_message_id: checkpoint.root_message_id,
114
+ context_message_ids: checkpoint.context_message_ids,
115
+ auto_pending: true,
116
+ stopped: false,
117
+ recovered: true,
118
+ });
119
+ }
120
+ for (const message of this.messages_by_id) {
121
+ if (message.sender_type === "agent" && message.turn_id && pending_turn_ids.has(message.turn_id)) {
122
+ this.consumed_auto_message_ids.delete(message.id);
123
+ }
124
+ }
125
+ this.auto_frontier_messages = (metadata.auto_frontier_message_ids || [])
126
+ .map((message_id) => this.messages_by_id.find((message) => message.id === message_id))
127
+ .filter((message): message is GroupMessage => Boolean(message));
128
+ for (const message of this.auto_frontier_messages) this.consumed_auto_message_ids.delete(message.id);
129
+ const member_session_ids = metadata.member_session_ids || {};
130
+ const valid_member_session_ids: Record<string, string> = {};
131
+ for (const [agent_id, session_id] of Object.entries(member_session_ids)) {
132
+ const member = this.get_member(agent_id);
133
+ if (!member) continue;
134
+ try {
135
+ const session = await member.sessions.get(session_id, {
136
+ ...(this.workspace ? { workspace: this.workspace } : {}),
137
+ origin: {
138
+ type: "group",
139
+ group_id: this.group_id,
140
+ group_session_id: this.id,
141
+ },
142
+ });
143
+ this.member_sessions.set(agent_id, session);
144
+ this.member_session_ids.set(agent_id, session.id);
145
+ this.subscribe_member_session(agent_id, session);
146
+ valid_member_session_ids[agent_id] = session.id;
147
+ } catch {
148
+ // 成员已被删除或其 Session 不再匹配时,清理失效映射并继续恢复群聊。
149
+ }
150
+ }
151
+ if (Object.keys(valid_member_session_ids).length !== Object.keys(member_session_ids).length) {
152
+ await store.update_metadata({ member_session_ids: valid_member_session_ids });
153
+ }
154
+ if (this.group_turns_by_id.size > 0 || this.auto_frontier_messages.length > 0) {
155
+ this.request_auto_dispatch();
156
+ }
157
+ return this;
158
+ }
159
+
160
+ /** 立即追加用户消息,并异步触发本条消息的初始调度。 */
161
+ async prompt(input: GroupPromptInput): Promise<GroupPromptResult> {
162
+ this.assert_available();
163
+ const text = String(input?.query || "").trim();
164
+ if (!text) throw new Error("group_session.prompt requires a non-empty query");
165
+ const turn_id = `group-turn-${nanoid(12)}`;
166
+ const context_message_ids = this.messages_by_id.map((message) => message.id);
167
+ const message = await this.append_message({ sender_type: "user", sender_id: "user", text, turn_id });
168
+ const group_turn: GroupTurnRuntime = {
169
+ turn_id,
170
+ root_message_id: message.id,
171
+ context_message_ids: [...context_message_ids, message.id],
172
+ auto_pending: false,
173
+ stopped: false,
174
+ };
175
+ this.group_turns_by_id.set(turn_id, group_turn);
176
+ await this.persist_dispatch_checkpoint();
177
+ this.publish_status(turn_id, "dispatching", { message_id: message.id });
178
+ const dispatch = this.run_user_dispatch(message, turn_id);
179
+ group_turn.user_dispatch = dispatch;
180
+ this.pending_deliveries.add(dispatch);
181
+ void dispatch.finally(() => {
182
+ this.pending_deliveries.delete(dispatch);
183
+ if (group_turn.auto_pending) this.request_auto_dispatch();
184
+ });
185
+ return { turn_id, success: true, message_count: this.messages_by_id.length };
186
+ }
187
+
188
+ /** 读取共享消息快照。 */
189
+ async messages(): Promise<readonly GroupMessage[]> {
190
+ return this.messages_by_id.map((message) => ({ ...message }));
191
+ }
192
+
193
+ /** 订阅未来共享消息。 */
194
+ subscribe(subscriber: GroupEventSubscriber): GroupEventUnsubscribe {
195
+ this.assert_available();
196
+ this.subscribers.add(subscriber);
197
+ return () => this.subscribers.delete(subscriber);
198
+ }
199
+
200
+ async respond_interaction(input: RespondSessionInteractionInput): Promise<void> {
201
+ const session = this.interaction_sessions.get(input.interaction_id);
202
+ if (!session) throw new Error(`Group interaction not found: ${input.interaction_id}`);
203
+ await session.respond(input);
204
+ this.interaction_sessions.delete(input.interaction_id);
205
+ }
206
+
207
+ /** 读取成员运行态快照;成员是否回复由成员自身决定。 */
208
+ private member_statuses(): readonly GroupMemberRuntime[] {
209
+ return this.members.map((member) => ({
210
+ agent_id: member.id,
211
+ running: (this.member_running_counts.get(member.id) || 0) > 0,
212
+ }));
213
+ }
214
+
215
+ /** 停止当前传播和成员执行。 */
216
+ async stop(): Promise<void> {
217
+ if (this.disposed) return;
218
+ this.stop_requested = true;
219
+ this.publish_status(undefined, "stopped");
220
+ await Promise.allSettled([...this.member_sessions.values()].map((session) => session.stop()));
221
+ await Promise.allSettled([...this.pending_deliveries]);
222
+ if (this.auto_dispatch_promise) await this.auto_dispatch_promise;
223
+ this.auto_dispatch_requested = false;
224
+ this.auto_frontier_messages = [];
225
+ this.group_turns_by_id.clear();
226
+ this.auto_dispatch_path_keys.clear();
227
+ await this.persist_dispatch_checkpoint();
228
+ this.stop_requested = false;
229
+ }
230
+
231
+ /** 释放当前上下文及其成员 Session 引用。 */
232
+ async dispose(): Promise<void> {
233
+ if (this.disposed) return;
234
+ await this.stop();
235
+ this.disposed = true;
236
+ this.subscribers.clear();
237
+ for (const unsubscribe of this.member_session_unsubscribes.values()) unsubscribe();
238
+ this.member_session_unsubscribes.clear();
239
+ this.interaction_sessions.clear();
240
+ this.member_sessions.clear();
241
+ this.member_session_promises.clear();
242
+ this.member_running_counts.clear();
243
+ this.group_turns_by_id.clear();
244
+ this.consumed_auto_message_ids.clear();
245
+ this.auto_frontier_messages = [];
246
+ this.auto_dispatch_path_keys.clear();
247
+ }
248
+
249
+ private async run_user_dispatch(message: GroupMessage, turn_id: string): Promise<void> {
250
+ const group_turn = this.group_turns_by_id.get(turn_id);
251
+ if (!group_turn) return;
252
+ try {
253
+ const decision = await this.decide_dispatch("user", message, [message]);
254
+ const context_messages = group_turn.context_message_ids
255
+ .map((message_id) => this.messages_by_id.find((item) => item.id === message_id))
256
+ .filter((item): item is GroupMessage => Boolean(item));
257
+ await this.run_dispatch_plan(decision, message, context_messages, turn_id);
258
+ if (this.stop_requested || this.disposed) {
259
+ group_turn.stopped = true;
260
+ this.group_turns_by_id.delete(turn_id);
261
+ await this.persist_dispatch_checkpoint();
262
+ return;
263
+ }
264
+ group_turn.auto_pending = true;
265
+ await this.persist_dispatch_checkpoint();
266
+ } catch (error) {
267
+ group_turn.stopped = this.stop_requested;
268
+ await this.append_dispatch_failure(message, error);
269
+ this.group_turns_by_id.delete(turn_id);
270
+ await this.persist_dispatch_checkpoint();
271
+ this.publish_status(turn_id, this.stop_requested ? "stopped" : "failed");
272
+ }
273
+ }
274
+
275
+ /** GroupSession 唯一的自动调度循环,统一消费已完成成员产生的新消息。 */
276
+ private request_auto_dispatch(): void {
277
+ if (this.stop_requested || this.disposed) return;
278
+ this.auto_dispatch_requested = true;
279
+ if (this.auto_dispatch_promise) return;
280
+ this.auto_dispatch_promise = this.run_auto_dispatch().finally(() => {
281
+ this.auto_dispatch_promise = null;
282
+ if (this.auto_dispatch_requested && !this.stop_requested && !this.disposed) this.request_auto_dispatch();
283
+ });
284
+ }
285
+
286
+ private async run_auto_dispatch(): Promise<void> {
287
+ let frontier_messages: readonly GroupMessage[] = this.auto_frontier_messages;
288
+ let dispatch_count = 0;
289
+ if (frontier_messages.length === 0 && [...this.group_turns_by_id.values()].some((group_turn) => group_turn.auto_pending)) {
290
+ this.auto_dispatch_path_keys.clear();
291
+ }
292
+ while (this.auto_dispatch_requested && !this.stop_requested && !this.disposed) {
293
+ this.auto_dispatch_requested = false;
294
+ if (frontier_messages.length > 0) {
295
+ if (++dispatch_count > max_auto_dispatch_count) {
296
+ await this.append_message({ sender_type: "system", sender_id: "system", text: "Group auto dispatch limit reached." });
297
+ return;
298
+ }
299
+ const message = frontier_messages[frontier_messages.length - 1];
300
+ this.publish_status(undefined, "dispatching", { message_id: message.id });
301
+ try {
302
+ const decision = await this.decide_dispatch("auto", message, frontier_messages);
303
+ const dispatch_key = JSON.stringify(decision);
304
+ if (this.auto_dispatch_path_keys.has(dispatch_key)) {
305
+ await this.append_message({ sender_type: "system", sender_id: "system", text: "Group auto dispatch detected a repeated path.", reply_to: message.id });
306
+ frontier_messages = [];
307
+ this.auto_frontier_messages = [];
308
+ this.auto_dispatch_path_keys.clear();
309
+ await this.persist_dispatch_checkpoint();
310
+ this.publish_status(undefined, "idle");
311
+ continue;
312
+ }
313
+ this.auto_dispatch_path_keys.add(dispatch_key);
314
+ frontier_messages = await this.run_dispatch_plan(decision, message, this.messages_by_id, undefined, `group-dispatch-${nanoid(12)}`);
315
+ if (decision.terminal || frontier_messages.length === 0) {
316
+ frontier_messages = [];
317
+ this.auto_frontier_messages = [];
318
+ this.auto_dispatch_path_keys.clear();
319
+ await this.persist_dispatch_checkpoint();
320
+ this.publish_status(undefined, "idle");
321
+ } else {
322
+ this.auto_frontier_messages = frontier_messages;
323
+ await this.persist_dispatch_checkpoint();
324
+ this.auto_dispatch_requested = true;
325
+ }
326
+ } catch (error) {
327
+ await this.append_dispatch_failure(message, error);
328
+ this.publish_status(undefined, "failed");
329
+ frontier_messages = [];
330
+ this.auto_frontier_messages = [];
331
+ this.auto_dispatch_path_keys.clear();
332
+ await this.persist_dispatch_checkpoint();
333
+ }
334
+ continue;
335
+ }
336
+ // 一个 auto dispatch 负责一个已经收口的输入批次;先等待当下已排队的
337
+ // user dispatch,避免第一条输入完成后抢先消费,导致并发输入被拆散。
338
+ const pending_deliveries = [...this.pending_deliveries];
339
+ if (pending_deliveries.length > 0) {
340
+ await Promise.allSettled(pending_deliveries);
341
+ this.auto_dispatch_requested = true;
342
+ continue;
343
+ }
344
+ const batch_turns = [...this.group_turns_by_id.values()].filter((group_turn) => group_turn.auto_pending);
345
+ const batch_turn_ids = new Set(batch_turns.map((group_turn) => group_turn.turn_id));
346
+ const pending_messages = this.messages_by_id
347
+ .filter((message) => (
348
+ message.turn_id && batch_turn_ids.has(message.turn_id) &&
349
+ message.sender_type === "agent" &&
350
+ !this.consumed_auto_message_ids.has(message.id)
351
+ ));
352
+ const candidate_messages = pending_messages;
353
+ if (candidate_messages.length === 0) {
354
+ if (this.pending_deliveries.size === 0 && this.active_member_deliveries.size === 0) {
355
+ for (const group_turn of batch_turns) this.group_turns_by_id.delete(group_turn.turn_id);
356
+ await this.persist_dispatch_checkpoint();
357
+ this.publish_status(undefined, "idle");
358
+ }
359
+ continue;
360
+ }
361
+ batch_turns.forEach((group_turn) => { group_turn.auto_pending = false; });
362
+ for (const message of candidate_messages) this.consumed_auto_message_ids.add(message.id);
363
+ await this.persist_dispatch_checkpoint();
364
+ const dispatch_id = `group-dispatch-${nanoid(12)}`;
365
+ dispatch_count = 0;
366
+ const message = candidate_messages[candidate_messages.length - 1];
367
+ this.publish_status(undefined, "dispatching", { message_id: message.id });
368
+ try {
369
+ const decision = await this.decide_dispatch("auto", message, pending_messages);
370
+ const context_messages = this.messages_by_id.filter((item) => item.turn_id && batch_turn_ids.has(item.turn_id));
371
+ const outputs = await this.run_dispatch_plan(decision, message, context_messages, undefined, dispatch_id);
372
+ for (const turn_id of batch_turn_ids) this.group_turns_by_id.delete(turn_id);
373
+ if (decision.terminal || outputs.length === 0) {
374
+ this.auto_frontier_messages = [];
375
+ await this.persist_dispatch_checkpoint();
376
+ this.publish_status(undefined, "idle");
377
+ } else {
378
+ frontier_messages = outputs;
379
+ this.auto_frontier_messages = outputs;
380
+ await this.persist_dispatch_checkpoint();
381
+ this.auto_dispatch_requested = true;
382
+ }
383
+ } catch (error) {
384
+ await this.append_dispatch_failure(message, error);
385
+ this.publish_status(undefined, "failed");
386
+ for (const turn_id of batch_turn_ids) this.group_turns_by_id.delete(turn_id);
387
+ this.auto_frontier_messages = [];
388
+ await this.persist_dispatch_checkpoint();
389
+ }
390
+ }
391
+ }
392
+
393
+ private async decide_dispatch(
394
+ trigger: "user" | "auto",
395
+ message: GroupMessage,
396
+ pending_messages: readonly GroupMessage[],
397
+ ): Promise<DispatchDecision> {
398
+ return await this.dispatch_strategy.decide_dispatch({
399
+ trigger,
400
+ message,
401
+ pending_messages,
402
+ messages: await this.messages(),
403
+ members: this.members,
404
+ });
405
+ }
406
+
407
+ /** 执行当前响应图;节点依赖只影响投递顺序,不创建额外的调度循环。 */
408
+ private async run_dispatch_plan(
409
+ decision: DispatchDecision,
410
+ source_message: GroupMessage,
411
+ base_messages: readonly GroupMessage[],
412
+ turn_id?: string,
413
+ dispatch_id?: string,
414
+ ): Promise<readonly GroupMessage[]> {
415
+ const node_promises = new Map<string, Promise<void>>();
416
+ const nodes_by_id = new Map(decision.nodes.map((node) => [node.node_id, node]));
417
+ const node_outputs = new Map<string, GroupMessage[]>();
418
+ const resolving_node_ids = new Set<string>();
419
+ const run_node = (node: DispatchNode): Promise<void> => {
420
+ const cached = node_promises.get(node.node_id);
421
+ if (cached) return cached;
422
+ if (resolving_node_ids.has(node.node_id)) {
423
+ return Promise.reject(new Error(`Dispatch graph contains a cycle at node "${node.node_id}"`));
424
+ }
425
+ resolving_node_ids.add(node.node_id);
426
+ const promise = (async () => {
427
+ try {
428
+ await Promise.all(node.depends_on_node_ids.map((node_id) => {
429
+ const dependency = nodes_by_id.get(node_id);
430
+ if (!dependency) return Promise.reject(new Error(`Dispatch node "${node.node_id}" depends on missing node "${node_id}"`));
431
+ return run_node(dependency);
432
+ }));
433
+ const dependency_messages = node.depends_on_node_ids.flatMap((node_id) => node_outputs.get(node_id) || []);
434
+ // 依赖表示真实的前置回复;任一前置节点没有有效输出时,下游不应
435
+ // 退化为重新消费 source_message,否则会形成虚假的串行链路。
436
+ const dependencies_ready = node.depends_on_node_ids.every((node_id) => (
437
+ (node_outputs.get(node_id) || []).length > 0
438
+ ));
439
+ if (!dependencies_ready) {
440
+ node_outputs.set(node.node_id, []);
441
+ return;
442
+ }
443
+ const current_message = dependency_messages.at(-1) || source_message;
444
+ const context_messages = [...base_messages, ...dependency_messages];
445
+ const outputs = await this.execute_dispatch_node(node, current_message, context_messages, turn_id, dispatch_id);
446
+ node_outputs.set(node.node_id, outputs);
447
+ } finally {
448
+ resolving_node_ids.delete(node.node_id);
449
+ }
450
+ })();
451
+ node_promises.set(node.node_id, promise);
452
+ return promise;
453
+ };
454
+ const promises = decision.nodes.map(run_node);
455
+ await Promise.all(promises);
456
+ const depended_node_ids = new Set(
457
+ decision.nodes.flatMap((node) => node.depends_on_node_ids),
458
+ );
459
+ // 只把叶子节点作为下一次 auto dispatch 的 frontier;中间节点的回复
460
+ // 已经被下游节点消费,不能再次被当作新的群聊发言重复投递。
461
+ return decision.nodes
462
+ .filter((node) => !depended_node_ids.has(node.node_id))
463
+ .flatMap((node) => node_outputs.get(node.node_id) || []);
464
+ }
465
+
466
+ private async execute_dispatch_node(
467
+ node: DispatchNode,
468
+ message: GroupMessage,
469
+ context_messages: readonly GroupMessage[],
470
+ turn_id?: string,
471
+ dispatch_id?: string,
472
+ ): Promise<GroupMessage[]> {
473
+ const selected_ids = node.response_mode === "single" ? node.member_ids.slice(0, 1) : node.member_ids;
474
+ const targets = [...new Set(selected_ids)]
475
+ .map((agent_id) => this.get_member(agent_id))
476
+ .filter((agent): agent is Agent => Boolean(agent));
477
+ const prepared_results = await Promise.all(targets.map(async (agent) => {
478
+ try {
479
+ return { agent, turn: await this.prepare_member_delivery(agent, message, context_messages, node.instruction) } as const;
480
+ } catch (error) {
481
+ await this.append_member_failure(agent, message, error);
482
+ return null;
483
+ }
484
+ }));
485
+ const prepared_deliveries = prepared_results.filter((item): item is NonNullable<typeof item> => item !== null);
486
+ if (prepared_deliveries.length > 0) {
487
+ this.publish_status(turn_id, "dispatched", {
488
+ message_id: message.id,
489
+ dispatched_member_ids: prepared_deliveries.map(({ agent }) => agent.id),
490
+ });
491
+ }
492
+ const execution_results = Promise.all(prepared_deliveries.map(({ agent, turn }) => this.execute_member_delivery(agent, message, turn, turn_id, dispatch_id, node.node_id)));
493
+ const execution = execution_results.then(() => undefined);
494
+ this.active_member_deliveries.add(execution);
495
+ try {
496
+ await execution;
497
+ } finally {
498
+ this.active_member_deliveries.delete(execution);
499
+ }
500
+ return (await execution_results).filter((item): item is GroupMessage => Boolean(item));
501
+ }
502
+
503
+ /** 将消息提交给成员 Session,并返回已经接受的 Turn 句柄。 */
504
+ private async prepare_member_delivery(
505
+ agent: Agent,
506
+ message: GroupMessage,
507
+ context_messages: readonly GroupMessage[],
508
+ dispatch_instruction: string,
509
+ ): Promise<Awaited<ReturnType<AgentSession["prompt"]>>> {
510
+ if (this.stop_requested || this.disposed) {
511
+ throw new Error(`GroupSession "${this.id}" is stopping`);
512
+ }
513
+ const session = await this.ensure_member_session(agent);
514
+ return await session.prompt({
515
+ query: this.build_member_context(agent, message, context_messages, dispatch_instruction),
516
+ });
517
+ }
518
+
519
+ /** 等待成员 Turn 完成,并把有效回复写回 Group。 */
520
+ private async execute_member_delivery(
521
+ agent: Agent,
522
+ message: GroupMessage,
523
+ turn: Awaited<ReturnType<AgentSession["prompt"]>>,
524
+ turn_id?: string,
525
+ dispatch_id?: string,
526
+ dispatch_node_id?: string,
527
+ ): Promise<GroupMessage | null> {
528
+ this.set_member_running(agent.id, 1, turn_id);
529
+ let running = true;
530
+ try {
531
+ const result = await turn.finished;
532
+ // 成员已经产出最终结果,立刻结束输入态;后续的回复落盘和递归投递不属于输入阶段。
533
+ this.set_member_running(agent.id, -1, turn_id);
534
+ running = false;
535
+ if (this.stop_requested || this.disposed) return null;
536
+ if (!result.success) {
537
+ await this.append_member_failure(agent, message, result.error || "未知错误");
538
+ return null;
539
+ }
540
+ if (!result.text?.trim()) return null;
541
+ const reply = await this.append_message({
542
+ sender_type: "agent",
543
+ sender_id: agent.id,
544
+ text: result.text.trim(),
545
+ reply_to: message.id,
546
+ ...(turn_id ? { turn_id } : {}),
547
+ ...(dispatch_id ? { dispatch_id } : {}),
548
+ ...(dispatch_node_id ? { dispatch_node_id } : {}),
549
+ });
550
+ return reply;
551
+ } catch (error) {
552
+ if (!this.stop_requested && !this.disposed) {
553
+ await this.append_member_failure(agent, message, error);
554
+ }
555
+ return null;
556
+ } finally {
557
+ if (running) this.set_member_running(agent.id, -1, turn_id);
558
+ }
559
+ }
560
+
561
+ private async append_dispatch_failure(message: GroupMessage, error: unknown): Promise<void> {
562
+ if (this.stop_requested || this.disposed) return;
563
+ await this.append_message({
564
+ sender_type: "system",
565
+ sender_id: "system",
566
+ text: error instanceof Error ? error.message : String(error),
567
+ reply_to: message.id,
568
+ });
569
+ }
570
+
571
+ /** 把成员自身的投递或执行失败记录为共享事实,避免群聊静默。 */
572
+ private async append_member_failure(
573
+ agent: Agent,
574
+ message: GroupMessage,
575
+ error: unknown,
576
+ ): Promise<void> {
577
+ if (this.stop_requested || this.disposed) return;
578
+ await this.append_message({
579
+ sender_type: "system",
580
+ sender_id: "system",
581
+ text: `${agent.id} 执行失败:${error instanceof Error ? error.message : String(error)}`,
582
+ reply_to: message.id,
583
+ });
584
+ }
585
+
586
+ /** 获取或创建一个成员专属的 AgentSession。 */
587
+ private async ensure_member_session(agent: Agent): Promise<AgentSession> {
588
+ let session = this.member_sessions.get(agent.id);
589
+ if (session) return session;
590
+ let create_promise = this.member_session_promises.get(agent.id);
591
+ if (!create_promise) {
592
+ create_promise = this.create_member_session(agent);
593
+ this.member_session_promises.set(agent.id, create_promise);
594
+ }
595
+ try {
596
+ return await create_promise;
597
+ } finally {
598
+ if (this.member_session_promises.get(agent.id) === create_promise) {
599
+ this.member_session_promises.delete(agent.id);
600
+ }
601
+ }
602
+ }
603
+
604
+ /** 创建并登记一个成员专属的 AgentSession。 */
605
+ private async create_member_session(agent: Agent): Promise<AgentSession> {
606
+ const session = await agent.sessions.create({
607
+ ...(this.workspace ? { workspace: this.workspace } : {}),
608
+ origin: {
609
+ type: "group",
610
+ group_id: this.group_id,
611
+ group_session_id: this.id,
612
+ },
613
+ });
614
+ this.member_sessions.set(agent.id, session);
615
+ this.member_session_ids.set(agent.id, session.id);
616
+ this.subscribe_member_session(agent.id, session);
617
+ const store = this.store;
618
+ if (!store) throw new Error(`GroupSession "${this.id}" is not initialized`);
619
+ await store.update_metadata({
620
+ member_session_ids: Object.fromEntries(this.member_session_ids),
621
+ });
622
+ return session;
623
+ }
624
+
625
+ private subscribe_member_session(agent_id: string, session: AgentSession): void {
626
+ this.member_session_unsubscribes.get(agent_id)?.();
627
+ this.member_session_unsubscribes.set(agent_id, session.subscribe((mutation) => {
628
+ if (mutation.variant !== "part" || mutation.type !== "interaction") return;
629
+ if (mutation.part.status === "pending") {
630
+ this.interaction_sessions.set(mutation.part.interaction_id, session);
631
+ this.publish_event({ type: "interaction", agent_id, request: mutation.part.request });
632
+ } else {
633
+ this.interaction_sessions.delete(mutation.part.interaction_id);
634
+ }
635
+ }));
636
+ }
637
+
638
+ private build_member_context(
639
+ agent: Agent,
640
+ message: GroupMessage,
641
+ context_messages: readonly GroupMessage[],
642
+ dispatch_instruction: string,
643
+ ): string {
644
+ const history = context_messages
645
+ .filter((item) => item.id !== message.id)
646
+ .map((item) => `${item.sender_type === "user" ? "User" : item.sender_id}: ${item.text}`)
647
+ .join("\n");
648
+ return [
649
+ this.instruction ? `Group objective: ${this.instruction}` : "",
650
+ `你是 Group ${this.group_name} 的成员 ${agent.id}。`,
651
+ "Group conversation:",
652
+ history,
653
+ `Current message from ${message.sender_type === "agent" ? message.sender_id : "user"}: ${message.text}`,
654
+ dispatch_instruction,
655
+ `你只能代表成员 ${agent.id} 自己发言。不得代替、指挥、裁定、总结或转述其他成员;不得假装自己是其他成员;不得创建队长、裁判或协调者。`,
656
+ "如果不需要你发言,返回空内容。",
657
+ ].filter(Boolean).join("\n");
658
+ }
659
+
660
+ private get_member(agent_id: string): Agent | null {
661
+ return this.members.find((member) => member.id === agent_id) || null;
662
+ }
663
+
664
+ /** 更新成员运行计数,并在可见状态发生变化时通知订阅者。 */
665
+ private set_member_running(agent_id: string, delta: 1 | -1, turn_id?: string): void {
666
+ const previous_count = this.member_running_counts.get(agent_id) || 0;
667
+ const next_count = Math.max(0, previous_count + delta);
668
+ if (next_count === 0) this.member_running_counts.delete(agent_id);
669
+ else this.member_running_counts.set(agent_id, next_count);
670
+ if ((previous_count > 0) === (next_count > 0)) return;
671
+ this.publish_status(turn_id, this.stop_requested ? "stopped" : "executing");
672
+ }
673
+
674
+ private async append_message(input: Omit<GroupMessage, "id" | "group_id" | "created_at">): Promise<GroupMessage> {
675
+ const message: GroupMessage = {
676
+ ...input,
677
+ id: `group-message-${nanoid(12)}`,
678
+ group_id: this.group_id,
679
+ created_at: Date.now(),
680
+ };
681
+ if (!this.store) throw new Error(`GroupSession "${this.id}" is not initialized`);
682
+ await this.store.append_message(message);
683
+ this.messages_by_id.push(message);
684
+ for (const subscriber of this.subscribers) void subscriber({ type: "message", message });
685
+ return message;
686
+ }
687
+
688
+ /** 持久化当前调度检查点;只记录可由消息事实安全恢复的状态。 */
689
+ private async persist_dispatch_checkpoint(): Promise<void> {
690
+ if (!this.store) return;
691
+ await this.store.update_metadata({
692
+ pending_turns: [...this.group_turns_by_id.values()]
693
+ .filter((group_turn) => !group_turn.stopped)
694
+ .map((group_turn) => ({
695
+ turn_id: group_turn.turn_id,
696
+ root_message_id: group_turn.root_message_id,
697
+ context_message_ids: [...group_turn.context_message_ids],
698
+ })),
699
+ auto_frontier_message_ids: this.auto_frontier_messages.map((message) => message.id),
700
+ });
701
+ }
702
+
703
+ /** 发布 Group 阶段和成员运行态;所有状态与消息共用同一订阅流。 */
704
+ private publish_status(
705
+ turn_id: string | undefined,
706
+ phase: "idle" | "dispatching" | "dispatched" | "executing" | "stopped" | "failed",
707
+ context?: { message_id?: string; dispatched_member_ids?: readonly string[] },
708
+ ): void {
709
+ const event = {
710
+ type: "status" as const,
711
+ ...(turn_id ? { turn_id } : {}),
712
+ ...(context?.message_id ? { message_id: context.message_id } : {}),
713
+ ...(context?.dispatched_member_ids ? { dispatched_member_ids: [...context.dispatched_member_ids] } : {}),
714
+ phase,
715
+ members: this.member_statuses(),
716
+ };
717
+ for (const subscriber of this.subscribers) void subscriber(event);
718
+ }
719
+
720
+ private publish_event(event: Parameters<GroupEventSubscriber>[0]): void {
721
+ for (const subscriber of this.subscribers) void subscriber(event);
722
+ }
723
+
724
+ private assert_available(): void {
725
+ if (this.disposed) throw new Error(`GroupSession "${this.id}" is disposed`);
726
+ if (this.stop_requested) throw new Error(`GroupSession "${this.id}" is stopping`);
727
+ }
728
+ }