catalog.ts
2.41 KB
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
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import type { CaseItem, DatasetItem, DatasetState, IntentItem } from "../types";
import { fetchCases, fetchDatasets, fetchIntents, setDataset } from "../api/intentFusion";
export const useCatalogStore = defineStore("catalog", () => {
const intents = ref<IntentItem[]>([]);
const cases = ref<CaseItem[]>([]);
const datasets = ref<DatasetItem[]>([]);
const currentDataset = ref<DatasetState | null>(null);
const loading = ref(false);
const datasetLoading = ref(false);
const error = ref<string | null>(null);
const domains = computed(() => {
const map = new Map<string, IntentItem[]>();
for (const i of intents.value) {
const arr = map.get(i.domain) ?? [];
arr.push(i);
map.set(i.domain, arr);
}
return Array.from(map.entries()).map(([domain, items]) => ({
domain,
items: items.slice().sort((a, b) => a.name.localeCompare(b.name)),
}));
});
const datasetKey = computed(() => currentDataset.value?.key ?? "");
async function refreshDatasets(): Promise<void> {
datasetLoading.value = true;
error.value = null;
try {
const res = await fetchDatasets();
datasets.value = res.items;
currentDataset.value = res.current;
} catch (e: any) {
error.value = e?.message ? String(e.message) : "加载失败";
} finally {
datasetLoading.value = false;
}
}
async function refresh(): Promise<void> {
loading.value = true;
error.value = null;
try {
const [i, c] = await Promise.all([fetchIntents(), fetchCases()]);
intents.value = i;
cases.value = c;
} catch (e: any) {
error.value = e?.message ? String(e.message) : "加载失败";
} finally {
loading.value = false;
}
}
async function switchDataset(key: string): Promise<void> {
if (!key || key === currentDataset.value?.key) return;
datasetLoading.value = true;
error.value = null;
try {
const res = await setDataset(key);
currentDataset.value = res.current;
await refresh();
} catch (e: any) {
error.value = e?.message ? String(e.message) : "切换失败";
} finally {
datasetLoading.value = false;
}
}
return {
intents,
cases,
domains,
datasets,
currentDataset,
datasetKey,
loading,
datasetLoading,
error,
refresh,
refreshDatasets,
switchDataset,
};
});