-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathindex.vue
More file actions
272 lines (250 loc) · 8.91 KB
/
index.vue
File metadata and controls
272 lines (250 loc) · 8.91 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<template>
<DrawerPro v-model="open" :header="headerTitle" size="large" @close="handleClose">
<template #content>
<div v-loading="loading">
<div class="toolbar">
<el-button type="primary" @click="openCreate">
{{ $t('commons.button.add') }}
</el-button>
</div>
<ComplexTable :data="items">
<el-table-column :label="$t('aiTools.model.model')" prop="id" min-width="220" />
<el-table-column :label="$t('commons.table.name')" prop="name" min-width="180" />
<el-table-column label="Context Window" prop="contextWindow" min-width="140" />
<el-table-column label="Max Tokens" prop="maxTokens" min-width="120" />
<el-table-column :label="$t('aiTools.agents.modelInputTypes')" min-width="140">
<template #default="{ row }">
{{ (row.input || []).join(', ') || '-' }}
</template>
</el-table-column>
<el-table-column :label="$t('aiTools.agents.reasoning')" min-width="120">
<template #default="{ row }">
<el-tag :type="row.reasoning ? 'success' : 'info'">
{{ row.reasoning ? $t('commons.true') : $t('commons.false') }}
</el-tag>
</template>
</el-table-column>
<fu-table-operations :buttons="buttons" :label="$t('commons.table.operate')" fixed="right" />
</ComplexTable>
</div>
</template>
<template #footer>
<span class="dialog-footer">
<el-button @click="open = false">{{ $t('commons.button.cancel') }}</el-button>
</span>
</template>
</DrawerPro>
<DrawerPro v-model="editorOpen" :header="editorTitle" size="normal" @close="handleEditorClose">
<el-form ref="formRef" :model="form" label-position="top" :rules="rules" v-loading="saving">
<el-form-item :label="$t('aiTools.model.model')" prop="id">
<el-input v-model="form.id" clearable />
</el-form-item>
<el-form-item :label="$t('commons.table.name')" prop="name">
<el-input v-model="form.name" />
</el-form-item>
<el-form-item label="Context Window" prop="contextWindow">
<el-input-number v-model="form.contextWindow" :min="1" :max="2000000" />
</el-form-item>
<el-form-item label="Max Tokens" prop="maxTokens">
<el-input-number v-model="form.maxTokens" :min="1" :max="2000000" />
</el-form-item>
<el-form-item :label="$t('aiTools.agents.modelInputTypes')" prop="input">
<el-checkbox-group v-model="form.input">
<el-checkbox label="text">Text</el-checkbox>
<el-checkbox label="image">Image</el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item :label="$t('aiTools.agents.reasoning')" prop="reasoning">
<el-switch v-model="form.reasoning" />
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button :disabled="saving" @click="editorOpen = false">{{ $t('commons.button.cancel') }}</el-button>
<el-button :disabled="saving" type="primary" @click="submit">
{{ $t('commons.button.confirm') }}
</el-button>
</span>
</template>
</DrawerPro>
</template>
<script setup lang="ts">
import { computed, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessageBox } from 'element-plus';
import {
createAgentAccountModel,
deleteAgentAccountModel,
getAgentAccountModels,
updateAgentAccountModel,
} from '@/api/modules/ai';
import { AI } from '@/api/interface/ai';
import i18n from '@/lang';
import { MsgError } from '@/utils/message';
import { getAgentProviderDisplayName } from '@/utils/agent';
import { Rules } from '@/global/form-rules';
const emit = defineEmits(['updated']);
const open = ref(false);
const editorOpen = ref(false);
const loading = ref(false);
const saving = ref(false);
const formRef = ref<FormInstance>();
const items = ref<AI.AgentAccountModel[]>([]);
const account = reactive({
id: 0,
name: '',
provider: '',
});
const form = reactive<AI.AgentAccountModel>({
recordId: 0,
id: '',
name: '',
contextWindow: 128000,
maxTokens: 8192,
reasoning: false,
input: ['text'],
});
const headerTitle = computed(() => {
const accountName = account.name || getAgentProviderDisplayName(account.provider, account.provider);
return `${accountName} · ${i18n.global.t('aiTools.agents.modelPool')}`;
});
const editorTitle = computed(() =>
form.recordId ? i18n.global.t('commons.button.edit') : i18n.global.t('commons.button.add'),
);
const rules = reactive({
id: [Rules.requiredInput, Rules.noSpace],
name: [Rules.requiredInput],
contextWindow: [Rules.integerNumber],
maxTokens: [Rules.integerNumber],
input: [Rules.requiredSelect],
});
const buttons = [
{
label: i18n.global.t('commons.button.edit'),
click: (row: AI.AgentAccountModel) => openEdit(row),
},
{
label: i18n.global.t('commons.button.delete'),
click: (row: AI.AgentAccountModel) => onDelete(row),
},
];
const buildModelItem = (model?: Partial<AI.AgentAccountModel>): AI.AgentAccountModel => ({
recordId: Number(model?.recordId || 0),
id: String(model?.id || '').trim(),
name: String(model?.name || '').trim(),
contextWindow: Number(model?.contextWindow || 0),
maxTokens: Number(model?.maxTokens || 0),
reasoning: Boolean(model?.reasoning),
input: Array.from(new Set((model?.input || []).filter((value) => value === 'text' || value === 'image'))),
});
const search = async () => {
if (!account.id) {
items.value = [];
return;
}
loading.value = true;
try {
const res = await getAgentAccountModels({ accountId: account.id });
items.value = (res.data || []).map((item) => buildModelItem(item));
} finally {
loading.value = false;
}
};
const resetForm = () => {
form.recordId = 0;
form.id = '';
form.name = '';
form.contextWindow = 128000;
form.maxTokens = 8192;
form.reasoning = false;
form.input = ['text'];
};
const openCreate = () => {
resetForm();
editorOpen.value = true;
};
const openEdit = (row: AI.AgentAccountModel) => {
Object.assign(form, buildModelItem(row));
editorOpen.value = true;
};
const submit = async () => {
if (!account.id || !formRef.value) {
return;
}
await formRef.value.validate();
const duplicate = items.value.some((item) => item.id === form.id && item.recordId !== form.recordId);
if (duplicate) {
MsgError(i18n.global.t('aiTools.agents.accountModelsDuplicate'));
return;
}
saving.value = true;
try {
if (form.recordId) {
await updateAgentAccountModel({
accountId: account.id,
model: buildModelItem(form),
});
} else {
await createAgentAccountModel({
accountId: account.id,
model: buildModelItem(form),
});
}
editorOpen.value = false;
await search();
emit('updated');
} catch (error: any) {
MsgError(String(error?.message || i18n.global.t('commons.res.commonError')));
} finally {
saving.value = false;
}
};
const onDelete = async (row: AI.AgentAccountModel) => {
await ElMessageBox.confirm(
i18n.global.t('commons.msg.delete', [row.name || row.id]),
i18n.global.t('commons.button.delete'),
{
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
},
);
await deleteAgentAccountModel({
accountId: account.id,
recordId: row.recordId,
});
await search();
emit('updated');
};
const handleEditorClose = () => {
formRef.value?.resetFields();
resetForm();
saving.value = false;
};
const handleClose = () => {
loading.value = false;
account.id = 0;
account.name = '';
account.provider = '';
items.value = [];
editorOpen.value = false;
handleEditorClose();
};
const openDrawer = async (row: AI.AgentAccountItem) => {
account.id = row.id;
account.name = row.name;
account.provider = row.provider;
open.value = true;
await search();
};
defineExpose({
open: openDrawer,
});
</script>
<style lang="scss" scoped>
.toolbar {
display: flex;
justify-content: flex-start;
margin-bottom: 16px;
margin-top: 5px;
}
</style>