Phase E of the AFFiNE parity plan (ADR-15): - internal/ai: OpenAI-compatible, Anthropic (official Go SDK) and Ollama drivers; BYOK profiles per workspace and for the instance, with keys encrypted by DOCWELL_SECRET_KEY, failover order, a test call and usage counters. - Embeddings: an indexer that follows PocketBase like the search index, chunks pages along their blocks and keeps vectors in a bbolt file in the data dir (no cgo); progress, ignored pages and extra files. - Chat sessions scoped to a page, space or workspace, streamed as server-sent events, with citations to pages and blocks, fork and history; retrieval only from pages the asker can read. - Writing actions (rewrite, fix, shorter, longer, summarise, translate, continue, custom) in the editor's selection toolbar. - POST /mcp: MCP over Streamable HTTP with personal API tokens; read tokens search and read pages, write tokens also create and update them. - Web app chat panel (Ctrl+J), workspace AI settings, an admin AI page, OpenAPI, generated clients, migration and translations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
304 lines
11 KiB
Vue
304 lines
11 KiB
Vue
<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
|
||
<script setup lang="ts">
|
||
import type { AiFile, AiIndexStatus, AiSettings, SearchHit } from "@docwell/api-client";
|
||
import { DwButton, DwCallout } from "@docwell/ui";
|
||
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||
import { useI18n } from "vue-i18n";
|
||
import { useApi } from "../../api";
|
||
import { refreshAi } from "../../composables/useAi";
|
||
import { useFormErrors } from "../../composables/useFormErrors";
|
||
import AiProfiles from "./AiProfiles.vue";
|
||
|
||
// A workspace's AI setup (workspace admins): switches, providers, the
|
||
// embeddings index, extra files and pages left out (ADR-15).
|
||
const props = defineProps<{ workspace: string }>();
|
||
const api = useApi();
|
||
const { locale, t } = useI18n();
|
||
const errors = useFormErrors();
|
||
|
||
const settings = ref<AiSettings | null>(null);
|
||
const index = ref<AiIndexStatus | null>(null);
|
||
const files = ref<AiFile[]>([]);
|
||
const ignoredTitles = ref<Record<string, string>>({});
|
||
const busy = ref(false);
|
||
const unavailable = ref(false);
|
||
const query = ref("");
|
||
const hits = ref<SearchHit[]>([]);
|
||
|
||
async function loadIndex() {
|
||
index.value = await api.ai.index(props.workspace);
|
||
}
|
||
|
||
async function load() {
|
||
try {
|
||
settings.value = await api.ai.settings(props.workspace);
|
||
} catch (e) {
|
||
unavailable.value = true;
|
||
errors.show(e);
|
||
return;
|
||
}
|
||
await Promise.all([loadIndex(), api.ai.files(props.workspace).then((f) => (files.value = f))]);
|
||
for (const id of settings.value.ignored) void title(id);
|
||
}
|
||
onMounted(() => load().catch(errors.show));
|
||
|
||
// While the index is filling, check on it every few seconds.
|
||
let timer: ReturnType<typeof setInterval> | undefined;
|
||
watch(
|
||
() => !!index.value?.enabled && (index.value.running || index.value.indexed < index.value.pages),
|
||
(filling) => {
|
||
clearInterval(timer);
|
||
if (filling) timer = setInterval(() => void loadIndex().catch(() => {}), 4000);
|
||
},
|
||
);
|
||
onBeforeUnmount(() => clearInterval(timer));
|
||
|
||
async function title(id: string) {
|
||
if (ignoredTitles.value[id] !== undefined) return;
|
||
ignoredTitles.value[id] = "";
|
||
try {
|
||
ignoredTitles.value[id] = (await api.pages.get(id)).title;
|
||
} catch {
|
||
ignoredTitles.value[id] = id;
|
||
}
|
||
}
|
||
|
||
async function run(action: () => Promise<unknown>) {
|
||
busy.value = true;
|
||
errors.reset();
|
||
try {
|
||
await action();
|
||
} catch (e) {
|
||
errors.show(e);
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
const save = (patch: Partial<AiSettings>) =>
|
||
run(async () => {
|
||
if (!settings.value) return;
|
||
settings.value = await api.ai.saveSettings(props.workspace, { ...settings.value, ...patch });
|
||
refreshAi(props.workspace);
|
||
await loadIndex();
|
||
});
|
||
|
||
function changed() {
|
||
refreshAi(props.workspace);
|
||
void loadIndex().catch(errors.show);
|
||
}
|
||
|
||
const rebuild = () =>
|
||
run(async () => {
|
||
if (!window.confirm(t("ai.rebuildConfirm"))) return;
|
||
await api.ai.rebuildIndex(props.workspace);
|
||
await loadIndex();
|
||
});
|
||
|
||
const upload = (e: Event) =>
|
||
run(async () => {
|
||
const el = e.target as HTMLInputElement;
|
||
for (const f of Array.from(el.files ?? [])) await api.ai.addFile(props.workspace, f, f.name);
|
||
el.value = "";
|
||
files.value = await api.ai.files(props.workspace);
|
||
});
|
||
|
||
const removeFile = (f: AiFile) =>
|
||
run(async () => {
|
||
await api.ai.deleteFile(f.id);
|
||
files.value = await api.ai.files(props.workspace);
|
||
});
|
||
|
||
let searchTimer: ReturnType<typeof setTimeout> | undefined;
|
||
watch(query, (q) => {
|
||
clearTimeout(searchTimer);
|
||
if (!q.trim()) {
|
||
hits.value = [];
|
||
return;
|
||
}
|
||
searchTimer = setTimeout(async () => {
|
||
try {
|
||
hits.value = (await api.workspaces.search(props.workspace, q, 8)).items.filter((h) => !h.record);
|
||
} catch {
|
||
hits.value = [];
|
||
}
|
||
}, 250);
|
||
});
|
||
|
||
const ignore = (h: SearchHit) => {
|
||
if (!settings.value || settings.value.ignored.includes(h.id)) return;
|
||
ignoredTitles.value[h.id] = h.title;
|
||
query.value = "";
|
||
void save({ ignored: [...settings.value.ignored, h.id] });
|
||
};
|
||
const unignore = (id: string) => settings.value && save({ ignored: settings.value.ignored.filter((x) => x !== id) });
|
||
|
||
const progress = computed(() => {
|
||
const i = index.value;
|
||
return i && i.pages ? Math.min(100, Math.round((i.indexed / i.pages) * 100)) : 0;
|
||
});
|
||
const size = (n: number) =>
|
||
new Intl.NumberFormat(locale.value, { style: "unit", unit: "kilobyte", maximumFractionDigits: 0 }).format(n / 1024);
|
||
</script>
|
||
|
||
<template>
|
||
<section id="ai" class="dw-card space-y-4" data-testid="workspace-ai">
|
||
<div>
|
||
<h2 class="text-ui-lg">{{ $t("ai.settingsTitle") }}</h2>
|
||
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("ai.settingsHint") }}</p>
|
||
</div>
|
||
<DwCallout v-if="errors.message.value" tone="danger" size="ui">{{ errors.message.value }}</DwCallout>
|
||
|
||
<template v-if="settings && !unavailable">
|
||
<div class="space-y-2">
|
||
<label class="flex items-start gap-2 text-ui-md">
|
||
<input
|
||
type="checkbox"
|
||
:checked="settings.enabled"
|
||
:disabled="busy"
|
||
class="mt-1"
|
||
data-testid="ai-enabled"
|
||
@change="save({ enabled: !settings.enabled })"
|
||
/>
|
||
<span>
|
||
<span class="font-semibold">{{ $t("ai.enabled") }}</span>
|
||
<span class="block text-ui-sm text-ink-muted">{{ $t("ai.enabledHint") }}</span>
|
||
</span>
|
||
</label>
|
||
<label class="flex items-start gap-2 text-ui-md">
|
||
<input
|
||
type="checkbox"
|
||
:checked="settings.useInstanceProfiles"
|
||
:disabled="busy"
|
||
class="mt-1"
|
||
@change="save({ useInstanceProfiles: !settings.useInstanceProfiles })"
|
||
/>
|
||
<span>
|
||
<span class="font-semibold">{{ $t("ai.useInstance") }}</span>
|
||
<span class="block text-ui-sm text-ink-muted">{{ $t("ai.useInstanceHint") }}</span>
|
||
</span>
|
||
</label>
|
||
</div>
|
||
|
||
<div>
|
||
<h3 class="text-ui-md font-semibold">{{ $t("ai.profiles") }}</h3>
|
||
<p class="mb-2 text-ui-sm text-ink-muted">{{ $t("ai.profilesHint") }}</p>
|
||
<AiProfiles :workspace="workspace" @changed="changed" />
|
||
</div>
|
||
|
||
<div class="space-y-2">
|
||
<h3 class="text-ui-md font-semibold">{{ $t("ai.indexTitle") }}</h3>
|
||
<label class="flex items-start gap-2 text-ui-md">
|
||
<input
|
||
type="checkbox"
|
||
:checked="settings.index"
|
||
:disabled="busy"
|
||
class="mt-1"
|
||
data-testid="ai-index"
|
||
@change="save({ index: !settings.index })"
|
||
/>
|
||
<span>
|
||
<span class="font-semibold">{{ $t("ai.index") }}</span>
|
||
<span class="block text-ui-sm text-ink-muted">{{ $t("ai.indexHint") }}</span>
|
||
</span>
|
||
</label>
|
||
<template v-if="index && settings.index">
|
||
<p v-if="!index.profile" class="text-ui-sm text-warning">{{ $t("ai.noEmbedProfile") }}</p>
|
||
<template v-else>
|
||
<div
|
||
class="h-2 overflow-hidden rounded-full bg-surface-sunken"
|
||
role="progressbar"
|
||
:aria-valuenow="progress"
|
||
aria-valuemin="0"
|
||
aria-valuemax="100"
|
||
:aria-label="$t('ai.indexTitle')"
|
||
>
|
||
<div class="h-full bg-primary transition-[width]" :style="{ width: `${progress}%` }" />
|
||
</div>
|
||
<p class="text-ui-sm text-ink-muted" data-testid="ai-index-status">
|
||
{{
|
||
$t("ai.indexStatus", {
|
||
indexed: index.indexed,
|
||
pages: index.pages,
|
||
chunks: index.chunks,
|
||
model: index.model,
|
||
profile: index.profile,
|
||
})
|
||
}}
|
||
<template v-if="index.running"> · {{ $t("ai.indexing") }}</template>
|
||
</p>
|
||
<p v-if="index.lastError" class="text-ui-sm text-danger">
|
||
{{ $t("ai.lastError", { error: index.lastError }) }}
|
||
</p>
|
||
<DwButton size="sm" variant="secondary" :disabled="busy" @click="rebuild">{{ $t("ai.rebuild") }}</DwButton>
|
||
</template>
|
||
</template>
|
||
</div>
|
||
|
||
<div class="space-y-2">
|
||
<h3 class="text-ui-md font-semibold">{{ $t("ai.ignoredTitle") }}</h3>
|
||
<p class="text-ui-sm text-ink-muted">{{ $t("ai.ignoredHint") }}</p>
|
||
<ul v-if="settings.ignored.length" class="flex flex-wrap gap-1.5">
|
||
<li
|
||
v-for="id in settings.ignored"
|
||
:key="id"
|
||
class="flex items-center gap-1 rounded-full border border-line px-2 py-0.5 text-ui-sm"
|
||
>
|
||
{{ ignoredTitles[id] || "…" }}
|
||
<button class="text-ink-muted hover:text-danger" :aria-label="$t('workspace.remove')" @click="unignore(id)">
|
||
×
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
<div class="relative max-w-md">
|
||
<input
|
||
v-model="query"
|
||
type="search"
|
||
class="dw-input w-full"
|
||
:placeholder="$t('ai.ignoreSearch')"
|
||
:aria-label="$t('ai.ignoreSearch')"
|
||
/>
|
||
<ul
|
||
v-if="hits.length"
|
||
class="absolute z-10 mt-1 w-full rounded-md border border-line bg-surface-raised p-1 shadow-menu"
|
||
>
|
||
<li v-for="h in hits" :key="h.id">
|
||
<button class="w-full truncate rounded-sm px-2 py-1 text-left hover:bg-surface-hover" @click="ignore(h)">
|
||
{{ h.icon }} {{ h.title || $t("home.untitled") }}
|
||
<span class="text-ui-xs text-ink-muted">· {{ h.spaceName }}</span>
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="space-y-2">
|
||
<h3 class="text-ui-md font-semibold">{{ $t("ai.filesTitle") }}</h3>
|
||
<p class="text-ui-sm text-ink-muted">{{ $t("ai.filesHint") }}</p>
|
||
<ul v-if="files.length" class="divide-y divide-line">
|
||
<li v-for="f in files" :key="f.id" class="flex items-center gap-2 py-1.5 text-ui-md">
|
||
<span class="min-w-0 flex-1 truncate">{{ f.name }}</span>
|
||
<span class="text-ui-xs text-ink-muted"
|
||
>{{ size(f.size) }} · {{ f.indexed ? $t("ai.fileIndexed") : $t("ai.fileWaiting") }}</span
|
||
>
|
||
<button class="text-ui-sm text-danger hover:underline" :disabled="busy" @click="removeFile(f)">
|
||
{{ $t("workspace.remove") }}
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
<label class="inline-flex cursor-pointer items-center gap-2 dw-link text-ui-md">
|
||
<input
|
||
type="file"
|
||
multiple
|
||
accept=".txt,.md,.markdown,.html,.htm,.docx,.csv,.json"
|
||
class="sr-only"
|
||
:disabled="busy"
|
||
@change="upload"
|
||
/>
|
||
{{ $t("ai.addFiles") }}
|
||
</label>
|
||
</div>
|
||
</template>
|
||
</section>
|
||
</template>
|