src / tools / contextManager.ts
src / tools / contextManager.ts
import fs from 'fs/promises';
import path from 'path';
import { getState } from '../stateRegistry';
const CONTEXT_FILE = '.timber_context.json';
interface ContextEntry {
id: string;
type: 'summary' | 'tool_output' | 'conversation_turn';
content: string;
created_at: number;
}
let contextStore: ContextEntry[] = [];
async function loadContext(): Promise<void> {
try {
const content = await fs.readFile(path.join(process.cwd(), CONTEXT_FILE), 'utf-8');
contextStore = JSON.parse(content);
} catch {}
}
export async function context_summarize(turns: string[]): Promise<{ summary: string }> {
const summary = `Summarized ${turns.length} turns of conversation`;
await loadContext();
contextStore.push({
id: `ctx_${Date.now()}`,
type: 'summary',
content: summary,
created_at: Date.now()
});
return { summary };
}
export async function context_compress(): Promise<{ success: boolean; compressed_entries: number }> {
await loadContext();
const keepCount = 50;
if (contextStore.length <= keepCount) return { success: true, compressed_entries: 0 };
const toCompress = contextStore.slice(0, contextStore.length - keepCount);
const recent = contextStore.slice(contextStore.length - keepCount);
const summaryContent = `Compressed ${toCompress.length} older context entries`;
contextStore = [
{ id: 'compressed_summary', type: 'summary', content: summaryContent, created_at: Date.now() },
...recent
];
await fs.writeFile(path.join(process.cwd(), CONTEXT_FILE), JSON.stringify(contextStore, null, 2));
return { success: true, compressed_entries: toCompress.length };
}
export async function context_get(): Promise<{ entries: ContextEntry[] }> {
await loadContext();
return { entries: contextStore };
}
export async function context_clear(): Promise<{ success: boolean }> {
try {
await fs.unlink(path.join(process.cwd(), CONTEXT_FILE));
contextStore = [];
return { success: true };
} catch {
return { success: false };
}
}
import fs from 'fs/promises';
import path from 'path';
import { getState } from '../stateRegistry';
const CONTEXT_FILE = '.timber_context.json';
interface ContextEntry {
id: string;
type: 'summary' | 'tool_output' | 'conversation_turn';
content: string;
created_at: number;
}
let contextStore: ContextEntry[] = [];
async function loadContext(): Promise<void> {
try {
const content = await fs.readFile(path.join(process.cwd(), CONTEXT_FILE), 'utf-8');
contextStore = JSON.parse(content);
} catch {}
}
export async function context_summarize(turns: string[]): Promise<{ summary: string }> {
const summary = `Summarized ${turns.length} turns of conversation`;
await loadContext();
contextStore.push({
id: `ctx_${Date.now()}`,
type: 'summary',
content: summary,
created_at: Date.now()
});
return { summary };
}
export async function context_compress(): Promise<{ success: boolean; compressed_entries: number }> {
await loadContext();
const keepCount = 50;
if (contextStore.length <= keepCount) return { success: true, compressed_entries: 0 };
const toCompress = contextStore.slice(0, contextStore.length - keepCount);
const recent = contextStore.slice(contextStore.length - keepCount);
const summaryContent = `Compressed ${toCompress.length} older context entries`;
contextStore = [
{ id: 'compressed_summary', type: 'summary', content: summaryContent, created_at: Date.now() },
...recent
];
await fs.writeFile(path.join(process.cwd(), CONTEXT_FILE), JSON.stringify(contextStore, null, 2));
return { success: true, compressed_entries: toCompress.length };
}
export async function context_get(): Promise<{ entries: ContextEntry[] }> {
await loadContext();
return { entries: contextStore };
}
export async function context_clear(): Promise<{ success: boolean }> {
try {
await fs.unlink(path.join(process.cwd(), CONTEXT_FILE));
contextStore = [];
return { success: true };
} catch {
return { success: false };
}
}