@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,213 @@
1
+ /**
2
+ * ExecutorRecoveryPolicy:执行恢复与重试策略。
3
+ *
4
+ * 关键点(中文)
5
+ * - 统一封装“压缩后重试”和“普通失败兜底”逻辑。
6
+ * - Executor 只负责准备输入与调用策略,不再直接承载重试状态机。
7
+ * - 不改变外部行为,只把异常分流规则集中到一个地方。
8
+ */
9
+
10
+ import type { LanguageModel } from "ai";
11
+ import type { Logger } from "@/utils/logger/Logger.js";
12
+ import type { SessionCompactionComposer } from "@executor/composer/compaction/SessionCompactionComposer.js";
13
+ import type { SessionContextComposer } from "@executor/composer/context/SessionContextComposer.js";
14
+ import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
15
+ import type {
16
+ SessionExecuteInput,
17
+ SessionRunResult,
18
+ } from "@/executor/types/SessionRun.js";
19
+
20
+ /**
21
+ * 可压缩错误的最大重试次数。
22
+ */
23
+ const MAX_COMPACTION_RETRY_ATTEMPTS = 3;
24
+
25
+ interface ExecutorRecoveryPolicyOptions {
26
+ /**
27
+ * 当前 session 对应的 compaction Composer。
28
+ */
29
+ compaction_composer: SessionCompactionComposer;
30
+
31
+ /**
32
+ * 当前 session 对应的 context Composer。
33
+ */
34
+ context_composer: SessionContextComposer;
35
+
36
+ /**
37
+ * 当前 session 统一日志器。
38
+ */
39
+ logger: Logger;
40
+ }
41
+
42
+ interface ExecutorPrepareRunInput {
43
+ /**
44
+ * 当前轮用户 query。
45
+ */
46
+ query: string;
47
+
48
+ /**
49
+ * 当前轮模型实例。
50
+ */
51
+ model: LanguageModel;
52
+
53
+ /**
54
+ * 当前显式运行上下文。
55
+ */
56
+ run_context: SessionRunContext;
57
+
58
+ /**
59
+ * 当前压缩重试次数。
60
+ */
61
+ retry_count: number;
62
+ }
63
+
64
+ interface ExecutorExecutePreparedRunInput {
65
+ /**
66
+ * 已装配好的执行输入。
67
+ */
68
+ execute_input: SessionExecuteInput;
69
+
70
+ /**
71
+ * 当前轮模型实例。
72
+ */
73
+ model: LanguageModel;
74
+
75
+ /**
76
+ * 当前显式运行上下文。
77
+ */
78
+ run_context: SessionRunContext;
79
+ }
80
+
81
+ interface ExecutorRecoveryRunInput {
82
+ /**
83
+ * 当前轮用户 query。
84
+ */
85
+ query: string;
86
+
87
+ /**
88
+ * 当前轮模型实例。
89
+ */
90
+ model: LanguageModel;
91
+
92
+ /**
93
+ * 当前显式运行上下文。
94
+ */
95
+ run_context: SessionRunContext;
96
+
97
+ /**
98
+ * 运行前装配执行输入。
99
+ */
100
+ prepare_execute_input: (
101
+ input: ExecutorPrepareRunInput,
102
+ ) => Promise<SessionExecuteInput>;
103
+
104
+ /**
105
+ * 执行已装配好的运行输入。
106
+ */
107
+ execute_prepared_run: (
108
+ input: ExecutorExecutePreparedRunInput,
109
+ ) => Promise<SessionRunResult>;
110
+ }
111
+
112
+ /**
113
+ * 执行恢复与重试策略服务。
114
+ */
115
+ export class ExecutorRecoveryPolicy {
116
+ private readonly compaction_composer: SessionCompactionComposer;
117
+ private readonly context_composer: SessionContextComposer;
118
+ private readonly logger: Logger;
119
+ private retry_count = 0;
120
+
121
+ constructor(options: ExecutorRecoveryPolicyOptions) {
122
+ this.compaction_composer = options.compaction_composer;
123
+ this.context_composer = options.context_composer;
124
+ this.logger = options.logger;
125
+ }
126
+
127
+ /**
128
+ * 重置当前 run 级状态。
129
+ */
130
+ reset_run_state(): void {
131
+ this.retry_count = 0;
132
+ }
133
+
134
+ /**
135
+ * 执行一次带恢复策略的 session run。
136
+ */
137
+ async run_with_retry(
138
+ input: ExecutorRecoveryRunInput,
139
+ ): Promise<SessionRunResult> {
140
+ try {
141
+ const execute_input = await input.prepare_execute_input({
142
+ query: input.query,
143
+ model: input.model,
144
+ run_context: input.run_context,
145
+ retry_count: this.retry_count,
146
+ });
147
+ return await input.execute_prepared_run({
148
+ execute_input,
149
+ model: input.model,
150
+ run_context: input.run_context,
151
+ });
152
+ } catch (error) {
153
+ if (this.compaction_composer.shouldCompactOnError(error)) {
154
+ await this.logger.log("info", "[agent] compacting", {
155
+ retryCount: this.retry_count,
156
+ error: String(error),
157
+ });
158
+
159
+ if (this.retry_count < MAX_COMPACTION_RETRY_ATTEMPTS) {
160
+ this.retry_count += 1;
161
+ return await this.run_with_retry(input);
162
+ }
163
+
164
+ return this.build_failure_result({
165
+ error_text:
166
+ "Context length exceeded and retries failed. Please resend your question.",
167
+ fallback_text:
168
+ "Context length exceeded and retries failed. Please resend your question.",
169
+ run_context: input.run_context,
170
+ });
171
+ }
172
+
173
+ const error_text = String(error);
174
+ await this.logger.log("error", "Executor execution failed", {
175
+ error: error_text,
176
+ });
177
+ return this.build_failure_result({
178
+ error_text,
179
+ fallback_text: `Execution failed: ${error_text}`,
180
+ run_context: input.run_context,
181
+ });
182
+ }
183
+ }
184
+
185
+ private build_failure_result(input: {
186
+ /**
187
+ * 对外暴露的错误文本。
188
+ */
189
+ error_text: string;
190
+
191
+ /**
192
+ * fallback assistant 消息文本。
193
+ */
194
+ fallback_text: string;
195
+
196
+ /**
197
+ * 当前显式运行上下文。
198
+ */
199
+ run_context: SessionRunContext;
200
+ }): SessionRunResult {
201
+ return {
202
+ success: false,
203
+ error: input.error_text,
204
+ assistantMessage: this.context_composer.buildFallbackAssistantMessage(
205
+ input.fallback_text,
206
+ input.run_context,
207
+ ),
208
+ deferredPersistedUserMessages: [
209
+ ...input.run_context.deferredPersistedUserMessages,
210
+ ],
211
+ };
212
+ }
213
+ }
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Plugin tool 运行时桥接。
3
+ *
4
+ * 关键点(中文)
5
+ * - tool 层不直接持有 agent 或 plugin registry,只通过装配期注入的 PluginPort 调用 action。
6
+ * - 如果 action 返回 AI SDK UIMessage,则抽取 file parts 并入最终 assistant 消息。
7
+ * - 返回给模型的 tool result 只保留短摘要,避免 data URL 等大内容污染上下文。
8
+ */
9
+
10
+ import type { FileUIPart } from "ai";
11
+ import type { JsonObject, JsonValue } from "@/types/common/Json.js";
12
+ import type { PluginPort } from "@/plugin/types/Plugin.js";
13
+ import type {
14
+ PluginCallInput,
15
+ PluginCallToolResult,
16
+ } from "@/executor/tools/plugin/types/PluginTool.js";
17
+ import { enqueueAssistantFileParts } from "@executor/SessionRunScope.js";
18
+
19
+ let plugin_tool_runtime: PluginPort | null = null;
20
+
21
+ /**
22
+ * 注入 plugin tool 运行时。
23
+ */
24
+ export function setPluginToolRuntime(next: PluginPort): void {
25
+ plugin_tool_runtime = next;
26
+ }
27
+
28
+ /**
29
+ * 读取已注入的 plugin tool 运行时。
30
+ */
31
+ function require_plugin_tool_runtime(): PluginPort {
32
+ if (plugin_tool_runtime) return plugin_tool_runtime;
33
+ throw new Error(
34
+ "Plugin tool runtime is not initialized. Ensure agent assembly completed before using plugin_call.",
35
+ );
36
+ }
37
+
38
+ /**
39
+ * 判断值是否为普通对象。
40
+ */
41
+ function to_json_object(value: unknown): JsonObject | null {
42
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
43
+ return value as JsonObject;
44
+ }
45
+
46
+ /**
47
+ * 判断 UI part 是否为 file part。
48
+ */
49
+ function is_file_part(value: unknown): value is FileUIPart {
50
+ const record = to_json_object(value);
51
+ if (!record) return false;
52
+ return (
53
+ record.type === "file" &&
54
+ typeof record.mediaType === "string" &&
55
+ typeof record.url === "string"
56
+ );
57
+ }
58
+
59
+ /**
60
+ * 从 action data 中抽取可落盘的 assistant file parts。
61
+ */
62
+ function extract_assistant_file_parts(data: JsonValue | undefined): FileUIPart[] {
63
+ const message = to_json_object(data);
64
+ const parts: unknown[] = Array.isArray(message?.parts) ? message.parts : [];
65
+ return parts.filter(is_file_part);
66
+ }
67
+
68
+ /**
69
+ * 生成给模型读取的短摘要。
70
+ */
71
+ function summarize_action_data(data: JsonValue | undefined): JsonObject {
72
+ const message = to_json_object(data);
73
+ const parts: unknown[] = Array.isArray(message?.parts) ? message.parts : [];
74
+ const file_parts = parts.filter(is_file_part);
75
+ if (parts.length > 0) {
76
+ return {
77
+ kind: "ui_message",
78
+ role: typeof message?.role === "string" ? message.role : "assistant",
79
+ part_count: parts.length,
80
+ file_count: file_parts.length,
81
+ files: file_parts.map((part, index) => ({
82
+ index,
83
+ mediaType: part.mediaType,
84
+ filename: typeof part.filename === "string" ? part.filename : "",
85
+ // 关键点(中文):不把 data URL 或长 URL 原样返回给模型,完整内容只进入 assistant file part。
86
+ has_url: Boolean(part.url),
87
+ })),
88
+ };
89
+ }
90
+ if (data === undefined) return {};
91
+ return {
92
+ kind: "json",
93
+ value: data,
94
+ };
95
+ }
96
+
97
+ /**
98
+ * 调用 plugin action 并桥接最终 assistant file parts。
99
+ */
100
+ export async function invokePluginCallTool(
101
+ input: PluginCallInput,
102
+ ): Promise<PluginCallToolResult> {
103
+ const plugin = String(input.plugin || "").trim();
104
+ const action = String(input.action || "").trim();
105
+ const payload = to_json_object(input.payload ?? {}) ?? {};
106
+ if (!plugin) {
107
+ return {
108
+ success: false,
109
+ plugin,
110
+ action,
111
+ assistant_file_count: 0,
112
+ message: "plugin is required",
113
+ error: "plugin is required",
114
+ };
115
+ }
116
+ if (!action) {
117
+ return {
118
+ success: false,
119
+ plugin,
120
+ action,
121
+ assistant_file_count: 0,
122
+ message: "action is required",
123
+ error: "action is required",
124
+ };
125
+ }
126
+
127
+ try {
128
+ const runtime = require_plugin_tool_runtime();
129
+ const result = await runtime.runAction({
130
+ plugin,
131
+ action,
132
+ payload,
133
+ });
134
+ const file_parts = result.success
135
+ ? extract_assistant_file_parts(result.data)
136
+ : [];
137
+ if (file_parts.length > 0) {
138
+ enqueueAssistantFileParts(file_parts);
139
+ }
140
+ return {
141
+ success: result.success,
142
+ plugin,
143
+ action,
144
+ assistant_file_count: file_parts.length,
145
+ message:
146
+ String(result.message || result.error || "").trim() ||
147
+ (result.success ? "plugin action completed" : "plugin action failed"),
148
+ ...(result.error ? { error: result.error } : {}),
149
+ data: summarize_action_data(result.data),
150
+ };
151
+ } catch (error) {
152
+ return {
153
+ success: false,
154
+ plugin,
155
+ action,
156
+ assistant_file_count: 0,
157
+ message: String(error),
158
+ error: String(error),
159
+ };
160
+ }
161
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Plugin tools。
3
+ *
4
+ * 设计目标(中文)
5
+ * - plugin_call 是 agent 内置 plugin action 桥,类似 shell tool 的底层能力入口。
6
+ * - tool 只负责 AI SDK 工具协议适配,不理解具体 plugin 的业务语义。
7
+ * - 插件业务输出若包含 UIMessage file parts,会由 bridge 并入最终 assistant 消息。
8
+ */
9
+
10
+ import { tool } from "ai";
11
+ import type { PluginCallInput } from "@/executor/tools/plugin/types/PluginTool.js";
12
+ import { invokePluginCallTool, setPluginToolRuntime } from "./PluginToolBridge.js";
13
+ import { plugin_call_input_schema } from "./PluginToolSchemas.js";
14
+
15
+ export { setPluginToolRuntime } from "./PluginToolBridge.js";
16
+
17
+ /**
18
+ * `plugin_call`:调用已注册 plugin action。
19
+ */
20
+ export const plugin_call = tool({
21
+ description:
22
+ "Call a registered agent plugin action. Use this for plugin capabilities such as image generation. Generated files may be attached to the final assistant message automatically.",
23
+ inputSchema: plugin_call_input_schema,
24
+ execute: async (input) => await invokePluginCallTool(input as PluginCallInput),
25
+ });
26
+
27
+ /**
28
+ * Plugin 工具导出集合。
29
+ */
30
+ export const plugin_tools = {
31
+ plugin_call,
32
+ };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Plugin tool 输入 schema。
3
+ *
4
+ * 关键点(中文)
5
+ * - plugin_call 是 agent 内置的最低层 plugin action 桥。
6
+ * - schema 只描述模型需要提交的目标 plugin、action 与 JSON payload。
7
+ */
8
+
9
+ import { z } from "zod";
10
+
11
+ export const plugin_call_input_schema = z.object({
12
+ plugin: z.string().describe("Registered plugin name to call, for example image."),
13
+ action: z.string().describe("Plugin action name to execute, for example generate."),
14
+ payload: z
15
+ .object({})
16
+ .passthrough()
17
+ .optional()
18
+ .default({})
19
+ .describe("JSON payload passed to the plugin action."),
20
+ });
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Plugin tool 类型定义。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这里描述模型通过 plugin_call 提交的最低层调用协议。
6
+ * - payload 保持 JSON 对象,避免 tool 层理解具体 plugin 的业务字段。
7
+ */
8
+
9
+ import type { JsonObject } from "@/types/common/Json.js";
10
+
11
+ /**
12
+ * plugin_call 输入。
13
+ */
14
+ export interface PluginCallInput {
15
+ /** 目标 plugin 名称。 */
16
+ plugin: string;
17
+ /** 目标 action 名称。 */
18
+ action: string;
19
+ /** 传给 plugin action 的 JSON payload。 */
20
+ payload?: JsonObject;
21
+ }
22
+
23
+ /**
24
+ * plugin_call 返回给模型的摘要结果。
25
+ */
26
+ export interface PluginCallToolResult {
27
+ /** 调用是否成功。 */
28
+ success: boolean;
29
+ /** 目标 plugin 名称。 */
30
+ plugin: string;
31
+ /** 目标 action 名称。 */
32
+ action: string;
33
+ /** 本次 action 产生并写入 assistant 消息的 file part 数量。 */
34
+ assistant_file_count: number;
35
+ /** 人类可读消息。 */
36
+ message: string;
37
+ /** 错误信息。 */
38
+ error?: string;
39
+ /** 返回给模型读取的短摘要数据。 */
40
+ data?: JsonObject;
41
+ }
@@ -16,7 +16,7 @@ import type { ShellActionResponse } from "@/executor/tools/shell/types/ShellPlug
16
16
  import {
17
17
  enqueueDeferredPersistedUserMessage,
18
18
  enqueueInjectedUserMessage,
19
- getSessionRunScope,
19
+ getSessionRunContext,
20
20
  } from "@executor/SessionRunScope.js";
21
21
 
22
22
  /**
@@ -88,8 +88,8 @@ async function injectUserTextMessage(params: {
88
88
  */
89
89
  note?: string;
90
90
  }): Promise<boolean> {
91
- const store = getSessionRunScope();
92
- const sessionId = String(store?.sessionId || "").trim();
91
+ const run_context = getSessionRunContext();
92
+ const sessionId = String(run_context?.sessionId || "").trim();
93
93
  const text = String(params.text || "").trim();
94
94
  if (!sessionId || !text) return false;
95
95
  const note = String(params.note || "runtime_injected_user_message");
@@ -9,7 +9,9 @@
9
9
 
10
10
  import type { Tool, UIMessageChunk } from "ai";
11
11
  import type { SessionMessageV1 } from "@/executor/types/SessionMessages.js";
12
+ import type { SessionUserMessageV1 } from "@/executor/types/SessionMessages.js";
12
13
  import type { SessionSystemMessage } from "@/executor/types/SessionPrompts.js";
14
+ import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
13
15
 
14
16
  /**
15
17
  * Assistant step 可见性。
@@ -94,6 +96,15 @@ export interface SessionRunResult {
94
96
  * 最终 assistant 消息。
95
97
  */
96
98
  assistantMessage: SessionMessageV1;
99
+
100
+ /**
101
+ * 本轮执行结束后待写入长期历史的 user 消息。
102
+ *
103
+ * 关键点(中文)
104
+ * - 这些消息通常由 tool 运行时在执行过程中动态注入。
105
+ * - 为保证消息顺序稳定,统一在 assistant 结果落盘后再由外层 Session 持久化。
106
+ */
107
+ deferredPersistedUserMessages?: SessionUserMessageV1[];
97
108
  }
