AI assistant
Premium add-on. This page covers a premium add-on, included in Scriviamo Pro and Pro + Collaboration. See pricing
The assistant add-on adds an AI writing assistant. On the selected text, or the paragraph being
edited when nothing is selected, it can:
- improve the writing;
- fix spelling and grammar;
- make it shorter, longer or simpler;
- make it more formal or more casual;
- translate it;
- continue writing from the caret;
- do anything the user asks.
import "scriviamo/dist/addons/scriviamo.assistant.js" // and scriviamo.assistant.css
The answer streams into a preview beside the text. Nothing in the document changes until the user chooses one of these:
- Replace puts the answer in place of the text.
- Insert Below adds it as new blocks, reading its Markdown, so lists and headings come through.
- Try Again asks again.
- Discard closes the preview. Escape does the same.
Replace and Insert Below are each one undo step. Stop ends an answer that's still coming.
Connecting a language model
The add-on doesn't come with an AI service of its own. You give it a provider: a function that takes the request and returns the answer. Three are built in:
| Provider | Talks to |
|---|---|
Assistant.fetchProvider(url) | An endpoint of your own, which returns the answer's text |
Assistant.openAIProvider(url, {model}) | Anything that speaks the OpenAI Chat Completions format: OpenAI, Azure OpenAI, OpenRouter, Mistral, Groq, Together, or a model running locally in Ollama, LM Studio or vLLM |
Assistant.anthropicProvider(url, {model}) | Anything that speaks the Anthropic Messages format: Claude |
Keep API keys on your server, never in the page. For a hosted service, the url is an endpoint
of your own that adds the key and passes the request on; only a model on your own network, such
as Ollama's, can be called directly.
All three send the same instructions: Assistant.systemPrompt, and a user message holding the
text, the text around it and the instruction. Assistant.prompt(request) gives both, for a
provider or server of your own.
Your own endpoint
import {Assistant} from "scriviamo/dist/addons/scriviamo.assistant.js"
Assistant.provider = Assistant.fetchProvider("/api/assistant")
fetchProvider posts {action, instruction, text, before, after, system, prompt} as JSON, and
streams the response body into the preview as it arrives. system and prompt are
Assistant.prompt's; a server can use them or write its own. Pass a second argument, as for
fetch, to add headers such as an authorization token.
OpenAI and compatible services
// Through your server, which adds the key (below):
Assistant.provider = Assistant.openAIProvider("/api/openai", {model: "your-model"})
// A local model in Ollama, which needs no key:
Assistant.provider = Assistant.openAIProvider("http://localhost:11434/v1/chat/completions", {model: "llama3.2"})
The request asks for a streamed answer, and the preview fills in as it's written. extra adds
settings to the request, such as {extra: {temperature: 0.3}}, and init adds fetch options
such as headers.
A pass-through endpoint for OpenAI, in Node. It fixes the model on the server, so the page can't
choose a more expensive one, and reads the key from OPENAI_API_KEY:
import {createServer} from "node:http"
createServer(async (request, response) => {
if (request.method != "POST" || request.url != "/api/openai") {
response.writeHead(404).end()
return
}
let body = ""
for await (const chunk of request) body += chunk
const {messages} = JSON.parse(body)
const abort = new AbortController()
request.on("close", () => abort.abort()) // the user pressed Stop or Discard
try {
const upstream = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`, "Content-Type": "application/json"},
body: JSON.stringify({model: process.env.OPENAI_MODEL, stream: true, messages}),
signal: abort.signal
})
response.writeHead(upstream.status, {"Content-Type": upstream.headers.get("content-type") || "text/event-stream", "Cache-Control": "no-store"})
for await (const chunk of upstream.body) response.write(chunk)
} catch (error) {
if (!response.headersSent) response.writeHead(502)
}
response.end()
}).listen(3000)
The same server works for any other Chat Completions service: change the address, the key and the model.
Claude
Assistant.anthropicProvider("/api/claude", {model: "claude-opus-5-5"}) speaks the Messages
format through a pass-through endpoint like the one above. Or keep the model, the prompt and the
settings on the server with fetchProvider and the
Anthropic TypeScript SDK. This Node server
streams Claude's answer back as plain text, and reads the key from ANTHROPIC_API_KEY:
import {createServer} from "node:http"
import Anthropic from "@anthropic-ai/sdk"
const client = new Anthropic()
const system = `You are a writing assistant inside a document editor. Do what the instruction says to the
text, and reply with only the resulting text: no preamble, quotes or explanation. Keep the text's
language unless the instruction says to translate it. Use Markdown only for lists, headings or
emphasis the result needs.`
createServer(async (request, response) => {
if (request.method != "POST" || request.url != "/api/assistant") {
response.writeHead(404).end()
return
}
let body = ""
for await (const chunk of request) body += chunk
const {instruction, text, before, after} = JSON.parse(body)
const stream = client.beta.messages.stream({
model: "claude-opus-5-5",
max_tokens: 16000,
output_config: {effort: "low"}, // quick edits; raise it for long or careful work
betas: ["server-side-fallback-2026-07-01"],
fallbacks: "default", // another model answers if this one declines
system,
messages: [{
role: "user",
content: `<before>${before}</before>\n<text>${text}</text>\n<after>${after}</after>\n\nInstruction: ${instruction}`
}]
})
request.on("close", () => stream.abort()) // the user pressed Stop or Discard
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store"})
try {
for await (const event of stream) {
if (event.type == "content_block_delta" && event.delta.type == "text_delta") response.write(event.delta.text)
}
const message = await stream.finalMessage()
if (message.stop_reason == "refusal") response.write("\n\n(The model declined to answer this.)")
} catch (error) {
if (!response.headersSent) response.writeHead(500)
}
response.end()
}).listen(3000)
Whichever you use, put your own checks in front of the endpoint, such as signing users in and limiting how often each can ask, since every request costs money. An error the service returns, such as an invalid key, shows in the preview, with Try Again.
Writing a provider
A provider receives an AssistantRequest:
| Member | Meaning |
|---|---|
action | "improve", "fix", "shorten", "lengthen", "simplify", "formal", "casual", "translate", "continue" or "ask" |
instruction | The instruction for the model, in plain English, with the language or the user's question filled in |
text | The text to work on; "" when continuing |
before, after | Up to Assistant.contextLength characters (2000) of the document before and after it, for context |
signal | An AbortSignal that fires when the user stops or discards the answer |
editor | The editor |
It returns the answer as a string, an async iterable of text chunks, a ReadableStream, or a
fetch Response, or a Promise of any of these. Throwing or rejecting shows the error in the
preview, with Try Again.
Assistant.provider = async function* (request) {
const response = await fetch("/api/assistant", {method: "POST", body: JSON.stringify(request), signal: request.signal})
const reader = response.body.getReader()
const decoder = new TextDecoder()
while (true) {
const {done, value} = await reader.read()
if (done) return
yield decoder.decode(value, {stream: true})
}
}
Assistant.useProvider(editor, provider) gives one editor its own provider. Without a provider,
the AI Assistant tool is disabled.
Actions
Assistant.actions lists the actions in menu order, each with an identifier, a caption (a
string name, so locale packs translate it), an icon and its instruction. Change the list
before creating editors to add your own, remove some, or reword the instructions:
Assistant.actions.push({
identifier: "headline",
caption: "Suggest a Headline",
icon: "bi bi-type-h1",
instruction: "Write one short headline for this text."
})
An action can ask the user first, with asks: "language" (filling {language} in its
instruction) or asks: "instruction" (filling {instruction}).
In code
| Member | Does |
|---|---|
Assistant.prompt(request) | The {system, user} prompt the built-in providers send |
Assistant.serverSentEvents(response) | Reads a server-sent event stream as {event, data} objects, for a provider of your own |
Assistant.run(editor, action, values) | Runs an action on the selection, or the block being edited, and shows the preview. values gives the language or instruction without asking. Resolves with the AssistantSession, or null. |
session.finished | Resolves when the answer has finished, failed or been stopped |
session.text, session.state | The answer so far, and "running", "done", "failed" or "closed" |
session.replace(), insertBelow(), ask(), stop(), close() | What the preview's buttons do |