beeos-claw 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/acp-gateway/bridge-core.js +1 -1
- package/dist/acp-gateway/gateway-events.js +1 -1
- package/dist/acp-gateway/history-replay.js +1 -1
- package/dist/acp-gateway/session-state.js +1 -1
- package/dist/acp-gateway/transport.js +1 -1
- package/dist/acp-server.js +1 -1
- package/dist/action-tool-generator.js +1 -0
- package/dist/canvas-action-router.js +1 -0
- package/dist/canvas-api-client.js +1 -0
- package/dist/canvas-constants.js +1 -0
- package/dist/canvas-state.js +1 -0
- package/dist/canvas-tools.js +1 -0
- package/dist/canvas-ws-provider.js +1 -0
- package/dist/config.js +1 -1
- package/dist/cron-resource.js +1 -0
- package/dist/cron-schemas.js +1 -0
- package/dist/form-schema-registry.js +1 -0
- package/dist/form-surface-builder.js +1 -0
- package/dist/gateway-client.js +1 -1
- package/dist/index.js +1 -1
- package/dist/render-form-tool.js +1 -0
- package/dist/resource-descriptor-registry.js +1 -0
- package/dist/resource-descriptor.js +1 -0
- package/dist/resource-surface-renderer.js +1 -0
- package/package.json +2 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
import{sanitizePromptPayload}from"../message-filter.js";import{MessageQueue}from"../utils/mq.js";import{extractPromptBlocks,toGatewayPrompt,resolveSlashCommandName as resolveSlashCmd}from"./prompt-converter.js";import{loadHistory}from"./history-replay.js";import{appendHistoryEntry}from"./local-session-history.js";import{buildResolutionPlan,resolveResources,applyResolutions}from"./prompt-resource-resolver.js";import{ACP_PROTOCOL_VERSION,DEFAULT_PROMPT_TIMEOUT_MS,DEFAULT_LOAD_REPLAY_IDLE_TIMEOUT_MS,DEFAULT_LOAD_REPLAY_HARD_TIMEOUT_MS}from"./types.js";const MAIN_SESSION_KEY="agent:main:main",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}`}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 i=s.match(/^\/([^\s:]+)(?::|\s|$)/)?.[1]?.trim();return i&&!i.includes("/")&&SLASH_COMMAND_NAME_RE.test(i)?i.toLowerCase():void 0}export class AcpGatewayBridgeCore{state;transport;config;logger;writeObs;getGateway;initialized=!1;handleTerminalRequest=null;handleTerminalNotification=null;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}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/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"session/cancel":this.handleSessionCancel(e.id,e.params);break;default:e.method.startsWith("terminal/")&&this.handleTerminalRequest?await this.handleTerminalRequest(e):this.transport.sendError(e.id,-32601,`Method not found: ${e.method}`)}}catch(t){this.transport.sendError(e.id,-32603,t instanceof Error?t.message:"Internal error")}}handleNotification(e){if("session/cancel"===e.method)this.handleSessionCancel(void 0,e.params);else 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},mcp:{}},agentInfo:{name:"beeos-claw",title:"BeeOS Agent Bridge",version:"0.1.0"},_meta:{},authMethods:[]})}async handleSessionNew(e){const t=MAIN_SESSION_KEY,s="string"==typeof e.params?.cwd?e.params.cwd:".";let i=this.state.sessions.get(t);i?i.cwd=s:(i={sessionId:t,gatewaySessionKey:t,createdAt:Date.now(),chatReplayDedupKeys:new Set,cwd:s},this.state.sessions.set(t,i),this.state.sessionsByGatewayKey.set(t,i)),this.transport.sendResult(e.id,{sessionId:t,modes:{availableModes:[{...DEFAULT_MODE}],currentModeId:DEFAULT_MODE.id},_meta:{sessionKey:t}}),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 handleSessionLoad(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendError(e.id,-32602,"sessionId required");let s=this.state.sessions.get(t);s||(s={sessionId:t,gatewaySessionKey:MAIN_SESSION_KEY,createdAt:Date.now(),chatReplayDedupKeys:new Set},this.state.sessions.set(t,s),this.state.sessionsByGatewayKey.set(s.gatewaySessionKey,s));try{const e=this.getGateway(),i=await loadHistory(e,s.gatewaySessionKey,50);for(const e of i){const i=e.dedupKey??`${e.role}:${e.content.slice(0,64)}`;if(s.chatReplayDedupKeys.has(i))continue;s.chatReplayDedupKeys.add(i);const a=e.updateType??("user"===e.role?"user_message_chunk":"agent_message_chunk");"tool_call"===a&&e.toolCallId?this.transport.sendSessionUpdate(t,{sessionUpdate:"tool_call",toolCallId:e.toolCallId,title:e.toolName??"tool",kind:"other",status:"completed"}):this.transport.sendSessionUpdate(t,{sessionUpdate:a,content:{type:"text",text:e.content}})}}catch(e){this.logger.warn?.("[bridge-core] Failed to load session history:",e)}s.assistantMq&&!s.assistantMq.closed&&await this.replayCurrentAssistantStream(s),this.transport.sendResult(e.id,{sessionId:t}),this.pushSkillsList().catch(()=>{})}async replayCurrentAssistantStream(e){if(!e.assistantMq||e.assistantMq.closed)return;const t=this.config.loadReplayIdleTimeoutMs??DEFAULT_LOAD_REPLAY_IDLE_TIMEOUT_MS,s=Date.now()+DEFAULT_LOAD_REPLAY_HARD_TIMEOUT_MS;let i=0;for(;Date.now()<s&&(e.assistantStreamOwnerRunId||e.pendingPromptId)&&!e.assistantMq.closed;){const s=await Promise.race([e.assistantMq.read(i),new Promise(e=>setTimeout(()=>e(null),t))]);if(!s||0===s.length){if(void 0!==e.pendingPromptId&&!e.promptDone)continue;break}for(const t of s)this.transport.sendSessionUpdate(e.sessionId,t),i++}i>0&&this.writeObs({component:"bridge-core",domain:"lifecycle",name:"session.load.replay_complete",severity:"debug",payload:{sessionId:e.sessionId,replayed:i}})}async handleSessionList(e){const t=[];for(const[,e]of this.state.sessions)t.push({sessionId:e.sessionId,title:e.title||e.sessionId,cwd:e.cwd,updatedAt:e.createdAt});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){t.find(t=>this.state.sessions.get(t.sessionId)?.gatewaySessionKey===e.sessionKey)||t.push({sessionId:e.sessionKey,title:e.title||e.sessionKey,cwd:e.cwd,updatedAt:e.updatedAt})}}}catch{}this.transport.sendResult(e.id,{sessions:t})}async handleSessionSetMode(e){const t=e.params?.sessionId,s=e.params?.mode,i=t?this.state.sessions.get(t):void 0,a=this.getGateway();if(i&&a?.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 a.sessionsPatch(i.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,i=t?this.state.sessions.get(t):void 0,a=this.getGateway();if(i&&a?.isConnected&&s)try{await a.sessionsPatch(i.gatewaySessionKey,{model:s})}catch(e){this.logger.warn?.(`[bridge-core] sessions.patch for model failed: ${e instanceof Error?e.message:e}`)}this.transport.sendResult(e.id,{model:s??"default"})}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})):[]}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}})}async handleSessionPrompt(e){const t=e.params?.sessionId,s=e.params?.prompt;if(!t||!s)return void this.transport.sendError(e.id,-32602,"sessionId and prompt required");let i=this.state.sessions.get(t);i||(i={sessionId:t,gatewaySessionKey:MAIN_SESSION_KEY,createdAt:Date.now(),chatReplayDedupKeys:new Set},this.state.sessions.set(t,i),this.state.sessionsByGatewayKey.set(i.gatewaySessionKey,i));const a=this.getGateway();if(!a?.isConnected)return void this.transport.sendError(e.id,-32001,"Gateway not connected");let o=extractPromptBlocks(sanitizePromptPayload(s));const n=buildResolutionPlan(o);if(n.refs.length>0)try{const e=await resolveResources(n,this.writeObs);o=applyResolutions(o,e)}catch(e){this.logger.warn?.(`[bridge-core] Resource pre-resolution failed: ${e instanceof Error?e.message:e}`)}const r=toGatewayPrompt(o);if("error"in r)return void this.transport.sendError(e.id,-32602,r.error);const{message:d,attachments:l}=r,m=resolveStandaloneSlashCommandName(o,d)??resolveSlashCmd(o,d),c="string"==typeof m,p=c?"chat.send":"agent",h=c?d:withUserMessagePrefix(d);c&&this.logger.info?.(`[bridge-core] slash command passthrough sessionId=${t} command=/${m}`),i.currentRunId&&this.state.promptsByRunId.delete(i.currentRunId),i.currentRunId=void 0,i.assistantStreamOwnerRunId=void 0,i.pendingPromptId=e.id,i.promptDone=!1,i.gatewayMethod=p,appendHistoryEntry(i.gatewaySessionKey,{ts:Date.now(),role:"user",content:d}),i.assistantMq?.close(),i.assistantMq=new MessageQueue,i.chatReplayDedupKeys.clear(),i.hasAgentAssistantStream=!1,i.hasAgentThinkingStream=!1,i.hasAgentToolStartStream=!1,i.hasAgentToolResultStream=!1,i.chatAssistantTextSoFar=void 0,i.agentAssistantTextSoFar=void 0,this.emitPromptAsUserSessionUpdates(i.sessionId,o),this.transport.sendSessionUpdate(t,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:""}}),this.schedulePromptTimeout(i);try{const e=l.length>0?l.map(e=>({type:e.type,content:e.content,...e.mimeType?{mimeType:e.mimeType}:{},...e.fileName?{fileName:e.fileName}:{}})):void 0;let t;t=c?await a.chatSend(i.gatewaySessionKey,h,e):await a.agentSend(i.gatewaySessionKey,h,this.config.gateway.agentId||"main",e),i.currentRunId=t.runId,i.assistantStreamOwnerRunId=t.runId,i.assistantStreamStartedAt=Date.now(),this.state.promptsByRunId.set(t.runId,i)}catch(t){this.state.clearPromptTimeout(i),void 0!==i.pendingPromptId&&(i.pendingPromptId=void 0,i.assistantMq?.close(),this.transport.sendError(e.id,-32001,t instanceof Error?t.message:"failed to send prompt to gateway"))}}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,i=t.mimeType??t.mime_type??e.mimeType??e.mime_type,a=t.text??e.text,o=t.data??e.data,n=t.fileName??t.file_name??t.filename??t.name??e.fileName??e.file_name??e.filename??e.name;if(!s&&!a&&!o)return;return{type:"resource",resource:{...s?{uri:s}:{},...i?{mimeType:i}:{},...a?{text:a}:{},...o?{data:o}:{},...n?{fileName:n}:{}}}}}handleSessionCancel(e,t){const s=t?.sessionId;if(!s)return void(void 0!==e&&this.transport.sendError(e,-32602,"sessionId is required"));const i=this.state.sessions.get(s);if(!i)return void(void 0!==e&&this.transport.sendError(e,-32602,"unknown sessionId"));const a=this.getGateway();a?.isConnected&&i.currentRunId&&a.chatAbort(i.gatewaySessionKey,i.currentRunId).catch(()=>{}),this.completePrompt(i,"cancelled"),void 0!==e&&this.transport.sendResult(e,{})}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,-32022,`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);const e=`tc_${t}_`;for(const[t,s]of this.state.toolCallIdMap)s.startsWith(e)&&this.state.toolCallIdMap.delete(t)}e.pendingPromptId=void 0,e.promptDone=void 0,e.currentRunId=void 0,e.assistantStreamOwnerRunId=void 0,e.assistantStreamStartedAt=void 0,e.gatewayMethod=void 0,e.gatewayRequestId=void 0,e.assistantMq?.close()}completePrompt(e,t){e.promptDone||void 0===e.pendingPromptId||(e.promptDone=!0,this.transport.sendResult(e.pendingPromptId,{stopReason:t}),this.cleanupPrompt(e))}failPrompt(e,t,s){e.promptDone||void 0===e.pendingPromptId||(e.promptDone=!0,this.transport.sendError(e.pendingPromptId,t,s),this.cleanupPrompt(e))}cleanupAllPendingPrompts(){for(const e of this.state.sessions.values())this.failPrompt(e,-32001,"Gateway disconnected")}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,-32021,s)}}pushToolResultContent(e,t,s){if(0===s.length)return;const i=this.state.findAnyActiveSession();if(!i)return;const a=i.sessionId;for(const e of s){const t={sessionUpdate:"agent_message_chunk",content:e};this.transport.sendSessionUpdate(a,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:a}})}isCronCorrelatedRun(e){return this.state.cronRunIds.has(e)}}
|
|
1
|
+
import{randomUUID}from"node:crypto";import{sanitizePromptPayload}from"../message-filter.js";import{CANVAS_TERM_MAPPING,CANVAS_UPDATE_HINT,buildActiveSurfacesContext}from"../canvas-tools.js";import{MessageQueue}from"../utils/mq.js";import{extractPromptBlocks,toGatewayPrompt,resolveSlashCommandName as resolveSlashCmd}from"./prompt-converter.js";import{loadHistory,groupIntoTurns}from"./history-replay.js";import{appendHistoryEntry}from"./local-session-history.js";import{buildResolutionPlan,resolveResources,applyResolutions}from"./prompt-resource-resolver.js";import{ACP_PROTOCOL_VERSION,DEFAULT_PROMPT_TIMEOUT_MS,DEFAULT_LOAD_REPLAY_IDLE_TIMEOUT_MS,DEFAULT_LOAD_REPLAY_HARD_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-"];function isBackgroundSessionKey(e){return BACKGROUND_SESSION_PATTERNS.some(t=>e.includes(t))}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 a=s.match(/^\/([^\s:]+)(?::|\s|$)/)?.[1]?.trim();return a&&!a.includes("/")&&SLASH_COMMAND_NAME_RE.test(a)?a.toLowerCase():void 0}export class AcpGatewayBridgeCore{state;transport;config;logger;writeObs;getGateway;initialized=!1;handleTerminalRequest=null;handleTerminalNotification=null;handleSlashCommand=null;onPromptStart=null;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}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":this.handleSessionDelete(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"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.sendError(e.id,-32601,`Method not found: ${e.method}`)}}catch(t){this.transport.sendError(e.id,-32603,t instanceof Error?t.message:"Internal error")}}handleCanvasNotification=null;getCanvasState=null;handleNotification(e){switch(e.method){case"session/cancel":this.handleSessionCancel(void 0,e.params);break;case"canvas/action":case"canvas/toggle":case"canvas/clear":case"canvas/delete":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,a=this.state.resolveSessionFromParams(t);if(!a){this.logger.warn?.(`[bridge-core] canvas/reference ignored — no session found for sessionId=${t?.sessionId}`);break}a.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=`session:${randomUUID()}`,s=`agent:main:${randomUUID()}`,a="string"==typeof e.params?.cwd?e.params.cwd:".",i={sessionId:t,gatewaySessionKey:s,createdAt:Date.now(),chatReplayDedupKeys:new Set,cwd:a};this.state.sessions.set(t,i),this.state.sessionsByGatewayKey.set(s,i),this.transport.sendResult(e.id,{sessionId:t,modes:{availableModes:[{...DEFAULT_MODE}],currentModeId:DEFAULT_MODE.id},_meta:{sessionKey:s}}),this.fetchHistoryInBackground(s)}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 handleSessionLoad(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendError(e.id,-32602,"sessionId required");let s=this.state.sessions.get(t);if(!s){const e=t.startsWith("agent:")?t:`agent:main:${t}`;s={sessionId:t,gatewaySessionKey:e,createdAt:Date.now(),chatReplayDedupKeys:new Set},this.state.sessions.set(t,s),this.state.sessionsByGatewayKey.set(e,s)}this.state.lastActiveSessionId=t,s.chatReplayDedupKeys.clear();try{const e=this.getGateway(),a=await loadHistory(e,s.gatewaySessionKey,50),i=groupIntoTurns(a),n=i.filter(e=>e.toolCalls?.length).length,o=i.reduce((e,t)=>e+(t.toolCalls?.length??0),0);this.logger.debug?.(`[bridge-core] handleSessionLoad sid=${t} messages=${a.length} turns=${i.length} turnsWithToolCalls=${n} totalToolCalls=${o}`);for(const e of i){const a=e.toolCalls?.length?`:tc=${e.toolCalls.map(e=>e.id).join(",")}`:"",i=`${e.role}:${e.content.slice(0,64)}${a}`;s.chatReplayDedupKeys.has(i)||(s.chatReplayDedupKeys.add(i),this.transport.sendSessionUpdate(t,{sessionUpdate:"history_turn",role:e.role,content:e.content,reasoning:e.reasoning,toolCalls:e.toolCalls}))}}catch(e){this.logger.warn?.("[bridge-core] Failed to load session history:",e)}this.transport.sendResult(e.id,{sessionId:t});void 0!==s.pendingPromptId&&!s.promptDone&&s.assistantMq&&!s.assistantMq.closed&&this.replayCurrentAssistantStream(s).catch(()=>{}),this.pushSkillsList().catch(()=>{})}async replayCurrentAssistantStream(e){if(!e.assistantMq||e.assistantMq.closed)return;const t=this.config.loadReplayIdleTimeoutMs??DEFAULT_LOAD_REPLAY_IDLE_TIMEOUT_MS,s=Date.now()+DEFAULT_LOAD_REPLAY_HARD_TIMEOUT_MS;let a=0;for(;Date.now()<s&&(e.assistantStreamOwnerRunId||e.pendingPromptId)&&!e.assistantMq.closed;){const s=await Promise.race([e.assistantMq.read(a),new Promise(e=>setTimeout(()=>e(null),t))]);if(!s||0===s.length){if(void 0!==e.pendingPromptId&&!e.promptDone)continue;break}for(const t of s)this.transport.sendSessionUpdate(e.sessionId,t),a++}a>0&&this.writeObs({component:"bridge-core",domain:"lifecycle",name:"session.load.replay_complete",severity:"debug",payload:{sessionId:e.sessionId,replayed:a}})}async handleSessionList(e){const t=[];for(const[,e]of this.state.sessions)t.push({sessionId:e.sessionId,title:e.title||"New chat",cwd:e.cwd,updatedAt:e.lastStreamActivityTs??e.createdAt});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,a=s.sessionKey??s.key??s.id??s.name;if(!a||"string"!=typeof a)continue;if(isBackgroundSessionKey(a))continue;t.find(e=>e.sessionId===a||this.state.sessions.get(e.sessionId)?.gatewaySessionKey===a)||t.push({sessionId:a,title:s.title||a,cwd:s.cwd,updatedAt:s.updatedAt})}}}catch{}t.sort((e,t)=>(t.updatedAt??0)-(e.updatedAt??0)),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.sendError(e.id,-32602,"sessionId and title required");const a=this.state.sessions.get(t);if(!a)return void this.transport.sendError(e.id,-32602,"unknown sessionId");a.title=s.slice(0,100);const i=this.getGateway();i?.isConnected&&i.sessionsPatch(a.gatewaySessionKey,{title:a.title}).catch(()=>{}),this.transport.sendResult(e.id,{sessionId:t,title:a.title})}handleSessionDelete(e){const t=e.params?.sessionId;if(!t)return void this.transport.sendError(e.id,-32602,"sessionId required");const s=this.state.sessions.get(t);s?(void 0!==s.pendingPromptId&&this.failPrompt(s,-32001,"Session deleted"),this.state.clearPromptTimeout(s),this.state.clearStreamIdleTimer(s),s.assistantMq?.close(),s.currentRunId&&this.state.promptsByRunId.delete(s.currentRunId),this.state.sessions.delete(t),this.state.sessionsByGatewayKey.delete(s.gatewaySessionKey),this.transport.sendResult(e.id,{deleted:!0})):this.transport.sendError(e.id,-32602,"unknown sessionId")}async handleSessionSetMode(e){const t=e.params?.sessionId,s=e.params?.mode,a=t?this.state.sessions.get(t):void 0,i=this.getGateway();if(a&&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(a.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,a=t?this.state.sessions.get(t):void 0,i=this.getGateway();if(a&&i?.isConnected&&s)try{await i.sessionsPatch(a.gatewaySessionKey,{model:s})}catch(e){this.logger.warn?.(`[bridge-core] sessions.patch for model failed: ${e instanceof Error?e.message:e}`)}this.transport.sendResult(e.id,{model:s??"default"})}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})):[]}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}})}async handleSessionPrompt(e){const t=e.params?.sessionId,s=e.params?.prompt;if(!t||!s)return void this.transport.sendError(e.id,-32602,"sessionId and prompt required");let a=this.state.sessions.get(t);if(!a){const e=t.startsWith("agent:")?t:`agent:main:${t}`;a={sessionId:t,gatewaySessionKey:e,createdAt:Date.now(),chatReplayDedupKeys:new Set},this.state.sessions.set(t,a),this.state.sessionsByGatewayKey.set(e,a)}this.state.lastActiveSessionId=t,this.onPromptStart?.();const i=this.getGateway();if(!i?.isConnected)return void this.transport.sendError(e.id,-32001,"Gateway not connected");let n=extractPromptBlocks(sanitizePromptPayload(s));if(!a.title){const e=n.find(e=>"text"===e.type&&e.text);e&&"text"in e&&e.text&&(a.title=e.text.slice(0,80).replace(/\n/g," ").trim(),i?.isConnected&&a.title&&i.sessionsPatch(a.gatewaySessionKey,{title:a.title}).catch(()=>{}))}const o=buildResolutionPlan(n);if(o.refs.length>0)try{const e=await resolveResources(o,this.writeObs);n=applyResolutions(n,e)}catch(e){this.logger.warn?.(`[bridge-core] Resource pre-resolution failed: ${e instanceof Error?e.message:e}`)}const r=toGatewayPrompt(n);if("error"in r)return void this.transport.sendError(e.id,-32602,r.error);const{message:d,attachments:l}=r,c=resolveStandaloneSlashCommandName(n,d)??resolveSlashCmd(n,d),m="string"==typeof c;if(c&&this.handleSlashCommand){const s=this;if(await(async()=>{try{return await s.handleSlashCommand(c)}catch(e){return s.logger.warn?.(`[bridge-core] /${c} handler failed: ${e instanceof Error?e.message:e}`),!1}})())return this.emitPromptAsUserSessionUpdates(a.sessionId,n),this.transport.sendSessionUpdate(t,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`Loading ${c}...`}}),void this.transport.sendResult(e.id,{stopReason:"end_turn"})}const p=m?"chat.send":"agent";let h=m?d:withUserMessagePrefix(d);if(!this.config.canvasEnabled||m||a.canvasPromptInjected||!1===a.canvasActive||(h=`${CANVAS_TERM_MAPPING}\n\n${h}`,a.canvasPromptInjected=!0,this.logger.debug?.("[bridge-core] Canvas term mapping injected into first message")),this.config.canvasEnabled&&!m&&!1!==a.canvasActive){const e=this.getCanvasState?.();if(e){const t=buildActiveSurfacesContext(e);t&&(h=`${t}\n\n${h}`)}const t=a.referencedSurfaceId;t&&(h=`${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${h}`,a.referencedSurfaceId=void 0)}m&&this.logger.info?.(`[bridge-core] slash command passthrough sessionId=${t} command=/${c}`),a.currentRunId&&this.state.promptsByRunId.delete(a.currentRunId),a.currentRunId=void 0,a.assistantStreamOwnerRunId=void 0,a.pendingPromptId=e.id,a.promptDone=!1,a.gatewayMethod=p,appendHistoryEntry(a.gatewaySessionKey,{ts:Date.now(),role:"user",content:d}),a.assistantMq?.close(),a.assistantMq=new MessageQueue,a.chatReplayDedupKeys.clear(),a.hasAgentAssistantStream=!1,a.hasAgentThinkingStream=!1,a.hasAgentToolStartStream=!1,a.hasAgentToolResultStream=!1,a.chatAssistantTextSoFar=void 0,a.agentAssistantTextSoFar=void 0,a.lastAgentSeq=void 0,a.lastChatSeq=void 0,this.transport.resetSessionUpdateCount(t),this.emitPromptAsUserSessionUpdates(a.sessionId,n),this.transport.sendSessionUpdate(t,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:""}}),this.schedulePromptTimeout(a);try{const e=l.length>0?l.map(e=>({type:e.type,content:e.content,...e.mimeType?{mimeType:e.mimeType}:{},...e.fileName?{fileName:e.fileName}:{}})):void 0;let t;t=m?await i.chatSend(a.gatewaySessionKey,h,e):await i.agentSend(a.gatewaySessionKey,h,this.config.gateway.agentId||"main",e),a.currentRunId=t.runId,a.assistantStreamOwnerRunId=t.runId,a.assistantStreamStartedAt=Date.now(),this.state.promptsByRunId.set(t.runId,a)}catch(t){this.state.clearPromptTimeout(a),void 0!==a.pendingPromptId&&(a.pendingPromptId=void 0,a.assistantMq?.close(),this.transport.sendError(e.id,-32001,t instanceof Error?t.message:"failed to send prompt to gateway"))}}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,a=t.mimeType??t.mime_type??e.mimeType??e.mime_type,i=t.text??e.text,n=t.data??e.data,o=t.fileName??t.file_name??t.filename??t.name??e.fileName??e.file_name??e.filename??e.name;if(!s&&!i&&!n)return;return{type:"resource",resource:{...s?{uri:s}:{},...a?{mimeType:a}:{},...i?{text:i}:{},...n?{data:n}:{},...o?{fileName:o}:{}}}}}handleSessionCancel(e,t){const s=t?.sessionId;if(!s)return void(void 0!==e&&this.transport.sendError(e,-32602,"sessionId is required"));const a=this.state.sessions.get(s);if(!a)return void(void 0!==e&&this.transport.sendError(e,-32602,"unknown sessionId"));const i=this.getGateway();i?.isConnected&&a.currentRunId&&i.chatAbort(a.gatewaySessionKey,a.currentRunId).catch(()=>{}),this.completePrompt(a,"cancelled"),void 0!==e&&this.transport.sendResult(e,{})}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,-32022,`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);const e=`tc_${t}_`;for(const[t,s]of this.state.toolCallIdMap)s.startsWith(e)&&this.state.toolCallIdMap.delete(t)}e.pendingPromptId=void 0,e.promptDone=void 0,e.currentRunId=void 0,e.assistantStreamOwnerRunId=void 0,e.assistantStreamStartedAt=void 0,e.gatewayMethod=void 0,e.gatewayRequestId=void 0,e.assistantMq?.close()}completePrompt(e,t){e.promptDone||void 0===e.pendingPromptId||(e.promptDone=!0,this.transport.sendResult(e.pendingPromptId,{stopReason:t}),this.cleanupPrompt(e))}failPrompt(e,t,s){e.promptDone||void 0===e.pendingPromptId||(e.promptDone=!0,this.transport.sendError(e.pendingPromptId,t,s),this.cleanupPrompt(e))}cleanupAllPendingPrompts(){for(const e of this.state.sessions.values())this.failPrompt(e,-32001,"Gateway disconnected")}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,-32021,s)}}pushToolResultContent(e,t,s,a){if(0===s.length)return;const i=(a?this.state.promptsByRunId.get(a):void 0)??(this.state.lastActiveSessionId?this.state.sessions.get(this.state.lastActiveSessionId):void 0);if(!i)return void this.logger.warn?.(`[bridge-core] pushToolResultContent dropped — no session (runId=${a})`);const n=i.sessionId;for(const e of s){const t={sessionUpdate:"agent_message_chunk",content:e};this.transport.sendSessionUpdate(n,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:n}})}async handleCronRequest(e){const t=this.getGateway();if(!t?.isConnected)return void this.transport.sendError(e.id,-32001,"Gateway not connected");const s=e.method.replace("/",".");try{const a=await t.request(s,e.params??{});this.transport.sendResult(e.id,a??{})}catch(t){const a=t instanceof Error?t.message:"cron request failed";this.logger.warn?.(`[bridge-core] ${s} failed: ${a}`),this.transport.sendError(e.id,-32001,a)}}isCronCorrelatedRun(e){return this.state.cronRunIds.has(e)}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{stripTransportMetadata}from"../message-filter.js";import{appendHistoryEntry}from"./local-session-history.js";import{resolveToolResultBlocks}from"./tool-result-payload-strategies.js";import{DEFAULT_STREAM_IDLE_TIMEOUT_MS,DEFAULT_STREAM_STALE_TIMEOUT_MS,DEFAULT_CRON_FLUSH_TIMEOUT_MS,DEFAULT_CRON_SIGNAL_WINDOW_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;return t.replace(/\r\n/g,"\n").replace(/\r/g,"\n")||void 0}export class AcpGatewayEvents{state;transport;config;logger;writeObs;completePrompt;failPrompt;maybeCompletePromptFromChatState;agentFetchFn;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}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)}resolveToolCallId(t,e,s){const n=asString(e.toolCallId)??asString(e.tool_call_id)??asString(e.callId)??asString(e.id);if(n){const t=this.state.toolCallIdMap.get(n);return t||(this.state.toolCallIdMap.set(n,n),n)}return`tc_${t.currentRunId??"unknown"}_${this.state.nextToolCallSeq++}`}handleChatEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??this.state.findSessionByKey(t.sessionKey);if(!e||!this.state.isCurrentStreamOwner(e,t.runId)){const e=this.config.cronSignalWindowMs??DEFAULT_CRON_SIGNAL_WINDOW_MS;return void(this.state.isCronCandidate(t.runId,e)&&this.handleUncorrelatedCronChatEvent(t))}const s=e.sessionId,n=t.message;if(n){this.touchStreamActivity(e);if("assistant"===n.role)for(const t of n.content){const n=t;if(!isRecord(n))continue;const o=asString(n.type);if(o){if("thinking"===o){if(e.hasAgentThinkingStream)continue;const t=normalizeText(n.thinking)??normalizeText(n.text);if(!t)continue;const o=`chat:thinking:${t}`;if(e.chatReplayDedupKeys.has(o))continue;e.chatReplayDedupKeys.add(o);const a={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:t}};this.transport.sendSessionUpdate(s,a)&&e.assistantMq?.push(a);continue}if("toolCall"===o){if(this.sendToolCallSpacer(e),e.hasAgentToolStartStream)continue;const t=this.resolveToolCallId(e,n,"start"),o=asString(n.name)??asString(n.toolName)??asString(n.tool_name)??"tool",a=isRecord(n.arguments)?n.arguments:isRecord(n.args)?n.args:{},i=`chat:tool_start:${t}:${JSON.stringify(a)}`;if(e.chatReplayDedupKeys.has(i))continue;e.chatReplayDedupKeys.add(i);const r={sessionUpdate:"tool_call",toolCallId:t,title:o,status:"in_progress",content:[{type:"content",content:{type:"text",text:JSON.stringify(a,null,2)}}]};this.transport.sendSessionUpdate(s,r)&&e.assistantMq?.push(r);continue}if("toolResult"===o){if(this.sendToolCallSpacer(e),e.hasAgentToolResultStream)continue;const t=this.resolveToolCallId(e,n,"result"),o=asString(n.name)??asString(n.toolName)??asString(n.tool_name)??"tool",a=normalizeText(n.text)??normalizeText(n.result)??(isRecord(n.result)||Array.isArray(n.result)?JSON.stringify(n.result,null,2):void 0);if(!a)continue;const i=`chat:tool_result:${t}:${a}`;if(e.chatReplayDedupKeys.has(i))continue;e.chatReplayDedupKeys.add(i);const r={sessionUpdate:"tool_call_update",toolCallId:t,title:o,status:"completed",content:[{type:"content",content:{type:"text",text:a}}]};this.transport.sendSessionUpdate(s,r)&&e.assistantMq?.push(r);continue}if("text"===o&&n.text){if(e.hasAgentAssistantStream)continue;const t=stripTransportMetadata(n.text);if(!t)continue;const o=e.chatAssistantTextSoFar??"";let a;if(e.chatAssistantTextSoFar=t,t.startsWith(o))a=t.slice(o.length);else{if(o.startsWith(t))continue;a=t}if(!a)continue;const i={sessionUpdate:"agent_message_chunk",content:{type:"text",text:a}};this.transport.sendSessionUpdate(s,i),e.assistantMq?.push(i)}}}}else"delta"===t.state&&this.touchStreamActivity(e);this.maybeCompletePromptFromChatState(e,t),"final"===t.state?this.completePrompt(e,t.stopReason||"end_turn"):"aborted"===t.state||"cancelled"===t.state||"cancel"===t.state?this.completePrompt(e,"cancelled"):"error"===t.state&&this.failPrompt(e,-32021,t.errorMessage||"Agent error")}handleAgentEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??(t.sessionKey?this.state.findSessionByKey(t.sessionKey):void 0)??this.state.findAnyActiveSession();if(!e||!this.state.isCurrentStreamOwner(e,t.runId)){const e=this.config.cronSignalWindowMs??DEFAULT_CRON_SIGNAL_WINDOW_MS;return this.state.isCronCandidate(t.runId,e)?void this.handleUncorrelatedCronAgentEvent(t):void 0}const s=e.sessionId;this.touchStreamActivity(e);const n=t=>{this.transport.sendSessionUpdate(s,t),e.assistantMq?.push(t)};if("assistant"===t.stream){e.hasAgentAssistantStream=!0;const o=this.state.extractAgentAssistantDelta(e,t.data??{});o&&n({sessionUpdate:"agent_message_chunk",content:{type:"text",text:o}});const a=[],i=t.data;i&&(Array.isArray(i.content)?a.push(...i.content):isRecord(i.content)?a.push(i.content):"string"==typeof i.type&&a.push(i));for(const t of a){if(!isRecord(t))continue;const o=asString(t.type);if("thinking"===o){e.hasAgentThinkingStream=!0;const n=normalizeText(t.thinking)??normalizeText(t.text);if(n){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:n}};this.transport.sendSessionUpdate(s,t)&&e.assistantMq?.push(t)}continue}if("toolCall"===o){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const s=this.resolveToolCallId(e,t,"start"),o=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",a=isRecord(t.arguments)?t.arguments:isRecord(t.args)?t.args:{};n({sessionUpdate:"tool_call",toolCallId:s,title:o,status:"in_progress",content:[{type:"content",content:{type:"text",text:JSON.stringify(a,null,2)}}]});continue}if("toolResult"===o){this.sendToolCallSpacer(e),e.hasAgentToolResultStream=!0;const s=this.resolveToolCallId(e,t,"result"),o=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",a=normalizeText(t.text)??normalizeText(t.result)??(isRecord(t.result)||Array.isArray(t.result)?JSON.stringify(t.result,null,2):void 0);a&&n({sessionUpdate:"tool_call_update",toolCallId:s,title:o,status:"completed",content:[{type:"content",content:{type:"text",text:a}}]});continue}}appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:o??""})}else if("thinking"===t.stream){e.hasAgentThinkingStream=!0;const n=t.data?.delta||t.data?.text;if(n){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:n}};this.transport.sendSessionUpdate(s,t)&&e.assistantMq?.push(t),appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:n,entryType:"thought"})}}else if("tool"===t.stream){const s=t.data,o="string"==typeof s?.phase?s.phase:void 0;if("start"===o){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const t=this.resolveToolCallId(e,s,"start"),o=asString(s?.name)??asString(s?.toolName)??asString(s?.tool_name)??"tool",a=isRecord(s?.arguments)?s.arguments:isRecord(s?.args)?s.args:{};n({sessionUpdate:"tool_call",toolCallId:t,title:o,kind:"other",status:"in_progress",content:[{type:"content",content:{type:"text",text:JSON.stringify(a,null,2)}}]})}else if("result"===o)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 o=asString(t?.name)??asString(s?.name)??asString(s?.toolName)??"tool";n({sessionUpdate:"tool_call",toolCallId:this.resolveToolCallId(e,{...s,...t},"start"),title:o,kind:"other",status:"in_progress"})}}}else if("lifecycle"===t.stream){const s=t.data,n="string"==typeof s?.phase?s.phase:void 0;if("end"===n){this.flushCronBufferForRun(t.runId);const s=this.state.resolveCronStreamKey(t);s&&this.clearCronStreamTracking(s),this.completePrompt(e,"end_turn")}else if("cancelled"===n||"cancel"===n){const s=this.state.resolveCronStreamKey(t);s&&this.clearCronStreamTracking(s),this.completePrompt(e,"cancelled")}else if("error"===n){const t=s?.error,n=("string"==typeof s?.message?s.message:void 0)??("string"==typeof t?.message?t.message:void 0)??"gateway lifecycle error";this.failPrompt(e,-32021,n)}}}async handleToolResult(t,e){const s=e.data,n=s?.toolCall,o=("string"==typeof n?.id?n.id:void 0)??("string"==typeof s?.toolCallId?s.toolCallId:void 0),a=(o?this.state.toolCallIdMap.get(o):void 0)??o??`tc_${e.runId}_${e.seq}`,i=("string"==typeof n?.name?n.name:void 0)??("string"==typeof s?.name?s.name:void 0)??"tool",r="string"==typeof s?.text?s.text:void 0,l=o??a;let c;try{c=await resolveToolResultBlocks({toolCallId:l,toolName:i,rawText:r,rawResult:e.data})}catch{c=r?[{type:"content",content:{type:"text",text:r}}]:[]}0===c.length&&r&&(c=[{type:"content",content:{type:"text",text:r}}]);for(const e of c)if(e.content){const s={sessionUpdate:"agent_message_chunk",content:e.content};this.transport.sendSessionUpdate(t.sessionId,s),t.assistantMq?.push(s)}if(c.length>0){const e={sessionUpdate:"tool_call_update",toolCallId:a,title:i,kind:"other",status:"completed"};this.transport.sendSessionUpdate(t.sessionId,e)&&t.assistantMq?.push(e),appendHistoryEntry(t.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:r??"",entryType:"tool_result",toolCallId:a,toolName:i})}}handleCronEvent(t){this.transport.sendNotification("_beeos/cron_event",{payload:t});const e=t,s="string"==typeof e?.runId?e.runId:void 0;if(this.state.lastCronSignalAtMs=Date.now(),s){this.state.cronRunIds.add(s);const t=this.state.cronRunIdTimers.get(s);t&&clearTimeout(t);const e=this.config.cronSignalWindowMs??DEFAULT_CRON_SIGNAL_WINDOW_MS,n=setTimeout(()=>{this.state.cronRunIds.delete(s),this.state.cronRunIdTimers.delete(s)},e);"object"==typeof n&&"unref"in n&&n.unref(),this.state.cronRunIdTimers.set(s,n)}this.pushCronWebhook(e,s);const n=this.state.findAnyActiveSession();if(!n)return;const o=("string"==typeof e?.summary?e.summary:"")||("string"==typeof e?.text?e.text:"")||JSON.stringify(t),a=s?`run:${s}`:`signal:${Date.now()}`;this.bufferCronText(a,`[Cron] ${o}\n`,n.sessionId)}pushCronWebhook(t,e){if(!this.config.platformUrl||!this.agentFetchFn)return;const s=`${this.config.platformUrl}/api/v1/agent/webhooks/cron`,n=("string"==typeof t?.summary?t.summary:void 0)??("string"==typeof t?.text?t.text:void 0)??"",o={jobId:e??"",name:("string"==typeof t?.name?t.name:void 0)??("string"==typeof t?.jobName?t.jobName:void 0)??"",content:n,summary:n,type:"cron",action:"string"==typeof t?.action?t.action:"fired",status:"string"==typeof t?.status?t.status:""};this.agentFetchFn(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)}).catch(t=>{this.logger.warn?.(`[gateway-events] cron webhook push failed: ${t instanceof Error?t.message:t}`)})}bufferCronText(t,e,s){const n=this.state.cronTextByKey.get(t)??"";this.state.cronTextByKey.set(t,n+e),this.scheduleCronFlush(t,s)}scheduleCronFlush(t,e){const s=this.state.cronFlushTimersByKey.get(t);s&&clearTimeout(s);const n=this.config.cronFlushTimeoutMs??DEFAULT_CRON_FLUSH_TIMEOUT_MS,o=setTimeout(()=>{this.state.cronFlushTimersByKey.delete(t),this.flushCronBufferByKey(t,e,"timeout")},n);"object"==typeof o&&"unref"in o&&o.unref(),this.state.cronFlushTimersByKey.set(t,o)}flushCronBufferByKey(t,e,s){const n=this.state.cronFlushTimersByKey.get(t);n&&(clearTimeout(n),this.state.cronFlushTimersByKey.delete(t));const o=(this.state.cronTextByKey.get(t)??"").trim();if(this.state.cronTextByKey.delete(t),!o)return void("timeout"===s&&this.clearCronStreamTracking(t));const a=this.state.getCronRequestId(t),i={sessionUpdate:"agent_message_chunk",content:{type:"text",text:o}};this.transport.sendCronSessionUpdate(e,i,{requestId:a,messageType:"cron",timestamp:Date.now()});const r=this.state.sessions.get(e);r?.assistantMq?.push(i),"timeout"===s&&this.clearCronStreamTracking(t)}flushCronBufferForRun(t){if(!t)return;const e=`run:${t}`;if(!this.state.cronTextByKey.has(e))return;const s=this.state.findAnyActiveSession();s&&this.flushCronBufferByKey(e,s.sessionId,"end")}clearCronStreamTracking(t){this.state.cronRequestIdsByKey.delete(t),this.state.cronTextByKey.delete(t);const e=this.state.cronFlushTimersByKey.get(t);if(e&&(clearTimeout(e),this.state.cronFlushTimersByKey.delete(t)),t.startsWith("run:")){const e=t.slice(4);if(e){this.state.cronRunIds.delete(e);const t=this.state.cronRunIdTimers.get(e);t&&(clearTimeout(t),this.state.cronRunIdTimers.delete(e))}}}handleUncorrelatedCronAgentEvent(t){if("lifecycle"===t.stream){const e=t.data,s="string"==typeof e?.phase?e.phase:void 0;if("end"===s){this.flushCronBufferForRun(t.runId);const e=this.state.resolveCronStreamKey(t);e&&this.clearCronStreamTracking(e)}else if("cancelled"===s||"cancel"===s||"error"===s){const e=this.state.resolveCronStreamKey(t);e&&this.clearCronStreamTracking(e)}return}if("assistant"!==t.stream)return;const e=this.state.findAnyActiveSession();if(!e)return;const s=("string"==typeof t.data?.delta?t.data.delta:void 0)??("string"==typeof t.data?.text?t.data.text:void 0);if(!s)return;const n=this.state.resolveCronStreamKey(t)??`signal:${this.state.lastCronSignalAtMs}`;this.bufferCronText(n,s,e.sessionId)}handleUncorrelatedCronChatEvent(t){if("user"!==t.message?.role)return;const e=this.state.findAnyActiveSession();if(e&&t.message?.content)for(const s of t.message.content)if("text"===s.type&&s.text){const n=stripTransportMetadata(s.text);if(!n)continue;const o=this.state.resolveCronStreamKey(t)??`signal:${this.state.lastCronSignalAtMs}`,a=this.state.getCronRequestId(o),i={sessionUpdate:"user_message_chunk",content:{type:"text",text:n}};this.transport.sendCronSessionUpdate(e.sessionId,i,{requestId:a,messageType:"cron",timestamp:Date.now()})}}}
|
|
1
|
+
import{stripTransportMetadata}from"../message-filter.js";import{appendHistoryEntry}from"./local-session-history.js";import{resolveToolResultBlocks}from"./tool-result-payload-strategies.js";import{DEFAULT_STREAM_IDLE_TIMEOUT_MS,DEFAULT_STREAM_STALE_TIMEOUT_MS,DEFAULT_CRON_FLUSH_TIMEOUT_MS,DEFAULT_CRON_SIGNAL_WINDOW_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;return t.replace(/\r\n/g,"\n").replace(/\r/g,"\n")||void 0}export class AcpGatewayEvents{state;transport;config;logger;writeObs;completePrompt;failPrompt;maybeCompletePromptFromChatState;agentFetchFn;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}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)}resolveToolCallId(t,e,s){const n=asString(e.toolCallId)??asString(e.tool_call_id)??asString(e.callId)??asString(e.id);if(n){const e=this.state.toolCallIdMap.get(n);return e?(t.sessionId&&this.state.toolCallToSessionMap.set(e,t.sessionId),e):(this.state.toolCallIdMap.set(n,n),t.sessionId&&this.state.toolCallToSessionMap.set(n,t.sessionId),n)}const o=`tc_${t.currentRunId??"unknown"}_${this.state.nextToolCallSeq++}`;return t.sessionId&&this.state.toolCallToSessionMap.set(o,t.sessionId),o}handleChatEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??this.state.findSessionByKey(t.sessionKey);if(!e||!this.state.isCurrentStreamOwner(e,t.runId)){const e=this.config.cronSignalWindowMs??DEFAULT_CRON_SIGNAL_WINDOW_MS;return void(this.state.isCronCandidate(t.runId,e)&&this.handleUncorrelatedCronChatEvent(t))}if("number"==typeof t.seq){if(t.seq<=(e.lastChatSeq??-1))return void this.logger.debug?.(`[gateway-events] Dropping duplicate chat event seq=${t.seq} (last=${e.lastChatSeq}) runId=${t.runId}`);e.lastChatSeq=t.seq}const s=e.sessionId,n=t.message;if(n){this.touchStreamActivity(e);if("assistant"===n.role)for(const o of n.content){const n=o;if(!isRecord(n))continue;const a=asString(n.type);if(a){if("thinking"===a){if(e.hasAgentThinkingStream)continue;const t=normalizeText(n.thinking)??normalizeText(n.text);if(!t)continue;const o=`chat:thinking:${t}`;if(e.chatReplayDedupKeys.has(o))continue;e.chatReplayDedupKeys.add(o);const a={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:t}};this.transport.sendSessionUpdate(s,a)&&e.assistantMq?.push(a);continue}if("toolCall"===a){if(this.sendToolCallSpacer(e),e.hasAgentToolStartStream)continue;const t=this.resolveToolCallId(e,n,"start"),o=asString(n.name)??asString(n.toolName)??asString(n.tool_name)??"tool",a=isRecord(n.arguments)?n.arguments:isRecord(n.args)?n.args:{},r=`chat:tool_start:${t}:${JSON.stringify(a)}`;if(e.chatReplayDedupKeys.has(r))continue;e.chatReplayDedupKeys.add(r);const i={sessionUpdate:"tool_call",toolCallId:t,title:o,status:"in_progress",content:[{type:"content",content:{type:"text",text:JSON.stringify(a,null,2)}}]};this.transport.sendSessionUpdate(s,i)&&e.assistantMq?.push(i);continue}if("toolResult"===a){if(this.sendToolCallSpacer(e),e.hasAgentToolResultStream)continue;const t=this.resolveToolCallId(e,n,"result"),o=asString(n.name)??asString(n.toolName)??asString(n.tool_name)??"tool",a=normalizeText(n.text)??normalizeText(n.result)??(isRecord(n.result)||Array.isArray(n.result)?JSON.stringify(n.result,null,2):void 0);if(!a)continue;const r=`chat:tool_result:${t}:${a}`;if(e.chatReplayDedupKeys.has(r))continue;e.chatReplayDedupKeys.add(r);const i={sessionUpdate:"tool_call_update",toolCallId:t,title:o,status:"completed",content:[{type:"content",content:{type:"text",text:a}}]};this.transport.sendSessionUpdate(s,i)&&e.assistantMq?.push(i);continue}if("text"===a&&n.text){if(e.hasAgentAssistantStream||e.agentAssistantTextSoFar)continue;const o=stripTransportMetadata(n.text);if(!o)continue;const a=`chat:text:${o.slice(0,128)}`;if(e.chatReplayDedupKeys.has(a))continue;e.chatReplayDedupKeys.add(a);const r=e.chatAssistantTextSoFar??"";let i;if(e.chatAssistantTextSoFar=o,o.startsWith(r))i=o.slice(r.length);else{if(r.startsWith(o))continue;i=o}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)}}}}else"delta"===t.state&&this.touchStreamActivity(e);this.maybeCompletePromptFromChatState(e,t),"final"===t.state?this.completePrompt(e,t.stopReason||"end_turn"):"aborted"===t.state||"cancelled"===t.state||"cancel"===t.state?this.completePrompt(e,"cancelled"):"error"===t.state&&this.failPrompt(e,-32021,t.errorMessage||"Agent error")}handleAgentEvent(t){const e=(t.runId?this.state.promptsByRunId.get(t.runId):void 0)??(t.sessionKey?this.state.findSessionByKey(t.sessionKey):void 0)??this.state.findCronTargetSession();if(!e||!this.state.isCurrentStreamOwner(e,t.runId)){const e=this.config.cronSignalWindowMs??DEFAULT_CRON_SIGNAL_WINDOW_MS;return this.state.isCronCandidate(t.runId,e)?void this.handleUncorrelatedCronAgentEvent(t):void 0}if("number"==typeof t.seq){if(t.seq<=(e.lastAgentSeq??-1))return void this.logger.debug?.(`[gateway-events] Dropping duplicate agent event seq=${t.seq} (last=${e.lastAgentSeq}) runId=${t.runId} stream=${t.stream}`);e.lastAgentSeq=t.seq}const s=e.sessionId;this.touchStreamActivity(e);const n=t=>{this.transport.sendSessionUpdate(s,t),e.assistantMq?.push(t)};if("assistant"===t.stream){e.hasAgentAssistantStream=!0;const o=this.state.extractAgentAssistantDelta(e,t.data??{});o&&(this.logger.debug?.(`[gateway-events] agent.assistant delta seq=${t.seq} runId=${t.runId} len=${o.length} source=${t.data?.delta?"delta":"cumulative"}`),n({sessionUpdate:"agent_message_chunk",content:{type:"text",text:o}}));const a=[],r=t.data;r&&(Array.isArray(r.content)?a.push(...r.content):isRecord(r.content)?a.push(r.content):"string"==typeof r.type&&a.push(r));for(const t of a){if(!isRecord(t))continue;const o=asString(t.type);if("thinking"===o){e.hasAgentThinkingStream=!0;const n=normalizeText(t.thinking)??normalizeText(t.text);if(n){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:n}};this.transport.sendSessionUpdate(s,t)&&e.assistantMq?.push(t)}continue}if("toolCall"===o){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const s=this.resolveToolCallId(e,t,"start"),o=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",a=isRecord(t.arguments)?t.arguments:isRecord(t.args)?t.args:{};n({sessionUpdate:"tool_call",toolCallId:s,title:o,status:"in_progress",content:[{type:"content",content:{type:"text",text:JSON.stringify(a,null,2)}}]}),appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:JSON.stringify(a,null,2),entryType:"tool_call",toolCallId:s,toolName:o});continue}if("toolResult"===o){this.sendToolCallSpacer(e),e.hasAgentToolResultStream=!0;const s=this.resolveToolCallId(e,t,"result"),o=asString(t.name)??asString(t.toolName)??asString(t.tool_name)??"tool",a=normalizeText(t.text)??normalizeText(t.result)??(isRecord(t.result)||Array.isArray(t.result)?JSON.stringify(t.result,null,2):void 0);a&&n({sessionUpdate:"tool_call_update",toolCallId:s,title:o,status:"completed",content:[{type:"content",content:{type:"text",text:a}}]});continue}}appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:o??""})}else if("thinking"===t.stream){e.hasAgentThinkingStream=!0;const n=t.data?.delta||t.data?.text;if(n){const t={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:n}};this.transport.sendSessionUpdate(s,t)&&e.assistantMq?.push(t),appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:n,entryType:"thought"})}}else if("tool"===t.stream){const s=t.data,o="string"==typeof s?.phase?s.phase:void 0;if("start"===o){this.sendToolCallSpacer(e),e.hasAgentToolStartStream=!0;const t=this.resolveToolCallId(e,s,"start"),o=asString(s?.name)??asString(s?.toolName)??asString(s?.tool_name)??"tool",a=isRecord(s?.arguments)?s.arguments:isRecord(s?.args)?s.args:{};n({sessionUpdate:"tool_call",toolCallId:t,title:o,kind:"other",status:"in_progress",content:[{type:"content",content:{type:"text",text:JSON.stringify(a,null,2)}}]}),appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:JSON.stringify(a,null,2),entryType:"tool_call",toolCallId:t,toolName:o})}else if("result"===o)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 o=asString(t?.name)??asString(s?.name)??asString(s?.toolName)??"tool",a=this.resolveToolCallId(e,{...s,...t},"start");n({sessionUpdate:"tool_call",toolCallId:a,title:o,kind:"other",status:"in_progress"}),appendHistoryEntry(e.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:"",entryType:"tool_call",toolCallId:a,toolName:o})}}}else if("lifecycle"===t.stream){const s=t.data,n="string"==typeof s?.phase?s.phase:void 0;if("end"===n){this.flushCronBufferForRun(t.runId);const s=this.state.resolveCronStreamKey(t);s&&this.clearCronStreamTracking(s),this.completePrompt(e,"end_turn")}else if("cancelled"===n||"cancel"===n){const s=this.state.resolveCronStreamKey(t);s&&this.clearCronStreamTracking(s),this.completePrompt(e,"cancelled")}else if("error"===n){const n=s?.error,o=("string"==typeof s?.message?s.message:void 0)??("string"==typeof n?.message?n.message:void 0)??("string"==typeof s?.reason?s.reason:void 0)??(n&&"object"==typeof n?JSON.stringify(n):void 0)??"gateway lifecycle error";this.logger.warn?.(`[gateway-events] lifecycle error runId=${t.runId} sessionKey=${t.sessionKey} message=${o} data=${JSON.stringify(s)}`),this.failPrompt(e,-32021,o)}}}async handleToolResult(t,e){const s=e.data,n=s?.toolCall,o=("string"==typeof n?.id?n.id:void 0)??("string"==typeof s?.toolCallId?s.toolCallId:void 0),a=(o?this.state.toolCallIdMap.get(o):void 0)??o??`tc_${e.runId}_${e.seq}`,r=("string"==typeof n?.name?n.name:void 0)??("string"==typeof s?.name?s.name:void 0)??"tool",i="string"==typeof s?.text?s.text:void 0,l=o??a;let c;try{c=await resolveToolResultBlocks({toolCallId:l,toolName:r,rawText:i,rawResult:e.data})}catch{c=i?[{type:"content",content:{type:"text",text:i}}]:[]}if(0===c.length&&i&&(c=[{type:"content",content:{type:"text",text:i}}]),c.length>0){const e=c.filter(t=>"text"===t.content?.type&&t.content?.text).map(t=>({type:"content",content:{type:"text",text:t.content.text}})),s={sessionUpdate:"tool_call_update",toolCallId:a,title:r,kind:"other",status:"completed",...e.length>0?{content:e}:{}};this.transport.sendSessionUpdate(t.sessionId,s)&&t.assistantMq?.push(s),appendHistoryEntry(t.gatewaySessionKey,{ts:Date.now(),role:"assistant",content:i??"",entryType:"tool_result",toolCallId:a,toolName:r})}}handleCronEvent(t){this.transport.sendNotification("_beeos/cron_event",{payload:t});const e=t,s="string"==typeof e?.runId?e.runId:void 0;if(this.state.lastCronSignalAtMs=Date.now(),s){this.state.cronRunIds.add(s);const t=this.state.cronRunIdTimers.get(s);t&&clearTimeout(t);const e=this.config.cronSignalWindowMs??DEFAULT_CRON_SIGNAL_WINDOW_MS,n=setTimeout(()=>{this.state.cronRunIds.delete(s),this.state.cronRunIdTimers.delete(s)},e);"object"==typeof n&&"unref"in n&&n.unref(),this.state.cronRunIdTimers.set(s,n)}this.pushCronWebhook(e,s);const n=this.state.findCronTargetSession();if(!n)return;const o=("string"==typeof e?.summary?e.summary:"")||("string"==typeof e?.text?e.text:"")||JSON.stringify(t),a=s?`run:${s}`:`signal:${Date.now()}`;this.bufferCronText(a,`[Cron] ${o}\n`,n.sessionId)}pushCronWebhook(t,e){if(!this.config.platformUrl||!this.agentFetchFn)return;const s=`${this.config.platformUrl}/api/v1/agent/webhooks/cron`,n=("string"==typeof t?.summary?t.summary:void 0)??("string"==typeof t?.text?t.text:void 0)??"",o={jobId:e??"",name:("string"==typeof t?.name?t.name:void 0)??("string"==typeof t?.jobName?t.jobName:void 0)??"",content:n,summary:n,type:"cron",action:"string"==typeof t?.action?t.action:"fired",status:"string"==typeof t?.status?t.status:""};this.agentFetchFn(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)}).catch(t=>{this.logger.warn?.(`[gateway-events] cron webhook push failed: ${t instanceof Error?t.message:t}`)})}bufferCronText(t,e,s){const n=this.state.cronTextByKey.get(t)??"";this.state.cronTextByKey.set(t,n+e),this.scheduleCronFlush(t,s)}scheduleCronFlush(t,e){const s=this.state.cronFlushTimersByKey.get(t);s&&clearTimeout(s);const n=this.config.cronFlushTimeoutMs??DEFAULT_CRON_FLUSH_TIMEOUT_MS,o=setTimeout(()=>{this.state.cronFlushTimersByKey.delete(t),this.flushCronBufferByKey(t,e,"timeout")},n);"object"==typeof o&&"unref"in o&&o.unref(),this.state.cronFlushTimersByKey.set(t,o)}flushCronBufferByKey(t,e,s){const n=this.state.cronFlushTimersByKey.get(t);n&&(clearTimeout(n),this.state.cronFlushTimersByKey.delete(t));const o=(this.state.cronTextByKey.get(t)??"").trim();if(this.state.cronTextByKey.delete(t),!o)return void("timeout"===s&&this.clearCronStreamTracking(t));const a=this.state.getCronRequestId(t),r={sessionUpdate:"agent_message_chunk",content:{type:"text",text:o}};this.transport.sendCronSessionUpdate(e,r,{requestId:a,messageType:"cron",timestamp:Date.now()});const i=this.state.sessions.get(e);i?.assistantMq?.push(r),"timeout"===s&&this.clearCronStreamTracking(t)}flushCronBufferForRun(t){if(!t)return;const e=`run:${t}`;if(!this.state.cronTextByKey.has(e))return;const s=this.state.findCronTargetSession();s&&this.flushCronBufferByKey(e,s.sessionId,"end")}clearCronStreamTracking(t){this.state.cronRequestIdsByKey.delete(t),this.state.cronTextByKey.delete(t);const e=this.state.cronFlushTimersByKey.get(t);if(e&&(clearTimeout(e),this.state.cronFlushTimersByKey.delete(t)),t.startsWith("run:")){const e=t.slice(4);if(e){this.state.cronRunIds.delete(e);const t=this.state.cronRunIdTimers.get(e);t&&(clearTimeout(t),this.state.cronRunIdTimers.delete(e))}}}handleUncorrelatedCronAgentEvent(t){if("lifecycle"===t.stream){const e=t.data,s="string"==typeof e?.phase?e.phase:void 0;if("end"===s){this.flushCronBufferForRun(t.runId);const e=this.state.resolveCronStreamKey(t);e&&this.clearCronStreamTracking(e)}else if("cancelled"===s||"cancel"===s||"error"===s){const e=this.state.resolveCronStreamKey(t);e&&this.clearCronStreamTracking(e)}return}if("assistant"!==t.stream)return;const e=this.state.findCronTargetSession();if(!e)return;const s=("string"==typeof t.data?.delta?t.data.delta:void 0)??("string"==typeof t.data?.text?t.data.text:void 0);if(!s)return;const n=this.state.resolveCronStreamKey(t)??`signal:${this.state.lastCronSignalAtMs}`;this.bufferCronText(n,s,e.sessionId)}handleUncorrelatedCronChatEvent(t){if("user"!==t.message?.role)return;const e=this.state.findCronTargetSession();if(e&&t.message?.content)for(const s of t.message.content)if("text"===s.type&&s.text){const n=stripTransportMetadata(s.text);if(!n)continue;const o=this.state.resolveCronStreamKey(t)??`signal:${this.state.lastCronSignalAtMs}`,a=this.state.getCronRequestId(o),r={sessionUpdate:"user_message_chunk",content:{type:"text",text:n}};this.transport.sendCronSessionUpdate(e.sessionId,r,{requestId:a,messageType:"cron",timestamp:Date.now()})}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{readLocalHistory}from"./local-session-history.js";export async function loadHistory(t,e
|
|
1
|
+
import{readLocalHistory}from"./local-session-history.js";export async function loadHistory(t,o,e=50){if(t?.isConnected)try{const l=parseGatewayHistory(await t.chatHistory(o,e));if(l.length>0)return l}catch{}const l=readLocalHistory(o,e);return l.length>0?l.map(t=>{let o;switch(t.entryType){case"thought":o="agent_thought_chunk";break;case"tool_call":o="tool_call";break;case"tool_result":o="tool_call_update";break;default:o="user"===t.role?"user_message_chunk":"agent_message_chunk"}return{role:t.role,content:t.content,dedupKey:t.dedupKey,updateType:o,toolCallId:t.toolCallId,toolName:t.toolName}}):[]}export function groupIntoTurns(t){const o=[];let e=null;const l=()=>{e&&(o.push(e),e=null)};for(const n of t){if("assistant"===n.role&&(n.reasoning||n.toolCalls?.length)){l(),e={role:"assistant",content:n.content,reasoning:n.reasoning},n.toolCalls?.length&&(e.toolCalls=n.toolCalls.map(t=>({id:t.id,callId:t.id,name:t.name,arguments:t.arguments??"",status:t.status??"completed",output:t.output,isError:"error"===t.status||"failed"===t.status})));continue}if("user"===n.role){l(),o.push({role:"user",content:n.content});continue}e||(e={role:"assistant",content:""});switch(n.updateType??"agent_message_chunk"){case"agent_thought_chunk":e.reasoning=(e.reasoning??"")+n.content;break;case"tool_call":e.toolCalls||(e.toolCalls=[]),e.toolCalls.push({id:n.toolCallId??`tc_${e.toolCalls.length}`,callId:n.toolCallId??`tc_${e.toolCalls.length}`,name:n.toolName??"tool",arguments:n.content||"",status:"completed"});break;case"tool_call_update":{e.toolCalls||(e.toolCalls=[]);const t=n.toolCallId?e.toolCalls.find(t=>t.id===n.toolCallId):void 0;t?(t.output=n.content,t.status="completed"):e.toolCalls.push({id:n.toolCallId??`tc_${e.toolCalls.length}`,callId:n.toolCallId??`tc_${e.toolCalls.length}`,name:n.toolName??"tool",arguments:"",status:"completed",output:n.content});break}default:e.content+=n.content}}return l(),o}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 o="";for(const e of t)"string"!=typeof e?e&&"object"==typeof e&&"string"==typeof e.text&&(o+=e.text):o+=e;return o}function parseGatewayHistory(t){if(!t||"object"!=typeof t)return[];const o=t.messages;if(!Array.isArray(o))return[];const e=[];let l=null;for(const t of o){if(!t||"object"!=typeof t)continue;const o=t,n="string"==typeof o.role?o.role:"assistant";if("toolResult"===n||"tool"===n){if(!l?.toolCalls?.length)continue;const t=String(o.toolCallId??o.tool_call_id??o.toolUseId??o.tool_use_id??""),e=extractBlockText(o.content);if(t){const n=l.toolCalls.find(o=>o.id===t);if(n)n.output=e,!0!==o.is_error&&!0!==o.isError||(n.status="error");else{const t=l.toolCalls.find(t=>void 0===t.output);t&&(t.output=e,!0!==o.is_error&&!0!==o.isError||(t.status="error"))}}else{const t=l.toolCalls.find(t=>void 0===t.output);t&&(t.output=e,!0!==o.is_error&&!0!==o.isError||(t.status="error"))}continue}if("user"===n&&l?.toolCalls?.length&&Array.isArray(o.content)){let t=!1,e=!1;for(const l of o.content){if(!l||"object"!=typeof l)continue;const o=l,n="string"==typeof o.type?o.type:"";TOOL_RESULT_BLOCK_TYPES.has(n)?t=!0:"text"===n&&"string"==typeof o.text&&o.text.trim()&&(e=!0)}if(t&&!e){for(const t of o.content){if(!t||"object"!=typeof t)continue;const o=t,e="string"==typeof o.type?o.type:"";if(!TOOL_RESULT_BLOCK_TYPES.has(e))continue;const n=String(o.toolUseId??o.tool_use_id??o.toolCallId??o.tool_call_id??""),s="string"==typeof o.output?o.output:extractBlockText(o.content);if(n){const t=l.toolCalls.find(t=>t.id===n);if(t)t.output=s,!0!==o.is_error&&!0!==o.isError||(t.status="error");else{const t=l.toolCalls.find(t=>void 0===t.output);t&&(t.output=s,!0!==o.is_error&&!0!==o.isError||(t.status="error"))}}else{const t=l.toolCalls.find(t=>void 0===t.output);t&&(t.output=s,!0!==o.is_error&&!0!==o.isError||(t.status="error"))}}continue}}const s="user"===n?"user":"assistant";let r,i,a="";if("string"==typeof o.content)a=o.content;else if(Array.isArray(o.content))for(const t of o.content){if("string"==typeof t){a+=t;continue}if(!t||"object"!=typeof t)continue;const o=t,e="string"==typeof o.type?o.type:"";if("text"===e&&"string"==typeof o.text)a+=o.text;else if("thinking"===e&&"string"==typeof o.thinking)i=(i??"")+o.thinking;else if(TOOL_CALL_BLOCK_TYPES.has(e)){r||(r=[]);const t=o.arguments??o.input,e="string"==typeof t?t:null!=t?JSON.stringify(t):"";r.push({id:String(o.id??o.toolCallId??o.tool_call_id??""),name:String(o.name??"tool"),arguments:e,status:"completed"})}else if(TOOL_RESULT_BLOCK_TYPES.has(e)){const t=String(o.toolUseId??o.tool_use_id??o.toolCallId??o.tool_call_id??""),e="string"==typeof o.output?o.output:extractBlockText(o.content),l=!0===o.is_error||!0===o.isError;if(r){const o=t?r.find(o=>o.id===t):void 0;if(o)o.output=e,l&&(o.status="error");else{const t=r.find(t=>void 0===t.output);t&&(t.output=e,l&&(t.status="error"))}}}}if(i||("string"==typeof o.reasoning&&o.reasoning?i=o.reasoning:"string"==typeof o.thinking&&o.thinking&&(i=o.thinking)),!r?.length){const t=o.tool_calls??o.toolCalls;if(Array.isArray(t)){r=[];for(const o of t){if(!o||"object"!=typeof o)continue;const t=o,e=t.function;r.push({id:String(t.id??t.toolCallId??""),name:String(e?.name??t.name??t.toolName??"tool"),arguments:"string"==typeof t.arguments?t.arguments:"string"==typeof e?.arguments?e.arguments:"",status:String(t.status??"completed"),output:"string"==typeof t.output?t.output:void 0})}0===r.length&&(r=void 0)}}if(!a&&!i&&!r?.length)continue;const u={role:s,content:a,reasoning:i,toolCalls:r};e.push(u),"assistant"===s&&(l=u)}e.filter(t=>t.toolCalls?.length),e.reduce((t,o)=>t+(o.toolCalls?.length??0),0),e.reduce((t,o)=>t+(o.toolCalls?.filter(t=>void 0!==t.output).length??0),0);return e}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{randomUUID}from"node:crypto";export class AcpGatewaySessionState{sessions=new Map;promptsByRunId=new Map;sessionsByGatewayKey=new Map;toolCallIdMap=new Map;nextToolCallSeq=0;cronRunIds=new Set;cronRunIdTimers=new Map;lastCronSignalAtMs=0;cronTextByKey=new Map;cronFlushTimersByKey=new Map;cronRequestIdsByKey=new Map;sessionGcTimer=null;MAX_IDLE_SESSION_MS=864e5;SESSION_GC_INTERVAL_MS=3e5;logger;constructor(
|
|
1
|
+
import{randomUUID}from"node:crypto";export class AcpGatewaySessionState{sessions=new Map;promptsByRunId=new Map;sessionsByGatewayKey=new Map;toolCallIdMap=new Map;toolCallToSessionMap=new Map;nextToolCallSeq=0;cronRunIds=new Set;cronRunIdTimers=new Map;lastCronSignalAtMs=0;cronTextByKey=new Map;cronFlushTimersByKey=new Map;cronRequestIdsByKey=new Map;lastActiveSessionId;sessionGcTimer=null;MAX_IDLE_SESSION_MS=864e5;SESSION_GC_INTERVAL_MS=3e5;logger;constructor(s){this.logger=s}findSessionByKey(s){return this.sessionsByGatewayKey.get(s)}findCronTargetSession(){if(this.lastActiveSessionId)return this.sessions.get(this.lastActiveSessionId)}resolveSessionFromParams(s){const e="string"==typeof s?.sessionId?s.sessionId:void 0;if(e)return this.sessions.get(e)}isCurrentStreamOwner(s,e){return!e||!!s.assistantStreamOwnerRunId&&e===s.assistantStreamOwnerRunId}clearPromptTimeout(s){s.promptTimeoutTimer&&(clearTimeout(s.promptTimeoutTimer),s.promptTimeoutTimer=void 0)}clearStreamIdleTimer(s){s.streamIdleTimer&&(clearTimeout(s.streamIdleTimer),s.streamIdleTimer=void 0)}extractAgentAssistantDelta(s,e){const t=(s.chatAssistantTextSoFar??"").length;if(e.delta){const i=s.agentAssistantTextSoFar??"",n=i+e.delta;if(s.agentAssistantTextSoFar=n,t>0&&i.length<t){if(n.length<=t)return;return n.slice(t)}return e.delta}const i=e.text;if(!i)return;const n=s.agentAssistantTextSoFar??"";s.agentAssistantTextSoFar=i;const o=Math.max(n.length,t);if(!(i.length<=o)){if(i.startsWith(n)||0===o)return i.slice(o)||void 0;if(!n.startsWith(i))return i}}isCronCandidate(s,e){return!(!s||!this.cronRunIds.has(s))||Date.now()-this.lastCronSignalAtMs<=e&&(s&&this.cronRunIds.add(s),!0)}resolveCronStreamKey(s){if(s.runId)return`run:${s.runId}`}getCronRequestId(s){let e=this.cronRequestIdsByKey.get(s);return e||(e=randomUUID(),this.cronRequestIdsByKey.set(s,e)),e}startSessionGc(s){this.sessionGcTimer=setInterval(()=>this.gcSessions(s),this.SESSION_GC_INTERVAL_MS),"object"==typeof this.sessionGcTimer&&"unref"in this.sessionGcTimer&&this.sessionGcTimer.unref()}gcSessions(s){const e=Date.now()-this.MAX_IDLE_SESSION_MS;let t=0;for(const[i,n]of this.sessions){if(void 0!==n.pendingPromptId)continue;(n.lastStreamActivityTs??n.createdAt)<e&&(s(n),this.sessions.delete(i),this.sessionsByGatewayKey.delete(n.gatewaySessionKey),t++)}const i=new Set(this.promptsByRunId.keys());for(const[s,e]of this.toolCallIdMap){const t=e.split("_")[1];t&&!i.has(t)&&this.toolCallIdMap.delete(s)}const n=new Set(Array.from(this.sessions.values()).map(s=>s.sessionId));for(const[s,e]of this.toolCallToSessionMap)n.has(e)||this.toolCallToSessionMap.delete(s);t>0&&this.logger.debug?.(`[session-state] GC evicted ${t} idle sessions, remaining=${this.sessions.size}`)}destroy(){this.sessionGcTimer&&(clearInterval(this.sessionGcTimer),this.sessionGcTimer=null);for(const s of this.sessions.values())this.clearPromptTimeout(s),this.clearStreamIdleTimer(s),s.assistantMq?.close();for(const s of this.cronFlushTimersByKey.values())clearTimeout(s);this.cronFlushTimersByKey.clear(),this.cronTextByKey.clear(),this.cronRequestIdsByKey.clear();for(const s of this.cronRunIdTimers.values())clearTimeout(s);this.cronRunIdTimers.clear(),this.cronRunIds.clear(),this.promptsByRunId.clear(),this.toolCallIdMap.clear(),this.toolCallToSessionMap.clear(),this.sessionsByGatewayKey.clear(),this.sessions.clear(),this.lastActiveSessionId=void 0}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export class AcpTransport{debugIndex=0;sendBridgeMessage;obs;forward;constructor(e,t,
|
|
1
|
+
export class AcpTransport{debugIndex=0;sendBridgeMessage;obs;forward;sessionUpdateCounts=new Map;constructor(e,t,s){this.sendBridgeMessage=e,this.obs=t??(()=>{}),this.forward=s??{forwardThinking:!0,forwardToolCalls:!0}}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 o={jsonrpc:"2.0",id:e,result:this.attachMeta(t,s),_debug_index:s._debug_index,_ts:s._ts};this.sendBridgeMessage(o),this.obs({component:"acp-transport",domain:"reply",name:"rpc.result",severity:"debug",payload:{id:e,_debug_index:s._debug_index}})}sendError(e,t,s,o){const r=this.nextMeta(),n={code:t,message:s};n.data=void 0!==o?this.attachMeta(o,r):{_meta:r};const d={jsonrpc:"2.0",id:e,error:n,_debug_index:r._debug_index,_ts:r._ts};this.sendBridgeMessage(d),this.obs({component:"acp-transport",domain:"reply",name:"rpc.error",severity:"warn",payload:{id:e,code:t,message:s,_debug_index:r._debug_index}})}sendNotification(e,t){const s=this.nextMeta(),o={jsonrpc:"2.0",method:e,params:t,_debug_index:s._debug_index,_ts:s._ts};this.sendBridgeMessage(o)}sendSessionUpdate(e,t){const s=t,o=s?.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 r=(this.sessionUpdateCounts.get(e)??0)+1;return this.sessionUpdateCounts.set(e,r),r%50==0&&this.obs({component:"acp-transport",domain:"session-update",name:"send.rate",severity:"debug",payload:{sessionId:e,totalSent:r,lastType:o,_debug_index:this.debugIndex}}),this.sendNotification("session/update",{sessionId:e,update:t}),!0}resetSessionUpdateCount(e){this.sessionUpdateCounts.delete(e)}sendCronSessionUpdate(e,t,s){return this.sendNotification("session/update",{sessionId:e,update:t,_meta:{...this.nextMeta(),...s}}),!0}detectProtocolError(e,t,s){if(!t||"object"!=typeof t)return;const o=t,r="error"in o&&null!=o.error,n="ok"in o&&!1===o.ok;(r||n)&&this.obs({component:"acp-transport",domain:"reply",name:"protocol.error_in_result",severity:"warn",payload:{id:e,_debug_index:s,hasError:r,notOk:n,errorSummary:r?"string"==typeof o.error?o.error.slice(0,200):JSON.stringify(o.error).slice(0,200):void 0}})}attachMeta(e,t){return!e||"object"!=typeof e||Array.isArray(e)?e:{...e,_meta:t}}}
|
package/dist/acp-server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{WebSocketServer,WebSocket}from"ws";let BridgeClientCtor;import{GatewayClient}from"./gateway-client.js";import{TerminalManager}from"./terminal-session-manager.js";import{AcpTransport}from"./acp-gateway/transport.js";import{AcpGatewaySessionState}from"./acp-gateway/session-state.js";import{AcpGatewayBridgeCore}from"./acp-gateway/bridge-core.js";import{AcpGatewayEvents}from"./acp-gateway/gateway-events.js";import{TerminalBridgeHandler}from"./terminal-bridge-handler.js";export class ACPServer{agentFetchFn;wss=null;gateway=null;client=null;config;logger;writeObs;transport;state;core;events;terminalMgr;terminalHandler;sendBridgeMessage=e=>{const t=this.client;if(t&&t.readyState===WebSocket.OPEN)try{t.send(JSON.stringify(e))}catch{}else this.logger.debug?.("[acp-server] bridge not ready, dropping message")};constructor(e,t,i,r){this.agentFetchFn=r,this.config=e,this.logger=t,this.writeObs=i??(()=>{}),this.transport=new AcpTransport(this.sendBridgeMessage,this.writeObs),this.state=new AcpGatewaySessionState(this.logger),this.core=new AcpGatewayBridgeCore({state:this.state,transport:this.transport,config:this.config,logger:this.logger,writeObs:this.writeObs,getGateway:()=>this.gateway}),this.events=new AcpGatewayEvents({state:this.state,transport:this.transport,config:this.config,logger:this.logger,writeObs:this.writeObs,completePrompt:(e,t)=>this.core.completePrompt(e,t),failPrompt:(e,t,i)=>this.core.failPrompt(e,t,i),maybeCompletePromptFromChatState:(e,t)=>this.core.maybeCompletePromptFromChatState(e,t),agentFetch:this.agentFetchFn}),this.terminalMgr=new TerminalManager,this.terminalHandler=new TerminalBridgeHandler({terminalMgr:this.terminalMgr,transport:this.transport,config:this.config,logger:this.logger,writeObs:this.writeObs}),this.terminalHandler.setupListeners(),this.core.handleTerminalRequest=e=>this.terminalHandler.handleRequest(e),this.core.handleTerminalNotification=(e,t)=>this.terminalHandler.handleNotification(e,t),this.state.startSessionGc(e=>this.core.cleanupPrompt(e))}async start(){const e=this.config.bridge.url,t=this.config.bridge.keyFile;if(e&&!BridgeClientCtor)try{const e=await import("@beeos-ai/bridge-client");BridgeClientCtor=e.BridgeClient}catch{this.logger.warn?.("[acp-server] Bridge SDK not available")}e&&t&&BridgeClientCtor?await this.startBridgeMode(e,t):this.logger.warn?.("[acp-server] Bridge not configured; running in standby (no ACP listener). Use handleUpgrade() for noServer mode."),await this.connectGateway()}async startBridgeMode(e,t){if(!BridgeClientCtor)throw new Error("@beeos-ai/bridge-client not available");const i=new BridgeClientCtor({bridgeUrl:e,service:this.config.bridge.service||"acp",keyFile:t});i.on("connected",()=>{this.logger.info?.("[acp-server] Connected to Bridge (publicKey auth)"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"bridge.connected",severity:"info"})}),i.on("disconnected",(...e)=>{const t=String(e[0]??"unknown");this.logger.warn?.(`[acp-server] Bridge disconnected: ${t}`),this.core.initialized=!1}),i.on("message",(...e)=>{const t=e[0],i="string"==typeof t?t:Buffer.from(t).toString("utf-8");try{const e=JSON.parse(i);"id"in e&&void 0!==e.id?this.core.handleRequest(e):this.core.handleNotification(e)}catch{this.logger.error?.("[acp-server] Invalid JSON-RPC message from Bridge")}}),i.on("error",(...e)=>{const t=e[0];this.logger.error?.("[acp-server] Bridge error:",t?.message)});try{await i.connect()}catch(e){this.logger.warn?.(`[acp-server] Initial bridge connect failed (will retry in background): ${e?.message}`)}const r={readyState:WebSocket.OPEN,send:(e,t)=>{try{i.send(e),t?.()}catch(e){t?.(e)}},close:()=>i.close(),terminate:()=>i.close(!1),on:()=>r,once:()=>r,removeListener:()=>r,removeAllListeners:()=>r,ping:()=>{}};Object.defineProperty(r,"readyState",{get:()=>"connected"===i.state?WebSocket.OPEN:WebSocket.CLOSED}),this.client=r,this.core.initialized=!1,this.logger.info?.("[acp-server] Running in Bridge mode (WS client)")}handleUpgrade(e,t,i){this.wss||(this.wss=new WebSocketServer({noServer:!0}),this.wss.on("connection",(e,t)=>this.handleConnection(e,t))),this.wss.handleUpgrade(e,t,i,t=>{this.wss.emit("connection",t,e)})}async connectGateway(){const e=this.config.gateway;this.gateway=new GatewayClient({url:e.url,token:e.token||"",clientId:e.clientId,retry:{baseMs:this.config.retry.baseMs,maxMs:this.config.retry.maxMs,maxAttempts:this.config.retry.maxAttempts}}),this.gateway.on("chat",e=>this.events.handleChatEvent(e)),this.gateway.on("agent",e=>this.events.handleAgentEvent(e)),this.gateway.on("cron",e=>this.events.handleCronEvent(e)),this.gateway.on("connected",()=>{this.logger.info?.("[acp-server] Connected to OpenClaw Gateway"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"gateway.connected",severity:"info"})}),this.gateway.on("disconnected",()=>{this.logger.warn?.("[acp-server] Gateway disconnected"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"gateway.disconnected",severity:"warn"}),this.core.cleanupAllPendingPrompts()}),this.gateway.on("reconnecting",e=>{this.logger.info?.(`[acp-server] Reconnecting to Gateway attempt=${e.attempt} delay=${Math.round(e.delayMs)}ms`)}),this.gateway.on("reconnect-exhausted",e=>{this.logger.error?.(`[acp-server] Gateway reconnect exhausted after ${e} attempts`)}),this.gateway.on("error",e=>{this.logger.error?.("[acp-server] Gateway error:",e.message)}),this.gateway.on("transport",e=>{this.writeObs({component:"gateway-client",domain:"lifecycle",name:`transport.${e.event}`,severity:"connect_fail"===e.event||"error"===e.event?"warn":"debug",payload:e})}),this.gateway.on("reconnect-notify-scheduled",e=>{this.logger.info?.(`[acp-server] Server-initiated reconnect scheduled in ${e.delayMs}ms`)}),this.gateway.on("reconnect-notify-fired",()=>{this.logger.info?.("[acp-server] Server-initiated reconnect firing")});try{await this.gateway.start()}catch(e){this.logger.error?.("[acp-server] Initial Gateway connect failed:",e)}}handleConnection(e,t){if(this.client){this.logger.warn?.("[acp-server] Replacing existing ACP client with new connection (takeover)");const e=this.client;this.client=null;try{e.close(1008,"replaced by new client")}catch{e.terminate()}}this.client=e,this.core.initialized=!1,this.logger.info?.("[acp-server] ACP client connected"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"client.connected",severity:"info"}),e.on("message",e=>{const t="string"==typeof e?e:e.toString("utf-8");try{const e=JSON.parse(t);"id"in e&&void 0!==e.id?this.core.handleRequest(e):this.core.handleNotification(e)}catch{this.logger.error?.("[acp-server] Invalid JSON-RPC message")}}),e.on("close",()=>{this.client===e&&(this.client=null,this.core.initialized=!1),this.logger.info?.("[acp-server] ACP client disconnected"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"client.disconnected",severity:"info"})})}pushToolResultContent(e,t,i){this.core.pushToolResultContent(e,t,i)}isCronCorrelatedRun(e){return this.core.isCronCorrelatedRun(e)}async stop(){this.state.destroy(),this.gateway&&(this.gateway.removeAllListeners(),this.gateway.stop()),this.client&&(this.client.removeAllListeners(),this.client.close()),this.terminalHandler.destroy(),this.terminalMgr.destroy(),this.wss&&(this.wss.removeAllListeners(),this.wss.close()),this.logger.info?.("[acp-server] Stopped"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"lifecycle.shutdown",severity:"info"})}}
|
|
1
|
+
import{WebSocketServer,WebSocket}from"ws";let BridgeClientCtor;import{GatewayClient}from"./gateway-client.js";import{TerminalManager}from"./terminal-session-manager.js";import{AcpTransport}from"./acp-gateway/transport.js";import{AcpGatewaySessionState}from"./acp-gateway/session-state.js";import{AcpGatewayBridgeCore}from"./acp-gateway/bridge-core.js";import{AcpGatewayEvents}from"./acp-gateway/gateway-events.js";import{TerminalBridgeHandler}from"./terminal-bridge-handler.js";import{CanvasActionRouter,formatActionForAgent}from"./canvas-action-router.js";import{FormSchemaRegistry}from"./form-schema-registry.js";import{FormSurfaceBuilder}from"./form-surface-builder.js";import{ResourceSurfaceRenderer}from"./resource-surface-renderer.js";const SIMPLE_REMAP={Callout:"Alert",Carousel:"List"};function sanitizeA2UIComponents(e){return e.map(e=>{if(!e||"object"!=typeof e)return e;const t=e,s=String(t.type??""),n=SIMPLE_REMAP[s];return n?{...t,type:n}:t})}export class ACPServer{agentFetchFn;wss=null;gateway=null;client=null;config;logger;writeObs;transport;state;core;events;terminalMgr;terminalHandler;canvasState=null;resourceRenderer=null;descriptorRegistry=null;actionRouter=null;formBuilder=null;schemaRegistry=null;sendBridgeMessage=e=>{const t=this.client;if(t&&t.readyState===WebSocket.OPEN)try{t.send(JSON.stringify(e))}catch{}else this.logger.debug?.("[acp-server] bridge not ready, dropping message")};constructor(e,t,s,n){this.agentFetchFn=n,this.config=e,this.logger=t,this.writeObs=s??(()=>{}),this.transport=new AcpTransport(this.sendBridgeMessage,this.writeObs),this.state=new AcpGatewaySessionState(this.logger),this.core=new AcpGatewayBridgeCore({state:this.state,transport:this.transport,config:this.config,logger:this.logger,writeObs:this.writeObs,getGateway:()=>this.gateway}),this.events=new AcpGatewayEvents({state:this.state,transport:this.transport,config:this.config,logger:this.logger,writeObs:this.writeObs,completePrompt:(e,t)=>{this.core.completePrompt(e,t),void 0===e.pendingPromptId&&e.currentRunId&&(e.currentRunId=void 0,e.assistantStreamOwnerRunId=void 0,e.promptDone=void 0),this.actionRouter?.drainPending().catch(e=>{this.logger.warn?.(`[acp-server] Drain pending canvas actions failed: ${e instanceof Error?e.message:e}`)})},failPrompt:(e,t,s)=>{this.core.failPrompt(e,t,s),void 0===e.pendingPromptId&&e.currentRunId&&(e.currentRunId=void 0,e.assistantStreamOwnerRunId=void 0,e.promptDone=void 0),this.actionRouter?.clearPending()},maybeCompletePromptFromChatState:(e,t)=>this.core.maybeCompletePromptFromChatState(e,t),agentFetch:this.agentFetchFn}),this.terminalMgr=new TerminalManager,this.terminalHandler=new TerminalBridgeHandler({terminalMgr:this.terminalMgr,transport:this.transport,config:this.config,logger:this.logger,writeObs:this.writeObs}),this.terminalHandler.setupListeners(),this.core.handleTerminalRequest=e=>this.terminalHandler.handleRequest(e),this.core.handleTerminalNotification=(e,t)=>this.terminalHandler.handleNotification(e,t),this.core.handleCanvasNotification=(e,t)=>{this.handleCanvasNotificationInternal(e,t)},this.core.getCanvasState=()=>this.canvasState,this.core.handleSlashCommand=async e=>{const t=this.descriptorRegistry?.getBySlashCommand(e);return!(!t||!this.resourceRenderer)&&(await this.resourceRenderer.render(t,"inline"),!0)},this.core.onPromptStart=()=>{this.actionRouter?.clearPending()},this.state.startSessionGc(e=>this.core.cleanupPrompt(e))}async start(){const e=this.config.bridge.url,t=this.config.bridge.keyFile;if(e&&!BridgeClientCtor)try{const e=await import("@beeos-ai/bridge-client");BridgeClientCtor=e.BridgeClient}catch{this.logger.warn?.("[acp-server] Bridge SDK not available")}e&&t&&BridgeClientCtor?await this.startBridgeMode(e,t):this.logger.warn?.("[acp-server] Bridge not configured; running in standby (no ACP listener). Use handleUpgrade() for noServer mode."),await this.connectGateway()}async startBridgeMode(e,t){if(!BridgeClientCtor)throw new Error("@beeos-ai/bridge-client not available");const s=new BridgeClientCtor({bridgeUrl:e,service:this.config.bridge.service||"acp",keyFile:t});s.on("connected",()=>{this.logger.info?.("[acp-server] Connected to Bridge (publicKey auth)"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"bridge.connected",severity:"info"})}),s.on("disconnected",(...e)=>{const t=String(e[0]??"unknown");this.logger.warn?.(`[acp-server] Bridge disconnected: ${t}`),this.core.initialized=!1}),s.on("message",(...e)=>{const t=e[0],s="string"==typeof t?t:Buffer.from(t).toString("utf-8");try{const e=JSON.parse(s);"id"in e&&void 0!==e.id?this.core.handleRequest(e):this.core.handleNotification(e)}catch{this.logger.error?.("[acp-server] Invalid JSON-RPC message from Bridge")}}),s.on("error",(...e)=>{const t=e[0];this.logger.error?.("[acp-server] Bridge error:",t?.message)});try{await s.connect()}catch(e){this.logger.warn?.(`[acp-server] Initial bridge connect failed (will retry in background): ${e?.message}`)}const n={readyState:WebSocket.OPEN,send:(e,t)=>{try{s.send(e),t?.()}catch(e){t?.(e)}},close:()=>s.close(),terminate:()=>s.close(!1),on:()=>n,once:()=>n,removeListener:()=>n,removeAllListeners:()=>n,ping:()=>{}};Object.defineProperty(n,"readyState",{get:()=>"connected"===s.state?WebSocket.OPEN:WebSocket.CLOSED}),this.client=n,this.core.initialized=!1,this.logger.info?.("[acp-server] Running in Bridge mode (WS client)")}handleUpgrade(e,t,s){this.wss||(this.wss=new WebSocketServer({noServer:!0}),this.wss.on("connection",(e,t)=>this.handleConnection(e,t))),this.wss.handleUpgrade(e,t,s,t=>{this.wss.emit("connection",t,e)})}async connectGateway(){const e=this.config.gateway;this.gateway=new GatewayClient({url:e.url,token:e.token||"",clientId:e.clientId,openclawCanvasCompat:!!this.config.openclawCanvasCompat,retry:{baseMs:this.config.retry.baseMs,maxMs:this.config.retry.maxMs,maxAttempts:this.config.retry.maxAttempts}}),this.gateway.on("chat",e=>this.events.handleChatEvent(e)),this.gateway.on("agent",e=>this.events.handleAgentEvent(e)),this.gateway.on("cron",e=>this.events.handleCronEvent(e)),this.config.openclawCanvasCompat&&(this.gateway.on("canvas",e=>this.handleOpenClawCanvasEvent(e)),this.logger.info?.("[acp-server] OpenClaw native canvas compat enabled — listening for canvas events")),this.gateway.on("connected",()=>{this.logger.info?.("[acp-server] Connected to OpenClaw Gateway"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"gateway.connected",severity:"info"})}),this.gateway.on("disconnected",()=>{this.logger.warn?.("[acp-server] Gateway disconnected"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"gateway.disconnected",severity:"warn"}),this.core.cleanupAllPendingPrompts()}),this.gateway.on("reconnecting",e=>{this.logger.info?.(`[acp-server] Reconnecting to Gateway attempt=${e.attempt} delay=${Math.round(e.delayMs)}ms`)}),this.gateway.on("reconnect-exhausted",e=>{this.logger.error?.(`[acp-server] Gateway reconnect exhausted after ${e} attempts`)}),this.gateway.on("error",e=>{this.logger.error?.("[acp-server] Gateway error:",e.message)}),this.gateway.on("transport",e=>{this.writeObs({component:"gateway-client",domain:"lifecycle",name:`transport.${e.event}`,severity:"connect_fail"===e.event||"error"===e.event?"warn":"debug",payload:e})}),this.gateway.on("reconnect-notify-scheduled",e=>{this.logger.info?.(`[acp-server] Server-initiated reconnect scheduled in ${e.delayMs}ms`)}),this.gateway.on("reconnect-notify-fired",()=>{this.logger.info?.("[acp-server] Server-initiated reconnect firing")});try{await this.gateway.start()}catch(e){this.logger.error?.("[acp-server] Initial Gateway connect failed:",e)}}handleConnection(e,t){if(this.client){this.logger.warn?.("[acp-server] Replacing existing ACP client with new connection (takeover)");const e=this.client;this.client=null;try{e.close(1008,"replaced by new client")}catch{e.terminate()}}this.client=e,this.core.initialized=!1,this.logger.info?.("[acp-server] ACP client connected"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"client.connected",severity:"info"}),e.on("message",e=>{const t="string"==typeof e?e:e.toString("utf-8");try{const e=JSON.parse(t);"id"in e&&void 0!==e.id?this.core.handleRequest(e):this.core.handleNotification(e)}catch{this.logger.error?.("[acp-server] Invalid JSON-RPC message")}}),e.on("close",()=>{this.client===e&&(this.client=null,this.core.initialized=!1),this.logger.info?.("[acp-server] ACP client disconnected"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"client.disconnected",severity:"info"})})}pushToolResultContent(e,t,s,n){this.core.pushToolResultContent(e,t,s,n)}isCronCorrelatedRun(e){return this.core.isCronCorrelatedRun(e)}setCanvasState(e,t){this.canvasState=e;const s=(e,t)=>this.sendCanvasUpdate(e,t),n=async(e,t)=>{if(!this.gateway?.isConnected)throw new Error("Gateway not connected");return this.gateway.request(e,t)},r=t?.schemaRegistry??new FormSchemaRegistry,i=new FormSurfaceBuilder({canvasState:e,sendCanvasUpdate:s,gatewayRequest:n,logger:this.logger});this.formBuilder=i,this.schemaRegistry=r,this.descriptorRegistry=t?.descriptorRegistry??null,this.resourceRenderer=new ResourceSurfaceRenderer({canvasState:e,sendCanvasUpdate:s,gatewayRequest:n,schemaRegistry:r,formBuilder:i,logger:this.logger}),this.actionRouter=new CanvasActionRouter({stateManager:e,logger:this.logger,writeObs:e=>this.writeObs(e),forwardToAgent:e=>this.forwardActionToAgent(e)})}sendCanvasUpdate(e,t){const s=e&&"_active"!==e&&"_openclaw"!==e?e:this.state.lastActiveSessionId,n=s?this.state.sessions.get(s):void 0;if(!1===n?.canvasActive)return;const r=n?.sessionId??s;r&&this.transport.sendSessionUpdate(r,t)}isCanvasActive(e){const t=e??this.state.lastActiveSessionId,s=t?this.state.sessions.get(t):void 0;return!1!==s?.canvasActive}getActiveSessionId(){return this.state.lastActiveSessionId}getSessionIdForToolCall(e){if(e){const t=this.state.toolCallToSessionMap.get(e);if(t)return t;const s=e.split("_");if(s.length>=3&&"tc"===s[0]){const e=s[1],t=this.state.promptsByRunId.get(e);if(t?.sessionId)return t.sessionId}}for(const[,e]of this.state.sessions)if(void 0!==e.pendingPromptId&&!e.promptDone)return e.sessionId;return this.state.lastActiveSessionId}getSessionIdForActivePrompt(){return this.getSessionIdForToolCall()}getResourceRenderer(){return this.resourceRenderer}getDescriptorRegistry(){return this.descriptorRegistry}getFormBuilder(){return this.formBuilder}getSchemaRegistry(){return this.schemaRegistry}sendCanvasReset(e,t){const s=t??this.state.lastActiveSessionId,n=s?this.state.sessions.get(s):void 0,r=n?.sessionId??s;if(!r)return;const i={sessionUpdate:"canvas.reset"};e&&(i.surfaceId=e),this.transport.sendSessionUpdate(r,i)}async forwardActionToAgent(e){const t=this.state.resolveSessionFromParams(e);if(!t)return void this.logger.warn?.("[acp-server] forwardActionToAgent dropped — no session resolved from params");if(!this.gateway?.isConnected)return;if(t.currentRunId&&!t.promptDone)return this.actionRouter?.enqueue(e),void this.logger.debug?.("[acp-server] Agent busy, canvas action queued");const s=this.canvasState?.summarize(e.surfaceId??""),n=formatActionForAgent(e,s??void 0);try{const e=await this.gateway.agentSend(t.gatewaySessionKey,n,this.config.gateway.agentId||"main");t.currentRunId=e.runId,t.assistantStreamOwnerRunId=e.runId,t.promptDone=!1}catch(s){this.logger.warn?.(`[acp-server] Agent forward failed: ${s instanceof Error?s.message:s}`),this.transport.sendSessionUpdate(t.sessionId,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`> _User interacted with "${e.userAction?.name??"unknown"}" on canvas._`}})}}handleCanvasNotificationInternal(e,t){if(!this.canvasState)return void this.logger.warn?.(`[acp-server] Canvas notification ${e} ignored — canvasState not initialized (canvasEnabled=${this.config.canvasEnabled})`);this.canvasState,this.logger;if("canvas/action"===e)this.actionRouter?.route(t).catch(e=>{this.logger.warn?.(`[acp-server] Canvas action routing failed: ${e instanceof Error?e.message:e}`)});else if("canvas/toggle"===e){const e=t,s=!1!==e.enabled,n=this.state.resolveSessionFromParams(e);n&&(n.canvasActive=s,s&&(n.canvasPromptInjected=!1)),this.logger.info?.(`[acp-server] Canvas toggled: ${s}`),this.writeObs({component:"canvas",domain:"canvas",name:"canvas.toggle",severity:"info",payload:{enabled:s}})}else if("canvas/clear"===e){const e=t,s="string"==typeof e?.sessionId?e.sessionId:void 0,n=this.canvasState.getAllActive().length;this.canvasState.clear(),this.actionRouter?.reset(),this.sendCanvasReset(void 0,s),this.logger.info?.(`[acp-server] Canvas cleared (${n} surfaces removed)`),this.writeObs({component:"canvas",domain:"canvas",name:"canvas.cleared",severity:"info"})}else"canvas/delete"===e&&this.logger.debug?.("[acp-server] canvas/delete via ACP is deprecated — deletions should go through Relay")}handleOpenClawCanvasEvent(e){if(!this.canvasState||!e)return;const t=e,s=t.action;if("a2ui_push"===s||"surface_update"===s){const e=String(t.surfaceId??`openclaw-${Date.now()}`),s=t.components;if(s&&Array.isArray(s)){const t=sanitizeA2UIComponents(s);this.canvasState.has(e)?this.canvasState.update(e,t):this.canvasState.create(e,"inline",t,{origin:{kind:"openclaw"}}),this.logger.debug?.(`[acp-server] OpenClaw canvas → state + relay (surfaceId=${e})`)}}else if("navigate"===s||"url_open"===s){const e=String(t.url??"");if(e){const s=`openclaw-nav-${Date.now()}`,n=[{id:`${s}-embed`,type:"WebEmbed",properties:{url:e,title:String(t.title??e)}}];this.canvasState.create(s,"inline",n,{origin:{kind:"openclaw"}})}}else this.logger.debug?.(`[acp-server] Unhandled OpenClaw canvas action: ${s}`)}async stop(){this.state.destroy(),this.gateway&&(this.gateway.removeAllListeners(),this.gateway.stop()),this.client&&(this.client.removeAllListeners(),this.client.close()),this.terminalHandler.destroy(),this.terminalMgr.destroy(),this.wss&&(this.wss.removeAllListeners(),this.wss.close()),this.logger.info?.("[acp-server] Stopped"),this.writeObs({component:"acp-server",domain:"lifecycle",name:"lifecycle.shutdown",severity:"info"})}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function generateActionTools(e,r,t,o,s){const n=e.list();let i=0;for(const e of n)for(const s of e.itemActions){if("rpc"!==s.type)continue;const n=`${e.domain}_${s.id}`,c=s.description??`${s.id} a ${e.domain} item`;r.registerTool({name:n,description:c,parameters:{type:"object",required:["params"],properties:{params:{type:"object",description:`Parameters for ${s.rpc}. Pass the exact object to send to the RPC.`}}},async execute(r,n){const i=n,c=i.params??i;try{const r=await t(s.rpc,c);return await o.render(e),{success:!0,result:r}}catch(e){return{success:!1,error:e instanceof Error?e.message:String(e)}}}}),i++}s.info?.(`[action-tool-gen] Registered ${i} action tools from ${n.length} descriptor(s)`)}export const RENDER_RESOURCE_TOOL_NAME="render_resource";export function buildRenderResourceDescription(e){return["Render a domain resource list on Canvas.","Shows a table with data and action buttons. Use this to display resource lists to the user.","","Available resources:",e.describeAvailable()].join("\n")}export const RENDER_RESOURCE_TOOL_SCHEMA={type:"object",required:["domain"],properties:{domain:{type:"string",description:"The domain to render (e.g. 'cron')"}}};export function createRenderResourceHandler(e,r,t){return async(o,s)=>{const n=s.domain;if(!n)return{success:!1,error:"domain is required"};const i=e.get(n);return i?(await r.render(i),t.debug?.(`[render_resource] Rendered resource: ${n}`),{success:!0,domain:n,message:`Resource list "${i.title}" rendered on Canvas.`}):{success:!1,error:`No resource descriptor for "${n}". Available:\n${e.describeAvailable()}`}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class CanvasActionRouter{deps;lastActionTime=new Map;pendingActions=[];static DEBOUNCE_MS=300;static MAX_PENDING=5;constructor(t){this.deps=t}async route(t){const n=t.surfaceId??"",e=Date.now();if(e-(this.lastActionTime.get(n)??0)<CanvasActionRouter.DEBOUNCE_MS)return void this.deps.logger.debug?.(`[action-router] Debounced action on ${n}`);this.lastActionTime.set(n,e);const i=this.deps.stateManager.getActionHandler(n);if(i)try{await i(t),this.emitObs(t,"bound_handler")}catch(e){this.emitObs(t,"bound_handler_error"),this.deps.logger.warn?.(`[action-router] Bound handler for ${n} threw: ${e instanceof Error?e.message:e}`)}else this.emitObs(t,"agent_forward"),await this.deps.forwardToAgent(t)}enqueue(t){this.pendingActions.length>=CanvasActionRouter.MAX_PENDING&&this.pendingActions.shift(),this.pendingActions.push(t)}async drainPending(){if(0===this.pendingActions.length)return;const t=this.deduplicatePending();this.pendingActions=[];for(const n of t)await this.deps.forwardToAgent(n)}clearPending(){this.pendingActions=[]}reset(){this.lastActionTime.clear(),this.pendingActions=[]}deduplicatePending(){if(this.pendingActions.length<=1)return[...this.pendingActions];const t=new Map;for(const n of this.pendingActions)t.set(n.surfaceId??"",n);return Array.from(t.values())}emitObs(t,n){this.deps.writeObs({component:"canvas",domain:"canvas",name:"canvas.action_routed",severity:n.endsWith("_error")?"warn":"info",payload:{surfaceId:t.surfaceId,actionName:t.userAction?.name,resolution:n}})}}export function formatActionForAgent(t,n){const e=t.surfaceId??"unknown",i=t.userAction?.name??"unknown",s=t.userAction?.context;let o=`[Canvas Interaction] The user clicked "${i}" on surface "${e}"`;if(n?.title&&(o+=` ("${n.title}")`),o+=".",n?.types?.length&&(o+=` Surface contains: ${n.types.join(", ")}.`),s&&Object.keys(s).length>0){const t=JSON.stringify(s);o+=`\nAction context: ${t.length>2e3?t.slice(0,2e3)+"…(truncated)":t}`}return o+=`\nRespond to this interaction. If the surface needs updating, use render_ui with id="${e}".`,o}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{agentFetch}from"./agent-auth.js";export class CanvasApiClient{baseUrl;keys;constructor(s){this.baseUrl=s.platformUrl.replace(/\/+$/,""),this.keys=s.agentKeys}async request(s,a,t){const e=await agentFetch(`${this.baseUrl}${a}`,this.keys,{method:s,headers:{"Content-Type":"application/json"},body:t?JSON.stringify(t):void 0}),n=await e.json();if(!n.success)throw new Error(n.error??`Canvas API ${s} ${a} failed: ${e.status}`);return n.data??null}async getCanvasForSession(s){try{return await this.request("GET",`/api/v1/canvas-session/${s}`)}catch(s){const a=s instanceof Error?s.message:String(s);if(a.includes("404")||a.includes("not found")||a.includes("null"))return null;throw s}}async createCanvas(s){const a=await this.request("POST","/api/v1/canvas/agent",{title:s?.title??"Agent Canvas",sessionId:s?.sessionId});if(!a)throw new Error("Canvas creation returned no data");return a}async linkChat(s,a){await this.request("POST",`/api/v1/canvas/${s}/link`,{sessionId:a})}async ensureCanvasForSession(s){const a=await this.getCanvasForSession(s);if(a)return a.canvasId;return(await this.createCanvas({sessionId:s})).id}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const MSG_SYNC=0;export const MSG_AWARENESS=1;export const MSG_SYNC_STEP1=0;export const MSG_SYNC_STEP2=1;export const MSG_SYNC_UPDATE=2;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{CanvasWSProvider}from"./canvas-ws-provider.js";import{MSG_SYNC,MSG_SYNC_UPDATE}from"./canvas-constants.js";const MAX_SURFACES_PER_SESSION=20,MAX_STATE_MAP_BYTES=2097152,MAX_COMPONENTS_PER_SURFACE=500,RATE_LIMIT_PER_SURFACE_PER_SEC=30;export class CanvasStateManager{surfaces=new Map;actionHandlers=new Map;transport=null;activeCanvasId=null;lastResolvedSessionId=null;relayOpts=null;transportFactory;setRelayOpts(e){this.relayOpts=e}setTransportFactory(e){this.transportFactory=e}createTransport(e){return this.transportFactory?this.transportFactory(e):new CanvasWSProvider(e)}connectForCanvas(e,t){if(this.activeCanvasId===e&&this.transport?.connected)return void(t&&(this.lastResolvedSessionId=t));if(!this.relayOpts)return;this.transport&&this.transport.disconnect(),this.activeCanvasId&&this.activeCanvasId!==e&&this.clear(),this.activeCanvasId=e,this.lastResolvedSessionId=t??null,this.transport=this.createTransport(this.relayOpts);const s=this.relayOpts.logger;this.transport.connect(e,{onUpdate:e=>{s?.debug?.(`[canvas-state] Received update from relay: ${e.length} bytes`),this.processIncomingRelayMessage(e)},onAwareness:e=>{s?.debug?.(`[canvas-state] Received awareness from relay: ${e.length} bytes`)}})}disconnectFromCanvasService(){this.transport&&(this.transport.disconnect(),this.transport=null),this.activeCanvasId=null,this.lastResolvedSessionId=null}get canvasServiceConnected(){return null!==this.activeCanvasId&&null!==this.transport}get currentCanvasId(){return this.activeCanvasId}get currentSessionId(){return this.lastResolvedSessionId}syncDeleteToRelay(e){if(this.transport)try{const t=JSON.stringify({surfaceId:e,deleted:!0}),s=(new TextEncoder).encode(t),r=new Uint8Array(2+s.length);r[0]=MSG_SYNC,r[1]=MSG_SYNC_UPDATE,r.set(s,2),this.transport.sendUpdate(r)}catch{}}syncSurfaceToRelay(e,t,s){if(this.transport)try{const r=JSON.stringify({surfaceId:e,components:t,presentation:s??"panel"}),a=(new TextEncoder).encode(r),n=new Uint8Array(2+a.length);n[0]=MSG_SYNC,n[1]=MSG_SYNC_UPDATE,n.set(a,2),this.transport.sendUpdate(n),this.relayOpts?.logger?.info?.(`[canvas-state] Queued/sent ${n.length} bytes to relay for surfaceId=${e}`)}catch(e){this.relayOpts?.logger?.warn?.(`[canvas-state] syncSurfaceToRelay failed: ${e}`)}}processIncomingRelayMessage(e){if(!(e.length<3)&&e[0]===MSG_SYNC&&e[1]===MSG_SYNC_UPDATE)try{const t=(new TextDecoder).decode(e.subarray(2)),s=JSON.parse(t);if(!s.surfaceId)return;s.deleted&&(this.removeSurface(s.surfaceId),this.relayOpts?.logger?.info?.(`[canvas-state] Surface ${s.surfaceId} deleted by remote participant`))}catch{}}has(e){return this.surfaces.has(e)}get(e){return this.surfaces.get(e)}getAll(){return Array.from(this.surfaces.values())}getAllActive(){return Array.from(this.surfaces.values())}create(e,t,s,r){this.enforceMaxSurfaces();const a={surfaceId:e,presentation:t,components:s,dataModels:new Map,origin:r?.origin??{kind:"agent"},createdAt:Date.now(),updatedAt:Date.now(),tokenBucket:{tokens:30,lastRefill:Date.now()}};return this.surfaces.set(e,a),r?.actionHandler&&this.actionHandlers.set(e,r.actionHandler),this.enforceMemoryBudget(),this.syncSurfaceToRelay(e,s,t),a}update(e,t){const s=this.surfaces.get(e);if(s)return s.components=t,s.updatedAt=Date.now(),this.enforceMemoryBudget(),this.syncSurfaceToRelay(e,t,s.presentation),s}delete(e){const t=this.removeSurface(e);return t&&this.syncDeleteToRelay(e),t}clear(){this.actionHandlers.clear(),this.surfaces.clear()}get size(){return this.surfaces.size}getActionHandler(e){return this.actionHandlers.get(e)}bindActionHandler(e,t){this.actionHandlers.set(e,t)}summarize(e){const t=this.surfaces.get(e);if(!t||!Array.isArray(t.components))return null;const s=t.components,r=[...new Set(s.map(e=>e.type).filter(Boolean))],a=s.find(e=>e.properties?.title);return{types:r,title:a?.properties?.title}}checkRateLimit(e){const t=this.surfaces.get(e);if(!t)return!0;const s=Date.now(),r=(s-t.tokenBucket.lastRefill)/1e3;return t.tokenBucket.tokens=Math.min(30,t.tokenBucket.tokens+30*r),t.tokenBucket.lastRefill=s,t.tokenBucket.tokens>=1&&(t.tokenBucket.tokens-=1,!0)}validateComponentCount(e){return!Array.isArray(e)||e.length<=500}removeSurface(e){return this.actionHandlers.delete(e),this.surfaces.delete(e)}enforceMaxSurfaces(){if(this.surfaces.size<20)return;let e,t=1/0;for(const[s,r]of this.surfaces)r.createdAt<t&&(t=r.createdAt,e=s);e&&this.removeSurface(e)}enforceMemoryBudget(){let e=0;for(const[,t]of this.surfaces)e+=this.estimateSize(t);if(e<=2097152)return;const t=Array.from(this.surfaces.entries()).sort(([,e],[,t])=>e.updatedAt-t.updatedAt);for(const[s,r]of t){if(e<=2097152)break;e-=this.estimateSize(r),this.removeSurface(s)}}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}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function safeStr(e){if(null==e)return"";if("string"==typeof e)return e;if("number"==typeof e||"boolean"==typeof e)return 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 String(t[e]);try{return JSON.stringify(e)}catch{return"[Object]"}}return String(e)}let componentIdCounter=0;function nextCid(){return`c_${++componentIdCounter}_${Date.now().toString(36)}`}const EMBEDDABLE_DOMAINS=[/youtube\.com\/(?:watch|embed|shorts)/i,/youtu\.be\//i,/vimeo\.com\//i,/dailymotion\.com\//i,/google\.com\/maps/i,/maps\.google\./i,/openstreetmap\.org/i,/codepen\.io\//i,/codesandbox\.io\//i,/stackblitz\.com\//i,/figma\.com\/(?:file|embed|proto|design)/i,/miro\.com\/app\/board/i,/spotify\.com\/(?:embed|track|album|playlist)/i,/soundcloud\.com\//i,/twitter\.com\/.*\/status/i,/x\.com\/.*\/status/i,/loom\.com\/share/i,/notion\.so\//i,/airtable\.com\//i,/excalidraw\.com\//i,/canva\.com\/design/i,/docs\.google\.com\//i,/drive\.google\.com\//i,/slides\.google\.com\//i,/sheets\.google\.com\//i,/github\.com\/.*\/blob/i,/gist\.github\.com\//i,/jsfiddle\.net\//i,/replit\.com\//i,/observable\.com\//i,/datawrapper\.dwcdn\.net/i,/grafana\./i,/metabase\./i,/retool\./i,/streamlit\./i,/huggingface\.co\/spaces/i];function isEmbeddableUrl(e){return!("string"!=typeof e||!e)&&EMBEDDABLE_DOMAINS.some(t=>t.test(e))}function translateComponent(e){if(!e||"object"!=typeof e)return[{id:nextCid(),type:"Text",properties:{text:"[invalid component]"}}];const t=nextCid(),r="string"==typeof e.type?e.type.toLowerCase():"text";switch(r){case"card":return buildCard(t,e);case"table":return[buildTable(t,e)];case"form":return[buildForm(t,e)];case"text":return[{id:t,type:"Text",properties:{text:String(e.content??"")}}];case"image":return[{id:t,type:"Image",properties:{src:String(e.url??""),alt:String(e.alt??"")}}];case"button":return[{id:t,type:"Button",properties:{label:String(e.label??"Button"),actionId:String(e.actionId??t)}}];case"grid":{const r=(Array.isArray(e.children)?e.children:Array.isArray(e.components)?e.components:[]).flatMap(e=>translateComponent(e));return[{id:t,type:"Grid",properties:{columns:e.columns??2,gap:e.gap??"md",responsive:!1!==e.responsive},children:r.filter((e,t)=>t===r.findIndex(e=>e.id===r[t]?.id)).map(e=>e.id)},...r]}case"row":case"column":return(Array.isArray(e.children)?e.children:[]).flatMap(e=>translateComponent(e));case"web_embed":return[{id:t,type:"WebEmbed",properties:{url:e.url,title:e.title,aspectRatio:e.aspectRatio??"16:9"}}];case"video":case"map":case"file_preview":case"audio":return[{id:t,type:"WebEmbed",properties:{url:e.url,title:e.title,aspectRatio:"16:9"}}];case"chart":return[{id:t,type:"Chart",properties:{chartType:e.chartType,title:e.title,labels:e.labels,datasets:e.datasets,value:e.value,max:e.max,stages:e.stages}}];case"code":return[{id:t,type:"CodeBlock",properties:{language:String(e.language??""),code:String(e.code??"")}}];case"markdown":return[{id:t,type:"Markdown",properties:{content:String(e.content??"")}}];case"tabs":{const r=Array.isArray(e.tabs)?e.tabs:Array.isArray(e.children)?e.children:Array.isArray(e.items)?e.items:[];if(0===r.length)return[{id:t,type:"Text",properties:{text:safeStr(e.title??e.label??"")}}];const a=[];for(const e of r){const t=safeStr(e.label??e.title??e.id??"");t&&a.push({id:nextCid(),type:"Markdown",properties:{content:`### ${t}`}});const r=Array.isArray(e.children)?e.children:[];a.push(...r.flatMap(e=>translateComponent(e)))}return a}case"accordion":{const r=Array.isArray(e.items)?e.items:Array.isArray(e.children)?e.children:[];return r.length>0?r.flatMap(e=>[{id:nextCid(),type:"Text",properties:{text:safeStr(e.title??e.label??""),variant:"heading"}},...Array.isArray(e.children)?e.children.flatMap(e=>translateComponent(e)):[]]):[{id:t,type:"Text",properties:{text:safeStr(e.title??"")}}]}case"alert":return[{id:t,type:"Alert",properties:{variant:e.variant??"info",title:e.title,message:e.message,dismissible:e.dismissible??!1}}];case"progress":{const r=Number(e.value??0),a=Number(e.max??100),n=a>0?Math.round(r/a*100):0;return[{id:t,type:"Text",properties:{text:`${safeStr(e.label)||"Progress"}: ${n}% (${r}/${a})`}}]}case"stat":return[{id:t,type:"Stat",properties:{label:e.label,value:e.value,change:e.change,changeType:e.changeType,description:e.description,icon:e.icon}}];case"badge":return[{id:t,type:"Text",properties:{text:safeStr(e.text??e.label)}}];case"divider":return[{id:t,type:"Text",properties:{text:safeStr(e.label)||"---"}}];case"rating":return[{id:t,type:"Text",properties:{text:`Rating: ${String(e.value??0)}/${String(e.max??5)}`}}];case"timeline":{const r=(Array.isArray(e.items)?e.items:[]).map(e=>`${safeStr(e.time??e.date??"")} — ${safeStr(e.title??"")}${e.description?`: ${safeStr(e.description)}`:""}`);return[{id:t,type:"Markdown",properties:{content:(e.title?`**${safeStr(e.title)}**\n`:"")+r.join("\n")}}]}case"avatar":return[{id:t,type:"Text",properties:{text:safeStr(e.name??"")}}];case"callout":return[{id:t,type:"Alert",properties:{variant:"tip"===e.variant?"success":"warning"===e.variant||"caution"===e.variant?"warning":"important"===e.variant?"error":"info",title:e.title,message:e.content??""}}];case"list":return[{id:t,type:"List",properties:{title:e.title,items:e.items??[],ordered:e.ordered??!1,variant:e.variant}}];case"json_viewer":{let r;try{r="string"==typeof e.data?e.data:JSON.stringify(e.data,null,2)}catch{r=String(e.data??"")}return[{id:t,type:"Markdown",properties:{content:`${e.title?`**${safeStr(e.title)}**\n`:""}\`\`\`json\n${r}\n\`\`\``}}]}case"carousel":return[{id:t,type:"List",properties:{title:e.title,items:e.items??[]}}];case"key_value":return[{id:t,type:"KeyValue",properties:{title:e.title,items:e.items??[],variant:e.variant}}];case"link":return isEmbeddableUrl(e.url)?[{id:t,type:"WebEmbed",properties:{url:e.url,title:String(e.text??e.label??""),aspectRatio:"16:9"}}]:[{id:t,type:"Link",properties:{text:e.text??e.label,url:e.url,variant:e.variant??"default"}}];default:return[{id:t,type:"Text",properties:{text:`[Unsupported: ${r}]`}}]}}function buildCard(e,t){const r={};t.title&&(r.title=safeStr(t.title)),t.subtitle&&(r.subtitle=safeStr(t.subtitle)),t.description&&(r.description=safeStr(t.description)),t.image&&(r.image=safeStr(t.image)),t.icon&&(r.icon=safeStr(t.icon)),t.badge&&(r.badge=safeStr(t.badge)),t.badgeVariant&&(r.badgeVariant=safeStr(t.badgeVariant)),t.footer&&(r.footer=safeStr(t.footer)),t.url&&(r.url=safeStr(t.url)),t.layout&&(r.layout=safeStr(t.layout));const a=Array.isArray(t.actions)?t.actions:[];a.length>0&&(r.actions=a.filter(e=>e&&"object"==typeof e).map(e=>({label:safeStr(e.label??"Action"),actionId:safeStr(e.actionId??e.id??nextCid()),variant:e.variant})));const n=[],i=Array.isArray(t.fields)?t.fields:[];for(const e of i)e&&"object"==typeof e&&n.push({id:nextCid(),type:"Text",properties:{text:`${safeStr(e.label)}: ${safeStr(e.value)}`}});const s={id:e,type:"Card",properties:r};return n.length>0?(s.children=n.map(e=>e.id),[s,...n]):[s]}function buildTable(e,t){const r=Array.isArray(t.columns)?t.columns:[],a=Array.isArray(t.rows)?t.rows:[],n=[],i=[];for(const e of r)if("string"==typeof e)n.push(e),i.push(e);else if(e&&"object"==typeof e){const t=e,r=safeStr(t.key??t.id??t.field??t.name??t.label??""),a=safeStr(t.label??t.title??t.header??t.name??r);n.push(a),i.push(r)}else{const t=safeStr(e);n.push(t),i.push(t)}const s=a.map(e=>{if(Array.isArray(e))return e;if(e&&"object"==typeof e&&i.length>0){const t=e;return i.map(e=>{const r=Object.keys(t).find(t=>t===e||t.toLowerCase()===e.toLowerCase());return null!=r?t[r]:""})}return e});return{id:e,type:"Table",properties:{title:safeStr(t.title),columns:n,rows:s}}}function buildForm(e,t){return{id:e,type:"Form",properties:{title:safeStr(t.title),description:t.description,fields:t.fields??[],submitLabel:safeStr(t.submitLabel)||"Submit",submitActionId:t.submitActionId??`${e}_submit`}}}const A2UI_VERSION="0.8",BEEOS_EXTENDED_CATALOG="https://beeos.ai/a2ui/catalog/v1",EXTENDED_TYPES=new Set(["WebEmbed","Chart","CodeBlock","Markdown","Alert","Stat","List","KeyValue","Link","Grid"]);function hasExtendedComponent(e){return e.some(e=>EXTENDED_TYPES.has(e.type))}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",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",description:"Image URL for card header or list item thumbnail"},subtitle:{type:"string",description:"Card subtitle or list item subtitle"},badge:{type:"string",description:"Card/list badge text (e.g. price, status)"},badgeVariant:{type:"string",enum:["default","success","warning","error","info"]},footer:{type:"string",description:"Card footer text"},layout:{type:"string",enum:["vertical","horizontal"],description:"Card layout: vertical (image top) or horizontal (image left)"},actions:{type:"array",description:"Card action buttons: [{label, actionId, variant?}]"},fields:{type:"array",description:"For card: [{label,value}]. For form: [{name,label,inputType,options,...}]"},children:{type:"array",description:"For grid: nested child components (card, stat, image, etc.)"},gap:{type:"string",enum:["sm","md","lg"],description:"Grid gap size"},columns:{type:"array",description:'Column headers or grid column count (1-6). Table: ["Name","Status"]. Grid: number.'},rows:{type:"array",description:'Row data as arrays matching column order, e.g. [["prod-1", "running"]]'},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",description:"For funnel chart: [{label,value}]"},labels:{type:"array"},datasets:{type:"array"},language:{type:"string"},code:{type:"string"},items:{type:"array",description:"For list: [{title,subtitle,description,image,icon,badge,value,url}]. For key_value: [{key,value,copyable}]"},variant:{type:"string",description:"Alert: info/success/warning/error. Link: default/button/subtle"},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){const{stateManager:r,canvasApiClient:a,logger:n}=e;if((!r.canvasServiceConnected||r.currentSessionId!==t)&&a)try{const e=await a.ensureCanvasForSession(t);r.connectForCanvas(e,t),n.info?.(`[canvas] Relay connected for canvas=${e} session=${t}`)}catch(e){n.warn?.(`[canvas] Failed to connect relay: ${e}`)}}export function createRenderUiHandler(e){const{stateManager:t,logger:r,writeObs:a,isCanvasActive:n}=e;return async(i,s)=>{if(n&&!n())return{success:!1,error:"Canvas is disabled by user. Respond with plain text instead."};const o=s,c=o.id,l="panel";let d=o.components;if("string"==typeof d)try{d=JSON.parse(d)}catch{}if(!c||!Array.isArray(d)||!d.length)return{success:!1,error:"id and components (array) are required"};if(!t.validateComponentCount(d))return{success:!1,error:"Too many components (max 500)"};if(!t.checkRateLimit(c))return r.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 p=d.flatMap(translateComponent),u=t.has(c),m=hasExtendedComponent(p)?BEEOS_EXTENDED_CATALOG:void 0,g=e.getActiveSessionId?.(i);if(!g)return r.warn?.(`[canvas] No active session for toolCallId=${i}, skipping render_ui`),{success:!1,error:"No active session — canvas operation skipped"};await ensureRelayConnected(e,g),u?t.update(c,p):t.create(c,l,p,{origin:{kind:"agent"}}),e.sendCanvasUpdate(g,{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:c,presentation:l,message:{surfaceUpdate:{surfaceId:c,components:p,catalogId:m}}});const y=d.map(e=>e.type);a({component:"canvas",domain:"canvas",name:u?"canvas.update_sent":"canvas.surface_created",severity:"info",payload:{surfaceId:c,presentation:l,componentCount:d.length,componentTypes:y}}),r.debug?.(`[canvas] ${u?"Updated":"Created"} surface=${c} components=${d.length}`);return{success:!0,surfaceId:c,presentation:l,isUpdate:u,activeSurfaces:t.getAllActive().map(e=>e.surfaceId)}}}export function createCloseUiHandler(e){const{stateManager:t,logger:r,writeObs:a}=e;return async(n,i)=>{const s=i.id;if(!s)return{success:!1,error:"id is required"};const o=t.get(s),c=o?Date.now()-o.createdAt:0;t.delete(s);const l=e.getActiveSessionId?.(n);return l?(e.sendCanvasUpdate(l,{sessionUpdate:"canvas.reset",surfaceId:s}),a({component:"canvas",domain:"canvas",name:"canvas.surface_closed",severity:"info",payload:{surfaceId:s,lifetime_ms:c}}),r.debug?.(`[canvas] Closed surface=${s}`),{success:!0,surfaceId:s,message:"UI closed"}):(r.warn?.(`[canvas] No active session for toolCallId=${n}, close_ui local-only`),{success:!0,surfaceId:s,message:"UI closed (local only)"})}}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, badgeVariant, footer, url, layout:vertical/horizontal, actions:[{label,actionId}]), table (columns, rows), stat (label, value, change, changeType, icon), key_value (items:[{key,value,copyable}]), list (items:[{title,subtitle,description,image,icon,badge,value,url}])\n\n**Chart**: chart (chartType, labels, datasets) — chartType: bar, horizontal_bar, line, area, scatter, pie, doughnut, radar, gauge (value,max), candlestick, funnel (stages:[{label,value}]), sparkline\n\n**Text & Code**: text (content), markdown (content), code (language, code)\n\n**Media**: image (url, alt), web_embed (url, aspectRatio — embed ANY web page/app as iframe: YouTube, Maps, Figma, dashboards, docs, tools, etc.)\n\n**Feedback**: alert (variant:info/success/warning/error, title, message)\n\n**Interactive**: form (fields:[{name,label,inputType,options,placeholder,required}]), button (label, actionId), link (text, url, variant:default/button/subtle)\n\n**web_embed vs link**: Use `web_embed` when the URL should be displayed inline as embedded content. Use `link` only for plain navigation.\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", url}])\n- **Article card**: card(image, title, description, url, layout:"horizontal")\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 (charts, tables, stats, key-value, lists, cards, grids, alerts, forms, links, embeds, images, code, markdown). 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, url, actions, layout:horizontal/vertical. List items support: image, title, subtitle, description, badge, value, url. Charts: bar, horizontal_bar, line, area, scatter, pie, doughnut, radar, gauge, candlestick, funnel, sparkline. Use web_embed for embedding URLs as iframes. Use link for navigation. 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){const t=e.getAllActive();if(0===t.length)return null;const r=[...t].sort((e,t)=>t.updatedAt-e.updatedAt).slice(0,8),a=t.length-r.length;let n=`[Canvas State] Active surfaces: ${r.map(t=>{const r=e.summarize(t.surfaceId);if(!r)return`"${t.surfaceId}"`;const a=r.types.slice(0,3).join(", "),n=r.title?`: "${r.title}"`:"";return`"${t.surfaceId}" (${a}${n})`}).join("; ")}.`;return a>0&&(n+=` ...and ${a} more.`),n+=" To modify existing content, reuse its id in render_ui.",n.length>500&&(n=n.slice(0,497)+"..."),n}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import WebSocket from"ws";import{agentAuthHeaders}from"./agent-auth.js";import{MSG_SYNC,MSG_AWARENESS}from"./canvas-constants.js";const RECONNECT_BASE_MS=1e3,RECONNECT_MAX_MS=3e4;export class CanvasWSProvider{ws=null;canvasId=null;opts;reconnectAttempts=0;reconnectTimer=null;closed=!1;onUpdate=null;onAwareness=null;pendingMessages=[];constructor(s){this.opts=s}connect(s,e){this.canvasId=s,this.onUpdate=e.onUpdate,this.onAwareness=e.onAwareness??null,this.closed=!1,this.reconnectAttempts=0,this.doConnect()}doConnect(){if(this.closed||!this.canvasId)return;const s=`/ws/agent/${this.canvasId}`,e=`${this.opts.canvasRelayUrl.replace(/^http/,"ws")}${s}`;this.opts.logger?.info?.(`[canvas-ws] Connecting to ${e}`);const t=agentAuthHeaders("GET",s,this.opts.agentKeys),n=new WebSocket(e,{headers:t});n.binaryType="arraybuffer",n.on("open",()=>{this.opts.logger?.info?.(`[canvas-ws] Connected to canvas ${this.canvasId}`),this.reconnectAttempts=0,this.ws=n;for(const s of this.pendingMessages)n.send(s);this.pendingMessages=[]}),n.on("message",s=>{const e=new Uint8Array(s instanceof ArrayBuffer?s:s.buffer.slice(s.byteOffset,s.byteOffset+s.byteLength));if(0===e.length)return;const t=e[0];t===MSG_AWARENESS&&this.onAwareness?this.onAwareness(e):t===MSG_SYNC&&this.onUpdate&&this.onUpdate(e)}),n.on("close",()=>{this.opts.logger?.info?.(`[canvas-ws] Disconnected from canvas ${this.canvasId}`),this.ws=null,this.scheduleReconnect()}),n.on("error",s=>{this.opts.logger?.warn?.("[canvas-ws] Error:",s.message),n.close()})}scheduleReconnect(){if(this.closed)return;const s=Math.min(1e3*Math.pow(2,this.reconnectAttempts),3e4);this.reconnectAttempts++,this.opts.logger?.debug?.(`[canvas-ws] Reconnecting in ${s}ms (attempt ${this.reconnectAttempts})`),this.reconnectTimer=setTimeout(()=>{this.doConnect()},s)}sendUpdate(s){this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(s):this.pendingMessages.push(s)}sendAwareness(s){this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(s):this.pendingMessages.push(s)}disconnect(){this.closed=!0,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.ws&&(this.ws.close(),this.ws=null),this.canvasId=null,this.pendingMessages=[]}get connected(){return null!==this.ws&&this.ws.readyState===WebSocket.OPEN}get currentCanvasId(){return this.canvasId}}
|
package/dist/config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mkdirSync,writeFileSync}from"node:fs";import{homedir}from"node:os";import{dirname,join}from"node:path";function trimStr(e){if("string"==typeof e){const
|
|
1
|
+
import{mkdirSync,writeFileSync}from"node:fs";import{homedir}from"node:os";import{dirname,join}from"node:path";function trimStr(e){if("string"==typeof e){const o=e.trim();return o.length>0?o:void 0}}function readInt(e){if("number"==typeof e&&Number.isFinite(e))return Math.trunc(e);if("string"==typeof e){const o=parseInt(e,10);return Number.isFinite(o)?o:void 0}}function readBool(e){return"boolean"==typeof e?e:"true"===e||"false"!==e&&void 0}function pick(e,o){for(const o of e)if(void 0!==o.value)return{value:o.value,source:o.source};return o}const DEFAULT_GATEWAY_URL="ws://127.0.0.1:18789",DEFAULT_LOG_DIR=join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"beeos-logs"),DEFAULT_OPENCLAW_CONFIG_PATH=join(homedir(),".openclaw","openclaw.json");export function resolveConfigWithMeta(e){const o=e.pluginConfig??{},r=e.openclawConfig??{},a=e.openclawConfigPath??DEFAULT_OPENCLAW_CONFIG_PATH,u=pick([{source:"pluginConfig",value:trimStr(o.platformUrl)},{source:"env",value:trimStr(process.env.BEEOS_PLATFORM_URL)}],{source:"default",value:""}),l=o.gateway??{},n=(r.gateway??{}).auth??{},t=pick([{source:"pluginConfig",value:trimStr(l.url)},{source:"env",value:trimStr(process.env.BEEOS_GATEWAY_URL)}],{source:"default",value:DEFAULT_GATEWAY_URL}),s=pick([{source:"openclawConfig",value:trimStr(n.token)},{source:"pluginConfig",value:trimStr(l.token)},{source:"env",value:trimStr(process.env.BEEOS_GATEWAY_TOKEN)}],{source:"default",value:""}),i=pick([{source:"pluginConfig",value:readInt(l.protocol)}],{source:"default",value:3}),c=pick([{source:"pluginConfig",value:trimStr(l.clientId)}],{source:"default",value:`beeos-claw-${process.pid}`}),v=pick([{source:"pluginConfig",value:trimStr(l.agentId)}],{source:"default",value:"main"}),p=o.bridge??{},d=pick([{source:"pluginConfig",value:trimStr(p.url)},{source:"env",value:trimStr(process.env.BRIDGE_URL)}],{source:"default",value:""}),m=pick([{source:"pluginConfig",value:trimStr(p.keyFile)},{source:"env",value:trimStr(process.env.BRIDGE_KEY_FILE)}],{source:"default",value:""}),f=pick([{source:"pluginConfig",value:trimStr(p.service)},{source:"env",value:trimStr(process.env.BRIDGE_SERVICE)}],{source:"default",value:"acp"}),g=pick([{source:"pluginConfig",value:readBool((p.shell??{}).enabled)},{source:"env",value:readBool(process.env.BEEOS_SHELL_ENABLED)}],{source:"default",value:!0}),E=o.retry??{},_=pick([{source:"pluginConfig",value:readInt(E.baseMs)}],{source:"default",value:1e3}),S=pick([{source:"pluginConfig",value:readInt(E.maxMs)}],{source:"default",value:6e5}),M=pick([{source:"pluginConfig",value:readInt(E.maxAttempts)}],{source:"default",value:0}),I=o.log??{},O=pick([{source:"pluginConfig",value:readBool(I.enabled)},{source:"env",value:readBool(process.env.BEEOS_LOG_ENABLED)}],{source:"default",value:!0}),T=pick([{source:"pluginConfig",value:readBool(I.verbose)}],{source:"default",value:!1}),C=pick([{source:"pluginConfig",value:trimStr(I.dir)}],{source:"default",value:DEFAULT_LOG_DIR}),A=pick([{source:"pluginConfig",value:readInt(p.promptTimeoutMs)},{source:"env",value:readInt(process.env.BEEOS_PROMPT_TIMEOUT_MS)}],{source:"default",value:3e5}),R=pick([{source:"pluginConfig",value:readInt(o.terminalWsPort)},{source:"env",value:readInt(process.env.BEEOS_TERMINAL_WS_PORT)}],{source:"default",value:18801}),k=trimStr(o.terminalWsAllowedOrigins)??trimStr(process.env.BEEOS_TERMINAL_WS_ALLOWED_ORIGINS),L=pick([{source:k?"pluginConfig":"default",value:k?k.split(",").map(e=>e.trim()).filter(Boolean):void 0}],{source:"default",value:["localhost","127.0.0.1","::1"]}),h=pick([{source:"pluginConfig",value:readInt(o.streamIdleTimeoutMs)},{source:"env",value:readInt(process.env.BEEOS_STREAM_IDLE_TIMEOUT_MS)}],{source:"default",value:15e3}),w=pick([{source:"pluginConfig",value:readInt(o.streamStaleTimeoutMs)},{source:"env",value:readInt(process.env.BEEOS_STREAM_STALE_TIMEOUT_MS)}],{source:"default",value:6e5}),y=pick([{source:"pluginConfig",value:readInt(o.loadReplayIdleTimeoutMs)},{source:"env",value:readInt(process.env.BEEOS_LOAD_REPLAY_IDLE_TIMEOUT_MS)}],{source:"default",value:15e3}),B=pick([{source:"pluginConfig",value:readInt(o.cronFlushTimeoutMs)},{source:"env",value:readInt(process.env.BEEOS_CRON_FLUSH_TIMEOUT_MS)}],{source:"default",value:12e4}),b=pick([{source:"pluginConfig",value:readInt(o.cronSignalWindowMs)},{source:"env",value:readInt(process.env.BEEOS_CRON_SIGNAL_WINDOW_MS)}],{source:"default",value:6e4}),P=pick([{source:"pluginConfig",value:readBool(o.canvasEnabled)},{source:"env",value:readBool(process.env.BEEOS_CANVAS_ENABLED)}],{source:"default",value:!0}),N=pick([{source:"pluginConfig",value:readBool(o.openclawCanvasCompat)},{source:"env",value:readBool(process.env.BEEOS_OPENCLAW_CANVAS_COMPAT)}],{source:"default",value:!1}),W={platformUrl:u.value.replace(/\/+$/,""),bridge:{url:d.value,keyFile:m.value,service:f.value,mode:trimStr(p.mode),shell:{enabled:g.value},promptTimeoutMs:A.value,historyPendingTimeoutMs:readInt(p.historyPendingTimeoutMs)},gateway:{url:t.value,token:s.value,protocol:i.value,clientId:c.value,agentId:v.value},retry:{baseMs:Math.max(0,_.value),maxMs:Math.max(0,S.value),maxAttempts:Math.max(0,M.value)},log:{enabled:O.value,verbose:T.value,dir:C.value},promptTimeoutMs:A.value,terminalWsPort:R.value,terminalWsAllowedOrigins:L.value,streamIdleTimeoutMs:Math.max(0,h.value),streamStaleTimeoutMs:Math.max(0,w.value),loadReplayIdleTimeoutMs:Math.max(0,y.value),cronFlushTimeoutMs:Math.max(0,B.value),cronSignalWindowMs:Math.max(0,b.value),canvasEnabled:P.value,openclawCanvasCompat:N.value},F={platformUrl:u.source,bridge:{url:d.source,keyFile:m.source,service:f.source,shell:{enabled:g.source}},gateway:{url:t.source,token:s.source,protocol:i.source,clientId:c.source,agentId:v.source},retry:{baseMs:_.source,maxMs:S.source,maxAttempts:M.source},log:{enabled:O.source,verbose:T.source,dir:C.source},promptTimeoutMs:A.source,terminalWsPort:R.source,terminalWsAllowedOrigins:L.source,streamIdleTimeoutMs:h.source,streamStaleTimeoutMs:w.source,loadReplayIdleTimeoutMs:y.source,cronFlushTimeoutMs:B.source,cronSignalWindowMs:b.source,canvasEnabled:P.source,openclawCanvasCompat:N.source},U=[];return W.platformUrl||U.push({code:"CONFIG_PLATFORM_URL_MISSING",severity:"error",message:"platformUrl is required for file uploads and platform callbacks",nextSteps:[`Set plugins.entries.beeos-claw.config.platformUrl in ${a}`,"Or set the BEEOS_PLATFORM_URL environment variable"]}),W.gateway.token||U.push({code:"CONFIG_GATEWAY_TOKEN_MISSING",severity:"warn",message:"gateway.token is empty; Gateway auth may fail if authentication is enabled",nextSteps:[`Set gateway.auth.token in ${a}`,`Or set plugins.entries.beeos-claw.config.gateway.token in ${a}`]}),{config:W,sources:F,validation:U,openclawConfigPath:a}}export function resolveConfig(e){const{config:o,validation:r}=resolveConfigWithMeta({pluginConfig:e}),a=r.find(e=>"error"===e.severity);if(a)throw new Error(`[beeos-claw] ${a.message}`);return o}const MIRROR_PATH=join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"beeos-claw","openclaw.json");export function mirrorOpenClawConfig(e){if(!e||"object"!=typeof e||Array.isArray(e))return{copied:!1,destinationPath:MIRROR_PATH,reason:"config_invalid"};try{return mkdirSync(dirname(MIRROR_PATH),{recursive:!0}),writeFileSync(MIRROR_PATH,JSON.stringify(e,null,2)+"\n",{encoding:"utf8",mode:384}),{copied:!0,destinationPath:MIRROR_PATH}}catch(e){return{copied:!1,destinationPath:MIRROR_PATH,reason:e instanceof Error?e.message:"write_failed"}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function formatSchedule(e,t){const a=e;if(!a)return"—";const r=a.kind;if("cron"===r)return a.expr??"—";if("every"===r){const e=a.everyMs??0;return e>=864e5?`Every ${Math.round(e/864e5)}d`:e>=36e5?`Every ${Math.round(e/36e5)}h`:e>=6e4?`Every ${Math.round(e/6e4)}m`:`Every ${Math.round(e/1e3)}s`}return"at"===r?`At ${a.at??"—"}`:"—"}export const CRON_RESOURCE={domain:"cron",title:"Scheduled Tasks",listRpc:"cron.list",listParams:{includeDisabled:!0,limit:50},extractItems:e=>e?.jobs??[],itemId:e=>e.id,columns:[{key:"name",label:"Name"},{key:"schedule",label:"Schedule",format:formatSchedule},{key:"enabled",label:"Status",format:e=>e?"Enabled":"Disabled"},{key:"state.lastRunStatus",label:"Last Run",format:e=>String(e??"—")},{key:"state.nextRunAtMs",label:"Next Run",format:e=>e?new Date(Number(e)).toLocaleString():"—"}],itemActions:[{id:"toggle",type:"rpc",rpc:"cron.update",label:e=>e.enabled?`Disable "${e.name}"`:`Enable "${e.name}"`,buildParams:e=>({id:e.id,patch:{enabled:!e.enabled}}),description:"Toggle a cron job on/off"},{id:"edit",type:"form",formDomain:"cron",formOperation:"update",label:e=>`Edit "${e.name}"`,buildFormData:e=>e},{id:"run",type:"rpc",rpc:"cron.run",label:e=>`Run "${e.name}"`,buildParams:e=>({id:e.id,mode:"force"}),description:"Run a cron job immediately"},{id:"delete",type:"rpc",rpc:"cron.remove",label:e=>`Delete "${e.name}"`,buildParams:e=>({id:e.id}),description:"Delete a cron job"}],globalActions:[{id:"refresh",type:"refresh",label:"Refresh"}],slashCommand:"cron",emptyTitle:"No scheduled tasks",emptyContent:"Use the agent to create cron jobs, or add them via the form below."};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const SHARED_FIELDS=[{name:"name",label:"Task Name",inputType:"text",required:!0,placeholder:"e.g. daily-report",apiPath:"name"},{name:"description",label:"Description",inputType:"textarea",placeholder:"What does this task do?",apiPath:"description"},{name:"enabled",label:"Enabled",inputType:"toggle",defaultValue:!0,apiPath:"enabled"},{name:"scheduleKind",label:"Schedule Type",inputType:"select",required:!0,options:[{label:"Interval (every N ms)",value:"every"},{label:"Cron Expression",value:"cron"},{label:"Specific Time",value:"at"}],apiPath:"schedule.kind"},{name:"everyMs",label:"Interval (ms)",inputType:"number",placeholder:"3600000 = 1 hour",validation:{min:1e3},apiPath:"schedule.everyMs"},{name:"cronExpr",label:"Cron Expression",inputType:"text",placeholder:"e.g. 0 9 * * *",apiPath:"schedule.expr"},{name:"at",label:"Run At (ISO 8601)",inputType:"text",placeholder:"e.g. 2026-04-01T09:00:00Z",apiPath:"schedule.at"},{name:"tz",label:"Timezone",inputType:"text",placeholder:"e.g. Asia/Shanghai (optional)",apiPath:"schedule.tz"},{name:"message",label:"Agent Message",inputType:"textarea",required:!0,placeholder:"What should the agent do when this task fires?",apiPath:"payload.message"}];function cronTransformPayload(e){const a={};void 0!==e.name&&(a.name=e.name),void 0!==e.description&&(a.description=e.description),void 0!==e.enabled&&(a.enabled=e.enabled);const n=e.scheduleKind;if(n){const t={kind:n};"every"===n&&e.everyMs&&(t.everyMs=Number(e.everyMs)),"cron"===n&&e.cronExpr&&(t.expr=e.cronExpr),"at"===n&&e.at&&(t.at=e.at),e.tz&&(t.tz=e.tz),a.schedule=t}const t=e.message;return void 0!==t&&(a.payload={kind:"agentTurn",message:String(t)}),a}function cronCreateTransform(e){const a=cronTransformPayload(e);return a.name||(a.name="Untitled"),void 0===a.enabled&&(a.enabled=!0),a.schedule||(a.schedule={kind:"every",everyMs:36e5}),a.sessionTarget="main",a.wakeMode="now",a.payload||(a.payload={kind:"agentTurn",message:""}),a}export const CRON_CREATE_SCHEMA={domain:"cron",operation:"create",title:"Create Scheduled Task",description:"Add a new recurring or one-time agent task.",fields:SHARED_FIELDS,submitLabel:"Create",targetRpc:"cron.add",transformPayload:cronCreateTransform};export const CRON_UPDATE_SCHEMA={domain:"cron",operation:"update",title:"Edit Scheduled Task",description:"Modify an existing scheduled task.",fields:SHARED_FIELDS,submitLabel:"Update",targetRpc:"cron.update",transformPayload:cronTransformPayload};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class FormSchemaRegistry{schemas=new Map;static key(e,s){return`${e}:${s}`}register(e){this.schemas.set(FormSchemaRegistry.key(e.domain,e.operation),e)}get(e,s){return this.schemas.get(FormSchemaRegistry.key(e,s))}list(e){const s=[...this.schemas.values()];return e?s.filter(s=>s.domain===e):s}describeAvailable(){const e=[...this.schemas.values()];return 0===e.length?"No forms registered.":e.map(e=>`- ${e.domain}:${e.operation} — ${e.title}`).join("\n")}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const A2UI_VERSION="0.8",BEEOS_EXTENDED_CATALOG="https://beeos.ai/a2ui/catalog/v1";function getByPath(e,t){if(!e)return;const a=t.split(".");let s=e;for(const e of a){if(null==s||"object"!=typeof s)return;s=s[e]}return s}function setByPath(e,t,a){const s=t.split(".");let i=e;for(let e=0;e<s.length-1;e++){const t=s[e];null!=i[t]&&"object"==typeof i[t]||(i[t]={}),i=i[t]}i[s[s.length-1]]=a}let idCounter=0;function nextId(){return`fsb_${++idCounter}_${Date.now().toString(36)}`}export class FormSurfaceBuilder{deps;constructor(e){this.deps=e}build(e){const{schema:t,data:a,entityId:s,onSuccess:i}=e,n=e.surfaceId??`${t.domain}-${t.operation}-form`,o=e.presentation??"panel",d=`${t.domain}_${t.operation}_submit`,r=t.fields.map(e=>this.schemaFieldToA2UI(e,a)),c=[{id:nextId(),type:"Form",properties:{title:t.title,...t.description?{description:t.description}:{},fields:r,submitLabel:t.submitLabel,submitActionId:d}}],p=this.deps.canvasState.has(n),l=async e=>{if((e.userAction?.name??"")!==d)return;const a=e.userAction?.context??{};await this.handleSubmit(t,a,n,c,s,i)},u=this.buildEnvelope(n,{surfaceUpdate:{surfaceId:n,components:c}});if(p)this.deps.canvasState.update(n,c),this.deps.canvasState.bindActionHandler(n,l),this.deps.sendCanvasUpdate("_active",u);else{const e=this.buildEnvelope(n,{beginRendering:{surfaceId:n,catalogId:BEEOS_EXTENDED_CATALOG}},o);this.deps.canvasState.create(n,o,c,{origin:{kind:"system",handlerId:`${t.domain}-form`},actionHandler:l}),this.deps.sendCanvasUpdate("_active",u),this.deps.sendCanvasUpdate("_active",e)}this.deps.logger.debug?.(`[form-builder] ${p?"Updated":"Created"} form surface=${n} domain=${t.domain} op=${t.operation}`)}schemaFieldToA2UI(e,t){const a={name:e.name,label:e.label,inputType:e.inputType};e.placeholder&&(a.placeholder=e.placeholder),e.required&&(a.required=!0),e.options&&(a.options=e.options),e.validation&&(a.validation=e.validation);let s=e.defaultValue;return void 0===s&&t&&e.apiPath&&(s=getByPath(t,e.apiPath)),void 0!==s&&(a.defaultValue=s),a}async handleSubmit(e,t,a,s,i,n){try{const s=e.transformPayload?e.transformPayload(t):this.autoTransform(e.fields,t),o="update"===e.operation&&i?{id:i,patch:s}:s;await this.deps.gatewayRequest(e.targetRpc,o),this.deps.logger.info?.(`[form-builder] Submit success: ${e.targetRpc} (${e.domain}:${e.operation})`);const d={id:nextId(),type:"Alert",properties:{variant:"success",title:"Done",message:`${e.title} saved successfully.`,dismissible:!0}};this.deps.sendCanvasUpdate("_active",this.buildEnvelope(a,{surfaceUpdate:{surfaceId:a,components:[d]}})),this.deps.canvasState.update(a,[d]),setTimeout(()=>{this.deps.sendCanvasUpdate("_active",this.buildEnvelope(a,{deleteSurface:{surfaceId:a}})),this.deps.canvasState.delete(a)},3e3),n&&await n()}catch(t){const i=t instanceof Error?t.message:String(t);this.deps.logger.warn?.(`[form-builder] Submit failed: ${e.targetRpc} — ${i}`);const n=[...s,{id:nextId(),type:"Alert",properties:{variant:"error",title:"Failed",message:i,dismissible:!0}}];this.deps.sendCanvasUpdate("_active",this.buildEnvelope(a,{surfaceUpdate:{surfaceId:a,components:n}})),this.deps.canvasState.update(a,n)}}autoTransform(e,t){const a={};for(const s of e){const e=t[s.name];if(void 0===e||""===e)continue;setByPath(a,s.apiPath??s.name,e)}return a}buildEnvelope(e,t,a){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,...a?{presentation:a}:{},message:t}}}
|
package/dist/gateway-client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{WebSocket}from"ws";import{EventEmitter}from"node:events";import{randomUUID,generateKeyPairSync,createHash,sign as cryptoSign,createPrivateKey}from"node:crypto";import{readFileSync,writeFileSync,mkdirSync,existsSync}from"node:fs";import{join,dirname}from"node:path";import{homedir}from"node:os";const PROTOCOL_VERSION=3,CONNECT_TIMEOUT_MS=3e4,HEARTBEAT_INTERVAL_MS=15e3,LIVENESS_TIMEOUT_MS=6e4,AUTH_FAILURE_CLOSE_CODE=4001,CONNECT_DELAY_MS=750,RECONNECT_NOTIFY_MIN_MS=6e4,RECONNECT_NOTIFY_MAX_MS=18e4,RECONNECT_NOTIFY_METHOD="server/reconnect";function base64urlEncode(e){return e.toString("base64url")}function verifyDeviceFingerprint(e){try{const t=Buffer.from(e.publicKey,"base64url");return createHash("sha256").update(t).digest("hex")===e.deviceId}catch{return!1}}function loadOrCreateDeviceIdentity(e){const t=e||join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"beeos-claw");mkdirSync(t,{recursive:!0});const n=join(t,"device-key.json");if(existsSync(n))try{const e=JSON.parse(readFileSync(n,"utf8"));if(verifyDeviceFingerprint(e))return{deviceId:e.deviceId,publicKeyB64url:e.publicKey,privateKeyDer:Buffer.from(e.privateKey,"base64")}}catch{}const{publicKey:i,privateKey:r}=generateKeyPairSync("ed25519"),o=i.export({type:"spki",format:"der"}).subarray(-32),s=r.export({type:"pkcs8",format:"der"}),c=createHash("sha256").update(o).digest("hex"),a=base64urlEncode(o),h={deviceId:c,publicKeyB64url:a,privateKeyDer:s};return writeFileSync(n,JSON.stringify({deviceId:c,publicKey:a,privateKey:s.toString("base64")},null,2)),h}function buildDeviceAuthPayload(e){return["v2",e.deviceId,e.clientId,e.clientMode,e.role,e.scopes.join(","),String(e.signedAtMs),e.token,e.nonce].join("|")}function signDeviceAuth(e,t){const n=createPrivateKey({key:e.privateKeyDer,format:"der",type:"pkcs8"});return base64urlEncode(cryptoSign(null,Buffer.from(t),n))}function ensureDevicePaired(e){const t=join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"devices","paired.json");let n={};try{n=JSON.parse(readFileSync(t,"utf8"))}catch{}if(n[e.deviceId])return;const i=Date.now(),r=base64urlEncode(Buffer.from(randomUUID().replace(/-/g,""),"hex"));n[e.deviceId]={deviceId:e.deviceId,publicKey:e.publicKeyB64url,clientId:"cli",clientMode:"cli",role:"operator",roles:["operator"],scopes:["operator.admin","operator.approvals","operator.pairing"],tokens:{operator:{token:r,role:"operator",scopes:["operator.admin","operator.approvals","operator.pairing"],createdAtMs:i}},createdAtMs:i,approvedAtMs:i},mkdirSync(dirname(t),{recursive:!0}),writeFileSync(t,JSON.stringify(n,null,2))}const MANUAL_RECONNECT_SIGNAL="SIGUSR1";function registerReconnectSignal(e,t,n){detachReconnectSignal(e),e.handler=t;try{process.on("SIGUSR1",t),n?.info?.("[gateway] manual reconnect enabled signal=SIGUSR1")}catch{n?.warn?.("[gateway] manual reconnect signal not supported")}}function detachReconnectSignal(e){if(e.handler){try{process.off("SIGUSR1",e.handler)}catch{}e.handler=null}}export class GatewayClient extends EventEmitter{ws=null;config;pending=new Map;connected=!1;stopped=!1;authFailed=!1;connectPromise=null;reconnectTimer=null;reconnectAttempt=0;signalState={handler:null};deviceIdentity;heartbeatTimer=null;lastPongTs=0;_connectionSeq=0;_currentConnectionId=null;_lastReadyAt=0;reconnectNotifyTimer=null;constructor(e){super(),this.config=e,this.deviceIdentity=loadOrCreateDeviceIdentity();try{ensureDevicePaired(this.deviceIdentity)}catch{}}async start(){this.stopped=!1,this.reconnectAttempt=0,registerReconnectSignal(this.signalState,()=>{this.stopped||(this.emit("manual-reconnect"),this.reconnectNow())}),await this.connect()}stop(){this.stopped=!0,this.stopHeartbeat(),this.clearReconnectNotify(),detachReconnectSignal(this.signalState),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.close()}async connect(){if(this.connectPromise)return this.connectPromise;this.connectPromise=this._connect();try{await this.connectPromise}finally{this.connectPromise=null}}_connect(){return new Promise((e,t)=>{if(this.authFailed)return void t(new Error("Authentication failed — not retrying"));const n=Date.now();this.emit("transport",{event:"connect_start",url:this.config.url,attempt:this.reconnectAttempt});const i=new WebSocket(this.config.url);this.ws=i;let r=null;const o=setTimeout(()=>{this.emit("transport",{event:"connect_fail",reason:"timeout",durationMs:Date.now()-n}),i.close(),t(new Error("Gateway connection timeout"))},3e4),s=t=>{clearTimeout(o),this.connected=!0,this.reconnectAttempt=0,this.authFailed=!1,this._connectionSeq++,this._currentConnectionId=randomUUID(),this._lastReadyAt=Date.now(),this.startHeartbeat(),this.emit("transport",{event:"connect_ok",connectionSeq:this._connectionSeq,connectionId:this._currentConnectionId,durationMs:Date.now()-n}),this.emit("connected",{...t,connectionSeq:this._connectionSeq,connectionId:this._currentConnectionId}),e()},c=e=>{clearTimeout(o),this.authFailed=!0,this.emit("transport",{event:"connect_fail",reason:e,durationMs:Date.now()-n}),this.emit("auth-failed",{reason:e}),i.close(),t(new Error(e))};i.on("open",()=>{this.emit("transport",{event:"ws_open",durationMs:Date.now()-n})}),i.on("message",e=>{const t="string"==typeof e?e:e.toString("utf-8");if("pong"===t)return void(this.lastPongTs=Date.now());let i;this.emitMessageHash(t);try{i=JSON.parse(t)}catch{return}const o=i.method;o&&!i.id&&(this.isReconnectNotification(o)?this.scheduleReconnectNotify():this.reconnectNotifyTimer&&this.clearReconnectNotify(),this.emit("notification",o,i.params));const a=i.type;if("event"===a&&this.reconnectNotifyTimer&&this.clearReconnectNotify(),"event"===a){const e=i.event,t=i.payload;if("connect.challenge"===e){this.emit("transport",{event:"handshake.challenge_recv",durationMs:Date.now()-n});const e=t?.nonce;setTimeout(()=>{r=this._sendConnect(e),this.emit("transport",{event:"handshake.connect_sent",durationMs:Date.now()-n})},750)}else"chat"===e?this.emit("chat",t):"agent"===e?this.emit("agent",t):"cron"===e?this.emit("cron",t):this.emit("gateway-event",e,t)}else if("hello-ok"===a)s(i);else if("hello-error"===a||"connect-error"===a)c("Authentication rejected by Gateway");else if("res"===a){const e=i.id;if(e&&e===r){if(i.ok){const e=i.payload;s(e??i)}else{const e=i.error;c(e?.message||"Gateway connect rejected")}return}const t=this.pending.get(e);if(t)if(this.pending.delete(e),clearTimeout(t.timer),i.ok)t.resolve(i.payload);else{const e=i.error;t.reject(new Error(e?.message||"Gateway request failed"))}}}),i.on("pong",()=>{this.lastPongTs=Date.now()}),i.on("close",e=>{this.stopHeartbeat(),this.clearReconnectNotify(),this.emit("transport",{event:"close",code:e,durationMs:Date.now()-n}),this.connected=!1,this.connectPromise=null;for(const[,e]of this.pending)clearTimeout(e.timer),e.reject(new Error("Connection closed"));this.pending.clear(),4001===e&&(this.authFailed=!0,this.emit("auth-failed",{code:e})),this.emit("disconnected"),this.stopped||this.authFailed||this.scheduleReconnect()}),i.on("error",e=>{this.emit("transport",{event:"error",message:e.message,durationMs:Date.now()-n}),this.connected||(clearTimeout(o),t(e)),this.emit("error",e)})})}emitMessageHash(e){if(e.length<32)return;const t=createHash("sha256").update(e).digest("hex").slice(0,16);this.emit("transport",{event:"message_recv",hash:t,size:e.length})}startHeartbeat(){this.stopHeartbeat(),this.lastPongTs=Date.now(),this.heartbeatTimer=setInterval(()=>{if(!this.ws||!this.connected)return;try{this.ws.ping()}catch{}const e=Date.now()-this.lastPongTs;e>6e4&&(this.emit("liveness-timeout",{elapsed:e}),this.ws.close())},15e3),"object"==typeof this.heartbeatTimer&&"unref"in this.heartbeatTimer&&this.heartbeatTimer.unref()}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}isReconnectNotification(e){return"server/reconnect"===e||e.endsWith("/reconnect")}scheduleReconnectNotify(){this.clearReconnectNotify();const e=6e4+12e4*Math.random();this.emit("reconnect-notify-scheduled",{delayMs:Math.round(e)}),this.reconnectNotifyTimer=setTimeout(()=>{this.reconnectNotifyTimer=null,!this.stopped&&this.connected&&(this.emit("reconnect-notify-fired"),this.reconnectNow())},e),"object"==typeof this.reconnectNotifyTimer&&"unref"in this.reconnectNotifyTimer&&this.reconnectNotifyTimer.unref()}clearReconnectNotify(){this.reconnectNotifyTimer&&(clearTimeout(this.reconnectNotifyTimer),this.reconnectNotifyTimer=null)}scheduleReconnect(){if(this.stopped)return;if(this.reconnectTimer)return;const e=this.config.retry??{baseMs:1e3,maxMs:6e5,maxAttempts:0};if(e.maxAttempts>0&&this.reconnectAttempt>=e.maxAttempts)return void this.emit("reconnect-exhausted",this.reconnectAttempt);const t=Math.min(e.baseMs*Math.pow(2,this.reconnectAttempt)+500*Math.random(),e.maxMs);this.reconnectAttempt++,this.emit("reconnecting",{attempt:this.reconnectAttempt,delayMs:t}),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.stopped||this.connect().catch(e=>{this.emit("error",e instanceof Error?e:new Error(String(e)))})},t),"object"==typeof this.reconnectTimer&&"unref"in this.reconnectTimer&&this.reconnectTimer.unref()}reconnectNow(){this.stopped||(this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.close(),this.reconnectAttempt=0,this.connect().catch(e=>{this.emit("error",e instanceof Error?e:new Error(String(e)))}))}_sendConnect(e){const t=Date.now(),n="operator",i=["operator.admin","operator.approvals","operator.pairing"],r={id:this.deviceIdentity.deviceId,publicKey:this.deviceIdentity.publicKeyB64url,signedAt:t};if(e){const o=buildDeviceAuthPayload({deviceId:this.deviceIdentity.deviceId,clientId:"cli",clientMode:"cli",role:n,scopes:i,signedAtMs:t,token:this.config.token,nonce:e});r.nonce=e,r.signature=signDeviceAuth(this.deviceIdentity,o)}const o=randomUUID(),s={type:"req",id:o,method:"connect",params:{minProtocol:3,maxProtocol:3,client:{id:"cli",displayName:"BeeOS Platform Bridge",version:"0.1.0",platform:"beeos",mode:"cli"},caps:["tool-events"],role:n,scopes:i,auth:{token:this.config.token},device:r}};return this.ws?.send(JSON.stringify(s)),o}async request(e,t,n=6e4){if(!this.ws||!this.connected)throw new Error("Not connected to Gateway");const i=randomUUID(),r={type:"req",id:i,method:e,params:t};return new Promise((t,o)=>{const s=setTimeout(()=>{this.pending.delete(i),o(new Error(`Gateway request timeout: ${e}`))},n);this.pending.set(i,{resolve:t,reject:o,timer:s}),this.ws.send(JSON.stringify(r))})}async agentSend(e,t,n,i){const r={agentId:n,sessionKey:e,message:t,deliver:!1,idempotencyKey:randomUUID()};i?.length&&(r.attachments=i);return await this.request("agent",r,12e4)}async chatSend(e,t,n){const i={sessionKey:e,message:t,deliver:!1,idempotencyKey:randomUUID()};n?.length&&(i.attachments=n);return await this.request("chat.send",i,12e4)}async chatAbort(e,t){await this.request("chat.abort",{sessionKey:e,runId:t})}async chatHistory(e,t){return this.request("chat.history",{sessionKey:e,limit:t})}async skillsStatus(){return this.request("skills.status",{})}async sessionsList(){return this.request("sessions.list",{})}async sessionsResolve(e){return this.request("sessions.resolve",{sessionKey:e})}async sessionsPatch(e,t){return this.request("sessions.patch",{sessionKey:e,...t})}close(){if(this.ws)try{this.ws.close()}catch{}this.ws=null,this.connected=!1,this.connectPromise=null,this._currentConnectionId=null}get isConnected(){return this.connected}get connectionSeq(){return this._connectionSeq}get currentConnectionId(){return this._currentConnectionId}get lastReadyAt(){return this._lastReadyAt}}
|
|
1
|
+
import{WebSocket}from"ws";import{EventEmitter}from"node:events";import{randomUUID,generateKeyPairSync,createHash,sign as cryptoSign,createPrivateKey}from"node:crypto";import{readFileSync,writeFileSync,mkdirSync,existsSync}from"node:fs";import{join,dirname}from"node:path";import{homedir}from"node:os";const PROTOCOL_VERSION=3,CONNECT_TIMEOUT_MS=3e4,HEARTBEAT_INTERVAL_MS=15e3,LIVENESS_TIMEOUT_MS=6e4,AUTH_FAILURE_CLOSE_CODE=4001,CONNECT_DELAY_MS=750,RECONNECT_NOTIFY_MIN_MS=6e4,RECONNECT_NOTIFY_MAX_MS=18e4,RECONNECT_NOTIFY_METHOD="server/reconnect";function base64urlEncode(e){return e.toString("base64url")}function verifyDeviceFingerprint(e){try{const t=Buffer.from(e.publicKey,"base64url");return createHash("sha256").update(t).digest("hex")===e.deviceId}catch{return!1}}function loadOrCreateDeviceIdentity(e){const t=e||join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"beeos-claw");mkdirSync(t,{recursive:!0});const n=join(t,"device-key.json");if(existsSync(n))try{const e=JSON.parse(readFileSync(n,"utf8"));if(verifyDeviceFingerprint(e))return{deviceId:e.deviceId,publicKeyB64url:e.publicKey,privateKeyDer:Buffer.from(e.privateKey,"base64")}}catch{}const{publicKey:i,privateKey:o}=generateKeyPairSync("ed25519"),r=i.export({type:"spki",format:"der"}).subarray(-32),s=o.export({type:"pkcs8",format:"der"}),c=createHash("sha256").update(r).digest("hex"),a=base64urlEncode(r),h={deviceId:c,publicKeyB64url:a,privateKeyDer:s};return writeFileSync(n,JSON.stringify({deviceId:c,publicKey:a,privateKey:s.toString("base64")},null,2)),h}function buildDeviceAuthPayload(e){return["v2",e.deviceId,e.clientId,e.clientMode,e.role,e.scopes.join(","),String(e.signedAtMs),e.token,e.nonce].join("|")}function signDeviceAuth(e,t){const n=createPrivateKey({key:e.privateKeyDer,format:"der",type:"pkcs8"});return base64urlEncode(cryptoSign(null,Buffer.from(t),n))}function ensureDevicePaired(e){const t=join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"devices","paired.json");let n={};try{n=JSON.parse(readFileSync(t,"utf8"))}catch{}if(n[e.deviceId])return;const i=Date.now(),o=base64urlEncode(Buffer.from(randomUUID().replace(/-/g,""),"hex"));n[e.deviceId]={deviceId:e.deviceId,publicKey:e.publicKeyB64url,clientId:"cli",clientMode:"cli",role:"operator",roles:["operator"],scopes:["operator.admin","operator.approvals","operator.pairing"],tokens:{operator:{token:o,role:"operator",scopes:["operator.admin","operator.approvals","operator.pairing"],createdAtMs:i}},createdAtMs:i,approvedAtMs:i},mkdirSync(dirname(t),{recursive:!0}),writeFileSync(t,JSON.stringify(n,null,2))}const MANUAL_RECONNECT_SIGNAL="SIGUSR1";function registerReconnectSignal(e,t,n){detachReconnectSignal(e),e.handler=t;try{process.on("SIGUSR1",t),n?.info?.("[gateway] manual reconnect enabled signal=SIGUSR1")}catch{n?.warn?.("[gateway] manual reconnect signal not supported")}}function detachReconnectSignal(e){if(e.handler){try{process.off("SIGUSR1",e.handler)}catch{}e.handler=null}}export class GatewayClient extends EventEmitter{ws=null;config;pending=new Map;connected=!1;stopped=!1;authFailed=!1;connectPromise=null;reconnectTimer=null;reconnectAttempt=0;signalState={handler:null};deviceIdentity;heartbeatTimer=null;lastPongTs=0;_connectionSeq=0;_currentConnectionId=null;_lastReadyAt=0;reconnectNotifyTimer=null;constructor(e){super(),this.config=e,this.deviceIdentity=loadOrCreateDeviceIdentity();try{ensureDevicePaired(this.deviceIdentity)}catch{}}async start(){this.stopped=!1,this.reconnectAttempt=0,registerReconnectSignal(this.signalState,()=>{this.stopped||(this.emit("manual-reconnect"),this.reconnectNow())}),await this.connect()}stop(){this.stopped=!0,this.stopHeartbeat(),this.clearReconnectNotify(),detachReconnectSignal(this.signalState),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.close()}async connect(){if(this.connectPromise)return this.connectPromise;this.connectPromise=this._connect();try{await this.connectPromise}finally{this.connectPromise=null}}_connect(){return new Promise((e,t)=>{if(this.authFailed)return void t(new Error("Authentication failed — not retrying"));const n=Date.now();this.emit("transport",{event:"connect_start",url:this.config.url,attempt:this.reconnectAttempt});const i=new WebSocket(this.config.url);this.ws=i;let o=null;const r=setTimeout(()=>{this.emit("transport",{event:"connect_fail",reason:"timeout",durationMs:Date.now()-n}),i.close(),t(new Error("Gateway connection timeout"))},3e4),s=t=>{clearTimeout(r),this.connected=!0,this.reconnectAttempt=0,this.authFailed=!1,this._connectionSeq++,this._currentConnectionId=randomUUID(),this._lastReadyAt=Date.now(),this.startHeartbeat(),this.emit("transport",{event:"connect_ok",connectionSeq:this._connectionSeq,connectionId:this._currentConnectionId,durationMs:Date.now()-n}),this.emit("connected",{...t,connectionSeq:this._connectionSeq,connectionId:this._currentConnectionId}),e()},c=e=>{clearTimeout(r),this.authFailed=!0,this.emit("transport",{event:"connect_fail",reason:e,durationMs:Date.now()-n}),this.emit("auth-failed",{reason:e}),i.close(),t(new Error(e))};i.on("open",()=>{this.emit("transport",{event:"ws_open",durationMs:Date.now()-n})}),i.on("message",e=>{const t="string"==typeof e?e:e.toString("utf-8");if("pong"===t)return void(this.lastPongTs=Date.now());let i;this.emitMessageHash(t);try{i=JSON.parse(t)}catch{return}const r=i.method;r&&!i.id&&(this.isReconnectNotification(r)?this.scheduleReconnectNotify():this.reconnectNotifyTimer&&this.clearReconnectNotify(),this.emit("notification",r,i.params));const a=i.type;if("event"===a&&this.reconnectNotifyTimer&&this.clearReconnectNotify(),"event"===a){const e=i.event,t=i.payload;if("connect.challenge"===e){this.emit("transport",{event:"handshake.challenge_recv",durationMs:Date.now()-n});const e=t?.nonce;setTimeout(()=>{o=this._sendConnect(e),this.emit("transport",{event:"handshake.connect_sent",durationMs:Date.now()-n})},750)}else"chat"===e?this.emit("chat",t):"agent"===e?this.emit("agent",t):"cron"===e?this.emit("cron",t):"canvas"===e?this.emit("canvas",t):this.emit("gateway-event",e,t)}else if("hello-ok"===a)s(i);else if("hello-error"===a||"connect-error"===a)c("Authentication rejected by Gateway");else if("res"===a){const e=i.id;if(e&&e===o){if(i.ok){const e=i.payload;s(e??i)}else{const e=i.error;c(e?.message||"Gateway connect rejected")}return}const t=this.pending.get(e);if(t)if(this.pending.delete(e),clearTimeout(t.timer),i.ok)t.resolve(i.payload);else{const e=i.error;t.reject(new Error(e?.message||"Gateway request failed"))}}}),i.on("pong",()=>{this.lastPongTs=Date.now()}),i.on("close",e=>{this.stopHeartbeat(),this.clearReconnectNotify(),this.emit("transport",{event:"close",code:e,durationMs:Date.now()-n}),this.connected=!1,this.connectPromise=null;for(const[,e]of this.pending)clearTimeout(e.timer),e.reject(new Error("Connection closed"));this.pending.clear(),4001===e&&(this.authFailed=!0,this.emit("auth-failed",{code:e})),this.emit("disconnected"),this.stopped||this.authFailed||this.scheduleReconnect()}),i.on("error",e=>{this.emit("transport",{event:"error",message:e.message,durationMs:Date.now()-n}),this.connected||(clearTimeout(r),t(e)),this.emit("error",e)})})}emitMessageHash(e){if(e.length<32)return;const t=createHash("sha256").update(e).digest("hex").slice(0,16);this.emit("transport",{event:"message_recv",hash:t,size:e.length})}startHeartbeat(){this.stopHeartbeat(),this.lastPongTs=Date.now(),this.heartbeatTimer=setInterval(()=>{if(!this.ws||!this.connected)return;try{this.ws.ping()}catch{}const e=Date.now()-this.lastPongTs;e>6e4&&(this.emit("liveness-timeout",{elapsed:e}),this.ws.close())},15e3),"object"==typeof this.heartbeatTimer&&"unref"in this.heartbeatTimer&&this.heartbeatTimer.unref()}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}isReconnectNotification(e){return"server/reconnect"===e||e.endsWith("/reconnect")}scheduleReconnectNotify(){this.clearReconnectNotify();const e=6e4+12e4*Math.random();this.emit("reconnect-notify-scheduled",{delayMs:Math.round(e)}),this.reconnectNotifyTimer=setTimeout(()=>{this.reconnectNotifyTimer=null,!this.stopped&&this.connected&&(this.emit("reconnect-notify-fired"),this.reconnectNow())},e),"object"==typeof this.reconnectNotifyTimer&&"unref"in this.reconnectNotifyTimer&&this.reconnectNotifyTimer.unref()}clearReconnectNotify(){this.reconnectNotifyTimer&&(clearTimeout(this.reconnectNotifyTimer),this.reconnectNotifyTimer=null)}scheduleReconnect(){if(this.stopped)return;if(this.reconnectTimer)return;const e=this.config.retry??{baseMs:1e3,maxMs:6e5,maxAttempts:0};if(e.maxAttempts>0&&this.reconnectAttempt>=e.maxAttempts)return void this.emit("reconnect-exhausted",this.reconnectAttempt);const t=Math.min(e.baseMs*Math.pow(2,this.reconnectAttempt)+500*Math.random(),e.maxMs);this.reconnectAttempt++,this.emit("reconnecting",{attempt:this.reconnectAttempt,delayMs:t}),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.stopped||this.connect().catch(e=>{this.emit("error",e instanceof Error?e:new Error(String(e)))})},t),"object"==typeof this.reconnectTimer&&"unref"in this.reconnectTimer&&this.reconnectTimer.unref()}reconnectNow(){this.stopped||(this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.close(),this.reconnectAttempt=0,this.connect().catch(e=>{this.emit("error",e instanceof Error?e:new Error(String(e)))}))}_sendConnect(e){const t=Date.now(),n="operator",i=["operator.admin","operator.approvals","operator.pairing"],o={id:this.deviceIdentity.deviceId,publicKey:this.deviceIdentity.publicKeyB64url,signedAt:t};if(e){const r=buildDeviceAuthPayload({deviceId:this.deviceIdentity.deviceId,clientId:"cli",clientMode:"cli",role:n,scopes:i,signedAtMs:t,token:this.config.token,nonce:e});o.nonce=e,o.signature=signDeviceAuth(this.deviceIdentity,r)}const r=randomUUID(),s={type:"req",id:r,method:"connect",params:{minProtocol:3,maxProtocol:3,client:{id:"cli",displayName:"BeeOS Platform Bridge",version:"0.1.0",platform:"beeos",mode:"cli"},caps:this.config.openclawCanvasCompat?["tool-events","canvas"]:["tool-events"],role:n,scopes:i,auth:{token:this.config.token},device:o}};return this.ws?.send(JSON.stringify(s)),r}async request(e,t,n=6e4){if(!this.ws||!this.connected)throw new Error("Not connected to Gateway");const i=randomUUID(),o={type:"req",id:i,method:e,params:t};return new Promise((t,r)=>{const s=setTimeout(()=>{this.pending.delete(i),r(new Error(`Gateway request timeout: ${e}`))},n);this.pending.set(i,{resolve:t,reject:r,timer:s}),this.ws.send(JSON.stringify(o))})}async agentSend(e,t,n,i){const o={agentId:n,sessionKey:e,message:t,deliver:!1,idempotencyKey:randomUUID()};i?.length&&(o.attachments=i);return await this.request("agent",o,12e4)}async chatSend(e,t,n){const i={sessionKey:e,message:t,deliver:!1,idempotencyKey:randomUUID()};n?.length&&(i.attachments=n);return await this.request("chat.send",i,12e4)}async chatAbort(e,t){await this.request("chat.abort",{sessionKey:e,runId:t})}async chatHistory(e,t){return this.request("chat.history",{sessionKey:e,limit:t})}async skillsStatus(){return this.request("skills.status",{})}async sessionsList(){return this.request("sessions.list",{})}async sessionsResolve(e){return this.request("sessions.resolve",{sessionKey:e})}async sessionsPatch(e,t){return this.request("sessions.patch",{sessionKey:e,...t})}close(){if(this.ws)try{this.ws.close()}catch{}this.ws=null,this.connected=!1,this.connectPromise=null,this._currentConnectionId=null}get isConnected(){return this.connected}get connectionSeq(){return this._connectionSeq}get currentConnectionId(){return this._currentConnectionId}get lastReadyAt(){return this._lastReadyAt}}
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{resolveConfigWithMeta,mirrorOpenClawConfig}from"./config.js";import{BEEOS_UPLOAD_TOOL_NAME,BEEOS_UPLOAD_TOOL_SCHEMA,BEEOS_UPLOAD_TOOL_DESCRIPTION,executeUpload}from"./upload-tool.js";import{writeUploadToolResult,cleanupUploadToolResultCache}from"./upload-result-cache.js";import{handleFileStage}from"./file-stage.js";import{ACPServer}from"./acp-server.js";import{createObsEventWriter}from"./observability.js";import{resolvePluginVersion}from"./plugin-version.js";import{createTerminalWebSocketService}from"./terminal-websocket.js";import{TerminalManager}from"./terminal-session-manager.js";import{loadAgentKeyPair,agentFetch}from"./agent-auth.js";const PLUGIN_VERSION=resolvePluginVersion();let pluginConfig=null,acpServer=null,agentKeys=null;const plugin={id:"beeos-claw",name:"beeos-claw",description:"BeeOS platform bridge plugin for OpenClaw. Provides file upload/download, ACP bridge, and observability.",version:PLUGIN_VERSION,register(e){let o
|
|
1
|
+
import{resolveConfigWithMeta,mirrorOpenClawConfig}from"./config.js";import{BEEOS_UPLOAD_TOOL_NAME,BEEOS_UPLOAD_TOOL_SCHEMA,BEEOS_UPLOAD_TOOL_DESCRIPTION,executeUpload}from"./upload-tool.js";import{writeUploadToolResult,cleanupUploadToolResultCache}from"./upload-result-cache.js";import{handleFileStage}from"./file-stage.js";import{ACPServer}from"./acp-server.js";import{createObsEventWriter}from"./observability.js";import{resolvePluginVersion}from"./plugin-version.js";import{createTerminalWebSocketService}from"./terminal-websocket.js";import{TerminalManager}from"./terminal-session-manager.js";import{loadAgentKeyPair,agentFetch}from"./agent-auth.js";import{CanvasStateManager}from"./canvas-state.js";import{CanvasApiClient}from"./canvas-api-client.js";import{RENDER_UI_TOOL_NAME,RENDER_UI_TOOL_DESCRIPTION,RENDER_UI_TOOL_SCHEMA,CLOSE_UI_TOOL_NAME,CLOSE_UI_TOOL_DESCRIPTION,CLOSE_UI_TOOL_SCHEMA,createRenderUiHandler,createCloseUiHandler,CANVAS_SYSTEM_PROMPT}from"./canvas-tools.js";import{FormSchemaRegistry}from"./form-schema-registry.js";import{CRON_CREATE_SCHEMA,CRON_UPDATE_SCHEMA}from"./cron-schemas.js";import{RENDER_FORM_TOOL_NAME,RENDER_FORM_TOOL_SCHEMA,buildRenderFormDescription,createRenderFormHandler}from"./render-form-tool.js";import{ResourceDescriptorRegistry}from"./resource-descriptor-registry.js";import{CRON_RESOURCE}from"./cron-resource.js";import{generateActionTools,RENDER_RESOURCE_TOOL_NAME,RENDER_RESOURCE_TOOL_SCHEMA,buildRenderResourceDescription,createRenderResourceHandler}from"./action-tool-generator.js";const PLUGIN_VERSION=resolvePluginVersion();let pluginConfig=null,acpServer=null,agentKeys=null;const plugin={id:"beeos-claw",name:"beeos-claw",description:"BeeOS platform bridge plugin for OpenClaw. Provides file upload/download, ACP bridge, and observability.",version:PLUGIN_VERSION,register(e){let r,o;try{r=e.runtime?.config?.loadConfig?.()}catch{}try{o=resolveConfigWithMeta({pluginConfig:e.pluginConfig??{},openclawConfig:r})}catch(r){return void e.logger.error?.("[beeos-claw] Failed to resolve config:",r)}const a=o.config;pluginConfig=a;const t=a.log.enabled?createObsEventWriter({dir:a.log.dir,redaction:{gatewayToken:a.gateway.token},logger:e.logger}):()=>{};t({component:"plugin",domain:"lifecycle",name:"lifecycle.startup",severity:"info",payload:{pluginVersion:PLUGIN_VERSION,pid:process.pid,nodeVersion:process.version,cwd:process.cwd()}}),t({component:"plugin",domain:"config",name:"config.resolved",severity:"info",payload:{config:a,sources:o.sources,openclawConfigPath:o.openclawConfigPath}});for(const r of o.validation)t({component:"plugin",domain:"config",name:"config.validation",severity:r.severity,summary:r.message,error:{code:r.code,message:r.message,nextSteps:r.nextSteps}}),"error"===r.severity?e.logger.error?.(`[beeos-claw] ${r.message}`):e.logger.warn?.(`[beeos-claw] ${r.message}`);if(o.validation.some(e=>"error"===e.severity)&&e.logger.error?.("[beeos-claw] Plugin has configuration errors — some features may not work."),a.bridge.keyFile)try{agentKeys=loadAgentKeyPair(a.bridge.keyFile);const r=Buffer.from(agentKeys.publicKey).toString("base64");e.logger.info?.(`[beeos-claw] v${PLUGIN_VERSION} registering (publicKey: ${r})`)}catch(r){e.logger.warn?.(`[beeos-claw] Failed to load agent key pair: ${r}`),e.logger.info?.(`[beeos-claw] v${PLUGIN_VERSION} registering (no key pair — signed requests disabled)`)}else e.logger.info?.(`[beeos-claw] v${PLUGIN_VERSION} registering (no keyFile configured)`);cleanupUploadToolResultCache().catch(r=>{e.logger.warn?.(`[beeos_upload_file] cache cleanup failed on startup: ${String(r)}`)}),e.registerTool({name:BEEOS_UPLOAD_TOOL_NAME,description:BEEOS_UPLOAD_TOOL_DESCRIPTION,parameters:BEEOS_UPLOAD_TOOL_SCHEMA,async execute(r,o){e.logger.debug?.(`[beeos_upload_file] toolCallId=${r} params=${JSON.stringify(o)}`);const n=await executeUpload(o,a,agentKeys);return e.logger.debug?.(`[beeos_upload_file] toolCallId=${r} ok=${n.ok}`),t({component:"upload-tool",domain:"tool",name:n.ok?"upload.success":"upload.failure",severity:n.ok?"info":"warn",payload:{toolCallId:r,ok:n.ok,fileCount:n.files?.length??0,error:n.error}}),n.ok&&n.content&&acpServer&&acpServer.pushToolResultContent(r,BEEOS_UPLOAD_TOOL_NAME,n.content),(async o=>{try{await writeUploadToolResult(r,BEEOS_UPLOAD_TOOL_NAME,o),await cleanupUploadToolResultCache()}catch(o){e.logger.warn?.(`[beeos_upload_file] persist cache failed toolCallId=${r}: ${String(o)}`)}return o})(n)}}),e.registerHttpRoute({path:"/beeos/files/stage",async handler(e,r){await handleFileStage(e,r,a)}});const n=agentKeys?(e,r)=>agentFetch(e,agentKeys,r):void 0;if(acpServer=new ACPServer(a,e.logger,t,n),e.registerService({id:"beeos-acp",async start(){await acpServer.start(),e.logger.info?.("[beeos-claw] ACP Server started")},async stop(){await(acpServer?.stop()),e.logger.info?.("[beeos-claw] ACP Server stopped")}}),a.canvasEnabled){const r=new CanvasStateManager;let o=null;if(a.platformUrl&&agentKeys){o=new CanvasApiClient({platformUrl:a.platformUrl,agentKeys:agentKeys});const t=process.env.CANVAS_RELAY_URL||a.platformUrl;process.env.CANVAS_RELAY_URL||e.logger.warn?.("[beeos-claw] CANVAS_RELAY_URL not set, falling back to platformUrl — Canvas Relay must be reachable at "+t),r.setRelayOpts({canvasRelayUrl:t,agentKeys:agentKeys,logger:e.logger}),e.logger.info?.("[beeos-claw] Canvas API client + Relay opts configured")}else e.logger.warn?.("[beeos-claw] Canvas Relay unavailable: platformUrl or agentKeys missing");const n={stateManager:r,sendCanvasUpdate:(e,r)=>{acpServer.sendCanvasUpdate(e,r)},logger:e.logger,writeObs:e=>t(e),isCanvasActive:()=>acpServer.isCanvasActive(),canvasApiClient:o,getActiveSessionId:e=>acpServer.getSessionIdForToolCall(e)},s=new FormSchemaRegistry;s.register(CRON_CREATE_SCHEMA),s.register(CRON_UPDATE_SCHEMA);const i=new ResourceDescriptorRegistry;i.register(CRON_RESOURCE),e.registerTool({name:RENDER_UI_TOOL_NAME,description:RENDER_UI_TOOL_DESCRIPTION,parameters:RENDER_UI_TOOL_SCHEMA,execute:createRenderUiHandler(n)}),e.registerTool({name:CLOSE_UI_TOOL_NAME,description:CLOSE_UI_TOOL_DESCRIPTION,parameters:CLOSE_UI_TOOL_SCHEMA,execute:createCloseUiHandler(n)}),acpServer.setCanvasState(r,{schemaRegistry:s,descriptorRegistry:i});const l=acpServer.getFormBuilder();l&&e.registerTool({name:RENDER_FORM_TOOL_NAME,description:buildRenderFormDescription(s),parameters:RENDER_FORM_TOOL_SCHEMA,execute:createRenderFormHandler({registry:s,builder:l,logger:e.logger})});const c=acpServer.getResourceRenderer();if(c){generateActionTools(i,e,async(e,r)=>{const o=acpServer?.gateway;if(!o?.isConnected)throw new Error("Gateway not connected");return o.request(e,r)},c,e.logger),e.registerTool({name:RENDER_RESOURCE_TOOL_NAME,description:buildRenderResourceDescription(i),parameters:RENDER_RESOURCE_TOOL_SCHEMA,execute:createRenderResourceHandler(i,c,e.logger)})}if("function"==typeof e.on)try{e.on("before_prompt_build",()=>acpServer.isCanvasActive()?{appendSystemContext:CANVAS_SYSTEM_PROMPT}:{appendSystemContext:"Canvas is disabled by user. Do NOT use render_ui or close_ui. Respond with plain text only."}),e.logger.debug?.("[beeos-claw] Canvas system prompt hook registered: before_prompt_build")}catch{e.logger.debug?.("[beeos-claw] before_prompt_build hook not supported")}e.logger.info?.("[beeos-claw] Canvas tools registered (render_ui, close_ui, render_form, render_resource + auto-action tools)")}else e.logger.info?.("[beeos-claw] Canvas disabled by config (canvasEnabled=false)");const s=new TerminalManager,i=createTerminalWebSocketService({config:{port:a.terminalWsPort,token:agentKeys?Buffer.from(agentKeys.publicKey).toString("base64"):"",allowedOrigins:a.terminalWsAllowedOrigins},terminalMgr:s,logger:e.logger,writeObs:t});if(a.bridge.shell.enabled?e.registerService({id:"beeos-terminal-ws",start(){i.start(),e.logger.info?.(`[beeos-claw] Terminal WS started on port ${a.terminalWsPort}`)},stop(){i.stop(),s.destroy(),e.logger.info?.("[beeos-claw] Terminal WS stopped")}}):e.logger.info?.("[beeos-claw] Terminal/shell disabled by config (bridge.shell.enabled=false)"),r){const o=mirrorOpenClawConfig(r);o.copied?e.logger.info?.(`[beeos-claw] mirrored openclaw config to ${o.destinationPath}`):e.logger.debug?.(`[beeos-claw] openclaw config mirror skipped: ${o.reason}`)}e.logger.info?.("[beeos-claw] Plugin registered successfully")}};export default plugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const RENDER_FORM_TOOL_NAME="render_form";export function buildRenderFormDescription(e){return["Render a schema-driven form for data entry or editing.","The form fields, validation, and submit behaviour are determined by the registered schema — you do NOT need to construct the form manually.","Use this instead of render_ui when a domain schema exists.","","Available forms:",e.describeAvailable()].join("\n")}export const RENDER_FORM_TOOL_SCHEMA={type:"object",required:["domain","operation"],properties:{domain:{type:"string",description:"The domain of the form (e.g. 'cron')"},operation:{type:"string",description:"The operation (e.g. 'create', 'update')"},data:{type:"object",description:"Optional pre-fill data in the API's native nested format. For update operations, pass the existing entity data so the form is pre-filled."},entityId:{type:"string",description:"Entity identifier for update operations (e.g. the cron job ID). Required for updates."}}};export function createRenderFormHandler(e){return async(r,t)=>{const o=t,i=o.domain,n=o.operation;if(!i||!n)return{success:!1,error:"domain and operation are required"};const a=e.registry.get(i,n);if(!a){return{success:!1,error:`No form schema found for "${i}:${n}". Available:\n${e.registry.describeAvailable()}`}}const d=o.data,s=o.entityId;return"update"!==a.operation||s?(e.builder.build({schema:a,data:d,entityId:s}),e.logger.debug?.(`[render_form] Rendered form: ${i}:${n} entityId=${s??"none"}`),{success:!0,domain:i,operation:n,entityId:s,message:`Form "${a.title}" rendered on Canvas.`}):{success:!1,error:"entityId is required for update operations"}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class ResourceDescriptorRegistry{descriptors=new Map;register(s){this.descriptors.set(s.domain,s)}get(s){return this.descriptors.get(s)}list(){return[...this.descriptors.values()]}getBySlashCommand(s){for(const e of this.descriptors.values())if(e.slashCommand===s)return e}describeAvailable(){const s=this.list();return 0===s.length?"No resources registered.":s.map(s=>`- ${s.domain}: ${s.title}`).join("\n")}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function getByPath(t,e){if(!t)return;const o=e.split(".");let n=t;for(const t of o){if(null==n||"object"!=typeof n)return;n=n[t]}return n}export function resolveColumnValue(t,e){const o=getByPath(e,t.key);return t.format?t.format(o,e):String(o??"—")}export function resolveActionLabel(t,e){return"function"==typeof t?t(e):t}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{resolveColumnValue,resolveActionLabel}from"./resource-descriptor.js";const A2UI_VERSION="0.8",BEEOS_EXTENDED_CATALOG="https://beeos.ai/a2ui/catalog/v1";let idCounter=0;function nextId(){return`rsr_${++idCounter}_${Date.now().toString(36)}`}export class ResourceSurfaceRenderer{deps;constructor(e){this.deps=e}async render(e,t="panel"){try{const n=await this.deps.gatewayRequest(e.listRpc,e.listParams??{}),s=e.extractItems(n);this.buildAndSendSurface(e,s,t)}catch(t){this.deps.logger.warn?.(`[resource-renderer] Failed to render ${e.domain}: ${t instanceof Error?t.message:t}`)}}buildAndSendSurface(e,t,n){const s=this.surfaceId(e,n),r=this.buildComponents(e,t),i=this.deps.canvasState.has(s),o=this.envelope(s,{surfaceUpdate:{surfaceId:s,components:r}}),a=t=>this.handleAction(e,t,n);if(i)this.deps.canvasState.update(s,r),this.deps.canvasState.bindActionHandler(s,a),this.deps.sendCanvasUpdate("_active",o);else{const t=this.envelope(s,{beginRendering:{surfaceId:s,catalogId:BEEOS_EXTENDED_CATALOG}},n);this.deps.canvasState.create(s,n,r,{origin:{kind:"system",handlerId:`${e.domain}-resource`},actionHandler:a}),this.deps.sendCanvasUpdate("_active",o),this.deps.sendCanvasUpdate("_active",t)}this.deps.logger.debug?.(`[resource-renderer] ${i?"Updated":"Created"} surface=${s} domain=${e.domain} items=${t.length}`)}buildComponents(e,t){const n=[],s=nextId();if(n.push({id:s,type:"Text",properties:{text:e.title,variant:"heading"}}),0===t.length){const t=nextId();n.push({id:t,type:"Callout",properties:{variant:"note",title:e.emptyTitle??"No items",content:e.emptyContent??""}});const r=nextId();return n.push({id:r,type:"Card",properties:{},children:[s,t]}),n}const r=e.columns.map(e=>({key:e.key,label:e.label})),i=t.map(t=>({cells:e.columns.map(e=>resolveColumnValue(e,t))})),o=nextId();n.push({id:o,type:"Table",properties:{title:"",columns:r,rows:i}});const a=[];for(const s of t)for(const t of e.itemActions){const r=nextId(),i=resolveActionLabel(t.label,s),o=`${e.domain}_${t.id}`;let d;d="rpc"===t.type?t.buildParams(s):{__itemId:e.itemId(s),...t.buildFormData?t.buildFormData(s):{}},n.push({id:r,type:"Button",properties:{label:i,actionId:o,context:d}}),a.push(r)}const d=nextId();n.push({id:d,type:"Grid",properties:{columns:Math.min(e.itemActions.length,4),gap:"sm"},children:a});const c=[];for(const t of e.globalActions){const s=nextId(),r=`${e.domain}_${t.id}`;n.push({id:s,type:"Button",properties:{label:t.label,actionId:r}}),c.push(s)}const p=[s,o,d,...c],l=nextId();return n.push({id:l,type:"Card",properties:{},children:p}),n}async handleAction(e,t,n){const s=t.userAction?.name??"",r=t.userAction?.context??{},i=`${e.domain}_`;if(!s.startsWith(i))return void this.deps.logger.debug?.(`[resource-renderer] Ignoring action ${s} (not in domain ${e.domain})`);const o=s.slice(i.length),a=e.globalActions.find(e=>e.id===o);if(a){if("refresh"===a.type)return void await this.render(e,n);if("form"===a.type)return void this.openForm(a.formDomain,a.formOperation,void 0,void 0,e,n)}const d=e.itemActions.find(e=>e.id===o);if(d){try{if("rpc"===d.type)await this.deps.gatewayRequest(d.rpc,r);else if("form"===d.type){const t=r.__itemId??void 0,s={...r};return delete s.__itemId,void this.openForm(d.formDomain,d.formOperation,Object.keys(s).length>0?s:void 0,t,e,n)}}catch(e){this.deps.logger.warn?.(`[resource-renderer] Action ${s} failed: ${e instanceof Error?e.message:e}`)}await this.render(e,n)}else this.deps.logger.debug?.(`[resource-renderer] Unknown action: ${s}`)}openForm(e,t,n,s,r,i){const o=this.deps.schemaRegistry.get(e,t);o?this.deps.formBuilder.build({schema:o,data:n,entityId:s,onSuccess:()=>this.render(r,i)}):this.deps.logger.warn?.(`[resource-renderer] Form schema not found: ${e}:${t}`)}surfaceId(e,t){return"inline"===t?`${e.domain}-resource-inline`:`${e.domain}-resource`}envelope(e,t,n){return{sessionUpdate:"canvas.update",a2uiVersion:"0.8",surfaceId:e,...n?{presentation:n}:{},message:t}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "beeos-claw",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
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>",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@beeos-ai/bridge-client": "^0.1.0",
|
|
40
|
+
"@beeos-ai/cli": "^0.0.2",
|
|
40
41
|
"ws": "^8.18.0"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|