UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

241 lines (240 loc) • 9.24 kB
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId, t as DEFAULT_ACCOUNT_ID } from "./account-id-BqKT3GuY.mjs"; import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs"; import { D as tryResolveAgentOperationAgentId, h as resolveConfiguredAgentId, t as AgentSelectionRequiredError } from "./agent-scope-config-DmXgFGKP.mjs"; import { n as resolveChannelSetupExecutionAdapter } from "./setup-contract-hyC_qGOR.mjs"; import { f as readCurrentConfigForPolicyCheckAsync, l as readConfigFileSnapshotForWrite } from "./io.runtime-DsJ2dLCQ.mjs"; import { n as getLoadedChannelPlugin, t as getChannelPlugin } from "./registry-ByqVE5S3.mjs"; import "./plugins-BoMiRtiY.mjs"; import "./config-CMA-J084.mjs"; import { n as describeChannelAccount, r as resolveChannelAccount } from "./account-resolution-D7u1J1M0.mjs"; import { a as formatUnknownChannelMessage } from "./error-format-O7EzXfiI.mjs"; import { t as applyAgentBindings } from "./agents.bindings-Cz76H01e.mjs"; import { t as resolveChannelSetupOwner } from "./owner-DJdrBk7A.mjs"; import { i as withCommandPluginMetadata } from "./config-validation-Cy6dkTWs.mjs"; import { t as describeBinding } from "./agents.binding-format-DRkepwQw.mjs"; import { t as persistChannelPluginConfig } from "./plugin-config-persistence-B-x9ZZc_.mjs"; //#region src/commands/channels/add-wizard.ts function unresolvedInitialWizardChannelTarget(e) { return { kind: "unresolved", message: formatUnknownChannelMessage({ channel: e }) }; } /** Select a setup owner before workspace-scoped channel discovery. */ async function selectChannelSetupOwner(e, t, n) { const r = e.snapshot.sourceConfig; try { return resolveChannelSetupOwner(r, n); } catch (e) { if (!(e instanceof AgentSelectionRequiredError)) throw e; } const i = await t.select({ message: "Set up channels for agent", options: listAgentIds(r).map((e) => ({ value: { agentId: e }, label: e })) }); if (!isRecord(i) || typeof i.agentId !== "string") throw new Error("Invalid channel setup owner selection"); e.writeOptions.assertConfigPathForWrite?.(); const a = await readCurrentConfigForPolicyCheckAsync({ configPath: e.snapshot.path, env: process.env }); e.writeOptions.assertConfigPathForWrite?.(); const o = resolveConfiguredAgentId(a, i.agentId); return resolveChannelSetupOwner(a, o); } /** Resolve omitted, matched, and unmatched channel targets without collapsing caller intent. */ async function resolveInitialWizardChannelTarget(e, t, n) { if (e === void 0) return { kind: "omitted" }; const r = normalizeOptionalLowercaseString(e); if (!r) return unresolvedInitialWizardChannelTarget(""); const [{ listActiveChannelSetupPlugins: i }, { resolveChannelSetupEntries: a }] = await Promise.all([import("./setup-registry-B44ytNjV.mjs"), import("./discovery-BIieIm-B.mjs")]); const o = a({ cfg: t, installedPlugins: i(), workspaceDir: n ?? resolveChannelSetupOwner(t).workspaceDir }); const s = o.entries.find((e) => normalizeOptionalLowercaseString(e.id) === r) ?? o.entries.find((e) => (e.meta.aliases ?? []).some((e) => normalizeOptionalLowercaseString(e) === r)); return s ? { kind: "resolved", channel: s.id } : unresolvedInitialWizardChannelTarget(e.trim()); } /** Run the interactive channel-setup flow and persist the resulting config. */ async function runChannelsAddWizardFlow(e) { const { writeSnapshot: t, runtime: n, prompter: r } = e; const { sourceConfig: i, hash: a } = t.snapshot; const [{ buildAgentSummaries: o }, s] = await Promise.all([import("./agents.config-42cov6H0.mjs"), import("./channel-setup-BomzjZ7V.mjs")]); const c = s.createChannelSetupHooks({ runtime: n, ...e.beforePersistentEffect ? { beforePersistentEffect: e.beforePersistentEffect } : {} }); let l = []; const u = {}; const d = /* @__PURE__ */ new Map(); await r.intro("Channel setup"); let f = await s.setupChannels(i, n, r, { ...e.workspaceDir ? { workspaceDir: e.workspaceDir } : {}, ...e.initialChannel ? { initialSelection: [e.initialChannel], finishAfterInitialSelection: true } : {}, allowDisable: false, allowIMessageInstall: true, allowSignalInstall: true, ...e.beforePersistentEffect ? { beforePersistentEffect: e.beforePersistentEffect } : {}, ...e.assertPersistentEffectCurrent ? { assertPersistentEffectCurrent: e.assertPersistentEffectCurrent } : {}, ...e.deferDeviceLinkToClient ? { deferDeviceLinkToClient: true } : {}, onPostWriteHook: c.onPostWriteHook, promptAccountIds: true, deferStatusUntilSelection: true, skipStatusNote: true, onSelection: (e) => { l = e; }, onAccountId: (e, t) => { u[e] = t; }, onResolvedPlugin: (e, t) => { d.set(e, t); } }); const commitWizardConfig = async (r) => { const o = await persistChannelPluginConfig({ cfg: r, setup: { previousConfig: i, beforePersistentEffect: e.beforePersistentEffect, assertCurrent: () => { t.writeOptions.assertConfigPathForWrite?.(); e.assertPersistentEffectCurrent?.(); } }, pluginInstalled: false, writeOptions: t.writeOptions, baseHash: a, runtime: n }); await c.runPostWriteHooks(o.path); }; if (l.length === 0) { if (f !== i) { await commitWizardConfig(f); await r.outro("Channels updated."); return; } await r.outro("No channel changes made."); return; } const p = e.initialChannel !== void 0 && l.length === 1 && l[0] === e.initialChannel; if (p ? false : await r.confirm({ message: "Name these channel accounts now? (optional)", initialValue: false })) await withCommandPluginMetadata({ config: f, workspaceDir: e.workspaceDir }, async () => { for (const e of l) { const t = u[e] ?? "default"; const n = d.get(e) ?? getLoadedChannelPlugin(e); const i = n ? await resolveChannelAccount({ plugin: n, cfg: f, accountId: t }) : void 0; const a = (n ? await describeChannelAccount({ plugin: n, account: i, cfg: f }) : void 0)?.name ?? i?.name; const o = await r.text({ message: `${e} display name for account "${t}"`, initialValue: a }); if (o?.trim()) { const r = n ?? getChannelPlugin(e); const i = r ? resolveChannelSetupExecutionAdapter(r)?.applyAccountName : void 0; if (i) f = i({ cfg: f, accountId: normalizeAccountId(t), name: o }); } } }); const m = l.flatMap((e) => { const t = u[e]?.trim(); return t ? [{ channel: e, accountId: t }] : []; }); if (m.length > 0) { const t = await o(f); if (p && t.length <= 1 ? false : p ? true : await r.confirm({ message: "Route these channel accounts to agents now?", initialValue: true })) { const n = tryResolveAgentOperationAgentId(f); const i = n === void 0 ? void 0 : resolveConfiguredAgentId(f, n); for (const n of m) { const a = await r.select({ message: `Send ${n.channel}/${n.accountId} messages to agent`, options: t.map((e) => ({ value: e.id, label: e.isDefault ? `${e.id} (default)` : e.id })), initialValue: e.agentId ?? i }); const o = applyAgentBindings(f, [{ agentId: a, match: { channel: n.channel, accountId: n.accountId } }]); f = o.config; if (o.added.length > 0 || o.updated.length > 0) await r.note([...o.added.map((e) => `Added: ${describeBinding(e)}`), ...o.updated.map((e) => `Updated: ${describeBinding(e)}`)].join("\n"), "Routing bindings"); if (o.conflicts.length > 0) await r.note(["Skipped bindings already claimed by another agent:", ...o.conflicts.map((e) => `- ${describeBinding(e.binding)} (agent=${e.existingAgentId})`)].join("\n"), "Routing bindings"); } } } await commitWizardConfig(f); e.onConfigured?.(l.map((e) => ({ channel: e, accountId: u[e] ?? "default" }))); await r.outro("Channels updated."); } /** * Gateway entry for `wizard.start {flow:"channels"}`. Unlike the CLI path this * must never call runtime.exit — failures throw and surface as wizard errors. */ async function runChannelsSetupWizard(e, t, n) { const r = await readConfigFileSnapshotForWrite(); const { snapshot: i } = r; if (i.exists && !i.valid) throw new Error("OpenClaw config is invalid; run `openclaw doctor --fix`, then retry channel setup."); const a = i.sourceConfig; const { agentId: o, workspaceDir: s } = await selectChannelSetupOwner(r, n); const c = await resolveInitialWizardChannelTarget(e.channel, a, s); if (c.kind === "unresolved") throw new Error(c.message); await runChannelsAddWizardFlow({ writeSnapshot: r, agentId: o, runtime: t, prompter: n, workspaceDir: s, ...c.kind === "resolved" ? { initialChannel: c.channel } : {}, deferDeviceLinkToClient: true, ...e.onConfigured ? { onConfigured: e.onConfigured } : {}, ...e.beforePersistentEffect ? { beforePersistentEffect: e.beforePersistentEffect } : {}, ...e.assertPersistentEffectCurrent ? { assertPersistentEffectCurrent: e.assertPersistentEffectCurrent } : {} }); } //#endregion export { resolveInitialWizardChannelTarget, runChannelsAddWizardFlow, runChannelsSetupWizard, selectChannelSetupOwner };