Skip to main content
Glama

analyze_memory_patterns

Analyze memory usage patterns to identify trends, extract key insights, and optimize memory management strategies for improved system performance.

Instructions

分析記憶使用模式,提供統計信息和洞察

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
conversation_idYes對話 ID
memory_typeNo分析的記憶類型both
top_keywordsNo返回最常見關鍵詞的數量

Implementation Reference

  • The core handler function for the 'analyze_memory_patterns' tool. It fetches short-term and/or long-term memory managers based on the input parameters and analyzes their memories using the analyzeMemories helper function.
    async handler(args) { const { conversation_id, memory_type, top_keywords } = args; try { const analysis = { conversation_id, timestamp: new Date().toISOString(), short_term: null, long_term: null }; // 分析短期記憶 if (memory_type === 'short_term' || memory_type === 'both') { const manager = await getShortTermManager(conversation_id); analysis.short_term = analyzeMemories(manager.getMemories(), top_keywords); } // 分析長期記憶 if (memory_type === 'long_term' || memory_type === 'both') { const manager = await getLongTermManager(conversation_id); analysis.long_term = analyzeMemories(manager.getMemories(), top_keywords, true); } return { success: true, ...analysis }; } catch (error) { return { success: false, error: error.message }; } }
  • The Zod input schema defining parameters for the tool: conversation_id (required), memory_type (enum with default 'both'), top_keywords (number default 20).
    inputSchema: z.object({ conversation_id: z.string().describe('對話 ID'), memory_type: z.enum(['short_term', 'long_term', 'both']).default('both').describe('分析的記憶類型'), top_keywords: z.number().default(20).describe('返回最常見關鍵詞的數量') }),
  • src/index.js:165-166 (registration)
    Registration of search tools (including analyze_memory_patterns) into the toolRegistry with 'search' scope. Note that handlers are dynamically recreated per conversation during execution.
    const searchTools = createSearchTools(getShortTermManager, getLongTermManager); searchTools.forEach(tool => registerTool(tool, 'search'));
  • Core helper function that performs statistical analysis on a list of memories: calculates total count, average score, score distribution, top keywords by frequency, and time distribution.
    function analyzeMemories(memories, topN = 20, isLongTerm = false) { const total = memories.length; if (total === 0) { return { total: 0, average_score: 0, score_distribution: {}, top_keywords: [], time_distribution: {} }; } // 計算平均分數 let totalScore = 0; const scoreDistribution = { negative: 0, low: 0, medium: 0, high: 0 }; for (const mem of memories) { const score = mem.score || 0; totalScore += score; if (score < 0) scoreDistribution.negative++; else if (score < 10) scoreDistribution.low++; else if (score < 30) scoreDistribution.medium++; else scoreDistribution.high++; } const averageScore = totalScore / total; // 統計關鍵詞 const keywordCounts = new Map(); for (const mem of memories) { const keywords = mem.keywords || []; for (const kw of keywords) { const word = (kw.word || kw).toLowerCase(); const weight = kw.weight || 1; keywordCounts.set(word, (keywordCounts.get(word) || 0) + weight); } } // 獲取最常見的關鍵詞 const topKeywords = Array.from(keywordCounts.entries()) .sort((a, b) => b[1] - a[1]) .slice(0, topN) .map(([word, count]) => ({ word, count: count.toFixed(2) })); // 時間分布 const timeDistribution = analyzeTimeDistribution(memories, isLongTerm); return { total, average_score: averageScore.toFixed(2), score_distribution: scoreDistribution, top_keywords: topKeywords, time_distribution: timeDistribution }; }
  • Helper function that categorizes memories into time buckets: last_hour, last_day, last_week, last_month, older based on timestamp.
    function analyzeTimeDistribution(memories, isLongTerm) { const now = Date.now(); const distribution = { last_hour: 0, last_day: 0, last_week: 0, last_month: 0, older: 0 }; for (const mem of memories) { let timestamp; if (isLongTerm) { timestamp = mem.createdAt ? new Date(mem.createdAt).getTime() : 0; } else { timestamp = mem.time_stamp ? new Date(mem.time_stamp).getTime() : 0; } const age = now - timestamp; if (age < 3600000) distribution.last_hour++; else if (age < 86400000) distribution.last_day++; else if (age < 604800000) distribution.last_week++; else if (age < 2592000000) distribution.last_month++; else distribution.older++; } return distribution; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/win10ogod/memory-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server