Spaces:
Sleeping
Sleeping
File size: 7,254 Bytes
cc11e77 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | /**
* ProactiveAgent.ts — Context-aware proactive suggestions (FASE 6)
*
* Monitors conversation + time + memory to proactively suggest actions.
* No LLM required — pure pattern matching + temporal analysis.
*
* Triggers:
* - Time-based: "Good morning" greeting, "notizie del giorno" suggestion at 8am
* - Staleness: detects when a prior tool result might be outdated
* - Continuity: detects when the user might want to continue a prior task
* - Gaps: detects missing context that the agent could fill in advance
*/
import { agentMemory } from "../agentMemory";
import { sessionMemory } from "./MemoryUpdater";
export interface ProactiveSuggestion {
id: string;
text: string;
reason: string;
priority: "high" | "medium" | "low";
tool?: string;
args?: Record<string, unknown>;
expires?: number; // unix ms
}
// ── Time helpers ──────────────────────────────────────────────────────────────
function hourNow(): number { return new Date().getHours(); }
function isWeekday(): boolean { return new Date().getDay() >= 1 && new Date().getDay() <= 5; }
// ── Pattern matchers ───────────────────────────────────────�����──────────────────
export const NEWS_STALE_MS = 4 * 60 * 60 * 1000; // 4h
export const WEATHER_STALE_MS = 6 * 60 * 60 * 1000; // 6h
// ── Evaluators ────────────────────────────────────────────────────────────────
function checkTimeBased(): ProactiveSuggestion[] {
const h = hourNow();
const suggestions: ProactiveSuggestion[] = [];
if (h >= 7 && h <= 9) {
suggestions.push({
id: "morning-news",
text: "Vuoi che ti mostri le notizie del mattino?",
reason: "È mattina — potrebbe essere utile un riepilogo",
priority: "medium",
tool: "get_news",
args: { query: "" },
expires: Date.now() + 2 * 60 * 60 * 1000,
});
}
if (h >= 9 && h <= 10 && isWeekday()) {
suggestions.push({
id: "morning-currency",
text: "Controlla i tassi di cambio di apertura mercati?",
reason: "Apertura mercati finanziari",
priority: "low",
tool: "get_currency",
args: { from: "EUR", to: "USD", amount: 1 },
expires: Date.now() + 3 * 60 * 60 * 1000,
});
}
if (h >= 18 && h <= 20) {
suggestions.push({
id: "evening-summary",
text: "Vuoi un riepilogo delle notizie della giornata?",
reason: "Orario serale — riepilogo giornaliero",
priority: "low",
tool: "get_news",
args: { query: "oggi" },
expires: Date.now() + 2 * 60 * 60 * 1000,
});
}
return suggestions;
}
function checkStaleData(): ProactiveSuggestion[] {
const suggestions: ProactiveSuggestion[] = [];
const now = Date.now();
// S91: usa agentMemory.list() (Dexie-backed, in-memory cache) invece di localStorage raw.
// Evita deserializzazione JSON ad ogni check + accede al layer cache in-memory già caricato.
const allMemory = agentMemory.list();
// Check for stale weather
const weatherEntry = allMemory.find(e =>
e.key.startsWith("meteo:") || e.key.startsWith("weather:") ||
e.category === "weather" || e.category === "meteo"
);
if (weatherEntry && now - weatherEntry.updatedAt > WEATHER_STALE_MS) {
const val = weatherEntry.value;
const cityMatch = val.match(/a\s+(\w+)/i);
suggestions.push({
id: "stale-weather",
text: `Aggiorna meteo${cityMatch ? ` per ${cityMatch[1]}` : ""}?`,
reason: "Dati meteo aggiornati più di 6 ore fa",
priority: "low",
tool: "get_weather",
args: cityMatch ? { city: cityMatch[1] } : {},
expires: now + 60 * 60 * 1000,
});
}
// Check for stale news
const newsEntry = allMemory.find(e =>
e.key.startsWith("news:") || e.key.startsWith("notizie:") ||
e.category === "news" || e.category === "notizie"
);
if (newsEntry && now - newsEntry.updatedAt > NEWS_STALE_MS) {
suggestions.push({
id: "stale-news",
text: "Le ultime notizie salvate sono vecchie — aggiorno?",
reason: "Notizie aggiornate più di 4 ore fa",
priority: "medium",
tool: "get_news",
args: { query: "" },
expires: now + 30 * 60 * 1000,
});
}
return suggestions;
}
function checkContinuity(): ProactiveSuggestion[] {
const suggestions: ProactiveSuggestion[] = [];
// Look at session memory for interrupted tasks
const summary = sessionMemory.summarize();
if (!summary) return suggestions;
const hasCode = /run_code|python|javascript/i.test(summary);
const hasFile = /write_file|read_file/i.test(summary);
const hasResearch = /web_search|wikipedia/i.test(summary);
if (hasCode) {
suggestions.push({
id: "continue-code",
text: "Continuare con il codice della sessione precedente?",
reason: "Rilevata sessione di coding in corso",
priority: "medium",
expires: Date.now() + 10 * 60 * 1000,
});
}
if (hasFile && !hasCode) {
suggestions.push({
id: "continue-file",
text: "Mostra i file modificati di recente?",
reason: "Rilevata operazione su file",
priority: "low",
tool: "read_file",
expires: Date.now() + 10 * 60 * 1000,
});
}
if (hasResearch) {
suggestions.push({
id: "continue-research",
text: "Approfondire la ricerca con Wikipedia?",
reason: "Ricerca in corso nella sessione",
priority: "low",
tool: "search_wikipedia",
expires: Date.now() + 15 * 60 * 1000,
});
}
return suggestions;
}
// ── Public API ────────────────────────────────────────────────────────────────
let _lastGenerated = 0;
const COOLDOWN_MS = 5 * 60 * 1000; // don't spam — max once every 5min
/**
* Generate proactive suggestions based on current context.
* Returns an empty array if called too frequently.
*/
export function getProactiveSuggestions(maxResults = 3): ProactiveSuggestion[] {
const now = Date.now();
if (now - _lastGenerated < COOLDOWN_MS) return [];
_lastGenerated = now;
try {
const all = [
...checkTimeBased(),
...checkStaleData(),
...checkContinuity(),
];
// Sort by priority, filter expired
const valid = all
.filter(s => !s.expires || s.expires > now)
.sort((a, b) => {
const P = { high: 0, medium: 1, low: 2 };
return P[a.priority] - P[b.priority];
});
return valid.slice(0, maxResults);
} catch {
return [];
}
}
/**
* Lightweight version that just checks time (safe to call on every app load).
*/
export function getTimeBasedGreeting(): string {
const h = hourNow();
if (h >= 5 && h < 12) return "Buongiorno";
if (h >= 12 && h < 18) return "Buon pomeriggio";
if (h >= 18 && h < 22) return "Buona sera";
return "Buonanotte";
}
|