From a9858ef876f929bc13ec9061f16bf5261b616567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= <35082514+alezmad@users.noreply.github.com> Date: Mon, 13 Apr 2026 21:06:08 +0100 Subject: [PATCH] fix(broker): teach AI difference between mesh names and peer names Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/broker/src/telegram-ai.ts | 16 +++++++++++----- apps/broker/src/telegram-bridge.ts | 5 +++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/broker/src/telegram-ai.ts b/apps/broker/src/telegram-ai.ts index a2e76fa..f1e45f9 100644 --- a/apps/broker/src/telegram-ai.ts +++ b/apps/broker/src/telegram-ai.ts @@ -150,12 +150,18 @@ You have access to tools for mesh operations. When the user's intent maps to a t IMPORTANT: Always respond in the same language the user writes in. If they write in Spanish, respond in Spanish. If English, respond in English. +Key concepts: +- A MESH is a workspace/group (like "flexicar", "alexis-mou"). This Telegram chat can be connected to multiple meshes. +- A PEER is a person/agent connected to a mesh (like "Nedas", "Mou"). +- When user says "send to ", they mean BROADCAST to all peers in that mesh. Use send_message with to="*" — the system will route to the correct mesh. +- When user says "send to ", they mean a direct message to that peer. + Rules: - Be concise — Telegram messages should be short - When sending messages to peers, preserve the user's tone and intent -- For ambiguous peer names, ask for clarification -- Never fabricate peer names or data — use list_peers to find real names -- If unsure which mesh to target, ask the user`; +- If the target looks like a mesh name (matches one from context), broadcast to it +- Never fabricate peer names — use list_peers to find real names +- Default to the first connected mesh if not specified`; // --------------------------------------------------------------------------- // AI Engine @@ -177,13 +183,13 @@ function getClient(): Anthropic { */ export async function processMessage( userMessage: string, - context: { meshSlug?: string; userName?: string; recentPeers?: string[] }, + context: { meshSlug?: string; meshSlugs?: string[]; userName?: string; recentPeers?: string[] }, ): Promise { try { const anthropic = getClient(); const contextInfo = [ - context.meshSlug ? `Current mesh: ${context.meshSlug}` : null, + context.meshSlugs?.length ? `Connected meshes: ${context.meshSlugs.join(", ")}` : context.meshSlug ? `Current mesh: ${context.meshSlug}` : null, context.userName ? `User's name: ${context.userName}` : null, context.recentPeers?.length ? `Known peers: ${context.recentPeers.join(", ")}` : null, ].filter(Boolean).join(". "); diff --git a/apps/broker/src/telegram-bridge.ts b/apps/broker/src/telegram-bridge.ts index 6930423..00ad971 100644 --- a/apps/broker/src/telegram-bridge.ts +++ b/apps/broker/src/telegram-bridge.ts @@ -1603,7 +1603,7 @@ function setupBotCommands( // Gather context for the AI const firstMeshId = meshIds[0]!; const firstConn = meshConnections.get(firstMeshId); - const meshSlug = meshSlugs.get(firstMeshId) ?? firstMeshId.slice(0, 12); + const allMeshSlugs = meshIds.map(id => meshSlugs.get(id) ?? id.slice(0, 12)); let recentPeers: string[] = []; if (firstConn?.isConnected()) { try { @@ -1613,7 +1613,8 @@ function setupBotCommands( } const result = await processMessage(text, { - meshSlug, + meshSlug: allMeshSlugs[0], + meshSlugs: allMeshSlugs, userName: ctx.from?.first_name, recentPeers, });