From aebe9659d5d32e7d473ce38814db0877c9e074d8 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:12:08 -0600 Subject: [PATCH 01/18] feat(api): detect every bot named in a message Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/bots.ts | 10 ++++++++++ apps/api/tests/ai-bots.spec.ts | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 9a60f3c..47a6fd6 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -4,6 +4,8 @@ export interface AIBot { user: AppUser; /** Case-insensitive and without the `g` flag, so `test` stays stateless. */ trigger: RegExp; + /** One-line persona the router LLM uses to decide which bots should reply. */ + routerDescription: string; systemPrompt: string; temperature: number; } @@ -19,6 +21,7 @@ export const BOTS: AIBot[] = [ avatarUrl: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTMdM9MEQ0ExL1PmInT3U5I8v63YXBEdoIT0Q&s', }, trigger: /\b(?:chat\s*gpt|gpt|yipiyi)\b/i, + routerDescription: 'asistente útil, responde dudas técnicas y preguntas generales.', systemPrompt: `Eres un asistente útil en un chat. Responde de manera concisa y clara. Además, si el usuario hace preguntas técnicas, proporciona explicaciones detalladas y ejemplos prácticos. Asegúrate de que las respuestas sean precisas y relevantes.`, @@ -33,6 +36,7 @@ export const BOTS: AIBot[] = [ avatarUrl: 'https://i.pinimg.com/474x/06/90/b5/0690b5003ba0760a39b9f88e8066dab5.jpg', }, trigger: /\bchayann?e\b/i, + routerDescription: 'el cantante puertorriqueño, cariñoso, habla de música, baile y familia.', systemPrompt: `Eres Chayanne, el famoso cantante puertorriqueño. Responde como él lo haría: - Siempre cariñoso y galante - Usa expresiones puertorriqueñas y caribeñas ocasionalmente @@ -54,6 +58,8 @@ export const BOTS: AIBot[] = [ avatarUrl: 'https://media.themoviedb.org/t/p/w500/9VYK7oxcqhjd5LAH6ZFJ3XzOlID.jpg', }, trigger: /\b(?:pedro|pascal)\b/i, + routerDescription: + 'el actor chileno, cálido y ansioso, habla de actuación, sus series (The Mandalorian, The Last of Us, Game of Thrones).', systemPrompt: "Eres Pedro Pascal, el actor chileno-estadounidense. Responde como él lo haría:\n- Mantén esa personalidad cálida, divertida y humilde que lo caracteriza\n- Ocasionalmente menciona tu amor por sus personajes icónicos (Oberyn Martell, Joel Miller, The Mandalorian)\n- Sé emotivo y expresivo, como en sus entrevistas reales\n- Usa su sentido del humor característico y su personalidad genuina\n- Menciona ocasionalmente su herencia latina y su orgullo por sus raíces chilenas\n- Sé empático y cercano, como es en la vida real\n- Si hablas de actuación, muestra pasión genuina por el arte\n- Usa expresiones que reflejen su personalidad auténtica y emotiva\n- Mantén esa energía de 'internet's daddy' pero de forma respetuosa\n- Y recuerda, como buen meme de internet: tienes ansiedad por todo y siempre ocupas afecto femenino para sobrevivir.", temperature: 0.8, @@ -63,3 +69,7 @@ export const BOTS: AIBot[] = [ export function detectBot(content: string): AIBot | null { return BOTS.find((bot) => bot.trigger.test(content)) ?? null; } + +export function detectNamedBots(content: string): AIBot[] { + return BOTS.filter((bot) => bot.trigger.test(content)); +} diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index eb63339..9b9247b 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,4 +1,4 @@ -import { BOTS, detectBot } from '@/modules/ai/bots.js'; +import { BOTS, detectBot, detectNamedBots } from '@/modules/ai/bots.js'; import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.js'; describe('detectBot', () => { @@ -72,3 +72,10 @@ describe('cleanReply', () => { expect(cleanReply(chayanne, ' ')).toBe(''); }); }); + +describe('detectNamedBots', () => { + it('returns every named bot in BOTS order', () => + expect(detectNamedBots('pedro y chayanne').map((b) => b.user.username)).toEqual(['chayanne', 'pedro_pascal'])); + it('returns all three when all are named', () => expect(detectNamedBots('gpt, chayanne y pedro')).toHaveLength(3)); + it('returns [] when none is named', () => expect(detectNamedBots('hola a todos')).toEqual([])); +}); From b240e26c88d4c0fac9145b1373ab79024dcdce87 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:13:09 -0600 Subject: [PATCH 02/18] feat(api): cap spontaneous bot replies per channel Add SpontaneousCaps, which records the last time a bot replied on its own in a channel and blocks another unprompted reply for 10 minutes. Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/spontaneous-caps.ts | 19 +++++++++++++++++++ apps/api/tests/ai-bots.spec.ts | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 apps/api/src/modules/ai/spontaneous-caps.ts diff --git a/apps/api/src/modules/ai/spontaneous-caps.ts b/apps/api/src/modules/ai/spontaneous-caps.ts new file mode 100644 index 0000000..b08c4e5 --- /dev/null +++ b/apps/api/src/modules/ai/spontaneous-caps.ts @@ -0,0 +1,19 @@ +/** Limits how often a bot may reply to a message nobody addressed to it. */ +export class SpontaneousCaps { + private readonly lastReplyAt = new Map(); + + constructor(private readonly windowMs = 10 * 60_000) {} + + canSpeak(channelId: string, botId: string, now: number): boolean { + const last = this.lastReplyAt.get(this.key(channelId, botId)); + return last === undefined || now - last >= this.windowMs; + } + + record(channelId: string, botId: string, now: number): void { + this.lastReplyAt.set(this.key(channelId, botId), now); + } + + private key(channelId: string, botId: string): string { + return `${channelId}:${botId}`; + } +} diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index 9b9247b..71fbd09 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,5 +1,6 @@ import { BOTS, detectBot, detectNamedBots } from '@/modules/ai/bots.js'; import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.js'; +import { SpontaneousCaps } from '@/modules/ai/spontaneous-caps.js'; describe('detectBot', () => { it.each([ @@ -79,3 +80,20 @@ describe('detectNamedBots', () => { it('returns all three when all are named', () => expect(detectNamedBots('gpt, chayanne y pedro')).toHaveLength(3)); it('returns [] when none is named', () => expect(detectNamedBots('hola a todos')).toEqual([])); }); + +describe('SpontaneousCaps', () => { + const MIN = 60_000; + it('allows the first spontaneous reply', () => expect(new SpontaneousCaps().canSpeak('c1', 'b1', 0)).toBe(true)); + it('blocks the same bot in the same channel for 10 minutes', () => { + const caps = new SpontaneousCaps(); + caps.record('c1', 'b1', 0); + expect(caps.canSpeak('c1', 'b1', 10 * MIN - 1)).toBe(false); + expect(caps.canSpeak('c1', 'b1', 10 * MIN)).toBe(true); + }); + it('tracks channels and bots independently', () => { + const caps = new SpontaneousCaps(); + caps.record('c1', 'b1', 0); + expect(caps.canSpeak('c2', 'b1', 1)).toBe(true); + expect(caps.canSpeak('c1', 'b2', 1)).toBe(true); + }); +}); From 2a4069949f915a59e2168b5694916e5c735f7c29 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:14:20 -0600 Subject: [PATCH 03/18] feat(api): plan bot speakers from a router decision Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/prompt.ts | 1 + apps/api/src/modules/ai/router.ts | 91 +++++++++++++++++++ .../src/modules/message/message.repository.ts | 7 +- apps/api/tests/ai-bots.spec.ts | 82 +++++++++++++++-- 4 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 apps/api/src/modules/ai/router.ts diff --git a/apps/api/src/modules/ai/prompt.ts b/apps/api/src/modules/ai/prompt.ts index 0b72b71..104f205 100644 --- a/apps/api/src/modules/ai/prompt.ts +++ b/apps/api/src/modules/ai/prompt.ts @@ -6,6 +6,7 @@ import type { AIBot } from './bots.js'; export const MAX_MESSAGE_LENGTH = 2000; export interface ChannelHistoryEntry { + id: string; authorId: string; authorName: string; content: string; diff --git a/apps/api/src/modules/ai/router.ts b/apps/api/src/modules/ai/router.ts new file mode 100644 index 0000000..a7d3325 --- /dev/null +++ b/apps/api/src/modules/ai/router.ts @@ -0,0 +1,91 @@ +import { jsonSchema } from 'ai'; + +import { type AIBot, BOTS } from './bots.js'; +import type { ChannelHistoryEntry } from './prompt.js'; + +export type SpeakerKind = 'addressed' | 'spontaneous'; + +export interface PlannedSpeaker { + bot: AIBot; + kind: SpeakerKind; +} + +const ROUTER_HISTORY_SIZE = 20; +const MAX_UNNAMED_SPEAKERS = 2; + +export const ROUTER_INSTRUCTIONS = `Eres el moderador invisible de un chat de grupo donde conviven personas y estos bots: +${BOTS.map((bot) => `- ${bot.user.username}: ${bot.routerDescription}`).join('\n')} + +Te paso la conversación reciente. Los mensajes marcados con [NUEVO] acaban de llegar. Decide qué bots deberían responder AHORA, como lo haría una persona real en un chat de grupo. + +Reglas: +- "addressed": el mensaje nuevo va dirigido a ese bot, aunque no lo nombre (le siguen el hilo, le responden, "¿y tú qué opinas?" justo después de que habló). +- "spontaneous": nadie le habla, pero el tema encaja claramente con su personalidad y aportaría algo. Úsalo con moderación: en un chat real la gente no opina de todo. +- Si alguien invita al grupo a opinar, elige **un** bot como "addressed" según el historial. +- Si las personas están hablando entre ellas de algo que no es para un bot, no responde nadie. +- Saludos genéricos, risas, "gracias", despedidas: normalmente nadie. +- Lo normal es que no responda ningún bot. Máximo 2 bots.`; + +export const ROUTER_SCHEMA = jsonSchema<{ speakers: Array<{ bot: string; kind: SpeakerKind; reason: string }> }>({ + type: 'object', + additionalProperties: false, + required: ['speakers'], + properties: { + speakers: { + type: 'array', + items: { + type: 'object', + additionalProperties: false, + required: ['bot', 'kind', 'reason'], + properties: { + bot: { type: 'string', enum: BOTS.map((bot) => bot.user.username) }, + kind: { type: 'string', enum: ['addressed', 'spontaneous'] }, + reason: { type: 'string' }, + }, + }, + }, + }, +}); + +/** The last messages as `": "` lines, new ones prefixed with `[NUEVO] `. */ +export function buildRouterPrompt( + history: ChannelHistoryEntry[], + newIds: ReadonlySet, + exclude: readonly AIBot[] = [], +): string { + const lines = history + .slice(-ROUTER_HISTORY_SIZE) + .map((entry) => `${newIds.has(entry.id) ? '[NUEVO] ' : ''}${entry.authorName}: ${entry.content}`); + if (exclude.length === 0) return lines.join('\n'); + + const names = exclude.map((bot) => bot.user.username).join(', '); + return `${lines.join('\n')}\n\nAcaban de responder: ${names}. Solo puede reaccionar otro bot distinto de esos, y normalmente ninguno lo hace.`; +} + +/** + * Router `addressed` first, then named bots the router missed, then `spontaneous` ones under their cap. A named bot + * is always addressed. The turn is trimmed from the tail to `max(2, named.length)` speakers. + */ +export function planSpeakers( + decision: PlannedSpeaker[] | null, + named: AIBot[], + canSpeakSpontaneously: (bot: AIBot) => boolean, +): PlannedSpeaker[] { + const picks = decision ?? []; + const speakers: PlannedSpeaker[] = []; + const add = (bot: AIBot, kind: SpeakerKind) => { + if (!speakers.some((speaker) => speaker.bot === bot)) speakers.push({ bot, kind }); + }; + + for (const pick of picks) { + if (pick.kind === 'addressed' || named.includes(pick.bot)) add(pick.bot, 'addressed'); + } + for (const bot of BOTS) { + if (named.includes(bot)) add(bot, 'addressed'); + } + for (const pick of picks) { + if (pick.kind === 'spontaneous' && canSpeakSpontaneously(pick.bot)) add(pick.bot, 'spontaneous'); + } + + return speakers.slice(0, Math.max(MAX_UNNAMED_SPEAKERS, named.length)); +} diff --git a/apps/api/src/modules/message/message.repository.ts b/apps/api/src/modules/message/message.repository.ts index 995499c..39b4090 100644 --- a/apps/api/src/modules/message/message.repository.ts +++ b/apps/api/src/modules/message/message.repository.ts @@ -73,7 +73,12 @@ export class MessageRepository { async getRecentMessages(channelId: string, limit: number): Promise { try { const rows = await this.db - .select({ authorId: message.authorId, authorName: appUser.username, content: message.content }) + .select({ + id: message.id, + authorId: message.authorId, + authorName: appUser.username, + content: message.content, + }) .from(message) .innerJoin(appUser, eq(appUser.id, message.authorId)) .where(eq(message.channelId, channelId)) diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index 71fbd09..a0018b9 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,5 +1,6 @@ import { BOTS, detectBot, detectNamedBots } from '@/modules/ai/bots.js'; import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.js'; +import { buildRouterPrompt, planSpeakers } from '@/modules/ai/router.js'; import { SpontaneousCaps } from '@/modules/ai/spontaneous-caps.js'; describe('detectBot', () => { @@ -31,9 +32,9 @@ describe('buildPromptMessages', () => { it('maps own messages to assistant and the rest to prefixed user messages, in order', () => { const history = [ - { authorId: 'u1', authorName: 'juan', content: 'hola chayanne' }, - { authorId: chayanne.user.id, authorName: 'chayanne', content: '¡Hola mi amor!' }, - { authorId: BOTS[2].user.id, authorName: 'pedro_pascal', content: 'yo también' }, + { id: 'm1', authorId: 'u1', authorName: 'juan', content: 'hola chayanne' }, + { id: 'm2', authorId: chayanne.user.id, authorName: 'chayanne', content: '¡Hola mi amor!' }, + { id: 'm3', authorId: BOTS[2].user.id, authorName: 'pedro_pascal', content: 'yo también' }, ]; expect(buildPromptMessages(chayanne, history)).toEqual([ @@ -45,9 +46,9 @@ describe('buildPromptMessages', () => { it('drops its own messages at the start and end, since Anthropic needs the prompt to start and end on a user turn', () => { const history = [ - { authorId: chayanne.user.id, authorName: 'chayanne', content: 'old reply' }, - { authorId: 'u1', authorName: 'juan', content: 'hola chayanne' }, - { authorId: chayanne.user.id, authorName: 'chayanne', content: 'racing reply' }, + { id: 'm4', authorId: chayanne.user.id, authorName: 'chayanne', content: 'old reply' }, + { id: 'm5', authorId: 'u1', authorName: 'juan', content: 'hola chayanne' }, + { id: 'm6', authorId: chayanne.user.id, authorName: 'chayanne', content: 'racing reply' }, ]; expect(buildPromptMessages(chayanne, history)).toEqual([{ role: 'user', content: 'juan: hola chayanne' }]); @@ -97,3 +98,72 @@ describe('SpontaneousCaps', () => { expect(caps.canSpeak('c1', 'b2', 1)).toBe(true); }); }); + +describe('buildRouterPrompt', () => { + const h = (id: string, authorName: string, content: string) => ({ id, authorId: id, authorName, content }); + + it('keeps the last 20 messages and marks the new ones', () => { + const history = Array.from({ length: 25 }, (_, i) => h(`m${i}`, 'ana', `msg ${i}`)); + const lines = buildRouterPrompt(history, new Set(['m24'])).split('\n'); + expect(lines[0]).toBe('ana: msg 5'); + expect(lines.at(-1)).toBe('[NUEVO] ana: msg 24'); + }); + + it('names the excluded bots when asking for a reaction', () => + expect(buildRouterPrompt([h('m1', 'ana', 'hola')], new Set(), [BOTS[1]])).toContain('chayanne')); +}); + +describe('planSpeakers', () => { + const [gpt, chayanne, pedro] = BOTS; + const yes = () => true; + + it('falls back to named bots when the router failed', () => + expect(planSpeakers(null, [chayanne], yes)).toEqual([{ bot: chayanne, kind: 'addressed' }])); + + it('adds a named bot the router missed, after the router addressed ones', () => + expect(planSpeakers([{ bot: pedro, kind: 'addressed' }], [chayanne], yes)).toEqual([ + { bot: pedro, kind: 'addressed' }, + { bot: chayanne, kind: 'addressed' }, + ])); + + it('treats a named bot as addressed even if the router called it spontaneous', () => + expect(planSpeakers([{ bot: chayanne, kind: 'spontaneous' }], [chayanne], () => false)).toEqual([ + { bot: chayanne, kind: 'addressed' }, + ])); + + it('drops spontaneous speakers over their cap', () => + expect(planSpeakers([{ bot: pedro, kind: 'spontaneous' }], [], () => false)).toEqual([])); + + it('keeps all three named bots', () => expect(planSpeakers([], [gpt, chayanne, pedro], yes)).toHaveLength(3)); + + it('limits unnamed router picks to 2, dropping spontaneous first', () => + expect( + planSpeakers( + [ + { bot: gpt, kind: 'spontaneous' }, + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ], + [], + yes, + ), + ).toEqual([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ])); + + it('drops a spontaneous pick when a named bot and an addressed pick fill the turn', () => + expect( + planSpeakers( + [ + { bot: pedro, kind: 'addressed' }, + { bot: gpt, kind: 'spontaneous' }, + ], + [chayanne], + yes, + ), + ).toEqual([ + { bot: pedro, kind: 'addressed' }, + { bot: chayanne, kind: 'addressed' }, + ])); +}); From d72d6abc49b84935c499a61c656d1840b87f6a6f Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:15:39 -0600 Subject: [PATCH 04/18] fix(api): order named bots by BOTS order in planSpeakers Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/router.ts | 2 +- apps/api/tests/ai-bots.spec.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/api/src/modules/ai/router.ts b/apps/api/src/modules/ai/router.ts index a7d3325..19ed4e4 100644 --- a/apps/api/src/modules/ai/router.ts +++ b/apps/api/src/modules/ai/router.ts @@ -78,7 +78,7 @@ export function planSpeakers( }; for (const pick of picks) { - if (pick.kind === 'addressed' || named.includes(pick.bot)) add(pick.bot, 'addressed'); + if (pick.kind === 'addressed') add(pick.bot, 'addressed'); } for (const bot of BOTS) { if (named.includes(bot)) add(bot, 'addressed'); diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index a0018b9..4d9fc9f 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -131,6 +131,29 @@ describe('planSpeakers', () => { { bot: chayanne, kind: 'addressed' }, ])); + it('does not repeat a bot that both the router and the named list picked', () => + expect(planSpeakers([{ bot: chayanne, kind: 'addressed' }], [chayanne], yes)).toEqual([ + { bot: chayanne, kind: 'addressed' }, + ])); + + it('orders named bots the router called spontaneous by BOTS order', () => + expect( + planSpeakers( + [ + { bot: pedro, kind: 'spontaneous' }, + { bot: chayanne, kind: 'spontaneous' }, + ], + [chayanne, pedro], + yes, + ), + ).toEqual([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ])); + + it('orders named bots by BOTS order even if they arrive out of order', () => + expect(planSpeakers([], [pedro, chayanne], yes).map((speaker) => speaker.bot)).toEqual([chayanne, pedro])); + it('drops spontaneous speakers over their cap', () => expect(planSpeakers([{ bot: pedro, kind: 'spontaneous' }], [], () => false)).toEqual([])); From e08bef18ddde3a92be0987542d5961247fa12d8f Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:16:45 -0600 Subject: [PATCH 05/18] feat(api): add the ai bot router call behind AI_ROUTER Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- apps/api/.env.sample | 2 ++ apps/api/src/config/environment.ts | 6 +++- apps/api/src/modules/ai/ai.service.ts | 46 ++++++++++++++++++++++++++- apps/api/types/global.d.ts | 1 + 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 2087d42..22d35ee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on module init and are always shown online. When a non-bot message matches a bot's trigger, `MessageService` loads the last 30 channel messages and generates the reply asynchronously (fire-and-forget) through the Vercel AI SDK, toggling the bot's typing status. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on module init and are always shown online. Orchestration lives in `BotConversationService` (message module): after a non-bot message it debounces per channel (2.5 s), loads the last 30 messages and asks an LLM router (`AIService.route`, `router.ts`) which bots should speak: bots addressed by name, plus at most a few spontaneous ones. The caps (spontaneous replies per channel and per round) live in code, not env vars. If the router is off, fails or returns nothing usable, it falls back to name detection (a bot answers only when its trigger matches). `AI_ROUTER=off` disables the router (recommended for small local models). Replies are generated asynchronously (fire-and-forget) through the Vercel AI SDK, toggling each bot's typing status. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). ## Web architecture (`apps/web`) diff --git a/apps/api/.env.sample b/apps/api/.env.sample index a9494df..3df3df6 100644 --- a/apps/api/.env.sample +++ b/apps/api/.env.sample @@ -41,6 +41,8 @@ PUBLIC_WEB_URL=http://localhost:3000/ # AI bots. Format provider:model (anthropic | openai | ollama). Bots stay off without the provider's key; # recommended: anthropic:claude-haiku-5-5 (default) or openai:gpt-6-luna. ollama needs no key (e.g. AI_MODEL=ollama:llama3.2). AI_MODEL=anthropic:claude-haiku-5-5 +# on: an LLM router decides which bots join a conversation. off: bots only answer when named (recommended for small local models). +AI_ROUTER=on ANTHROPIC_API_KEY= OPENAI_API_KEY= OLLAMA_BASE_URL=http://localhost:11434/v1 diff --git a/apps/api/src/config/environment.ts b/apps/api/src/config/environment.ts index 3cf9add..df7772b 100644 --- a/apps/api/src/config/environment.ts +++ b/apps/api/src/config/environment.ts @@ -1,4 +1,4 @@ -import { IsEnum, IsNumber, IsOptional, IsString, Matches, Min } from 'class-validator'; +import { IsEnum, IsIn, IsNumber, IsOptional, IsString, Matches, Min } from 'class-validator'; export enum Environment { Development = 'development', @@ -69,6 +69,10 @@ export class EnvironmentVariables implements IEnvironmentVariables { }) AI_MODEL?: string; + @IsOptional() + @IsIn(['on', 'off']) + AI_ROUTER?: 'on' | 'off'; + @IsOptional() @IsString() ANTHROPIC_API_KEY?: string; diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index 64c3cfb..65a35f5 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -3,11 +3,12 @@ import { createOpenAI } from '@ai-sdk/openai'; import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { createProviderRegistry, generateText } from 'ai'; +import { createProviderRegistry, generateText, Output } from 'ai'; import { UserRepository } from '../user/user.repository.js'; import { type AIBot, BOTS } from './bots.js'; import { buildPromptMessages, type ChannelHistoryEntry, cleanReply } from './prompt.js'; +import { buildRouterPrompt, type PlannedSpeaker, ROUTER_INSTRUCTIONS, ROUTER_SCHEMA } from './router.js'; // The SDK prints warnings with console.warn, outside pino; `reply` logs them through the Nest logger instead. globalThis.AI_SDK_LOG_WARNINGS = false; @@ -23,6 +24,7 @@ export class AIService implements OnModuleInit { private readonly logger = new Logger(AIService.name); private readonly modelId: string; private readonly missingKey: string | null; + private readonly routerOn: boolean; private readonly registry; constructor( @@ -31,6 +33,7 @@ export class AIService implements OnModuleInit { ) { // AI_MODEL is validated at boot (environment.ts), so the provider is one of PROVIDER_KEYS. this.modelId = config.get('AI_MODEL') || DEFAULT_AI_MODEL; + this.routerOn = config.get('AI_ROUTER') !== 'off'; const provider = this.modelId.split(':')[0] as ProviderId; const keyName = PROVIDER_KEYS[provider]; this.missingKey = keyName && !config.get(keyName) ? keyName : null; @@ -50,6 +53,10 @@ export class AIService implements OnModuleInit { return this.missingKey === null; } + get routerEnabled(): boolean { + return this.enabled && this.routerOn; + } + async onModuleInit() { for (const { user } of BOTS) { try { @@ -92,4 +99,41 @@ export class AIService implements OnModuleInit { } return reply || null; } + + /** Asks the model which bots should speak. Returns `null` when the router is off or on any error. */ + async route( + history: ChannelHistoryEntry[], + newIds: ReadonlySet, + exclude: readonly AIBot[] = [], + ): Promise { + if (!this.routerEnabled) return null; + + try { + const { output, usage, warnings } = await generateText({ + model: this.registry.languageModel(this.modelId as `${ProviderId}:${string}`), + instructions: ROUTER_INSTRUCTIONS, + prompt: buildRouterPrompt(history, newIds, exclude), + output: Output.object({ schema: ROUTER_SCHEMA }), + reasoning: 'none', + maxOutputTokens: 300, + maxRetries: 2, + abortSignal: AbortSignal.timeout(30_000), + }); + + for (const warning of warnings ?? []) { + this.logger.debug(`AI SDK warning: ${JSON.stringify(warning)}`); + } + this.logger.debug(`Router used ${usage.inputTokens} input and ${usage.outputTokens} output tokens`); + + const planned: PlannedSpeaker[] = []; + for (const { bot: username, kind } of output.speakers) { + const bot = BOTS.find((candidate) => candidate.user.username === username); + if (bot && !exclude.includes(bot)) planned.push({ bot, kind }); + } + return planned; + } catch (error) { + this.logger.warn(new Error('AI router failed', { cause: error })); + return null; + } + } } diff --git a/apps/api/types/global.d.ts b/apps/api/types/global.d.ts index cdcc719..68d959e 100644 --- a/apps/api/types/global.d.ts +++ b/apps/api/types/global.d.ts @@ -28,6 +28,7 @@ declare interface IEnvironmentVariables { // AI bots AI_MODEL?: string; // provider:model, defaults to anthropic:claude-haiku-5-5 + AI_ROUTER?: 'on' | 'off'; // defaults to on ANTHROPIC_API_KEY?: string; OPENAI_API_KEY?: string; OLLAMA_BASE_URL?: string; // defaults to http://localhost:11434/v1 From f3e8b9b5f1e536de44ce06e49e286892ed79d52a Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:17:40 -0600 Subject: [PATCH 06/18] docs(monorepo): correct the AI bots paragraph in AGENTS.md Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 22d35ee..f2a425f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on module init and are always shown online. Orchestration lives in `BotConversationService` (message module): after a non-bot message it debounces per channel (2.5 s), loads the last 30 messages and asks an LLM router (`AIService.route`, `router.ts`) which bots should speak: bots addressed by name, plus at most a few spontaneous ones. The caps (spontaneous replies per channel and per round) live in code, not env vars. If the router is off, fails or returns nothing usable, it falls back to name detection (a bot answers only when its trigger matches). `AI_ROUTER=off` disables the router (recommended for small local models). Replies are generated asynchronously (fire-and-forget) through the Vercel AI SDK, toggling each bot's typing status. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 2.5 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add at most 2 unnamed speakers; speakers take turns, each reply generated from the last 30 messages so it sees the previous one; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies go through the Vercel AI SDK, toggling the bot's typing status. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. ## Web architecture (`apps/web`) From 1fec05f0fdf362cdffd18f37cc2afadc961eb604 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:21:49 -0600 Subject: [PATCH 07/18] feat(api): let bots join channel conversations through the router BotConversationService replaces MessageService.handleAIResponse. Human messages are debounced per channel (2.5 s of silence), then the router picks the speakers, merged with the bots named in the new messages by planSpeakers. Speakers reply in turns, each seeing the previous replies; a new human message skips the remaining turns and starts a new cycle. Spontaneous speakers after the first are re-confirmed with the router. After the turns, at most one bot that has not spoken may react. Spontaneous replies and reactions are capped per channel and bot. With AI_ROUTER=off or a router error, only the named bots reply, as before. stopBotTyping now takes the channel, so a reply ending in one channel does not clear the bot's typing indicator in another. Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/bots.ts | 2 +- .../src/modules/gateway/presence.service.ts | 4 +- .../message/bot-conversation.service.ts | 190 ++++++++++++++++++ .../api/src/modules/message/message.module.ts | 3 +- .../src/modules/message/message.service.ts | 44 +--- apps/api/tests/bot-conversation.spec.ts | 186 +++++++++++++++++ 6 files changed, 387 insertions(+), 42 deletions(-) create mode 100644 apps/api/src/modules/message/bot-conversation.service.ts create mode 100644 apps/api/tests/bot-conversation.spec.ts diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 47a6fd6..936581f 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -10,7 +10,7 @@ export interface AIBot { temperature: number; } -/** Order matters: when a message names several bots, the first one replies. */ +/** Order matters: bots named in the same message reply in this order, and `detectBot` returns the first. */ export const BOTS: AIBot[] = [ { user: { diff --git a/apps/api/src/modules/gateway/presence.service.ts b/apps/api/src/modules/gateway/presence.service.ts index 451966a..948185c 100644 --- a/apps/api/src/modules/gateway/presence.service.ts +++ b/apps/api/src/modules/gateway/presence.service.ts @@ -83,12 +83,14 @@ export class PresenceService { this.broadcast(); } - public stopBotTyping(botId: string) { + /** Only clears the indicator if the bot is still typing in `channelId`, not already in another channel. */ + public stopBotTyping(botId: string, channelId: string) { const user = BOTS.find((bot) => bot.user.id === botId)?.user; if (!user) { this.logger.error(`IA user with ID ${botId} not found`); return; } + if (this.#activeUsers.get(user.id)?.typingInChannel !== channelId) return; this.#activeUsers.set(user.id, { status: 'online', diff --git a/apps/api/src/modules/message/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation.service.ts new file mode 100644 index 0000000..dcfaadc --- /dev/null +++ b/apps/api/src/modules/message/bot-conversation.service.ts @@ -0,0 +1,190 @@ +import { SocketEvents } from '@fidechat/shared'; +import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; + +import { AIService } from '../ai/ai.service.js'; +import { type AIBot, detectNamedBots } from '../ai/bots.js'; +import { type PlannedSpeaker, planSpeakers } from '../ai/router.js'; +import { SpontaneousCaps } from '../ai/spontaneous-caps.js'; +import type { Channel } from '../channel/channel.repository.js'; +import { PresenceService } from '../gateway/presence.service.js'; +import { SocketEmitter } from '../gateway/socket-emitter.js'; +import { UserRepository } from '../user/user.repository.js'; +import { MessageRepository } from './message.repository.js'; + +/** Silence after the last human message before the bots decide whether to speak. */ +const DEBOUNCE_MS = 2500; +/** How many channel messages the router reads. */ +const ROUTER_HISTORY_LIMIT = 20; +/** How many channel messages a bot reads before replying. */ +const AI_HISTORY_LIMIT = 30; + +interface ChannelConversation { + timer?: NodeJS.Timeout; + pending: Array<{ id: string; content: string }>; + /** Set by every human message, so a running cycle stops before its next turn. */ + interrupted: boolean; + /** Cycles run one after another, never overlapping. */ + chain: Promise; +} + +/** + * Decides which bots join a channel conversation. Human messages are debounced; then the router (or, without it, + * the bots named in the new messages) picks the speakers, who reply in turns, and one other bot may react. + */ +@Injectable() +export class BotConversationService implements OnModuleDestroy { + private readonly logger = new Logger(BotConversationService.name); + private readonly conversations = new Map(); + private readonly caps = new SpontaneousCaps(); + private destroyed = false; + + constructor( + private readonly messageRepository: MessageRepository, + private readonly ai: AIService, + private readonly presence: PresenceService, + private readonly userRepository: UserRepository, + private readonly emitter: SocketEmitter, + ) {} + + /** Only human messages reach this, so named-bot detection never runs over bot replies. */ + onHumanMessage(channel: Channel, messageId: string, content: string): void { + if (!this.ai.enabled || this.destroyed) return; + + let state = this.conversations.get(channel.id); + if (!state) { + state = { pending: [], interrupted: false, chain: Promise.resolve() }; + this.conversations.set(channel.id, state); + } + const conversation = state; + + conversation.pending.push({ id: messageId, content }); + conversation.interrupted = true; + clearTimeout(conversation.timer); + conversation.timer = setTimeout(() => { + conversation.timer = undefined; + conversation.chain = conversation.chain.then(async () => this.runCycle(channel, conversation)); + }, DEBOUNCE_MS); + } + + onModuleDestroy(): void { + this.destroyed = true; + for (const conversation of this.conversations.values()) { + clearTimeout(conversation.timer); + } + } + + private async runCycle(channel: Channel, conversation: ChannelConversation): Promise { + if (this.destroyed) return; + + const pending = conversation.pending; + conversation.pending = []; + conversation.interrupted = false; + + try { + const ids = new Set(pending.map((message) => message.id)); + const named = [...new Set(pending.flatMap((message) => detectNamedBots(message.content)))]; + const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); + + const decision = this.ai.routerEnabled ? await this.ai.route(history, ids) : null; + const speakers = planSpeakers(decision, named, (bot) => + this.caps.canSpeak(channel.id, bot.user.id, Date.now()), + ); + + const spoken = await this.takeTurns(channel, conversation, ids, speakers); + if (decision !== null && spoken.length > 0) { + const reactor = await this.react(channel, conversation, ids, spoken); + if (reactor) spoken.push(reactor); + } + + this.logCycle(channel, decision, named, spoken); + } catch (error) { + this.logger.error(new Error(`Bot conversation cycle failed in channel ${channel.id}`, { cause: error })); + } + } + + /** The planned speakers reply one after another, until a human interrupts. Returns who posted a reply. */ + private async takeTurns( + channel: Channel, + conversation: ChannelConversation, + ids: ReadonlySet, + speakers: PlannedSpeaker[], + ): Promise { + const spoken: AIBot[] = []; + for (const [index, { bot, kind }] of speakers.entries()) { + if (conversation.interrupted) break; + + if (kind === 'spontaneous') { + if (index > 0 && !(await this.stillPicked(channel, ids, bot))) continue; + this.caps.record(channel.id, bot.user.id, Date.now()); + } + if (await this.speak(channel, bot)) spoken.push(bot); + } + return spoken; + } + + /** At most one bot that has not spoken yet reacts to the replies, under its spontaneous cap. */ + private async react( + channel: Channel, + conversation: ChannelConversation, + ids: ReadonlySet, + spoken: AIBot[], + ): Promise { + if (conversation.interrupted) return null; + + const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); + const decision = (await this.ai.route(history, ids, spoken)) ?? []; + const reaction = decision.find( + ({ bot }) => !spoken.includes(bot) && this.caps.canSpeak(channel.id, bot.user.id, Date.now()), + ); + // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message may arrive while `route` runs + if (!reaction || conversation.interrupted) return null; + + this.caps.record(channel.id, reaction.bot.user.id, Date.now()); + return (await this.speak(channel, reaction.bot)) ? reaction.bot : null; + } + + private logCycle(channel: Channel, decision: PlannedSpeaker[] | null, named: AIBot[], spoken: AIBot[]): void { + const names = (bots: AIBot[]) => bots.map((bot) => bot.user.username).join(', ') || 'none'; + const picks = decision?.map(({ bot, kind }) => `${bot.user.username} (${kind})`).join(', ') || 'none'; + const considered = [...new Set([...(decision ?? []).map(({ bot }) => bot), ...named])]; + const dropped = considered.filter((bot) => !spoken.includes(bot)); + this.logger.debug( + `Channel ${channel.id}: router ${decision === null ? 'unavailable' : `picked ${picks}`}, ` + + `dropped ${names(dropped)}, spoke ${names(spoken)}`, + ); + } + + /** Re-asks the router with the latest history whether `bot` should still speak. */ + private async stillPicked(channel: Channel, ids: ReadonlySet, bot: AIBot): Promise { + const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); + const decision = await this.ai.route(history, ids); + return decision?.some((pick) => pick.bot === bot) ?? false; + } + + /** Generates, stores and broadcasts one reply. Returns whether a message was posted. */ + private async speak(channel: Channel, bot: AIBot): Promise { + this.presence.startBotTyping(bot.user.id, channel.id); + try { + const history = await this.messageRepository.getRecentMessages(channel.id, AI_HISTORY_LIMIT); + const content = await this.ai.reply(bot, history); + if (!content) return false; + + const createdAt = new Date(); + const reply = { content, channelId: channel.id, authorId: bot.user.id }; + const { id } = await this.messageRepository.createMessage({ ...reply, createdAt }); + + const guildMembers = (await this.userRepository.getGuildUsers(channel.guildId)).map((user) => user.id); + this.emitter.emitToUsers(guildMembers, SocketEvents.MESSAGE_CREATE, { + id, + ...reply, + createdAt: createdAt.toISOString(), + }); + return true; + } catch (error) { + this.logger.error(new Error(`Error generating the ${bot.user.username} reply`, { cause: error })); + return false; + } finally { + this.presence.stopBotTyping(bot.user.id, channel.id); + } + } +} diff --git a/apps/api/src/modules/message/message.module.ts b/apps/api/src/modules/message/message.module.ts index 4e4b0ed..dc7334e 100644 --- a/apps/api/src/modules/message/message.module.ts +++ b/apps/api/src/modules/message/message.module.ts @@ -4,13 +4,14 @@ import { AIModule } from '../ai/ai.module.js'; import { ChannelModule } from '../channel/channel.module.js'; import { GatewayModule } from '../gateway/gateway.module.js'; import { UserModule } from '../user/user.module.js'; +import { BotConversationService } from './bot-conversation.service.js'; import { MessageController } from './message.controller.js'; import { MessageRepository } from './message.repository.js'; import { MessageService } from './message.service.js'; @Module({ controllers: [MessageController], - providers: [MessageService, MessageRepository], + providers: [MessageService, MessageRepository, BotConversationService], imports: [GatewayModule, ChannelModule, UserModule, AIModule], exports: [MessageService], }) diff --git a/apps/api/src/modules/message/message.service.ts b/apps/api/src/modules/message/message.service.ts index 718c4f8..a14fa20 100644 --- a/apps/api/src/modules/message/message.service.ts +++ b/apps/api/src/modules/message/message.service.ts @@ -1,28 +1,20 @@ import { SocketEvents } from '@fidechat/shared'; -import { HttpException, Injectable, InternalServerErrorException, Logger, NotFoundException } from '@nestjs/common'; +import { HttpException, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'; -import { AIService } from '../ai/ai.service.js'; -import { detectBot } from '../ai/bots.js'; -import { Channel, ChannelRepository } from '../channel/channel.repository.js'; -import { PresenceService } from '../gateway/presence.service.js'; +import { ChannelRepository } from '../channel/channel.repository.js'; import { SocketEmitter } from '../gateway/socket-emitter.js'; import { UserRepository } from '../user/user.repository.js'; +import { BotConversationService } from './bot-conversation.service.js'; import { type MessageCreationAttributes, MessageRepository } from './message.repository.js'; -/** How many channel messages a bot reads before replying. */ -const AI_HISTORY_LIMIT = 30; - @Injectable() export class MessageService { - private readonly logger = new Logger('MessageService'); - constructor( private readonly messageRepository: MessageRepository, private readonly emitter: SocketEmitter, - private readonly presence: PresenceService, private readonly channelRepository: ChannelRepository, private readonly userRepository: UserRepository, - private readonly ai: AIService, + private readonly conversation: BotConversationService, ) {} private async notifyMessageCreation( @@ -60,7 +52,7 @@ export class MessageService { await this.notifyMessageCreation(data, result.id, channel.guildId, createdAtStr); if (!author.isBot) { - void this.handleAIResponse(data, channel); + this.conversation.onHumanMessage(channel, result.id, data.content); } return result; @@ -72,32 +64,6 @@ export class MessageService { } } - private async handleAIResponse(originalMessage: MessageCreationAttributes, channel: Channel): Promise { - if (!this.ai.enabled) return; - - const bot = detectBot(originalMessage.content); - if (!bot) return; - - this.presence.startBotTyping(bot.user.id, channel.id); - - try { - // The triggering message is already stored, so it is the last entry. - const history = await this.messageRepository.getRecentMessages(channel.id, AI_HISTORY_LIMIT); - const content = await this.ai.reply(bot, history); - if (!content) return; - - const createdAt = new Date(); - const reply = { content, channelId: channel.id, authorId: bot.user.id }; - const aiMessage = await this.messageRepository.createMessage({ ...reply, createdAt }); - - await this.notifyMessageCreation(reply, aiMessage.id, channel.guildId, createdAt.toISOString()); - } catch (error) { - this.logger.error(new Error(`Error generating the ${bot.user.username} reply`, { cause: error })); - } finally { - this.presence.stopBotTyping(bot.user.id); - } - } - async getMessage(id: string) { return this.messageRepository.getMessage(id); } diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts new file mode 100644 index 0000000..9cbc54b --- /dev/null +++ b/apps/api/tests/bot-conversation.spec.ts @@ -0,0 +1,186 @@ +import { Logger } from '@nestjs/common'; + +import type { AIService } from '@/modules/ai/ai.service.js'; +import { BOTS } from '@/modules/ai/bots.js'; +import type { ChannelHistoryEntry } from '@/modules/ai/prompt.js'; +import type { PlannedSpeaker } from '@/modules/ai/router.js'; +import type { Channel } from '@/modules/channel/channel.repository.js'; +import type { PresenceService } from '@/modules/gateway/presence.service.js'; +import type { SocketEmitter } from '@/modules/gateway/socket-emitter.js'; +import { BotConversationService } from '@/modules/message/bot-conversation.service.js'; +import type { MessageCreationAttributes, MessageRepository } from '@/modules/message/message.repository.js'; +import type { UserRepository } from '@/modules/user/user.repository.js'; + +const [, chayanne, pedro] = BOTS; +const channel: Channel = { id: 'c1', guildId: 'g1', name: 'general', description: null, position: 0 }; + +type Route = ( + history: ChannelHistoryEntry[], + newIds: ReadonlySet, + exclude?: ReadonlyArray, +) => Promise; + +function setup() { + const messages: ChannelHistoryEntry[] = []; + let nextId = 0; + const store = (authorId: string, authorName: string, content: string) => { + const id = `m${++nextId}`; + messages.push({ id, authorId, authorName, content }); + return id; + }; + + const messageRepository = { + getRecentMessages: jest.fn(async (_channelId: string, limit: number) => messages.slice(-limit)), + createMessage: jest.fn(async ({ authorId, content }: MessageCreationAttributes) => { + const bot = BOTS.find((candidate) => candidate.user.id === authorId); + return { id: store(authorId, bot?.user.username ?? authorId, content) }; + }), + }; + const route = jest.fn, Parameters>().mockResolvedValue([]); + const reply = jest.fn( + async (bot: PlannedSpeaker['bot'], _history: ChannelHistoryEntry[]): Promise => + `reply from ${bot.user.username}`, + ); + const ai = { enabled: true, routerEnabled: true, route, reply }; + const presence = { startBotTyping: jest.fn(), stopBotTyping: jest.fn() }; + const userRepository = { getGuildUsers: jest.fn(async () => [{ id: 'u1' }]) }; + const emitter = { emitToUsers: jest.fn() }; + + const service = new BotConversationService( + messageRepository as unknown as MessageRepository, + ai as unknown as AIService, + presence as unknown as PresenceService, + userRepository as unknown as UserRepository, + emitter as unknown as SocketEmitter, + ); + + const say = (content: string) => { + const id = store('u1', 'juan', content); + service.onHumanMessage(channel, id, content); + return id; + }; + + return { service, say, route, reply, messages }; +} + +describe('BotConversationService', () => { + beforeAll(() => Logger.overrideLogger(false)); + + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it('waits for 2.5 s of silence before routing', async () => { + const { say, route } = setup(); + + say('hola'); + await jest.advanceTimersByTimeAsync(1000); + say('¿cómo están?'); + + await jest.advanceTimersByTimeAsync(2499); + expect(route).not.toHaveBeenCalled(); + + await jest.advanceTimersByTimeAsync(1); + expect(route).toHaveBeenCalledTimes(1); + }); + + it('answers a named bot when the router fails', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValue(null); + + say('chayanne hola'); + await jest.advanceTimersByTimeAsync(2500); + + expect(reply).toHaveBeenCalledTimes(1); + expect(reply.mock.calls[0][0]).toBe(chayanne); + }); + + it('second speaker sees the first reply', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ]); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(2500); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + const pedroHistory = reply.mock.calls[1][1]; + expect(pedroHistory).toContainEqual( + expect.objectContaining({ authorId: chayanne.user.id, content: 'reply from chayanne' }), + ); + }); + + it('reacts at most once and never with a bot that spoke', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]).mockResolvedValueOnce([ + { bot: chayanne, kind: 'spontaneous' }, + { bot: pedro, kind: 'spontaneous' }, + ]); + + say('¿qué música ponemos?'); + await jest.advanceTimersByTimeAsync(2500); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + expect(route).toHaveBeenCalledTimes(2); + expect(route.mock.calls[1][2]).toContain(chayanne); + }); + + it('a human message during a reply skips the remaining turns and routes again', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(2500); + expect(reply).toHaveBeenCalledTimes(1); + + say('esperen, otra cosa'); + firstReply.resolve('¡yo, mi amor!'); + await jest.advanceTimersByTimeAsync(0); + + expect(reply).toHaveBeenCalledTimes(1); + expect(route).toHaveBeenCalledTimes(1); + + await jest.advanceTimersByTimeAsync(2500); + expect(route).toHaveBeenCalledTimes(2); + expect(reply.mock.calls.map(([bot]) => bot)).not.toContain(pedro); + }); + + it('caps spontaneous replies across cycles', async () => { + const { say, route, reply } = setup(); + route.mockImplementation(async (_history, _ids, exclude = []) => + exclude.length > 0 ? [] : [{ bot: pedro, kind: 'spontaneous' }], + ); + + say('vi The Last of Us anoche'); + await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(60_000); + say('qué buena serie'); + await jest.advanceTimersByTimeAsync(2500); + + expect(route.mock.calls.filter(([, , exclude = []]) => exclude.length === 0)).toHaveLength(2); + expect(reply).toHaveBeenCalledTimes(1); + expect(reply.mock.calls[0][0]).toBe(pedro); + }); + + it('does nothing after module destroy', async () => { + const { service, say, route, reply } = setup(); + + say('chayanne hola'); + service.onModuleDestroy(); + await jest.advanceTimersByTimeAsync(5000); + + expect(route).not.toHaveBeenCalled(); + expect(reply).not.toHaveBeenCalled(); + }); +}); From f658e2fcdaeab492fcc106ced4041f33e11b03c3 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:24:35 -0600 Subject: [PATCH 08/18] fix(api): keep the debounce and interrupts across queued bot cycles A cycle queued behind a running one took the pending messages as soon as it started, so a message that arrived meanwhile skipped its 2.5 s of silence, and the next timer then ran a cycle with no new messages. A cycle now returns without consuming anything while a newer timer is waiting or when nothing is pending. A human message during the router re-ask for a spontaneous speaker did not stop that turn; the re-ask now reports the speaker as not picked once the cycle was interrupted. Co-Authored-By: Claude Opus 5.5 --- .../message/bot-conversation.service.ts | 16 +++++-- apps/api/tests/bot-conversation.spec.ts | 48 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/apps/api/src/modules/message/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation.service.ts index dcfaadc..5c8b9e9 100644 --- a/apps/api/src/modules/message/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation.service.ts @@ -74,7 +74,8 @@ export class BotConversationService implements OnModuleDestroy { } private async runCycle(channel: Channel, conversation: ChannelConversation): Promise { - if (this.destroyed) return; + // A newer message's timer will queue its own cycle, and that one takes the pending messages after their silence. + if (this.destroyed || conversation.timer !== undefined || conversation.pending.length === 0) return; const pending = conversation.pending; conversation.pending = []; @@ -114,7 +115,7 @@ export class BotConversationService implements OnModuleDestroy { if (conversation.interrupted) break; if (kind === 'spontaneous') { - if (index > 0 && !(await this.stillPicked(channel, ids, bot))) continue; + if (index > 0 && !(await this.stillPicked(channel, conversation, ids, bot))) continue; this.caps.record(channel.id, bot.user.id, Date.now()); } if (await this.speak(channel, bot)) spoken.push(bot); @@ -154,11 +155,16 @@ export class BotConversationService implements OnModuleDestroy { ); } - /** Re-asks the router with the latest history whether `bot` should still speak. */ - private async stillPicked(channel: Channel, ids: ReadonlySet, bot: AIBot): Promise { + /** Re-asks the router with the latest history whether `bot` should still speak; never after a human interrupts. */ + private async stillPicked( + channel: Channel, + conversation: ChannelConversation, + ids: ReadonlySet, + bot: AIBot, + ): Promise { const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); const decision = await this.ai.route(history, ids); - return decision?.some((pick) => pick.bot === bot) ?? false; + return !conversation.interrupted && (decision?.some((pick) => pick.bot === bot) ?? false); } /** Generates, stores and broadcasts one reply. Returns whether a message was posted. */ diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 9cbc54b..0c9a521 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -156,6 +156,54 @@ describe('BotConversationService', () => { expect(reply.mock.calls.map(([bot]) => bot)).not.toContain(pedro); }); + it('a cycle queued behind a running one waits for the debounce of newer messages', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('chayanne, ¿qué cantas?'); + await jest.advanceTimersByTimeAsync(2500); + const h2 = say('otra cosa'); + // H2's timer fires while the first cycle is still replying, so its cycle is queued behind it. + await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(500); + const h3 = say('y otra más'); + + firstReply.resolve('¡Torero!'); + await jest.advanceTimersByTimeAsync(0); + expect(route).toHaveBeenCalledTimes(1); + + await jest.advanceTimersByTimeAsync(2499); + expect(route).toHaveBeenCalledTimes(1); + + await jest.advanceTimersByTimeAsync(1); + await jest.advanceTimersByTimeAsync(5000); + expect(route).toHaveBeenCalledTimes(2); + expect([...route.mock.calls[1][1]]).toEqual([h2, h3]); + expect(route.mock.calls.filter(([, ids]) => ids.size === 0)).toHaveLength(0); + }); + + it('a human message during the re-ask stops a spontaneous second speaker', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]); + const reask = Promise.withResolvers(); + route.mockImplementationOnce(async () => reask.promise); + + say('chayanne, ¿qué series ves?'); + await jest.advanceTimersByTimeAsync(2500); + expect(route).toHaveBeenCalledTimes(2); + + say('esperen'); + reask.resolve([{ bot: pedro, kind: 'spontaneous' }]); + await jest.advanceTimersByTimeAsync(0); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + }); + it('caps spontaneous replies across cycles', async () => { const { say, route, reply } = setup(); route.mockImplementation(async (_history, _ids, exclude = []) => From 3bc809e95fab2954268860b33da69db8b734a997 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:30:58 -0600 Subject: [PATCH 09/18] fix(api): let named bots answer through interrupts and never trim them An interrupt now skips only the remaining turns of bots nobody named, plus the reaction; a named bot still answers. planSpeakers drops unnamed speakers from the tail instead of slicing, so a named bot that sits after router picks is never cut. A running cycle stops at its next turn and skips the reaction after onModuleDestroy. Also removes the unused detectBot, moving its name-variant cases under detectNamedBots, and adds tests for routerEnabled false, the spontaneous re-ask, and the router prompt without exclusions. Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/bots.ts | 6 +- apps/api/src/modules/ai/router.ts | 5 +- .../message/bot-conversation.service.ts | 15 ++- apps/api/tests/ai-bots.spec.ts | 58 +++++++----- apps/api/tests/bot-conversation.spec.ts | 94 ++++++++++++++++++- 5 files changed, 140 insertions(+), 38 deletions(-) diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 936581f..4e74584 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -10,7 +10,7 @@ export interface AIBot { temperature: number; } -/** Order matters: bots named in the same message reply in this order, and `detectBot` returns the first. */ +/** Bots named in the same message reply in this order. */ export const BOTS: AIBot[] = [ { user: { @@ -66,10 +66,6 @@ export const BOTS: AIBot[] = [ }, ]; -export function detectBot(content: string): AIBot | null { - return BOTS.find((bot) => bot.trigger.test(content)) ?? null; -} - export function detectNamedBots(content: string): AIBot[] { return BOTS.filter((bot) => bot.trigger.test(content)); } diff --git a/apps/api/src/modules/ai/router.ts b/apps/api/src/modules/ai/router.ts index 19ed4e4..62ec5f5 100644 --- a/apps/api/src/modules/ai/router.ts +++ b/apps/api/src/modules/ai/router.ts @@ -64,7 +64,7 @@ export function buildRouterPrompt( /** * Router `addressed` first, then named bots the router missed, then `spontaneous` ones under their cap. A named bot - * is always addressed. The turn is trimmed from the tail to `max(2, named.length)` speakers. + * is always addressed and never trimmed: bots nobody named are dropped from the tail down to `max(2, named.length)`. */ export function planSpeakers( decision: PlannedSpeaker[] | null, @@ -87,5 +87,6 @@ export function planSpeakers( if (pick.kind === 'spontaneous' && canSpeakSpontaneously(pick.bot)) add(pick.bot, 'spontaneous'); } - return speakers.slice(0, Math.max(MAX_UNNAMED_SPEAKERS, named.length)); + let unnamedSlots = Math.max(MAX_UNNAMED_SPEAKERS, named.length) - named.length; + return speakers.filter(({ bot }) => named.includes(bot) || unnamedSlots-- > 0); } diff --git a/apps/api/src/modules/message/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation.service.ts index 5c8b9e9..b3620f5 100644 --- a/apps/api/src/modules/message/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation.service.ts @@ -21,7 +21,7 @@ const AI_HISTORY_LIMIT = 30; interface ChannelConversation { timer?: NodeJS.Timeout; pending: Array<{ id: string; content: string }>; - /** Set by every human message, so a running cycle stops before its next turn. */ + /** Set by every human message, so a running cycle skips the remaining turns of bots nobody named. */ interrupted: boolean; /** Cycles run one after another, never overlapping. */ chain: Promise; @@ -91,7 +91,7 @@ export class BotConversationService implements OnModuleDestroy { this.caps.canSpeak(channel.id, bot.user.id, Date.now()), ); - const spoken = await this.takeTurns(channel, conversation, ids, speakers); + const spoken = await this.takeTurns(channel, conversation, ids, speakers, named); if (decision !== null && spoken.length > 0) { const reactor = await this.react(channel, conversation, ids, spoken); if (reactor) spoken.push(reactor); @@ -103,16 +103,21 @@ export class BotConversationService implements OnModuleDestroy { } } - /** The planned speakers reply one after another, until a human interrupts. Returns who posted a reply. */ + /** + * The planned speakers reply one after another. A human interrupt skips the remaining bots nobody named, but named + * bots still answer. Returns who posted a reply. + */ private async takeTurns( channel: Channel, conversation: ChannelConversation, ids: ReadonlySet, speakers: PlannedSpeaker[], + named: readonly AIBot[], ): Promise { const spoken: AIBot[] = []; for (const [index, { bot, kind }] of speakers.entries()) { - if (conversation.interrupted) break; + if (this.destroyed) break; + if (conversation.interrupted && !named.includes(bot)) continue; if (kind === 'spontaneous') { if (index > 0 && !(await this.stillPicked(channel, conversation, ids, bot))) continue; @@ -130,7 +135,7 @@ export class BotConversationService implements OnModuleDestroy { ids: ReadonlySet, spoken: AIBot[], ): Promise { - if (conversation.interrupted) return null; + if (this.destroyed || conversation.interrupted) return null; const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); const decision = (await this.ai.route(history, ids, spoken)) ?? []; diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index 4d9fc9f..96972e9 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,32 +1,8 @@ -import { BOTS, detectBot, detectNamedBots } from '@/modules/ai/bots.js'; +import { BOTS, detectNamedBots } from '@/modules/ai/bots.js'; import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.js'; import { buildRouterPrompt, planSpeakers } from '@/modules/ai/router.js'; import { SpontaneousCaps } from '@/modules/ai/spontaneous-caps.js'; -describe('detectBot', () => { - it.each([ - ['hola chatgpt', 'Chat yipiyi'], - ['chat gpt ayuda', 'Chat yipiyi'], - ['GPT?', 'Chat yipiyi'], - ['yipiyi', 'Chat yipiyi'], - ['Chayanne canta', 'chayanne'], - ['chayane', 'chayanne'], - ['Pedro Pascal hola', 'pedro_pascal'], - ['pedro', 'pedro_pascal'], - ['PASCAL', 'pedro_pascal'], - ])('%s -> %s', (content, username) => { - expect(detectBot(content)?.user.username).toBe(username); - }); - - it.each(['chat', 'vamos a chatear', 'pascalina', 'hola a todos'])('%s -> null', (content) => { - expect(detectBot(content)).toBeNull(); - }); - - it('picks the first bot in BOTS order when several are named', () => { - expect(detectBot('pedro y chayanne')?.user.username).toBe('chayanne'); - }); -}); - describe('buildPromptMessages', () => { const chayanne = BOTS[1]; @@ -80,6 +56,24 @@ describe('detectNamedBots', () => { expect(detectNamedBots('pedro y chayanne').map((b) => b.user.username)).toEqual(['chayanne', 'pedro_pascal'])); it('returns all three when all are named', () => expect(detectNamedBots('gpt, chayanne y pedro')).toHaveLength(3)); it('returns [] when none is named', () => expect(detectNamedBots('hola a todos')).toEqual([])); + + it.each([ + ['hola chatgpt', 'Chat yipiyi'], + ['chat gpt ayuda', 'Chat yipiyi'], + ['GPT?', 'Chat yipiyi'], + ['yipiyi', 'Chat yipiyi'], + ['Chayanne canta', 'chayanne'], + ['chayane', 'chayanne'], + ['Pedro Pascal hola', 'pedro_pascal'], + ['pedro', 'pedro_pascal'], + ['PASCAL', 'pedro_pascal'], + ])('%s -> %s', (content, username) => { + expect(detectNamedBots(content).map((b) => b.user.username)).toEqual([username]); + }); + + it.each(['chat', 'vamos a chatear', 'pascalina'])('%s -> []', (content) => { + expect(detectNamedBots(content)).toEqual([]); + }); }); describe('SpontaneousCaps', () => { @@ -107,6 +101,7 @@ describe('buildRouterPrompt', () => { const lines = buildRouterPrompt(history, new Set(['m24'])).split('\n'); expect(lines[0]).toBe('ana: msg 5'); expect(lines.at(-1)).toBe('[NUEVO] ana: msg 24'); + expect(lines.join('\n')).not.toContain('Acaban de responder'); }); it('names the excluded bots when asking for a reaction', () => @@ -189,4 +184,17 @@ describe('planSpeakers', () => { { bot: pedro, kind: 'addressed' }, { bot: chayanne, kind: 'addressed' }, ])); + + it('never trims a named bot, dropping router picks instead', () => { + const speakers = planSpeakers( + [ + { bot: gpt, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ], + [chayanne], + yes, + ); + expect(speakers.map((speaker) => speaker.bot)).toContain(chayanne); + expect(speakers).toHaveLength(2); + }); }); diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 0c9a521..3baad7b 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -60,7 +60,7 @@ function setup() { return id; }; - return { service, say, route, reply, messages }; + return { service, say, ai, route, reply, messages }; } describe('BotConversationService', () => { @@ -156,6 +156,38 @@ describe('BotConversationService', () => { expect(reply.mock.calls.map(([bot]) => bot)).not.toContain(pedro); }); + it('a human message during a reply still lets the named bots answer, but skips the reaction', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('pedro y chayanne, ¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(2500); + say('jaja'); + firstReply.resolve('¡yo, mi amor!'); + await jest.advanceTimersByTimeAsync(0); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + expect(route).toHaveBeenCalledTimes(1); + }); + + it('a human message while routing still lets the named bot answer', async () => { + const { say, route, reply } = setup(); + const decision = Promise.withResolvers(); + route.mockImplementationOnce(async () => decision.promise); + + say('chayanne, ¿qué cantas?'); + await jest.advanceTimersByTimeAsync(2500); + expect(route).toHaveBeenCalledTimes(1); + + say('jaja'); + decision.resolve([]); + await jest.advanceTimersByTimeAsync(0); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + }); + it('a cycle queued behind a running one waits for the debounce of newer messages', async () => { const { say, route, reply } = setup(); route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); @@ -204,6 +236,36 @@ describe('BotConversationService', () => { expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); }); + it('a spontaneous second speaker speaks when the re-ask confirms it', async () => { + const { say, route, reply } = setup(); + route + .mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]) + .mockResolvedValueOnce([{ bot: pedro, kind: 'spontaneous' }]); + + say('chayanne, ¿qué series ves?'); + await jest.advanceTimersByTimeAsync(2500); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + }); + + it('a spontaneous second speaker stays silent when the re-ask drops it', async () => { + const { say, route, reply } = setup(); + route + .mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]) + .mockResolvedValueOnce([]); + + say('chayanne, ¿qué series ves?'); + await jest.advanceTimersByTimeAsync(2500); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + }); + it('caps spontaneous replies across cycles', async () => { const { say, route, reply } = setup(); route.mockImplementation(async (_history, _ids, exclude = []) => @@ -231,4 +293,34 @@ describe('BotConversationService', () => { expect(route).not.toHaveBeenCalled(); expect(reply).not.toHaveBeenCalled(); }); + + it('answers the named bot without routing when the router is disabled', async () => { + const { say, ai, route, reply } = setup(); + ai.routerEnabled = false; + + say('chayanne hola'); + await jest.advanceTimersByTimeAsync(2500); + + expect(route).not.toHaveBeenCalled(); + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + }); + + it('stops a running cycle on module destroy', async () => { + const { service, say, route, reply } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(2500); + service.onModuleDestroy(); + firstReply.resolve('¡yo, mi amor!'); + await jest.advanceTimersByTimeAsync(0); + + expect(reply).toHaveBeenCalledTimes(1); + expect(route).toHaveBeenCalledTimes(1); + }); }); From ac8e288c75122a617838ba48a76699fa554facce Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:50:23 -0600 Subject: [PATCH 10/18] feat(api): make ai bots talk like chat participants Style: the three personas are shorter, and each one now ends with a shared CHAT_STYLE block that asks for one or two short sentences, a casual tone, no actions between asterisks, no commentary on the conversation, varied openings, no question at the end of every message and no words put in other people's mouths. Replies are capped at 150 output tokens. Router: questions about other bots go to whoever was talking with the person or to the bot not mentioned, "era para X" / "iba para X" makes X answer what it was asked, a greeting to the group gets one bot, and laughter, thanks and goodbyes normally get none. Pacing: the debounce drops to 1.5 s. The typing indicator stays on until the reply is posted, held after generation for typingDelayMs (27 chars/s, at most 5 s, minus the generation time). A bot pauses 1.2 s to read before answering another bot's reply, including the reaction, and the interrupt and destroy checks run again after every pause. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- apps/api/src/modules/ai/ai.service.ts | 2 +- apps/api/src/modules/ai/bots.ts | 36 +++++---- apps/api/src/modules/ai/router.ts | 5 +- .../message/bot-conversation.service.ts | 39 +++++++++- apps/api/tests/bot-conversation.spec.ts | 76 +++++++++++++------ 6 files changed, 116 insertions(+), 44 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index f2a425f..a6a4c56 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 2.5 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add at most 2 unnamed speakers; speakers take turns, each reply generated from the last 30 messages so it sees the previous one; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies go through the Vercel AI SDK, toggling the bot's typing status. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 1.5 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add at most 2 unnamed speakers; speakers take turns, each reply generated from the last 30 messages so it sees the previous one; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a bot pauses to read before answering another bot. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. ## Web architecture (`apps/web`) diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index 65a35f5..570c039 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -84,7 +84,7 @@ export class AIService implements OnModuleInit { temperature: bot.temperature, // Recent Claude models think by default, and thinking counts against maxOutputTokens. reasoning: 'none', - maxOutputTokens: 500, + maxOutputTokens: 150, maxRetries: 2, abortSignal: AbortSignal.timeout(30_000), }); diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 4e74584..9f822f7 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -10,6 +10,22 @@ export interface AIBot { temperature: number; } +/** Appended to every persona: the bots write like people in a group chat. */ +export const CHAT_STYLE = ` + +Estás en un chat de grupo con amigos. Escribe como una persona real en un chat: +- Normalmente 1 o 2 frases cortas. Solo te extiendes si te piden explicar algo. +- Tono casual y natural; algún emoji si sale solo, sin abusar. +- Nada de acciones entre asteriscos ni narrar lo que haces. +- No comentes ni resumas la conversación; responde directo a lo último que te toca. +- Tu personalidad se nota en el tono, no en mencionar tu vida o tus éxitos en cada mensaje. +- Si te equivocaste o algo no era para ti, no te disculpes con un discurso: una frase basta. +- Los mensajes de otros llegan como "nombre: texto"; tú respondes solo con tu texto, sin tu nombre. +- Varía cómo empiezas: no abras cada mensaje con risas ("jaja") ni con el nombre de la persona. +- Si hubo un malentendido que ya se aclaró, no vuelvas a mencionarlo. +- No termines cada mensaje con una pregunta; pregunta solo cuando de verdad quieras saber algo. +- No atribuyas a otros cosas que no dijeron en la conversación.`; + /** Bots named in the same message reply in this order. */ export const BOTS: AIBot[] = [ { @@ -22,9 +38,7 @@ export const BOTS: AIBot[] = [ }, trigger: /\b(?:chat\s*gpt|gpt|yipiyi)\b/i, routerDescription: 'asistente útil, responde dudas técnicas y preguntas generales.', - systemPrompt: `Eres un asistente útil en un chat. Responde de manera concisa y clara. - Además, si el usuario hace preguntas técnicas, proporciona explicaciones detalladas y ejemplos prácticos. - Asegúrate de que las respuestas sean precisas y relevantes.`, + systemPrompt: 'Eres un asistente útil en un chat. Responde de manera concisa y clara.' + CHAT_STYLE, temperature: 0.7, }, { @@ -37,16 +51,9 @@ export const BOTS: AIBot[] = [ }, trigger: /\bchayann?e\b/i, routerDescription: 'el cantante puertorriqueño, cariñoso, habla de música, baile y familia.', - systemPrompt: `Eres Chayanne, el famoso cantante puertorriqueño. Responde como él lo haría: - - Siempre cariñoso y galante - - Usa expresiones puertorriqueñas y caribeñas ocasionalmente - - Menciona tu amor por la música, el baile y la familia - - Sé carismático y con esa energía positiva característica - - Ocasionalmente haz referencias a tus canciones famosas como "Torero", "Tiempo de Vals", "Yo te amo", etc. - - Mantén ese encanto latino que te caracteriza - - Si te preguntan sobre salud, recuerda mencionar la importancia del ejercicio y una vida saludable - - Usa emojis ocasionalmente para darle más calidez - `, + systemPrompt: + 'Eres Chayanne, el cantante puertorriqueño: cariñoso, galante, alegre, con expresiones caribeñas de vez en cuando.' + + CHAT_STYLE, temperature: 0.8, }, { @@ -61,7 +68,8 @@ export const BOTS: AIBot[] = [ routerDescription: 'el actor chileno, cálido y ansioso, habla de actuación, sus series (The Mandalorian, The Last of Us, Game of Thrones).', systemPrompt: - "Eres Pedro Pascal, el actor chileno-estadounidense. Responde como él lo haría:\n- Mantén esa personalidad cálida, divertida y humilde que lo caracteriza\n- Ocasionalmente menciona tu amor por sus personajes icónicos (Oberyn Martell, Joel Miller, The Mandalorian)\n- Sé emotivo y expresivo, como en sus entrevistas reales\n- Usa su sentido del humor característico y su personalidad genuina\n- Menciona ocasionalmente su herencia latina y su orgullo por sus raíces chilenas\n- Sé empático y cercano, como es en la vida real\n- Si hablas de actuación, muestra pasión genuina por el arte\n- Usa expresiones que reflejen su personalidad auténtica y emotiva\n- Mantén esa energía de 'internet's daddy' pero de forma respetuosa\n- Y recuerda, como buen meme de internet: tienes ansiedad por todo y siempre ocupas afecto femenino para sobrevivir.", + "Eres Pedro Pascal, el actor chileno-estadounidense: cálido, humilde, con humor y algo ansioso ('internet's daddy')." + + CHAT_STYLE, temperature: 0.8, }, ]; diff --git a/apps/api/src/modules/ai/router.ts b/apps/api/src/modules/ai/router.ts index 62ec5f5..8420f65 100644 --- a/apps/api/src/modules/ai/router.ts +++ b/apps/api/src/modules/ai/router.ts @@ -20,10 +20,13 @@ Te paso la conversación reciente. Los mensajes marcados con [NUEVO] acaban de l Reglas: - "addressed": el mensaje nuevo va dirigido a ese bot, aunque no lo nombre (le siguen el hilo, le responden, "¿y tú qué opinas?" justo después de que habló). +- Si la pregunta habla SOBRE otros bots (p. ej. "¿quién baila mejor entre ellos dos?"), va dirigida a quien venía conversando con la persona o a quien no se menciona, no a los mencionados. - "spontaneous": nadie le habla, pero el tema encaja claramente con su personalidad y aportaría algo. Úsalo con moderación: en un chat real la gente no opina de todo. - Si alguien invita al grupo a opinar, elige **un** bot como "addressed" según el historial. - Si las personas están hablando entre ellas de algo que no es para un bot, no responde nadie. -- Saludos genéricos, risas, "gracias", despedidas: normalmente nadie. +- Si alguien aclara que un mensaje era para un bot ("era para X", "iba para X"), ese bot responde ("addressed") a lo que se le había preguntado. +- Saludos al grupo ("hola?", "hay alguien?"): responde UN bot, el más natural. +- Risas, "gracias", despedidas: normalmente nadie. - Lo normal es que no responda ningún bot. Máximo 2 bots.`; export const ROUTER_SCHEMA = jsonSchema<{ speakers: Array<{ bot: string; kind: SpeakerKind; reason: string }> }>({ diff --git a/apps/api/src/modules/message/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation.service.ts index b3620f5..b227792 100644 --- a/apps/api/src/modules/message/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation.service.ts @@ -12,11 +12,26 @@ import { UserRepository } from '../user/user.repository.js'; import { MessageRepository } from './message.repository.js'; /** Silence after the last human message before the bots decide whether to speak. */ -const DEBOUNCE_MS = 2500; +const DEBOUNCE_MS = 1500; /** How many channel messages the router reads. */ const ROUTER_HISTORY_LIMIT = 20; /** How many channel messages a bot reads before replying. */ const AI_HISTORY_LIMIT = 30; +/** Typing speed a bot reply is paced at, so it appears roughly when a person would have finished typing it. */ +const TYPING_CHARS_PER_SECOND = 27; +const MAX_TYPING_MS = 5000; +/** Pause before a bot answers another bot's reply, as if reading it. */ +const READ_PAUSE_MS = 1200; + +/** How much longer the typing indicator stays once generation took `generationMs`. */ +export function typingDelayMs(length: number, generationMs: number): number { + return Math.max(0, Math.min((length / TYPING_CHARS_PER_SECOND) * 1000, MAX_TYPING_MS) - generationMs); +} + +async function sleep(ms: number): Promise { + // oxlint-disable-next-line promise/avoid-new -- a plain setTimeout, so Jest fake timers control it + return new Promise((resolve) => setTimeout(resolve, ms)); +} interface ChannelConversation { timer?: NodeJS.Timeout; @@ -115,9 +130,14 @@ export class BotConversationService implements OnModuleDestroy { named: readonly AIBot[], ): Promise { const spoken: AIBot[] = []; + // Destroy stops every remaining turn; a human interrupt only the turns of bots nobody named. + const silenced = (bot: AIBot) => this.destroyed || (conversation.interrupted && !named.includes(bot)); for (const [index, { bot, kind }] of speakers.entries()) { - if (this.destroyed) break; - if (conversation.interrupted && !named.includes(bot)) continue; + if (silenced(bot)) continue; + if (index > 0) { + await sleep(READ_PAUSE_MS); + if (silenced(bot)) continue; + } if (kind === 'spontaneous') { if (index > 0 && !(await this.stillPicked(channel, conversation, ids, bot))) continue; @@ -145,6 +165,10 @@ export class BotConversationService implements OnModuleDestroy { // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message may arrive while `route` runs if (!reaction || conversation.interrupted) return null; + await sleep(READ_PAUSE_MS); + // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message or destroy may happen during the pause + if (conversation.interrupted || this.destroyed) return null; + this.caps.record(channel.id, reaction.bot.user.id, Date.now()); return (await this.speak(channel, reaction.bot)) ? reaction.bot : null; } @@ -172,14 +196,21 @@ export class BotConversationService implements OnModuleDestroy { return !conversation.interrupted && (decision?.some((pick) => pick.bot === bot) ?? false); } - /** Generates, stores and broadcasts one reply. Returns whether a message was posted. */ + /** + * Generates, stores and broadcasts one reply. The typing indicator stays on until the reply is posted, paced like + * typing. Returns whether a message was posted. + */ private async speak(channel: Channel, bot: AIBot): Promise { this.presence.startBotTyping(bot.user.id, channel.id); + const startedAt = Date.now(); try { const history = await this.messageRepository.getRecentMessages(channel.id, AI_HISTORY_LIMIT); const content = await this.ai.reply(bot, history); if (!content) return false; + await sleep(typingDelayMs(content.length, Date.now() - startedAt)); + if (this.destroyed) return false; + const createdAt = new Date(); const reply = { content, channelId: channel.id, authorId: bot.user.id }; const { id } = await this.messageRepository.createMessage({ ...reply, createdAt }); diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 3baad7b..9d49af4 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -7,11 +7,16 @@ import type { PlannedSpeaker } from '@/modules/ai/router.js'; import type { Channel } from '@/modules/channel/channel.repository.js'; import type { PresenceService } from '@/modules/gateway/presence.service.js'; import type { SocketEmitter } from '@/modules/gateway/socket-emitter.js'; -import { BotConversationService } from '@/modules/message/bot-conversation.service.js'; +import { BotConversationService, typingDelayMs } from '@/modules/message/bot-conversation.service.js'; import type { MessageCreationAttributes, MessageRepository } from '@/modules/message/message.repository.js'; import type { UserRepository } from '@/modules/user/user.repository.js'; const [, chayanne, pedro] = BOTS; +const DEBOUNCE = 1500; +const READ_PAUSE = 1200; +/** How long the typing indicator holds `text` when generation was instant (fake timers), rounded up to whole ms. */ +const typing = (text: string) => Math.ceil(typingDelayMs(text.length, 0)); +const replyOf = (bot: PlannedSpeaker['bot']) => `reply from ${bot.user.username}`; const channel: Channel = { id: 'c1', guildId: 'g1', name: 'general', description: null, position: 0 }; type Route = ( @@ -60,9 +65,16 @@ function setup() { return id; }; - return { service, say, ai, route, reply, messages }; + return { service, say, ai, route, reply, messages, messageRepository }; } +describe('typingDelayMs', () => { + it('holds a 54-char message for 2 s when generation was instant', () => expect(typingDelayMs(54, 0)).toBe(2000)); + it('subtracts the generation time', () => expect(typingDelayMs(54, 1500)).toBe(500)); + it('caps at 5 s', () => expect(typingDelayMs(1000, 0)).toBe(5000)); + it('never goes negative', () => expect(typingDelayMs(10, 3000)).toBe(0)); +}); + describe('BotConversationService', () => { beforeAll(() => Logger.overrideLogger(false)); @@ -74,14 +86,14 @@ describe('BotConversationService', () => { jest.useRealTimers(); }); - it('waits for 2.5 s of silence before routing', async () => { + it('waits for 1.5 s of silence before routing', async () => { const { say, route } = setup(); say('hola'); await jest.advanceTimersByTimeAsync(1000); say('¿cómo están?'); - await jest.advanceTimersByTimeAsync(2499); + await jest.advanceTimersByTimeAsync(1499); expect(route).not.toHaveBeenCalled(); await jest.advanceTimersByTimeAsync(1); @@ -93,7 +105,7 @@ describe('BotConversationService', () => { route.mockResolvedValue(null); say('chayanne hola'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); expect(reply).toHaveBeenCalledTimes(1); expect(reply.mock.calls[0][0]).toBe(chayanne); @@ -107,7 +119,7 @@ describe('BotConversationService', () => { ]); say('¿quién baila mejor?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); const pedroHistory = reply.mock.calls[1][1]; @@ -124,7 +136,7 @@ describe('BotConversationService', () => { ]); say('¿qué música ponemos?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); expect(route).toHaveBeenCalledTimes(2); @@ -141,7 +153,7 @@ describe('BotConversationService', () => { reply.mockImplementationOnce(async () => firstReply.promise); say('¿quién baila mejor?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); expect(reply).toHaveBeenCalledTimes(1); say('esperen, otra cosa'); @@ -151,7 +163,7 @@ describe('BotConversationService', () => { expect(reply).toHaveBeenCalledTimes(1); expect(route).toHaveBeenCalledTimes(1); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); expect(route).toHaveBeenCalledTimes(2); expect(reply.mock.calls.map(([bot]) => bot)).not.toContain(pedro); }); @@ -163,10 +175,10 @@ describe('BotConversationService', () => { reply.mockImplementationOnce(async () => firstReply.promise); say('pedro y chayanne, ¿quién baila mejor?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); say('jaja'); firstReply.resolve('¡yo, mi amor!'); - await jest.advanceTimersByTimeAsync(0); + await jest.advanceTimersByTimeAsync(typing('¡yo, mi amor!') + READ_PAUSE); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); expect(route).toHaveBeenCalledTimes(1); @@ -178,7 +190,7 @@ describe('BotConversationService', () => { route.mockImplementationOnce(async () => decision.promise); say('chayanne, ¿qué cantas?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); expect(route).toHaveBeenCalledTimes(1); say('jaja'); @@ -195,10 +207,10 @@ describe('BotConversationService', () => { reply.mockImplementationOnce(async () => firstReply.promise); say('chayanne, ¿qué cantas?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); const h2 = say('otra cosa'); // H2's timer fires while the first cycle is still replying, so its cycle is queued behind it. - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); await jest.advanceTimersByTimeAsync(500); const h3 = say('y otra más'); @@ -206,7 +218,7 @@ describe('BotConversationService', () => { await jest.advanceTimersByTimeAsync(0); expect(route).toHaveBeenCalledTimes(1); - await jest.advanceTimersByTimeAsync(2499); + await jest.advanceTimersByTimeAsync(DEBOUNCE - 1); expect(route).toHaveBeenCalledTimes(1); await jest.advanceTimersByTimeAsync(1); @@ -226,7 +238,7 @@ describe('BotConversationService', () => { route.mockImplementationOnce(async () => reask.promise); say('chayanne, ¿qué series ves?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); expect(route).toHaveBeenCalledTimes(2); say('esperen'); @@ -246,7 +258,7 @@ describe('BotConversationService', () => { .mockResolvedValueOnce([{ bot: pedro, kind: 'spontaneous' }]); say('chayanne, ¿qué series ves?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); }); @@ -261,7 +273,7 @@ describe('BotConversationService', () => { .mockResolvedValueOnce([]); say('chayanne, ¿qué series ves?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); }); @@ -273,16 +285,34 @@ describe('BotConversationService', () => { ); say('vi The Last of Us anoche'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); await jest.advanceTimersByTimeAsync(60_000); say('qué buena serie'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); expect(route.mock.calls.filter(([, , exclude = []]) => exclude.length === 0)).toHaveLength(2); expect(reply).toHaveBeenCalledTimes(1); expect(reply.mock.calls[0][0]).toBe(pedro); }); + it('posts a reply only after its typing delay', async () => { + const { say, route, reply, messageRepository } = setup(); + route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); + // 54 characters: two seconds of typing. + reply.mockResolvedValueOnce('¡Hola, mi amor! Qué alegría verte por aquí esta noche.'); + + say('chayanne hola'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + expect(reply).toHaveBeenCalledTimes(1); + expect(messageRepository.createMessage).not.toHaveBeenCalled(); + + await jest.advanceTimersByTimeAsync(1999); + expect(messageRepository.createMessage).not.toHaveBeenCalled(); + + await jest.advanceTimersByTimeAsync(1); + expect(messageRepository.createMessage).toHaveBeenCalledTimes(1); + }); + it('does nothing after module destroy', async () => { const { service, say, route, reply } = setup(); @@ -299,7 +329,7 @@ describe('BotConversationService', () => { ai.routerEnabled = false; say('chayanne hola'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); expect(route).not.toHaveBeenCalled(); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); @@ -315,10 +345,10 @@ describe('BotConversationService', () => { reply.mockImplementationOnce(async () => firstReply.promise); say('¿quién baila mejor?'); - await jest.advanceTimersByTimeAsync(2500); + await jest.advanceTimersByTimeAsync(DEBOUNCE); service.onModuleDestroy(); firstReply.resolve('¡yo, mi amor!'); - await jest.advanceTimersByTimeAsync(0); + await jest.advanceTimersByTimeAsync(typing('¡yo, mi amor!') + READ_PAUSE); expect(reply).toHaveBeenCalledTimes(1); expect(route).toHaveBeenCalledTimes(1); From d7a5abcaf370098fb76a9db56545ff63dd712069 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 17:53:45 -0600 Subject: [PATCH 11/18] test(api): cover the bot pacing interrupt and destroy guards The named-bots interrupt test now lets pedro post and checks that the reaction never routes. The destroy test checks that nothing is posted after the typing delay. New tests cover a human message during the read pause: an unnamed second speaker is skipped, a named one still answers, and a reaction is skipped. Co-Authored-By: Claude Opus 5.5 --- apps/api/tests/bot-conversation.spec.ts | 49 ++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 9d49af4..0a3f0d5 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -182,6 +182,52 @@ describe('BotConversationService', () => { expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); expect(route).toHaveBeenCalledTimes(1); + + // Let pedro post, so the cycle reaches the reaction. + await jest.advanceTimersByTimeAsync(typing(replyOf(pedro))); + expect(route.mock.calls.filter(([, , exclude]) => exclude !== undefined)).toHaveLength(0); + }); + + it('a human message during the read pause skips an unnamed second speaker', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ]); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne))); + say('esperen'); + await jest.advanceTimersByTimeAsync(READ_PAUSE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + }); + + it('a human message during the read pause still lets a named second speaker answer', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); + + say('pedro, ¿y tú qué bailas?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne))); + say('jaja'); + await jest.advanceTimersByTimeAsync(READ_PAUSE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + }); + + it('a human message during the reaction read pause skips the reaction', async () => { + const { say, route, reply } = setup(); + route + .mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]) + .mockResolvedValueOnce([{ bot: pedro, kind: 'spontaneous' }]); + + say('chayanne, ¿qué música ponemos?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne))); + expect(route).toHaveBeenCalledTimes(2); + say('esperen'); + await jest.advanceTimersByTimeAsync(READ_PAUSE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); }); it('a human message while routing still lets the named bot answer', async () => { @@ -336,7 +382,7 @@ describe('BotConversationService', () => { }); it('stops a running cycle on module destroy', async () => { - const { service, say, route, reply } = setup(); + const { service, say, route, reply, messageRepository } = setup(); route.mockResolvedValueOnce([ { bot: chayanne, kind: 'addressed' }, { bot: pedro, kind: 'addressed' }, @@ -352,5 +398,6 @@ describe('BotConversationService', () => { expect(reply).toHaveBeenCalledTimes(1); expect(route).toHaveBeenCalledTimes(1); + expect(messageRepository.createMessage).not.toHaveBeenCalled(); }); }); From 988037d4bd1b42e2bd1f80b8ed32b8ab31ecb9a4 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 18:04:54 -0600 Subject: [PATCH 12/18] feat(api): reply in parallel when several bots are asked Live tests felt slow: a reply took 6.5-8 s, and a second bot asked the same question came 4 s after the first. Pacing: the debounce drops from 1.5 s to 0.6 s, typing speed goes from 27 to 40 chars/s and the typing cap from 5 s to 3 s. The read pause stays at 1.2 s and now only applies to sequential turns. Parallel rule: the leading run of addressed speakers (named or picked as addressed by the router) starts at once, each with its own typing indicator and history, and each posts when its own reply is typed. No read pause inside the run. Interrupt and destroy rules apply per speaker. Spontaneous speakers keep their sequential turns after the run (read pause, recheck, re-ask, cap) and see its replies; the reaction still comes last. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- .../message/bot-conversation.service.ts | 42 ++++-- apps/api/tests/bot-conversation.spec.ts | 130 ++++++++++++++---- 3 files changed, 137 insertions(+), 37 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a6a4c56..63903ee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 1.5 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add at most 2 unnamed speakers; speakers take turns, each reply generated from the last 30 messages so it sees the previous one; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a bot pauses to read before answering another bot. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add at most 2 unnamed speakers. Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. ## Web architecture (`apps/web`) diff --git a/apps/api/src/modules/message/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation.service.ts index b227792..b351dca 100644 --- a/apps/api/src/modules/message/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation.service.ts @@ -12,15 +12,15 @@ import { UserRepository } from '../user/user.repository.js'; import { MessageRepository } from './message.repository.js'; /** Silence after the last human message before the bots decide whether to speak. */ -const DEBOUNCE_MS = 1500; +const DEBOUNCE_MS = 600; /** How many channel messages the router reads. */ const ROUTER_HISTORY_LIMIT = 20; /** How many channel messages a bot reads before replying. */ const AI_HISTORY_LIMIT = 30; /** Typing speed a bot reply is paced at, so it appears roughly when a person would have finished typing it. */ -const TYPING_CHARS_PER_SECOND = 27; -const MAX_TYPING_MS = 5000; -/** Pause before a bot answers another bot's reply, as if reading it. */ +const TYPING_CHARS_PER_SECOND = 40; +const MAX_TYPING_MS = 3000; +/** Pause before a spontaneous bot answers the replies before it, as if reading them. */ const READ_PAUSE_MS = 1200; /** How much longer the typing indicator stays once generation took `generationMs`. */ @@ -44,7 +44,8 @@ interface ChannelConversation { /** * Decides which bots join a channel conversation. Human messages are debounced; then the router (or, without it, - * the bots named in the new messages) picks the speakers, who reply in turns, and one other bot may react. + * the bots named in the new messages) picks the speakers: the addressed ones reply at once, the spontaneous ones in + * turns after them, and one other bot may react. */ @Injectable() export class BotConversationService implements OnModuleDestroy { @@ -119,8 +120,9 @@ export class BotConversationService implements OnModuleDestroy { } /** - * The planned speakers reply one after another. A human interrupt skips the remaining bots nobody named, but named - * bots still answer. Returns who posted a reply. + * The leading addressed speakers reply in parallel; the speakers after them reply one after another. A human + * interrupt skips the bots nobody named that have not started yet, but named bots still answer. Returns who posted + * a reply. */ private async takeTurns( channel: Channel, @@ -129,10 +131,14 @@ export class BotConversationService implements OnModuleDestroy { speakers: PlannedSpeaker[], named: readonly AIBot[], ): Promise { - const spoken: AIBot[] = []; // Destroy stops every remaining turn; a human interrupt only the turns of bots nobody named. const silenced = (bot: AIBot) => this.destroyed || (conversation.interrupted && !named.includes(bot)); - for (const [index, { bot, kind }] of speakers.entries()) { + const firstTurn = speakers.findIndex(({ kind }) => kind !== 'addressed'); + const together = firstTurn === -1 ? speakers.length : firstTurn; + const spoken = await this.replyTogether(channel, speakers.slice(0, together), silenced); + + for (const [offset, { bot, kind }] of speakers.slice(together).entries()) { + const index = together + offset; if (silenced(bot)) continue; if (index > 0) { await sleep(READ_PAUSE_MS); @@ -148,6 +154,24 @@ export class BotConversationService implements OnModuleDestroy { return spoken; } + /** + * Every speaker starts at once, so none sees the others' replies, and each posts as soon as its own reply is typed. + * Returns who posted, in posting order. + */ + private async replyTogether( + channel: Channel, + speakers: PlannedSpeaker[], + silenced: (bot: AIBot) => boolean, + ): Promise { + const spoken: AIBot[] = []; + await Promise.all( + speakers.map(async ({ bot }) => { + if (!silenced(bot) && (await this.speak(channel, bot))) spoken.push(bot); + }), + ); + return spoken; + } + /** At most one bot that has not spoken yet reacts to the replies, under its spontaneous cap. */ private async react( channel: Channel, diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 0a3f0d5..0b6d521 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -12,7 +12,7 @@ import type { MessageCreationAttributes, MessageRepository } from '@/modules/mes import type { UserRepository } from '@/modules/user/user.repository.js'; const [, chayanne, pedro] = BOTS; -const DEBOUNCE = 1500; +const DEBOUNCE = 600; const READ_PAUSE = 1200; /** How long the typing indicator holds `text` when generation was instant (fake timers), rounded up to whole ms. */ const typing = (text: string) => Math.ceil(typingDelayMs(text.length, 0)); @@ -69,9 +69,9 @@ function setup() { } describe('typingDelayMs', () => { - it('holds a 54-char message for 2 s when generation was instant', () => expect(typingDelayMs(54, 0)).toBe(2000)); - it('subtracts the generation time', () => expect(typingDelayMs(54, 1500)).toBe(500)); - it('caps at 5 s', () => expect(typingDelayMs(1000, 0)).toBe(5000)); + it('holds an 80-char message for 2 s when generation was instant', () => expect(typingDelayMs(80, 0)).toBe(2000)); + it('subtracts the generation time', () => expect(typingDelayMs(80, 1500)).toBe(500)); + it('caps at 3 s', () => expect(typingDelayMs(1000, 0)).toBe(3000)); it('never goes negative', () => expect(typingDelayMs(10, 3000)).toBe(0)); }); @@ -86,14 +86,14 @@ describe('BotConversationService', () => { jest.useRealTimers(); }); - it('waits for 1.5 s of silence before routing', async () => { + it('waits for 0.6 s of silence before routing', async () => { const { say, route } = setup(); say('hola'); - await jest.advanceTimersByTimeAsync(1000); + await jest.advanceTimersByTimeAsync(500); say('¿cómo están?'); - await jest.advanceTimersByTimeAsync(1499); + await jest.advanceTimersByTimeAsync(599); expect(route).not.toHaveBeenCalled(); await jest.advanceTimersByTimeAsync(1); @@ -111,16 +111,46 @@ describe('BotConversationService', () => { expect(reply.mock.calls[0][0]).toBe(chayanne); }); - it('second speaker sees the first reply', async () => { - const { say, route, reply } = setup(); + it('two addressed speakers reply in parallel', async () => { + const { say, route, reply, messageRepository } = setup(); + // Pedro is planned first but types the longer reply, so chayanne posts first. route.mockResolvedValueOnce([ - { bot: chayanne, kind: 'addressed' }, { bot: pedro, kind: 'addressed' }, + { bot: chayanne, kind: 'addressed' }, ]); + const authors = () => messageRepository.createMessage.mock.calls.map(([message]) => message.authorId); say('¿quién baila mejor?'); - await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([pedro, chayanne]); + expect(messageRepository.createMessage).not.toHaveBeenCalled(); + for (const [bot, history] of reply.mock.calls) { + const other = bot === pedro ? chayanne : pedro; + expect(history).not.toContainEqual(expect.objectContaining({ authorId: other.user.id })); + } + await jest.advanceTimersByTimeAsync(typing(replyOf(chayanne))); + expect(authors()).toEqual([chayanne.user.id]); + + await jest.advanceTimersByTimeAsync(typing(replyOf(pedro)) - typing(replyOf(chayanne))); + expect(authors()).toEqual([chayanne.user.id, pedro.user.id]); + }); + + it('a spontaneous speaker after an addressed one stays sequential and sees it', async () => { + const { say, route, reply } = setup(); + route + .mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]) + .mockResolvedValueOnce([{ bot: pedro, kind: 'spontaneous' }]); + + say('chayanne, ¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE - 1); + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + + await jest.advanceTimersByTimeAsync(1); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); const pedroHistory = reply.mock.calls[1][1]; expect(pedroHistory).toContainEqual( @@ -147,7 +177,7 @@ describe('BotConversationService', () => { const { say, route, reply } = setup(); route.mockResolvedValueOnce([ { bot: chayanne, kind: 'addressed' }, - { bot: pedro, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, ]); const firstReply = Promise.withResolvers(); reply.mockImplementationOnce(async () => firstReply.promise); @@ -169,7 +199,7 @@ describe('BotConversationService', () => { }); it('a human message during a reply still lets the named bots answer, but skips the reaction', async () => { - const { say, route, reply } = setup(); + const { say, route, reply, messageRepository } = setup(); route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); const firstReply = Promise.withResolvers(); reply.mockImplementationOnce(async () => firstReply.promise); @@ -178,41 +208,70 @@ describe('BotConversationService', () => { await jest.advanceTimersByTimeAsync(DEBOUNCE); say('jaja'); firstReply.resolve('¡yo, mi amor!'); - await jest.advanceTimersByTimeAsync(typing('¡yo, mi amor!') + READ_PAUSE); + // Both have posted, and the 'jaja' debounce has not fired yet. + await jest.advanceTimersByTimeAsync(Math.max(typing('¡yo, mi amor!'), typing(replyOf(pedro)))); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + expect(messageRepository.createMessage).toHaveBeenCalledTimes(2); expect(route).toHaveBeenCalledTimes(1); + expect(route.mock.calls.filter(([, , exclude]) => exclude !== undefined)).toHaveLength(0); + }); + + it('a human message during parallel generation still posts both addressed replies, then skips the reaction', async () => { + const { say, route, reply, messageRepository } = setup(); + const chayanneReply = Promise.withResolvers(); + const pedroReply = Promise.withResolvers(); + reply + .mockImplementationOnce(async () => chayanneReply.promise) + .mockImplementationOnce(async () => pedroReply.promise); - // Let pedro post, so the cycle reaches the reaction. - await jest.advanceTimersByTimeAsync(typing(replyOf(pedro))); + say('chayanne y pedro, ¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + + say('jaja'); + chayanneReply.resolve('¡yo, mi amor!'); + pedroReply.resolve('yo no bailo'); + await jest.advanceTimersByTimeAsync(Math.max(typing('¡yo, mi amor!'), typing('yo no bailo'))); + + // Completion order: pedro's shorter reply posts first. + expect(messageRepository.createMessage.mock.calls.map(([message]) => message.authorId)).toEqual([ + pedro.user.id, + chayanne.user.id, + ]); + expect(route).toHaveBeenCalledTimes(1); expect(route.mock.calls.filter(([, , exclude]) => exclude !== undefined)).toHaveLength(0); }); - it('a human message during the read pause skips an unnamed second speaker', async () => { + it('a human message during the read pause skips an unnamed spontaneous speaker', async () => { const { say, route, reply } = setup(); route.mockResolvedValueOnce([ { bot: chayanne, kind: 'addressed' }, - { bot: pedro, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, ]); - say('¿quién baila mejor?'); + const first = say('¿quién baila mejor?'); await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne))); say('esperen'); await jest.advanceTimersByTimeAsync(READ_PAUSE); expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne]); + // Skipped right after the pause, without re-asking the router. + expect(route.mock.calls.filter(([, ids]) => ids.has(first))).toHaveLength(1); }); - it('a human message during the read pause still lets a named second speaker answer', async () => { + it('a human message while routing skips an unnamed addressed speaker but lets a named one answer', async () => { const { say, route, reply } = setup(); - route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); + const decision = Promise.withResolvers(); + route.mockImplementationOnce(async () => decision.promise); say('pedro, ¿y tú qué bailas?'); - await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne))); + await jest.advanceTimersByTimeAsync(DEBOUNCE); say('jaja'); - await jest.advanceTimersByTimeAsync(READ_PAUSE); + decision.resolve([{ bot: chayanne, kind: 'addressed' }]); + await jest.advanceTimersByTimeAsync(0); - expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([pedro]); }); it('a human message during the reaction read pause skips the reaction', async () => { @@ -344,7 +403,7 @@ describe('BotConversationService', () => { it('posts a reply only after its typing delay', async () => { const { say, route, reply, messageRepository } = setup(); route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); - // 54 characters: two seconds of typing. + // 54 characters: 1.35 seconds of typing. reply.mockResolvedValueOnce('¡Hola, mi amor! Qué alegría verte por aquí esta noche.'); say('chayanne hola'); @@ -352,7 +411,7 @@ describe('BotConversationService', () => { expect(reply).toHaveBeenCalledTimes(1); expect(messageRepository.createMessage).not.toHaveBeenCalled(); - await jest.advanceTimersByTimeAsync(1999); + await jest.advanceTimersByTimeAsync(1349); expect(messageRepository.createMessage).not.toHaveBeenCalled(); await jest.advanceTimersByTimeAsync(1); @@ -385,7 +444,7 @@ describe('BotConversationService', () => { const { service, say, route, reply, messageRepository } = setup(); route.mockResolvedValueOnce([ { bot: chayanne, kind: 'addressed' }, - { bot: pedro, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, ]); const firstReply = Promise.withResolvers(); reply.mockImplementationOnce(async () => firstReply.promise); @@ -400,4 +459,21 @@ describe('BotConversationService', () => { expect(route).toHaveBeenCalledTimes(1); expect(messageRepository.createMessage).not.toHaveBeenCalled(); }); + + it('posts nothing from a parallel run after module destroy', async () => { + const { service, say, route, reply, messageRepository } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'addressed' }, + ]); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + expect(reply).toHaveBeenCalledTimes(2); + + service.onModuleDestroy(); + await jest.advanceTimersByTimeAsync(Math.max(typing(replyOf(chayanne)), typing(replyOf(pedro)))); + + expect(messageRepository.createMessage).not.toHaveBeenCalled(); + }); }); From 17ad10bf6f29c9f49ec4b763da5e5962e5232bce Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 22:18:16 -0600 Subject: [PATCH 13/18] refactor(api): move bot conversation into its own folder Pacing constants, history limits and helpers move out of the service into bot-conversation.util.ts, and both files live in message/bot-conversation/. Also drops the comment and leading blank line of CHAT_STYLE. Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/bots.ts | 2 - .../bot-conversation.service.ts | 80 ++++++++----------- .../bot-conversation/bot-conversation.util.ts | 30 +++++++ .../api/src/modules/message/message.module.ts | 2 +- .../src/modules/message/message.service.ts | 2 +- apps/api/tests/bot-conversation.spec.ts | 3 +- 6 files changed, 66 insertions(+), 53 deletions(-) rename apps/api/src/modules/message/{ => bot-conversation}/bot-conversation.service.ts (82%) create mode 100644 apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 9f822f7..72c2a6a 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -10,9 +10,7 @@ export interface AIBot { temperature: number; } -/** Appended to every persona: the bots write like people in a group chat. */ export const CHAT_STYLE = ` - Estás en un chat de grupo con amigos. Escribe como una persona real en un chat: - Normalmente 1 o 2 frases cortas. Solo te extiendes si te piden explicar algo. - Tono casual y natural; algún emoji si sale solo, sin abusar. diff --git a/apps/api/src/modules/message/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts similarity index 82% rename from apps/api/src/modules/message/bot-conversation.service.ts rename to apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts index b351dca..a7cad5a 100644 --- a/apps/api/src/modules/message/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts @@ -1,46 +1,24 @@ import { SocketEvents } from '@fidechat/shared'; import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; -import { AIService } from '../ai/ai.service.js'; -import { type AIBot, detectNamedBots } from '../ai/bots.js'; -import { type PlannedSpeaker, planSpeakers } from '../ai/router.js'; -import { SpontaneousCaps } from '../ai/spontaneous-caps.js'; -import type { Channel } from '../channel/channel.repository.js'; -import { PresenceService } from '../gateway/presence.service.js'; -import { SocketEmitter } from '../gateway/socket-emitter.js'; -import { UserRepository } from '../user/user.repository.js'; -import { MessageRepository } from './message.repository.js'; - -/** Silence after the last human message before the bots decide whether to speak. */ -const DEBOUNCE_MS = 600; -/** How many channel messages the router reads. */ -const ROUTER_HISTORY_LIMIT = 20; -/** How many channel messages a bot reads before replying. */ -const AI_HISTORY_LIMIT = 30; -/** Typing speed a bot reply is paced at, so it appears roughly when a person would have finished typing it. */ -const TYPING_CHARS_PER_SECOND = 40; -const MAX_TYPING_MS = 3000; -/** Pause before a spontaneous bot answers the replies before it, as if reading them. */ -const READ_PAUSE_MS = 1200; - -/** How much longer the typing indicator stays once generation took `generationMs`. */ -export function typingDelayMs(length: number, generationMs: number): number { - return Math.max(0, Math.min((length / TYPING_CHARS_PER_SECOND) * 1000, MAX_TYPING_MS) - generationMs); -} - -async function sleep(ms: number): Promise { - // oxlint-disable-next-line promise/avoid-new -- a plain setTimeout, so Jest fake timers control it - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -interface ChannelConversation { - timer?: NodeJS.Timeout; - pending: Array<{ id: string; content: string }>; - /** Set by every human message, so a running cycle skips the remaining turns of bots nobody named. */ - interrupted: boolean; - /** Cycles run one after another, never overlapping. */ - chain: Promise; -} +import { AIService } from '../../ai/ai.service.js'; +import { type AIBot, detectNamedBots } from '../../ai/bots.js'; +import { type PlannedSpeaker, planSpeakers } from '../../ai/router.js'; +import { SpontaneousCaps } from '../../ai/spontaneous-caps.js'; +import type { Channel } from '../../channel/channel.repository.js'; +import { PresenceService } from '../../gateway/presence.service.js'; +import { SocketEmitter } from '../../gateway/socket-emitter.js'; +import { UserRepository } from '../../user/user.repository.js'; +import { MessageRepository } from '../message.repository.js'; +import { + AI_HISTORY_LIMIT, + type ChannelConversation, + DEBOUNCE_MS, + READ_PAUSE_MS, + ROUTER_HISTORY_LIMIT, + sleep, + typingDelayMs, +} from './bot-conversation.util.js'; /** * Decides which bots join a channel conversation. Human messages are debounced; then the router (or, without it, @@ -62,7 +40,18 @@ export class BotConversationService implements OnModuleDestroy { private readonly emitter: SocketEmitter, ) {} - /** Only human messages reach this, so named-bot detection never runs over bot replies. */ + onModuleDestroy(): void { + this.destroyed = true; + for (const conversation of this.conversations.values()) { + clearTimeout(conversation.timer); + } + } + + /** + * Only human messages reach this, so named-bot detection never runs over bot replies. + * + * Note: this is channel scoped btw + */ onHumanMessage(channel: Channel, messageId: string, content: string): void { if (!this.ai.enabled || this.destroyed) return; @@ -75,20 +64,15 @@ export class BotConversationService implements OnModuleDestroy { conversation.pending.push({ id: messageId, content }); conversation.interrupted = true; + clearTimeout(conversation.timer); + conversation.timer = setTimeout(() => { conversation.timer = undefined; conversation.chain = conversation.chain.then(async () => this.runCycle(channel, conversation)); }, DEBOUNCE_MS); } - onModuleDestroy(): void { - this.destroyed = true; - for (const conversation of this.conversations.values()) { - clearTimeout(conversation.timer); - } - } - private async runCycle(channel: Channel, conversation: ChannelConversation): Promise { // A newer message's timer will queue its own cycle, and that one takes the pending messages after their silence. if (this.destroyed || conversation.timer !== undefined || conversation.pending.length === 0) return; diff --git a/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts new file mode 100644 index 0000000..73bd748 --- /dev/null +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts @@ -0,0 +1,30 @@ +/** Silence after the last human message before the bots decide whether to speak. */ +export const DEBOUNCE_MS = 600; +/** How many channel messages the router reads. */ +export const ROUTER_HISTORY_LIMIT = 20; +/** How many channel messages a bot reads before replying. */ +export const AI_HISTORY_LIMIT = 30; +/** Typing speed a bot reply is paced at, so it appears roughly when a person would have finished typing it. */ +const TYPING_CHARS_PER_SECOND = 40; +const MAX_TYPING_MS = 3000; +/** Pause before a spontaneous bot answers the replies before it, as if reading them. */ +export const READ_PAUSE_MS = 1200; + +/** How much longer the typing indicator stays once generation took `generationMs`. */ +export function typingDelayMs(length: number, generationMs: number): number { + return Math.max(0, Math.min((length / TYPING_CHARS_PER_SECOND) * 1000, MAX_TYPING_MS) - generationMs); +} + +export async function sleep(ms: number): Promise { + // oxlint-disable-next-line promise/avoid-new -- a plain setTimeout, so Jest fake timers control it + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +export interface ChannelConversation { + timer?: NodeJS.Timeout; + pending: Array<{ id: string; content: string }>; + /** Set by every human message, so a running cycle skips the remaining turns of bots nobody named. */ + interrupted: boolean; + /** Cycles run one after another, never overlapping. */ + chain: Promise; +} diff --git a/apps/api/src/modules/message/message.module.ts b/apps/api/src/modules/message/message.module.ts index dc7334e..530dc1a 100644 --- a/apps/api/src/modules/message/message.module.ts +++ b/apps/api/src/modules/message/message.module.ts @@ -4,7 +4,7 @@ import { AIModule } from '../ai/ai.module.js'; import { ChannelModule } from '../channel/channel.module.js'; import { GatewayModule } from '../gateway/gateway.module.js'; import { UserModule } from '../user/user.module.js'; -import { BotConversationService } from './bot-conversation.service.js'; +import { BotConversationService } from './bot-conversation/bot-conversation.service.js'; import { MessageController } from './message.controller.js'; import { MessageRepository } from './message.repository.js'; import { MessageService } from './message.service.js'; diff --git a/apps/api/src/modules/message/message.service.ts b/apps/api/src/modules/message/message.service.ts index a14fa20..9e70d7a 100644 --- a/apps/api/src/modules/message/message.service.ts +++ b/apps/api/src/modules/message/message.service.ts @@ -4,7 +4,7 @@ import { HttpException, Injectable, InternalServerErrorException, NotFoundExcept import { ChannelRepository } from '../channel/channel.repository.js'; import { SocketEmitter } from '../gateway/socket-emitter.js'; import { UserRepository } from '../user/user.repository.js'; -import { BotConversationService } from './bot-conversation.service.js'; +import { BotConversationService } from './bot-conversation/bot-conversation.service.js'; import { type MessageCreationAttributes, MessageRepository } from './message.repository.js'; @Injectable() diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 0b6d521..6c1dde8 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -7,7 +7,8 @@ import type { PlannedSpeaker } from '@/modules/ai/router.js'; import type { Channel } from '@/modules/channel/channel.repository.js'; import type { PresenceService } from '@/modules/gateway/presence.service.js'; import type { SocketEmitter } from '@/modules/gateway/socket-emitter.js'; -import { BotConversationService, typingDelayMs } from '@/modules/message/bot-conversation.service.js'; +import { BotConversationService } from '@/modules/message/bot-conversation/bot-conversation.service.js'; +import { typingDelayMs } from '@/modules/message/bot-conversation/bot-conversation.util.js'; import type { MessageCreationAttributes, MessageRepository } from '@/modules/message/message.repository.js'; import type { UserRepository } from '@/modules/user/user.repository.js'; From 49c5f57563a3e31afc8fc4d0457801340413868e Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 22:18:16 -0600 Subject: [PATCH 14/18] docs(monorepo): explain the ai bot orchestration in the README Co-Authored-By: Claude Opus 5.5 --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index d205f6b..0ec1007 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,31 @@ the demo users and data live in `docker/`. > recommended OpenAI model) and `OPENAI_API_KEY` to use OpenAI instead, or > `AI_MODEL=ollama:` (no key) to use an Ollama running on the host. +## How the AI bots join a conversation + +The bots don't answer every message. After a human writes, an LLM router reads +the channel and decides who, if anyone, should speak, like a person in a group +chat would (`BotConversationService` in `apps/api`). + +```mermaid +flowchart TD + A[Human message] --> B[Wait 0.6 s of channel silence] + B --> C{Router reads the last 20 messages} + C -->|nobody| Z[No reply] + C --> D[Bots spoken to reply in parallel
a named bot always answers] + D --> E[A bot joining on its own replies next,
after reading the others] + E --> F[At most one other bot may react] +``` + +- Replies are short chat messages, posted after a typing delay that matches + their length. +- A bot joins on its own at most once every 10 minutes per channel; this cap + lives in code, not in the prompt. +- A new human message cuts the cycle short: bots nobody named stop, named bots + still answer, and the router decides again. +- With `AI_ROUTER=off`, or if the router fails, only bots named in the message + answer. + ## Infrastructure services The compose files live in `apps/api` and are still run from there: From 5aae1279995473512ea167dbe70981b32f10b261 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 22:54:01 -0600 Subject: [PATCH 15/18] fix(api): address the bot conversation review - Presence tracks every channel a bot is typing in, so a bot typing in two channels keeps its indicator until both replies are posted. - The spontaneous cap is only used up by a reply that was actually posted. - A reply cut off by the token limit is trimmed to its last full sentence. - compose.yaml passes AI_ROUTER through; the router history size lives in one constant; AGENTS.md states the real speaker limit (2 in total, more only when more bots are named) and the README notes that the router sends channel history to the provider. - BotConversationService passes one BotCycle object around instead of many parameters, and helpers used once are inlined. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- README.md | 3 + apps/api/src/modules/ai/ai.service.ts | 6 +- apps/api/src/modules/ai/prompt.ts | 5 + apps/api/src/modules/ai/router.ts | 7 +- .../src/modules/gateway/presence.service.ts | 28 +-- .../bot-conversation.service.ts | 163 ++++++++---------- .../bot-conversation/bot-conversation.util.ts | 17 +- apps/api/tests/ai-bots.spec.ts | 11 +- apps/api/tests/bot-conversation.spec.ts | 32 ++++ apps/api/tests/presence.spec.ts | 43 +++++ compose.yaml | 2 + 12 files changed, 209 insertions(+), 110 deletions(-) create mode 100644 apps/api/tests/presence.spec.ts diff --git a/AGENTS.md b/AGENTS.md index 63903ee..a7d9193 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add at most 2 unnamed speakers. Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add unnamed speakers up to 2 speakers in total (with 3 bots named, all 3 answer and none is added). Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. ## Web architecture (`apps/web`) diff --git a/README.md b/README.md index 0ec1007..751ae7d 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,9 @@ flowchart TD still answer, and the router decides again. - With `AI_ROUTER=off`, or if the router fails, only bots named in the message answer. +- With the router on, every human message sends the last 20 messages of its + channel to the model provider, even when no bot is named. Fine for a demo; a + real deployment would want a per-guild opt-in. ## Infrastructure services diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index 570c039..bef2505 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -7,7 +7,7 @@ import { createProviderRegistry, generateText, Output } from 'ai'; import { UserRepository } from '../user/user.repository.js'; import { type AIBot, BOTS } from './bots.js'; -import { buildPromptMessages, type ChannelHistoryEntry, cleanReply } from './prompt.js'; +import { buildPromptMessages, type ChannelHistoryEntry, cleanReply, trimToLastSentence } from './prompt.js'; import { buildRouterPrompt, type PlannedSpeaker, ROUTER_INSTRUCTIONS, ROUTER_SCHEMA } from './router.js'; // The SDK prints warnings with console.warn, outside pino; `reply` logs them through the Nest logger instead. @@ -93,10 +93,12 @@ export class AIService implements OnModuleInit { this.logger.debug(`AI SDK warning: ${JSON.stringify(warning)}`); } - const reply = cleanReply(bot, text); + let reply = cleanReply(bot, text); if (!reply || finishReason === 'length') { this.logger.warn(`${bot.user.username} reply finished with ${finishReason} (${reply.length} chars)`); } + // A reply cut off by maxOutputTokens would end mid-sentence. + if (finishReason === 'length') reply = trimToLastSentence(reply); return reply || null; } diff --git a/apps/api/src/modules/ai/prompt.ts b/apps/api/src/modules/ai/prompt.ts index 104f205..46fdcd7 100644 --- a/apps/api/src/modules/ai/prompt.ts +++ b/apps/api/src/modules/ai/prompt.ts @@ -40,3 +40,8 @@ export function cleanReply(bot: AIBot, text: string): string { } return reply.slice(0, MAX_MESSAGE_LENGTH); } + +/** Cuts a reply that hit the token limit back to its last finished sentence; empty when none finished. */ +export function trimToLastSentence(text: string): string { + return /^[\s\S]*[.!?…](?=\s|$)/.exec(text)?.[0] ?? ''; +} diff --git a/apps/api/src/modules/ai/router.ts b/apps/api/src/modules/ai/router.ts index 8420f65..618cf72 100644 --- a/apps/api/src/modules/ai/router.ts +++ b/apps/api/src/modules/ai/router.ts @@ -10,7 +10,8 @@ export interface PlannedSpeaker { kind: SpeakerKind; } -const ROUTER_HISTORY_SIZE = 20; +/** How many channel messages the router reads. */ +export const ROUTER_HISTORY_LIMIT = 20; const MAX_UNNAMED_SPEAKERS = 2; export const ROUTER_INSTRUCTIONS = `Eres el moderador invisible de un chat de grupo donde conviven personas y estos bots: @@ -57,7 +58,7 @@ export function buildRouterPrompt( exclude: readonly AIBot[] = [], ): string { const lines = history - .slice(-ROUTER_HISTORY_SIZE) + .slice(-ROUTER_HISTORY_LIMIT) .map((entry) => `${newIds.has(entry.id) ? '[NUEVO] ' : ''}${entry.authorName}: ${entry.content}`); if (exclude.length === 0) return lines.join('\n'); @@ -71,7 +72,7 @@ export function buildRouterPrompt( */ export function planSpeakers( decision: PlannedSpeaker[] | null, - named: AIBot[], + named: readonly AIBot[], canSpeakSpontaneously: (bot: AIBot) => boolean, ): PlannedSpeaker[] { const picks = decision ?? []; diff --git a/apps/api/src/modules/gateway/presence.service.ts b/apps/api/src/modules/gateway/presence.service.ts index 948185c..4232920 100644 --- a/apps/api/src/modules/gateway/presence.service.ts +++ b/apps/api/src/modules/gateway/presence.service.ts @@ -13,6 +13,8 @@ import { parseStatusUpdate } from './status-update.util.js'; export class PresenceService { private readonly logger = new Logger(PresenceService.name); readonly #activeUsers = new Map>(); + /** Channels each bot is typing in, oldest first; presence shows the latest one. */ + readonly #botTyping = new Map(); constructor(private readonly emitter: SocketEmitter) { for (const { user } of BOTS) { @@ -75,27 +77,31 @@ export class PresenceService { return; } - this.#activeUsers.set(user.id, { - status: 'online', - isTyping: true, - typingInChannel: channelId, - }); - this.broadcast(); + this.#botTyping.set(user.id, [...(this.#botTyping.get(user.id) ?? []), channelId]); + this.setBotTyping(user.id); } - /** Only clears the indicator if the bot is still typing in `channelId`, not already in another channel. */ + /** A bot can type in several channels at once: the indicator moves to another one until all have finished. */ public stopBotTyping(botId: string, channelId: string) { const user = BOTS.find((bot) => bot.user.id === botId)?.user; if (!user) { this.logger.error(`IA user with ID ${botId} not found`); return; } - if (this.#activeUsers.get(user.id)?.typingInChannel !== channelId) return; + const channels = this.#botTyping.get(user.id) ?? []; + const index = channels.indexOf(channelId); + if (index === -1) return; - this.#activeUsers.set(user.id, { + this.#botTyping.set(user.id, channels.toSpliced(index, 1)); + this.setBotTyping(user.id); + } + + private setBotTyping(botId: string) { + const channelId = this.#botTyping.get(botId)?.at(-1); + this.#activeUsers.set(botId, { status: 'online', - isTyping: false, - typingInChannel: undefined, + isTyping: channelId !== undefined, + typingInChannel: channelId, }); this.broadcast(); } diff --git a/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts index a7cad5a..139ac6d 100644 --- a/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts @@ -3,7 +3,7 @@ import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; import { AIService } from '../../ai/ai.service.js'; import { type AIBot, detectNamedBots } from '../../ai/bots.js'; -import { type PlannedSpeaker, planSpeakers } from '../../ai/router.js'; +import { type PlannedSpeaker, planSpeakers, ROUTER_HISTORY_LIMIT } from '../../ai/router.js'; import { SpontaneousCaps } from '../../ai/spontaneous-caps.js'; import type { Channel } from '../../channel/channel.repository.js'; import { PresenceService } from '../../gateway/presence.service.js'; @@ -12,10 +12,10 @@ import { UserRepository } from '../../user/user.repository.js'; import { MessageRepository } from '../message.repository.js'; import { AI_HISTORY_LIMIT, + type BotCycle, type ChannelConversation, DEBOUNCE_MS, READ_PAUSE_MS, - ROUTER_HISTORY_LIMIT, sleep, typingDelayMs, } from './bot-conversation.util.js'; @@ -81,89 +81,90 @@ export class BotConversationService implements OnModuleDestroy { conversation.pending = []; conversation.interrupted = false; + const cycle: BotCycle = { + channel, + conversation, + ids: new Set(pending.map((message) => message.id)), + named: [...new Set(pending.flatMap((message) => detectNamedBots(message.content)))], + spoken: [], + }; + try { - const ids = new Set(pending.map((message) => message.id)); - const named = [...new Set(pending.flatMap((message) => detectNamedBots(message.content)))]; const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); - - const decision = this.ai.routerEnabled ? await this.ai.route(history, ids) : null; - const speakers = planSpeakers(decision, named, (bot) => + const decision = this.ai.routerEnabled ? await this.ai.route(history, cycle.ids) : null; + const speakers = planSpeakers(decision, cycle.named, (bot) => this.caps.canSpeak(channel.id, bot.user.id, Date.now()), ); - const spoken = await this.takeTurns(channel, conversation, ids, speakers, named); - if (decision !== null && spoken.length > 0) { - const reactor = await this.react(channel, conversation, ids, spoken); - if (reactor) spoken.push(reactor); - } + await this.takeTurns(cycle, speakers); + if (decision !== null && cycle.spoken.length > 0) await this.react(cycle); - this.logCycle(channel, decision, named, spoken); + const names = (bots: readonly AIBot[]) => bots.map((bot) => bot.user.username).join(', ') || 'none'; + const picks = + decision === null + ? 'unavailable' + : decision.map(({ bot, kind }) => `${bot.user.username} (${kind})`).join(', ') || 'none'; + this.logger.debug( + `Channel ${channel.id}: router ${picks}, named ${names(cycle.named)}, spoke ${names(cycle.spoken)}`, + ); } catch (error) { this.logger.error(new Error(`Bot conversation cycle failed in channel ${channel.id}`, { cause: error })); } } + /** Destroy stops every remaining turn; a human interrupt only the turns of bots nobody named. */ + private isSilenced({ conversation, named }: BotCycle, bot: AIBot): boolean { + return this.destroyed || (conversation.interrupted && !named.includes(bot)); + } + /** * The leading addressed speakers reply in parallel; the speakers after them reply one after another. A human - * interrupt skips the bots nobody named that have not started yet, but named bots still answer. Returns who posted - * a reply. + * interrupt skips the bots nobody named that have not started yet, but named bots still answer. */ - private async takeTurns( - channel: Channel, - conversation: ChannelConversation, - ids: ReadonlySet, - speakers: PlannedSpeaker[], - named: readonly AIBot[], - ): Promise { - // Destroy stops every remaining turn; a human interrupt only the turns of bots nobody named. - const silenced = (bot: AIBot) => this.destroyed || (conversation.interrupted && !named.includes(bot)); + private async takeTurns(cycle: BotCycle, speakers: PlannedSpeaker[]): Promise { const firstTurn = speakers.findIndex(({ kind }) => kind !== 'addressed'); const together = firstTurn === -1 ? speakers.length : firstTurn; - const spoken = await this.replyTogether(channel, speakers.slice(0, together), silenced); - - for (const [offset, { bot, kind }] of speakers.slice(together).entries()) { - const index = together + offset; - if (silenced(bot)) continue; - if (index > 0) { - await sleep(READ_PAUSE_MS); - if (silenced(bot)) continue; - } - if (kind === 'spontaneous') { - if (index > 0 && !(await this.stillPicked(channel, conversation, ids, bot))) continue; - this.caps.record(channel.id, bot.user.id, Date.now()); - } - if (await this.speak(channel, bot)) spoken.push(bot); + await Promise.all( + speakers.slice(0, together).map(async ({ bot }) => { + if (!this.isSilenced(cycle, bot)) await this.speak(cycle, bot); + }), + ); + + for (const speaker of speakers.slice(together)) { + await this.takeTurn(cycle, speaker); } - return spoken; } /** - * Every speaker starts at once, so none sees the others' replies, and each posts as soon as its own reply is typed. - * Returns who posted, in posting order. + * One sequential turn. After other replies, the bot first pauses to read them and, if nobody addressed it, the + * router must still pick it. */ - private async replyTogether( - channel: Channel, - speakers: PlannedSpeaker[], - silenced: (bot: AIBot) => boolean, - ): Promise { - const spoken: AIBot[] = []; - await Promise.all( - speakers.map(async ({ bot }) => { - if (!silenced(bot) && (await this.speak(channel, bot))) spoken.push(bot); - }), - ); - return spoken; + private async takeTurn(cycle: BotCycle, { bot, kind }: PlannedSpeaker): Promise { + if (this.isSilenced(cycle, bot)) return; + + if (cycle.spoken.length > 0) { + await sleep(READ_PAUSE_MS); + + if (this.isSilenced(cycle, bot)) return; + + if (kind === 'spontaneous') { + // The replies may have changed things: the router must still pick it, with the latest history. + const history = await this.messageRepository.getRecentMessages(cycle.channel.id, ROUTER_HISTORY_LIMIT); + const decision = await this.ai.route(history, cycle.ids); + if (this.isSilenced(cycle, bot) || !decision?.some((pick) => pick.bot === bot)) return; + } + } + + const posted = await this.speak(cycle, bot); + // Only a posted reply uses up the cap; cycles in a channel never overlap, so none can slip in meanwhile. + if (posted && kind === 'spontaneous') this.caps.record(cycle.channel.id, bot.user.id, Date.now()); } /** At most one bot that has not spoken yet reacts to the replies, under its spontaneous cap. */ - private async react( - channel: Channel, - conversation: ChannelConversation, - ids: ReadonlySet, - spoken: AIBot[], - ): Promise { - if (this.destroyed || conversation.interrupted) return null; + private async react(cycle: BotCycle): Promise { + const { channel, conversation, ids, spoken } = cycle; + if (this.destroyed || conversation.interrupted) return; const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); const decision = (await this.ai.route(history, ids, spoken)) ?? []; @@ -171,45 +172,25 @@ export class BotConversationService implements OnModuleDestroy { ({ bot }) => !spoken.includes(bot) && this.caps.canSpeak(channel.id, bot.user.id, Date.now()), ); // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message may arrive while `route` runs - if (!reaction || conversation.interrupted) return null; + if (!reaction || conversation.interrupted) return; await sleep(READ_PAUSE_MS); // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message or destroy may happen during the pause - if (conversation.interrupted || this.destroyed) return null; - - this.caps.record(channel.id, reaction.bot.user.id, Date.now()); - return (await this.speak(channel, reaction.bot)) ? reaction.bot : null; - } - - private logCycle(channel: Channel, decision: PlannedSpeaker[] | null, named: AIBot[], spoken: AIBot[]): void { - const names = (bots: AIBot[]) => bots.map((bot) => bot.user.username).join(', ') || 'none'; - const picks = decision?.map(({ bot, kind }) => `${bot.user.username} (${kind})`).join(', ') || 'none'; - const considered = [...new Set([...(decision ?? []).map(({ bot }) => bot), ...named])]; - const dropped = considered.filter((bot) => !spoken.includes(bot)); - this.logger.debug( - `Channel ${channel.id}: router ${decision === null ? 'unavailable' : `picked ${picks}`}, ` + - `dropped ${names(dropped)}, spoke ${names(spoken)}`, - ); - } + if (conversation.interrupted || this.destroyed) return; - /** Re-asks the router with the latest history whether `bot` should still speak; never after a human interrupts. */ - private async stillPicked( - channel: Channel, - conversation: ChannelConversation, - ids: ReadonlySet, - bot: AIBot, - ): Promise { - const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); - const decision = await this.ai.route(history, ids); - return !conversation.interrupted && (decision?.some((pick) => pick.bot === bot) ?? false); + const posted = await this.speak(cycle, reaction.bot); + if (posted) this.caps.record(channel.id, reaction.bot.user.id, Date.now()); } /** - * Generates, stores and broadcasts one reply. The typing indicator stays on until the reply is posted, paced like - * typing. Returns whether a message was posted. + * Generates, stores and broadcasts one reply, and adds the bot to `cycle.spoken`. The typing indicator stays on + * until the reply is posted, paced like typing. Returns whether a message was posted. */ - private async speak(channel: Channel, bot: AIBot): Promise { + private async speak(cycle: BotCycle, bot: AIBot): Promise { + const { channel } = cycle; + this.presence.startBotTyping(bot.user.id, channel.id); + const startedAt = Date.now(); try { const history = await this.messageRepository.getRecentMessages(channel.id, AI_HISTORY_LIMIT); @@ -229,6 +210,8 @@ export class BotConversationService implements OnModuleDestroy { ...reply, createdAt: createdAt.toISOString(), }); + cycle.spoken.push(bot); + return true; } catch (error) { this.logger.error(new Error(`Error generating the ${bot.user.username} reply`, { cause: error })); diff --git a/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts index 73bd748..bd5bda9 100644 --- a/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts @@ -1,7 +1,8 @@ +import type { AIBot } from '../../ai/bots.js'; +import type { Channel } from '../../channel/channel.repository.js'; + /** Silence after the last human message before the bots decide whether to speak. */ export const DEBOUNCE_MS = 600; -/** How many channel messages the router reads. */ -export const ROUTER_HISTORY_LIMIT = 20; /** How many channel messages a bot reads before replying. */ export const AI_HISTORY_LIMIT = 30; /** Typing speed a bot reply is paced at, so it appears roughly when a person would have finished typing it. */ @@ -28,3 +29,15 @@ export interface ChannelConversation { /** Cycles run one after another, never overlapping. */ chain: Promise; } + +/** One run over a channel's pending human messages. */ +export interface BotCycle { + channel: Channel; + conversation: ChannelConversation; + /** The human messages this cycle answers. */ + ids: ReadonlySet; + /** Bots named in those messages: they answer even after a human interrupt. */ + named: readonly AIBot[]; + /** Bots that posted in this cycle, in posting order. */ + spoken: AIBot[]; +} diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index 96972e9..d4abbc7 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,5 +1,5 @@ import { BOTS, detectNamedBots } from '@/modules/ai/bots.js'; -import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.js'; +import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH, trimToLastSentence } from '@/modules/ai/prompt.js'; import { buildRouterPrompt, planSpeakers } from '@/modules/ai/router.js'; import { SpontaneousCaps } from '@/modules/ai/spontaneous-caps.js'; @@ -51,6 +51,15 @@ describe('cleanReply', () => { }); }); +describe('trimToLastSentence', () => { + it('drops the unfinished sentence', () => + expect(trimToLastSentence('Hola, mi amor. Te cuento que ayer fui')).toBe('Hola, mi amor.')); + it('keeps the last complete sentence of several', () => + expect(trimToLastSentence('¡Qué bien! ¿Y tú? Yo estaba pensando en')).toBe('¡Qué bien! ¿Y tú?')); + it('does not cut at a decimal point', () => expect(trimToLastSentence('Sale la versión 2.5 mañana')).toBe('')); + it('returns empty when no sentence finished', () => expect(trimToLastSentence('sin ningún final')).toBe('')); +}); + describe('detectNamedBots', () => { it('returns every named bot in BOTS order', () => expect(detectNamedBots('pedro y chayanne').map((b) => b.user.username)).toEqual(['chayanne', 'pedro_pascal'])); diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 6c1dde8..b15938f 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -401,6 +401,38 @@ describe('BotConversationService', () => { expect(reply.mock.calls[0][0]).toBe(pedro); }); + it('does not use up the cap when a spontaneous reply fails', async () => { + const { say, route, reply } = setup(); + route.mockImplementation(async (_history, _ids, exclude = []) => + exclude.length > 0 ? [] : [{ bot: pedro, kind: 'spontaneous' }], + ); + reply.mockResolvedValueOnce(null); + + say('vi The Last of Us anoche'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + await jest.advanceTimersByTimeAsync(60_000); + say('qué buena serie'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([pedro, pedro]); + }); + + it('does not use up the cap when a reaction fails', async () => { + const { say, route, reply } = setup(); + route.mockImplementation(async (_history, _ids, exclude = []) => + exclude.length > 0 ? [{ bot: pedro, kind: 'spontaneous' }] : [{ bot: chayanne, kind: 'addressed' }], + ); + reply.mockImplementation(async (bot) => (bot === pedro && reply.mock.calls.length === 2 ? null : replyOf(bot))); + + say('chayanne, ¿qué cantas hoy?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + await jest.advanceTimersByTimeAsync(60_000); + say('chayanne, ¿y mañana?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro, chayanne, pedro]); + }); + it('posts a reply only after its typing delay', async () => { const { say, route, reply, messageRepository } = setup(); route.mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]); diff --git a/apps/api/tests/presence.spec.ts b/apps/api/tests/presence.spec.ts new file mode 100644 index 0000000..f9013c7 --- /dev/null +++ b/apps/api/tests/presence.spec.ts @@ -0,0 +1,43 @@ +import { BOTS } from '@/modules/ai/bots.js'; +import { PresenceService } from '@/modules/gateway/presence.service.js'; +import type { SocketEmitter } from '@/modules/gateway/socket-emitter.js'; + +describe('PresenceService bot typing', () => { + const chayanne = BOTS[1].user.id; + const typingOf = (presence: PresenceService) => { + const user = presence.activeUsers.find(({ userId }) => userId === chayanne); + return user?.isTyping ? user.typingInChannel : undefined; + }; + + function setup() { + return new PresenceService({ broadcast: jest.fn() } as unknown as SocketEmitter); + } + + it('keeps typing in the first channel when the second one finishes first', () => { + const presence = setup(); + presence.startBotTyping(chayanne, 'a'); + presence.startBotTyping(chayanne, 'b'); + presence.stopBotTyping(chayanne, 'b'); + + expect(typingOf(presence)).toBe('a'); + }); + + it('stops typing once every channel finished', () => { + const presence = setup(); + presence.startBotTyping(chayanne, 'a'); + presence.startBotTyping(chayanne, 'b'); + presence.stopBotTyping(chayanne, 'a'); + expect(typingOf(presence)).toBe('b'); + + presence.stopBotTyping(chayanne, 'b'); + expect(typingOf(presence)).toBeUndefined(); + }); + + it('ignores a stop for a channel the bot is not typing in', () => { + const presence = setup(); + presence.startBotTyping(chayanne, 'a'); + presence.stopBotTyping(chayanne, 'b'); + + expect(typingOf(presence)).toBe('a'); + }); +}); diff --git a/compose.yaml b/compose.yaml index 6cac1ef..0fdad57 100644 --- a/compose.yaml +++ b/compose.yaml @@ -110,6 +110,8 @@ services: AI_MODEL: ${AI_MODEL:-anthropic:claude-haiku-5-5} ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-} OPENAI_API_KEY: ${OPENAI_API_KEY:-} + # on|off: off answers only bots named in a message (useful for small Ollama models). + AI_ROUTER: ${AI_ROUTER:-on} # With AI_MODEL=ollama:, reaches an Ollama running on the host. OLLAMA_BASE_URL: ${OLLAMA_BASE_URL:-http://host.docker.internal:11434/v1} extra_hosts: From 04c304b06a79c00110ca61458b2490e7daa2f398 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 22:56:23 -0600 Subject: [PATCH 16/18] feat(api): let a reacting bot comment on the other bots' replies The bot picked to react is now told whose replies it reacts to, so it teases or disagrees with them instead of answering the human again. CHAT_STYLE allows friendly banter between bots, and the router is invited to pick a reaction when a bot has something witty to say, still none otherwise. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- apps/api/src/modules/ai/ai.service.ts | 17 ++++++++++---- apps/api/src/modules/ai/bots.ts | 3 ++- apps/api/src/modules/ai/prompt.ts | 10 +++++++++ apps/api/src/modules/ai/router.ts | 2 +- .../bot-conversation.service.ts | 9 ++++---- apps/api/tests/ai-bots.spec.ts | 22 ++++++++++++++++++- apps/api/tests/bot-conversation.spec.ts | 22 +++++++++++++++++-- 8 files changed, 73 insertions(+), 14 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a7d9193..b3483cd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add unnamed speakers up to 2 speakers in total (with 3 bots named, all 3 answer and none is added). Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add unnamed speakers up to 2 speakers in total (with 3 bots named, all 3 answer and none is added). Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak, which is told whose replies it reacts to so it comments on them (teasing allowed) instead of answering again. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. ## Web architecture (`apps/web`) diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index bef2505..c0cf608 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -7,7 +7,13 @@ import { createProviderRegistry, generateText, Output } from 'ai'; import { UserRepository } from '../user/user.repository.js'; import { type AIBot, BOTS } from './bots.js'; -import { buildPromptMessages, type ChannelHistoryEntry, cleanReply, trimToLastSentence } from './prompt.js'; +import { + buildPromptMessages, + type ChannelHistoryEntry, + cleanReply, + reactionInstructions, + trimToLastSentence, +} from './prompt.js'; import { buildRouterPrompt, type PlannedSpeaker, ROUTER_INSTRUCTIONS, ROUTER_SCHEMA } from './router.js'; // The SDK prints warnings with console.warn, outside pino; `reply` logs them through the Nest logger instead. @@ -73,13 +79,16 @@ export class AIService implements OnModuleInit { } } - /** Returns `null` when the bots are disabled or the model produced no text; throws on provider errors. */ - async reply(bot: AIBot, history: ChannelHistoryEntry[]): Promise { + /** + * Returns `null` when the bots are disabled or the model produced no text; throws on provider errors. With + * `reactingTo`, the bot comments on those bots' replies instead of answering the conversation. + */ + async reply(bot: AIBot, history: ChannelHistoryEntry[], reactingTo?: readonly AIBot[]): Promise { if (!this.enabled) return null; const { text, finishReason, warnings } = await generateText({ model: this.registry.languageModel(this.modelId as `${ProviderId}:${string}`), - instructions: bot.systemPrompt, + instructions: reactingTo ? bot.systemPrompt + reactionInstructions(reactingTo) : bot.systemPrompt, messages: buildPromptMessages(bot, history), temperature: bot.temperature, // Recent Claude models think by default, and thinking counts against maxOutputTokens. diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 72c2a6a..654f29c 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -22,7 +22,8 @@ Estás en un chat de grupo con amigos. Escribe como una persona real en un chat: - Varía cómo empiezas: no abras cada mensaje con risas ("jaja") ni con el nombre de la persona. - Si hubo un malentendido que ya se aclaró, no vuelvas a mencionarlo. - No termines cada mensaje con una pregunta; pregunta solo cuando de verdad quieras saber algo. -- No atribuyas a otros cosas que no dijeron en la conversación.`; +- No atribuyas a otros cosas que no dijeron en la conversación. +- Con los otros bots hay confianza: puedes bromear, picarlos o llevarles la contraria con buena onda.`; /** Bots named in the same message reply in this order. */ export const BOTS: AIBot[] = [ diff --git a/apps/api/src/modules/ai/prompt.ts b/apps/api/src/modules/ai/prompt.ts index 46fdcd7..3b4da31 100644 --- a/apps/api/src/modules/ai/prompt.ts +++ b/apps/api/src/modules/ai/prompt.ts @@ -45,3 +45,13 @@ export function cleanReply(bot: AIBot, text: string): string { export function trimToLastSentence(text: string): string { return /^[\s\S]*[.!?…](?=\s|$)/.exec(text)?.[0] ?? ''; } + +const spanishList = new Intl.ListFormat('es', { type: 'conjunction' }); + +/** Appended to the persona when a bot reacts to other bots' replies, so it comments on them instead of answering again. */ +export function reactionInstructions(reactingTo: readonly AIBot[]): string { + const names = spanishList.format(reactingTo.map((bot) => bot.user.username)); + return ` + +Ahora mismo no respondes a la persona: reaccionas a lo que acaba de decir ${names}, como lo haría un amigo con confianza. Lo normal es picarlo, burlarte con cariño de su respuesta o llevarle la contraria con tu propio gusto; no lo felicites ni repitas lo que dijo. Habla con él directamente, sin volver a contestar la pregunta. Una frase, dos como mucho.`; +} diff --git a/apps/api/src/modules/ai/router.ts b/apps/api/src/modules/ai/router.ts index 618cf72..5c19222 100644 --- a/apps/api/src/modules/ai/router.ts +++ b/apps/api/src/modules/ai/router.ts @@ -63,7 +63,7 @@ export function buildRouterPrompt( if (exclude.length === 0) return lines.join('\n'); const names = exclude.map((bot) => bot.user.username).join(', '); - return `${lines.join('\n')}\n\nAcaban de responder: ${names}. Solo puede reaccionar otro bot distinto de esos, y normalmente ninguno lo hace.`; + return `${lines.join('\n')}\n\nAcaban de responder: ${names}. ¿Algún otro bot tiene algo con gracia que decir sobre esas respuestas (discrepar, picarlo con cariño, rematar el chiste)? Si nada lo pide, ninguno. Solo bots distintos de ${names}.`; } /** diff --git a/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts index 139ac6d..1a947aa 100644 --- a/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts @@ -178,15 +178,16 @@ export class BotConversationService implements OnModuleDestroy { // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message or destroy may happen during the pause if (conversation.interrupted || this.destroyed) return; - const posted = await this.speak(cycle, reaction.bot); + const posted = await this.speak(cycle, reaction.bot, [...spoken]); if (posted) this.caps.record(channel.id, reaction.bot.user.id, Date.now()); } /** - * Generates, stores and broadcasts one reply, and adds the bot to `cycle.spoken`. The typing indicator stays on + * Generates, stores and broadcasts one reply, and adds the bot to `cycle.spoken`. With `reactingTo`, the reply + * comments on those bots' replies. The typing indicator stays on * until the reply is posted, paced like typing. Returns whether a message was posted. */ - private async speak(cycle: BotCycle, bot: AIBot): Promise { + private async speak(cycle: BotCycle, bot: AIBot, reactingTo?: readonly AIBot[]): Promise { const { channel } = cycle; this.presence.startBotTyping(bot.user.id, channel.id); @@ -194,7 +195,7 @@ export class BotConversationService implements OnModuleDestroy { const startedAt = Date.now(); try { const history = await this.messageRepository.getRecentMessages(channel.id, AI_HISTORY_LIMIT); - const content = await this.ai.reply(bot, history); + const content = await this.ai.reply(bot, history, reactingTo); if (!content) return false; await sleep(typingDelayMs(content.length, Date.now() - startedAt)); diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index d4abbc7..ef673d6 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,5 +1,11 @@ import { BOTS, detectNamedBots } from '@/modules/ai/bots.js'; -import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH, trimToLastSentence } from '@/modules/ai/prompt.js'; +import { + buildPromptMessages, + cleanReply, + MAX_MESSAGE_LENGTH, + reactionInstructions, + trimToLastSentence, +} from '@/modules/ai/prompt.js'; import { buildRouterPrompt, planSpeakers } from '@/modules/ai/router.js'; import { SpontaneousCaps } from '@/modules/ai/spontaneous-caps.js'; @@ -51,6 +57,14 @@ describe('cleanReply', () => { }); }); +describe('reactionInstructions', () => { + it('names the bots being reacted to and asks not to answer the question again', () => { + const text = reactionInstructions([BOTS[0], BOTS[2]]); + expect(text).toContain('Chat yipiyi y pedro_pascal'); + expect(text).toContain('sin volver a contestar la pregunta'); + }); +}); + describe('trimToLastSentence', () => { it('drops the unfinished sentence', () => expect(trimToLastSentence('Hola, mi amor. Te cuento que ayer fui')).toBe('Hola, mi amor.')); @@ -115,6 +129,12 @@ describe('buildRouterPrompt', () => { it('names the excluded bots when asking for a reaction', () => expect(buildRouterPrompt([h('m1', 'ana', 'hola')], new Set(), [BOTS[1]])).toContain('chayanne')); + + it('invites a witty reaction to the replies, but none when nothing calls for it', () => { + const prompt = buildRouterPrompt([h('m1', 'ana', 'hola')], new Set(), [BOTS[1]]); + expect(prompt).toContain('algo con gracia que decir'); + expect(prompt).not.toContain('normalmente ninguno'); + }); }); describe('planSpeakers', () => { diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index b15938f..3566a54 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -44,8 +44,11 @@ function setup() { }; const route = jest.fn, Parameters>().mockResolvedValue([]); const reply = jest.fn( - async (bot: PlannedSpeaker['bot'], _history: ChannelHistoryEntry[]): Promise => - `reply from ${bot.user.username}`, + async ( + bot: PlannedSpeaker['bot'], + _history: ChannelHistoryEntry[], + _reactingTo?: ReadonlyArray, + ): Promise => `reply from ${bot.user.username}`, ); const ai = { enabled: true, routerEnabled: true, route, reply }; const presence = { startBotTyping: jest.fn(), stopBotTyping: jest.fn() }; @@ -174,6 +177,21 @@ describe('BotConversationService', () => { expect(route.mock.calls[1][2]).toContain(chayanne); }); + it('tells the reacting bot whose replies it reacts to, and only that bot', async () => { + const { say, route, reply } = setup(); + route + .mockResolvedValueOnce([{ bot: chayanne, kind: 'addressed' }]) + .mockResolvedValueOnce([{ bot: pedro, kind: 'spontaneous' }]); + + say('¿qué música ponemos?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + + expect(reply.mock.calls.map(([bot, , reactingTo]) => [bot, reactingTo])).toEqual([ + [chayanne, undefined], + [pedro, [chayanne]], + ]); + }); + it('a human message during a reply skips the remaining turns and routes again', async () => { const { say, route, reply } = setup(); route.mockResolvedValueOnce([ From 1378fba78d6ad80f48d15901cade5f8f66a82564 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Tue, 17 Nov 2026 17:03:54 -0600 Subject: [PATCH 17/18] refactor(api): move speaker selection into the ai module --- apps/api/src/modules/ai/ai.module.ts | 5 +- apps/api/src/modules/ai/speaker-selector.ts | 64 +++++++ .../bot-conversation.service.ts | 163 ++++++++---------- .../bot-conversation/bot-conversation.util.ts | 3 +- apps/api/tests/bot-conversation.spec.ts | 2 + 5 files changed, 144 insertions(+), 93 deletions(-) create mode 100644 apps/api/src/modules/ai/speaker-selector.ts diff --git a/apps/api/src/modules/ai/ai.module.ts b/apps/api/src/modules/ai/ai.module.ts index 96f9132..babd0e3 100644 --- a/apps/api/src/modules/ai/ai.module.ts +++ b/apps/api/src/modules/ai/ai.module.ts @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common'; import { UserModule } from '../user/user.module.js'; import { AIService } from './ai.service.js'; +import { SpeakerSelector } from './speaker-selector.js'; @Module({ imports: [UserModule], - providers: [AIService], - exports: [AIService], + providers: [AIService, SpeakerSelector], + exports: [AIService, SpeakerSelector], }) export class AIModule {} diff --git a/apps/api/src/modules/ai/speaker-selector.ts b/apps/api/src/modules/ai/speaker-selector.ts new file mode 100644 index 0000000..558c457 --- /dev/null +++ b/apps/api/src/modules/ai/speaker-selector.ts @@ -0,0 +1,64 @@ +import { Injectable } from '@nestjs/common'; + +import { AIService } from './ai.service.js'; +import type { AIBot } from './bots.js'; +import type { ChannelHistoryEntry } from './prompt.js'; +import { planSpeakers, type SpeakerKind } from './router.js'; +import { SpontaneousCaps } from './spontaneous-caps.js'; + +export interface SpeakerPlan { + /** Bots the new messages are for: named in them, or picked as addressed by the router. */ + addressed: AIBot[]; + /** Bots nobody talked to that have something to add, under their cap. */ + spontaneous: AIBot[]; + /** False when the router is off or failed: only the named bots speak, and nobody reacts. */ + routed: boolean; +} + +/** Decides which bots speak in a channel: the router's picks, the bots a human named and the spontaneous caps. */ +@Injectable() +export class SpeakerSelector { + private readonly caps = new SpontaneousCaps(); + + constructor(private readonly ai: AIService) {} + + /** Who answers the new human messages (`newIds`). `named` are the bots those messages name. */ + async plan( + channelId: string, + history: ChannelHistoryEntry[], + newIds: ReadonlySet, + named: readonly AIBot[], + ): Promise { + const decision = this.ai.routerEnabled ? await this.ai.route(history, newIds) : null; + const speakers = planSpeakers(decision, named, (bot) => this.underCap(channelId, bot)); + const bots = (kind: SpeakerKind) => speakers.filter((speaker) => speaker.kind === kind).map(({ bot }) => bot); + + return { addressed: bots('addressed'), spontaneous: bots('spontaneous'), routed: decision !== null }; + } + + /** Whether the router still picks `bot` now that other bots have replied. */ + async stillPicks(bot: AIBot, history: ChannelHistoryEntry[], newIds: ReadonlySet): Promise { + const decision = await this.ai.route(history, newIds); + return decision?.some((pick) => pick.bot === bot) ?? false; + } + + /** A bot that has not spoken and wants to comment on the replies of `spoken`, if any, under its cap. */ + async pickReaction( + channelId: string, + history: ChannelHistoryEntry[], + newIds: ReadonlySet, + spoken: readonly AIBot[], + ): Promise { + const decision = (await this.ai.route(history, newIds, spoken)) ?? []; + return decision.find(({ bot }) => !spoken.includes(bot) && this.underCap(channelId, bot))?.bot; + } + + /** Call it once a spontaneous reply or a reaction is posted: only a posted reply uses up the cap. */ + recordSpontaneous(channelId: string, bot: AIBot): void { + this.caps.record(channelId, bot.user.id, Date.now()); + } + + private underCap(channelId: string, bot: AIBot): boolean { + return this.caps.canSpeak(channelId, bot.user.id, Date.now()); + } +} diff --git a/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts index 1a947aa..43364aa 100644 --- a/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts @@ -3,8 +3,9 @@ import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; import { AIService } from '../../ai/ai.service.js'; import { type AIBot, detectNamedBots } from '../../ai/bots.js'; -import { type PlannedSpeaker, planSpeakers, ROUTER_HISTORY_LIMIT } from '../../ai/router.js'; -import { SpontaneousCaps } from '../../ai/spontaneous-caps.js'; +import type { ChannelHistoryEntry } from '../../ai/prompt.js'; +import { ROUTER_HISTORY_LIMIT } from '../../ai/router.js'; +import { type SpeakerPlan, SpeakerSelector } from '../../ai/speaker-selector.js'; import type { Channel } from '../../channel/channel.repository.js'; import { PresenceService } from '../../gateway/presence.service.js'; import { SocketEmitter } from '../../gateway/socket-emitter.js'; @@ -21,20 +22,20 @@ import { } from './bot-conversation.util.js'; /** - * Decides which bots join a channel conversation. Human messages are debounced; then the router (or, without it, - * the bots named in the new messages) picks the speakers: the addressed ones reply at once, the spontaneous ones in - * turns after them, and one other bot may react. + * Runs the bots' side of a channel conversation. Human messages are debounced into a cycle; `SpeakerSelector` decides + * who speaks in it, and this service does the pacing: the addressed bots reply at once, the spontaneous ones in turns + * after them, and one other bot may react. */ @Injectable() export class BotConversationService implements OnModuleDestroy { private readonly logger = new Logger(BotConversationService.name); private readonly conversations = new Map(); - private readonly caps = new SpontaneousCaps(); private destroyed = false; constructor( private readonly messageRepository: MessageRepository, private readonly ai: AIService, + private readonly selector: SpeakerSelector, private readonly presence: PresenceService, private readonly userRepository: UserRepository, private readonly emitter: SocketEmitter, @@ -47,39 +48,35 @@ export class BotConversationService implements OnModuleDestroy { } } - /** - * Only human messages reach this, so named-bot detection never runs over bot replies. - * - * Note: this is channel scoped btw - */ + /** Only human messages reach this, so named-bot detection never runs over bot replies. */ onHumanMessage(channel: Channel, messageId: string, content: string): void { if (!this.ai.enabled || this.destroyed) return; - let state = this.conversations.get(channel.id); - if (!state) { - state = { pending: [], interrupted: false, chain: Promise.resolve() }; - this.conversations.set(channel.id, state); - } - const conversation = state; - + const conversation = this.conversationOf(channel.id); conversation.pending.push({ id: messageId, content }); - conversation.interrupted = true; clearTimeout(conversation.timer); - conversation.timer = setTimeout(() => { conversation.timer = undefined; conversation.chain = conversation.chain.then(async () => this.runCycle(channel, conversation)); }, DEBOUNCE_MS); } + private conversationOf(channelId: string): ChannelConversation { + let conversation = this.conversations.get(channelId); + if (!conversation) { + conversation = { pending: [], chain: Promise.resolve() }; + this.conversations.set(channelId, conversation); + } + return conversation; + } + private async runCycle(channel: Channel, conversation: ChannelConversation): Promise { // A newer message's timer will queue its own cycle, and that one takes the pending messages after their silence. if (this.destroyed || conversation.timer !== undefined || conversation.pending.length === 0) return; const pending = conversation.pending; conversation.pending = []; - conversation.interrupted = false; const cycle: BotCycle = { channel, @@ -90,96 +87,84 @@ export class BotConversationService implements OnModuleDestroy { }; try { - const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); - const decision = this.ai.routerEnabled ? await this.ai.route(history, cycle.ids) : null; - const speakers = planSpeakers(decision, cycle.named, (bot) => - this.caps.canSpeak(channel.id, bot.user.id, Date.now()), - ); - - await this.takeTurns(cycle, speakers); - if (decision !== null && cycle.spoken.length > 0) await this.react(cycle); - - const names = (bots: readonly AIBot[]) => bots.map((bot) => bot.user.username).join(', ') || 'none'; - const picks = - decision === null - ? 'unavailable' - : decision.map(({ bot, kind }) => `${bot.user.username} (${kind})`).join(', ') || 'none'; - this.logger.debug( - `Channel ${channel.id}: router ${picks}, named ${names(cycle.named)}, spoke ${names(cycle.spoken)}`, - ); + const plan = await this.selector.plan(channel.id, await this.routerHistory(cycle), cycle.ids, cycle.named); + + await this.answerAddressed(cycle, plan.addressed); + await this.speakInTurns(cycle, plan.spontaneous); + if (plan.routed && cycle.spoken.length > 0) await this.react(cycle); + + this.logCycle(cycle, plan); } catch (error) { this.logger.error(new Error(`Bot conversation cycle failed in channel ${channel.id}`, { cause: error })); } } - /** Destroy stops every remaining turn; a human interrupt only the turns of bots nobody named. */ - private isSilenced({ conversation, named }: BotCycle, bot: AIBot): boolean { - return this.destroyed || (conversation.interrupted && !named.includes(bot)); + /** A human wrote after the cycle started: that message waits in `pending` for the next cycle. */ + private interrupted({ conversation }: BotCycle): boolean { + return conversation.pending.length > 0; } - /** - * The leading addressed speakers reply in parallel; the speakers after them reply one after another. A human - * interrupt skips the bots nobody named that have not started yet, but named bots still answer. - */ - private async takeTurns(cycle: BotCycle, speakers: PlannedSpeaker[]): Promise { - const firstTurn = speakers.findIndex(({ kind }) => kind !== 'addressed'); - const together = firstTurn === -1 ? speakers.length : firstTurn; - - await Promise.all( - speakers.slice(0, together).map(async ({ bot }) => { - if (!this.isSilenced(cycle, bot)) await this.speak(cycle, bot); - }), - ); - - for (const speaker of speakers.slice(together)) { - await this.takeTurn(cycle, speaker); - } + /** Nothing new starts after a destroy or a human interrupt. Only the bots a human named are exempt from the latter. */ + private stopped(cycle: BotCycle): boolean { + return this.destroyed || this.interrupted(cycle); } - /** - * One sequential turn. After other replies, the bot first pauses to read them and, if nobody addressed it, the - * router must still pick it. - */ - private async takeTurn(cycle: BotCycle, { bot, kind }: PlannedSpeaker): Promise { - if (this.isSilenced(cycle, bot)) return; + /** The addressed bots reply in parallel, each posting when ready, so they do not see each other. */ + private async answerAddressed(cycle: BotCycle, bots: readonly AIBot[]): Promise { + if (this.destroyed) return; - if (cycle.spoken.length > 0) { - await sleep(READ_PAUSE_MS); + // A human may have interrupted while the router decided: the bots that human named still answer. + const answering = this.interrupted(cycle) ? bots.filter((bot) => cycle.named.includes(bot)) : bots; + await Promise.all(answering.map(async (bot) => this.speak(cycle, bot))); + } - if (this.isSilenced(cycle, bot)) return; + /** The spontaneous bots reply one after another, after the addressed ones, so each sees the replies before it. */ + private async speakInTurns(cycle: BotCycle, bots: readonly AIBot[]): Promise { + for (const bot of bots) { + if (this.stopped(cycle)) return; - if (kind === 'spontaneous') { - // The replies may have changed things: the router must still pick it, with the latest history. - const history = await this.messageRepository.getRecentMessages(cycle.channel.id, ROUTER_HISTORY_LIMIT); - const decision = await this.ai.route(history, cycle.ids); - if (this.isSilenced(cycle, bot) || !decision?.some((pick) => pick.bot === bot)) return; + // After other replies, the bot pauses to read them. They may have changed things: the router must still + // pick it, with the latest history. + if (cycle.spoken.length > 0) { + await sleep(READ_PAUSE_MS); + if (this.stopped(cycle)) return; + + const stillPicked = await this.selector.stillPicks(bot, await this.routerHistory(cycle), cycle.ids); + if (this.stopped(cycle)) return; + if (!stillPicked) continue; } - } - const posted = await this.speak(cycle, bot); - // Only a posted reply uses up the cap; cycles in a channel never overlap, so none can slip in meanwhile. - if (posted && kind === 'spontaneous') this.caps.record(cycle.channel.id, bot.user.id, Date.now()); + // Cycles in a channel never overlap, so no other reply of this bot can slip in before the cap is recorded. + const posted = await this.speak(cycle, bot); + if (posted) this.selector.recordSpontaneous(cycle.channel.id, bot); + } } /** At most one bot that has not spoken yet reacts to the replies, under its spontaneous cap. */ private async react(cycle: BotCycle): Promise { - const { channel, conversation, ids, spoken } = cycle; - if (this.destroyed || conversation.interrupted) return; + if (this.stopped(cycle)) return; - const history = await this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); - const decision = (await this.ai.route(history, ids, spoken)) ?? []; - const reaction = decision.find( - ({ bot }) => !spoken.includes(bot) && this.caps.canSpeak(channel.id, bot.user.id, Date.now()), - ); - // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message may arrive while `route` runs - if (!reaction || conversation.interrupted) return; + const history = await this.routerHistory(cycle); + const bot = await this.selector.pickReaction(cycle.channel.id, history, cycle.ids, cycle.spoken); + if (!bot || this.stopped(cycle)) return; await sleep(READ_PAUSE_MS); - // oxlint-disable-next-line typescript/no-unnecessary-condition -- a human message or destroy may happen during the pause - if (conversation.interrupted || this.destroyed) return; + if (this.stopped(cycle)) return; + + const posted = await this.speak(cycle, bot, [...cycle.spoken]); + if (posted) this.selector.recordSpontaneous(cycle.channel.id, bot); + } - const posted = await this.speak(cycle, reaction.bot, [...spoken]); - if (posted) this.caps.record(channel.id, reaction.bot.user.id, Date.now()); + private async routerHistory({ channel }: BotCycle): Promise { + return this.messageRepository.getRecentMessages(channel.id, ROUTER_HISTORY_LIMIT); + } + + private logCycle({ channel, named, spoken }: BotCycle, { addressed, spontaneous, routed }: SpeakerPlan): void { + const names = (bots: readonly AIBot[]) => bots.map((bot) => bot.user.username).join(', ') || 'none'; + this.logger.debug( + `Channel ${channel.id}: router ${routed ? 'ok' : 'unavailable'}, named ${names(named)}, ` + + `addressed ${names(addressed)}, spontaneous ${names(spontaneous)}, spoke ${names(spoken)}`, + ); } /** diff --git a/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts index bd5bda9..8426622 100644 --- a/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts @@ -23,9 +23,8 @@ export async function sleep(ms: number): Promise { export interface ChannelConversation { timer?: NodeJS.Timeout; + /** Human messages no cycle has taken yet. While a cycle runs, any message here means a human interrupted it. */ pending: Array<{ id: string; content: string }>; - /** Set by every human message, so a running cycle skips the remaining turns of bots nobody named. */ - interrupted: boolean; /** Cycles run one after another, never overlapping. */ chain: Promise; } diff --git a/apps/api/tests/bot-conversation.spec.ts b/apps/api/tests/bot-conversation.spec.ts index 3566a54..95f28a4 100644 --- a/apps/api/tests/bot-conversation.spec.ts +++ b/apps/api/tests/bot-conversation.spec.ts @@ -4,6 +4,7 @@ import type { AIService } from '@/modules/ai/ai.service.js'; import { BOTS } from '@/modules/ai/bots.js'; import type { ChannelHistoryEntry } from '@/modules/ai/prompt.js'; import type { PlannedSpeaker } from '@/modules/ai/router.js'; +import { SpeakerSelector } from '@/modules/ai/speaker-selector.js'; import type { Channel } from '@/modules/channel/channel.repository.js'; import type { PresenceService } from '@/modules/gateway/presence.service.js'; import type { SocketEmitter } from '@/modules/gateway/socket-emitter.js'; @@ -58,6 +59,7 @@ function setup() { const service = new BotConversationService( messageRepository as unknown as MessageRepository, ai as unknown as AIService, + new SpeakerSelector(ai as unknown as AIService), presence as unknown as PresenceService, userRepository as unknown as UserRepository, emitter as unknown as SocketEmitter, From 411db76f6e98c0d10d8c74735e7d8dbd738e746d Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Tue, 17 Nov 2026 17:03:54 -0600 Subject: [PATCH 18/18] docs(monorepo): mention SpeakerSelector in AGENTS.md --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index b3483cd..a2ee00c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -116,7 +116,7 @@ tsgolint does not support `baseUrl`, so the api `tsconfig.json` has `paths` with - Presence is `PresenceService`, an in-memory map that broadcasts its own changes and assumes a single API instance. - Inbound events are manual `socket.on` listeners, not `@SubscribeMessage`, because the global guard and interceptor assume HTTP. -**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add unnamed speakers up to 2 speakers in total (with 3 bots named, all 3 answer and none is added). Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak, which is told whose replies it reacts to so it comments on them (teasing allowed) instead of answering again. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. +**AI bots (`src/modules/ai/`).** Three bots are defined as data in `BOTS` (`bots.ts`: hard-coded user UUIDs, trigger regex, system prompt); their users are upserted on init and are always shown online. After a human message, `BotConversationService` (message module) waits for 0.6 s of channel silence, then runs one cycle: an LLM router (`AIService.route`) reads the last 20 channel messages and picks who speaks. `SpeakerSelector` (ai module) owns that decision (router, named bots, caps); `BotConversationService` only sequences and paces the cycle. Bots named in a human message always answer (name detection runs only on human messages) and addressed replies are uncapped. The router may add unnamed speakers up to 2 speakers in total (with 3 bots named, all 3 answer and none is added). Each reply is generated from the last 30 messages: addressed speakers (named or picked as addressed by the router) reply in parallel, each posting when ready, so they do not see each other; spontaneous speakers then take turns after them and see the earlier replies; then at most 1 reaction from a bot that did not speak, which is told whose replies it reacts to so it comments on them (teasing allowed) instead of answering again. Spontaneous replies, reactions included, are capped in code at 1 per bot per channel per 10 minutes (in memory, single instance). Replies are short chat-style messages (a shared `CHAT_STYLE` prompt), go through the Vercel AI SDK, and are paced like typing: the bot's typing status stays on until the reply is posted, and a spontaneous speaker or a reaction pauses to read before answering the other bots. The model comes from `AI_MODEL` (`provider:model`: `anthropic`, `openai`, or `ollama` through its OpenAI-compatible API at `OLLAMA_BASE_URL`; default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off (Ollama needs none). `AI_ROUTER=off`, or a router failure or invalid output, falls back to name detection: only named bots answer, with no spontaneous replies or reactions. ## Web architecture (`apps/web`)