File size: 6,914 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
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
// ─── Code execution tools — run_code, execute_shell, pip_install, run_python, math_eval, run_tests, lint_code, diff_text ───────────────────────────────────────────────────────────
  // P3-4 split da toolDefinitions.ts — DO NOT edit toolDefinitions.ts directly
  // Tools (8): run_code, execute_shell, pip_install, run_python, math_eval, run_tests, lint_code, diff_text

  import type { ChatCompletionTool } from "openai/resources/chat/completions";

  export const CODE_TOOLS: (ChatCompletionTool | undefined)[] = [
      {
      type: "function",
      function: {
        name: "run_code",
        description: "Esegui codice JavaScript in un sandbox sicuro nel browser. Accede a fetch(), vfs, memory, uuid(), csvParse(), download(), generatePDF(). Usa per calcoli, elaborazione dati, generazione file.",
        parameters: { type: "object", properties: { code: { type: "string", description: "Codice JavaScript async da eseguire." } }, required: ["code"] }
      }
    },
    {
      type: "function",
      function: {
        name: "execute_shell",
        description: "Esegui un comando shell Linux sul backend server (Python 3.11, git, curl, ls, grep disponibili). Ogni esecuzione è in una directory temporanea isolata. Usa per: calcoli Python, git operations, download file, elaborazione dati lato server.",
        parameters: { type: "object", properties: { command: { type: "string", description: "Comando shell da eseguire (es. 'python3 -c \"import math; print(math.pi)\"')" }, timeout: { type: "number", description: "Timeout in secondi (default 15, max 60)" } }, required: ["command"] }
      }
    },
    {
      type: "function",
      function: {
        name: "pip_install",
        description: "Installa pacchetti Python sul backend server con pip. Usa prima di execute_shell quando serve una libreria non standard (es. numpy, pandas, requests, matplotlib).",
        parameters: { type: "object", properties: { packages: { type: "array", items: { type: "string" }, description: "Lista pacchetti da installare (es. ['numpy', 'pandas'])" } }, required: ["packages"] }
      }
    },
    {
      type: "function",
      function: {
        name: "run_python",
        description: "Esegui Python nel browser con Pyodide (offline, zero backend). numpy/pandas/scipy/matplotlib inclusi. micropip installa altri pacchetti automaticamente dagli import. Preferisci a execute_shell per calcoli, data analysis, scripting.",
        parameters: {
          type: "object",
          properties: {
            code:     { type: "string", description: "Codice Python da eseguire" },
            packages: { type: "array",  items: { type: "string" }, description: "Pacchetti extra via micropip (opzionale)" }
          },
          required: ["code"]
        }
      }
    },
    {
      type: "function",
      function: {
        name: "math_eval",
        description: "Calcola espressioni matematiche in modo sicuro. Supporta: +,-,*,/,^,%, sin,cos,tan,sqrt,log,pow,PI,E,abs,ceil,floor,round,min,max. Usa per calcoli precisi invece di fare a mente.",
        parameters: { type: "object", properties: { expr: { type: "string", description: "Espressione matematica (es. 'sqrt(144) + pow(2,10)', 'sin(PI/4) * 100')" } }, required: ["expr"] }
      }
    },
    {
      type: "function",
      function: {
        name: "run_tests",
        description: "Esegui test JavaScript con un framework built-in (describe/it/expect). Usa per verificare funzioni, algoritmi, API. Supporta: toBe, toEqual, toBeTruthy, toBeFalsy, toContain, toBeGreaterThan, toBeLessThan, toBeNull, toThrow.",
        parameters: {
          type: "object",
          properties: {
            code: { type: "string", description: "Codice JavaScript con test. describe/it/expect/test disponibili. Puoi importare funzioni dal codice applicativo." },
            description: { type: "string", description: "Breve descrizione di cosa si sta testando" }
          },
          required: ["code"]
        }
      }
    },
    {
      type: "function",
      function: {
        name: "lint_code",
        description: "Analisi statica del codice JavaScript/TypeScript. Rileva: var (→ let/const), == (→ ===), console.log, any TypeScript, TODO/FIXME, linee troppo lunghe, bracket non chiusi, pattern pericolosi. Restituisce lista di warning/errori con numero di riga.",
        parameters: {
          type: "object",
          properties: {
            code:     { type: "string", description: "Codice sorgente da analizzare" },
            language: { type: "string", enum: ["javascript","typescript","jsx","tsx"], description: "Linguaggio (default: typescript)" },
            strict:   { type: "boolean", description: "Modalità strict: include anche warning minori (default: false)" }
          },
          required: ["code"]
        }
      }
    },
    {
      type: "function",
      function: {
        name: "diff_text",
        description: "Mostra le differenze tra due testi/codici (diff unificato). Usa per confrontare versioni di file, vedere cosa è cambiato tra vecchio e nuovo codice.",
        parameters: {
          type: "object",
          properties: {
            original: { type: "string", description: "Testo originale (vecchia versione)" },
            modified: { type: "string", description: "Testo modificato (nuova versione)" },
            label_a:  { type: "string", description: "Label per l'originale (default: 'original')" },
            label_b:  { type: "string", description: "Label per la versione modificata (default: 'modified')" }
          },
          required: ["original", "modified"]
        }
      }
    },


  // ── P37-PA: python_analyze — analisi statica Python (2026-06-21) ────────────
  {
    type: 'function',
    function: {
      name: 'python_analyze',
      description: 'Analisi statica di codice Python lato backend: sintassi (ast.parse), complessità ciclomatica per funzione, lista import, suggerimenti di qualità. Opzionale: esegue in sandbox isolata con run_code=true. Usa per: trovare errori sintassi, misurare qualità codice, preparare review, debug prima di execute_shell.',
      parameters: {
        type: 'object',
        properties: {
          content:   { type: 'string',  description: 'Codice Python da analizzare (max 50000 caratteri).' },
          filepath:  { type: 'string',  description: 'Nome file (es. main.py) — opzionale, per logging.' },
          goal:      { type: 'string',  description: 'Obiettivo analisi (es. "trova bug", "misura qualità") — opzionale.' },
          run_code:  { type: 'boolean', description: 'Se true: esegue il codice in sandbox isolata dopo analisi (max 200 righe). Default: false.' }
        },
        required: ['content']
      }
    }
  },
];