@kodelyth/twitch 2026.5.42 → 2026.6.2

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.
Files changed (45) hide show
  1. package/klaw.plugin.json +219 -2
  2. package/package.json +19 -2
  3. package/api.ts +0 -21
  4. package/channel-plugin-api.ts +0 -1
  5. package/index.test.ts +0 -13
  6. package/index.ts +0 -16
  7. package/runtime-api.ts +0 -22
  8. package/setup-entry.ts +0 -9
  9. package/setup-plugin-api.ts +0 -3
  10. package/src/access-control.test.ts +0 -373
  11. package/src/access-control.ts +0 -195
  12. package/src/actions.test.ts +0 -75
  13. package/src/actions.ts +0 -175
  14. package/src/client-manager-registry.ts +0 -87
  15. package/src/config-schema.test.ts +0 -46
  16. package/src/config-schema.ts +0 -88
  17. package/src/config.test.ts +0 -233
  18. package/src/config.ts +0 -177
  19. package/src/monitor.ts +0 -311
  20. package/src/outbound.test.ts +0 -572
  21. package/src/outbound.ts +0 -242
  22. package/src/plugin.lifecycle.test.ts +0 -86
  23. package/src/plugin.live.test.ts +0 -120
  24. package/src/plugin.test.ts +0 -77
  25. package/src/plugin.ts +0 -220
  26. package/src/probe.test.ts +0 -196
  27. package/src/probe.ts +0 -130
  28. package/src/resolver.ts +0 -139
  29. package/src/runtime.ts +0 -9
  30. package/src/send.test.ts +0 -342
  31. package/src/send.ts +0 -191
  32. package/src/setup-surface.test.ts +0 -529
  33. package/src/setup-surface.ts +0 -526
  34. package/src/status.test.ts +0 -298
  35. package/src/status.ts +0 -179
  36. package/src/test-fixtures.ts +0 -30
  37. package/src/token.test.ts +0 -198
  38. package/src/token.ts +0 -93
  39. package/src/twitch-client.test.ts +0 -574
  40. package/src/twitch-client.ts +0 -276
  41. package/src/types.ts +0 -104
  42. package/src/utils/markdown.ts +0 -98
  43. package/src/utils/twitch.ts +0 -81
  44. package/test/setup.ts +0 -7
  45. package/tsconfig.json +0 -16
