Merge pull request #7 from eggent-ai/perf/optimize-context-caching

perf(agent): optimize context for prompt caching and limit history
This commit is contained in:
ilya-bov
2026-02-28 15:22:33 +03:00
committed by GitHub
2 changed files with 14 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ import { getChat, saveChat } from "@/lib/storage/chat-store";
import { createAgentTools } from "@/lib/tools/tool";
import { getProjectMcpTools } from "@/lib/mcp/client";
import type { AgentContext } from "@/lib/agent/types";
import { History } from "@/lib/agent/history";
import type { ChatMessage } from "@/lib/types";
import { publishUiSyncEvent } from "@/lib/realtime/event-bus";
@@ -584,7 +585,10 @@ export async function runAgent(options: {
const chat = await getChat(options.chatId);
if (chat) {
// Convert stored messages to ModelMessage format (including tool calls/results)
context.history = convertChatMessagesToModelMessages(chat.messages);
const allMessages = convertChatMessagesToModelMessages(chat.messages);
const history = new History(80);
history.addMany(allMessages);
context.history = history.getAll();
}
// Build tools: base + optional MCP tools from project .meta/mcp
@@ -717,7 +721,10 @@ export async function runAgentText(options: {
const chat = await getChat(options.chatId);
if (chat) {
context.history = convertChatMessagesToModelMessages(chat.messages);
const allMessages = convertChatMessagesToModelMessages(chat.messages);
const history = new History(80);
history.addMany(allMessages);
context.history = history.getAll();
}
const baseTools = createAgentTools(context, settings);

View File

@@ -201,9 +201,12 @@ export async function buildSystemPrompt(options: {
}
}
// 6. Current date/time
// 6. Current date/time (rounded to the hour for prompt caching)
const now = new Date();
now.setMinutes(0, 0, 0);
const dateStr = now.toISOString().slice(0, 13) + ":00:00Z";
parts.push(
`\n## Current Information\n- Date/Time: ${new Date().toISOString()}\n- Timezone: ${Intl.DateTimeFormat().resolvedOptions().timeZone}`
`\n## Current Information\n- Date/Time: ${dateStr}\n- Timezone: ${Intl.DateTimeFormat().resolvedOptions().timeZone}`
);
return parts.join("\n\n");