Skip to content

Commit f650244

Browse files
committed
refactor(twitch): consolidate inbound and outbound sending
1 parent 0449cd1 commit f650244

10 files changed

Lines changed: 289 additions & 629 deletions

File tree

‎extensions/twitch/src/client-manager-registry.test.ts‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getOrCreateClientManager,
77
removeClientManager,
88
} from "./client-manager-registry.js";
9-
import { sendMessageTwitchInternal } from "./send.js";
9+
import { twitchOutbound } from "./outbound.js";
1010
import { BASE_TWITCH_TEST_ACCOUNT, makeTwitchTestConfig } from "./test-fixtures.js";
1111
import type { ChannelLogSink, TwitchAccountConfig } from "./types.js";
1212

@@ -108,33 +108,30 @@ describe("client manager registry", () => {
108108
const removal = removeClientManager("default");
109109

110110
try {
111-
const whileRetiring = await sendMessageTwitchInternal(
112-
"#testchannel",
113-
"while retiring",
114-
config,
115-
"default",
116-
false,
111+
await expect(
112+
twitchOutbound.sendText!({
113+
to: "#testchannel",
114+
text: "while retiring",
115+
cfg: config,
116+
accountId: "default",
117+
}),
118+
).rejects.toThrow(
119+
"Client manager not found for account: default. Please start the Twitch gateway first.",
117120
);
118-
119-
expect(whileRetiring).toMatchObject({
120-
ok: false,
121-
error:
122-
"Client manager not found for account: default. Please start the Twitch gateway first.",
123-
});
124121
expect(reconnect).not.toHaveBeenCalled();
125122
expect(first.transport.say).not.toHaveBeenCalled();
126123

127124
const replacement = getOrCreateClientManager("default", makeLogger());
128125
const { transport } = attachFakeTransport(replacement);
129-
const afterRestart = await sendMessageTwitchInternal(
130-
"#testchannel",
131-
"after restart",
132-
config,
133-
"default",
134-
false,
135-
);
126+
const afterRestart = await twitchOutbound.sendText!({
127+
to: "#testchannel",
128+
text: "after restart",
129+
cfg: config,
130+
accountId: "default",
131+
});
136132

137-
expect(afterRestart.ok).toBe(true);
133+
expect(afterRestart.messageId).toEqual(expect.any(String));
134+
expect(afterRestart.receipt?.platformMessageIds).toEqual([afterRestart.messageId]);
138135
expect(transport.say).toHaveBeenCalledWith("testchannel", "after restart");
139136

140137
cleanup.resolve();

‎extensions/twitch/src/monitor.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ describe("monitorTwitchProvider", () => {
129129
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
130130
recordInboundSession: vi.fn(),
131131
},
132-
text: {
133-
resolveMarkdownTableMode: vi.fn(() => "off"),
134-
},
135132
},
136133
});
137134
});