package/src/config.ts DELETED
@@ -1,177 +0,0 @@
1
- import {
2
- listCombinedAccountIds,
3
- normalizeAccountId,
4
- resolveNormalizedAccountEntry,
5
- } from "klaw/plugin-sdk/account-resolution";
6
- import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
7
- import { resolveTwitchToken, type TwitchTokenResolution } from "./token.js";
8
- import type { TwitchAccountConfig } from "./types.js";
9
- import { isAccountConfigured } from "./utils/twitch.js";
10
-
11
- /**
12
- * Default account ID for Twitch
13
- */
14
- export const DEFAULT_ACCOUNT_ID = "default";
15
-
16
- export type ResolvedTwitchAccountContext = {
17
- accountId: string;
18
- account: TwitchAccountConfig | null;
19
- tokenResolution: TwitchTokenResolution;
20
- configured: boolean;
21
- availableAccountIds: string[];
22
- };
23
-
24
- /**
25
- * Get account config from core config
26
- *
27
- * Handles two patterns:
28
- * 1. Simplified single-account: base-level properties create implicit "default" account
29
- * 2. Multi-account: explicit accounts object
30
- *
31
- * For "default" account, base-level properties take precedence over accounts.default
32
- * For other accounts, only the accounts object is checked
33
- */
34
- export function getAccountConfig(
35
- coreConfig: unknown,
36
- accountId: string,
37
- ): TwitchAccountConfig | null {
38
- if (!coreConfig || typeof coreConfig !== "object") {
39
- return null;
40
- }
41
-
42
- const cfg = coreConfig as KlawConfig;
43
- const normalizedAccountId = normalizeAccountId(accountId);
44
- const twitch = cfg.channels?.twitch;
45
- // Access accounts via unknown to handle union type (single-account vs multi-account)
46
- const twitchRaw = twitch as Record<string, unknown> | undefined;
47
- const accounts = twitchRaw?.accounts as Record<string, TwitchAccountConfig> | undefined;
48
-
49
- // For default account, check base-level config first
50
- if (normalizedAccountId === DEFAULT_ACCOUNT_ID) {
51
- const accountFromAccounts = resolveNormalizedAccountEntry(
52
- accounts,
53
- DEFAULT_ACCOUNT_ID,
54
- normalizeAccountId,
55
- );
56
-
57
- // Base-level properties that can form an implicit default account
58
- const baseLevel = {
59
- username: typeof twitchRaw?.username === "string" ? twitchRaw.username : undefined,
60
- accessToken: typeof twitchRaw?.accessToken === "string" ? twitchRaw.accessToken : undefined,
61
- clientId: typeof twitchRaw?.clientId === "string" ? twitchRaw.clientId : undefined,
62
- channel: typeof twitchRaw?.channel === "string" ? twitchRaw.channel : undefined,
63
- enabled: typeof twitchRaw?.enabled === "boolean" ? twitchRaw.enabled : undefined,
64
- allowFrom: Array.isArray(twitchRaw?.allowFrom) ? twitchRaw.allowFrom : undefined,
65
- allowedRoles: Array.isArray(twitchRaw?.allowedRoles) ? twitchRaw.allowedRoles : undefined,
66
- requireMention:
67
- typeof twitchRaw?.requireMention === "boolean" ? twitchRaw.requireMention : undefined,
68
- clientSecret:
69
- typeof twitchRaw?.clientSecret === "string" ? twitchRaw.clientSecret : undefined,
70
- refreshToken:
71
- typeof twitchRaw?.refreshToken === "string" ? twitchRaw.refreshToken : undefined,
72
- expiresIn: typeof twitchRaw?.expiresIn === "number" ? twitchRaw.expiresIn : undefined,
73
- obtainmentTimestamp:
74
- typeof twitchRaw?.obtainmentTimestamp === "number"
75
- ? twitchRaw.obtainmentTimestamp
76
- : undefined,
77
- };
78
-
79
- // Merge: base-level takes precedence over accounts.default
80
- const merged: Partial<TwitchAccountConfig> = {
81
- ...accountFromAccounts,
82
- ...baseLevel,
83
- } as Partial<TwitchAccountConfig>;
84
-
85
- // Only return if we have at least username
86
- if (merged.username) {
87
- return merged as TwitchAccountConfig;
88
- }
89
-
90
- // Fall through to accounts.default if no base-level username
91
- if (accountFromAccounts) {
92
- return accountFromAccounts;
93
- }
94
-
95
- return null;
96
- }
97
-
98
- // For non-default accounts, only check accounts object
99
- const account = resolveNormalizedAccountEntry(accounts, normalizedAccountId, normalizeAccountId);
100
- if (!account) {
101
- return null;
102
- }
103
-
104
- return account;
105
- }
106
-
107
- /**
108
- * List all configured account IDs
109
- *
110
- * Includes both explicit accounts and implicit "default" from base-level config
111
- */
112
- export function listAccountIds(cfg: KlawConfig): string[] {
113
- const twitch = cfg.channels?.twitch;
114
- // Access accounts via unknown to handle union type (single-account vs multi-account)
115
- const twitchRaw = twitch as Record<string, unknown> | undefined;
116
- const accountMap = twitchRaw?.accounts as Record<string, unknown> | undefined;
117
-
118
- // Add implicit "default" if base-level config exists and "default" not already present
119
- const hasBaseLevelConfig =
120
- twitchRaw &&
121
- (typeof twitchRaw.username === "string" ||
122
- typeof twitchRaw.accessToken === "string" ||
123
- typeof twitchRaw.channel === "string");
124
-
125
- return listCombinedAccountIds({
126
- configuredAccountIds: Object.keys(accountMap ?? {}).map((accountId) =>
127
- normalizeAccountId(accountId),
128
- ),
129
- implicitAccountId: hasBaseLevelConfig ? DEFAULT_ACCOUNT_ID : undefined,
130
- });
131
- }
132
-
133
- export function resolveDefaultTwitchAccountId(cfg: KlawConfig): string {
134
- const preferredRaw =
135
- typeof cfg.channels?.twitch?.defaultAccount === "string"
136
- ? cfg.channels.twitch.defaultAccount.trim()
137
- : "";
138
- const preferred = preferredRaw ? normalizeAccountId(preferredRaw) : "";
139
- const ids = listAccountIds(cfg);
140
- if (preferred && ids.includes(preferred)) {
141
- return preferred;
142
- }
143
- if (ids.includes(DEFAULT_ACCOUNT_ID)) {
144
- return DEFAULT_ACCOUNT_ID;
145
- }
146
- return ids[0] ?? DEFAULT_ACCOUNT_ID;
147
- }
148
-
149
- export function resolveTwitchAccountContext(
150
- cfg: KlawConfig,
151
- accountId?: string | null,
152
- ): ResolvedTwitchAccountContext {
153
- const resolvedAccountId = accountId?.trim()
154
- ? normalizeAccountId(accountId)
155
- : resolveDefaultTwitchAccountId(cfg);
156
- const account = getAccountConfig(cfg, resolvedAccountId);
157
- const tokenResolution = resolveTwitchToken(cfg, { accountId: resolvedAccountId });
158
- return {
159
- accountId: resolvedAccountId,
160
- account,
161
- tokenResolution,
162
- configured: account ? isAccountConfigured(account, tokenResolution.token) : false,
163
- availableAccountIds: listAccountIds(cfg),
164
- };
165
- }
166
-
167
- export function resolveTwitchSnapshotAccountId(
168
- cfg: KlawConfig,
169
- account: TwitchAccountConfig,
170
- ): string {
171
- const twitch = (cfg as Record<string, unknown>).channels as Record<string, unknown> | undefined;
172
- const twitchCfg = twitch?.twitch as Record<string, unknown> | undefined;
173
- const accountMap = (twitchCfg?.accounts as Record<string, unknown> | undefined) ?? {};
174
- return (
175
- Object.entries(accountMap).find(([, value]) => value === account)?.[0] ?? DEFAULT_ACCOUNT_ID
176
- );
177
- }
package/src/monitor.ts DELETED
@@ -1,311 +0,0 @@
1
- /**
2
- * Twitch message monitor - processes incoming messages and routes to agents.
3
- *
4
- * This monitor connects to the Twitch client manager, processes incoming messages,
5
- * resolves agent routes, and handles replies.
6
- */
7
-
8
- import type { MarkdownTableMode, KlawConfig } from "klaw/plugin-sdk/config-contracts";
9
- import { formatErrorMessage } from "klaw/plugin-sdk/error-runtime";
10
- import type { ReplyPayload } from "klaw/plugin-sdk/reply-runtime";
11
- import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
12
- import { checkTwitchAccessControl } from "./access-control.js";
13
- import { getOrCreateClientManager } from "./client-manager-registry.js";
14
- import { getTwitchRuntime } from "./runtime.js";
15
- import type { TwitchAccountConfig, TwitchChatMessage } from "./types.js";
16
- import { stripMarkdownForTwitch } from "./utils/markdown.js";
17
-
18
- export type TwitchRuntimeEnv = {
19
- log?: (message: string) => void;
20
- error?: (message: string) => void;
21
- };
22
-
23
- export type TwitchMonitorOptions = {
24
- account: TwitchAccountConfig;
25
- accountId: string;
26
- config: unknown; // KlawConfig
27
- runtime: TwitchRuntimeEnv;
28
- abortSignal: AbortSignal;
29
- statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
30
- };
31
-
32
- export type TwitchMonitorResult = {
33
- stop: () => void;
34
- };
35
-
36
- type TwitchCoreRuntime = ReturnType<typeof getTwitchRuntime>;
37
-
38
- /**
39
- * Process an incoming Twitch message and dispatch to agent.
40
- */
41
- async function processTwitchMessage(params: {
42
- message: TwitchChatMessage;
43
- account: TwitchAccountConfig;
44
- accountId: string;
45
- config: unknown;
46
- runtime: TwitchRuntimeEnv;
47
- core: TwitchCoreRuntime;
48
- statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
49
- }): Promise<void> {
50
- const { message, account, accountId, config, runtime, core, statusSink } = params;
51
- const cfg = config as KlawConfig;
52
-
53
- await core.channel.turn.run({
54
- channel: "twitch",
55
- accountId,
56
- raw: message,
57
- adapter: {
58
- ingest: (incoming) => ({
59
- id: incoming.id ?? `${incoming.channel}:${incoming.timestamp?.getTime() ?? Date.now()}`,
60
- timestamp: incoming.timestamp?.getTime(),
61
- rawText: incoming.message,
62
- textForAgent: incoming.message,
63
- textForCommands: incoming.message,
64
- raw: incoming,
65
- }),
66
- resolveTurn: (input) => {
67
- const route = core.channel.routing.resolveAgentRoute({
68
- cfg,
69
- channel: "twitch",
70
- accountId,
71
- peer: {
72
- kind: "group",
73
- id: message.channel,
74
- },
75
- });
76
- const senderId = message.userId ?? message.username;
77
- const fromLabel = message.displayName ?? message.username;
78
- const body = core.channel.reply.formatAgentEnvelope({
79
- channel: "Twitch",
80
- from: fromLabel,
81
- timestamp: input.timestamp,
82
- envelope: core.channel.reply.resolveEnvelopeFormatOptions(cfg),
83
- body: input.rawText,
84
- });
85
- const ctxPayload = core.channel.turn.buildContext({
86
- channel: "twitch",
87
- accountId,
88
- messageId: input.id,
89
- timestamp: input.timestamp,
90
- from: `twitch:user:${senderId}`,
91
- sender: {
92
- id: senderId,
93
- name: fromLabel,
94
- username: message.username,
95
- },
96
- conversation: {
97
- kind: "group",
98
- id: message.channel,
99
- label: message.channel,
100
- routePeer: {
101
- kind: "group",
102
- id: message.channel,
103
- },
104
- },
105
- route: {
106
- agentId: route.agentId,
107
- accountId: route.accountId,
108
- routeSessionKey: route.sessionKey,
109
- },
110
- reply: {
111
- to: `twitch:channel:${message.channel}`,
112
- originatingTo: `twitch:channel:${message.channel}`,
113
- },
114
- message: {
115
- body,
116
- rawBody: input.rawText,
117
- bodyForAgent: input.textForAgent,
118
- commandBody: input.textForCommands,
119
- envelopeFrom: fromLabel,
120
- },
121
- });
122
- const storePath = core.channel.session.resolveStorePath(cfg.session?.store, {
123
- agentId: route.agentId,
124
- });
125
- const tableMode = core.channel.text.resolveMarkdownTableMode({
126
- cfg,
127
- channel: "twitch",
128
- accountId,
129
- });
130
- return {
131
- cfg,
132
- channel: "twitch",
133
- accountId,
134
- agentId: route.agentId,
135
- routeSessionKey: route.sessionKey,
136
- storePath,
137
- ctxPayload,
138
- recordInboundSession: core.channel.session.recordInboundSession,
139
- dispatchReplyWithBufferedBlockDispatcher:
140
- core.channel.reply.dispatchReplyWithBufferedBlockDispatcher,
141
- delivery: {
142
- durable: () => ({
143
- to: `twitch:channel:${message.channel}`,
144
- }),
145
- deliver: async (payload) => {
146
- return await deliverTwitchReply({
147
- payload,
148
- channel: message.channel,
149
- account,
150
- accountId,
151
- config,
152
- tableMode,
153
- runtime,
154
- });
155
- },
156
- onDelivered: (_payload, _info, result) => {
157
- if (result?.visibleReplySent !== false) {
158
- statusSink?.({ lastOutboundAt: Date.now() });
159
- }
160
- },
161
- onError: (err, info) => {
162
- runtime.error?.(`Twitch ${info.kind} reply failed: ${String(err)}`);
163
- },
164
- },
165
- replyPipeline: {},
166
- record: {
167
- onRecordError: (err) => {
168
- runtime.error?.(`Failed updating session meta: ${String(err)}`);
169
- },
170
- },
171
- };
172
- },
173
- },
174
- });
175
- }
176
-
177
- /**
178
- * Deliver a reply to Twitch chat.
179
- */
180
- async function deliverTwitchReply(params: {
181
- payload: ReplyPayload;
182
- channel: string;
183
- account: TwitchAccountConfig;
184
- accountId: string;
185
- config: unknown;
186
- tableMode: MarkdownTableMode;
187
- runtime: TwitchRuntimeEnv;
188
- }): Promise<{ visibleReplySent: boolean }> {
189
- const { payload, channel, account, accountId, config, runtime } = params;
190
-
191
- try {
192
- const clientManager = getOrCreateClientManager(accountId, {
193
- info: (msg) => runtime.log?.(msg),
194
- warn: (msg) => runtime.log?.(msg),
195
- error: (msg) => runtime.error?.(msg),
196
- debug: (msg) => runtime.log?.(msg),
197
- });
198
-
199
- const client = await clientManager.getClient(
200
- account,
201
- config as Parameters<typeof clientManager.getClient>[1],
202
- accountId,
203
- );
204
- if (!client) {
205
- runtime.error?.(`No client available for sending reply`);
206
- return { visibleReplySent: false };
207
- }
208
-
209
- // Send the reply
210
- if (!payload.text) {
211
- runtime.error?.(`No text to send in reply payload`);
212
- return { visibleReplySent: false };
213
- }
214
-
215
- const textToSend = stripMarkdownForTwitch(payload.text);
216
-
217
- await client.say(channel, textToSend);
218
- return { visibleReplySent: true };
219
- } catch (err) {
220
- runtime.error?.(`Failed to send reply: ${String(err)}`);
221
- return { visibleReplySent: false };
222
- }
223
- }
224
-
225
- /**
226
- * Main monitor provider for Twitch.
227
- *
228
- * Sets up message handlers and processes incoming messages.
229
- */
230
- export async function monitorTwitchProvider(
231
- options: TwitchMonitorOptions,
232
- ): Promise<TwitchMonitorResult> {
233
- const { account, accountId, config, runtime, abortSignal, statusSink } = options;
234
-
235
- const core = getTwitchRuntime();
236
- let stopped = false;
237
-
238
- const coreLogger = core.logging.getChildLogger({ module: "twitch" });
239
- const logVerboseMessage = (message: string) => {
240
- if (!core.logging.shouldLogVerbose()) {
241
- return;
242
- }
243
- coreLogger.debug?.(message);
244
- };
245
- const logger = {
246
- info: (msg: string) => coreLogger.info(msg),
247
- warn: (msg: string) => coreLogger.warn(msg),
248
- error: (msg: string) => coreLogger.error(msg),
249
- debug: logVerboseMessage,
250
- };
251
-
252
- const clientManager = getOrCreateClientManager(accountId, logger);
253
-
254
- try {
255
- await clientManager.getClient(
256
- account,
257
- config as Parameters<typeof clientManager.getClient>[1],
258
- accountId,
259
- );
260
- } catch (error) {
261
- const errorMsg = formatErrorMessage(error);
262
- runtime.error?.(`Failed to connect: ${errorMsg}`);
263
- throw error;
264
- }
265
-
266
- const unregisterHandler = clientManager.onMessage(account, (message) => {
267
- if (stopped) {
268
- return;
269
- }
270
-
271
- void (async () => {
272
- const botUsername = normalizeLowercaseStringOrEmpty(account.username);
273
- if (normalizeLowercaseStringOrEmpty(message.username) === botUsername) {
274
- return;
275
- }
276
-
277
- const access = await checkTwitchAccessControl({
278
- message,
279
- account,
280
- botUsername,
281
- });
282
-
283
- if (stopped || !access.allowed) {
284
- return;
285
- }
286
-
287
- statusSink?.({ lastInboundAt: Date.now() });
288
-
289
- await processTwitchMessage({
290
- message,
291
- account,
292
- accountId,
293
- config,
294
- runtime,
295
- core,
296
- statusSink,
297
- });
298
- })().catch((err) => {
299
- runtime.error?.(`Message processing failed: ${String(err)}`);
300
- });
301
- });
302
-
303
- const stop = () => {
304
- stopped = true;
305
- unregisterHandler();
306
- };
307
-
308
- abortSignal.addEventListener("abort", stop, { once: true });
309
-
310
- return { stop };
311
- }