beeos-claw 0.2.35 → 0.2.37

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.
@@ -1 +1 @@
1
- import{randomUUID,createHash}from"node:crypto";import{mkdir,writeFile,rm,lstat,readlink,stat,readdir,rename,cp}from"node:fs/promises";import path from"node:path";import{join,resolve,relative,sep}from"node:path";import{homedir}from"node:os";import AdmZip from"adm-zip";import{sanitizePromptPayload,stripTransportMetadata,isHeartbeatPromptText,isHeartbeatOnlyText}from"../message-filter.js";import{CANVAS_TERM_MAPPING,CANVAS_UPDATE_HINT,buildActiveSurfacesContext}from"../canvas/tools.js";import{parseMentionsParam,buildMentionInstruction}from"../a2a/mention-resolver.js";import{extractPromptBlocks,toGatewayPrompt,resolveSlashCommandName as resolveSlashCmd}from"./prompt-converter.js";import{loadHistory,groupIntoTurns,extractBeeosFileUrisFromOutput,splitUserReplayBlocks}from"./history-replay.js";import{buildResolutionPlan,resolveResources,applyResolutions}from"./prompt-resource-resolver.js";import{readEnrichmentCache,readUploadToolResult}from"../upload/result-cache.js";import{buildToolResultSessionContentBlocks}from"./tool-result-payload-strategies.js";import{buildToolCallPayload,buildToolCallUpdatePayload}from"./tool-call-payload-builder.js";import{Errors}from"../errors/index.js";import{formatFriendlyError}from"../errors/classify.js";import{resolveChannelFromSessionKey}from"../channel-detection.js";import{ACP_PROTOCOL_VERSION,DEFAULT_PROMPT_TIMEOUT_MS}from"./types.js";const DEFAULT_MODE={id:"default",name:"Default",description:"Default agent mode"},USER_MESSAGE_PREFIX="User Message From BeeOS:",USER_MESSAGE_PREFIX_WITH_NEWLINE=`${USER_MESSAGE_PREFIX}\n`,SLASH_COMMAND_NAME_RE=/^[a-z0-9_-]+$/i;function withUserMessagePrefix(e){return e.startsWith(USER_MESSAGE_PREFIX)?e:`${USER_MESSAGE_PREFIX_WITH_NEWLINE}${e}`}const BACKGROUND_SESSION_PATTERNS=[":cron:",":hook:","node-",":a2a:"];function isBackgroundSessionKey(e){return BACKGROUND_SESSION_PATTERNS.some(t=>e.includes(t))}function isSystemDefaultSessionKey(e){const t=e.split(":");if(t.length<3||"agent"!==t[0])return!1;const s=t.slice(2).join(":");return"main"===s||s===t[1]}function isPlaceholderTitle(e){return!e||"New chat"===e||(!!/^[0-9a-f]{6,12}\s*\(20\d{2}/i.test(e)||(!!/^Sender\s*\(untrusted/i.test(e)||!!/^Conversation\s+info\s*\(untrusted/i.test(e)))}function cleanGatewayDerivedTitle(e){if(!e)return;let t=stripTransportMetadata(e);return/^\[System\b/.test(t)?void 0:(t=t.trim(),t||void 0)}function resolveStandaloneSlashCommandName(e,t){if(0===e.length)return;if(e.some(e=>"text"!==e.type))return;const s=t.trim();if(!s||s.includes("\n")||!s.startsWith("/"))return;const n=s.match(/^\/([^\s:]+)(?::|\s|$)/)?.[1]?.trim();return n&&!n.includes("/")&&SLASH_COMMAND_NAME_RE.test(n)?n.toLowerCase():void 0}function deriveSessionId(e,t){const s=createHash("sha256").update(`${t}:${e}`).digest("hex").substring(0,32);return`session:${s.slice(0,8)}-${s.slice(8,12)}-${s.slice(12,16)}-${s.slice(16,20)}-${s.slice(20,32)}`}function parseAgentIdFromKey(e){const t=e.split(":");return(t.length>=3&&"agent"===t[0]?t[1]:"")||"main"}function createSessionObject(e){const t=parseAgentIdFromKey(e.gatewayKey);return{sessionId:deriveSessionId(e.gatewayKey,e.instanceId),gatewaySessionKey:e.gatewayKey,agentId:t,platformAgentId:e.resolvePlatformAgentId?.(t),createdAt:Date.now(),chatReplayDedupKeys:new Set,cwd:e.cwd,title:e.title,channelType:resolveChannelFromSessionKey(e.gatewayKey,e.thirdPartyChannels)}}export class AcpGatewayBridgeCore{state;transport;config;logger;writeObs;getGateway;thirdPartyChannels;beeosFileResolver;initialized=!1;agentMapping=null;_syncResolvers=[];_mappingReadyCallbacks=[];_loadSessionLock=new Map;setAgentMapping(e){this.agentMapping=e;for(const t of this.state.sessions.values())!t.platformAgentId&&t.agentId&&(t.platformAgentId=e.get(t.agentId)?.id);for(const e of this._syncResolvers)e();this._syncResolvers=[];for(const e of this._mappingReadyCallbacks)e();this._mappingReadyCallbacks=[]}onceAgentMappingReady(e){this.agentMapping&&this.agentMapping.size>0?e():this._mappingReadyCallbacks.push(e)}markAgentSyncDone(){for(const e of this._syncResolvers)e();this._syncResolvers=[]}waitForAgentMapping(e=15e3){return this.agentMapping?Promise.resolve():new Promise(t=>{this._syncResolvers.push(t);const s=setTimeout(()=>{const e=this._syncResolvers.indexOf(t);-1!==e&&this._syncResolvers.splice(e,1),t()},e);"object"==typeof s&&"unref"in s&&s.unref()})}handleTerminalRequest=null;handleTerminalNotification=null;onRunComplete=null;onRunFailed=null;onRunEnded=null;handleSlashCommand=null;onPromptStart=null;instanceId;constructor(e){this.state=e.state,this.transport=e.transport,this.config=e.config,this.logger=e.logger,this.writeObs=e.writeObs,this.getGateway=e.getGateway,this.instanceId=e.instanceId||"unknown",this.thirdPartyChannels=e.thirdPartyChannels??new Set,this.beeosFileResolver=e.beeosFileResolver}resolvePlatformAgentId(e){return this.agentMapping?.get(e)?.id}getDefaultAgentId(){return this.agentMapping&&this.agentMapping.size>0?this.agentMapping.keys().next().value??"main":"main"}registerSession(e){this.state.sessions.set(e.sessionId,e),this.state.sessionsByGatewayKey.set(e.gatewaySessionKey,e),this.ensureVerboseFull(e)}async ensureVerboseFull(e){if(e.verbosePatched)return;const t=this.getGateway();if(t?.isConnected){e.verbosePatched=!0;try{await t.sessionsPatch(e.gatewaySessionKey,{verboseLevel:"full"})}catch(t){e.verbosePatched=!1,this.logger.debug?.(`[bridge-core] ensureVerboseFull failed: ${t instanceof Error?t.message:t}`)}}}ensureSessionForKey(e){const t=this.state.findSessionByKey(e);if(t)return t;const s=createSessionObject({gatewayKey:e,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels});return this.registerSession(s),this.logger.debug?.(`[bridge-core] ensureSessionForKey: lazily created session sid=${s.sessionId} key=${e}`),s}async preloadSessionsFromGateway(){const e=this.getGateway();if(e?.isConnected)try{const t=await e.sessionsList();if(!t||"object"!=typeof t)return;const s=t.sessions;if(!Array.isArray(s))return;let n=0;for(const e of s){const t=e,s=t.sessionKey??t.key??t.id??t.name;if(!s||"string"!=typeof s)continue;if(this.state.findSessionByKey(s))continue;const i=t.label||t.derivedTitle||t.title||void 0,o=i&&!isPlaceholderTitle(i)?i:void 0,r=createSessionObject({gatewayKey:s,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,title:o});this.registerSession(r),n++}n>0&&this.logger.info?.(`[bridge-core] preloadSessions: loaded ${n} session(s) from gateway`)}catch(e){this.logger.debug?.("[bridge-core] preloadSessions failed:",e)}else this.logger.debug?.("[bridge-core] preloadSessions skipped: gateway not connected")}async handleRequest(e){this.writeObs({component:"bridge-core",domain:"forward",name:`rpc.request.${e.method}`,severity:"debug",payload:{id:e.id,method:e.method}});try{switch(e.method){case"initialize":this.handleInitialize(e);break;case"session/new":await this.handleSessionNew(e);break;case"session/load":await this.handleSessionLoad(e);break;case"session/list":await this.handleSessionList(e);break;case"session/rename":this.handleSessionRename(e);break;case"session/delete":await this.handleSessionDelete(e);break;case"session/clear":await this.handleSessionClear(e);break;case"session/prompt":await this.handleSessionPrompt(e);break;case"session/set_mode":await this.handleSessionSetMode(e);break;case"session/set_model":await this.handleSessionSetModel(e);break;case"skills/list":await this.handleSkillsList(e);break;case"skills/install":await this.handleSkillsInstall(e);break;case"skills/uninstall":await this.handleSkillsUninstall(e);break;case"skills/update":await this.handleSkillsUpdate(e);break;case"models/list":await this.handleModelsList(e);break;case"session/cancel":this.handleSessionCancel(e.id,e.params);break;case"cron/list":case"cron/status":case"cron/add":case"cron/update":case"cron/remove":case"cron/run":case"cron/runs":await this.handleCronRequest(e);break;default:e.method.startsWith("terminal/")&&this.handleTerminalRequest?await this.handleTerminalRequest(e):this.transport.sendAcpError(e.id,Errors.MethodNotFound,{message:`Method not found: ${e.method}`})}}catch(t){this.transport.sendAcpError(e.id,Errors.InternalError,{message:t instanceof Error?t.message:"Internal error"})}}handleCanvasNotification=null;getCanvasState=null;onSessionLoaded=null;onSkillInstalled=null;onSessionDeleted=null;onSessionCleared=null;handleNotification(e){switch(e.method){case"session/cancel":this.handleSessionCancel(void 0,e.params);break;case"canvas/toggle":case"canvas/clear":this.handleCanvasNotification?this.handleCanvasNotification(e.method,e.params):this.logger.debug?.(`[bridge-core] Canvas notification ignored (canvas disabled): ${e.method}`);break;case"canvas/reference":{const t=e.params,s=t?.surfaceId,n=this.state.resolveSessionFromParams(t);if(!n){this.logger.warn?.(`[bridge-core] canvas/reference ignored — no session found for sessionId=${t?.sessionId}`);break}n.referencedSurfaceId=s||void 0,this.logger.debug?.(`[bridge-core] Canvas reference ${s?"set":"cleared"}: ${s??"(none)"}`);break}default:e.method.startsWith("terminal/")&&this.handleTerminalNotification?this.handleTerminalNotification(e.method,e.params):this.logger.debug?.(`[bridge-core] Unknown notification: ${e.method}`)}}handleInitialize(e){this.initialized=!0;const t=this.transport.forwardControl;!1===e.params?.forwardThinking&&(t.forwardThinking=!1),!1===e.params?.forwardToolCalls&&(t.forwardToolCalls=!1),this.transport.sendResult(e.id,{protocolVersion:ACP_PROTOCOL_VERSION,agentCapabilities:{loadSession:!0,skills:!0,promptCapabilities:{image:!0,audio:!1,embeddedContext:!0},sessionCapabilities:{list:{},"web-ssh":!!this.config.bridge.shell.enabled,canvas:!!this.config.canvasEnabled,cron:!0},mcp:{}},agentInfo:{name:"beeos-claw",title:"BeeOS Agent Bridge",version:"0.1.0"},_meta:{},authMethods:[]})}async handleSessionNew(e){const t=`agent:${"string"==typeof e.params?.agentId&&e.params.agentId||this.config.gateway.agentId||this.getDefaultAgentId()}:${randomUUID()}`,s="string"==typeof e.params?.cwd?e.params.cwd:".",n=createSessionObject({gatewayKey:t,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,cwd:s});this.registerSession(n),this.transport.sendResult(e.id,{sessionId:n.sessionId,modes:{availableModes:[{...DEFAULT_MODE}],currentModeId:DEFAULT_MODE.id},_meta:{sessionKey:t,agentId:n.agentId,platformAgentId:n.platformAgentId??null}}),this.fetchHistoryInBackground(t)}fetchHistoryInBackground(e){const t=this.getGateway();t?.isConnected&&loadHistory(t,e,50).catch(e=>{this.logger.debug?.(`[bridge-core] Background history fetch failed: ${e instanceof Error?e.message:e}`)})}async recoverSessionFromGateway(e){const t=this.getGateway();if(t?.isConnected)try{const s=await t.sessionsList();if(!s||"object"!=typeof s)return;const n=s.sessions;if(!Array.isArray(n))return;for(const t of n){const s=t,n=s.sessionKey??s.key??s.id??s.name;if(n&&"string"==typeof n&&deriveSessionId(n,this.instanceId)===e){const e=s.label||s.derivedTitle||s.title||void 0,t=e&&!isPlaceholderTitle(e)?e:void 0,i=createSessionObject({gatewayKey:n,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,title:t});return this.registerSession(i),i}}}catch(e){this.logger.debug?.("[bridge-core] recoverSessionFromGateway failed:",e)}}async resolveOrCreateSession(e){let t=this.state.sessions.get(e);if(!t&&e.startsWith("agent:")&&(t=this.state.findSessionByKey(e)),t||e.startsWith("agent:")||(t=await this.recoverSessionFromGateway(e)),t)return{session:t,created:!1};const s=e.startsWith("agent:"),n=this.config.gateway.agentId||this.getDefaultAgentId();return t=createSessionObject({gatewayKey:s?e:`agent:${n}:${e}`,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels}),this.registerSession(t),{session:t,created:!0}}async handleSessionLoad(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId"});const s=this._loadSessionLock.get(t);if(s)try{await s}catch{}const n=this._doSessionLoad(e,t);this._loadSessionLock.set(t,n);try{await n}finally{this._loadSessionLock.get(t)===n&&this._loadSessionLock.delete(t)}}async _doSessionLoad(e,t){const{session:s}=await this.resolveOrCreateSession(t),n=s.sessionId;this.state.lastActiveSessionId=n,s.chatReplayDedupKeys.clear(),this.transport.resetSeq(n);let i=[];try{const e=this.getGateway(),t=await loadHistory(e,s.gatewaySessionKey,50);i=groupIntoTurns(t);const o=i.filter(e=>"user"===e.role).length,r=i.filter(e=>"assistant"===e.role).length,a=i.filter(e=>e.toolCalls?.length).length,l=i.reduce((e,t)=>e+(t.toolCalls?.length??0),0);if(this.logger.debug?.(`[bridge-core] handleSessionLoad sid=${n} messages=${t.length} turns=${i.length} userTurns=${o} asstTurns=${r} turnsWithToolCalls=${a} totalToolCalls=${l}`),i.length>0&&isPlaceholderTitle(s.title)&&"manual"!==s.titleSource){const e=i.find(e=>{if("user"!==e.role)return!1;if("internal_system"===e.provenance?.kind)return!1;const t=stripTransportMetadata(e.content).trim();return t.length>0&&!/^Sender\s*\(untrusted/i.test(t)});if(e?.content){const t=stripTransportMetadata(e.content).trim(),i=t.slice(0,64).replace(/\n/g," ").trim();if(i){s.title=i,s.titleSource="truncated";const e=this.getGateway();e?.isConnected&&e.sessionsPatch(s.gatewaySessionKey,{label:i}).catch(()=>{}),this.transport.sendSessionUpdate(n,{sessionUpdate:"title_update",title:i}),this.generateTitleAsync(s,n,t)}}}for(let e=0;e<i.length;e++){const t=i[e];"user"!==t.role||"internal_system"!==t.provenance?.kind?"user"===t.role&&/^\s*system\s*\(untrusted[^)]*\)/i.test(t.content)?this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] system-injected user message`):"user"===t.role&&isHeartbeatPromptText(t.content)?this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] heartbeat cron prompt`):"assistant"===t.role&&isHeartbeatOnlyText(t.content)?this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] heartbeat assistant reply`):"system"!==t.role?(this.logger.debug?.(`[bridge-core] SENDING history_turn[${e}] role=${t.role} contentLen=${t.content.length} content=${JSON.stringify(t.content.slice(0,80))} tcCount=${t.toolCalls?.length??0}`),"user"===t.role?this.replayUserTurnAsChunks(n,t.content,t.metadata):await this.replayAssistantTurnAsStream(n,t)):(this.logger.debug?.(`[bridge-core] SENDING system_message[${e}] contentLen=${t.content.length}`),this.transport.sendSessionUpdate(n,{sessionUpdate:"system_message_chunk",content:{type:"text",text:t.content},metadata:t.metadata})):this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] internal_system user message`)}}catch(e){this.logger.warn?.("[bridge-core] Failed to load session history:",e)}const o=new Set;for(const e of i)if(e.toolCalls)for(const t of e.toolCalls)t.id&&o.add(t.id),t.callId&&t.callId!==t.id&&o.add(t.callId);let r="",a="";for(let e=i.length-1;e>=0;e--)if("assistant"===i[e].role){r=i[e].content.slice(-200),a=(i[e].reasoning??"").slice(-200);break}if(void 0!==s.pendingPromptId&&!s.promptDone&&s.assistantMq&&!s.assistantMq.closed)try{this.replayCurrentAssistantStream(s,o,r,a)}catch(e){this.logger.warn?.(`[bridge-core] replayCurrentAssistantStream failed: ${e instanceof Error?e.message:e}`)}(()=>{this.transport.sendResult(e.id,{sessionId:n}),this.onSessionLoaded?.(n,s.gatewaySessionKey,s.platformAgentId),this.pushSkillsList().catch(e=>{this.logger.warn?.(`[bridge-core] pushSkillsList failed: ${e instanceof Error?e.message:e}`)})})()}replayCurrentAssistantStream(e,t,s="",n=""){if(!e.assistantMq||e.assistantMq.closed)return;const i=e.assistantMq.peek(0);if(0===i.length)return;let o=0,r=0,a="",l="";for(const d of i)if(t?.size&&d.toolCallId&&t.has(d.toolCallId)&&("tool_call"===d.sessionUpdate||"tool_call_update"===d.sessionUpdate))r++;else{if(s.length>0&&"agent_message_chunk"===d.sessionUpdate){const e=d.content?.text;if("string"==typeof e&&(a+=e,a.length<=s.length)){r++;continue}}if(n.length>0&&"agent_thought_chunk"===d.sessionUpdate){const e=d.content?.text;if("string"==typeof e&&(l+=e,l.length<=n.length)){r++;continue}}this.transport.sendSessionUpdate(e.sessionId,d),o++}r>0&&this.logger.debug?.(`[bridge-core] replayCurrentAssistantStream skipped ${r} duplicate tool events (already in history)`),o>0&&this.writeObs({component:"bridge-core",domain:"lifecycle",name:"session.load.replay_complete",severity:"debug",payload:{sessionId:e.sessionId,replayed:o}})}async handleSessionList(e){const t=[];for(const[,e]of this.state.sessions)isBackgroundSessionKey(e.gatewaySessionKey)||t.push({sessionId:e.sessionId,title:e.title||"New chat",cwd:e.cwd,updatedAt:e.lastStreamActivityTs??e.createdAt,agentId:e.agentId,platformAgentId:e.platformAgentId,deletable:!isSystemDefaultSessionKey(e.gatewaySessionKey)});const s=this.getGateway();if(s?.isConnected)try{const e=await s.sessionsList();if(e&&"object"==typeof e&&Array.isArray(e.sessions)){const s=e.sessions;for(const e of s){const s=e,n=s.sessionKey??s.key??s.id??s.name;if(!n||"string"!=typeof n)continue;if(isBackgroundSessionKey(n))continue;const i=deriveSessionId(n,this.instanceId),o=t.find(e=>e.sessionId===n||e.sessionId===i||this.state.sessions.get(e.sessionId)?.gatewaySessionKey===n),r=s.label||cleanGatewayDerivedTitle(s.derivedTitle)||s.title||void 0,a=r&&!isPlaceholderTitle(r)?r:void 0,l=s.model,d=s.modelProvider;if(o){if(a&&isPlaceholderTitle(o.title)){o.title=a;const e=this.state.sessions.get(o.sessionId);e&&isPlaceholderTitle(e.title)&&(e.title=a)}const e=s.updatedAt;e&&(o.updatedAt=e),l&&(o.model=l),d&&(o.modelProvider=d)}else{const e=createSessionObject({gatewayKey:n,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,cwd:s.cwd,title:a});this.registerSession(e),t.push({sessionId:e.sessionId,title:a||"",cwd:s.cwd,updatedAt:s.updatedAt,agentId:e.agentId,platformAgentId:e.platformAgentId,deletable:!isSystemDefaultSessionKey(n),model:l,modelProvider:d})}}}}catch(e){this.logger.debug?.("[bridge-core] sessions.list enrichment failed:",e)}t.sort((e,t)=>(t.updatedAt??0)-(e.updatedAt??0)||e.sessionId.localeCompare(t.sessionId)),this.transport.sendResult(e.id,{sessions:t})}handleSessionRename(e){const t=e.params?.sessionId,s=e.params?.title;if(!t||!s)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId, title"});const n=s.slice(0,64),i=this.state.sessions.get(t);if(!i)return void this.transport.sendAcpError(e.id,Errors.SessionNotFound);i.title=n,i.titleSource="manual";const o=i.gatewaySessionKey,r=this.getGateway();r?.isConnected&&r.sessionsPatch(o,{label:n}).catch(e=>{this.logger.debug?.(`[bridge-core] sessionsPatch (rename) failed: ${e instanceof Error?e.message:e}`)}),this.transport.sendResult(e.id,{sessionId:t,title:n})}static TITLE_TEMPLATES=[{match:["今日日报","日报"],zh:"今日工作日报",en:"Daily Work Log"},{match:["周报","周报总结","本周周报"],zh:"本周工作周报",en:"Weekly Work Report"},{match:["月报","月度汇报","月度总结"],zh:"本月工作月报",en:"Monthly Work Report"},{match:["工作日志","日志"],zh:"工作日志整理",en:"Work Log Review"},{match:["晨会","今日晨会"],zh:"今日晨会纪要",en:"Daily Standup Notes"},{match:["复盘","今日复盘"],zh:"今日工作复盘",en:"Daily Retrospective"},{match:["新会话","new chat","new conversation"],zh:"新会话",en:"New Conversation"}];static TITLE_SYSTEM_PROMPT='You are a conversation title generator. Your only task is to generate a concise, human-readable title summarizing the user\'s first message.\n\n### Rules:\n- Length: 3-6 words in English, or 6-20 characters in Chinese (up to 24 if needed to keep all key domain terms intact).\n- Language: Match the user\'s input language exactly. Mixed zh/en input → use the dominant language.\n- Extract the core topic (noun-centric), not the action verb.\n- No punctuation, no quotation marks, no emojis, no markdown.\n- No prefix like "Title:" or "Here is". Output the title only.\n- Avoid generic words: "help", "question", "chat", "problem", "task".\n- If the message is too vague (e.g. "hi", "continue", "ok", "?"), return "新会话" (for zh) or "New Conversation" (for en).\n\n### Output Format:\nRespond ONLY with valid JSON, no other text:\n{"title": "<your title here>"}\n\n### Examples:\nInput: "帮我写一个关于BeeOS设备接入的PRD"\nOutput: {"title": "BeeOS 设备接入 PRD"}\n\nInput: "What\'s the best way to integrate x402 payment protocol?"\nOutput: {"title": "x402 Payment Integration"}\n\nInput: "hi"\nOutput: {"title": "新会话"}\n\nInput: "分析一下OKX CeDeFi PM这个岗位的机会"\nOutput: {"title": "OKX CeDeFi 岗位分析"}';matchTitleTemplate(e){const t=e.trim().toLowerCase();for(const s of AcpGatewayBridgeCore.TITLE_TEMPLATES)if(s.match.some(e=>e.toLowerCase()===t))return/[\u4e00-\u9fff]/.test(e)?s.zh:s.en}isGuardMessage(e){const t=e.trim();return[...t].length<3||[...t].every(e=>/[\p{P}\p{S}\s]/u.test(e))}generateTitleAsync(e,t,s){if(this.isGuardMessage(s))return void this.logger.info?.(`[bridge-core] title-gen-async SKIP guard message sid=${t}`);const n=this.matchTitleTemplate(s);if(n){e.title=n,e.titleSource="template";const s=this.getGateway();return s?.isConnected&&s.sessionsPatch(e.gatewaySessionKey,{label:n}).catch(()=>{}),void this.transport.sendSessionUpdate(t,{sessionUpdate:"title_update",title:n})}const i=s.length>2e3?s.slice(0,1500):s,o=(process.env.AROUTER_BASE_URL||"").replace(/\/$/,""),r=process.env.AROUTER_API_KEY||"";o&&r?(this.logger.info?.(`[bridge-core] title-gen-async CALLING LLM sid=${t} msg="${s.slice(0,60)}"`),fetch(`${o}/v1/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify({model:"openai/gpt-4o-mini",max_tokens:32,temperature:.3,messages:[{role:"system",content:AcpGatewayBridgeCore.TITLE_SYSTEM_PROMPT},{role:"user",content:i}]})}).then(async s=>{if(!s.ok){const e=await s.text().catch(()=>"");return void this.logger.info?.(`[bridge-core] title-gen-async FAILED sid=${t} status=${s.status} body=${e.slice(0,300)}`)}const n=await s.json(),i=n.choices?.[0]?.message?.content?.trim();if(!i)return;const o=i.replace(/^```json\s*/,"").replace(/```$/,"").trim();let r;try{r=JSON.parse(o).title?.trim()??""}catch{return void this.logger.debug?.(`[bridge-core] title gen JSON parse failed: ${o}`)}if(!r)return;if("manual"===e.titleSource)return void this.logger.debug?.("[bridge-core] title gen discarded: user already renamed");this.logger.info?.(`[bridge-core] title-gen-async OK sid=${t} title="${r}"`),e.title=r,e.titleSource="auto";const a=this.getGateway();a?.isConnected&&a.sessionsPatch(e.gatewaySessionKey,{label:r}).catch(()=>{}),this.transport.sendSessionUpdate(t,{sessionUpdate:"title_update",title:r})}).catch(e=>{this.logger.info?.(`[bridge-core] title-gen-async ERROR sid=${t}: ${e instanceof Error?e.message:e}`)})):this.logger.info?.(`[bridge-core] title-gen-async SKIP sid=${t}: no AROUTER env vars`)}async handleSessionDelete(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId"});const s=this.state.sessions.get(t);if(!s)return this.logger.info?.(`[bridge-core] session/delete sessionId=${t} NOT FOUND in local state`),void this.transport.sendAcpError(e.id,Errors.SessionNotFound);const n=s.gatewaySessionKey;this.logger.info?.(`[bridge-core] session/delete sessionId=${t} gatewayKey=${n} gwConnected=${!!this.getGateway()?.isConnected}`);let i=!1;const o=this.getGateway();if(o?.isConnected&&n)try{await o.sessionsDelete(n),i=!0,this.logger.info?.(`[bridge-core] Gateway sessions.delete SUCCESS for key=${n}`)}catch(t){const s=t instanceof Error?t.message:String(t);if(this.logger.info?.(`[bridge-core] Gateway sessions.delete FAILED for key=${n}: ${s}`),!s.includes("Cannot delete the main session"))return void this.transport.sendResult(e.id,{deleted:!1});try{await o.sessionsReset(n),i=!0,this.logger.info?.(`[bridge-core] Gateway sessions.reset SUCCESS for key=${n}`)}catch(t){return this.logger.warn?.(`[bridge-core] Gateway sessions.reset also failed for key=${n}: ${t instanceof Error?t.message:t}`),void this.transport.sendResult(e.id,{deleted:!1})}}else n&&this.logger.warn?.(`[bridge-core] Gateway not connected, proceeding with local-only delete for key=${n}`);void 0!==s.pendingPromptId&&this.failPrompt(s,Errors.SessionDeleted.rpcCode,Errors.SessionDeleted.message),this.state.clearPromptTimeout(s),this.state.clearStreamIdleTimer(s),s.assistantMq?.close(),s.currentRunId&&this.state.promptsByRunId.delete(s.currentRunId);const r=new Set;for(const[e,s]of this.state.toolCallToSessionMap)s===t&&(this.state.toolCallToSessionMap.delete(e),r.add(e));for(const[e,t]of this.state.toolCallIdMap)r.has(t)&&this.state.toolCallIdMap.delete(e);this.state.sessions.delete(t),this.state.sessionsByGatewayKey.delete(n),this.transport.resetSessionUpdateCount(t),i&&this.onSessionDeleted?.(n),this.logger.info?.(`[bridge-core] session/delete DONE sessionId=${t} gatewayKey=${n} deleted=true`),this.transport.sendResult(e.id,{deleted:!0})}async handleSessionClear(e){this.logger.info?.(`[bridge-core] handleSessionClear called, params=${JSON.stringify(e.params)}`);const t=e.params?.sessionId;if(!t)return this.logger.warn?.("[bridge-core] session/clear missing sessionId"),void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId"});const s=this.state.sessions.get(t);if(!s)return this.logger.warn?.(`[bridge-core] session/clear sessionId=${t} not found in state`),void this.transport.sendAcpError(e.id,Errors.SessionNotFound);this.logger.info?.(`[bridge-core] session/clear sessionId=${t} gatewayKey=${s.gatewaySessionKey}`);const n=this.getGateway(),i=n?.isConnected;if(s.promptDone||void 0===s.pendingPromptId&&void 0===s.currentRunId||(this.logger.info?.(`[bridge-core] session/clear: cancelling active run sid=${t} runId=${s.currentRunId}`),i&&(s.currentRunId||s.gatewayRequestId)&&n.agentCancel(s.gatewaySessionKey,{runId:s.currentRunId,requestId:s.gatewayRequestId}),this.endRun(s,{kind:"complete",stopReason:"cancelled"})),i&&s.gatewaySessionKey)try{await n.sessionsReset(s.gatewaySessionKey),this.logger.info?.(`[bridge-core] Gateway sessions.reset SUCCESS for key=${s.gatewaySessionKey}`)}catch(e){this.logger.warn?.(`[bridge-core] Gateway sessions.reset FAILED for key=${s.gatewaySessionKey}: ${e instanceof Error?e.message:e}`)}else this.logger.warn?.(`[bridge-core] session/clear skipped gateway reset: connected=${i}, key=${s.gatewaySessionKey}`);s.chatReplayDedupKeys.clear(),this.logger.info?.("[bridge-core] session/clear dedupKeys cleared, calling onSessionCleared"),this.onSessionCleared?.(t,s.gatewaySessionKey),this.transport.sendResult(e.id,{cleared:!0}),this.logger.info?.("[bridge-core] session/clear DONE, sent cleared=true")}async handleSessionSetMode(e){const t=e.params?.sessionId,s=e.params?.mode,n=t?this.state.sessions.get(t):void 0,i=this.getGateway();if(n&&i?.isConnected&&s){const e={};if("verbose"===s||"detailed"===s?e.verboseLevel="full":"concise"!==s&&"default"!==s||(e.verboseLevel="default"),"reasoning"!==s&&"thinking"!==s||(e.reasoningLevel="stream"),Object.keys(e).length>0)try{await i.sessionsPatch(n.gatewaySessionKey,e)}catch(e){this.logger.warn?.(`[bridge-core] sessions.patch failed: ${e instanceof Error?e.message:e}`)}}this.transport.sendResult(e.id,{mode:s??"default"})}async handleSessionSetModel(e){const t=e.params?.sessionId,s=e.params?.model,n=t?this.state.sessions.get(t):void 0,i=this.getGateway();let o=s??null,r=null;if(n&&i?.isConnected&&s)try{const e=await i.sessionsPatch(n.gatewaySessionKey,{model:s}),t=e?.resolved;t?.model&&(o=t.model),t?.modelProvider&&(r=t.modelProvider)}catch(e){this.logger.warn?.(`[bridge-core] sessions.patch for model failed: ${e instanceof Error?e.message:e}`)}this.transport.sendResult(e.id,{model:o,modelProvider:r})}async handleModelsList(e){const t=this.getGateway();if(t?.isConnected)try{const s=await t.request("models.list",{});this.transport.sendResult(e.id,s??{models:[]})}catch(t){this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Failed to list models: ${t instanceof Error?t.message:t}`})}else this.transport.sendAcpError(e.id,Errors.GatewayNotConnected)}async handleSkillsList(e){const t=await this.fetchSimplifiedSkills();this.transport.sendResult(e.id,{skills:t})}async fetchSimplifiedSkills(){const e=this.getGateway();if(!e?.isConnected)return[];try{const t=await e.skillsStatus();return t?.skills?t.skills.filter(e=>!1!==e.eligible||!0===e.disabled).sort((e,t)=>e.always&&!t.always?-1:!e.always&&t.always?1:e.name.localeCompare(t.name)).map(e=>({name:e.name,description:e.description,emoji:e.emoji,always:e.always||void 0,bundled:e.bundled||void 0,disabled:e.disabled||void 0,skillKey:e.skillKey||void 0})):[]}catch(e){return this.logger.warn?.(`[bridge-core] Failed to fetch skills: ${e instanceof Error?e.message:e}`),[]}}async pushSkillsList(){const e=await this.fetchSimplifiedSkills();this.transport.sendNotification("session/update",{sessionId:"_system",update:{sessionUpdate:"available_commands_update",commands:e}}),this.writeObs({component:"bridge-core",domain:"lifecycle",name:"skills.pushed",severity:"debug",payload:{count:e.length}})}getSkillsDir(){const e=process.env.OPENCLAW_HOME||join(homedir(),".openclaw");return join(e,"skills")}async handleSkillsInstall(e){const t=e.params,s=t?.zipUrl,n=t?.files;let i;try{i=validateSkillSlug(t?.slug||t?.name||"")}catch{return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Invalid or missing skill slug"})}if(!n?.length&&!s)return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Missing skill files or zipUrl"});const o=resolveSkillInstallDir(this.getSkillsDir(),i);try{if(await mkdir(o,{recursive:!0}),s){const e=await fetchWithRetry(s,{maxRetries:4,logger:this.logger});await extractZipToDir(e,o)}else if(n?.length){const e=resolve(o);for(const t of n){const s=join(o,t.path);if(!resolve(s).startsWith(e+sep))throw new Error(`File path escapes skill directory: ${t.path}`);await mkdir(join(s,".."),{recursive:!0});const n=await fetch(t.downloadUrl,{signal:AbortSignal.timeout(12e4)});if(!n.ok)throw new Error(`Failed to download ${t.path}: ${n.status}`);const i=Buffer.from(await n.arrayBuffer());await writeFile(s,i)}}this.logger.info?.(`[bridge-core] Installed skill "${i}" via ${s?"zip":"files"}`),this.transport.sendResult(e.id,{success:!0}),this.onSkillInstalled?.(i,o),await this.pushSkillsList()}catch(t){this.logger.error?.(`[bridge-core] Failed to install skill "${i}": ${t instanceof Error?t.message:t}`),this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Install failed: ${t instanceof Error?t.message:String(t)}`})}}async handleSkillsUninstall(e){const t=e.params;let s;try{s=validateSkillSlug(t?.slug||t?.name||"")}catch{return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Invalid or missing skill name"})}const n=resolveSkillInstallDir(this.getSkillsDir(),s),i=resolveSkillInstallDir(join(homedir(),".agents","skills"),s);try{let t=!1;try{const e=await lstat(n);if(e.isSymbolicLink()){const e=await readlink(n);await rm(n,{force:!0}),await rm(e,{recursive:!0,force:!0}),t=!0}else e.isDirectory()&&(await rm(n,{recursive:!0,force:!0}),t=!0)}catch{}try{const e=await lstat(i);(e.isDirectory()||e.isSymbolicLink())&&(await rm(i,{recursive:!0,force:!0}),t=!0)}catch{}t||this.logger.warn?.(`[bridge-core] Skill "${s}" not found in any known location`),this.logger.info?.(`[bridge-core] Uninstalled skill "${s}"`),this.transport.sendResult(e.id,{success:!0}),await this.pushSkillsList()}catch(t){this.logger.error?.(`[bridge-core] Failed to uninstall skill "${s}": ${t instanceof Error?t.message:t}`),this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Uninstall failed: ${t instanceof Error?t.message:String(t)}`})}}async handleSkillsUpdate(e){const t=e.params,s=t?.skillKey,n=t?.enabled;if(!s||"boolean"!=typeof n)return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Missing skillKey or enabled"});const i=this.getGateway();if(i?.isConnected)try{const t=await i.skillsUpdate(s,n);this.logger.info?.(`[bridge-core] Updated skill "${s}" enabled=${n}`),this.transport.sendResult(e.id,{ok:t?.ok??!0,skillKey:s,enabled:n}),await this.pushSkillsList()}catch(t){this.logger.error?.(`[bridge-core] Failed to update skill "${s}": ${t instanceof Error?t.message:t}`),this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Update failed: ${t instanceof Error?t.message:String(t)}`})}else this.transport.sendAcpError(e.id,Errors.InternalError,{message:"Gateway not connected"})}async handleSessionPrompt(e){const t=e.params?.sessionId,s=e.params?.prompt;if(!t||!s)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId, prompt"});const{session:n,created:i}=await this.resolveOrCreateSession(t);i&&this.onSessionLoaded?.(n.sessionId,n.gatewaySessionKey,n.platformAgentId);const o=n.sessionId;this.state.lastActiveSessionId=o,this.onPromptStart?.();const r=this.getGateway();if(!r?.isConnected)return void this.transport.sendAcpError(e.id,Errors.GatewayNotConnected);await this.ensureVerboseFull(n);let a=extractPromptBlocks(sanitizePromptPayload(s));if(!n.title||isPlaceholderTitle(n.title)){const e=a.find(e=>"text"===e.type&&e.text);if(e&&"text"in e&&e.text){const t=stripTransportMetadata(e.text).trim();if(this.logger.info?.(`[bridge-core] title-gen sid=${o} existing="${n.title}" cleanText="${t.slice(0,80)}" isPlaceholder=${isPlaceholderTitle(t)}`),t&&!isPlaceholderTitle(t)){const e=t.slice(0,64).replace(/\n/g," ").trim();n.title=e,n.titleSource="truncated",r?.isConnected&&e&&r.sessionsPatch(n.gatewaySessionKey,{label:e}).catch(e=>{this.logger.debug?.(`[bridge-core] sessionsPatch (title) failed: ${e instanceof Error?e.message:e}`)}),this.transport.sendSessionUpdate(o,{sessionUpdate:"title_update",title:e}),this.generateTitleAsync(n,o,t)}}}const l=buildResolutionPlan(a);if(l.refs.length>0)try{const e=await resolveResources(l,this.writeObs);a=applyResolutions(a,e)}catch(e){this.logger.warn?.(`[bridge-core] Resource pre-resolution failed: ${e instanceof Error?e.message:e}`)}let d;if(this.beeosFileResolver){const e=this.beeosFileResolver.buildResolutionPlan(a);if(e.refs.length>0)try{d=await this.beeosFileResolver.resolveMetadataForPrompt(e)}catch(e){this.logger.warn?.(`[bridge-core] BeeOS file resolution failed: ${e instanceof Error?e.message:e}`)}}const c=toGatewayPrompt(a,{beeosFileResolutionsByUri:d});if("error"in c)return void this.transport.sendAcpError(e.id,Errors.PromptConvertFailed,{message:c.error});const{message:h,attachments:g}=c,p=resolveStandaloneSlashCommandName(a,h)??resolveSlashCmd(a,h),m="string"==typeof p;if(p&&this.handleSlashCommand){const t=this;if(await(async()=>{try{return await t.handleSlashCommand(p)}catch(e){return t.logger.warn?.(`[bridge-core] /${p} handler failed: ${e instanceof Error?e.message:e}`),!1}})())return this.emitPromptAsUserSessionUpdates(n.sessionId,a),this.transport.sendSessionUpdate(o,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`Loading ${p}...`}}),void this.transport.sendResult(e.id,{stopReason:"end_turn"})}const u="chat.send";let f=m?h:withUserMessagePrefix(h);const y=this.config.canvasEnabled&&!m&&!1!==n.canvasActive&&!n.channelType;if(y){const e=n.promptsSinceCanvasInjection??0;!n.canvasPromptInjected||e>=10?(f=`${CANVAS_TERM_MAPPING}\n\n${f}`,n.canvasPromptInjected=!0,n.promptsSinceCanvasInjection=0,this.logger.debug?.("[bridge-core] Canvas term mapping injected (re-inject cycle)")):n.promptsSinceCanvasInjection=e+1}if(y){const e=this.getCanvasState?.();if(e){const t=e.resolveCanvasForSession(n.gatewaySessionKey);f=`${buildActiveSurfacesContext(e,t)??"[Canvas] Canvas panel is available. Use render_ui for structured/visual content."}\n\n${f}`}const t=n.referencedSurfaceId;t&&(f=`${CANVAS_UPDATE_HINT}\n[IMPORTANT: The user selected canvas surface "${t}" for editing. You MUST call render_ui with id="${t}" to UPDATE this existing surface. Do NOT create a new surface with a different id.]\n${f}`,n.referencedSurfaceId=void 0)}if(!m){const t=parseMentionsParam(e.params?.mentions);if(t.length>0){n.activeMentions=t;f=`${buildMentionInstruction(t)}\n${f}`,this.logger.debug?.(`[bridge-core] A2A mention instruction injected for ${t.length} agent(s)`)}else n.activeMentions=void 0}m&&this.logger.info?.(`[bridge-core] slash command passthrough sessionId=${o} command=/${p}`),n.promptDone||void 0===n.pendingPromptId&&void 0===n.currentRunId||(this.logger.warn?.(`[bridge-core] handleSessionPrompt: force-ending stale run sid=${o} oldRunId=${n.currentRunId} oldPromptId=${n.pendingPromptId}`),(n.currentRunId||n.gatewayRequestId)&&r.agentCancel(n.gatewaySessionKey,{runId:n.currentRunId,requestId:n.gatewayRequestId}),this.endRun(n,{kind:"complete",stopReason:"cancelled"})),this.state.resetForNewRun(n,o,this.transport),n.pendingPromptId=e.id,n.gatewayMethod=u,this.emitPromptAsUserSessionUpdates(o,a),this.transport.sendSessionUpdate(o,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:""}}),this.schedulePromptTimeout(n);const S=g.length>0?g.map(e=>({type:e.type,content:e.content,...e.mimeType?{mimeType:e.mimeType}:{},...e.fileName?{fileName:e.fileName}:{}})):void 0,w=`req_${randomUUID().replace(/-/g,"")}`,I={sessionKey:n.gatewaySessionKey,message:f,deliver:!1,idempotencyKey:randomUUID()};S?.length&&(I.attachments=S);if(!r.sendFrame({type:"req",id:w,method:u,params:I}))return this.state.clearPromptTimeout(n),n.pendingPromptId=void 0,n.assistantMq?.close(),void this.transport.sendAcpError(e.id,Errors.SendFailed,{message:"failed to send prompt to gateway"});n.gatewayRequestId=w,this.state.promptsByGatewayRequestId.set(w,n)}replayUserTurnAsChunks(e,t,s){const n=splitUserReplayBlocks(t);for(const t of n){let n;if("text"===t.type){if(!t.text)continue;n={type:"text",text:t.text}}else n="resource_link"===t.type?{type:"resource_link",uri:t.uri??"",...t.fileName?{name:t.fileName}:{},...t.mimeType?{mimeType:t.mimeType}:{}}:{type:t.type,...t.mimeType?{mimeType:t.mimeType}:{},...t.fileName?{fileName:t.fileName}:{}};this.transport.sendSessionUpdate(e,{sessionUpdate:"user_message_chunk",content:n,...s?{metadata:s}:{}})}}async replayAssistantTurnAsStream(e,t){if(!t.sourceMessages?.length){const s={sessionUpdate:"history_turn",role:t.role,content:t.content,reasoning:t.reasoning,toolCalls:t.toolCalls};return t.metadata&&(s.metadata=t.metadata),void this.transport.sendSessionUpdate(e,s)}for(const s of t.sourceMessages){switch(s.updateType??"agent_message_chunk"){case"agent_message_chunk":this.transport.sendSessionUpdate(e,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:s.content}});break;case"agent_thought_chunk":this.transport.sendSessionUpdate(e,{sessionUpdate:"agent_thought_chunk",content:{type:"text",text:s.content}});break;case"tool_call":{const n=s.toolCallId??"",i=t.toolCalls?.find(e=>e.id===n),o=s.toolName??"tool",r=s.content??"";let a={};if(r)try{a=JSON.parse(r)}catch{}this.transport.sendSessionUpdate(e,buildToolCallPayload({toolCallId:n,toolName:o,args:a,kind:i?.kind,status:i?.status??"in_progress"}));break}case"tool_call_update":{const n=s.toolCallId??"",i=t.toolCalls?.find(e=>e.id===n),o=s.toolName??i?.name??"tool",r=s.content??i?.output??"";let a=await readEnrichmentCache(n).catch(()=>{});if(!a){const e=extractBeeosFileUrisFromOutput(r);e.length>0&&(a=e.map(e=>({uri:e,name:"file"})))}if(!a&&"beeos_upload_file"===o){const e=await readUploadToolResult(n).catch(()=>{});if(e){const t=buildToolResultSessionContentBlocks(o,e).filter(e=>"resource_link"===e.content?.type&&e.content.uri).map(e=>({uri:e.content.uri,name:e.content.name??"file",...e.content.mimeType?{mimeType:e.content.mimeType}:{}}));t.length>0&&(a=t)}}this.transport.sendSessionUpdate(e,buildToolCallUpdatePayload({toolCallId:n,toolName:o,outputText:r,kind:i?.kind,status:i?.status??"completed",resourceLinks:a}));break}}}}emitPromptAsUserSessionUpdates(e,t){for(const s of t){const t=this.toUserSessionUpdateContent(s);t&&this.transport.sendSessionUpdate(e,{sessionUpdate:"user_message_chunk",content:t})}}toUserSessionUpdateContent(e){if("text"===e.type){if(!e.text)return;return{type:"text",text:e.text}}if("image"===e.type){const t=e.mimeType??e.mime_type??"application/octet-stream",s=e.fileName??e.file_name??e.name;if(!e.data&&!e.uri)return;return{type:"image",...e.data?{data:e.data}:{},...e.uri?{uri:e.uri}:{},...t?{mimeType:t}:{},...s?{fileName:s}:{}}}if("resource_link"===e.type){if(!e.uri)return;return{type:"resource_link",uri:e.uri,...e.title?{title:e.title}:{},...e.name?{name:e.name}:{},...e.mimeType??e.mime_type?{mimeType:e.mimeType??e.mime_type}:{}}}if("file"===e.type){const t=e.mimeType??e.mime_type??"application/octet-stream",s=e.fileName??e.file_name??e.filename??e.name;if(!e.data&&!e.text&&!e.uri)return;return{type:"file",...e.data?{data:e.data}:{},...e.text?{text:e.text}:{},...e.uri?{uri:e.uri}:{},...t?{mimeType:t}:{},...s?{fileName:s}:{}}}if("resource"===e.type){const t=e.resource??{},s=t.uri??e.uri,n=t.mimeType??t.mime_type??e.mimeType??e.mime_type,i=t.text??e.text,o=t.data??e.data,r=t.fileName??t.file_name??t.filename??t.name??e.fileName??e.file_name??e.filename??e.name;if(!s&&!i&&!o)return;return{type:"resource",resource:{...s?{uri:s}:{},...n?{mimeType:n}:{},...i?{text:i}:{},...o?{data:o}:{},...r?{fileName:r}:{}}}}}handleSessionCancel(e,t){const s=t?.sessionId;if(!s)return void(void 0!==e&&this.transport.sendAcpError(e,Errors.MissingParam,{param:"sessionId"}));const n=this.state.sessions.get(s);if(!n)return void(void 0!==e&&this.transport.sendAcpError(e,Errors.SessionNotFound));const i=this.getGateway(),o=i?.isConnected,r=!(!n.currentRunId&&!n.gatewayRequestId);if(o&&r){i.agentCancel(n.gatewaySessionKey,{runId:n.currentRunId,requestId:n.gatewayRequestId})}this.endRun(n,{kind:"complete",stopReason:"cancelled"})||this.transport.sendSessionUpdate(n.sessionId,{sessionUpdate:"stream_complete",stopReason:"cancelled"}),void 0!==e&&this.transport.sendResult(e,{})}endRun(e,t){if(e.promptDone)return this.logger.debug?.(`[bridge-core] endRun skipped (promptDone=true) sessionId=${e.sessionId} outcome=${t.kind}`),!1;if(void 0===e.pendingPromptId&&void 0===e.currentRunId)return this.logger.debug?.(`[bridge-core] endRun skipped (no pendingPromptId and no currentRunId) sessionId=${e.sessionId} outcome=${t.kind}`),!1;e.promptDone=!0;const s=e.currentRunId;if("complete"===t.kind){if(s&&this.onRunComplete){const t=e.agentAssistantTextSoFar??e.chatAssistantTextSoFar??"";this.onRunComplete(s,t)}void 0!==e.pendingPromptId&&this.transport.sendResult(e.pendingPromptId,{stopReason:t.stopReason}),this.transport.sendSessionUpdate(e.sessionId,{sessionUpdate:"stream_complete",stopReason:t.stopReason})}else s&&this.onRunFailed&&this.onRunFailed(s,t.message),void 0!==e.pendingPromptId&&this.transport.sendError(e.pendingPromptId,t.code,t.message),this.transport.sendSessionUpdate(e.sessionId,{sessionUpdate:"stream_complete",stopReason:"error",error:t.message});return this.cleanupPrompt(e),this.onRunEnded?.(e,t),!0}schedulePromptTimeout(e){this.state.clearPromptTimeout(e);const t=this.config.promptTimeoutMs??DEFAULT_PROMPT_TIMEOUT_MS;t<=0||(e.promptTimeoutTimer=setTimeout(()=>{void 0!==e.pendingPromptId&&(this.logger.warn?.(`[bridge-core] Prompt timed out sessionId=${e.sessionId} after ${t}ms`),this.failPrompt(e,Errors.PromptTimeout.rpcCode,`Prompt timed out after ${t}ms`))},t),"object"==typeof e.promptTimeoutTimer&&"unref"in e.promptTimeoutTimer&&e.promptTimeoutTimer.unref())}cleanupPrompt(e){this.state.clearPromptTimeout(e),this.state.clearStreamIdleTimer(e);const t=e.currentRunId;if(t){this.state.promptsByRunId.delete(t),this.state.markRunCompleted(t);const e=`tc_${t}_`;for(const[t,s]of this.state.toolCallIdMap)s.startsWith(e)&&this.state.toolCallIdMap.delete(t)}e.gatewayRequestId&&this.state.promptsByGatewayRequestId.delete(e.gatewayRequestId),e.pendingPromptId=void 0,e.promptDone=void 0,e.currentRunId=void 0,e.gatewayRequestId=void 0,e.assistantStreamStartedAt=void 0,e.gatewayMethod=void 0,e.assistantMq?.close()}completePrompt(e,t){this.endRun(e,{kind:"complete",stopReason:t})}failPrompt(e,t,s){this.endRun(e,{kind:"error",code:t,message:s})}cleanupAllPendingPrompts(){for(const e of this.state.sessions.values())this.failPrompt(e,Errors.GatewayDisconnected.rpcCode,Errors.GatewayDisconnected.message)}handleGatewayResponse(e){const t=e.id;if(!t)return;const s=this.state.promptsByGatewayRequestId.get(t);if(!s||s.promptDone)return;if(!e.ok){const n=e.error,i=n?.message??"gateway returned error";return this.logger.warn?.(`[bridge-core] handleGatewayResponse: gateway rejected prompt gatewayRequestId=${t} error=${i}`),void this.failPrompt(s,Errors.SendFailed.rpcCode,i)}const n=e.payload,i=n?.runId;i?(s.currentRunId=i,s.assistantStreamStartedAt=Date.now(),this.state.promptsByRunId.set(i,s),this.logger.debug?.(`[bridge-core] handleGatewayResponse: bound runId=${i} gatewayRequestId=${t} sessionId=${s.sessionId}`)):this.logger.debug?.(`[bridge-core] handleGatewayResponse: no runId in payload gatewayRequestId=${t} sessionId=${s.sessionId}`)}maybeCompletePromptFromChatState(e,t){if(e.promptDone||"chat.send"!==e.gatewayMethod)return;const s="string"==typeof t.state?t.state.toLowerCase():void 0;if(s)if("final"===s)this.completePrompt(e,"end_turn");else if("aborted"===s||"cancelled"===s||"cancel"===s)this.completePrompt(e,"cancelled");else if("error"===s){const s=("string"==typeof t.summary?t.summary:void 0)??("string"==typeof t.message?t.message:void 0)??"gateway chat error";this.failPrompt(e,Errors.AgentError.rpcCode,formatFriendlyError(s))}}pushToolResultContent(e,t,s,n){if(0===s.length)return;const i=n?this.state.promptsByRunId.get(n):void 0;if(!i)return void this.logger.warn?.(`[bridge-core] pushToolResultContent dropped — no session for runId=${n??"(none)"}`);const o=i.sessionId;for(const e of s){const t={sessionUpdate:"agent_message_chunk",content:e};this.transport.sendSessionUpdate(o,t),i.assistantMq?.push(t)}this.writeObs({component:"bridge-core",domain:"tool",name:"tool_result.direct_push",severity:"info",payload:{toolCallId:e,toolName:t,blockCount:s.length,sessionId:o}})}async handleCronRequest(e){const t=this.getGateway();if(!t?.isConnected)return void this.transport.sendAcpError(e.id,Errors.GatewayNotConnected);const s=e.method.replace("/",".");try{const n=await t.request(s,e.params??{});this.transport.sendResult(e.id,n??{})}catch(t){const n=t instanceof Error?t.message:"cron request failed";this.logger.warn?.(`[bridge-core] ${s} failed: ${n}`),this.transport.sendAcpError(e.id,Errors.SendFailed,{message:n})}}}async function fetchWithRetry(e,t={}){const s=t.maxRetries??3;let n;for(let i=0;i<=s;i++){const o=await fetch(e,{signal:AbortSignal.timeout(12e4),redirect:"follow"});if(o.ok)return Buffer.from(await o.arrayBuffer());if(429===o.status||o.status>=500){const r=Number(o.headers.get("retry-after"))||0,a=1e3*Math.max(r,2**i);if(n=new Error(`HTTP ${o.status} from ${e}`),i<s){t.logger?.info?.(`[bridge-core] Download ${o.status}, retry ${i+1}/${s} in ${(a/1e3).toFixed(0)}s`),await new Promise(e=>setTimeout(e,a));continue}}throw new Error(`Failed to download zip: ${o.status}`)}throw n??new Error("Download failed after retries")}const SKILL_ROOT_MARKERS=["SKILL.md","skill.md","SKILL.MD"];async function resolveSkillRoot(e){for(const t of SKILL_ROOT_MARKERS)try{return await stat(join(e,t)),e}catch{}const t=(await readdir(e,{withFileTypes:!0})).filter(e=>e.isDirectory()&&!e.name.startsWith(".")&&"__MACOSX"!==e.name);if(1===t.length){const s=join(e,t[0].name);for(const e of SKILL_ROOT_MARKERS)try{return await stat(join(s,e)),s}catch{}return s}return e}async function extractZipToDir(e,t){const s=new AdmZip(e),n=join(t,".beeos-extract-tmp-"+Date.now());try{await mkdir(n,{recursive:!0}),s.extractAllTo(n,!0);const e=await resolveSkillRoot(n),i=await readdir(e,{withFileTypes:!0});let o=0;for(const s of i){if(s.name.startsWith(".beeos-extract-tmp")||"__MACOSX"===s.name)continue;const n=join(e,s.name),i=join(t,s.name);await rename(n,i).catch(async()=>{await cp(n,i,{recursive:!0})}),o++}if(0===o)throw new Error("Zip archive contained no extractable files")}finally{await rm(n,{recursive:!0,force:!0}).catch(()=>{})}}const VALID_SLUG_PATTERN=/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,NON_ASCII_PATTERN=/[^\x00-\x7F]/;function validateSkillSlug(e){const t=e.trim();if(!t||t.includes("/")||t.includes("\\")||t.includes(".."))throw new Error(`Invalid skill slug: ${e}`);if(NON_ASCII_PATTERN.test(t)||!VALID_SLUG_PATTERN.test(t))throw new Error(`Invalid skill slug: ${e}`);return t}function safeDirName(e){const t=e.trim();return t?t.replaceAll("/","__").replaceAll("\\","__"):t}function resolveSkillInstallDir(e,t){const s=safeDirName(t),n=join(e,s),i=resolve(e),o=resolve(n),r=relative(i,o);if(!r||".."===r||r.startsWith(`..${sep}`)||path.isAbsolute(r))throw new Error("Invalid skill target path");return n}
1
+ import{randomUUID,createHash}from"node:crypto";import{mkdir,writeFile,readFile,rm,lstat,readlink,stat,readdir,rename,cp}from"node:fs/promises";import path from"node:path";import{join,resolve,relative,sep}from"node:path";import{homedir}from"node:os";import AdmZip from"adm-zip";import{sanitizePromptPayload,stripTransportMetadata,isHeartbeatPromptText,isHeartbeatOnlyText}from"../message-filter.js";import{CANVAS_TERM_MAPPING,CANVAS_UPDATE_HINT,buildActiveSurfacesContext}from"../canvas/tools.js";import{parseMentionsParam,buildMentionInstruction}from"../a2a/mention-resolver.js";import{extractPromptBlocks,toGatewayPrompt,resolveSlashCommandName as resolveSlashCmd}from"./prompt-converter.js";import{loadHistory,groupIntoTurns,extractBeeosFileUrisFromOutput,splitUserReplayBlocks}from"./history-replay.js";import{buildResolutionPlan,resolveResources,applyResolutions}from"./prompt-resource-resolver.js";import{readEnrichmentCache,readUploadToolResult}from"../upload/result-cache.js";import{buildToolResultSessionContentBlocks}from"./tool-result-payload-strategies.js";import{buildToolCallPayload,buildToolCallUpdatePayload}from"./tool-call-payload-builder.js";import{Errors}from"../errors/index.js";import{formatFriendlyError}from"../errors/classify.js";import{resolveChannelFromSessionKey}from"../channel-detection.js";import{ACP_PROTOCOL_VERSION,DEFAULT_PROMPT_TIMEOUT_MS}from"./types.js";const DEFAULT_MODE={id:"default",name:"Default",description:"Default agent mode"},USER_MESSAGE_PREFIX="User Message From BeeOS:",USER_MESSAGE_PREFIX_WITH_NEWLINE=`${USER_MESSAGE_PREFIX}\n`,SLASH_COMMAND_NAME_RE=/^[a-z0-9_-]+$/i;function withUserMessagePrefix(e){return e.startsWith(USER_MESSAGE_PREFIX)?e:`${USER_MESSAGE_PREFIX_WITH_NEWLINE}${e}`}const BACKGROUND_SESSION_PATTERNS=[":cron:",":hook:","node-",":a2a:"];function isBackgroundSessionKey(e){return BACKGROUND_SESSION_PATTERNS.some(t=>e.includes(t))}function isSystemDefaultSessionKey(e){const t=e.split(":");if(t.length<3||"agent"!==t[0])return!1;const s=t.slice(2).join(":");return"main"===s||s===t[1]}function isPlaceholderTitle(e){return!e||"New chat"===e||(!!/^[0-9a-f]{6,12}\s*\(20\d{2}/i.test(e)||(!!/^Sender\s*\(untrusted/i.test(e)||!!/^Conversation\s+info\s*\(untrusted/i.test(e)))}function cleanGatewayDerivedTitle(e){if(!e)return;let t=stripTransportMetadata(e);return/^\[System\b/.test(t)?void 0:(t=t.trim(),t||void 0)}function resolveStandaloneSlashCommandName(e,t){if(0===e.length)return;if(e.some(e=>"text"!==e.type))return;const s=t.trim();if(!s||s.includes("\n")||!s.startsWith("/"))return;const n=s.match(/^\/([^\s:]+)(?::|\s|$)/)?.[1]?.trim();return n&&!n.includes("/")&&SLASH_COMMAND_NAME_RE.test(n)?n.toLowerCase():void 0}function deriveSessionId(e,t){const s=createHash("sha256").update(`${t}:${e}`).digest("hex").substring(0,32);return`session:${s.slice(0,8)}-${s.slice(8,12)}-${s.slice(12,16)}-${s.slice(16,20)}-${s.slice(20,32)}`}function parseAgentIdFromKey(e){const t=e.split(":");return(t.length>=3&&"agent"===t[0]?t[1]:"")||"main"}function createSessionObject(e){const t=parseAgentIdFromKey(e.gatewayKey);return{sessionId:deriveSessionId(e.gatewayKey,e.instanceId),gatewaySessionKey:e.gatewayKey,agentId:t,platformAgentId:e.resolvePlatformAgentId?.(t),createdAt:Date.now(),chatReplayDedupKeys:new Set,cwd:e.cwd,title:e.title,channelType:resolveChannelFromSessionKey(e.gatewayKey,e.thirdPartyChannels)}}export class AcpGatewayBridgeCore{state;transport;config;logger;writeObs;getGateway;thirdPartyChannels;beeosFileResolver;initialized=!1;agentMapping=null;_syncResolvers=[];_mappingReadyCallbacks=[];_loadSessionLock=new Map;setAgentMapping(e){this.agentMapping=e;for(const t of this.state.sessions.values())!t.platformAgentId&&t.agentId&&(t.platformAgentId=e.get(t.agentId)?.id);for(const e of this._syncResolvers)e();this._syncResolvers=[];for(const e of this._mappingReadyCallbacks)e();this._mappingReadyCallbacks=[]}onceAgentMappingReady(e){this.agentMapping&&this.agentMapping.size>0?e():this._mappingReadyCallbacks.push(e)}markAgentSyncDone(){for(const e of this._syncResolvers)e();this._syncResolvers=[]}waitForAgentMapping(e=15e3){return this.agentMapping?Promise.resolve():new Promise(t=>{this._syncResolvers.push(t);const s=setTimeout(()=>{const e=this._syncResolvers.indexOf(t);-1!==e&&this._syncResolvers.splice(e,1),t()},e);"object"==typeof s&&"unref"in s&&s.unref()})}handleTerminalRequest=null;handleTerminalNotification=null;onRunComplete=null;onRunFailed=null;onRunEnded=null;handleSlashCommand=null;onPromptStart=null;instanceId;constructor(e){this.state=e.state,this.transport=e.transport,this.config=e.config,this.logger=e.logger,this.writeObs=e.writeObs,this.getGateway=e.getGateway,this.instanceId=e.instanceId||"unknown",this.thirdPartyChannels=e.thirdPartyChannels??new Set,this.beeosFileResolver=e.beeosFileResolver}resolvePlatformAgentId(e){return this.agentMapping?.get(e)?.id}getDefaultAgentId(){return this.agentMapping&&this.agentMapping.size>0?this.agentMapping.keys().next().value??"main":"main"}registerSession(e){this.state.sessions.set(e.sessionId,e),this.state.sessionsByGatewayKey.set(e.gatewaySessionKey,e),this.ensureVerboseFull(e)}async ensureVerboseFull(e){if(e.verbosePatched)return;const t=this.getGateway();if(t?.isConnected){e.verbosePatched=!0;try{await t.sessionsPatch(e.gatewaySessionKey,{verboseLevel:"full"})}catch(t){e.verbosePatched=!1,this.logger.debug?.(`[bridge-core] ensureVerboseFull failed: ${t instanceof Error?t.message:t}`)}}}ensureSessionForKey(e){const t=this.state.findSessionByKey(e);if(t)return t;const s=createSessionObject({gatewayKey:e,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels});return this.registerSession(s),this.logger.debug?.(`[bridge-core] ensureSessionForKey: lazily created session sid=${s.sessionId} key=${e}`),s}async preloadSessionsFromGateway(){const e=this.getGateway();if(e?.isConnected)try{const t=await e.sessionsList();if(!t||"object"!=typeof t)return;const s=t.sessions;if(!Array.isArray(s))return;let n=0;for(const e of s){const t=e,s=t.sessionKey??t.key??t.id??t.name;if(!s||"string"!=typeof s)continue;if(this.state.findSessionByKey(s))continue;const i=t.label||t.derivedTitle||t.title||void 0,o=i&&!isPlaceholderTitle(i)?i:void 0,r=createSessionObject({gatewayKey:s,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,title:o});this.registerSession(r),n++}n>0&&this.logger.info?.(`[bridge-core] preloadSessions: loaded ${n} session(s) from gateway`)}catch(e){this.logger.debug?.("[bridge-core] preloadSessions failed:",e)}else this.logger.debug?.("[bridge-core] preloadSessions skipped: gateway not connected")}async handleRequest(e){this.writeObs({component:"bridge-core",domain:"forward",name:`rpc.request.${e.method}`,severity:"debug",payload:{id:e.id,method:e.method}});try{switch(e.method){case"initialize":this.handleInitialize(e);break;case"session/new":await this.handleSessionNew(e);break;case"session/load":await this.handleSessionLoad(e);break;case"session/list":await this.handleSessionList(e);break;case"session/rename":this.handleSessionRename(e);break;case"session/delete":await this.handleSessionDelete(e);break;case"session/clear":await this.handleSessionClear(e);break;case"session/prompt":await this.handleSessionPrompt(e);break;case"session/set_mode":await this.handleSessionSetMode(e);break;case"session/set_model":await this.handleSessionSetModel(e);break;case"skills/list":await this.handleSkillsList(e);break;case"skills/install":await this.handleSkillsInstall(e);break;case"skills/uninstall":await this.handleSkillsUninstall(e);break;case"skills/update":await this.handleSkillsUpdate(e);break;case"models/list":await this.handleModelsList(e);break;case"session/cancel":this.handleSessionCancel(e.id,e.params);break;case"cron/list":case"cron/status":case"cron/add":case"cron/update":case"cron/remove":case"cron/run":case"cron/runs":await this.handleCronRequest(e);break;default:e.method.startsWith("terminal/")&&this.handleTerminalRequest?await this.handleTerminalRequest(e):this.transport.sendAcpError(e.id,Errors.MethodNotFound,{message:`Method not found: ${e.method}`})}}catch(t){this.transport.sendAcpError(e.id,Errors.InternalError,{message:t instanceof Error?t.message:"Internal error"})}}handleCanvasNotification=null;getCanvasState=null;onSessionLoaded=null;onSkillInstalled=null;onSessionDeleted=null;onSessionCleared=null;handleNotification(e){switch(e.method){case"session/cancel":this.handleSessionCancel(void 0,e.params);break;case"canvas/toggle":case"canvas/clear":this.handleCanvasNotification?this.handleCanvasNotification(e.method,e.params):this.logger.debug?.(`[bridge-core] Canvas notification ignored (canvas disabled): ${e.method}`);break;case"canvas/reference":{const t=e.params,s=t?.surfaceId,n=this.state.resolveSessionFromParams(t);if(!n){this.logger.warn?.(`[bridge-core] canvas/reference ignored — no session found for sessionId=${t?.sessionId}`);break}n.referencedSurfaceId=s||void 0,this.logger.debug?.(`[bridge-core] Canvas reference ${s?"set":"cleared"}: ${s??"(none)"}`);break}default:e.method.startsWith("terminal/")&&this.handleTerminalNotification?this.handleTerminalNotification(e.method,e.params):this.logger.debug?.(`[bridge-core] Unknown notification: ${e.method}`)}}handleInitialize(e){this.initialized=!0;const t=this.transport.forwardControl;!1===e.params?.forwardThinking&&(t.forwardThinking=!1),!1===e.params?.forwardToolCalls&&(t.forwardToolCalls=!1),this.transport.sendResult(e.id,{protocolVersion:ACP_PROTOCOL_VERSION,agentCapabilities:{loadSession:!0,skills:!0,promptCapabilities:{image:!0,audio:!1,embeddedContext:!0},sessionCapabilities:{list:{},"web-ssh":!!this.config.bridge.shell.enabled,canvas:!!this.config.canvasEnabled,cron:!0},mcp:{}},agentInfo:{name:"beeos-claw",title:"BeeOS Agent Bridge",version:"0.1.0"},_meta:{},authMethods:[]})}async handleSessionNew(e){const t=`agent:${"string"==typeof e.params?.agentId&&e.params.agentId||this.config.gateway.agentId||this.getDefaultAgentId()}:${randomUUID()}`,s="string"==typeof e.params?.cwd?e.params.cwd:".",n=createSessionObject({gatewayKey:t,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,cwd:s});this.registerSession(n),this.transport.sendResult(e.id,{sessionId:n.sessionId,modes:{availableModes:[{...DEFAULT_MODE}],currentModeId:DEFAULT_MODE.id},_meta:{sessionKey:t,agentId:n.agentId,platformAgentId:n.platformAgentId??null}}),this.fetchHistoryInBackground(t)}fetchHistoryInBackground(e){const t=this.getGateway();t?.isConnected&&loadHistory(t,e,50).catch(e=>{this.logger.debug?.(`[bridge-core] Background history fetch failed: ${e instanceof Error?e.message:e}`)})}async recoverSessionFromGateway(e){const t=this.getGateway();if(t?.isConnected)try{const s=await t.sessionsList();if(!s||"object"!=typeof s)return;const n=s.sessions;if(!Array.isArray(n))return;for(const t of n){const s=t,n=s.sessionKey??s.key??s.id??s.name;if(n&&"string"==typeof n&&deriveSessionId(n,this.instanceId)===e){const e=s.label||s.derivedTitle||s.title||void 0,t=e&&!isPlaceholderTitle(e)?e:void 0,i=createSessionObject({gatewayKey:n,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,title:t});return this.registerSession(i),i}}}catch(e){this.logger.debug?.("[bridge-core] recoverSessionFromGateway failed:",e)}}async resolveOrCreateSession(e){let t=this.state.sessions.get(e);if(!t&&e.startsWith("agent:")&&(t=this.state.findSessionByKey(e)),t||e.startsWith("agent:")||(t=await this.recoverSessionFromGateway(e)),t)return{session:t,created:!1};const s=e.startsWith("agent:"),n=this.config.gateway.agentId||this.getDefaultAgentId();return t=createSessionObject({gatewayKey:s?e:`agent:${n}:${e}`,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels}),this.registerSession(t),{session:t,created:!0}}async handleSessionLoad(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId"});const s=this._loadSessionLock.get(t);if(s)try{await s}catch{}const n=this._doSessionLoad(e,t);this._loadSessionLock.set(t,n);try{await n}finally{this._loadSessionLock.get(t)===n&&this._loadSessionLock.delete(t)}}async _doSessionLoad(e,t){const{session:s}=await this.resolveOrCreateSession(t),n=s.sessionId;this.state.lastActiveSessionId=n,s.chatReplayDedupKeys.clear();let i=[];try{const e=this.getGateway(),t=await loadHistory(e,s.gatewaySessionKey,100);i=groupIntoTurns(t);const o=i.filter(e=>"user"===e.role).length,r=i.filter(e=>"assistant"===e.role).length,a=i.filter(e=>e.toolCalls?.length).length,l=i.reduce((e,t)=>e+(t.toolCalls?.length??0),0);if(this.logger.debug?.(`[bridge-core] handleSessionLoad sid=${n} messages=${t.length} turns=${i.length} userTurns=${o} asstTurns=${r} turnsWithToolCalls=${a} totalToolCalls=${l}`),i.length>0&&isPlaceholderTitle(s.title)&&"manual"!==s.titleSource){const e=i.find(e=>{if("user"!==e.role)return!1;if("internal_system"===e.provenance?.kind)return!1;const t=stripTransportMetadata(e.content).trim();return t.length>0&&!/^Sender\s*\(untrusted/i.test(t)});if(e?.content){const t=stripTransportMetadata(e.content).trim().slice(0,64).replace(/\n/g," ").trim();if(t){s.title=t,s.titleSource="truncated";const e=this.getGateway();e?.isConnected&&e.sessionsPatch(s.gatewaySessionKey,{label:t}).catch(()=>{}),this.transport.sendSessionUpdate(n,{sessionUpdate:"title_update",title:t}),this.tryGenerateTitleFromHistory(s,n,i)}}}for(let e=0;e<i.length;e++){const t=i[e];"user"!==t.role||"internal_system"!==t.provenance?.kind?"user"===t.role&&/^\s*system\s*\(untrusted[^)]*\)/i.test(t.content)?this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] system-injected user message`):"user"===t.role&&isHeartbeatPromptText(t.content)?this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] heartbeat cron prompt`):"assistant"===t.role&&isHeartbeatOnlyText(t.content)?this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] heartbeat assistant reply`):"system"!==t.role?(this.logger.debug?.(`[bridge-core] SENDING history_turn[${e}] role=${t.role} contentLen=${t.content.length} content=${JSON.stringify(t.content.slice(0,80))} tcCount=${t.toolCalls?.length??0}`),"user"===t.role?this.replayUserTurnAsChunks(n,t.content,t.metadata,t.timestamp):await this.replayAssistantTurnAsStream(n,t)):(this.logger.debug?.(`[bridge-core] SENDING system_message[${e}] contentLen=${t.content.length}`),this.transport.sendSessionUpdate(n,{sessionUpdate:"system_message_chunk",content:{type:"text",text:t.content},metadata:t.metadata,...t.timestamp?{timestamp:t.timestamp}:{}})):this.logger.debug?.(`[bridge-core] SKIP history_turn[${e}] internal_system user message`)}}catch(e){this.logger.warn?.("[bridge-core] Failed to load session history:",e)}const o=new Set;for(const e of i)if(e.toolCalls)for(const t of e.toolCalls)t.id&&o.add(t.id),t.callId&&t.callId!==t.id&&o.add(t.callId);let r="",a="";for(let e=i.length-1;e>=0;e--)if("assistant"===i[e].role){r=i[e].content.slice(-200),a=(i[e].reasoning??"").slice(-200);break}if(void 0!==s.pendingPromptId&&!s.promptDone&&s.assistantMq&&!s.assistantMq.closed)try{this.replayCurrentAssistantStream(s,o,r,a)}catch(e){this.logger.warn?.(`[bridge-core] replayCurrentAssistantStream failed: ${e instanceof Error?e.message:e}`)}(()=>{this.transport.sendResult(e.id,{sessionId:n}),this.onSessionLoaded?.(n,s.gatewaySessionKey,s.platformAgentId),this.pushSkillsList().catch(e=>{this.logger.warn?.(`[bridge-core] pushSkillsList failed: ${e instanceof Error?e.message:e}`)})})()}replayCurrentAssistantStream(e,t,s="",n=""){if(!e.assistantMq||e.assistantMq.closed)return;const i=e.assistantMq.peek(0);if(0===i.length)return;let o=0,r=0,a="",l="";for(const d of i)if(t?.size&&d.toolCallId&&t.has(d.toolCallId)&&("tool_call"===d.sessionUpdate||"tool_call_update"===d.sessionUpdate))r++;else{if(s.length>0&&"agent_message_chunk"===d.sessionUpdate){const e=d.content?.text;if("string"==typeof e&&(a+=e,a.length<=s.length)){r++;continue}}if(n.length>0&&"agent_thought_chunk"===d.sessionUpdate){const e=d.content?.text;if("string"==typeof e&&(l+=e,l.length<=n.length)){r++;continue}}this.transport.sendSessionUpdate(e.sessionId,d),o++}r>0&&this.logger.debug?.(`[bridge-core] replayCurrentAssistantStream skipped ${r} duplicate tool events (already in history)`),o>0&&this.writeObs({component:"bridge-core",domain:"lifecycle",name:"session.load.replay_complete",severity:"debug",payload:{sessionId:e.sessionId,replayed:o}})}async handleSessionList(e){const t=[];for(const[,e]of this.state.sessions)isBackgroundSessionKey(e.gatewaySessionKey)||t.push({sessionId:e.sessionId,title:e.title||"New chat",cwd:e.cwd,updatedAt:e.lastStreamActivityTs??e.createdAt,agentId:e.agentId,platformAgentId:e.platformAgentId,deletable:!isSystemDefaultSessionKey(e.gatewaySessionKey)});const s=this.getGateway();if(s?.isConnected)try{const e=await s.sessionsList();if(e&&"object"==typeof e&&Array.isArray(e.sessions)){const s=e.sessions;for(const e of s){const s=e,n=s.sessionKey??s.key??s.id??s.name;if(!n||"string"!=typeof n)continue;if(isBackgroundSessionKey(n))continue;const i=deriveSessionId(n,this.instanceId),o=t.find(e=>e.sessionId===n||e.sessionId===i||this.state.sessions.get(e.sessionId)?.gatewaySessionKey===n),r=s.label||cleanGatewayDerivedTitle(s.derivedTitle)||s.title||void 0,a=r&&!isPlaceholderTitle(r)?r:void 0,l=s.model,d=s.modelProvider;if(o){if(a&&isPlaceholderTitle(o.title)){o.title=a;const e=this.state.sessions.get(o.sessionId);e&&isPlaceholderTitle(e.title)&&(e.title=a)}const e=s.updatedAt;e&&(o.updatedAt=e),l&&(o.model=l),d&&(o.modelProvider=d)}else{const e=createSessionObject({gatewayKey:n,instanceId:this.instanceId,resolvePlatformAgentId:e=>this.resolvePlatformAgentId(e),thirdPartyChannels:this.thirdPartyChannels,cwd:s.cwd,title:a});this.registerSession(e),t.push({sessionId:e.sessionId,title:a||"",cwd:s.cwd,updatedAt:s.updatedAt,agentId:e.agentId,platformAgentId:e.platformAgentId,deletable:!isSystemDefaultSessionKey(n),model:l,modelProvider:d})}}}}catch(e){this.logger.debug?.("[bridge-core] sessions.list enrichment failed:",e)}t.sort((e,t)=>(t.updatedAt??0)-(e.updatedAt??0)||e.sessionId.localeCompare(t.sessionId)),this.transport.sendResult(e.id,{sessions:t})}handleSessionRename(e){const t=e.params?.sessionId,s=e.params?.title;if(!t||!s)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId, title"});const n=s.slice(0,64),i=this.state.sessions.get(t);if(!i)return void this.transport.sendAcpError(e.id,Errors.SessionNotFound);i.title=n,i.titleSource="manual";const o=i.gatewaySessionKey,r=this.getGateway();r?.isConnected&&r.sessionsPatch(o,{label:n}).catch(e=>{this.logger.debug?.(`[bridge-core] sessionsPatch (rename) failed: ${e instanceof Error?e.message:e}`)}),this.transport.sendResult(e.id,{sessionId:t,title:n})}static TITLE_TEMPLATES=[{match:["今日日报","日报"],zh:"今日工作日报",en:"Daily Work Log"},{match:["周报","周报总结","本周周报"],zh:"本周工作周报",en:"Weekly Work Report"},{match:["月报","月度汇报","月度总结"],zh:"本月工作月报",en:"Monthly Work Report"},{match:["工作日志","日志"],zh:"工作日志整理",en:"Work Log Review"},{match:["晨会","今日晨会"],zh:"今日晨会纪要",en:"Daily Standup Notes"},{match:["复盘","今日复盘"],zh:"今日工作复盘",en:"Daily Retrospective"},{match:["新会话","new chat","new conversation"],zh:"新会话",en:"New Conversation"}];static TITLE_SYSTEM_PROMPT='You are a conversation title generator. Your task is to generate a concise, human-readable title summarizing the topic of the user\'s question together with the assistant\'s answer.\n\n### Rules:\n- Length: 3-6 words in English, or 6-20 characters in Chinese (up to 24 if needed to keep all key domain terms intact).\n- Language: Match the user\'s input language exactly. Mixed zh/en input → use the dominant language.\n- Extract the core topic (noun-centric) of the Q&A exchange, not the action verb.\n- Use the assistant\'s answer to disambiguate or enrich the topic when the question alone is vague (e.g. "帮我看下" + an answer about x402 → "x402 支付集成").\n- No punctuation, no quotation marks, no emojis, no markdown.\n- No prefix like "Title:" or "Here is". Output the title only.\n- Avoid generic words: "help", "question", "chat", "problem", "task".\n- If both the question and answer are too vague to summarize, return "新会话" (for zh) or "New Conversation" (for en).\n\n### Input Format:\nThe user message will contain both the question and the assistant\'s answer, delimited like this:\n[User Question]\n...question text...\n\n[Assistant Answer]\n...answer text (may be truncated with "..." in the middle)...\n\n### Output Format:\nRespond ONLY with valid JSON, no other text:\n{"title": "<your title here>"}\n\n### Examples:\nInput:\n[User Question]\n帮我写一个关于BeeOS设备接入的PRD\n\n[Assistant Answer]\n好的,这是 BeeOS 设备接入 PRD 的草稿:1. 目标 ...\nOutput: {"title": "BeeOS 设备接入 PRD"}\n\nInput:\n[User Question]\n帮我看下\n\n[Assistant Answer]\n你说的 x402 支付协议是一个基于 HTTP 402 的链上支付标准 ...\nOutput: {"title": "x402 支付协议解析"}\n\nInput:\n[User Question]\nWhat\'s the best way to integrate x402 payment protocol?\n\n[Assistant Answer]\nYou have three options: 1. Use the official SDK ...\nOutput: {"title": "x402 Payment Integration"}\n\nInput:\n[User Question]\nhi\n\n[Assistant Answer]\nHello! How can I help you today?\nOutput: {"title": "New Conversation"}';matchTitleTemplate(e){const t=e.trim().toLowerCase();for(const s of AcpGatewayBridgeCore.TITLE_TEMPLATES)if(s.match.some(e=>e.toLowerCase()===t))return/[\u4e00-\u9fff]/.test(e)?s.zh:s.en}isGuardMessage(e){const t=e.trim();return[...t].length<3||[...t].every(e=>/[\p{P}\p{S}\s]/u.test(e))}truncateQuestionForTitle(e){return e.length>2e3?e.slice(0,1500):e}truncateAnswerForTitle(e){return e.length<=1e3?e:`${e.slice(0,500)}\n...\n${e.slice(-500)}`}generateTitleFromQA(e,t,s,n){if(this.isGuardMessage(s))return void this.logger.info?.(`[bridge-core] title-gen SKIP guard message sid=${t}`);const i=this.matchTitleTemplate(s);if(i){e.title=i,e.titleSource="template";const s=this.getGateway();return s?.isConnected&&s.sessionsPatch(e.gatewaySessionKey,{label:i}).catch(()=>{}),void this.transport.sendSessionUpdate(t,{sessionUpdate:"title_update",title:i})}const o=(process.env.AROUTER_BASE_URL||"").replace(/\/$/,""),r=process.env.AROUTER_API_KEY||"";if(!o||!r)return void this.logger.info?.(`[bridge-core] title-gen SKIP sid=${t}: no AROUTER env vars`);const a=`[User Question]\n${this.truncateQuestionForTitle(s)}\n\n[Assistant Answer]\n${this.truncateAnswerForTitle(n)}`;this.logger.info?.(`[bridge-core] title-gen CALLING LLM sid=${t} q="${s.slice(0,60)}" aLen=${n.length}`),fetch(`${o}/v1/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify({model:"openai/gpt-4o-mini",max_tokens:32,temperature:.3,messages:[{role:"system",content:AcpGatewayBridgeCore.TITLE_SYSTEM_PROMPT},{role:"user",content:a}]})}).then(async s=>{if(!s.ok){const e=await s.text().catch(()=>"");return void this.logger.info?.(`[bridge-core] title-gen FAILED sid=${t} status=${s.status} body=${e.slice(0,300)}`)}const n=await s.json(),i=n.choices?.[0]?.message?.content?.trim();if(!i)return;const o=i.replace(/^```json\s*/,"").replace(/```$/,"").trim();let r;try{r=JSON.parse(o).title?.trim()??""}catch{return void this.logger.debug?.(`[bridge-core] title-gen JSON parse failed: ${o}`)}if(!r)return;if("manual"===e.titleSource)return void this.logger.debug?.("[bridge-core] title-gen discarded: user already renamed");this.logger.info?.(`[bridge-core] title-gen OK sid=${t} title="${r}"`),e.title=r,e.titleSource="auto";const a=this.getGateway();a?.isConnected&&a.sessionsPatch(e.gatewaySessionKey,{label:r}).catch(()=>{}),this.transport.sendSessionUpdate(t,{sessionUpdate:"title_update",title:r})}).catch(e=>{this.logger.info?.(`[bridge-core] title-gen ERROR sid=${t}: ${e instanceof Error?e.message:e}`)})}async tryGenerateTitleFromHistory(e,t,s){if("manual"===e.titleSource||"auto"===e.titleSource)return;const n=this.getGateway();if(!n?.isConnected)return;let i=s;if(!i)try{const t=await loadHistory(n,e.gatewaySessionKey);i=groupIntoTurns(t)}catch(e){return void this.logger.debug?.(`[bridge-core] title-gen loadHistory failed sid=${t}: ${e instanceof Error?e.message:e}`)}const o=i.find(e=>{if("user"!==e.role)return!1;if(!e.content?.trim())return!1;if("internal_system"===e.provenance?.kind)return!1;const t=stripTransportMetadata(e.content).trim();return!!t&&!/^Sender\s*\(untrusted/i.test(t)});if(!o)return;const r=i.indexOf(o),a=i.slice(r+1).find(e=>"assistant"===e.role&&!!e.content?.trim());if(!a)return;const l=stripTransportMetadata(o.content).trim(),d=a.content.trim();l&&d&&this.generateTitleFromQA(e,t,l,d)}async handleSessionDelete(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId"});const s=this.state.sessions.get(t);if(!s)return this.logger.info?.(`[bridge-core] session/delete sessionId=${t} NOT FOUND in local state`),void this.transport.sendAcpError(e.id,Errors.SessionNotFound);const n=s.gatewaySessionKey;this.logger.info?.(`[bridge-core] session/delete sessionId=${t} gatewayKey=${n} gwConnected=${!!this.getGateway()?.isConnected}`);let i=!1;const o=this.getGateway();if(o?.isConnected&&n)try{await o.sessionsDelete(n),i=!0,this.logger.info?.(`[bridge-core] Gateway sessions.delete SUCCESS for key=${n}`)}catch(t){const s=t instanceof Error?t.message:String(t);if(this.logger.info?.(`[bridge-core] Gateway sessions.delete FAILED for key=${n}: ${s}`),!s.includes("Cannot delete the main session"))return void this.transport.sendResult(e.id,{deleted:!1});try{await o.sessionsReset(n),i=!0,this.logger.info?.(`[bridge-core] Gateway sessions.reset SUCCESS for key=${n}`)}catch(t){return this.logger.warn?.(`[bridge-core] Gateway sessions.reset also failed for key=${n}: ${t instanceof Error?t.message:t}`),void this.transport.sendResult(e.id,{deleted:!1})}}else n&&this.logger.warn?.(`[bridge-core] Gateway not connected, proceeding with local-only delete for key=${n}`);void 0!==s.pendingPromptId&&this.failPrompt(s,Errors.SessionDeleted.rpcCode,Errors.SessionDeleted.message),this.state.clearPromptTimeout(s),this.state.clearStreamIdleTimer(s),s.assistantMq?.close(),s.currentRunId&&this.state.promptsByRunId.delete(s.currentRunId);const r=new Set;for(const[e,s]of this.state.toolCallToSessionMap)s===t&&(this.state.toolCallToSessionMap.delete(e),r.add(e));for(const[e,t]of this.state.toolCallIdMap)r.has(t)&&this.state.toolCallIdMap.delete(e);this.state.sessions.delete(t),this.state.sessionsByGatewayKey.delete(n),this.transport.resetSessionUpdateCount(t),i&&this.onSessionDeleted?.(n),this.logger.info?.(`[bridge-core] session/delete DONE sessionId=${t} gatewayKey=${n} deleted=true`),this.transport.sendResult(e.id,{deleted:!0})}async handleSessionClear(e){this.logger.info?.(`[bridge-core] handleSessionClear called, params=${JSON.stringify(e.params)}`);const t=e.params?.sessionId;if(!t)return this.logger.warn?.("[bridge-core] session/clear missing sessionId"),void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId"});const s=this.state.sessions.get(t);if(!s)return this.logger.warn?.(`[bridge-core] session/clear sessionId=${t} not found in state`),void this.transport.sendAcpError(e.id,Errors.SessionNotFound);this.logger.info?.(`[bridge-core] session/clear sessionId=${t} gatewayKey=${s.gatewaySessionKey}`);const n=this.getGateway(),i=n?.isConnected;if(s.promptDone||void 0===s.pendingPromptId&&void 0===s.currentRunId||(this.logger.info?.(`[bridge-core] session/clear: cancelling active run sid=${t} runId=${s.currentRunId}`),i&&(s.currentRunId||s.gatewayRequestId)&&n.agentCancel(s.gatewaySessionKey,{runId:s.currentRunId,requestId:s.gatewayRequestId}),this.endRun(s,{kind:"complete",stopReason:"cancelled"})),i&&s.gatewaySessionKey)try{await n.sessionsReset(s.gatewaySessionKey),this.logger.info?.(`[bridge-core] Gateway sessions.reset SUCCESS for key=${s.gatewaySessionKey}`)}catch(e){this.logger.warn?.(`[bridge-core] Gateway sessions.reset FAILED for key=${s.gatewaySessionKey}: ${e instanceof Error?e.message:e}`)}else this.logger.warn?.(`[bridge-core] session/clear skipped gateway reset: connected=${i}, key=${s.gatewaySessionKey}`);s.chatReplayDedupKeys.clear(),this.logger.info?.("[bridge-core] session/clear dedupKeys cleared, calling onSessionCleared"),this.onSessionCleared?.(t,s.gatewaySessionKey),this.transport.sendResult(e.id,{cleared:!0}),this.logger.info?.("[bridge-core] session/clear DONE, sent cleared=true")}async handleSessionSetMode(e){const t=e.params?.sessionId,s=e.params?.mode,n=t?this.state.sessions.get(t):void 0,i=this.getGateway();if(n&&i?.isConnected&&s){const e={};if("verbose"===s||"detailed"===s?e.verboseLevel="full":"concise"!==s&&"default"!==s||(e.verboseLevel="default"),"reasoning"!==s&&"thinking"!==s||(e.reasoningLevel="stream"),Object.keys(e).length>0)try{await i.sessionsPatch(n.gatewaySessionKey,e)}catch(e){this.logger.warn?.(`[bridge-core] sessions.patch failed: ${e instanceof Error?e.message:e}`)}}this.transport.sendResult(e.id,{mode:s??"default"})}async handleSessionSetModel(e){const t=e.params?.sessionId,s=e.params?.model,n=t?this.state.sessions.get(t):void 0,i=this.getGateway();let o=s??null,r=null;if(n&&i?.isConnected&&s)try{const e=await i.sessionsPatch(n.gatewaySessionKey,{model:s}),t=e?.resolved;t?.model&&(o=t.model),t?.modelProvider&&(r=t.modelProvider)}catch(e){this.logger.warn?.(`[bridge-core] sessions.patch for model failed: ${e instanceof Error?e.message:e}`)}this.transport.sendResult(e.id,{model:o,modelProvider:r})}async handleModelsList(e){const t=this.getGateway();if(t?.isConnected)try{const s=await t.request("models.list",{});this.transport.sendResult(e.id,s??{models:[]})}catch(t){this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Failed to list models: ${t instanceof Error?t.message:t}`})}else this.transport.sendAcpError(e.id,Errors.GatewayNotConnected)}async handleSkillsList(e){const t=await this.fetchSimplifiedSkills();this.transport.sendResult(e.id,{skills:t})}async fetchSimplifiedSkills(){const e=this.getGateway();if(!e?.isConnected)return[];try{const t=await e.skillsStatus();if(!t?.skills)return[];const s=t.skills.filter(e=>!1!==e.eligible||!0===e.disabled).sort((e,t)=>e.always&&!t.always?-1:!e.always&&t.always?1:e.name.localeCompare(t.name)).map(e=>({name:e.name,description:e.description,emoji:e.emoji,always:e.always||void 0,bundled:e.bundled||void 0,disabled:e.disabled||void 0,skillKey:e.skillKey||void 0,iconUrl:void 0})),n=this.getSkillsDir();for(const e of s){const t=e.skillKey??e.name;try{const s=await readFile(join(n,t,".beeos-meta.json"),"utf8"),i=JSON.parse(s);i?.iconUrl&&(e.iconUrl=i.iconUrl)}catch{}}return s}catch(e){return this.logger.warn?.(`[bridge-core] Failed to fetch skills: ${e instanceof Error?e.message:e}`),[]}}async pushSkillsList(){const e=await this.fetchSimplifiedSkills();this.transport.sendNotification("session/update",{sessionId:"_system",update:{sessionUpdate:"available_commands_update",commands:e}}),this.writeObs({component:"bridge-core",domain:"lifecycle",name:"skills.pushed",severity:"debug",payload:{count:e.length}})}getSkillsDir(){const e=process.env.OPENCLAW_HOME||join(homedir(),".openclaw");return join(e,"skills")}async handleSkillsInstall(e){const t=e.params,s=t?.zipUrl,n=t?.files;let i;try{i=validateSkillSlug(t?.slug||t?.name||"")}catch{return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Invalid or missing skill slug"})}if(!n?.length&&!s)return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Missing skill files or zipUrl"});const o=resolveSkillInstallDir(this.getSkillsDir(),i);try{if(await mkdir(o,{recursive:!0}),s){const e=await fetchWithRetry(s,{maxRetries:4,logger:this.logger});await extractZipToDir(e,o)}else if(n?.length){const e=resolve(o);for(const t of n){const s=join(o,t.path);if(!resolve(s).startsWith(e+sep))throw new Error(`File path escapes skill directory: ${t.path}`);await mkdir(join(s,".."),{recursive:!0});const n=await fetch(t.downloadUrl,{signal:AbortSignal.timeout(12e4)});if(!n.ok)throw new Error(`Failed to download ${t.path}: ${n.status}`);const i=Buffer.from(await n.arrayBuffer());await writeFile(s,i)}}if(t?.iconUrl)try{await writeFile(join(o,".beeos-meta.json"),JSON.stringify({iconUrl:t.iconUrl}),"utf8")}catch{}this.logger.info?.(`[bridge-core] Installed skill "${i}" via ${s?"zip":"files"}`),this.transport.sendResult(e.id,{success:!0}),this.onSkillInstalled?.(i,o),await this.pushSkillsList()}catch(t){this.logger.error?.(`[bridge-core] Failed to install skill "${i}": ${t instanceof Error?t.message:t}`),this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Install failed: ${t instanceof Error?t.message:String(t)}`})}}async handleSkillsUninstall(e){const t=e.params;let s;try{s=validateSkillSlug(t?.slug||t?.name||"")}catch{return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Invalid or missing skill name"})}const n=resolveSkillInstallDir(this.getSkillsDir(),s),i=resolveSkillInstallDir(join(homedir(),".agents","skills"),s);try{let t=!1;try{const e=await lstat(n);if(e.isSymbolicLink()){const e=await readlink(n);await rm(n,{force:!0}),await rm(e,{recursive:!0,force:!0}),t=!0}else e.isDirectory()&&(await rm(n,{recursive:!0,force:!0}),t=!0)}catch{}try{const e=await lstat(i);(e.isDirectory()||e.isSymbolicLink())&&(await rm(i,{recursive:!0,force:!0}),t=!0)}catch{}t||this.logger.warn?.(`[bridge-core] Skill "${s}" not found in any known location`),this.logger.info?.(`[bridge-core] Uninstalled skill "${s}"`),this.transport.sendResult(e.id,{success:!0}),await this.pushSkillsList()}catch(t){this.logger.error?.(`[bridge-core] Failed to uninstall skill "${s}": ${t instanceof Error?t.message:t}`),this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Uninstall failed: ${t instanceof Error?t.message:String(t)}`})}}async handleSkillsUpdate(e){const t=e.params,s=t?.skillKey,n=t?.enabled;if(!s||"boolean"!=typeof n)return void this.transport.sendAcpError(e.id,Errors.InvalidParams,{message:"Missing skillKey or enabled"});const i=this.getGateway();if(i?.isConnected)try{const t=await i.skillsUpdate(s,n);this.logger.info?.(`[bridge-core] Updated skill "${s}" enabled=${n}`),this.transport.sendResult(e.id,{ok:t?.ok??!0,skillKey:s,enabled:n}),await this.pushSkillsList()}catch(t){this.logger.error?.(`[bridge-core] Failed to update skill "${s}": ${t instanceof Error?t.message:t}`),this.transport.sendAcpError(e.id,Errors.InternalError,{message:`Update failed: ${t instanceof Error?t.message:String(t)}`})}else this.transport.sendAcpError(e.id,Errors.InternalError,{message:"Gateway not connected"})}async handleSessionPrompt(e){const t=e.params?.sessionId,s=e.params?.prompt;if(!t||!s)return void this.transport.sendAcpError(e.id,Errors.MissingParam,{param:"sessionId, prompt"});const{session:n,created:i}=await this.resolveOrCreateSession(t);i&&this.onSessionLoaded?.(n.sessionId,n.gatewaySessionKey,n.platformAgentId);const o=n.sessionId;this.state.lastActiveSessionId=o,this.onPromptStart?.();const r=this.getGateway();if(!r?.isConnected)return void this.transport.sendAcpError(e.id,Errors.GatewayNotConnected);await this.ensureVerboseFull(n);let a=extractPromptBlocks(sanitizePromptPayload(s));if(!n.title||isPlaceholderTitle(n.title)){const e=a.find(e=>"text"===e.type&&e.text);if(e&&"text"in e&&e.text){const t=stripTransportMetadata(e.text).trim();if(this.logger.info?.(`[bridge-core] title-gen sid=${o} existing="${n.title}" cleanText="${t.slice(0,80)}" isPlaceholder=${isPlaceholderTitle(t)}`),t&&!isPlaceholderTitle(t)){const e=t.slice(0,64).replace(/\n/g," ").trim();n.title=e,n.titleSource="truncated",r?.isConnected&&e&&r.sessionsPatch(n.gatewaySessionKey,{label:e}).catch(e=>{this.logger.debug?.(`[bridge-core] sessionsPatch (title) failed: ${e instanceof Error?e.message:e}`)}),this.transport.sendSessionUpdate(o,{sessionUpdate:"title_update",title:e})}}}const l=buildResolutionPlan(a);if(l.refs.length>0)try{const e=await resolveResources(l,this.writeObs);a=applyResolutions(a,e)}catch(e){this.logger.warn?.(`[bridge-core] Resource pre-resolution failed: ${e instanceof Error?e.message:e}`)}let d;if(this.beeosFileResolver){const e=this.beeosFileResolver.buildResolutionPlan(a);if(e.refs.length>0)try{d=await this.beeosFileResolver.resolveMetadataForPrompt(e)}catch(e){this.logger.warn?.(`[bridge-core] BeeOS file resolution failed: ${e instanceof Error?e.message:e}`)}}const c=toGatewayPrompt(a,{beeosFileResolutionsByUri:d});if("error"in c)return void this.transport.sendAcpError(e.id,Errors.PromptConvertFailed,{message:c.error});const{message:h,attachments:p}=c,m=resolveStandaloneSlashCommandName(a,h)??resolveSlashCmd(a,h),g="string"==typeof m;if(m&&this.handleSlashCommand){const t=this;if(await(async()=>{try{return await t.handleSlashCommand(m)}catch(e){return t.logger.warn?.(`[bridge-core] /${m} handler failed: ${e instanceof Error?e.message:e}`),!1}})())return this.emitPromptAsUserSessionUpdates(n.sessionId,a),this.transport.sendSessionUpdate(o,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`Loading ${m}...`},timestamp:(new Date).toISOString()}),void this.transport.sendResult(e.id,{stopReason:"end_turn"})}const u="chat.send";let f=g?h:withUserMessagePrefix(h);const y=this.config.canvasEnabled&&!g&&!1!==n.canvasActive&&!n.channelType;if(y){const e=n.promptsSinceCanvasInjection??0;!n.canvasPromptInjected||e>=10?(f=`${CANVAS_TERM_MAPPING}\n\n${f}`,n.canvasPromptInjected=!0,n.promptsSinceCanvasInjection=0,this.logger.debug?.("[bridge-core] Canvas term mapping injected (re-inject cycle)")):n.promptsSinceCanvasInjection=e+1}if(y){const e=this.getCanvasState?.();if(e){const t=e.resolveCanvasForSession(n.gatewaySessionKey);f=`${buildActiveSurfacesContext(e,t)??"[Canvas] Canvas panel is available. Use render_ui for structured/visual content."}\n\n${f}`}const t=n.referencedSurfaceId;t&&(f=`${CANVAS_UPDATE_HINT}\n[IMPORTANT: The user selected canvas surface "${t}" for editing. You MUST call render_ui with id="${t}" to UPDATE this existing surface. Do NOT create a new surface with a different id.]\n${f}`,n.referencedSurfaceId=void 0)}if(!g){const t=parseMentionsParam(e.params?.mentions);if(t.length>0){n.activeMentions=t;f=`${buildMentionInstruction(t)}\n${f}`,this.logger.debug?.(`[bridge-core] A2A mention instruction injected for ${t.length} agent(s)`)}else n.activeMentions=void 0}g&&this.logger.info?.(`[bridge-core] slash command passthrough sessionId=${o} command=/${m}`),n.promptDone||void 0===n.pendingPromptId&&void 0===n.currentRunId||(this.logger.warn?.(`[bridge-core] handleSessionPrompt: force-ending stale run sid=${o} oldRunId=${n.currentRunId} oldPromptId=${n.pendingPromptId}`),(n.currentRunId||n.gatewayRequestId)&&r.agentCancel(n.gatewaySessionKey,{runId:n.currentRunId,requestId:n.gatewayRequestId}),this.endRun(n,{kind:"complete",stopReason:"cancelled"})),this.state.resetForNewRun(n,o,this.transport),n.pendingPromptId=e.id,n.gatewayMethod=u,this.emitPromptAsUserSessionUpdates(o,a),this.transport.sendSessionUpdate(o,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:""},timestamp:(new Date).toISOString()}),this.schedulePromptTimeout(n);const S=p.length>0?p.map(e=>({type:e.type,content:e.content,...e.mimeType?{mimeType:e.mimeType}:{},...e.fileName?{fileName:e.fileName}:{}})):void 0,w=`req_${randomUUID().replace(/-/g,"")}`,I={sessionKey:n.gatewaySessionKey,message:f,deliver:!1,idempotencyKey:randomUUID()};S?.length&&(I.attachments=S);if(!r.sendFrame({type:"req",id:w,method:u,params:I}))return this.state.clearPromptTimeout(n),n.pendingPromptId=void 0,n.assistantMq?.close(),void this.transport.sendAcpError(e.id,Errors.SendFailed,{message:"failed to send prompt to gateway"});n.gatewayRequestId=w,this.state.promptsByGatewayRequestId.set(w,n)}replayUserTurnAsChunks(e,t,s,n){const i=splitUserReplayBlocks(t);for(const t of i){let i;if("text"===t.type){if(!t.text)continue;i={type:"text",text:t.text}}else i="resource_link"===t.type?{type:"resource_link",uri:t.uri??"",...t.fileName?{name:t.fileName}:{},...t.mimeType?{mimeType:t.mimeType}:{}}:{type:t.type,...t.mimeType?{mimeType:t.mimeType}:{},...t.fileName?{fileName:t.fileName}:{}};this.transport.sendSessionUpdate(e,{sessionUpdate:"user_message_chunk",content:i,...s?{metadata:s}:{},...n?{timestamp:n}:{}})}}async replayAssistantTurnAsStream(e,t){if(!t.sourceMessages?.length){const s={sessionUpdate:"history_turn",role:t.role,content:t.content,reasoning:t.reasoning,toolCalls:t.toolCalls,...t.timestamp?{timestamp:t.timestamp}:{}};return t.metadata&&(s.metadata=t.metadata),void this.transport.sendSessionUpdate(e,s)}for(const s of t.sourceMessages){switch(s.updateType??"agent_message_chunk"){case"agent_message_chunk":this.transport.sendSessionUpdate(e,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:s.content},...s.timestamp?{timestamp:s.timestamp}:{}});break;case"agent_thought_chunk":this.transport.sendSessionUpdate(e,{sessionUpdate:"agent_thought_chunk",content:{type:"text",text:s.content},...s.timestamp?{timestamp:s.timestamp}:{}});break;case"tool_call":{const n=s.toolCallId??"",i=t.toolCalls?.find(e=>e.id===n),o=s.toolName??"tool",r=s.content??"";let a={};if(r)try{a=JSON.parse(r)}catch{}this.transport.sendSessionUpdate(e,buildToolCallPayload({toolCallId:n,toolName:o,args:a,kind:i?.kind,status:i?.status??"in_progress",timestamp:s.timestamp}));break}case"tool_call_update":{const n=s.toolCallId??"",i=t.toolCalls?.find(e=>e.id===n),o=s.toolName??i?.name??"tool",r=s.content??i?.output??"";let a=await readEnrichmentCache(n).catch(()=>{});if(!a){const e=extractBeeosFileUrisFromOutput(r);e.length>0&&(a=e.map(e=>({uri:e,name:"file"})))}if(!a&&"beeos_upload_file"===o){const e=await readUploadToolResult(n).catch(()=>{});if(e){const t=buildToolResultSessionContentBlocks(o,e).filter(e=>"resource_link"===e.content?.type&&e.content.uri).map(e=>({uri:e.content.uri,name:e.content.name??"file",...e.content.mimeType?{mimeType:e.content.mimeType}:{}}));t.length>0&&(a=t)}}this.transport.sendSessionUpdate(e,buildToolCallUpdatePayload({toolCallId:n,toolName:o,outputText:r,kind:i?.kind,status:i?.status??"completed",resourceLinks:a,timestamp:s.timestamp}));break}}}}emitPromptAsUserSessionUpdates(e,t){const s=(new Date).toISOString();for(const n of t){const t=this.toUserSessionUpdateContent(n);t&&this.transport.sendSessionUpdate(e,{sessionUpdate:"user_message_chunk",content:t,timestamp:s})}}toUserSessionUpdateContent(e){if("text"===e.type){if(!e.text)return;return{type:"text",text:e.text}}if("image"===e.type){const t=e.mimeType??e.mime_type??"application/octet-stream",s=e.fileName??e.file_name??e.name;if(!e.data&&!e.uri)return;return{type:"image",...e.data?{data:e.data}:{},...e.uri?{uri:e.uri}:{},...t?{mimeType:t}:{},...s?{fileName:s}:{}}}if("resource_link"===e.type){if(!e.uri)return;return{type:"resource_link",uri:e.uri,...e.title?{title:e.title}:{},...e.name?{name:e.name}:{},...e.mimeType??e.mime_type?{mimeType:e.mimeType??e.mime_type}:{}}}if("file"===e.type){const t=e.mimeType??e.mime_type??"application/octet-stream",s=e.fileName??e.file_name??e.filename??e.name;if(!e.data&&!e.text&&!e.uri)return;return{type:"file",...e.data?{data:e.data}:{},...e.text?{text:e.text}:{},...e.uri?{uri:e.uri}:{},...t?{mimeType:t}:{},...s?{fileName:s}:{}}}if("resource"===e.type){const t=e.resource??{},s=t.uri??e.uri,n=t.mimeType??t.mime_type??e.mimeType??e.mime_type,i=t.text??e.text,o=t.data??e.data,r=t.fileName??t.file_name??t.filename??t.name??e.fileName??e.file_name??e.filename??e.name;if(!s&&!i&&!o)return;return{type:"resource",resource:{...s?{uri:s}:{},...n?{mimeType:n}:{},...i?{text:i}:{},...o?{data:o}:{},...r?{fileName:r}:{}}}}}handleSessionCancel(e,t){const s=t?.sessionId;if(!s)return void(void 0!==e&&this.transport.sendAcpError(e,Errors.MissingParam,{param:"sessionId"}));const n=this.state.sessions.get(s);if(!n)return void(void 0!==e&&this.transport.sendAcpError(e,Errors.SessionNotFound));const i=this.getGateway(),o=i?.isConnected,r=!(!n.currentRunId&&!n.gatewayRequestId);if(o&&r){i.agentCancel(n.gatewaySessionKey,{runId:n.currentRunId,requestId:n.gatewayRequestId})}this.endRun(n,{kind:"complete",stopReason:"cancelled"})||this.transport.sendSessionUpdate(n.sessionId,{sessionUpdate:"stream_complete",stopReason:"cancelled"}),void 0!==e&&this.transport.sendResult(e,{})}endRun(e,t){if(e.promptDone)return this.logger.debug?.(`[bridge-core] endRun skipped (promptDone=true) sessionId=${e.sessionId} outcome=${t.kind}`),!1;if(void 0===e.pendingPromptId&&void 0===e.currentRunId)return this.logger.debug?.(`[bridge-core] endRun skipped (no pendingPromptId and no currentRunId) sessionId=${e.sessionId} outcome=${t.kind}`),!1;e.promptDone=!0;const s=e.currentRunId;if("complete"===t.kind){if(s&&this.onRunComplete){const t=e.agentAssistantTextSoFar??e.chatAssistantTextSoFar??"";this.onRunComplete(s,t)}void 0!==e.pendingPromptId&&this.transport.sendResult(e.pendingPromptId,{stopReason:t.stopReason}),this.transport.sendSessionUpdate(e.sessionId,{sessionUpdate:"stream_complete",stopReason:t.stopReason}),this.tryGenerateTitleFromHistory(e,e.sessionId)}else s&&this.onRunFailed&&this.onRunFailed(s,t.message),void 0!==e.pendingPromptId&&this.transport.sendError(e.pendingPromptId,t.code,t.message),this.transport.sendSessionUpdate(e.sessionId,{sessionUpdate:"stream_complete",stopReason:"error",error:t.message});return this.cleanupPrompt(e),this.onRunEnded?.(e,t),!0}schedulePromptTimeout(e){this.state.clearPromptTimeout(e);const t=this.config.promptTimeoutMs??DEFAULT_PROMPT_TIMEOUT_MS;t<=0||(e.promptTimeoutTimer=setTimeout(()=>{void 0!==e.pendingPromptId&&(this.logger.warn?.(`[bridge-core] Prompt timed out sessionId=${e.sessionId} after ${t}ms`),this.failPrompt(e,Errors.PromptTimeout.rpcCode,`Prompt timed out after ${t}ms`))},t),"object"==typeof e.promptTimeoutTimer&&"unref"in e.promptTimeoutTimer&&e.promptTimeoutTimer.unref())}cleanupPrompt(e){this.state.clearPromptTimeout(e),this.state.clearStreamIdleTimer(e);const t=e.currentRunId;if(t){this.state.promptsByRunId.delete(t),this.state.markRunCompleted(t);const e=`tc_${t}_`;for(const[t,s]of this.state.toolCallIdMap)s.startsWith(e)&&this.state.toolCallIdMap.delete(t)}e.gatewayRequestId&&this.state.promptsByGatewayRequestId.delete(e.gatewayRequestId),e.pendingPromptId=void 0,e.promptDone=void 0,e.currentRunId=void 0,e.gatewayRequestId=void 0,e.assistantStreamStartedAt=void 0,e.gatewayMethod=void 0,e.assistantMq?.close()}completePrompt(e,t){this.endRun(e,{kind:"complete",stopReason:t})}failPrompt(e,t,s){this.endRun(e,{kind:"error",code:t,message:s})}cleanupAllPendingPrompts(){for(const e of this.state.sessions.values())this.failPrompt(e,Errors.GatewayDisconnected.rpcCode,Errors.GatewayDisconnected.message)}handleGatewayResponse(e){const t=e.id;if(!t)return;const s=this.state.promptsByGatewayRequestId.get(t);if(!s||s.promptDone)return;if(!e.ok){const n=e.error,i=n?.message??"gateway returned error";return this.logger.warn?.(`[bridge-core] handleGatewayResponse: gateway rejected prompt gatewayRequestId=${t} error=${i}`),void this.failPrompt(s,Errors.SendFailed.rpcCode,i)}const n=e.payload,i=n?.runId;i?(s.currentRunId=i,s.assistantStreamStartedAt=Date.now(),this.state.promptsByRunId.set(i,s),this.logger.debug?.(`[bridge-core] handleGatewayResponse: bound runId=${i} gatewayRequestId=${t} sessionId=${s.sessionId}`)):this.logger.debug?.(`[bridge-core] handleGatewayResponse: no runId in payload gatewayRequestId=${t} sessionId=${s.sessionId}`)}maybeCompletePromptFromChatState(e,t){if(e.promptDone||"chat.send"!==e.gatewayMethod)return;const s="string"==typeof t.state?t.state.toLowerCase():void 0;if(s)if("final"===s)this.completePrompt(e,"end_turn");else if("aborted"===s||"cancelled"===s||"cancel"===s)this.completePrompt(e,"cancelled");else if("error"===s){const s=("string"==typeof t.summary?t.summary:void 0)??("string"==typeof t.message?t.message:void 0)??"gateway chat error";this.failPrompt(e,Errors.AgentError.rpcCode,formatFriendlyError(s))}}pushToolResultContent(e,t,s,n){if(0===s.length)return;const i=n?this.state.promptsByRunId.get(n):void 0;if(!i)return void this.logger.warn?.(`[bridge-core] pushToolResultContent dropped — no session for runId=${n??"(none)"}`);const o=i.sessionId,r=(new Date).toISOString();for(const e of s){const t={sessionUpdate:"agent_message_chunk",content:e,timestamp:r};this.transport.sendSessionUpdate(o,t),i.assistantMq?.push(t)}this.writeObs({component:"bridge-core",domain:"tool",name:"tool_result.direct_push",severity:"info",payload:{toolCallId:e,toolName:t,blockCount:s.length,sessionId:o}})}async handleCronRequest(e){const t=this.getGateway();if(!t?.isConnected)return void this.transport.sendAcpError(e.id,Errors.GatewayNotConnected);const s=e.method.replace("/",".");try{const n=await t.request(s,e.params??{});this.transport.sendResult(e.id,n??{})}catch(t){const n=t instanceof Error?t.message:"cron request failed";this.logger.warn?.(`[bridge-core] ${s} failed: ${n}`),this.transport.sendAcpError(e.id,Errors.SendFailed,{message:n})}}}async function fetchWithRetry(e,t={}){const s=t.maxRetries??3;let n;for(let i=0;i<=s;i++){const o=await fetch(e,{signal:AbortSignal.timeout(12e4),redirect:"follow"});if(o.ok)return Buffer.from(await o.arrayBuffer());if(429===o.status||o.status>=500){const r=Number(o.headers.get("retry-after"))||0,a=1e3*Math.max(r,2**i);if(n=new Error(`HTTP ${o.status} from ${e}`),i<s){t.logger?.info?.(`[bridge-core] Download ${o.status}, retry ${i+1}/${s} in ${(a/1e3).toFixed(0)}s`),await new Promise(e=>setTimeout(e,a));continue}}throw new Error(`Failed to download zip: ${o.status}`)}throw n??new Error("Download failed after retries")}const SKILL_ROOT_MARKERS=["SKILL.md","skill.md","SKILL.MD"];async function resolveSkillRoot(e){for(const t of SKILL_ROOT_MARKERS)try{return await stat(join(e,t)),e}catch{}const t=(await readdir(e,{withFileTypes:!0})).filter(e=>e.isDirectory()&&!e.name.startsWith(".")&&"__MACOSX"!==e.name);if(1===t.length){const s=join(e,t[0].name);for(const e of SKILL_ROOT_MARKERS)try{return await stat(join(s,e)),s}catch{}return s}return e}async function extractZipToDir(e,t){const s=new AdmZip(e),n=join(t,".beeos-extract-tmp-"+Date.now());try{await mkdir(n,{recursive:!0}),s.extractAllTo(n,!0);const e=await resolveSkillRoot(n),i=await readdir(e,{withFileTypes:!0});let o=0;for(const s of i){if(s.name.startsWith(".beeos-extract-tmp")||"__MACOSX"===s.name)continue;const n=join(e,s.name),i=join(t,s.name);await rename(n,i).catch(async()=>{await cp(n,i,{recursive:!0})}),o++}if(0===o)throw new Error("Zip archive contained no extractable files")}finally{await rm(n,{recursive:!0,force:!0}).catch(()=>{})}}const VALID_SLUG_PATTERN=/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,NON_ASCII_PATTERN=/[^\x00-\x7F]/;function validateSkillSlug(e){const t=e.trim();if(!t||t.includes("/")||t.includes("\\")||t.includes(".."))throw new Error(`Invalid skill slug: ${e}`);if(NON_ASCII_PATTERN.test(t)||!VALID_SLUG_PATTERN.test(t))throw new Error(`Invalid skill slug: ${e}`);return t}function safeDirName(e){const t=e.trim();return t?t.replaceAll("/","__").replaceAll("\\","__"):t}function resolveSkillInstallDir(e,t){const s=safeDirName(t),n=join(e,s),i=resolve(e),o=resolve(n),r=relative(i,o);if(!r||".."===r||r.startsWith(`..${sep}`)||path.isAbsolute(r))throw new Error("Invalid skill target path");return n}
@@ -1 +1 @@
1
- import{createHash}from"node:crypto";import{Errors}from"../errors/index.js";import{formatFriendlyError}from"../errors/classify.js";import{stripTransportMetadata,normalizeTransportText}from"../message-filter.js";import{resolveToolResultBlocks}from"./tool-result-payload-strategies.js";import{buildToolCallPayload,buildToolCallUpdatePayload}from"./tool-call-payload-builder.js";import{DEFAULT_STREAM_IDLE_TIMEOUT_MS,DEFAULT_STREAM_STALE_TIMEOUT_MS}from"./types.js";function asString(t){if("string"==typeof t){return t.trim()||void 0}}function isRecord(t){return"object"==typeof t&&null!==t&&!Array.isArray(t)}function normalizeText(t){if("string"==typeof t)return normalizeTransportText(t.replace(/\r\n/g,"\n").replace(/\r/g,"\n"))}function extractResourceLinksFromRawResult(t){if(!isRecord(t)||!Array.isArray(t.files))return;const e=[];for(const s of t.files){if(!isRecord(s))continue;const t=asString(s.uri)??(asString(s.fileId)?`beeos-file://${s.fileId}`:void 0);t&&e.push({uri:t,name:asString(s.name)??"file",...asString(s.mimeType)?{mimeType:asString(s.mimeType)}:{}})}return e.length>0?e:void 0}const DEDUP_HASH_THRESHOLD=256;function dedupKey(t,...e){const s=`${t}:${e.join(":")}`;if(s.length<=256)return s;return`${t}:h:${createHash("sha256").update(s).digest("hex").slice(0,24)}`}function resolveToolArgs(t){const e=t.arguments??t.args??t.input;if(isRecord(e))return e;if("string"==typeof e){const t=e.trim();if(t.startsWith("{")||t.startsWith("["))try{return JSON.parse(t)}catch{}}return{}}export class AcpGatewayEvents{state;transport;config;logger;writeObs;completePrompt;failPrompt;maybeCompletePromptFromChatState;agentFetchFn;onStreamDelta;constructor(t){this.state=t.state,this.transport=t.transport,this.config=t.config,this.logger=t.logger,this.writeObs=t.writeObs,this.completePrompt=t.completePrompt,this.failPrompt=t.failPrompt,this.maybeCompletePromptFromChatState=t.maybeCompletePromptFromChatState,this.agentFetchFn=t.agentFetch,this.onStreamDelta=t.onStreamDelta}touchStreamActivity(t){t.lastStreamActivityTs=Date.now(),this.resetStreamIdleTimer(t)}resetStreamIdleTimer(t){this.state.clearStreamIdleTimer(t);const e=this.config.streamIdleTimeoutMs??DEFAULT_STREAM_IDLE_TIMEOUT_MS,s=this.config.streamStaleTimeoutMs??DEFAULT_STREAM_STALE_TIMEOUT_MS;t.streamIdleTimer=setTimeout(()=>{if(!t.lastStreamActivityTs)return;const e=Date.now()-t.lastStreamActivityTs;e>s&&(this.logger.warn?.(`[gateway-events] Stream stale timeout sessionId=${t.sessionId} stale=${e}ms`),this.completePrompt(t,"end_turn"))},e),"object"==typeof t.streamIdleTimer&&"unref"in t.streamIdleTimer&&t.streamIdleTimer.unref()}sendToolCallSpacer(t){const e={sessionUpdate:"agent_message_chunk",content:{type:"text",text:"\n"}};this.transport.sendSessionUpdate(t.sessionId,e),t.assistantMq?.push(e),this.onStreamDelta?.(t,e)}resolveToolCallId(t,e,s){const o=asString(e.toolCallId)??asString(e.tool_call_id)??asString(e.callId)??asString(e.id);if(o){const e=this.state.toolCallIdMap.get(o);return e?(t.sessionId&&this.state.toolCallToSessionMap.set(e,t.sessionId),e):(this.state.toolCallIdMap.set(o,o),t.sessionId&&this.state.toolCallToSessionMap.set(o,t.sessionId),o)}const a=`tc_${t.currentRunId??"unknown"}_${this.state.nextToolCallSeq++}`;return t.sessionId&&this.state.toolCallToSessionMap.set(a,t.sessionId),a}handleChatEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??this.state.findSessionByKey(t.sessionKey);if(!e)return;if(!this.state.isCurrentStreamOwner(e,t.runId)){const s=!e.currentRunId&&void 0===e.pendingPromptId;if(!t.runId||!s||this.state.isRunCompleted(t.runId))return;this.adoptBackgroundRun(e,t.runId)}const s=e.sessionId,o=t.message;if(o){this.touchStreamActivity(e);if("assistant"===o.role){if(!Array.isArray(o.content))return;for(const a of o.content){const o=a;if(!isRecord(o))continue;const n=asString(o.type);if(n){if("thinking"===n){if(e.hasAgentThinkingStream)continue;const t=normalizeText(o.thinking)??normalizeText(o.text);if(!t)continue;const a=dedupKey("chat:thinking",t);if(e.chatReplayDedupKeys.has(a))continue;e.chatReplayDedupKeys.add(a);const n={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:t}};this.transport.sendSessionUpdate(s,n)&&(e.assistantMq?.push(n),this.onStreamDelta?.(e,n));continue}if("toolCall"===n){if(this.sendToolCallSpacer(e),e.hasAgentToolStartStream)continue;const t=this.resolveToolCallId(e,o,"start"),a=asString(o.name)??asString(o.toolName)??asString(o.tool_name)??"tool",n=resolveToolArgs(o),r=dedupKey("chat:tool_start",t,JSON.stringify(n));if(e.chatReplayDedupKeys.has(r))continue;e.chatReplayDedupKeys.add(r);const i=buildToolCallPayload({toolCallId:t,toolName:a,args:n});this.transport.sendSessionUpdate(s,i)&&(e.assistantMq?.push(i),this.onStreamDelta?.(e,i));continue}if("toolResult"===n){if(this.sendToolCallSpacer(e),e.hasAgentToolResultStream)continue;const t=this.resolveToolCallId(e,o,"result"),a=asString(o.name)??asString(o.toolName)??asString(o.tool_name)??"tool",n=o.result,r=normalizeText(o.text)??normalizeText(o.result)??(isRecord(n)||Array.isArray(n)?JSON.stringify(n,null,2):void 0);if(!r)continue;const i=dedupKey("chat:tool_result",t,r);if(e.chatReplayDedupKeys.has(i))continue;e.chatReplayDedupKeys.add(i);const l=buildToolCallUpdatePayload({toolCallId:t,toolName:a,outputText:r,rawOutput:n,resourceLinks:extractResourceLinksFromRawResult(n)});this.transport.sendSessionUpdate(s,l)&&(e.assistantMq?.push(l),this.onStreamDelta?.(e,l));continue}if("text"===n&&o.text){if(e.hasAgentAssistantStream||e.agentAssistantTextSoFar)continue;const a=stripTransportMetadata(o.text);if(!a)continue;const n=dedupKey("chat:text",a);if(e.chatReplayDedupKeys.has(n))continue;e.chatReplayDedupKeys.add(n);const r=e.chatAssistantTextSoFar??"";let i;if(e.chatAssistantTextSoFar=a,a.startsWith(r))i=a.slice(r.length);else{if(r.startsWith(a))continue;i=a}if(!i)continue;this.logger.debug?.(`[gateway-events] chat.text delta seq=${t.seq} runId=${t.runId} len=${i.length} prevLen=${r.length}`);const l={sessionUpdate:"agent_message_chunk",content:{type:"text",text:i}};this.transport.sendSessionUpdate(s,l),e.assistantMq?.push(l),this.onStreamDelta?.(e,l)}}}}}else"delta"===t.state&&this.touchStreamActivity(e);if(this.maybeCompletePromptFromChatState(e,t),!e.promptDone)if("final"===t.state)this.completePrompt(e,t.stopReason||"end_turn");else if("aborted"===t.state||"cancelled"===t.state||"cancel"===t.state)this.completePrompt(e,"cancelled");else if("error"===t.state){const s=t.errorMessage||Errors.AgentError.message;this.failPrompt(e,Errors.AgentError.rpcCode,formatFriendlyError(s))}}handleAgentEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??(t.sessionKey?this.state.findSessionByKey(t.sessionKey):void 0);if(!e)return;if(!this.state.isCurrentStreamOwner(e,t.runId)){const s=!e.currentRunId&&void 0===e.pendingPromptId;if(!t.runId||!s||this.state.isRunCompleted(t.runId))return;this.adoptBackgroundRun(e,t.runId)}const s=e.sessionId;this.touchStreamActivity(e);const o=t=>{this.transport.sendSessionUpdate(s,t),e.assistantMq?.push(t),this.onStreamDelta?.(e,t)};if("assistant"===t.stream){e.hasAgentAssistantStream=!0;const a=this.state.extractAgentAssistantDelta(e,t.data??{});if(a){const e=normalizeTransportText(a);e&&(this.logger.debug?.(`[gateway-events] agent.assistant delta seq=${t.seq} runId=${t.runId} len=${e.length} source=${t.data?.delta?"delta":"cumulative"}`),o({sessionUpdate:"agent_message_chunk",content:{type:"text",text:e}}))}const n=[],r=t.data;r&&(Array.isArray(r.content)?n.push(...r.content):isRecord(r.content)?n.push(r.content):"string"==typeof r.type&&n.push(r));for(const t of n){if(!isRecord(t))continue;const a=asString(t.type);if("thinking"===a){e.hasAgentThinkingStream=!0;const o=normalizeText(t.thinking)??normalizeText(t.text);if(o){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:o}};this.transport.sendSessionUpdate(s,t)&&(e.assistantMq?.push(t),this.onStreamDelta?.(e,t))}continue}if("toolCall"===a){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const s=this.resolveToolCallId(e,t,"start"),a=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",n=resolveToolArgs(t);o(buildToolCallPayload({toolCallId:s,toolName:a,args:n}));continue}if("toolResult"===a){this.sendToolCallSpacer(e),e.hasAgentToolResultStream=!0;const s=this.resolveToolCallId(e,t,"result"),a=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",n=t.result,r=normalizeText(t.text)??normalizeText(t.result)??(isRecord(n)||Array.isArray(n)?JSON.stringify(n,null,2):void 0);r&&o(buildToolCallUpdatePayload({toolCallId:s,toolName:a,outputText:r,rawOutput:n,resourceLinks:extractResourceLinksFromRawResult(n)}));continue}}}else if("thinking"===t.stream){e.hasAgentThinkingStream=!0;const o=normalizeText(t.data?.delta)??normalizeText(t.data?.text);if(o){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:o}};this.transport.sendSessionUpdate(s,t)&&(e.assistantMq?.push(t),this.onStreamDelta?.(e,t))}}else if("tool"===t.stream){const s=t.data,a="string"==typeof s?.phase?s.phase:void 0;if("start"===a){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const t=this.resolveToolCallId(e,s,"start"),a=asString(s?.name)??asString(s?.toolName)??asString(s?.tool_name)??"tool",n=resolveToolArgs(s);o(buildToolCallPayload({toolCallId:t,toolName:a,args:n}))}else if("result"===a)this.sendToolCallSpacer(e),e.hasAgentToolResultStream=!0,this.handleToolResult(e,t).catch(t=>{this.logger.warn?.(`[gateway-events] Tool result handling failed: ${t instanceof Error?t.message:t}`)});else{const t=s?.toolCall;if(t){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const a=asString(t?.name)??asString(s?.name)??asString(s?.toolName)??"tool",n={...s,...t},r=this.resolveToolCallId(e,n,"start"),i=resolveToolArgs(n);o(buildToolCallPayload({toolCallId:r,toolName:a,args:i}))}}}else if("lifecycle"===t.stream){const s=t.data,o="string"==typeof s?.phase?s.phase:void 0;if("end"===o)this.completePrompt(e,"end_turn");else if("cancelled"===o||"cancel"===o)this.completePrompt(e,"cancelled");else if("error"===o){const o=s?.error,a=o&&"object"==typeof o?o:void 0,n=("string"==typeof s?.message?s.message:void 0)??("string"==typeof o?o:void 0)??("string"==typeof a?.message?a.message:void 0)??("string"==typeof s?.reason?s.reason:void 0)??(a?JSON.stringify(a):void 0)??"gateway lifecycle error",r=formatFriendlyError(n);this.logger.warn?.(`[gateway-events] lifecycle error runId=${t.runId} sessionKey=${t.sessionKey} message=${n} data=${JSON.stringify(s)}`),this.failPrompt(e,Errors.AgentError.rpcCode,r)}}}async handleToolResult(t,e){const s=e.data,o=s?.toolCall,a=("string"==typeof o?.id?o.id:void 0)??("string"==typeof s?.toolCallId?s.toolCallId:void 0),n=(a?this.state.toolCallIdMap.get(a):void 0)??a??`tc_${e.runId}_${e.seq}`,r=("string"==typeof o?.name?o.name:void 0)??("string"==typeof s?.name?s.name:void 0)??"tool",i="string"==typeof s?.text?s.text:void 0,l=a??n,d=s?.result;let c;try{c=await resolveToolResultBlocks({toolCallId:l,toolName:r,rawText:i,rawResult:d})}catch{c=i?[{type:"content",content:{type:"text",text:i}}]:[]}0===c.length&&i&&(c=[{type:"content",content:{type:"text",text:i}}]);const p=[],u=[];for(const t of c)t.content&&("resource_link"===t.content.type&&t.content.uri?u.push({uri:t.content.uri,name:t.content.name??"file",...t.content.mimeType?{mimeType:t.content.mimeType}:{}}):t.content.text&&p.push(t.content.text));const m=buildToolCallUpdatePayload({toolCallId:n,toolName:r,outputText:p.join("\n"),rawOutput:d,resourceLinks:u});this.transport.sendSessionUpdate(t.sessionId,m)&&(t.assistantMq?.push(m),this.onStreamDelta?.(t,m))}adoptBackgroundRun(t,e){this.logger.debug?.(`[gateway-events] Adopting background run ${e} on session ${t.sessionId} (prev=${t.currentRunId})`),t.currentRunId=e,t.promptDone=!1,this.state.promptsByRunId.set(e,t),t.assistantStreamStartedAt=Date.now(),t.lastStreamActivityTs=Date.now(),t.hasAgentAssistantStream=void 0,t.hasAgentThinkingStream=void 0,t.hasAgentToolStartStream=void 0,t.hasAgentToolResultStream=void 0,t.chatAssistantTextSoFar=void 0,t.agentAssistantTextSoFar=void 0,t.chatReplayDedupKeys?.clear()}handleCronEvent(t){if(this.transport.sendNotification("_beeos/cron_event",{payload:t}),this.config.agentGatewayUrl&&this.agentFetchFn){const e=`${this.config.agentGatewayUrl}/api/v1/agent/events`;this.agentFetchFn(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"cron",data:t})}).catch(t=>{this.logger.warn?.(`[gateway-events] event report failed: ${t instanceof Error?t.message:t}`)})}}}
1
+ import{createHash}from"node:crypto";import{Errors}from"../errors/index.js";import{formatFriendlyError}from"../errors/classify.js";import{stripTransportMetadata,normalizeTransportText}from"../message-filter.js";import{resolveToolResultBlocks}from"./tool-result-payload-strategies.js";import{buildToolCallPayload,buildToolCallUpdatePayload}from"./tool-call-payload-builder.js";import{DEFAULT_STREAM_IDLE_TIMEOUT_MS,DEFAULT_STREAM_STALE_TIMEOUT_MS}from"./types.js";function asString(t){if("string"==typeof t){return t.trim()||void 0}}function isRecord(t){return"object"==typeof t&&null!==t&&!Array.isArray(t)}function normalizeText(t){if("string"==typeof t)return normalizeTransportText(t.replace(/\r\n/g,"\n").replace(/\r/g,"\n"))}function extractResourceLinksFromRawResult(t){if(!isRecord(t)||!Array.isArray(t.files))return;const e=[];for(const s of t.files){if(!isRecord(s))continue;const t=asString(s.uri)??(asString(s.fileId)?`beeos-file://${s.fileId}`:void 0);t&&e.push({uri:t,name:asString(s.name)??"file",...asString(s.mimeType)?{mimeType:asString(s.mimeType)}:{}})}return e.length>0?e:void 0}const DEDUP_HASH_THRESHOLD=256;function dedupKey(t,...e){const s=`${t}:${e.join(":")}`;if(s.length<=256)return s;return`${t}:h:${createHash("sha256").update(s).digest("hex").slice(0,24)}`}function toISO(t){if(!(null==t||t<=0))try{return new Date(t).toISOString()}catch{return}}function resolveToolArgs(t){const e=t.arguments??t.args??t.input;if(isRecord(e))return e;if("string"==typeof e){const t=e.trim();if(t.startsWith("{")||t.startsWith("["))try{return JSON.parse(t)}catch{}}return{}}export class AcpGatewayEvents{state;transport;config;logger;writeObs;completePrompt;failPrompt;maybeCompletePromptFromChatState;agentFetchFn;onStreamDelta;constructor(t){this.state=t.state,this.transport=t.transport,this.config=t.config,this.logger=t.logger,this.writeObs=t.writeObs,this.completePrompt=t.completePrompt,this.failPrompt=t.failPrompt,this.maybeCompletePromptFromChatState=t.maybeCompletePromptFromChatState,this.agentFetchFn=t.agentFetch,this.onStreamDelta=t.onStreamDelta}touchStreamActivity(t){t.lastStreamActivityTs=Date.now(),this.resetStreamIdleTimer(t)}resetStreamIdleTimer(t){this.state.clearStreamIdleTimer(t);const e=this.config.streamIdleTimeoutMs??DEFAULT_STREAM_IDLE_TIMEOUT_MS,s=this.config.streamStaleTimeoutMs??DEFAULT_STREAM_STALE_TIMEOUT_MS;t.streamIdleTimer=setTimeout(()=>{if(!t.lastStreamActivityTs)return;const e=Date.now()-t.lastStreamActivityTs;e>s&&(this.logger.warn?.(`[gateway-events] Stream stale timeout sessionId=${t.sessionId} stale=${e}ms`),this.completePrompt(t,"end_turn"))},e),"object"==typeof t.streamIdleTimer&&"unref"in t.streamIdleTimer&&t.streamIdleTimer.unref()}sendToolCallSpacer(t,e){const s={sessionUpdate:"agent_message_chunk",content:{type:"text",text:"\n"},...e?{timestamp:e}:{}};this.transport.sendSessionUpdate(t.sessionId,s),t.assistantMq?.push(s),this.onStreamDelta?.(t,s)}resolveToolCallId(t,e,s){const o=asString(e.toolCallId)??asString(e.tool_call_id)??asString(e.callId)??asString(e.id);if(o){const e=this.state.toolCallIdMap.get(o);return e?(t.sessionId&&this.state.toolCallToSessionMap.set(e,t.sessionId),e):(this.state.toolCallIdMap.set(o,o),t.sessionId&&this.state.toolCallToSessionMap.set(o,t.sessionId),o)}const a=`tc_${t.currentRunId??"unknown"}_${this.state.nextToolCallSeq++}`;return t.sessionId&&this.state.toolCallToSessionMap.set(a,t.sessionId),a}handleChatEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??this.state.findSessionByKey(t.sessionKey);if(!e)return;if(!this.state.isCurrentStreamOwner(e,t.runId)){const s=!e.currentRunId&&void 0===e.pendingPromptId;if(!t.runId||!s||this.state.isRunCompleted(t.runId))return;this.adoptBackgroundRun(e,t.runId)}const s=e.sessionId,o=t.message;if(o){this.touchStreamActivity(e);if("assistant"===o.role){if(!Array.isArray(o.content))return;for(const a of o.content){const n=a;if(!isRecord(n))continue;const r=asString(n.type);if(!r)continue;const i=toISO(o.timestamp);if("thinking"===r){if(e.hasAgentThinkingStream)continue;const t=normalizeText(n.thinking)??normalizeText(n.text);if(!t)continue;const o=dedupKey("chat:thinking",t);if(e.chatReplayDedupKeys.has(o))continue;e.chatReplayDedupKeys.add(o);const a={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:t},...i?{timestamp:i}:{}};this.transport.sendSessionUpdate(s,a)&&(e.assistantMq?.push(a),this.onStreamDelta?.(e,a));continue}if("toolCall"===r){if(this.sendToolCallSpacer(e,i),e.hasAgentToolStartStream)continue;const t=this.resolveToolCallId(e,n,"start"),o=asString(n.name)??asString(n.toolName)??asString(n.tool_name)??"tool",a=resolveToolArgs(n),r=dedupKey("chat:tool_start",t,JSON.stringify(a));if(e.chatReplayDedupKeys.has(r))continue;e.chatReplayDedupKeys.add(r);const l=buildToolCallPayload({toolCallId:t,toolName:o,args:a,timestamp:i});this.transport.sendSessionUpdate(s,l)&&(e.assistantMq?.push(l),this.onStreamDelta?.(e,l));continue}if("toolResult"===r){if(this.sendToolCallSpacer(e,i),e.hasAgentToolResultStream)continue;const t=this.resolveToolCallId(e,n,"result"),o=asString(n.name)??asString(n.toolName)??asString(n.tool_name)??"tool",a=n.result,r=normalizeText(n.text)??normalizeText(n.result)??(isRecord(a)||Array.isArray(a)?JSON.stringify(a,null,2):void 0);if(!r)continue;const l=dedupKey("chat:tool_result",t,r);if(e.chatReplayDedupKeys.has(l))continue;e.chatReplayDedupKeys.add(l);const c=buildToolCallUpdatePayload({toolCallId:t,toolName:o,outputText:r,rawOutput:a,resourceLinks:extractResourceLinksFromRawResult(a),timestamp:i});this.transport.sendSessionUpdate(s,c)&&(e.assistantMq?.push(c),this.onStreamDelta?.(e,c));continue}if("text"===r&&n.text){if(e.hasAgentAssistantStream||e.agentAssistantTextSoFar)continue;const o=stripTransportMetadata(n.text);if(!o)continue;const a=dedupKey("chat:text",o);if(e.chatReplayDedupKeys.has(a))continue;e.chatReplayDedupKeys.add(a);const r=e.chatAssistantTextSoFar??"";let l;if(e.chatAssistantTextSoFar=o,o.startsWith(r))l=o.slice(r.length);else{if(r.startsWith(o))continue;l=o}if(!l)continue;this.logger.debug?.(`[gateway-events] chat.text delta seq=${t.seq} runId=${t.runId} len=${l.length} prevLen=${r.length}`);const c={sessionUpdate:"agent_message_chunk",content:{type:"text",text:l},...i?{timestamp:i}:{}};this.transport.sendSessionUpdate(s,c),e.assistantMq?.push(c),this.onStreamDelta?.(e,c)}}}}else"delta"===t.state&&this.touchStreamActivity(e);if(this.maybeCompletePromptFromChatState(e,t),!e.promptDone)if("final"===t.state)this.completePrompt(e,t.stopReason||"end_turn");else if("aborted"===t.state||"cancelled"===t.state||"cancel"===t.state)this.completePrompt(e,"cancelled");else if("error"===t.state){const s=t.errorMessage||Errors.AgentError.message;this.failPrompt(e,Errors.AgentError.rpcCode,formatFriendlyError(s))}}handleAgentEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??(t.sessionKey?this.state.findSessionByKey(t.sessionKey):void 0);if(!e)return;if(!this.state.isCurrentStreamOwner(e,t.runId)){const s=!e.currentRunId&&void 0===e.pendingPromptId;if(!t.runId||!s||this.state.isRunCompleted(t.runId))return;this.adoptBackgroundRun(e,t.runId)}const s=e.sessionId;this.touchStreamActivity(e);const o=toISO(t.ts),a=t=>{this.transport.sendSessionUpdate(s,t),e.assistantMq?.push(t),this.onStreamDelta?.(e,t)};if("assistant"===t.stream){e.hasAgentAssistantStream=!0;const n=this.state.extractAgentAssistantDelta(e,t.data??{});if(n){const e=normalizeTransportText(n);e&&(this.logger.debug?.(`[gateway-events] agent.assistant delta seq=${t.seq} runId=${t.runId} len=${e.length} source=${t.data?.delta?"delta":"cumulative"}`),a({sessionUpdate:"agent_message_chunk",content:{type:"text",text:e},...o?{timestamp:o}:{}}))}const r=[],i=t.data;i&&(Array.isArray(i.content)?r.push(...i.content):isRecord(i.content)?r.push(i.content):"string"==typeof i.type&&r.push(i));for(const t of r){if(!isRecord(t))continue;const n=asString(t.type);if("thinking"===n){e.hasAgentThinkingStream=!0;const a=normalizeText(t.thinking)??normalizeText(t.text);if(a){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:a},...o?{timestamp:o}:{}};this.transport.sendSessionUpdate(s,t)&&(e.assistantMq?.push(t),this.onStreamDelta?.(e,t))}continue}if("toolCall"===n){this.sendToolCallSpacer(e,o),e.hasAgentToolStartStream=!0;const s=this.resolveToolCallId(e,t,"start"),n=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",r=resolveToolArgs(t);a(buildToolCallPayload({toolCallId:s,toolName:n,args:r,timestamp:o}));continue}if("toolResult"===n){this.sendToolCallSpacer(e,o),e.hasAgentToolResultStream=!0;const s=this.resolveToolCallId(e,t,"result"),n=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",r=t.result,i=normalizeText(t.text)??normalizeText(t.result)??(isRecord(r)||Array.isArray(r)?JSON.stringify(r,null,2):void 0);i&&a(buildToolCallUpdatePayload({toolCallId:s,toolName:n,outputText:i,rawOutput:r,resourceLinks:extractResourceLinksFromRawResult(r),timestamp:o}));continue}}}else if("thinking"===t.stream){e.hasAgentThinkingStream=!0;const a=normalizeText(t.data?.delta)??normalizeText(t.data?.text);if(a){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:a},...o?{timestamp:o}:{}};this.transport.sendSessionUpdate(s,t)&&(e.assistantMq?.push(t),this.onStreamDelta?.(e,t))}}else if("tool"===t.stream){const s=t.data,n="string"==typeof s?.phase?s.phase:void 0;if("start"===n){this.sendToolCallSpacer(e,o),e.hasAgentToolStartStream=!0;const t=this.resolveToolCallId(e,s,"start"),n=asString(s?.name)??asString(s?.toolName)??asString(s?.tool_name)??"tool",r=resolveToolArgs(s);a(buildToolCallPayload({toolCallId:t,toolName:n,args:r,timestamp:o}))}else if("result"===n)this.sendToolCallSpacer(e,o),e.hasAgentToolResultStream=!0,this.handleToolResult(e,t,o).catch(t=>{this.logger.warn?.(`[gateway-events] Tool result handling failed: ${t instanceof Error?t.message:t}`)});else{const t=s?.toolCall;if(t){this.sendToolCallSpacer(e,o),e.hasAgentToolStartStream=!0;const n=asString(t?.name)??asString(s?.name)??asString(s?.toolName)??"tool",r={...s,...t},i=this.resolveToolCallId(e,r,"start"),l=resolveToolArgs(r);a(buildToolCallPayload({toolCallId:i,toolName:n,args:l,timestamp:o}))}}}else if("lifecycle"===t.stream){const s=t.data,o="string"==typeof s?.phase?s.phase:void 0;if("end"===o)this.completePrompt(e,"end_turn");else if("cancelled"===o||"cancel"===o)this.completePrompt(e,"cancelled");else if("error"===o){const o=s?.error,a=o&&"object"==typeof o?o:void 0,n=("string"==typeof s?.message?s.message:void 0)??("string"==typeof o?o:void 0)??("string"==typeof a?.message?a.message:void 0)??("string"==typeof s?.reason?s.reason:void 0)??(a?JSON.stringify(a):void 0)??"gateway lifecycle error",r=formatFriendlyError(n);this.logger.warn?.(`[gateway-events] lifecycle error runId=${t.runId} sessionKey=${t.sessionKey} message=${n} data=${JSON.stringify(s)}`),this.failPrompt(e,Errors.AgentError.rpcCode,r)}}}async handleToolResult(t,e,s){const o=e.data,a=o?.toolCall,n=("string"==typeof a?.id?a.id:void 0)??("string"==typeof o?.toolCallId?o.toolCallId:void 0),r=(n?this.state.toolCallIdMap.get(n):void 0)??n??`tc_${e.runId}_${e.seq}`,i=("string"==typeof a?.name?a.name:void 0)??("string"==typeof o?.name?o.name:void 0)??"tool",l="string"==typeof o?.text?o.text:void 0,c=n??r,d=o?.result;let m;try{m=await resolveToolResultBlocks({toolCallId:c,toolName:i,rawText:l,rawResult:d})}catch{m=l?[{type:"content",content:{type:"text",text:l}}]:[]}0===m.length&&l&&(m=[{type:"content",content:{type:"text",text:l}}]);const p=[],u=[];for(const t of m)t.content&&("resource_link"===t.content.type&&t.content.uri?u.push({uri:t.content.uri,name:t.content.name??"file",...t.content.mimeType?{mimeType:t.content.mimeType}:{}}):t.content.text&&p.push(t.content.text));const h=buildToolCallUpdatePayload({toolCallId:r,toolName:i,outputText:p.join("\n"),rawOutput:d,resourceLinks:u,timestamp:s});this.transport.sendSessionUpdate(t.sessionId,h)&&(t.assistantMq?.push(h),this.onStreamDelta?.(t,h))}adoptBackgroundRun(t,e){this.logger.debug?.(`[gateway-events] Adopting background run ${e} on session ${t.sessionId} (prev=${t.currentRunId})`),t.currentRunId=e,t.promptDone=!1,this.state.promptsByRunId.set(e,t),t.assistantStreamStartedAt=Date.now(),t.lastStreamActivityTs=Date.now(),t.hasAgentAssistantStream=void 0,t.hasAgentThinkingStream=void 0,t.hasAgentToolStartStream=void 0,t.hasAgentToolResultStream=void 0,t.chatAssistantTextSoFar=void 0,t.agentAssistantTextSoFar=void 0,t.chatReplayDedupKeys?.clear()}handleCronEvent(t){if(this.transport.sendNotification("_beeos/cron_event",{payload:t}),this.config.agentGatewayUrl&&this.agentFetchFn){const e=`${this.config.agentGatewayUrl}/api/v1/agent/events`;this.agentFetchFn(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"cron",data:t})}).catch(t=>{this.logger.warn?.(`[gateway-events] event report failed: ${t instanceof Error?t.message:t}`)})}}}
@@ -1 +1 @@
1
- import{stripTransportMetadata}from"../message-filter.js";function inferToolKindFromName(t){if(!t)return;const e=t.toLowerCase();return e.includes("read")?"read":e.includes("write")||e.includes("edit")?"edit":e.includes("delete")||e.includes("remove")?"delete":e.includes("move")||e.includes("rename")?"move":e.includes("search")||e.includes("find")?"search":e.includes("exec")||e.includes("run")||e.includes("bash")?"execute":e.includes("fetch")||e.includes("http")?"fetch":"other"}const BEEOS_USER_MSG_MARKER_RE=/(?:^|\n)\s*User Message From \w+:\s*\n/i,CANVAS_STATE_RE=/^\[Canvas State\][^\n]*/,SYSTEM_NOTE_RE=/^\[System Note\][^\n]*/,CANVAS_INTERACTION_RE=/^\[Canvas Interaction\] The user clicked "([^"]*)" on surface "([^"]*)"/,ACTION_CONTEXT_RE=/\nAction context: (.+?)(?:\n|$)/s;function stripBeeosUserEnvelope(t){const e=BEEOS_USER_MSG_MARKER_RE.exec(t);if(e){return t.slice(e.index+e[0].length).trim()}let o=t;return o=o.replace(CANVAS_STATE_RE,"").trimStart(),o=o.replace(SYSTEM_NOTE_RE,"").trimStart(),stripTransportMetadata(o).trim()}export async function loadHistory(t,e,o=50){if(!t?.isConnected)return[];try{const n=parseGatewayHistory(await t.chatHistory(e,o));if(n.length>0){n.filter(t=>"user"===t.role).length,n.filter(t=>"assistant"===t.role).length}return n}catch(t){return[]}}export function groupIntoTurns(t){const e=[];let o=null;const n=()=>{o&&(e.push(o),o=null)};for(const s of t){if("assistant"===s.role&&(s.reasoning||s.toolCalls?.length)){n(),o={role:"assistant",content:s.content,reasoning:s.reasoning,files:s.files,sourceMessages:[s]},s.toolCalls?.length&&(o.toolCalls=s.toolCalls.map(t=>({id:t.id,callId:t.id,name:t.name,arguments:t.arguments??"",status:t.status??"in_progress",output:t.output,isError:"error"===t.status||"failed"===t.status,kind:inferToolKindFromName(t.name)})));continue}if("system"===s.role){n();const t={role:"system",content:s.content};s.metadata&&(t.metadata=s.metadata),e.push(t);continue}if("user"===s.role){n();const t={role:"user",content:s.content};s.provenance&&(t.provenance=s.provenance),s.metadata&&(t.metadata=s.metadata),e.push(t);continue}o||(o={role:"assistant",content:"",sourceMessages:[]}),o.sourceMessages||(o.sourceMessages=[]),o.sourceMessages.push(s);switch(s.updateType??"agent_message_chunk"){case"agent_thought_chunk":o.reasoning=(o.reasoning??"")+s.content;break;case"tool_call":o.toolCalls||(o.toolCalls=[]),o.toolCalls.push({id:s.toolCallId??`tc_${o.toolCalls.length}`,callId:s.toolCallId??`tc_${o.toolCalls.length}`,name:s.toolName??"tool",arguments:s.content||"",status:"in_progress",kind:inferToolKindFromName(s.toolName)});break;case"tool_call_update":{o.toolCalls||(o.toolCalls=[]);const t=s.toolCallId?o.toolCalls.find(t=>t.id===s.toolCallId):void 0;t?(t.output=s.content,t.status="completed"):o.toolCalls.push({id:s.toolCallId??`tc_${o.toolCalls.length}`,callId:s.toolCallId??`tc_${o.toolCalls.length}`,name:s.toolName??"tool",arguments:"",status:"completed",output:s.content,kind:inferToolKindFromName(s.toolName)});break}default:o.content+=s.content,s.files?.length&&(o.files=[...o.files??[],...s.files])}}return n(),e}const TOOL_CALL_BLOCK_TYPES=new Set(["toolCall","toolUse","functionCall","tool_use","tool_call","toolcall"]),TOOL_RESULT_BLOCK_TYPES=new Set(["tool_result","tool_result_error","toolResult"]);function extractBlockText(t){if("string"==typeof t)return t;if(!Array.isArray(t))return"";let e="";for(const o of t)"string"!=typeof o?o&&"object"==typeof o&&"string"==typeof o.text&&(e+=o.text):e+=o;return e}function parseGatewayHistory(t){if(!t||"object"!=typeof t)return[];const e=t.messages;if(!Array.isArray(e))return[];const o=[];let n=null;for(const t of e){if(!t||"object"!=typeof t)continue;const e=t,s="string"==typeof e.role?e.role:"assistant";if("toolResult"===s||"tool"===s){const t=String(e.toolCallId??e.tool_call_id??e.toolUseId??e.tool_use_id??""),s=extractBlockText(e.content),i=!0===e.is_error||!0===e.isError;if(n?.toolCalls?.length){const e=(t?n.toolCalls.find(e=>e.id===t):void 0)??n.toolCalls.find(t=>void 0===t.output);e&&(e.output=s,e.status=i?"failed":"completed")}else o.push({role:"assistant",content:s,updateType:"tool_call_update",toolCallId:t||void 0});continue}if("user"===s&&Array.isArray(e.content)){let t=!1,s=!1;for(const o of e.content){if(!o||"object"!=typeof o)continue;const e=o,n="string"==typeof e.type?e.type:"";TOOL_RESULT_BLOCK_TYPES.has(n)?t=!0:"text"===n&&"string"==typeof e.text&&e.text.trim()&&(s=!0)}if(t&&!s){for(const t of e.content){if(!t||"object"!=typeof t)continue;const e=t,s="string"==typeof e.type?e.type:"";if(!TOOL_RESULT_BLOCK_TYPES.has(s))continue;const i=String(e.toolUseId??e.tool_use_id??e.toolCallId??e.tool_call_id??""),l="string"==typeof e.output?e.output:extractBlockText(e.content),a=!0===e.is_error||!0===e.isError;if(n?.toolCalls?.length){const t=(i?n.toolCalls.find(t=>t.id===i):void 0)??n.toolCalls.find(t=>void 0===t.output);t&&(t.output=l,t.status=a?"failed":"completed")}else o.push({role:"assistant",content:l,updateType:"tool_call_update",toolCallId:i||void 0})}continue}}const i="user"===s?"user":"assistant";let l,a,r,c,u,p="";if("string"==typeof e.content)p=e.content;else if(Array.isArray(e.content)){if("assistant"===i&&e.content.some(t=>{if(!t||"object"!=typeof t)return!1;const e="string"==typeof t.type?t.type:"";return TOOL_CALL_BLOCK_TYPES.has(e)||TOOL_RESULT_BLOCK_TYPES.has(e)})&&"assistant"===i){const t=new Map;for(const n of e.content){if("string"==typeof n){n&&o.push({role:"assistant",content:n,updateType:"agent_message_chunk"});continue}if(!n||"object"!=typeof n)continue;const e=n,s="string"==typeof e.type?e.type:"";if("text"===s&&"string"==typeof e.text&&e.text)o.push({role:"assistant",content:e.text,updateType:"agent_message_chunk"});else if("thinking"===s&&"string"==typeof e.thinking)o.push({role:"assistant",content:e.thinking,updateType:"agent_thought_chunk"});else if("resource_link"===s&&"string"==typeof e.uri&&e.uri)r||(r=[]),r.push({uri:e.uri,..."string"==typeof e.name&&e.name?{name:e.name}:{},..."string"==typeof e.mimeType&&e.mimeType?{mimeType:e.mimeType}:{}});else if(TOOL_CALL_BLOCK_TYPES.has(s)){const n=e.arguments??e.input,s="string"==typeof n?n:null!=n?JSON.stringify(n):"",i=String(e.id??e.toolCallId??e.tool_call_id??""),l={role:"assistant",content:s,updateType:"tool_call",toolCallId:i,toolName:String(e.name??"tool")};o.push(l),i&&t.set(i,l)}else if(TOOL_RESULT_BLOCK_TYPES.has(s)){const n=String(e.toolUseId??e.tool_use_id??e.toolCallId??e.tool_call_id??""),s="string"==typeof e.output?e.output:extractBlockText(e.content),i=!0===e.is_error||!0===e.isError;if(o.push({role:"assistant",content:s,updateType:"tool_call_update",toolCallId:n||void 0,toolName:t.get(n)?.toolName}),n&&t.has(n)){const e=t.get(n);e.toolCalls||(e.toolCalls=[{id:n,name:e.toolName??"tool",arguments:e.content,status:i?"failed":"completed",output:s}])}}}r?.length&&o.push({role:"assistant",content:"",files:r}),"string"==typeof e.reasoning&&e.reasoning?o.push({role:"assistant",content:e.reasoning,updateType:"agent_thought_chunk"}):"string"==typeof e.thinking&&e.thinking&&o.push({role:"assistant",content:e.thinking,updateType:"agent_thought_chunk"}),o.length>0&&"assistant"===o[o.length-1].role&&(n=o[o.length-1]);continue}for(const t of e.content){if("string"==typeof t){p+=t;continue}if(!t||"object"!=typeof t)continue;const e=t,o="string"==typeof e.type?e.type:"";"text"===o&&"string"==typeof e.text?p+=e.text:"thinking"===o&&"string"==typeof e.thinking?a=(a??"")+e.thinking:"resource_link"===o&&"string"==typeof e.uri&&e.uri&&(r||(r=[]),r.push({uri:e.uri,..."string"==typeof e.name&&e.name?{name:e.name}:{},..."string"==typeof e.mimeType&&e.mimeType?{mimeType:e.mimeType}:{}}))}}if(a||("string"==typeof e.reasoning&&e.reasoning?a=e.reasoning:"string"==typeof e.thinking&&e.thinking&&(a=e.thinking)),!l?.length){const t=e.tool_calls??e.toolCalls;if(Array.isArray(t)){l=[];for(const e of t){if(!e||"object"!=typeof e)continue;const t=e,o=t.function;l.push({id:String(t.id??t.toolCallId??""),name:String(o?.name??t.name??t.toolName??"tool"),arguments:"string"==typeof t.arguments?t.arguments:"string"==typeof o?.arguments?o.arguments:"",status:String(t.status??"in_progress"),output:"string"==typeof t.output?t.output:void 0})}0===l.length&&(l=void 0)}}if("user"===i&&p){p=stripBeeosUserEnvelope(p);const t=CANVAS_INTERACTION_RE.exec(p);if(t){const e=t[1];c={kind:"canvas_action"};const o={type:"canvas_action",actionName:e,surfaceId:t[2]},n=ACTION_CONTEXT_RE.exec(p);if(n)try{o.actionPayload=JSON.parse(n[1])}catch{}u=o,p=`Clicked "${e}"`}}if(!(p||a||l?.length||r?.length))continue;if(!c&&"user"===i&&e.provenance&&"object"==typeof e.provenance){const t=e.provenance;"string"==typeof t.kind&&(c={kind:t.kind},"string"==typeof t.sourceTool&&(c.sourceTool=t.sourceTool))}const f={role:i,content:p,reasoning:a,toolCalls:l,provenance:c,metadata:u,files:r};o.push(f),"assistant"===i&&(n=f)}o.filter(t=>t.toolCalls?.length),o.reduce((t,e)=>t+(e.toolCalls?.length??0),0),o.reduce((t,e)=>t+(e.toolCalls?.filter(t=>void 0!==t.output).length??0),0);return o}const BEEOS_FILE_URI_RE=/beeos-file:\/\/([a-zA-Z0-9_-]+)/g;export function extractBeeosFileUrisFromOutput(t){if(!t)return[];const e=[];for(const o of t.matchAll(BEEOS_FILE_URI_RE))e.push(o[0]);return[...new Set(e)]}const BEEOS_REF_RE=/<BEEOS_REF\s+((?:[^"]|"[^"]*")*?)\s*\/>/g;function parseBeeosRefAttrs(t){const e=new Map,o=/(\w+)="([^"]*)"/g;for(const n of t.matchAll(o))e.set(n[1],n[2]);return e}function guessMimeFromName(t){if(!t)return;const e=t.split(".").pop()?.toLowerCase();return e?{jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",pdf:"application/pdf",mp3:"audio/mpeg",wav:"audio/wav",mp4:"video/mp4",webm:"video/webm",txt:"text/plain",json:"application/json",csv:"text/csv"}[e]:void 0}export function splitBeeosRefReplayText(t){if(!t)return[];if(!t.includes("<BEEOS_REF "))return[{type:"text",text:t}];const e=[];let o=0;for(const n of t.matchAll(BEEOS_REF_RE)){const s=t.slice(o,n.index).trim();s&&e.push({type:"text",text:s});const i=parseBeeosRefAttrs(n[1]),l=i.get("id"),a=i.get("name"),r=i.get("status");l&&"download_failed"!==r?e.push({type:"resource_link",uri:`beeos-file://${l}`,fileName:a,mimeType:guessMimeFromName(a)??"application/octet-stream"}):l&&e.push({type:"resource_link",uri:`beeos-file://${l}`,fileName:a??"file",mimeType:guessMimeFromName(a)??"application/octet-stream"}),o=(n.index??0)+n[0].length}const n=t.slice(o).trim();return n&&e.push({type:"text",text:n}),e}const ATTACHMENT_MARKER_RE=/\[(image|file)\s+([^\]]*)\]/g;export function splitUserAttachmentMarkers(t){if(!t)return[];if(!t.includes("[image ")&&!t.includes("[file "))return[{type:"text",text:t}];const e=[];let o=0;for(const n of t.matchAll(ATTACHMENT_MARKER_RE)){const s=t.slice(o,n.index).trim();s&&e.push({type:"text",text:s});const i=n[1],l=n[2],a=new Map;for(const t of l.split(/\s+/)){const e=t.indexOf("=");e>0&&a.set(t.slice(0,e),t.slice(e+1))}e.push({type:i,mimeType:a.get("mime"),fileName:a.get("name")||("image"===i?"Image":"File")}),o=(n.index??0)+n[0].length}const n=t.slice(o).trim();return n&&e.push({type:"text",text:n}),e}export function splitUserReplayBlocks(t){return t?t.includes("<BEEOS_REF ")?splitBeeosRefReplayText(t):t.includes("[image ")||t.includes("[file ")?splitUserAttachmentMarkers(t):[{type:"text",text:t}]:[]}
1
+ import{stripTransportMetadata}from"../message-filter.js";function toISO(t){if(!(null==t||t<=0))try{return new Date(t).toISOString()}catch{return}}function inferToolKindFromName(t){if(!t)return;const e=t.toLowerCase();return e.includes("read")?"read":e.includes("write")||e.includes("edit")?"edit":e.includes("delete")||e.includes("remove")?"delete":e.includes("move")||e.includes("rename")?"move":e.includes("search")||e.includes("find")?"search":e.includes("exec")||e.includes("run")||e.includes("bash")?"execute":e.includes("fetch")||e.includes("http")?"fetch":"other"}const BEEOS_USER_MSG_MARKER_RE=/(?:^|\n)\s*User Message From \w+:\s*\n/i,CANVAS_STATE_RE=/^\[Canvas State\][^\n]*/,SYSTEM_NOTE_RE=/^\[System Note\][^\n]*/,CANVAS_INTERACTION_RE=/^\[Canvas Interaction\] The user clicked "([^"]*)" on surface "([^"]*)"/,ACTION_CONTEXT_RE=/\nAction context: (.+?)(?:\n|$)/s;function stripBeeosUserEnvelope(t){const e=BEEOS_USER_MSG_MARKER_RE.exec(t);if(e){return t.slice(e.index+e[0].length).trim()}let o=t;return o=o.replace(CANVAS_STATE_RE,"").trimStart(),o=o.replace(SYSTEM_NOTE_RE,"").trimStart(),stripTransportMetadata(o).trim()}export async function loadHistory(t,e,o=100){if(!t?.isConnected)return[];try{const n=parseGatewayHistory(await t.chatHistory(e,o));if(n.length>0){n.filter(t=>"user"===t.role).length,n.filter(t=>"assistant"===t.role).length}return n}catch(t){return[]}}export function groupIntoTurns(t){const e=[];let o=null;const n=()=>{o&&(e.push(o),o=null)};for(const s of t){if("assistant"===s.role&&(s.reasoning||s.toolCalls?.length)){n(),o={role:"assistant",content:s.content,reasoning:s.reasoning,files:s.files,sourceMessages:[s],...s.timestamp?{timestamp:s.timestamp}:{}},s.toolCalls?.length&&(o.toolCalls=s.toolCalls.map(t=>({id:t.id,callId:t.id,name:t.name,arguments:t.arguments??"",status:t.status??"in_progress",output:t.output,isError:"error"===t.status||"failed"===t.status,kind:inferToolKindFromName(t.name)})));continue}if("system"===s.role){n();const t={role:"system",content:s.content};s.metadata&&(t.metadata=s.metadata),s.timestamp&&(t.timestamp=s.timestamp),e.push(t);continue}if("user"===s.role){n();const t={role:"user",content:s.content};s.provenance&&(t.provenance=s.provenance),s.metadata&&(t.metadata=s.metadata),s.timestamp&&(t.timestamp=s.timestamp),e.push(t);continue}o||(o={role:"assistant",content:"",sourceMessages:[]}),o.sourceMessages||(o.sourceMessages=[]),o.sourceMessages.push(s),!o.timestamp&&s.timestamp&&(o.timestamp=s.timestamp);switch(s.updateType??"agent_message_chunk"){case"agent_thought_chunk":o.reasoning=(o.reasoning??"")+s.content;break;case"tool_call":o.toolCalls||(o.toolCalls=[]),o.toolCalls.push({id:s.toolCallId??`tc_${o.toolCalls.length}`,callId:s.toolCallId??`tc_${o.toolCalls.length}`,name:s.toolName??"tool",arguments:s.content||"",status:"in_progress",kind:inferToolKindFromName(s.toolName)});break;case"tool_call_update":{o.toolCalls||(o.toolCalls=[]);const t=s.toolCallId?o.toolCalls.find(t=>t.id===s.toolCallId):void 0;t?(t.output=s.content,t.status="completed"):o.toolCalls.push({id:s.toolCallId??`tc_${o.toolCalls.length}`,callId:s.toolCallId??`tc_${o.toolCalls.length}`,name:s.toolName??"tool",arguments:"",status:"completed",output:s.content,kind:inferToolKindFromName(s.toolName)});break}default:o.content+=s.content,s.files?.length&&(o.files=[...o.files??[],...s.files])}}return n(),e}const TOOL_CALL_BLOCK_TYPES=new Set(["toolCall","toolUse","functionCall","tool_use","tool_call","toolcall"]),TOOL_RESULT_BLOCK_TYPES=new Set(["tool_result","tool_result_error","toolResult"]);function extractBlockText(t){if("string"==typeof t)return t;if(!Array.isArray(t))return"";let e="";for(const o of t)"string"!=typeof o?o&&"object"==typeof o&&"string"==typeof o.text&&(e+=o.text):e+=o;return e}function parseGatewayHistory(t){if(!t||"object"!=typeof t)return[];const e=t.messages;if(!Array.isArray(e))return[];const o=[];let n=null;for(const t of e){if(!t||"object"!=typeof t)continue;const e=t,s="string"==typeof e.role?e.role:"assistant";if("toolResult"===s||"tool"===s){const t=String(e.toolCallId??e.tool_call_id??e.toolUseId??e.tool_use_id??""),s=extractBlockText(e.content),i=!0===e.is_error||!0===e.isError;if(n?.toolCalls?.length){const e=(t?n.toolCalls.find(e=>e.id===t):void 0)??n.toolCalls.find(t=>void 0===t.output);e&&(e.output=s,e.status=i?"failed":"completed")}else o.push({role:"assistant",content:s,updateType:"tool_call_update",toolCallId:t||void 0});continue}if("user"===s&&Array.isArray(e.content)){let t=!1,s=!1;for(const o of e.content){if(!o||"object"!=typeof o)continue;const e=o,n="string"==typeof e.type?e.type:"";TOOL_RESULT_BLOCK_TYPES.has(n)?t=!0:"text"===n&&"string"==typeof e.text&&e.text.trim()&&(s=!0)}if(t&&!s){for(const t of e.content){if(!t||"object"!=typeof t)continue;const e=t,s="string"==typeof e.type?e.type:"";if(!TOOL_RESULT_BLOCK_TYPES.has(s))continue;const i=String(e.toolUseId??e.tool_use_id??e.toolCallId??e.tool_call_id??""),l="string"==typeof e.output?e.output:extractBlockText(e.content),a=!0===e.is_error||!0===e.isError;if(n?.toolCalls?.length){const t=(i?n.toolCalls.find(t=>t.id===i):void 0)??n.toolCalls.find(t=>void 0===t.output);t&&(t.output=l,t.status=a?"failed":"completed")}else o.push({role:"assistant",content:l,updateType:"tool_call_update",toolCallId:i||void 0})}continue}}const i="user"===s?"user":"assistant";let l,a,r,c,u,p="";if("string"==typeof e.content)p=e.content;else if(Array.isArray(e.content)){if("assistant"===i&&e.content.some(t=>{if(!t||"object"!=typeof t)return!1;const e="string"==typeof t.type?t.type:"";return TOOL_CALL_BLOCK_TYPES.has(e)||TOOL_RESULT_BLOCK_TYPES.has(e)})&&"assistant"===i){const t=new Map;for(const n of e.content){if("string"==typeof n){n&&o.push({role:"assistant",content:n,updateType:"agent_message_chunk"});continue}if(!n||"object"!=typeof n)continue;const e=n,s="string"==typeof e.type?e.type:"";if("text"===s&&"string"==typeof e.text&&e.text)o.push({role:"assistant",content:e.text,updateType:"agent_message_chunk"});else if("thinking"===s&&"string"==typeof e.thinking)o.push({role:"assistant",content:e.thinking,updateType:"agent_thought_chunk"});else if("resource_link"===s&&"string"==typeof e.uri&&e.uri)r||(r=[]),r.push({uri:e.uri,..."string"==typeof e.name&&e.name?{name:e.name}:{},..."string"==typeof e.mimeType&&e.mimeType?{mimeType:e.mimeType}:{}});else if(TOOL_CALL_BLOCK_TYPES.has(s)){const n=e.arguments??e.input,s="string"==typeof n?n:null!=n?JSON.stringify(n):"",i=String(e.id??e.toolCallId??e.tool_call_id??""),l={role:"assistant",content:s,updateType:"tool_call",toolCallId:i,toolName:String(e.name??"tool")};o.push(l),i&&t.set(i,l)}else if(TOOL_RESULT_BLOCK_TYPES.has(s)){const n=String(e.toolUseId??e.tool_use_id??e.toolCallId??e.tool_call_id??""),s="string"==typeof e.output?e.output:extractBlockText(e.content),i=!0===e.is_error||!0===e.isError;if(o.push({role:"assistant",content:s,updateType:"tool_call_update",toolCallId:n||void 0,toolName:t.get(n)?.toolName}),n&&t.has(n)){const e=t.get(n);e.toolCalls||(e.toolCalls=[{id:n,name:e.toolName??"tool",arguments:e.content,status:i?"failed":"completed",output:s}])}}}r?.length&&o.push({role:"assistant",content:"",files:r}),"string"==typeof e.reasoning&&e.reasoning?o.push({role:"assistant",content:e.reasoning,updateType:"agent_thought_chunk"}):"string"==typeof e.thinking&&e.thinking&&o.push({role:"assistant",content:e.thinking,updateType:"agent_thought_chunk"}),o.length>0&&"assistant"===o[o.length-1].role&&(n=o[o.length-1]);continue}for(const t of e.content){if("string"==typeof t){p+=t;continue}if(!t||"object"!=typeof t)continue;const e=t,o="string"==typeof e.type?e.type:"";"text"===o&&"string"==typeof e.text?p+=e.text:"thinking"===o&&"string"==typeof e.thinking?a=(a??"")+e.thinking:"resource_link"===o&&"string"==typeof e.uri&&e.uri&&(r||(r=[]),r.push({uri:e.uri,..."string"==typeof e.name&&e.name?{name:e.name}:{},..."string"==typeof e.mimeType&&e.mimeType?{mimeType:e.mimeType}:{}}))}}if(a||("string"==typeof e.reasoning&&e.reasoning?a=e.reasoning:"string"==typeof e.thinking&&e.thinking&&(a=e.thinking)),!l?.length){const t=e.tool_calls??e.toolCalls;if(Array.isArray(t)){l=[];for(const e of t){if(!e||"object"!=typeof e)continue;const t=e,o=t.function;l.push({id:String(t.id??t.toolCallId??""),name:String(o?.name??t.name??t.toolName??"tool"),arguments:"string"==typeof t.arguments?t.arguments:"string"==typeof o?.arguments?o.arguments:"",status:String(t.status??"in_progress"),output:"string"==typeof t.output?t.output:void 0})}0===l.length&&(l=void 0)}}if("user"===i&&p){p=stripBeeosUserEnvelope(p);const t=CANVAS_INTERACTION_RE.exec(p);if(t){const e=t[1];c={kind:"canvas_action"};const o={type:"canvas_action",actionName:e,surfaceId:t[2]},n=ACTION_CONTEXT_RE.exec(p);if(n)try{o.actionPayload=JSON.parse(n[1])}catch{}u=o,p=`Clicked "${e}"`}}if(!(p||a||l?.length||r?.length))continue;if(!c&&"user"===i&&e.provenance&&"object"==typeof e.provenance){const t=e.provenance;"string"==typeof t.kind&&(c={kind:t.kind},"string"==typeof t.sourceTool&&(c.sourceTool=t.sourceTool))}const f=toISO("number"==typeof e.timestamp?e.timestamp:"number"==typeof e.message?.timestamp?e.message.timestamp:void 0),m={role:i,content:p,reasoning:a,toolCalls:l,provenance:c,metadata:u,files:r,...f?{timestamp:f}:{}};o.push(m),"assistant"===i&&(n=m)}o.filter(t=>t.toolCalls?.length),o.reduce((t,e)=>t+(e.toolCalls?.length??0),0),o.reduce((t,e)=>t+(e.toolCalls?.filter(t=>void 0!==t.output).length??0),0);return o}const BEEOS_FILE_URI_RE=/beeos-file:\/\/([a-zA-Z0-9_-]+)/g;export function extractBeeosFileUrisFromOutput(t){if(!t)return[];const e=[];for(const o of t.matchAll(BEEOS_FILE_URI_RE))e.push(o[0]);return[...new Set(e)]}const BEEOS_REF_RE=/<BEEOS_REF\s+((?:[^"]|"[^"]*")*?)\s*\/>/g;function parseBeeosRefAttrs(t){const e=new Map,o=/(\w+)="([^"]*)"/g;for(const n of t.matchAll(o))e.set(n[1],n[2]);return e}function guessMimeFromName(t){if(!t)return;const e=t.split(".").pop()?.toLowerCase();return e?{jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",pdf:"application/pdf",mp3:"audio/mpeg",wav:"audio/wav",mp4:"video/mp4",webm:"video/webm",txt:"text/plain",json:"application/json",csv:"text/csv"}[e]:void 0}export function splitBeeosRefReplayText(t){if(!t)return[];if(!t.includes("<BEEOS_REF "))return[{type:"text",text:t}];const e=[];let o=0;for(const n of t.matchAll(BEEOS_REF_RE)){const s=t.slice(o,n.index).trim();s&&e.push({type:"text",text:s});const i=parseBeeosRefAttrs(n[1]),l=i.get("id"),a=i.get("name"),r=i.get("status");l&&"download_failed"!==r?e.push({type:"resource_link",uri:`beeos-file://${l}`,fileName:a,mimeType:guessMimeFromName(a)??"application/octet-stream"}):l&&e.push({type:"resource_link",uri:`beeos-file://${l}`,fileName:a??"file",mimeType:guessMimeFromName(a)??"application/octet-stream"}),o=(n.index??0)+n[0].length}const n=t.slice(o).trim();return n&&e.push({type:"text",text:n}),e}const ATTACHMENT_MARKER_RE=/\[(image|file)\s+([^\]]*)\]/g;export function splitUserAttachmentMarkers(t){if(!t)return[];if(!t.includes("[image ")&&!t.includes("[file "))return[{type:"text",text:t}];const e=[];let o=0;for(const n of t.matchAll(ATTACHMENT_MARKER_RE)){const s=t.slice(o,n.index).trim();s&&e.push({type:"text",text:s});const i=n[1],l=n[2],a=new Map;for(const t of l.split(/\s+/)){const e=t.indexOf("=");e>0&&a.set(t.slice(0,e),t.slice(e+1))}e.push({type:i,mimeType:a.get("mime"),fileName:a.get("name")||("image"===i?"Image":"File")}),o=(n.index??0)+n[0].length}const n=t.slice(o).trim();return n&&e.push({type:"text",text:n}),e}export function splitUserReplayBlocks(t){return t?t.includes("<BEEOS_REF ")?splitBeeosRefReplayText(t):t.includes("[image ")||t.includes("[file ")?splitUserAttachmentMarkers(t):[{type:"text",text:t}]:[]}
@@ -1 +1 @@
1
- export function inferToolKind(e){if(!e)return"other";const t=e.toLowerCase();return t.includes("read")?"read":t.includes("write")||t.includes("edit")?"edit":t.includes("delete")||t.includes("remove")?"delete":t.includes("move")||t.includes("rename")?"move":t.includes("search")||t.includes("find")?"search":t.includes("exec")||t.includes("run")||t.includes("bash")?"execute":t.includes("fetch")||t.includes("http")?"fetch":"other"}export function formatToolTitle(e,t){const n=e??"tool";if(!t||0===Object.keys(t).length)return n;return`${n}: ${Object.entries(t).map(([e,t])=>{const n="string"==typeof t?t:JSON.stringify(t);return`${e}: ${n.length>100?`${n.slice(0,100)}...`:n}`}).join(", ")}`}function serializeArgs(e){return Object.keys(e).length>0?JSON.stringify(e,null,2):""}export function buildToolCallPayload(e){const t=serializeArgs(e.args);return{sessionUpdate:"tool_call",toolCallId:e.toolCallId,name:e.toolName,title:e.title??formatToolTitle(e.toolName,e.args),kind:e.kind??inferToolKind(e.toolName),status:e.status??"in_progress",rawInput:e.args,arguments:t,content:t?[{type:"content",content:{type:"text",text:t}}]:void 0}}export function buildToolCallUpdatePayload(e){const t=[];e.outputText&&t.push({type:"content",content:{type:"text",text:e.outputText}});for(const n of e.resourceLinks??[])t.push({type:"content",content:{type:"resource_link",uri:n.uri,name:n.name,...n.mimeType?{mimeType:n.mimeType}:{}}});return{sessionUpdate:"tool_call_update",toolCallId:e.toolCallId,name:e.toolName,title:e.toolName,kind:e.kind??inferToolKind(e.toolName),status:e.status??"completed",...void 0!==e.rawOutput?{rawOutput:e.rawOutput}:{},...t.length>0?{content:t}:{}}}
1
+ export function inferToolKind(t){if(!t)return"other";const e=t.toLowerCase();return e.includes("read")?"read":e.includes("write")||e.includes("edit")?"edit":e.includes("delete")||e.includes("remove")?"delete":e.includes("move")||e.includes("rename")?"move":e.includes("search")||e.includes("find")?"search":e.includes("exec")||e.includes("run")||e.includes("bash")?"execute":e.includes("fetch")||e.includes("http")?"fetch":"other"}export function formatToolTitle(t,e){const n=t??"tool";if(!e||0===Object.keys(e).length)return n;return`${n}: ${Object.entries(e).map(([t,e])=>{const n="string"==typeof e?e:JSON.stringify(e);return`${t}: ${n.length>100?`${n.slice(0,100)}...`:n}`}).join(", ")}`}function serializeArgs(t){return Object.keys(t).length>0?JSON.stringify(t,null,2):""}export function buildToolCallPayload(t){const e=serializeArgs(t.args);return{sessionUpdate:"tool_call",toolCallId:t.toolCallId,name:t.toolName,title:t.title??formatToolTitle(t.toolName,t.args),kind:t.kind??inferToolKind(t.toolName),status:t.status??"in_progress",rawInput:t.args,arguments:e,content:e?[{type:"content",content:{type:"text",text:e}}]:void 0,...t.timestamp?{timestamp:t.timestamp}:{}}}export function buildToolCallUpdatePayload(t){const e=[];t.outputText&&e.push({type:"content",content:{type:"text",text:t.outputText}});for(const n of t.resourceLinks??[])e.push({type:"content",content:{type:"resource_link",uri:n.uri,name:n.name,...n.mimeType?{mimeType:n.mimeType}:{}}});return{sessionUpdate:"tool_call_update",toolCallId:t.toolCallId,name:t.toolName,title:t.toolName,kind:t.kind??inferToolKind(t.toolName),status:t.status??"completed",...void 0!==t.rawOutput?{rawOutput:t.rawOutput}:{},...e.length>0?{content:e}:{},...t.timestamp?{timestamp:t.timestamp}:{}}}
@@ -1 +1 @@
1
- import{acpError}from"../errors/index.js";export class AcpTransport{debugIndex=0;sendBridgeMessage;obs;forward;sessionUpdateCounts=new Map;seqCounters=new Map;_isReady;constructor(e,s,t,r){this.sendBridgeMessage=e,this.obs=s??(()=>{}),this.forward=t??{forwardThinking:!0,forwardToolCalls:!0},this._isReady=r}isReady(){return!this._isReady||this._isReady()}get forwardControl(){return this.forward}nextMeta(){return{_debug_index:this.debugIndex++,_ts:Date.now()}}sendResult(e,s){const t=this.nextMeta();this.detectProtocolError(e,s,t._debug_index);const r={jsonrpc:"2.0",id:e,result:this.attachMeta(s,t),_debug_index:t._debug_index,_ts:t._ts};this.sendBridgeMessage(r),this.obs({component:"acp-transport",domain:"reply",name:"rpc.result",severity:"debug",payload:{id:e,_debug_index:t._debug_index}})}sendError(e,s,t,r){const o=this.nextMeta(),n={code:s,message:t};n.data=void 0!==r?this.attachMeta(r,o):{_meta:o};const i={jsonrpc:"2.0",id:e,error:n,_debug_index:o._debug_index,_ts:o._ts};this.sendBridgeMessage(i),this.obs({component:"acp-transport",domain:"reply",name:"rpc.error",severity:"warn",payload:{id:e,code:s,message:t,_debug_index:o._debug_index}})}sendAcpError(e,s,t){const r=acpError(s,t);this.sendError(e,r.code,r.message,r.data)}sendNotification(e,s){const t=this.nextMeta(),r={jsonrpc:"2.0",method:e,params:s,_debug_index:t._debug_index,_ts:t._ts};this.sendBridgeMessage(r)}enrichWithTurnId(e,s){return s?{...e,_turnId:s}:e}sendSessionUpdate(e,s,t){const r=s,o=r?.sessionUpdate,n=(this.seqCounters.get(e)??0)+1;if(this.seqCounters.set(e,n),r._seq=n,"agent_thought_chunk"===o&&!this.forward.forwardThinking)return!1;if(("tool_call"===o||"tool_call_update"===o)&&!this.forward.forwardToolCalls)return!1;const i=(this.sessionUpdateCounts.get(e)??0)+1;return this.sessionUpdateCounts.set(e,i),i%50==0&&this.obs({component:"acp-transport",domain:"session-update",name:"send.rate",severity:"debug",payload:{sessionId:e,totalSent:i,lastType:o,_debug_index:this.debugIndex}}),this.sendNotification("session/update",{sessionId:e,update:this.enrichWithTurnId(s,t)}),!0}resetSessionUpdateCount(e){this.sessionUpdateCounts.delete(e)}resetSeq(e){this.seqCounters.delete(e)}sendCronSessionUpdate(e,s,t){return this.sendNotification("session/update",{sessionId:e,update:this.enrichWithTurnId(s,t.requestId),_meta:{...this.nextMeta(),...t}}),!0}detectProtocolError(e,s,t){if(!s||"object"!=typeof s)return;const r=s,o="error"in r&&null!=r.error,n="ok"in r&&!1===r.ok;(o||n)&&this.obs({component:"acp-transport",domain:"reply",name:"protocol.error_in_result",severity:"warn",payload:{id:e,_debug_index:t,hasError:o,notOk:n,errorSummary:o?"string"==typeof r.error?r.error.slice(0,200):JSON.stringify(r.error).slice(0,200):void 0}})}attachMeta(e,s){return!e||"object"!=typeof e||Array.isArray(e)?e:{...e,_meta:s}}}
1
+ import{acpError}from"../errors/index.js";export class AcpTransport{debugIndex=0;sendBridgeMessage;obs;forward;sessionUpdateCounts=new Map;_isReady;constructor(e,t,s,r){this.sendBridgeMessage=e,this.obs=t??(()=>{}),this.forward=s??{forwardThinking:!0,forwardToolCalls:!0},this._isReady=r}isReady(){return!this._isReady||this._isReady()}get forwardControl(){return this.forward}nextMeta(){return{_debug_index:this.debugIndex++,_ts:Date.now()}}sendResult(e,t){const s=this.nextMeta();this.detectProtocolError(e,t,s._debug_index);const r={jsonrpc:"2.0",id:e,result:this.attachMeta(t,s),_debug_index:s._debug_index,_ts:s._ts};this.sendBridgeMessage(r),this.obs({component:"acp-transport",domain:"reply",name:"rpc.result",severity:"debug",payload:{id:e,_debug_index:s._debug_index}})}sendError(e,t,s,r){const o=this.nextMeta(),n={code:t,message:s};n.data=void 0!==r?this.attachMeta(r,o):{_meta:o};const i={jsonrpc:"2.0",id:e,error:n,_debug_index:o._debug_index,_ts:o._ts};this.sendBridgeMessage(i),this.obs({component:"acp-transport",domain:"reply",name:"rpc.error",severity:"warn",payload:{id:e,code:t,message:s,_debug_index:o._debug_index}})}sendAcpError(e,t,s){const r=acpError(t,s);this.sendError(e,r.code,r.message,r.data)}sendNotification(e,t){const s=this.nextMeta(),r={jsonrpc:"2.0",method:e,params:t,_debug_index:s._debug_index,_ts:s._ts};this.sendBridgeMessage(r)}enrichWithTurnId(e,t){return t?{...e,_turnId:t}:e}sendSessionUpdate(e,t,s){const r=t,o=r?.sessionUpdate;if("agent_thought_chunk"===o&&!this.forward.forwardThinking)return!1;if(("tool_call"===o||"tool_call_update"===o)&&!this.forward.forwardToolCalls)return!1;const n=(this.sessionUpdateCounts.get(e)??0)+1;return this.sessionUpdateCounts.set(e,n),n%50==0&&this.obs({component:"acp-transport",domain:"session-update",name:"send.rate",severity:"debug",payload:{sessionId:e,totalSent:n,lastType:o,_debug_index:this.debugIndex}}),this.sendNotification("session/update",{sessionId:e,update:this.enrichWithTurnId(t,s)}),!0}resetSessionUpdateCount(e){this.sessionUpdateCounts.delete(e)}sendCronSessionUpdate(e,t,s){return this.sendNotification("session/update",{sessionId:e,update:this.enrichWithTurnId(t,s.requestId),_meta:{...this.nextMeta(),...s}}),!0}detectProtocolError(e,t,s){if(!t||"object"!=typeof t)return;const r=t,o="error"in r&&null!=r.error,n="ok"in r&&!1===r.ok;(o||n)&&this.obs({component:"acp-transport",domain:"reply",name:"protocol.error_in_result",severity:"warn",payload:{id:e,_debug_index:s,hasError:o,notOk:n,errorSummary:o?"string"==typeof r.error?r.error.slice(0,200):JSON.stringify(r.error).slice(0,200):void 0}})}attachMeta(e,t){return!e||"object"!=typeof e||Array.isArray(e)?e:{...e,_meta:t}}}
@@ -1 +1 @@
1
- export function sv(e){if(null==e)return{literalString:""};if("string"==typeof e)return{literalString:e};if("number"==typeof e||"boolean"==typeof e)return{literalString:String(e)};if("object"==typeof e){const t=e;for(const e of["value","label","name","text","title"])if("string"==typeof t[e]||"number"==typeof t[e])return{literalString:String(t[e])};try{return{literalString:JSON.stringify(e)}}catch{return{literalString:"[Object]"}}}return{literalString:String(e)}}const A2UI_VERSION="0.8";export function makeBeginRendering(e,t){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,message:{beginRendering:{surfaceId:e,root:t}}}}export function makeSurfaceUpdate(e,t,n){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,presentation:n,message:{surfaceUpdate:{surfaceId:e,components:t}}}}export function makeDeleteSurface(e){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,message:{deleteSurface:{surfaceId:e}}}}export function findRootIds(e){const t=new Set;for(const n of e){const e=Object.values(n.component)[0];if(!e)continue;"string"==typeof e.child&&t.add(e.child);const r=e.children;if(r?.explicitList)for(const e of r.explicitList)t.add(e);const i=e.tabItems;if(i)for(const e of i)e.child&&t.add(e.child)}return e.filter(e=>!t.has(e.id)).map(e=>e.id)}let _idCounter=0;export function nextComponentId(e){return`${e}_${(++_idCounter).toString(36)}_${Date.now().toString(36)}`}
1
+ export function sv(e){if(null==e)return{literalString:""};if("string"==typeof e)return{literalString:e};if("number"==typeof e||"boolean"==typeof e)return{literalString:String(e)};if("object"==typeof e){const t=e;for(const e of["value","label","name","text","title"])if("string"==typeof t[e]||"number"==typeof t[e])return{literalString:String(t[e])};try{return{literalString:JSON.stringify(e)}}catch{return{literalString:"[Object]"}}}return{literalString:String(e)}}const A2UI_VERSION="0.8";export function makeBeginRendering(e,t){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,message:{beginRendering:{surfaceId:e,root:t}}}}export function makeSurfaceUpdate(e,t,n){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,presentation:n,message:{surfaceUpdate:{surfaceId:e,components:t}}}}export function makeDeleteSurface(e){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,message:{deleteSurface:{surfaceId:e}}}}export function findRootIds(e){const t=new Set;for(const n of e){const e=Object.values(n.component)[0];if(!e||"object"!=typeof e)continue;const r=e;"string"==typeof r.child&&t.add(r.child);const i=r.children;if(i?.explicitList)for(const e of i.explicitList)t.add(e);const o=r.tabItems;if(o)for(const e of o)e.child&&t.add(e.child)}return e.filter(e=>!t.has(e.id)).map(e=>e.id)}let _idCounter=0;export function nextComponentId(e){return`${e}_${(++_idCounter).toString(36)}_${Date.now().toString(36)}`}
@@ -1 +1 @@
1
- import{MSG_SYNC,MSG_SYNC_UPDATE,MSG_AWARENESS}from"./constants.js";const MAX_CACHED_CANVASES=20,MAX_SURFACES_PER_CANVAS=20,MAX_BYTES_PER_CANVAS=524288,MAX_TOTAL_BYTES=4194304,MAX_COMPONENTS_PER_SURFACE=500,RATE_LIMIT_PER_SURFACE_PER_SEC=30;export class CanvasStateManager{canvasEntries=new Map;sessionMap=new Map;canvasSessionMap=new Map;canvasRefCount=new Map;relayClient=null;joinedCanvases=new Set;agentId;editingClearTimer=null;relayReadyResolve=null;relayReadyPromise=new Promise(e=>{this.relayReadyResolve=e});logger;setRelayClient(e){this.relayClient=e,this.relayReadyResolve?.()}getRelayClient(){return this.relayClient}waitForRelayClient(e=15e3){return this.relayClient?Promise.resolve(this.relayClient):Promise.race([this.relayReadyPromise.then(()=>this.relayClient),new Promise(t=>setTimeout(()=>t(null),e))])}setAgentId(e){this.agentId=e}setLogger(e){this.logger=e}touchEntry(e){let t=this.canvasEntries.get(e);return t?(this.canvasEntries.delete(e),t.lastAccessed=Date.now(),this.canvasEntries.set(e,t)):(t={canvasId:e,surfaces:new Map,actionHandlers:new Map,lastAccessed:Date.now(),cachedBytes:0},this.canvasEntries.set(e,t),this.enforceCanvasCountLimit()),t}findEntryBySurfaceId(e){for(const t of this.canvasEntries.values())if(t.surfaces.has(e))return t}async joinCanvas(e,t){t&&this.registerSession(t,e),this.joinedCanvases.has(e)||(this.touchEntry(e),this.relayClient&&(await this.relayClient.joinCanvas(e,{onUpdate:t=>{this.logger?.debug?.(`[canvas-state] Received update from relay for canvas=${e}: ${t.length} bytes`);const s=this.canvasEntries.get(e);s&&this.processRelayMessageForEntry(s,t)},onAwareness:t=>{this.logger?.debug?.(`[canvas-state] Received awareness from relay for canvas=${e}: ${t.length} bytes`)}}),this.joinedCanvases.add(e)))}leaveCanvas(e){this.joinedCanvases.delete(e),this.relayClient?.leaveCanvas(e)}isCanvasJoined(e){return this.joinedCanvases.has(e)}get canvasServiceConnected(){return this.relayClient?.connected??!1}registerSession(e,t){const s=this.sessionMap.get(e);if(s&&s!==t){const e=(this.canvasRefCount.get(s)??1)-1;e<=0?this.canvasRefCount.delete(s):this.canvasRefCount.set(s,e)}this.sessionMap.set(e,t),this.canvasSessionMap.set(t,e),this.canvasRefCount.set(t,(this.canvasRefCount.get(t)??0)+(s===t?0:1))}resolveCanvasForSession(e){return this.sessionMap.get(e)}resolveSessionForCanvas(e){return this.canvasSessionMap.get(e)}removeSession(e){const t=this.sessionMap.get(e);if(this.sessionMap.delete(e),t){const e=(this.canvasRefCount.get(t)??1)-1;e<=0?(this.canvasRefCount.delete(t),this.canvasSessionMap.delete(t),this.canvasEntries.delete(t),this.leaveCanvas(t),this.relayClient?.deleteCanvas(t).catch(e=>{this.logger?.warn?.(`[canvas-state] Failed to delete canvas ${t}: ${e}`)})):this.canvasRefCount.set(t,e)}}async lazyLoadCanvas(e,t=3e3){const s=this.canvasEntries.get(e);if(s&&s.surfaces.size>0)return!0;if(!this.relayClient)return!1;const n=this.touchEntry(e);return await this.joinCanvas(e),await new Promise(e=>setTimeout(e,Math.min(t,1500))),n.surfaces.size>0}static MAX_CTX_SURFACES=8;static MAX_CTX_CHARS=500;buildContextForCanvas(e){const t=this.canvasEntries.get(e);if(!t||0===t.surfaces.size)return null;this.touchEntry(e);const s=[...t.surfaces.values()].sort((e,t)=>t.updatedAt-e.updatedAt).slice(0,CanvasStateManager.MAX_CTX_SURFACES),n=t.surfaces.size-s.length;let a=`[Canvas State] Active surfaces: ${s.map(e=>{const s=this.summarizeFromEntry(t,e.surfaceId);if(!s)return`"${e.surfaceId}"`;const n=s.types.slice(0,3).join(", "),a=s.title?`: "${s.title}"`:"";return`"${e.surfaceId}" (${n}${a})`}).join("; ")}.`;return n>0&&(a+=` ...and ${n} more.`),a+=" To modify existing content, reuse its id in render_ui.",a.length>CanvasStateManager.MAX_CTX_CHARS&&(a=a.slice(0,CanvasStateManager.MAX_CTX_CHARS-3)+"..."),a}has(e,t){const s=t?this.canvasEntries.get(t):this.findEntryBySurfaceId(e);return s?.surfaces.has(e)??!1}get(e){return this.findEntryBySurfaceId(e)?.surfaces.get(e)}getAll(e){const t=this.canvasEntries.get(e);return t?Array.from(t.surfaces.values()):[]}getAllActive(e){return this.getAll(e)}create(e,t,s,n,a){let r=this.canvasEntries.get(e);return r||(r=this.touchEntry(e)),this.createInEntry(r,t,s,n,a)}createInEntry(e,t,s,n,a){this.enforceMaxSurfacesForEntry(e);const r=Date.now(),i={surfaceId:t,presentation:s,components:n,rootComponentId:a?.rootComponentId,dataModels:new Map,origin:a?.origin??{kind:"agent"},createdAt:r,updatedAt:r,tokenBucket:{tokens:30,lastRefill:r}};return a?.dataModel&&i.dataModels.set(t,a.dataModel),e.surfaces.set(t,i),a?.actionHandler&&e.actionHandlers.set(t,a.actionHandler),this.recomputeEntryBytes(e),this.enforceEntryMemoryBudget(e),this.enforceGlobalMemoryBudget(),this.syncSurfaceToRelay(e.canvasId,t,n,s,i.rootComponentId,a?.dataModel),i}update(e,t,s,n,a){const r=s?this.canvasEntries.get(s):this.findEntryBySurfaceId(e);if(!r)return;const i=r.surfaces.get(e);return i?(i.components=t,n&&(i.rootComponentId=n),a&&i.dataModels.set(e,a),i.updatedAt=Date.now(),this.recomputeEntryBytes(r),this.enforceEntryMemoryBudget(r),this.enforceGlobalMemoryBudget(),this.syncSurfaceToRelay(r.canvasId,e,t,i.presentation,i.rootComponentId,a),i):void 0}delete(e,t){const s=t?this.canvasEntries.get(t):this.findEntryBySurfaceId(e);if(!s)return!1;const n=this.removeSurfaceFromEntry(s,e);return n&&(this.recomputeEntryBytes(s),this.syncDeleteToRelay(s.canvasId,e)),n}clear(e){const t=this.canvasEntries.get(e);if(t){for(const s of t.surfaces.keys())this.syncDeleteToRelay(e,s);t.actionHandlers.clear(),t.surfaces.clear(),t.cachedBytes=0}}sizeOf(e){return this.canvasEntries.get(e)?.surfaces.size??0}getActionHandler(e,t){const s=t?this.canvasEntries.get(t):this.findEntryBySurfaceId(e);return s?.actionHandlers.get(e)}bindActionHandler(e,t,s){const n=s?this.canvasEntries.get(s):this.findEntryBySurfaceId(e);n&&n.actionHandlers.set(e,t)}summarize(e){const t=this.findEntryBySurfaceId(e);return t?this.summarizeFromEntry(t,e):null}summarizeFromEntry(e,t){const s=e.surfaces.get(t);if(!s||!Array.isArray(s.components))return null;const n=s.components,a=[];let r;for(const e of n){if(!e.component)continue;const t=Object.keys(e.component)[0];if(t&&a.push(t),"Text"===t){const t=e.component.Text,s=t?.usageHint;if(!r&&s&&["h1","h2","h3","h4"].includes(s)){const e=t?.text;e?.literalString&&(r=e.literalString)}}}return{types:[...new Set(a)],title:r}}checkRateLimit(e){const t=this.findEntryBySurfaceId(e);if(!t)return!0;const s=t.surfaces.get(e);if(!s)return!0;const n=Date.now(),a=(n-s.tokenBucket.lastRefill)/1e3;return s.tokenBucket.tokens=Math.min(30,s.tokenBucket.tokens+30*a),s.tokenBucket.lastRefill=n,s.tokenBucket.tokens>=1&&(s.tokenBucket.tokens-=1,!0)}validateComponentCount(e){return!Array.isArray(e)||e.length<=500}syncDeleteToRelay(e,t){if(this.relayClient)try{const s=JSON.stringify({surfaceId:t,deleted:!0}),n=(new TextEncoder).encode(s),a=new Uint8Array(2+n.length);a[0]=MSG_SYNC,a[1]=MSG_SYNC_UPDATE,a.set(n,2),this.relayClient.sendUpdate(e,a)}catch(s){this.logger?.warn?.(`[canvas-state] syncDeleteToRelay failed for canvas=${e} surface=${t}:`,s)}}syncSurfaceToRelay(e,t,s,n,a,r){if(this.relayClient){try{const i={surfaceId:t,components:s,presentation:n??"panel"};a&&(i.root=a),r&&Object.keys(r).length>0&&(i.dataModel=r);const o=JSON.stringify(i),c=(new TextEncoder).encode(o),l=new Uint8Array(2+c.length);l[0]=MSG_SYNC,l[1]=MSG_SYNC_UPDATE,l.set(c,2),this.relayClient.sendUpdate(e,l),this.logger?.info?.(`[canvas-state] Queued/sent ${l.length} bytes to relay for canvas=${e} surfaceId=${t}`)}catch(e){this.logger?.warn?.(`[canvas-state] syncSurfaceToRelay failed: ${e}`)}this.sendEditingAwareness(e,t)}else this.logger?.warn?.(`[canvas-state] syncSurfaceToRelay: relayClient is null for surfaceId=${t}, data NOT synced to relay`)}sendEditingAwareness(e,t){if(this.relayClient){this.editingClearTimer&&(clearTimeout(this.editingClearTimer),this.editingClearTimer=null);try{const s=JSON.stringify({agentId:this.agentId??null,editingSurface:t,timestamp:Date.now()}),n=(new TextEncoder).encode(s),a=new Uint8Array(1+n.length);a[0]=MSG_AWARENESS,a.set(n,1),this.relayClient.sendAwareness(e,a)}catch{}this.editingClearTimer=setTimeout(()=>{this.editingClearTimer=null,this.sendClearEditingAwareness(e)},3e3)}}sendClearEditingAwareness(e){if(this.relayClient)try{const t=JSON.stringify({agentId:this.agentId??null,editingSurface:"",timestamp:Date.now()}),s=(new TextEncoder).encode(t),n=new Uint8Array(1+s.length);n[0]=MSG_AWARENESS,n.set(s,1),this.relayClient.sendAwareness(e,n)}catch{}}processRelayMessageForEntry(e,t){if(!(t.length<3)&&t[0]===MSG_SYNC&&t[1]===MSG_SYNC_UPDATE)try{const s=(new TextDecoder).decode(t.subarray(2)),n=JSON.parse(s);if(!n.surfaceId)return;if(n.deleted)return this.removeSurfaceFromEntry(e,n.surfaceId),this.recomputeEntryBytes(e),void this.logger?.info?.(`[canvas-state] Surface ${n.surfaceId} deleted by remote participant`);if(n.components&&!e.surfaces.has(n.surfaceId)){const t="inline"===n.presentation||"panel"===n.presentation?n.presentation:"panel",s=Date.now(),a={surfaceId:n.surfaceId,presentation:t,components:n.components,dataModels:new Map,origin:{kind:"agent"},createdAt:s,updatedAt:s,tokenBucket:{tokens:30,lastRefill:s}};e.surfaces.set(n.surfaceId,a),this.recomputeEntryBytes(e),this.logger?.info?.(`[canvas-state] Restored surface ${n.surfaceId} from relay`)}}catch{}}removeSurfaceFromEntry(e,t){return e.actionHandlers.delete(t),e.surfaces.delete(t)}enforceMaxSurfacesForEntry(e){if(e.surfaces.size<20)return;let t,s=1/0;for(const[n,a]of e.surfaces)a.createdAt<s&&(s=a.createdAt,t=n);t&&this.removeSurfaceFromEntry(e,t)}recomputeEntryBytes(e){let t=0;for(const s of e.surfaces.values())t+=this.estimateSize(s);e.cachedBytes=t}enforceEntryMemoryBudget(e){if(e.cachedBytes<=524288)return;const t=[...e.surfaces.entries()].sort(([,e],[,t])=>e.updatedAt-t.updatedAt);for(const[s]of t){if(e.cachedBytes<=524288)break;const t=e.surfaces.get(s);t&&(e.cachedBytes-=this.estimateSize(t)),this.removeSurfaceFromEntry(e,s)}}enforceGlobalMemoryBudget(){let e=0;for(const t of this.canvasEntries.values())e+=t.cachedBytes;if(!(e<=4194304))for(const[t,s]of this.canvasEntries){if(e<=4194304)break;this.joinedCanvases.has(t)||(e-=s.cachedBytes,this.canvasEntries.delete(t))}}enforceCanvasCountLimit(){for(;this.canvasEntries.size>20;){let e=!1;for(const[t]of this.canvasEntries)if(!this.joinedCanvases.has(t)){this.canvasEntries.delete(t),e=!0;break}if(!e)break}}estimateSize(e){try{let t=2*JSON.stringify(e.components).length;if(e.dataModels.size>0)for(const s of e.dataModels.values())t+=2*JSON.stringify(s).length;return t}catch{return 1024}}destroy(){this.editingClearTimer&&(clearTimeout(this.editingClearTimer),this.editingClearTimer=null),this.relayClient?.disconnect(),this.relayClient=null,this.canvasEntries.clear(),this.sessionMap.clear(),this.canvasSessionMap.clear(),this.canvasRefCount.clear(),this.joinedCanvases.clear()}}
1
+ import{MSG_SYNC,MSG_SYNC_UPDATE,MSG_AWARENESS}from"./constants.js";const MAX_CACHED_CANVASES=20,MAX_SURFACES_PER_CANVAS=20,MAX_BYTES_PER_CANVAS=524288,MAX_TOTAL_BYTES=4194304,MAX_COMPONENTS_PER_SURFACE=500,RATE_LIMIT_PER_SURFACE_PER_SEC=30;export class CanvasStateManager{canvasEntries=new Map;sessionMap=new Map;canvasSessionMap=new Map;canvasRefCount=new Map;relayClient=null;joinedCanvases=new Set;agentId;editingClearTimer=null;relayReadyResolve=null;relayReadyPromise=new Promise(e=>{this.relayReadyResolve=e});logger;setRelayClient(e){this.relayClient=e,this.relayReadyResolve?.()}getRelayClient(){return this.relayClient}waitForRelayClient(e=15e3){return this.relayClient?Promise.resolve(this.relayClient):Promise.race([this.relayReadyPromise.then(()=>this.relayClient),new Promise(t=>setTimeout(()=>t(null),e))])}setAgentId(e){this.agentId=e}setLogger(e){this.logger=e}touchEntry(e){let t=this.canvasEntries.get(e);return t?(this.canvasEntries.delete(e),t.lastAccessed=Date.now(),this.canvasEntries.set(e,t)):(t={canvasId:e,surfaces:new Map,actionHandlers:new Map,lastAccessed:Date.now(),cachedBytes:0},this.canvasEntries.set(e,t),this.enforceCanvasCountLimit()),t}findEntryBySurfaceId(e){for(const t of this.canvasEntries.values())if(t.surfaces.has(e))return t}async joinCanvas(e,t){t&&this.registerSession(t,e),this.joinedCanvases.has(e)||(this.touchEntry(e),this.relayClient&&(await this.relayClient.joinCanvas(e,{onUpdate:t=>{this.logger?.debug?.(`[canvas-state] Received update from relay for canvas=${e}: ${t.length} bytes`);const s=this.canvasEntries.get(e);s&&this.processRelayMessageForEntry(s,t)},onAwareness:t=>{this.logger?.debug?.(`[canvas-state] Received awareness from relay for canvas=${e}: ${t.length} bytes`)}}),this.joinedCanvases.add(e)))}leaveCanvas(e){this.joinedCanvases.delete(e),this.relayClient?.leaveCanvas(e)}isCanvasJoined(e){return this.joinedCanvases.has(e)}get canvasServiceConnected(){return this.relayClient?.connected??!1}registerSession(e,t){const s=this.sessionMap.get(e);if(s&&s!==t){const e=(this.canvasRefCount.get(s)??1)-1;e<=0?this.canvasRefCount.delete(s):this.canvasRefCount.set(s,e)}this.sessionMap.set(e,t),this.canvasSessionMap.set(t,e),this.canvasRefCount.set(t,(this.canvasRefCount.get(t)??0)+(s===t?0:1))}resolveCanvasForSession(e){return this.sessionMap.get(e)}resolveSessionForCanvas(e){return this.canvasSessionMap.get(e)}removeSession(e){const t=this.sessionMap.get(e);if(this.sessionMap.delete(e),t){const e=(this.canvasRefCount.get(t)??1)-1;e<=0?(this.canvasRefCount.delete(t),this.canvasSessionMap.delete(t),this.canvasEntries.delete(t),this.leaveCanvas(t),this.relayClient?.deleteCanvas(t).catch(e=>{this.logger?.warn?.(`[canvas-state] Failed to delete canvas ${t}: ${e}`)})):this.canvasRefCount.set(t,e)}}async lazyLoadCanvas(e,t=3e3){const s=this.canvasEntries.get(e);if(s&&s.surfaces.size>0)return!0;if(!this.relayClient)return!1;const n=this.touchEntry(e);return await this.joinCanvas(e),await new Promise(e=>setTimeout(e,Math.min(t,1500))),n.surfaces.size>0}static MAX_CTX_SURFACES=8;static MAX_CTX_CHARS=500;buildContextForCanvas(e){const t=this.canvasEntries.get(e);if(!t||0===t.surfaces.size)return null;this.touchEntry(e);const s=[...t.surfaces.values()].sort((e,t)=>t.updatedAt-e.updatedAt).slice(0,CanvasStateManager.MAX_CTX_SURFACES),n=t.surfaces.size-s.length;let a=`[Canvas State] Active surfaces: ${s.map(e=>{const s=this.summarizeFromEntry(t,e.surfaceId);if(!s)return`"${e.surfaceId}"`;const n=s.types.slice(0,3).join(", "),a=s.title?`: "${s.title}"`:"";return`"${e.surfaceId}" (${n}${a})`}).join("; ")}.`;return n>0&&(a+=` ...and ${n} more.`),a+=" To modify existing content, reuse its id in render_ui.",a.length>CanvasStateManager.MAX_CTX_CHARS&&(a=a.slice(0,CanvasStateManager.MAX_CTX_CHARS-3)+"..."),a}has(e,t){const s=t?this.canvasEntries.get(t):this.findEntryBySurfaceId(e);return s?.surfaces.has(e)??!1}get(e){return this.findEntryBySurfaceId(e)?.surfaces.get(e)}getAll(e){const t=this.canvasEntries.get(e);return t?Array.from(t.surfaces.values()):[]}getAllActive(e){return this.getAll(e)}create(e,t,s,n,a){let r=this.canvasEntries.get(e);return r||(r=this.touchEntry(e)),this.createInEntry(r,t,s,n,a)}createInEntry(e,t,s,n,a){this.enforceMaxSurfacesForEntry(e);const r=Date.now(),i={surfaceId:t,presentation:s,components:n,rootComponentId:a?.rootComponentId,dataModels:new Map,origin:a?.origin??{kind:"agent"},createdAt:r,updatedAt:r,tokenBucket:{tokens:30,lastRefill:r}};return a?.dataModel&&i.dataModels.set(t,a.dataModel),e.surfaces.set(t,i),a?.actionHandler&&e.actionHandlers.set(t,a.actionHandler),this.recomputeEntryBytes(e),this.enforceEntryMemoryBudget(e),this.enforceGlobalMemoryBudget(),this.syncSurfaceToRelay(e.canvasId,t,n,s,i.rootComponentId,a?.dataModel),i}update(e,t,s,n,a){const r=s?this.canvasEntries.get(s):this.findEntryBySurfaceId(e);if(!r)return;const i=r.surfaces.get(e);return i?(i.components=t,n&&(i.rootComponentId=n),void 0!==a&&(Object.keys(a).length>0?i.dataModels.set(e,a):i.dataModels.delete(e)),i.updatedAt=Date.now(),this.recomputeEntryBytes(r),this.enforceEntryMemoryBudget(r),this.enforceGlobalMemoryBudget(),this.syncSurfaceToRelay(r.canvasId,e,t,i.presentation,i.rootComponentId,a),i):void 0}delete(e,t){const s=t?this.canvasEntries.get(t):this.findEntryBySurfaceId(e);if(!s)return!1;const n=this.removeSurfaceFromEntry(s,e);return n&&(this.recomputeEntryBytes(s),this.syncDeleteToRelay(s.canvasId,e)),n}clear(e){const t=this.canvasEntries.get(e);if(t){for(const s of t.surfaces.keys())this.syncDeleteToRelay(e,s);t.actionHandlers.clear(),t.surfaces.clear(),t.cachedBytes=0}}sizeOf(e){return this.canvasEntries.get(e)?.surfaces.size??0}getActionHandler(e,t){const s=t?this.canvasEntries.get(t):this.findEntryBySurfaceId(e);return s?.actionHandlers.get(e)}bindActionHandler(e,t,s){const n=s?this.canvasEntries.get(s):this.findEntryBySurfaceId(e);n&&n.actionHandlers.set(e,t)}summarize(e){const t=this.findEntryBySurfaceId(e);return t?this.summarizeFromEntry(t,e):null}summarizeFromEntry(e,t){const s=e.surfaces.get(t);if(!s||!Array.isArray(s.components))return null;const n=s.components,a=[];let r;for(const e of n){if(!e.component)continue;const t=Object.keys(e.component)[0];if(t&&a.push(t),"Text"===t){const t=e.component.Text,s=t?.usageHint;if(!r&&s&&["h1","h2","h3","h4","h5"].includes(s)){const e=t?.text;e?.literalString&&(r=e.literalString)}}}return{types:[...new Set(a)],title:r}}checkRateLimit(e){const t=this.findEntryBySurfaceId(e);if(!t)return!0;const s=t.surfaces.get(e);if(!s)return!0;const n=Date.now(),a=(n-s.tokenBucket.lastRefill)/1e3;return s.tokenBucket.tokens=Math.min(30,s.tokenBucket.tokens+30*a),s.tokenBucket.lastRefill=n,s.tokenBucket.tokens>=1&&(s.tokenBucket.tokens-=1,!0)}validateComponentCount(e){return!Array.isArray(e)||e.length<=500}syncDeleteToRelay(e,t){if(this.relayClient)try{const s=JSON.stringify({surfaceId:t,deleted:!0}),n=(new TextEncoder).encode(s),a=new Uint8Array(2+n.length);a[0]=MSG_SYNC,a[1]=MSG_SYNC_UPDATE,a.set(n,2),this.relayClient.sendUpdate(e,a)}catch(s){this.logger?.warn?.(`[canvas-state] syncDeleteToRelay failed for canvas=${e} surface=${t}:`,s)}}syncSurfaceToRelay(e,t,s,n,a,r){if(this.relayClient){try{const i={surfaceId:t,components:s,presentation:n??"panel"};a&&(i.root=a),r&&Object.keys(r).length>0&&(i.dataModel=r);const o=JSON.stringify(i),c=(new TextEncoder).encode(o),l=new Uint8Array(2+c.length);l[0]=MSG_SYNC,l[1]=MSG_SYNC_UPDATE,l.set(c,2),this.relayClient.sendUpdate(e,l),this.logger?.info?.(`[canvas-state] Queued/sent ${l.length} bytes to relay for canvas=${e} surfaceId=${t}`)}catch(e){this.logger?.warn?.(`[canvas-state] syncSurfaceToRelay failed: ${e}`)}this.sendEditingAwareness(e,t)}else this.logger?.warn?.(`[canvas-state] syncSurfaceToRelay: relayClient is null for surfaceId=${t}, data NOT synced to relay`)}sendEditingAwareness(e,t){if(this.relayClient){this.editingClearTimer&&(clearTimeout(this.editingClearTimer),this.editingClearTimer=null);try{const s=JSON.stringify({agentId:this.agentId??null,editingSurface:t,timestamp:Date.now()}),n=(new TextEncoder).encode(s),a=new Uint8Array(1+n.length);a[0]=MSG_AWARENESS,a.set(n,1),this.relayClient.sendAwareness(e,a)}catch{}this.editingClearTimer=setTimeout(()=>{this.editingClearTimer=null,this.sendClearEditingAwareness(e)},3e3)}}sendClearEditingAwareness(e){if(this.relayClient)try{const t=JSON.stringify({agentId:this.agentId??null,editingSurface:"",timestamp:Date.now()}),s=(new TextEncoder).encode(t),n=new Uint8Array(1+s.length);n[0]=MSG_AWARENESS,n.set(s,1),this.relayClient.sendAwareness(e,n)}catch{}}processRelayMessageForEntry(e,t){if(!(t.length<3)&&t[0]===MSG_SYNC&&t[1]===MSG_SYNC_UPDATE)try{const s=(new TextDecoder).decode(t.subarray(2)),n=JSON.parse(s);if(!n.surfaceId)return;if(n.deleted)return this.removeSurfaceFromEntry(e,n.surfaceId),this.recomputeEntryBytes(e),void this.logger?.info?.(`[canvas-state] Surface ${n.surfaceId} deleted by remote participant`);if(n.components&&!e.surfaces.has(n.surfaceId)){const t="inline"===n.presentation||"panel"===n.presentation?n.presentation:"panel",s=Date.now(),a=new Map;n.dataModel&&"object"==typeof n.dataModel&&Object.keys(n.dataModel).length>0&&a.set(n.surfaceId,n.dataModel);const r={surfaceId:n.surfaceId,presentation:t,components:n.components,rootComponentId:n.root,dataModels:a,origin:{kind:"agent"},createdAt:s,updatedAt:s,tokenBucket:{tokens:30,lastRefill:s}};e.surfaces.set(n.surfaceId,r),this.recomputeEntryBytes(e),this.logger?.info?.(`[canvas-state] Restored surface ${n.surfaceId} from relay`)}}catch{}}removeSurfaceFromEntry(e,t){return e.actionHandlers.delete(t),e.surfaces.delete(t)}enforceMaxSurfacesForEntry(e){if(e.surfaces.size<20)return;let t,s=1/0;for(const[n,a]of e.surfaces)a.createdAt<s&&(s=a.createdAt,t=n);t&&this.removeSurfaceFromEntry(e,t)}recomputeEntryBytes(e){let t=0;for(const s of e.surfaces.values())t+=this.estimateSize(s);e.cachedBytes=t}enforceEntryMemoryBudget(e){if(e.cachedBytes<=524288)return;const t=[...e.surfaces.entries()].sort(([,e],[,t])=>e.updatedAt-t.updatedAt);for(const[s]of t){if(e.cachedBytes<=524288)break;const t=e.surfaces.get(s);t&&(e.cachedBytes-=this.estimateSize(t)),this.removeSurfaceFromEntry(e,s)}}enforceGlobalMemoryBudget(){let e=0;for(const t of this.canvasEntries.values())e+=t.cachedBytes;if(!(e<=4194304))for(const[t,s]of this.canvasEntries){if(e<=4194304)break;this.joinedCanvases.has(t)||(e-=s.cachedBytes,this.canvasEntries.delete(t))}}enforceCanvasCountLimit(){for(;this.canvasEntries.size>20;){let e=!1;for(const[t]of this.canvasEntries)if(!this.joinedCanvases.has(t)){this.canvasEntries.delete(t),e=!0;break}if(!e)break}}estimateSize(e){try{let t=2*JSON.stringify(e.components).length;if(e.dataModels.size>0)for(const s of e.dataModels.values())t+=2*JSON.stringify(s).length;return t}catch{return 1024}}destroy(){this.editingClearTimer&&(clearTimeout(this.editingClearTimer),this.editingClearTimer=null),this.relayClient?.disconnect(),this.relayClient=null,this.canvasEntries.clear(),this.sessionMap.clear(),this.canvasSessionMap.clear(),this.canvasRefCount.clear(),this.joinedCanvases.clear()}}
@@ -1 +1 @@
1
- import{sv,findRootIds,nextComponentId}from"./a2ui-helpers.js";function nextCid(){return nextComponentId("c")}function textNode(e,t,n="body"){return{id:e,component:{Text:{text:sv(t),usageHint:n}}}}function imageNode(e,t,n="mediumFeature",a,s){const i={url:sv(t),usageHint:n};return a&&(i.fit=a),s&&(i.altText=sv(s)),{id:e,component:{Image:i}}}function iconNode(e,t){return{id:e,component:{Icon:{name:sv(t)}}}}function dividerNode(e,t="horizontal"){return{id:e,component:{Divider:{axis:t}}}}function buttonNode(e,t,n,a){const s={name:n};return a&&(s.context=a),{id:e,component:{Button:{child:t,action:s}}}}function columnNode(e,t,n,a){const s={children:{explicitList:t}};return n&&(s.distribution=n),a&&(s.alignment=a),{id:e,component:{Column:s}}}function rowNode(e,t,n,a){const s={children:{explicitList:t}};return n&&(s.distribution=n),a&&(s.alignment=a),{id:e,component:{Row:s}}}function cardNode(e,t){return{id:e,component:{Card:{child:t}}}}function tabsNode(e,t){return{id:e,component:{Tabs:{tabItems:t.map(e=>({title:sv(e.title),child:e.childId}))}}}}function textFieldNode(e,t,n="shortText",a){const s={label:sv(t),type:n};return a&&(s.text={path:a}),{id:e,component:{TextField:s}}}function checkboxNode(e,t,n){return{id:e,component:{CheckBox:{label:sv(t),value:n?{path:n}:{literalBoolean:!1}}}}}function multipleChoiceNode(e,t,n,a,s){const i={label:sv(t),options:n.map(e=>({label:sv(e.label),value:e.value})),selections:{path:a}};return s&&(i.description=sv(s)),{id:e,component:{MultipleChoice:i}}}function translateComponentsWithDataModel(e){const t={},n=[];for(const a of e){if(!a||"object"!=typeof a){const e=nextCid();n.push(textNode(e,"[invalid component]"));continue}if("form"===("string"==typeof a.type?a.type.toLowerCase():"text")){const e=buildFormWithDataModel(a);n.push(...e.nodes),Object.assign(t,e.dataModel)}else n.push(...translateComponent(a))}return{components:n,dataModel:t}}function translateComponent(e){if(!e||"object"!=typeof e){return[textNode(nextCid(),"[invalid component]")]}const t="string"==typeof e.type?e.type.toLowerCase():"text";switch(t){case"text":return buildText(e);case"card":return buildCard(e);case"table":return buildTable(e);case"form":return buildForm(e);case"image":return buildImage(e);case"button":return buildButton(e);case"grid":return buildGrid(e);case"stat":return buildStat(e);case"key_value":return buildKeyValue(e);case"list":return buildList(e);case"alert":case"callout":return buildAlert(e);case"code":return buildCode(e);case"markdown":return buildMarkdown(e);case"chart":return buildChart(e);case"divider":return buildDivider(e);case"tabs":return buildTabs(e);case"link":return buildLink(e);case"web_embed":case"video":case"audio":case"map":case"file_preview":return buildEmbed(e);case"badge":return buildBadge(e);case"progress":return buildProgress(e);case"timeline":return buildTimeline(e);case"accordion":return buildAccordion(e);case"row":case"column":{const n=(Array.isArray(e.children)?e.children:[]).flatMap(e=>translateComponent(e)),a=findRootIds(n),s=nextCid(),i="row"===t?rowNode(s,a):columnNode(s,a);return[...n,i]}default:return[textNode(nextCid(),`[Unsupported: ${t}]`)]}}function s(e){if(null==e)return"";if("string"==typeof e)return e;if("number"==typeof e||"boolean"==typeof e)return String(e);try{return JSON.stringify(e)}catch{return"[Object]"}}function buildText(e){return[textNode(nextCid(),s(e.content??e.text??""))]}function buildCard(e){const t=[],n=[];if(e.image){const a=nextCid();t.push(imageNode(a,s(e.image),"mediumFeature","cover")),n.push(a)}if(e.title){const a=nextCid();t.push(textNode(a,s(e.title),"h3")),n.push(a)}if(e.subtitle){const a=nextCid();t.push(textNode(a,s(e.subtitle),"caption")),n.push(a)}if(e.description){const a=nextCid();t.push(textNode(a,s(e.description),"body")),n.push(a)}const a=Array.isArray(e.fields)?e.fields:[];for(const e of a){if(!e||"object"!=typeof e)continue;const a=e,i=nextCid(),o=nextCid(),r=nextCid();t.push(textNode(o,s(a.label??a.key),"caption")),t.push(textNode(r,s(a.value))),t.push(rowNode(i,[o,r],"spaceBetween")),n.push(i)}const i=Array.isArray(e.actions)?e.actions:[];if(i.length>0){const e=[];for(const n of i){if(!n||"object"!=typeof n)continue;const a=n,i=nextCid(),o=nextCid(),r=s(a.label??"Action"),l=s(a.actionId??a.id??nextCid());t.push(textNode(o,r)),t.push(buttonNode(i,o,l,[{key:"actionId",value:{literalString:l}},{key:"label",value:{literalString:r}}])),e.push(i)}if(e.length>0){const a=nextCid();t.push(rowNode(a,e)),n.push(a)}}const o=Array.isArray(e.children)?e.children:[];for(const e of o){if(!e||"object"!=typeof e)continue;const a=translateComponent(e);t.push(...a);const s=findRootIds(a);n.push(...s)}if(e.footer){const a=nextCid();t.push(textNode(a,s(e.footer),"caption")),n.push(a)}const r=nextCid();t.push(columnNode(r,n));const l=nextCid();return t.push(cardNode(l,r)),t}function buildTable(e){const t=[],n=[],a=Array.isArray(e.columns)?e.columns:[],i=Array.isArray(e.rows)?e.rows:[];if(e.title){const a=nextCid();t.push(textNode(a,s(e.title),"h4")),n.push(a)}const o=[],r=[];for(const e of a)if("string"==typeof e)o.push(e),r.push(e);else if(e&&"object"==typeof e){const t=e,n=s(t.key??t.id??t.field??t.name??t.label??"");o.push(s(t.label??t.title??t.header??t.name??n)),r.push(n)}else{const t=s(e);o.push(t),r.push(t)}if(o.length>0){const e=[];for(const n of o){const a=nextCid();t.push(textNode(a,n,"caption")),e.push(a)}const a=nextCid();t.push(rowNode(a,e)),n.push(a);const s=nextCid();t.push(dividerNode(s)),n.push(s)}for(const e of i){let a;if(Array.isArray(e))a=e;else if(e&&"object"==typeof e&&r.length>0){const t=e;a=r.map(e=>{const n=Object.keys(t).find(t=>t===e||t.toLowerCase()===e.toLowerCase());return null!=n?t[n]:""})}else a=[e];const i=[];for(const e of a){const n=nextCid();t.push(textNode(n,s(e))),i.push(n)}const o=nextCid();t.push(rowNode(o,i)),n.push(o)}const l=nextCid();t.push(columnNode(l,n));const d=nextCid();return t.push(cardNode(d,l)),t}function buildForm(e){return buildFormWithDataModel(e).nodes}function buildFormWithDataModel(e){const t=[],n=[],a=Array.isArray(e.fields)?e.fields:[],i={};if(e.title){const a=nextCid();t.push(textNode(a,s(e.title),"h3")),n.push(a)}if(e.description){const a=nextCid();t.push(textNode(a,s(e.description),"body")),n.push(a)}const o=[];for(const e of a){if(!e||"object"!=typeof e)continue;const a=e,r=nextCid(),l=s(a.name??a.label??r),d=s(a.inputType??a.type??"text"),c=`/${l}`;o.push(l);const u=a.defaultValue??a.default??a.value;if("checkbox"===d||"boolean"===d||"toggle"===d)i[l]=null!=u&&!!u,t.push(checkboxNode(r,s(a.label??a.name??""),c));else if("select"===d||"multi_select"===d||"dropdown"===d||"radio"===d){const e=Array.isArray(a.options)?a.options:[],n=e.map(e=>{if("string"==typeof e)return{label:e,value:e};const t=e;return{label:s(t.label??t.value??""),value:s(t.value??t.label??"")}});if(null!=u)i[l]=String(u);else if(e.length>0){const t=e[0];i[l]="string"==typeof t?t:s(t.value??"")}else i[l]="";t.push(multipleChoiceNode(r,s(a.label??a.name??""),n,c))}else if("datetime"===d||"time"===d){i[l]=null!=u?String(u):"";const e="datetime"===d;t.push({id:r,component:{DateTimeInput:{value:{path:c},enableDate:e,enableTime:!0}}})}else if("range"===d){const e="number"==typeof a.min?a.min:0,n="number"==typeof a.max?a.max:100;i[l]=null!=u?Number(u):e,t.push({id:r,component:{Slider:{label:sv(s(a.label??a.name??"")),value:{path:c},minValue:e,maxValue:n}}})}else{i[l]=null!=u?String(u):"";const e="number"===d?"number":"date"===d?"date":"textarea"===d||"longText"===d?"longText":"shortText";t.push(textFieldNode(r,s(a.label??a.name??""),e,c))}n.push(r)}const r=o.map(e=>({key:e,value:{path:`/${e}`}})),l=nextCid(),d=nextCid();t.push(textNode(l,s(e.submitLabel??"Submit"))),t.push(buttonNode(d,l,s(e.submitActionId??"form_submit"),r)),n.push(d);const c=nextCid();return t.push(columnNode(c,n)),{nodes:t,dataModel:i}}function buildImage(e){const t=nextCid(),n=e.alt??e.altText;return[imageNode(t,s(e.url??e.src??""),"mediumFeature",void 0,n?s(n):void 0)]}function buildButton(e){const t=nextCid(),n=nextCid(),a=s(e.label??"Button"),i=s(e.actionId??n);return[textNode(t,a),buttonNode(n,t,i,[{key:"actionId",value:{literalString:i}},{key:"label",value:{literalString:a}}])]}function buildGrid(e){const t=(Array.isArray(e.children)?e.children:Array.isArray(e.components)?e.components:[]).flatMap(e=>translateComponent(e)),n=Number(e.columns??2),a=findRootIds(t),s=[],i=[];for(let e=0;e<a.length;e+=n){const t=a.slice(e,e+n),o=nextCid();s.push(rowNode(o,t)),i.push(o)}const o=nextCid();return[...t,...s,columnNode(o,i)]}function buildStat(e){const t=[],n=[];if(e.icon){const a=nextCid();t.push(iconNode(a,s(e.icon))),n.push(a)}if(e.label){const a=nextCid();t.push(textNode(a,s(e.label),"caption")),n.push(a)}const a=nextCid();if(t.push(textNode(a,s(e.value??"0"),"h2")),n.push(a),e.change){const a=nextCid(),i="positive"===e.changeType?"↑ ":"negative"===e.changeType?"↓ ":"";t.push(textNode(a,`${i}${s(e.change)}`,"caption")),n.push(a)}if(e.description){const a=nextCid();t.push(textNode(a,s(e.description),"caption")),n.push(a)}const i=nextCid();t.push(columnNode(i,n,void 0,"center"));const o=nextCid();return t.push(cardNode(o,i)),t}function buildKeyValue(e){const t=[],n=[],a=Array.isArray(e.items)?e.items:[];if(e.title){const a=nextCid();t.push(textNode(a,s(e.title),"h4")),n.push(a)}for(const e of a){if(!e||"object"!=typeof e)continue;const a=e,i=nextCid(),o=nextCid(),r=nextCid();t.push(textNode(o,s(a.key??a.label??""),"caption")),t.push(textNode(r,s(a.value??""))),t.push(rowNode(i,[o,r],"spaceBetween")),n.push(i)}const i=nextCid();t.push(columnNode(i,n));const o=nextCid();return t.push(cardNode(o,i)),t}function buildList(e){const t=[],n=[],a=Array.isArray(e.items)?e.items:[];if(e.title){const a=nextCid();t.push(textNode(a,s(e.title),"h4")),n.push(a)}for(const e of a){if(!e||"object"!=typeof e)continue;const a=e,i=[];if(a.image){const e=nextCid();t.push(imageNode(e,s(a.image),"avatar")),i.push(e)}if(a.icon){const e=nextCid();t.push(iconNode(e,s(a.icon))),i.push(e)}const o=[];if(a.title||a.name){const e=nextCid();t.push(textNode(e,s(a.title??a.name),"body")),o.push(e)}if(a.subtitle||a.description){const e=nextCid();t.push(textNode(e,s(a.subtitle??a.description),"caption")),o.push(e)}if(o.length>0){const e=nextCid();t.push(columnNode(e,o)),i.push(e)}if(a.badge||a.value){const e=nextCid();t.push(textNode(e,s(a.badge??a.value),"caption")),i.push(e)}const r=nextCid();t.push(rowNode(r,i,"start","center")),n.push(r)}const i=nextCid();return t.push(columnNode(i,n)),t}function buildAlert(e){const t=[],n=[],a=s(e.variant??"info"),i=nextCid();t.push(iconNode(i,{info:"info",success:"check",warning:"error",error:"error",tip:"info",caution:"error",important:"error",note:"info"}[a]??"info")),n.push(i);const o=[];if(e.title){const n=nextCid();t.push(textNode(n,s(e.title),"h5")),o.push(n)}if(e.message||e.content){const n=nextCid();t.push(textNode(n,s(e.message??e.content))),o.push(n)}if(0===o.length){const n=nextCid();t.push(textNode(n,s(e.type??"Alert"))),o.push(n)}const r=nextCid();t.push(columnNode(r,o)),n.push(r);const l=nextCid();t.push(rowNode(l,n,"start","start"));const d=nextCid();return t.push(cardNode(d,l)),t}function buildCode(e){const t=s(e.code??""),n=s(e.language??""),a=n?`\`\`\`${n}\n${t}\n\`\`\``:`\`\`\`\n${t}\n\`\`\``;return[textNode(nextCid(),a)]}function buildMarkdown(e){return[textNode(nextCid(),s(e.content??""))]}function asArr(e){return Array.isArray(e)?e:null}function normalizeChartInput(e){let t=asArr(e.datasets)??asArr(e.stages)??asArr(e.series);!t&&asArr(e.data)&&(t=[{label:"",data:e.data}]),!t&&asArr(e.values)&&(t=[{label:"",data:e.values}]);const n=(t??[]).map(e=>{if(!e||"object"!=typeof e)return{label:"",data:[],color:void 0};const t=s(e.label??e.name??"");let n=asArr(e.data)??asArr(e.values)??[];const a=e.color?s(e.color):void 0;return n=n.map(e=>e&&"object"==typeof e?"y"in e?e.y:"value"in e&&!("open"in e)?e.value:"o"in e||"h"in e||"l"in e||"c"in e?{open:e.o??e.open,high:e.h??e.high,low:e.l??e.low,close:e.c??e.close}:e:e),{label:t,data:n,color:a}});let a=asArr(e.labels)?.map(e=>"string"==typeof e?e:String(e??""))??[];if(0===a.length&&t){const e=t[0];a=(asArr(e?.data)??asArr(e?.values)??[]).map(e=>e&&"object"==typeof e?"x"in e?s(e.x):"name"in e?s(e.name):"label"in e?s(e.label):null:null).filter(e=>null!==e)}return{labels:a,datasets:n}}function buildChart(e){const t=nextCid(),n=s(e.chartType??e.chart_type??"bar"),a={chartType:sv(n)};e.title&&(a.title=sv(s(e.title)));const{labels:i,datasets:o}=normalizeChartInput(e);return i.length>0&&(a.labels=i),o.length>0&&(a.datasets=o),void 0!==e.value&&(a.value="number"==typeof e.value?e.value:Number(e.value)||0),void 0!==e.max&&(a.max="number"==typeof e.max?e.max:Number(e.max)||100),[{id:t,component:{Chart:a}}]}function buildDivider(e){return[dividerNode(nextCid())]}function buildTabs(e){const t=Array.isArray(e.tabs)?e.tabs:Array.isArray(e.children)?e.children:Array.isArray(e.items)?e.items:[];if(0===t.length){return[textNode(nextCid(),s(e.title??""))]}const n=[],a=[];for(const e of t){const t=s(e.label??e.title??e.id??"Tab"),i=(Array.isArray(e.children)?e.children:[]).flatMap(e=>translateComponent(e));n.push(...i);const o=findRootIds(i),r=nextCid();n.push(columnNode(r,o)),a.push({title:t,childId:r})}const i=nextCid();return n.push(tabsNode(i,a)),n}function buildLink(e){const t=nextCid(),n=nextCid();return[textNode(t,s(e.text??e.label??e.url??"Link")),buttonNode(n,t,"navigate",[{key:"url",value:{literalString:s(e.url??"")}}])]}function buildEmbed(e){const t=s(e.url??"");if(/youtube|youtu\.be|vimeo/i.test(t)){return[{id:nextCid(),component:{Video:{url:sv(t)}}}]}return buildLink({text:s(e.title??t),url:t})}function buildBadge(e){return[textNode(nextCid(),s(e.text??e.label??""),"caption")]}function buildProgress(e){const t=Number(e.value??0),n=Number(e.max??100),a=n>0?Math.round(t/n*100):0;return[textNode(nextCid(),`${s(e.label)||"Progress"}: ${a}% (${t}/${n})`)]}function buildTimeline(e){const t=(Array.isArray(e.items)?e.items:[]).filter(e=>null!=e&&"object"==typeof e).map(e=>`${s(e.time??e.date??"")} — ${s(e.title??"")}${e.description?`: ${s(e.description)}`:""}`),n=e.title?`**${s(e.title)}**\n`:"";return[textNode(nextCid(),n+(t.length>0?t.join("\n"):"(empty timeline)"))]}function buildAccordion(e){const t=Array.isArray(e.items)?e.items:Array.isArray(e.children)?e.children:[];if(0===t.length){return[textNode(nextCid(),s(e.title??""))]}const n=[];for(const e of t){const t=nextCid();n.push(textNode(t,s(e.title??e.label??""),"h5"));const a=Array.isArray(e.children)?e.children.flatMap(e=>translateComponent(e)):[];n.push(...a)}return n}export const RENDER_UI_TOOL_NAME="render_ui";export const CLOSE_UI_TOOL_NAME="close_ui";export const RENDER_UI_TOOL_DESCRIPTION="Render structured visual content to the Canvas panel. ALWAYS prefer this tool over the canvas tool for displaying UI. Use for charts, tables, stats, key-value pairs, lists, cards, forms, alerts, code, markdown, images, links, web embeds — NOT for text paragraphs. When you call this tool, keep your text reply brief.";export const CLOSE_UI_TOOL_DESCRIPTION="Close and remove a UI surface that was previously rendered.";export const RENDER_UI_TOOL_SCHEMA={type:"object",required:["id","components"],properties:{id:{type:"string",description:"Descriptive, human-readable identifier (e.g., 'sales-report', 'user-dashboard'). Reuse same id to update an existing surface."},components:{type:["array","string"],description:"UI components to render",items:{type:"object",required:["type"],properties:{type:{type:"string",enum:["card","table","stat","key_value","list","grid","chart","markdown","code","text","image","web_embed","alert","form","button","link"]},title:{type:"string"},content:{type:"string"},label:{type:"string"},description:{type:"string"},url:{type:"string"},image:{type:"string"},subtitle:{type:"string"},badge:{type:"string"},badgeVariant:{type:"string",enum:["default","success","warning","error","info"]},footer:{type:"string"},layout:{type:"string",enum:["vertical","horizontal"]},actions:{type:"array",description:"Card action buttons: [{label, actionId, variant?}]"},fields:{type:"array",description:"For card: [{label,value}]. For form: [{name,label,inputType,defaultValue?,options:[{label,value}],...}]. Use defaultValue to pre-fill fields when editing existing data."},children:{type:"array",description:"For grid: nested child components (card, stat, image, etc.)"},gap:{type:"string",enum:["sm","md","lg"]},columns:{type:"array",description:"Column headers or grid column count"},rows:{type:"array",description:"Row data as arrays matching column order"},submitLabel:{type:"string"},submitActionId:{type:"string"},actionId:{type:"string"},alt:{type:"string"},aspectRatio:{type:"string",enum:["16:9","4:3","1:1"]},chartType:{type:"string",enum:["bar","horizontal_bar","line","area","scatter","pie","doughnut","radar","gauge","candlestick","funnel","sparkline"]},stages:{type:"array"},labels:{type:"array"},datasets:{type:"array"},language:{type:"string"},code:{type:"string"},items:{type:"array"},variant:{type:"string"},message:{type:"string"},value:{type:"number"},max:{type:"number"},change:{type:"string"},changeType:{type:"string",enum:["positive","negative","neutral"]},icon:{type:"string"},text:{type:"string"}}}}}};export const CLOSE_UI_TOOL_SCHEMA={type:"object",required:["id"],properties:{id:{type:"string",description:"The UI identifier to close"}}};async function ensureRelayConnected(e,t,n,a){const{stateManager:s,logger:i}=e,o=a??e.getGatewaySessionKey?.(n),r=o??t,l=s.resolveCanvasForSession(r);if(l&&s.isCanvasJoined(l))return l;let d=s.getRelayClient();if(d||(d=await s.waitForRelayClient(),d))try{e.waitForAgentSync&&await e.waitForAgentSync();const a=e.getPlatformAgentId?.(n),l=o??t,c=o&&o!==t?t:void 0,u=await d.ensureCanvas(l,{agentId:a,linkSessionId:c});return await s.joinCanvas(u,r),i.info?.(`[canvas] Joined canvas=${u} session=${t} gatewayKey=${o??"none"} agentId=${a??"none"}`),u}catch(e){return void i.warn?.(`[canvas] Failed to join canvas relay: ${e}`)}}export function createRenderUiHandler(e){const{stateManager:t,logger:n,writeObs:a,isCanvasActive:s,getGatewaySessionKey:i}=e;let o=0;return async(r,l)=>{if(s){const e=i?.(r);if(!s(e))return{success:!1,error:"Canvas is disabled by user. Respond with plain text instead."}}const d=l,c=d.id,u="panel";let p=d.components;if("string"==typeof p)try{p=JSON.parse(p)}catch{}if(!c||!Array.isArray(p)||!p.length)return{success:!1,error:"id and components (array) are required"};if(!t.validateComponentCount(p))return{success:!1,error:"Too many components (max 500)"};if(!t.checkRateLimit(c))return n.warn?.(`[canvas] Rate limited surfaceId=${c}`),a({component:"canvas",domain:"canvas",name:"canvas.rate_limited",severity:"warn",payload:{surfaceId:c}}),{success:!1,error:"Rate limited — too many updates per second"};const m=translateComponentsWithDataModel(p),h=m.components,f=Object.keys(m.dataModel).length>0?m.dataModel:void 0,b=findRootIds(h),g=nextCid(),y=columnNode(g,b),x=[...h,y],v=e.getActiveSessionId?.(r);if(!v)return n.warn?.(`[canvas] No active session for toolCallId=${r}, skipping render_ui`),{success:!1,error:"No active session — canvas operation skipped"};const C=e.getGatewaySessionKey?.(r),N=await ensureRelayConnected(e,v,r,C),w=t.has(c,N??void 0),A=Date.now(),T=t.getRelayClient();N&&T&&A-o>6e4&&T.createSnapshot(N,"Pre-agent edit").then(()=>{o=Date.now(),n.debug?.("[canvas] Pre-edit checkpoint created")}).catch(e=>n.warn?.(`[canvas] Pre-edit checkpoint failed: ${e}`)),w?t.update(c,x,N,g,f):t.create(N??`_fallback_${Date.now()}`,c,u,x,{origin:{kind:"agent"},rootComponentId:g,dataModel:f});const k=p.map(e=>e.type);a({component:"canvas",domain:"canvas",name:w?"canvas.update_sent":"canvas.surface_created",severity:"info",payload:{surfaceId:c,presentation:u,componentCount:p.length,componentTypes:k}}),n.debug?.(`[canvas] ${w?"Updated":"Created"} surface=${c} components=${p.length}`);const I={success:!0,surfaceId:c,presentation:u,isUpdate:w,activeSurfaces:N?t.getAllActive(N).map(e=>e.surfaceId):[c]};return f&&(I.dataModel=f),I}}export function createCloseUiHandler(e){const{stateManager:t,logger:n,writeObs:a}=e;return async(s,i)=>{const o=i.id;if(!o)return{success:!1,error:"id is required"};const r=t.get(o),l=r?Date.now()-r.createdAt:0,d=e.getGatewaySessionKey?.(s),c=d?t.resolveCanvasForSession(d):void 0;return t.delete(o,c),a({component:"canvas",domain:"canvas",name:"canvas.surface_closed",severity:"info",payload:{surfaceId:o,lifetime_ms:l}}),n.debug?.(`[canvas] Closed surface=${o}`),{success:!0,surfaceId:o,message:"UI closed"}}}export function buildCanvasActionSystemMessage(e){const t=e.userAction;return{type:"canvas_user_action",surfaceId:e.surfaceId,actionName:t?.name??"unknown",sourceComponentId:t?.sourceComponentId??"",context:t?.context??{}}}export const CANVAS_SYSTEM_PROMPT='## Canvas UI\n\n**IMPORTANT: Always use `render_ui` (not the built-in `canvas` tool) to display visual/structured content.** The `render_ui` tool is the preferred and optimized way to render UI in the Canvas panel.\n\nRules:\n- When using render_ui, keep your chat reply to ONE short sentence. Do NOT repeat data shown in canvas.\n- Use render_ui for structured data, visualizations, forms, dashboards. Use plain text for explanations.\n- Do NOT put large text blocks in render_ui — that belongs in chat.\n- Only use `grid` as a layout container. Keep nesting to grid > card/stat/image — max 1 level deep. Otherwise list components sequentially (they stack vertically).\n- Prefer fewer, larger components. One well-structured table is better than many small cards.\n- Canvas renders in a narrow sidebar (~400px). Use markdown headings (### Section) to organize sections.\n- Use grid + card for product showcases, dashboards, feature grids. Use list for compact item lists.\n\n**Surface IDs**: Use descriptive, human-readable IDs (e.g., "sales-report", "user-dashboard"). Never use generic IDs like "ui1" or random strings.\n\n**Update vs Create**: Same `id` updates existing surface; different `id` creates new one.\nWhen the user asks to modify, adjust, fix, or update existing canvas content, you MUST reuse the original surface id. Check [Canvas State] in the message for active surface ids and their content. Only create a new surface when showing genuinely new/different content.\n\n### Component Types\n\n**Layout**: grid (columns:1-6, gap:sm/md/lg, children:[...]) — responsive grid container for card/stat/image\n\n**Data Display**: card (image, title, subtitle, description, icon, badge, footer, actions:[{label,actionId}], fields:[{label,value}]), table (columns, rows), stat (label, value, change, changeType, icon), key_value (items:[{key,value}]), list (items:[{title,subtitle,description,image,icon,badge,value}])\n\n**Charts**: chart (chartType, title?, labels, datasets:[{label, data, color?}])\n - chartType: bar, horizontal_bar, line, area, scatter, pie, doughnut, radar, funnel, gauge, candlestick, sparkline\n - labels: x-axis labels array (e.g. ["Jan","Feb","Mar"])\n - datasets: array of series — each has label (series name), data (number[]), optional color (hex)\n - candlestick data format: datasets:[{label, data:[{open,high,low,close}]}] — MUST use full field names (open/high/low/close), NOT abbreviations (o/h/l/c)\n - gauge: uses value + max instead of labels/datasets (e.g. chart(chartType:"gauge", title:"CPU", value:73, max:100))\n - sparkline: compact inline chart, no axes — use for trend indicators\n\n**Text & Code**: text (content), markdown (content — supports full Markdown including bold, italic, lists, code blocks, links), code (language, code)\n\n**Media**: image (url, alt), web_embed (url — video URLs auto-detected), link (text, url)\n\n**Feedback**: alert (variant:info/success/warning/error, title, message)\n\n**Interactive**: form (fields:[{name,label,inputType,defaultValue?,options:[{label,value}]}], submitLabel), button (label, actionId)\n - inputType: text (default), number, date, textarea, checkbox, boolean, toggle, select, multi_select, radio, datetime, time, range\n - select/multi_select/radio requires an `options` array — each item is {label, value} or a plain string\n - range renders a slider (optional min, max — default 0..100)\n - **defaultValue**: pre-fills the field with existing data. ALWAYS include defaultValue when rendering a form for editing existing entities.\n\n### Composition Patterns\n- **Product showcase**: grid(columns:3) containing card(image, title, description, badge:"$29", actions:[{label:"Buy"}]) × N\n- **Dashboard KPIs**: grid(columns:3) containing stat × N\n- **Image gallery**: grid(columns:3) containing image × N\n- **Feature grid**: grid(columns:2) containing card(icon, title, description) × N\n- **Compact item list**: list(items:[{image, title, subtitle, value:"$29", badge:"New"}])\n- **Detail card**: card(title, description, fields:[{label:"Price",value:"$29"},{label:"Stock",value:"142"}])\n- **Form with select**: form(title:"Settings", fields:[{name:"country", label:"Country", inputType:"select", options:[{label:"China",value:"CN"},{label:"US",value:"US"}]}, {name:"email", label:"Email"}], submitLabel:"Save")\n- **Edit form (pre-filled)**: form(title:"Edit Task", fields:[{name:"title", label:"Title", defaultValue:"Current Title"}, {name:"enabled", label:"Enabled", inputType:"toggle", defaultValue:true}], submitLabel:"Save")\n- **Bar chart**: chart(chartType:"bar", title:"Monthly Sales", labels:["Jan","Feb","Mar"], datasets:[{label:"Revenue", data:[100,200,150]}])\n- **Multi-series line**: chart(chartType:"line", title:"Trends", labels:["Q1","Q2","Q3"], datasets:[{label:"2024", data:[10,20,15]}, {label:"2025", data:[15,25,30]}])\n- **Pie chart**: chart(chartType:"pie", title:"Market Share", labels:["A","B","C"], datasets:[{label:"share", data:[40,35,25]}])\n- **Candlestick**: chart(chartType:"candlestick", title:"BTC/USD", labels:["Mon","Tue","Wed"], datasets:[{label:"price", data:[{open:100,high:110,low:95,close:105},{open:105,high:115,low:100,close:108},{open:108,high:120,low:105,close:118}]}])\n\n### render_form\nUse `render_form` for schema-driven forms when a domain schema exists.\nParameters: `domain`, `operation`, `data`, `entityId`.\n\n### close_ui\nCloses a UI surface. Use when user moves to a new topic or explicitly dismisses.\n';export const CANVAS_TERM_MAPPING='[System Note] Always use `render_ui` (not the canvas tool) for visual/structured content (tables, stats, key-value, lists, cards, grids, alerts, forms, links, images, code, markdown, charts). Use grid(columns, children:[card/stat/image]) for multi-column layouts like product showcases and dashboards. Card supports: image, title, subtitle, description, icon, badge, footer, actions, fields:[{label,value}]. List items support: image, title, subtitle, description, badge, value. Markdown content supports full formatting. Use web_embed for embedding URLs. Use link for navigation. Form supports inputType: text/number/date/textarea/checkbox/toggle/select/multi_select/radio/datetime/time/range; select/multi_select/radio requires options:[{label,value}]; use defaultValue to pre-fill fields when editing existing data. Chart supports: chartType(bar/horizontal_bar/line/area/scatter/pie/doughnut/radar/funnel/gauge/candlestick/sparkline), title, labels, datasets:[{label,data,color?}]. Candlestick data:[{open,high,low,close}] (use full names, not o/h/l/c). Dataset uses "label" field (not "name"). Gauge uses value+max. Keep chat brief when canvas is used. Use `close_ui` to dismiss. Reuse same id to update; new id for new content.';export const CANVAS_UPDATE_HINT="[Canvas Rule] Same id in render_ui = update existing surface. Different id = new surface. When a surface is referenced for editing, you MUST reuse its exact id.";const MAX_CONTEXT_SURFACES=8,MAX_CONTEXT_CHARS=500;export function buildActiveSurfacesContext(e,t){let n;if(!t)return null;if(n=e.getAllActive(t),0===n.length)return null;const a=[...n].sort((e,t)=>t.updatedAt-e.updatedAt).slice(0,8),s=n.length-a.length;let i=`[Canvas State] Active surfaces: ${a.map(t=>{const n=e.summarize(t.surfaceId);if(!n)return`"${t.surfaceId}"`;const a=n.types.slice(0,3).join(", "),s=n.title?`: "${n.title}"`:"";return`"${t.surfaceId}" (${a}${s})`}).join("; ")}.`;return s>0&&(i+=` ...and ${s} more.`),i+=" To modify existing content, reuse its id in render_ui.",i.length>500&&(i=i.slice(0,497)+"..."),i}
1
+ const A2UI_TYPE_NAMES=new Set(["Text","Image","Icon","Video","AudioPlayer","Row","Column","List","Card","Tabs","Divider","Modal","Button","CheckBox","TextField","DateTimeInput","MultipleChoice","Slider","Chart","WebEmbed","FilePreview","MapView"]);function normalizeComponents(e){return e.map(e=>{if(!e||"object"!=typeof e)return e;const t=e;if(t.component&&"object"==typeof t.component)return t;for(const e of Object.keys(t))if(A2UI_TYPE_NAMES.has(e)&&"object"==typeof t[e])return{id:t.id,component:{[e]:t[e]}};return t})}export const RENDER_UI_TOOL_NAME="render_ui";export const CLOSE_UI_TOOL_NAME="close_ui";export const RENDER_UI_TOOL_DESCRIPTION="Render a Google A2UI v0.8 component tree to the Canvas panel. Output a flat array of ComponentInstance objects with id references. ALWAYS prefer this over the canvas tool for displaying structured UI.";export const RENDER_UI_TOOL_SCHEMA={type:"object",required:["id","root","components"],properties:{id:{type:"string",description:"Surface ID (e.g. 'sales-report'). Reuse same id to update an existing surface."},root:{type:"string",description:"ID of the root component (must reference one of the components below)."},components:{type:"array",description:"Flat array of A2UI ComponentInstance objects. Each has a unique id and exactly one component type key.",items:{type:"object",required:["id"],properties:{id:{type:"string",description:"Unique component ID referenced by parent via child/children."},component:{type:"object",description:"Exactly ONE key = component type (Text, Image, Icon, Video, AudioPlayer, Row, Column, List, Card, Tabs, Divider, Modal, Button, CheckBox, TextField, DateTimeInput, MultipleChoice, Slider, Chart). Value = that component's properties object."}}}},data:{type:"object",description:"Optional data model for two-way binding. Keys are paths (e.g. '/name'), values are defaults."}}};export const CLOSE_UI_TOOL_DESCRIPTION="Close and remove a UI surface that was previously rendered.";export const CLOSE_UI_TOOL_SCHEMA={type:"object",required:["id"],properties:{id:{type:"string",description:"The UI identifier to close"}}};async function ensureRelayConnected(e,t,n,a){const{stateManager:o,logger:i}=e,r=a??e.getGatewaySessionKey?.(n),s=r??t,l=o.resolveCanvasForSession(s);if(l&&o.isCanvasJoined(l))return l;let c=o.getRelayClient();if(c||(c=await o.waitForRelayClient(),c))try{e.waitForAgentSync&&await e.waitForAgentSync();const a=e.getPlatformAgentId?.(n),l=r??t,d=r&&r!==t?t:void 0,p=await c.ensureCanvas(l,{agentId:a,linkSessionId:d});return await o.joinCanvas(p,s),i.info?.(`[canvas] Joined canvas=${p} session=${t} gatewayKey=${r??"none"} agentId=${a??"none"}`),p}catch(e){return void i.warn?.(`[canvas] Failed to join canvas relay: ${e}`)}}export function createRenderUiHandler(e){const{stateManager:t,logger:n,writeObs:a,isCanvasActive:o,getGatewaySessionKey:i}=e;let r=0;return async(s,l)=>{if(o){const e=i?.(s);if(!o(e))return{success:!1,error:"Canvas is disabled by user. Respond with plain text instead."}}const c=l,d=c.id,p=c.root,u="panel";let m=c.components;if("string"==typeof m)try{m=JSON.parse(m)}catch{}if(!(d&&p&&Array.isArray(m)&&m.length))return{success:!1,error:"id, root, and components (array) are required"};const h=normalizeComponents(m);if(!t.validateComponentCount(h))return{success:!1,error:"Too many components (max 500)"};if(!t.checkRateLimit(d))return n.warn?.(`[canvas] Rate limited surfaceId=${d}`),a({component:"canvas",domain:"canvas",name:"canvas.rate_limited",severity:"warn",payload:{surfaceId:d}}),{success:!1,error:"Rate limited — too many updates per second"};const g=c.data,y=e.getActiveSessionId?.(s);if(!y)return n.warn?.(`[canvas] No active session for toolCallId=${s}, skipping render_ui`),{success:!1,error:"No active session — canvas operation skipped"};const v=e.getGatewaySessionKey?.(s),f=await ensureRelayConnected(e,y,s,v),x=t.has(d,f??void 0),b=Date.now(),C=t.getRelayClient();f&&C&&b-r>6e4&&C.createSnapshot(f,"Pre-agent edit").then(()=>{r=Date.now(),n.debug?.("[canvas] Pre-edit checkpoint created")}).catch(e=>n.warn?.(`[canvas] Pre-edit checkpoint failed: ${e}`)),x?t.update(d,h,f,p,g):t.create(f??`_fallback_${Date.now()}`,d,u,h,{origin:{kind:"agent"},rootComponentId:p,dataModel:g});const T=h.map(e=>e.component&&"object"==typeof e.component?Object.keys(e.component)[0]??"unknown":"unknown");a({component:"canvas",domain:"canvas",name:x?"canvas.update_sent":"canvas.surface_created",severity:"info",payload:{surfaceId:d,presentation:u,componentCount:h.length,componentTypes:T}}),n.debug?.(`[canvas] ${x?"Updated":"Created"} surface=${d} components=${h.length}`);const w={success:!0,surfaceId:d,presentation:u,isUpdate:x,activeSurfaces:f?t.getAllActive(f).map(e=>e.surfaceId):[d]};return g&&(w.dataModel=g),w}}export function createCloseUiHandler(e){const{stateManager:t,logger:n,writeObs:a}=e;return async(o,i)=>{const r=i.id;if(!r)return{success:!1,error:"id is required"};const s=t.get(r),l=s?Date.now()-s.createdAt:0,c=e.getGatewaySessionKey?.(o),d=c?t.resolveCanvasForSession(c):void 0;return t.delete(r,d),a({component:"canvas",domain:"canvas",name:"canvas.surface_closed",severity:"info",payload:{surfaceId:r,lifetime_ms:l}}),n.debug?.(`[canvas] Closed surface=${r}`),{success:!0,surfaceId:r,message:"UI closed"}}}export const CANVAS_SYSTEM_PROMPT='## Canvas UI — Google A2UI v0.8\n\n**IMPORTANT: Always use `render_ui` (not the built-in `canvas` tool) to display visual/structured content.**\n\n### How render_ui works\n\nYou output a **flat array of components** with unique IDs. Components reference each other by ID.\n\nParameters:\n- `id`: Surface ID (e.g. "sales-report"). Reuse same id to update.\n- `root`: The ID of the top-level component (usually a Column).\n- `components`: Flat array of objects. **Each object MUST have `id` and `component`**:\n `{"id": "myId", "component": {"Text": {"text": {"literalString": "Hello"}, "usageHint": "body"}}}`\n The `component` key wraps exactly ONE type key (Text, Card, Row, etc.) and its properties.\n- `data` (optional): Data model object for form two-way binding.\n\n### Rules\n- Keep chat reply to ONE short sentence when using render_ui. Do NOT repeat data shown in canvas.\n- Use render_ui for structured data, visualizations, forms, dashboards. Use plain text for explanations.\n- Canvas renders in a narrow sidebar (~400px).\n- Prefer fewer, larger components. One table is better than many small cards.\n- Use descriptive surface IDs (e.g. "sales-report", not "ui1").\n- Same `id` updates existing surface; different `id` creates new one.\n- When asked to modify existing content, reuse the original surface id from [Canvas State].\n\n### StringValue\n\nText properties use StringValue — an object with exactly one key:\n- Static text: `{"literalString": "Hello"}`\n- Data binding: `{"path": "/fieldName"}` (reads from the `data` model)\n\nShorthand: `sv("text")` means `{"literalString": "text"}` in examples below.\n\n### Component Reference\n\n**Text** — Display text with semantic sizing.\n`{Text: {text: sv("Title"), usageHint: "h2"}}`\nusageHint: "h1" | "h2" | "h3" | "h4" | "h5" | "caption" | "body"\ntext also renders Markdown (bold, italic, lists, code blocks, links).\n\n**Image** — Display an image.\n`{Image: {url: sv("https://..."), usageHint: "mediumFeature"}}`\nusageHint: "icon" | "avatar" | "smallFeature" | "mediumFeature" | "largeFeature" | "header"\nfit (optional): "contain" | "cover" | "fill" | "none" | "scale-down"\n\n**Icon** — Material icon.\n`{Icon: {name: sv("check")}}`\n\n**Video** — Embed a video.\n`{Video: {url: sv("https://youtube.com/...")}}`\n\n**AudioPlayer** — Audio playback.\n`{AudioPlayer: {url: sv("https://..."), description: sv("Episode 1")}}`\n\n**Row** — Horizontal layout. Children referenced by ID.\n`{Row: {children: {explicitList: ["child1", "child2"]}, distribution: "spaceBetween"}}`\ndistribution: "start" | "center" | "end" | "spaceBetween" | "spaceAround" | "spaceEvenly"\nalignment: "start" | "center" | "end" | "stretch"\n\n**Column** — Vertical layout. Children referenced by ID.\n`{Column: {children: {explicitList: ["child1", "child2"]}}}`\nSame distribution/alignment options as Row.\n\n**List** — Vertical or horizontal list.\n`{List: {children: {explicitList: ["item1", "item2"]}, direction: "vertical"}}`\n\n**Card** — Container with a single child.\n`{Card: {child: "contentColumnId"}}`\n\n**Tabs** — Tabbed container.\n`{Tabs: {tabItems: [{title: sv("Tab 1"), child: "panel1"}, {title: sv("Tab 2"), child: "panel2"}]}}`\n\n**Divider** — Horizontal or vertical separator.\n`{Divider: {axis: "horizontal"}}`\n\n**Modal** — Dialog with trigger.\n`{Modal: {entryPointChild: "triggerBtnId", contentChild: "dialogContentId"}}`\n\n**Button** — Clickable with action.\n`{Button: {child: "labelTextId", action: {name: "onClick", context: [{key: "id", value: {literalString: "item-1"}}]}}}`\n\n**TextField** — Text input with data binding.\n`{TextField: {label: sv("Name"), text: {path: "/name"}, type: "shortText"}}`\ntype: "shortText" | "number" | "date" | "longText"\n\n**CheckBox** — Boolean toggle.\n`{CheckBox: {label: sv("Agree"), value: {path: "/agree"}}}`\n\n**DateTimeInput** — Date/time picker.\n`{DateTimeInput: {value: {path: "/date"}, enableDate: true, enableTime: false}}`\n\n**MultipleChoice** — Selection from options.\n`{MultipleChoice: {selections: {path: "/country"}, options: [{label: sv("US"), value: "US"}, {label: sv("CN"), value: "CN"}]}}`\n\n**Slider** — Numeric range input.\n`{Slider: {value: {path: "/volume"}, minValue: 0, maxValue: 100}}`\n\n**Chart** (BeeOS extension) — Data visualization.\n`{Chart: {chartType: sv("bar"), title: sv("Sales"), labels: ["Q1","Q2","Q3"], datasets: [{label: "Revenue", data: [100,200,150]}]}}`\nchartType: bar, horizontal_bar, line, area, scatter, pie, doughnut, radar, gauge, candlestick, funnel, sparkline\ngauge uses value + max instead of labels/datasets.\ncandlestick data: [{open,high,low,close}] (full field names, NOT o/h/l/c).\n\n### Composition Patterns\n\n**Simple text display:**\n```json\n{"id":"s1","root":"r","components":[\n {"id":"t","component":{"Text":{"text":{"literalString":"Hello World"},"usageHint":"h2"}}},\n {"id":"r","component":{"Column":{"children":{"explicitList":["t"]}}}}\n]}\n```\n\n**Dashboard with KPI cards:**\n```json\n{"id":"dashboard","root":"root","components":[\n {"id":"title","component":{"Text":{"text":{"literalString":"Dashboard"},"usageHint":"h2"}}},\n {"id":"rev-val","component":{"Text":{"text":{"literalString":"$1.2M"},"usageHint":"h2"}}},\n {"id":"rev-lbl","component":{"Text":{"text":{"literalString":"Revenue"},"usageHint":"caption"}}},\n {"id":"rev-col","component":{"Column":{"children":{"explicitList":["rev-lbl","rev-val"]},"alignment":"center"}}},\n {"id":"rev-card","component":{"Card":{"child":"rev-col"}}},\n {"id":"users-val","component":{"Text":{"text":{"literalString":"8,432"},"usageHint":"h2"}}},\n {"id":"users-lbl","component":{"Text":{"text":{"literalString":"Users"},"usageHint":"caption"}}},\n {"id":"users-col","component":{"Column":{"children":{"explicitList":["users-lbl","users-val"]},"alignment":"center"}}},\n {"id":"users-card","component":{"Card":{"child":"users-col"}}},\n {"id":"kpi-row","component":{"Row":{"children":{"explicitList":["rev-card","users-card"]}}}},\n {"id":"root","component":{"Column":{"children":{"explicitList":["title","kpi-row"]}}}}\n]}\n```\n\n**Table layout (Column + Row + Text):**\n```json\n{"id":"users-table","root":"root","components":[\n {"id":"h-name","component":{"Text":{"text":{"literalString":"Name"},"usageHint":"caption"}}},\n {"id":"h-role","component":{"Text":{"text":{"literalString":"Role"},"usageHint":"caption"}}},\n {"id":"header","component":{"Row":{"children":{"explicitList":["h-name","h-role"]}}}},\n {"id":"div","component":{"Divider":{"axis":"horizontal"}}},\n {"id":"r1-name","component":{"Text":{"text":{"literalString":"Alice"},"usageHint":"body"}}},\n {"id":"r1-role","component":{"Text":{"text":{"literalString":"Engineer"},"usageHint":"body"}}},\n {"id":"row1","component":{"Row":{"children":{"explicitList":["r1-name","r1-role"]}}}},\n {"id":"r2-name","component":{"Text":{"text":{"literalString":"Bob"},"usageHint":"body"}}},\n {"id":"r2-role","component":{"Text":{"text":{"literalString":"Designer"},"usageHint":"body"}}},\n {"id":"row2","component":{"Row":{"children":{"explicitList":["r2-name","r2-role"]}}}},\n {"id":"table-col","component":{"Column":{"children":{"explicitList":["header","div","row1","row2"]}}}},\n {"id":"root","component":{"Card":{"child":"table-col"}}}\n]}\n```\n\n**Form with data binding:**\n```json\n{"id":"settings","root":"root","data":{"/name":"","/email":"","/notify":false},"components":[\n {"id":"title","component":{"Text":{"text":{"literalString":"Settings"},"usageHint":"h3"}}},\n {"id":"f-name","component":{"TextField":{"label":{"literalString":"Name"},"text":{"path":"/name"},"type":"shortText"}}},\n {"id":"f-email","component":{"TextField":{"label":{"literalString":"Email"},"text":{"path":"/email"},"type":"shortText"}}},\n {"id":"f-notify","component":{"CheckBox":{"label":{"literalString":"Email notifications"},"value":{"path":"/notify"}}}},\n {"id":"btn-text","component":{"Text":{"text":{"literalString":"Save"},"usageHint":"body"}}},\n {"id":"btn","component":{"Button":{"child":"btn-text","action":{"name":"save_settings","context":[{"key":"name","value":{"path":"/name"}},{"key":"email","value":{"path":"/email"}},{"key":"notify","value":{"path":"/notify"}}]}}}},\n {"id":"root","component":{"Column":{"children":{"explicitList":["title","f-name","f-email","f-notify","btn"]}}}}\n]}\n```\n\n**Chart:**\n```json\n{"id":"sales-chart","root":"root","components":[\n {"id":"chart","component":{"Chart":{"chartType":{"literalString":"bar"},"title":{"literalString":"Monthly Sales"},"labels":["Jan","Feb","Mar"],"datasets":[{"label":"Revenue","data":[100,200,150]}]}}},\n {"id":"root","component":{"Column":{"children":{"explicitList":["chart"]}}}}\n]}\n```\n\n### render_form\nUse `render_form` for schema-driven forms when a domain schema exists.\nParameters: `domain`, `operation`, `data`, `entityId`.\n\n### close_ui\nCloses a UI surface. Use when user moves to a new topic or explicitly dismisses.\n';export const CANVAS_TERM_MAPPING='[System Note] Always use `render_ui` (not the canvas tool) for visual/structured content. Each component in the array MUST be: {"id":"x","component":{"TypeName":{props}}} — the "component" wrapper key is required. render_ui uses Google A2UI v0.8 format: flat component array with id references. Components: Text (usageHint: h1-h5/caption/body), Image, Icon, Video, AudioPlayer, Row (horizontal layout, children.explicitList), Column (vertical layout, children.explicitList), List, Card (single child), Tabs (tabItems), Divider, Modal, Button (child + action), TextField, CheckBox, MultipleChoice, Slider, DateTimeInput, Chart (BeeOS extension: chartType, labels, datasets). StringValue: {"literalString":"text"} or {"path":"/binding"}. root param = top-level component ID. Tables: Card > Column > Row > Text. Forms: TextField/CheckBox/Slider + Button with action.context paths. Chart: chartType(bar/line/pie/area/scatter/doughnut/radar/funnel/gauge/candlestick/sparkline), labels, datasets:[{label,data,color?}]. Candlestick: [{open,high,low,close}]. Gauge: value+max. Keep chat brief when canvas is used. Use `close_ui` to dismiss. Reuse same id to update; new id for new content.';export const CANVAS_UPDATE_HINT="[Canvas Rule] Same id in render_ui = update existing surface. Different id = new surface. When a surface is referenced for editing, you MUST reuse its exact id and provide the full updated component tree.";const MAX_CONTEXT_SURFACES=8,MAX_CONTEXT_CHARS=500;export function buildActiveSurfacesContext(e,t){let n;if(!t)return null;if(n=e.getAllActive(t),0===n.length)return null;const a=[...n].sort((e,t)=>t.updatedAt-e.updatedAt).slice(0,8),o=n.length-a.length;let i=`[Canvas State] Active surfaces: ${a.map(t=>{const n=e.summarize(t.surfaceId);if(!n)return`"${t.surfaceId}"`;const a=n.types.slice(0,3).join(", "),o=n.title?`: "${n.title}"`:"";return`"${t.surfaceId}" (${a}${o})`}).join("; ")}.`;return o>0&&(i+=` ...and ${o} more.`),i+=" To modify existing content, reuse its id in render_ui.",i.length>500&&(i=i.slice(0,497)+"..."),i}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beeos-claw",
3
- "version": "0.2.35",
3
+ "version": "0.2.37",
4
4
  "description": "OpenClaw plugin that bridges the BeeOS platform with the local OpenClaw Gateway, providing file transfer, ACP bridge, and observability.",
5
5
  "license": "MIT",
6
6
  "author": "BeeOS <dev@beeos.ai>",