fix(broker): teach AI difference between mesh names and peer names
Some checks failed
CI / Typecheck (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Broker tests (Postgres) (push) Has been cancelled
CI / Docker build (linux/amd64) (push) Has been cancelled

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alejandro Gutiérrez
2026-04-13 21:06:08 +01:00
parent 6836a495a4
commit a9858ef876
2 changed files with 14 additions and 7 deletions

View File

@@ -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. 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 <mesh-name>", 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 <person-name>", they mean a direct message to that peer.
Rules: Rules:
- Be concise — Telegram messages should be short - Be concise — Telegram messages should be short
- When sending messages to peers, preserve the user's tone and intent - When sending messages to peers, preserve the user's tone and intent
- For ambiguous peer names, ask for clarification - If the target looks like a mesh name (matches one from context), broadcast to it
- Never fabricate peer names or data — use list_peers to find real names - Never fabricate peer names — use list_peers to find real names
- If unsure which mesh to target, ask the user`; - Default to the first connected mesh if not specified`;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// AI Engine // AI Engine
@@ -177,13 +183,13 @@ function getClient(): Anthropic {
*/ */
export async function processMessage( export async function processMessage(
userMessage: string, userMessage: string,
context: { meshSlug?: string; userName?: string; recentPeers?: string[] }, context: { meshSlug?: string; meshSlugs?: string[]; userName?: string; recentPeers?: string[] },
): Promise<AiResult> { ): Promise<AiResult> {
try { try {
const anthropic = getClient(); const anthropic = getClient();
const contextInfo = [ 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.userName ? `User's name: ${context.userName}` : null,
context.recentPeers?.length ? `Known peers: ${context.recentPeers.join(", ")}` : null, context.recentPeers?.length ? `Known peers: ${context.recentPeers.join(", ")}` : null,
].filter(Boolean).join(". "); ].filter(Boolean).join(". ");

View File

@@ -1603,7 +1603,7 @@ function setupBotCommands(
// Gather context for the AI // Gather context for the AI
const firstMeshId = meshIds[0]!; const firstMeshId = meshIds[0]!;
const firstConn = meshConnections.get(firstMeshId); 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[] = []; let recentPeers: string[] = [];
if (firstConn?.isConnected()) { if (firstConn?.isConnected()) {
try { try {
@@ -1613,7 +1613,8 @@ function setupBotCommands(
} }
const result = await processMessage(text, { const result = await processMessage(text, {
meshSlug, meshSlug: allMeshSlugs[0],
meshSlugs: allMeshSlugs,
userName: ctx.from?.first_name, userName: ctx.from?.first_name,
recentPeers, recentPeers,
}); });