AUDIT / src /lib /agent /thinkingExpander.ts
Arypulka98's picture
feat(audit): deploy full backend cluster node (part 2)
cc11e77 verified
Raw
History Blame Contribute Delete
1.64 kB
/**
* thinkingExpander.ts — Gap 4: Inference Scaling con 2 approcci alternativi
*
* Genera 2 approcci per task ad alta incertezza, sceglie il migliore.
* Budget: 1 LLM call. Attivazione: solo task incerti o > 100 chars.
*/
import type { LLMCallerFn } from "./agentRuntimeTypes";
export interface ThinkingResult {
chosenApproach: string;
alternatives: string[];
confidence: number;
}
const HIGH_UNCERTAINTY_SIGNALS = [
"non so", "non sono sicuro", "forse", "potrebbe",
"I'm not sure", "might", "could be", "unclear",
"come fare", "how to", "best way",
];
function isHighUncertainty(task: string): boolean {
const t = task.toLowerCase();
return HIGH_UNCERTAINTY_SIGNALS.some(s => t.includes(s));
}
export async function expandThinking(
task: string,
llmCaller: LLMCallerFn,
): Promise<ThinkingResult | null> {
if (!isHighUncertainty(task) && task.length < 100) return null;
try {
const raw = await llmCaller([
{
role: "system",
content:
"Genera 2 approcci alternativi per risolvere il task. " +
'Rispondi SOLO con JSON: {"approaches": ["approccio 1...", "approccio 2..."], "best": 0, "confidence": 7}\n' +
"best è l'indice (0 o 1) dell'approccio migliore.",
},
{ role: "user", content: `TASK: ${task}` },
]);
const clean = raw.replace(/```json|```/g, "").trim();
const parsed = JSON.parse(clean);
return {
chosenApproach: parsed.approaches?.[parsed.best ?? 0] ?? task,
alternatives: parsed.approaches ?? [],
confidence: parsed.confidence ?? 5,
};
} catch {
return null;
}
}