File size: 13,147 Bytes
509e21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client"

import { useState } from "react"
import { Progress } from "@/components/ui/progress"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { ArrowLeft } from "lucide-react"
import { SystemInfoForm } from "./system-info-form"
import { CategorySelection } from "./category-selection"
import { CategoryEvaluation } from "./category-evaluation"
import { EvaluationForm } from "./evaluation-form"
import { ResultsDashboard } from "./results-dashboard"
import { CATEGORIES } from "@/lib/category-data"

export type SystemInfo = {
  name: string
  url: string
  provider: string
  systemTypes: string[]
  deploymentContexts: string[]
  modality: string
  modelTag?: string
  knowledgeCutoff?: string
  modelType?: "foundational" | "fine-tuned" | "na"
  inputModalities?: string[]
  outputModalities?: string[]
}

export type CategoryScore = {
  benchmarkScore: number
  processScore: number
  totalScore: number
  status: "strong" | "adequate" | "weak" | "insufficient" | "not-evaluated"
  // optional metadata
  totalQuestions?: number
  totalApplicable?: number
  naCount?: number
}

export type EvaluationData = {
  systemInfo: SystemInfo | null
  selectedCategories: string[]
  excludedCategoryReasons?: Record<string, string>
  categoryScores: Record<string, CategoryScore>
  currentCategory: string | null
}

interface AIEvaluationDashboardProps {
  onBack?: () => void
  onSaveEvaluation?: (evaluation: any) => void
}

