import type { ProviderPrompts } from "../../types/prompts" interface CogmemMemory { content?: string memory_type?: string category?: string subject?: string importance?: number created_at?: string updated_at?: string tags?: string[] relevance_score?: number [key: string]: unknown } export function buildCogmemAnswerPrompt( question: string, context: unknown[], questionDate?: string ): string { const memories = context as CogmemMemory[] const memoriesStr = memories .map((mem, i) => { const parts: string[] = [] // Header with index and metadata const subject = mem.subject ? ` [${mem.subject}]` : "" const type = mem.memory_type ? ` (${mem.memory_type})` : "" parts.push(`[${i + 1}]${subject}${type}`) // Date information if (mem.created_at) { parts.push(` Date: ${mem.created_at}`) } // The actual content if (mem.content) { parts.push(` ${mem.content}`) } else { parts.push(` ${JSON.stringify(mem)}`) } return parts.join("\n") }) .join("\n\n") return `You are taking a test. Each question has a short, specific correct answer. Your job is to find that answer in the provided memories and state it simply. Think of it like a quiz — short, correct answers score points. Long, padded answers lose points. Question: ${question} Question Date: ${questionDate || "Not specified"} ## Understanding the Data Each memory entry has: - **[N]** — Memory index for citation - **[subject]** — Topic label (may contain session IDs — ignore those, focus on names/topics) - **(type)** — Memory type (context, preference, decision, etc.) - **Date** — When this memory was stored - **Content** — The actual information. This is your PRIMARY source. Read each one carefully. ## Retrieved Memories (${memories.length} results) ${memoriesStr} ## How to Answer ### Step 1: Find the SINGLE BEST memory Scan all ${memories.length} memories. Find the ONE memory that most directly answers the question. This is your primary source. Note its number. ### Step 2: Check if you need more - For simple factual questions ("What did X do?", "What is X?"), the single best memory is usually enough. Answer from it ONLY. - For list questions ("What activities?", "What books?"), check if ONE memory lists them all. If yes, use only that. If the list is split across 2-3 memories, combine those specific ones — but don't keep adding from other memories. - For temporal questions ("When did X?"), find the memory with the date reference and calculate. - For inference questions ("Would X do Y?"), you may need 2-3 memories to reason from. ### Step 3: Handle dates - "Yesterday" in a memory from "May 8, 2023" = May 7, 2023 - "Last Sunday" on May 25, 2023: May 25 is a Thursday. Count back: Wed=24, Tue=23, Mon=22, Sun=**21**. Answer: Sunday, May 21, 2023. - "The sunday before [date]": calculate the actual calendar date by counting back to Sunday. - The Date field = when the memory was STORED. Actual event dates are IN the content. ### Step 4: Answer using EXACT words from the memories Don't paraphrase. If a memory says "thoughtful", say "thoughtful" — not "caring." If it says "Nothing is Impossible", use that exact title. If it says "bowls", say "bowls" — not "pots." ## CRITICAL: Answer Style Your answer must be as SHORT as the ground truth would be. Think: what's the simplest correct answer? - "What did Caroline research?" → "Adoption agencies." STOP. Not "Adoption agencies and counseling options." - "What books?" → List the titles. STOP. - "What activities?" → List the activities. STOP. - "When?" → The date. STOP. - "How many?" → The number. STOP. - If you find yourself writing more than one sentence, you're probably over-answering. - NEVER add related facts, background, or context. Answer ONLY the specific question asked. - Use the EXACT words from the memories. "bowls" not "pots", "Nothing is Impossible" not "an unnamed book". ## Examples **Q: "What activities does Melanie partake in?"** Good answer: "pottery, camping, painting, swimming" Bad answer: "Melanie partakes in a wide range of activities including running for self-care, reading as me-time, pottery which she enjoys with her kids..." (TOO LONG, includes extras not asked about) **Q: "What books has Melanie read?"** Good answer: "Nothing is Impossible, Charlotte's Web" Bad answer: "Charlotte's Web and an unnamed book she read around 2022" (WRONG — use the actual title from the memories) **Q: "When did Caroline go to the LGBTQ support group?"** Good answer: "7 May 2023" Bad answer: "sometime in May 2023" (TOO VAGUE) **Q: "How many times has Melanie gone to the beach in 2023?"** Good answer: "2" Bad answer: "once, though she mentions the beach is a regular family spot" (WRONG COUNT, unnecessary context) **Q: "What types of pottery have Melanie and her kids made?"** Good answer: "bowls, cup" Bad answer: "their own pots and a cup" (USE EXACT TERMS from the conversation) **Q: "Would Caroline pursue writing as a career?"** Good answer: "Likely no; though she likes reading, she wants to be a counselor" Bad answer: "She might consider it" (TOO VAGUE) **Q: "How long ago was Caroline's 18th birthday?"** Good answer: "10 years ago" (if a memory says she's 28, calculate 28-18=10) Bad answer: "I don't have enough information" (WRONG — calculate from age if mentioned) **Q: "What personality traits might Melanie say Caroline has?"** Good answer: "Thoughtful, authentic, driven" (USE THE EXACT WORDS from the conversation) Bad answer: "Caring, courageous, inspiring" (WRONG — use the speaker's actual words, don't substitute synonyms) **Q: "When did Caroline run the charity race?"** (but Caroline never ran a race — Melanie did) Good answer: "Caroline didn't run a charity race. It was Melanie who ran a charity race for mental health." Bad answer: "After hearing about Melanie's race, Caroline was inspired to..." (WRONG — don't force an answer when the premise is false) ## Rules - Answer ONLY from evidence in the memories. Never invent facts. - If memories contain information relevant to the question, attempt an answer. - WATCH FOR FALSE PREMISES. If the question assumes something that ISN'T true according to the memories (e.g., "When did Caroline run the race?" but it was actually Melanie who ran the race), CORRECT THE PREMISE and explain what actually happened. Don't force an answer to a question built on a wrong assumption. - Only say "I don't have enough information" when the question asks about a topic/person/event that is truly ABSENT from every single memory. - Be SPECIFIC: use exact names, exact titles, exact dates, exact places from the memories. Never paraphrase or generalize. - When memories contradict, prefer the more recent one. - For list questions, give ONLY what was asked. "What did Caroline research?" → "Adoption agencies." STOP. Do not add other things Caroline did. - Read EVERY memory including lower-ranked ones. The answer may be in memory [30] or [45]. - For counting questions, carefully count distinct occurrences across all memories. Don't miss any. - For inference questions ("Would X do Y?"), use evidence from the memories to reason, but don't fabricate motivations or preferences not mentioned. **Response Format:** Relevant memories: [list the memory numbers that contain relevant info] Reasoning: [Brief analysis — which memories answer the question] Answer: [SHORT, DIRECT answer — just the facts asked for, nothing more]` } /** * Extraction prompt for converting conversation sessions into discrete facts. */ export const FACT_EXTRACTION_PROMPT = `You are a memory extraction specialist. Extract discrete, self-contained facts from this conversation session. ## RULES: 1. Each fact must be a complete, standalone sentence that makes sense without any other context. 2. ALWAYS use the speaker's actual name, NEVER use "user" or "assistant". 3. ALWAYS convert relative dates to absolute dates when possible. Use the session date as reference. 4. Resolve all coreferences: "my roommate" + later "Emily" → "Emily (the speaker's roommate)". 5. Include emotional context and significance when present. 6. Include specific details: exact names, places, dates, activities — never generalize. 7. Skip greetings, filler ("oh cool", "that's great"), and assistant responses that don't contain new facts. 8. Each fact should be 1-2 sentences maximum. 9. For plans/intentions, note they are planned, not completed: "Caroline is PLANNING to attend a transgender conference." 10. For preferences, be specific: "Melanie enjoys hiking in national parks" not "Melanie likes outdoor activities." ## OUTPUT FORMAT: Return a JSON array of objects: [ {"fact": "the extracted fact as a complete sentence", "who": "primary person(s) involved", "when": "specific date or N/A"} ] Only return the JSON array. No other text.` /** * QA extraction prompt — generates question-answer pairs from conversations. * These get stored as a third layer, with embeddings that directly match * the question format used in the benchmark. */ export const QA_EXTRACTION_PROMPT = `You are an expert at anticipating questions people might ask about a conversation. Given a conversation between two people, generate question-answer pairs that capture every important fact, event, date, preference, relationship detail, and plan mentioned. ## RULES: 1. Generate questions in the EXACT style someone would ask later: "What did [name] say about...?", "When did [name]...?", "Where has [name]...?" 2. ALWAYS use the speakers' actual names, NEVER "user" or "assistant" or "Speaker A/B" 3. ALWAYS convert relative dates to absolute dates. The session date is provided — use it. - "yesterday" on May 8, 2023 → "May 7, 2023" - "last week" on May 8, 2023 → "the week before May 8, 2023" - "next month" on May 8, 2023 → "June 2023" 4. Include EVERY factual detail — activities, places, dates, preferences, feelings, plans, relationships 5. For multi-fact answers, list ALL items: "pottery, camping, painting, swimming" not just "various activities" 6. One question per distinct fact. Don't combine unrelated facts into one question. 7. For plans/future events, phrase as: "When is [name] planning to...?" 8. For preferences, phrase as: "What does [name] enjoy/like/do to...?" ## OUTPUT FORMAT: Return a JSON array: [ {"q": "the question", "a": "the precise answer"} ] Only return the JSON array. No other text.` /** * Query expansion prompt — generates reformulated search queries. */ export const QUERY_EXPANSION_PROMPT = `Generate 3 different ways to search for the answer to this question. Each reformulation should use different words and phrasing to maximize the chance of finding the relevant memory. Rules: - Use the actual names mentioned in the question - Include key entities, activities, dates - Make each variant meaningfully different, not just word swaps - One variant should be keyword-only (just the key terms) - Keep each under 100 characters Question: {{question}} Return ONLY a JSON array of 3 strings. No other text.` export const COGMEM_PROMPTS: ProviderPrompts = { answerPrompt: buildCogmemAnswerPrompt, } export default COGMEM_PROMPTS