|
1 | 1 | import { |
2 | 2 | AgentServiceBase, SingleAgentService, GenerativeAgentService, |
3 | | - ACPAgentService, T_ACPCommand, T_ACPArgs, T_ACPEnv, T_ACPSessionMode, T_ACPWorkPath, |
| 3 | + TransientACPAgentService, PersistentACPAgentService, T_ACPCommand, T_ACPArgs, T_ACPEnv, T_ACPWorkPath, |
4 | 4 | IModelService, |
5 | 5 | IAgentSaverService, AgentMemorySaver, ILoggerService, |
6 | 6 | ConversationCompactor, IConversationCompactor, T_SummaryModelService, T_CompactPromptTemplate, |
@@ -274,37 +274,34 @@ export class AgentFactory { |
274 | 274 | ): Promise<AgentServiceBase> { |
275 | 275 | const workPath = options.workPath ?? process.cwd(); |
276 | 276 | const sessionMode = entry.sessionMode ?? ACPSessionMode.Persistent; |
| 277 | + const acpArgs = { |
| 278 | + [T_ACPCommand]: entry.command, |
| 279 | + [T_ACPArgs]: entry.args ?? [], |
| 280 | + [T_ACPWorkPath]: workPath, |
| 281 | + ...(entry.env && { [T_ACPEnv]: entry.env }), |
| 282 | + }; |
277 | 283 |
|
278 | 284 | if (sessionMode === ACPSessionMode.Persistent) { |
279 | 285 | const pool = ACPAgentPool.getInstance(); |
280 | 286 | const key = `${options.agentId}:${options.dbSessionId}`; |
281 | 287 | const agentName = entry.name ?? options.agentId; |
282 | 288 | const configHash = JSON.stringify({ command: entry.command, args: entry.args ?? [], env: entry.env ?? {}, workPath }); |
283 | 289 |
|
284 | | - return pool.acquire(key, options.agentId, agentName, options.dbSessionId, configHash, async () => { |
285 | | - const sub = new ServiceContainer(); |
286 | | - if (container.isRegistered(ILoggerService)) |
287 | | - sub.registerInstance(ILoggerService, await container.resolve(ILoggerService)); |
288 | | - if (container.isRegistered(IAgentSaverService)) |
289 | | - sub.registerInstance(IAgentSaverService, await container.resolve(IAgentSaverService)); |
290 | | - sub.registerWithArgs(ACPAgentService, { |
291 | | - [T_ACPCommand]: entry.command, |
292 | | - [T_ACPArgs]: entry.args ?? [], |
293 | | - [T_ACPWorkPath]: workPath, |
294 | | - ...(entry.env && { [T_ACPEnv]: entry.env }), |
295 | | - [T_ACPSessionMode]: ACPSessionMode.Persistent, |
296 | | - }); |
297 | | - return sub.resolve<ACPAgentService>(ACPAgentService); |
298 | | - }); |
| 290 | + const cached = await pool.tryGet(key, configHash); |
| 291 | + if (cached) return cached; |
| 292 | + |
| 293 | + const sub = new ServiceContainer(); |
| 294 | + if (container.isRegistered(ILoggerService)) |
| 295 | + sub.registerInstance(ILoggerService, await container.resolve(ILoggerService)); |
| 296 | + if (container.isRegistered(IAgentSaverService)) |
| 297 | + sub.registerInstance(IAgentSaverService, await container.resolve(IAgentSaverService)); |
| 298 | + sub.registerWithArgs(PersistentACPAgentService, acpArgs); |
| 299 | + const instance = await sub.resolve<PersistentACPAgentService>(PersistentACPAgentService); |
| 300 | + pool.put(key, instance, { agentId: options.agentId, agentName, dbSessionId: options.dbSessionId, configHash }); |
| 301 | + return instance; |
299 | 302 | } |
300 | 303 |
|
301 | | - container.registerWithArgs(ACPAgentService, { |
302 | | - [T_ACPCommand]: entry.command, |
303 | | - [T_ACPArgs]: entry.args ?? [], |
304 | | - [T_ACPWorkPath]: workPath, |
305 | | - ...(entry.env && { [T_ACPEnv]: entry.env }), |
306 | | - [T_ACPSessionMode]: ACPSessionMode.Transient, |
307 | | - }); |
308 | | - return container.resolve(ACPAgentService); |
| 304 | + container.registerWithArgs(TransientACPAgentService, acpArgs); |
| 305 | + return container.resolve(TransientACPAgentService); |
309 | 306 | } |
310 | 307 | } |
0 commit comments