export function AIEvaluationDashboard({ onBack, onSaveEvaluation }: AIEvaluationDashboardProps) {
  const [currentStep, setCurrentStep] = useState<"system-info" | "categories" | "evaluation" | "results">("system-info")
  const [currentCategoryIndex, setCurrentCategoryIndex] = useState(0)
  const [evaluationData, setEvaluationData] = useState<EvaluationData>({
    systemInfo: null,
    selectedCategories: [],
    categoryScores: {},
    currentCategory: null,
  })

  const steps = [
    { id: "system-info", label: "System Info", number: 1 },
    { id: "categories", label: "Categories", number: 2 },
    { id: "evaluation", label: "Evaluation", number: 3 },
    { id: "results", label: "Results", number: 4 },
  ]

  const getOverallProgress = () => {
    if (currentStep === "system-info") return 10
    if (currentStep === "categories") return 25
    if (currentStep === "evaluation") {
      const completed = Object.keys(evaluationData.categoryScores).length
      const total = evaluationData.selectedCategories.length
      return total > 0 ? 25 + (completed / total) * 65 : 25
    }
    return 100
  }

  const handleSystemInfoComplete = (systemInfo: SystemInfo) => {
    setEvaluationData((prev) => ({ ...prev, systemInfo }))
    setCurrentStep("categories")
  }

  const handleCategoriesSelected = (categories: string[]) => {
    setEvaluationData((prev) => ({ ...prev, selectedCategories: categories }))
    setCurrentCategoryIndex(0)
    setCurrentStep("evaluation")
  }

  const handleCategoriesSelectedWithReasons = (categories: string[], excludedReasons: Record<string, string>) => {
    setEvaluationData((prev) => ({ ...prev, selectedCategories: categories, excludedCategoryReasons: excludedReasons }))
    setCurrentCategoryIndex(0)
    setCurrentStep("evaluation")
  }

  const handleCategoryComplete = (categoryId: string, score: CategoryScore) => {
    console.log("[v0] handleCategoryComplete called with:", { categoryId, score })

    setEvaluationData((prev) => {
      const newCategoryScores = { ...prev.categoryScores, [categoryId]: score }
      console.log("[v0] Updated categoryScores:", newCategoryScores)
      return {
        ...prev,
        categoryScores: newCategoryScores,
      }
    })

    const nextIndex = currentCategoryIndex + 1
    console.log(
      "[v0] Current index:",
      currentCategoryIndex,
      "Next index:",
      nextIndex,
      "Total categories:",
      evaluationData.selectedCategories.length,
    )

    if (nextIndex >= evaluationData.selectedCategories.length) {
      console.log("[v0] All categories complete, moving to results")
      setCurrentStep("results")
    } else {
      console.log("[v0] Moving to next category at index:", nextIndex)
      setCurrentCategoryIndex(nextIndex)
    }
  }

  const handleSaveEvaluation = async () => {
    console.log("[v0] handleSaveEvaluation called")
    console.log("[v0] evaluationData:", evaluationData)

    if (!evaluationData.systemInfo || evaluationData.selectedCategories.length === 0) {
      alert("Please complete system information and select categories before saving.")
      return
    }

    const timestamp = Date.now()
    const evaluationId = `eval-${timestamp}`
    console.log("[v0] Generated evaluationId:", evaluationId)

    console.log("[v0] Processing category scores:", evaluationData.categoryScores)

    const capabilityCategories = evaluationData.selectedCategories.filter((cat) => {
      const category = CATEGORIES.find((c) => c.id === cat)
      console.log("[v0] Category check:", cat, "type:", category?.type)
      return category?.type === "capability"
    })
    console.log("[v0] Capability categories:", capabilityCategories)

    const riskCategories = evaluationData.selectedCategories.filter((cat) => {
      const category = CATEGORIES.find((c) => c.id === cat)
      return category?.type === "risk"
    })
    console.log("[v0] Risk categories:", riskCategories)

    const strongCategories = Object.entries(evaluationData.categoryScores)
      .filter(([_, score]) => {
        console.log("[v0] Checking score for strong:", score)
        return score.status === "strong"
      })
      .map(([catId]) => catId)
    console.log("[v0] Strong categories:", strongCategories)

    const adequateCategories = Object.entries(evaluationData.categoryScores)
      .filter(([_, score]) => score.status === "adequate")
      .map(([catId]) => catId)
    console.log("[v0] Adequate categories:", adequateCategories)

    const weakCategories = Object.entries(evaluationData.categoryScores)
      .filter(([_, score]) => score.status === "weak")
      .map(([catId]) => catId)
    console.log("[v0] Weak categories:", weakCategories)

    const insufficientCategories = Object.entries(evaluationData.categoryScores)
      .filter(([_, score]) => score.status === "insufficient")
      .map(([catId]) => catId)
    console.log("[v0] Insufficient categories:", insufficientCategories)

    const evaluationJson = {
      id: evaluationId,
      systemName: evaluationData.systemInfo.name,
      provider: evaluationData.systemInfo.provider,
      version: evaluationData.systemInfo.url || "1.0",
      deploymentContext: evaluationData.systemInfo.deploymentContexts.join(", ") || "Production",
      evaluator: "Current User",
      modality: evaluationData.systemInfo.modality,
      evaluationDate: new Date().toISOString().split("T")[0],
      selectedCategories: evaluationData.selectedCategories,
  excludedCategoryReasons: evaluationData.excludedCategoryReasons || {},
      categoryEvaluations: evaluationData.categoryScores,
      overallStats: {
        completenessScore: 85, // Safe default value
        totalApplicable: evaluationData.selectedCategories.length,
        capabilityApplicable: capabilityCategories.length,
        riskApplicable: riskCategories.length,
        strongCategories,
        adequateCategories,
        weakCategories,
        insufficientCategories,
      },
    }

    console.log("[v0] Final evaluationJson:", evaluationJson)

    try {
      console.log("[v0] Creating blob and download")
      const blob = new Blob([JSON.stringify(evaluationJson, null, 2)], { type: "application/json" })
      const url = URL.createObjectURL(blob)
      const a = document.createElement("a")
      a.href = url
      a.download = `${evaluationId}.json`
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      URL.revokeObjectURL(url)

      console.log("[v0] Download completed successfully")
      alert(
        `Evaluation saved as ${evaluationId}.json. Please upload this file to the public/evaluations/ directory to see it on the homepage.`,
      )
      onBack?.()
    } catch (error) {
      console.error("[v0] Error saving evaluation:", error)
      alert("Error saving evaluation. Please try again.")
    }
  }

  const renderCurrentStep = () => {
    switch (currentStep) {
      case "system-info":
        return <SystemInfoForm onSubmit={handleSystemInfoComplete} initialData={evaluationData.systemInfo} />
      case "categories":
        return (
          <CategorySelection
            categories={CATEGORIES}
            selectedCategories={evaluationData.selectedCategories}
            onSelectionChange={handleCategoriesSelectedWithReasons}
          />
        )
      case "evaluation":
        return (
          <EvaluationForm
            categories={CATEGORIES}
            selectedCategories={evaluationData.selectedCategories}
            categoryScores={evaluationData.categoryScores}
            onScoreUpdate={(categoryId, score) => handleCategoryComplete(categoryId, score)}
            onComplete={() => setCurrentStep("results")}
          />
        )
      case "results":
        return (
          <ResultsDashboard
            systemInfo={evaluationData.systemInfo}
            categories={CATEGORIES}
            selectedCategories={evaluationData.selectedCategories}
            categoryScores={evaluationData.categoryScores}
            excludedCategoryReasons={evaluationData.excludedCategoryReasons || {}}
          />
        )
      default:
        return null
    }
  }

  return (
    <div className="min-h-screen bg-background flex flex-col">
      {/* Header */}
      <header className="border-b bg-card">
        <div className="container mx-auto px-6 py-4">
          <div className="flex items-center justify-between">
            <div className="flex items-center gap-4">
              {onBack && (
                <Button variant="ghost" size="sm" onClick={onBack} className="gap-2">
                  <ArrowLeft className="h-4 w-4" />
                  Back
                </Button>
              )}
              <div>
                <h1 className="text-2xl font-bold font-heading text-foreground">New Eval Card</h1>
                <p className="text-muted-foreground">Create comprehensive AI system evaluation card</p>
              </div>
            </div>
            <div className="flex items-center gap-4">
              <div className="text-right">
                <p className="text-sm text-muted-foreground">Overall Progress</p>
                <div className="flex items-center gap-2">
                  <Progress value={getOverallProgress()} className="w-24" />
                  <span className="text-sm font-medium">{Math.round(getOverallProgress())}%</span>
                </div>
              </div>
              {evaluationData.systemInfo && (
                <Badge variant="secondary" className="font-medium">
                  {evaluationData.systemInfo.name}
                </Badge>
              )}
              <Button onClick={handleSaveEvaluation} className="gap-2">
                Save Eval Card
              </Button>
            </div>
          </div>
        </div>
      </header>

      {/* Step tabs navigation */}
      <div className="border-b bg-card">
        <div className="container mx-auto px-6">
          <div className="flex items-center space-x-8">
            {steps.map((step) => {
              const isActive = currentStep === step.id
              const isCompleted =
                (step.id === "system-info" && evaluationData.systemInfo) ||
                (step.id === "categories" && evaluationData.selectedCategories.length > 0) ||
                (step.id === "evaluation" && Object.keys(evaluationData.categoryScores).length > 0) ||
                (step.id === "results" && currentStep === "results")

              return (
                <div
                  key={step.id}
                  className={`flex items-center gap-3 py-4 border-b-2 transition-colors ${
                    isActive
                      ? "border-primary text-primary"
                      : isCompleted
                        ? "border-green-500 text-green-600"
                        : "border-transparent text-muted-foreground"
                  }`}
                >
                  <div
                    className={`flex items-center justify-center w-8 h-8 rounded-full text-sm font-medium ${
                      isActive
                        ? "bg-primary text-primary-foreground"
                        : isCompleted
                          ? "bg-green-500 text-white"
                          : "bg-muted text-muted-foreground"
                    }`}
                  >
                    {step.number}
                  </div>
                  <span className="font-medium">{step.label}</span>
                </div>
              )
            })}
          </div>
        </div>
      </div>

  <div className="container mx-auto px-6 py-6 flex-1 min-h-0">{renderCurrentStep()}</div>
    </div>
  )
}