UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

180 lines (179 loc) 7.66 kB
import { m as resolveAgentWorkspaceDir } from "./agent-scope-config-DcbEhP0R.js"; import { r as normalizeProviderId } from "./provider-id-DMd-TDFp.js"; import { c as resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot-w-4EjxI4.js"; import "./agent-scope-DbtJyKUL.js"; import { t as SessionManager } from "./session-manager-DO0bDxav.js"; import { o as prepareSystemAgentRunAdmission } from "./admitted-run-context-CHY5SdVF.js"; import { t as SystemAgentInferenceUnavailableError } from "./inference-error-Dyv9IgLa.js"; import { a as resolveSystemAgentVerifiedInferenceRoute, i as resolveSystemAgentExpectedAgentHarnessRuntimeArtifact } from "./verified-inference-D8I0G1w6.js"; import { i as extractAgentRunText } from "./agent-run-result-JlQhspoW.js"; import { c as parseSystemAgentAssistantPlanText, i as SYSTEM_AGENT_GREETING_SYSTEM_PROMPT, n as SYSTEM_AGENT_ASSISTANT_SYSTEM_PROMPT, o as buildSystemAgentAssistantUserPrompt, r as SYSTEM_AGENT_ASSISTANT_TIMEOUT_MS, s as buildSystemAgentGreetingUserPrompt, t as SYSTEM_AGENT_ASSISTANT_LOCAL_TIMEOUT_MS } from "./assistant-prompts-Cs9_Vt57.js"; import path from "node:path"; import fs from "node:fs/promises"; import os from "node:os"; import { randomUUID } from "node:crypto"; //#region src/system-agent/assistant-timeout.ts function resolveSystemAgentAssistantTimeoutFromManifests(params) { const providers = /* @__PURE__ */ new Set([normalizeProviderId(params.route.provider), normalizeProviderId(params.route.modelLabel.split("/", 1)[0] ?? "")]); return params.plugins.some((plugin) => Object.entries(plugin.modelPricing?.providers ?? {}).some(([provider, pricing]) => providers.has(normalizeProviderId(provider)) && pricing.external === false)) ? SYSTEM_AGENT_ASSISTANT_LOCAL_TIMEOUT_MS : SYSTEM_AGENT_ASSISTANT_TIMEOUT_MS; } function resolveSystemAgentAssistantTimeoutMs(route) { try { const workspaceDir = resolveAgentWorkspaceDir(route.runConfig, route.agentId); return resolveSystemAgentAssistantTimeoutFromManifests({ route, plugins: resolvePluginMetadataSnapshot({ config: route.runConfig, workspaceDir, env: process.env, allowWorkspaceScopedCurrent: true }).plugins }); } catch { return SYSTEM_AGENT_ASSISTANT_TIMEOUT_MS; } } if (process.env.VITEST || false) globalThis[Symbol.for("openclaw.systemAgentTimeoutTestApi")] = { resolveSystemAgentAssistantTimeoutFromManifests }; //#endregion //#region 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(params) { return await planSystemAgentCommandWithConfiguredModel(params); } /** Plan only through the configured default agent's verified route. */ async function planSystemAgentCommandWithConfiguredModel(params) { const input = params.input.trim(); if (!input) return null; const result = await runConfiguredSystemAgentText({ prompt: buildSystemAgentAssistantUserPrompt({ input, overview: params.overview, ...params.history ? { history: params.history } : {}, ...params.pendingOperation ? { pendingOperation: params.pendingOperation } : {} }), systemPrompt: SYSTEM_AGENT_ASSISTANT_SYSTEM_PROMPT, runIdPrefix: "openclaw-planner", verifiedInference: params.verifiedInference, deps: params.deps, responseFormat: SYSTEM_AGENT_PLANNER_RESPONSE_SCHEMA }); const parsed = parseSystemAgentAssistantPlanText(result?.text); return parsed && result ? { ...parsed, modelLabel: result.modelLabel } : null; } /** One tool-free, verified inference turn for the cached caretaker greeting. */ async function planSystemAgentGreetingWithConfiguredModel(params) { const result = await runConfiguredSystemAgentText({ prompt: buildSystemAgentGreetingUserPrompt(params), systemPrompt: SYSTEM_AGENT_GREETING_SYSTEM_PROMPT, runIdPrefix: "openclaw-greeting", verifiedInference: params.verifiedInference, deps: params.deps, timeoutMs: params.timeoutMs }); return result ? { text: result.text, modelRef: result.modelLabel } : null; } async function runConfiguredSystemAgentText(params) { const route = await requireVerifiedPlannerRoute(params.verifiedInference, params.deps); let expectedAgentHarnessRuntimeArtifact; try { expectedAgentHarnessRuntimeArtifact = resolveSystemAgentExpectedAgentHarnessRuntimeArtifact(params.verifiedInference); } catch (error) { throw new SystemAgentInferenceUnavailableError("planner", [error]); } const responseFormat = expectedAgentHarnessRuntimeArtifact ? void 0 : params.responseFormat; const tempDir = await (params.deps?.createTempDir ?? createTempPlannerDir)(); let text; let preparedRunAdmission; try { const runId = `${params.runIdPrefix}-${randomUUID()}`; const timeoutMs = params.timeoutMs ?? (params.deps?.resolveAssistantTimeoutMs ?? resolveSystemAgentAssistantTimeoutMs)(route); preparedRunAdmission = prepareSystemAgentRunAdmission(route.runConfig, runId, route.agentId, "system-agent.assistant"); const shared = { sessionId: `${runId}-session`, agentId: route.agentId, trigger: "manual", sessionFile: `in-memory:${runId}`, sessionManager: SessionManager.inMemory(tempDir), workspaceDir: tempDir, cwd: tempDir, agentDir: route.agentDir, config: route.runConfig, prompt: params.prompt, provider: route.provider, model: route.model, timeoutMs, thinkLevel: "off", runId, extraSystemPrompt: params.systemPrompt, extraSystemPromptStatic: params.systemPrompt, messageChannel: "openclaw", messageProvider: "openclaw", disableTools: true, disableTrajectory: true, ...responseFormat ? { streamParams: { responseFormat } } : {}, ...route.authProfileId ? { authProfileId: route.authProfileId } : {} }; const result = route.runner === "cli" ? await (params.deps?.runCliAgent ?? (await import("./cli-runner-msdgHSKR.js")).runCliAgent)({ ...shared, preparedRunAdmission, executionMode: "side-question", cleanupCliLiveSessionOnRunEnd: true }) : await (params.deps?.runEmbeddedAgent ?? (await import("./embedded-agent-CUhbhIMN.js")).runEmbeddedAgent)({ ...shared, preparedRunAdmission, toolsAllow: [], agentHarnessRuntimeOverride: route.agentHarnessRuntimeOverride, ...expectedAgentHarnessRuntimeArtifact ? { expectedAgentHarnessRuntimeArtifact } : {}, cleanupBundleMcpOnRunEnd: true, ...route.authProfileId ? { authProfileIdSource: "user" } : {} }); text = extractAgentRunText(result)?.trim(); } catch (error) { if (error instanceof SystemAgentInferenceUnavailableError) throw error; text = void 0; } finally { preparedRunAdmission?.close(); await (params.deps?.removeTempDir ?? removeTempPlannerDir)(tempDir); } if (!text) return null; await requireVerifiedPlannerRoute(params.verifiedInference, params.deps); return { text, modelLabel: route.modelLabel }; } async function requireVerifiedPlannerRoute(binding, deps) { if (!binding) throw new SystemAgentInferenceUnavailableError("planner"); try { const route = await resolveSystemAgentVerifiedInferenceRoute(binding, deps); if (route) return route; } catch (error) { throw new SystemAgentInferenceUnavailableError("planner", [error]); } throw new SystemAgentInferenceUnavailableError("planner"); } async function createTempPlannerDir() { return await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-planner-")); } async function removeTempPlannerDir(dir) { await fs.rm(dir, { recursive: true, force: true }); } //#endregion export { planSystemAgentCommand, planSystemAgentGreetingWithConfiguredModel };