File size: 5,665 Bytes
6a059d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client"

/**

 * Knowledge Base Selector - WeKnora Integration

 * 

 * Allows users to select which knowledge bases to query

 */

import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
import { Database, Scale, Newspaper, FileText, ChevronDown, Check } from "lucide-react"
import { cn } from "@/lib/utils"

interface KnowledgeBase {
  id: string
  name: string
  description: string
  icon: React.ReactNode
  docCount?: number
  color: string
}

// Real namespaces from vector store
const KNOWLEDGE_BASES: KnowledgeBase[] = [
  {
    id: "kenya_law",
    name: "Kenya Law",
    description: "Constitution, Acts, Bills, Case Law",
    icon: <Scale className="h-4 w-4" />,
    color: "bg-blue-500"
  },
  {
    id: "kenya_news",
    name: "Kenya News",
    description: "Current affairs and news articles",
    icon: <Newspaper className="h-4 w-4" />,
    color: "bg-green-500"
  },
  {
    id: "parliament",
    name: "Parliament",
    description: "Bills, Hansard, Committee Reports",
    icon: <FileText className="h-4 w-4" />,
    color: "bg-purple-500"
  },
  {
    id: "general",
    name: "General",
    description: "General knowledge base",
    icon: <Database className="h-4 w-4" />,
    color: "bg-orange-500"
  }
]

interface KnowledgeBaseSelectorProps {
  selected: string[]
  onChange: (selected: string[]) => void
  className?: string
}

export function KnowledgeBaseSelector({

  selected,

  onChange,

  className

}: KnowledgeBaseSelectorProps) {
  const [open, setOpen] = useState(false)

  const toggleKnowledgeBase = (id: string) => {
    if (selected.includes(id)) {
      onChange(selected.filter(s => s !== id))
    } else {
      onChange([...selected, id])
    }
  }

  const selectAll = () => {
    onChange(KNOWLEDGE_BASES.map(kb => kb.id))
  }

  const clearAll = () => {
    onChange([])
  }

  const selectedKBs = KNOWLEDGE_BASES.filter(kb => selected.includes(kb.id))

  return (
    <Popover open={open} onOpenChange={setOpen}>

      <PopoverTrigger asChild>

        <Button

          variant="outline"

          role="combobox"

          aria-expanded={open}

          className={cn("justify-between min-w-[200px]", className)}

        >

          <div className="flex items-center gap-2">

            <Database className="h-4 w-4 text-muted-foreground" />

            <span>

              {selected.length === 0 

                ? "Select sources" 

                : selected.length === KNOWLEDGE_BASES.length 

                  ? "All sources"

                  : `${selected.length} sources`}

            </span>

          </div>

          <ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />

        </Button>

      </PopoverTrigger>

      <PopoverContent className="w-[320px] p-0" align="start">

        <div className="border-b p-3 flex items-center justify-between">

          <span className="text-sm font-medium">Knowledge Bases</span>

          <div className="flex gap-2">

            <Button variant="ghost" size="sm" onClick={selectAll}>

              All

            </Button>

            <Button variant="ghost" size="sm" onClick={clearAll}>

              Clear

            </Button>

          </div>

        </div>

        <div className="p-2 space-y-1">

          {KNOWLEDGE_BASES.map((kb) => {

            const isSelected = selected.includes(kb.id)

            return (

              <div

                key={kb.id}

                className={cn(

                  "flex items-center gap-3 p-2 rounded-md cursor-pointer transition-colors",

                  isSelected ? "bg-accent" : "hover:bg-accent/50"

                )}

                onClick={() => toggleKnowledgeBase(kb.id)}

              >

                <Checkbox 

                  checked={isSelected}

                  onCheckedChange={() => toggleKnowledgeBase(kb.id)}

                />

                <div className={cn("p-1.5 rounded", kb.color, "text-white")}>

                  {kb.icon}

                </div>

                <div className="flex-1">

                  <div className="flex items-center gap-2">

                    <Label className="font-medium cursor-pointer">{kb.name}</Label>

                    {kb.docCount && (

                      <Badge variant="secondary" className="text-xs">

                        {(kb.docCount / 1000).toFixed(0)}k docs

                      </Badge>

                    )}

                  </div>

                  <p className="text-xs text-muted-foreground">{kb.description}</p>

                </div>

                {isSelected && <Check className="h-4 w-4 text-primary" />}

              </div>

            )

          })}

        </div>

        {selectedKBs.length > 0 && (

          <div className="border-t p-2">

            <div className="flex flex-wrap gap-1">

              {selectedKBs.map(kb => (

                <Badge 

                  key={kb.id} 

                  variant="secondary"

                  className="text-xs cursor-pointer"

                  onClick={() => toggleKnowledgeBase(kb.id)}

                >

                  {kb.name} ×

                </Badge>

              ))}

            </div>

          </div>

        )}

      </PopoverContent>

    </Popover>
  )
}

export default KnowledgeBaseSelector