openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
233 lines (232 loc) • 9.8 kB
JavaScript
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { i as createPluginCache, w as withPluginCache } from "./plugin-cache-B_eRuqTM.mjs";
import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { t as runOutsidePluginRuntimeGenerationScope } from "./generation-scope-Cq7RLFex.mjs";
import { i as appendSystemAgentAuditEntry } from "./audit-nBEMz_02.mjs";
import { stat } from "node:fs/promises";
//#region src/system-agent/hosted-setup.runtime.ts
const log = createSubsystemLogger("system-agent/chat-wizard-host");
const GATEWAY_WRITE_POLICY = {
mode: "none",
reason: "Gateway setup defers runtime apply until explicit restart"
};
function requireLocalGateway(e) {
if (e.gateway?.mode === "local") return;
throw new Error("Hosted Gateway setup manages only a local Gateway. Use `openclaw onboard` for fresh setup or `openclaw configure` for the mode question, then retry after selecting local mode.");
}
function createHostedWizardRuntime(e) {
return {
...e,
exit: (e) => {
throw new Error(`hosted wizard exited with code ${String(e)}`);
}
};
}
async function runHostedSetup(e) {
try {
var t = _usingCtx();
const n = t.a(createPluginCache());
return await runOutsidePluginRuntimeGenerationScope(() => withPluginCache(n, async () => {
const { readSetupConfigFileSnapshot: t, writeWizardConfigFile: n } = await import("./setup.shared-D5wOOEt0.mjs");
const r = await t();
if (!r.exists || !r.valid || !r.hash) throw new Error(`${e.label} requires a valid saved config snapshot. On the machine running OpenClaw, run \`openclaw doctor --fix\` and resolve any remaining validation errors; then retry.`);
const i = r.sourceConfig ?? r.config;
const a = e.runtime ?? createHostedWizardRuntime(defaultRuntime);
const o = await e.run({
baseConfig: i,
runtime: a
});
if ("keptCurrent" in o) return "kept-current";
await e.beforePersistentApply(a);
const s = await n(o.nextConfig, {
allowConfigSizeDrop: false,
baseHash: r.hash,
...e.afterWrite ? { afterWrite: e.afterWrite } : {}
});
await o.afterWrite?.(s.path);
return "applied";
}));
} catch (e) {
t.e = e;
} finally {
await t.d();
}
}
async function runHostedChannelSetup(e, t, n, r, i) {
const { createChannelSetupHooks: a, setupChannels: o } = await import("./channel-setup-BomzjZ7V.mjs");
return await runHostedSetup({
label: "Channel setup",
runtime: r,
beforePersistentApply: n,
run: async ({ baseConfig: r, runtime: s }) => {
const c = a({
runtime: s,
beforePersistentEffect: async () => await n(s)
});
return {
nextConfig: await o(r, s, t, {
initialSelection: [e],
forceAllowFromChannels: [e],
allowIMessageInstall: true,
allowSignalInstall: true,
deferStatusUntilSelection: true,
quickstartDefaults: true,
skipDmPolicyPrompt: true,
skipConfirm: true,
beforePersistentEffect: async () => await n(s),
...i ? { assertPersistentEffectCurrent: i } : {},
onPostWriteHook: c.onPostWriteHook
}),
afterWrite: async (e) => await c.runPostWriteHooks(e)
};
}
});
}
async function runHostedSkillsSetup(e, t, n) {
const [{ setupSkills: r }, { resolveSystemAgentOnboardingTarget: i }] = await Promise.all([import("./onboard-skills-Cqi3hcq1.mjs"), import("./onboard-agent-target-D-wF44vd.mjs")]);
return await runHostedSetup({
label: "Skills setup",
runtime: n,
beforePersistentApply: t,
run: async ({ baseConfig: n, runtime: a }) => ({ nextConfig: await r(n, i(n).workspaceDir, a, e, { beforePersistentEffect: async () => await t(a) }) })
});
}
async function runHostedSearchSetup(e, t, n) {
const { runSearchSetupFlow: r } = await import("./search-setup-BUgcR-Xv.mjs");
return await runHostedSetup({
label: "Web search setup",
runtime: n,
beforePersistentApply: t,
run: async ({ baseConfig: n, runtime: i }) => {
const a = await r(n, i, e, {
preserveDisabledSearchState: false,
beforePersistentEffect: async () => await t(i)
});
if (a.outcome === "install-failed") {
const e = a.reason === "timed-out" ? "timed out" : "failed";
throw new Error(`web search provider ${a.providerId} installation ${e}`);
}
if (a.outcome === "kept-current") {
if (a.reason === "user-skipped" || a.reason === "provider-install-skipped") return { keptCurrent: true };
const e = a.reason === "no-providers" ? "no web search providers are available under the current plugin policy" : "the selected web search provider is no longer available";
throw new Error(e);
}
return { nextConfig: a.config };
}
});
}
async function runHostedGatewaySetup(e, t, n) {
const [{ configureGatewayForSetup: r }, { resolveQuickstartGatewayDefaults: i }] = await Promise.all([import("./setup.gateway-config-KSUmfYUN.mjs"), import("./setup.shared-D5wOOEt0.mjs")]);
return await runHostedSetup({
label: "Gateway setup",
runtime: n,
beforePersistentApply: t,
afterWrite: GATEWAY_WRITE_POLICY,
run: async ({ baseConfig: t }) => {
requireLocalGateway(t);
return { nextConfig: (await r({
flow: "advanced",
baseConfig: t,
nextConfig: t,
quickstartGateway: i(t),
prompter: e
})).nextConfig };
}
});
}
async function runHostedMemoryImport(e, t, n) {
const [{ readSetupConfigFileSnapshot: r }, { resolveSystemAgentOnboardingTarget: i }] = await Promise.all([import("./setup.shared-D5wOOEt0.mjs"), import("./onboard-agent-target-D-wF44vd.mjs")]);
const a = await r();
if (!a.exists || !a.valid || !a.hash) throw new Error("Memory import requires a valid saved config. Run `openclaw doctor --fix`, then retry.");
const o = a.config;
const { agentId: s, workspaceDir: c } = i(o);
try {
if (!(await stat(c)).isDirectory()) return {
status: "workspace-missing",
providers: [],
workspace: c
};
} catch (e) {
const t = e.code;
if (t === "ENOENT" || t === "ENOTDIR") return {
status: "workspace-missing",
providers: [],
workspace: c
};
throw e;
}
const { runSetupMemoryImportStep: l } = await import("./setup.memory-import-DvHgkMyZ.mjs");
const u = createHostedWizardRuntime(defaultRuntime);
return await l({
config: o,
agentId: s,
prompter: e,
runtime: u,
beforeApply: async () => await t(u),
onProviderOutcome: n
});
}
function hasConfirmedMemoryImportCount(e) {
return e.copiesIndeterminate !== true;
}
function formatItemCount(e) {
return `${e} ${e === 1 ? "item" : "items"}`;
}
function formatMemoryImportProviders(e) {
return e.map((e) => `${e.label} (${formatItemCount(e.migrated)})`).join(", ");
}
async function auditMemoryImport(e) {
const t = e.filter(hasConfirmedMemoryImportCount).filter((e) => e.migrated > 0);
const n = e.filter((e) => e.copiesIndeterminate === true);
const r = t.reduce((e, t) => e + t.migrated, 0);
if (r === 0 && n.length === 0) return;
const i = formatMemoryImportProviders(t);
const a = n.map((e) => `${e.label} (copy count indeterminate)`).join(", ");
const o = n.length > 0 ? `Memory import failed partway via chat: ${[i ? `confirmed ${i}` : "", a].filter(Boolean).join("; ")}` : `Imported memory via chat: ${i}`;
try {
await appendSystemAgentAuditEntry({
operation: "memory.import",
summary: o,
details: {
...n.length > 0 ? {
confirmedItems: r,
copiesIndeterminate: true
} : { totalItems: r },
providers: e.map((e) => e.copiesIndeterminate === true ? {
providerId: e.providerId,
copiesIndeterminate: true
} : {
providerId: e.providerId,
items: e.migrated,
...e.failure ? { partial: true } : {}
})
}
});
} catch (e) {
log.warn(`memory import completed without audit entry: ${formatErrorMessage(e)}`);
}
}
async function renderMemoryImport(e) {
if (!e) return "Memory import did not complete. No outcome was reported, and no success was assumed.";
if (e.status === "workspace-missing") return [`Memory import is unavailable because the default agent workspace does not exist at ${e.workspace}.`, "Finish onboarding first with `openclaw onboard`, then retry."].join("\n");
if (e.status === "nothing-to-import") return "Nothing to import — no new memory files were detected in supported local agent homes.";
if (e.status === "skipped") return "Memory import skipped. Nothing was copied.";
const t = e.providers.filter(hasConfirmedMemoryImportCount);
const n = t.filter((e) => e.migrated > 0);
const r = t.filter((e) => e.failure);
const i = e.providers.filter((e) => e.copiesIndeterminate === true);
const a = n.reduce((e, t) => e + t.migrated, 0);
const o = formatMemoryImportProviders(n);
await auditMemoryImport(e.providers);
if (a === 0) {
if (i.length > 0) return ["Memory import failed partway. Some files may have been copied before the failure.", `Copy counts are indeterminate for: ${i.map((e) => e.label).join(", ")}.`].join("\n");
if (r.length > 0) return ["Memory import did not complete. No files were copied.", `Failed providers: ${r.map((e) => e.label).join(", ")}.`].join("\n");
return "Nothing was imported. No files were copied.";
}
const s = n.length === 1 ? n[0].label : o;
return [`Imported ${formatItemCount(a)} from ${s}.`, i.length > 0 ? `Memory import failed partway for ${i.map((e) => e.label).join(", ")}; some additional files may have been copied before the failure.` : r.length > 0 ? `Some providers did not complete: ${r.map((e) => e.label).join(", ")}.` : ""].filter(Boolean).join("\n");
}
//#endregion
export { GATEWAY_WRITE_POLICY, auditMemoryImport, renderMemoryImport, requireLocalGateway, runHostedChannelSetup, runHostedGatewaySetup, runHostedMemoryImport, runHostedSearchSetup, runHostedSetup, runHostedSkillsSetup };