openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
134 lines (133 loc) • 6.14 kB
JavaScript
import { a as writeRuntimeJson, r as defaultRuntime } from "../runtime-BRg2NubB.mjs";
import { t as _usingCtx } from "../usingCtx-BhiZp_rS.mjs";
import { a as isPersistentSystemAgentOperation, o as parseSystemAgentOperation } from "../operations-parse-C5s4RucT.mjs";
import { t as executeSystemAgentOperation } from "../operations-JhXppu9O.mjs";
import { r as withProgress } from "../progress-CGcf72of.mjs";
import { n as SystemAgentInferenceUnavailableError, t as requireSystemAgentInferenceRoute } from "../inference-guard-BxdOp_Ea.mjs";
import { i as loadSystemAgentOverview, n as formatSystemAgentOverview } from "../overview-BcjlGHpv.mjs";
import { r as hasCurrentSystemAgentOwnerPluginArtifacts } from "../verified-inference-CrWsRNYK.mjs";
import { n as resolveSystemAgentOperation } from "../dialogue-1WDyzajw.mjs";
import { stdin, stdout } from "node:process";
//#region src/system-agent/system-agent.ts
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], "route-changed");
}
throw new SystemAgentInferenceUnavailableError("conversation", [], "route-changed");
}
async function runOneShot(operation, runtime, opts) {
if (operation.kind === "none" && operation.message === "") return;
await requireSystemAgentInferenceRoute(opts.verifiedInference, opts.deps, "conversation");
const approved = opts.yes === true || !isPersistentSystemAgentOperation(operation);
if (approved && isPersistentSystemAgentOperation(operation)) await requirePersistentApplyInference(opts, runtime);
await executeSystemAgentOperation(operation, runtime, {
approved,
deps: opts.deps || opts.formatOverview || opts.loadOverview ? {
...opts.deps,
...opts.formatOverview ? { formatOverview: opts.formatOverview } : {},
...opts.loadOverview ? { loadOverview: opts.loadOverview } : {}
} : void 0
});
}
async function runSystemAgent(opts, runtime = defaultRuntime) {
try {
var _usingCtx$1 = _usingCtx();
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-HzHUNfyn.mjs");
const { loadAgentRuntimePluginRegistryHandle } = await import("../runtime-plugins-C7R1YgRY.mjs");
const { withPluginLifecycleLease } = await import("../plugin-lifecycle-lease-DCNQD_-Y.mjs");
const { createPluginCache, withPluginCache } = await import("../plugin-cache-Lgl70ptw.mjs");
const { withPluginRuntimeRegistryScope } = await import("../gateway-request-scope-BQknVQxo.mjs");
const cache = _usingCtx$1.a(createPluginCache());
const readSnapshot = boundOpts.deps?.readConfigFileSnapshot ?? (await import("../config/config.js")).readConfigFileSnapshot;
await withPluginRuntimeRegistryScope(await withPluginLifecycleLease({}, async (lease) => {
const snapshot = await readSnapshot();
if (!await hasCurrentSystemAgentOwnerPluginArtifacts(binding, {
...boundOpts.deps,
readConfigFileSnapshot: async () => snapshot
})) throw new SystemAgentInferenceUnavailableError("conversation", [], "route-changed");
const config = snapshot.runtimeConfig ?? snapshot.config;
const workspaceDir = resolveAgentWorkspaceDir(config, route.agentId);
lease.assertOwned();
return withPluginCache(cache, () => loadAgentRuntimePluginRegistryHandle({
basePluginIds: [],
config,
workspaceDir,
selections: [{
provider: route.provider,
modelId: route.model,
runtime: route.agentHarnessRuntimeOverride,
agentId: route.agentId
}]
}));
}), run);
} catch (_) {
_usingCtx$1.e = _;
} finally {
await _usingCtx$1.d();
}
}
async function runBoundSystemAgent(boundOpts, runtime) {
await requireSystemAgentInferenceRoute(boundOpts.verifiedInference, boundOpts.deps, "conversation");
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-DKa8UYpl.mjs")).runSystemAgentTui;
boundOpts.onReady?.();
await runInteractiveTui(boundOpts, runtime);
}
//#endregion
export { runSystemAgent };