File size: 3,912 Bytes
95eb75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 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: "" };
  }
}