UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

266 lines (265 loc) • 9.29 kB
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs"; import { n as WizardCancelledError } from "./prompts-CCXo2K24.mjs"; import { s as isGatewayTransportError } from "./transport-error-BL_kuysc.mjs"; import { l as isGatewayClientRequestError, v as resolveDeviceIdentityForGatewayCall } from "./call-DVCK1deJ.mjs"; import { r as t } from "./i18n-AiHKCZaZ.mjs"; import { t as answerGatewayWizardStep } from "./gateway-wizard-step-BShKPhM0.mjs"; import { randomUUID } from "node:crypto"; import { setTimeout } from "node:timers/promises"; //#region src/commands/onboard-remote-gateway.ts const GATEWAY_SETUP_DETECT_TIMEOUT_MS = 4e4; const GATEWAY_SETUP_ACTIVATE_TIMEOUT_MS = 15e4; const GATEWAY_CODEX_SETUP_ACTIVATE_TIMEOUT_MS = 48e4; const GATEWAY_SETUP_VERIFY_TIMEOUT_MS = 3e4; const GATEWAY_SYSTEM_AGENT_CHAT_TIMEOUT_MS = 19e4; const GATEWAY_RESTART_WAIT_TIMEOUT_MS = 45e3; const GATEWAY_RESTART_IDENTITY_ERROR = "Inference settings were saved, but the Gateway did not provide a boot identity. Update and restart the remote Gateway, then run onboarding again."; function toSetupInferenceDetection(e) { return { ...e, candidates: e.candidates.map((e) => ({ ...e, recommended: false })), authOptions: e.authOptions ?? [], recommendedInstalls: e.recommendedInstalls ?? [], unavailableCandidates: e.unavailableCandidates ?? [] }; } function activationTimeoutMs(e) { return e === "codex-cli" ? GATEWAY_CODEX_SETUP_ACTIVATE_TIMEOUT_MS : GATEWAY_SETUP_ACTIVATE_TIMEOUT_MS; } /** * Configure missing inference on the selected remote Gateway, then let that * Gateway's OpenClaw finish setup before handing off to its normal TUI. * The local config is routing input only; every setup mutation runs through * Gateway RPC. */ async function runRemoteGatewayInferenceOnboarding(e, n = defaultRuntime) { const { callGatewayCli: r } = await import("./call-CT5jlfNe.mjs"); const { runGuidedOnboarding: i } = await import("./commands/onboard-guided.js"); const a = { ...e.config, gateway: { ...e.config.gateway, mode: "remote", remote: { ...e.config.gateway?.remote, url: e.gatewayUrl, ...e.configuredRemote ? {} : { transport: "direct" } } } }; const o = Boolean(e.token || e.password); let s; const request = async (n) => await r({ ...n, config: a, ...o && !e.configuredRemote ? { url: e.gatewayUrl } : {}, ...e.token ? { token: e.token } : {}, ...e.password ? { password: e.password } : {}, ...e.tlsFingerprint ? { tlsFingerprint: e.tlsFingerprint } : {}, ignoreEnvUrlOverride: true }); const detect = async () => { const e = toSetupInferenceDetection(await request({ method: "openclaw.setup.detect", params: {}, timeoutMs: GATEWAY_SETUP_DETECT_TIMEOUT_MS })); s = e.workspace; return e; }; const activate = async (e) => { let n; const r = randomUUID(); const i = e.prompter ?? (await import("./clack-prompter-190puVWq.mjs")).createClackPrompter(); let a; try { a = await request({ method: "openclaw.setup.activate.start", params: { sessionId: r, kind: e.kind, ...e.modelTarget ? { modelTarget: e.modelTarget } : {}, ...e.agentId ? { agentId: e.agentId } : {}, ...e.modelRef !== void 0 ? { modelRef: e.modelRef } : {}, ...e.authChoice !== void 0 ? { authChoice: e.authChoice } : {}, ...e.apiKey !== void 0 ? { apiKey: e.apiKey } : {}, ...s ? { workspace: s } : {} }, timeoutMs: activationTimeoutMs(e.kind), onHelloOk: (e) => { n = e.server.bootId?.trim(); } }); while (!a.done) { e.signal?.throwIfAborted(); const n = a.step; let o; if (n) { if (a.error) await i.note(a.error); if (n.externalUrl) await i.note(n.externalUrl, "Open this URL to continue"); const e = (n.type === "confirm" || n.type === "action") && n.executor === "gateway" ? void 0 : await answerGatewayWizardStep(n, i); if (n.type !== "progress" && n.executor !== "gateway") o = { stepId: n.id, value: e }; } a = await request({ method: "wizard.next", params: { sessionId: r, ...o ? { answer: o } : {} }, signal: e.signal, timeoutMs: activationTimeoutMs(e.kind) }); } } catch (n) { if (!a?.done && (a !== void 0 || !isGatewayClientRequestError(n))) try { await request({ method: "wizard.cancel", params: { sessionId: r, closeInput: true }, timeoutMs: activationTimeoutMs(e.kind) }); } catch (e) { throw new AggregateError([n, e], "Remote activation failed and its setup session could not be closed.", { cause: e }); } throw n; } if (a.status === "cancelled") throw new WizardCancelledError(a.error); if (a.activationRejection && a.error) return { ok: false, status: a.activationRejection.status, error: a.error }; const o = a.modelActivation; if (!o) throw new Error(a.error ?? "Gateway setup ended without a verified activation."); const c = o.gatewayRestartRequired ? n : void 0; if (o.gatewayRestartRequired && !c) throw new Error(GATEWAY_RESTART_IDENTITY_ERROR); const l = performance.now() + GATEWAY_RESTART_WAIT_TIMEOUT_MS; let u = 250; for (;;) { const n = l - performance.now(); if (c && n <= 0) throw new Error("Inference settings were saved, but the Gateway did not finish restarting and verifying inference. Check the remote Gateway, then run onboarding again."); const r = new AbortController(); let i = u; try { const i = await request({ method: "openclaw.setup.verify", params: { ...e.modelTarget ? { modelTarget: e.modelTarget } : {}, ...e.agentId ? { agentId: e.agentId } : {} }, timeoutMs: c ? Math.min(GATEWAY_SETUP_VERIFY_TIMEOUT_MS, n) : GATEWAY_SETUP_VERIFY_TIMEOUT_MS, signal: r.signal, onHelloOk: (e) => { if (!c) return; const n = e.server.bootId?.trim(); if (!n || n === c) r.abort(n ? "unchanged-boot" : "missing-boot"); } }); if (!c || i.ok || i.status !== "unavailable") { const n = e.modelRef || void 0; const r = e.modelTarget || void 0; if (o.modelTarget !== r) throw new Error("Gateway activated a different model role than the selected connection. Refresh model setup and try again."); if (n && o.modelRef.trim() !== n.trim()) throw new Error(`Gateway activated ${o.modelRef}, not the selected ${n}.`); if (!i.ok) throw new Error(`Gateway inference verification failed: ${i.error}`); if (i.modelRef.trim() !== o.modelRef.trim()) throw new Error(`Gateway verified ${i.modelRef}, not the activated ${o.modelRef}.`); if (i.modelTarget !== o.modelTarget) throw new Error("Gateway verified a different model role than the activated connection."); return { ok: true, ...o, latencyMs: i.latencyMs, lines: [] }; } } catch (e) { if (r.signal.reason === "missing-boot") throw new Error(GATEWAY_RESTART_IDENTITY_ERROR, { cause: e }); if (!(c && (r.signal.reason === "unchanged-boot" || isGatewayTransportError(e) || isGatewayClientRequestError(e) && e.retryable))) throw e; if (isGatewayClientRequestError(e)) i = e.retryAfterMs ?? u; } await setTimeout(Math.min(i, Math.max(0, l - performance.now()))); u = Math.min(u * 2, 2e3); } }; await i({}, n, { detect, activate, handoffMode: "chat", runSetupMemoryImportStep: async () => ({ status: "skipped", providers: [] }), runSystemAgentChat: async () => { const { createClackPrompter: n } = await import("./clack-prompter-190puVWq.mjs"); const r = n(); await r.intro("OpenClaw"); const i = await resolveDeviceIdentityForGatewayCall(); const o = randomUUID(); let s = await request({ method: "openclaw.chat", deviceIdentity: i, params: { sessionId: o, welcomeVariant: "onboarding" }, timeoutMs: GATEWAY_SYSTEM_AGENT_CHAT_TIMEOUT_MS }); let c; try { for (;;) { await r.note(s.reply, "OpenClaw"); if (s.action === "exit") { await r.outro("OpenClaw setup finished."); return; } if (s.action === "open-agent") { c = s.agentDraft; await r.outro("Opening your agent…"); break; } const e = await r.text({ message: "Reply to OpenClaw", ...s.sensitive ? { sensitive: true } : {}, validate: (e) => e.trim() ? void 0 : "Required" }); s = await request({ method: "openclaw.chat", deviceIdentity: i, params: { sessionId: o, message: e }, timeoutMs: GATEWAY_SYSTEM_AGENT_CHAT_TIMEOUT_MS }); } } catch (e) { if (e instanceof WizardCancelledError) { await r.outro("OpenClaw setup paused."); return; } throw e; } const { runTui: l } = await import("./tui-CRxv9Fqs.mjs"); await l({ config: a, deliver: false, ...c === "hatch" ? { message: t("wizard.finalize.bootstrapHatchMessage") } : {}, boundGateway: { url: e.gatewayUrl, ...e.configuredRemote ? { configuredRemote: true } : {}, ...e.token ? { token: e.token } : {}, ...e.password ? { password: e.password } : {}, ...e.tlsFingerprint ? { tlsFingerprint: e.tlsFingerprint } : {} } }); } }); } //#endregion export { runRemoteGatewayInferenceOnboarding };