MCPcopy
hub / github.com/Doorman11991/smallcode / getMemoryContext

Function getMemoryContext

bin/smallcode.js:2060–2087  ·  view source on GitHub ↗
(messages)

Source from the content-addressed store, hash-verified

2058// Fix #15: Only inject memory objects with score >= 2 (at least 2 word matches)
2059// to avoid burning tokens on low-relevance hits.
2060function getMemoryContext(messages) {
2061 try {
2062 // Get the last user message to find relevant memory
2063 const lastUser = [...messages].reverse().find(m => m.role === 'user');
2064 if (!lastUser || !memoryStore.loadForTask) return '';
2065
2066 // loadForTask returns scored objects; only include those with meaningful relevance
2067 const maxTokens = Math.min(800, Math.floor(((config?.context?.detected_window || 32768) * 0.03)));
2068 const objects = memoryStore.loadForTask(lastUser.content, maxTokens);
2069 // Handle both old format (array) and new format ({objects, tokens_used})
2070 const items = Array.isArray(objects) ? objects : (objects?.objects || []);
2071 if (items.length === 0) return '';
2072
2073 // Cap total injection to ~800 tokens (3200 chars)
2074 let output = '\n\nRelevant project memory:\n';
2075 let chars = output.length;
2076 const maxChars = 3200;
2077 for (const o of items) {
2078 const entry = `[${o.type}] ${o.title}: ${o.content}\n`;
2079 if (chars + entry.length > maxChars) break;
2080 output += entry;
2081 chars += entry.length;
2082 }
2083 return output;
2084 } catch {
2085 return '';
2086 }
2087}
2088
2089// Auto-load relevant skills based on the user's message
2090// Fix #18: Cap skill injection to ~1000 tokens (4000 chars). Multiple matching

Callers 2

buildCompactSystemPromptFunction · 0.70
buildDynamicContextFunction · 0.70

Calls 1

loadForTaskMethod · 0.80

Tested by

no test coverage detected