|
2 | 2 | import { onMount } from 'svelte'; |
3 | 3 | import { Card, CardBody, Input, Button } from '@sveltestrap/sveltestrap'; |
4 | 4 | import { getVectorKnowledgeCollections } from '$lib/services/knowledge-base-service'; |
| 5 | + import { KnowledgeCollectionDisplayType } from '$lib/helpers/enums'; |
5 | 6 |
|
6 | 7 | const limit = 5; |
7 | 8 |
|
|
12 | 13 | const candidates = innerKnowledgeBases?.filter(x => !!x.name)?.map(x => { |
13 | 14 | return { |
14 | 15 | name: x.name, |
| 16 | + type: x.type, |
15 | 17 | disabled: x.disabled |
16 | 18 | }; |
17 | 19 | }); |
18 | | - candidates.map(x => { |
19 | | - console.log(x.disabled); |
20 | | - console.log(x.name); |
| 20 | +
|
| 21 | + /** @type {import('$agentTypes').AgentKnowledgeBase[]} */ |
| 22 | + const knowledgeBases = []; |
| 23 | + const unique = new Set(); |
| 24 | + candidates.forEach(x => { |
| 25 | + if (!unique.has(x.name)) { |
| 26 | + knowledgeBases.push(x); |
| 27 | + unique.add(x.name); |
| 28 | + } |
21 | 29 | }); |
22 | | - refresh(candidates); |
23 | | - return candidates; |
| 30 | +
|
| 31 | + refresh(knowledgeBases); |
| 32 | + return knowledgeBases; |
24 | 33 | } |
25 | 34 |
|
26 | | - /** @type {string[]} */ |
| 35 | + /** @type {any[]} */ |
27 | 36 | let knowledgeBaseOptions = []; |
28 | 37 |
|
29 | 38 | /** @type {import('$agentTypes').AgentKnowledgeBase[]} */ |
30 | 39 | let innerKnowledgeBases = []; |
31 | 40 |
|
32 | 41 | onMount(async () =>{ |
33 | | - const type = 'question-answer'; |
34 | | - getVectorKnowledgeCollections(type).then(data => { |
35 | | - const names = data || []; |
36 | | - knowledgeBaseOptions = ["", ...names]; |
| 42 | + getVectorKnowledgeCollections().then(data => { |
| 43 | + const list = data?.map(x => { |
| 44 | + return { |
| 45 | + name: x.name, |
| 46 | + type: x.type, |
| 47 | + displayName: getDisplayOption(x) |
| 48 | + }; |
| 49 | + }) || []; |
| 50 | + knowledgeBaseOptions = [{ |
| 51 | + name: "", |
| 52 | + type: "", |
| 53 | + displayName: "" |
| 54 | + }, ...list]; |
37 | 55 | }); |
38 | 56 | init(); |
39 | 57 | }); |
|
42 | 60 | const list = agent.knowledge_bases?.map(x => { |
43 | 61 | return { |
44 | 62 | ...x, |
| 63 | + displayName: getDisplayOption(x), |
45 | 64 | disabled: false |
46 | 65 | }; |
47 | 66 | }) || []; |
48 | 67 | refresh(list); |
49 | 68 | } |
50 | 69 |
|
| 70 | + /** @param {import('$agentTypes').AgentKnowledgeBase | any} b */ |
| 71 | + function getDisplayOption(b) { |
| 72 | + return `${b.name} ${!!KnowledgeCollectionDisplayType[b.type] |
| 73 | + ? `(${KnowledgeCollectionDisplayType[b.type]})` : ''}` |
| 74 | + } |
| 75 | +
|
51 | 76 | /** |
52 | 77 | * @param {any} e |
53 | 78 | * @param {number} idx |
|
56 | 81 | const found = innerKnowledgeBases.find((_, index) => index === idx); |
57 | 82 | if (!found) return; |
58 | 83 | |
59 | | - const name = e.target.value; |
60 | | - found.name = name; |
| 84 | + const vals = e.target.value.split("#"); |
| 85 | + found.name = vals[0]; |
| 86 | + found.type = vals[1]; |
61 | 87 | refresh(innerKnowledgeBases); |
62 | 88 | } |
63 | 89 |
|
|
66 | 92 | ...innerKnowledgeBases, |
67 | 93 | { |
68 | 94 | name: '', |
| 95 | + type: '', |
| 96 | + displayName: '', |
69 | 97 | disabled: false |
70 | 98 | } |
71 | 99 | ]; |
|
94 | 122 | innerKnowledgeBases = list?.map(x => { |
95 | 123 | return { |
96 | 124 | name: x.name, |
| 125 | + type: x.type, |
| 126 | + displayName: x.displayName, |
97 | 127 | disabled: x.disabled |
98 | 128 | } |
99 | 129 | }) || []; |
|
135 | 165 | <div class="utility-input line-align-center"> |
136 | 166 | <Input |
137 | 167 | type="select" |
138 | | - value={knowledge.name} |
139 | 168 | disabled={knowledge.disabled} |
140 | 169 | on:change={e => changeKnowledgeBase(e, uid)} |
141 | 170 | > |
142 | | - {#each [...knowledgeBaseOptions, "one.data-assistant"] as option} |
143 | | - <option value={option} selected={option == knowledge.name}>{option}</option> |
| 171 | + {#each [...knowledgeBaseOptions] as option} |
| 172 | + <option value={`${option.name}#${option.type}`} selected={option.name == knowledge.name}> |
| 173 | + {option.displayName} |
| 174 | + </option> |
144 | 175 | {/each} |
145 | 176 | </Input> |
146 | 177 | </div> |
|
0 commit comments