Files
Docwell/Database/pb_migrations/1764100000_ai.js
T
tajniak81andClaude Opus 5.5 78b7710090 Add AI chat, writing actions, workspace embeddings and an MCP server
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>
2026-11-15 21:58:22 +01:00

92 lines
4.0 KiB
JavaScript

// SPDX-License-Identifier: AGPL-3.0-or-later
/// <reference path="../pb_data/types.d.ts" />
// Phase E of docs/AFFINE.md: AI and MCP (ADR-15).
//
// - ai_profiles: provider settings (BYOK). workspace empty = an instance
// default a superadmin manages. api_key is AES-GCM ciphertext (base64).
// - workspaces.ai_settings: { enabled, useInstanceProfiles, index, ignored }.
// - ai_sessions: one person's chat in a workspace, messages inline as JSON.
// - ai_files: extra documents for retrieval; their text is extracted on upload.
migrate(
(app) => {
const users = app.findCollectionByNameOrId("users");
const workspaces = app.findCollectionByNameOrId("workspaces");
workspaces.fields.add(new JSONField({ name: "ai_settings", maxSize: 200000 }));
app.save(workspaces);
app.save(
new Collection({
type: "base",
name: "ai_profiles",
fields: [
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, cascadeDelete: true },
{ name: "name", type: "text", required: true, max: 80 },
{ name: "kind", type: "select", required: true, maxSelect: 1, values: ["openai", "anthropic", "ollama"] },
{ name: "base_url", type: "text", max: 500 },
{ name: "api_key", type: "text", max: 2000 },
{ name: "chat_model", type: "text", max: 200 },
{ name: "embed_model", type: "text", max: 200 },
{ name: "effort", type: "text", max: 20 },
{ name: "enabled", type: "bool" },
{ name: "position", type: "number" },
{ name: "requests", type: "number" },
{ name: "input_tokens", type: "number" },
{ name: "output_tokens", type: "number" },
{ name: "last_used", type: "date" },
{ name: "last_error", type: "text", max: 500 },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: ["CREATE INDEX idx_ai_profiles_ws ON ai_profiles (workspace, position)"],
}),
);
app.save(
new Collection({
type: "base",
name: "ai_sessions",
fields: [
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, required: true, cascadeDelete: true },
{ name: "user", type: "relation", collectionId: users.id, maxSelect: 1, required: true, cascadeDelete: true },
{ name: "title", type: "text", max: 200 },
{ name: "scope", type: "select", maxSelect: 1, values: ["workspace", "space", "page"] },
{ name: "scope_id", type: "text", max: 15 },
{ name: "messages", type: "json", maxSize: 5000000 },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: ["CREATE INDEX idx_ai_sessions_user ON ai_sessions (user, workspace, updated)"],
}),
);
app.save(
new Collection({
type: "base",
name: "ai_files",
fields: [
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, required: true, cascadeDelete: true },
{ name: "name", type: "text", required: true, max: 200 },
{ name: "content_text", type: "text", max: 2000000 },
{ name: "size", type: "number" },
{ name: "created_by", type: "relation", collectionId: users.id, maxSelect: 1 },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: ["CREATE INDEX idx_ai_files_ws ON ai_files (workspace)"],
}),
);
},
(app) => {
for (const name of ["ai_files", "ai_sessions", "ai_profiles"]) {
app.delete(app.findCollectionByNameOrId(name));
}
const ws = app.findCollectionByNameOrId("workspaces");
ws.fields.removeByName("ai_settings");
app.save(ws);
},
);