openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
140 lines (139 loc) • 6.28 kB
JavaScript
import { a as writeRuntimeJson, r as defaultRuntime } from "../runtime-CF2WjnNZ.js";
import { a as isPersistentSystemAgentOperation, o as parseSystemAgentOperation } from "../operations-parse-DIiMdt6S.js";
import { t as executeSystemAgentOperation } from "../operations-BAC9TFg6.js";
import { r as withProgress } from "../progress-DSeqxbrQ.js";
import { t as SystemAgentInferenceUnavailableError } from "../inference-error-Dyv9IgLa.js";
import { i as loadSystemAgentOverview, n as formatSystemAgentOverview } from "../overview-BoFF75kz.js";
import { a as resolveSystemAgentVerifiedInferenceRoute, r as hasCurrentSystemAgentOwnerPluginArtifacts } from "../verified-inference-D8I0G1w6.js";
import { n as resolveSystemAgentOperation } from "../dialogue-DF7jf9Tr.js";
import { stdin, stdout } from "node:process";
//#region src/system-agent/system-agent.ts
function systemAgentCommandDepsFromOptions(opts) {
if (!opts.deps && !opts.formatOverview && !opts.loadOverview) return;
return {
...opts.deps,
...opts.formatOverview ? { formatOverview: opts.formatOverview } : {},
...opts.loadOverview ? { loadOverview: opts.loadOverview } : {}
};
}
async function requireVerifiedInference(opts) {
if (!opts.verifiedInference) throw new SystemAgentInferenceUnavailableError("conversation");
try {
if (await resolveSystemAgentVerifiedInferenceRoute(opts.verifiedInference, opts.deps)) return;
} catch (error) {
throw new SystemAgentInferenceUnavailableError("conversation", [error]);
}
throw new SystemAgentInferenceUnavailableError("conversation");
}
async function requirePersistentApplyInference(opts, runtime) {
if (!opts.verifiedInference) throw new SystemAgentInferenceUnavailableError("conversation");
try {
const { resolvePersistentApplyInference } = await import("./setup-inference.js");
if (await resolvePersistentApplyInference({
binding: opts.verifiedInference,
runtime,
deps: opts.deps
})) return;
} catch (error) {
if (error instanceof SystemAgentInferenceUnavailableError) throw error;
throw new SystemAgentInferenceUnavailableError("conversation", [error]);
}
throw new SystemAgentInferenceUnavailableError("conversation");
}
async function runOneShot(operation, runtime, opts) {
if (operation.kind === "none" && operation.message === "") return;
await requireVerifiedInference(opts);
const approved = opts.yes === true || !isPersistentSystemAgentOperation(operation);
if (approved && isPersistentSystemAgentOperation(operation)) await requirePersistentApplyInference(opts, runtime);
await executeSystemAgentOperation(operation, runtime, {
approved,
deps: systemAgentCommandDepsFromOptions(opts)
});
}
/** Run OpenClaw in JSON, one-shot message, or interactive TUI mode. */
async function runSystemAgent(opts, runtime = defaultRuntime) {
const binding = opts?.verifiedInference;
if (!binding) throw new SystemAgentInferenceUnavailableError("conversation");
const boundOpts = {
...opts,
verifiedInference: binding
};
const run = () => runBoundSystemAgent(boundOpts, runtime);
const route = binding.execution;
if (route.runner !== "embedded" || route.agentHarnessRuntimeOverride === "openclaw") return await run();
const { resolveAgentWorkspaceDir } = await import("../agent-scope-BHLMs19p.js");
const { loadAgentRuntimePluginRegistryHandle } = await import("../runtime-plugins-fj7bbrVP.js");
const { withPluginLifecycleLease } = await import("../plugin-lifecycle-lease-CLKma701.js");
const { withPluginRuntimeRegistryScope } = await import("../gateway-request-scope-CncyXiPJ.js");
const readSnapshot = boundOpts.deps?.readConfigFileSnapshot ?? (await import("../config/config.js")).readConfigFileSnapshot;
const registry = await withPluginLifecycleLease({}, async (lease) => {
const snapshot = await readSnapshot();
if (!await hasCurrentSystemAgentOwnerPluginArtifacts(binding, {
...boundOpts.deps,
readConfigFileSnapshot: async () => snapshot
})) throw new SystemAgentInferenceUnavailableError("conversation");
const config = snapshot.runtimeConfig ?? snapshot.config;
const workspaceDir = resolveAgentWorkspaceDir(config, route.agentId);
lease.assertOwned();
return loadAgentRuntimePluginRegistryHandle({
basePluginIds: [],
config,
workspaceDir,
selections: [{
provider: route.provider,
modelId: route.model,
runtime: route.agentHarnessRuntimeOverride,
agentId: route.agentId
}]
});
});
if (!registry) throw new SystemAgentInferenceUnavailableError("conversation");
await withPluginRuntimeRegistryScope(registry, run);
}
async function runBoundSystemAgent(boundOpts, runtime) {
await requireVerifiedInference(boundOpts);
if (boundOpts.json) {
const overview = await (boundOpts.loadOverview ?? loadSystemAgentOverview)();
writeRuntimeJson(runtime, overview);
return;
}
if (boundOpts.message?.trim()) {
const parsed = parseSystemAgentOperation(boundOpts.message);
if (parsed.kind === "overview") {
await runOneShot(parsed, runtime, boundOpts);
return;
}
const overview = await withProgress({
label: "Loading OpenClaw overview…",
indeterminate: true,
delayMs: 0,
fallback: "none"
}, async () => await (boundOpts.loadOverview ?? loadSystemAgentOverview)());
runtime.log((boundOpts.formatOverview ?? formatSystemAgentOverview)(overview));
runtime.log("");
await runOneShot(await resolveSystemAgentOperation(boundOpts.message, runtime, {
...boundOpts,
loadOverview: async () => overview
}), runtime, boundOpts);
return;
}
if (boundOpts.interactive === false) {
const overview = await (boundOpts.loadOverview ?? loadSystemAgentOverview)();
runtime.log((boundOpts.formatOverview ?? formatSystemAgentOverview)(overview));
return;
}
const input = boundOpts.input ?? stdin;
const output = boundOpts.output ?? stdout;
const inputIsTty = input.isTTY === true;
const outputIsTty = output.isTTY === true;
if (!inputIsTty || !outputIsTty) {
runtime.error("OpenClaw needs an interactive TTY. Use --message for one command.");
runtime.exit(1);
return;
}
const runInteractiveTui = boundOpts.runInteractiveTui ?? (await import("../tui-backend-Cgmss4OU.js")).runSystemAgentTui;
boundOpts.onReady?.();
await runInteractiveTui(boundOpts, runtime);
}
//#endregion
export { runSystemAgent };