feat: replace browser login with imported Reddit sessions

This commit is contained in:
jjbrunton committed 2026-08-08 16:48:59 +01:00
1 parent 2b0545afcf
commit c483c7b8be
76 files changed
+2048 -2828

No files matched your search

+50 -4
View File
@@ -1,6 +1,11 @@
import { Router } from 'express';
import type { ActiveChat } from '../../services/chat.js';
import type { WebContext } from '../context.js';
import {
buildConversationState,
chooseConversationIntent,
mergeConversationState,
} from '../../services/conversation-state.js';
export function createConversationsRouter(ctx: WebContext): Router {
const router = Router();
@@ -13,6 +18,18 @@ export function createConversationsRouter(ctx: WebContext): Router {
);
}
function getConversationIntelligence(conversation: ActiveChat, requestedIntent?: 'media-ask') {
const messages = (conversation.messages ?? []).map((message) => ({
sender: message.sender,
text: message.text,
isSelf: message.isSelf,
}));
const saved = conversation.roomId ? ctx.services.conversationState?.get(conversation.roomId) : undefined;
const state = mergeConversationState(saved, buildConversationState(messages));
if (conversation.roomId) ctx.services.conversationState?.save(conversation.roomId, state);
return { messages, state, intent: chooseConversationIntent(messages, state, requestedIntent) };
}
router.get('/conversations', async (_req, res) => {
if (!ctx.services.chat) {
res.json([]);
@@ -138,11 +155,14 @@ export function createConversationsRouter(ctx: WebContext): Router {
return;
}
const intelligence = getConversationIntelligence(conversation);
const suggestedReply = await ctx.services.ai.generateReply(
conversation.messages.map((m) => ({ sender: m.sender, text: m.text, isSelf: m.isSelf })),
intelligence.messages,
conversation.username,
intelligence.state,
intelligence.intent,
);
res.json({ suggestedReply });
res.json({ suggestedReply, intent: intelligence.intent, conversationState: intelligence.state });
} catch (error) {
ctx.logger.error('Failed to generate suggested reply:', error);
res.status(500).json({ error: String(error) });
@@ -173,11 +193,18 @@ export function createConversationsRouter(ctx: WebContext): Router {
return;
}
const intelligence = getConversationIntelligence(conversation, 'media-ask');
if (intelligence.intent.kind !== 'media-ask') {
res.status(409).json({ error: intelligence.intent.reason, intent: intelligence.intent, conversationState: intelligence.state });
return;
}
const suggestedReply = await ctx.services.ai.generateMediaAsk(
conversation.messages.map((m) => ({ sender: m.sender, text: m.text, isSelf: m.isSelf })),
intelligence.messages,
conversation.username,
intelligence.state,
intelligence.intent,
);
res.json({ suggestedReply });
res.json({ suggestedReply, intent: intelligence.intent, conversationState: intelligence.state });
} catch (error) {
ctx.logger.error('Failed to generate media-ask message:', error);
res.status(500).json({ error: String(error) });
@@ -208,6 +235,19 @@ export function createConversationsRouter(ctx: WebContext): Router {
const success = await ctx.services.chat.sendChatMessage(conversation.url, message.trim());
if (success) {
if (conversation.roomId) {
const currentMessages = (conversation.messages ?? []).map((item) => ({
sender: item.sender,
text: item.text,
isSelf: item.isSelf,
}));
const saved = ctx.services.conversationState?.get(conversation.roomId);
const updatedState = mergeConversationState(
saved,
buildConversationState([...currentMessages, { sender: 'Me', text: message.trim(), isSelf: true }]),
);
ctx.services.conversationState?.save(conversation.roomId, updatedState);
}
ctx.activity.push('sent', `Sent chat reply to u/${conversation.username}`, conversation.username);
ctx.broadcaster.push('info', `Chat reply sent to u/${conversation.username}`);
res.json({ success: true });
@@ -249,6 +289,12 @@ export function createConversationsRouter(ctx: WebContext): Router {
return;
}
ctx.activity.push('sent', `Started chat with u/${chat.username}`, chat.username);
if (chat.roomId) {
ctx.services.conversationState?.save(
chat.roomId,
buildConversationState(chat.messages ?? [{ sender: 'Me', text: message.trim(), isSelf: true }]),
);
}
ctx.broadcaster.push('info', `Started new chat with u/${chat.username}`);
res.json({ success: true, chat });
} catch (error) {