-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathAdminAI.vue
More file actions
315 lines (295 loc) · 11 KB
/
AdminAI.vue
File metadata and controls
315 lines (295 loc) · 11 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!--
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="ai-settings">
<NcSettingsSection
:name="t('settings', 'Unified task processing')"
:description="t('settings', 'AI tasks can be implemented by different apps. Here you can set which app should be used for which task.')">
<NcCheckboxRadioSwitch
v-model="settings['ai.taskprocessing_guests']"
type="switch"
@update:modelValue="saveChanges">
{{ t('settings', 'Allow AI usage for guest users') }}
</NcCheckboxRadioSwitch>
<h3>{{ t('settings', 'Provider for Task types') }}</h3>
<template v-for="type in taskProcessingTaskTypes">
<div :key="type" class="tasktype-item">
<p class="tasktype-name">
{{ getTaskTypeName(type) }}
</p>
<NcCheckboxRadioSwitch
v-model="settings['ai.taskprocessing_type_preferences'][type.id]"
type="switch"
@update:modelValue="saveChanges">
{{ t('settings', 'Enable') }}
</NcCheckboxRadioSwitch><NcSelect
v-model="settings['ai.taskprocessing_provider_preferences'][type.id]"
class="provider-select"
:clearable="false"
:disabled="!settings['ai.taskprocessing_type_preferences'][type.id]"
:options="taskProcessingProviders.filter(p => p.taskType === type.id).map(p => p.id)"
@input="saveChanges">
<template #option="{ label }">
{{ taskProcessingProviders.find(p => p.id === label)?.name }}
</template>
<template #selected-option="{ label }">
{{ taskProcessingProviders.find(p => p.id === label)?.name }}
</template>
</NcSelect>
</div>
</template>
<template v-if="!hasTaskProcessing">
<NcNoteCard type="info">
{{ t('settings', 'None of your currently installed apps provide Task processing functionality') }}
</NcNoteCard>
</template>
</NcSettingsSection>
<NcButton class="legacy-toggle" variant="tertiary" @click="showLegacySections = !showLegacySections">
<template #icon>
<ChevronUpIcon v-if="showLegacySections" />
<ChevronDownIcon v-else />
</template>
{{ showLegacySections ? t('settings', 'Hide legacy provider settings') : t('settings', 'Show legacy provider settings') }}
</NcButton>
<div v-show="showLegacySections">
<NcSettingsSection
:name="t('settings', 'Machine translation (Legacy)')"
:description="t('settings', 'Machine translation can be implemented by different apps. Here you can define the precedence of the machine translation apps you have installed at the moment.')">
<NcNoteCard type="warning">
{{ t('settings', 'This API is deprecated. Install apps that provide Unified task processing to get the best experience.') }}
<a href="https://docs.nextcloud.com/server/latest/admin_manual/ai/overview.html" target="_blank" rel="noopener noreferrer">{{ t('settings', 'Learn more about AI providers') }}</a>
</NcNoteCard>
<VueDraggable v-model="settings['ai.translation_provider_preferences']" @change="saveChanges">
<div v-for="(providerClass, i) in settings['ai.translation_provider_preferences']" :key="providerClass" class="draggable__item">
<DragVerticalIcon /> <span class="draggable__number">{{ i + 1 }}</span> {{ translationProviders.find(p => p.class === providerClass)?.name }}
<NcButton aria-label="Move up" variant="tertiary" @click="moveUp(i)">
<template #icon>
<ArrowUpIcon />
</template>
</NcButton>
<NcButton aria-label="Move down" variant="tertiary" @click="moveDown(i)">
<template #icon>
<ArrowDownIcon />
</template>
</NcButton>
</div>
</VueDraggable>
</NcSettingsSection>
<NcSettingsSection
:name="t('settings', 'Image generation (Legacy)')"
:description="t('settings', 'Image generation can be implemented by different apps. Here you can set which app should be used.')">
<NcNoteCard type="warning">
{{ t('settings', 'This API is deprecated. Install apps that provide Unified task processing to get the best experience.') }}
<a href="https://docs.nextcloud.com/server/latest/admin_manual/ai/overview.html" target="_blank" rel="noopener noreferrer">{{ t('settings', 'Learn more about AI providers') }}</a>
</NcNoteCard>
<template v-for="provider in text2imageProviders">
<NcCheckboxRadioSwitch
:key="provider.id"
v-model="settings['ai.text2image_provider']"
:value="provider.id"
name="text2image_provider"
type="radio"
@update:modelValue="saveChanges">
{{ provider.name }}
</NcCheckboxRadioSwitch>
</template>
<template v-if="!hasText2ImageProviders">
<NcNoteCard type="info">
{{ t('settings', 'None of your currently installed apps provide image generation functionality') }}
</NcNoteCard>
</template>
</NcSettingsSection>
<NcSettingsSection
:name="t('settings', 'Text processing (Legacy)')"
:description="t('settings', 'Text processing tasks can be implemented by different apps. Here you can set which app should be used for which task.')">
<NcNoteCard type="warning">
{{ t('settings', 'This API is deprecated. Install apps that provide Unified task processing to get the best experience.') }}
<a href="https://docs.nextcloud.com/server/latest/admin_manual/ai/overview.html" target="_blank" rel="noopener noreferrer">{{ t('settings', 'Learn more about AI providers') }}</a>
</NcNoteCard>
<template v-for="type in tpTaskTypes">
<div :key="type">
<h3>{{ t('settings', 'Task:') }} {{ getTextProcessingTaskType(type).name }}</h3>
<p>{{ getTextProcessingTaskType(type).description }}</p>
<p> </p>
<NcSelect
v-model="settings['ai.textprocessing_provider_preferences'][type]"
class="provider-select"
:clearable="false"
:options="textProcessingProviders.filter(p => p.taskType === type).map(p => p.class)"
@input="saveChanges">
<template #option="{ label }">
{{ textProcessingProviders.find(p => p.class === label)?.name }}
</template>
<template #selected-option="{ label }">
{{ textProcessingProviders.find(p => p.class === label)?.name }}
</template>
</NcSelect>
<p> </p>
</div>
</template>
<template v-if="tpTaskTypes.length === 0">
<NcNoteCard type="info">
<!-- TRANSLATORS Text processing is the name of a Nextcloud-internal API -->
{{ t('settings', 'None of your currently installed apps provide text processing functionality using the Text Processing API.') }}
</NcNoteCard>
</template>
</NcSettingsSection>
</div>
</div>
</template>
<script>
import axios from '@nextcloud/axios'
import { loadState } from '@nextcloud/initial-state'
import { generateUrl } from '@nextcloud/router'
import { nextTick } from 'vue'
import VueDraggable from 'vuedraggable'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
import NcSelect from '@nextcloud/vue/components/NcSelect'
import NcSettingsSection from '@nextcloud/vue/components/NcSettingsSection'
import ArrowDownIcon from 'vue-material-design-icons/ArrowDown.vue'
import ArrowUpIcon from 'vue-material-design-icons/ArrowUp.vue'
import ChevronDownIcon from 'vue-material-design-icons/ChevronDown.vue'
import ChevronUpIcon from 'vue-material-design-icons/ChevronUp.vue'
import DragVerticalIcon from 'vue-material-design-icons/DragVertical.vue'
import logger from '../logger.ts'
export default {
name: 'AdminAI',
components: {
NcCheckboxRadioSwitch,
NcSettingsSection,
NcSelect,
VueDraggable,
ChevronDownIcon,
ChevronUpIcon,
DragVerticalIcon,
ArrowDownIcon,
ArrowUpIcon,
NcButton,
NcNoteCard,
},
data() {
return {
loading: false,
dirty: false,
showLegacySections: false,
groups: [],
loadingGroups: false,
sttProviders: loadState('settings', 'ai-stt-providers'),
translationProviders: loadState('settings', 'ai-translation-providers'),
textProcessingProviders: loadState('settings', 'ai-text-processing-providers'),
textProcessingTaskTypes: loadState('settings', 'ai-text-processing-task-types'),
text2imageProviders: loadState('settings', 'ai-text2image-providers'),
taskProcessingProviders: loadState('settings', 'ai-task-processing-providers'),
taskProcessingTaskTypes: loadState('settings', 'ai-task-processing-task-types'),
settings: loadState('settings', 'ai-settings'),
}
},
computed: {
hasTextProcessing() {
return Object.keys(this.settings['ai.textprocessing_provider_preferences']).length > 0 && Array.isArray(this.textProcessingTaskTypes)
},
tpTaskTypes() {
const builtinTextProcessingTypes = [
'OCP\\TextProcessing\\FreePromptTaskType',
'OCP\\TextProcessing\\HeadlineTaskType',
'OCP\\TextProcessing\\SummaryTaskType',
'OCP\\TextProcessing\\TopicsTaskType',
]
return Object.keys(this.settings['ai.textprocessing_provider_preferences'])
.filter((type) => !!this.getTextProcessingTaskType(type))
.filter((type) => !builtinTextProcessingTypes.includes(type))
},
hasText2ImageProviders() {
return this.text2imageProviders.length > 0
},
hasTaskProcessing() {
return Object.keys(this.settings['ai.taskprocessing_provider_preferences']).length > 0 && Array.isArray(this.taskProcessingTaskTypes)
},
},
methods: {
moveUp(i) {
this.settings['ai.translation_provider_preferences'].splice(
Math.min(i - 1, 0),
0,
...this.settings['ai.translation_provider_preferences'].splice(i, 1),
)
this.saveChanges()
},
moveDown(i) {
this.settings['ai.translation_provider_preferences'].splice(
i + 1,
0,
...this.settings['ai.translation_provider_preferences'].splice(i, 1),
)
this.saveChanges()
},
async saveChanges() {
this.loading = true
await nextTick()
const data = { settings: this.settings }
try {
await axios.put(generateUrl('/settings/api/admin/ai'), data)
} catch (error) {
logger.error('could not save changes', { error })
}
this.loading = false
},
getTaskTypeName(type) {
if (type.id === 'core:text2text') {
return t('settings', 'Generate text')
}
return type.name
},
getTextProcessingTaskType(type) {
if (!Array.isArray(this.textProcessingTaskTypes)) {
return null
}
return this.textProcessingTaskTypes.find((taskType) => taskType.class === type)
},
},
}
</script>
<style scoped>
.draggable__item {
margin-bottom: 5px;
display: flex;
align-items: center;
}
.draggable__item,
.draggable__item * {
cursor: grab;
}
.draggable__number {
border-radius: 20px;
border: 2px solid var(--color-primary-element);
color: var(--color-primary-element);
padding: 0px 7px;
margin-inline-end: 3px;
}
.drag-vertical-icon {
float: left;
}
.ai-settings h3 {
font-size: 16px; /* to offset against the 20px section heading */
}
.provider-select {
min-width: 350px !important;
}
.legacy-toggle {
margin-top: 12px;
margin-bottom: 12px;
}
.tasktype-item {
display: flex;
align-items: center;
gap: 8px;
.tasktype-name {
flex: 1;
margin: 0;
}
}
</style>