File size: 4,924 Bytes
0b9dc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { FileX, Search, SearchX } from 'lucide-react';
import { useMemo, useState } from 'react';

import type { KnowledgeBaseView, SessionKnowledgeConfig } from '@/api';
import { PanelEmpty } from '@/components/panel/PanelEmpty';
import { Badge } from '@/components/ui/badge';
import { Checkbox } from '@/components/ui/checkbox';
import { InputGroup, InputGroupAddon, InputGroupInput } from '@/components/ui/input-group';
import { Item, ItemContent, ItemDescription, ItemTitle } from '@/components/ui/item';
import { useTranslation } from '@/i18n/useI18n';

interface KnowledgeBasePanelProps {
	/** The user's knowledge bases. */
	knowledgeBases: KnowledgeBaseView[];
	/** Whether the KB list is still loading. */
	loading?: boolean;
	/**

	 * Current attachment for this session. `null` means no KBs attached

	 * and the panel renders with an empty selection.

	 */
	value: SessionKnowledgeConfig | null;
	/**

	 * Persist a new attachment to the session. The owner is responsible

	 * for awaiting the backend round-trip and refreshing session state.

	 * Pass `null` to detach every KB.

	 */
	onChange: (next: SessionKnowledgeConfig | null) => void;
	/** Disable the entire panel — e.g. when no session is selected. */
	disabled?: boolean;
}

/**

 * Pure content body for the Knowledge Base dock panel: a search box and

 * a checkbox list of the user's KBs.

 *

 * Middleware parameter editing lives in

 * {@link KnowledgeBaseParametersPopover}, which the owner mounts in the

 * panel header's `actions` slot — that keeps this body focused on

 * picking KBs rather than mixing two unrelated forms in the same scroll

 * area.

 */
export function KnowledgeBasePanel({

	knowledgeBases,

	loading = false,

	value,

	onChange,

	disabled = false,

}: KnowledgeBasePanelProps) {
	const { t } = useTranslation();
	const [search, setSearch] = useState('');

	const selectedIds = useMemo(() => new Set(value?.knowledge_base_ids ?? []), [value]);

	const filtered = search
		? knowledgeBases.filter((kb) => kb.name.toLowerCase().includes(search.toLowerCase()))
		: knowledgeBases;

	const toggleKb = (kbId: string, checked: boolean) => {
		const next = new Set(selectedIds);
		if (checked) next.add(kbId);
		else next.delete(kbId);
		const ids = Array.from(next);
		if (ids.length === 0) {
			onChange(null);
			return;
		}
		onChange({
			knowledge_base_ids: ids,
			parameters: value?.parameters ?? {},
		});
	};

	return (
		<div className="flex flex-col flex-1 min-h-0 gap-y-2">

			<span className="text-muted-foreground text-sm">

				{t('panel.knowledge.description')}

			</span>

			<InputGroup>

				<InputGroupInput

					placeholder={t('panel.knowledge.searchPlaceholder')}

					value={search}

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

					disabled={disabled}

				/>

				<InputGroupAddon align="inline-end">

					<Search />

				</InputGroupAddon>

			</InputGroup>



			<div className="flex flex-col flex-1 min-h-0 gap-y-3 overflow-y-auto">

				{loading ? (

					<div className="flex flex-1 items-center justify-center">

						<p className="text-muted-foreground text-sm">{t('panel.loading')}</p>

					</div>

				) : filtered.length === 0 ? (

					<PanelEmpty

						icon={search ? SearchX : FileX}

						title={

							search ? t('panel.search.emptyTitle') : t('panel.knowledge.emptyTitle')

						}

						description={

							search

								? t('panel.search.emptyDescription', { query: search })

								: t('panel.knowledge.emptyDescription')

						}

					/>

				) : (

					<div className="flex flex-col gap-y-2">

						{filtered.map((kb) => {

							const isSelected = selectedIds.has(kb.id);

							const inputId = `kb-${kb.id}`;

							return (

								<Item

									key={kb.id}

									variant="outline"

									data-selected={isSelected || undefined}

								>

									<Checkbox

										id={inputId}

										checked={isSelected}

										disabled={disabled}

										onCheckedChange={(checked) => toggleKb(kb.id, !!checked)}

									/>

									<ItemContent>

										<ItemTitle>

											<label htmlFor={inputId} className="cursor-pointer">

												{kb.name}

											</label>

										</ItemTitle>

										{kb.description ? (

											<ItemDescription>{kb.description}</ItemDescription>

										) : null}

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

											<Badge

												variant="outline"

												className="text-[10px] px-1 py-0"

											>

												{kb.embedding_model_config.model}

											</Badge>

											<Badge

												variant="outline"

												className="text-[10px] px-1 py-0"

											>

												{kb.embedding_model_config.dimensions}d

											</Badge>

										</div>

									</ItemContent>

								</Item>

							);

						})}

					</div>

				)}

			</div>

		</div>
	);
}