@@ -71,6 +71,7 @@ import { type Tool, ToolErrorType, type ToolResult } from '../tools/types/index.
7171import { getEnvironmentContext } from '../utils/environment.js' ;
7272import { isThinkingModel } from '../utils/modelDetection.js' ;
7373import { ExecutionEngine } from './ExecutionEngine.js' ;
74+ import { SessionRuntime } from './runtime/SessionRuntime.js' ;
7475import { subagentRegistry } from './subagents/SubagentRegistry.js' ;
7576import type {
7677 AgentOptions ,
@@ -115,15 +116,18 @@ export class Agent {
115116 // 当前模型的上下文窗口大小(用于 tokenUsage 上报)
116117 private currentModelMaxContextTokens ! : number ;
117118 private currentModelId ?: string ;
119+ private sessionRuntime ?: SessionRuntime ;
118120
119121 constructor (
120122 config : BladeConfig ,
121123 runtimeOptions : AgentOptions = { } ,
122- executionPipeline ?: ExecutionPipeline
124+ executionPipeline ?: ExecutionPipeline ,
125+ sessionRuntime ?: SessionRuntime
123126 ) {
124127 this . config = config ;
125128 this . runtimeOptions = runtimeOptions ;
126129 this . executionPipeline = executionPipeline || this . createDefaultPipeline ( ) ;
130+ this . sessionRuntime = sessionRuntime ;
127131 // sessionId 不再存储在 Agent 内部,改为从 context 传入
128132 }
129133
@@ -190,6 +194,11 @@ export class Agent {
190194 }
191195
192196 private async switchModelIfNeeded ( modelId : string ) : Promise < void > {
197+ if ( this . sessionRuntime ) {
198+ await this . sessionRuntime . refresh ( { modelId } ) ;
199+ this . syncRuntimeState ( ) ;
200+ return ;
201+ }
193202 if ( ! modelId || modelId === this . currentModelId ) return ;
194203 const modelConfig = getModelById ( modelId ) ;
195204 if ( ! modelConfig ) {
@@ -204,6 +213,12 @@ export class Agent {
204213 * 使用 Store 获取配置
205214 */
206215 static async create ( options : AgentOptions = { } ) : Promise < Agent > {
216+ if ( options . sessionId ) {
217+ throw new Error (
218+ 'Agent.create() does not accept sessionId. Create a SessionRuntime explicitly and use Agent.createWithRuntime().'
219+ ) ;
220+ }
221+
207222 // 0. 确保 store 已初始化(防御性检查)
208223 await ensureStoreInitialized ( ) ;
209224
@@ -242,6 +257,20 @@ export class Agent {
242257 return agent ;
243258 }
244259
260+ static async createWithRuntime (
261+ runtime : SessionRuntime ,
262+ options : AgentOptions = { }
263+ ) : Promise < Agent > {
264+ const agent = new Agent (
265+ runtime . getConfig ( ) ,
266+ options ,
267+ runtime . createExecutionPipeline ( options ) ,
268+ runtime
269+ ) ;
270+ await agent . initialize ( ) ;
271+ return agent ;
272+ }
273+
245274 /**
246275 * 初始化Agent
247276 */
@@ -253,6 +282,17 @@ export class Agent {
253282 try {
254283 this . log ( '初始化Agent...' ) ;
255284
285+ if ( this . sessionRuntime ) {
286+ await this . initializeSystemPrompt ( ) ;
287+ await this . sessionRuntime . refresh ( this . runtimeOptions ) ;
288+ this . syncRuntimeState ( ) ;
289+ this . isInitialized = true ;
290+ this . log (
291+ `Agent初始化完成,已加载 ${ this . executionPipeline . getRegistry ( ) . getAll ( ) . length } 个工具`
292+ ) ;
293+ return ;
294+ }
295+
256296 // 1. 初始化系统提示
257297 await this . initializeSystemPrompt ( ) ;
258298
@@ -1903,6 +1943,19 @@ IMPORTANT: Execute according to the approved plan above. Follow the steps exactl
19031943 }
19041944 }
19051945
1946+ private syncRuntimeState ( ) : void {
1947+ if ( ! this . sessionRuntime ) {
1948+ return ;
1949+ }
1950+
1951+ this . chatService = this . sessionRuntime . getChatService ( ) ;
1952+ this . executionEngine = this . sessionRuntime . getExecutionEngine ( ) ;
1953+ this . attachmentCollector = this . sessionRuntime . getAttachmentCollector ( ) ;
1954+ this . currentModelId = this . sessionRuntime . getCurrentModelId ( ) ;
1955+ this . currentModelMaxContextTokens =
1956+ this . sessionRuntime . getCurrentModelMaxContextTokens ( ) ;
1957+ }
1958+
19061959 /**
19071960 * 生成任务ID
19081961 */
0 commit comments