98
109
 
99
110
  /**
@@ -104,6 +115,15 @@ export interface SessionRunInput {
104
115
  * 本轮用户输入查询文本。
105
116
  */
106
117
  query: string;
118
+
119
+ /**
120
+ * 本轮显式运行上下文。
121
+ *
122
+ * 关键点(中文)
123
+ * - 这里承载 step 合并、UI chunk 回调等跨组件运行期数据。
124
+ * - 若未传入,则由执行器按最小默认值兜底创建。
125
+ */
126
+ runContext?: SessionRunContext;
107
127
  }
108
128
 
109
129
  /**
package/src/index.ts CHANGED
@@ -10,6 +10,8 @@
10
10
  // Agent 入口
11
11
  export { Agent } from "./agent/local/Agent.js";
12
12
  export { RemoteAgent } from "./agent/remote/RemoteAgent.js";
13
+ export { Session } from "./session/Session.js";
14
+ export type { SessionOptions } from "./types/session/SessionOptions.js";
13
15
  export {
14
16
  inferAgentModelLabel,
15
17
  normalizeAgentModel,
@@ -17,8 +19,10 @@ export {
17
19
  export type {
18
20
  AgentSessionCollection,
19
21
  AgentModel,
22
+ AgentManagedSession,
20
23
  AgentSessionActor,
21
24
  AgentSession,
25
+ AgentSessionConstructor,
22
26
  AgentCreateSessionInput,
23
27
  AgentListSessionsInput,
24
28
  AgentOptions,
@@ -65,6 +69,7 @@ export type {
65
69
 
66
70
  // Plugin 作者 API
67
71
  export { BasePlugin } from "./plugin/core/BasePlugin.js";
72
+ export { ImagePlugin } from "./plugin/core/ImagePlugin.js";
68
73
 
69
74
  // Session 与即时执行集成
70
75
  export { Executor } from "./executor/Executor.js";
@@ -73,9 +78,27 @@ export {
73
78
  getSessionRunScope,
74
79
  } from "./executor/SessionRunScope.js";
75
80
  export { JsonlSessionHistoryComposer } from "./executor/composer/history/jsonl/JsonlSessionHistoryComposer.js";
81
+ export { LocalSessionContextComposer } from "./executor/composer/context/LocalSessionContextComposer.js";
76
82
  export { JsonlSessionHistoryStore } from "./executor/store/history/jsonl/JsonlSessionHistoryStore.js";
77
83
  export { JsonlSessionCompactionComposer } from "./executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.js";
84
+ export type {
85
+ SessionCompactionComposer,
86
+ SessionCompactionInput,
87
+ } from "./executor/composer/compaction/SessionCompactionComposer.js";
88
+ export type {
89
+ SessionContextComposer,
90
+ SessionContextComposeResult,
91
+ } from "./executor/composer/context/SessionContextComposer.js";
92
+ export type {
93
+ SessionHistoryComposer,
94
+ SessionHistoryPrepareInput,
95
+ } from "./executor/composer/history/SessionHistoryComposer.js";
78
96
  export type { SessionSystemComposer } from "./executor/composer/system/SessionSystemComposer.js";
97
+ export type {
98
+ SessionComposerFactoryContext,
99
+ SessionComposerInput,
100
+ SessionComposerOptions,
101
+ } from "./types/session/SessionComposerOptions.js";
79
102
  export { transformPromptsIntoSystemMessages } from "./executor/composer/system/default/PromptRenderer.js";
80
103
  export {
81
104
  loadStaticSystemPrompts,
@@ -202,6 +225,16 @@ export type {
202
225
  } from "./types/runtime/http/InlineInstant.js";
203
226
 
204
227
  // Plugin 作者与控制面类型
228
+ export type {
229
+ ImagePluginContent,
230
+ ImagePluginFileContent,
231
+ ImagePluginInput,
232
+ ImagePluginMessage,
233
+ ImagePluginOptions,
234
+ ImagePluginResult,
235
+ ImagePluginTextContent,
236
+ } from "./types/plugin/ImagePlugin.js";
237
+
205
238
  export type {
206
239
  Plugin,
207
240
  PluginAction,
@@ -55,12 +55,12 @@ export abstract class BasePlugin implements Plugin {
55
55
  /**
56
56
  * 插件标题。
57
57
  */
58
- readonly title = "";
58
+ readonly title: string = "";
59
59
 
60
60
  /**
61
61
  * 插件说明。
62
62
  */
63
- readonly description = "";
63
+ readonly description: string = "";
64
64
 
65
65
  /**
66
66
  * 插件配置定义。