File size: 12,756 Bytes
b6ecafa
 
 
b180108
b6ecafa
 
 
 
 
b180108
b6ecafa
 
b180108
 
 
 
 
 
 
b6ecafa
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
b180108
b6ecafa
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
b180108
b6ecafa
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
b180108
b6ecafa
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
b180108
b6ecafa
 
b180108
b6ecafa
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
b180108
b6ecafa
 
 
 
b180108
b6ecafa
b180108
b6ecafa
 
 
 
 
b180108
 
 
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
b180108
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use client'

import { useState, useEffect, useCallback } from 'react'
import { useTranslations } from 'next-intl'
import { Button } from '@/components/ui/button'

type Tab = 'status' | 'health' | 'models' | 'apicall'

export function DebugPanel() {
  const t = useTranslations('debug')
  const [activeTab, setActiveTab] = useState<Tab>('status')

  const tabLabels: Record<Tab, string> = {
    status: t('tabStatus'),
    health: t('tabHealth'),
    models: t('tabModels'),
    apicall: t('tabApiCall'),
  }

  return (
    <div className="m-4">
      <div className="flex gap-1 mb-4 border-b border-border pb-2">
        {(['status', 'health', 'models', 'apicall'] as const).map((tab) => (
          <Button
            key={tab}
            variant={activeTab === tab ? 'default' : 'ghost'}
            size="sm"
            onClick={() => setActiveTab(tab)}
          >
            {tabLabels[tab]}
          </Button>
        ))}
      </div>

      {activeTab === 'status' && <StatusTab />}
      {activeTab === 'health' && <HealthTab />}
      {activeTab === 'models' && <ModelsTab />}
      {activeTab === 'apicall' && <ApiCallTab />}
    </div>
  )
}

// ---------------------------------------------------------------------------
// Status Tab
// ---------------------------------------------------------------------------

function StatusTab() {
  const t = useTranslations('debug')
  const [data, setData] = useState<any>(null)
  const [loading, setLoading] = useState(true)

  const fetchStatus = useCallback(async () => {
    setLoading(true)
    try {
      const res = await fetch('/api/debug?action=status')
      setData(await res.json())
    } catch {
      setData({ error: 'Failed to fetch status' })
    } finally {
      setLoading(false)
    }
  }, [])

  useEffect(() => { fetchStatus() }, [fetchStatus])

  const reachable = data && !data.gatewayReachable === false && data.gatewayReachable !== false

  return (
    <div>
      <div className="flex items-center gap-3 mb-3">
        <span className="text-sm text-muted-foreground">{t('gateway')}:</span>
        {loading ? (
          <span className="text-xs text-muted-foreground">{t('checking')}</span>
        ) : (
          <span
            className={`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${
              reachable
                ? 'bg-green-500/20 text-green-400 border border-green-500/30'
                : 'bg-red-500/20 text-red-400 border border-red-500/30'
            }`}
          >
            {reachable ? t('reachable') : t('unreachable')}
          </span>
        )}
        <Button variant="ghost" size="xs" onClick={fetchStatus} disabled={loading}>
          {t('refresh')}
        </Button>
      </div>
      <pre className="bg-secondary rounded-lg p-4 text-xs font-mono overflow-auto max-h-96 text-foreground">
        {loading ? t('loading') : JSON.stringify(data, null, 2)}
      </pre>
    </div>
  )
}

// ---------------------------------------------------------------------------
// Health Tab
// ---------------------------------------------------------------------------

