diff --git a/AGENTS.md b/AGENTS.md index 2087d42..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 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 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`) diff --git a/README.md b/README.md index d205f6b..751ae7d 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,34 @@ 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. +- 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 The compose files live in `apps/api` and are still run from there: 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.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/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index 64c3cfb..c0cf608 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -3,11 +3,18 @@ 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 { + 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. globalThis.AI_SDK_LOG_WARNINGS = false; @@ -23,6 +30,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 +39,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 +59,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 { @@ -66,18 +79,21 @@ 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. reasoning: 'none', - maxOutputTokens: 500, + maxOutputTokens: 150, maxRetries: 2, abortSignal: AbortSignal.timeout(30_000), }); @@ -86,10 +102,49 @@ 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; } + + /** 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/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts index 9a60f3c..654f29c 100644 --- a/apps/api/src/modules/ai/bots.ts +++ b/apps/api/src/modules/ai/bots.ts @@ -4,11 +4,28 @@ 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; } -/** Order matters: when a message names several bots, the first one replies. */ +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. +- 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[] = [ { user: { @@ -19,9 +36,8 @@ export const BOTS: AIBot[] = [ avatarUrl: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTMdM9MEQ0ExL1PmInT3U5I8v63YXBEdoIT0Q&s', }, trigger: /\b(?:chat\s*gpt|gpt|yipiyi)\b/i, - 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.`, + routerDescription: 'asistente útil, responde dudas técnicas y preguntas generales.', + systemPrompt: 'Eres un asistente útil en un chat. Responde de manera concisa y clara.' + CHAT_STYLE, temperature: 0.7, }, { @@ -33,16 +49,10 @@ export const BOTS: AIBot[] = [ avatarUrl: 'https://i.pinimg.com/474x/06/90/b5/0690b5003ba0760a39b9f88e8066dab5.jpg', }, trigger: /\bchayann?e\b/i, - 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 - `, + routerDescription: 'el cantante puertorriqueño, cariñoso, habla de música, baile y familia.', + systemPrompt: + 'Eres Chayanne, el cantante puertorriqueño: cariñoso, galante, alegre, con expresiones caribeñas de vez en cuando.' + + CHAT_STYLE, temperature: 0.8, }, { @@ -54,12 +64,15 @@ 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.", + "Eres Pedro Pascal, el actor chileno-estadounidense: cálido, humilde, con humor y algo ansioso ('internet's daddy')." + + CHAT_STYLE, temperature: 0.8, }, ]; -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/prompt.ts b/apps/api/src/modules/ai/prompt.ts index 0b72b71..3b4da31 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; @@ -39,3 +40,18 @@ 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] ?? ''; +} + +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 new file mode 100644 index 0000000..5c19222 --- /dev/null +++ b/apps/api/src/modules/ai/router.ts @@ -0,0 +1,96 @@ +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; +} + +/** 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: +${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ó). +- 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. +- 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 }> }>({ + 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_LIMIT) + .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}. ¿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}.`; +} + +/** + * Router `addressed` first, then named bots the router missed, then `spontaneous` ones under their cap. A named bot + * 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, + named: readonly 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') 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'); + } + + 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/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/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/src/modules/gateway/presence.service.ts b/apps/api/src/modules/gateway/presence.service.ts index 451966a..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,25 +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); } - public stopBotTyping(botId: string) { + /** 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; } + 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 new file mode 100644 index 0000000..43364aa --- /dev/null +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.service.ts @@ -0,0 +1,209 @@ +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 { 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'; +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, + sleep, + typingDelayMs, +} from './bot-conversation.util.js'; + +/** + * 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 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, + ) {} + + 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. */ + onHumanMessage(channel: Channel, messageId: string, content: string): void { + if (!this.ai.enabled || this.destroyed) return; + + const conversation = this.conversationOf(channel.id); + conversation.pending.push({ id: messageId, content }); + + 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 = []; + + 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 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 })); + } + } + + /** 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; + } + + /** 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); + } + + /** 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; + + // 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))); + } + + /** 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; + + // 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; + } + + // 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 { + if (this.stopped(cycle)) 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); + if (this.stopped(cycle)) return; + + const posted = await this.speak(cycle, bot, [...cycle.spoken]); + if (posted) this.selector.recordSpontaneous(cycle.channel.id, bot); + } + + 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)}`, + ); + } + + /** + * 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, reactingTo?: readonly 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); + const content = await this.ai.reply(bot, history, reactingTo); + 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 }); + + const guildMembers = (await this.userRepository.getGuildUsers(channel.guildId)).map((user) => user.id); + this.emitter.emitToUsers(guildMembers, SocketEvents.MESSAGE_CREATE, { + id, + ...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 })); + return false; + } finally { + this.presence.stopBotTyping(bot.user.id, channel.id); + } + } +} 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..8426622 --- /dev/null +++ b/apps/api/src/modules/message/bot-conversation/bot-conversation.util.ts @@ -0,0 +1,42 @@ +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 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; + /** 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 }>; + /** 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/src/modules/message/message.module.ts b/apps/api/src/modules/message/message.module.ts index 4e4b0ed..530dc1a 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/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.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/src/modules/message/message.service.ts b/apps/api/src/modules/message/message.service.ts index 718c4f8..9e70d7a 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/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/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index eb63339..ef673d6 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,38 +1,22 @@ -import { BOTS, detectBot } from '@/modules/ai/bots.js'; -import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.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'); - }); -}); +import { BOTS, detectNamedBots } from '@/modules/ai/bots.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'; describe('buildPromptMessages', () => { const chayanne = BOTS[1]; 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([ @@ -44,9 +28,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' }]); @@ -72,3 +56,174 @@ describe('cleanReply', () => { expect(cleanReply(chayanne, ' ')).toBe(''); }); }); + +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.')); + 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'])); + 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', () => { + 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); + }); +}); + +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'); + expect(lines.join('\n')).not.toContain('Acaban de responder'); + }); + + 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', () => { + 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('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([])); + + 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' }, + ])); + + 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 new file mode 100644 index 0000000..95f28a4 --- /dev/null +++ b/apps/api/tests/bot-conversation.spec.ts @@ -0,0 +1,532 @@ +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 { 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'; +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'; + +const [, chayanne, pedro] = BOTS; +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)); +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 = ( + 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[], + _reactingTo?: ReadonlyArray, + ): 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, + new SpeakerSelector(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, ai, route, reply, messages, messageRepository }; +} + +describe('typingDelayMs', () => { + 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)); +}); + +describe('BotConversationService', () => { + beforeAll(() => Logger.overrideLogger(false)); + + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it('waits for 0.6 s of silence before routing', async () => { + const { say, route } = setup(); + + say('hola'); + await jest.advanceTimersByTimeAsync(500); + say('¿cómo están?'); + + await jest.advanceTimersByTimeAsync(599); + 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(DEBOUNCE); + + expect(reply).toHaveBeenCalledTimes(1); + expect(reply.mock.calls[0][0]).toBe(chayanne); + }); + + 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: 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); + + 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( + 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(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([chayanne, pedro]); + expect(route).toHaveBeenCalledTimes(2); + 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([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + 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(DEBOUNCE); + expect(route).toHaveBeenCalledTimes(2); + 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, messageRepository } = 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(DEBOUNCE); + say('jaja'); + firstReply.resolve('¡yo, mi amor!'); + // 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); + + 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 spontaneous speaker', async () => { + const { say, route, reply } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]); + + 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 while routing skips an unnamed addressed speaker but lets a named one answer', async () => { + const { say, route, reply } = setup(); + const decision = Promise.withResolvers(); + route.mockImplementationOnce(async () => decision.promise); + + say('pedro, ¿y tú qué bailas?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + say('jaja'); + decision.resolve([{ bot: chayanne, kind: 'addressed' }]); + await jest.advanceTimersByTimeAsync(0); + + expect(reply.mock.calls.map(([bot]) => bot)).toEqual([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 () => { + const { say, route, reply } = setup(); + const decision = Promise.withResolvers(); + route.mockImplementationOnce(async () => decision.promise); + + say('chayanne, ¿qué cantas?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + 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' }]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('chayanne, ¿qué cantas?'); + 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(DEBOUNCE); + 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(DEBOUNCE - 1); + 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(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + 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('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(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + + 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(DEBOUNCE + typing(replyOf(chayanne)) + READ_PAUSE); + + 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 = []) => + exclude.length > 0 ? [] : [{ bot: pedro, kind: 'spontaneous' }], + ); + + 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(route.mock.calls.filter(([, , exclude = []]) => exclude.length === 0)).toHaveLength(2); + expect(reply).toHaveBeenCalledTimes(1); + 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' }]); + // 54 characters: 1.35 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(1349); + 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(); + + say('chayanne hola'); + service.onModuleDestroy(); + await jest.advanceTimersByTimeAsync(5000); + + 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(DEBOUNCE); + + 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, messageRepository } = setup(); + route.mockResolvedValueOnce([ + { bot: chayanne, kind: 'addressed' }, + { bot: pedro, kind: 'spontaneous' }, + ]); + const firstReply = Promise.withResolvers(); + reply.mockImplementationOnce(async () => firstReply.promise); + + say('¿quién baila mejor?'); + await jest.advanceTimersByTimeAsync(DEBOUNCE); + service.onModuleDestroy(); + firstReply.resolve('¡yo, mi amor!'); + await jest.advanceTimersByTimeAsync(typing('¡yo, mi amor!') + READ_PAUSE); + + expect(reply).toHaveBeenCalledTimes(1); + 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(); + }); +}); 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/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 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: