fix: honor twitch default send account

This commit is contained in:
Tak Hoffman
2026-04-03 15:06:52 -05:00
parent 4c0f51df81
commit 78022740fc
2 changed files with 60 additions and 8 deletions

View File

@@ -258,5 +258,52 @@ describe("send", () => {
"default",
);
});
it("uses the configured default account when accountId is omitted", async () => {
const secondaryAccount = {
...mockAccount,
username: "secondary-user",
channel: "secondary-channel",
};
vi.mocked(resolveTwitchAccountContext).mockImplementation((_cfg, accountId) => ({
accountId: accountId?.trim() || "secondary",
account: secondaryAccount,
tokenResolution: { source: "config", token: secondaryAccount.accessToken ?? "" },
configured: true,
availableAccountIds: ["default", "secondary"],
}));
const mockSend = vi.fn().mockResolvedValue({
ok: true,
messageId: "twitch-msg-secondary",
});
vi.mocked(getClientManager).mockReturnValue({
sendMessage: mockSend,
} as unknown as ReturnType<typeof getClientManager>);
const result = await sendMessageTwitchInternal(
"",
"Hello!",
{
channels: {
twitch: {
defaultAccount: "secondary",
},
},
} as never,
undefined,
false,
mockLogger as unknown as Console,
);
expect(result.ok).toBe(true);
expect(getClientManager).toHaveBeenCalledWith("secondary");
expect(mockSend).toHaveBeenCalledWith(
secondaryAccount,
"secondary-channel",
"Hello!",
expect.any(Object),
"secondary",
);
});
});
});

View File

@@ -7,7 +7,7 @@
import type { OpenClawConfig } from "../runtime-api.js";
import { getClientManager as getRegistryClientManager } from "./client-manager-registry.js";
import { DEFAULT_ACCOUNT_ID, resolveTwitchAccountContext } from "./config.js";
import { resolveTwitchAccountContext } from "./config.js";
import { stripMarkdownForTwitch } from "./utils/markdown.js";
import { generateMessageId, normalizeTwitchChannel } from "./utils/twitch.js";
@@ -51,16 +51,21 @@ export async function sendMessageTwitchInternal(
channel: string,
text: string,
cfg: OpenClawConfig,
accountId: string = DEFAULT_ACCOUNT_ID,
accountId?: string,
stripMarkdown: boolean = true,
logger: Console = console,
): Promise<SendMessageResult> {
const { account, configured, availableAccountIds } = resolveTwitchAccountContext(cfg, accountId);
const {
account,
configured,
availableAccountIds,
accountId: resolvedAccountId,
} = resolveTwitchAccountContext(cfg, accountId);
if (!account) {
return {
ok: false,
messageId: generateMessageId(),
error: `Account not found: ${accountId}. Available accounts: ${availableAccountIds.join(", ") || "none"}`,
error: `Account not found: ${accountId ?? "(default)"}. Available accounts: ${availableAccountIds.join(", ") || "none"}`,
};
}
@@ -69,7 +74,7 @@ export async function sendMessageTwitchInternal(
ok: false,
messageId: generateMessageId(),
error:
`Account ${accountId} is not properly configured. ` +
`Account ${resolvedAccountId} is not properly configured. ` +
"Required: username, clientId, and token (config or env for default account).",
};
}
@@ -91,12 +96,12 @@ export async function sendMessageTwitchInternal(
};
}
const clientManager = getRegistryClientManager(accountId);
const clientManager = getRegistryClientManager(resolvedAccountId);
if (!clientManager) {
return {
ok: false,
messageId: generateMessageId(),
error: `Client manager not found for account: ${accountId}. Please start the Twitch gateway first.`,
error: `Client manager not found for account: ${resolvedAccountId}. Please start the Twitch gateway first.`,
};
}
@@ -106,7 +111,7 @@ export async function sendMessageTwitchInternal(
normalizeTwitchChannel(normalizedChannel),
cleanedText,
cfg,
accountId,
resolvedAccountId,
);
if (!result.ok) {