Files
Docwell/Server/internal/ai/write.go
T
tajniak81andClaude Opus 5.5 0ebf064173
CI / go (Server) (push) Failing after 25m5s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m38s
CI / desktop (ubuntu-latest) (push) Successful in 1h41m20s
CI / mobile (push) Failing after 4m19s
CI / docker (push) Failing after 44m8s
Docs / build (push) Successful in 17m11s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add audio transcripts, monthly AI limits, PDF sources and AI chat on phones
Follow-ups to Phase E (ADR-15):

- Transcription (E2): OpenAI-compatible profiles can name a speech-to-text
  model; POST /files/{id}/transcribe turns an audio or video attachment
  (up to 25 MB) into text and a summary with key points and action items.
  The editor gets a Transcribe button on media files that inserts the
  summary and the transcript in a toggle.
- Monthly token limits: ai_usage counts requests and tokens per workspace
  and month. Workspace admins can cap their workspace; superadmins can cap
  what each workspace uses through the server's providers, after which
  the workspace's own providers keep working. The admin panel lists usage.
- PDF text for extra retrieval files (ledongthuc/pdf, pure Go).
- Phone app: an AI chat screen for the workspace or a page, with streamed
  answers and sources that open the cited block.
- OpenAPI, generated clients, migration, translations and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:26:27 +01:00

95 lines
3.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package ai
import (
"context"
"strings"
"unicode/utf8"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
)
// ---- Writing actions on a selection --------------------------------------------------
// WriteInput is a writing action. Text is the selection; Before is the text
// in front of it (for "continue"); Language is the target of "translate";
// Instruction is the request of "custom".
type WriteInput struct {
Action string `json:"action"`
Text string `json:"text"`
Before string `json:"before"`
Language string `json:"language"`
Instruction string `json:"instruction"`
}
const writeSystem = `You are a writing assistant inside Docwell, a team wiki. You get a piece of a page and a task. Reply with the resulting text only, in Markdown, ready to put into the page: no preamble, no quotes around it, no remarks about what you changed. Keep the meaning, facts, names, links and formatting (lists, headings, emphasis) unless the task asks otherwise. Unless told to translate, write in the language of the text.`
var writeTasks = map[string]string{
"rewrite": "Rewrite the text so it reads clearly and naturally.",
"summarize": "Summarise the text in a few sentences or a short list of the key points.",
"translate": "Translate the text into %s.",
"continue": "Continue writing after the text, in the same voice and format. Reply with the continuation only.",
"shorter": "Make the text shorter, keeping what matters.",
"longer": "Expand the text with more detail and explanation, without inventing facts.",
"fix": "Fix spelling, grammar and punctuation. Change nothing else.",
"custom": "%s",
}
// Write streams the result of a writing action to onDelta.
func (s *Service) Write(ctx context.Context, c access.Caller, wsID string, in WriteInput, onDelta func(string)) error {
task, ok := writeTasks[in.Action]
if !ok {
return apperr.InvalidFields(map[string]string{"action": "Unknown writing action."})
}
text := strings.TrimSpace(in.Text)
if text == "" && in.Action != "continue" {
return apperr.InvalidFields(map[string]string{"text": "Select some text first."})
}
if utf8.RuneCountInString(text) > 20000 {
return apperr.InvalidFields(map[string]string{"text": "Select at most 20 000 characters."})
}
switch in.Action {
case "translate":
lang := strings.TrimSpace(in.Language)
if lang == "" || len(lang) > 60 {
return apperr.InvalidFields(map[string]string{"language": "Choose a language."})
}
task = strings.Replace(task, "%s", lang, 1)
case "custom":
ins := strings.TrimSpace(in.Instruction)
if ins == "" || utf8.RuneCountInString(ins) > 2000 {
return apperr.InvalidFields(map[string]string{"instruction": "Say what to do, in up to 2 000 characters."})
}
task = ins
}
_, chain, err := s.ready(ctx, c, wsID)
if err != nil {
return err
}
var prompt strings.Builder
prompt.WriteString("Task: " + task + "\n\n")
if in.Action == "continue" {
before := in.Before
if utf8.RuneCountInString(before) > 6000 {
r := []rune(before)
before = string(r[len(r)-6000:])
}
prompt.WriteString("Text so far:\n<text>\n" + strings.TrimSpace(before+"\n"+text) + "\n</text>")
} else {
prompt.WriteString("Text:\n<text>\n" + text + "\n</text>")
}
_, err = s.chat(ctx, wsID, chain, ChatRequest{
System: writeSystem, MaxTokens: 8000,
Messages: []Message{{Role: "user", Content: prompt.String()}},
}, onDelta)
if err != nil {
if _, ok := apperr.As(err); ok {
return err
}
return apperr.Invalid(userError(err))
}
return nil
}