@downcity/agent 1.1.79 → 1.1.81

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 (173) hide show
  1. package/README.md +1 -1
  2. package/bin/agent/local/Agent.d.ts +8 -121
  3. package/bin/agent/local/Agent.d.ts.map +1 -1
  4. package/bin/agent/local/Agent.js +63 -381
  5. package/bin/agent/local/Agent.js.map +1 -1
  6. package/bin/agent/local/AgentRuntimeFactory.d.ts +2 -2
  7. package/bin/agent/local/AgentRuntimeFactory.d.ts.map +1 -1
  8. package/bin/agent/local/AgentRuntimeFactory.js +2 -23
  9. package/bin/agent/local/AgentRuntimeFactory.js.map +1 -1
  10. package/bin/agent/local/services/AgentAssemblyService.d.ts +112 -0
  11. package/bin/agent/local/services/AgentAssemblyService.d.ts.map +1 -0
  12. package/bin/agent/local/services/AgentAssemblyService.js +135 -0
  13. package/bin/agent/local/services/AgentAssemblyService.js.map +1 -0
  14. package/bin/agent/local/services/AgentLifecycleService.d.ts +59 -0
  15. package/bin/agent/local/services/AgentLifecycleService.d.ts.map +1 -0
  16. package/bin/agent/local/services/AgentLifecycleService.js +136 -0
  17. package/bin/agent/local/services/AgentLifecycleService.js.map +1 -0
  18. package/bin/agent/local/services/AgentSessionManager.d.ts +106 -0
  19. package/bin/agent/local/services/AgentSessionManager.d.ts.map +1 -0
  20. package/bin/agent/local/services/AgentSessionManager.js +182 -0
  21. package/bin/agent/local/services/AgentSessionManager.js.map +1 -0
  22. package/bin/executor/Executor.d.ts +7 -24
  23. package/bin/executor/Executor.d.ts.map +1 -1
  24. package/bin/executor/Executor.js +73 -361
  25. package/bin/executor/Executor.js.map +1 -1
  26. package/bin/executor/SessionRunScope.d.ts +18 -34
  27. package/bin/executor/SessionRunScope.d.ts.map +1 -1
  28. package/bin/executor/SessionRunScope.js +42 -28
  29. package/bin/executor/SessionRunScope.js.map +1 -1
  30. package/bin/executor/composer/context/LocalSessionContextComposer.d.ts +5 -3
  31. package/bin/executor/composer/context/LocalSessionContextComposer.d.ts.map +1 -1
  32. package/bin/executor/composer/context/LocalSessionContextComposer.js +11 -18
  33. package/bin/executor/composer/context/LocalSessionContextComposer.js.map +1 -1
  34. package/bin/executor/composer/context/SessionContextComposer.d.ts +8 -3
  35. package/bin/executor/composer/context/SessionContextComposer.d.ts.map +1 -1
  36. package/bin/executor/composer/system/SessionSystemComposer.d.ts +2 -1
  37. package/bin/executor/composer/system/SessionSystemComposer.d.ts.map +1 -1
  38. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts +2 -1
  39. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts.map +1 -1
  40. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js +2 -4
  41. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js.map +1 -1
  42. package/bin/executor/core-engine/CoreEngineRunner.d.ts +62 -0
  43. package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -0
  44. package/bin/executor/core-engine/CoreEngineRunner.js +309 -0
  45. package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -0
  46. package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts +5 -0
  47. package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts.map +1 -1
  48. package/bin/executor/core-engine/CoreEngineUiStreamCollector.js +2 -4
  49. package/bin/executor/core-engine/CoreEngineUiStreamCollector.js.map +1 -1
  50. package/bin/executor/services/ExecutorInflightService.d.ts +39 -0
  51. package/bin/executor/services/ExecutorInflightService.d.ts.map +1 -0
  52. package/bin/executor/services/ExecutorInflightService.js +75 -0
  53. package/bin/executor/services/ExecutorInflightService.js.map +1 -0
  54. package/bin/executor/services/ExecutorRecoveryPolicy.d.ts +103 -0
  55. package/bin/executor/services/ExecutorRecoveryPolicy.d.ts.map +1 -0
  56. package/bin/executor/services/ExecutorRecoveryPolicy.js +87 -0
  57. package/bin/executor/services/ExecutorRecoveryPolicy.js.map +1 -0
  58. package/bin/executor/tools/plugin/PluginToolBridge.d.ts +19 -0
  59. package/bin/executor/tools/plugin/PluginToolBridge.d.ts.map +1 -0
  60. package/bin/executor/tools/plugin/PluginToolBridge.js +143 -0
  61. package/bin/executor/tools/plugin/PluginToolBridge.js.map +1 -0
  62. package/bin/executor/tools/plugin/PluginToolDefinition.d.ts +32 -0
  63. package/bin/executor/tools/plugin/PluginToolDefinition.d.ts.map +1 -0
  64. package/bin/executor/tools/plugin/PluginToolDefinition.js +27 -0
  65. package/bin/executor/tools/plugin/PluginToolDefinition.js.map +1 -0
  66. package/bin/executor/tools/plugin/PluginToolSchemas.d.ts +14 -0
  67. package/bin/executor/tools/plugin/PluginToolSchemas.d.ts.map +1 -0
  68. package/bin/executor/tools/plugin/PluginToolSchemas.js +19 -0
  69. package/bin/executor/tools/plugin/PluginToolSchemas.js.map +1 -0
  70. package/bin/executor/tools/plugin/types/PluginTool.d.ts +39 -0
  71. package/bin/executor/tools/plugin/types/PluginTool.d.ts.map +1 -0
  72. package/bin/executor/tools/plugin/types/PluginTool.js +9 -0
  73. package/bin/executor/tools/plugin/types/PluginTool.js.map +1 -0
  74. package/bin/executor/tools/shell/ShellToolBridge.js +3 -3
  75. package/bin/executor/tools/shell/ShellToolBridge.js.map +1 -1
  76. package/bin/executor/types/SessionRun.d.ts +18 -0
  77. package/bin/executor/types/SessionRun.d.ts.map +1 -1
  78. package/bin/index.d.ts +10 -1
  79. package/bin/index.d.ts.map +1 -1
  80. package/bin/index.js +3 -0
  81. package/bin/index.js.map +1 -1
  82. package/bin/plugin/core/BasePlugin.d.ts +2 -2
  83. package/bin/plugin/core/BasePlugin.d.ts.map +1 -1
  84. package/bin/plugin/core/BasePlugin.js.map +1 -1
  85. package/bin/plugin/core/ImagePlugin.d.ts +56 -0
  86. package/bin/plugin/core/ImagePlugin.d.ts.map +1 -0
  87. package/bin/plugin/core/ImagePlugin.js +109 -0
  88. package/bin/plugin/core/ImagePlugin.js.map +1 -0
  89. package/bin/session/Session.d.ts +14 -83
  90. package/bin/session/Session.d.ts.map +1 -1
  91. package/bin/session/Session.js +139 -362
  92. package/bin/session/Session.js.map +1 -1
  93. package/bin/session/SessionSystemBuilder.d.ts +2 -1
  94. package/bin/session/SessionSystemBuilder.d.ts.map +1 -1
  95. package/bin/session/SessionSystemBuilder.js +2 -3
  96. package/bin/session/SessionSystemBuilder.js.map +1 -1
  97. package/bin/session/services/SessionStateService.d.ts +132 -0
  98. package/bin/session/services/SessionStateService.d.ts.map +1 -0
  99. package/bin/session/services/SessionStateService.js +242 -0
  100. package/bin/session/services/SessionStateService.js.map +1 -0
  101. package/bin/session/services/SessionTurnService.d.ts +66 -0
  102. package/bin/session/services/SessionTurnService.d.ts.map +1 -0
  103. package/bin/session/services/SessionTurnService.js +137 -0
  104. package/bin/session/services/SessionTurnService.js.map +1 -0
  105. package/bin/session/services/SessionViewService.d.ts +105 -0
  106. package/bin/session/services/SessionViewService.d.ts.map +1 -0
  107. package/bin/session/services/SessionViewService.js +184 -0
  108. package/bin/session/services/SessionViewService.js.map +1 -0
  109. package/bin/types/agent/AgentOptions.d.ts +18 -0
  110. package/bin/types/agent/AgentOptions.d.ts.map +1 -1
  111. package/bin/types/agent/AgentTypes.d.ts +3 -1
  112. package/bin/types/agent/AgentTypes.d.ts.map +1 -1
  113. package/bin/types/agent/SessionTypes.d.ts.map +1 -1
  114. package/bin/types/executor/SessionRunContext.d.ts +66 -0
  115. package/bin/types/executor/SessionRunContext.d.ts.map +1 -0
  116. package/bin/types/executor/SessionRunContext.js +10 -0
  117. package/bin/types/executor/SessionRunContext.js.map +1 -0
  118. package/bin/types/plugin/ImagePlugin.d.ts +94 -0
  119. package/bin/types/plugin/ImagePlugin.d.ts.map +1 -0
  120. package/bin/types/plugin/ImagePlugin.js +10 -0
  121. package/bin/types/plugin/ImagePlugin.js.map +1 -0
  122. package/bin/types/session/SessionComposerOptions.d.ts +90 -0
  123. package/bin/types/session/SessionComposerOptions.d.ts.map +1 -0
  124. package/bin/types/session/SessionComposerOptions.js +10 -0
  125. package/bin/types/session/SessionComposerOptions.js.map +1 -0
  126. package/bin/types/session/SessionLocalState.d.ts +35 -0
  127. package/bin/types/session/SessionLocalState.d.ts.map +1 -0
  128. package/bin/types/session/SessionLocalState.js +10 -0
  129. package/bin/types/session/SessionLocalState.js.map +1 -0
  130. package/bin/types/session/SessionOptions.d.ts +85 -0
  131. package/bin/types/session/SessionOptions.d.ts.map +1 -0
  132. package/bin/types/session/SessionOptions.js +10 -0
  133. package/bin/types/session/SessionOptions.js.map +1 -0
  134. package/package.json +1 -1
  135. package/src/agent/local/Agent.ts +74 -433
  136. package/src/agent/local/AgentRuntimeFactory.ts +4 -25
  137. package/src/agent/local/services/AgentAssemblyService.ts +268 -0
  138. package/src/agent/local/services/AgentLifecycleService.ts +187 -0
  139. package/src/agent/local/services/AgentSessionManager.ts +291 -0
  140. package/src/executor/Executor.ts +103 -441
  141. package/src/executor/README.md +4 -4
  142. package/src/executor/SessionRunScope.ts +47 -71
  143. package/src/executor/composer/context/LocalSessionContextComposer.ts +24 -20
  144. package/src/executor/composer/context/SessionContextComposer.ts +13 -3
  145. package/src/executor/composer/system/SessionSystemComposer.ts +2 -1
  146. package/src/executor/composer/system/default/DefaultSessionSystemComposer.ts +3 -4
  147. package/src/executor/core-engine/CoreEngineRunner.ts +433 -0
  148. package/src/executor/core-engine/CoreEngineUiStreamCollector.ts +7 -5
  149. package/src/executor/services/ExecutorInflightService.ts +101 -0
  150. package/src/executor/services/ExecutorRecoveryPolicy.ts +213 -0
  151. package/src/executor/tools/plugin/PluginToolBridge.ts +161 -0
  152. package/src/executor/tools/plugin/PluginToolDefinition.ts +32 -0
  153. package/src/executor/tools/plugin/PluginToolSchemas.ts +20 -0
  154. package/src/executor/tools/plugin/types/PluginTool.ts +41 -0
  155. package/src/executor/tools/shell/ShellToolBridge.ts +3 -3
  156. package/src/executor/types/SessionRun.ts +20 -0
  157. package/src/index.ts +33 -0
  158. package/src/plugin/core/BasePlugin.ts +2 -2
  159. package/src/plugin/core/ImagePlugin.ts +128 -0
  160. package/src/session/Session.ts +178 -485
  161. package/src/session/SessionSystemBuilder.ts +3 -3
  162. package/src/session/services/SessionStateService.ts +341 -0
  163. package/src/session/services/SessionTurnService.ts +202 -0
  164. package/src/session/services/SessionViewService.ts +301 -0
  165. package/src/types/agent/AgentOptions.ts +25 -0
  166. package/src/types/agent/AgentTypes.ts +10 -0
  167. package/src/types/agent/SessionTypes.ts +1 -0
  168. package/src/types/executor/SessionRunContext.ts +76 -0
  169. package/src/types/plugin/ImagePlugin.ts +103 -0
  170. package/src/types/session/SessionComposerOptions.ts +107 -0
  171. package/src/types/session/SessionLocalState.ts +40 -0
  172. package/src/types/session/SessionOptions.ts +99 -0
  173. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,301 @@
