-
Notifications
You must be signed in to change notification settings - Fork 37
feat: twitch chat integration + audience-aware AI #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,127 @@ | ||||||||||||||||
| import { db } from "./db.ts"; | ||||||||||||||||
| import type { ChatMessage } from "./chat.ts"; | ||||||||||||||||
|
|
||||||||||||||||
| // ── Schema ────────────────────────────────────────────────────────────────── | ||||||||||||||||
|
|
||||||||||||||||
| db.exec(` | ||||||||||||||||
| CREATE TABLE IF NOT EXISTS chat_messages ( | ||||||||||||||||
| id TEXT PRIMARY KEY, | ||||||||||||||||
| username TEXT NOT NULL, | ||||||||||||||||
| display_name TEXT NOT NULL, | ||||||||||||||||
| content TEXT NOT NULL, | ||||||||||||||||
| timestamp INTEGER NOT NULL, | ||||||||||||||||
| badges TEXT, | ||||||||||||||||
| is_subscriber INTEGER DEFAULT 0, | ||||||||||||||||
| is_mod INTEGER DEFAULT 0, | ||||||||||||||||
| round_num INTEGER | ||||||||||||||||
| ); | ||||||||||||||||
| CREATE INDEX IF NOT EXISTS idx_chat_round ON chat_messages(round_num); | ||||||||||||||||
| CREATE INDEX IF NOT EXISTS idx_chat_timestamp ON chat_messages(timestamp); | ||||||||||||||||
| `); | ||||||||||||||||
|
|
||||||||||||||||
| // ── Persistence ───────────────────────────────────────────────────────────── | ||||||||||||||||
|
|
||||||||||||||||
| const insertStmt = db.prepare(` | ||||||||||||||||
| INSERT OR IGNORE INTO chat_messages | ||||||||||||||||
| (id, username, display_name, content, timestamp, badges, is_subscriber, is_mod, round_num) | ||||||||||||||||
| VALUES ($id, $username, $displayName, $content, $timestamp, $badges, $isSubscriber, $isMod, $roundNum) | ||||||||||||||||
| `); | ||||||||||||||||
|
|
||||||||||||||||
| const insertBatch = db.transaction((messages: ChatMessage[]) => { | ||||||||||||||||
| for (const msg of messages) { | ||||||||||||||||
| insertStmt.run({ | ||||||||||||||||
| $id: msg.id, | ||||||||||||||||
| $username: msg.username, | ||||||||||||||||
| $displayName: msg.displayName, | ||||||||||||||||
| $content: msg.content, | ||||||||||||||||
| $timestamp: msg.timestamp, | ||||||||||||||||
| $badges: JSON.stringify(msg.badges), | ||||||||||||||||
| $isSubscriber: msg.isSubscriber ? 1 : 0, | ||||||||||||||||
| $isMod: msg.isMod ? 1 : 0, | ||||||||||||||||
| $roundNum: msg.roundNum, | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| const pendingMessages: ChatMessage[] = []; | ||||||||||||||||
| let flushTimer: ReturnType<typeof setInterval> | null = null; | ||||||||||||||||
|
|
||||||||||||||||
| const FLUSH_INTERVAL_MS = 5_000; | ||||||||||||||||
|
|
||||||||||||||||
| function flush() { | ||||||||||||||||
| if (pendingMessages.length === 0) return; | ||||||||||||||||
| const batch = pendingMessages.splice(0); | ||||||||||||||||
| insertBatch(batch); | ||||||||||||||||
| } | ||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| export function queueMessage(message: ChatMessage) { | ||||||||||||||||
| pendingMessages.push(message); | ||||||||||||||||
| if (pendingMessages.length >= 50) { | ||||||||||||||||
| flush(); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export function startPersistence() { | ||||||||||||||||
| flushTimer = setInterval(flush, FLUSH_INTERVAL_MS); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+70
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium Calling
Suggested change
🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||||||||||||||||
|
|
||||||||||||||||
| export function stopPersistence() { | ||||||||||||||||
| if (flushTimer) { | ||||||||||||||||
| clearInterval(flushTimer); | ||||||||||||||||
| flushTimer = null; | ||||||||||||||||
| } | ||||||||||||||||
| flush(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // ── Queries ───────────────────────────────────────────────────────────────── | ||||||||||||||||
|
|
||||||||||||||||
| type ChatRow = { | ||||||||||||||||
| id: string; | ||||||||||||||||
| username: string; | ||||||||||||||||
| display_name: string; | ||||||||||||||||
| content: string; | ||||||||||||||||
| timestamp: number; | ||||||||||||||||
| badges: string; | ||||||||||||||||
| is_subscriber: number; | ||||||||||||||||
| is_mod: number; | ||||||||||||||||
| round_num: number | null; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| function rowToMessage(row: ChatRow): ChatMessage { | ||||||||||||||||
| return { | ||||||||||||||||
| id: row.id, | ||||||||||||||||
| username: row.username, | ||||||||||||||||
| displayName: row.display_name, | ||||||||||||||||
| content: row.content, | ||||||||||||||||
| timestamp: row.timestamp, | ||||||||||||||||
| badges: JSON.parse(row.badges || "{}"), | ||||||||||||||||
| isSubscriber: row.is_subscriber === 1, | ||||||||||||||||
| isMod: row.is_mod === 1, | ||||||||||||||||
| roundNum: row.round_num, | ||||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export function getRecentChat(limit = 50): ChatMessage[] { | ||||||||||||||||
| const rows = db | ||||||||||||||||
| .query( | ||||||||||||||||
| "SELECT * FROM chat_messages ORDER BY timestamp DESC LIMIT $limit", | ||||||||||||||||
| ) | ||||||||||||||||
| .all({ $limit: limit }) as ChatRow[]; | ||||||||||||||||
| return rows.reverse().map(rowToMessage); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+111
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This function queries SQLite, but up to 50 messages (or 5 seconds worth) may still be sitting in 🤖 Prompt for AI Agents |
||||||||||||||||
|
|
||||||||||||||||
| export function getChatForRound(roundNum: number): ChatMessage[] { | ||||||||||||||||
| const rows = db | ||||||||||||||||
| .query( | ||||||||||||||||
| "SELECT * FROM chat_messages WHERE round_num = $roundNum ORDER BY timestamp ASC", | ||||||||||||||||
| ) | ||||||||||||||||
| .all({ $roundNum: roundNum }) as ChatRow[]; | ||||||||||||||||
| return rows.map(rowToMessage); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export function getChatCount(): number { | ||||||||||||||||
| const result = db | ||||||||||||||||
| .query("SELECT COUNT(*) as count FROM chat_messages") | ||||||||||||||||
| .get() as { count: number }; | ||||||||||||||||
| return result.count; | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
chat-store.ts:51Messages are removed from
pendingMessagesbeforeinsertBatchsucceeds. If the insert throws, those messages are lost. Consider restoring the batch on error (e.g.,catch { pendingMessages.unshift(...batch); throw e; }) or only splicing after success.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: