openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.43 kB
JavaScript
import{f as prepareSystemAgentRunAdmission}from"./admitted-run-context-xwC9VVgE.mjs";import{r as SystemAgentInferenceUnavailableError,t as requireSystemAgentInferenceRoute}from"./inference-guard-u0LFXoxk.mjs";import{i as resolveSystemAgentExpectedAgentHarnessRuntimeArtifact}from"./verified-inference-C2wCwCrh.mjs";import{n as extractAgentRunTerminalError,r as extractAgentRunText}from"./agent-run-result-DtgfdC_4.mjs";import{t as SessionManager}from"./session-manager-BPWwNp25.mjs";import{t as isLocalSystemAgentRoute}from"./route-locality-DYviyRJo.mjs";import{a as SYSTEM_AGENT_ASSISTANT_TIMEOUT_MS,c as buildSystemAgentGreetingUserPrompt,i as SYSTEM_AGENT_ASSISTANT_SYSTEM_PROMPT,n as systemAgentTerminalFailureGuidance,o as SYSTEM_AGENT_GREETING_SYSTEM_PROMPT,r as SYSTEM_AGENT_ASSISTANT_LOCAL_TIMEOUT_MS,s as buildSystemAgentAssistantUserPrompt,t as systemAgentRuntimeFailureGuidance,u as parseSystemAgentAssistantPlanText}from"./inference-run-errors-CJrsW3bY.mjs";import path from"node:path";import{randomUUID}from"node:crypto";import fs from"node:fs/promises";import os from"node:os";
//!src/system-agent/assistant.ts
const SYSTEM_AGENT_PLANNER_RESPONSE_SCHEMA={type:`object`,properties:{reply:{type:`string`},command:{type:`string`}},required:[`reply`],additionalProperties:false};async function planSystemAgentCommand(e){const t=e.input.trim();if(!t)return null;const n=await runConfiguredSystemAgentText({prompt:buildSystemAgentAssistantUserPrompt({input:t,overview:e.overview}),systemPrompt:SYSTEM_AGENT_ASSISTANT_SYSTEM_PROMPT,runIdPrefix:`openclaw-planner`,verifiedInference:e.verifiedInference,responseFormat:SYSTEM_AGENT_PLANNER_RESPONSE_SCHEMA});const r=parseSystemAgentAssistantPlanText(n?.text);return r&&n?{...r,modelLabel:n.modelLabel}:null}async function planSystemAgentGreetingWithConfiguredModel(e){const t=await runConfiguredSystemAgentText({prompt:buildSystemAgentGreetingUserPrompt(e),systemPrompt:SYSTEM_AGENT_GREETING_SYSTEM_PROMPT,runIdPrefix:`openclaw-greeting`,verifiedInference:e.verifiedInference,timeoutMs:e.timeoutMs});return t?{text:t.text,modelRef:t.modelLabel}:null}async function runConfiguredSystemAgentText(e){const t=await requireSystemAgentInferenceRoute(e.verifiedInference,`planner`);let n;try{n=resolveSystemAgentExpectedAgentHarnessRuntimeArtifact(e.verifiedInference)}catch(e){throw new SystemAgentInferenceUnavailableError(`planner`,[e])}const r=n?void 0:e.responseFormat;const i=await fs.mkdtemp(path.join(os.tmpdir(),`openclaw-planner-`));let a;let o;try{const s=`${e.runIdPrefix}-${randomUUID()}`;const c=e.timeoutMs??(isLocalSystemAgentRoute(t)?12e4:3e4);o=prepareSystemAgentRunAdmission(t.runConfig,s,t.agentId,`system-agent.assistant`);const l={sessionId:`${s}-session`,agentId:t.agentId,trigger:`manual`,sessionFile:`in-memory:${s}`,sessionManager:SessionManager.inMemory(i),workspaceDir:i,cwd:i,agentDir:t.agentDir,config:t.runConfig,prompt:e.prompt,provider:t.provider,model:t.model,timeoutMs:c,thinkLevel:`off`,runId:s,extraSystemPrompt:e.systemPrompt,extraSystemPromptStatic:e.systemPrompt,messageChannel:`openclaw`,messageProvider:`openclaw`,disableTools:true,disableTrajectory:true,bootstrapContextMode:`lightweight`,...r?{streamParams:{responseFormat:r}}:{},...t.authProfileId?{authProfileId:t.authProfileId}:{}};const u=t.runner===`cli`?await(await import(`./cli-runner-DsAtE3l8.mjs`)).runCliAgent({...l,preparedRunAdmission:o,executionMode:`side-question`,cleanupCliLiveSessionOnRunEnd:true}):await(await import(`./embedded-agent-B_ZN5S8R.mjs`)).runEmbeddedAgent({...l,lane:`system-agent-inference`,preparedRunAdmission:o,toolsAllow:[],agentHarnessRuntimeOverride:t.agentHarnessRuntimeOverride,...n?{expectedAgentHarnessRuntimeArtifact:n}:{},cleanupBundleMcpOnRunEnd:true,...t.authProfileId?{authProfileIdSource:`user`}:{}});const d=extractAgentRunTerminalError(u);if(d)throw new SystemAgentInferenceUnavailableError(`planner`,[new Error(d)],systemAgentTerminalFailureGuidance(u));a=extractAgentRunText(u)}catch(e){if(e instanceof SystemAgentInferenceUnavailableError)throw e;const t=systemAgentRuntimeFailureGuidance(e);if(t)throw new SystemAgentInferenceUnavailableError(`planner`,[e],t);a=void 0}finally{o?.close();await fs.rm(i,{recursive:true,force:true})}if(!a)return null;await requireSystemAgentInferenceRoute(e.verifiedInference,`planner`);return{text:a,modelLabel:t.modelLabel}}export{planSystemAgentCommand,planSystemAgentGreetingWithConfiguredModel};