‎extensions/twitch/src/monitor.ts‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
import type { ChannelAccountSnapshot } from "openclaw/plugin-sdk/channel-contract";
99
import { createChannelInboundEnvelopeBuilder } from "openclaw/plugin-sdk/channel-inbound";
10-
import type { MarkdownTableMode, OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
10+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
1111
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
1212
import { resolveOutboundMediaUrls } from "openclaw/plugin-sdk/reply-payload";
1313
import type { ReplyPayload } from "openclaw/plugin-sdk/reply-runtime";
1414
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
1515
import { checkTwitchAccessControl } from "./access-control.js";
1616
import { getOrCreateClientManager } from "./client-manager-registry.js";
1717
import { getTwitchRuntime } from "./runtime.js";
18+
import { sendMessageTwitchInternal } from "./send.js";
1819
import { createTwitchIngress } from "./twitch-ingress.js";
1920
import type { TwitchAccountConfig, TwitchChatMessage } from "./types.js";
20-
import { stripMarkdownForTwitch } from "./utils/markdown.js";
2121

2222
type TwitchRuntimeEnv = {
2323
log?: (message: string) => void;
@@ -145,11 +145,6 @@ async function processTwitchMessage(params: {
145145
commandBody: input.textForCommands,
146146
},
147147
});
148-
const tableMode = channelRuntime.text.resolveMarkdownTableMode({
149-
cfg,
150-
channel: "twitch",
151-
accountId,
152-
});
153148
return {
154149
cfg,
155150
channel: "twitch",
@@ -167,7 +162,6 @@ async function processTwitchMessage(params: {
167162
account,
168163
accountId,
169164
config,
170-
tableMode,
171165
runtime,
172166
});
173167
},
@@ -201,7 +195,6 @@ async function deliverTwitchReply(params: {
201195
account: TwitchAccountConfig;
202196
accountId: string;
203197
config: unknown;
204-
tableMode: MarkdownTableMode;
205198
runtime: TwitchRuntimeEnv;
206199
}): Promise<{ visibleReplySent: boolean }> {
207200
const { payload, channel, account, accountId, config, runtime } = params;
@@ -214,22 +207,17 @@ async function deliverTwitchReply(params: {
214207
debug: (msg) => runtime.log?.(msg),
215208
});
216209

217-
const textToSend = stripMarkdownForTwitch(
218-
[payload.text, ...resolveOutboundMediaUrls(payload)].filter(Boolean).join(" "),
219-
);
220-
if (!textToSend) {
221-
runtime.error?.(`No text to send in reply payload`);
222-
return { visibleReplySent: false };
223-
}
224-
const result = await clientManager.sendMessage(
225-
account,
210+
const result = await sendMessageTwitchInternal({
226211
channel,
227-
textToSend,
228-
config as Parameters<typeof clientManager.sendMessage>[3],
212+
text: [payload.text, ...resolveOutboundMediaUrls(payload)].filter(Boolean).join(" "),
213+
cfg: config as OpenClawConfig,
214+
account,
229215
accountId,
230-
);
231-
if (!result.ok) {
232-
throw new Error(result.error ?? "Send failed");
216+
clientManager,
217+
});
218+
if (result.outcome === "not_sent") {
219+
runtime.error?.(`No text to send in reply payload`);
220+
return { visibleReplySent: false };
233221
}
234222
return { visibleReplySent: true };
235223
} catch (err) {

‎extensions/twitch/src/outbound.test.ts‎

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ describe("outbound", () => {
8686

8787
function setupAccountContext(params?: {
8888
account?: typeof mockAccount | null;
89+
configured?: boolean;
8990
availableAccountIds?: string[];
9091
}) {
9192
const account = params?.account === undefined ? mockAccount : params.account;
9293
vi.mocked(resolveTwitchAccountContext).mockImplementation((_cfg, accountId) => ({
9394
accountId: accountId?.trim() || "default",
9495
account,
9596
tokenResolution: { source: "config", token: account?.accessToken ?? "" },
96-
configured: account !== null,
97+
configured: account ? (params?.configured ?? true) : false,
9798
availableAccountIds: params?.availableAccountIds ?? ["default"],
9899
}));
99100
}
@@ -148,7 +149,6 @@ describe("outbound", () => {
148149
setupAccountContext();
149150
const receipt = twitchTestReceipt("twitch-msg-123");
150151
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
151-
ok: true,
152152
messageId: "twitch-msg-123",
153153
receipt,
154154
});
@@ -184,14 +184,14 @@ describe("outbound", () => {
184184
timestamp: expect.any(Number),
185185
});
186186
expect(result?.receipt.parts.map((part) => part.kind)).toEqual(["text"]);
187-
expect(sendMessageTwitchInternal).toHaveBeenLastCalledWith(
188-
"testchannel",
189-
"image https://example.com/image.png",
190-
mockConfig,
191-
"default",
192-
true,
193-
console,
194-
);
187+
expect(sendMessageTwitchInternal).toHaveBeenLastCalledWith({
188+
channel: "testchannel",
189+
text: "image https://example.com/image.png",
190+
cfg: mockConfig,
191+
account: mockAccount,
192+
accountId: "default",
193+
clientManager: undefined,
194+
});
195195
},
196196
messageSendingHooks: () => {
197197
expect(twitchMessageAdapter.durableFinal?.capabilities?.messageSendingHooks).toBe(true);
@@ -343,7 +343,6 @@ describe("outbound", () => {
343343
const { sendMessageTwitchInternal } = await import("./send.js");
344344
setupAccountContext();
345345
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
346-
ok: true,
347346
outcome: "not_sent",
348347
messageId: "",
349348
receipt: createMessageReceiptFromOutboundResults({ results: [] }),
@@ -368,7 +367,6 @@ describe("outbound", () => {
368367

369368
setupAccountContext();
370369
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
371-
ok: true,
372370
messageId: "twitch-msg-123",
373371
receipt: twitchTestReceipt("twitch-msg-123"),
374372
});
@@ -383,14 +381,14 @@ describe("outbound", () => {
383381
expect(result.channel).toBe("twitch");
384382
expect(result.messageId).toBe("twitch-msg-123");
385383
expect(result.receipt?.platformMessageIds).toEqual(["twitch-msg-123"]);
386-
expect(sendMessageTwitchInternal).toHaveBeenCalledWith(
387-
"testchannel",
388-
"Hello Twitch!",
389-
mockConfig,
390-
"default",
391-
true,
392-
console,
393-
);
384+
expect(sendMessageTwitchInternal).toHaveBeenCalledWith({
385+
channel: "testchannel",
386+
text: "Hello Twitch!",
387+
cfg: mockConfig,
388+
account: mockAccount,
389+
accountId: "default",
390+
clientManager: undefined,
391+
});
394392
expect(result.timestamp).toBeGreaterThan(0);
395393
});
396394

@@ -421,12 +419,28 @@ describe("outbound", () => {
421419
).rejects.toThrow("No channel specified");
422420
});
423421

422+
it("rejects an unconfigured account before attempting delivery", async () => {
423+
const { sendMessageTwitchInternal } = await import("./send.js");
424+
setupAccountContext({ configured: false });
425+
426+
await expect(
427+
twitchOutbound.sendText!({
428+
cfg: mockConfig,
429+
to: "#testchannel",
430+
text: "Hello!",
431+
accountId: "default",
432+
}),
433+
).rejects.toThrow(
434+
"Account default is not properly configured. Required: username, clientId, and token (config or env for default account).",
435+
);
436+
expect(sendMessageTwitchInternal).not.toHaveBeenCalled();
437+
});
438+
424439
it("should use account channel when target not provided", async () => {
425440
const { sendMessageTwitchInternal } = await import("./send.js");
426441

427442
setupAccountContext();
428443
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
429-
ok: true,
430444
messageId: "msg-456",
431445
receipt: twitchTestReceipt("msg-456"),
432446
});
@@ -438,14 +452,14 @@ describe("outbound", () => {
438452
accountId: "default",
439453
});
440454

441-
expect(sendMessageTwitchInternal).toHaveBeenCalledWith(
442-
"testchannel",
443-
"Hello!",
444-
mockConfig,
445-
"default",
446-
true,
447-
console,
448-
);
455+
expect(sendMessageTwitchInternal).toHaveBeenCalledWith({
456+
channel: "testchannel",
457+
text: "Hello!",
458+
cfg: mockConfig,
459+
account: mockAccount,
460+
accountId: "default",
461+
clientManager: undefined,
462+
});
449463
});
450464

451465
it("uses configured defaultAccount when accountId is omitted", async () => {
@@ -473,7 +487,6 @@ describe("outbound", () => {
473487
availableAccountIds: ["default", "secondary"],
474488
}));
475489
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
476-
ok: true,
477490
messageId: "msg-secondary",
478491
receipt: twitchTestReceipt("msg-secondary"),
479492
});
@@ -492,26 +505,21 @@ describe("outbound", () => {
492505
text: "Hello!",
493506
});
494507

495-
expect(sendMessageTwitchInternal).toHaveBeenCalledWith(
496-
"secondary-channel",
497-
"Hello!",
498-
defaultAccountConfig,
499-
"secondary",
500-
true,
501-
console,
502-
);
508+
expect(sendMessageTwitchInternal).toHaveBeenCalledWith({
509+
channel: "secondary-channel",
510+
text: "Hello!",
511+
cfg: defaultAccountConfig,
512+
account: { ...mockAccount, channel: "secondary-channel" },
513+
accountId: "secondary",
514+
clientManager: undefined,
515+
});
503516
});
504517

505518
it("should throw on send failure", async () => {
506519
const { sendMessageTwitchInternal } = await import("./send.js");
507520

508521
setupAccountContext();
509-
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
510-
ok: false,
511-
messageId: "failed-msg",
512-
receipt: createMessageReceiptFromOutboundResults({ results: [] }),
513-
error: "Connection lost",
514-
});
522+
vi.mocked(sendMessageTwitchInternal).mockRejectedValue(new Error("Connection lost"));
515523

516524
await expect(
517525
twitchOutbound.sendText!({
@@ -530,7 +538,6 @@ describe("outbound", () => {
530538

531539
setupAccountContext();
532540
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
533-
ok: true,
534541
messageId: "media-msg-123",
535542
receipt: twitchTestReceipt("media-msg-123"),
536543
});
@@ -546,22 +553,21 @@ describe("outbound", () => {
546553
expect(result.channel).toBe("twitch");
547554
expect(result.messageId).toBe("media-msg-123");
548555
expect(result.receipt?.platformMessageIds).toEqual(["media-msg-123"]);
549-
expect(sendMessageTwitchInternal).toHaveBeenCalledWith(
550-
"testchannel",
551-
"Check this: https://example.com/image.png",
552-
mockConfig,
553-
"default",
554-
true,
555-
console,
556-
);
556+
expect(sendMessageTwitchInternal).toHaveBeenCalledWith({
557+
channel: "testchannel",
558+
text: "Check this: https://example.com/image.png",
559+
cfg: mockConfig,
560+
account: mockAccount,
561+
accountId: "default",
562+
clientManager: undefined,
563+
});
557564
});
558565

559566
it("should send media URL only when no text", async () => {
560567
const { sendMessageTwitchInternal } = await import("./send.js");
561568

562569
setupAccountContext();
563570
vi.mocked(sendMessageTwitchInternal).mockResolvedValue({
564-
ok: true,
565571
messageId: "media-only-msg",
566572
receipt: twitchTestReceipt("media-only-msg"),
567573
});
@@ -574,14 +580,14 @@ describe("outbound", () => {
574580
accountId: "default",
575581
});
576582

577-
expect(sendMessageTwitchInternal).toHaveBeenCalledWith(
578-
"testchannel",
579-
"https://example.com/image.png",
580-
mockConfig,
581-
"default",
582-
true,
583-
console,
584-
);
583+
expect(sendMessageTwitchInternal).toHaveBeenCalledWith({
584+
channel: "testchannel",
585+
text: "https://example.com/image.png",
586+
cfg: mockConfig,
587+
account: mockAccount,
588+
accountId: "default",
589+
clientManager: undefined,
590+
});
585591
});
586592
});
587593
});

0 commit comments

Comments
 (0)