openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
154 lines (153 loc) • 5.97 kB
JavaScript
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { p as resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot-By9ySblt.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { f as prepareSystemAgentRunAdmission } from "./admitted-run-context-D3Jvk_f_.mjs";
import { r as SystemAgentInferenceUnavailableError, t as requireSystemAgentInferenceRoute } from "./inference-guard-D92Jot8f.mjs";
import { i as resolveSystemAgentExpectedAgentHarnessRuntimeArtifact } from "./verified-inference-B9tngoud.mjs";
import { n as extractAgentRunTerminalError, r as extractAgentRunText } from "./agent-run-result-Bi1gRTvj.mjs";
import { t as SessionManager } from "./session-manager-DuaaMI5O.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-BNCBVqvM.mjs";
import path from "node:path";
import { randomUUID } from "node:crypto";
import fs from "node:fs/promises";
import os from "node:os";
//#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
};
/** Plan only through the configured default agent's verified route. */
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;
}
/** One tool-free, verified inference turn for the cached caretaker greeting. */
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 ?? (() => {
try {
const e = resolveAgentWorkspaceDir(t.runConfig, t.agentId);
const n = resolvePluginMetadataSnapshot({
config: t.runConfig,
workspaceDir: e,
env: process.env,
allowWorkspaceScopedCurrent: true
}).plugins;
const r = /* @__PURE__ */ new Set([normalizeProviderId(t.provider), normalizeProviderId(t.modelLabel.split("/", 1)[0] ?? "")]);
return n.some((e) => Object.entries(e.modelPricing?.providers ?? {}).some(([e, t]) => r.has(normalizeProviderId(e)) && t.external === false)) ? 12e4 : 3e4;
} catch {
return 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-D5gydz-B.mjs")).runCliAgent({
...l,
preparedRunAdmission: o,
executionMode: "side-question",
cleanupCliLiveSessionOnRunEnd: true
}) : await (await import("./embedded-agent-CncWgJKu.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
};
}
//#endregion
export { planSystemAgentCommand, planSystemAgentGreetingWithConfiguredModel };