File size: 10,636 Bytes
0110dee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**

 * XML COMPILER PANEL

 * Natural language → valid XML system prompts

 * Control mini models (Granite, Nemotron, etc.) via XML-derived prompts

 */

import React, { useState } from 'react';

interface CompilationResult {
  mode: string;
  xmlOutput: string;
  validationStatus: 'VALID' | 'INVALID' | 'PARTIAL';
  metadata: {
    timestamp: number;
    executionTimeMs: number;
  };
}

interface ModelResponse {
  model: string;
  response: string;
  promptUsed: string;
  executionTimeMs: number;
}

export function XMLCompilerPanel() {
  const [mode, setMode] = useState<'gbnf' | 'skeleton' | 'dual-pass'>('skeleton');
  const [naturalLanguage, setNaturalLanguage] = useState('');
  const [xmlResult, setXmlResult] = useState<CompilationResult | null>(null);
  const [modelSelection, setModelSelection] = useState('nemotron');
  const [userQuery, setUserQuery] = useState('');
  const [modelResponse, setModelResponse] = useState<ModelResponse | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  async function compileToXML() {
    setLoading(true);
    setError(null);

    try {
      const res = await fetch('/api/xml/compile', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          mode,
          naturalLanguage,
          temperature: 0.3,
        }),
      });

      const data = await res.json();
      if (data.ok) {
        setXmlResult(data);
      } else {
        setError(data.error || 'Compilation failed');
      }
    } catch (e: any) {
      setError(e.message);
    } finally {
      setLoading(false);
    }
  }

  async function controlModel() {
    if (!xmlResult) {
      setError('Compile natural language to XML first');
      return;
    }

    setLoading(true);
    setError(null);

    try {
      const res = await fetch('/api/xml/control-model', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          xmlPrompt: xmlResult.xmlOutput,
          model: modelSelection,
          userQuery,
          temperature: 0.7,
          maxTokens: 512,
        }),
      });

      const data = await res.json();
      if (data.ok) {
        setModelResponse(data);
      } else {
        setError(data.error || 'Model control failed');
      }
    } catch (e: any) {
      setError(e.message);
    } finally {
      setLoading(false);
    }
  }

  return (
    <div style={{ display: 'flex', flexDirection: 'column', height: '100%', gap: '8px', padding: '12px' }}>

      <div style={{ fontSize: '14px', fontWeight: 600, color: '#00d4cc' }}>🔮 XML Compiler</div>



      {/* ===== STEP 1: COMPILE ===== */}

      <div style={{ border: '1px solid #3e3e42', padding: '12px', borderRadius: '4px' }}>

        <div style={{ fontSize: '12px', color: '#00d4cc', marginBottom: '8px' }}>Step 1: Natural Language → XML</div>



        <div style={{ marginBottom: '8px' }}>

          <label style={{ fontSize: '11px', color: '#a0a0a0' }}>Mode:</label>

          <select

            value={mode}

            onChange={(e) => setMode(e.target.value as any)}

            style={{

              width: '100%',

              padding: '4px 6px',

              backgroundColor: '#1e1e1e',

              color: '#d4d4d4',

              border: '1px solid #3e3e42',

              marginTop: '4px',

              fontSize: '11px',

            }}

          >

            <option value="gbnf">GBNF (Grammar-constrained, 100% valid)</option>

            <option value="skeleton">Skeleton (Fill {{PLACEHOLDERS}})</option>

            <option value="dual-pass">Dual-pass (CoT + XML)</option>

          </select>

        </div>



        <div style={{ marginBottom: '8px' }}>

          <label style={{ fontSize: '11px', color: '#a0a0a0' }}>Natural Language:</label>

          <textarea

            value={naturalLanguage}

            onChange={(e) => setNaturalLanguage(e.target.value)}

            placeholder="You are a zero-sorry Lean 4 verifier..."

            style={{

              width: '100%',

              height: '80px',

              padding: '6px',

              backgroundColor: '#1e1e1e',

              color: '#d4d4d4',

              border: '1px solid #3e3e42',

              marginTop: '4px',

              fontSize: '11px',

              fontFamily: 'monospace',

              resize: 'none',

            }}

          />

        </div>



        <button

          onClick={compileToXML}

          disabled={loading}

          style={{

            width: '100%',

            padding: '6px',

            backgroundColor: loading ? '#3e3e42' : '#00d4cc',

            color: loading ? '#a0a0a0' : '#000',

            border: 'none',

            borderRadius: '3px',

            cursor: loading ? 'not-allowed' : 'pointer',

            fontWeight: 600,

            fontSize: '12px',

          }}

        >

          {loading ? 'Compiling...' : 'Compile to XML'}

        </button>

      </div>



      {/* ===== RESULT ===== */}

      {xmlResult && (

        <div style={{ border: '1px solid #00d4cc', padding: '12px', borderRadius: '4px' }}>

          <div style={{ fontSize: '12px', color: '#00ff88', marginBottom: '8px' }}>

            ✓ {xmlResult.validationStatus === 'VALID' ? '✅ VALID XML' : '⚠️ ' + xmlResult.validationStatus}

          </div>



          <div

            style={{

              backgroundColor: '#1e1e1e',

              padding: '8px',

              borderRadius: '3px',

              fontSize: '10px',

              fontFamily: 'monospace',

              color: '#d4d4d4',

              maxHeight: '120px',

              overflow: 'auto',

              border: '1px solid #3e3e42',

              marginBottom: '8px',

            }}

          >

            <pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>

              {xmlResult.xmlOutput.slice(0, 500)}

              {xmlResult.xmlOutput.length > 500 ? '...' : ''}

            </pre>

          </div>



          <div style={{ fontSize: '10px', color: '#a0a0a0' }}>

            {xmlResult.metadata.executionTimeMs}ms

          </div>

        </div>

      )}



      {/* ===== STEP 2: CONTROL MODEL ===== */}

      {xmlResult && (

        <div style={{ border: '1px solid #3e3e42', padding: '12px', borderRadius: '4px' }}>

          <div style={{ fontSize: '12px', color: '#00d4cc', marginBottom: '8px' }}>Step 2: Control Model with XML</div>



          <div style={{ marginBottom: '8px' }}>

            <label style={{ fontSize: '11px', color: '#a0a0a0' }}>Model:</label>

            <select

              value={modelSelection}

              onChange={(e) => setModelSelection(e.target.value)}

              style={{

                width: '100%',

                padding: '4px 6px',

                backgroundColor: '#1e1e1e',

                color: '#d4d4d4',

                border: '1px solid #3e3e42',

                marginTop: '4px',

                fontSize: '11px',

              }}

            >

              <option value="nemotron">Nemotron (Nvidia)</option>

              <option value="granite">IBM Granite</option>

              <option value="mistral">Mistral</option>

              <option value="llama2">Llama 2</option>

            </select>

          </div>



          <div style={{ marginBottom: '8px' }}>

            <label style={{ fontSize: '11px', color: '#a0a0a0' }}>Query:</label>

            <textarea

              value={userQuery}

              onChange={(e) => setUserQuery(e.target.value)}

              placeholder="Ask the model something..."

              style={{

                width: '100%',

                height: '60px',

                padding: '6px',

                backgroundColor: '#1e1e1e',

                color: '#d4d4d4',

                border: '1px solid #3e3e42',

                marginTop: '4px',

                fontSize: '11px',

                fontFamily: 'monospace',

                resize: 'none',

              }}

            />

          </div>



          <button

            onClick={controlModel}

            disabled={loading}

            style={{

              width: '100%',

              padding: '6px',

              backgroundColor: loading ? '#3e3e42' : '#ffd700',

              color: loading ? '#a0a0a0' : '#000',

              border: 'none',

              borderRadius: '3px',

              cursor: loading ? 'not-allowed' : 'pointer',

              fontWeight: 600,

              fontSize: '12px',

            }}

          >

            {loading ? 'Running...' : 'Run Model'}

          </button>

        </div>

      )}



      {/* ===== MODEL RESPONSE ===== */}

      {modelResponse && (

        <div style={{ border: '1px solid #ffd700', padding: '12px', borderRadius: '4px' }}>

          <div style={{ fontSize: '12px', color: '#ffd700', marginBottom: '8px' }}>✓ Model Response ({modelResponse.model})</div>



          <div

            style={{

              backgroundColor: '#1e1e1e',

              padding: '8px',

              borderRadius: '3px',

              fontSize: '10px',

              fontFamily: 'monospace',

              color: '#d4d4d4',

              maxHeight: '120px',

              overflow: 'auto',

              border: '1px solid #3e3e42',

              marginBottom: '8px',

              whiteSpace: 'pre-wrap',

              wordBreak: 'break-word',

            }}

          >

            {modelResponse.response}

          </div>



          <div style={{ fontSize: '10px', color: '#a0a0a0' }}>

            {modelResponse.executionTimeMs}ms

          </div>

        </div>

      )}



      {/* ===== ERROR ===== */}

      {error && (

        <div

          style={{

            backgroundColor: '#ff5470',

            color: '#000',

            padding: '8px',

            borderRadius: '3px',

            fontSize: '11px',

            fontWeight: 600,

          }}

        >

          ❌ {error}

        </div>

      )}

    </div>
  );
}

/**

 * USAGE IN APP:

 *

 * import { XMLCompilerPanel } from './components/xml/XMLCompilerPanel';

 *

 * export function App() {

 *   return (

 *     <div style={{ display: 'grid', gridTemplateColumns: '1fr 300px', height: '100vh' }}>

 *       <SovereignIDE />

 *       <XMLCompilerPanel />

 *     </div>

 *   );

 * }

 */