1
+ /**
2
+ * SessionViewService:本地 Session 查询与派生视图服务。
3
+ *
4
+ * 关键点(中文)
5
+ * - 统一管理 info/history/system/fork 这类读取型能力。
6
+ * - 该服务只负责拼装查询结果,不负责编排 prompt turn。
7
+ * - 需要写入持久化状态的地方仍委托给 SessionStateService。
8
+ */
9
+
10
+ import { nanoid } from "nanoid";
11
+ import {
12
+ buildSessionHistoryPage,
13
+ buildSessionInfo,
14
+ ensureSessionTitle,
15
+ readSessionMetadata,
16
+ } from "@/session/index.js";
17
+ import { buildSessionSystemBlocks } from "@/session/SessionSystemBuilder.js";
18
+ import type { JsonlSessionHistoryStore } from "@/executor/store/history/jsonl/JsonlSessionHistoryStore.js";
19
+ import type { SessionSystemComposer } from "@/executor/composer/system/SessionSystemComposer.js";
20
+ import type {
21
+ AgentSessionForkInput,
22
+ AgentSessionHistoryInput,
23
+ AgentSessionHistoryPage,
24
+ AgentSessionInfo,
25
+ AgentSessionSystemBlock,
26
+ AgentSessionSystemSnapshot,
27
+ } from "@/types/agent/AgentTypes.js";
28
+ import type { SessionMessageV1 } from "@/executor/types/SessionMessages.js";
29
+ import { SessionStateService } from "@/session/services/SessionStateService.js";
30
+ import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
31
+
32
+ type SessionViewServiceOptions<TSession> = {
33
+ /**
34
+ * 当前 agent 稳定标识。
35
+ */
36
+ agent_id: string;
37
+
38
+ /**
39
+ * 当前项目根目录。
40
+ */
41
+ project_root: string;
42
+
43
+ /**
44
+ * 当前 session 标识。
45
+ */
46
+ session_id: string;
47
+
48
+ /**
49
+ * 当前 session 历史事实源。
50
+ */
51
+ history_store: JsonlSessionHistoryStore;
52
+
53
+ /**
54
+ * 当前 session 状态服务。
55
+ */
56
+ state_service: SessionStateService;
57
+
58
+ /**
59
+ * 判断当前 session 是否正在执行。
60
+ */
61
+ is_executing: () => boolean;
62
+
63
+ /**
64
+ * 读取 instruction system blocks。
65
+ */
66
+ get_instruction_system_blocks: () => AgentSessionSystemBlock[];
67
+
68
+ /**
69
+ * 读取受托管 plugin system blocks。
70
+ */
71
+ get_managed_plugin_system_blocks: () => Promise<AgentSessionSystemBlock[]>;
72
+
73
+ /**
74
+ * 读取显式注册 plugin system blocks。
75
+ */
76
+ get_plugin_system_blocks: () => Promise<AgentSessionSystemBlock[]>;
77
+
78
+ /**
79
+ * 可选自定义 system composer。
80
+ *
81
+ * 关键点(中文)
82
+ * - 仅当调用方覆盖了 system composer 时传入。
83
+ * - 默认 SDK system snapshot 仍保留 block 级来源信息。
84
+ */
85
+ custom_system_composer?: SessionSystemComposer;
86
+
87
+ /**
88
+ * 创建一个新的本地 Session 实例。
89
+ */
90
+ create_fork_session: (session_id: string) => Promise<{
91
+ session: TSession;
92
+ history_store: JsonlSessionHistoryStore;
93
+ state_service: SessionStateService;
94
+ }>;
95
+ };
96
+
97
+ /**
98
+ * 本地 Session 查询与派生视图服务。
99
+ */
100
+ export class SessionViewService<TSession> {
101
+ private readonly agent_id: string;
102
+ private readonly project_root: string;
103
+ private readonly session_id: string;
104
+ private readonly history_store: JsonlSessionHistoryStore;
105
+ private readonly state_service: SessionStateService;
106
+ private readonly is_executing: SessionViewServiceOptions<TSession>["is_executing"];
107
+ private readonly get_instruction_system_blocks: SessionViewServiceOptions<TSession>["get_instruction_system_blocks"];
108
+ private readonly get_managed_plugin_system_blocks: SessionViewServiceOptions<TSession>["get_managed_plugin_system_blocks"];
109
+ private readonly get_plugin_system_blocks: SessionViewServiceOptions<TSession>["get_plugin_system_blocks"];
110
+ private readonly custom_system_composer?: SessionSystemComposer;
111
+ private readonly create_fork_session: SessionViewServiceOptions<TSession>["create_fork_session"];
112
+
113
+ constructor(options: SessionViewServiceOptions<TSession>) {
114
+ this.agent_id = options.agent_id;
115
+ this.project_root = options.project_root;
116
+ this.session_id = options.session_id;
117
+ this.history_store = options.history_store;
118
+ this.state_service = options.state_service;
119
+ this.is_executing = options.is_executing;
120
+ this.get_instruction_system_blocks = options.get_instruction_system_blocks;
121
+ this.get_managed_plugin_system_blocks =
122
+ options.get_managed_plugin_system_blocks;
123
+ this.get_plugin_system_blocks = options.get_plugin_system_blocks;
124
+ this.custom_system_composer = options.custom_system_composer;
125
+ this.create_fork_session = options.create_fork_session;
126
+ }
127
+
128
+ /**
129
+ * 读取当前 session 详情。
130
+ */
131
+ async get_info(): Promise<AgentSessionInfo> {
132
+ const [metadata, messages] = await Promise.all([
133
+ readSessionMetadata({
134
+ projectRoot: this.project_root,
135
+ agentId: this.agent_id,
136
+ sessionId: this.session_id,
137
+ }),
138
+ this.history_store.list(),
139
+ ]);
140
+ const metadata_with_title = metadata.title
141
+ ? metadata
142
+ : await ensureSessionTitle({
143
+ projectRoot: this.project_root,
144
+ agentId: this.agent_id,
145
+ sessionId: this.session_id,
146
+ messages,
147
+ });
148
+ return buildSessionInfo({
149
+ projectRoot: this.project_root,
150
+ agentId: this.agent_id,
151
+ sessionId: this.session_id,
152
+ metadata: metadata_with_title,
153
+ messages,
154
+ executing: this.is_executing(),
155
+ });
156
+ }
157
+
158
+ /**
159
+ * 读取当前 session 历史分页。
160
+ */
161
+ async history(
162
+ input?: AgentSessionHistoryInput,
163
+ ): Promise<AgentSessionHistoryPage> {
164
+ const [session, messages] = await Promise.all([
165
+ this.get_info(),
166
+ this.history_store.list(),
167
+ ]);
168
+ return buildSessionHistoryPage({
169
+ session,
170
+ messages,
171
+ input,
172
+ });
173
+ }
174
+
175
+ /**
176
+ * 读取当前 session 生效的 system 快照。
177
+ */
178
+ async system(): Promise<AgentSessionSystemSnapshot> {
179
+ const blocks = this.custom_system_composer
180
+ ? await this.resolve_custom_system_blocks(this.custom_system_composer)
181
+ : await buildSessionSystemBlocks({
182
+ agentId: this.agent_id,
183
+ projectRoot: this.project_root,
184
+ sessionId: this.session_id,
185
+ createdAt: this.state_service.get_created_at(),
186
+ timezone: this.state_service.get_timezone(),
187
+ getInstructionSystemBlocks: this.get_instruction_system_blocks,
188
+ getManagedPluginSystemBlocks: this.get_managed_plugin_system_blocks,
189
+ getPluginSystemBlocks: this.get_plugin_system_blocks,
190
+ });
191
+ return {
192
+ sessionId: this.session_id,
193
+ session: {
194
+ agentId: this.agent_id,
195
+ sessionId: this.session_id,
196
+ projectRoot: this.project_root,
197
+ createdAt: new Date(this.state_service.get_created_at()).toISOString(),
198
+ timezone: this.state_service.get_timezone(),
199
+ },
200
+ blocks,
201
+ };
202
+ }
203
+
204
+ private async resolve_custom_system_blocks(
205
+ composer: SessionSystemComposer,
206
+ ): Promise<AgentSessionSystemBlock[]> {
207
+ const run_context: SessionRunContext = {
208
+ sessionId: this.session_id,
209
+ injectedUserMessages: [],
210
+ deferredPersistedUserMessages: [],
211
+ pendingAssistantFileParts: [],
212
+ };
213
+ const messages = await composer.resolve(run_context);
214
+ const blocks: AgentSessionSystemBlock[] = [];
215
+ messages.forEach((message, index) => {
216
+ const content = this.stringify_system_content(
217
+ (message as { content?: unknown }).content,
218
+ );
219
+ if (!content) return;
220
+ blocks.push({
221
+ source: "session",
222
+ name: `${composer.name || "custom_system"}:${index + 1}`,
223
+ content,
224
+ });
225
+ });
226
+ return blocks;
227
+ }
228
+
229
+ /**
230
+ * 从当前 session 创建一个分叉会话。
231
+ */
232
+ async fork(input?: AgentSessionForkInput | string): Promise<TSession> {
233
+ const message_id =
234
+ typeof input === "string"
235
+ ? String(input || "").trim() || undefined
236
+ : String(input?.messageId || "").trim() || undefined;
237
+ const messages = await this.history_store.list();
238
+ const fork_messages =
239
+ !message_id
240
+ ? messages
241
+ : this.resolve_fork_messages(messages, message_id);
242
+
243
+ const forked_bundle = await this.create_fork_session(
244
+ `fork-${Date.now()}-${nanoid(8)}`,
245
+ );
246
+ const forked = forked_bundle.session;
247
+ const session_config = this.state_service.get_config();
248
+ if (session_config.model) {
249
+ await (
250
+ forked as unknown as {
251
+ set(input: { model: unknown }): Promise<void>;
252
+ }
253
+ ).set({
254
+ model: session_config.model,
255
+ });
256
+ }
257
+ await this.append_fork_messages(forked_bundle, fork_messages);
258
+ return forked;
259
+ }
260
+
261
+ private resolve_fork_messages(
262
+ messages: SessionMessageV1[],
263
+ message_id: string,
264
+ ): SessionMessageV1[] {
265
+ const target_index = messages.findIndex(
266
+ (message) => String(message.id || "").trim() === message_id,
267
+ );
268
+ if (target_index < 0) {
269
+ throw new Error(
270
+ `Cannot fork session "${this.session_id}": messageId "${message_id}" not found.`,
271
+ );
272
+ }
273
+ return messages.slice(0, target_index + 1);
274
+ }
275
+
276
+ private async append_fork_messages(
277
+ forked_bundle: {
278
+ history_store: JsonlSessionHistoryStore;
279
+ state_service: SessionStateService;
280
+ },
281
+ messages: SessionMessageV1[],
282
+ ): Promise<void> {
283
+ for (const message of messages) {
284
+ await forked_bundle.history_store.append(message);
285
+ }
286
+ await forked_bundle.state_service.ensure_title_from_history({
287
+ generate: true,
288
+ });
289
+ await forked_bundle.state_service.touch_metadata();
290
+ }
291
+
292
+ private stringify_system_content(content: unknown): string {
293
+ if (typeof content === "string") return content.trim();
294
+ if (content === null || content === undefined) return "";
295
+ try {
296
+ return JSON.stringify(content);
297
+ } catch {
298
+ return String(content || "").trim();
299
+ }
300
+ }
301
+ }
@@ -10,6 +10,21 @@ import type { Tool } from "ai";
10
10
  import type { BasePlugin } from "@/plugin/core/BasePlugin.js";
