Spaces:
Sleeping
Sleeping
| /** | |
| * extendedThinkingBlock.ts — S689: Extended Thinking | |
| * | |
| * Attiva un reasoning prefix nel system prompt quando il task è complesso | |
| * (complexity >= 7 o modelTier === "premium"). Migliora la qualità delle | |
| * risposte per task architetturali, refactoring, full_app, debug complesso. | |
| * | |
| * Design: | |
| * - Zero side-effect, deterministica, safe in ogni contesto Safari/iPhone | |
| * - Iniettato tramite sezione "ext_thinking" in assembleSystemPrompt (budget-aware) | |
| * - Il hint informa il modello di ragionare in modo strutturato prima di agire | |
| * - Restituisce stringa vuota per task semplici → zero overhead sul context | |
| * | |
| * @module extendedThinkingBlock | |
| */ | |
| import type { TaskClassification } from "../taskClassifierTypes"; | |
| // ─── Interfacce ──────────────────────────────────────────────────────────────── | |
| export interface ExtendedThinkingResult { | |
| /** true se il task richiede ragionamento approfondito */ | |
| isExtended: boolean; | |
| /** testo da iniettare nel system prompt — stringa vuota se !isExtended */ | |
| hint: string; | |
| /** label per onStatus — visibile all'utente durante l'elaborazione */ | |
| statusLabel: string; | |
| } | |
| // ─── Costanti ────────────────────────────────────────────────────────────────── | |
| /** Complessità minima per attivare Extended Thinking */ | |
| const ET_COMPLEXITY_THRESHOLD = 7; | |
| /** Testo iniettato nel system prompt — istruisce il modello a pianificare prima di agire */ | |
| const ET_HINT_TEXT = `[EXTENDED THINKING — S689] | |
| Task ad alta complessità rilevato. Prima di rispondere, ragiona strutturalmente: | |
| 1. Analizza il problema — individua componenti, vincoli e casi limite. | |
| 2. Considera almeno 2 approcci alternativi e scegli quello ottimale. | |
| 3. Rispetta le dipendenze: infrastruttura → logica → UI (mai il contrario). | |
| 4. Identifica possibili regressioni e come prevenirle. | |
| 5. Produci output preciso, completo e direttamente utilizzabile — niente approssimazioni.`; | |
| /** Mappa tipo task → etichetta leggibile per l'utente */ | |
| const ET_TYPE_LABELS: Partial<Record<string, string>> = { | |
| code_generation: "generazione codice", | |
| full_app: "app completa", | |
| refactor: "refactor", | |
| multi_file: "multi-file", | |
| architecture: "architettura", | |
| debug: "debug", | |
| build_fix: "build fix", | |
| data_analysis: "analisi dati", | |
| unknown: "task complesso", | |
| }; | |
| // ─── API pubblica ────────────────────────────────────────────────────────────── | |
| /** | |
| * Valuta se attivare Extended Thinking per il task classificato. | |
| * | |
| * Deterministica — nessuna chiamata esterna, nessun side-effect. | |
| * Restituisce sempre un oggetto valido anche se cls è null. | |
| * | |
| * @param cls Risultato di classifyTask() — può essere null | |
| * @returns ExtendedThinkingResult con isExtended, hint, statusLabel | |
| */ | |
| export function evaluateExtendedThinking( | |
| cls: TaskClassification | null, | |
| ): ExtendedThinkingResult { | |
| try { | |
| if (!cls) { | |
| return { isExtended: false, hint: "", statusLabel: "" }; | |
| } | |
| const isExtended = | |
| cls.complexity >= ET_COMPLEXITY_THRESHOLD || | |
| cls.modelTier === "premium"; | |
| if (!isExtended) { | |
| return { isExtended: false, hint: "", statusLabel: "" }; | |
| } | |
| const typeLabel = ET_TYPE_LABELS[cls.type] ?? "task complesso"; | |
| return { | |
| isExtended, | |
| hint: ET_HINT_TEXT, | |
| statusLabel: `🧠 Ragionamento approfondito (${typeLabel} · complessità ${cls.complexity}/10)`, | |
| }; | |
| } catch { | |
| return { isExtended: false, hint: "", statusLabel: "" }; | |
| } | |
| } | |