@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.
- package/README.md +1 -1
- package/bin/agent/local/Agent.d.ts +8 -121
- package/bin/agent/local/Agent.d.ts.map +1 -1
- package/bin/agent/local/Agent.js +63 -381
- package/bin/agent/local/Agent.js.map +1 -1
- package/bin/agent/local/AgentRuntimeFactory.d.ts +2 -2
- package/bin/agent/local/AgentRuntimeFactory.d.ts.map +1 -1
- package/bin/agent/local/AgentRuntimeFactory.js +2 -23
- package/bin/agent/local/AgentRuntimeFactory.js.map +1 -1
- package/bin/agent/local/services/AgentAssemblyService.d.ts +112 -0
- package/bin/agent/local/services/AgentAssemblyService.d.ts.map +1 -0
- package/bin/agent/local/services/AgentAssemblyService.js +135 -0
- package/bin/agent/local/services/AgentAssemblyService.js.map +1 -0
- package/bin/agent/local/services/AgentLifecycleService.d.ts +59 -0
- package/bin/agent/local/services/AgentLifecycleService.d.ts.map +1 -0
- package/bin/agent/local/services/AgentLifecycleService.js +136 -0
- package/bin/agent/local/services/AgentLifecycleService.js.map +1 -0
- package/bin/agent/local/services/AgentSessionManager.d.ts +106 -0
- package/bin/agent/local/services/AgentSessionManager.d.ts.map +1 -0
- package/bin/agent/local/services/AgentSessionManager.js +182 -0
- package/bin/agent/local/services/AgentSessionManager.js.map +1 -0
- package/bin/executor/Executor.d.ts +7 -24
- package/bin/executor/Executor.d.ts.map +1 -1
- package/bin/executor/Executor.js +73 -361
- package/bin/executor/Executor.js.map +1 -1
- package/bin/executor/SessionRunScope.d.ts +18 -34
- package/bin/executor/SessionRunScope.d.ts.map +1 -1
- package/bin/executor/SessionRunScope.js +42 -28
- package/bin/executor/SessionRunScope.js.map +1 -1
- package/bin/executor/composer/context/LocalSessionContextComposer.d.ts +5 -3
- package/bin/executor/composer/context/LocalSessionContextComposer.d.ts.map +1 -1
- package/bin/executor/composer/context/LocalSessionContextComposer.js +11 -18
- package/bin/executor/composer/context/LocalSessionContextComposer.js.map +1 -1
- package/bin/executor/composer/context/SessionContextComposer.d.ts +8 -3
- package/bin/executor/composer/context/SessionContextComposer.d.ts.map +1 -1
- package/bin/executor/composer/system/SessionSystemComposer.d.ts +2 -1
- package/bin/executor/composer/system/SessionSystemComposer.d.ts.map +1 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts +2 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts.map +1 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js +2 -4
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineRunner.d.ts +62 -0
- package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -0
- package/bin/executor/core-engine/CoreEngineRunner.js +309 -0
- package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -0
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts +5 -0
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.js +2 -4
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.js.map +1 -1
- package/bin/executor/services/ExecutorInflightService.d.ts +39 -0
- package/bin/executor/services/ExecutorInflightService.d.ts.map +1 -0
- package/bin/executor/services/ExecutorInflightService.js +75 -0
- package/bin/executor/services/ExecutorInflightService.js.map +1 -0
- package/bin/executor/services/ExecutorRecoveryPolicy.d.ts +103 -0
- package/bin/executor/services/ExecutorRecoveryPolicy.d.ts.map +1 -0
- package/bin/executor/services/ExecutorRecoveryPolicy.js +87 -0
- package/bin/executor/services/ExecutorRecoveryPolicy.js.map +1 -0
- package/bin/executor/tools/plugin/PluginToolBridge.d.ts +19 -0
- package/bin/executor/tools/plugin/PluginToolBridge.d.ts.map +1 -0
- package/bin/executor/tools/plugin/PluginToolBridge.js +143 -0
- package/bin/executor/tools/plugin/PluginToolBridge.js.map +1 -0
- package/bin/executor/tools/plugin/PluginToolDefinition.d.ts +32 -0
- package/bin/executor/tools/plugin/PluginToolDefinition.d.ts.map +1 -0
- package/bin/executor/tools/plugin/PluginToolDefinition.js +27 -0
- package/bin/executor/tools/plugin/PluginToolDefinition.js.map +1 -0
- package/bin/executor/tools/plugin/PluginToolSchemas.d.ts +14 -0
- package/bin/executor/tools/plugin/PluginToolSchemas.d.ts.map +1 -0
- package/bin/executor/tools/plugin/PluginToolSchemas.js +19 -0
- package/bin/executor/tools/plugin/PluginToolSchemas.js.map +1 -0
- package/bin/executor/tools/plugin/types/PluginTool.d.ts +39 -0
- package/bin/executor/tools/plugin/types/PluginTool.d.ts.map +1 -0
- package/bin/executor/tools/plugin/types/PluginTool.js +9 -0
- package/bin/executor/tools/plugin/types/PluginTool.js.map +1 -0
- package/bin/executor/tools/shell/ShellToolBridge.js +3 -3
- package/bin/executor/tools/shell/ShellToolBridge.js.map +1 -1
- package/bin/executor/types/SessionRun.d.ts +18 -0
- package/bin/executor/types/SessionRun.d.ts.map +1 -1
- package/bin/index.d.ts +10 -1
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +3 -0
- package/bin/index.js.map +1 -1
- package/bin/plugin/core/BasePlugin.d.ts +2 -2
- package/bin/plugin/core/BasePlugin.d.ts.map +1 -1
- package/bin/plugin/core/BasePlugin.js.map +1 -1
- package/bin/plugin/core/ImagePlugin.d.ts +56 -0
- package/bin/plugin/core/ImagePlugin.d.ts.map +1 -0
- package/bin/plugin/core/ImagePlugin.js +109 -0
- package/bin/plugin/core/ImagePlugin.js.map +1 -0
- package/bin/session/Session.d.ts +14 -83
- package/bin/session/Session.d.ts.map +1 -1
- package/bin/session/Session.js +139 -362
- package/bin/session/Session.js.map +1 -1
- package/bin/session/SessionSystemBuilder.d.ts +2 -1
- package/bin/session/SessionSystemBuilder.d.ts.map +1 -1
- package/bin/session/SessionSystemBuilder.js +2 -3
- package/bin/session/SessionSystemBuilder.js.map +1 -1
- package/bin/session/services/SessionStateService.d.ts +132 -0
- package/bin/session/services/SessionStateService.d.ts.map +1 -0
- package/bin/session/services/SessionStateService.js +242 -0
- package/bin/session/services/SessionStateService.js.map +1 -0
- package/bin/session/services/SessionTurnService.d.ts +66 -0
- package/bin/session/services/SessionTurnService.d.ts.map +1 -0
- package/bin/session/services/SessionTurnService.js +137 -0
- package/bin/session/services/SessionTurnService.js.map +1 -0
- package/bin/session/services/SessionViewService.d.ts +105 -0
- package/bin/session/services/SessionViewService.d.ts.map +1 -0
- package/bin/session/services/SessionViewService.js +184 -0
- package/bin/session/services/SessionViewService.js.map +1 -0
- package/bin/types/agent/AgentOptions.d.ts +18 -0
- package/bin/types/agent/AgentOptions.d.ts.map +1 -1
- package/bin/types/agent/AgentTypes.d.ts +3 -1
- package/bin/types/agent/AgentTypes.d.ts.map +1 -1
- package/bin/types/agent/SessionTypes.d.ts.map +1 -1
- package/bin/types/executor/SessionRunContext.d.ts +66 -0
- package/bin/types/executor/SessionRunContext.d.ts.map +1 -0
- package/bin/types/executor/SessionRunContext.js +10 -0
- package/bin/types/executor/SessionRunContext.js.map +1 -0
- package/bin/types/plugin/ImagePlugin.d.ts +94 -0
- package/bin/types/plugin/ImagePlugin.d.ts.map +1 -0
- package/bin/types/plugin/ImagePlugin.js +10 -0
- package/bin/types/plugin/ImagePlugin.js.map +1 -0
- package/bin/types/session/SessionComposerOptions.d.ts +90 -0
- package/bin/types/session/SessionComposerOptions.d.ts.map +1 -0
- package/bin/types/session/SessionComposerOptions.js +10 -0
- package/bin/types/session/SessionComposerOptions.js.map +1 -0
- package/bin/types/session/SessionLocalState.d.ts +35 -0
- package/bin/types/session/SessionLocalState.d.ts.map +1 -0
- package/bin/types/session/SessionLocalState.js +10 -0
- package/bin/types/session/SessionLocalState.js.map +1 -0
- package/bin/types/session/SessionOptions.d.ts +85 -0
- package/bin/types/session/SessionOptions.d.ts.map +1 -0
- package/bin/types/session/SessionOptions.js +10 -0
- package/bin/types/session/SessionOptions.js.map +1 -0
- package/package.json +1 -1
- package/src/agent/local/Agent.ts +74 -433
- package/src/agent/local/AgentRuntimeFactory.ts +4 -25
- package/src/agent/local/services/AgentAssemblyService.ts +268 -0
- package/src/agent/local/services/AgentLifecycleService.ts +187 -0
- package/src/agent/local/services/AgentSessionManager.ts +291 -0
- package/src/executor/Executor.ts +103 -441
- package/src/executor/README.md +4 -4
- package/src/executor/SessionRunScope.ts +47 -71
- package/src/executor/composer/context/LocalSessionContextComposer.ts +24 -20
- package/src/executor/composer/context/SessionContextComposer.ts +13 -3
- package/src/executor/composer/system/SessionSystemComposer.ts +2 -1
- package/src/executor/composer/system/default/DefaultSessionSystemComposer.ts +3 -4
- package/src/executor/core-engine/CoreEngineRunner.ts +433 -0
- package/src/executor/core-engine/CoreEngineUiStreamCollector.ts +7 -5
- package/src/executor/services/ExecutorInflightService.ts +101 -0
- package/src/executor/services/ExecutorRecoveryPolicy.ts +213 -0
- package/src/executor/tools/plugin/PluginToolBridge.ts +161 -0
- package/src/executor/tools/plugin/PluginToolDefinition.ts +32 -0
- package/src/executor/tools/plugin/PluginToolSchemas.ts +20 -0
- package/src/executor/tools/plugin/types/PluginTool.ts +41 -0
- package/src/executor/tools/shell/ShellToolBridge.ts +3 -3
- package/src/executor/types/SessionRun.ts +20 -0
- package/src/index.ts +33 -0
- package/src/plugin/core/BasePlugin.ts +2 -2
- package/src/plugin/core/ImagePlugin.ts +128 -0
- package/src/session/Session.ts +178 -485
- package/src/session/SessionSystemBuilder.ts +3 -3
- package/src/session/services/SessionStateService.ts +341 -0
- package/src/session/services/SessionTurnService.ts +202 -0
- package/src/session/services/SessionViewService.ts +301 -0
- package/src/types/agent/AgentOptions.ts +25 -0
- package/src/types/agent/AgentTypes.ts +10 -0
- package/src/types/agent/SessionTypes.ts +1 -0
- package/src/types/executor/SessionRunContext.ts +76 -0
- package/src/types/plugin/ImagePlugin.ts +103 -0
- package/src/types/session/SessionComposerOptions.ts +107 -0
- package/src/types/session/SessionLocalState.ts +40 -0
- package/src/types/session/SessionOptions.ts +99 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentSessionManager:本地 Agent session 管理服务。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 统一管理 session 缓存、创建、恢复、默认配置注入与 session collection。
|
|
6
|
+
* - 该服务只负责 session 生命周期与查询,不负责 plugin / RPC 启停。
|
|
7
|
+
* - Session 对象创建细节集中在这里,避免 facade 和 lifecycle 重复依赖 Session 构造逻辑。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from "fs-extra";
|
|
11
|
+
import { nanoid } from "nanoid";
|
|
12
|
+
import type { Tool } from "ai";
|
|
13
|
+
import type { Logger } from "@/utils/logger/Logger.js";
|
|
14
|
+
import type {
|
|
15
|
+
AgentCreateSessionInput,
|
|
16
|
+
AgentListSessionsInput,
|
|
17
|
+
AgentManagedSession,
|
|
18
|
+
AgentModel,
|
|
19
|
+
AgentSession,
|
|
20
|
+
AgentSessionCollection,
|
|
21
|
+
AgentSessionConstructor,
|
|
22
|
+
AgentSessionSummaryPage,
|
|
23
|
+
AgentSessionSystemBlock,
|
|
24
|
+
} from "@/types/agent/AgentTypes.js";
|
|
25
|
+
import { Session } from "@/session/Session.js";
|
|
26
|
+
import {
|
|
27
|
+
getSdkAgentSessionDirPath,
|
|
28
|
+
listAgentSessionSummaryPage,
|
|
29
|
+
} from "@/session/index.js";
|
|
30
|
+
import type { AgentRuntime } from "@/types/runtime/agent/AgentRuntime.js";
|
|
31
|
+
import type { AgentContext } from "@/types/runtime/agent/AgentContext.js";
|
|
32
|
+
import type { SessionPort } from "@/types/runtime/agent/AgentContext.js";
|
|
33
|
+
import type { BasePlugin } from "@/plugin/core/BasePlugin.js";
|
|
34
|
+
import { isPluginEnabled } from "@/plugin/core/Activation.js";
|
|
35
|
+
import { createInstructionSystemBlocks } from "@/agent/local/AgentInstructions.js";
|
|
36
|
+
|
|
37
|
+
type AgentSessionManagerOptions = {
|
|
38
|
+
/**
|
|
39
|
+
* 当前 agent 稳定标识。
|
|
40
|
+
*/
|
|
41
|
+
agent_id: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 当前项目根目录。
|
|
45
|
+
*/
|
|
46
|
+
project_root: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 当前 agent 默认工具集合。
|
|
50
|
+
*/
|
|
51
|
+
tools: Record<string, Tool>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 当前统一日志器。
|
|
55
|
+
*/
|
|
56
|
+
logger: Logger;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 当前 agent runtime。
|
|
60
|
+
*/
|
|
61
|
+
runtime: AgentRuntime;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 延迟读取当前 agent context。
|
|
65
|
+
*/
|
|
66
|
+
get_agent_context: () => AgentContext;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 当前静态 instruction 文本集合。
|
|
70
|
+
*/
|
|
71
|
+
get_instruction: () => string[];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 当前 plugin 实例集合。
|
|
75
|
+
*/
|
|
76
|
+
plugin_instances: Map<string, BasePlugin>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 当前默认模型实例。
|
|
80
|
+
*/
|
|
81
|
+
default_model?: AgentModel;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 当前 agent 使用的本地 Session 类。
|
|
85
|
+
*/
|
|
86
|
+
SessionClass?: AgentSessionConstructor;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 本地 Agent session 管理服务。
|
|
91
|
+
*/
|
|
92
|
+
export class AgentSessionManager {
|
|
93
|
+
private readonly agent_id: string;
|
|
94
|
+
private readonly project_root: string;
|
|
95
|
+
private readonly tools: Record<string, Tool>;
|
|
96
|
+
private readonly logger: Logger;
|
|
97
|
+
private readonly runtime: AgentRuntime;
|
|
98
|
+
private readonly get_agent_context: AgentSessionManagerOptions["get_agent_context"];
|
|
99
|
+
private readonly get_instruction: AgentSessionManagerOptions["get_instruction"];
|
|
100
|
+
private readonly plugin_instances: Map<string, BasePlugin>;
|
|
101
|
+
private readonly default_model?: AgentModel;
|
|
102
|
+
private readonly SessionClass: AgentSessionConstructor;
|
|
103
|
+
private readonly sessions_by_id = new Map<string, AgentManagedSession>();
|
|
104
|
+
private readonly configured_session_ids = new Set<string>();
|
|
105
|
+
private readonly session_collection: AgentSessionCollection;
|
|
106
|
+
|
|
107
|
+
constructor(options: AgentSessionManagerOptions) {
|
|
108
|
+
this.agent_id = options.agent_id;
|
|
109
|
+
this.project_root = options.project_root;
|
|
110
|
+
this.tools = options.tools;
|
|
111
|
+
this.logger = options.logger;
|
|
112
|
+
this.runtime = options.runtime;
|
|
113
|
+
this.get_agent_context = options.get_agent_context;
|
|
114
|
+
this.get_instruction = options.get_instruction;
|
|
115
|
+
this.plugin_instances = options.plugin_instances;
|
|
116
|
+
this.default_model = options.default_model;
|
|
117
|
+
this.SessionClass = options.SessionClass || Session;
|
|
118
|
+
this.session_collection = {
|
|
119
|
+
createSession: async (input) => await this.create_session(input),
|
|
120
|
+
getSession: async (session_id) => await this.get_session(session_id),
|
|
121
|
+
listSessions: async (input) => await this.list_sessions(input),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 返回当前缓存的 session 实例。
|
|
127
|
+
*/
|
|
128
|
+
list_cached_sessions(): AgentManagedSession[] {
|
|
129
|
+
return [...this.sessions_by_id.values()];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 返回对外暴露的 session collection。
|
|
134
|
+
*/
|
|
135
|
+
get_session_collection(): AgentSessionCollection {
|
|
136
|
+
return this.session_collection;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 获取或创建一个 session runtime port。
|
|
141
|
+
*/
|
|
142
|
+
get_session_port(session_id: string): SessionPort {
|
|
143
|
+
return this.get_or_create_session({
|
|
144
|
+
session_id,
|
|
145
|
+
}).getRuntimePort();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 新建一个 session。
|
|
150
|
+
*/
|
|
151
|
+
async create_session(
|
|
152
|
+
input?: AgentCreateSessionInput,
|
|
153
|
+
): Promise<AgentSession> {
|
|
154
|
+
const explicit_session_id =
|
|
155
|
+
String(input?.sessionId || "").trim() || undefined;
|
|
156
|
+
if (
|
|
157
|
+
explicit_session_id &&
|
|
158
|
+
(this.sessions_by_id.has(explicit_session_id) ||
|
|
159
|
+
(await fs.pathExists(
|
|
160
|
+
getSdkAgentSessionDirPath(
|
|
161
|
+
this.project_root,
|
|
162
|
+
this.agent_id,
|
|
163
|
+
explicit_session_id,
|
|
164
|
+
),
|
|
165
|
+
)))
|
|
166
|
+
) {
|
|
167
|
+
throw new Error(`Session "${explicit_session_id}" already exists`);
|
|
168
|
+
}
|
|
169
|
+
const session = this.get_or_create_session({
|
|
170
|
+
session_id: explicit_session_id,
|
|
171
|
+
});
|
|
172
|
+
await session.initialize();
|
|
173
|
+
await this.apply_session_defaults(session);
|
|
174
|
+
return session;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 获取一个已存在的 session。
|
|
179
|
+
*/
|
|
180
|
+
async get_session(session_id: string): Promise<AgentSession> {
|
|
181
|
+
const resolved_session_id = String(session_id || "").trim();
|
|
182
|
+
if (!resolved_session_id) {
|
|
183
|
+
throw new Error("getSession requires a non-empty sessionId");
|
|
184
|
+
}
|
|
185
|
+
const session_dir_path = getSdkAgentSessionDirPath(
|
|
186
|
+
this.project_root,
|
|
187
|
+
this.agent_id,
|
|
188
|
+
resolved_session_id,
|
|
189
|
+
);
|
|
190
|
+
if (
|
|
191
|
+
!this.sessions_by_id.has(resolved_session_id) &&
|
|
192
|
+
!(await fs.pathExists(session_dir_path))
|
|
193
|
+
) {
|
|
194
|
+
throw new Error(`Session "${resolved_session_id}" not found`);
|
|
195
|
+
}
|
|
196
|
+
const session = this.get_or_create_session({
|
|
197
|
+
session_id: resolved_session_id,
|
|
198
|
+
});
|
|
199
|
+
await session.initialize();
|
|
200
|
+
await this.apply_session_defaults(session);
|
|
201
|
+
return session;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 列出当前 agent 的 session 摘要页。
|
|
206
|
+
*/
|
|
207
|
+
async list_sessions(
|
|
208
|
+
input?: AgentListSessionsInput,
|
|
209
|
+
): Promise<AgentSessionSummaryPage> {
|
|
210
|
+
return await listAgentSessionSummaryPage({
|
|
211
|
+
projectRoot: this.project_root,
|
|
212
|
+
agentId: this.agent_id,
|
|
213
|
+
input,
|
|
214
|
+
executingSessionIds: new Set(this.runtime.listExecutingSessionIds()),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private get_or_create_session(input?: {
|
|
219
|
+
/**
|
|
220
|
+
* 可选指定 session id。
|
|
221
|
+
*/
|
|
222
|
+
session_id?: string;
|
|
223
|
+
|
|
224
|
+
}): AgentManagedSession {
|
|
225
|
+
const resolved_session_id =
|
|
226
|
+
String(input?.session_id || "").trim() ||
|
|
227
|
+
`session-${Date.now()}-${nanoid(8)}`;
|
|
228
|
+
const cached = this.sessions_by_id.get(resolved_session_id);
|
|
229
|
+
if (cached) return cached;
|
|
230
|
+
|
|
231
|
+
const created = new this.SessionClass({
|
|
232
|
+
agentId: this.agent_id,
|
|
233
|
+
projectRoot: this.project_root,
|
|
234
|
+
sessionId: resolved_session_id,
|
|
235
|
+
tools: this.tools,
|
|
236
|
+
logger: this.logger,
|
|
237
|
+
getInstructionSystemBlocks: () => this.load_instruction_system_blocks(),
|
|
238
|
+
getManagedPluginSystemBlocks: async () => [],
|
|
239
|
+
getPluginSystemBlocks: async () => await this.load_plugin_system_blocks(),
|
|
240
|
+
ensureConfigured: async (session) => {
|
|
241
|
+
await this.apply_session_defaults(session);
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
this.sessions_by_id.set(resolved_session_id, created);
|
|
245
|
+
return created;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private async apply_session_defaults(
|
|
249
|
+
session: AgentManagedSession,
|
|
250
|
+
): Promise<void> {
|
|
251
|
+
if (this.configured_session_ids.has(session.id)) return;
|
|
252
|
+
if (this.default_model) {
|
|
253
|
+
await session.set({
|
|
254
|
+
model: this.default_model,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
this.configured_session_ids.add(session.id);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private load_instruction_system_blocks(): AgentSessionSystemBlock[] {
|
|
261
|
+
return createInstructionSystemBlocks(
|
|
262
|
+
this.get_instruction(),
|
|
263
|
+
this.project_root,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private async load_plugin_system_blocks(): Promise<AgentSessionSystemBlock[]> {
|
|
268
|
+
const context = this.get_agent_context();
|
|
269
|
+
const out: AgentSessionSystemBlock[] = [];
|
|
270
|
+
for (const plugin of this.plugin_instances.values()) {
|
|
271
|
+
if (typeof plugin.system !== "function") continue;
|
|
272
|
+
try {
|
|
273
|
+
if (!isPluginEnabled({ plugin, context })) continue;
|
|
274
|
+
if (typeof plugin.availability === "function") {
|
|
275
|
+
const availability = await plugin.availability(context);
|
|
276
|
+
if (!availability.available) continue;
|
|
277
|
+
}
|
|
278
|
+
const text = String(await plugin.system(context)).trim();
|
|
279
|
+
if (!text) continue;
|
|
280
|
+
out.push({
|
|
281
|
+
source: "plugin",
|
|
282
|
+
name: plugin.name,
|
|
283
|
+
content: text,
|
|
284
|
+
});
|
|
285
|
+
} catch {
|
|
286
|
+
// 单个 plugin system 失败不应阻断 SDK session 主链路。
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
}
|