@openclaw/twitch 2026.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/plugin.ts ADDED
@@ -0,0 +1,274 @@
1
+ /**
2
+ * Twitch channel plugin for OpenClaw.
3
+ *
4
+ * Main plugin export combining all adapters (outbound, actions, status, gateway).
5
+ * This is the primary entry point for the Twitch channel integration.
6
+ */
7
+
8
+ import type { OpenClawConfig } from "openclaw/plugin-sdk";
9
+ import { buildChannelConfigSchema } from "openclaw/plugin-sdk";
10
+ import { twitchMessageActions } from "./actions.js";
11
+ import { removeClientManager } from "./client-manager-registry.js";
12
+ import { TwitchConfigSchema } from "./config-schema.js";
13
+ import { DEFAULT_ACCOUNT_ID, getAccountConfig, listAccountIds } from "./config.js";
14
+ import { twitchOnboardingAdapter } from "./onboarding.js";
15
+ import { twitchOutbound } from "./outbound.js";
16
+ import { probeTwitch } from "./probe.js";
17
+ import { resolveTwitchTargets } from "./resolver.js";
18
+ import { collectTwitchStatusIssues } from "./status.js";
19
+ import { resolveTwitchToken } from "./token.js";
20
+ import type {
21
+ ChannelAccountSnapshot,
22
+ ChannelCapabilities,
23
+ ChannelLogSink,
24
+ ChannelMeta,
25
+ ChannelPlugin,
26
+ ChannelResolveKind,
27
+ ChannelResolveResult,
28
+ TwitchAccountConfig,
29
+ } from "./types.js";
30
+ import { isAccountConfigured } from "./utils/twitch.js";
31
+
32
+ /**
33
+ * Twitch channel plugin.
34
+ *
35
+ * Implements the ChannelPlugin interface to provide Twitch chat integration
36
+ * for OpenClaw. Supports message sending, receiving, access control, and
37
+ * status monitoring.
38
+ */
39
+ export const twitchPlugin: ChannelPlugin<TwitchAccountConfig> = {
40
+ /** Plugin identifier */
41
+ id: "twitch",
42
+
43
+ /** Plugin metadata */
44
+ meta: {
45
+ id: "twitch",
46
+ label: "Twitch",
47
+ selectionLabel: "Twitch (Chat)",
48
+ docsPath: "/channels/twitch",
49
+ blurb: "Twitch chat integration",
50
+ aliases: ["twitch-chat"],
51
+ } satisfies ChannelMeta,
52
+
53
+ /** Onboarding adapter */
54
+ onboarding: twitchOnboardingAdapter,
55
+
56
+ /** Pairing configuration */
57
+ pairing: {
58
+ idLabel: "twitchUserId",
59
+ normalizeAllowEntry: (entry) => entry.replace(/^(twitch:)?user:?/i, ""),
60
+ notifyApproval: async ({ id }) => {
61
+ // Note: Twitch doesn't support DMs from bots, so pairing approval is limited
62
+ // We'll log the approval instead
63
+ console.warn(`Pairing approved for user ${id} (notification sent via chat if possible)`);
64
+ },
65
+ },
66
+
67
+ /** Supported chat capabilities */
68
+ capabilities: {
69
+ chatTypes: ["group"],
70
+ } satisfies ChannelCapabilities,
71
+
72
+ /** Configuration schema for Twitch channel */
73
+ configSchema: buildChannelConfigSchema(TwitchConfigSchema),
74
+
75
+ /** Account configuration management */
76
+ config: {
77
+ /** List all configured account IDs */
78
+ listAccountIds: (cfg: OpenClawConfig): string[] => listAccountIds(cfg),
79
+
80
+ /** Resolve an account config by ID */
81
+ resolveAccount: (cfg: OpenClawConfig, accountId?: string | null): TwitchAccountConfig => {
82
+ const account = getAccountConfig(cfg, accountId ?? DEFAULT_ACCOUNT_ID);
83
+ if (!account) {
84
+ // Return a default/empty account if not configured
85
+ return {
86
+ username: "",
87
+ accessToken: "",
88
+ clientId: "",
89
+ enabled: false,
90
+ } as TwitchAccountConfig;
91
+ }
92
+ return account;
93
+ },
94
+
95
+ /** Get the default account ID */
96
+ defaultAccountId: (): string => DEFAULT_ACCOUNT_ID,
97
+
98
+ /** Check if an account is configured */
99
+ isConfigured: (_account: unknown, cfg: OpenClawConfig): boolean => {
100
+ const account = getAccountConfig(cfg, DEFAULT_ACCOUNT_ID);
101
+ const tokenResolution = resolveTwitchToken(cfg, { accountId: DEFAULT_ACCOUNT_ID });
102
+ return account ? isAccountConfigured(account, tokenResolution.token) : false;
103
+ },
104
+
105
+ /** Check if an account is enabled */
106
+ isEnabled: (account: TwitchAccountConfig | undefined): boolean => account?.enabled !== false,
107
+
108
+ /** Describe account status */
109
+ describeAccount: (account: TwitchAccountConfig | undefined) => {
110
+ return {
111
+ accountId: DEFAULT_ACCOUNT_ID,
112
+ enabled: account?.enabled !== false,
113
+ configured: account ? isAccountConfigured(account, account?.accessToken) : false,
114
+ };
115
+ },
116
+ },
117
+
118
+ /** Outbound message adapter */
119
+ outbound: twitchOutbound,
120
+
121
+ /** Message actions adapter */
122
+ actions: twitchMessageActions,
123
+
124
+ /** Resolver adapter for username -> user ID resolution */
125
+ resolver: {
126
+ resolveTargets: async ({
127
+ cfg,
128
+ accountId,
129
+ inputs,
130
+ kind,
131
+ runtime,
132
+ }: {
133
+ cfg: OpenClawConfig;
134
+ accountId?: string | null;
135
+ inputs: string[];
136
+ kind: ChannelResolveKind;
137
+ runtime: import("../../../src/runtime.js").RuntimeEnv;
138
+ }): Promise<ChannelResolveResult[]> => {
139
+ const account = getAccountConfig(cfg, accountId ?? DEFAULT_ACCOUNT_ID);
140
+
141
+ if (!account) {
142
+ return inputs.map((input) => ({
143
+ input,
144
+ resolved: false,
145
+ note: "account not configured",
146
+ }));
147
+ }
148
+
149
+ // Adapt RuntimeEnv.log to ChannelLogSink
150
+ const log: ChannelLogSink = {
151
+ info: (msg) => runtime.log(msg),
152
+ warn: (msg) => runtime.log(msg),
153
+ error: (msg) => runtime.error(msg),
154
+ debug: (msg) => runtime.log(msg),
155
+ };
156
+ return await resolveTwitchTargets(inputs, account, kind, log);
157
+ },
158
+ },
159
+
160
+ /** Status monitoring adapter */
161
+ status: {
162
+ /** Default runtime state */
163
+ defaultRuntime: {
164
+ accountId: DEFAULT_ACCOUNT_ID,
165
+ running: false,
166
+ lastStartAt: null,
167
+ lastStopAt: null,
168
+ lastError: null,
169
+ },
170
+
171
+ /** Build channel summary from snapshot */
172
+ buildChannelSummary: ({ snapshot }: { snapshot: ChannelAccountSnapshot }) => ({
173
+ configured: snapshot.configured ?? false,
174
+ running: snapshot.running ?? false,
175
+ lastStartAt: snapshot.lastStartAt ?? null,
176
+ lastStopAt: snapshot.lastStopAt ?? null,
177
+ lastError: snapshot.lastError ?? null,
178
+ probe: snapshot.probe,
179
+ lastProbeAt: snapshot.lastProbeAt ?? null,
180
+ }),
181
+
182
+ /** Probe account connection */
183
+ probeAccount: async ({
184
+ account,
185
+ timeoutMs,
186
+ }: {
187
+ account: TwitchAccountConfig;
188
+ timeoutMs: number;
189
+ }): Promise<unknown> => {
190
+ return await probeTwitch(account, timeoutMs);
191
+ },
192
+
193
+ /** Build account snapshot with current status */
194
+ buildAccountSnapshot: ({
195
+ account,
196
+ cfg,
197
+ runtime,
198
+ probe,
199
+ }: {
200
+ account: TwitchAccountConfig;
201
+ cfg: OpenClawConfig;
202
+ runtime?: ChannelAccountSnapshot;
203
+ probe?: unknown;
204
+ }): ChannelAccountSnapshot => {
205
+ const twitch = (cfg as Record<string, unknown>).channels as
206
+ | Record<string, unknown>
207
+ | undefined;
208
+ const twitchCfg = twitch?.twitch as Record<string, unknown> | undefined;
209
+ const accountMap = (twitchCfg?.accounts as Record<string, unknown> | undefined) ?? {};
210
+ const resolvedAccountId =
211
+ Object.entries(accountMap).find(([, value]) => value === account)?.[0] ??
212
+ DEFAULT_ACCOUNT_ID;
213
+ const tokenResolution = resolveTwitchToken(cfg, { accountId: resolvedAccountId });
214
+ return {
215
+ accountId: resolvedAccountId,
216
+ enabled: account?.enabled !== false,
217
+ configured: isAccountConfigured(account, tokenResolution.token),
218
+ running: runtime?.running ?? false,
219
+ lastStartAt: runtime?.lastStartAt ?? null,
220
+ lastStopAt: runtime?.lastStopAt ?? null,
221
+ lastError: runtime?.lastError ?? null,
222
+ probe,
223
+ };
224
+ },
225
+
226
+ /** Collect status issues for all accounts */
227
+ collectStatusIssues: collectTwitchStatusIssues,
228
+ },
229
+
230
+ /** Gateway adapter for connection lifecycle */
231
+ gateway: {
232
+ /** Start an account connection */
233
+ startAccount: async (ctx): Promise<void> => {
234
+ const account = ctx.account;
235
+ const accountId = ctx.accountId;
236
+
237
+ ctx.setStatus?.({
238
+ accountId,
239
+ running: true,
240
+ lastStartAt: Date.now(),
241
+ lastError: null,
242
+ });
243
+
244
+ ctx.log?.info(`Starting Twitch connection for ${account.username}`);
245
+
246
+ // Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
247
+ const { monitorTwitchProvider } = await import("./monitor.js");
248
+ await monitorTwitchProvider({
249
+ account,
250
+ accountId,
251
+ config: ctx.cfg,
252
+ runtime: ctx.runtime,
253
+ abortSignal: ctx.abortSignal,
254
+ });
255
+ },
256
+
257
+ /** Stop an account connection */
258
+ stopAccount: async (ctx): Promise<void> => {
259
+ const account = ctx.account;
260
+ const accountId = ctx.accountId;
261
+
262
+ // Disconnect and remove client manager from registry
263
+ await removeClientManager(accountId);
264
+
265
+ ctx.setStatus?.({
266
+ accountId,
267
+ running: false,
268
+ lastStopAt: Date.now(),
269
+ });
270
+
271
+ ctx.log?.info(`Stopped Twitch connection for ${account.username}`);
272
+ },
273
+ },
274
+ };
@@ -0,0 +1,196 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { probeTwitch } from "./probe.js";
3
+ import type { TwitchAccountConfig } from "./types.js";
4
+
5
+ // Mock Twurple modules - Vitest v4 compatible mocking
6
+ const mockUnbind = vi.fn();
7
+
8
+ // Event handler storage
9
+ let connectHandler: (() => void) | null = null;
10
+ let disconnectHandler: ((manually: boolean, reason?: Error) => void) | null = null;
11
+
12
+ // Event listener mocks that store handlers and return unbind function
13
+ const mockOnConnect = vi.fn((handler: () => void) => {
14
+ connectHandler = handler;
15
+ return { unbind: mockUnbind };
16
+ });
17
+
18
+ const mockOnDisconnect = vi.fn((handler: (manually: boolean, reason?: Error) => void) => {
19
+ disconnectHandler = handler;
20
+ return { unbind: mockUnbind };
21
+ });
22
+
23
+ const mockOnAuthenticationFailure = vi.fn((_handler: () => void) => {
24
+ return { unbind: mockUnbind };
25
+ });
26
+
27
+ // Connect mock that triggers the registered handler
28
+ const defaultConnectImpl = async () => {
29
+ // Simulate successful connection by calling the handler immediately.
30
+ if (connectHandler) {
31
+ connectHandler();
32
+ }
33
+ };
34
+
35
+ const mockConnect = vi.fn().mockImplementation(defaultConnectImpl);
36
+
37
+ const mockQuit = vi.fn().mockResolvedValue(undefined);
38
+
39
+ vi.mock("@twurple/chat", () => ({
40
+ ChatClient: class {
41
+ connect = mockConnect;
42
+ quit = mockQuit;
43
+ onConnect = mockOnConnect;
44
+ onDisconnect = mockOnDisconnect;
45
+ onAuthenticationFailure = mockOnAuthenticationFailure;
46
+ },
47
+ }));
48
+
49
+ vi.mock("@twurple/auth", () => ({
50
+ StaticAuthProvider: class {},
51
+ }));
52
+
53
+ describe("probeTwitch", () => {
54
+ const mockAccount: TwitchAccountConfig = {
55
+ username: "testbot",
56
+ accessToken: "oauth:test123456789",
57
+ clientId: "test-client-id",
58
+ channel: "testchannel",
59
+ };
60
+
61
+ beforeEach(() => {
62
+ vi.clearAllMocks();
63
+ // Reset handlers
64
+ connectHandler = null;
65
+ disconnectHandler = null;
66
+ });
67
+
68
+ it("returns error when username is missing", async () => {
69
+ const account = { ...mockAccount, username: "" };
70
+ const result = await probeTwitch(account, 5000);
71
+
72
+ expect(result.ok).toBe(false);
73
+ expect(result.error).toContain("missing credentials");
74
+ });
75
+
76
+ it("returns error when token is missing", async () => {
77
+ const account = { ...mockAccount, accessToken: "" };
78
+ const result = await probeTwitch(account, 5000);
79
+
80
+ expect(result.ok).toBe(false);
81
+ expect(result.error).toContain("missing credentials");
82
+ });
83
+
84
+ it("attempts connection regardless of token prefix", async () => {
85
+ // Note: probeTwitch doesn't validate token format - it tries to connect with whatever token is provided
86
+ // The actual connection would fail in production with an invalid token
87
+ const account = { ...mockAccount, accessToken: "raw_token_no_prefix" };
88
+ const result = await probeTwitch(account, 5000);
89
+
90
+ // With mock, connection succeeds even without oauth: prefix
91
+ expect(result.ok).toBe(true);
92
+ });
93
+
94
+ it("successfully connects with valid credentials", async () => {
95
+ const result = await probeTwitch(mockAccount, 5000);
96
+
97
+ expect(result.ok).toBe(true);
98
+ expect(result.connected).toBe(true);
99
+ expect(result.username).toBe("testbot");
100
+ expect(result.channel).toBe("testchannel"); // uses account's configured channel
101
+ });
102
+
103
+ it("uses custom channel when specified", async () => {
104
+ const account: TwitchAccountConfig = {
105
+ ...mockAccount,
106
+ channel: "customchannel",
107
+ };
108
+
109
+ const result = await probeTwitch(account, 5000);
110
+
111
+ expect(result.ok).toBe(true);
112
+ expect(result.channel).toBe("customchannel");
113
+ });
114
+
115
+ it("times out when connection takes too long", async () => {
116
+ vi.useFakeTimers();
117
+ try {
118
+ mockConnect.mockImplementationOnce(() => new Promise(() => {})); // Never resolves
119
+ const resultPromise = probeTwitch(mockAccount, 100);
120
+ await vi.advanceTimersByTimeAsync(100);
121
+ const result = await resultPromise;
122
+
123
+ expect(result.ok).toBe(false);
124
+ expect(result.error).toContain("timeout");
125
+ } finally {
126
+ vi.useRealTimers();
127
+ mockConnect.mockImplementation(defaultConnectImpl);
128
+ }
129
+ });
130
+
131
+ it("cleans up client even on failure", async () => {
132
+ mockConnect.mockImplementationOnce(async () => {
133
+ // Simulate connection failure by calling disconnect handler
134
+ // onDisconnect signature: (manually: boolean, reason?: Error) => void
135
+ if (disconnectHandler) {
136
+ disconnectHandler(false, new Error("Connection failed"));
137
+ }
138
+ });
139
+
140
+ const result = await probeTwitch(mockAccount, 5000);
141
+
142
+ expect(result.ok).toBe(false);
143
+ expect(result.error).toContain("Connection failed");
144
+ expect(mockQuit).toHaveBeenCalled();
145
+
146
+ // Reset mocks
147
+ mockConnect.mockImplementation(defaultConnectImpl);
148
+ });
149
+
150
+ it("handles connection errors gracefully", async () => {
151
+ mockConnect.mockImplementationOnce(async () => {
152
+ // Simulate connection failure by calling disconnect handler
153
+ // onDisconnect signature: (manually: boolean, reason?: Error) => void
154
+ if (disconnectHandler) {
155
+ disconnectHandler(false, new Error("Network error"));
156
+ }
157
+ });
158
+
159
+ const result = await probeTwitch(mockAccount, 5000);
160
+
161
+ expect(result.ok).toBe(false);
162
+ expect(result.error).toContain("Network error");
163
+
164
+ // Reset mock
165
+ mockConnect.mockImplementation(defaultConnectImpl);
166
+ });
167
+
168
+ it("trims token before validation", async () => {
169
+ const account: TwitchAccountConfig = {
170
+ ...mockAccount,
171
+ accessToken: " oauth:test123456789 ",
172
+ };
173
+
174
+ const result = await probeTwitch(account, 5000);
175
+
176
+ expect(result.ok).toBe(true);
177
+ });
178
+
179
+ it("handles non-Error objects in catch block", async () => {
180
+ mockConnect.mockImplementationOnce(async () => {
181
+ // Simulate connection failure by calling disconnect handler
182
+ // onDisconnect signature: (manually: boolean, reason?: Error) => void
183
+ if (disconnectHandler) {
184
+ disconnectHandler(false, "String error" as unknown as Error);
185
+ }
186
+ });
187
+
188
+ const result = await probeTwitch(mockAccount, 5000);
189
+
190
+ expect(result.ok).toBe(false);
191
+ expect(result.error).toBe("String error");
192
+
193
+ // Reset mock
194
+ mockConnect.mockImplementation(defaultConnectImpl);
195
+ });
196
+ });
package/src/probe.ts ADDED
@@ -0,0 +1,119 @@
1
+ import { StaticAuthProvider } from "@twurple/auth";
2
+ import { ChatClient } from "@twurple/chat";
3
+ import type { BaseProbeResult } from "openclaw/plugin-sdk";
4
+ import type { TwitchAccountConfig } from "./types.js";
5
+ import { normalizeToken } from "./utils/twitch.js";
6
+
7
+ /**
8
+ * Result of probing a Twitch account
9
+ */
10
+ export type ProbeTwitchResult = BaseProbeResult<string> & {
11
+ username?: string;
12
+ elapsedMs: number;
13
+ connected?: boolean;
14
+ channel?: string;
15
+ };
16
+
17
+ /**
18
+ * Probe a Twitch account to verify the connection is working
19
+ *
20
+ * This tests the Twitch OAuth token by attempting to connect
21
+ * to the chat server and verify the bot's username.
22
+ */
23
+ export async function probeTwitch(
24
+ account: TwitchAccountConfig,
25
+ timeoutMs: number,
26
+ ): Promise<ProbeTwitchResult> {
27
+ const started = Date.now();
28
+
29
+ if (!account.accessToken || !account.username) {
30
+ return {
31
+ ok: false,
32
+ error: "missing credentials (accessToken, username)",
33
+ username: account.username,
34
+ elapsedMs: Date.now() - started,
35
+ };
36
+ }
37
+
38
+ const rawToken = normalizeToken(account.accessToken.trim());
39
+
40
+ let client: ChatClient | undefined;
41
+
42
+ try {
43
+ const authProvider = new StaticAuthProvider(account.clientId ?? "", rawToken);
44
+
45
+ client = new ChatClient({
46
+ authProvider,
47
+ });
48
+
49
+ // Create a promise that resolves when connected
50
+ const connectionPromise = new Promise<void>((resolve, reject) => {
51
+ let settled = false;
52
+ let connectListener: ReturnType<ChatClient["onConnect"]> | undefined;
53
+ let disconnectListener: ReturnType<ChatClient["onDisconnect"]> | undefined;
54
+ let authFailListener: ReturnType<ChatClient["onAuthenticationFailure"]> | undefined;
55
+
56
+ const cleanup = () => {
57
+ if (settled) {
58
+ return;
59
+ }
60
+ settled = true;
61
+ connectListener?.unbind();
62
+ disconnectListener?.unbind();
63
+ authFailListener?.unbind();
64
+ };
65
+
66
+ // Success: connection established
67
+ connectListener = client?.onConnect(() => {
68
+ cleanup();
69
+ resolve();
70
+ });
71
+
72
+ // Failure: disconnected (e.g., auth failed)
73
+ disconnectListener = client?.onDisconnect((_manually, reason) => {
74
+ cleanup();
75
+ reject(reason || new Error("Disconnected"));
76
+ });
77
+
78
+ // Failure: authentication failed
79
+ authFailListener = client?.onAuthenticationFailure(() => {
80
+ cleanup();
81
+ reject(new Error("Authentication failed"));
82
+ });
83
+ });
84
+
85
+ const timeout = new Promise<never>((_, reject) => {
86
+ setTimeout(() => reject(new Error(`timeout after ${timeoutMs}ms`)), timeoutMs);
87
+ });
88
+
89
+ client.connect();
90
+ await Promise.race([connectionPromise, timeout]);
91
+
92
+ client.quit();
93
+ client = undefined;
94
+
95
+ return {
96
+ ok: true,
97
+ connected: true,
98
+ username: account.username,
99
+ channel: account.channel,
100
+ elapsedMs: Date.now() - started,
101
+ };
102
+ } catch (error) {
103
+ return {
104
+ ok: false,
105
+ error: error instanceof Error ? error.message : String(error),
106
+ username: account.username,
107
+ channel: account.channel,
108
+ elapsedMs: Date.now() - started,
109
+ };
110
+ } finally {
111
+ if (client) {
112
+ try {
113
+ client.quit();
114
+ } catch {
115
+ // Ignore cleanup errors
116
+ }
117
+ }
118
+ }
119
+ }