Tinyflow.vue
1.37 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
<template>
<div ref="divRef" :class="['tinyflow', className]" :style="style" style="height: 100%"> </div>
</template>
<script setup lang="ts">
import { Item, Tinyflow as TinyflowNative } from './ui'
import './ui/index.css'
import { onMounted, onUnmounted, ref } from 'vue'
const props = defineProps<{
className?: string
style?: Record<string, string>
data?: Record<string, any>
provider?: {
llm?: () => Item[] | Promise<Item[]>
knowledge?: () => Item[] | Promise<Item[]>
internal?: () => Item[] | Promise<Item[]>
}
}>()
const divRef = ref<HTMLDivElement | null>(null)
let tinyflow: TinyflowNative | null = null
// 定义默认的 provider 方法
const defaultProvider = {
llm: () => [] as Item[],
knowledge: () => [] as Item[],
internal: () => [] as Item[]
}
onMounted(() => {
if (divRef.value) {
// 合并默认 provider 和传入的 props.provider
const mergedProvider = {
...defaultProvider,
...props.provider
}
tinyflow = new TinyflowNative({
element: divRef.value as Element,
data: props.data || {},
provider: mergedProvider
})
}
})
onUnmounted(() => {
if (tinyflow) {
tinyflow.destroy()
tinyflow = null
}
})
const getData = () => {
if (tinyflow) {
return tinyflow.getData()
}
console.warn('Tinyflow instance is not initialized')
return null
}
defineExpose({
getData
})
</script>