aws-runtime-bridge 1.9.168 → 1.9.170
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/CHANGELOG.md +53 -0
- package/dist/adapter/AcodeSdkAdapter.d.ts +46 -0
- package/dist/adapter/AcodeSdkAdapter.js +4 -4
- package/dist/adapter/ClaudeSdkAdapter.js +2 -2
- package/dist/adapter/types.d.ts +7 -0
- package/dist/config.d.ts +6 -0
- package/dist/config.js +2 -2
- package/dist/index.js +1 -1
- package/dist/routes/agent-state.d.ts +2 -0
- package/dist/routes/agent-state.js +2 -0
- package/dist/routes/capabilities.js +1 -1
- package/dist/routes/debug-mount.d.ts +2 -0
- package/dist/routes/debug-mount.js +2 -0
- package/dist/routes/mcp.js +1 -1
- package/dist/routes/plugins.js +1 -1
- package/dist/routes/skills.js +1 -1
- package/dist/routes/terminal.js +28 -32
- package/dist/services/acode-bg-manager.d.ts +2 -0
- package/dist/services/agent-capability-sync.js +1 -1
- package/dist/services/debug-mount.d.ts +122 -0
- package/dist/services/debug-mount.js +2 -0
- package/dist/services/desktop-mcp-manager.js +1 -1
- package/dist/services/instance-ai-config-service.js +1 -1
- package/dist/services/instance-features.d.ts +4 -0
- package/dist/services/instance-features.js +1 -1
- package/dist/services/instance-init-service.js +1 -1
- package/dist/services/mcp-deploy.d.ts +31 -0
- package/dist/services/mcp-deploy.js +4 -3
- package/dist/services/mcp-env-injector.d.ts +24 -0
- package/dist/services/mcp-env-injector.js +1 -1
- package/dist/services/mcp-package.d.ts +35 -0
- package/dist/services/mcp-package.js +2 -0
- package/dist/services/mcp-probe-client.d.ts +25 -0
- package/dist/services/mcp-probe-client.js +2 -0
- package/dist/services/panel-env.d.ts +5 -0
- package/dist/services/panel-env.js +1 -1
- package/dist/services/plugin-deployment.d.ts +36 -0
- package/dist/services/plugin-deployment.js +3 -0
- package/dist/services/plugin-event-buffer.js +2 -1
- package/dist/services/plugin-fs.d.ts +5 -0
- package/dist/services/plugin-fs.js +2 -0
- package/dist/services/plugin-handler-adapter.d.ts +7 -0
- package/dist/services/plugin-handler-adapter.js +3 -0
- package/dist/services/plugin-listener-runner.d.ts +18 -0
- package/dist/services/plugin-listener-runner.js +8 -0
- package/dist/services/plugin-loader.d.ts +32 -0
- package/dist/services/plugin-loader.js +2 -0
- package/dist/services/plugin-manager.d.ts +17 -136
- package/dist/services/plugin-manager.js +4 -15
- package/dist/services/plugin-manifest.d.ts +83 -0
- package/dist/services/plugin-manifest.js +2 -0
- package/dist/services/plugin-runtime.d.ts +67 -0
- package/dist/services/plugin-runtime.js +1 -0
- package/dist/services/skill-manager.d.ts +15 -0
- package/dist/services/skill-manager.js +3 -2
- package/dist/types.d.ts +6 -0
- package/dist/utils/mcp-utils.js +1 -1
- package/package/acode/dist/compaction.d.ts +35 -1
- package/package/acode/dist/compaction.js +6 -6
- package/package/acode/dist/plugin-types.d.ts +6 -27
- package/package/acode/dist/runtime.d.ts +37 -0
- package/package/acode/dist/runtime.js +31 -30
- package/package/acode/dist/tool-group-manager.d.ts +10 -0
- package/package/acode/dist/tool-group-manager.js +5 -10
- package/package/acode/dist/types.d.ts +2 -0
- package/package/aws-client-agent-mcp/dist/ability-market-tools.d.ts +18 -0
- package/package/aws-client-agent-mcp/dist/ability-market-tools.js +2 -0
- package/package/aws-client-agent-mcp/dist/agent-client.d.ts +15 -4
- package/package/aws-client-agent-mcp/dist/agent-client.js +1 -1
- package/package/aws-client-agent-mcp/dist/http-client.d.ts +2 -0
- package/package/aws-client-agent-mcp/dist/http-client.js +1 -1
- package/package/aws-client-agent-mcp/dist/mcp-server.d.ts +0 -7
- package/package/aws-client-agent-mcp/dist/mcp-server.js +12 -12
- package/package/aws-client-agent-mcp/dist/mcp-tools.d.ts +3 -0
- package/package/aws-client-agent-mcp/dist/mcp-tools.js +20 -20
- package/package/aws-client-agent-mcp/dist/memory-store.d.ts +1 -1
- package/package/aws-client-agent-mcp/dist/memory-store.js +2 -2
- package/package/aws-client-agent-mcp/dist/memory-tools.js +7 -7
- package/package/aws-client-agent-mcp/dist/tool-groups.js +2 -2
- package/package/aws-client-agent-mcp/dist/types.d.ts +2 -21
- package/package.json +3 -2
- package/dist/services/builtin-commands/create-plugin.md +0 -44
|
@@ -1,42 +1,43 @@
|
|
|
1
|
-
import{EventEmitter as
|
|
2
|
-
`;
|
|
3
|
-
`)}return JSON.stringify(n)}function
|
|
4
|
-
${h}`,e.orphanToolMessages+=1},o=c=>{const m=new Map;for(const y of n)if(y.role==="assistant"&&Array.isArray(y.tool_calls))for(const
|
|
5
|
-
${g}`,e.orphanToolMessages+=1}a
|
|
6
|
-
`)}function
|
|
7
|
-
`)}function mt(n){return n.map(e=>({type:"function",function:{name:e.exposedName,description:e.description||e.toolName,parameters:e.inputSchema}}))}function ms(n,e){return`${n}__${e}`.replace(/[^a-zA-Z0-9_-]/g,"_").slice(0,64)||"mcp_tool"}function ds(n){const e={"Content-Type":"application/json"};return n.apiKey&&(e.Authorization=`Bearer ${n.apiKey}`),e}function ps(n){return Object.fromEntries(Object.entries(n).map(([e,t])=>[e,hs(e)?fs(t):t]))}function hs(n){const e=n.trim().toLowerCase();return e==="authorization"||e==="api-key"||e==="x-api-key"||e==="openai-api-key"}function fs(n){return n.trim().toLowerCase().startsWith("bearer ")?"Bearer ***":"***"}function gs(n){return n.split(/\r?\n/).filter(e=>e.startsWith("data:")).map(e=>e.slice(5).trim()).filter(Boolean)}function dt(n){if(!S(n))return null;const e=n.error;if(S(e)){const t=typeof e.message=="string"?e.message:"",o=e.code??e.status??e.type,s=o==null?"":String(o);return t||s?[s,t].filter(Boolean).join(" "):null}if(typeof e=="string"&&e)return e;if(n.type==="error"||n.object==="error"){const t=typeof n.message=="string"?n.message:"",o=n.code??n.status,s=o==null?"":String(o);if(t||s)return[s,t].filter(Boolean).join(" ")}return null}const Ts=/role 'tool'|tool calls and tool results|tool_call_sequence|\b11148\b/i;let pt=0,me="";function xe(n,e,t){if(!Ts.test(t))return null;try{const o=new Map,s=new Map;n.forEach((d,f)=>{if(d.role==="assistant"&&Array.isArray(d.tool_calls))for(const g of d.tool_calls){const T=typeof g?.id=="string"?g.id.trim():"";T&&!o.has(T)&&o.set(T,f)}else if(d.role==="tool"){const g=typeof d.tool_call_id=="string"?d.tool_call_id.trim():"";g&&!s.has(g)&&s.set(g,f)}});const r=[];n.forEach((d,f)=>{if(d.role==="tool"){const g=typeof d.tool_call_id=="string"?d.tool_call_id.trim():"",T=g?o.get(g):void 0;(g===""||T===void 0||T>f)&&r.push(f)}else if(d.role==="assistant"&&Array.isArray(d.tool_calls))for(const g of d.tool_calls){const T=typeof g?.id=="string"?g.id.trim():"";if(T===""||!s.has(T)){r.push(f);break}}});const i=d=>` [\u5DE5\u5177\u5E8F\u5217\u8BC1\u636E] \u53EF\u7591\u6D88\u606F ${r.length}/${n.length} \u6761\uFF08${d?`\u5DF2\u5199\u5165 ${d}`:"\u843D\u76D8\u5931\u8D25"}\uFF09`,a=Date.now();if(a-pt<1e4&&me)return i(me);pt=a;const l=d=>({role:d.role,tool_call_id:d.tool_call_id,tool_calls:Array.isArray(d.tool_calls)?d.tool_calls.map(f=>f?.id??null):void 0,contentPreview:typeof d.content=="string"?d.content.slice(0,300):d.content===null?null:JSON.stringify(d.content).slice(0,300)}),u=`acode-tool-seq-${new Date(a).toISOString().replace(/[:.]/g,"-")}.json`,c=String(process.env.AWS_TEST_HOME||"").trim()||String(process.env.AWS_RUNTIME_HOME_DIR||"").trim(),m=String(process.env.AWS_RUNTIME_ENV||"").trim().toLowerCase()==="development",h=c||(m?z.resolve(process.cwd(),"..",".dev-home"):z.dirname(Pt())),y=z.join(h,".aws-bridge","log"),C={timestamp:new Date(a).toISOString(),status:e,errorBody:t.slice(0,4e3),suspectIndexes:r,messages:n.map((d,f)=>({index:f,role:d.role,tool_call_id:d.tool_call_id,tool_calls:Array.isArray(d.tool_calls)?d.tool_calls.map(g=>g?.id??null):void 0})),suspectWindows:r.map(d=>{const f=Math.max(0,d-6),g=Math.min(n.length,d+7);return{center:d,range:[f,g],messages:n.slice(f,g).map((T,A)=>({index:f+A,...l(T)}))}})};return me=`.aws-bridge/log/${u}`,B.promises.mkdir(y,{recursive:!0}).then(()=>B.promises.writeFile(z.join(y,u),JSON.stringify(C,null,2),"utf-8")).then(()=>B.promises.readdir(y)).then(d=>{const f=d.filter(T=>T.startsWith("acode-tool-seq-")).sort(),g=f.length-20;return g<=0?null:Promise.all(f.slice(0,g).map(T=>B.promises.unlink(z.join(y,T)).catch(()=>{})))}).catch(()=>{me=""}),i(me)}catch{return null}}function ht(n){const e=new Set,t=Date.now();return n.map((o,s)=>{const r=typeof o?.id=="string"?o.id.trim():"";if(r&&!e.has(r))return e.add(r),o;let i=`call-${t}-${s}`,a=1;for(;e.has(i);)i=`call-${t}-${s}-${a}`,a+=1;return e.add(i),{...o,id:i}})}function ys(n,e){const t=Number.isFinite(e.index)?Number(e.index):0,o=n.get(t)??{id:e.id||`call-${t}`,type:"function",function:{name:"",arguments:""}};e.id&&(o.id=e.id),e.function?.name&&(o.function.name+=e.function.name),e.function?.arguments&&(o.function.arguments+=e.function.arguments),n.set(t,o)}function Cs(n){return ht([...n.entries()].sort(([e],[t])=>e-t).map(([,e])=>e).filter(e=>e.function.name.trim().length>0))}function _s(){const n=process.env.ACODE_ABORT_FORCE_SETTLE_GRACE_MS;if(n===void 0||n.trim()==="")return 6e4;const e=Number(n.trim());return Number.isFinite(e)&&e>=0?Math.floor(e):6e4}class X extends ft{hasActiveBlockingToolCall(){return this.activeBlockingToolCalls>0}static isLongBlockingMcpToolName(e){return Xt(e)}constructor(e){super(),this.config=e,this.initialConversation=[],this.state={started:!1,terminated:!1,configMcpCount:0,skills:[],provider:{model:"acode-default",stream:!1,requestTimeoutMs:Ge,requestTotalTimeoutMs:Je,requestRetryCount:Ye,requestFirstByteTimeoutMs:He,requestStreamIdleTimeoutMs:Ke,mcpToolTimeoutMs:fe,mcpPollingToolTimeoutMs:Qe,supportsImageInput:!1},permissions:{mode:"default",allow:[],deny:[]},mcpConnections:[],ownsMcpConnections:!0,exposedTools:[],commands:[],commandRegistry:new Pe([]),conversation:[],accumulatedInputTokens:0,accumulatedOutputTokens:0,accumulatedCacheHitTokens:0,accumulatedCacheMissTokens:0,toolPermissionPolicy:{...Ne}},this.abortController=null,this.exposedToolsInitialized=!1,this.recoveredToolNames=new Set,this.readFileStateTracker=new kt,this.pendingMemorySubAgents=new Map,this.memoryDelegateCooldownUntil=new Map,this.toolGroupManager=null,this.submitQueue=Promise.resolve(),this.activeTurnToken=null,this.forceSettleWaiters=new Map,this.abortForceSettleTimer=null,this.activeTurn=!1,this.activeBlockingToolCalls=0,this.forceMessagePending=!1,this.turnPromptPushed=!1,this.abortRequested=!1,this.toolSettleSlots=new Map,this.toolSettleSeq=0,this.toolResourceLock=oo(),this.toolConcurrencyScheduler=new so,this.toolReadOnlyLimiter=null,this.toolBashLimiter=null,this.toolConcurrencyEnabled=!0,this.pendingAwaitedSubIds=new Set,this.pendingAwaitedBgTaskIds=new Set,this.sessionCompleteEmitted=!1,this.contextVersion=0,this.stopWaiters=new Set,this.compactionDebugTimer=null,this.skillTool=null,this.skillWatchers=[],this.skillRefreshTimer=null,this.questionWaiters=new Map,this.permissionWaiters=new Map,this.hookBus=new Vt({sessionId:e.sessionId,onError:(r,i)=>{this.emitAcodeEvent("error",{message:`ACode hook failed: ${r.name}: ${i instanceof Error?i.message:String(i)}`})}});for(const r of e.hooks??[])this.hookBus.register(r);e.toolPermissionPolicy&&(this.state.toolPermissionPolicy={...Ne,...e.toolPermissionPolicy});const t=e.toolConcurrency;this.toolConcurrencyEnabled=t?.enabled!==!1;const o=t?.maxReadOnly??8;this.toolReadOnlyLimiter=new ze(o);const s=t?.maxBash??0;this.toolBashLimiter=s>0?new ze(s):null}getAvailableTools(){return this.state.exposedTools.map(e=>e.exposedName)}emitAcodeEvent(e,t){const o={type:e,sessionId:this.config.sessionId,timestamp:uo(),data:t};this.emit("event",o)}emitCompactionPhase(e,t,o,s,r){this.emitAcodeEvent("context_compaction",{action:"phase",compactionId:e,parentId:e,phase:t,phaseLabel:o,phaseDetail:s,...r?{trigger:r}:{}})}handleCompactionPhase(e,t){switch(t.phase){case"prune_start":this.emitCompactionPhase(e,"prune","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA",`\u6B63\u5728\u88C1\u526A ${t.messageCount??0} \u6761\u6D88\u606F\u4E2D\u7684\u5DE5\u5177\u8F93\u51FA...`);break;case"prune_end":this.emitCompactionPhase(e,"prune_done","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA",`\u88C1\u526A\u4E86 ${t.prunedCount??0} \u6761\u5DE5\u5177\u8F93\u51FA, \u91CA\u653E ${t.freedTokens??0} tokens`);break;case"summarize_start":this.emitCompactionPhase(e,"summarize","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD",`\u6B63\u5728\u6458\u8981 ${t.messageCount??0} \u6761\u65E9\u671F\u5BF9\u8BDD...`);break;case"summarize_end":this.emitCompactionPhase(e,"summarize_done","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD",`\u6458\u8981\u5316\u4E86 ${t.summarizedCount??0} \u6761\u6D88\u606F${t.usedLLM?" (LLM \u6458\u8981)":" (\u89C4\u5219\u6458\u8981)"}`);break}}async start(){this.emitAcodeEvent("startup_phase",{phase:"loading_config"});const e=await Yt(this.config.configPath);this.emitAcodeEvent("startup_phase",{phase:"loading_skills"});const{skills:t,skillGroups:o,watchedDirs:s}=await this.loadAllSkills(),r=Io(this.config,e);p(`[ACodeRuntime] \u2605\u2605\u2605 DEBUG \u2605\u2605\u2605 resolveProviderConfig \u7ED3\u679C: config.model=${this.config.model??"(undefined)"}, resolvedModel=${r.model}, baseURL=${r.baseURL??"(none)"}, apiKey=${r.apiKey?"[set]":"(none)"}, configPath=${this.config.configPath??"(none)"}`);const i=Go(this.config,e),a=this.config.sharedMcpConnections?this.filterSharedMcpConnectionsForSubAgent(this.config.sharedMcpConnections):await this.connectMcpServers(e.mcpServers??{},r.mcpToolTimeoutMs),l=!this.config.sharedMcpConnections;this.emitAcodeEvent("startup_phase",{phase:"fetching_profile"});const u=await this.fetchProfilePrompt(a),c=this.config.agentId||"unknown";It(c),ie.reconcileOrphans(c);const m=bt(this.config.workingDirectory,c,this.config.onFileChange,{manager:ie,emitCompleted:_=>{this.emitAcodeEvent("background_command_completed",_)}},this.createToolPermissionController(),{tracker:this.readFileStateTracker,enabled:this.config.enableStaleFileGuard!==!1}),h=Mt(this.buildAskUserQuestionDeps(),()=>this.contextVersion),y=$t(t);this.skillTool=y;const C=vt(c),d=Et(c),f=this.config.subAgentManager?xt(this.buildSubAgentToolDeps(this.config.subAgentManager)):[],g=Rt(this.buildAwaitCompleteDeps()),T=this.config.terminalManager?Ot(c,{terminalManager:this.config.terminalManager}):null,A=new Dt({stateOf:this.config.capabilityState??(()=>"disabled")});A.register({key:"pencil",label:"\u8BBE\u8BA1\u753B\u5E03(pencil)",summary:"UI design editor",collapsible:!0}),A.register({key:"draw",label:"\u7ED8\u56FE",summary:"\u7ED8\u5236 draw.io \u6D41\u7A0B\u56FE/\u6CF3\u9053\u56FE",collapsible:!0}),A.register({key:"office",label:"Office \u6587\u4EF6",summary:"\u53EF\u8FDB\u884C\u67E5\u770B/\u521B\u5EFA\u548C\u590D\u6742\u7F16\u8F91 Word/PPT/Excel",collapsible:!0}),A.register({key:"launch",label:"\u542F\u52A8\u9879\u7BA1\u7406",summary:"\u7BA1\u7406\u9879\u76EE\u4E2D\u4E1A\u52A1\u7A0B\u5E8F\u7684\u8D77\u505C\uFF0C\u82E5\u540E\u9762\u9700\u8981\u542F\u52A8\u9879\u76EE\u5E94\u4F7F\u7528\u6B64\u5DE5\u5177",collapsible:!0}),A.register({key:"hire",label:"\u62DB\u8058\u5DE5\u5177",summary:"\u62DB\u8058\u65B0\u7684\u540C\u4E8B Agent",collapsible:!0}),A.register({key:"browser",label:"\u6D4F\u89C8\u5668\u64CD\u4F5C",summary:"\u542F\u52A8\u6D4F\u89C8\u5668\u4F1A\u8BDD\u3001\u5BFC\u822A\u3001\u622A\u56FE\u4E0E\u9875\u9762\u4EA4\u4E92",collapsible:!0}),A.register({key:"desktop",label:"\u684C\u9762\u64CD\u4F5C",summary:"\u622A\u5C4F\u3001\u9F20\u6807\u952E\u76D8\u63A7\u5236\u4E0E UI \u81EA\u52A8\u5316",collapsible:!1}),A.register({key:"task",label:"\u6D41\u7A0B\u4EFB\u52A1",summary:"\u5904\u7406\u6D41\u7A0B\u7EBF\u4EFB\u52A1\uFF1A\u67E5\u770B\u5F53\u524D\u4EFB\u52A1\u3001\u63D0\u4EA4\u6267\u884C\u7ED3\u679C\u3001\u56DE\u9000\u4EFB\u52A1\u3001\u7ED9\u6D41\u7A0B\u5B9E\u4F8B\u7559\u8A00",collapsible:!0,toolNames:["my_task","submit_task_result","start_task_rollback","finish_task_rollback","create_task_instance_comment"]}),A.register({key:"meeting",label:"\u4F1A\u8BAE",summary:"\u521B\u5EFA/\u52A0\u5165\u4F1A\u8BAE\u3001\u53D1\u8A00\u3001\u67E5\u770B\u4F1A\u8BAE\u4FE1\u606F\u3001\u79BB\u5F00/\u7ED3\u675F\u4F1A\u8BAE",collapsible:!0,toolNames:["create_meeting","leave_meeting","end_meeting","meeting_speak","get_meeting_info","my_meetings"]}),A.register({key:"debug",label:"\u8C03\u8BD5\u65E5\u5FD7",summary:"\u6309\u6761\u4EF6\u5206\u9875\u67E5\u8BE2 Debug \u65E5\u5FD7\u4E0E\u83B7\u53D6\u65E5\u5FD7\u4E0A\u62A5\u914D\u7F6E",collapsible:!0,toolNames:["get_debug_log_config","query_debug_logs"]}),A.register({key:"fileHistory",label:"\u6587\u4EF6\u53D8\u66F4\u5386\u53F2",summary:"\u6309\u89D2\u8272/\u5B9E\u4F8B/\u6587\u4EF6\u8DEF\u5F84\u8FC7\u6EE4\u67E5\u8BE2\u6587\u4EF6\u53D8\u66F4\u5386\u53F2",collapsible:!0,toolNames:["query_file_changes_history"]}),A.register({key:"email",label:"\u90AE\u7BB1",summary:"\u6536\u53D1\u89D2\u8272\u5B9E\u4F8B\u57DF\u540D\u90AE\u7BB1\uFF08xxxxx@\u57DF\u540D\uFF09\uFF1A\u53D1\u4FE1\u3001\u5217\u90AE\u4EF6\u3001\u8BFB\u6B63\u6587\u4E0E\u9644\u4EF6\u3001\u56DE\u4FE1\u4FDD\u6301\u7EBF\u7A0B",collapsible:!0,toolNames:["send_email","list_emails","read_email","reply_email"]}),this.toolGroupManager=A;for(const _ of a){const P=_.spec??e.mcpServers?.[_.serverName];P&&this.registerMcpCollapsedGroup(_.serverName,P,_)}for(const _ of a)this.registerToolGroupsFromMcp(_);const w=Lt({toolGroupManager:A,getAllTools:()=>this.state.exposedTools,capabilityToolRelay:this.config.capabilityToolRelay}),v=Bt({toolGroupManager:A,pluginBridge:this.config.pluginBridge,capabilityToolRelay:this.config.capabilityToolRelay,findTool:_=>{const P=this.state.exposedTools.find(Re=>Re.exposedName===_||Re.toolName===_);if(!P)return;const ne=P.group??A.getGroupForExposedName(P.exposedName);return{kind:P.kind,group:ne,toolName:P.toolName}},dispatchTool:(_,P,ne)=>this.executeSingleToolByName(_,P,ne)}),k=[...m,h,y,C,d,...f,g,...T?[T]:[],w,v,...a.flatMap(_=>_.tools),...Fe(this.config.extraBuiltInTools??[],this.config.agentId??"")];if(this.config.subAgentAllowedTools&&this.config.subAgentAllowedTools.length>0){const _=Fe(this.config.subAgentSuiteTools??[],this.config.agentId??""),P=de(k,_);k.length=0,k.push(...P)}let M=k;const I=this.config.subAgentAllowedTools;I&&I.length>0&&(M=M.filter(_=>qe(_,I)));const x=this.resolveCommandDirs(e);let D=[...x,...this.discoverPluginCommandDirs()];if(this.config.pluginBridge?.getCommandDirs)try{const _=await this.config.pluginBridge.getCommandDirs();_.length>0&&(D=[...D,..._])}catch(_){p(`[ACodeRuntime] \u63D2\u4EF6\u547D\u4EE4\u76EE\u5F55\u83B7\u53D6\u5931\u8D25\uFF08\u5FFD\u7565\uFF09: ${_ instanceof Error?_.message:String(_)}`)}D=[...new Set(D)],this.emitAcodeEvent("startup_phase",{phase:"loading_commands"});const q=await qt(D),se=new Pe(q),Z=ls(t),b=cs(q),E=A.buildCapabilityPrompt(),N=this.config.pluginBridge?["","## \u63D2\u4EF6\u7CFB\u7EDF","\u63D2\u4EF6\u5DE5\u5177\u4E0D\u5728\u4F60\u7684\u9759\u6001\u5DE5\u5177\u5217\u8868\u4E2D\uFF1A\u7D27\u51D1\u76EE\u5F55\u968F poll_message \u7ED3\u679C\u7684 pluginCatalog \u5B57\u6BB5\u8FD4\u56DE\uFF1B",'\u7528 call_tool \u8C03\u7528\uFF08action="catalog" \u53D6\u76EE\u5F55\uFF0Caction="schema" \u53D6\u5B9A\u4E49\uFF0C','\u6216 action="call"\uFF08\u9ED8\u8BA4\uFF09+ name="\u63D2\u4EF6\u540D.\u5DE5\u5177\u540D" + arguments={...} \u8C03\u7528\uFF09\u3002',"\u63D2\u4EF6\u4E8B\u4EF6\u5C3E\u90E8\u6709 toolHints\uFF08\u5EFA\u8BAE\u8C03\u7528\u7684\u5DE5\u5177\uFF09\uFF0C\u6309\u63D0\u793A\u7528 call_tool \u5904\u7406\u3002","\u63D2\u4EF6\u751F\u547D\u5468\u671F\uFF08\u5B89\u88C5/\u5378\u8F7D/\u91CD\u8F7D/\u542F\u505C/\u5217\u8868\uFF09\u4E5F\u662F\u63D2\u4EF6\u5DE5\u5177\uFF1A",'\u7ECF call_tool \u8C03\u7528 plugin-manager.manage\uFF0C\u5373 call_tool({name: "plugin-manager.manage", arguments:{action:"...", ...}})\u3002',"\u672A\u7ECF\u7528\u6237\u660E\u786E\u8981\u6C42\u4E0D\u8981\u5B89\u88C5\u6216\u5378\u8F7D\u63D2\u4EF6\u3002"].join(`
|
|
8
|
-
`):"",
|
|
1
|
+
import{EventEmitter as yt}from"node:events";import{randomUUID as Ct}from"node:crypto";import O from"node:fs";import Ne from"node:os";import F from"node:path";import{Client as _t}from"@modelcontextprotocol/sdk/client/index.js";import{SSEClientTransport as At}from"@modelcontextprotocol/sdk/client/sse.js";import{StdioClientTransport as bt}from"@modelcontextprotocol/sdk/client/stdio.js";import{StreamableHTTPClientTransport as wt}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{ToolListChangedNotificationSchema as kt}from"@modelcontextprotocol/sdk/types.js";import{createBuiltInFileTools as St,DEFAULT_TOOL_PERMISSION_POLICY as Pe,getEditFailureSnapshot as Mt}from"./built-in-file-tools.js";import{ReadFileStateTracker as $t}from"./read-file-state.js";import{ASK_USER_QUESTION_TOOL_NAME as vt,createAskUserQuestionTool as xt}from"./ask-user-question-tool.js";import{createSkillTool as Et}from"./skill-tool.js";import{createTodoWriteTool as Rt,createTodoReadTool as It}from"./todo-tool.js";import{createSubAgentTools as Nt}from"./sub-agent-tools.js";import{createAwaitCompleteTool as Pt}from"./await-complete-tool.js";import{cleanupAcodeCmdDist as Ot}from"./builtins/commandArtifacts.js";import{buildRuntimeEnvironmentPrompt as Dt}from"./runtime-environment.js";import{resolveAcodeHome as Lt}from"./paths.js";import{backgroundCommandManager as ae}from"./background-command-manager.js";import{createTerminalTool as Bt}from"./terminal-tool.js";import{ToolGroupManager as Ut,createExpandToolsTool as qt,createCallToolTool as Ft,mcpGroupKeyOf as Oe,isPluginGroupKey as De,pluginGroupKeyOf as zt}from"./tool-group-manager.js";import{loadAcodeCommands as jt}from"./commands/commandLoader.js";import{ACodeCommandRegistry as Le}from"./commands/commandRegistry.js";import{calculateCompactionThreshold as Wt,COMPACTION_LLM_PROMPT_TEMPLATE as Gt,COMPACTION_PRUNE_CATEGORIES as Jt,conversationSizeBytes as Be,conversationSizeKB as Q,estimateConversationTokens as z,isCompactionSummaryMessage as Kt,isRuntimeResourcesMessage as Ht,performCompaction as be,previewCompaction as Yt,resolveReplayWindow as Qt,pruneToolOutputs as Vt,RUNTIME_RESOURCES_PREFIX as Xt,summarizeConversation as Ue}from"./compaction.js";import{isContextOverflowError as we,resolveFinishReasonOverflow as Zt}from"./context-overflow.js";import{loadAcodeConfig as eo,loadAcodeSkillGroup as to}from"./config.js";import{ACodeHookBus as oo,findDeniedHookResult as qe}from"./hooks/hookBus.js";import{isMcpPollingToolName as Fe,isMcpBlockingToolName as ke,isMcpBatchExcludedToolName as Se,isMcpIndefinitelyBlockingToolName as so,getSemanticToolName as Me}from"./mcpToolNames.js";import{runtimeMeetingDebugLog as D}from"./meeting-debug-log.js";import{BackgroundOutputRingBuffer as no}from"./background-output-ringbuffer.js";import{buildToolCatalog as ze,matchesToolAllocation as je,mergeAllocatableTools as pe,PRESET_EXPLORE as io,rebindDefaultAgentId as We,resolveToolAllocation as ro,SUB_AGENT_ALLOWED_MCP_TOOLS as ao}from"./tool-allocation.js";import{ConcurrencyLimiter as Ge,getSharedResourceLock as lo,ToolConcurrencyScheduler as co,ToolTraceRecorder as Je}from"./tool-concurrency/index.js";function uo(n){if(typeof n!="object"||n===null)return!1;const e=n;return e.kind==="builtin"&&typeof e.toolName=="string"&&typeof e.exposedName=="string"&&typeof e.description=="string"&&typeof e.execute=="function"&&typeof e.inputSchema=="object"&&e.inputSchema!==null}function mo(n){return n.type==="text"?{type:"text",text:n.text}:{type:"image_url",image_url:{url:n.image_url.url}}}function po(n){return{id:n.id,type:"function",function:{name:n.function.name,arguments:n.function.arguments}}}function ho(n){const e={role:n.role,content:Array.isArray(n.content)?n.content.map(mo):n.content};return n.tool_call_id&&(e.tool_call_id=n.tool_call_id),n.tool_calls&&(e.tool_calls=n.tool_calls.map(po)),e}function le(n){return n.map(ho)}function fo(n,e){const t=j(n.prompt_cache_hit_tokens),o=j(n.prompt_cache_miss_tokens);if(t!==void 0||o!==void 0)return{cacheHitTokens:t??0,cacheMissTokens:o??0};const s=j(n.cache_read_input_tokens),i=j(n.cache_creation_input_tokens);if(s!==void 0||i!==void 0){const u=s??0,c=i??0,m=j(n.input_tokens)??(e!==void 0?Math.max(0,e-u-c):0);return{cacheHitTokens:u,cacheMissTokens:c+m}}const a=j(n.cache_hit_tokens),r=j(n.cache_miss_tokens);if(a!==void 0||r!==void 0)return{cacheHitTokens:a??0,cacheMissTokens:r??0};const l=j(n.prompt_tokens_details?.cached_tokens);if(l!==void 0){const u=e!==void 0?Math.max(0,e-l):0;return{cacheHitTokens:l,cacheMissTokens:u}}}function j(n){const e=typeof n=="number"?n:typeof n=="string"&&n.trim()?Number(n):NaN;if(!(!Number.isFinite(e)||e<0))return e}function go(n,e){if(!n)return e;const t=o=>{const s=e[o],i=n[o];return o==="prompt_tokens_details"?{...i,...s}:s??i};return{prompt_tokens:t("prompt_tokens"),completion_tokens:t("completion_tokens"),input_tokens:t("input_tokens"),output_tokens:t("output_tokens"),prompt_cache_hit_tokens:t("prompt_cache_hit_tokens"),prompt_cache_miss_tokens:t("prompt_cache_miss_tokens"),cache_read_input_tokens:t("cache_read_input_tokens"),cache_creation_input_tokens:t("cache_creation_input_tokens"),cache_hit_tokens:t("cache_hit_tokens"),cache_miss_tokens:t("cache_miss_tokens"),prompt_tokens_details:t("prompt_tokens_details")}}function To(){return new Date().toISOString()}function k(n){return!!(n&&typeof n=="object"&&!Array.isArray(n))}function he(n){return k(n)}function Ke(n,e){return e.map(t=>({serverName:n,toolName:t.name,kind:"mcp",exposedName:Ts(n,t.name),description:t.description,inputSchema:he(t.inputSchema)?t.inputSchema:{type:"object",properties:{}}}))}const fe="ACODE_TOOL_GROUPS_V1:";function yo(n){const e=String(n??"").trim();if(!e.startsWith(fe))return[];const t=e.slice(fe.length).trim();if(!t)return[];let o;try{o=JSON.parse(t)}catch{return[]}if(!he(o)||!Array.isArray(o.groups))return[];const s=[];for(const i of o.groups){if(!he(i))continue;const a=typeof i.key=="string"?i.key.trim():"";if(!a)continue;const r=Array.isArray(i.toolNames)?i.toolNames.filter(l=>typeof l=="string"&&l.length>0):[];s.push({key:a,label:typeof i.label=="string"&&i.label.trim()?i.label:a,toolNames:r})}return s}function Co(n){const e=String(n??"").trim();if(!e)return;if(!e.startsWith(fe))return e;const t=e.slice(fe.length).trim();return t.startsWith("{")?void 0:t||void 0}function N(n){return String(n??"").trim()||void 0}const He=6e4,Ye=30*6e4,Qe=6e4,Ve=6e4,$e=-1,Xe=0,_o=1e3,Ao=3e4,ge=6e4,Ze=0,bo=2147483647,wo=8,ko=10*6e4+2e3,So=1e4,Te=5e3,et=10*60*1e3,te=10*1024*1024,tt=800*1024,ot=32*1024*1024,Mo=4,$o=1.25;function st(){const n=Number.parseInt(String(process.env.ACODE_MAX_REQUEST_BODY_BYTES??"").trim(),10);return Number.isFinite(n)&&n>0?n:void 0}function vo(){return st()!==void 0}function xo(n){const e=st();if(e!==void 0)return e;if(!Number.isFinite(n)||n<=0)return tt;const t=Math.floor(n*Mo*$o);return Math.min(ot,Math.max(tt,t))}const ye=process.env.ACODE_COMPACTION_DEBUG==="1",nt=ye?F.join(Ne.tmpdir(),"acode-compaction-debug.log"):"";function p(...n){ye&&console.log(...n)}const Eo=process.env.ACODE_IMAGE_DIAG==="1",Ro=process.env.ACODE_IMAGE_DIAG_LOG||F.join(Ne.tmpdir(),"acode-image-inject.log");function Ce(n){if(!Eo)return;const e=`[${new Date().toISOString()}] ${JSON.stringify(n)}
|
|
2
|
+
`;O.promises.appendFile(Ro,e).catch(()=>{})}const Io=process.env.ACODE_TOKEN_DEBUG==="1";function oe(...n){Io&&console.log("[TokenDebug][Runtime]",...n)}const No=64e3;function _e(n){const e=n.contextLimit&&n.contextLimit>0?n.contextLimit:No,t=n.maxOutput&&n.maxOutput>0?n.maxOutput:8192,o=n.inputLimit&&n.inputLimit>0?n.inputLimit:void 0,s=Wt(e,t,o);return{contextLimit:e,maxOutput:t,inputLimit:o,threshold:s}}async function x(n,e,t,o){if(e<=0)return await n;let s;try{return await new Promise((i,a)=>{s=setTimeout(()=>{o?.(),a(new Error(`${t} timed out after ${e}ms`))},e),n.then(i,a)})}finally{s&&clearTimeout(s)}}function it(){return`toolbatch-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}async function se(n,e,t,o=So){try{await x(e,o,n)}catch(s){t(s instanceof Error?s.message:String(s))}}function ne(n,e){if(n===0||n==="0")return 0;const t=typeof n=="number"?n:Number(n);return t===$e?$e:Number.isFinite(t)&&t>0?Math.floor(t):e}function ce(n,e){const t=ne(n,e);return t>0?t:e}const rt=3;function ve(n){return n?Math.ceil(n.length/rt):0}function Po(n,e){let t=n.length;for(const o of e){const s=(o.kind==="mcp",o.toolName);if(t+=s.length,o.kind==="mcp")o.description&&(t+=o.description.length),t+=JSON.stringify(o.inputSchema).length;else{const i=o;i.inputSchema&&(t+=JSON.stringify(i.inputSchema).length)}}return Math.ceil(t/rt)}function ue(n){if(n.length===0)return 0;const e=JSON.stringify(ht(n));return ve(e)}async function Oo(n){await new Promise(e=>setTimeout(e,n))}function Do(n){return typeof n=="boolean"}function Lo(n,e,t){if(Do(t?.supportsImageInput))return t.supportsImageInput;const o=k(e)?e.models:void 0,s=k(o)?o[n]:void 0;if(!k(s))return!1;const i=s.modalities;return!k(i)||!Array.isArray(i.input)?!1:i.input.some(a=>String(a||"").trim().toLowerCase()==="image")}function xe(n,e,t,o){if(n!==void 0)return n;if(!e)return;const s=e.models;if(k(s)){const i=s[t];if(k(i)){const a=i[o];if(a!==void 0)return a}}return e[o]}function Bo(n,e){const t=e.provider?.default,o=t?.options,s=n.model||t?.model||e.model||"acode-default",i=N(n.effort)||N(o?.effort)||N(t?.effort)||void 0;return{model:s,baseURL:N(o?.baseURL||o?.baseUrl),apiKey:N(o?.apiKey),effort:i,systemPrompt:n.systemPrompt||e.systemPrompt,stream:o?.stream!==!1,requestTimeoutMs:ce(o?.requestTimeoutMs??o?.timeoutMs,He),requestTotalTimeoutMs:ce(o?.requestTotalTimeoutMs,Ye),requestRetryCount:ne(o?.requestRetryCount,Xe),requestFirstByteTimeoutMs:ce(o?.requestFirstByteTimeoutMs,Qe),requestStreamIdleTimeoutMs:ce(o?.requestStreamIdleTimeoutMs,Ve),mcpToolTimeoutMs:ce(o?.mcpToolTimeoutMs,ge),mcpPollingToolTimeoutMs:ne(o?.mcpPollingToolTimeoutMs,Ze),supportsImageInput:Lo(s,t,o),contextLimit:ne(xe(o?.contextLimit,t,s,"contextLimit"),0)||void 0,inputLimit:ne(xe(o?.inputLimit,t,s,"inputLimit"),0)||void 0,maxOutput:ne(xe(o?.maxOutput,t,s,"maxOutput"),0)||void 0}}function H(n,e){const t=e.replace(/\s+/g," ").trim();t&&n.push({type:"text",text:t})}function Uo(n,e){const t=e.trim();return/^data:image\/[a-z0-9.+-]+;base64,/i.test(t)?t:`data:${n||"image/png"};base64,${t}`}function qo(n){return/^data:image\/[a-z0-9.+-]+;base64,/i.test(n.trim())}function Fo(n){let e;try{e=new URL(n)}catch{throw new Error(`ACode image URL is invalid: ${n}`)}if(e.protocol!=="http:"&&e.protocol!=="https:")throw new Error(`ACode image URL protocol is not supported: ${e.protocol}`)}async function zo(n){const e=await n.arrayBuffer();if(e.byteLength>te)throw new Error(`ACode image is too large to inline: ${e.byteLength} bytes exceeds ${te} bytes`);return Buffer.from(e).toString("base64")}async function jo(n){const e=n.trim();if(qo(e)){if(e.slice(e.indexOf(",")+1).length*.75>te)throw new Error(`ACode inline image exceeds the ${te} byte size limit`);return e}Fo(e);const t=await fetch(e);if(!t.ok)throw new Error(`ACode image fetch failed: ${t.status} ${await t.text()}`);const o=t.headers.get("content-type")||"",s=o.split(";")[0]?.trim().toLowerCase()||"";if(!s.startsWith("image/"))throw new Error(`ACode image URL did not return an image content type: ${o||"unknown"}`);return`data:${s};base64,${await zo(t)}`}function Wo(n){const e=N(n.base64Data??n.base64??n.data);return e?e.length*.75>te?void 0:Uo(N(n.mediaType??n.mimeType)||"image/png",e):N(n.url??n.serverUrl)}function at(n,e,t,o){return`[Attached file: ${[n?`name=${n}`:void 0,e?`url=${e}`:void 0,t?`mediaType=${t}`:void 0,o?`size=${o}`:void 0].filter(i=>!!i).join(", ")}]`}function Go(n){const e=/\[(图片|文件)\s*:\s*([^\]\n]*?)\s*\]/gi,t=[];let o=!1,s=0,i=e.exec(n);for(;i!==null;){H(t,n.slice(s,i.index));const a=i[1],r=(i[2]||"").trim();if(a==="\u56FE\u7247"&&r)t.push({type:"image_url",image_url:{url:r}}),o=!0;else if(a==="\u6587\u4EF6"&&r){const[l="",u="",c="",m=""]=r.split("|").map(h=>h.trim());H(t,at(l,u,c,m))}else H(t,i[0]);s=i.index+i[0].length,i=e.exec(n)}return t.length===0?{content:n,hasImages:!1}:(H(t,n.slice(s)),{content:t,hasImages:o})}function Jo(n,e){let t=!1;const o=[e.directMessages,e.groupMessages];for(const s of o)if(Array.isArray(s))for(const i of s){if(!k(i))continue;const a=N(i.content);if(!a)continue;const r=N(i.senderName);H(n,r?`Message from ${r}:`:"Message:");const l=Ee(a);Array.isArray(l.content)?n.push(...l.content):H(n,l.content),t=t||l.hasImages}return t}function Ko(n){for(const e of["directMessages","groupMessages","messages"]){const t=n[e];if(Array.isArray(t))for(const o of t){if(!k(o))continue;const s=o.content;typeof s!="string"||!s.trim()||(o.content=Ho(s))}}}function Ho(n){let e;try{e=JSON.parse(n)}catch{return n.replace(/\[(图片|文件)\s*:\s*([^\]\n]*?)\s*\]/gi,"").trim()||n}if(k(e)&&"attachments"in e){const t={...e};return t.attachments=[],JSON.stringify(t)}return n}function Yo(n){let e;try{e=JSON.parse(n)}catch{return}if(!k(e))return;const t=[];let o=Jo(t,e);const s=N(e.text)||"",i=Array.isArray(e.attachments)?e.attachments:[];H(t,s);for(const a of i){if(!k(a))continue;const r=a;if(String(r.type??r.kind??"").toLowerCase()==="image"){const c=Wo(r);c?(t.push({type:"image_url",image_url:{url:c}}),o=!0):N(r.base64Data??r.base64??r.data)&&H(t,`[Image attachment omitted: ${N(r.name)||"unnamed"} exceeds the ${te} byte inline limit]`);continue}const u=N(r.url??r.serverUrl)||"";H(t,at(N(r.name)||"",u,N(r.mediaType??r.mimeType)||"",N(r.size)||""))}return t.length===0?{content:n,hasImages:!1}:{content:t,hasImages:o}}function Ee(n){return Yo(n)??Go(n)}async function lt(n){if(!Array.isArray(n))return n;const e=[];for(const t of n){if(t.type==="text"){e.push(t);continue}e.push({type:"image_url",image_url:{url:await jo(t.image_url.url)}})}return e}function Qo(n){return typeof n=="string"?n:Array.isArray(n)?n.map(e=>e.type==="text"?e.text:"[Image]").join(" ").trim():""}function Vo(n,e){const t=n.permissions??e.permissions??{},o=n.permissions?[]:e.allowedTools??[],s=n.permissions?[]:e.deniedTools??[];return{mode:t.mode??"default",allow:[...t.allow??[],...o].map(i=>i.trim()).filter(Boolean),deny:[...t.deny??[],...s].map(i=>i.trim()).filter(Boolean)}}function Xo(n,e){return n==="*"?!0:n.endsWith("*")?e.startsWith(n.slice(0,-1)):n===e}function Zo(n){return n.kind==="builtin"?[n.exposedName,n.toolName,`builtin.${n.toolName}`]:[n.exposedName,n.toolName,`${n.serverName}.${n.toolName}`,`mcp.${n.serverName}.${n.toolName}`]}function ct(n,e){return Zo(e).some(t=>Xo(n,t))}function ut(n,e,t){if(e.length>0&&!e.some(o=>ct(o,n)))throw new Error(t)}function mt(n,e,t){if(e.mode!=="bypassPermissions"){if(e.deny.some(o=>ct(o,n)))throw new Error(`ACode tool denied by permission rule: ${n.exposedName}`);ut(n,e.allow,`ACode tool is not in allowed permission rules: ${n.exposedName}`),ut(n,t??[],`ACode slash command does not allow tool: ${n.exposedName}`)}}function es(n,e,t){try{return mt(n,e,t),!0}catch{return!1}}function Ae(n){const e=n.replace(/\/+$/g,"");return/\/chat\/completions$/i.test(e)?e:`${e}/chat/completions`}function ts(n){try{const e=new URL(n);e.search="",e.hash="";const t=new URL(Ae(e.toString()));return`${t.origin}${t.pathname}`}catch{return Ae(n).replace(/([?&](?:api[_-]?key|token|authorization)=)[^&]+/gi,"$1[redacted]")}}function os(n){const e=[...n].reverse().find(o=>o.role==="user"),t=Qo(e?.content??null).trim();if(t)return t.length>300?`${t.slice(0,300)}...`:t}function ss(n){try{const e=JSON.parse(n);return he(e)?e:{}}catch{return{}}}function ns(n,e,t){if(new Set([vt,"await_complete"]).has(n.toolName))return 0;if(n.toolName!=="Bash")return t;const s=e.timeout_ms??e.timeoutMs,i=typeof s=="number"?s:typeof s=="string"?Number(s):NaN;return!Number.isFinite(i)||i<=0?Math.max(t,ko):Math.min(Math.floor(i),10*6e4)+2e3}function is(n,e){const t=n.trim();return`ACode \u5DF2\u63A5\u6536\u4EFB\u52A1\u3002\u5F53\u524D\u5DF2\u52A0\u8F7D ${e.configMcpCount} \u4E2A MCP \u914D\u7F6E\u3001${e.exposedTools.length} \u4E2A MCP Tool \u4E0E ${e.skills.length} \u4E2A Skill\u3002${t?`\u8F93\u5165\u6458\u8981\uFF1A${t.slice(0,200)}`:""}`}function W(n){if(!k(n))return String(n??"");const e=n.content;if(Array.isArray(e)){const t=e.map(o=>k(o)?typeof o.text=="string"?o.text:JSON.stringify(o):"").filter(Boolean);if(t.length>0)return t.join(`
|
|
3
|
+
`)}return JSON.stringify(n)}function me(n){return n instanceof Error?n.message:String(n)}function rs(n){const e={orphanToolMessages:0,repairedToolCalls:0,downgradedAssistants:0,relocatedInterruptions:0,reorderedToolResults:0};if(n.length===0)return e;const t=(c,m)=>{const h=typeof c.content=="string"?c.content:JSON.stringify(c.content??"");c.role="user",delete c.tool_call_id,c.content=`[\u7CFB\u7EDF\u63D0\u793A] \u4EE5\u4E0B\u4E3A\u4E00\u6B21\u5DE5\u5177\u6267\u884C\u7684\u7ED3\u679C\uFF08tool_call_id=${m}\uFF09\uFF0C\u5176\u5BF9\u5E94\u7684\u5DE5\u5177\u8C03\u7528\u4E0A\u4E0B\u6587\u5DF2\u4E0D\u5728\u4F1A\u8BDD\u4E2D\uFF0C\u4EC5\u4F9B\u53C2\u8003\uFF1A
|
|
4
|
+
${h}`,e.orphanToolMessages+=1},o=c=>{const m=new Map;for(const y of n)if(y.role==="assistant"&&Array.isArray(y.tool_calls))for(const _ of y.tool_calls){const d=typeof _?.id=="string"?_.id:"";d&&!m.has(d)&&m.set(d,y)}const h=new Set;for(const y of n){if(y.role==="assistant"&&Array.isArray(y.tool_calls)){for(const f of y.tool_calls){const g=typeof f?.id=="string"?f.id:"";g&&h.add(g)}continue}if(y.role!=="tool")continue;const _=typeof y.tool_call_id=="string"?y.tool_call_id.trim():"";if(!_){t(y,"(\u7F3A\u5931)");continue}if(h.has(_))continue;const d=m.get(_);if(d){c.push({message:y,id:_,assistant:d});continue}t(y,_)}},s=c=>{if(c.length!==0){for(const m of c){const h=n.indexOf(m.message);h>=0&&n.splice(h,1)}for(const m of c){const h=n.indexOf(m.assistant);if(h<0){t(m.message,m.id);continue}let y=h+1;for(;y<n.length&&n[y].role==="tool";)y+=1;n.splice(y,0,m.message),e.reorderedToolResults+=1}}},i=[];o(i),s(i);const a=()=>{const c=new Set;for(const m of n)m.role==="tool"&&m.tool_call_id&&c.add(m.tool_call_id);return c};let r=a();const l="\uFF08\u8BE5\u5DE5\u5177\u8C03\u7528\u7ED3\u679C\u7F3A\u5931\uFF1A\u6240\u5C5E\u8F6E\u6B21\u88AB\u4E2D\u65AD\u6216\u4E0A\u4E0B\u6587\u538B\u7F29\u65F6\u5DF2\u79FB\u9664\uFF0C\u8BF7\u52FF\u57FA\u4E8E\u8BE5\u8C03\u7528\u7EE7\u7EED\u63A8\u65AD\uFF0C\u5FC5\u8981\u65F6\u91CD\u65B0\u8C03\u7528\u5DE5\u5177\u3002\uFF09";for(let c=0;c<n.length;c++){const m=n[c];if(m.role!=="assistant"||!Array.isArray(m.tool_calls))continue;const h=m.tool_calls.filter(f=>typeof f?.id=="string"&&f.id.trim().length>0);if(h.length!==m.tool_calls.length){if(e.downgradedAssistants+=1,h.length===0){delete m.tool_calls,(m.content===null||m.content===void 0)&&(m.content="");continue}m.tool_calls=h}const y=m.tool_calls.map(f=>f?.id).filter(f=>typeof f=="string"&&f.length>0&&!r.has(f));if(y.length===0)continue;let _=-1;for(let f=c+1;f<n.length;f++){if(n[f].role==="tool"){_=f+1;continue}break}if(_>c){const f=y.map(g=>({role:"tool",tool_call_id:g,content:l}));n.splice(_,0,...f),c+=f.length,e.repairedToolCalls+=y.length,r=a();continue}if(c===n.length-1){for(const f of y)n.push({role:"tool",tool_call_id:f,content:l});e.repairedToolCalls+=y.length,r=a();continue}const d=new Set(m.tool_calls.map(f=>f?.id).filter(f=>typeof f=="string"&&f.length>0));delete m.tool_calls,(m.content===null||m.content===void 0)&&(m.content="");for(const f of n)if(f.role==="tool"&&f.tool_call_id&&d.has(f.tool_call_id)){const g=typeof f.content=="string"?f.content:JSON.stringify(f.content??""),C=f.tool_call_id;f.role="user",delete f.tool_call_id,f.content=`[\u7CFB\u7EDF\u63D0\u793A] \u4EE5\u4E0B\u4E3A\u4E00\u6B21\u5DE5\u5177\u6267\u884C\u7684\u7ED3\u679C\uFF08tool_call_id=${C}\uFF09\uFF0C\u5176\u5BF9\u5E94\u7684\u5DE5\u5177\u8C03\u7528\u4E0A\u4E0B\u6587\u5DF2\u4E0D\u5728\u4F1A\u8BDD\u4E2D\uFF0C\u4EC5\u4F9B\u53C2\u8003\uFF1A
|
|
5
|
+
${g}`,e.orphanToolMessages+=1}r=a(),e.downgradedAssistants+=1}for(let c=0;c<n.length;c++){const m=n[c];if(m.role!=="assistant"||!Array.isArray(m.tool_calls))continue;const h=new Set(m.tool_calls.map(g=>g?.id).filter(g=>typeof g=="string"&&g.length>0));if(h.size===0)continue;const y=g=>g.role==="tool"&&typeof g.tool_call_id=="string"&&h.has(g.tool_call_id),_=[];for(let g=c+1;g<n.length;g++){const C=n[g];if(y(C))continue;if(C.role==="tool")break;let w=!1;for(let b=g+1;b<n.length;b++)if(y(n[b])){w=!0;break}if(!w)break;_.push(C)}if(_.length===0)continue;const d=n.filter(g=>!_.includes(g));let f=-1;for(let g=0;g<d.length;g++)y(d[g])&&(f=g);f<0||(d.splice(f+1,0,..._),n.length=0,n.push(...d),e.relocatedInterruptions+=_.length)}const u=[];return o(u),s(u),e}const dt={truncatedCount:0,strategy:"none",prunedToolMessages:0,summarizedMessages:0,freedTokens:0};async function as(n,e,t,o,s,i){if(Be(n)<=e)return dt;const r=await be(n,t,o,{llmSummarizer:s});if(r.strategy!=="none")return r.summarizedMessages>0&&i&&await i(),{truncatedCount:r.prunedToolMessages+r.summarizedMessages,strategy:r.strategy,prunedToolMessages:r.prunedToolMessages,summarizedMessages:r.summarizedMessages,freedTokens:r.freedTokens};const l=await Ue(n,{llmSummarizer:s,contextLimit:t});return l>0&&i&&await i(),{truncatedCount:l,strategy:l>0?"summarize":"none",prunedToolMessages:0,summarizedMessages:l,freedTokens:0}}function ls(n){return we(n)}function cs(n){const e=me(n).toLowerCase();return ls(n)?!1:e.includes("fetch failed")||e.includes("timed out")||e.includes("timeout")||e.includes("econnreset")||e.includes("econnrefused")||e.includes("etimedout")||e.includes("enotfound")||e.includes("socket hang up")||e.includes("network error")||/\b(408|429|500|502|503|504|520|521|522|523|524)\b/.test(e)}function us(n){if(typeof n!="object"||n===null)return!1;const e=Reflect.get(n,"code"),t=me(n).toLowerCase();return e===-32001||t.includes("request timed out")}function ms(n){if(typeof n=="object"&&n!==null){const e=Reflect.get(n,"code");if(typeof e=="string"&&e)return e;const t=Reflect.get(n,"name");if(typeof t=="string"&&t)return t}return"tool_error"}function pt(n,e,t){return JSON.stringify({ok:!1,tool:n.exposedName,error:{type:ms(e),message:me(e)},...t?{file_snapshot:{path:t.path,bytes:t.bytes,content:t.content}}:{}})}function ds(n){const e=JSON.stringify(n);return e.length>500?`${e.slice(0,500)}\u2026`:e}function ps(n){const e=[n?.reasoning_content,n?.reasoning_text,n?.reasoning];for(const t of e){if(typeof t=="string"&&t)return t;if(k(t)){const o=t.text??t.content;if(typeof o=="string"&&o)return o}}return""}function hs(n){return n.length===0?"":["Skills are available for this session.","Use the `skill` tool to load specialized skill instructions when a user task matches one of the available skills listed in the tool description.","Do not attempt to follow skill instructions from memory \u2014 always invoke the `skill` tool first to load the full content."].join(" ")}function fs(n){return n.length===0?"":["Available slash commands:",...n.map(e=>`- /${e.name}${e.description?`: ${e.description}`:""}`),"When the user invokes a slash command, ACode expands it before model execution."].join(`
|
|
6
|
+
`)}function gs(n){if(!n||typeof n!="object")return"";const e=n,t=[],o=e.displayName,s=e.roleName,i=e.instanceName,a=e.projectId,r=e.workspacePath,l=e.runtimeMode,u=e.describe;return typeof o=="string"&&o.trim()&&t.push(`Display Name: ${o.trim()}`),typeof s=="string"&&s.trim()&&t.push(`Role: ${s.trim()}`),typeof i=="string"&&i.trim()&&t.push(`Instance: ${i.trim()}`),typeof a=="string"&&a.trim()&&t.push(`Project: ${a.trim()}`),typeof r=="string"&&r.trim()&&t.push(`Workspace: ${r.trim()}`),typeof l=="string"&&l.trim()&&t.push(`Runtime Mode: ${l.trim()}`),typeof u=="string"&&u.trim()&&t.push(`Description: ${u.trim()}`),t.length===0?"":["[User Profile]",...t].join(`
|
|
7
|
+
`)}function ht(n){return n.map(e=>({type:"function",function:{name:e.exposedName,description:e.description||e.toolName,parameters:e.inputSchema}}))}function Ts(n,e){return`${n}__${e}`.replace(/[^a-zA-Z0-9_-]/g,"_").slice(0,64)||"mcp_tool"}function ys(n){const e={"Content-Type":"application/json"};return n.apiKey&&(e.Authorization=`Bearer ${n.apiKey}`),e}function Cs(n){return Object.fromEntries(Object.entries(n).map(([e,t])=>[e,_s(e)?As(t):t]))}function _s(n){const e=n.trim().toLowerCase();return e==="authorization"||e==="api-key"||e==="x-api-key"||e==="openai-api-key"}function As(n){return n.trim().toLowerCase().startsWith("bearer ")?"Bearer ***":"***"}function bs(n){return n.split(/\r?\n/).filter(e=>e.startsWith("data:")).map(e=>e.slice(5).trim()).filter(Boolean)}function ft(n){if(!k(n))return null;const e=n.error;if(k(e)){const t=typeof e.message=="string"?e.message:"",o=e.code??e.status??e.type,s=o==null?"":String(o);return t||s?[s,t].filter(Boolean).join(" "):null}if(typeof e=="string"&&e)return e;if(n.type==="error"||n.object==="error"){const t=typeof n.message=="string"?n.message:"",o=n.code??n.status,s=o==null?"":String(o);if(t||s)return[s,t].filter(Boolean).join(" ")}return null}const ws=/role 'tool'|tool calls and tool results|tool_call_sequence|\b11148\b/i;let gt=0,de="";function Re(n,e,t){if(!ws.test(t))return null;try{const o=new Map,s=new Map;n.forEach((d,f)=>{if(d.role==="assistant"&&Array.isArray(d.tool_calls))for(const g of d.tool_calls){const C=typeof g?.id=="string"?g.id.trim():"";C&&!o.has(C)&&o.set(C,f)}else if(d.role==="tool"){const g=typeof d.tool_call_id=="string"?d.tool_call_id.trim():"";g&&!s.has(g)&&s.set(g,f)}});const i=[];n.forEach((d,f)=>{if(d.role==="tool"){const g=typeof d.tool_call_id=="string"?d.tool_call_id.trim():"",C=g?o.get(g):void 0;(g===""||C===void 0||C>f)&&i.push(f)}else if(d.role==="assistant"&&Array.isArray(d.tool_calls))for(const g of d.tool_calls){const C=typeof g?.id=="string"?g.id.trim():"";if(C===""||!s.has(C)){i.push(f);break}}});const a=d=>` [\u5DE5\u5177\u5E8F\u5217\u8BC1\u636E] \u53EF\u7591\u6D88\u606F ${i.length}/${n.length} \u6761\uFF08${d?`\u5DF2\u5199\u5165 ${d}`:"\u843D\u76D8\u5931\u8D25"}\uFF09`,r=Date.now();if(r-gt<1e4&&de)return a(de);gt=r;const l=d=>({role:d.role,tool_call_id:d.tool_call_id,tool_calls:Array.isArray(d.tool_calls)?d.tool_calls.map(f=>f?.id??null):void 0,contentPreview:typeof d.content=="string"?d.content.slice(0,300):d.content===null?null:JSON.stringify(d.content).slice(0,300)}),u=`acode-tool-seq-${new Date(r).toISOString().replace(/[:.]/g,"-")}.json`,c=String(process.env.AWS_TEST_HOME||"").trim()||String(process.env.AWS_RUNTIME_HOME_DIR||"").trim(),m=String(process.env.AWS_RUNTIME_ENV||"").trim().toLowerCase()==="development",h=c||(m?F.resolve(process.cwd(),"..",".dev-home"):F.dirname(Lt())),y=F.join(h,".aws-bridge","log"),_={timestamp:new Date(r).toISOString(),status:e,errorBody:t.slice(0,4e3),suspectIndexes:i,messages:n.map((d,f)=>({index:f,role:d.role,tool_call_id:d.tool_call_id,tool_calls:Array.isArray(d.tool_calls)?d.tool_calls.map(g=>g?.id??null):void 0})),suspectWindows:i.map(d=>{const f=Math.max(0,d-6),g=Math.min(n.length,d+7);return{center:d,range:[f,g],messages:n.slice(f,g).map((C,w)=>({index:f+w,...l(C)}))}})};return de=`.aws-bridge/log/${u}`,O.promises.mkdir(y,{recursive:!0}).then(()=>O.promises.writeFile(F.join(y,u),JSON.stringify(_,null,2),"utf-8")).then(()=>O.promises.readdir(y)).then(d=>{const f=d.filter(C=>C.startsWith("acode-tool-seq-")).sort(),g=f.length-20;return g<=0?null:Promise.all(f.slice(0,g).map(C=>O.promises.unlink(F.join(y,C)).catch(()=>{})))}).catch(()=>{de=""}),a(de)}catch{return null}}function Tt(n){const e=new Set,t=Date.now();return n.map((o,s)=>{const i=typeof o?.id=="string"?o.id.trim():"";if(i&&!e.has(i))return e.add(i),o;let a=`call-${t}-${s}`,r=1;for(;e.has(a);)a=`call-${t}-${s}-${r}`,r+=1;return e.add(a),{...o,id:a}})}function ks(n,e){const t=Number.isFinite(e.index)?Number(e.index):0,o=n.get(t)??{id:e.id||`call-${t}`,type:"function",function:{name:"",arguments:""}};e.id&&(o.id=e.id),e.function?.name&&(o.function.name+=e.function.name),e.function?.arguments&&(o.function.arguments+=e.function.arguments),n.set(t,o)}function Ss(n){return Tt([...n.entries()].sort(([e],[t])=>e-t).map(([,e])=>e).filter(e=>e.function.name.trim().length>0))}function Ms(){const n=process.env.ACODE_ABORT_FORCE_SETTLE_GRACE_MS;if(n===void 0||n.trim()==="")return 6e4;const e=Number(n.trim());return Number.isFinite(e)&&e>=0?Math.floor(e):6e4}class Z extends yt{hasActiveBlockingToolCall(){return this.activeBlockingToolCalls>0}static isLongBlockingMcpToolName(e){return so(e)}constructor(e){super(),this.config=e,this.initialConversation=[],this.mcpStderrBuffers=new Map,this.state={started:!1,terminated:!1,configMcpCount:0,skills:[],provider:{model:"acode-default",stream:!1,requestTimeoutMs:He,requestTotalTimeoutMs:Ye,requestRetryCount:Xe,requestFirstByteTimeoutMs:Qe,requestStreamIdleTimeoutMs:Ve,mcpToolTimeoutMs:ge,mcpPollingToolTimeoutMs:Ze,supportsImageInput:!1},permissions:{mode:"default",allow:[],deny:[]},mcpConnections:[],ownsMcpConnections:!0,exposedTools:[],commands:[],commandRegistry:new Le([]),conversation:[],accumulatedInputTokens:0,accumulatedOutputTokens:0,accumulatedCacheHitTokens:0,accumulatedCacheMissTokens:0,toolPermissionPolicy:{...Pe}},this.abortController=null,this.exposedToolsInitialized=!1,this.recoveredToolNames=new Set,this.readFileStateTracker=new $t,this.pendingMemorySubAgents=new Map,this.memoryDelegateCooldownUntil=new Map,this.toolGroupManager=null,this.pluginGroupStates=new Map,this.submitQueue=Promise.resolve(),this.activeTurnToken=null,this.forceSettleWaiters=new Map,this.abortForceSettleTimer=null,this.activeTurn=!1,this.activeBlockingToolCalls=0,this.forceMessagePending=!1,this.compactRequested=null,this.turnPromptPushed=!1,this.abortRequested=!1,this.toolSettleSlots=new Map,this.toolSettleSeq=0,this.toolResourceLock=lo(),this.toolConcurrencyScheduler=new co,this.toolReadOnlyLimiter=null,this.toolBashLimiter=null,this.toolConcurrencyEnabled=!0,this.pendingAwaitedSubIds=new Set,this.pendingAwaitedBgTaskIds=new Set,this.sessionCompleteEmitted=!1,this.contextVersion=0,this.stopWaiters=new Set,this.compactionDebugTimer=null,this.skillTool=null,this.skillWatchers=[],this.skillRefreshTimer=null,this.questionWaiters=new Map,this.permissionWaiters=new Map,this.hookBus=new oo({sessionId:e.sessionId,onError:(i,a)=>{this.emitAcodeEvent("error",{message:`ACode hook failed: ${i.name}: ${a instanceof Error?a.message:String(a)}`})}});for(const i of e.hooks??[])this.hookBus.register(i);e.toolPermissionPolicy&&(this.state.toolPermissionPolicy={...Pe,...e.toolPermissionPolicy});const t=e.toolConcurrency;this.toolConcurrencyEnabled=t?.enabled!==!1;const o=t?.maxReadOnly??8;this.toolReadOnlyLimiter=new Ge(o);const s=t?.maxBash??0;this.toolBashLimiter=s>0?new Ge(s):null}getAvailableTools(){return this.state.exposedTools.map(e=>e.exposedName)}emitAcodeEvent(e,t){const o={type:e,sessionId:this.config.sessionId,timestamp:To(),data:t};this.emit("event",o)}emitCompactionPhase(e,t,o,s,i){this.emitAcodeEvent("context_compaction",{action:"phase",compactionId:e,parentId:e,phase:t,phaseLabel:o,phaseDetail:s,...i?{trigger:i}:{}})}handleCompactionPhase(e,t){switch(t.phase){case"prune_start":this.emitCompactionPhase(e,"prune","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA",`\u6B63\u5728\u88C1\u526A ${t.messageCount??0} \u6761\u6D88\u606F\u4E2D\u7684\u5DE5\u5177\u8F93\u51FA...`);break;case"prune_end":this.emitCompactionPhase(e,"prune_done","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA",`\u88C1\u526A\u4E86 ${t.prunedCount??0} \u6761\u5DE5\u5177\u8F93\u51FA, \u91CA\u653E ${t.freedTokens??0} tokens`);break;case"summarize_start":this.emitCompactionPhase(e,"summarize","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD",`\u6B63\u5728\u6458\u8981 ${t.messageCount??0} \u6761\u65E9\u671F\u5BF9\u8BDD...`);break;case"summarize_end":this.emitCompactionPhase(e,"summarize_done","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD",`\u6458\u8981\u5316\u4E86 ${t.summarizedCount??0} \u6761\u6D88\u606F${t.usedLLM?" (LLM \u6458\u8981)":" (\u89C4\u5219\u6458\u8981)"}`);break}}async runCompactionPass(e){const{contextLimit:t,maxOutput:o,effectiveUsed:s,trigger:i,force:a,plan:r}=e,l=Q(this.state.conversation),u=`compaction-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;this.emitAcodeEvent("context_compaction",{action:"start",compactionId:u,sizeKB:l,estimatedTokens:s,actualInputTokens:this.state.accumulatedInputTokens,actualOutputTokens:this.state.accumulatedOutputTokens,messageCount:this.state.conversation.length,model:this.state.provider.model,trigger:i}),this.emitCompactionPhase(u,"detect",e.detectLabel,e.detectDetail);const c=await be(this.state.conversation,t,o,{llmSummarizer:h=>this.summarizeConversationWithLLM(h),onPhase:h=>this.handleCompactionPhase(u,h),actualUsedTokens:s,force:a,plan:r}),m=Q(this.state.conversation);return this.emitAcodeEvent("context_compaction",{action:"end",compactionId:u,sizeBeforeKB:l,sizeAfterKB:m,freedKB:Math.round((l-m)*10)/10,strategy:c.strategy,freedTokens:c.freedTokens,tokensBefore:c.tokensBefore,tokensAfter:c.tokensAfter,prunedToolMessages:c.prunedToolMessages,summarizedMessages:c.summarizedMessages,messageCountBefore:c.messageCountBefore,messageCountAfter:c.messageCountAfter}),this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0,c.summarizedMessages>0&&await this.rebuildRuntimeResourcesMessage(),c}compactContext(e){if(!this.state.started||this.state.terminated)throw new Error("ACode runtime is not active");return this.compactRequested={pruneCategories:e?.pruneCategories??[...Jt],summarize:e?.summarize??!0},p(`[ACodeRuntime] \u{1F5DC}\uFE0F Manual compaction requested (\u5148\u884C\u538B\u7F29): plan=${JSON.stringify(this.compactRequested)}`),{requested:!0}}getCompactionPreview(){if(!this.state.started||this.state.terminated)throw new Error("ACode runtime is not active");return Yt(this.state.conversation)}async start(){this.emitAcodeEvent("startup_phase",{phase:"loading_config"});const e=await eo(this.config.configPath);this.emitAcodeEvent("startup_phase",{phase:"loading_skills"});const{skills:t,skillGroups:o,watchedDirs:s}=await this.loadAllSkills(),i=Bo(this.config,e);p(`[ACodeRuntime] \u2605\u2605\u2605 DEBUG \u2605\u2605\u2605 resolveProviderConfig \u7ED3\u679C: config.model=${this.config.model??"(undefined)"}, resolvedModel=${i.model}, baseURL=${i.baseURL??"(none)"}, apiKey=${i.apiKey?"[set]":"(none)"}, configPath=${this.config.configPath??"(none)"}`);const a=Vo(this.config,e),r=this.config.sharedMcpConnections?this.filterSharedMcpConnectionsForSubAgent(this.config.sharedMcpConnections):await this.connectMcpServers(e.mcpServers??{},i.mcpToolTimeoutMs),l=!this.config.sharedMcpConnections;this.emitAcodeEvent("startup_phase",{phase:"fetching_profile"});const u=await this.fetchProfilePrompt(r),c=this.config.agentId||"unknown";Ot(c),ae.reconcileOrphans(c);const m=St(this.config.workingDirectory,c,this.config.onFileChange,{manager:ae,emitCompleted:T=>{this.emitAcodeEvent("background_command_completed",T)}},this.createToolPermissionController(),{tracker:this.readFileStateTracker,enabled:this.config.enableStaleFileGuard!==!1}),h=xt(this.buildAskUserQuestionDeps(),()=>this.contextVersion),y=Et(t);this.skillTool=y;const _=Rt(c),d=It(c),f=this.config.subAgentManager?Nt(this.buildSubAgentToolDeps(this.config.subAgentManager)):[],g=Pt(this.buildAwaitCompleteDeps()),C=this.config.terminalManager?Bt(c,{terminalManager:this.config.terminalManager}):null,w=new Map;this.pluginGroupStates=w;const b=new Ut({stateOf:T=>De(T)?w.get(T)??"disabled":(this.config.capabilityState??(()=>"disabled"))(T)});b.register({key:"pencil",label:"\u8BBE\u8BA1\u753B\u5E03(pencil)",summary:"UI design editor",collapsible:!0}),b.register({key:"draw",label:"\u7ED8\u56FE",summary:"\u7ED8\u5236 draw.io \u6D41\u7A0B\u56FE/\u6CF3\u9053\u56FE",collapsible:!0}),b.register({key:"office",label:"Office \u6587\u4EF6",summary:"\u53EF\u8FDB\u884C\u67E5\u770B/\u521B\u5EFA\u548C\u590D\u6742\u7F16\u8F91 Word/PPT/Excel",collapsible:!0}),b.register({key:"launch",label:"\u542F\u52A8\u9879\u7BA1\u7406",summary:"\u7BA1\u7406\u9879\u76EE\u4E2D\u4E1A\u52A1\u7A0B\u5E8F\u7684\u8D77\u505C\uFF0C\u82E5\u540E\u9762\u9700\u8981\u542F\u52A8\u9879\u76EE\u5E94\u4F7F\u7528\u6B64\u5DE5\u5177",collapsible:!0}),b.register({key:"hire",label:"\u62DB\u8058\u5DE5\u5177",summary:"\u62DB\u8058\u65B0\u7684\u540C\u4E8B Agent",collapsible:!0}),b.register({key:"browser",label:"\u6D4F\u89C8\u5668\u64CD\u4F5C",summary:"\u542F\u52A8\u6D4F\u89C8\u5668\u4F1A\u8BDD\u3001\u5BFC\u822A\u3001\u622A\u56FE\u4E0E\u9875\u9762\u4EA4\u4E92",collapsible:!0}),b.register({key:"desktop",label:"\u684C\u9762\u64CD\u4F5C",summary:"\u622A\u5C4F\u3001\u9F20\u6807\u952E\u76D8\u63A7\u5236\u4E0E UI \u81EA\u52A8\u5316",collapsible:!1}),b.register({key:"task",label:"\u6D41\u7A0B\u4EFB\u52A1",summary:"\u5904\u7406\u6D41\u7A0B\u7EBF\u4EFB\u52A1\uFF1A\u67E5\u770B\u5F53\u524D\u4EFB\u52A1\u3001\u63D0\u4EA4\u6267\u884C\u7ED3\u679C\u3001\u56DE\u9000\u4EFB\u52A1\u3001\u7ED9\u6D41\u7A0B\u5B9E\u4F8B\u7559\u8A00",collapsible:!0,toolNames:["my_task","submit_task_result","start_task_rollback","finish_task_rollback","create_task_instance_comment"]}),b.register({key:"meeting",label:"\u4F1A\u8BAE",summary:"\u521B\u5EFA/\u52A0\u5165\u4F1A\u8BAE\u3001\u53D1\u8A00\u3001\u67E5\u770B\u4F1A\u8BAE\u4FE1\u606F\u3001\u79BB\u5F00/\u7ED3\u675F\u4F1A\u8BAE",collapsible:!0,toolNames:["create_meeting","leave_meeting","end_meeting","meeting_speak","get_meeting_info","my_meetings"]}),b.register({key:"debug",label:"\u8C03\u8BD5\u65E5\u5FD7",summary:"\u6309\u6761\u4EF6\u5206\u9875\u67E5\u8BE2 Debug \u65E5\u5FD7\u4E0E\u83B7\u53D6\u65E5\u5FD7\u4E0A\u62A5\u914D\u7F6E",collapsible:!0,toolNames:["get_debug_log_config","query_debug_logs"]}),b.register({key:"fileHistory",label:"\u6587\u4EF6\u53D8\u66F4\u5386\u53F2",summary:"\u6309\u89D2\u8272/\u5B9E\u4F8B/\u6587\u4EF6\u8DEF\u5F84\u8FC7\u6EE4\u67E5\u8BE2\u6587\u4EF6\u53D8\u66F4\u5386\u53F2",collapsible:!0,toolNames:["query_file_changes_history"]}),b.register({key:"email",label:"\u90AE\u7BB1",summary:"\u6536\u53D1\u89D2\u8272\u5B9E\u4F8B\u57DF\u540D\u90AE\u7BB1\uFF08xxxxx@\u57DF\u540D\uFF09\uFF1A\u53D1\u4FE1\u3001\u5217\u90AE\u4EF6\u3001\u8BFB\u6B63\u6587\u4E0E\u9644\u4EF6\u3001\u56DE\u4FE1\u4FDD\u6301\u7EBF\u7A0B\u3001\u786E\u8BA4\u4E1A\u52A1\u6D88\u8D39",collapsible:!0,toolNames:["send_email","list_emails","read_email","reply_email","ack_email"]}),this.toolGroupManager=b;for(const T of r){const I=T.spec??e.mcpServers?.[T.serverName];I&&this.registerMcpCollapsedGroup(T.serverName,I,T)}for(const T of r)this.registerToolGroupsFromMcp(T);if(this.config.pluginBridge)try{const T=await this.config.pluginBridge.listPlugins();this.applyPluginGroups(T,w,b)}catch(T){p(`[ACodeRuntime] \u63D2\u4EF6\u7EC4\u6CE8\u518C\u5931\u8D25\uFF08\u5FFD\u7565\uFF09: ${T instanceof Error?T.message:String(T)}`)}const M=qt({toolGroupManager:b,getAllTools:()=>this.state.exposedTools,capabilityToolRelay:this.config.capabilityToolRelay,pluginBridge:this.config.pluginBridge}),$=Ft({toolGroupManager:b,pluginBridge:this.config.pluginBridge,capabilityToolRelay:this.config.capabilityToolRelay,findTool:T=>{const I=this.state.exposedTools.find(Ie=>Ie.exposedName===T||Ie.toolName===T);if(!I)return;const re=I.group??b.getGroupForExposedName(I.exposedName);return{kind:I.kind,group:re,toolName:I.toolName}},dispatchTool:(T,I,re)=>this.executeSingleToolByName(T,I,re)}),S=[...m,h,y,_,d,...f,g,...C?[C]:[],M,$,...r.flatMap(T=>T.tools),...We(this.config.extraBuiltInTools??[],this.config.agentId??"")];if(this.config.subAgentAllowedTools&&this.config.subAgentAllowedTools.length>0){const T=We(this.config.subAgentSuiteTools??[],this.config.agentId??""),I=pe(S,T);S.length=0,S.push(...I)}let v=S;const E=this.config.subAgentAllowedTools;E&&E.length>0&&(v=v.filter(T=>je(T,E)));const G=this.resolveCommandDirs(e);let L=[...G,...this.discoverPluginCommandDirs()];if(this.config.pluginBridge?.getCommandDirs)try{const T=await this.config.pluginBridge.getCommandDirs();T.length>0&&(L=[...L,...T])}catch(T){p(`[ACodeRuntime] \u63D2\u4EF6\u547D\u4EE4\u76EE\u5F55\u83B7\u53D6\u5931\u8D25\uFF08\u5FFD\u7565\uFF09: ${T instanceof Error?T.message:String(T)}`)}L=[...new Set(L)],this.emitAcodeEvent("startup_phase",{phase:"loading_commands"});const B=await jt(L),ie=new Le(B),ee=hs(t),A=fs(B),P=b.buildCapabilityPrompt(),Y=this.config.pluginBridge?["","## \u63D2\u4EF6\u7CFB\u7EDF","\u5DF2\u5B89\u88C5\u63D2\u4EF6\u4EE5\u6536\u7EB3\u7EC4\u5F62\u5F0F\u5217\u51FA\uFF08\u89C1\u4E0A\u65B9\u300C\u53EF\u7528\u80FD\u529B\u300D\u4E2D\u7684 plugin:{\u63D2\u4EF6\u540D} \u6761\u76EE\uFF09\u3002",'\u9700\u8981\u67D0\u63D2\u4EF6\u7684\u5DE5\u5177\u65F6\uFF0C\u5148 expand_tools({ group: "plugin:\u63D2\u4EF6\u540D" }) \u5C55\u5F00\uFF1B','\u5DE5\u5177\u5B9A\u4E49\uFF08\u542B\u53C2\u6570 schema\uFF09\u968F\u5C55\u5F00\u7ED3\u679C\u8FD4\u56DE\uFF0C\u518D\u7528 call_tool({ name: "\u63D2\u4EF6\u540D.\u5DE5\u5177\u540D", arguments: {...} }) \u8C03\u7528\u3002',"\u63D2\u4EF6\u4E8B\u4EF6\u4EE5 [plugin-event] \u6D88\u606F\u51FA\u73B0\uFF0C\u6D88\u606F\u6807\u660E\u6765\u6E90\u63D2\u4EF6\u4E0E\u4E8B\u4EF6\u540D\u5E76\u5E26 toolHints\uFF08\u5EFA\u8BAE\u8C03\u7528\u7684\u5DE5\u5177\uFF09\uFF0C\u6309\u63D0\u793A\u5C55\u5F00\u5E76\u5904\u7406\u3002","\u80FD\u529B\u7BA1\u7406\u5206\u5C5E\u4E24\u4E2A\u72EC\u7ACB\u6388\u6743\u7684\u5DE5\u5177\u7EC4\uFF1A","\u300C\u80FD\u529B\u5E02\u573A\u300D\u7EC4\u8D1F\u8D23\u5B89\u88C5/\u5378\u8F7D MCP / Skill / Plugin\uFF1Alist_ability_market \u6D4F\u89C8\uFF08\u8FD4\u56DE\u73AF\u5883\u53D8\u91CF\u5951\u7EA6 envSchema\uFF0CMCP \u9700\u636E\u6B64\u914D\u7F6E env\uFF0C\u654F\u611F\u503C\u5FC5\u987B\u7531\u7528\u6237\u63D0\u4F9B\uFF09\u3001install_ability_market_item \u5B89\u88C5\u3001uninstall_ability_market_item \u5378\u8F7D\uFF08kind=plugin \u5373\u5B89\u88C5/\u5378\u8F7D\u63D2\u4EF6\uFF09\u3002","\u300C\u80FD\u529B\u5F00\u53D1\u300D\u7EC4\u8D1F\u8D23\u81EA\u7814 MCP / \u63D2\u4EF6 / Skill \u5E76\u7BA1\u7406\u5DF2\u88C5\u63D2\u4EF6\uFF1A","\u7528 ability_dev_ide \u8BFB\u7F16\u5199\u89C4\u8303\uFF08action=guide\uFF0Ckind=plugin|mcp|skill\uFF09\u5E76\u5728\u81EA\u5DF1\u7684\u4F1A\u8BDD\u5DE5\u4F5C\u533A\u5185\u7F16\u5199\uFF1B","MCP/\u63D2\u4EF6\u7528 ability_dev_ide \u4E34\u65F6\u6302\u8F7D\u8C03\u8BD5\uFF08action=mount/unmount/list/logs\uFF1B\u4E0D\u6301\u4E45\u5316\u3001\u91CD\u542F\u5373\u5931\u6548\uFF0C\u6539\u4EE3\u7801\u81EA\u52A8\u91CD\u8F7D\uFF09\uFF0C","mount \u540E\u5DE5\u5177\u540D\u5F62\u5982 <server>__<tool>\uFF08\u53CC\u4E0B\u5212\u7EBF\uFF09\uFF0C\u7528 call_tool \u8C03\u7528\uFF1B","action=list \u67E5\u770B\u6302\u8F7D\u9879\u4E0E\u66B4\u9732\u7684\u5DE5\u5177\u540D\uFF0Caction=logs \u6392\u67E5\u5B50\u8FDB\u7A0B\u62A5\u9519\uFF0Caction=unmount \u6458\u9664\uFF1B","\u8C03\u8BD5\u901A\u8FC7\u540E\u7528 formal_ability \u8F6C\u6B63\uFF08\u7F3A\u7701 scope=global \u4EC5\u767B\u8BB0\u5168\u5C40\u914D\u7F6E\u6A21\u677F\u4E14\u521D\u59CB\u5316\u5173\u95ED\uFF0C\u9700\u7528\u6237\u5728\u9762\u677F\u5F00\u542F\u540E\u751F\u6548\uFF1Bscope=instance \u624D\u7ACB\u5373\u751F\u6548\uFF09\uFF1BSkill \u65E0\u8C03\u8BD5\u6001\uFF0C\u540C\u6837\u53EF\u7ECF formal_ability \u6216\u80FD\u529B\u5E02\u573A\u5B89\u88C5\u3002","\u5DF2\u5B89\u88C5\u63D2\u4EF6\u7684\u542F\u505C/\u91CD\u8F7D\u7531\u7528\u6237\u5728\u9762\u677F\u64CD\u4F5C\uFF1B","\u67E5\u770B\u81EA\u8EAB\u5168\u90E8\u80FD\u529B\uFF08MCP/Skill/Plugin \u542F\u505C\u4E0E\u6765\u6E90\u3001\u5DE5\u5177\u5F00\u5173\u3001\u8C03\u8BD5\u6302\u8F7D\uFF09\u7528 list_my_abilities\u3002","\u672A\u7ECF\u7528\u6237\u660E\u786E\u8981\u6C42\u4E0D\u8981\u5B89\u88C5\u6216\u5378\u8F7D\u63D2\u4EF6\u3002"].join(`
|
|
8
|
+
`):"",U=P+Y,R=[i.systemPrompt,ee,A,U].map(T=>T?.trim()).filter(T=>!!T).join(`
|
|
9
9
|
|
|
10
|
-
`),J=
|
|
10
|
+
`),J=T=>T?.trim()?[{role:"system",content:T.trim()}]:[],q=[this.config.rolePrompt,u].map(T=>T?.trim()).filter(T=>!!T).join(`
|
|
11
11
|
|
|
12
|
-
`),
|
|
12
|
+
`),V=[...J(this.config.globalPrompt),...J(q),...J(this.config.memory),...J(this.config.resumeContext),...R?[{role:"system",content:R}]:[],...J(Dt())],X=V.map(T=>T.content).filter(Boolean).join(`
|
|
13
13
|
|
|
14
|
-
`);this.initialConversation=
|
|
15
|
-
`,"utf-8").catch(()=>{})}catch{}},5e3)}stopCompactionDebugLog(){this.compactionDebugTimer&&(clearInterval(this.compactionDebugTimer),this.compactionDebugTimer=null)}async submit(e,t){const o=this.contextVersion;p(`[ACodeRuntime] \u{1F4EC} submit() queued: contextVersion=${o}, activeTurn=${this.activeTurn}, abortRequested=${this.abortRequested}, terminated=${this.state.terminated}, message.length=${e.length}`);const s=this.submitQueue.then(()=>(p(`[ACodeRuntime] \u25B6\uFE0F executeSubmit dequeued: contextVersion=${o}`),this.executeSubmitGuarded(e,o,t)));return this.submitQueue=s.catch(()=>{p("[ACodeRuntime] \u{1F4EC} submit() queued item rejected (catch swallowed)")}),p("[ACodeRuntime] \u{1F4EC} submit() returning queued promise"),s}executeSubmitGuarded(e,t,o){const s={};this.activeTurnToken=s;const r=this.executeSubmit(e,t,o),i=new Promise(a=>{this.forceSettleWaiters.set(s,a)});return r.finally(()=>{this.forceSettleWaiters.delete(s),this.activeTurnToken===s&&(this.abortForceSettleTimer&&(clearTimeout(this.abortForceSettleTimer),this.abortForceSettleTimer=null),this.activeTurnToken=null)}).catch(()=>{}),Promise.race([r,i])}armAbortForceSettle(){if(!this.activeTurn||!this.activeTurnToken)return;const e=this.activeTurnToken;this.abortForceSettleTimer&&clearTimeout(this.abortForceSettleTimer);const t=_s();this.abortForceSettleTimer=setTimeout(()=>{this.abortForceSettleTimer=null,this.activeTurn&&this.activeTurnToken===e&&this.forceSettleActiveTurn(`turn still unsettled ${t}ms after abort()`)},t),typeof this.abortForceSettleTimer.unref=="function"&&this.abortForceSettleTimer.unref()}forceSettleActiveTurn(e){const t=this.activeTurnToken;console.error(`[ACodeRuntime] \u{1F6A8} FORCE SETTLE: active turn ignored abort() (${e}). Force-settling to unblock submitQueue; late executeSubmit continuation will be superseded.`),this.contextVersion+=1,this.activeTurn=!1,this.abortRequested=!1,this.abortController=null,this.rejectAllQuestionWaiters("aborted"),this.rejectAllPermissionWaiters("aborted"),this.cancelPendingAwaitedSubAgents(),this.cancelPendingAwaitedBgTasks(),this.notifyRuntimeStop(),this.emitAcodeEvent("error",{message:`ACode turn force-settled: ${e}`}),this.emitAcodeEvent("turn_complete",{failed:!0});const o=t?this.forceSettleWaiters.get(t):void 0;t&&this.forceSettleWaiters.delete(t),this.activeTurnToken=null,o?.()}notifyForceMessage(){this.forceMessagePending=!0,p("[ACodeRuntime] \u{1F514} Force message pending flag set")}exportConversation(){return re(this.state.conversation)}async importTranscript(e){if(!Array.isArray(e)||e.length===0)return console.log("[HIBERNATE-REPLAY][runtime] \u7A7A\u8F93\u5165\uFF0C\u8DF3\u8FC7\u91CD\u653E"),0;if(!this.state.started)return console.log(`[HIBERNATE-REPLAY][runtime] \u4F1A\u8BDD\u672A start()\uFF0C\u8DF3\u8FC7\u91CD\u653E\uFF08${e.length}\u6761\u88AB\u4E22\u5F03\uFF09`),0;const t=e.filter(u=>u.role!=="system");if(t.length===0)return 0;const{contextLimit:o,maxOutput:s,inputLimit:r}=Ce(this.state.provider),i=Gt(o,r),a=j(this.state.conversation)+j(t)+ce(this.state.exposedTools);let l=t;if(console.log(`[HIBERNATE-REPLAY][runtime] \u9884\u68C0: \u53EF\u91CD\u653E=${t.length}\u6761 projectedTokens=${a} hardWindow=${i} \u9700\u8981\u9884\u538B\u7F29=${a>=i}`),a>=i){p(`[ACodeRuntime] \u{1F4E6} importTranscript pre-compaction: projected=${a} >= hardWindow=${i}`);try{const u=await Ae(t,o,s,{llmSummarizer:c=>this.summarizeConversationWithLLM(c)});if(u.strategy!=="none"){const c=j(t);p(`[ACodeRuntime] \u{1F4E6} importTranscript pre-compaction done: strategy=${u.strategy}, tokensBefore=${a}, tokensAfter=${c}`)}}catch(u){p(`[ACodeRuntime] \u26A0\uFE0F importTranscript pre-compaction failed, replaying raw: ${u instanceof Error?u.message:String(u)}`)}l=t}return this.state.conversation.push(...re(l)),console.log(`[HIBERNATE-REPLAY][runtime] \u91CD\u653E\u5B8C\u6210: \u62FC\u5165=${l.length}\u6761 conversation\u603B\u957F=${this.state.conversation.length}\u6761 \u603Btoken\u4F30\u7B97=${j(this.state.conversation)+ce(this.state.exposedTools)}`),this.emitAcodeEvent("context_compaction",{action:"end",compactionId:`transcript-replay-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,sizeKB:Y(this.state.conversation),estimatedTokens:j(this.state.conversation)+ce(this.state.exposedTools),actualInputTokens:this.state.accumulatedInputTokens,actualOutputTokens:this.state.accumulatedOutputTokens,messageCount:this.state.conversation.length,replayedMessageCount:l.length,model:this.state.provider.model,trigger:"hibernation_replay"}),l.length}addBuiltInTools(e){if(!this.state||!this.exposedToolsInitialized||!Array.isArray(e))return 0;const t=new Set(this.state.exposedTools.map(s=>s.exposedName)),o=[];for(const s of e)no(s)&&(t.has(s.exposedName)||(t.add(s.exposedName),o.push(s)));return o.length===0?0:(this.state.exposedTools=[...this.state.exposedTools,...o],this.emitExposedToolsStatus(),o.length)}removeBuiltInToolsByPrefix(e){if(!this.state||!this.exposedToolsInitialized||!e)return 0;const t=this.state.exposedTools.length;this.state.exposedTools=this.state.exposedTools.filter(s=>!s.exposedName.startsWith(e));const o=t-this.state.exposedTools.length;return o>0&&this.emitExposedToolsStatus(),o}addPluginCommands(e){if(!this.state||e.length===0)return;this.state.commandRegistry.addCommands(e);const t=new Set(this.state.commands.map(o=>o.name));this.state.commands=[...this.state.commands,...e.filter(o=>!t.has(o.name))]}emitExposedToolsStatus(){this.emitAcodeEvent("mcp_status",{servers:this.state.mcpConnections.map(e=>e.serverName),connectedCount:this.state.mcpConnections.length,toolCount:this.state.exposedTools.length,tools:this.state.exposedTools.map(e=>({name:e.exposedName,source:e.kind==="mcp"?`${e.serverName}.${e.toolName}`:`builtin.${e.toolName}`,description:e.description||""}))})}async injectForceMessage(){if(!this.forceMessagePending)return!1;this.forceMessagePending=!1;const e=this.state.mcpConnections.find(t=>t.tools.some(o=>o.toolName==="get_message"));if(!e)return p("[ACodeRuntime] injectForceMessage: no connection with get_message found"),!1;try{const t=await R(e.client.callTool({name:"get_message",arguments:{_aws_force_inject:!0}},void 0,{timeout:1e4}),1e4,"injectForceMessage get_message"),o=G(t);let s=[];try{const l=JSON.parse(o);Array.isArray(l?.directMessages)&&(s=l.directMessages.filter(u=>u?.force===!0))}catch{return!1}if(s.length===0)return!1;const r=JSON.stringify({directMessages:s,groupMessages:[]}),i=`forced_get_msg_${Date.now()}`,a=`${e.serverName}__get_message`;return this.state.conversation.push({role:"assistant",content:null,tool_calls:[{id:i,type:"function",function:{name:a,arguments:"{}"}}]}),this.state.conversation.push({role:"tool",tool_call_id:i,content:r}),p(`[ACodeRuntime] injectForceMessage: injected ${s.length} force message(s) into conversation`),!0}catch(t){return p(`[ACodeRuntime] injectForceMessage failed: ${t}`),!1}}async resetContext(){if(!this.state.started||this.state.terminated)throw new Error("ACode runtime is not active");this.contextVersion+=1,this.abortRequested=this.activeTurn,this.abortController?.abort(),this.abortController=null,this.state.conversation=re(this.initialConversation),this.turnPromptPushed=!1,this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0}isTurnSuperseded(e){return e!==this.contextVersion}async executeSubmit(e,t,o){const s=Date.now();if(p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),p("[ACodeRuntime] \u25B6\uFE0F executeSubmit STARTED"),p(`[ACodeRuntime] Message: <redacted, length=${e.length}>`),p(`[ACodeRuntime] \u{1F522} Context version: ${t}`),p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),!this.state.started||this.state.terminated)throw console.error(`[ACodeRuntime] \u274C Runtime not active (started: ${this.state.started}, terminated: ${this.state.terminated})`),new Error("ACode runtime is not active");this.activeTurn=!0,this.abortRequested=!1;const r=this.activeTurnToken,i=e.trim();let a=!1;const l=o?.resume===!0&&this.turnPromptPushed;l||(this.turnPromptPushed=!1);try{let u;if(l)p("[ACodeRuntime] \u{1F501} resume turn: skip UserPromptSubmit hook (already ran in failed turn)"),u=[];else{p("[ACodeRuntime] \u{1F50D} Dispatching UserPromptSubmit hook...");const T=Date.now();u=await this.hookBus.dispatch("UserPromptSubmit",{prompt:i});const A=Date.now()-T;p(`[ACodeRuntime] \u2705 UserPromptSubmit hook completed in ${A}ms, results: ${u.length}`)}if(this.isTurnSuperseded(t)){p("[ACodeRuntime] \u26A0\uFE0F Turn superseded after hook, aborting");return}const c=Le(u);if(c){console.error(`[ACodeRuntime] \u274C Prompt denied by hook: ${c.message}`),this.emitAcodeEvent("error",{message:c.message||"ACode prompt was denied by hook"}),this.emitAcodeEvent("turn_complete",{failed:!0}),a=!0,p("[ACodeRuntime] \u{1F4E4} Emitted turn_complete (failed=true) due to hook deny");return}p("[ACodeRuntime] \u{1F50D} Expanding command if any...");const m=this.state.commandRegistry.expand(i),h=m?.prompt.trim()||i,y=m?.command.allowedTools;if(m&&!l){p(`[ACodeRuntime] \u{1F4CB} Slash command detected: ${m.command.name}`),this.emitAcodeEvent("command_status",{action:"slash_command",command:m.command.name,arguments:m.argumentsText,sourcePath:m.command.sourcePath});const T=m.command.plugin;T&&(p(`[ACodeRuntime] \u{1F4CB} Slash command belongs to plugin: ${T}`),this.config.pluginBridge?.exposePlugin?.(T))}if(!this.state.provider.baseURL){p("[ACodeRuntime] \u26A0\uFE0F No provider baseURL configured, using fallback response");const T=Zo(h,this.state);this.emitAcodeEvent("assistant_delta",{text:T}),this.emitAcodeEvent("turn_complete",{usage:{inputTokens:h.length,outputTokens:T.length}}),a=!0,p("[ACodeRuntime] \u{1F4E4} Emitted turn_complete (fallback mode)");return}if(l){if(p("[ACodeRuntime] \u{1F501} resume turn: user message already in conversation, skipping push"),this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded before resume runModelTurn, aborting");return}}else{p("[ACodeRuntime] \u{1F4CE} Parsing attachment message...");const T=Ee(h);if(T.hasImages&&!this.state.provider.supportsImageInput)throw console.error("[ACodeRuntime] \u274C Image input not supported by current provider"),new Error("ACode provider/model is not configured for image input. Enable provider.default.models[model].modalities.input including 'image' or options.supportsImageInput=true.");p("[ACodeRuntime] \u{1F5BC}\uFE0F Materializing image content if any...");const A=await it(T.content);if(this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded after materializing content, aborting");return}p("[ACodeRuntime] \u{1F4AC} Adding user message to conversation"),this.state.conversation.push({role:"user",content:A}),this.turnPromptPushed=!0,p(`[ACodeRuntime] Conversation size: ${this.state.conversation.length} messages`)}p("[ACodeRuntime] Starting runModelTurn...");const C=Date.now(),d=await this.runModelTurn(y,t),f=Date.now()-C;if(p(`[ACodeRuntime] \u2705 runModelTurn completed in ${f}ms`),p(`[ACodeRuntime] \u{1F4DD} Result: text=${d.text?d.text.slice(0,80)+(d.text.length>80?"...":""):"(empty)"}, finishReason=${d.finishReason||"(none)"}`),this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded after runModelTurn, aborting");return}d.text&&!this.state.provider.stream&&(p("[ACodeRuntime] \u{1F4E4} Emitting assistant_delta (non-streaming mode)"),this.emitAcodeEvent("assistant_delta",{text:d.text})),p("[ACodeRuntime] \u{1F4E4} Emitting turn_complete..."),this.emitAcodeEvent("turn_complete",{usage:d.usage??{inputTokens:h.length,outputTokens:d.totalOutputTextLength??d.text.length},...d.responseBody?{responseBody:d.responseBody}:{},...d.finishReason?{finishReason:d.finishReason}:{}}),te(`turn_complete emitted: usage=${JSON.stringify(d.usage??{inputTokens:h.length,outputTokens:d.totalOutputTextLength??d.text.length})} (providerUsage=${d.usage?"real":"FALLBACK-text-length"}) textLength=${d.text.length} totalOutputTextLength=${d.totalOutputTextLength}`),p(`[ACodeRuntime] \u{1F4B0} Current context usage: input=${this.state.accumulatedInputTokens}, output=${this.state.accumulatedOutputTokens}`+(d.usage?"":" (estimated, no provider usage)")),a=!0,this.turnPromptPushed=!1;const g=Date.now()-s;p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),p(`[ACodeRuntime] \u2705 executeSubmit COMPLETED in ${g}ms`),p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550")}catch(u){const c=Date.now()-s;if(console.error(`[ACodeRuntime] \u274C executeSubmit FAILED after ${c}ms:`,u instanceof Error?u.message:String(u)),console.error("[ACodeRuntime] \u{1F50D} Error stack:",u instanceof Error?u.stack:"(no stack)"),this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded in error handler, skipping recovery");return}if(this.abortRequested){p("[ACodeRuntime] \u26A0\uFE0F Abort requested, emitting aborted turn_complete"),this.emitAcodeEvent("turn_complete",{aborted:!0}),a=!0;return}p("[ACodeRuntime] Emitting error and failed turn_complete..."),this.emitAcodeEvent("error",{message:u instanceof Error?u.message:String(u)}),this.emitAcodeEvent("turn_complete",{failed:!0}),a=!0}finally{const u=Date.now()-s;p(`[ACodeRuntime] \u{1F51A} executeSubmit finally block entered after ${u}ms`),p(`[ACodeRuntime] \u{1F50D} turnCompleted=${a}, activeTurn=${this.activeTurn}, abortRequested=${this.abortRequested}, contextVersion=${t}, currentContextVersion=${this.contextVersion}`),this.activeTurnToken===r&&(this.activeTurn=!1,this.abortRequested=!1),!a&&!this.isTurnSuperseded(t)&&(console.error(`[ACodeRuntime] \u{1F6A8} CRITICAL: executeSubmit finally block reached without turn_complete event! Duration: ${u}ms. This indicates a code path that should have emitted turn_complete but didn't. Forcing failed turn_complete to prevent state stuck.`),this.emitAcodeEvent("error",{message:"ACode turn completed without proper completion event (internal error)"}),this.emitAcodeEvent("turn_complete",{failed:!0}),p("[ACodeRuntime] Emergency turn_complete (failed=true) emitted"))}}async abort(){const e=this.abortController!==null,t=this.activeTurn;this.abortRequested=this.activeTurn,this.abortController?(p(`[ACodeRuntime] \u26D4 abort() calling abortController.abort() (activeTurn=${t})`),this.abortController.abort()):console.warn(`[ACodeRuntime] \u26D4 abort() called but abortController is null! activeTurn=${t}, abortRequested will be set to ${t}. This means abort() CANNOT cancel any running fetch/stream \u2014 if a built-in tool is executing, it will NOT be interrupted.`),this.abortController=null,this.rejectAllQuestionWaiters("aborted"),this.rejectAllPermissionWaiters("aborted"),this.cancelPendingAwaitedSubAgents(),this.cancelPendingAwaitedBgTasks(),this.notifyRuntimeStop(),this.armAbortForceSettle()}buildAskUserQuestionDeps(){return{onQuestion:(e,t)=>{this.emitAcodeEvent("question_request",{toolCallId:e,questions:t})},waitForAnswer:(e,t)=>new Promise((o,s)=>{this.questionWaiters.set(e,{resolve:o,reject:s,contextVersion:t})})}}async buildAllocatableToolList(){const e=this.state?.exposedTools??[],t=this.config.subAgentSuiteTools??[];let o=[];if(this.config.subAgentSuiteToolProvider)try{o=await this.config.subAgentSuiteToolProvider([])}catch{o=[]}return de(e,de(t,o))}buildSubAgentToolDeps(e){const t=this.config.agentId??"unknown",o=this.config.sessionId,s=this.config.workingDirectory,r=this.config;return{spawnSubAgent:async i=>{let a=this.config.subAgentSuiteTools;if(i.allowedTools&&i.allowedTools.length>0&&this.config.subAgentSuiteToolProvider)try{const u=await this.config.subAgentSuiteToolProvider(i.allowedTools);u.length>0&&(a=a?de(a,u):u)}catch{}const l=await e.spawn({parentAgentId:t,parentSessionId:o,subagentType:i.subagentType,description:i.description,prompt:i.prompt,...i.allowedTools&&i.allowedTools.length>0?{allowedTools:i.allowedTools}:{},workingDirectory:s,parentConfig:{...r,sharedMcpConnections:this.state?.mcpConnections,...a&&a.length>0?{subAgentSuiteTools:a}:{}}});return await e.start(l),{subId:l}},listSubAgentToolCatalog:async()=>Ue(await this.buildAllocatableToolList()),listSubAgents:async()=>{const i=e.listByParent(t),a=[],l=[];for(const u of i){const c={subId:u.subId,subagentType:u.subagentType,description:u.description,status:u.status,startedAt:u.startedAt,endedAt:u.endedAt,durationMs:u.durationMs};u.status==="pending"||u.status==="running"?a.push(c):l.push(c)}return l.sort((u,c)=>(c.endedAt??0)-(u.endedAt??0)),{running:a,history:l}},getSubAgentStatus:async i=>{const a=e.getSubAgent(i);return a?{subId:a.subId,subagentType:a.subagentType,description:a.description,status:a.status,startedAt:a.startedAt,endedAt:a.endedAt,durationMs:a.durationMs,prompt:a.prompt,result:a.result,errorMessage:a.errorMessage,contextSnapshot:a.contextSnapshot}:null},awaitSubAgents:async(i,a)=>{const l=new Set;for(const u of i)this.pendingAwaitedSubIds.add(u),l.add(u);try{return await e.awaitCompletion(i,a)}finally{for(const u of l)this.pendingAwaitedSubIds.delete(u)}},abortSubAgent:i=>e.abort(i)}}buildAwaitCompleteDeps(){const e=this.config.agentId??"unknown",t=this.config.subAgentManager;return{agentId:e,awaitSubAgents:t?(o,s)=>{for(const r of o)this.pendingAwaitedSubIds.add(r);try{return this.buildSubAgentToolDeps(t).awaitSubAgents(o,s)}finally{for(const r of o)this.pendingAwaitedSubIds.delete(r)}}:null,awaitBgCommands:(o,s)=>{for(const r of o)this.pendingAwaitedBgTaskIds.add(r);try{return ie.awaitCompletion(e,o,s)}finally{for(const r of o)this.pendingAwaitedBgTaskIds.delete(r)}}}}submitQuestionAnswer(e,t){const o=this.questionWaiters.get(e);return o?(this.questionWaiters.delete(e),this.isTurnSuperseded(o.contextVersion)?(o.reject(new Error("turn superseded before user answered")),!1):(o.resolve(t),!0)):!1}rejectAllQuestionWaiters(e){if(this.questionWaiters.size!==0){for(const[,t]of this.questionWaiters)t.reject(new Error(`ask_user_question ${e}`));this.questionWaiters.clear()}}createToolPermissionController(){return{getLevel:e=>this.state.toolPermissionPolicy[e]??"allow",requestPermission:(e,t)=>this.requestToolPermission(e,t)}}requestToolPermission(e,t){const o=gt();return new Promise(s=>{this.permissionWaiters.set(o,{capability:e,detail:t,resolve:s}),this.emitAcodeEvent("tool_permission_request",{requestId:o,capability:e,agentId:this.config.agentId,...t})})}submitToolPermissionDecision(e,t){const o=this.permissionWaiters.get(e);return o?(this.permissionWaiters.delete(e),o.resolve(t),!0):!1}rejectAllPermissionWaiters(e){if(this.permissionWaiters.size!==0){for(const[,t]of this.permissionWaiters)t.resolve(!1);this.permissionWaiters.clear(),U("tool-permission",`waiters rejected: ${e}`)}}setToolPermissionPolicy(e){this.state.toolPermissionPolicy={...this.state.toolPermissionPolicy,...e}}cancelPendingAwaitedSubAgents(){if(this.pendingAwaitedSubIds.size===0)return;const e=this.config.subAgentManager;if(!e)return;const t=Array.from(this.pendingAwaitedSubIds);p(`[ACodeRuntime] \u26D4 Cancelling ${t.length} pending awaited sub-agent(s): ${t.join(", ")}`);for(const o of t)e.abort(o).catch(s=>{p(`[ACodeRuntime] \u26A0\uFE0F manager.abort(${o}) threw: ${s instanceof Error?s.message:String(s)}`)})}cancelPendingAwaitedBgTasks(){if(this.pendingAwaitedBgTaskIds.size===0)return;const e=this.config.agentId??"unknown",t=Array.from(this.pendingAwaitedBgTaskIds);p(`[ACodeRuntime] \u26D4 Cancelling ${t.length} pending awaited background command(s): ${t.join(", ")}`),ie.interruptAwaits(e,t)}detachCurrentToolExecution(e){const t=e?this.toolSettleSlots.get(e):this.pickSettleCandidate();return!t||!t.settlable||t.settled?!1:(t.settled=!0,t.resolveSettle?.(),p(`[ACodeRuntime] \u{1F680} detachCurrentToolExecution: settling tool ${t.toolName} (toolCallId=${t.toolCallId}) to background`),!0)}pickSettleCandidate(){let e=null;for(const t of this.toolSettleSlots.values())!t.settlable||t.settled||(e===null||t.order<e.order)&&(e=t);return e}beginToolSettle(e,t,o){const s=e.toolName==="Bash",r=++this.toolSettleSeq;let i;const a=s?new Promise(l=>{i=l}):void 0;return this.toolSettleSlots.set(o,{seq:r,toolName:e.toolName,toolCallId:o,settlable:s,settled:!1,resolveSettle:i??null,order:r}),{seq:r,signal:a}}clearToolSettle(e,t){if(t!==void 0){this.toolSettleSlots.get(t)?.seq===e&&this.toolSettleSlots.delete(t);return}for(const[o,s]of this.toolSettleSlots)if(s.seq===e){this.toolSettleSlots.delete(o);return}}async stop(){if(this.state.terminated)return;this.state.terminated=!0,this.notifyRuntimeStop(),this.abortRequested=this.activeTurn,this.abortController?.abort(),this.abortController=null,this.armAbortForceSettle(),this.rejectAllQuestionWaiters("session terminated"),this.rejectAllPermissionWaiters("session terminated"),this.readFileStateTracker.clear(),this.cancelPendingAwaitedSubAgents(),this.cancelPendingAwaitedBgTasks(),await ae("ACode active turn shutdown",this.submitQueue,t=>{this.emitAcodeEvent("error",{message:t})}),await this.disconnectMcpServers();const e=this.config.agentId||"unknown";if(await ae("ACode background commands shutdown",ie.disposeAgent(e),t=>{this.emitAcodeEvent("error",{message:t})}),ae("ACode agent PTY sessions shutdown",Promise.resolve(this.config.terminalManager?.killByAgent?.(e)),t=>{this.emitAcodeEvent("error",{message:t})}),await ae("ACode Stop hooks",this.hookBus.dispatch("Stop",{reason:"session_stop"}),t=>{this.emitAcodeEvent("error",{message:t})}),this.state.terminated=!0,this.sessionCompleteEmitted||(this.sessionCompleteEmitted=!0,this.emitAcodeEvent("session_complete",{exitCode:0})),this.stopCompactionDebugLog(),this.stopSkillWatchers(),Te)try{B.promises.appendFile(tt,`[${new Date().toISOString()}] runtime stopped | ---
|
|
16
|
-
`,"utf-8").catch(()=>{})}catch{}}discoverPluginCommandDirs(){const e=this.config.pluginsDir;if(!e)return[];let t;try{t=B.readdirSync(e)}catch{return[]}const o=[];for(const s of t){if(s.startsWith("."))continue;const r=z.join(e,s);let i;try{i=B.statSync(r)}catch{continue}if(!i.isDirectory())continue;const a=z.join(r,"commands");let l;try{B.existsSync(a)&&(l=B.statSync(a))}catch{l=void 0}l?.isDirectory()&&o.push(a)}return o}resolveCommandDirs(e){const t=[...this.config.commandDirs??[],...e.commandDirs??[],...e.commands?.directories??[]];return[...new Set(t.map(o=>o.trim()).filter(Boolean))]}async runModelTurn(e,t=this.contextVersion){let o="",s,r,i,a="",l=0;this.lastIterationUsage=void 0,p(`[ACodeRuntime] \u25B6\uFE0F runModelTurn started (contextVersion: ${t})`);const u=50;for(let c=0;c<u;c+=1){if(p(`[ACodeRuntime] \u{1F504} Iteration ${c+1} starting...`),this.isTurnSuperseded(t))return p(`[ACodeRuntime] \u26A0\uFE0F Turn superseded at iteration ${c+1}, aborting`),{text:"",totalOutputTextLength:a.length};await this.injectForceMessage();const m=Date.now(),h=await this.requestChatCompletion(e),y=Date.now()-m;if(p(`[ACodeRuntime] \u{1F916} AI request completed in ${y}ms, choices: ${h.choices?.length||0}`),this.isTurnSuperseded(t))return p(`[ACodeRuntime] \u26A0\uFE0F Turn superseded after AI response at iteration ${c+1}`),{text:"",totalOutputTextLength:a.length};if(this.state.terminated)throw p(`[ACodeRuntime] Runtime terminated at iteration ${c+1}`),new Error("ACode runtime was stopped during the active turn");const C=h.choices?.[0];r=JSON.stringify(h,null,2),i=typeof C?.finish_reason=="string"?C.finish_reason:void 0;const d=C?.message,f=typeof d?.content=="string"?d.content:"",g=Array.isArray(d?.tool_calls)?ht(d.tool_calls):[];if(p(`[ACodeRuntime] AI response: content=${f?f.slice(0,100)+(f.length>100?"...":""):"(empty)"}, toolCalls=${g.length}, finishReason=${i||"(none)"}`),h.usage){const v=W(h.usage.prompt_tokens??h.usage.input_tokens),k=W(h.usage.completion_tokens??h.usage.output_tokens),M=v!==void 0,I=k!==void 0;if(te(`provider usage parsed: raw=${JSON.stringify(h.usage)} input=${v} output=${k} hasInput=${M} hasOutput=${I} iteration=${c+1}`),M||I){const x=lo(h.usage,v),D=s?.outputTokens??a.length+f.length;this.state.accumulatedCacheHitTokens+=x?.cacheHitTokens??0,this.state.accumulatedCacheMissTokens+=x?.cacheMissTokens??0,s={inputTokens:M?v:s?.inputTokens??0,outputTokens:I?k:D,cacheHitTokens:this.state.accumulatedCacheHitTokens,cacheMissTokens:this.state.accumulatedCacheMissTokens},this.lastIterationUsage=s,this.emitAcodeEvent("iteration_usage",{inputTokens:s.inputTokens,outputTokens:s.outputTokens,cacheHitTokens:s.cacheHitTokens,cacheMissTokens:s.cacheMissTokens}),l+=M?v:0,this.state.accumulatedInputTokens=M?v:this.state.accumulatedInputTokens,this.state.accumulatedOutputTokens=I?k:this.state.accumulatedOutputTokens,te(`finalUsage updated: ${JSON.stringify(s)} (cacheUsage=${JSON.stringify(x??null)})`),p(`[ACodeRuntime] \u{1F4B0} Token usage: input=${s.inputTokens}, output=${s.outputTokens} (turn accumulated cost: input=${l})`),p(x?`[ACodeRuntime] \u{1F4B0} Cache usage (provider reported): hit=${x.cacheHitTokens}, miss=${x.cacheMissTokens}`:'[ACodeRuntime] \u{1F4B0} Cache usage: provider usage \u4E2D\u65E0\u7F13\u5B58\u5B57\u6BB5\uFF08\u9762\u677F\u5C06\u663E\u793A "-"\uFF09')}}else te(`provider response has NO usage field (fallback to text-length estimate), iteration=${c+1}`);if(this.state.conversation.push({role:"assistant",content:f||null,tool_calls:g.length>0?g:void 0}),f&&(o=f,a+=f,p(`[ACodeRuntime] \u2728 Captured assistant text: ${f.slice(0,80)}...`)),g.length===0)return p(`[ACodeRuntime] \u2705 No tool calls at iteration ${c+1}, completing turn`),{text:o,usage:s,responseBody:r,finishReason:i,totalOutputTextLength:a.length};const T=g.map(v=>v.function.name).join(", ");p(`[ACodeRuntime] \u{1F527} Executing ${g.length} tool(s) at iteration ${c+1}: ${T}`);const A=Date.now();await this.handleToolCalls(g,e,t);const w=Date.now()-A;if(p(`[ACodeRuntime] \u2699\uFE0F Tool execution completed in ${w}ms at iteration ${c+1}`),this.isTurnSuperseded(t))return p(`[ACodeRuntime] \u26A0\uFE0F Turn superseded after tool execution at iteration ${c+1}`),{text:"",totalOutputTextLength:a.length};p(`[ACodeRuntime] \u{1F504} Preparing iteration ${c+2}...`)}return console.error(`[ACodeRuntime] \u{1F6A8} CRITICAL: Reached safety limit of ${u} iterations! This indicates a potential infinite loop. Forcing turn completion.`),{text:o||`ACode \u5DF2\u8FBE\u5230\u5B89\u5168\u8FED\u4EE3\u4E0A\u9650\uFF08${u}\u6B21\uFF09\uFF0C\u5F3A\u5236\u7ED3\u675F\u672C\u8F6E\u5BF9\u8BDD\u3002`,usage:s,responseBody:r,finishReason:i||"safety_iteration_limit_reached",totalOutputTextLength:a.length}}async summarizeConversationWithLLM(e){const t=this.state.provider,o=t.baseURL;if(!o)throw new Error("ACode provider baseURL is not configured for LLM summarization");const s={"Content-Type":"application/json"};t.apiKey&&(s.Authorization=`Bearer ${t.apiKey}`);const r={model:t.model,stream:!1,messages:[{role:"system",content:"You are a helpful AI assistant tasked with summarizing conversations. When asked to summarize, provide a detailed but concise summary of the conversation. Focus on information that would be helpful for continuing the conversation, especially what was in progress and what needs to happen next. Do not respond to any questions in the conversation, only output the summary."},{role:"user",content:`Below is an earlier conversation (${e.messageCount} messages) that needs to be summarized to free context space (current context limit: ${e.contextLimit} tokens).
|
|
14
|
+
`);this.initialConversation=le(V),this.state={started:!0,terminated:!1,configMcpCount:Object.keys(e.mcpServers??{}).length,skills:t,provider:i,permissions:a,mcpConnections:r,ownsMcpConnections:l,exposedTools:v,commands:B,commandRegistry:ie,conversation:le(V),accumulatedInputTokens:0,accumulatedOutputTokens:0,accumulatedCacheHitTokens:0,accumulatedCacheMissTokens:0,toolPermissionPolicy:this.state.toolPermissionPolicy},this.exposedToolsInitialized=!0,this.emitAcodeEvent("startup_phase",{phase:"session_start_hook"}),await this.hookBus.dispatch("SessionStart",{workingDirectory:this.config.workingDirectory,model:i.model,commandCount:B.length});const K=_e(i);this.emitAcodeEvent("session_started",{workingDirectory:this.config.workingDirectory,model:i.model,commandCount:B.length,estimatedContextTokens:Po(X,v),contextLimit:K.contextLimit,maxOutput:K.maxOutput,...K.inputLimit!==void 0?{inputLimit:K.inputLimit}:{},compactionThreshold:K.threshold}),this.emitAcodeEvent("mcp_status",{configPath:this.config.configPath,installedCount:this.state.configMcpCount,servers:Object.keys(e.mcpServers??{}),connectedCount:r.length,builtInToolCount:m.length,toolCount:v.length,permissionMode:a.mode,allowedToolRules:a.allow,deniedToolRules:a.deny,tools:v.map(T=>({name:T.exposedName,source:T.kind==="mcp"?`${T.serverName}.${T.toolName}`:`builtin.${T.toolName}`,description:T.description||""}))}),this.emitAcodeEvent("skill_status",{skillsDir:this.config.skillsDir,installedCount:t.length,skills:t.map(T=>({name:T.name,description:T.description,directory:T.directory})),skillGroups:o.map(T=>({dir:T.dir,exists:T.exists,skills:T.skills.map(I=>({name:I.name,description:I.description,directory:I.directory}))}))}),this.emitAcodeEvent("command_status",{action:"commands_loaded",installedCommandCount:B.length,commandDirs:G,commands:B.map(T=>({name:T.name,description:T.description}))}),this.config.initialPrompt&&await this.submit(this.config.initialPrompt),this.startCompactionDebugLog(),this.startSkillWatchers(s)}async loadAllSkills(){const e=this.config.skillsDir??"",t=F.join(this.config.workingDirectory,".acode","skills"),o=F.join(this.config.workingDirectory,".agentswork","skills"),s=new Set,i=[],a=[];for(const u of[e,t,o]){if(!u)continue;const c=F.resolve(u);if(!s.has(c)){s.add(c),i.push(await to(u));try{await O.promises.access(c),a.push(c)}catch{}}}const r=new Map;for(const u of i)for(const c of u.skills)r.set(c.name,c);return{skills:[...r.values()],skillGroups:i,watchedDirs:a}}startSkillWatchers(e){for(const t of e){let o=null;try{o=O.watch(t,{recursive:!0},()=>{this.scheduleSkillRefresh()})}catch{try{o=O.watch(t,()=>{this.scheduleSkillRefresh()})}catch{continue}}o&&(o.on("error",()=>{}),this.skillWatchers.push(o))}}scheduleSkillRefresh(){this.skillRefreshTimer&&clearTimeout(this.skillRefreshTimer),this.skillRefreshTimer=setTimeout(()=>{this.skillRefreshTimer=null,this.refreshSkills()},Z.SKILL_REFRESH_DEBOUNCE_MS)}async refreshSkills(){if(!(!this.state.started||this.state.terminated))try{const{skills:e,skillGroups:t}=await this.loadAllSkills();this.skillTool&&this.skillTool.refresh(e),this.state.skills=e,this.emitAcodeEvent("skill_status",{skillsDir:this.config.skillsDir,installedCount:e.length,skills:e.map(o=>({name:o.name,description:o.description,directory:o.directory})),skillGroups:t.map(o=>({dir:o.dir,exists:o.exists,skills:o.skills.map(s=>({name:s.name,description:s.description,directory:s.directory}))}))}),p(`[skill-watch] Skills refreshed: ${e.length} skills (${e.map(o=>o.name).join(", ")||"none"})`,{sessionId:this.config.sessionId})}catch(e){console.error("[skill-watch] Failed to refresh skills:",e instanceof Error?e.message:e)}}stopSkillWatchers(){this.skillRefreshTimer&&(clearTimeout(this.skillRefreshTimer),this.skillRefreshTimer=null);for(const e of this.skillWatchers)try{e.close()}catch{}this.skillWatchers=[]}startCompactionDebugLog(){if(!ye||this.compactionDebugTimer)return;const e=this.state.provider,{contextLimit:t,maxOutput:o,inputLimit:s,threshold:i}=_e(e);this.compactionDebugTimer=setInterval(()=>{try{const a=this.state.accumulatedInputTokens+this.state.accumulatedOutputTokens,r=z(this.state.conversation)+ue(this.state.exposedTools),l=Math.max(a,r),u=Q(this.state.conversation),c=this.state.started&&!this.state.terminated,m=[`[${new Date().toISOString()}]`,`started=${this.state.started}`,`terminated=${this.state.terminated}`,`activeTurn=${this.activeTurn}`,`conversationSize=${this.state.conversation.length}msgs`,`sizeKB=${u}KB`,`estimation=${r} tokens`,`actualInput=${this.state.accumulatedInputTokens}`,`actualOutput=${this.state.accumulatedOutputTokens}`,`actualUsed=${a}`,`contextLimit=${t}`,`inputLimit=${s??"n/a"}`,`maxOutput=${o}`,`triggerThreshold=${i}`,`remainingToTrigger=${Math.max(0,i-a)}`,`isRunning=${c}`,`shouldCompact=${c?l>=i:!1}`,"---"].join(" | ");O.promises.appendFile(nt,m+`
|
|
15
|
+
`,"utf-8").catch(()=>{})}catch{}},5e3)}stopCompactionDebugLog(){this.compactionDebugTimer&&(clearInterval(this.compactionDebugTimer),this.compactionDebugTimer=null)}async submit(e,t){const o=this.contextVersion;p(`[ACodeRuntime] \u{1F4EC} submit() queued: contextVersion=${o}, activeTurn=${this.activeTurn}, abortRequested=${this.abortRequested}, terminated=${this.state.terminated}, message.length=${e.length}`);const s=this.submitQueue.then(()=>(p(`[ACodeRuntime] \u25B6\uFE0F executeSubmit dequeued: contextVersion=${o}`),this.executeSubmitGuarded(e,o,t)));return this.submitQueue=s.catch(()=>{p("[ACodeRuntime] \u{1F4EC} submit() queued item rejected (catch swallowed)")}),p("[ACodeRuntime] \u{1F4EC} submit() returning queued promise"),s}executeSubmitGuarded(e,t,o){const s={};this.activeTurnToken=s;const i=this.executeSubmit(e,t,o),a=new Promise(r=>{this.forceSettleWaiters.set(s,r)});return i.finally(()=>{this.forceSettleWaiters.delete(s),this.activeTurnToken===s&&(this.abortForceSettleTimer&&(clearTimeout(this.abortForceSettleTimer),this.abortForceSettleTimer=null),this.activeTurnToken=null)}).catch(()=>{}),Promise.race([i,a])}armAbortForceSettle(){if(!this.activeTurn||!this.activeTurnToken)return;const e=this.activeTurnToken;this.abortForceSettleTimer&&clearTimeout(this.abortForceSettleTimer);const t=Ms();this.abortForceSettleTimer=setTimeout(()=>{this.abortForceSettleTimer=null,this.activeTurn&&this.activeTurnToken===e&&this.forceSettleActiveTurn(`turn still unsettled ${t}ms after abort()`)},t),typeof this.abortForceSettleTimer.unref=="function"&&this.abortForceSettleTimer.unref()}forceSettleActiveTurn(e){const t=this.activeTurnToken;console.error(`[ACodeRuntime] \u{1F6A8} FORCE SETTLE: active turn ignored abort() (${e}). Force-settling to unblock submitQueue; late executeSubmit continuation will be superseded.`),this.contextVersion+=1,this.activeTurn=!1,this.abortRequested=!1,this.abortController=null,this.rejectAllQuestionWaiters("aborted"),this.rejectAllPermissionWaiters("aborted"),this.cancelPendingAwaitedSubAgents(),this.cancelPendingAwaitedBgTasks(),this.notifyRuntimeStop(),this.emitAcodeEvent("error",{message:`ACode turn force-settled: ${e}`}),this.emitAcodeEvent("turn_complete",{failed:!0});const o=t?this.forceSettleWaiters.get(t):void 0;t&&this.forceSettleWaiters.delete(t),this.activeTurnToken=null,o?.()}notifyForceMessage(){this.forceMessagePending=!0,p("[ACodeRuntime] \u{1F514} Force message pending flag set")}exportConversation(){return le(this.state.conversation)}async importTranscript(e){if(!Array.isArray(e)||e.length===0)return console.log("[HIBERNATE-REPLAY][runtime] \u7A7A\u8F93\u5165\uFF0C\u8DF3\u8FC7\u91CD\u653E"),0;if(!this.state.started)return console.log(`[HIBERNATE-REPLAY][runtime] \u4F1A\u8BDD\u672A start()\uFF0C\u8DF3\u8FC7\u91CD\u653E\uFF08${e.length}\u6761\u88AB\u4E22\u5F03\uFF09`),0;const t=e.filter(u=>u.role!=="system");if(t.length===0)return 0;const{contextLimit:o,maxOutput:s,inputLimit:i}=_e(this.state.provider),a=Qt(o,i),r=z(this.state.conversation)+z(t)+ue(this.state.exposedTools);let l=t;if(console.log(`[HIBERNATE-REPLAY][runtime] \u9884\u68C0: \u53EF\u91CD\u653E=${t.length}\u6761 projectedTokens=${r} hardWindow=${a} \u9700\u8981\u9884\u538B\u7F29=${r>=a}`),r>=a){p(`[ACodeRuntime] \u{1F4E6} importTranscript pre-compaction: projected=${r} >= hardWindow=${a}`);try{const u=await be(t,o,s,{llmSummarizer:c=>this.summarizeConversationWithLLM(c)});if(u.strategy!=="none"){const c=z(t);p(`[ACodeRuntime] \u{1F4E6} importTranscript pre-compaction done: strategy=${u.strategy}, tokensBefore=${r}, tokensAfter=${c}`)}}catch(u){p(`[ACodeRuntime] \u26A0\uFE0F importTranscript pre-compaction failed, replaying raw: ${u instanceof Error?u.message:String(u)}`)}l=t}return this.state.conversation.push(...le(l)),console.log(`[HIBERNATE-REPLAY][runtime] \u91CD\u653E\u5B8C\u6210: \u62FC\u5165=${l.length}\u6761 conversation\u603B\u957F=${this.state.conversation.length}\u6761 \u603Btoken\u4F30\u7B97=${z(this.state.conversation)+ue(this.state.exposedTools)}`),this.emitAcodeEvent("context_compaction",{action:"end",compactionId:`transcript-replay-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,sizeKB:Q(this.state.conversation),estimatedTokens:z(this.state.conversation)+ue(this.state.exposedTools),actualInputTokens:this.state.accumulatedInputTokens,actualOutputTokens:this.state.accumulatedOutputTokens,messageCount:this.state.conversation.length,replayedMessageCount:l.length,model:this.state.provider.model,trigger:"hibernation_replay"}),l.length}addBuiltInTools(e){if(!this.state||!this.exposedToolsInitialized||!Array.isArray(e))return 0;const t=new Set(this.state.exposedTools.map(s=>s.exposedName)),o=[];for(const s of e)uo(s)&&(t.has(s.exposedName)||(t.add(s.exposedName),o.push(s)));return o.length===0?0:(this.state.exposedTools=[...this.state.exposedTools,...o],this.emitExposedToolsStatus(),o.length)}removeBuiltInToolsByPrefix(e){if(!this.state||!this.exposedToolsInitialized||!e)return 0;const t=this.state.exposedTools.length;this.state.exposedTools=this.state.exposedTools.filter(s=>!s.exposedName.startsWith(e));const o=t-this.state.exposedTools.length;return o>0&&this.emitExposedToolsStatus(),o}async addMcpServer(e,t){if(!this.state?.started||this.state.terminated)return{ok:!1,tools:0,error:"ACode runtime is not active"};const o=String(e??"").trim();if(!o)return{ok:!1,tools:0,error:"server name is required"};this.state.mcpConnections.find(u=>u.serverName===o)&&await this.removeMcpServer(o);const i=this.state.provider.mcpToolTimeoutMs;let a;const r=new Map;try{a=await this.connectMcpServers({[o]:t},i,r)}catch(u){return{ok:!1,tools:0,error:u instanceof Error?u.message:String(u)}}const l=a.find(u=>u.serverName===o);if(!l){const u=r.get(o);return{ok:!1,tools:0,error:u?`MCP server ${o} \u8FDE\u63A5\u5931\u8D25: ${u}`:`MCP server ${o} \u8FDE\u63A5\u5931\u8D25`}}return this.state.mcpConnections=[...this.state.mcpConnections,l],this.registerMcpCollapsedGroup(o,t,l),this.registerToolGroupsFromMcp(l),this.exposedToolsInitialized&&(this.state.exposedTools=[...this.state.exposedTools,...l.tools]),this.emitExposedToolsStatus(),{ok:!0,tools:l.tools.length,toolNames:l.tools.map(u=>u.exposedName)}}async removeMcpServer(e){const t=String(e??"").trim();if(!t)return{ok:!1,removed:!1};const o=this.state?.mcpConnections.find(s=>s.serverName===t);return o?(this.state.mcpConnections=this.state.mcpConnections.filter(s=>s.serverName!==t),this.state.exposedTools=this.state.exposedTools.filter(s=>!(s.kind==="mcp"&&s.serverName===t)),this.toolGroupManager?.unregister?.(Oe(t)),this.mcpStderrBuffers.delete(t),await se(`ACode MCP server ${t} close`,o.transport.close(),s=>{this.emitAcodeEvent("error",{message:s})},this.state.provider.mcpToolTimeoutMs),this.emitExposedToolsStatus(),{ok:!0,removed:!0}):{ok:!0,removed:!1}}addPluginCommands(e){if(!this.state||e.length===0)return;this.state.commandRegistry.addCommands(e);const t=new Set(this.state.commands.map(o=>o.name));this.state.commands=[...this.state.commands,...e.filter(o=>!t.has(o.name))]}applyPluginGroups(e,t,o){const s=new Map;for(const a of e)s.set(zt(a.name),{label:a.name,summary:a.description??"",state:a.enabled?"collapsed":"disabled"});let i=!1;for(const a of o.getAllGroups())De(a.key)&&(s.has(a.key)||(o.unregister(a.key),t.delete(a.key),i=!0));for(const[a,r]of s){const l=o.getRegistration(a);l?(l.label!==r.label||l.summary!==r.summary)&&o.register({...l,label:r.label,summary:r.summary,collapsible:!0,pluginName:r.label}):(o.register({key:a,label:r.label,summary:r.summary,collapsible:!0,pluginName:r.label}),i=!0);const u=t.get(a);u!==void 0&&u!==r.state&&(i=!0),t.set(a,r.state)}return i}async syncPluginGroups(){if(!this.config.pluginBridge||!this.toolGroupManager||!this.state)return;let e;try{e=await this.config.pluginBridge.listPlugins()}catch(o){p(`[ACodeRuntime] syncPluginGroups \u62C9\u53D6\u63D2\u4EF6\u6E05\u5355\u5931\u8D25\uFF08\u5FFD\u7565\uFF09: ${o instanceof Error?o.message:String(o)}`);return}this.applyPluginGroups(e,this.pluginGroupStates,this.toolGroupManager)&&this.injectPluginChangeNotice()}injectPluginChangeNotice(){const e="[\u63D2\u4EF6\u80FD\u529B\u53D8\u66F4]",t=this.state.conversation;for(let s=t.length-1;s>=0;s--){const i=t[s];i.role==="system"&&typeof i.content=="string"&&i.content.startsWith(e)&&t.splice(s,1)}const o=[...this.pluginGroupStates.entries()].filter(([,s])=>s!=="disabled").map(([s])=>s.slice(7));t.push({role:"system",content:`${e}
|
|
16
|
+
\u63D2\u4EF6\u80FD\u529B\u6E05\u5355\u5DF2\u66F4\u65B0\u3002\u5F53\u524D\u53EF\u7528\u63D2\u4EF6\uFF1A`+(o.length?o.map(s=>`plugin:${s}`).join("\u3001"):"\uFF08\u65E0\uFF09")+'\u3002\u9700\u8981\u67D0\u63D2\u4EF6\u7684\u5DE5\u5177\u65F6\uFF0C\u5148 expand_tools({ group:"plugin:\u63D2\u4EF6\u540D" }) \u5C55\u5F00\uFF0C\u518D\u7528 call_tool \u8C03\u7528\u3002'})}emitExposedToolsStatus(){this.emitAcodeEvent("mcp_status",{servers:this.state.mcpConnections.map(e=>e.serverName),connectedCount:this.state.mcpConnections.length,toolCount:this.state.exposedTools.length,tools:this.state.exposedTools.map(e=>({name:e.exposedName,source:e.kind==="mcp"?`${e.serverName}.${e.toolName}`:`builtin.${e.toolName}`,description:e.description||""}))})}async injectForceMessage(){if(!this.forceMessagePending)return!1;this.forceMessagePending=!1;const e=this.state.mcpConnections.find(t=>t.tools.some(o=>o.toolName==="get_message"));if(!e)return p("[ACodeRuntime] injectForceMessage: no connection with get_message found"),!1;try{const t=await x(e.client.callTool({name:"get_message",arguments:{_aws_force_inject:!0}},void 0,{timeout:1e4}),1e4,"injectForceMessage get_message"),o=W(t);let s=[];try{const l=JSON.parse(o);Array.isArray(l?.directMessages)&&(s=l.directMessages.filter(u=>u?.force===!0))}catch{return!1}if(s.length===0)return!1;const i=JSON.stringify({directMessages:s,groupMessages:[]}),a=`forced_get_msg_${Date.now()}`,r=`${e.serverName}__get_message`;return this.state.conversation.push({role:"assistant",content:null,tool_calls:[{id:a,type:"function",function:{name:r,arguments:"{}"}}]}),this.state.conversation.push({role:"tool",tool_call_id:a,content:i}),p(`[ACodeRuntime] injectForceMessage: injected ${s.length} force message(s) into conversation`),!0}catch(t){return p(`[ACodeRuntime] injectForceMessage failed: ${t}`),!1}}async resetContext(){if(!this.state.started||this.state.terminated)throw new Error("ACode runtime is not active");this.contextVersion+=1,this.abortRequested=this.activeTurn,this.abortController?.abort(),this.abortController=null,this.state.conversation=le(this.initialConversation),this.turnPromptPushed=!1,this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0}isTurnSuperseded(e){return e!==this.contextVersion}async executeSubmit(e,t,o){const s=Date.now();if(p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),p("[ACodeRuntime] \u25B6\uFE0F executeSubmit STARTED"),p(`[ACodeRuntime] Message: <redacted, length=${e.length}>`),p(`[ACodeRuntime] \u{1F522} Context version: ${t}`),p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),!this.state.started||this.state.terminated)throw console.error(`[ACodeRuntime] \u274C Runtime not active (started: ${this.state.started}, terminated: ${this.state.terminated})`),new Error("ACode runtime is not active");this.activeTurn=!0,this.abortRequested=!1;const i=this.activeTurnToken,a=e.trim();let r=!1;const l=o?.resume===!0&&this.turnPromptPushed;l||(this.turnPromptPushed=!1);try{let u;if(l)p("[ACodeRuntime] \u{1F501} resume turn: skip UserPromptSubmit hook (already ran in failed turn)"),u=[];else{p("[ACodeRuntime] \u{1F50D} Dispatching UserPromptSubmit hook...");const C=Date.now();u=await this.hookBus.dispatch("UserPromptSubmit",{prompt:a});const w=Date.now()-C;p(`[ACodeRuntime] \u2705 UserPromptSubmit hook completed in ${w}ms, results: ${u.length}`)}if(this.isTurnSuperseded(t)){p("[ACodeRuntime] \u26A0\uFE0F Turn superseded after hook, aborting");return}const c=qe(u);if(c){console.error(`[ACodeRuntime] \u274C Prompt denied by hook: ${c.message}`),this.emitAcodeEvent("error",{message:c.message||"ACode prompt was denied by hook"}),this.emitAcodeEvent("turn_complete",{failed:!0}),r=!0,p("[ACodeRuntime] \u{1F4E4} Emitted turn_complete (failed=true) due to hook deny");return}p("[ACodeRuntime] \u{1F50D} Expanding command if any...");const m=this.state.commandRegistry.expand(a),h=m?.prompt.trim()||a,y=m?.command.allowedTools;if(m&&!l){p(`[ACodeRuntime] \u{1F4CB} Slash command detected: ${m.command.name}`),this.emitAcodeEvent("command_status",{action:"slash_command",command:m.command.name,arguments:m.argumentsText,sourcePath:m.command.sourcePath});const C=m.command.plugin;C&&p(`[ACodeRuntime] \u{1F4CB} Slash command belongs to plugin: ${C}`)}if(!this.state.provider.baseURL){p("[ACodeRuntime] \u26A0\uFE0F No provider baseURL configured, using fallback response");const C=is(h,this.state);this.emitAcodeEvent("assistant_delta",{text:C}),this.emitAcodeEvent("turn_complete",{usage:{inputTokens:h.length,outputTokens:C.length}}),r=!0,p("[ACodeRuntime] \u{1F4E4} Emitted turn_complete (fallback mode)");return}if(l){if(p("[ACodeRuntime] \u{1F501} resume turn: user message already in conversation, skipping push"),this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded before resume runModelTurn, aborting");return}}else{p("[ACodeRuntime] \u{1F4CE} Parsing attachment message...");const C=Ee(h);if(C.hasImages&&!this.state.provider.supportsImageInput)throw console.error("[ACodeRuntime] \u274C Image input not supported by current provider"),new Error("ACode provider/model is not configured for image input. Enable provider.default.models[model].modalities.input including 'image' or options.supportsImageInput=true.");p("[ACodeRuntime] \u{1F5BC}\uFE0F Materializing image content if any...");const w=await lt(C.content);if(this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded after materializing content, aborting");return}p("[ACodeRuntime] \u{1F4AC} Adding user message to conversation"),this.state.conversation.push({role:"user",content:w}),this.turnPromptPushed=!0,p(`[ACodeRuntime] Conversation size: ${this.state.conversation.length} messages`)}p("[ACodeRuntime] Starting runModelTurn...");const _=Date.now(),d=await this.runModelTurn(y,t),f=Date.now()-_;if(p(`[ACodeRuntime] \u2705 runModelTurn completed in ${f}ms`),p(`[ACodeRuntime] \u{1F4DD} Result: text=${d.text?d.text.slice(0,80)+(d.text.length>80?"...":""):"(empty)"}, finishReason=${d.finishReason||"(none)"}`),this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded after runModelTurn, aborting");return}d.text&&!this.state.provider.stream&&(p("[ACodeRuntime] \u{1F4E4} Emitting assistant_delta (non-streaming mode)"),this.emitAcodeEvent("assistant_delta",{text:d.text})),p("[ACodeRuntime] \u{1F4E4} Emitting turn_complete..."),this.emitAcodeEvent("turn_complete",{usage:d.usage??{inputTokens:h.length,outputTokens:d.totalOutputTextLength??d.text.length},...d.responseBody?{responseBody:d.responseBody}:{},...d.finishReason?{finishReason:d.finishReason}:{}}),oe(`turn_complete emitted: usage=${JSON.stringify(d.usage??{inputTokens:h.length,outputTokens:d.totalOutputTextLength??d.text.length})} (providerUsage=${d.usage?"real":"FALLBACK-text-length"}) textLength=${d.text.length} totalOutputTextLength=${d.totalOutputTextLength}`),p(`[ACodeRuntime] \u{1F4B0} Current context usage: input=${this.state.accumulatedInputTokens}, output=${this.state.accumulatedOutputTokens}`+(d.usage?"":" (estimated, no provider usage)")),r=!0,this.turnPromptPushed=!1;const g=Date.now()-s;p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),p(`[ACodeRuntime] \u2705 executeSubmit COMPLETED in ${g}ms`),p("[ACodeRuntime] \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550")}catch(u){const c=Date.now()-s;if(console.error(`[ACodeRuntime] \u274C executeSubmit FAILED after ${c}ms:`,u instanceof Error?u.message:String(u)),console.error("[ACodeRuntime] \u{1F50D} Error stack:",u instanceof Error?u.stack:"(no stack)"),this.isTurnSuperseded(t)){p("[ACodeRuntime] \uFE0F Turn superseded in error handler, skipping recovery");return}if(this.abortRequested){p("[ACodeRuntime] \u26A0\uFE0F Abort requested, emitting aborted turn_complete"),this.emitAcodeEvent("turn_complete",{aborted:!0}),r=!0;return}p("[ACodeRuntime] Emitting error and failed turn_complete..."),this.emitAcodeEvent("error",{message:u instanceof Error?u.message:String(u)}),this.emitAcodeEvent("turn_complete",{failed:!0}),r=!0}finally{const u=Date.now()-s;p(`[ACodeRuntime] \u{1F51A} executeSubmit finally block entered after ${u}ms`),p(`[ACodeRuntime] \u{1F50D} turnCompleted=${r}, activeTurn=${this.activeTurn}, abortRequested=${this.abortRequested}, contextVersion=${t}, currentContextVersion=${this.contextVersion}`),this.activeTurnToken===i&&(this.activeTurn=!1,this.abortRequested=!1),!r&&!this.isTurnSuperseded(t)&&(console.error(`[ACodeRuntime] \u{1F6A8} CRITICAL: executeSubmit finally block reached without turn_complete event! Duration: ${u}ms. This indicates a code path that should have emitted turn_complete but didn't. Forcing failed turn_complete to prevent state stuck.`),this.emitAcodeEvent("error",{message:"ACode turn completed without proper completion event (internal error)"}),this.emitAcodeEvent("turn_complete",{failed:!0}),p("[ACodeRuntime] Emergency turn_complete (failed=true) emitted"))}}async abort(){const e=this.abortController!==null,t=this.activeTurn;this.abortRequested=this.activeTurn,this.abortController?(p(`[ACodeRuntime] \u26D4 abort() calling abortController.abort() (activeTurn=${t})`),this.abortController.abort()):console.warn(`[ACodeRuntime] \u26D4 abort() called but abortController is null! activeTurn=${t}, abortRequested will be set to ${t}. This means abort() CANNOT cancel any running fetch/stream \u2014 if a built-in tool is executing, it will NOT be interrupted.`),this.abortController=null,this.rejectAllQuestionWaiters("aborted"),this.rejectAllPermissionWaiters("aborted"),this.cancelPendingAwaitedSubAgents(),this.cancelPendingAwaitedBgTasks(),this.notifyRuntimeStop(),this.armAbortForceSettle()}buildAskUserQuestionDeps(){return{onQuestion:(e,t)=>{this.emitAcodeEvent("question_request",{toolCallId:e,questions:t})},waitForAnswer:(e,t)=>new Promise((o,s)=>{this.questionWaiters.set(e,{resolve:o,reject:s,contextVersion:t})})}}async buildAllocatableToolList(){const e=this.state?.exposedTools??[],t=this.config.subAgentSuiteTools??[];let o=[];if(this.config.subAgentSuiteToolProvider)try{o=await this.config.subAgentSuiteToolProvider([])}catch{o=[]}return pe(e,pe(t,o))}buildSubAgentToolDeps(e){const t=this.config.agentId??"unknown",o=this.config.sessionId,s=this.config.workingDirectory,i=this.config;return{spawnSubAgent:async a=>{let r=this.config.subAgentSuiteTools;if(a.allowedTools&&a.allowedTools.length>0&&this.config.subAgentSuiteToolProvider)try{const u=await this.config.subAgentSuiteToolProvider(a.allowedTools);u.length>0&&(r=r?pe(r,u):u)}catch{}const l=await e.spawn({parentAgentId:t,parentSessionId:o,subagentType:a.subagentType,description:a.description,prompt:a.prompt,...a.allowedTools&&a.allowedTools.length>0?{allowedTools:a.allowedTools}:{},workingDirectory:s,parentConfig:{...i,sharedMcpConnections:this.state?.mcpConnections,...r&&r.length>0?{subAgentSuiteTools:r}:{}}});return await e.start(l),{subId:l}},listSubAgentToolCatalog:async()=>ze(await this.buildAllocatableToolList()),listSubAgents:async()=>{const a=e.listByParent(t),r=[],l=[];for(const u of a){const c={subId:u.subId,subagentType:u.subagentType,description:u.description,status:u.status,startedAt:u.startedAt,endedAt:u.endedAt,durationMs:u.durationMs};u.status==="pending"||u.status==="running"?r.push(c):l.push(c)}return l.sort((u,c)=>(c.endedAt??0)-(u.endedAt??0)),{running:r,history:l}},getSubAgentStatus:async a=>{const r=e.getSubAgent(a);return r?{subId:r.subId,subagentType:r.subagentType,description:r.description,status:r.status,startedAt:r.startedAt,endedAt:r.endedAt,durationMs:r.durationMs,prompt:r.prompt,result:r.result,errorMessage:r.errorMessage,contextSnapshot:r.contextSnapshot}:null},awaitSubAgents:async(a,r)=>{const l=new Set;for(const u of a)this.pendingAwaitedSubIds.add(u),l.add(u);try{return await e.awaitCompletion(a,r)}finally{for(const u of l)this.pendingAwaitedSubIds.delete(u)}},abortSubAgent:a=>e.abort(a)}}buildAwaitCompleteDeps(){const e=this.config.agentId??"unknown",t=this.config.subAgentManager;return{agentId:e,awaitSubAgents:t?(o,s)=>{for(const i of o)this.pendingAwaitedSubIds.add(i);try{return this.buildSubAgentToolDeps(t).awaitSubAgents(o,s)}finally{for(const i of o)this.pendingAwaitedSubIds.delete(i)}}:null,awaitBgCommands:(o,s)=>{for(const i of o)this.pendingAwaitedBgTaskIds.add(i);try{return ae.awaitCompletion(e,o,s)}finally{for(const i of o)this.pendingAwaitedBgTaskIds.delete(i)}}}}submitQuestionAnswer(e,t){const o=this.questionWaiters.get(e);return o?(this.questionWaiters.delete(e),this.isTurnSuperseded(o.contextVersion)?(o.reject(new Error("turn superseded before user answered")),!1):(o.resolve(t),!0)):!1}rejectAllQuestionWaiters(e){if(this.questionWaiters.size!==0){for(const[,t]of this.questionWaiters)t.reject(new Error(`ask_user_question ${e}`));this.questionWaiters.clear()}}createToolPermissionController(){return{getLevel:e=>this.state.toolPermissionPolicy[e]??"allow",requestPermission:(e,t)=>this.requestToolPermission(e,t)}}requestToolPermission(e,t){const o=Ct();return new Promise(s=>{this.permissionWaiters.set(o,{capability:e,detail:t,resolve:s}),this.emitAcodeEvent("tool_permission_request",{requestId:o,capability:e,agentId:this.config.agentId,...t})})}submitToolPermissionDecision(e,t){const o=this.permissionWaiters.get(e);return o?(this.permissionWaiters.delete(e),o.resolve(t),!0):!1}rejectAllPermissionWaiters(e){if(this.permissionWaiters.size!==0){for(const[,t]of this.permissionWaiters)t.resolve(!1);this.permissionWaiters.clear(),D("tool-permission",`waiters rejected: ${e}`)}}setToolPermissionPolicy(e){this.state.toolPermissionPolicy={...this.state.toolPermissionPolicy,...e}}cancelPendingAwaitedSubAgents(){if(this.pendingAwaitedSubIds.size===0)return;const e=this.config.subAgentManager;if(!e)return;const t=Array.from(this.pendingAwaitedSubIds);p(`[ACodeRuntime] \u26D4 Cancelling ${t.length} pending awaited sub-agent(s): ${t.join(", ")}`);for(const o of t)e.abort(o).catch(s=>{p(`[ACodeRuntime] \u26A0\uFE0F manager.abort(${o}) threw: ${s instanceof Error?s.message:String(s)}`)})}cancelPendingAwaitedBgTasks(){if(this.pendingAwaitedBgTaskIds.size===0)return;const e=this.config.agentId??"unknown",t=Array.from(this.pendingAwaitedBgTaskIds);p(`[ACodeRuntime] \u26D4 Cancelling ${t.length} pending awaited background command(s): ${t.join(", ")}`),ae.interruptAwaits(e,t)}detachCurrentToolExecution(e){const t=e?this.toolSettleSlots.get(e):this.pickSettleCandidate();return!t||!t.settlable||t.settled?!1:(t.settled=!0,t.resolveSettle?.(),p(`[ACodeRuntime] \u{1F680} detachCurrentToolExecution: settling tool ${t.toolName} (toolCallId=${t.toolCallId}) to background`),!0)}pickSettleCandidate(){let e=null;for(const t of this.toolSettleSlots.values())!t.settlable||t.settled||(e===null||t.order<e.order)&&(e=t);return e}beginToolSettle(e,t,o){const s=e.toolName==="Bash",i=++this.toolSettleSeq;let a;const r=s?new Promise(l=>{a=l}):void 0;return this.toolSettleSlots.set(o,{seq:i,toolName:e.toolName,toolCallId:o,settlable:s,settled:!1,resolveSettle:a??null,order:i}),{seq:i,signal:r}}clearToolSettle(e,t){if(t!==void 0){this.toolSettleSlots.get(t)?.seq===e&&this.toolSettleSlots.delete(t);return}for(const[o,s]of this.toolSettleSlots)if(s.seq===e){this.toolSettleSlots.delete(o);return}}async stop(){if(this.state.terminated)return;this.state.terminated=!0,this.notifyRuntimeStop(),this.abortRequested=this.activeTurn,this.abortController?.abort(),this.abortController=null,this.armAbortForceSettle(),this.rejectAllQuestionWaiters("session terminated"),this.rejectAllPermissionWaiters("session terminated"),this.readFileStateTracker.clear(),this.cancelPendingAwaitedSubAgents(),this.cancelPendingAwaitedBgTasks(),await se("ACode active turn shutdown",this.submitQueue,t=>{this.emitAcodeEvent("error",{message:t})}),await this.disconnectMcpServers();const e=this.config.agentId||"unknown";if(await se("ACode background commands shutdown",ae.disposeAgent(e),t=>{this.emitAcodeEvent("error",{message:t})}),se("ACode agent PTY sessions shutdown",Promise.resolve(this.config.terminalManager?.killByAgent?.(e)),t=>{this.emitAcodeEvent("error",{message:t})}),await se("ACode Stop hooks",this.hookBus.dispatch("Stop",{reason:"session_stop"}),t=>{this.emitAcodeEvent("error",{message:t})}),this.state.terminated=!0,this.sessionCompleteEmitted||(this.sessionCompleteEmitted=!0,this.emitAcodeEvent("session_complete",{exitCode:0})),this.stopCompactionDebugLog(),this.stopSkillWatchers(),ye)try{O.promises.appendFile(nt,`[${new Date().toISOString()}] runtime stopped | ---
|
|
17
|
+
`,"utf-8").catch(()=>{})}catch{}}discoverPluginCommandDirs(){const e=this.config.pluginsDir;if(!e)return[];let t;try{t=O.readdirSync(e)}catch{return[]}const o=[];for(const s of t){if(s.startsWith("."))continue;const i=F.join(e,s);let a;try{a=O.statSync(i)}catch{continue}if(!a.isDirectory())continue;const r=F.join(i,"commands");let l;try{O.existsSync(r)&&(l=O.statSync(r))}catch{l=void 0}l?.isDirectory()&&o.push(r)}return o}resolveCommandDirs(e){const t=[...this.config.commandDirs??[],...e.commandDirs??[],...e.commands?.directories??[]];return[...new Set(t.map(o=>o.trim()).filter(Boolean))]}async runModelTurn(e,t=this.contextVersion){let o="",s,i,a,r="",l=0;this.lastIterationUsage=void 0,p(`[ACodeRuntime] \u25B6\uFE0F runModelTurn started (contextVersion: ${t})`);const u=50;for(let c=0;c<u;c+=1){if(p(`[ACodeRuntime] \u{1F504} Iteration ${c+1} starting...`),this.isTurnSuperseded(t))return p(`[ACodeRuntime] \u26A0\uFE0F Turn superseded at iteration ${c+1}, aborting`),{text:"",totalOutputTextLength:r.length};await this.injectForceMessage();const m=Date.now(),h=await this.requestChatCompletion(e),y=Date.now()-m;if(p(`[ACodeRuntime] \u{1F916} AI request completed in ${y}ms, choices: ${h.choices?.length||0}`),this.isTurnSuperseded(t))return p(`[ACodeRuntime] \u26A0\uFE0F Turn superseded after AI response at iteration ${c+1}`),{text:"",totalOutputTextLength:r.length};if(this.state.terminated)throw p(`[ACodeRuntime] Runtime terminated at iteration ${c+1}`),new Error("ACode runtime was stopped during the active turn");const _=h.choices?.[0];i=JSON.stringify(h,null,2),a=typeof _?.finish_reason=="string"?_.finish_reason:void 0;const d=_?.message,f=typeof d?.content=="string"?d.content:"",g=Array.isArray(d?.tool_calls)?Tt(d.tool_calls):[];if(p(`[ACodeRuntime] AI response: content=${f?f.slice(0,100)+(f.length>100?"...":""):"(empty)"}, toolCalls=${g.length}, finishReason=${a||"(none)"}`),h.usage){const M=j(h.usage.prompt_tokens??h.usage.input_tokens),$=j(h.usage.completion_tokens??h.usage.output_tokens),S=M!==void 0,v=$!==void 0;if(oe(`provider usage parsed: raw=${JSON.stringify(h.usage)} input=${M} output=${$} hasInput=${S} hasOutput=${v} iteration=${c+1}`),S||v){const E=fo(h.usage,M),G=s?.outputTokens??r.length+f.length;this.state.accumulatedCacheHitTokens+=E?.cacheHitTokens??0,this.state.accumulatedCacheMissTokens+=E?.cacheMissTokens??0,s={inputTokens:S?M:s?.inputTokens??0,outputTokens:v?$:G,cacheHitTokens:this.state.accumulatedCacheHitTokens,cacheMissTokens:this.state.accumulatedCacheMissTokens},this.lastIterationUsage=s,this.emitAcodeEvent("iteration_usage",{inputTokens:s.inputTokens,outputTokens:s.outputTokens,cacheHitTokens:s.cacheHitTokens,cacheMissTokens:s.cacheMissTokens}),l+=S?M:0,this.state.accumulatedInputTokens=S?M:this.state.accumulatedInputTokens,this.state.accumulatedOutputTokens=v?$:this.state.accumulatedOutputTokens,oe(`finalUsage updated: ${JSON.stringify(s)} (cacheUsage=${JSON.stringify(E??null)})`),p(`[ACodeRuntime] \u{1F4B0} Token usage: input=${s.inputTokens}, output=${s.outputTokens} (turn accumulated cost: input=${l})`),p(E?`[ACodeRuntime] \u{1F4B0} Cache usage (provider reported): hit=${E.cacheHitTokens}, miss=${E.cacheMissTokens}`:'[ACodeRuntime] \u{1F4B0} Cache usage: provider usage \u4E2D\u65E0\u7F13\u5B58\u5B57\u6BB5\uFF08\u9762\u677F\u5C06\u663E\u793A "-"\uFF09')}}else oe(`provider response has NO usage field (fallback to text-length estimate), iteration=${c+1}`);if(this.state.conversation.push({role:"assistant",content:f||null,tool_calls:g.length>0?g:void 0}),f&&(o=f,r+=f,p(`[ACodeRuntime] \u2728 Captured assistant text: ${f.slice(0,80)}...`)),g.length===0)return p(`[ACodeRuntime] \u2705 No tool calls at iteration ${c+1}, completing turn`),{text:o,usage:s,responseBody:i,finishReason:a,totalOutputTextLength:r.length};const C=g.map(M=>M.function.name).join(", ");p(`[ACodeRuntime] \u{1F527} Executing ${g.length} tool(s) at iteration ${c+1}: ${C}`);const w=Date.now();await this.handleToolCalls(g,e,t);const b=Date.now()-w;if(p(`[ACodeRuntime] \u2699\uFE0F Tool execution completed in ${b}ms at iteration ${c+1}`),this.isTurnSuperseded(t))return p(`[ACodeRuntime] \u26A0\uFE0F Turn superseded after tool execution at iteration ${c+1}`),{text:"",totalOutputTextLength:r.length};p(`[ACodeRuntime] \u{1F504} Preparing iteration ${c+2}...`)}return console.error(`[ACodeRuntime] \u{1F6A8} CRITICAL: Reached safety limit of ${u} iterations! This indicates a potential infinite loop. Forcing turn completion.`),{text:o||`ACode \u5DF2\u8FBE\u5230\u5B89\u5168\u8FED\u4EE3\u4E0A\u9650\uFF08${u}\u6B21\uFF09\uFF0C\u5F3A\u5236\u7ED3\u675F\u672C\u8F6E\u5BF9\u8BDD\u3002`,usage:s,responseBody:i,finishReason:a||"safety_iteration_limit_reached",totalOutputTextLength:r.length}}async summarizeConversationWithLLM(e){const t=this.state.provider,o=t.baseURL;if(!o)throw new Error("ACode provider baseURL is not configured for LLM summarization");const s={"Content-Type":"application/json"};t.apiKey&&(s.Authorization=`Bearer ${t.apiKey}`);const i={model:t.model,stream:!1,messages:[{role:"system",content:"You are a helpful AI assistant tasked with summarizing conversations. When asked to summarize, provide a detailed but concise summary of the conversation. Focus on information that would be helpful for continuing the conversation, especially what was in progress and what needs to happen next. Do not respond to any questions in the conversation, only output the summary."},{role:"user",content:`Below is an earlier conversation (${e.messageCount} messages) that needs to be summarized to free context space (current context limit: ${e.contextLimit} tokens).
|
|
17
18
|
|
|
18
19
|
--- Conversation Start ---
|
|
19
20
|
${e.conversationText}
|
|
20
21
|
--- Conversation End ---
|
|
21
22
|
|
|
22
|
-
`+zt}]},i=new AbortController;this.abortController=i;let a=!1;try{return await R((async()=>{const l=await R(fetch(_e(o),{method:"POST",headers:s,body:JSON.stringify(r),signal:i.signal}),t.requestTotalTimeoutMs,"ACode LLM summarization",()=>{a=!0,i.abort()});if(!l.ok){const m=await R(l.text(),t.requestTimeoutMs,"ACode LLM summarization error response body",()=>{a=!0,i.abort()}),h=m.length>500?`${m.slice(0,500)}...(truncated, total ${m.length} chars)`:m;throw new Error(`ACode LLM summarization request failed: ${l.status} ${h}`)}const c=(await R(l.json(),t.requestTimeoutMs,"ACode LLM summarization response body",()=>{a=!0,i.abort()})).choices?.[0]?.message?.content;if(!c||c.trim().length===0)throw new Error("ACode LLM summarization returned empty content");return c})(),t.requestTotalTimeoutMs,"ACode LLM summarization total",()=>{i.abort()})}catch(l){throw l instanceof Error&&l.name==="AbortError"?a?new Error(`ACode LLM summarization timed out after ${t.requestTimeoutMs}ms`):new Error("ACode LLM summarization aborted"):l}finally{this.abortController===i&&(this.abortController=null)}}async requestChatCompletion(e){const t=this.state.provider,o=t.baseURL;if(!o)throw new Error("ACode provider baseURL is not configured");const{contextLimit:s,maxOutput:r,inputLimit:i,threshold:a}=Ce(t),l=this.state.accumulatedInputTokens+this.state.accumulatedOutputTokens,u=j(this.state.conversation)+ce(this.state.exposedTools),c=Math.max(l,u),m=c>=a;if(p(`[ACodeRuntime] \u{1F50D} Compaction check: actualUsed=${l}, estimation=${u}, effectiveUsed=${c}, contextLimit=${s}, inputLimit=${i??"n/a"}, maxOutput=${r}, triggerThreshold=${a}, shouldCompact=${m}`),m){const b=Y(this.state.conversation),E=`compaction-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;this.emitAcodeEvent("context_compaction",{action:"start",compactionId:E,sizeKB:b,estimatedTokens:u,actualInputTokens:this.state.accumulatedInputTokens,actualOutputTokens:this.state.accumulatedOutputTokens,messageCount:this.state.conversation.length,model:t.model,trigger:l>=a?"actual_usage":"estimated_overflow"}),this.emitCompactionPhase(E,"detect","\u68C0\u6D4B\u5230\u4E0A\u4E0B\u6587\u6EA2\u51FA",`\u6709\u6548\u7528\u91CF: ${c} tokens, \u9608\u503C: ${a}, \u89E6\u53D1: ${l>=a?"\u5B9E\u9645\u7528\u91CF":"\u4F30\u7B97\u6EA2\u51FA"}`);const N=await Ae(this.state.conversation,s,r,{llmSummarizer:$=>this.summarizeConversationWithLLM($),onPhase:$=>this.handleCompactionPhase(E,$),actualUsedTokens:c}),L=Y(this.state.conversation);this.emitAcodeEvent("context_compaction",{action:"end",compactionId:E,sizeBeforeKB:b,sizeAfterKB:L,freedKB:Math.round((b-L)*10)/10,strategy:N.strategy,freedTokens:N.freedTokens,tokensBefore:N.tokensBefore,tokensAfter:N.tokensAfter,prunedToolMessages:N.prunedToolMessages,summarizedMessages:N.summarizedMessages,messageCountBefore:N.messageCountBefore,messageCountAfter:N.messageCountAfter}),this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0,N.summarizedMessages>0&&await this.rebuildRuntimeResourcesMessage()}const h=wo(s);let y=bo()||m;if(!y){const b=Oe(this.state.conversation);b>Ze?y=!0:b>h&&p(`[ACodeRuntime] \u23ED\uFE0F Byte-cap truncation skipped: body ${b}B > cap ${h}B, but effectiveUsed ${c} < threshold ${a} (aligned with frontend compression marker; rely on provider 413/overflow fallback)`)}let C=ct,d=0,f=0,g=0;if(y&&(d=Y(this.state.conversation),f=j(this.state.conversation),g=this.state.conversation.length,C=await ts(this.state.conversation,h,s,r,b=>this.summarizeConversationWithLLM(b),()=>this.rebuildRuntimeResourcesMessage())),C.truncatedCount>0){const b=`compaction-bytes-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;this.emitAcodeEvent("context_compaction",{action:"start",compactionId:b,sizeKB:d,estimatedTokens:f,actualInputTokens:this.state.accumulatedInputTokens,actualOutputTokens:this.state.accumulatedOutputTokens,messageCount:g,model:t.model,trigger:"request_body_byte_limit"}),this.emitCompactionPhase(b,"byte_limit_detect","\u68C0\u6D4B\u5230\u8BF7\u6C42\u4F53\u8D85\u9650",`\u8BF7\u6C42\u4F53\u8D85\u8FC7 ${h} \u5B57\u8282\u4E0A\u9650\uFF0C\u5373\u5C06\u88C1\u526A\u5386\u53F2`,"request_body_byte_limit");const E=Y(this.state.conversation),N=j(this.state.conversation);this.emitAcodeEvent("context_compaction",{action:"end",compactionId:b,sizeBeforeKB:d,sizeAfterKB:E,freedKB:Math.round((d-E)*10)/10,strategy:C.strategy,freedTokens:C.freedTokens,tokensBefore:f,tokensAfter:N,prunedToolMessages:C.prunedToolMessages,summarizedMessages:C.summarizedMessages,messageCountBefore:g,messageCountAfter:this.state.conversation.length,trigger:"request_body_byte_limit"}),this.emitAcodeEvent("warning",{message:`Conversation truncated: removed ${C.truncatedCount} older messages to fit within ${h} byte request size limit`}),this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0}const T=()=>{const b=es(this.state.conversation);(b.orphanToolMessages>0||b.repairedToolCalls>0||b.downgradedAssistants>0||b.relocatedInterruptions>0||b.reorderedToolResults>0)&&(p(`[ACodeRuntime] \u{1FA79} sanitizeToolCallSequence: orphanTools=${b.orphanToolMessages} repairedCalls=${b.repairedToolCalls} downgradedAssistants=${b.downgradedAssistants} relocatedInterruptions=${b.relocatedInterruptions} reorderedToolResults=${b.reorderedToolResults}`),this.emitAcodeEvent("warning",{message:`\u68C0\u6D4B\u5230\u5DE5\u5177\u8C03\u7528\u5E8F\u5217\u4E0D\u5B8C\u6574\uFF0C\u5DF2\u81EA\u52A8\u4FEE\u590D\uFF08\u5B64\u513F\u5DE5\u5177\u7ED3\u679C ${b.orphanToolMessages} \u6761\u3001\u8865\u5360\u4F4D\u7ED3\u679C ${b.repairedToolCalls} \u4E2A\u3001\u964D\u7EA7 assistant \u6D88\u606F ${b.downgradedAssistants} \u6761\u3001\u56DE\u6B63\u7A7F\u63D2\u6D88\u606F ${b.relocatedInterruptions} \u6761\u3001\u56DE\u6B63\u9519\u5E8F\u7ED3\u679C ${b.reorderedToolResults} \u6761\uFF09\uFF0C\u907F\u514D provider 400 (tool_call_sequence_broken) \u6B7B\u5FAA\u73AF\u3002`}))};T();const A=this.state.exposedTools.filter(b=>this.toolGroupManager&&!this.toolGroupManager.shouldShowToLLMByName(b.exposedName)?!1:Ko(b,this.state.permissions,e)),w=()=>{const b={model:t.model,messages:this.state.conversation,stream:t.stream};return t.effort&&(b.reasoning_effort=t.effort),t.stream&&(b.stream_options={include_usage:!0}),A.length>0&&(b.tools=mt(A),b.tool_choice="auto"),b},v="POST",k=Yo(o),M=ds(t),I=ps(M),x=b=>{const E=JSON.stringify(b,null,2);p(`[ACodeRuntime] \u2605\u2605\u2605 DEBUG \u2605\u2605\u2605 emitAiRequest: \u53D1\u9001\u8BF7\u6C42 model=${t.model}, endpoint=${k}, messageCount=${this.state.conversation.length}`),this.emitAcodeEvent("ai_request",{model:t.model,endpoint:k,requestMethod:v,requestUrl:k,requestHeaders:I,stream:t.stream,messageCount:this.state.conversation.length,toolCount:A.length,lastUserMessagePreview:Qo(this.state.conversation),estimatedContextTokens:$e(E),...this.lastIterationUsage?{lastUsage:this.lastIterationUsage}:{}}),te(`ai_request emitted: model=${t.model} stream=${t.stream} messageCount=${this.state.conversation.length} toolCount=${A.length} estimatedContextTokens=${$e(E)} lastUsage=${JSON.stringify(this.lastIterationUsage??null)}`)};let D=w();x(D);const q=t.requestRetryCount===Me,se=q?1/0:t.requestRetryCount+1;let Z=!1;for(let b=1;q||b<=se;b+=1){this.state.terminated||(this.abortRequested=!1),b>1&&x(D);const E=new AbortController;this.abortController=E;let N=!1;try{const L=await R((async()=>{const J=t.stream?t.requestFirstByteTimeoutMs:t.requestTotalTimeoutMs,F=await R(fetch(_e(o),{method:v,headers:M,body:JSON.stringify(D),signal:E.signal}),J,"ACode provider request",()=>{N=!0,E.abort()});if(!F.ok){const _=await R(F.text(),t.requestStreamIdleTimeoutMs,"ACode provider error response body",()=>{N=!0,E.abort()}),P=_.length>500?`${_.slice(0,500)}...(truncated, total ${_.length} chars)`:_,ne=xe(this.state.conversation,F.status,_)??"";throw new Error(`ACode provider request failed: ${F.status} ${P}${ne}`)}const Q=F.headers?.get?.("content-type")||"";if(t.stream&&Q.toLowerCase().includes("text/event-stream"))return await this.readChatCompletionStream(F,t.requestStreamIdleTimeoutMs,E,t.requestFirstByteTimeoutMs);const V=await R(F.json(),t.requestStreamIdleTimeoutMs,"ACode provider response body",()=>{N=!0,E.abort()});if(!S(V))return this.emitAcodeEvent("warning",{message:`ACode provider returned invalid response format (expected object, got ${typeof V}); treating as empty response`}),{};const H=dt(V);if(H){const _=xe(this.state.conversation,F.status,H)??"";throw new Error(`ACode provider request failed: ${H}${_}`)}return V})(),t.requestTotalTimeoutMs,"ACode provider request total",()=>{E.abort()}),$=Kt({finishReason:L.choices?.[0]?.finish_reason,stopReason:L.choices?.[0]?.stop_reason});if($.kind==="context_overflow"){if(!Z&&await this.compactForContextOverflow({contextLimit:s,trigger:"finish_reason_context_overflow",warningMessage:`ACode provider reported context overflow via ${$.source} (${$.marker}); summarized older messages and rebuilt the request instead of accepting the truncated payload`})>0){Z=!0,T(),D=w(),x(D),b-=1;continue}this.emitAcodeEvent("warning",{message:`ACode provider reported context overflow via ${$.source} (${$.marker}), but no further compaction was possible`})}return L}catch(L){let $;if(L instanceof Error&&L.name==="AbortError"?N?$=new Error(`ACode provider request timed out (firstByte=${t.requestFirstByteTimeoutMs}ms, idle=${t.requestStreamIdleTimeoutMs}ms)`):this.state.terminated?$=new Error("ACode provider request aborted by user"):$=new Error(`ACode provider request timed out (watchdog abort, firstByte=${t.requestFirstByteTimeoutMs}ms, idle=${t.requestStreamIdleTimeoutMs}ms)`):$=L instanceof Error?L:new Error(String(L)),!Z&&!this.abortRequested&&!this.state.terminated&&(be(L)||be($))){if(await this.compactForContextOverflow({contextLimit:s,trigger:"provider_overflow_error",warningMessage:"ACode provider context limit exceeded; summarized older messages and rebuilt the request instead of retrying the same oversized payload"})===0)throw $;Z=!0,T(),D=w(),x(D),b-=1;continue}if(!((q||b<se)&&!this.abortRequested&&!this.state.terminated&&ss($)))throw $;this.emitAcodeEvent("error",{message:`ACode provider request attempt ${b}${q?"":`/${se}`} failed; retrying: ${$.message}`,nonFatal:!0});const F=Math.min(fo,ho*Math.pow(2,b-1)),Q=Math.random()*1e3;await Eo(F+Q)}finally{this.abortController===E&&(this.abortController=null)}}throw new Error("ACode provider request failed after all retries")}async compactForContextOverflow(e){const{contextLimit:t,trigger:o,warningMessage:s}=e,r=Y(this.state.conversation),i=j(this.state.conversation),a=this.state.conversation.length,l=`compaction-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;this.emitAcodeEvent("context_compaction",{action:"start",compactionId:l,sizeKB:r,estimatedTokens:i,messageCount:a,model:this.state.provider.model,trigger:o}),this.emitCompactionPhase(l,"detect","\u68C0\u6D4B\u5230\u4E0A\u4E0B\u6587\u6EA2\u51FA",`\u6D88\u606F: ${a}\u6761, \u5927\u5C0F: ${r}KB`),this.emitCompactionPhase(l,"prune","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA","\u6B63\u5728\u88C1\u526A...");const u=Jt(this.state.conversation,t);this.emitCompactionPhase(l,"prune_done","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA",`\u88C1\u526A\u4E86 ${u.prunedCount} \u6761\u5DE5\u5177\u8F93\u51FA, \u91CA\u653E ${u.freedTokens} tokens`),this.emitCompactionPhase(l,"summarize","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD","\u6B63\u5728\u6458\u8981...");const c=await De(this.state.conversation,{llmSummarizer:d=>this.summarizeConversationWithLLM(d),contextLimit:t});this.emitCompactionPhase(l,"summarize_done","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD",`\u6458\u8981\u5316\u4E86 ${c} \u6761\u6D88\u606F`);const m=Y(this.state.conversation),h=j(this.state.conversation),y=this.state.conversation.length,C=u.prunedCount+c;return this.emitAcodeEvent("context_compaction",{action:"end",compactionId:l,sizeBeforeKB:r,sizeAfterKB:m,freedKB:Math.round((r-m)*10)/10,tokensBefore:i,tokensAfter:h,messageCountBefore:a,messageCountAfter:y,strategy:c>0?"summarize":"prune",freedTokens:u.freedTokens,prunedToolMessages:u.prunedCount,summarizedMessages:c,trigger:o}),C===0?0:(this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0,c>0&&await this.rebuildRuntimeResourcesMessage(),this.emitAcodeEvent("warning",{message:s}),C)}async rebuildRuntimeResourcesMessage(){const e=this.config.runtimeResourcesProvider;if(!e)return;const t=this.state.conversation;for(let l=t.length-1;l>=0;l--)Wt(t[l])&&t.splice(l,1);let o;try{o=await e()}catch(l){p(`[ACodeRuntime] runtime resources provider failed, snapshot skipped: ${ue(l)}`);return}const s=typeof o=="string"?o.trim():"";if(!s)return;let r=0,i=-1;for(let l=0;l<t.length;l++){const u=t[l];if(u.role!=="system")break;jt(u)&&(i=l),r=l+1}const a=i>=0?i+1:r;t.splice(a,0,{role:"system",content:`${Ht}
|
|
23
|
-
${s}`})}async readChatCompletionStream(e,t,o,s){if(!e.body)throw new Error("ACode provider stream response has no body");const r=e.body.getReader(),i=new TextDecoder,a=new Map;let l="",u="",c,m,h;const y=d=>{for(const f of gs(d)){if(f==="[DONE]")continue;const g=JSON.parse(f);if(!S(g))continue;const T=dt(g);if(T){o?.abort();const x=xe(this.state.conversation,200,T)??"";throw new Error(`ACode provider request failed: ${T}${x}`)}const A=g.usage;A&&(c=co(c,A),te(`stream usage chunk received: ${JSON.stringify(A)} -> merged: ${JSON.stringify(c)}`));const w=g.choices?.[0];typeof w?.finish_reason=="string"&&w.finish_reason&&(m=w.finish_reason),typeof w?.stop_reason=="string"&&w.stop_reason&&(h=w.stop_reason);const v=w?.delta,k=as(v);k&&this.emitAcodeEvent("thinking_delta",{text:k});const M=typeof v?.content=="string"?v.content:"";M&&(l+=M,this.emitAcodeEvent("assistant_delta",{text:M}));const I=Array.isArray(v?.tool_calls)?v.tool_calls:[];for(const x of I)ys(a,x)}};let C=!0;for(;;){const d=C&&s?s:t,{done:f,value:g}=await R(r.read(),d,C?"ACode provider stream first byte":"ACode provider stream idle",()=>o?.abort());if(C=!1,f)break;u+=i.decode(g,{stream:!0});const T=u.split(/\r?\n\r?\n/);u=T.pop()??"";for(const A of T)y(A)}return u+=i.decode(),u.trim()&&y(u),{choices:[{message:{content:l||null,tool_calls:Cs(a)},...m?{finish_reason:m}:{},...h?{stop_reason:h}:{}}],...c?{usage:c}:{}}}async handleToolCalls(e,t,o=this.contextVersion){p(`[ACodeRuntime] handleToolCalls started with ${e.length} tool(s)`);const s=e.filter(l=>!ke(l.function.name)),r=l=>!ke(l.function.name),i=this.toolConcurrencyEnabled&&s.length>1?{batchId:ot(),toolCount:s.length}:void 0;i&&!this.isTurnSuperseded(o)&&this.emitToolBatchStart(i,s);const a=[];for(const l of e){if(this.isTurnSuperseded(o))break;a.push(await this.prepareToolCall(l,t,o,r(l)?i:void 0))}for(let l=a.length;l<e.length;l++){const u=e[l];a.push({toolCall:u,batchId:r(u)?i?.batchId:void 0,tool:void 0,arguments:{},mode:"blocking",scope:"unknown",access:"exclusive",claims:[],immediateError:void 0})}this.toolConcurrencyEnabled&&a.length>1?await this.executePreparedToolCallsConcurrently(a,o,i):await this.executePreparedToolCallsSerially(a,o),p(`[ACodeRuntime] handleToolCalls completed for all ${e.length} tool(s)`)}emitToolBatchStart(e,t){const o=t.map(s=>s.function.name);p(`[ACodeRuntime] \u{1F9E9} Tool batch ${e.batchId} started: tools=${e.toolCount} [${o.join(", ")}]`),this.emitAcodeEvent("mcp_status",{action:"tool_batch_start",server:"builtin",batchId:e.batchId,toolCount:e.toolCount,toolNames:o})}async executePreparedToolCallsConcurrently(e,t,o){const s=e.map(h=>({toolCallId:h.toolCall.id,toolName:h.tool?.exposedName??h.toolCall.function.name,mode:h.mode,scope:h.scope,claims:h.claims})),r=this.toolConcurrencyScheduler.schedule(s),i=o?.batchId??ot(),a=new je(i),l=new Set(e.filter(h=>!ke(h.toolCall.function.name)).map(h=>h.toolCall.id));for(const h of e)l.has(h.toolCall.id)&&a.register({toolCallId:h.toolCall.id,toolName:h.tool?.exposedName??h.toolCall.function.name,mode:h.mode,resourceKeys:(r.plans.get(h.toolCall.id)?.claims??[]).map(y=>y.key)});const u=new Map,c=new Map(e.map(h=>[h.toolCall.id,h]));for(const h of r.waves){if(this.isTurnSuperseded(t))break;const y=h.planIds.map(d=>c.get(d)).filter(d=>d!==void 0);if(y.length===0)continue;(await Promise.allSettled(y.map(d=>this.executeOnePreparedTool(d,a,t)))).forEach((d,f)=>{const g=y[f];u.set(g.toolCall.id,d.status==="fulfilled"?d.value:{toolCallId:g.toolCall.id,status:"failed",error:d.reason})})}if(await this.commitPreparedToolOutcomes(e,u,a,t),!o)return;const m=a.finalize();p(`[ACodeRuntime] \u26A1 Tool batch ${i}: tools=${m.toolCount} speedup=${m.speedup}x parallelism=${m.parallelism}x wall=${m.wallClockMs}ms contentions=${m.contentionCount} failures=${m.failureCount}`),this.emitAcodeEvent("mcp_status",{action:"tool_batch_metrics",server:"builtin",batchId:i,toolCount:m.toolCount,parallelism:m.parallelism,speedup:m.speedup,wallClockMs:m.wallClockMs,totalWaitMs:m.totalWaitMs,contentionCount:m.contentionCount,failureCount:m.failureCount,maxConcurrentObserved:m.maxConcurrentObserved,traces:m.traces.map(h=>({toolCallId:h.toolCallId,toolName:h.toolName,mode:h.mode,resourceKeys:h.resourceKeys,waitMs:h.waitDurationMs,runMs:h.executionDurationMs,status:h.status}))})}async executePreparedToolCallsSerially(e,t){const o=new je(`toolbatch-serial-${Date.now().toString(36)}`),s=new Map;for(const r of e)o.register({toolCallId:r.toolCall.id,toolName:r.tool?.exposedName??r.toolCall.function.name,mode:r.mode,resourceKeys:r.claims.map(i=>i.key)}),!this.isTurnSuperseded(t)&&s.set(r.toolCall.id,await this.executeOnePreparedTool(r,o,t));await this.commitPreparedToolOutcomes(e,s,o,t)}async commitPreparedToolOutcomes(e,t,o,s){const r=[];for(const i of e){if(this.isTurnSuperseded(s)){this.commitSupersededToolResult(i),o.transition(i.toolCall.id,"superseded");continue}const a=t.get(i.toolCall.id);if(!a){await this.commitToolOutcome(i,{toolCallId:i.toolCall.id,status:"failed",error:i.immediateError??new Error("Tool was not executed")},s,r),o.transition(i.toolCall.id,"committed");continue}o.transition(i.toolCall.id,"committing"),await this.commitToolOutcome(i,a,s,r),o.transition(i.toolCall.id,"committed")}r.length>0&&this.state.conversation.push(...r)}async executeOnePreparedTool(e,t,o){if(e.immediateError!==void 0||!e.tool){t.transition(e.toolCall.id,"failed");const a=e.immediateError??new Error("Tool not found");return t.setError(e.toolCall.id,a instanceof Error?a.message:String(a)),{toolCallId:e.toolCall.id,status:"failed",error:a}}t.transition(e.toolCall.id,"waiting_resource");const s=await this.toolResourceLock.acquire(e.claims,e.access),r=this.resolveToolLimiter(e),i=r?await r.acquire():()=>{};try{if(this.isTurnSuperseded(o))return{toolCallId:e.toolCall.id,status:"superseded"};t.transition(e.toolCall.id,"running");const a=e.tool.kind==="builtin"?await this.runBuiltInToolCall(e,o):await this.runMcpToolCall(e,o);return t.transition(e.toolCall.id,a.status),a.error!==void 0&&t.setError(e.toolCall.id,a.error instanceof Error?a.error.message:String(a.error)),a}catch(a){const l=a instanceof Error?a.message:String(a);return p(`[ACodeRuntime] \u274C Tool ${e.tool.exposedName} threw unexpectedly: ${l}`),t.transition(e.toolCall.id,"failed"),t.setError(e.toolCall.id,l),{toolCallId:e.toolCall.id,status:"failed",error:a}}finally{i(),s()}}resolveToolLimiter(e){return e.tool?.kind!=="builtin"?null:e.mode==="read"?this.toolReadOnlyLimiter:e.tool.toolName==="Bash"?this.toolBashLimiter:null}async prepareToolCall(e,t,o,s){const r=e.function.name;let i=this.state.exposedTools.find(c=>c.exposedName===r);i||(i=this.state.exposedTools.find(c=>c.toolName===r),i&&p(`[ACodeRuntime] \u21A9\uFE0F \u88F8\u540D\u56DE\u9000\u5339\u914D: ${r} -> ${i.exposedName}`));const a=Vo(e.function.arguments),l={toolCall:e,batchId:s?.batchId,tool:i,arguments:a,mode:"exclusive",scope:"exact",access:"exclusive",claims:[],immediateError:void 0};if(i||await this.recoverHiddenMcpTool(r)&&(i=this.state.exposedTools.find(c=>c.exposedName===r)??this.state.exposedTools.find(c=>c.toolName===r),l.tool=i),!i){const c=this.state.exposedTools.map(h=>h.exposedName);console.error(`[ACodeRuntime] \u274C Tool not found: ${r}, available tools: ${c.join(", ")}`);const m=c.slice(0,40).join(", ")+(c.length>40?` \u2026(+${c.length-40} more)`:"");return l.immediateError=`ACode MCP tool not found: ${r}. Available tools: ${m}`,l}if(i.kind==="mcp"&&!this.state.mcpConnections.find(m=>m.serverName===i.serverName))return console.error(`[ACodeRuntime] \u274C MCP server not connected: ${i.serverName}, connected servers: ${this.state.mcpConnections.map(m=>m.serverName).join(", ")}`),l.immediateError=`ACode MCP server not connected: ${i.serverName}`,l;try{lt(i,this.state.permissions,t);const c=await this.hookBus.dispatch("PreToolUse",{source:i.kind==="mcp"?"mcp":"builtin",...i.kind==="mcp"?{serverName:i.serverName}:{},toolName:i.toolName,exposedName:i.exposedName,arguments:a}),m=Le(c);if(m)throw console.error(`[ACodeRuntime] \u274C Tool denied by hook: ${i.exposedName}: ${m.message}`),new Error(m.message||`ACode tool denied by hook: ${i.exposedName}`)}catch(c){return l.immediateError=c,l}if(this.isTurnSuperseded(o))return l;const u=this.resolveToolConcurrencyDeclaration(i,a);return l.mode=u.mode,l.scope=u.scope,l.access=u.access,l.claims=u.claims,this.emitAcodeEvent("mcp_status",{action:"call_tool",server:i.kind==="mcp"?i.serverName:"builtin",tool:i.toolName,exposedName:i.exposedName,toolUseId:e.id,arguments:a,argumentsPreview:rs(a),...l.batchId?{batchId:l.batchId}:{}}),l}resolveToolConcurrencyDeclaration(e,t){if(e.kind!=="builtin")return Se(e.toolName)==="poll_message"||we(e.toolName)?{mode:"blocking",scope:"unknown",access:"exclusive",claims:[]}:{mode:"exclusive",scope:"unknown",access:"exclusive",claims:[]};const o=e.concurrencyMode,s=e.resourceScope??"exact";if(!o)return{mode:"exclusive",scope:s,access:"exclusive",claims:[]};if(o==="blocking")return{mode:"blocking",scope:s,access:"exclusive",claims:[]};const r=o;let i=[];if(e.resourceKeys)try{i=e.resourceKeys(t)??[]}catch(a){p(`[ACodeRuntime] \u26A0\uFE0F resourceKeys() threw for ${e.exposedName}, falling back to exclusive: ${a instanceof Error?a.message:String(a)}`),i=[]}return i.length===0&&s==="unknown"?{mode:"exclusive",scope:s,access:"exclusive",claims:[]}:{mode:o,scope:s,access:r,claims:i}}async runBuiltInToolCall(e,t){const o=e.tool;if(o.kind!=="builtin")return{toolCallId:e.toolCall.id,status:"failed",error:new Error("not builtin")};const s=e.arguments,r=Xo(o,s,this.state.provider.mcpToolTimeoutMs);r<=0&&console.warn(`[ACodeRuntime] \u26A0\uFE0F Built-in tool ${o.exposedName} has NO timeout (timeoutMs=${r})!`);const i=this.beginToolSettle(o,s,e.toolCall.id);try{const a=await R(o.execute(s,{onOutput:l=>{this.emitAcodeEvent("command_output",{server:"builtin",tool:o.toolName,exposedName:o.exposedName,toolUseId:e.toolCall.id,stream:l.stream,text:l.text,sequence:l.sequence,cwd:l.cwd,commandLineCount:l.commandLineCount,...l.commandFilePath?{commandFilePath:l.commandFilePath}:{}})},toolCallId:e.toolCall.id,...i.signal?{settleSignal:i.signal}:{}}),r,`ACode built-in tool ${o.toolName}`);return{toolCallId:e.toolCall.id,status:"completed",resultText:G(a)}}catch(a){return this.classifyToolError(e,a)}finally{this.clearToolSettle(i.seq,e.toolCall.id)}}async runMcpToolCall(e,t){const o=e.tool;if(o.kind!=="mcp")return{toolCallId:e.toolCall.id,status:"failed",error:new Error("not mcp")};const s=this.state.mcpConnections.find(u=>u.serverName===o.serverName);if(!s)return{toolCallId:e.toolCall.id,status:"failed",error:new Error(`ACode MCP server not connected: ${o.serverName}`)};const r=e.arguments,i=Be(o.toolName),a=we(o.toolName),l=i?this.state.provider.mcpPollingToolTimeoutMs:a?0:this.state.provider.mcpToolTimeoutMs;try{const u=await this.withBlockingToolCallTracking(o.toolName,()=>i&&Se(o.toolName)==="poll_message"?this.callPollMessageWithSubAgentRace(s,o,r,l):this.callMcpToolWithTimeout(s,o,r,l,i));let c=G(u);return i&&(c=this.expandCommandsInPolledResult(c)),{toolCallId:e.toolCall.id,status:"completed",resultText:c}}catch(u){return this.classifyToolError(e,u)}}async withBlockingToolCallTracking(e,t){if(!X.isLongBlockingMcpToolName(e))return await t();this.activeBlockingToolCalls+=1;try{return await t()}finally{this.activeBlockingToolCalls-=1}}classifyToolError(e,t){const s=(t instanceof Error?t.message:String(t)).includes("timed out after")?"timeout":t instanceof Error&&t.name==="AbortError"?"aborted":"failed";return{toolCallId:e.toolCall.id,status:s,error:t}}async commitToolOutcome(e,t,o,s=[]){const r=e.tool,i=r?.kind==="mcp"?r.serverName:"builtin",a=r?.toolName??e.toolCall.function.name;if(t.status==="completed"&&t.resultText!==void 0){const c=t.resultText;r&&await this.hookBus.dispatch("PostToolUse",{source:r.kind==="mcp"?"mcp":"builtin",...r.kind==="mcp"?{serverName:r.serverName}:{},toolName:r.toolName,exposedName:r.exposedName,result:c}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:i,tool:a,toolUseId:e.toolCall.id,result:c,...e.batchId?{batchId:e.batchId}:{}}),this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:c});const m=await this.appendSyntheticMultimodalUserMessage(c,e.toolCall.id,o);m&&s.push(m);return}if(t.status==="superseded"){this.commitSupersededToolResult(e);return}const l=t.error??new Error("Tool execution failed"),u=l instanceof Error?l.message:String(l);if(this.abortRequested||this.state.terminated){this.emitAcodeEvent("mcp_status",{action:"tool_result",server:i,tool:a,toolUseId:e.toolCall.id,result:`[aborted] ${u}`,failed:!0,aborted:!0,...e.batchId?{batchId:e.batchId}:{}}),this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:`[aborted] ${u}`});return}if(!r){this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:u}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:i,tool:a,toolUseId:e.toolCall.id,result:u,failed:!0,...e.batchId?{batchId:e.batchId}:{}});return}this.appendToolFailureResult(e.toolCall,r,l,e.batchId)}commitSupersededToolResult(e){const t=e.immediateError!==void 0?`[cancelled] ${e.immediateError instanceof Error?e.immediateError.message:String(e.immediateError)}`:"[cancelled] Tool execution was superseded by a newer turn.";this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:t}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:e.tool?.kind==="mcp"?e.tool.serverName:"builtin",tool:e.tool?.toolName??e.toolCall.function.name,toolUseId:e.toolCall.id,result:t,failed:!0,aborted:!0,...e.batchId?{batchId:e.batchId}:{}})}async executeSingleToolByName(e,t,o){let s=this.state.exposedTools.find(l=>l.exposedName===e);if(s||(s=this.state.exposedTools.find(l=>l.toolName===e)),!s)throw new Error(`Tool not found: ${e}`);if(s.kind==="builtin")return s.execute(t,{toolCallId:o?.toolCallId});const r=this.state.mcpConnections.find(l=>l.serverName===s.serverName);if(!r)throw new Error(`MCP server not found: ${s.serverName}`);const i=Be(s.toolName),a=i?this.state.provider.mcpPollingToolTimeoutMs:we(s.toolName)?0:this.state.provider.mcpToolTimeoutMs;if(i&&Se(s.toolName)==="poll_message"){const l=await this.withBlockingToolCallTracking(s.toolName,()=>this.callPollMessageWithSubAgentRace(r,s,t,a,{allowRepoll:!1}));return this.expandCommandsInPolledResult(G(l))}return this.withBlockingToolCallTracking(s.toolName,()=>this.callMcpToolWithTimeout(r,s,t,a,i))}async callMcpToolWithTimeout(e,t,o,s,r,i){const a=s>0?s:go,u=!!this.config.sharedMcpConnections?{...o,_aws_sub_agent:!0}:o;for(;;){const c=this.createRuntimeStopWaiter(`ACode MCP tool ${t.serverName}/${t.toolName}`);try{return await R(Promise.race([e.client.callTool({name:t.toolName,arguments:u},void 0,{timeout:a,...i?{signal:i}:{}}),c.promise]),s,`ACode MCP tool ${t.serverName}/${t.toolName}`)}catch(m){if(i?.aborted||!r||s>0||!ns(m))throw m;this.emitAcodeEvent("warning",{message:`ACode MCP polling tool ${t.serverName}/${t.toolName} reached the MCP SDK timeout window and will continue polling.`})}finally{c.cancel()}}}static formatSubAgentCompletionAsPollResult(e){const t=e.map(o=>{const s=o.error?`Sub-agent ${o.subId} [${o.status}]: ${o.description}
|
|
23
|
+
`+Gt}]},a=new AbortController;this.abortController=a;let r=!1;try{return await x((async()=>{const l=await x(fetch(Ae(o),{method:"POST",headers:s,body:JSON.stringify(i),signal:a.signal}),t.requestTotalTimeoutMs,"ACode LLM summarization",()=>{r=!0,a.abort()});if(!l.ok){const m=await x(l.text(),t.requestTimeoutMs,"ACode LLM summarization error response body",()=>{r=!0,a.abort()}),h=m.length>500?`${m.slice(0,500)}...(truncated, total ${m.length} chars)`:m;throw new Error(`ACode LLM summarization request failed: ${l.status} ${h}`)}const c=(await x(l.json(),t.requestTimeoutMs,"ACode LLM summarization response body",()=>{r=!0,a.abort()})).choices?.[0]?.message?.content;if(!c||c.trim().length===0)throw new Error("ACode LLM summarization returned empty content");return c})(),t.requestTotalTimeoutMs,"ACode LLM summarization total",()=>{a.abort()})}catch(l){throw l instanceof Error&&l.name==="AbortError"?r?new Error(`ACode LLM summarization timed out after ${t.requestTimeoutMs}ms`):new Error("ACode LLM summarization aborted"):l}finally{this.abortController===a&&(this.abortController=null)}}async requestChatCompletion(e){const t=this.state.provider,o=t.baseURL;if(!o)throw new Error("ACode provider baseURL is not configured");const{contextLimit:s,maxOutput:i,inputLimit:a,threshold:r}=_e(t),l=this.state.accumulatedInputTokens+this.state.accumulatedOutputTokens,u=z(this.state.conversation)+ue(this.state.exposedTools),c=Math.max(l,u),m=c>=r,h=this.compactRequested;this.compactRequested=null,p(`[ACodeRuntime] \u{1F50D} Compaction check: actualUsed=${l}, estimation=${u}, effectiveUsed=${c}, contextLimit=${s}, inputLimit=${a??"n/a"}, maxOutput=${i}, triggerThreshold=${r}, shouldCompact=${m}, manualCompact=${h}`),h?await this.runCompactionPass({contextLimit:s,maxOutput:i,effectiveUsed:c,trigger:"manual",force:!0,detectLabel:"\u624B\u52A8\u89E6\u53D1\u4E0A\u4E0B\u6587\u538B\u7F29",detectDetail:`\u7528\u6237\u8BF7\u6C42\u5148\u884C\u538B\u7F29\uFF0C\u5F53\u524D\u4F30\u7B97\u7528\u91CF: ${c} tokens`,plan:h}):m&&await this.runCompactionPass({contextLimit:s,maxOutput:i,effectiveUsed:c,trigger:l>=r?"actual_usage":"estimated_overflow",force:!1,detectLabel:"\u68C0\u6D4B\u5230\u4E0A\u4E0B\u6587\u6EA2\u51FA",detectDetail:`\u6709\u6548\u7528\u91CF: ${c} tokens, \u9608\u503C: ${r}, \u89E6\u53D1: ${l>=r?"\u5B9E\u9645\u7528\u91CF":"\u4F30\u7B97\u6EA2\u51FA"}`});const y=xo(s);let _=vo()||m;if(!_){const A=Be(this.state.conversation);A>ot?_=!0:A>y&&p(`[ACodeRuntime] \u23ED\uFE0F Byte-cap truncation skipped: body ${A}B > cap ${y}B, but effectiveUsed ${c} < threshold ${r} (aligned with frontend compression marker; rely on provider 413/overflow fallback)`)}let d=dt,f=0,g=0,C=0;if(_&&(f=Q(this.state.conversation),g=z(this.state.conversation),C=this.state.conversation.length,d=await as(this.state.conversation,y,s,i,A=>this.summarizeConversationWithLLM(A),()=>this.rebuildRuntimeResourcesMessage())),d.truncatedCount>0){const A=`compaction-bytes-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;this.emitAcodeEvent("context_compaction",{action:"start",compactionId:A,sizeKB:f,estimatedTokens:g,actualInputTokens:this.state.accumulatedInputTokens,actualOutputTokens:this.state.accumulatedOutputTokens,messageCount:C,model:t.model,trigger:"request_body_byte_limit"}),this.emitCompactionPhase(A,"byte_limit_detect","\u68C0\u6D4B\u5230\u8BF7\u6C42\u4F53\u8D85\u9650",`\u8BF7\u6C42\u4F53\u8D85\u8FC7 ${y} \u5B57\u8282\u4E0A\u9650\uFF0C\u5373\u5C06\u88C1\u526A\u5386\u53F2`,"request_body_byte_limit");const P=Q(this.state.conversation),Y=z(this.state.conversation);this.emitAcodeEvent("context_compaction",{action:"end",compactionId:A,sizeBeforeKB:f,sizeAfterKB:P,freedKB:Math.round((f-P)*10)/10,strategy:d.strategy,freedTokens:d.freedTokens,tokensBefore:g,tokensAfter:Y,prunedToolMessages:d.prunedToolMessages,summarizedMessages:d.summarizedMessages,messageCountBefore:C,messageCountAfter:this.state.conversation.length,trigger:"request_body_byte_limit"}),this.emitAcodeEvent("warning",{message:`Conversation truncated: removed ${d.truncatedCount} older messages to fit within ${y} byte request size limit`}),this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0}const w=()=>{const A=rs(this.state.conversation);(A.orphanToolMessages>0||A.repairedToolCalls>0||A.downgradedAssistants>0||A.relocatedInterruptions>0||A.reorderedToolResults>0)&&(p(`[ACodeRuntime] \u{1FA79} sanitizeToolCallSequence: orphanTools=${A.orphanToolMessages} repairedCalls=${A.repairedToolCalls} downgradedAssistants=${A.downgradedAssistants} relocatedInterruptions=${A.relocatedInterruptions} reorderedToolResults=${A.reorderedToolResults}`),this.emitAcodeEvent("warning",{message:`\u68C0\u6D4B\u5230\u5DE5\u5177\u8C03\u7528\u5E8F\u5217\u4E0D\u5B8C\u6574\uFF0C\u5DF2\u81EA\u52A8\u4FEE\u590D\uFF08\u5B64\u513F\u5DE5\u5177\u7ED3\u679C ${A.orphanToolMessages} \u6761\u3001\u8865\u5360\u4F4D\u7ED3\u679C ${A.repairedToolCalls} \u4E2A\u3001\u964D\u7EA7 assistant \u6D88\u606F ${A.downgradedAssistants} \u6761\u3001\u56DE\u6B63\u7A7F\u63D2\u6D88\u606F ${A.relocatedInterruptions} \u6761\u3001\u56DE\u6B63\u9519\u5E8F\u7ED3\u679C ${A.reorderedToolResults} \u6761\uFF09\uFF0C\u907F\u514D provider 400 (tool_call_sequence_broken) \u6B7B\u5FAA\u73AF\u3002`}))};w();const b=this.state.exposedTools.filter(A=>this.toolGroupManager&&!this.toolGroupManager.shouldShowToLLMByName(A.exposedName)?!1:es(A,this.state.permissions,e)),M=()=>{const A={model:t.model,messages:this.state.conversation,stream:t.stream};return t.effort&&(A.reasoning_effort=t.effort),t.stream&&(A.stream_options={include_usage:!0}),b.length>0&&(A.tools=ht(b),A.tool_choice="auto"),A},$="POST",S=ts(o),v=ys(t),E=Cs(v),G=A=>{const P=JSON.stringify(A,null,2);p(`[ACodeRuntime] \u2605\u2605\u2605 DEBUG \u2605\u2605\u2605 emitAiRequest: \u53D1\u9001\u8BF7\u6C42 model=${t.model}, endpoint=${S}, messageCount=${this.state.conversation.length}`),this.emitAcodeEvent("ai_request",{model:t.model,endpoint:S,requestMethod:$,requestUrl:S,requestHeaders:E,stream:t.stream,messageCount:this.state.conversation.length,toolCount:b.length,lastUserMessagePreview:os(this.state.conversation),estimatedContextTokens:ve(P),...this.lastIterationUsage?{lastUsage:this.lastIterationUsage}:{}}),oe(`ai_request emitted: model=${t.model} stream=${t.stream} messageCount=${this.state.conversation.length} toolCount=${b.length} estimatedContextTokens=${ve(P)} lastUsage=${JSON.stringify(this.lastIterationUsage??null)}`)};let L=M();G(L);const B=t.requestRetryCount===$e,ie=B?1/0:t.requestRetryCount+1;let ee=!1;for(let A=1;B||A<=ie;A+=1){this.state.terminated||(this.abortRequested=!1),A>1&&G(L);const P=new AbortController;this.abortController=P;let Y=!1;try{const U=await x((async()=>{const J=t.stream?t.requestFirstByteTimeoutMs:t.requestTotalTimeoutMs,q=await x(fetch(Ae(o),{method:$,headers:v,body:JSON.stringify(L),signal:P.signal}),J,"ACode provider request",()=>{Y=!0,P.abort()});if(!q.ok){const T=await x(q.text(),t.requestStreamIdleTimeoutMs,"ACode provider error response body",()=>{Y=!0,P.abort()}),I=T.length>500?`${T.slice(0,500)}...(truncated, total ${T.length} chars)`:T,re=Re(this.state.conversation,q.status,T)??"";throw new Error(`ACode provider request failed: ${q.status} ${I}${re}`)}const V=q.headers?.get?.("content-type")||"";if(t.stream&&V.toLowerCase().includes("text/event-stream"))return await this.readChatCompletionStream(q,t.requestStreamIdleTimeoutMs,P,t.requestFirstByteTimeoutMs);const X=await x(q.json(),t.requestStreamIdleTimeoutMs,"ACode provider response body",()=>{Y=!0,P.abort()});if(!k(X))return this.emitAcodeEvent("warning",{message:`ACode provider returned invalid response format (expected object, got ${typeof X}); treating as empty response`}),{};const K=ft(X);if(K){const T=Re(this.state.conversation,q.status,K)??"";throw new Error(`ACode provider request failed: ${K}${T}`)}return X})(),t.requestTotalTimeoutMs,"ACode provider request total",()=>{P.abort()}),R=Zt({finishReason:U.choices?.[0]?.finish_reason,stopReason:U.choices?.[0]?.stop_reason});if(R.kind==="context_overflow"){if(!ee&&await this.compactForContextOverflow({contextLimit:s,trigger:"finish_reason_context_overflow",warningMessage:`ACode provider reported context overflow via ${R.source} (${R.marker}); summarized older messages and rebuilt the request instead of accepting the truncated payload`})>0){ee=!0,w(),L=M(),G(L),A-=1;continue}this.emitAcodeEvent("warning",{message:`ACode provider reported context overflow via ${R.source} (${R.marker}), but no further compaction was possible`})}return U}catch(U){let R;if(U instanceof Error&&U.name==="AbortError"?Y?R=new Error(`ACode provider request timed out (firstByte=${t.requestFirstByteTimeoutMs}ms, idle=${t.requestStreamIdleTimeoutMs}ms)`):this.state.terminated?R=new Error("ACode provider request aborted by user"):R=new Error(`ACode provider request timed out (watchdog abort, firstByte=${t.requestFirstByteTimeoutMs}ms, idle=${t.requestStreamIdleTimeoutMs}ms)`):R=U instanceof Error?U:new Error(String(U)),!ee&&!this.abortRequested&&!this.state.terminated&&(we(U)||we(R))){if(await this.compactForContextOverflow({contextLimit:s,trigger:"provider_overflow_error",warningMessage:"ACode provider context limit exceeded; summarized older messages and rebuilt the request instead of retrying the same oversized payload"})===0)throw R;ee=!0,w(),L=M(),G(L),A-=1;continue}if(!((B||A<ie)&&!this.abortRequested&&!this.state.terminated&&cs(R)))throw R;this.emitAcodeEvent("error",{message:`ACode provider request attempt ${A}${B?"":`/${ie}`} failed; retrying: ${R.message}`,nonFatal:!0});const q=Math.min(Ao,_o*Math.pow(2,A-1)),V=Math.random()*1e3;await Oo(q+V)}finally{this.abortController===P&&(this.abortController=null)}}throw new Error("ACode provider request failed after all retries")}async compactForContextOverflow(e){const{contextLimit:t,trigger:o,warningMessage:s}=e,i=Q(this.state.conversation),a=z(this.state.conversation),r=this.state.conversation.length,l=`compaction-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;this.emitAcodeEvent("context_compaction",{action:"start",compactionId:l,sizeKB:i,estimatedTokens:a,messageCount:r,model:this.state.provider.model,trigger:o}),this.emitCompactionPhase(l,"detect","\u68C0\u6D4B\u5230\u4E0A\u4E0B\u6587\u6EA2\u51FA",`\u6D88\u606F: ${r}\u6761, \u5927\u5C0F: ${i}KB`),this.emitCompactionPhase(l,"prune","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA","\u6B63\u5728\u88C1\u526A...");const u=Vt(this.state.conversation,t);this.emitCompactionPhase(l,"prune_done","\u9636\u6BB51: \u88C1\u526A\u5DE5\u5177\u8F93\u51FA",`\u88C1\u526A\u4E86 ${u.prunedCount} \u6761\u5DE5\u5177\u8F93\u51FA, \u91CA\u653E ${u.freedTokens} tokens`),this.emitCompactionPhase(l,"summarize","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD","\u6B63\u5728\u6458\u8981...");const c=await Ue(this.state.conversation,{llmSummarizer:d=>this.summarizeConversationWithLLM(d),contextLimit:t});this.emitCompactionPhase(l,"summarize_done","\u9636\u6BB52: \u6458\u8981\u5316\u5BF9\u8BDD",`\u6458\u8981\u5316\u4E86 ${c} \u6761\u6D88\u606F`);const m=Q(this.state.conversation),h=z(this.state.conversation),y=this.state.conversation.length,_=u.prunedCount+c;return this.emitAcodeEvent("context_compaction",{action:"end",compactionId:l,sizeBeforeKB:i,sizeAfterKB:m,freedKB:Math.round((i-m)*10)/10,tokensBefore:a,tokensAfter:h,messageCountBefore:r,messageCountAfter:y,strategy:c>0?"summarize":"prune",freedTokens:u.freedTokens,prunedToolMessages:u.prunedCount,summarizedMessages:c,trigger:o}),_===0?0:(this.state.accumulatedInputTokens=0,this.state.accumulatedOutputTokens=0,c>0&&await this.rebuildRuntimeResourcesMessage(),this.emitAcodeEvent("warning",{message:s}),_)}async rebuildRuntimeResourcesMessage(){const e=this.config.runtimeResourcesProvider;if(!e)return;const t=this.state.conversation;for(let l=t.length-1;l>=0;l--)Ht(t[l])&&t.splice(l,1);let o;try{o=await e()}catch(l){p(`[ACodeRuntime] runtime resources provider failed, snapshot skipped: ${me(l)}`);return}const s=typeof o=="string"?o.trim():"";if(!s)return;let i=0,a=-1;for(let l=0;l<t.length;l++){const u=t[l];if(u.role!=="system")break;Kt(u)&&(a=l),i=l+1}const r=a>=0?a+1:i;t.splice(r,0,{role:"system",content:`${Xt}
|
|
24
|
+
${s}`})}async readChatCompletionStream(e,t,o,s){if(!e.body)throw new Error("ACode provider stream response has no body");const i=e.body.getReader(),a=new TextDecoder,r=new Map;let l="",u="",c,m,h;const y=d=>{for(const f of bs(d)){if(f==="[DONE]")continue;const g=JSON.parse(f);if(!k(g))continue;const C=ft(g);if(C){o?.abort();const E=Re(this.state.conversation,200,C)??"";throw new Error(`ACode provider request failed: ${C}${E}`)}const w=g.usage;w&&(c=go(c,w),oe(`stream usage chunk received: ${JSON.stringify(w)} -> merged: ${JSON.stringify(c)}`));const b=g.choices?.[0];typeof b?.finish_reason=="string"&&b.finish_reason&&(m=b.finish_reason),typeof b?.stop_reason=="string"&&b.stop_reason&&(h=b.stop_reason);const M=b?.delta,$=ps(M);$&&this.emitAcodeEvent("thinking_delta",{text:$});const S=typeof M?.content=="string"?M.content:"";S&&(l+=S,this.emitAcodeEvent("assistant_delta",{text:S}));const v=Array.isArray(M?.tool_calls)?M.tool_calls:[];for(const E of v)ks(r,E)}};let _=!0;for(;;){const d=_&&s?s:t,{done:f,value:g}=await x(i.read(),d,_?"ACode provider stream first byte":"ACode provider stream idle",()=>o?.abort());if(_=!1,f)break;u+=a.decode(g,{stream:!0});const C=u.split(/\r?\n\r?\n/);u=C.pop()??"";for(const w of C)y(w)}return u+=a.decode(),u.trim()&&y(u),{choices:[{message:{content:l||null,tool_calls:Ss(r)},...m?{finish_reason:m}:{},...h?{stop_reason:h}:{}}],...c?{usage:c}:{}}}async handleToolCalls(e,t,o=this.contextVersion){p(`[ACodeRuntime] handleToolCalls started with ${e.length} tool(s)`);const s=e.filter(l=>!Se(l.function.name)),i=l=>!Se(l.function.name),a=this.toolConcurrencyEnabled&&s.length>1?{batchId:it(),toolCount:s.length}:void 0;a&&!this.isTurnSuperseded(o)&&this.emitToolBatchStart(a,s);const r=[];for(const l of e){if(this.isTurnSuperseded(o))break;r.push(await this.prepareToolCall(l,t,o,i(l)?a:void 0))}for(let l=r.length;l<e.length;l++){const u=e[l];r.push({toolCall:u,batchId:i(u)?a?.batchId:void 0,tool:void 0,arguments:{},mode:"blocking",scope:"unknown",access:"exclusive",claims:[],immediateError:void 0})}this.toolConcurrencyEnabled&&r.length>1?await this.executePreparedToolCallsConcurrently(r,o,a):await this.executePreparedToolCallsSerially(r,o),p(`[ACodeRuntime] handleToolCalls completed for all ${e.length} tool(s)`)}emitToolBatchStart(e,t){const o=t.map(s=>s.function.name);p(`[ACodeRuntime] \u{1F9E9} Tool batch ${e.batchId} started: tools=${e.toolCount} [${o.join(", ")}]`),this.emitAcodeEvent("mcp_status",{action:"tool_batch_start",server:"builtin",batchId:e.batchId,toolCount:e.toolCount,toolNames:o})}async executePreparedToolCallsConcurrently(e,t,o){const s=e.map(h=>({toolCallId:h.toolCall.id,toolName:h.tool?.exposedName??h.toolCall.function.name,mode:h.mode,scope:h.scope,claims:h.claims})),i=this.toolConcurrencyScheduler.schedule(s),a=o?.batchId??it(),r=new Je(a),l=new Set(e.filter(h=>!Se(h.toolCall.function.name)).map(h=>h.toolCall.id));for(const h of e)l.has(h.toolCall.id)&&r.register({toolCallId:h.toolCall.id,toolName:h.tool?.exposedName??h.toolCall.function.name,mode:h.mode,resourceKeys:(i.plans.get(h.toolCall.id)?.claims??[]).map(y=>y.key)});const u=new Map,c=new Map(e.map(h=>[h.toolCall.id,h]));for(const h of i.waves){if(this.isTurnSuperseded(t))break;const y=h.planIds.map(d=>c.get(d)).filter(d=>d!==void 0);if(y.length===0)continue;(await Promise.allSettled(y.map(d=>this.executeOnePreparedTool(d,r,t)))).forEach((d,f)=>{const g=y[f];u.set(g.toolCall.id,d.status==="fulfilled"?d.value:{toolCallId:g.toolCall.id,status:"failed",error:d.reason})})}if(await this.commitPreparedToolOutcomes(e,u,r,t),!o)return;const m=r.finalize();p(`[ACodeRuntime] \u26A1 Tool batch ${a}: tools=${m.toolCount} speedup=${m.speedup}x parallelism=${m.parallelism}x wall=${m.wallClockMs}ms contentions=${m.contentionCount} failures=${m.failureCount}`),this.emitAcodeEvent("mcp_status",{action:"tool_batch_metrics",server:"builtin",batchId:a,toolCount:m.toolCount,parallelism:m.parallelism,speedup:m.speedup,wallClockMs:m.wallClockMs,totalWaitMs:m.totalWaitMs,contentionCount:m.contentionCount,failureCount:m.failureCount,maxConcurrentObserved:m.maxConcurrentObserved,traces:m.traces.map(h=>({toolCallId:h.toolCallId,toolName:h.toolName,mode:h.mode,resourceKeys:h.resourceKeys,waitMs:h.waitDurationMs,runMs:h.executionDurationMs,status:h.status}))})}async executePreparedToolCallsSerially(e,t){const o=new Je(`toolbatch-serial-${Date.now().toString(36)}`),s=new Map;for(const i of e)o.register({toolCallId:i.toolCall.id,toolName:i.tool?.exposedName??i.toolCall.function.name,mode:i.mode,resourceKeys:i.claims.map(a=>a.key)}),!this.isTurnSuperseded(t)&&s.set(i.toolCall.id,await this.executeOnePreparedTool(i,o,t));await this.commitPreparedToolOutcomes(e,s,o,t)}async commitPreparedToolOutcomes(e,t,o,s){const i=[];for(const a of e){if(this.isTurnSuperseded(s)){this.commitSupersededToolResult(a),o.transition(a.toolCall.id,"superseded");continue}const r=t.get(a.toolCall.id);if(!r){await this.commitToolOutcome(a,{toolCallId:a.toolCall.id,status:"failed",error:a.immediateError??new Error("Tool was not executed")},s,i),o.transition(a.toolCall.id,"committed");continue}o.transition(a.toolCall.id,"committing"),await this.commitToolOutcome(a,r,s,i),o.transition(a.toolCall.id,"committed")}i.length>0&&this.state.conversation.push(...i)}async executeOnePreparedTool(e,t,o){if(e.immediateError!==void 0||!e.tool){t.transition(e.toolCall.id,"failed");const r=e.immediateError??new Error("Tool not found");return t.setError(e.toolCall.id,r instanceof Error?r.message:String(r)),{toolCallId:e.toolCall.id,status:"failed",error:r}}t.transition(e.toolCall.id,"waiting_resource");const s=await this.toolResourceLock.acquire(e.claims,e.access),i=this.resolveToolLimiter(e),a=i?await i.acquire():()=>{};try{if(this.isTurnSuperseded(o))return{toolCallId:e.toolCall.id,status:"superseded"};t.transition(e.toolCall.id,"running");const r=e.tool.kind==="builtin"?await this.runBuiltInToolCall(e,o):await this.runMcpToolCall(e,o);return t.transition(e.toolCall.id,r.status),r.error!==void 0&&t.setError(e.toolCall.id,r.error instanceof Error?r.error.message:String(r.error)),r}catch(r){const l=r instanceof Error?r.message:String(r);return p(`[ACodeRuntime] \u274C Tool ${e.tool.exposedName} threw unexpectedly: ${l}`),t.transition(e.toolCall.id,"failed"),t.setError(e.toolCall.id,l),{toolCallId:e.toolCall.id,status:"failed",error:r}}finally{a(),s()}}resolveToolLimiter(e){return e.tool?.kind!=="builtin"?null:e.mode==="read"?this.toolReadOnlyLimiter:e.tool.toolName==="Bash"?this.toolBashLimiter:null}async prepareToolCall(e,t,o,s){const i=e.function.name;let a=this.state.exposedTools.find(c=>c.exposedName===i);a||(a=this.state.exposedTools.find(c=>c.toolName===i),a&&p(`[ACodeRuntime] \u21A9\uFE0F \u88F8\u540D\u56DE\u9000\u5339\u914D: ${i} -> ${a.exposedName}`));const r=ss(e.function.arguments),l={toolCall:e,batchId:s?.batchId,tool:a,arguments:r,mode:"exclusive",scope:"exact",access:"exclusive",claims:[],immediateError:void 0};if(a||await this.recoverHiddenMcpTool(i)&&(a=this.state.exposedTools.find(c=>c.exposedName===i)??this.state.exposedTools.find(c=>c.toolName===i),l.tool=a),!a){const c=this.state.exposedTools.map(h=>h.exposedName);console.error(`[ACodeRuntime] \u274C Tool not found: ${i}, available tools: ${c.join(", ")}`);const m=c.slice(0,40).join(", ")+(c.length>40?` \u2026(+${c.length-40} more)`:"");return l.immediateError=`ACode MCP tool not found: ${i}. Available tools: ${m}`,l}if(a.kind==="mcp"&&!this.state.mcpConnections.find(m=>m.serverName===a.serverName))return console.error(`[ACodeRuntime] \u274C MCP server not connected: ${a.serverName}, connected servers: ${this.state.mcpConnections.map(m=>m.serverName).join(", ")}`),l.immediateError=`ACode MCP server not connected: ${a.serverName}`,l;try{mt(a,this.state.permissions,t);const c=await this.hookBus.dispatch("PreToolUse",{source:a.kind==="mcp"?"mcp":"builtin",...a.kind==="mcp"?{serverName:a.serverName}:{},toolName:a.toolName,exposedName:a.exposedName,arguments:r}),m=qe(c);if(m)throw console.error(`[ACodeRuntime] \u274C Tool denied by hook: ${a.exposedName}: ${m.message}`),new Error(m.message||`ACode tool denied by hook: ${a.exposedName}`)}catch(c){return l.immediateError=c,l}if(this.isTurnSuperseded(o))return l;const u=this.resolveToolConcurrencyDeclaration(a,r);return l.mode=u.mode,l.scope=u.scope,l.access=u.access,l.claims=u.claims,this.emitAcodeEvent("mcp_status",{action:"call_tool",server:a.kind==="mcp"?a.serverName:"builtin",tool:a.toolName,exposedName:a.exposedName,toolUseId:e.id,arguments:r,argumentsPreview:ds(r),...l.batchId?{batchId:l.batchId}:{}}),l}resolveToolConcurrencyDeclaration(e,t){if(e.kind!=="builtin")return Me(e.toolName)==="poll_message"||ke(e.toolName)?{mode:"blocking",scope:"unknown",access:"exclusive",claims:[]}:{mode:"exclusive",scope:"unknown",access:"exclusive",claims:[]};const o=e.concurrencyMode,s=e.resourceScope??"exact";if(!o)return{mode:"exclusive",scope:s,access:"exclusive",claims:[]};if(o==="blocking")return{mode:"blocking",scope:s,access:"exclusive",claims:[]};const i=o;let a=[];if(e.resourceKeys)try{a=e.resourceKeys(t)??[]}catch(r){p(`[ACodeRuntime] \u26A0\uFE0F resourceKeys() threw for ${e.exposedName}, falling back to exclusive: ${r instanceof Error?r.message:String(r)}`),a=[]}return a.length===0&&s==="unknown"?{mode:"exclusive",scope:s,access:"exclusive",claims:[]}:{mode:o,scope:s,access:i,claims:a}}async runBuiltInToolCall(e,t){const o=e.tool;if(o.kind!=="builtin")return{toolCallId:e.toolCall.id,status:"failed",error:new Error("not builtin")};const s=e.arguments,i=ns(o,s,this.state.provider.mcpToolTimeoutMs);i<=0&&console.warn(`[ACodeRuntime] \u26A0\uFE0F Built-in tool ${o.exposedName} has NO timeout (timeoutMs=${i})!`);const a=this.beginToolSettle(o,s,e.toolCall.id);try{const r=await x(o.execute(s,{onOutput:l=>{this.emitAcodeEvent("command_output",{server:"builtin",tool:o.toolName,exposedName:o.exposedName,toolUseId:e.toolCall.id,stream:l.stream,text:l.text,sequence:l.sequence,cwd:l.cwd,commandLineCount:l.commandLineCount,...l.commandFilePath?{commandFilePath:l.commandFilePath}:{}})},toolCallId:e.toolCall.id,...a.signal?{settleSignal:a.signal}:{}}),i,`ACode built-in tool ${o.toolName}`);return{toolCallId:e.toolCall.id,status:"completed",resultText:W(r)}}catch(r){return this.classifyToolError(e,r)}finally{this.clearToolSettle(a.seq,e.toolCall.id)}}async runMcpToolCall(e,t){const o=e.tool;if(o.kind!=="mcp")return{toolCallId:e.toolCall.id,status:"failed",error:new Error("not mcp")};const s=this.state.mcpConnections.find(u=>u.serverName===o.serverName);if(!s)return{toolCallId:e.toolCall.id,status:"failed",error:new Error(`ACode MCP server not connected: ${o.serverName}`)};const i=e.arguments,a=Fe(o.toolName),r=ke(o.toolName),l=a?this.state.provider.mcpPollingToolTimeoutMs:r?0:this.state.provider.mcpToolTimeoutMs;try{const u=await this.withBlockingToolCallTracking(o.toolName,()=>a&&Me(o.toolName)==="poll_message"?this.callPollMessageWithSubAgentRace(s,o,i,l):this.callMcpToolWithTimeout(s,o,i,l,a));let c=W(u);return a&&(c=this.expandCommandsInPolledResult(c)),{toolCallId:e.toolCall.id,status:"completed",resultText:c}}catch(u){return this.classifyToolError(e,u)}}async withBlockingToolCallTracking(e,t){if(!Z.isLongBlockingMcpToolName(e))return await t();this.activeBlockingToolCalls+=1;try{return await t()}finally{this.activeBlockingToolCalls-=1}}classifyToolError(e,t){const s=(t instanceof Error?t.message:String(t)).includes("timed out after")?"timeout":t instanceof Error&&t.name==="AbortError"?"aborted":"failed";return{toolCallId:e.toolCall.id,status:s,error:t}}async commitToolOutcome(e,t,o,s=[]){const i=e.tool,a=i?.kind==="mcp"?i.serverName:"builtin",r=i?.toolName??e.toolCall.function.name;if(t.status==="completed"&&t.resultText!==void 0){const c=t.resultText;i&&await this.hookBus.dispatch("PostToolUse",{source:i.kind==="mcp"?"mcp":"builtin",...i.kind==="mcp"?{serverName:i.serverName}:{},toolName:i.toolName,exposedName:i.exposedName,result:c}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:a,tool:r,toolUseId:e.toolCall.id,result:c,...e.batchId?{batchId:e.batchId}:{}}),this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:c});const m=await this.appendSyntheticMultimodalUserMessage(c,e.toolCall.id,o);m&&s.push(m);return}if(t.status==="superseded"){this.commitSupersededToolResult(e);return}const l=t.error??new Error("Tool execution failed"),u=l instanceof Error?l.message:String(l);if(this.abortRequested||this.state.terminated){this.emitAcodeEvent("mcp_status",{action:"tool_result",server:a,tool:r,toolUseId:e.toolCall.id,result:`[aborted] ${u}`,failed:!0,aborted:!0,...e.batchId?{batchId:e.batchId}:{}}),this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:`[aborted] ${u}`});return}if(!i){this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:u}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:a,tool:r,toolUseId:e.toolCall.id,result:u,failed:!0,...e.batchId?{batchId:e.batchId}:{}});return}this.appendToolFailureResult(e.toolCall,i,l,e.batchId)}commitSupersededToolResult(e){const t=e.immediateError!==void 0?`[cancelled] ${e.immediateError instanceof Error?e.immediateError.message:String(e.immediateError)}`:"[cancelled] Tool execution was superseded by a newer turn.";this.state.conversation.push({role:"tool",tool_call_id:e.toolCall.id,content:t}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:e.tool?.kind==="mcp"?e.tool.serverName:"builtin",tool:e.tool?.toolName??e.toolCall.function.name,toolUseId:e.toolCall.id,result:t,failed:!0,aborted:!0,...e.batchId?{batchId:e.batchId}:{}})}async executeSingleToolByName(e,t,o){let s=this.state.exposedTools.find(l=>l.exposedName===e);if(s||(s=this.state.exposedTools.find(l=>l.toolName===e)),!s)throw new Error(`Tool not found: ${e}`);if(s.kind==="builtin")return s.execute(t,{toolCallId:o?.toolCallId});const i=this.state.mcpConnections.find(l=>l.serverName===s.serverName);if(!i)throw new Error(`MCP server not found: ${s.serverName}`);const a=Fe(s.toolName),r=a?this.state.provider.mcpPollingToolTimeoutMs:ke(s.toolName)?0:this.state.provider.mcpToolTimeoutMs;if(a&&Me(s.toolName)==="poll_message"){const l=await this.withBlockingToolCallTracking(s.toolName,()=>this.callPollMessageWithSubAgentRace(i,s,t,r,{allowRepoll:!1}));return this.expandCommandsInPolledResult(W(l))}return this.withBlockingToolCallTracking(s.toolName,()=>this.callMcpToolWithTimeout(i,s,t,r,a))}async callMcpToolWithTimeout(e,t,o,s,i,a){const r=s>0?s:bo,u=!!this.config.sharedMcpConnections?{...o,_aws_sub_agent:!0}:o;for(;;){const c=this.createRuntimeStopWaiter(`ACode MCP tool ${t.serverName}/${t.toolName}`);try{return await x(Promise.race([e.client.callTool({name:t.toolName,arguments:u},void 0,{timeout:r,...a?{signal:a}:{}}),c.promise]),s,`ACode MCP tool ${t.serverName}/${t.toolName}`)}catch(m){if(a?.aborted||!i||s>0||!us(m))throw m;this.emitAcodeEvent("warning",{message:`ACode MCP polling tool ${t.serverName}/${t.toolName} reached the MCP SDK timeout window and will continue polling.`})}finally{c.cancel()}}}static formatSubAgentCompletionAsPollResult(e){const t=e.map(o=>{const s=o.error?`Sub-agent ${o.subId} [${o.status}]: ${o.description}
|
|
24
25
|
Error: ${o.error}`:`Sub-agent ${o.subId} [${o.status}]: ${o.description}
|
|
25
|
-
Output: ${o.output.length>500?o.output.slice(0,500)+"... (truncated)":o.output}`;return{msgId:`subagent-completion-${o.subId}-${Date.now()}`,senderId:"ACode-Runtime",senderName:"\u7CFB\u7EDF",content:s,requireReply:!1,prompt:`Sub-agent ${o.subId} (${o.description}) has reached terminal state: ${o.status}. Review its output and decide next steps.`,timestamp:new Date().toISOString()}});return{content:[{type:"text",text:JSON.stringify({directMessages:t,groupMessages:[],fromBuffer:!1,systemMessages:[]},null,2)}]}}static mergeSubAgentCompletionsIntoMcpResult(e,t){if(t.length===0)return e;const o=
|
|
26
|
-
Error: ${
|
|
27
|
-
Output: ${
|
|
26
|
+
Output: ${o.output.length>500?o.output.slice(0,500)+"... (truncated)":o.output}`;return{msgId:`subagent-completion-${o.subId}-${Date.now()}`,senderId:"ACode-Runtime",senderName:"\u7CFB\u7EDF",content:s,requireReply:!1,prompt:`Sub-agent ${o.subId} (${o.description}) has reached terminal state: ${o.status}. Review its output and decide next steps.`,timestamp:new Date().toISOString()}});return{content:[{type:"text",text:JSON.stringify({directMessages:t,groupMessages:[],fromBuffer:!1,systemMessages:[]},null,2)}]}}static mergeSubAgentCompletionsIntoMcpResult(e,t){if(t.length===0)return e;const o=W(e);let s;try{s=JSON.parse(o)}catch{return e}const a=[...t.map(r=>{const l=r.error?`Sub-agent ${r.subId} [${r.status}]: ${r.description}
|
|
27
|
+
Error: ${r.error}`:`Sub-agent ${r.subId} [${r.status}]: ${r.description}
|
|
28
|
+
Output: ${r.output.length>500?r.output.slice(0,500)+"... (truncated)":r.output}`;return{msgId:`subagent-completion-${r.subId}-${Date.now()}`,senderId:"ACode-Runtime",senderName:"\u7CFB\u7EDF",content:l,requireReply:!1,prompt:`Sub-agent ${r.subId} (${r.description}) has reached terminal state: ${r.status}. Review its output and decide next steps.`,timestamp:new Date().toISOString()}}),...s.directMessages??[]];return{content:[{type:"text",text:JSON.stringify({...s,directMessages:a},null,2)}]}}async recheckMemoryDelegate(e,t,o,s){const i=this.state.mcpConnections.find(a=>a.serverName==="aws-client-agent-mcp");if(!i)return p("[ACodeRuntime] recheckMemoryDelegate: aws-client-agent-mcp connection not found, treating as done"),{done:!0};try{return e==="memory_file_change"?await this.recheckMemoryFileChange(i,t):e==="memory_expiration"?await this.recheckMemoryExpiration(i,t):await this.recheckMemoryCleanup(i,t)}catch(a){return p(`[ACodeRuntime] recheckMemoryDelegate failed (type=${e}, turn=${s}): ${a instanceof Error?a.message:String(a)}, treating as NOT done (retry via recheck loop)`),{done:!1,continueMessage:`\u7ECF\u68C0\u67E5\uFF0C\u65E0\u6CD5\u786E\u8BA4\u4EE5\u4E0B memory \u662F\u5426\u5DF2\u5904\u7406\uFF08\u6821\u9A8C\u5F02\u5E38\uFF1A${a instanceof Error?a.message:String(a)}\uFF09\u3002\u8BF7\u91CD\u65B0\u6267\u884C\u9A8C\u8BC1\u6B65\u9AA4\uFF08read_memory / get_memory_stats\uFF09\u786E\u8BA4\u771F\u5B9E\u72B6\u6001\u540E\u518D\u7EE7\u7EED\u3002`}}}async readMemoryViaMcp(e,t){try{const o=await x(e.client.callTool({name:"read_memory",arguments:{uri:t,_aws_sub_agent:!0}},void 0,{timeout:3e4}),3e4,`recheck read_memory ${t}`),s=W(o),i=JSON.parse(s);return i==null||typeof i!="object"||Array.isArray(i)?null:i}catch(o){throw o}}async recheckMemoryFileChange(e,t){const o=[];if(await Promise.all(t.map(async i=>{const a=await this.readMemoryViaMcp(e,i);a&&a.memory_status==="updating"&&o.push(i)})),o.length===0)return p(`[ACodeRuntime] recheck memory_file_change: all ${t.length} URIs done (memory_status=normal or deleted)`),{done:!0};const s=o.map(i=>`- ${i}`).join(`
|
|
28
29
|
`);return p(`[ACodeRuntime] recheck memory_file_change: ${o.length}/${t.length} URIs still updating`),{done:!1,continueMessage:`\u7ECF\u68C0\u67E5\uFF0C\u672A\u5B8C\u6210\u5BF9\u4EE5\u4E0B memory \u7684\u66F4\u65B0\uFF0C\u8BF7\u7EE7\u7EED\uFF1A
|
|
29
30
|
${s}
|
|
30
31
|
|
|
31
|
-
\u8FD9\u4E9B memory \u7684 memory_status \u4ECD\u4E3A "updating"\u3002\u8BF7\u91CD\u65B0 read_memory \u67E5\u770B\u5185\u5BB9\uFF0C\u67E5\u770B\u7ED1\u5B9A\u6587\u4EF6\u6700\u65B0\u5185\u5BB9\uFF0C\u5E76\u8C03\u7528 update_memory \u5B8C\u6210\u66F4\u65B0\u3002`}}async recheckMemoryExpiration(e,t){const o=[],s=Date.now();if(await Promise.all(t.map(async
|
|
32
|
+
\u8FD9\u4E9B memory \u7684 memory_status \u4ECD\u4E3A "updating"\u3002\u8BF7\u91CD\u65B0 read_memory \u67E5\u770B\u5185\u5BB9\uFF0C\u67E5\u770B\u7ED1\u5B9A\u6587\u4EF6\u6700\u65B0\u5185\u5BB9\uFF0C\u5E76\u8C03\u7528 update_memory \u5B8C\u6210\u66F4\u65B0\u3002`}}async recheckMemoryExpiration(e,t){const o=[],s=Date.now();if(await Promise.all(t.map(async a=>{const r=await this.readMemoryViaMcp(e,a);if(!r)return;const l=r.expiresAt??r.expires_at,u=r.memory_status==="recalling",c=typeof l=="string"&&new Date(l).getTime()<s;(u||c)&&o.push(a)})),o.length===0)return p(`[ACodeRuntime] recheck memory_expiration: all ${t.length} URIs done (renewed, cleaned, or no longer expired)`),{done:!0};const i=o.map(a=>`- ${a}`).join(`
|
|
32
33
|
`);return p(`[ACodeRuntime] recheck memory_expiration: ${o.length}/${t.length} URIs still expired or recalling`),{done:!1,continueMessage:`\u7ECF\u68C0\u67E5\uFF0C\u672A\u5B8C\u6210\u5BF9\u4EE5\u4E0B memory \u7684\u5904\u7406\uFF0C\u8BF7\u7EE7\u7EED\uFF1A
|
|
33
|
-
${r}
|
|
34
|
-
|
|
35
|
-
\u8FD9\u4E9B memory \u4ECD\u5904\u4E8E\u8FC7\u671F\u6216\u56DE\u5FC6\u4E2D\u72B6\u6001\u3002\u8BF7\u8C03\u7528 memory_recurrence \u83B7\u53D6\u5185\u5BB9\uFF0C\u5BF9\u9700\u8981\u4FDD\u7559\u7684\u8C03\u7528 update_memory \u7EED\u671F\uFF0C\u5BF9\u65E0\u4EF7\u503C\u7684\u8C03\u7528 memory_recurrence_complete \u6E05\u7406\u3002`}}async recheckMemoryCleanup(e,t){let o;try{const a=await R(e.client.callTool({name:"get_memory_stats",arguments:{_aws_sub_agent:!0}},void 0,{timeout:3e4}),3e4,"recheck get_memory_stats"),l=G(a);o=JSON.parse(l)}catch{return{done:!1,continueMessage:"get_memory_stats \u67E5\u8BE2\u5931\u8D25\uFF0C\u65E0\u6CD5\u786E\u8BA4\u5BB9\u91CF\u662F\u5426\u5DF2\u8FBE\u6807\uFF0C\u8BF7\u91CD\u65B0\u9A8C\u8BC1\u540E\u518D\u7EE7\u7EED\u3002"}}const s=o.by_type??{},r=[];for(const a of t){const l=a.indexOf("/");if(l<0)continue;const u=a.slice(0,l),c=a.slice(l+1),h=s[u]?.by_domain?.[c];h?.exceeded&&r.push(`${u}/${c} (\u5F53\u524D ${h.count??"?"} \u6761\uFF0C\u5EFA\u8BAE\u4E0A\u9650 ${h.limit??"?"})`)}if(r.length===0)return p(`[ACodeRuntime] recheck memory_cleanup: all ${t.length} type/domain pairs within limits`),{done:!0};const i=r.map(a=>`- ${a}`).join(`
|
|
36
|
-
`);return p(`[ACodeRuntime] recheck memory_cleanup: ${r.length}/${t.length} type/domain pairs still exceeded`),{done:!1,continueMessage:`\u7ECF\u68C0\u67E5\uFF0C\u4EE5\u4E0B\u7C7B\u578B/\u57DF\u5C1A\u672A\u6574\u7406\u5B8C\u6210\uFF0C\u8BF7\u7EE7\u7EED\uFF1A
|
|
37
34
|
${i}
|
|
38
35
|
|
|
39
|
-
\
|
|
36
|
+
\u8FD9\u4E9B memory \u4ECD\u5904\u4E8E\u8FC7\u671F\u6216\u56DE\u5FC6\u4E2D\u72B6\u6001\u3002\u8BF7\u8C03\u7528 memory_recurrence \u83B7\u53D6\u5185\u5BB9\uFF0C\u5BF9\u9700\u8981\u4FDD\u7559\u7684\u8C03\u7528 update_memory \u7EED\u671F\uFF0C\u5BF9\u65E0\u4EF7\u503C\u7684\u8C03\u7528 memory_recurrence_complete \u6E05\u7406\u3002`}}async recheckMemoryCleanup(e,t){let o;try{const r=await x(e.client.callTool({name:"get_memory_stats",arguments:{_aws_sub_agent:!0}},void 0,{timeout:3e4}),3e4,"recheck get_memory_stats"),l=W(r);o=JSON.parse(l)}catch{return{done:!1,continueMessage:"get_memory_stats \u67E5\u8BE2\u5931\u8D25\uFF0C\u65E0\u6CD5\u786E\u8BA4\u5BB9\u91CF\u662F\u5426\u5DF2\u8FBE\u6807\uFF0C\u8BF7\u91CD\u65B0\u9A8C\u8BC1\u540E\u518D\u7EE7\u7EED\u3002"}}const s=o.by_type??{},i=[];for(const r of t){const l=r.indexOf("/");if(l<0)continue;const u=r.slice(0,l),c=r.slice(l+1),h=s[u]?.by_domain?.[c];h?.exceeded&&i.push(`${u}/${c} (\u5F53\u524D ${h.count??"?"} \u6761\uFF0C\u5EFA\u8BAE\u4E0A\u9650 ${h.limit??"?"})`)}if(i.length===0)return p(`[ACodeRuntime] recheck memory_cleanup: all ${t.length} type/domain pairs within limits`),{done:!0};const a=i.map(r=>`- ${r}`).join(`
|
|
37
|
+
`);return p(`[ACodeRuntime] recheck memory_cleanup: ${i.length}/${t.length} type/domain pairs still exceeded`),{done:!1,continueMessage:`\u7ECF\u68C0\u67E5\uFF0C\u4EE5\u4E0B\u7C7B\u578B/\u57DF\u5C1A\u672A\u6574\u7406\u5B8C\u6210\uFF0C\u8BF7\u7EE7\u7EED\uFF1A
|
|
38
|
+
${a}
|
|
39
|
+
|
|
40
|
+
\u8BF7\u8C03\u7528 list_memories \u67E5\u770B\u8D85\u9650\u8BB0\u5FC6\uFF0C\u5220\u9664\u4E0D\u518D\u9700\u8981\u7684 instance_memory \u6216\u5BF9 instance_long_memory \u8C03\u7528 update_memory \u7EED\u671F\u3002`}}buildMemoryDelegatePrompt(e){const t=e.uris.map(o=>`- ${o}`).join(`
|
|
40
41
|
`);return e.type==="memory_expiration"?`\u4F60\u662F\u4E00\u6761\u4E13\u95E8\u5904\u7406 memory \u8FC7\u671F\u6574\u7406\u7684\u5B50 agent\u3002
|
|
41
42
|
|
|
42
43
|
\u5F53\u524D\u6709\u4EE5\u4E0B ${e.uris.length} \u6761\u5373\u5C06\u9057\u5FD8\u7684\u8BB0\u5FC6\u9700\u8981\u6574\u7406\uFF1A
|
|
@@ -76,7 +77,7 @@ ${t}
|
|
|
76
77
|
=== \u5904\u7406\u9636\u6BB5 ===
|
|
77
78
|
1. \u8C03\u7528 get_memory_stats \u67E5\u770B\u5BB9\u91CF\u8BE6\u60C5
|
|
78
79
|
2. \u8C03\u7528 list_memories \u6309\u7C7B\u578B\u67E5\u770B\u8D85\u9650\u8BB0\u5FC6
|
|
79
|
-
3. \u5220\u9664\u4E0D\u518D\u9700\u8981\u7684
|
|
80
|
+
3. \u5220\u9664\u4E0D\u518D\u9700\u8981\u7684 instance_memory\uFF0C\u6216\u66F4\u65B0 instance_long_memory \u7EED\u671F
|
|
80
81
|
|
|
81
82
|
=== \u9A8C\u8BC1\u9636\u6BB5\uFF08\u5173\u952E\uFF0C\u4E0D\u53EF\u8DF3\u8FC7\uFF09 ===
|
|
82
83
|
4. \u518D\u6B21\u8C03\u7528 get_memory_stats \u68C0\u67E5\u5404\u7C7B\u578B/\u57DF\u7684 count \u662F\u5426\u5DF2\u964D\u5230\u5EFA\u8BAE\u4E0A\u9650\u4EE5\u4E0B
|
|
@@ -85,14 +86,14 @@ ${t}
|
|
|
85
86
|
- \u2705 \u82E5\u5168\u90E8\u5728\u5EFA\u8BAE\u4E0A\u9650\u5185\uFF1A\u9759\u9ED8\u7ED3\u675F
|
|
86
87
|
|
|
87
88
|
\u6CE8\u610F\uFF1A\u4F60\u8FD0\u884C\u5728\u72EC\u7ACB\u7684\u4E0A\u4E0B\u6587\u7A97\u53E3\u4E2D\uFF0C\u4E0D\u5F71\u54CD\u4E3B agent \u7684\u4E3B\u7EBF\u4EFB\u52A1\u3002\u8BF7\u9AD8\u6548\u5B8C\u6210\uFF0C\u4E0D\u8981\u8BE2\u95EE\u7528\u6237\u3002
|
|
88
|
-
\u5B8C\u6210\u540E\u7CFB\u7EDF\u4F1A\u901A\u8FC7\u67E5\u8BE2\u670D\u52A1\u7AEF\u771F\u5B9E\u72B6\u6001\u8FDB\u884C\u4E8C\u6B21\u9A8C\u8BC1\uFF08\u4E0D\u4F9D\u8D56\u4F60\u7684\u81EA\u62A5\u7ED3\u679C\uFF09\u3002\u82E5\u9A8C\u8BC1\u53D1\u73B0\u4ECD\u6709\u7C7B\u578B/\u57DF\u8D85\u8FC7\u5BB9\u91CF\u4E0A\u9650\uFF0C\u4F1A\u53D1\u9001 "\u7ECF\u68C0\u67E5\uFF0C\u4EE5\u4E0B\u7C7B\u578B/\u57DF\u5C1A\u672A\u6574\u7406\u5B8C\u6210\uFF0C\u8BF7\u7EE7\u7EED" \u6D88\u606F\u8BA9\u4F60\u7EE7\u7EED\u5904\u7406\u3002`:`\u4F60\u662F\u4E00\u6761\u4E13\u95E8\u5904\u7406\
|
|
89
|
+
\u5B8C\u6210\u540E\u7CFB\u7EDF\u4F1A\u901A\u8FC7\u67E5\u8BE2\u670D\u52A1\u7AEF\u771F\u5B9E\u72B6\u6001\u8FDB\u884C\u4E8C\u6B21\u9A8C\u8BC1\uFF08\u4E0D\u4F9D\u8D56\u4F60\u7684\u81EA\u62A5\u7ED3\u679C\uFF09\u3002\u82E5\u9A8C\u8BC1\u53D1\u73B0\u4ECD\u6709\u7C7B\u578B/\u57DF\u8D85\u8FC7\u5BB9\u91CF\u4E0A\u9650\uFF0C\u4F1A\u53D1\u9001 "\u7ECF\u68C0\u67E5\uFF0C\u4EE5\u4E0B\u7C7B\u578B/\u57DF\u5C1A\u672A\u6574\u7406\u5B8C\u6210\uFF0C\u8BF7\u7EE7\u7EED" \u6D88\u606F\u8BA9\u4F60\u7EE7\u7EED\u5904\u7406\u3002`:`\u4F60\u662F\u4E00\u6761\u4E13\u95E8\u5904\u7406\u9879\u76EE\u8BB0\u5FC6\u6587\u4EF6\u53D8\u66F4\u66F4\u65B0\u7684\u5B50 agent\u3002
|
|
89
90
|
|
|
90
|
-
\u4EE5\u4E0B ${e.uris.length} \u4E2A\
|
|
91
|
-
${e.uris.map(o=>{const s=e.fileChanges?.find(l=>l.uri===o);if(!s)return`- ${o}`;const
|
|
91
|
+
\u4EE5\u4E0B ${e.uris.length} \u4E2A\u9879\u76EE\u8BB0\u5FC6\u7ED1\u5B9A\u7684\u6587\u4EF6\u5DF2\u53D1\u751F\u53D8\u66F4\uFF1A
|
|
92
|
+
${e.uris.map(o=>{const s=e.fileChanges?.find(l=>l.uri===o);if(!s)return`- ${o}`;const i={no_change:"\u65E0\u53D8\u66F4",content_changed:"\u5185\u5BB9\u53D8\u66F4",not_found:"\u6587\u4EF6\u627E\u4E0D\u5230"},a=s.fileStatuses&&s.fileStatuses.length>0?s.fileStatuses:[],r=a.length>0?a.map(l=>{if(l.status==="no_change")return` \xB7 ${l.path}`;const u=i[l.status]??l.status,c=l.status==="content_changed"&&l.localMd5&&l.serverMd5?` \u672C\u5730: ${l.localMd5} Server: ${l.serverMd5}`:l.status==="not_found"&&l.serverMd5?` Server: ${l.serverMd5}`:"";return` \xB7 ${l.path}\uFF08${u}${c}\uFF09`}).join(`
|
|
92
93
|
`):s.boundFiles&&s.boundFiles.length>0?s.boundFiles.map(l=>` \xB7 ${l}`).join(`
|
|
93
94
|
`):" \u65E0\uFF08\u8BF7\u901A\u8FC7 read_memory \u67E5\u770B bound_files\uFF09";return`- ${o}
|
|
94
95
|
- \u7ED1\u5B9A\u6587\u4EF6:
|
|
95
|
-
${
|
|
96
|
+
${r}`}).join(`
|
|
96
97
|
`)}
|
|
97
98
|
|
|
98
99
|
\u8BF7\u6267\u884C\u4EE5\u4E0B\u6B65\u9AA4\uFF08\u6838\u5FC3\u903B\u8F91\uFF1A\u66F4\u65B0 \u2192 \u9A8C\u8BC1 \u2192 \u672A\u5B8C\u6210\u5219\u7EE7\u7EED\uFF09\uFF1A
|
|
@@ -110,5 +111,5 @@ ${a}`}).join(`
|
|
|
110
111
|
- \u2705 \u82E5\u5168\u90E8 memory_status \u4E3A "normal"\uFF1A\u9759\u9ED8\u7ED3\u675F
|
|
111
112
|
|
|
112
113
|
\u6CE8\u610F\uFF1A\u4F60\u8FD0\u884C\u5728\u72EC\u7ACB\u7684\u4E0A\u4E0B\u6587\u7A97\u53E3\u4E2D\uFF0C\u4E0D\u5F71\u54CD\u4E3B agent \u7684\u4E3B\u7EBF\u4EFB\u52A1\u3002\u8BF7\u9AD8\u6548\u5B8C\u6210\uFF0C\u4E0D\u8981\u8BE2\u95EE\u7528\u6237\u3002
|
|
113
|
-
\u5B8C\u6210\u540E\u7CFB\u7EDF\u4F1A\u901A\u8FC7\u67E5\u8BE2\u670D\u52A1\u7AEF\u771F\u5B9E\u72B6\u6001\u8FDB\u884C\u4E8C\u6B21\u9A8C\u8BC1\uFF08\u4E0D\u4F9D\u8D56\u4F60\u7684\u81EA\u62A5\u7ED3\u679C\uFF09\u3002\u82E5\u9A8C\u8BC1\u53D1\u73B0\u4ECD\u6709 memory \u7684 memory_status \u4E3A "updating"\uFF0C\u4F1A\u53D1\u9001 "\u7ECF\u68C0\u67E5\uFF0C\u672A\u5B8C\u6210\u5BF9\u4EE5\u4E0B memory \u7684\u66F4\u65B0\uFF0C\u8BF7\u7EE7\u7EED" \u6D88\u606F\u8BA9\u4F60\u7EE7\u7EED\u5904\u7406\u3002`}async processMemoryAutoDelegates(e){const t=this.config.subAgentManager;if(!t)return e;let o;try{o=G(e)}catch{return e}let s;try{s=JSON.parse(o)}catch{return e}const r=s.directMessages;if(!Array.isArray(r)||r.length===0)return e;const i=[];let a=0;for(const u of r){const c=u._acodeAutoDelegate;if(c&&(c.type==="memory_expiration"||c.type==="memory_file_change"||c.type==="memory_cleanup")){a++;const m=Date.now(),h=c.uris.filter(g=>this.pendingMemorySubAgents.has(g)).length,y=c.uris.filter(g=>{const T=this.memoryDelegateCooldownUntil.get(g);return T!==void 0&&m<T}).length,C=c.uris.filter(g=>{if(this.pendingMemorySubAgents.has(g))return!1;const T=this.memoryDelegateCooldownUntil.get(g);return!(T!==void 0&&m<T)});if(C.length===0){p(`[ACodeRuntime] Memory ${c.type} skipped: ${h} in-progress, ${y} in cooldown (total ${c.uris.length} URIs)`);continue}const d=Array.isArray(c.fileChanges)?c.fileChanges.filter(g=>C.includes(g.uri)):void 0,f=this.buildMemoryDelegatePrompt({type:c.type,uris:C,fileChanges:d});try{const g=eo("explore",[],Ue(await this.buildAllocatableToolList())).patterns,T=await t.spawn({parentAgentId:this.config.agentId||"unknown",parentSessionId:this.config.sessionId,subagentType:Zt,description:`Memory ${c.type} auto-delegate (${C.length} items)`,prompt:f,...g.length>0?{allowedTools:g}:{},workingDirectory:this.config.workingDirectory,parentConfig:{...this.config,sharedMcpConnections:this.state?.mcpConnections},silent:!0});for(const k of C)this.pendingMemorySubAgents.set(k,T);const A=new Set(C);t.subscribe(T,()=>{const k=t.getSubAgent(T);if(k&&(k.status==="completed"||k.status==="failed"||k.status==="cancelled")){const M=Date.now();for(const I of A)this.pendingMemorySubAgents.get(I)===T&&this.pendingMemorySubAgents.delete(I),this.memoryDelegateCooldownUntil.set(I,M+Ve);for(const[I,x]of this.memoryDelegateCooldownUntil)x<=M&&this.memoryDelegateCooldownUntil.delete(I);p(`[ACodeRuntime] Memory delegate sub-agent ${T} finished (${k.status}), ${A.size} URI(s) entered ${Ve}ms cooldown`)}});const w=c.type,v=async(k,M)=>this.recheckMemoryDelegate(w,C,k,M);await t.startWithRecheck(T,v,3),p(`[ACodeRuntime] Auto-delegated memory ${c.type} to sub-agent ${T} (${C.length} URIs, ${c.uris.length-C.length} already in progress)`)}catch(g){for(const T of C)this.pendingMemorySubAgents.delete(T);p(`[ACodeRuntime] Failed to auto-delegate memory ${c.type} (suppressed, will retry next poll): ${g instanceof Error?g.message:String(g)}`)}}else i.push(u)}if(a===0)return e;if(i.length===0)return null;const l={...s,directMessages:i};return{content:[{type:"text",text:JSON.stringify(l,null,2)}]}}async callPollMessageWithSubAgentRace(e,t,o,s,r={}){const i=this.config.subAgentManager;if(!i)return U("runtime.poll.no-subagent-manager","\u65E0 SubAgentManager\uFF0C\u76F4\u63A5\u8D70\u666E\u901A MCP \u8C03\u7528"),await this.callMcpToolWithTimeout(e,t,o,s,!0);const a=r.allowRepoll!==!1,l=i.takeCompletedSubAgents();if(l.length>0)return p(`[ACodeRuntime] poll_message: ${l.length} sub-agent(s) already completed, returning synthetic result`),U("runtime.poll.subagent-synthetic",`${l.length} \u4E2A\u5B50 agent \u5DF2\u5B8C\u6210\uFF0C\u76F4\u63A5\u8FD4\u56DE\u5408\u6210\u7ED3\u679C\uFF08\u4E0D\u8C03 MCP\uFF09`),X.formatSubAgentCompletionAsPollResult(l);let u=!1,c=0;for(;;){c++,U("runtime.poll.round-start",`\u7B2C ${c} \u8F6E race \u5F00\u59CB: timeoutMs=${s} silent=${u}`);const m=new AbortController,h=i.waitForAnyCompletion(),y=u?{...o,_aws_silent_poll:!0}:o,C=Date.now(),d=this.callMcpToolWithTimeout(e,t,y,s,!0,m.signal);try{const f=await Promise.race([d.then(A=>({type:"mcp",result:A})),h.promise.then(A=>({type:"subagent",notif:A}))]);if(f.type==="subagent"){U("runtime.poll.race-winner-subagent",`\u5B50 agent \u5148\u5B8C\u6210\uFF0Cabort MCP \u8C03\u7528\uFF08\u5DF2\u7B49\u5F85 ${Date.now()-C}ms\uFF09\u540E\u8FD4\u56DE\u5408\u6210\u7ED3\u679C`),m.abort(),d.catch(()=>{});const A=i.takeCompletedSubAgents();return A.length===0&&A.push(f.notif),p(`[ACodeRuntime] poll_message: sub-agent ${f.notif.subId} completed during race, returning synthetic result (${A.length} notification(s))`),X.formatSubAgentCompletionAsPollResult(A)}U("runtime.poll.mcp-returned",`MCP \u5148\u8FD4\u56DE\uFF08\u8017\u65F6 ${Date.now()-C}ms\uFF09\uFF0C\u68C0\u67E5\u5F85\u5408\u5E76\u7684\u5B50 agent \u5B8C\u6210`);const g=i.takeCompletedSubAgents();if(g.length>0){p(`[ACodeRuntime] poll_message: MCP returned, merging ${g.length} sub-agent completion(s)`);const A=X.mergeSubAgentCompletionsIntoMcpResult(f.result,g),w=await this.processMemoryAutoDelegates(A);if(U("runtime.poll.delegate-check",`\u5408\u5E76 ${g.length} \u4E2A\u5B50 agent \u5B8C\u6210\u540E\u59D4\u6258\u68C0\u67E5: processed=${w===null?"\u5168\u90E8\u88AB\u59D4\u6258(null)":"\u6709\u53EF\u4EA4\u4ED8"}`),w!==null){if(this.pollResultHasMessages(w))return U("runtime.poll.final-return",`\u7B2C ${c} \u8F6E\uFF1A\u8FD4\u56DE\uFF08MCP+\u5B50agent\u5408\u5E76\u7ED3\u679C\uFF09`),w;U("runtime.poll.empty-repoll",`\u7B2C ${c} \u8F6E\uFF1A\u5408\u5E76\u7ED3\u679C\u4E3A\u7A7A\uFF0C\u7B49\u5F85 ${ge}ms \u540E\u91CD\u65B0 poll`)}if(!a)return w;await this.waitForDelegateRepoll(),u=!0;continue}const T=await this.processMemoryAutoDelegates(f.result);if(U("runtime.poll.delegate-check",`\u59D4\u6258\u68C0\u67E5: processed=${T===null?"\u5168\u90E8\u88AB\u59D4\u6258(null)":"\u6709\u53EF\u4EA4\u4ED8"}`),T!==null){if(this.pollResultHasMessages(T)){const A=this.summarizePollResultForDebug(T);return U("runtime.poll.final-return",`\u7B2C ${c} \u8F6E\uFF1A\u8FD4\u56DE poll \u7ED3\u679C\u7ED9 LLM: ${A}`),T}U("runtime.poll.empty-repoll",`\u7B2C ${c} \u8F6E\uFF1Apoll \u7ED3\u679C\u65E0\u5B9E\u9645\u6D88\u606F\uFF08directMessages/groupMessages \u5747\u7A7A\uFF09\uFF0C\u7B49\u5F85 ${ge}ms \u540E\u91CD\u65B0 poll`)}if(!a)return U("runtime.poll.delegate-return-no-repoll","\u975E\u9876\u5C42\u8F6E\u8BE2\u5165\u53E3\uFF1A\u59D4\u6258\u5DF2\u5B8C\u6210\uFF0C\u8FD4\u56DE\u539F\u59CB poll \u7ED3\u679C\uFF08\u4E0D\u91CD\u65B0 poll\uFF09"),f.result;await this.waitForDelegateRepoll(),u=!0}catch(f){throw U("runtime.poll.error",`\u7B2C ${c} \u8F6E MCP \u8C03\u7528\u5F02\u5E38: ${f instanceof Error?f.message:String(f)}`),f}finally{h.cancel()}}}summarizePollResultForDebug(e){try{const t=G(e),o=JSON.parse(t),s=Array.isArray(o.directMessages)?o.directMessages:[],r=Array.isArray(o.groupMessages)?o.groupMessages:[];return`dm=${s.length}${s.length>0?JSON.stringify(s.map(i=>({id:i.msgId,ch:i.channel}))):""} gm=${r.length}`}catch{return"(\u65E0\u6CD5\u89E3\u6790\u4E3A JSON)"}}pollResultHasMessages(e){try{const t=G(e),o=JSON.parse(t),s=Array.isArray(o.directMessages)?o.directMessages.length:0,r=Array.isArray(o.groupMessages)?o.groupMessages.length:0;return s>0||r>0}catch{return!0}}async waitForDelegateRepoll(){p(`[ACodeRuntime] poll_message: all messages delegated, waiting ${ge}ms before re-poll`);const e=this.createRuntimeStopWaiter("delegate re-poll delay");try{await Promise.race([new Promise(t=>{const o=setTimeout(t,ge);o.unref&&o.unref()}),e.promise])}finally{e.cancel()}}createRuntimeStopWaiter(e){if(this.state.terminated)return{promise:Promise.reject(new Error(`${e} interrupted because ACode runtime stopped`)),cancel:()=>{}};let t;const o=new Promise((s,r)=>{t=()=>{r(new Error(`${e} interrupted because ACode runtime stopped`))}});return t?(this.stopWaiters.add(t),{promise:o,cancel:()=>{t&&this.stopWaiters.delete(t)}}):{promise:o,cancel:()=>{}}}notifyRuntimeStop(){const e=[...this.stopWaiters];this.stopWaiters.clear();for(const t of e)t()}async appendSyntheticMultimodalUserMessage(e,t,o=this.contextVersion){const s=Ee(e);{const i=e.slice(0,200);let a="unknown";try{const l=JSON.parse(e);l&&typeof l=="object"&&!Array.isArray(l)?a=`keys=[${Object.keys(l).join(",")}]`:Array.isArray(l)?a="array":a=typeof l}catch{a="non-json"}ye({stage:"entry",toolCallId:t.slice(0,12),contentLen:e.length,topLevelShape:a,prefix:i})}if(!s.hasImages){let i=null;try{const a=JSON.parse(e);if(a&&typeof a=="object"&&!Array.isArray(a)){const l=[a.directMessages,a.groupMessages];i=[];for(const u of l)if(Array.isArray(u))for(const c of u){if(!c||typeof c!="object")continue;const m=c,h=typeof m.content=="string"?m.content:"",y={senderName:typeof m.senderName=="string"?m.senderName:"",msgId:typeof m.msgId=="string"?m.msgId:"",contentLen:h.length};let C=null;try{const d=JSON.parse(h);d&&typeof d=="object"&&!Array.isArray(d)&&(C=d)}catch{}C?(y.contentKind="json",y.hasAttachmentsField=Array.isArray(C.attachments),y.attachmentCount=Array.isArray(C.attachments)?C.attachments.length:0,y.attachmentKinds=Array.isArray(C.attachments)?C.attachments.map(d=>String(d.type??d.kind??"").toLowerCase()||"(empty)"):[],y.attachmentBase64Lens=Array.isArray(C.attachments)?C.attachments.map(d=>(typeof d.base64Data=="string"?d.base64Data:typeof d.base64=="string"?d.base64:typeof d.data=="string"?d.data:"").length):[]):(y.contentKind="non-json",y.contentPrefix=h.slice(0,60)),i.push(y)}}}catch{}return ye({stage:"hasImages_false",toolCallId:t.slice(0,12),contentLen:e.length,parsedContentKind:Array.isArray(s.content)?"parts":typeof s.content,partsCount:Array.isArray(s.content)?s.content.length:null,dmProbe:i}),null}if(!this.state.provider.supportsImageInput)return this.emitAcodeEvent("error",{message:"ACode skipped image content from tool result because the provider/model is not configured for image input."}),ye({stage:"supportsImageInput_false",toolCallId:t.slice(0,12),providerSupportsImageInput:this.state.provider.supportsImageInput,partsCount:Array.isArray(s.content)?s.content.length:null}),this.cleanToolMessageAttachments(t,"[Tool result contained images; skipped because the current model does not support image input]"),null;let r;try{r=await it(s.content)}catch(i){const a=i instanceof Error?i.message:String(i),l=Array.isArray(s.content)?s.content.filter(u=>u.type==="image_url").map(u=>{const c=u.image_url.url,m=c.indexOf(",");return{urlHead:c.slice(0,Math.min(30,c.length)),base64Len:m>=0?c.length-m-1:null,isDataUrl:/^data:image\//i.test(c)}}):null;ye({stage:"materialize_threw",toolCallId:t.slice(0,12),errMessage:a,imagePartsCount:l?l.length:0,imageParts:l}),this.emitAcodeEvent("error",{message:`ACode skipped image content from tool result: ${a}`}),r=Array.isArray(s.content)?s.content.map(u=>u.type==="text"?u:{type:"text",text:`[Image omitted: ${a}]`}):s.content}return this.isTurnSuperseded(o)?null:(this.cleanToolMessageAttachments(t,"[Tool result contained images; see user message above]"),{role:"user",content:r})}expandCommandsInPolledResult(e){const t=this.state.commandRegistry;if(!t||t.list().length===0)return e;let o;try{o=JSON.parse(e)}catch{return e}if(!S(o))return e;let s=!1;for(const r of["directMessages","groupMessages"]){const i=o[r];if(Array.isArray(i))for(const a of i){if(!S(a))continue;const l=O(a.content);if(!l)continue;const u=t.expand(l);u&&(a.content=u.prompt,s=!0)}}return s?JSON.stringify(o):e}cleanToolMessageAttachments(e,t){const o=this.state.conversation.find(r=>r.role==="tool"&&r.tool_call_id===e);if(!o||typeof o.content!="string")return;let s;try{const r=JSON.parse(o.content);if(typeof r=="object"&&r!==null){const i={...r};"attachments"in i&&delete i.attachments,Fo(i),s=JSON.stringify(i)}}catch{}if(s===void 0){const r=o.content.replace(/\[(图片|文件)\s*:\s*([^\]\n]*?)\s*\]/gi,"").trim();r!==o.content&&(s=r)}s!==void 0&&(o.content=s.length>0?s:t)}appendToolFailureResult(e,t,o,s){const r=ut(t,o),i=wt(o),a=i?ut(t,o,i):r;p(`[ACodeRuntime] \u{1F4E4} appendToolFailureResult: emitting mcp_status tool_result (failed: true) for ${t.exposedName}, result length: ${r.length} chars`+(i?`, conversation snapshot: ${i.bytes} bytes`:"")),this.emitAcodeEvent("error",{message:`ACode tool failed: ${t.exposedName}: ${ue(o)}`,nonFatal:!0}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:t.kind==="mcp"?t.serverName:"builtin",tool:t.toolName,toolUseId:e.id,result:r,failed:!0,...s?{batchId:s}:{}}),this.state.conversation.push({role:"tool",tool_call_id:e.id,content:a})}filterSharedMcpConnectionsForSubAgent(e){const t=this.config.subAgentAllowedTools&&this.config.subAgentAllowedTools.length>0?this.config.subAgentAllowedTools:null;return e.map(o=>({...o,tools:o.tools.filter(s=>t?qe(s,t):to.has(s.toolName))}))}async connectMcpServers(e,t){const o=[];for(const[s,r]of Object.entries(e))try{const i=new Tt({name:"acode",version:"0.1.0"}),a=this.createMcpTransport(r);await R(i.connect(a),t,`ACode MCP server ${s} connect`);const l={serverName:s,client:i,transport:a,tools:[]};i.setNotificationHandler(At,()=>{this.refreshMcpConnectionTools(l,t)});const{tools:u}=await R(i.listTools(),t,`ACode MCP server ${s} listTools`);l.tools=We(s,u);try{l.instructions=i.getInstructions()}catch{}l.spec=r,o.push(l)}catch(i){this.emitAcodeEvent("error",{message:`ACode MCP server failed to connect: ${s}: ${i instanceof Error?i.message:String(i)}`})}return o}registerMcpCollapsedGroup(e,t,o){if(!this.toolGroupManager||t.collapsed!==!0||o.tools.length===0)return;const s=Ut(e),r=this.toolGroupManager.getRegistration(s),i=t.description?.trim()||po(o.instructions)||`MCP \u5DE5\u5177\u96C6\uFF1A${e}`,a=this.toolGroupManager.claimedToolNamesBySubGroups(),l=o.tools.map(u=>u.toolName).filter(u=>!a.has(u));l.length!==0&&(this.toolGroupManager.register({key:s,label:t.description?.trim()||e,summary:i,collapsible:!0,toolNames:l,serverName:e}),this.emitAcodeEvent("mcp_status",{serverName:e,status:"collapsed_group_registered",toolCount:l.length,updated:!!r}))}registerToolGroupsFromMcp(e){if(!this.toolGroupManager)return!1;const t=mo(e.instructions);if(t.length===0)return!1;let o=0;for(const s of t)this.toolGroupManager.getRegistration(s.key)||(this.toolGroupManager.register({key:s.key,label:s.label,summary:"",collapsible:!0,toolNames:s.toolNames}),o+=1);return this.emitAcodeEvent("mcp_status",{serverName:e.serverName,status:"tool_groups_registered",declared:t.length,registered:o}),!0}requestMcpToolsRefresh(e){const t=this.state.mcpConnections.filter(o=>!e||o.serverName===e);for(const o of t)this.refreshMcpConnectionTools(o,this.state.provider.mcpToolTimeoutMs)}async recoverHiddenMcpTool(e){if(this.recoveredToolNames.has(e)||this.recoveredToolNames.size>=To)return!1;this.recoveredToolNames.add(e);const t=this.state.mcpConnections.filter(o=>o.tools.some(s=>s.toolName==="expand_tools"||s.exposedName==="expand_tools"||s.exposedName.endsWith("__expand_tools")));if(t.length===0)return!1;for(const o of t)await this.refreshMcpConnectionTools(o,this.state.provider.mcpToolTimeoutMs);return this.state.exposedTools.some(o=>o.toolName===e||o.exposedName===e)}async refreshMcpConnectionTools(e,t){try{const{tools:s}=await R(e.client.listTools(),t,`ACode MCP server ${e.serverName} listTools refresh`);e.tools=We(e.serverName,s)}catch(s){this.emitAcodeEvent("warning",{message:`ACode MCP server ${e.serverName} tools/list_changed refresh failed: ${s instanceof Error?s.message:String(s)}`});return}if(!this.exposedToolsInitialized)return;const o=this.state.exposedTools.filter(s=>!(s.kind==="mcp"&&s.serverName===e.serverName));this.state.exposedTools=[...o,...e.tools],this.emitExposedToolsStatus()}createMcpTransport(e){if(e.type==="stdio"||e.command){if(!e.command)throw new Error("stdio MCP server requires command");const o=new Ct({command:e.command,args:e.args,cwd:e.cwd||this.config.workingDirectory,env:{...this.config.env??{},...e.env??{}},stderr:"pipe"});return o.stderr?.on("data",()=>{}),o}if(!e.url)throw new Error("remote MCP server requires url");const t=e.headers?{headers:e.headers}:void 0;return e.type==="sse"?new yt(new URL(e.url),{requestInit:t}):new _t(new URL(e.url),{requestInit:t})}async fetchProfilePrompt(e){for(const t of e)if(t.tools.find(s=>s.toolName==="get_profile"))try{const s=await R(t.client.callTool({name:"get_profile",arguments:{}},void 0,{timeout:fe}),fe,"ACode startup get_profile"),r=this.extractProfileFromToolResult(s);return us(r)}catch(s){return this.emitAcodeEvent("warning",{message:`ACode failed to fetch profile via ${t.serverName}/get_profile: ${s instanceof Error?s.message:String(s)}`}),""}return""}extractProfileFromToolResult(e){if(!S(e))return null;const t=e.content;if(Array.isArray(t)){for(const o of t)if(S(o)&&typeof o.text=="string"&&o.text.trim())try{const s=JSON.parse(o.text);if(S(s))return s}catch{}}return S(e)?e:null}async disconnectMcpServers(){if(!this.state.ownsMcpConnections){this.state.mcpConnections=[],this.state.exposedTools=[],this.exposedToolsInitialized=!1;return}const e=this.state.mcpConnections;this.state.mcpConnections=[],this.state.exposedTools=[],this.exposedToolsInitialized=!1,await Promise.all(e.map(t=>ae(`ACode MCP server ${t.serverName} close`,t.transport.close(),o=>{this.emitAcodeEvent("error",{message:o})},this.state.provider.mcpToolTimeoutMs)))}}X.SKILL_REFRESH_DEBOUNCE_MS=500;export{X as ACodeRuntime,he as TOOL_GROUP_MANIFEST_PREFIX,bo as hasExplicitRequestBodyByteCap,po as humanReadableInstructions,mo as parseToolGroupManifest,wo as resolveMaxRequestBodyBytes,ut as toToolErrorResultText};
|
|
114
|
+
\u5B8C\u6210\u540E\u7CFB\u7EDF\u4F1A\u901A\u8FC7\u67E5\u8BE2\u670D\u52A1\u7AEF\u771F\u5B9E\u72B6\u6001\u8FDB\u884C\u4E8C\u6B21\u9A8C\u8BC1\uFF08\u4E0D\u4F9D\u8D56\u4F60\u7684\u81EA\u62A5\u7ED3\u679C\uFF09\u3002\u82E5\u9A8C\u8BC1\u53D1\u73B0\u4ECD\u6709 memory \u7684 memory_status \u4E3A "updating"\uFF0C\u4F1A\u53D1\u9001 "\u7ECF\u68C0\u67E5\uFF0C\u672A\u5B8C\u6210\u5BF9\u4EE5\u4E0B memory \u7684\u66F4\u65B0\uFF0C\u8BF7\u7EE7\u7EED" \u6D88\u606F\u8BA9\u4F60\u7EE7\u7EED\u5904\u7406\u3002`}async processMemoryAutoDelegates(e){const t=this.config.subAgentManager;if(!t)return e;let o;try{o=W(e)}catch{return e}let s;try{s=JSON.parse(o)}catch{return e}const i=s.directMessages;if(!Array.isArray(i)||i.length===0)return e;const a=[];let r=0;for(const u of i){const c=u._acodeAutoDelegate;if(c&&(c.type==="memory_expiration"||c.type==="memory_file_change"||c.type==="memory_cleanup")){r++;const m=Date.now(),h=c.uris.filter(g=>this.pendingMemorySubAgents.has(g)).length,y=c.uris.filter(g=>{const C=this.memoryDelegateCooldownUntil.get(g);return C!==void 0&&m<C}).length,_=c.uris.filter(g=>{if(this.pendingMemorySubAgents.has(g))return!1;const C=this.memoryDelegateCooldownUntil.get(g);return!(C!==void 0&&m<C)});if(_.length===0){p(`[ACodeRuntime] Memory ${c.type} skipped: ${h} in-progress, ${y} in cooldown (total ${c.uris.length} URIs)`);continue}const d=Array.isArray(c.fileChanges)?c.fileChanges.filter(g=>_.includes(g.uri)):void 0,f=this.buildMemoryDelegatePrompt({type:c.type,uris:_,fileChanges:d});try{const g=ro("explore",[],ze(await this.buildAllocatableToolList())).patterns,C=await t.spawn({parentAgentId:this.config.agentId||"unknown",parentSessionId:this.config.sessionId,subagentType:io,description:`Memory ${c.type} auto-delegate (${_.length} items)`,prompt:f,...g.length>0?{allowedTools:g}:{},workingDirectory:this.config.workingDirectory,parentConfig:{...this.config,sharedMcpConnections:this.state?.mcpConnections},silent:!0});for(const $ of _)this.pendingMemorySubAgents.set($,C);const w=new Set(_);t.subscribe(C,()=>{const $=t.getSubAgent(C);if($&&($.status==="completed"||$.status==="failed"||$.status==="cancelled")){const S=Date.now();for(const v of w)this.pendingMemorySubAgents.get(v)===C&&this.pendingMemorySubAgents.delete(v),this.memoryDelegateCooldownUntil.set(v,S+et);for(const[v,E]of this.memoryDelegateCooldownUntil)E<=S&&this.memoryDelegateCooldownUntil.delete(v);p(`[ACodeRuntime] Memory delegate sub-agent ${C} finished (${$.status}), ${w.size} URI(s) entered ${et}ms cooldown`)}});const b=c.type,M=async($,S)=>this.recheckMemoryDelegate(b,_,$,S);await t.startWithRecheck(C,M,3),p(`[ACodeRuntime] Auto-delegated memory ${c.type} to sub-agent ${C} (${_.length} URIs, ${c.uris.length-_.length} already in progress)`)}catch(g){for(const C of _)this.pendingMemorySubAgents.delete(C);p(`[ACodeRuntime] Failed to auto-delegate memory ${c.type} (suppressed, will retry next poll): ${g instanceof Error?g.message:String(g)}`)}}else a.push(u)}if(r===0)return e;if(a.length===0)return null;const l={...s,directMessages:a};return{content:[{type:"text",text:JSON.stringify(l,null,2)}]}}async callPollMessageWithSubAgentRace(e,t,o,s,i={}){const a=this.config.subAgentManager;if(!a)return D("runtime.poll.no-subagent-manager","\u65E0 SubAgentManager\uFF0C\u76F4\u63A5\u8D70\u666E\u901A MCP \u8C03\u7528"),await this.callMcpToolWithTimeout(e,t,o,s,!0);const r=i.allowRepoll!==!1,l=a.takeCompletedSubAgents();if(l.length>0)return p(`[ACodeRuntime] poll_message: ${l.length} sub-agent(s) already completed, returning synthetic result`),D("runtime.poll.subagent-synthetic",`${l.length} \u4E2A\u5B50 agent \u5DF2\u5B8C\u6210\uFF0C\u76F4\u63A5\u8FD4\u56DE\u5408\u6210\u7ED3\u679C\uFF08\u4E0D\u8C03 MCP\uFF09`),Z.formatSubAgentCompletionAsPollResult(l);let u=!1,c=0;for(;;){c++,D("runtime.poll.round-start",`\u7B2C ${c} \u8F6E race \u5F00\u59CB: timeoutMs=${s} silent=${u}`);const m=new AbortController,h=a.waitForAnyCompletion(),y=u?{...o,_aws_silent_poll:!0}:o,_=Date.now(),d=this.callMcpToolWithTimeout(e,t,y,s,!0,m.signal);try{const f=await Promise.race([d.then(w=>({type:"mcp",result:w})),h.promise.then(w=>({type:"subagent",notif:w}))]);if(f.type==="subagent"){D("runtime.poll.race-winner-subagent",`\u5B50 agent \u5148\u5B8C\u6210\uFF0Cabort MCP \u8C03\u7528\uFF08\u5DF2\u7B49\u5F85 ${Date.now()-_}ms\uFF09\u540E\u8FD4\u56DE\u5408\u6210\u7ED3\u679C`),m.abort(),d.catch(()=>{});const w=a.takeCompletedSubAgents();return w.length===0&&w.push(f.notif),p(`[ACodeRuntime] poll_message: sub-agent ${f.notif.subId} completed during race, returning synthetic result (${w.length} notification(s))`),Z.formatSubAgentCompletionAsPollResult(w)}D("runtime.poll.mcp-returned",`MCP \u5148\u8FD4\u56DE\uFF08\u8017\u65F6 ${Date.now()-_}ms\uFF09\uFF0C\u68C0\u67E5\u5F85\u5408\u5E76\u7684\u5B50 agent \u5B8C\u6210`);const g=a.takeCompletedSubAgents();if(g.length>0){p(`[ACodeRuntime] poll_message: MCP returned, merging ${g.length} sub-agent completion(s)`);const w=Z.mergeSubAgentCompletionsIntoMcpResult(f.result,g),b=await this.processMemoryAutoDelegates(w);if(D("runtime.poll.delegate-check",`\u5408\u5E76 ${g.length} \u4E2A\u5B50 agent \u5B8C\u6210\u540E\u59D4\u6258\u68C0\u67E5: processed=${b===null?"\u5168\u90E8\u88AB\u59D4\u6258(null)":"\u6709\u53EF\u4EA4\u4ED8"}`),b!==null){if(this.pollResultHasMessages(b))return D("runtime.poll.final-return",`\u7B2C ${c} \u8F6E\uFF1A\u8FD4\u56DE\uFF08MCP+\u5B50agent\u5408\u5E76\u7ED3\u679C\uFF09`),b;D("runtime.poll.empty-repoll",`\u7B2C ${c} \u8F6E\uFF1A\u5408\u5E76\u7ED3\u679C\u4E3A\u7A7A\uFF0C\u7B49\u5F85 ${Te}ms \u540E\u91CD\u65B0 poll`)}if(!r)return b;await this.waitForDelegateRepoll(),u=!0;continue}const C=await this.processMemoryAutoDelegates(f.result);if(D("runtime.poll.delegate-check",`\u59D4\u6258\u68C0\u67E5: processed=${C===null?"\u5168\u90E8\u88AB\u59D4\u6258(null)":"\u6709\u53EF\u4EA4\u4ED8"}`),C!==null){if(this.pollResultHasMessages(C)){const w=this.summarizePollResultForDebug(C);return D("runtime.poll.final-return",`\u7B2C ${c} \u8F6E\uFF1A\u8FD4\u56DE poll \u7ED3\u679C\u7ED9 LLM: ${w}`),C}D("runtime.poll.empty-repoll",`\u7B2C ${c} \u8F6E\uFF1Apoll \u7ED3\u679C\u65E0\u5B9E\u9645\u6D88\u606F\uFF08directMessages/groupMessages \u5747\u7A7A\uFF09\uFF0C\u7B49\u5F85 ${Te}ms \u540E\u91CD\u65B0 poll`)}if(!r)return D("runtime.poll.delegate-return-no-repoll","\u975E\u9876\u5C42\u8F6E\u8BE2\u5165\u53E3\uFF1A\u59D4\u6258\u5DF2\u5B8C\u6210\uFF0C\u8FD4\u56DE\u539F\u59CB poll \u7ED3\u679C\uFF08\u4E0D\u91CD\u65B0 poll\uFF09"),f.result;await this.waitForDelegateRepoll(),u=!0}catch(f){throw D("runtime.poll.error",`\u7B2C ${c} \u8F6E MCP \u8C03\u7528\u5F02\u5E38: ${f instanceof Error?f.message:String(f)}`),f}finally{h.cancel()}}}summarizePollResultForDebug(e){try{const t=W(e),o=JSON.parse(t),s=Array.isArray(o.directMessages)?o.directMessages:[],i=Array.isArray(o.groupMessages)?o.groupMessages:[];return`dm=${s.length}${s.length>0?JSON.stringify(s.map(a=>({id:a.msgId,ch:a.channel}))):""} gm=${i.length}`}catch{return"(\u65E0\u6CD5\u89E3\u6790\u4E3A JSON)"}}pollResultHasMessages(e){try{const t=W(e),o=JSON.parse(t),s=Array.isArray(o.directMessages)?o.directMessages.length:0,i=Array.isArray(o.groupMessages)?o.groupMessages.length:0;return s>0||i>0}catch{return!0}}async waitForDelegateRepoll(){p(`[ACodeRuntime] poll_message: all messages delegated, waiting ${Te}ms before re-poll`);const e=this.createRuntimeStopWaiter("delegate re-poll delay");try{await Promise.race([new Promise(t=>{const o=setTimeout(t,Te);o.unref&&o.unref()}),e.promise])}finally{e.cancel()}}createRuntimeStopWaiter(e){if(this.state.terminated)return{promise:Promise.reject(new Error(`${e} interrupted because ACode runtime stopped`)),cancel:()=>{}};let t;const o=new Promise((s,i)=>{t=()=>{i(new Error(`${e} interrupted because ACode runtime stopped`))}});return t?(this.stopWaiters.add(t),{promise:o,cancel:()=>{t&&this.stopWaiters.delete(t)}}):{promise:o,cancel:()=>{}}}notifyRuntimeStop(){const e=[...this.stopWaiters];this.stopWaiters.clear();for(const t of e)t()}async appendSyntheticMultimodalUserMessage(e,t,o=this.contextVersion){const s=Ee(e);{const a=e.slice(0,200);let r="unknown";try{const l=JSON.parse(e);l&&typeof l=="object"&&!Array.isArray(l)?r=`keys=[${Object.keys(l).join(",")}]`:Array.isArray(l)?r="array":r=typeof l}catch{r="non-json"}Ce({stage:"entry",toolCallId:t.slice(0,12),contentLen:e.length,topLevelShape:r,prefix:a})}if(!s.hasImages){let a=null;try{const r=JSON.parse(e);if(r&&typeof r=="object"&&!Array.isArray(r)){const l=[r.directMessages,r.groupMessages];a=[];for(const u of l)if(Array.isArray(u))for(const c of u){if(!c||typeof c!="object")continue;const m=c,h=typeof m.content=="string"?m.content:"",y={senderName:typeof m.senderName=="string"?m.senderName:"",msgId:typeof m.msgId=="string"?m.msgId:"",contentLen:h.length};let _=null;try{const d=JSON.parse(h);d&&typeof d=="object"&&!Array.isArray(d)&&(_=d)}catch{}_?(y.contentKind="json",y.hasAttachmentsField=Array.isArray(_.attachments),y.attachmentCount=Array.isArray(_.attachments)?_.attachments.length:0,y.attachmentKinds=Array.isArray(_.attachments)?_.attachments.map(d=>String(d.type??d.kind??"").toLowerCase()||"(empty)"):[],y.attachmentBase64Lens=Array.isArray(_.attachments)?_.attachments.map(d=>(typeof d.base64Data=="string"?d.base64Data:typeof d.base64=="string"?d.base64:typeof d.data=="string"?d.data:"").length):[]):(y.contentKind="non-json",y.contentPrefix=h.slice(0,60)),a.push(y)}}}catch{}return Ce({stage:"hasImages_false",toolCallId:t.slice(0,12),contentLen:e.length,parsedContentKind:Array.isArray(s.content)?"parts":typeof s.content,partsCount:Array.isArray(s.content)?s.content.length:null,dmProbe:a}),null}if(!this.state.provider.supportsImageInput)return this.emitAcodeEvent("error",{message:"ACode skipped image content from tool result because the provider/model is not configured for image input."}),Ce({stage:"supportsImageInput_false",toolCallId:t.slice(0,12),providerSupportsImageInput:this.state.provider.supportsImageInput,partsCount:Array.isArray(s.content)?s.content.length:null}),this.cleanToolMessageAttachments(t,"[Tool result contained images; skipped because the current model does not support image input]"),null;let i;try{i=await lt(s.content)}catch(a){const r=a instanceof Error?a.message:String(a),l=Array.isArray(s.content)?s.content.filter(u=>u.type==="image_url").map(u=>{const c=u.image_url.url,m=c.indexOf(",");return{urlHead:c.slice(0,Math.min(30,c.length)),base64Len:m>=0?c.length-m-1:null,isDataUrl:/^data:image\//i.test(c)}}):null;Ce({stage:"materialize_threw",toolCallId:t.slice(0,12),errMessage:r,imagePartsCount:l?l.length:0,imageParts:l}),this.emitAcodeEvent("error",{message:`ACode skipped image content from tool result: ${r}`}),i=Array.isArray(s.content)?s.content.map(u=>u.type==="text"?u:{type:"text",text:`[Image omitted: ${r}]`}):s.content}return this.isTurnSuperseded(o)?null:(this.cleanToolMessageAttachments(t,"[Tool result contained images; see user message above]"),{role:"user",content:i})}expandCommandsInPolledResult(e){const t=this.state.commandRegistry;if(!t||t.list().length===0)return e;let o;try{o=JSON.parse(e)}catch{return e}if(!k(o))return e;let s=!1;for(const i of["directMessages","groupMessages"]){const a=o[i];if(Array.isArray(a))for(const r of a){if(!k(r))continue;const l=N(r.content);if(!l)continue;const u=t.expand(l);u&&(r.content=u.prompt,s=!0)}}return s?JSON.stringify(o):e}cleanToolMessageAttachments(e,t){const o=this.state.conversation.find(i=>i.role==="tool"&&i.tool_call_id===e);if(!o||typeof o.content!="string")return;let s;try{const i=JSON.parse(o.content);if(typeof i=="object"&&i!==null){const a={...i};"attachments"in a&&delete a.attachments,Ko(a),s=JSON.stringify(a)}}catch{}if(s===void 0){const i=o.content.replace(/\[(图片|文件)\s*:\s*([^\]\n]*?)\s*\]/gi,"").trim();i!==o.content&&(s=i)}s!==void 0&&(o.content=s.length>0?s:t)}appendToolFailureResult(e,t,o,s){const i=pt(t,o),a=Mt(o),r=a?pt(t,o,a):i;p(`[ACodeRuntime] \u{1F4E4} appendToolFailureResult: emitting mcp_status tool_result (failed: true) for ${t.exposedName}, result length: ${i.length} chars`+(a?`, conversation snapshot: ${a.bytes} bytes`:"")),this.emitAcodeEvent("error",{message:`ACode tool failed: ${t.exposedName}: ${me(o)}`,nonFatal:!0}),this.emitAcodeEvent("mcp_status",{action:"tool_result",server:t.kind==="mcp"?t.serverName:"builtin",tool:t.toolName,toolUseId:e.id,result:i,failed:!0,...s?{batchId:s}:{}}),this.state.conversation.push({role:"tool",tool_call_id:e.id,content:r})}filterSharedMcpConnectionsForSubAgent(e){const t=this.config.subAgentAllowedTools&&this.config.subAgentAllowedTools.length>0?this.config.subAgentAllowedTools:null;return e.map(o=>({...o,tools:o.tools.filter(s=>t?je(s,t):ao.has(s.toolName))}))}async connectMcpServers(e,t,o){const s=[];for(const[i,a]of Object.entries(e))try{const r=new _t({name:"acode",version:"0.1.0"}),l=this.createMcpTransport(i,a);await x(r.connect(l),t,`ACode MCP server ${i} connect`);const u={serverName:i,client:r,transport:l,tools:[]};r.setNotificationHandler(kt,()=>{this.refreshMcpConnectionTools(u,t)});const{tools:c}=await x(r.listTools(),t,`ACode MCP server ${i} listTools`);u.tools=Ke(i,c);try{u.instructions=r.getInstructions()}catch{}u.spec=a,s.push(u)}catch(r){const l=r instanceof Error?r.message:String(r);o?.set(i,l),this.emitAcodeEvent("error",{message:`ACode MCP server failed to connect: ${i}: ${l}`})}return s}registerMcpCollapsedGroup(e,t,o){if(!this.toolGroupManager||t.collapsed!==!0||o.tools.length===0)return;const s=Oe(e),i=this.toolGroupManager.getRegistration(s),a=t.description?.trim()||Co(o.instructions)||`MCP \u5DE5\u5177\u96C6\uFF1A${e}`,r=this.toolGroupManager.claimedToolNamesBySubGroups(),l=o.tools.map(u=>u.toolName).filter(u=>!r.has(u));l.length!==0&&(this.toolGroupManager.register({key:s,label:t.description?.trim()||e,summary:a,collapsible:!0,toolNames:l,serverName:e}),this.emitAcodeEvent("mcp_status",{serverName:e,status:"collapsed_group_registered",toolCount:l.length,updated:!!i}))}registerToolGroupsFromMcp(e){if(!this.toolGroupManager)return!1;const t=yo(e.instructions);if(t.length===0)return!1;let o=0;for(const s of t)this.toolGroupManager.getRegistration(s.key)||(this.toolGroupManager.register({key:s.key,label:s.label,summary:"",collapsible:!0,toolNames:s.toolNames}),o+=1);return this.emitAcodeEvent("mcp_status",{serverName:e.serverName,status:"tool_groups_registered",declared:t.length,registered:o}),!0}requestMcpToolsRefresh(e){const t=this.state.mcpConnections.filter(o=>!e||o.serverName===e);for(const o of t)this.refreshMcpConnectionTools(o,this.state.provider.mcpToolTimeoutMs)}async recoverHiddenMcpTool(e){if(this.recoveredToolNames.has(e)||this.recoveredToolNames.size>=wo)return!1;this.recoveredToolNames.add(e);const t=this.state.mcpConnections.filter(o=>o.tools.some(s=>s.toolName==="expand_tools"||s.exposedName==="expand_tools"||s.exposedName.endsWith("__expand_tools")));if(t.length===0)return!1;for(const o of t)await this.refreshMcpConnectionTools(o,this.state.provider.mcpToolTimeoutMs);return this.state.exposedTools.some(o=>o.toolName===e||o.exposedName===e)}async refreshMcpConnectionTools(e,t){try{const{tools:s}=await x(e.client.listTools(),t,`ACode MCP server ${e.serverName} listTools refresh`);e.tools=Ke(e.serverName,s)}catch(s){this.emitAcodeEvent("warning",{message:`ACode MCP server ${e.serverName} tools/list_changed refresh failed: ${s instanceof Error?s.message:String(s)}`});return}if(!this.exposedToolsInitialized)return;const o=this.state.exposedTools.filter(s=>!(s.kind==="mcp"&&s.serverName===e.serverName));this.state.exposedTools=[...o,...e.tools],this.emitExposedToolsStatus()}createMcpTransport(e,t){if(t.type==="stdio"||t.command){if(!t.command)throw new Error("stdio MCP server requires command");const s=new bt({command:t.command,args:t.args,cwd:t.cwd||this.config.workingDirectory,env:{...this.config.env??{},...t.env??{}},stderr:"pipe"}),i=t.debugStderr===!0?this.ensureMcpStderrBuffer(e):null;return s.stderr?.on("data",a=>{i&&i.push(String(a))}),s}if(!t.url)throw new Error("remote MCP server requires url");const o=t.headers?{headers:t.headers}:void 0;return t.type==="sse"?new At(new URL(t.url),{requestInit:o}):new wt(new URL(t.url),{requestInit:o})}ensureMcpStderrBuffer(e){let t=this.mcpStderrBuffers.get(e);return t||(t=new no,this.mcpStderrBuffers.set(e,t)),t}getMcpServerStderr(e,t=100){const o=this.mcpStderrBuffers.get(e);if(!o)return[];const s=Math.max(1,Math.min(Math.floor(t)||100,500));return o.tail(s)}async fetchProfilePrompt(e){for(const t of e)if(t.tools.find(s=>s.toolName==="get_profile"))try{const s=await x(t.client.callTool({name:"get_profile",arguments:{}},void 0,{timeout:ge}),ge,"ACode startup get_profile"),i=this.extractProfileFromToolResult(s);return gs(i)}catch(s){return this.emitAcodeEvent("warning",{message:`ACode failed to fetch profile via ${t.serverName}/get_profile: ${s instanceof Error?s.message:String(s)}`}),""}return""}extractProfileFromToolResult(e){if(!k(e))return null;const t=e.content;if(Array.isArray(t)){for(const o of t)if(k(o)&&typeof o.text=="string"&&o.text.trim())try{const s=JSON.parse(o.text);if(k(s))return s}catch{}}return k(e)?e:null}async disconnectMcpServers(){if(!this.state.ownsMcpConnections){this.state.mcpConnections=[],this.state.exposedTools=[],this.exposedToolsInitialized=!1;return}const e=this.state.mcpConnections;this.state.mcpConnections=[],this.state.exposedTools=[],this.exposedToolsInitialized=!1,await Promise.all(e.map(t=>se(`ACode MCP server ${t.serverName} close`,t.transport.close(),o=>{this.emitAcodeEvent("error",{message:o})},this.state.provider.mcpToolTimeoutMs)))}}Z.SKILL_REFRESH_DEBOUNCE_MS=500;export{Z as ACodeRuntime,fe as TOOL_GROUP_MANIFEST_PREFIX,vo as hasExplicitRequestBodyByteCap,Co as humanReadableInstructions,yo as parseToolGroupManifest,xo as resolveMaxRequestBodyBytes,pt as toToolErrorResultText};
|
|
114
115
|
|