File size: 763 Bytes
cc11e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// agentLoop/archCtxCache.ts
// S567: estratto da agentLoop.ts — cache con TTL 5s per buildArchContext (S440).
// Evita O(n) sincrono ad ogni rebuild del system prompt.

import { buildArchContext } from "@/lib/projectArchGraph";

let _cache: { val: string; at: number } = { val: "", at: 0 };

/**
 * Restituisce buildArchContext() con cache in-memory TTL 5s.
 * Chiamato ad ogni _rebuildSys() → esecuzione O(1) se recente, O(n) solo alla prima
 * chiamata o dopo 5 secondi dall'ultima (ad es. dopo modifica file).
 */
export function buildArchCtxCached(): string {
  if (Date.now() - _cache.at < 5_000) return _cache.val;
  try { _cache = { val: buildArchContext(), at: Date.now() }; }
  catch { _cache = { val: "", at: Date.now() }; }
  return _cache.val;
}