UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

136 lines (135 loc) • 6.22 kB
import { l as normalizeOptionalString, m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs"; import { n as resolveFilesystemPath, t as expandHomePrefix } from "./home-dir-EKmgmdPG.mjs"; import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { o as writeRuntimeJson, r as defaultRuntime } from "./runtime-B-SLYP-m.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { n as normalizeAgentId, r as normalizeAgentIdStrict } from "./agent-id-CQjtp2ft.mjs"; import "./session-key-CqAyZeVk.mjs"; import { i as listAgentIds, n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs"; import { _ as resolveDefaultAgentId, f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs"; import { t as applyImplicitAgentRosterDefaults } from "./implicit-agent-roster-DtoGie9T.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { t as quoteCliArg } from "./quote-cli-arg-J7A9UE6m.mjs"; import { r as replaceConfigFile } from "./mutate-DhAOi0-2.mjs"; import "./config-CMA-J084.mjs"; import { r as DEFAULT_IDENTITY_FILENAME } from "./workspace-bootstrap-policy-D7EJ00Ub.mjs"; import { a as loadAgentIdentityFromWorkspaceAsync, r as loadAgentIdentityFromFile } from "./identity-file-C4Fvs-OY.mjs"; import { t as applyAgentConfig } from "./agents.config-T4w0h7rr.mjs"; import "./workspace-DLlXz3VE.mjs"; import { d as throwExpectedCliError } from "./failure-output-DVVDqwuu.mjs"; import { r as logConfigUpdated } from "./logging-D0RirHBi.mjs"; import { r as requireValidConfigForWrite } from "./config-validation-Cy6dkTWs.mjs"; import path from "node:path"; //#region src/commands/agents.commands.identity.ts const normalizeWorkspacePath = (e) => resolveFilesystemPath(expandHomePrefix(e)); async function agentsSetIdentityCommand(e) { const t = await requireValidConfigForWrite(defaultRuntime); if (!t) return; const n = applyImplicitAgentRosterDefaults(t.snapshot.sourceConfig); const r = normalizeOptionalString(e.name); const i = normalizeOptionalString(e.emoji); const a = normalizeOptionalString(e.theme); const o = normalizeOptionalString(e.avatar); const s = Boolean(r || i || a || o); const c = readNonBlankString(e.identityFile); const l = readNonBlankString(e.workspace); if (e.identityFile !== void 0 && !c) throwExpectedCliError("--identity-file must not be blank"); if (e.workspace !== void 0 && !l) throwExpectedCliError("--workspace must not be blank"); const u = Boolean(e.fromIdentity || c || !s); const d = e.agent === void 0 ? null : normalizeAgentIdStrict(e.agent); if (d && !d.ok) throwExpectedCliError(`Agent "${e.agent}" not found. Create it with \`openclaw agents add\`.`); let f = d?.value; let p; let m; let h; if (c) { p = normalizeWorkspacePath(c); m = path.dirname(p); } else if (l) { h = normalizeWorkspacePath(l); m = h; } else if (f && u) m = resolveAgentWorkspaceDir(n, f); else if (u || !f) m = path.resolve(process.cwd()); if (!f) { const e = expectDefined(m, "agent workspace"); const t = listAgentEntries(n); const r = t.length > 0 ? t.map((e) => normalizeAgentId(e.id)) : [resolveDefaultAgentId(n)]; const i = normalizeWorkspacePath(e); const a = r.filter((e) => normalizeWorkspacePath(resolveAgentWorkspaceDir(n, e)) === i); if (a.length === 0) throwExpectedCliError(`No agent workspace matches ${shortenHomePath(e)}. Pass --agent to target a specific agent.`); if (a.length > 1) throwExpectedCliError(`Multiple agents match ${shortenHomePath(e)}: ${a.join(", ")}. Pass --agent to choose one.`); f = a[0]; } const g = expectDefined(f, "agent id"); if (!listAgentIds(n).map((e) => normalizeAgentId(e)).includes(g)) throwExpectedCliError(`Agent "${g}" not found. Create it with \`openclaw agents add\`.`); let _ = null; if (u) { if (p) try { _ = await loadAgentIdentityFromFile(p); } catch (e) { throwExpectedCliError(formatErrorMessage(e)); } else if (m) _ = await loadAgentIdentityFromWorkspaceAsync(m); if (!_) { const e = p ?? (m ? path.join(m, "IDENTITY.md") : "IDENTITY.md"); throwExpectedCliError(`No identity data found in ${shortenHomePath(e)}.`); } } const v = _?.theme ?? _?.creature ?? _?.vibe; const y = { ...r || _?.name ? { name: r ?? _?.name } : {}, ...i || _?.emoji ? { emoji: i ?? _?.emoji } : {}, ...a || v ? { theme: a ?? v } : {}, ...o || _?.avatar ? { avatar: o ?? _?.avatar } : {} }; const b = applyAgentConfig(n, { agentId: g, identity: y }); const x = await replaceConfigFile({ ...t, sourceConfig: b, writeOptions: { ...t.writeOptions, allowConfigSizeDrop: true } }); const S = expectDefined(listAgentEntries(x.nextConfig).find((e) => normalizeAgentId(e.id) === g), "committed agent config"); const C = expectDefined(S.identity, "committed agent identity"); const w = resolveAgentWorkspaceDir(x.nextConfig, g); const T = _ ? m : void 0; const E = h !== void 0 && normalizeWorkspacePath(h) !== normalizeWorkspacePath(w); const D = T !== void 0 && normalizeWorkspacePath(T) !== normalizeWorkspacePath(w); if (e.json) { writeRuntimeJson(defaultRuntime, { agentId: g, identity: C, workspace: m ?? null, storedWorkspace: w, identityFile: p ?? null }); return; } logConfigUpdated(defaultRuntime); defaultRuntime.log(`Agent: ${sanitizeTerminalText(g)}`); for (const [e, t] of [ ["name", "Name"], ["theme", "Theme"], ["emoji", "Emoji"], ["avatar", "Avatar"] ]) { const n = C[e]; if (n) defaultRuntime.log(`${t}: ${sanitizeTerminalText(n)}`); } defaultRuntime.log(`Workspace: ${sanitizeTerminalText(shortenHomePath(w))}`); if (E && h) { defaultRuntime.log(`Workspace locator: ${sanitizeTerminalText(shortenHomePath(h))}`); defaultRuntime.log(`Stored workspace unchanged. Relocate with ${formatCliCommand(`openclaw config set agents.entries.${g}.workspace ${quoteCliArg(h)}`)}.`); } else if (D && T) defaultRuntime.log(`Identity source: ${sanitizeTerminalText(shortenHomePath(T))}`); } //#endregion export { agentsSetIdentityCommand };