From 6b58df05260bb5b7c7b81c12aa3acc4c900865a0 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 15:56:57 -0600 Subject: [PATCH 1/7] refactor(api): define ai bots as data with stricter triggers Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/bots.ts | 65 +++++++++++++++++++++++++++++++++ apps/api/tests/ai-bots.spec.ts | 25 +++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 apps/api/src/modules/ai/bots.ts create mode 100644 apps/api/tests/ai-bots.spec.ts diff --git a/apps/api/src/modules/ai/bots.ts b/apps/api/src/modules/ai/bots.ts new file mode 100644 index 0000000..9a60f3c --- /dev/null +++ b/apps/api/src/modules/ai/bots.ts @@ -0,0 +1,65 @@ +import type { AppUser } from '@/infrastructure/database/schema/index.js'; + +export interface AIBot { + user: AppUser; + /** Case-insensitive and without the `g` flag, so `test` stays stateless. */ + trigger: RegExp; + systemPrompt: string; + temperature: number; +} + +/** Order matters: when a message names several bots, the first one replies. */ +export const BOTS: AIBot[] = [ + { + user: { + id: '25d23d6a-b162-4e4b-a674-d4c41934728d', + username: 'Chat yipiyi', + email: 'chatgpt@openai.com', + isBot: true, + 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.`, + temperature: 0.7, + }, + { + user: { + id: '42951c8d-51ba-453b-9758-b632020475a9', + username: 'chayanne', + email: 'chayanne@fidechat.com', + isBot: true, + 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 + `, + temperature: 0.8, + }, + { + user: { + id: '61e3b006-c1db-4e86-8ee2-434a86674040', + username: 'pedro_pascal', + email: 'pedro.pascal@fidechat.com', + isBot: true, + avatarUrl: 'https://media.themoviedb.org/t/p/w500/9VYK7oxcqhjd5LAH6ZFJ3XzOlID.jpg', + }, + trigger: /\b(?:pedro|pascal)\b/i, + 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, + }, +]; + +export function detectBot(content: string): AIBot | null { + return BOTS.find((bot) => bot.trigger.test(content)) ?? null; +} diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts new file mode 100644 index 0000000..1281310 --- /dev/null +++ b/apps/api/tests/ai-bots.spec.ts @@ -0,0 +1,25 @@ +import { detectBot } from '@/modules/ai/bots.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'); + }); +}); From 0912f036064f7df667150cbcd5a2cb80b94835f5 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 15:57:33 -0600 Subject: [PATCH 2/7] feat(api): build ai bot prompts from channel history Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/prompt.ts | 34 +++++++++++++++++++++++++ apps/api/tests/ai-bots.spec.ts | 41 ++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/modules/ai/prompt.ts diff --git a/apps/api/src/modules/ai/prompt.ts b/apps/api/src/modules/ai/prompt.ts new file mode 100644 index 0000000..aa8b3d5 --- /dev/null +++ b/apps/api/src/modules/ai/prompt.ts @@ -0,0 +1,34 @@ +import type { ModelMessage } from 'ai'; + +import type { AIBot } from './bots.js'; + +/** `message.content` is a `varchar(2000)`. */ +export const MAX_MESSAGE_LENGTH = 2000; + +export interface ChannelHistoryEntry { + authorId: string; + authorName: string; + content: string; +} + +/** + * The bot's own messages are its turns; everything else, other bots included, is user input prefixed with the + * author's name so the model can tell who said what. + */ +export function buildPromptMessages(bot: AIBot, history: ChannelHistoryEntry[]): ModelMessage[] { + return history.map((entry) => + entry.authorId === bot.user.id + ? { role: 'assistant', content: entry.content } + : { role: 'user', content: `${entry.authorName}: ${entry.content}` }, + ); +} + +/** Drops the `": "` prefix the model may copy from the history format. */ +export function cleanReply(bot: AIBot, text: string): string { + const ownPrefix = `${bot.user.username}:`; + let reply = text.trim(); + if (reply.toLowerCase().startsWith(ownPrefix.toLowerCase())) { + reply = reply.slice(ownPrefix.length).trim(); + } + return reply.slice(0, MAX_MESSAGE_LENGTH); +} diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index 1281310..cfac6ea 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -1,4 +1,5 @@ -import { detectBot } from '@/modules/ai/bots.js'; +import { BOTS, detectBot } from '@/modules/ai/bots.js'; +import { buildPromptMessages, cleanReply, MAX_MESSAGE_LENGTH } from '@/modules/ai/prompt.js'; describe('detectBot', () => { it.each([ @@ -23,3 +24,41 @@ describe('detectBot', () => { expect(detectBot('pedro y chayanne')?.user.username).toBe('chayanne'); }); }); + +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' }, + ]; + + expect(buildPromptMessages(chayanne, history)).toEqual([ + { role: 'user', content: 'juan: hola chayanne' }, + { role: 'assistant', content: '¡Hola mi amor!' }, + { role: 'user', content: 'pedro_pascal: yo también' }, + ]); + }); +}); + +describe('cleanReply', () => { + const chayanne = BOTS[1]; + + it('strips its own name prefix and trims', () => { + expect(cleanReply(chayanne, ' chayanne: hola ')).toBe('hola'); + }); + + it('keeps other text intact', () => { + expect(cleanReply(chayanne, 'juan: dijo algo')).toBe('juan: dijo algo'); + }); + + it('truncates to 2000 chars', () => { + expect(cleanReply(chayanne, 'a'.repeat(2500))).toHaveLength(MAX_MESSAGE_LENGTH); + }); + + it('returns empty for blank output', () => { + expect(cleanReply(chayanne, ' ')).toBe(''); + }); +}); From bce14a2d8e6e4355ad19884db39053b1692b2764 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 15:59:10 -0600 Subject: [PATCH 3/7] refactor(api)!: run ai bots on the vercel ai sdk with channel history The bots no longer depend on OpenAI: the model comes from AI_MODEL (provider:model, default anthropic:claude-haiku-5-5) through an AI SDK provider registry with Anthropic and OpenAI. Each reply now reads the last 30 channel messages, so the bots keep the conversation context. BREAKING CHANGE: OPENAI_API_KEY is no longer required. Set AI_MODEL and the provider key (ANTHROPIC_API_KEY or OPENAI_API_KEY); without the key the bots stay off and the api still boots. Co-Authored-By: Claude Opus 5.5 --- apps/api/.env.sample | 4 +- apps/api/package.json | 4 +- apps/api/src/config/environment.ts | 14 +- apps/api/src/modules/ai/ai-command.service.ts | 295 ------------------ apps/api/src/modules/ai/ai.module.ts | 6 +- apps/api/src/modules/ai/ai.service.ts | 73 +++++ .../src/modules/gateway/presence.service.ts | 8 +- .../src/modules/message/message.repository.ts | 23 +- .../src/modules/message/message.service.ts | 43 ++- apps/api/tests/presence.e2e-spec.ts | 4 +- apps/api/types/global.d.ts | 6 +- pnpm-lock.yaml | 139 ++++++--- 12 files changed, 250 insertions(+), 369 deletions(-) delete mode 100644 apps/api/src/modules/ai/ai-command.service.ts create mode 100644 apps/api/src/modules/ai/ai.service.ts diff --git a/apps/api/.env.sample b/apps/api/.env.sample index 5f6723c..b2b0d77 100644 --- a/apps/api/.env.sample +++ b/apps/api/.env.sample @@ -38,5 +38,7 @@ KEYCLOAK_CLIENT_SECRET= AUTH_CALLBACK_URL=http://localhost:3000/api/v1/auth/callback PUBLIC_WEB_URL=http://localhost:3000/ -# chat yipiti +# AI bots. Format provider:model (anthropic | openai). Bots stay off without the provider's key. +AI_MODEL=anthropic:claude-haiku-5-5 +ANTHROPIC_API_KEY= OPENAI_API_KEY= diff --git a/apps/api/package.json b/apps/api/package.json index 575fd56..d4b23be 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -28,6 +28,8 @@ "typecheck": "tsc --noEmit -p tsconfig.json" }, "dependencies": { + "@ai-sdk/anthropic": "^4.0.105", + "@ai-sdk/openai": "^4.0.118", "@fastify/compress": "^9.3.0", "@fastify/helmet": "^13.2.0", "@fastify/static": "^10.1.8", @@ -41,6 +43,7 @@ "@nestjs/throttler": "^6.7.1", "@nestjs/websockets": "^12.3.2", "@sentry/nestjs": "^11.10.0", + "ai": "^7.0.173", "class-transformer": "^0.5.1", "class-validator": "^0.15.1", "dotenv": "^18.0.9", @@ -49,7 +52,6 @@ "jsonwebtoken": "^9.0.3", "jwks-rsa": "^4.1.0", "nestjs-pino": "^5.3.1", - "openai": "^7.47.0", "pg": "^8.23.1", "pino": "^10.4.0", "pino-http": "^11.0.0", diff --git a/apps/api/src/config/environment.ts b/apps/api/src/config/environment.ts index f0d1ad0..ac8fa94 100644 --- a/apps/api/src/config/environment.ts +++ b/apps/api/src/config/environment.ts @@ -1,4 +1,4 @@ -import { IsEnum, IsNumber, IsOptional, IsString, Min } from 'class-validator'; +import { IsEnum, IsNumber, IsOptional, IsString, Matches, Min } from 'class-validator'; export enum Environment { Development = 'development', @@ -62,6 +62,16 @@ export class EnvironmentVariables implements IEnvironmentVariables { @IsString() PUBLIC_WEB_URL!: string; + // AI bots + @IsOptional() + @Matches(/^(anthropic|openai):.+$/, { message: 'AI_MODEL must look like anthropic: or openai:' }) + AI_MODEL?: string; + + @IsOptional() + @IsString() + ANTHROPIC_API_KEY?: string; + + @IsOptional() @IsString() - OPENAI_API_KEY!: string; + OPENAI_API_KEY?: string; } diff --git a/apps/api/src/modules/ai/ai-command.service.ts b/apps/api/src/modules/ai/ai-command.service.ts deleted file mode 100644 index aa3bf03..0000000 --- a/apps/api/src/modules/ai/ai-command.service.ts +++ /dev/null @@ -1,295 +0,0 @@ -/* eslint-disable complexity -- there is no complexity just configuring prompts */ - -// ai-command.service.ts -import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; -import OpenAI from 'openai'; - -import type { AppUser } from '@/infrastructure/database/schema/index.js'; - -import { Message } from '../message/message.repository.js'; -import { UserRepository } from '../user/user.repository.js'; - -export interface AICommandResult { - success: boolean; - response?: string; - error?: string; - model?: string; - usage?: { - promptTokens: number; - completionTokens: number; - totalTokens: number; - }; -} - -export const IAUserModels: AppUser[] = [ - { - id: '25d23d6a-b162-4e4b-a674-d4c41934728d', - username: 'Chat yipiyi', - email: 'chatgpt@openai.com', - isBot: true, - avatarUrl: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTMdM9MEQ0ExL1PmInT3U5I8v63YXBEdoIT0Q&s', - }, - { - id: '42951c8d-51ba-453b-9758-b632020475a9', - username: 'chayanne', - email: 'chayanne@fidechat.com', - isBot: true, - avatarUrl: 'https://i.pinimg.com/474x/06/90/b5/0690b5003ba0760a39b9f88e8066dab5.jpg', - }, - { - id: '61e3b006-c1db-4e86-8ee2-434a86674040', - username: 'pedro_pascal', - email: 'pedro.pascal@fidechat.com', - isBot: true, - avatarUrl: 'https://media.themoviedb.org/t/p/w500/9VYK7oxcqhjd5LAH6ZFJ3XzOlID.jpg', - }, -]; - -export interface AICommandInput { - message: string; - userId: string; - channelId: string; -} - -@Injectable() -export class AICommandService implements OnModuleInit { - private readonly logger = new Logger('AICommandService'); - private readonly openai: OpenAI; - - constructor( - private readonly config: ConfigService, - private readonly userRepository: UserRepository, - ) { - this.openai = new OpenAI({ - apiKey: this.config.getOrThrow('OPENAI_API_KEY'), - }); - } - - public async onModuleInit() { - for (const user of IAUserModels) { - try { - await this.userRepository.upsertUser(user); - this.logger.debug(`IA bot ${user.username} created/updated successfully`); - } catch (error) { - this.logger.error(new Error(`Error creating/updating IA bot ${user.username}`, { cause: error })); - } - } - - this.logger.log('All IA bots created/updated successfully'); - } - - async processAICommand({ message, userId, channelId }: AICommandInput): Promise | null> { - try { - const aiCommand = this.parseAICommand(message); - if (!aiCommand) { - this.logger.debug(`No AI command detected.`); - return null; - } - - this.logger.debug(`Processing AI command: ${aiCommand.type} for user ${userId} in channel ${channelId}`); - - let result: AICommandResult; - let botId: string; - - switch (aiCommand.type) { - case 'gpt': - botId = IAUserModels[0].id; // Chat yipiyi - result = await this.handleGPTCommand(aiCommand.query); - break; - case 'chayanne': - botId = IAUserModels[1].id; // Chayanne - result = await this.handleChayanneCommand(aiCommand.query); - break; - case 'pedro': - botId = IAUserModels[2].id; // Pedro Pascal - result = await this.handlePedroPascalCommand(aiCommand.query); - break; - default: - this.logger.debug(`Unknown AI command: ${aiCommand.type}`); - return null; - } - - if (result.success && result.response) { - return { - content: result.response, - channelId, - authorId: botId, - createdAt: new Date(), - } satisfies Omit; - } - - return null; - } catch (error) { - this.logger.error(error); - return null; - } - } - - public parseAICommand(message: string): { type: string; query: string; botId: string } | null { - const trimmedMessage = message.trim(); - - const patterns = [ - { - regex: /(?:chat\s*(?:gpt|yipiyi)|gpt|yipiyi|chat\s*(?:bot)?)/gi, - type: 'gpt', - botId: '25d23d6a-b162-4e4b-a674-d4c41934728d', - }, - { regex: /chayanne?/gi, type: 'chayanne', botId: '42951c8d-51ba-453b-9758-b632020475a9' }, - { - regex: /(?:pedro\s*pascal|pedro|pascal)/gi, - type: 'pedro', - botId: '61e3b006-c1db-4e86-8ee2-434a86674040', - }, - ]; - - for (const pattern of patterns) { - const match = trimmedMessage.match(pattern.regex); - if (match) { - return { - type: pattern.type, - query: message, - botId: pattern.botId, - }; - } - } - - return null; - } - - private async handleGPTCommand(query: string): Promise { - try { - const completion = await this.openai.chat.completions.create({ - model: 'gpt-3.5-turbo', - messages: [ - { - role: 'system', - content: `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.`, - }, - { - role: 'user', - content: query, - }, - ], - max_tokens: 500, - temperature: 0.7, - }); - - const response = completion.choices[0]?.message?.content; - if (!response) { - return { success: false, error: 'No response generated' }; - } - - return { - success: true, - response: response.trim(), - model: completion.model, - usage: { - promptTokens: completion.usage?.prompt_tokens || 0, - completionTokens: completion.usage?.completion_tokens || 0, - totalTokens: completion.usage?.total_tokens || 0, - }, - }; - } catch (error) { - this.logger.error(`Error in GPT command: ${error}`); - return { success: false, error: 'Error processing GPT request' }; - } - } - - /** - * Maneja comandos de Chayanne - */ - private async handleChayanneCommand(query: string): Promise { - try { - const completion = await this.openai.chat.completions.create({ - model: 'gpt-3.5-turbo', - messages: [ - { - role: 'system', - content: `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 - `, - }, - { - role: 'user', - content: query, - }, - ], - max_tokens: 500, - temperature: 0.8, // Más creatividad para la personalidad - }); - - const response = completion.choices[0]?.message?.content; - if (!response) { - return { success: false, error: 'No response generated by Chayanne' }; - } - - return { - success: true, - response: response.trim(), - model: `chayanne-${completion.model}`, - usage: { - promptTokens: completion.usage?.prompt_tokens || 0, - completionTokens: completion.usage?.completion_tokens || 0, - totalTokens: completion.usage?.total_tokens || 0, - }, - }; - } catch (error) { - this.logger.error(`Error in Chayanne command: ${error}`); - return { success: false, error: 'Error procesando consulta a Chayanne' }; - } - } - - /** - * Maneja comandos de Pedro Pascal - */ - private async handlePedroPascalCommand(query: string): Promise { - try { - const completion = await this.openai.chat.completions.create({ - model: 'gpt-3.5-turbo', - messages: [ - { - role: 'system', - content: - "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.", - }, - - { - role: 'user', - content: query, - }, - ], - max_tokens: 500, - temperature: 0.8, - }); - - const response = completion.choices[0]?.message?.content; - if (!response) { - return { success: false, error: 'No response generated by Pedro Pascal' }; - } - - return { - success: true, - response: response.trim(), - model: `pedro-${completion.model}`, - usage: { - promptTokens: completion.usage?.prompt_tokens || 0, - completionTokens: completion.usage?.completion_tokens || 0, - totalTokens: completion.usage?.total_tokens || 0, - }, - }; - } catch (error) { - this.logger.error(`Error in Pedro Pascal command: ${error}`); - return { success: false, error: 'Error procesando consulta a Pedro Pascal' }; - } - } -} diff --git a/apps/api/src/modules/ai/ai.module.ts b/apps/api/src/modules/ai/ai.module.ts index cda55c8..96f9132 100644 --- a/apps/api/src/modules/ai/ai.module.ts +++ b/apps/api/src/modules/ai/ai.module.ts @@ -1,11 +1,11 @@ import { Module } from '@nestjs/common'; import { UserModule } from '../user/user.module.js'; -import { AICommandService } from './ai-command.service.js'; +import { AIService } from './ai.service.js'; @Module({ imports: [UserModule], - providers: [AICommandService], - exports: [AICommandService], + providers: [AIService], + exports: [AIService], }) export class AIModule {} diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts new file mode 100644 index 0000000..25693ac --- /dev/null +++ b/apps/api/src/modules/ai/ai.service.ts @@ -0,0 +1,73 @@ +import { createAnthropic } from '@ai-sdk/anthropic'; +import { createOpenAI } from '@ai-sdk/openai'; +import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { createProviderRegistry, generateText } from 'ai'; + +import { UserRepository } from '../user/user.repository.js'; +import { type AIBot, BOTS } from './bots.js'; +import { buildPromptMessages, type ChannelHistoryEntry, cleanReply } from './prompt.js'; + +const DEFAULT_AI_MODEL = 'anthropic:claude-haiku-5-5'; +const PROVIDER_KEYS = { anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY' } as const; +type ProviderId = keyof typeof PROVIDER_KEYS; + +@Injectable() +export class AIService implements OnModuleInit { + private readonly logger = new Logger(AIService.name); + private readonly modelId: string; + private readonly missingKey: string | null; + private readonly registry; + + constructor( + config: ConfigService, + private readonly userRepository: UserRepository, + ) { + // 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; + const provider = this.modelId.split(':')[0] as ProviderId; + this.missingKey = config.get(PROVIDER_KEYS[provider]) ? null : PROVIDER_KEYS[provider]; + + this.registry = createProviderRegistry({ + anthropic: createAnthropic({ apiKey: config.get('ANTHROPIC_API_KEY') }), + openai: createOpenAI({ apiKey: config.get('OPENAI_API_KEY') }), + }); + } + + get enabled(): boolean { + return this.missingKey === null; + } + + async onModuleInit() { + for (const { user } of BOTS) { + try { + await this.userRepository.upsertUser(user); + } catch (error) { + this.logger.error(new Error(`Error creating/updating AI bot ${user.username}`, { cause: error })); + } + } + + if (this.enabled) { + this.logger.log(`AI bots using ${this.modelId}`); + } else { + this.logger.warn(`AI bots disabled: ${this.missingKey} is not set`); + } + } + + /** Returns `null` when the bots are disabled or the model produced no text; throws on provider errors. */ + async reply(bot: AIBot, history: ChannelHistoryEntry[]): Promise { + if (!this.enabled) return null; + + const { text } = await generateText({ + model: this.registry.languageModel(this.modelId as `${ProviderId}:${string}`), + instructions: bot.systemPrompt, + messages: buildPromptMessages(bot, history), + temperature: bot.temperature, + maxOutputTokens: 500, + maxRetries: 2, + abortSignal: AbortSignal.timeout(30_000), + }); + + return cleanReply(bot, text) || null; + } +} diff --git a/apps/api/src/modules/gateway/presence.service.ts b/apps/api/src/modules/gateway/presence.service.ts index 841c9c6..451966a 100644 --- a/apps/api/src/modules/gateway/presence.service.ts +++ b/apps/api/src/modules/gateway/presence.service.ts @@ -1,7 +1,7 @@ import { type ConnectedUser, SocketEvents } from '@fidechat/shared'; import { Injectable, Logger } from '@nestjs/common'; -import { IAUserModels } from '../ai/ai-command.service.js'; +import { BOTS } from '../ai/bots.js'; import { SocketEmitter } from './socket-emitter.js'; import { parseStatusUpdate } from './status-update.util.js'; @@ -15,7 +15,7 @@ export class PresenceService { readonly #activeUsers = new Map>(); constructor(private readonly emitter: SocketEmitter) { - for (const user of IAUserModels) { + for (const { user } of BOTS) { this.#activeUsers.set(user.id, { status: 'online', isTyping: false, @@ -69,7 +69,7 @@ export class PresenceService { } public startBotTyping(botId: string, channelId: string) { - const user = IAUserModels.find((user) => user.id === botId); + const user = BOTS.find((bot) => bot.user.id === botId)?.user; if (!user) { this.logger.error(`IA user with ID ${botId} not found`); return; @@ -84,7 +84,7 @@ export class PresenceService { } public stopBotTyping(botId: string) { - const user = IAUserModels.find((user) => user.id === botId); + const user = BOTS.find((bot) => bot.user.id === botId)?.user; if (!user) { this.logger.error(`IA user with ID ${botId} not found`); return; diff --git a/apps/api/src/modules/message/message.repository.ts b/apps/api/src/modules/message/message.repository.ts index 6d8ec9e..995499c 100644 --- a/apps/api/src/modules/message/message.repository.ts +++ b/apps/api/src/modules/message/message.repository.ts @@ -1,10 +1,12 @@ import { Inject, Injectable, InternalServerErrorException, Logger, NotFoundException } from '@nestjs/common'; -import { eq } from 'drizzle-orm'; +import { desc, eq } from 'drizzle-orm'; import { v4 } from 'uuid'; import { AuditService } from '@/infrastructure/database/audit/audit.service.js'; import { DRIZZLE, type DrizzleDB } from '@/infrastructure/database/drizzle.provider.js'; -import { type Message, message } from '@/infrastructure/database/schema/index.js'; +import { appUser, type Message, message } from '@/infrastructure/database/schema/index.js'; + +import type { ChannelHistoryEntry } from '../ai/prompt.js'; export type { Message }; @@ -67,6 +69,23 @@ export class MessageRepository { } } + /** The last `limit` messages of a channel, oldest first, with each author's username. */ + async getRecentMessages(channelId: string, limit: number): Promise { + try { + const rows = await this.db + .select({ authorId: message.authorId, authorName: appUser.username, content: message.content }) + .from(message) + .innerJoin(appUser, eq(appUser.id, message.authorId)) + .where(eq(message.channelId, channelId)) + .orderBy(desc(message.createdAt)) + .limit(limit); + return rows.reverse(); + } catch (error) { + this.logger.error(error); + throw new InternalServerErrorException('Failed to get recent messages', { cause: error }); + } + } + /** Reemplaza a `PKG_MESSAGES.UPDATE_MESSAGE`. */ async updateMessage(id: string, content: string, actorId?: string): Promise<{ success: boolean }> { try { diff --git a/apps/api/src/modules/message/message.service.ts b/apps/api/src/modules/message/message.service.ts index 61887d8..718c4f8 100644 --- a/apps/api/src/modules/message/message.service.ts +++ b/apps/api/src/modules/message/message.service.ts @@ -1,13 +1,17 @@ import { SocketEvents } from '@fidechat/shared'; import { HttpException, Injectable, InternalServerErrorException, Logger, NotFoundException } from '@nestjs/common'; -import { AICommandService } from '../ai/ai-command.service.js'; +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 { SocketEmitter } from '../gateway/socket-emitter.js'; import { UserRepository } from '../user/user.repository.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'); @@ -18,7 +22,7 @@ export class MessageService { private readonly presence: PresenceService, private readonly channelRepository: ChannelRepository, private readonly userRepository: UserRepository, - private readonly ai: AICommandService, + private readonly ai: AIService, ) {} private async notifyMessageCreation( @@ -69,33 +73,28 @@ export class MessageService { } private async handleAIResponse(originalMessage: MessageCreationAttributes, channel: Channel): Promise { - const targetModel = this.ai.parseAICommand(originalMessage.content); - if (!targetModel) return; + if (!this.ai.enabled) return; - this.presence.startBotTyping(targetModel.botId, channel.id); + const bot = detectBot(originalMessage.content); + if (!bot) return; - try { - const iaResponse = await this.ai.processAICommand({ - message: originalMessage.content, - userId: originalMessage.authorId, - channelId: originalMessage.channelId, - }); + this.presence.startBotTyping(bot.user.id, channel.id); - if (iaResponse) { - const createAt = new Date(); - const createdAtStr = createAt.toISOString(); + 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 aiMessage = await this.messageRepository.createMessage({ - ...iaResponse, - createdAt: createAt, - }); + 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(iaResponse, aiMessage.id, channel.guildId, createdAtStr); - } + await this.notifyMessageCreation(reply, aiMessage.id, channel.guildId, createdAt.toISOString()); } catch (error) { - this.logger.error(new Error('Error processing AI response', { cause: error })); + this.logger.error(new Error(`Error generating the ${bot.user.username} reply`, { cause: error })); } finally { - this.presence.stopBotTyping(targetModel.botId); + this.presence.stopBotTyping(bot.user.id); } } diff --git a/apps/api/tests/presence.e2e-spec.ts b/apps/api/tests/presence.e2e-spec.ts index 7d07799..58f586a 100644 --- a/apps/api/tests/presence.e2e-spec.ts +++ b/apps/api/tests/presence.e2e-spec.ts @@ -10,7 +10,7 @@ import { io, type Socket } from 'socket.io-client'; import { KeycloakClient } from '@/infrastructure/keycloak/index.js'; -import { IAUserModels } from '../src/modules/ai/ai-command.service.js'; +import { BOTS } from '../src/modules/ai/bots.js'; import { AppModule } from '../src/modules/app.module.js'; /** Resolves with the next `event` payload that matches, or rejects after 3s. */ @@ -122,7 +122,7 @@ describe('Presence and membership E2E', () => { }); expect(res.statusCode).toBe(200); const { connectedUsers } = res.json(); - const botId = IAUserModels[0].id; + const botId = BOTS[0].user.id; expect(entryOf(connectedUsers, bob.id)).toEqual({ userId: bob.id, status: 'online', isTyping: false }); expect(entryOf(connectedUsers, botId)).toEqual({ userId: botId, status: 'online', isTyping: false }); }); diff --git a/apps/api/types/global.d.ts b/apps/api/types/global.d.ts index 538299b..62079c9 100644 --- a/apps/api/types/global.d.ts +++ b/apps/api/types/global.d.ts @@ -26,8 +26,10 @@ declare interface IEnvironmentVariables { AUTH_CALLBACK_URL: string; // Public URL of GET /api/v1/auth/callback, registered in the Keycloak client PUBLIC_WEB_URL: string; // Where the browser lands after login and logout - // chat yipiti - OPENAI_API_KEY: string; + // AI bots + AI_MODEL?: string; // provider:model, defaults to anthropic:claude-haiku-5-5 + ANTHROPIC_API_KEY?: string; + OPENAI_API_KEY?: string; } declare interface IReqUser { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fa398f2..f75fd1f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -184,6 +184,12 @@ importers: apps/api: dependencies: + '@ai-sdk/anthropic': + specifier: ^4.0.105 + version: 4.0.105(zod@4.6.7) + '@ai-sdk/openai': + specifier: ^4.0.118 + version: 4.0.118(zod@4.6.7) '@fastify/compress': specifier: ^9.3.0 version: 9.3.0 @@ -223,6 +229,9 @@ importers: '@sentry/nestjs': specifier: ^11.10.0 version: 11.10.0(@nestjs/common@12.3.2(class-transformer@0.5.1)(class-validator@0.15.1)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/core@12.3.2)(webpack@5.106.2(@swc/core@1.16.21(@swc/helpers@0.5.23))(esbuild@0.28.2)(lightningcss@1.33.0)(postcss@8.5.32)(uglify-js@3.19.3)) + ai: + specifier: ^7.0.173 + version: 7.0.173(zod@4.6.7) class-transformer: specifier: ^0.5.1 version: 0.5.1 @@ -247,9 +256,6 @@ importers: nestjs-pino: specifier: ^5.3.1 version: 5.3.1(@nestjs/common@12.3.2(class-transformer@0.5.1)(class-validator@0.15.1)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/core@12.3.2)(pino-http@11.0.0)(pino@10.4.0)(rxjs@7.8.2) - openai: - specifier: ^7.47.0 - version: 7.47.0(ws@8.21.3)(zod@4.6.7) pg: specifier: ^8.23.1 version: 8.23.1 @@ -552,6 +558,34 @@ importers: packages: + '@ai-sdk/anthropic@4.0.105': + resolution: {integrity: sha512-i6kmv6y+FGTBxOPtLRrb5I+92WcFFGubhd33CD0JeaSyFOnLJ8my4a9olkAgyR9fACHMzxFBmoEHd0uO4i72bA==} + engines: {node: '>=22'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + + '@ai-sdk/gateway@4.0.146': + resolution: {integrity: sha512-rJ5NQSyjnpo6HSBEYc9Ff6k1EROUuB7KeAfHYVbVDsk68ynY0Eb8JeWflZKXlT6NhYgqyCiBDxE5blXWACXMKQ==} + engines: {node: '>=22'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + + '@ai-sdk/openai@4.0.118': + resolution: {integrity: sha512-pBsFeB+IudrX96P0ic9AO/1NPnfB6IyoS2B2pnhBJ/Ovt4bsvAmBOPeUxwEfYbQyfetJdVFep9sfYX42uVX3SQ==} + engines: {node: '>=22'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + + '@ai-sdk/provider-utils@5.0.81': + resolution: {integrity: sha512-pfvvdQ3d1WxdBuug/mnGEP6E48cPokwD8dMBnsFeeAUp0/rb/H2aj0f3mwOiY62gu/Cu5GO44e+SE5nDHyBWMA==} + engines: {node: '>=22'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + + '@ai-sdk/provider@4.0.38': + resolution: {integrity: sha512-GElz70UUCuDFFyD6m6aQJFgRY0HEABQzB5otzgkrisYaF5mnss7o0upAmbBTSD6KWiM9h7lZm1ls2c7xzHR8CA==} + engines: {node: '>=22'} + '@angular-devkit/core@22.2.3': resolution: {integrity: sha512-bi1oBjJeg1XCgRrBYSuJikaJV4sO/mcnyFE/0a1H646yHhQpVYoU/M2Vh69Qv8h+YoMfC7RuSxT4LZHaubhhLw==} engines: {node: ^22.22.3 || ^24.15.0 || >=26.0.0, npm: ^6.11.0 || ^7.5.6 || >=8.0.0, yarn: '>= 1.13.0'} @@ -3957,6 +3991,10 @@ packages: cpu: [x64] os: [win32] + '@vercel/oidc@3.2.0': + resolution: {integrity: sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug==} + engines: {node: '>= 20'} + '@vitejs/plugin-react@6.2.1': resolution: {integrity: sha512-9KyWCij3r0+qLG3Q62vBmv0VyevmYibAtxaCIVK9o0k6CXgaajZRIe8tpfchQcdZArJ5b/7OrIz4W3b2uxVRDA==} engines: {node: ^20.19.0 || >=22.12.0} @@ -4018,6 +4056,9 @@ packages: '@webassemblyjs/wast-printer@1.14.1': resolution: {integrity: sha512-kPSSXE6De1XOR820C90RIo2ogvZG+c3KiHzqUoO/F34Y2shGzesfqv7o57xrxovZJH/MetF5UjroJ/R/3isoiw==} + '@workflow/serde@4.1.0': + resolution: {integrity: sha512-pav4F2BoirECWR7Nf1TKt+2eETcBj7jj4cBefQ8VXQCA6NPkaKeLfj/zMgi+3zYV5ZIBT4GuUiphsj0/b9hPQQ==} + '@xhmikosr/archive-type@8.1.0': resolution: {integrity: sha512-EXOjEbnZFE5c/nFMf4FOrEURVanzHpnkPYmnmr78u02/8hAhE0FMq8p9TK1IM0/bFr5VcyBUY0gfLm8f7dKy+Q==} engines: {node: '>=20'} @@ -4095,6 +4136,12 @@ packages: resolution: {integrity: sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==} engines: {node: '>= 6.0.0'} + ai@7.0.173: + resolution: {integrity: sha512-dLea37zT1h7cjkUsBprU4Rq35HJqhtHHaq1jTAXjHQoyfpeswpgCrhjZJWdWc10gjEAxmz114py7iLGQ3xIMxw==} + engines: {node: '>=22'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + ajv-formats@2.1.1: resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==} peerDependencies: @@ -4916,6 +4963,10 @@ packages: resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==} engines: {node: '>=0.8.x'} + eventsource-parser@3.1.1: + resolution: {integrity: sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ==} + engines: {node: '>=18.0.0'} + execa@5.1.1: resolution: {integrity: sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==} engines: {node: '>=10'} @@ -5537,6 +5588,9 @@ packages: json-schema-traverse@1.0.0: resolution: {integrity: sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==} + json-schema@0.4.0: + resolution: {integrity: sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==} + json-stable-stringify-without-jsonify@1.0.1: resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==} @@ -5884,6 +5938,7 @@ packages: node-abort-controller@3.1.1: resolution: {integrity: sha512-AGK2yQKIjRuqnc6VkX2Xj5d+QW8xZ87pa1UK6yA6ouUyuxfHuMP6umE5QK7UmTeOAymo+Zx1Fxiuw9rVx8taHQ==} + deprecated: This package is no longer needed on supported Node.js versions. Node.js 16+ includes AbortController and AbortSignal as built-in globals. All Node.js versions requiring this polyfill are end-of-life. Use the built-in globals instead. node-addon-api@7.1.1: resolution: {integrity: sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==} @@ -5946,33 +6001,6 @@ packages: resolution: {integrity: sha512-VXJjc87FScF88uafS3JllDgvAm+c/Slfz06lorj2uAY34rlUu0Nt+v8wreiImcrgAjjIHp1rXpTDlLOGw29WwQ==} engines: {node: '>=18'} - openai@7.47.0: - resolution: {integrity: sha512-BYa2RYgocAv9WjPUJmNu8fICuA1fFhkTBEpiR3sC5L1K6v8Hnhc8pcnQEhu8l/+W1XKA+JUaBa7HpT+AK1s77w==} - engines: {node: '>=22.0.0'} - peerDependencies: - '@aws-sdk/credential-provider-node': '>=3.972.0 <4' - '@smithy/hash-node': '>=4.3.0 <5' - '@smithy/signature-v4': '>=5.4.0 <6' - google-auth-library: '>=9.15.1 <12' - undici: '>=5 <9' - ws: ^8.21.0 - zod: ^3.25 || ^4.0 - peerDependenciesMeta: - '@aws-sdk/credential-provider-node': - optional: true - '@smithy/hash-node': - optional: true - '@smithy/signature-v4': - optional: true - google-auth-library: - optional: true - undici: - optional: true - ws: - optional: true - zod: - optional: true - optionator@0.9.4: resolution: {integrity: sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==} engines: {node: '>= 0.8.0'} @@ -7183,6 +7211,37 @@ packages: snapshots: + '@ai-sdk/anthropic@4.0.105(zod@4.6.7)': + dependencies: + '@ai-sdk/provider': 4.0.38 + '@ai-sdk/provider-utils': 5.0.81(zod@4.6.7) + zod: 4.6.7 + + '@ai-sdk/gateway@4.0.146(zod@4.6.7)': + dependencies: + '@ai-sdk/provider': 4.0.38 + '@ai-sdk/provider-utils': 5.0.81(zod@4.6.7) + '@vercel/oidc': 3.2.0 + zod: 4.6.7 + + '@ai-sdk/openai@4.0.118(zod@4.6.7)': + dependencies: + '@ai-sdk/provider': 4.0.38 + '@ai-sdk/provider-utils': 5.0.81(zod@4.6.7) + zod: 4.6.7 + + '@ai-sdk/provider-utils@5.0.81(zod@4.6.7)': + dependencies: + '@ai-sdk/provider': 4.0.38 + '@standard-schema/spec': 1.1.0 + '@workflow/serde': 4.1.0 + eventsource-parser: 3.1.1 + zod: 4.6.7 + + '@ai-sdk/provider@4.0.38': + dependencies: + json-schema: 0.4.0 + '@angular-devkit/core@22.2.3(chokidar@5.0.0)': dependencies: ajv: 8.20.0 @@ -10140,6 +10199,8 @@ snapshots: '@unrs/resolver-binding-win32-x64-msvc@1.13.0': optional: true + '@vercel/oidc@3.2.0': {} + '@vitejs/plugin-react@6.2.1(vite@8.3.6(@types/node@26.6.6)(esbuild@0.28.2)(jiti@2.7.0)(terser@5.50.0)(tsx@4.23.15))': dependencies: '@rolldown/pluginutils': 1.0.1 @@ -10221,6 +10282,8 @@ snapshots: '@webassemblyjs/ast': 1.14.1 '@xtuc/long': 4.2.2 + '@workflow/serde@4.1.0': {} + '@xhmikosr/archive-type@8.1.0(supports-color@8.1.1)': dependencies: file-type: 21.3.4(supports-color@8.1.1) @@ -10345,6 +10408,13 @@ snapshots: transitivePeerDependencies: - supports-color + ai@7.0.173(zod@4.6.7): + dependencies: + '@ai-sdk/gateway': 4.0.146(zod@4.6.7) + '@ai-sdk/provider': 4.0.38 + '@ai-sdk/provider-utils': 5.0.81(zod@4.6.7) + zod: 4.6.7 + ajv-formats@2.1.1(ajv@8.20.0): optionalDependencies: ajv: 8.20.0 @@ -11112,6 +11182,8 @@ snapshots: events@3.3.0: {} + eventsource-parser@3.1.1: {} + execa@5.1.1: dependencies: cross-spawn: 7.0.6 @@ -11948,6 +12020,8 @@ snapshots: json-schema-traverse@1.0.0: {} + json-schema@0.4.0: {} + json-stable-stringify-without-jsonify@1.0.1: {} json5@2.2.3: {} @@ -12285,11 +12359,6 @@ snapshots: dependencies: mimic-function: 5.0.1 - openai@7.47.0(ws@8.21.3)(zod@4.6.7): - optionalDependencies: - ws: 8.21.3 - zod: 4.6.7 - optionator@0.9.4: dependencies: deep-is: 0.1.4 From e47540189027cb3f5426dc13f1db793746194644 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 16:02:55 -0600 Subject: [PATCH 4/7] fix(api): keep ai bot prompts valid for current claude models Drop the bot's own messages at the edges of the history window (Anthropic needs a leading user turn and rejects assistant prefill), disable thinking so it does not consume the 500-token reply budget, warn on empty or truncated replies, and route AI SDK warnings through the Nest logger. Co-Authored-By: Claude Opus 5.5 --- apps/api/src/modules/ai/ai.service.ts | 17 +++++++++++++++-- apps/api/src/modules/ai/prompt.ts | 19 +++++++++++++------ apps/api/tests/ai-bots.spec.ts | 10 ++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index 25693ac..1fd9f3b 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -8,6 +8,9 @@ import { UserRepository } from '../user/user.repository.js'; import { type AIBot, BOTS } from './bots.js'; import { buildPromptMessages, type ChannelHistoryEntry, cleanReply } from './prompt.js'; +// The SDK prints warnings with console.warn, outside pino; `reply` logs them through the Nest logger instead. +globalThis.AI_SDK_LOG_WARNINGS = false; + const DEFAULT_AI_MODEL = 'anthropic:claude-haiku-5-5'; const PROVIDER_KEYS = { anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY' } as const; type ProviderId = keyof typeof PROVIDER_KEYS; @@ -58,16 +61,26 @@ export class AIService implements OnModuleInit { async reply(bot: AIBot, history: ChannelHistoryEntry[]): Promise { if (!this.enabled) return null; - const { text } = await generateText({ + const { text, finishReason, warnings } = await generateText({ model: this.registry.languageModel(this.modelId as `${ProviderId}:${string}`), instructions: bot.systemPrompt, messages: buildPromptMessages(bot, history), temperature: bot.temperature, + // Recent Claude models think by default, and thinking counts against maxOutputTokens. + reasoning: 'none', maxOutputTokens: 500, maxRetries: 2, abortSignal: AbortSignal.timeout(30_000), }); - return cleanReply(bot, text) || null; + for (const warning of warnings ?? []) { + this.logger.debug(`AI SDK warning: ${JSON.stringify(warning)}`); + } + + const reply = cleanReply(bot, text); + if (!reply || finishReason === 'length') { + this.logger.warn(`${bot.user.username} reply finished with ${finishReason} (${reply.length} chars)`); + } + return reply || null; } } diff --git a/apps/api/src/modules/ai/prompt.ts b/apps/api/src/modules/ai/prompt.ts index aa8b3d5..0b72b71 100644 --- a/apps/api/src/modules/ai/prompt.ts +++ b/apps/api/src/modules/ai/prompt.ts @@ -13,14 +13,21 @@ export interface ChannelHistoryEntry { /** * The bot's own messages are its turns; everything else, other bots included, is user input prefixed with the - * author's name so the model can tell who said what. + * author's name so the model can tell who said what. Anthropic requires the first turn to be the user's and rejects + * a final assistant turn (prefill), so the bot's own messages at either edge of the window are dropped. */ export function buildPromptMessages(bot: AIBot, history: ChannelHistoryEntry[]): ModelMessage[] { - return history.map((entry) => - entry.authorId === bot.user.id - ? { role: 'assistant', content: entry.content } - : { role: 'user', content: `${entry.authorName}: ${entry.content}` }, - ); + const first = history.findIndex((entry) => entry.authorId !== bot.user.id); + const last = history.findLastIndex((entry) => entry.authorId !== bot.user.id); + if (first === -1) return []; + + return history + .slice(first, last + 1) + .map((entry) => + entry.authorId === bot.user.id + ? { role: 'assistant', content: entry.content } + : { role: 'user', content: `${entry.authorName}: ${entry.content}` }, + ); } /** Drops the `": "` prefix the model may copy from the history format. */ diff --git a/apps/api/tests/ai-bots.spec.ts b/apps/api/tests/ai-bots.spec.ts index cfac6ea..eb63339 100644 --- a/apps/api/tests/ai-bots.spec.ts +++ b/apps/api/tests/ai-bots.spec.ts @@ -41,6 +41,16 @@ describe('buildPromptMessages', () => { { role: 'user', content: 'pedro_pascal: yo también' }, ]); }); + + 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' }, + ]; + + expect(buildPromptMessages(chayanne, history)).toEqual([{ role: 'user', content: 'juan: hola chayanne' }]); + }); }); describe('cleanReply', () => { From d743936086444562982e8280982684726409c9f9 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 16:02:55 -0600 Subject: [PATCH 5/7] docs(monorepo): describe the optional ai bot configuration Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 ++-- README.md | 11 +++++------ compose.yaml | 9 +++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 7194a53..0615b81 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,7 +59,7 @@ Default local ports: API and Socket.IO on 8080 (`API_PORT`), Keycloak on 8081, w ### Tests -The api only has **e2e tests** (`apps/api/tests/*.e2e-spec.ts`), and the web app has none. `test` and `test:e2e` both run the same Jest suite. The tests boot the full `AppModule` and create and delete real users through the Keycloak admin API, so they need Postgres, Keycloak and a complete `.env` (including `OPENAI_API_KEY`, since env validation fails without it). +The api only has **e2e tests** (`apps/api/tests/*.e2e-spec.ts`), and the web app has none. `test` and `test:e2e` both run the same Jest suite. The tests boot the full `AppModule` and create and delete real users through the Keycloak admin API, so they need Postgres, Keycloak and a complete `.env`. The AI keys are optional: without them the bots stay off. `tests/ai-bots.spec.ts` is a plain unit test that needs no infrastructure. The api is ESM (`"type": "module"`), so relative and `@/` imports carry the `.js` extension. The tests, however, are compiled to CommonJS by ts-jest (Jest's ESM loader trips on a `require(esm)` cycle inside the Nest packages), and the scripts run Jest as `node --experimental-vm-modules .../jest.js` so it can `require()` the ESM-only Nest 12, uuid and jwks-rsa (jose). Use the `test` script rather than a bare `jest`. Node >= 26.13 (current LTS) is required. @@ -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 bot users with hard-coded UUIDs (`IAUserModels`) are upserted on module init and are always shown online. When a non-bot message matches a bot's name pattern, `MessageService` generates the reply asynchronously (fire-and-forget) through OpenAI and toggles the bot's typing status. +**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` or `openai`, default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off. ## Web architecture (`apps/web`) diff --git a/README.md b/README.md index 45db796..82cc297 100644 --- a/README.md +++ b/README.md @@ -120,12 +120,11 @@ The ports are fixed, because the Keycloak realm's redirect URIs use them, and the demo users and data live in `docker/`. > [!NOTE] -> **AI bots and `OPENAI_API_KEY`.** The api needs the variable to boot, so -> `compose.yaml` passes the placeholder `not-set` when it is missing; the bots -> still show up online, but stay silent. To give them a real key, set -> `OPENAI_API_KEY` in the shell or in a root `.env` (git-ignored, so it is the -> right place for it). Even with a valid key the bots do not answer yet: v3 -> still asks for `gpt-3.5-turbo`, which OpenAI has retired. (ima changed it to the sdk from vercel in the next prs i hope) +> **AI bots.** They are optional: without a key they show up online but stay +> silent. To turn them on, set `ANTHROPIC_API_KEY` in the shell or in a root +> `.env` (git-ignored, so it is the right place for it). The model defaults to +> `anthropic:claude-haiku-5-5`; set `AI_MODEL=openai:` and +> `OPENAI_API_KEY` to use OpenAI instead. ## Infrastructure services diff --git a/compose.yaml b/compose.yaml index a4a4a24..959d364 100644 --- a/compose.yaml +++ b/compose.yaml @@ -105,10 +105,11 @@ services: PUBLIC_WEB_URL: http://localhost:4300/ # Guild every user is added to on first login (seeded in docker/postgres/seed.sql). DEFAULT_GUILD_ID: 44444444-4444-4444-4444-444444444444 - # Required by env validation; set a real key in the shell or a root .env. - # The bots stay silent anyway until v3 stops asking for the retired - # gpt-3.5-turbo (see the README's "Quick launch (v3)"). - OPENAI_API_KEY: ${OPENAI_API_KEY:-not-set} + # AI bots: optional. Set the provider key in the shell or a root .env; + # without it the bots show up online but stay silent. + AI_MODEL: ${AI_MODEL:-anthropic:claude-haiku-5-5} + ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-} + OPENAI_API_KEY: ${OPENAI_API_KEY:-} ports: - '4300:8080' depends_on: From c498bd20db4ece1aa9c1d60a6f680f1bcde2c63f Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 16:08:32 -0600 Subject: [PATCH 6/7] feat(api): support ollama as an ai bot provider AI_MODEL=ollama: talks to Ollama through its OpenAI-compatible API (@ai-sdk/openai-compatible) at OLLAMA_BASE_URL, default http://localhost:11434/v1. It needs no key, so the bots are on whenever it is selected. The demo compose reaches an Ollama on the host. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- README.md | 3 ++- apps/api/.env.sample | 4 +++- apps/api/package.json | 1 + apps/api/src/config/environment.ts | 8 +++++++- apps/api/src/modules/ai/ai.service.ts | 13 +++++++++++-- apps/api/types/global.d.ts | 1 + compose.yaml | 4 ++++ pnpm-lock.yaml | 15 +++++++++++++++ 9 files changed, 45 insertions(+), 6 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0615b81..2087d42 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` or `openai`, default `anthropic:claude-haiku-5-5`); without that provider's key the bots stay off. +**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). ## Web architecture (`apps/web`) diff --git a/README.md b/README.md index 82cc297..bd79d75 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,8 @@ the demo users and data live in `docker/`. > silent. To turn them on, set `ANTHROPIC_API_KEY` in the shell or in a root > `.env` (git-ignored, so it is the right place for it). The model defaults to > `anthropic:claude-haiku-5-5`; set `AI_MODEL=openai:` and -> `OPENAI_API_KEY` to use OpenAI instead. +> `OPENAI_API_KEY` to use OpenAI instead, or `AI_MODEL=ollama:` (no +> key) to use an Ollama running on the host. ## Infrastructure services diff --git a/apps/api/.env.sample b/apps/api/.env.sample index b2b0d77..2b3b5a7 100644 --- a/apps/api/.env.sample +++ b/apps/api/.env.sample @@ -38,7 +38,9 @@ KEYCLOAK_CLIENT_SECRET= AUTH_CALLBACK_URL=http://localhost:3000/api/v1/auth/callback PUBLIC_WEB_URL=http://localhost:3000/ -# AI bots. Format provider:model (anthropic | openai). Bots stay off without the provider's key. +# AI bots. Format provider:model (anthropic | openai | ollama). Bots stay off without the provider's key; +# ollama needs no key (e.g. AI_MODEL=ollama:llama3.2). AI_MODEL=anthropic:claude-haiku-5-5 ANTHROPIC_API_KEY= OPENAI_API_KEY= +OLLAMA_BASE_URL=http://localhost:11434/v1 diff --git a/apps/api/package.json b/apps/api/package.json index d4b23be..e0183d2 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -30,6 +30,7 @@ "dependencies": { "@ai-sdk/anthropic": "^4.0.105", "@ai-sdk/openai": "^4.0.118", + "@ai-sdk/openai-compatible": "^3.0.92", "@fastify/compress": "^9.3.0", "@fastify/helmet": "^13.2.0", "@fastify/static": "^10.1.8", diff --git a/apps/api/src/config/environment.ts b/apps/api/src/config/environment.ts index ac8fa94..3cf9add 100644 --- a/apps/api/src/config/environment.ts +++ b/apps/api/src/config/environment.ts @@ -64,7 +64,9 @@ export class EnvironmentVariables implements IEnvironmentVariables { // AI bots @IsOptional() - @Matches(/^(anthropic|openai):.+$/, { message: 'AI_MODEL must look like anthropic: or openai:' }) + @Matches(/^(anthropic|openai|ollama):.+$/, { + message: 'AI_MODEL must look like anthropic:, openai: or ollama:', + }) AI_MODEL?: string; @IsOptional() @@ -74,4 +76,8 @@ export class EnvironmentVariables implements IEnvironmentVariables { @IsOptional() @IsString() OPENAI_API_KEY?: string; + + @IsOptional() + @IsString() + OLLAMA_BASE_URL?: string; } diff --git a/apps/api/src/modules/ai/ai.service.ts b/apps/api/src/modules/ai/ai.service.ts index 1fd9f3b..64c3cfb 100644 --- a/apps/api/src/modules/ai/ai.service.ts +++ b/apps/api/src/modules/ai/ai.service.ts @@ -1,5 +1,6 @@ import { createAnthropic } from '@ai-sdk/anthropic'; 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'; @@ -12,7 +13,9 @@ import { buildPromptMessages, type ChannelHistoryEntry, cleanReply } from './pro globalThis.AI_SDK_LOG_WARNINGS = false; const DEFAULT_AI_MODEL = 'anthropic:claude-haiku-5-5'; -const PROVIDER_KEYS = { anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY' } as const; +const DEFAULT_OLLAMA_BASE_URL = 'http://localhost:11434/v1'; +/** The env var holding each provider's API key; Ollama runs locally and needs none. */ +const PROVIDER_KEYS = { anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY', ollama: null } as const; type ProviderId = keyof typeof PROVIDER_KEYS; @Injectable() @@ -29,11 +32,17 @@ 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; const provider = this.modelId.split(':')[0] as ProviderId; - this.missingKey = config.get(PROVIDER_KEYS[provider]) ? null : PROVIDER_KEYS[provider]; + const keyName = PROVIDER_KEYS[provider]; + this.missingKey = keyName && !config.get(keyName) ? keyName : null; this.registry = createProviderRegistry({ anthropic: createAnthropic({ apiKey: config.get('ANTHROPIC_API_KEY') }), openai: createOpenAI({ apiKey: config.get('OPENAI_API_KEY') }), + // Ollama serves an OpenAI-compatible API under /v1. + ollama: createOpenAICompatible({ + name: 'ollama', + baseURL: config.get('OLLAMA_BASE_URL') || DEFAULT_OLLAMA_BASE_URL, + }), }); } diff --git a/apps/api/types/global.d.ts b/apps/api/types/global.d.ts index 62079c9..cdcc719 100644 --- a/apps/api/types/global.d.ts +++ b/apps/api/types/global.d.ts @@ -30,6 +30,7 @@ declare interface IEnvironmentVariables { AI_MODEL?: string; // provider:model, defaults to anthropic:claude-haiku-5-5 ANTHROPIC_API_KEY?: string; OPENAI_API_KEY?: string; + OLLAMA_BASE_URL?: string; // defaults to http://localhost:11434/v1 } declare interface IReqUser { diff --git a/compose.yaml b/compose.yaml index 959d364..6cac1ef 100644 --- a/compose.yaml +++ b/compose.yaml @@ -110,6 +110,10 @@ services: AI_MODEL: ${AI_MODEL:-anthropic:claude-haiku-5-5} ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-} OPENAI_API_KEY: ${OPENAI_API_KEY:-} + # 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: + - 'host.docker.internal:host-gateway' ports: - '4300:8080' depends_on: diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f75fd1f..fd7c57d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -190,6 +190,9 @@ importers: '@ai-sdk/openai': specifier: ^4.0.118 version: 4.0.118(zod@4.6.7) + '@ai-sdk/openai-compatible': + specifier: ^3.0.92 + version: 3.0.92(zod@4.6.7) '@fastify/compress': specifier: ^9.3.0 version: 9.3.0 @@ -570,6 +573,12 @@ packages: peerDependencies: zod: ^3.25.76 || ^4.1.8 + '@ai-sdk/openai-compatible@3.0.92': + resolution: {integrity: sha512-2gV7HHV4qpx+8GotOZMmGqY270ExkftwFG5+DM6nChLmRaP6WP9JKj4e9WjLhih/GsPL9dSGnZHzwCsofHwiAQ==} + engines: {node: '>=22'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + '@ai-sdk/openai@4.0.118': resolution: {integrity: sha512-pBsFeB+IudrX96P0ic9AO/1NPnfB6IyoS2B2pnhBJ/Ovt4bsvAmBOPeUxwEfYbQyfetJdVFep9sfYX42uVX3SQ==} engines: {node: '>=22'} @@ -7224,6 +7233,12 @@ snapshots: '@vercel/oidc': 3.2.0 zod: 4.6.7 + '@ai-sdk/openai-compatible@3.0.92(zod@4.6.7)': + dependencies: + '@ai-sdk/provider': 4.0.38 + '@ai-sdk/provider-utils': 5.0.81(zod@4.6.7) + zod: 4.6.7 + '@ai-sdk/openai@4.0.118(zod@4.6.7)': dependencies: '@ai-sdk/provider': 4.0.38 From f7cfb190bd077d957439dd5768f7534c776543d2 Mon Sep 17 00:00:00 2001 From: Datzu712 <41zohan@gmail.com> Date: Mon, 16 Nov 2026 16:32:19 -0600 Subject: [PATCH 7/7] docs(api): recommend gpt-6-luna for the openai provider Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +++--- apps/api/.env.sample | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bd79d75..d205f6b 100644 --- a/README.md +++ b/README.md @@ -123,9 +123,9 @@ the demo users and data live in `docker/`. > **AI bots.** They are optional: without a key they show up online but stay > silent. To turn them on, set `ANTHROPIC_API_KEY` in the shell or in a root > `.env` (git-ignored, so it is the right place for it). The model defaults to -> `anthropic:claude-haiku-5-5`; set `AI_MODEL=openai:` and -> `OPENAI_API_KEY` to use OpenAI instead, or `AI_MODEL=ollama:` (no -> key) to use an Ollama running on the host. +> `anthropic:claude-haiku-5-5`; set `AI_MODEL=openai:gpt-6-luna` (the +> 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. ## Infrastructure services diff --git a/apps/api/.env.sample b/apps/api/.env.sample index 2b3b5a7..a9494df 100644 --- a/apps/api/.env.sample +++ b/apps/api/.env.sample @@ -39,7 +39,7 @@ AUTH_CALLBACK_URL=http://localhost:3000/api/v1/auth/callback PUBLIC_WEB_URL=http://localhost:3000/ # AI bots. Format provider:model (anthropic | openai | ollama). Bots stay off without the provider's key; -# ollama needs no key (e.g. AI_MODEL=ollama:llama3.2). +# 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 ANTHROPIC_API_KEY= OPENAI_API_KEY=