11
11
  import type { AgentModel } from "@/model/CityModelAdapter.js";
12
12
  import type { RpcServerInstance } from "@/rpc/Server.js";
13
+ import type {
14
+ AgentManagedSession,
15
+ SessionOptions,
16
+ } from "@/types/session/SessionOptions.js";
17
+
18
+ /**
19
+ * Agent 可使用的 Session 类。
20
+ *
21
+ * 关键点(中文)
22
+ * - 传 class,而不是传实例,保证 Agent 可以按 sessionId 创建多个 session。
23
+ * - 自定义类可继承默认 `Session`,并在构造函数里注入自己的 Composer。
24
+ */
25
+ export type AgentSessionConstructor = new (
26
+ options: SessionOptions,
27
+ ) => AgentManagedSession;
13
28
 
14
29
  /**
15
30
  * 本地 Agent 构造参数。
@@ -67,6 +82,16 @@ export interface AgentOptions {
67
82
  */
68
83
  plugins?: BasePlugin[];
69
84
 
85
+ /**
86
+ * 当前 agent 使用的本地 Session 类。
87
+ *
88
+ * 关键点(中文)
89
+ * - Agent 只负责用这个类创建/恢复 session,不感知具体 Composer 策略。
90
+ * - 如果需要自定义 Composer,请在自定义 Session 类内部传给 `super({ composers })`。
91
+ * - 该能力仅适用于本地 `Agent`。
92
+ */
93
+ Session?: AgentSessionConstructor;
94
+
70
95
  /**
71
96
  * 当前 agent 的显式环境变量覆盖项。
72
97
  *
@@ -8,10 +8,20 @@
8
8
  */
9
9
 
10
10
  export type { AgentModel } from "@/model/CityModelAdapter.js";
11
+ export type {
12
+ SessionComposerFactoryContext,
13
+ SessionComposerInput,
14
+ SessionComposerOptions,
15
+ } from "@/types/session/SessionComposerOptions.js";
16
+ export type {
17
+ AgentManagedSession,
18
+ SessionOptions,
19
+ } from "@/types/session/SessionOptions.js";
11
20
  export type {
12
21
  AgentOptions,
13
22
  AgentRpcBinding,
14
23
  AgentRpcStartOptions,
24
+ AgentSessionConstructor,
15
25
  AgentStartOptions,
16
26
  AgentStartResult,
17
27
  AgentStopResult,
@@ -23,6 +23,7 @@ export interface AgentCreateSessionInput {
23
23
  * - 省略时由 SDK 自动生成稳定且不可推导的 sessionId。
24
24
  */
25
25
  sessionId?: string;
26
+
26
27
  }
27
28
 
28
29
  /**
@@ -0,0 +1,76 @@
1
+ /**
2
+ * SessionRunContext:单次 session run 的显式运行上下文。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这里承载本轮执行过程中需要在多个组件间透传的运行期数据。
6
+ * - 业务侧上下文优先通过该对象显式传递,避免分散读取隐式全局状态。
7
+ * - tool 深层回调等无法直接传参的场景,才通过 run scope 间接读取这个对象。
8
+ */
9
+
10
+ import type {
11
+ SessionAssistantStepCallback,
12
+ SessionUiMessageChunkCallback,
13
+ } from "@/executor/types/SessionRun.js";
14
+ import type { SessionUserMessageV1 } from "@/executor/types/SessionMessages.js";
15
+ import type { FileUIPart } from "ai";
16
+
17
+ /**
18
+ * 单次 session run 的运行上下文。
19
+ */
20
+ export interface SessionRunContext {
21
+ /**
22
+ * 当前执行所属的 session 标识。
23
+ */
24
+ sessionId: string;
25
+
26
+ /**
27
+ * step 边界合并回调。
28
+ *
29
+ * 关键点(中文)
30
+ * - 由 Session actor 在 turn 运行期间注入。
31
+ * - 用于把运行中追加的 user 消息并入下一 step。
32
+ */
33
+ onStepCallback?: () => Promise<SessionUserMessageV1[]>;
34
+
35
+ /**
36
+ * assistant step 完成回调。
37
+ *
38
+ * 关键点(中文)
39
+ * - 用于把中间 step 文本或 reasoning 事件回传给 Session 事件流。
40
+ */
41
+ onAssistantStepCallback?: SessionAssistantStepCallback;
42
+
43
+ /**
44
+ * UI stream chunk 回调。
45
+ *
46
+ * 关键点(中文)
47
+ * - 用于把底层模型 UI chunk 旁路输出到订阅流或 transport。
48
+ */
49
+ onUiMessageChunkCallback?: SessionUiMessageChunkCallback;
50
+
51
+ /**
52
+ * 本轮运行中待并入下一 step 的 user 消息。
53
+ *
54
+ * 关键点(中文)
55
+ * - 主要由 tool runtime 在当前 turn 内动态注入。
56
+ * - 这些消息只影响当前执行,不会自动持久化。
57
+ */
58
+ injectedUserMessages: SessionUserMessageV1[];
59
+
60
+ /**
61
+ * 本轮运行结束后待写入长期历史的 user 消息。
62
+ *
63
+ * 关键点(中文)
64
+ * - 为保证时间线顺序稳定,这些消息会在 assistant 结果落盘之后统一持久化。
65
+ */
66
+ deferredPersistedUserMessages: SessionUserMessageV1[];
67
+
68
+ /**
69
+ * 本轮运行结束前待并入最终 assistant 消息的 file parts。
70
+ *
71
+ * 关键点(中文)
72
+ * - 用于 tool/plugin 在执行期产生图片、文件等最终输出。
73
+ * - 这些 part 不依赖模型把 tool result 再复述一遍,直接落入 assistant UIMessage。
74
+ */
75
+ pendingAssistantFileParts: FileUIPart[];
76
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * ImagePlugin 类型定义。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这里仅定义 agent 对图片能力的最低层协议,不绑定 city 或任意上游 provider。
6
+ * - 图片生成结果使用 AI SDK UIMessage,保证 session 落盘格式与现有消息系统一致。
7
+ * - 字段保持 JSON 可序列化,便于通过 plugin action 与 tool bridge 传递。
8
+ */
9
+
10
+ import type { UIMessage } from "ai";
11
+ import type { JsonObject, JsonValue } from "@/types/common/Json.js";
12
+
13
+ /**
14
+ * 图片生成文本内容片段。
15
+ */
16
+ export interface ImagePluginTextContent {
17
+ /** 内容类型,固定为文本。 */
18
+ type: "text";
19
+ /** 生图提示词或上下文文本。 */
20
+ text: string;
21
+ }
22
+
23
+ /**
24
+ * 图片生成参考图片内容片段。
25
+ */
26
+ export interface ImagePluginFileContent {
27
+ /** 内容类型,固定为图片。 */
28
+ type: "image";
29
+ /** 远程图片 URL。 */
30
+ url?: string;
31
+ /** data URL 图片内容。 */
32
+ data_url?: string;
33
+ /** 图片 MIME 类型,例如 `image/png`。 */
34
+ media_type?: string;
35
+ }
36
+
37
+ /**
38
+ * 图片生成多模态内容片段。
39
+ */
40
+ export type ImagePluginContent =
41
+ | ImagePluginTextContent
42
+ | ImagePluginFileContent;
43
+
44
+ /**
45
+ * 图片生成上下文消息。
46
+ */
47
+ export interface ImagePluginMessage {
48
+ /** 消息角色。 */
49
+ role: "system" | "user" | "assistant";
50
+ /** 该消息内的文本与图片内容。 */
51
+ content: ImagePluginContent[];
52
+ }
53
+
54
+ /**
55
+ * ImagePlugin 调用输入。
56
+ */
57
+ export interface ImagePluginInput {
58
+ /** 图片模型引用。 */
59
+ model?: string;
60
+ /** 单句快捷提示词。 */
61
+ prompt?: string;
62
+ /** 多轮或多模态图片生成上下文。 */
63
+ messages?: ImagePluginMessage[];
64
+ /** 生成图片数量。 */
65
+ n?: number;
66
+ /** 生成图片数量,兼容部分上游使用的 count 命名。 */
67
+ count?: number;
68
+ /** 图片尺寸,例如 `1024x1024`。 */
69
+ size?: string;
70
+ /** 图片宽高比,例如 `1:1`。 */
71
+ aspect_ratio?: string;
72
+ /** 图片宽高比,兼容部分上游使用的 ratio 命名。 */
73
+ ratio?: string;
74
+ /** 图片质量,例如 `standard`、`hd`、`ultra`、`4k`。 */
75
+ quality?: string;
76
+ /** 随机种子。 */
77
+ seed?: number;
78
+ /** 业务侧任务 ID,用于异步图片任务幂等、追踪和恢复。 */
79
+ client_job_id?: string;
80
+ /** Provider 私有参数,例如 `{ openai: {...}, gemini: {...}, luchi: {...} }`。 */
81
+ provider_options?: JsonObject;
82
+ /** 允许外部 image 函数接收其他 JSON 可序列化参数。 */
83
+ [key: string]: JsonValue | ImagePluginMessage[] | undefined;
84
+ }
85
+
86
+ /**
87
+ * ImagePlugin 生成结果。
88
+ */
89
+ export type ImagePluginResult = UIMessage;
90
+
91
+ /**
92
+ * ImagePlugin 构造参数。
93
+ */
94
+ export interface ImagePluginOptions {
95
+ /** Plugin 稳定名称,默认 `image`。 */
96
+ name?: string;
97
+ /** Plugin 展示标题,默认 `Image`。 */
98
+ title?: string;
99
+ /** Plugin 用途说明。 */
100
+ description?: string;
101
+ /** 图片生成函数,通常传入 `(input) => city.ai.image(input)`。 */
102
+ image: (input: ImagePluginInput) => Promise<ImagePluginResult> | ImagePluginResult;
103
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Session Composer 配置类型。
3
+ *
4
+ * 关键点(中文)
5
+ * - Composer 是 session 执行阶段的可替换策略对象。
6
+ * - 调用方可以传入现成实例,也可以传入 factory 为每个 session 创建独立实例。
7
+ * - factory 适合 history/system 等可能绑定 sessionId、historyStore 或 session 元信息的 composer。
8
+ */
9
+
10
+ import type { Tool } from "ai";
11
+ import type { SessionCompactionComposer } from "@/executor/composer/compaction/SessionCompactionComposer.js";
12
+ import type { SessionContextComposer } from "@/executor/composer/context/SessionContextComposer.js";
13
+ import type { SessionHistoryComposer } from "@/executor/composer/history/SessionHistoryComposer.js";
14
+ import type { SessionSystemComposer } from "@/executor/composer/system/SessionSystemComposer.js";
15
+ import type { SessionHistoryStore } from "@/executor/store/history/SessionHistoryStore.js";
16
+ import type { AgentSessionSystemBlock } from "@/types/agent/SessionTypes.js";
17
+
18
+ /**
19
+ * 单个 session 创建 Composer 时可读取的稳定上下文。
20
+ */
21
+ export interface SessionComposerFactoryContext {
22
+ /**
23
+ * 当前 agent 的稳定标识。
24
+ */
25
+ agentId: string;
26
+
27
+ /**
28
+ * 当前 agent 绑定的项目根目录。
29
+ */
30
+ projectRoot: string;
31
+
32
+ /**
33
+ * 当前 session 唯一标识。
34
+ */
35
+ sessionId: string;
36
+
37
+ /**
38
+ * 当前 session 对应的 history 事实源。
39
+ *
40
+ * 关键点(中文)
41
+ * - history composer 和 compaction composer 通常需要基于它读取或压缩历史。
42
+ * - factory 每次收到的都是当前 session 自己的 store。
43
+ */
44
+ historyStore: SessionHistoryStore;
45
+
46
+ /**
47
+ * 当前 agent 默认工具集合读取器。
48
+ */
49
+ getTools: () => Record<string, Tool>;
50
+
51
+ /**
52
+ * 读取当前 SDK 调用方传入的 instruction system blocks。
53
+ */
54
+ getInstructionSystemBlocks: () => AgentSessionSystemBlock[];
55
+
56
+ /**
57
+ * 读取当前 agent 显式注入的受托管 plugin system blocks。
58
+ */
59
+ getManagedPluginSystemBlocks: () => Promise<AgentSessionSystemBlock[]>;
60
+
61
+ /**
62
+ * 读取当前 agent 显式注册 plugin 的 system blocks。
63
+ */
64
+ getPluginSystemBlocks: () => Promise<AgentSessionSystemBlock[]>;
65
+
66
+ /**
67
+ * 读取当前 session 首次创建时间(ms)。
68
+ */
69
+ getSessionCreatedAt: () => number;
70
+
71
+ /**
72
+ * 读取当前 session 初始化时解析到的系统时区。
73
+ */
74
+ getSessionTimezone: () => string;
75
+ }
76
+
77
+ /**
78
+ * 可直接传入 composer 实例,也可传入按 session 创建 composer 的 factory。
79
+ */
80
+ export type SessionComposerInput<TComposer> =
81
+ | TComposer
82
+ | ((context: SessionComposerFactoryContext) => TComposer);
83
+
84
+ /**
85
+ * Session Composer 覆盖项。
86
+ */
87
+ export interface SessionComposerOptions {
88
+ /**
89
+ * 覆盖本轮 system messages 的 composer。
90
+ */
91
+ systemComposer?: SessionComposerInput<SessionSystemComposer>;
92
+
93
+ /**
94
+ * 覆盖本轮 history messages 的 composer。
95
+ */
96
+ historyComposer?: SessionComposerInput<SessionHistoryComposer>;
97
+
98
+ /**
99
+ * 覆盖本轮 tools、step hooks、fallback assistant message 等运行上下文 composer。
100
+ */
101
+ contextComposer?: SessionComposerInput<SessionContextComposer>;
102
+
103
+ /**
104
+ * 覆盖本轮上下文压缩 composer。
105
+ */
106
+ compactionComposer?: SessionComposerInput<SessionCompactionComposer>;
107
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * SessionLocalState:本地 Session 运行态快照。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这里承载本地 Session 在内存中的可变运行态。
6
+ * - state / turn / view 三类 service 通过共享该对象协作,避免彼此复制状态。
7
+ * - 这里只保存运行态,不承载行为逻辑。
8
+ */
9
+
10
+ import type { AgentSessionConfigSnapshot } from "@/types/agent/AgentTypes.js";
11
+
12
+ /**
13
+ * 本地 Session 内存状态。
14
+ */
15
+ export interface SessionLocalState {
16
+ /**
17
+ * 当前 session 配置快照。
18
+ */
19
+ sessionConfig: AgentSessionConfigSnapshot;
20
+
21
+ /**
22
+ * 当前 session 创建时间(毫秒时间戳)。
23
+ */
24
+ createdAt: number;
25
+
26
+ /**
27
+ * 当前 session 参考时区。
28
+ */
29
+ timezone: string;
30
+
31
+ /**
32
+ * initialize() 过程中的并发复用 Promise。
33
+ */
34
+ initializePromise: Promise<void> | null;
35
+
36
+ /**
37
+ * ensureReadyForExecution() 过程中的并发复用 Promise。
38
+ */
39
+ ensureConfiguredPromise: Promise<void> | null;
40
+ }