Viewer: preview files read-only, and ask your assistant about them
Open an attachment read-only with no toolbar: the file name picks the kit, your storage only reads, and your assistant answers questions about the open document without being able to change it.
Most files in an OA or CRM system are attachments: a contract in .docx, a quote in .xlsx, a
proposal in .pptx, a receipt in .pdf. People open them to read, not to edit. KitionViewer
shows any of them in your page with one call: it picks the kit by the file's name, opens the file
read-only, and leaves out the kit's ribbon tabs. Next to it, your own assistant can answer
questions about the open document; it cannot change it either.
The viewer is the read-only form of the embed (KitEmbed 1.9, EM32 in
the embed set). To let a person edit, see
Two modes.
Show a file
import { EmbedError, KitionViewer, type EmbedKitRoute } from '@kition/sdk/embed'
// The kit packages you host, with the file extensions each opens (with their dot).
const kits: EmbedKitRoute[] = [
{ id: 'kit.doc', url: 'https://cdn.example.com/kition/doc-ui/', extensions: ['.docx'] },
{ id: 'kit.sheet', url: 'https://cdn.example.com/kition/sheet-ui/', extensions: ['.xlsx', '.xls', '.csv'] },
{ id: 'kit.slide', url: 'https://cdn.example.com/kition/slide-ui/', extensions: ['.pptx'] },
{ id: 'kit.pdf', url: 'https://cdn.example.com/kition/pdf-ui/', extensions: ['.pdf'] },
{ id: 'kit.note', url: 'https://cdn.example.com/kition/note-ui/', extensions: ['.md'] },
]
try {
const viewer = await KitionViewer.mount(element, {
file: { id: attachment.id, name: attachment.name }, // the name picks the kit
kits,
storage: {
read: async (id) => ({ bytes: await fetchBytes(id), version: await currentVersion(id) }),
stamp: (id) => currentVersion(id), // the version without the bytes; null when it is gone
},
})
await viewer.ready()
} catch (error) {
if (error instanceof EmbedError && error.code === 'unsupported-file') showDownloadLink(attachment)
else throw error
}
- The storage needs only
readandstamp. The viewer writes nothing: the kit's saves are refused, it is given no way to make new files, and it keeps no replica. - A file no listed kit opens rejects with
EmbedErrorunsupported-filebefore anything loads: offer a download.kitForFile(name, kits)answers the same without mounting, so a list of attachments can show "Preview" only where it works. - When two kits open the same extension (
.html: HTML edits it, Doc only views it), the first inkitsopens it. - The handle has
kit,mode(alwaysview),ready(),on('error' | 'notify' | 'newerKit'),setTheme,setLocale,check(),agentPort()anddestroy(). It has nosave, no draft and no commands. identity,locale,themeandtimeoutMsare as in the embed.
What a viewer may take out
Use display to mark a contract with the reader’s name and withhold download, export, print and copy.
await KitionViewer.mount(element, {
file, kits, storage,
display: {
permissions: { download: false, export: false, share: false, copy: false },
watermark: { text: `${user.name} · Confidential` },
},
})
The shell draws the watermark over the document in every mode and repeats it on every page sent to the host printer. The mark is a visual deterrent. Anyone with the original file still has its unmarked bytes. Enforce access in your storage and download endpoints. If the shell cannot mark a print PDF, it reports a print failure and withholds the job.
| Setting | Effect |
|---|---|
export: false | Removes the shell’s export and print commands and shortcuts. The host refuses export writes and print requests. |
download: false | Removes the frame’s browser download grant and the shell’s download and Save a Copy commands. |
copy: false | Blocks copying, cutting and dragging content out of the frame, including clipboard writes by the connected kit. The shell’s search field still supports native text editing. Omit copy to allow it. |
A reader can still take screenshots. The host’s assistant can still read through the agent port.
A read-only canvas can show the engine’s selection outline without edit handles. With
KitionEmbed.mount, ui.panels: [] gives the panel columns’ space back to the document.
Kits acquire these shell behaviors when their editor package is rebuilt with the updated shell.
The files it opens
<!-- viewer-formats:begin (generated by tools/viewer-formats.mjs from the kits' kit.json) -->| Kit | Opens (it also edits these) | Opens read-only only | Tested in the viewer |
|---|---|---|---|
kit.doc | .docx | .html, .htm | yes: opens read-only, the assistant reads it |
kit.sheet | .xlsx, .xlsm | .xltx, .xltm, .xls, .xlt, .et, .ett, .xlsb, .ods, .csv | yes: opens read-only, the assistant reads it |
kit.slide | .kslide, .pptx | — | yes: opens read-only, the assistant reads it |
kit.pdf | .pdf | — | yes: opens read-only, the assistant reads it |
kit.note | .md, .markdown | — | yes: opens read-only, the assistant reads it |
kit.board | .kboard | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.cad | .dxf | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.design | .kdesign | .fig, .sketch | not yet: read-only holds, its look in a viewer is not reviewed |
kit.film | .kfilm | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.html | .html, .htm | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.layout | .klayout | .idml | not yet: read-only holds, its look in a viewer is not reviewed |
kit.motion | .kmotion | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.photo | .psd, .psb, .kphoto | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.scene | .kscene | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.table | .ktable | — | not yet: read-only holds, its look in a viewer is not reviewed |
kit.vector | .kvector | .svg, .ai, .eps | not yet: read-only holds, its look in a viewer is not reviewed |
.doc, .ppt, pictures (.png, .jpg) and plain text (.txt) are not opened yet: they get
unsupported-file.
"Tested" means examples/viewer-chat's end-to-end test opens a file of that kit read-only, without ribbon tabs, and its assistant reads it and is refused a write. Some kits still show more than a viewer needs (Note its File and View menus, Sheet its formula bar, PDF its comment tools); fixes in the kits' shared editor shell reach them with their next release.
Ask your assistant about the document
The chat is your page's, and the model runs on your server. viewer.agentPort() is the open
document as an MCP server; relay it to your server with relayAgentPort, and give your model its
tools:
import { relayAgentPort } from '@kition/sdk/embed'
void relayAgentPort(viewer.agentPort({ label: 'Assistant' }), {
next: async () => { /* the next MCP request for this viewer from your server, or null */ },
reply: async (message) => { /* send the viewer's answer back to your server */ },
})
On the server, createAgentRelay() of @kition/sdk/script/node is the other end: your model's
MCP client calls relay.request(message), and the page takes it with next and answers with
reply (Agents (MCP), EM19). Bind each relay to the signed-in person's page and the file
they have open.
What the assistant can do:
| It can | It cannot |
|---|---|
Read the document with <prefix>_inspect (JSON and a picture) | Change it with a command or a model operation. Writes answer read-only |
Discover reading commands with <prefix>_commands and call them with <prefix>_run, or run one in a <prefix>_batch with optional object loads | Run commands without an explicit mutates: false declaration, or use Undo or Redo |
Read its objects: <prefix>_query, where the kit has an object model | Make a draft for the person to merge: a viewer has none |
List what the kit finds wrong: <prefix>_check, where the kit checks | Open another file |
<prefix> is the kit's name. For example, find cell.get with sheet_commands and pass
steps: [{ command: 'cell.get', params: { cell: 'F3' } }] to sheet_run. For PDF, use
pdf_run with text.extract and a pages array to read beyond the initial preview.
The port checks every step before running any. Reads use the live document without saving or
opening a draft, even when a call asks for one.
examples/viewer-chat is a page with attachments, a viewer and a chat panel. Its assistant is a Mock AI that answers from what the viewer reads, with no model and no key; set three environment variables and the same seam talks to a model behind an OpenAI-compatible chat-completions endpoint, offered only the read tools.
How much loads
A kit package is the kit's whole editor: its engine (WebAssembly), its fonts and its interface.
The packages of the kits tested above are 23 MB (Note) to 39 MB (Slide) on disk; most of it is the
engine and the fonts, and the engine compresses to about a third. The browser downloads a package
the first time a file of that kind opens and keeps it in its cache after, per the cache headers
your kit origin sends: files whose names carry a content hash (under assets/) can be cached for
long, the rest should be revalidated. Opening a second file of the same kind loads nothing again
but the file.
Without code in your page: URL embedding
The embed server shows the same read-only page in an iframe from a signed URL: sign the open
token with perms that allow no edit, and add &toolbar=none (Embed: by link).
Asking an assistant needs KitionViewer in your own page: the URL page has no channel to your
agent.