openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
228 lines (227 loc) • 8.67 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { E as parseStrictNonNegativeInteger } from "./number-coercion-D6u-4YD8.mjs";
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { n as createLazyPromise } from "./lazy-promise-DiCvjz9w.mjs";
import { i as getBundledChannelSetupPlugin } from "./bundled-BnH1gY8j.mjs";
import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs";
import { n as getLoadedChannelPlugin } from "./registry-ByqVE5S3.mjs";
import "./plugins-BoMiRtiY.mjs";
import { n as WizardCancelledError } from "./prompts-CCXo2K24.mjs";
import { r as parseOptionalDelimitedEntries } from "./helpers-ELZNDAnv.mjs";
import { a as formatUnknownChannelMessage, o as formatUnsupportedChannelActionMessage } from "./error-format-O7EzXfiI.mjs";
import { r as isTerminalInteractive } from "./terminal-interactivity-B_L_vZTP.mjs";
import { t as createClackPrompter } from "./clack-prompter-oqVcatgQ.mjs";
import { n as resolveChannelSetupCliOptionMetadata, t as channelOmitsEnvBackedSetupOption } from "./cli-add-options-BQ8frJVq.mjs";
import { t as parseAccountSelector } from "./account-selector-DYSCYrB8.mjs";
import { t as resolveChannelSetupOwner } from "./owner-DJdrBk7A.mjs";
import { i as withCommandPluginMetadata, r as requireValidConfigForWrite } from "./config-validation-Cy6dkTWs.mjs";
import { t as normalizeExternalChannelSetupConfig } from "./config-compatibility-BpBiCOfQ.mjs";
import { t as persistChannelPluginConfig } from "./plugin-config-persistence-B-x9ZZc_.mjs";
import { i as prepareChannelAccountConfiguration, r as applyPreparedChannelAccountConfiguration, t as channelLabel } from "./runtime-label-CUF9eNdr.mjs";
import "./shared-yNot1pgX.mjs";
//#region src/commands/channels/add.ts
const loadOnboardChannels = createLazyPromise(() => import("./channel-setup-BomzjZ7V.mjs"));
const CHANNEL_ADD_CONTROL_OPTION_KEYS = /* @__PURE__ */ new Set([
"agent",
"channel",
"account"
]);
function buildChannelSetupInput(e, t) {
const n = t === "legacy" ? resolveChannelSetupCliOptionMetadata(e.channel).valueMetadataByAttributeName : null;
const r = Object.entries(e).filter(([e, t]) => !CHANNEL_ADD_CONTROL_OPTION_KEYS.has(e) && t !== void 0);
if (!n) return Object.fromEntries(r);
const i = {};
for (const [e, t] of r) {
const r = n.get(e);
if (r?.valueType !== "int") {
i[e] = r?.valueType === "list" ? Array.isArray(t) ? t.filter((e) => typeof e === "string") : parseOptionalDelimitedEntries(typeof t === "string" ? t : void 0) : t;
continue;
}
if (t === null) {
i[e] = void 0;
continue;
}
const a = parseStrictNonNegativeInteger(t);
if (a === void 0) throw new Error(`${r.longFlag} must be a non-negative integer.`);
i[e] = a;
}
return i;
}
/** Add or configure a channel account, using the wizard when no concrete flags are supplied. */
async function channelsAddCommand(e, t = defaultRuntime, n) {
try {
parseAccountSelector(e.account);
const r = await requireValidConfigForWrite(t);
if (!r) return;
const i = r.snapshot.sourceConfig;
let a = i;
let o = false;
const effectContext = () => ({
runtime: t,
...n?.beforePersistentEffect ? { beforePersistentEffect: n.beforePersistentEffect } : {}
});
if (n?.hasFlags === false) {
const { resolveInitialWizardChannelTarget: n, runChannelsAddWizardFlow: a, selectChannelSetupOwner: o } = await import("./add-wizard-O93ZYtQ3.mjs");
const s = createClackPrompter();
if (!isTerminalInteractive()) {
t.error(channelOmitsEnvBackedSetupOption(e.channel) ? `Interactive channel setup requires a TTY. Run ${formatCliCommand(`openclaw channels add --channel ${e.channel?.trim() || "<id>"} --help`)} to list the setup flags this channel accepts, then pass them for non-interactive setup.` : "Interactive channel setup requires a TTY. Use `openclaw channels add --channel <id> --use-env` or pass the channel's credential flags for non-interactive setup.");
t.exit(1);
return;
}
const { agentId: c, workspaceDir: l } = await o(r, s, e.agent);
const u = await n(e.channel, i, l);
if (u.kind === "unresolved") {
t.error(u.message);
t.exit(1);
return;
}
await a({
writeSnapshot: r,
agentId: c,
prompter: s,
workspaceDir: l,
...u.kind === "resolved" ? { initialChannel: u.channel } : {},
...effectContext()
});
return;
}
const s = e.channel ?? "";
let c = normalizeAnyChannelId(s);
let l;
const resolveWorkspaceDir = () => l ??= resolveChannelSetupOwner(i, e.agent).workspaceDir;
const u = normalizeOptionalLowercaseString(s);
let d = u ? (await import("./trusted-catalog-Dmxetn03.mjs")).resolveTrustedChannelCatalogInput(u, {
cfg: a,
workspaceDir: resolveWorkspaceDir()
}) : void 0;
const loadScopedPlugin = async (e, n) => {
const r = getLoadedChannelPlugin(e);
if (r?.setupContract?.applyAccountConfig || r?.setup?.applyAccountConfig) return r;
const { loadChannelSetupPluginRegistrySnapshotForChannel: i } = await import("./plugin-install-DIAjesPC.mjs");
const o = await i({
cfg: a,
runtime: t,
channel: e,
...n ? { pluginId: n } : {},
workspaceDir: resolveWorkspaceDir(),
forceSetupOnlyChannelPlugins: true
});
return o.channelSetups.find((t) => t.plugin.id === e)?.plugin ?? getBundledChannelSetupPlugin(e) ?? o.channels.find((t) => t.plugin.id === e)?.plugin ?? r;
};
if (d) {
const e = resolveWorkspaceDir();
const { isCatalogChannelInstalled: t } = await import("./discovery-BIieIm-B.mjs");
const n = c ? getLoadedChannelPlugin(c) : void 0;
const r = c ? getBundledChannelSetupPlugin(c) : void 0;
if (!n && !r && !t({
cfg: a,
entry: d,
workspaceDir: e
})) {
const { ensureChannelSetupPluginInstalled: t } = await import("./plugin-install-DIAjesPC.mjs");
const n = createClackPrompter();
const r = await t({
cfg: a,
entry: d,
prompter: n,
workspaceDir: e,
promptInstall: false,
...effectContext()
});
a = r.cfg;
if (!r.installed) return;
o = true;
d = {
...d,
...r.pluginId ? { pluginId: r.pluginId } : {}
};
}
c ??= normalizeAnyChannelId(d.id) ?? d.id;
}
if (!c) {
const e = d ? `Plugin ${d.meta.label} could not be loaded after install. Run openclaw doctor --fix, then retry openclaw channels add.` : formatUnknownChannelMessage({ channel: s });
t.error(e);
t.exit(1);
return;
}
const f = c;
const unsupportedAddMessage = () => `${formatUnsupportedChannelActionMessage({
channel: f,
action: "non-interactive add"
})} Run ${formatCliCommand("openclaw channels add")} with no flags for guided setup.`;
return await withCommandPluginMetadata({
config: a,
workspaceDir: resolveWorkspaceDir()
}, async () => {
const s = await loadScopedPlugin(f, d?.pluginId);
if (!s) {
t.error(unsupportedAddMessage());
t.exit(1);
return;
}
const c = await prepareChannelAccountConfiguration({
cfg: a,
plugin: s,
requestedAccountId: e.account,
resolveInput: () => buildChannelSetupInput(e, s.setupContract ? "contract" : "legacy"),
...effectContext()
});
if (!c.ok) {
t.error(c.error.kind === "unsupported" ? unsupportedAddMessage() : c.error.message);
t.exit(1);
return;
}
const l = await applyPreparedChannelAccountConfiguration({
cfg: a,
channel: f,
prepared: c.value,
...effectContext()
});
a = normalizeExternalChannelSetupConfig({
cfg: l.nextConfig,
channel: f
});
const u = await persistChannelPluginConfig({
cfg: a,
setup: {
previousConfig: i,
beforePersistentEffect: n?.beforePersistentEffect,
assertCurrent: r.writeOptions.assertConfigPathForWrite
},
pluginInstalled: o,
writeOptions: r.writeOptions,
baseHash: r.snapshot.hash,
runtime: t
});
t.log(`Added ${s.meta.label ?? channelLabel(f)} account "${l.accountId}".`);
const p = l.afterAccountConfigWritten;
if (p) {
const { runCollectedChannelOnboardingPostWriteHooks: e } = await loadOnboardChannels();
await e({
hooks: [{
channel: f,
accountId: l.accountId,
run: async ({ cfg: e, runtime: t }) => await p({
previousCfg: i,
cfg: e,
accountId: l.accountId,
input: l.input,
runtime: t
})
}],
configPath: u.path,
...effectContext()
});
}
});
} catch (e) {
if (e instanceof WizardCancelledError) {
t.exit(1);
return;
}
throw e;
}
}
//#endregion
export { channelsAddCommand };