← back to Designer Wallcoverings

DW-Agents/dw-agents/enhance-memory-chat.ts

115 lines

#!/usr/bin/env npx tsx
/**
 * Enhance agent chat endpoints to include memory context in AI responses
 *
 * This script adds memory awareness to the AI so it can:
 * 1. Reference stored memories in responses
 * 2. Automatically save important info as memories
 * 3. Suggest adding memories when appropriate
 */

import * as fs from 'fs';
import * as path from 'path';

const AGENTS_TO_UPDATE = [
  '/root/Projects/Designer-Wallcoverings/DW-Agents/digital-samples-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/marketing-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/accounting-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/purchasing-office-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/task-orchestrator-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/zendesk-chat-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/new-client-signup-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/todays-highlights-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/completed-tasks-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/in-parallel-agent.ts',
  '/root/Projects/Designer-Wallcoverings/DW-Agents/agent-legal-team/legal-agent.ts',
];

function enhanceAgentChat(agentFile: string): boolean {
  try {
    if (!fs.existsSync(agentFile)) {
      console.log(`⏭️  Skipping ${agentFile} - file not found`);
      return false;
    }

    let content = fs.readFileSync(agentFile, 'utf8');

    // Check if already enhanced
    if (content.includes('agentMemory.getContextSummary()')) {
      console.log(`✅ ${path.basename(agentFile)} already has memory-aware chat`);
      return true;
    }

    console.log(`📝 Enhancing ${path.basename(agentFile)}...`);

    // Find the systemPrompt or system message definition in the chat endpoint
    // Look for patterns like: systemPrompt = or system:
    const systemPromptRegex = /(const systemPrompt = `[^`]+`|system: `[^`]+`|systemPrompt: `[^`]+`)/;

    if (content.match(systemPromptRegex)) {
      content = content.replace(systemPromptRegex, (match) => {
        // Extract the prompt content
        const promptContent = match.replace(/`$/, '').split('`')[1];
        return match.replace(promptContent, promptContent + `\n\n${agentMemory.getContextSummary()}\n\nUse the memories above to provide contextual responses. Reference relevant memories when appropriate.`);
      });
    } else {
      // Try to find anthropic.messages.create and add system prompt there
      const anthropicCallRegex = /(const response = await anthropic\.messages\.create\(\{[^}]*)/s;
      if (content.match(anthropicCallRegex)) {
        content = content.replace(anthropicCallRegex, (match) => {
          if (!match.includes('system:')) {
            return match.replace('{', `{\n      system: \`You are an AI assistant. \${agentMemory.getContextSummary()}\`,`);
          }
          return match;
        });
      }
    }

    // Save backup and write enhanced file
    const backupFile = agentFile + '.memory-backup';
    fs.writeFileSync(backupFile, fs.readFileSync(agentFile));
    fs.writeFileSync(agentFile, content);

    console.log(`✅ ${path.basename(agentFile)} enhanced!`);
    return true;

  } catch (error: any) {
    console.error(`❌ Error enhancing ${agentFile}:`, error.message);
    return false;
  }
}

// Main execution
console.log('🚀 Enhancing agent chats with memory awareness...\n');

let successCount = 0;
let failCount = 0;

AGENTS_TO_UPDATE.forEach((file) => {
  if (enhanceAgentChat(file)) {
    successCount++;
  } else {
    failCount++;
  }
});

console.log(`
================================================================================
📊 SUMMARY
================================================================================
✅ Successfully enhanced: ${successCount} agents
❌ Failed/Skipped: ${failCount} agents

Now agents will:
- Include memories in their AI responses automatically
- Reference past information naturally
- Provide context-aware answers

Test it:
1. Go to any agent's chat
2. Add a memory: "Remember: We prefer Tuesday shipments"
3. Ask a question: "When should I schedule the shipment?"
4. The AI will reference your preference in the response!
================================================================================
`);