mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 09:07:39 +00:00
fix(channels): add optional defaultAccount routing
This commit is contained in:
@@ -5,14 +5,25 @@ import { createAccountListHelpers } from "./account-helpers.js";
|
||||
const { listConfiguredAccountIds, listAccountIds, resolveDefaultAccountId } =
|
||||
createAccountListHelpers("testchannel");
|
||||
|
||||
function cfg(accounts?: Record<string, unknown> | null): OpenClawConfig {
|
||||
function cfg(accounts?: Record<string, unknown> | null, defaultAccount?: string): OpenClawConfig {
|
||||
if (accounts === null) {
|
||||
return { channels: { testchannel: {} } } as unknown as OpenClawConfig;
|
||||
return {
|
||||
channels: {
|
||||
testchannel: defaultAccount ? { defaultAccount } : {},
|
||||
},
|
||||
} as unknown as OpenClawConfig;
|
||||
}
|
||||
if (accounts === undefined) {
|
||||
if (accounts === undefined && !defaultAccount) {
|
||||
return {} as unknown as OpenClawConfig;
|
||||
}
|
||||
return { channels: { testchannel: { accounts } } } as unknown as OpenClawConfig;
|
||||
return {
|
||||
channels: {
|
||||
testchannel: {
|
||||
...(accounts === undefined ? {} : { accounts }),
|
||||
...(defaultAccount ? { defaultAccount } : {}),
|
||||
},
|
||||
},
|
||||
} as unknown as OpenClawConfig;
|
||||
}
|
||||
|
||||
describe("createAccountListHelpers", () => {
|
||||
@@ -56,6 +67,18 @@ describe("createAccountListHelpers", () => {
|
||||
});
|
||||
|
||||
describe("resolveDefaultAccountId", () => {
|
||||
it("prefers configured defaultAccount when it matches a configured account id", () => {
|
||||
expect(resolveDefaultAccountId(cfg({ alpha: {}, beta: {} }, "beta"))).toBe("beta");
|
||||
});
|
||||
|
||||
it("normalizes configured defaultAccount before matching", () => {
|
||||
expect(resolveDefaultAccountId(cfg({ "router-d": {} }, "Router D"))).toBe("router-d");
|
||||
});
|
||||
|
||||
it("falls back when configured defaultAccount is missing", () => {
|
||||
expect(resolveDefaultAccountId(cfg({ beta: {}, alpha: {} }, "missing"))).toBe("alpha");
|
||||
});
|
||||
|
||||
it('returns "default" when present', () => {
|
||||
expect(resolveDefaultAccountId(cfg({ default: {}, other: {} }))).toBe("default");
|
||||
});
|
||||
|
||||
@@ -1,7 +1,26 @@
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { DEFAULT_ACCOUNT_ID } from "../../routing/session-key.js";
|
||||
import {
|
||||
DEFAULT_ACCOUNT_ID,
|
||||
normalizeAccountId,
|
||||
normalizeOptionalAccountId,
|
||||
} from "../../routing/session-key.js";
|
||||
|
||||
export function createAccountListHelpers(channelKey: string) {
|
||||
function resolveConfiguredDefaultAccountId(cfg: OpenClawConfig): string | undefined {
|
||||
const channel = cfg.channels?.[channelKey] as Record<string, unknown> | undefined;
|
||||
const preferred = normalizeOptionalAccountId(
|
||||
typeof channel?.defaultAccount === "string" ? channel.defaultAccount : undefined,
|
||||
);
|
||||
if (!preferred) {
|
||||
return undefined;
|
||||
}
|
||||
const ids = listAccountIds(cfg);
|
||||
if (ids.some((id) => normalizeAccountId(id) === preferred)) {
|
||||
return preferred;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function listConfiguredAccountIds(cfg: OpenClawConfig): string[] {
|
||||
const channel = cfg.channels?.[channelKey];
|
||||
const accounts = (channel as Record<string, unknown> | undefined)?.accounts;
|
||||
@@ -20,6 +39,10 @@ export function createAccountListHelpers(channelKey: string) {
|
||||
}
|
||||
|
||||
function resolveDefaultAccountId(cfg: OpenClawConfig): string {
|
||||
const preferred = resolveConfiguredDefaultAccountId(cfg);
|
||||
if (preferred) {
|
||||
return preferred;
|
||||
}
|
||||
const ids = listAccountIds(cfg);
|
||||
if (ids.includes(DEFAULT_ACCOUNT_ID)) {
|
||||
return DEFAULT_ACCOUNT_ID;
|
||||
|
||||
Reference in New Issue
Block a user