function HealthTab() {
  const t = useTranslations('debug')
  const [data, setData] = useState<any>(null)
  const [loading, setLoading] = useState(true)
  const [heartbeat, setHeartbeat] = useState<{ ok: boolean; latencyMs: number; timestamp: number } | null>(null)
  const [hbLoading, setHbLoading] = useState(false)

  const fetchHealth = useCallback(async () => {
    setLoading(true)
    try {
      const res = await fetch('/api/debug?action=health')
      setData(await res.json())
    } catch {
      setData({ healthy: false, error: 'Failed to fetch' })
    } finally {
      setLoading(false)
    }
  }, [])

  useEffect(() => { fetchHealth() }, [fetchHealth])

  const pingHeartbeat = async () => {
    setHbLoading(true)
    try {
      const res = await fetch('/api/debug?action=heartbeat')
      setHeartbeat(await res.json())
    } catch {
      setHeartbeat({ ok: false, latencyMs: -1, timestamp: Date.now() })
    } finally {
      setHbLoading(false)
    }
  }

  const healthy = data?.healthy === true || (data && !data.error && data.healthy !== false)

  return (
    <div>
      <div className="flex items-center gap-3 mb-3">
        <span className="text-sm text-muted-foreground">{t('health')}:</span>
        {loading ? (
          <span className="text-xs text-muted-foreground">{t('checking')}</span>
        ) : (
          <span
            className={`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${
              healthy
                ? 'bg-green-500/20 text-green-400 border border-green-500/30'
                : 'bg-red-500/20 text-red-400 border border-red-500/30'
            }`}
          >
            {healthy ? t('healthy') : t('unhealthy')}
          </span>
        )}
        <Button variant="ghost" size="xs" onClick={fetchHealth} disabled={loading}>
          {t('refresh')}
        </Button>
        <Button variant="outline" size="xs" onClick={pingHeartbeat} disabled={hbLoading}>
          {hbLoading ? t('pinging') : t('heartbeat')}
        </Button>
        {heartbeat && (
          <span className="text-xs text-muted-foreground">
            {heartbeat.ok ? t('ok') : t('failed')} - {heartbeat.latencyMs}ms
          </span>
        )}
      </div>

      {data && !loading && (
        <div className="bg-secondary rounded-lg p-4 text-xs overflow-auto max-h-96">
          <table className="w-full text-left">
            <tbody>
              {Object.entries(data).map(([key, value]) => (
                <tr key={key} className="border-b border-border/50 last:border-0">
                  <td className="py-1 pr-4 font-medium text-muted-foreground whitespace-nowrap">{key}</td>
                  <td className="py-1 font-mono text-foreground">
                    {typeof value === 'object' ? JSON.stringify(value, null, 2) : String(value)}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      )}
    </div>
  )
}

// ---------------------------------------------------------------------------
// Models Tab
// ---------------------------------------------------------------------------

interface ModelEntry {
  name?: string
  id?: string
  provider?: string
  context_length?: number
  [key: string]: any
}

function ModelsTab() {
  const t = useTranslations('debug')
  const [data, setData] = useState<any>(null)
  const [loading, setLoading] = useState(true)

  const fetchModels = useCallback(async () => {
    setLoading(true)
    try {
      const res = await fetch('/api/debug?action=models')
      setData(await res.json())
    } catch {
      setData({ models: [] })
    } finally {
      setLoading(false)
    }
  }, [])

  useEffect(() => { fetchModels() }, [fetchModels])

  const models: ModelEntry[] = Array.isArray(data?.models) ? data.models : (Array.isArray(data?.data) ? data.data : [])

  return (
    <div>
      <div className="flex items-center gap-3 mb-3">
        <span className="text-sm text-muted-foreground">{t('models')}</span>
        <Button variant="ghost" size="xs" onClick={fetchModels} disabled={loading}>
          {t('refresh')}
        </Button>
      </div>

      {loading ? (
        <p className="text-xs text-muted-foreground">{t('loading')}</p>
      ) : models.length === 0 ? (
        <p className="text-sm text-muted-foreground">{t('noModels')}</p>
      ) : (
        <div className="bg-secondary rounded-lg overflow-auto max-h-96">
          <table className="w-full text-xs text-left">
            <thead>
              <tr className="border-b border-border">
                <th className="py-2 px-3 font-medium text-muted-foreground">{t('colName')}</th>
                <th className="py-2 px-3 font-medium text-muted-foreground">{t('colProvider')}</th>
                <th className="py-2 px-3 font-medium text-muted-foreground">{t('colContextLength')}</th>
              </tr>
            </thead>
            <tbody>
              {models.map((m, i) => (
                <tr key={m.id || m.name || i} className="border-b border-border/50 last:border-0">
                  <td className="py-1.5 px-3 font-mono text-foreground">{m.name || m.id || '?'}</td>
                  <td className="py-1.5 px-3 text-muted-foreground">{m.provider || '-'}</td>
                  <td className="py-1.5 px-3 text-muted-foreground">{m.context_length ?? '-'}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      )}
    </div>
  )
}

// ---------------------------------------------------------------------------
// API Call Tab
// ---------------------------------------------------------------------------

function ApiCallTab() {
  const t = useTranslations('debug')
  const [method, setMethod] = useState<'GET' | 'POST'>('GET')
  const [path, setPath] = useState('/api/')
  const [body, setBody] = useState('')
  const [response, setResponse] = useState<any>(null)
  const [loading, setLoading] = useState(false)

  const send = async () => {
    setLoading(true)
    setResponse(null)
    try {
      let parsedBody: any = undefined
      if (method === 'POST' && body.trim()) {
        try {
          parsedBody = JSON.parse(body)
        } catch {
          setResponse({ error: 'Invalid JSON in body' })
          setLoading(false)
          return
        }
      }

      const res = await fetch('/api/debug?action=call', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ method, path, body: parsedBody }),
      })
      setResponse(await res.json())
    } catch {
      setResponse({ error: 'Request failed' })
    } finally {
      setLoading(false)
    }
  }

  return (
    <div>
      <div className="flex items-end gap-2 mb-4">
        <div>
          <label className="block text-xs text-muted-foreground mb-1">{t('method')}</label>
          <select
            value={method}
            onChange={(e) => setMethod(e.target.value as 'GET' | 'POST')}
            className="h-8 px-2 rounded border border-border bg-secondary text-foreground text-sm"
          >
            <option value="GET">GET</option>
            <option value="POST">POST</option>
          </select>
        </div>

        <div className="flex-1">
          <label className="block text-xs text-muted-foreground mb-1">{t('path')}</label>
          <input
            type="text"
            value={path}
            onChange={(e) => setPath(e.target.value)}
            placeholder="/api/"
            className="h-8 w-full px-2 rounded border border-border bg-secondary text-foreground text-sm font-mono"
          />
        </div>

        <Button variant="default" size="sm" onClick={send} disabled={loading}>
          {loading ? t('sending') : t('send')}
        </Button>
      </div>

      {method === 'POST' && (
        <div className="mb-4">
          <label className="block text-xs text-muted-foreground mb-1">{t('bodyJson')}</label>
          <textarea
            value={body}
            onChange={(e) => setBody(e.target.value)}
            rows={5}
            placeholder='{"key": "value"}'
            className="w-full px-3 py-2 rounded border border-border bg-secondary text-foreground text-xs font-mono resize-y"
          />
        </div>
      )}

      {response && (
        <div>
          {response.status && (
            <div className="flex items-center gap-2 mb-2">
              <span className="text-xs text-muted-foreground">{t('statusLabel')}:</span>
              <span
                className={`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${
                  response.status >= 200 && response.status < 300
                    ? 'bg-green-500/20 text-green-400 border border-green-500/30'
                    : response.status >= 400
                      ? 'bg-red-500/20 text-red-400 border border-red-500/30'
                      : 'bg-yellow-500/20 text-yellow-400 border border-yellow-500/30'
                }`}
              >
                {response.status} {response.statusText}
              </span>
              {response.contentType && (
                <span className="text-xs text-muted-foreground">{response.contentType}</span>
              )}
            </div>
          )}
          <pre className="bg-secondary rounded-lg p-4 text-xs font-mono overflow-auto max-h-96 text-foreground">
            {typeof response.body !== 'undefined'
              ? (typeof response.body === 'string' ? response.body : JSON.stringify(response.body, null, 2))
              : JSON.stringify(response, null, 2)}
          </pre>
        </div>
      )}
    </div>
  )
}