@norio-office/rich-text 0.8.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -19
- package/README.md +145 -2394
- package/examples/ai/browser/App.vue +106 -0
- package/examples/ai/browser/README.md +30 -0
- package/examples/ai/browser/ai-proxy.d.mts +3 -0
- package/examples/ai/browser/ai-proxy.mjs +89 -0
- package/examples/ai/browser/vite.config.ts +3 -2
- package/package.json +4 -3
- package/skills/README.md +23 -0
- package/skills/assets/editor-screenshot.png +0 -0
- package/skills/norio-rich-text/SKILL.md +36 -0
- package/skills/norio-rich-text/examples/basic-document.json +7 -0
- package/skills/norio-rich-text/examples/custom-blocks-document.json +14 -0
- package/skills/norio-rich-text/examples/embedded-file-document.json +6 -0
- package/skills/norio-rich-text/examples/local-edit.json +10 -0
- package/skills/norio-rich-text/examples/media-reference-document.json +8 -0
- package/skills/norio-rich-text/examples/resource-map.json +7 -0
- package/skills/norio-rich-text/examples/sample-attachment.txt +1 -0
- package/skills/norio-rich-text/examples/streamed-document.ndjson +9 -0
- package/skills/norio-rich-text/examples/streamed-selection-edit.ndjson +3 -0
- package/skills/norio-rich-text/examples/styled-document.json +10 -0
- package/skills/norio-rich-text/examples/table-cell-edit.json +10 -0
- package/skills/norio-rich-text/references/document-format.md +17 -0
- package/skills/norio-rich-text/references/integration.md +46 -0
- package/skills/norio-rich-text/references/nordox-format.md +7 -0
- package/skills/norio-rich-text/references/operations.md +13 -0
- package/skills/norio-rich-text/references/streaming.md +15 -0
- package/skills/norio-rich-text/runtime-manifest.json +39 -0
- package/skills/norio-rich-text/schemas/document.schema.json +1580 -0
- package/skills/norio-rich-text/schemas/nordox-manifest.schema.json +105 -0
- package/skills/norio-rich-text/schemas/patch.schema.json +3164 -0
- package/skills/norio-rich-text/schemas/stream-event.schema.json +3409 -0
- package/skills/norio-rich-text/schemas/tool-inputs.schema.json +6028 -0
- package/skills/norio-rich-text/scripts/check-package.mjs +57 -0
- package/skills/norio-rich-text/scripts/generate-schemas.mjs +35 -0
- package/skills/norio-rich-text/scripts/rich-text.mjs +152 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises'
|
|
2
|
+
import { dirname, join, resolve } from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createRichTextStreamBuilder, createRichTextStreamDecoder,
|
|
7
|
+
getRichTextToolDefinitions, validateRichTextDocument, validateRichTextPatch,
|
|
8
|
+
} from '@norio-office/rich-text/ai-protocol'
|
|
9
|
+
import { packNordox, unpackNordox } from '@norio-office/rich-text/nordox'
|
|
10
|
+
|
|
11
|
+
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
12
|
+
const manifest = JSON.parse(await readFile(join(root, 'runtime-manifest.json'), 'utf8'))
|
|
13
|
+
const listed = new Set(manifest.files)
|
|
14
|
+
for (const relative of listed) {
|
|
15
|
+
const path = resolve(root, relative)
|
|
16
|
+
if (!path.startsWith(`${root}\\`) && !path.startsWith(`${root}/`)) throw new Error(`Unsafe listed path: ${relative}`)
|
|
17
|
+
if (!(await stat(path)).isFile()) throw new Error(`Not a file: ${relative}`)
|
|
18
|
+
}
|
|
19
|
+
for (const name of getRichTextToolDefinitions().map(tool => tool.name)) {
|
|
20
|
+
if (!manifest.tools.includes(name)) throw new Error(`Missing tool in manifest: ${name}`)
|
|
21
|
+
}
|
|
22
|
+
for (const name of ['basic-document', 'styled-document', 'custom-blocks-document', 'media-reference-document', 'embedded-file-document']) {
|
|
23
|
+
const value = JSON.parse(await readFile(join(root, 'examples', `${name}.json`), 'utf8'))
|
|
24
|
+
const result = validateRichTextDocument(value)
|
|
25
|
+
if (!result.valid) throw new Error(`${name}: ${JSON.stringify(result.errors)}`)
|
|
26
|
+
}
|
|
27
|
+
for (const name of ['local-edit', 'table-cell-edit']) {
|
|
28
|
+
const value = JSON.parse(await readFile(join(root, 'examples', `${name}.json`), 'utf8'))
|
|
29
|
+
const result = validateRichTextPatch(value)
|
|
30
|
+
if (!result.valid) throw new Error(`${name}: ${JSON.stringify(result.errors)}`)
|
|
31
|
+
}
|
|
32
|
+
for (const [name, mode] of [['streamed-document', 'createDocument'], ['streamed-selection-edit', 'replaceText']]) {
|
|
33
|
+
const decoder = createRichTextStreamDecoder()
|
|
34
|
+
const source = await readFile(join(root, 'examples', `${name}.ndjson`))
|
|
35
|
+
let builder
|
|
36
|
+
for (const event of decoder.push(source)) {
|
|
37
|
+
builder ??= createRichTextStreamBuilder({ streamId: event.streamId, mode })
|
|
38
|
+
builder.append(event)
|
|
39
|
+
}
|
|
40
|
+
decoder.finish()
|
|
41
|
+
builder?.result()
|
|
42
|
+
}
|
|
43
|
+
const example = JSON.parse(await readFile(join(root, 'examples/basic-document.json'), 'utf8'))
|
|
44
|
+
const archive = await packNordox(example)
|
|
45
|
+
await unpackNordox(archive)
|
|
46
|
+
const mediaExample = JSON.parse(await readFile(join(root, 'examples/media-reference-document.json'), 'utf8'))
|
|
47
|
+
const mediaArchive = await unpackNordox(await packNordox(mediaExample, {
|
|
48
|
+
assetPackaging: { images: 'reference', videos: 'reference', files: 'reference' },
|
|
49
|
+
}))
|
|
50
|
+
if (mediaArchive.manifest.resources.length !== 3 || mediaArchive.manifest.resources.some(resource => resource.storage !== 'external')) {
|
|
51
|
+
throw new Error('Media reference example did not produce three external resources.')
|
|
52
|
+
}
|
|
53
|
+
const skill = await readFile(join(root, 'SKILL.md'), 'utf8')
|
|
54
|
+
for (const link of skill.matchAll(/\]\((references\/[^)]+)\)/g)) {
|
|
55
|
+
if (!listed.has(link[1])) throw new Error(`Unlisted Skill reference: ${link[1]}`)
|
|
56
|
+
}
|
|
57
|
+
process.stdout.write(`Skill package verified: ${listed.size} files\n`)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { dirname, join } from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
getRichTextDocumentJsonSchema,
|
|
7
|
+
getRichTextPatchJsonSchema,
|
|
8
|
+
getRichTextStreamEventJsonSchema,
|
|
9
|
+
getRichTextToolDefinitions,
|
|
10
|
+
} from '@norio-office/rich-text/ai-protocol'
|
|
11
|
+
import { getNordoxManifestJsonSchema } from '@norio-office/rich-text/nordox'
|
|
12
|
+
|
|
13
|
+
const directory = join(dirname(fileURLToPath(import.meta.url)), '..', 'schemas')
|
|
14
|
+
const schemas = {
|
|
15
|
+
'document.schema.json': getRichTextDocumentJsonSchema(),
|
|
16
|
+
'nordox-manifest.schema.json': getNordoxManifestJsonSchema(),
|
|
17
|
+
'patch.schema.json': getRichTextPatchJsonSchema(),
|
|
18
|
+
'stream-event.schema.json': getRichTextStreamEventJsonSchema(),
|
|
19
|
+
'tool-inputs.schema.json': {
|
|
20
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
21
|
+
$id: 'https://norio-office.github.io/rich-text/tool-inputs.schema.json',
|
|
22
|
+
$defs: Object.fromEntries(getRichTextToolDefinitions().map(tool => [tool.name, tool.inputSchema])),
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const write = process.argv.includes('--write')
|
|
27
|
+
if (!write && !process.argv.includes('--check')) throw new Error('Use --write or --check')
|
|
28
|
+
if (write) await mkdir(directory, { recursive: true })
|
|
29
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
30
|
+
const path = join(directory, name)
|
|
31
|
+
const content = `${JSON.stringify(schema, null, 2)}\n`
|
|
32
|
+
if (write) await writeFile(path, content)
|
|
33
|
+
else if (await readFile(path, 'utf8') !== content) throw new Error(`${name} is out of sync; run --write`)
|
|
34
|
+
}
|
|
35
|
+
process.stdout.write(`${write ? 'Generated' : 'Checked'} ${Object.keys(schemas).length} schemas\n`)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createReadStream } from 'node:fs'
|
|
3
|
+
import { open, readFile } from 'node:fs/promises'
|
|
4
|
+
import { extname } from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
createRichTextStreamBuilder,
|
|
9
|
+
createRichTextStreamDecoder,
|
|
10
|
+
getRichTextDocumentSchema,
|
|
11
|
+
getRichTextToolDefinitions,
|
|
12
|
+
validateRichTextDocument,
|
|
13
|
+
validateRichTextPatch,
|
|
14
|
+
} from '@norio-office/rich-text/ai-protocol'
|
|
15
|
+
import { packNordox, unpackNordox } from '@norio-office/rich-text/nordox'
|
|
16
|
+
|
|
17
|
+
function argumentsOf(argv) {
|
|
18
|
+
const [command, ...rest] = argv
|
|
19
|
+
const options = {}
|
|
20
|
+
for (let index = 0; index < rest.length; index++) {
|
|
21
|
+
const argument = rest[index]
|
|
22
|
+
if (!argument.startsWith('--')) throw new Error(`Unexpected argument: ${argument}`)
|
|
23
|
+
const name = argument.slice(2)
|
|
24
|
+
if (name === 'stream') options.stream = true
|
|
25
|
+
else {
|
|
26
|
+
const value = rest[++index]
|
|
27
|
+
if (value === undefined) throw new Error(`Missing value for ${argument}`)
|
|
28
|
+
options[name] = value
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return { command, options }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function inputBytes(path) {
|
|
35
|
+
if (!path) throw new Error('--input is required')
|
|
36
|
+
if (path === '-') {
|
|
37
|
+
const chunks = []
|
|
38
|
+
for await (const chunk of process.stdin) chunks.push(chunk)
|
|
39
|
+
return Buffer.concat(chunks)
|
|
40
|
+
}
|
|
41
|
+
return readFile(path)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function* inputChunks(path) {
|
|
45
|
+
if (!path) throw new Error('--input is required')
|
|
46
|
+
const source = path === '-' ? process.stdin : createReadStream(path)
|
|
47
|
+
for await (const chunk of source) yield chunk
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function streamDocument(path, mode = 'createDocument') {
|
|
51
|
+
const decoder = createRichTextStreamDecoder()
|
|
52
|
+
let builder
|
|
53
|
+
for await (const chunk of inputChunks(path)) {
|
|
54
|
+
for (const event of decoder.push(chunk)) {
|
|
55
|
+
builder ??= createRichTextStreamBuilder({ streamId: event.streamId, mode })
|
|
56
|
+
builder.append(event)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
decoder.finish()
|
|
60
|
+
if (!builder) throw new Error('Stream contains no events')
|
|
61
|
+
return builder.result()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function assetPackaging(options) {
|
|
65
|
+
const packaging = {}
|
|
66
|
+
for (const kind of ['images', 'videos', 'files']) {
|
|
67
|
+
const mode = options[`asset-${kind}`]
|
|
68
|
+
if (mode !== undefined) {
|
|
69
|
+
if (mode !== 'embed' && mode !== 'reference') throw new Error(`--asset-${kind} must be embed or reference`)
|
|
70
|
+
packaging[kind] = mode
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return packaging
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function resourceResolver(path) {
|
|
77
|
+
if (!path) return undefined
|
|
78
|
+
const raw = JSON.parse(await readFile(path, 'utf8'))
|
|
79
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('Resource map must be an object')
|
|
80
|
+
return {
|
|
81
|
+
async readBytes(ref) {
|
|
82
|
+
const entry = raw[ref.id]
|
|
83
|
+
if (!entry || typeof entry.path !== 'string') throw new Error(`Resource ${ref.id} is not mapped`)
|
|
84
|
+
return { bytes: await readFile(entry.path), mimeType: entry.mimeType, name: entry.name }
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function writeNewFile(path, bytes) {
|
|
90
|
+
if (!path) throw new Error('--output is required')
|
|
91
|
+
const file = await open(path, 'wx')
|
|
92
|
+
try {
|
|
93
|
+
await file.writeFile(bytes)
|
|
94
|
+
} catch (error) {
|
|
95
|
+
await file.close()
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
await file.close()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function main() {
|
|
102
|
+
const { command, options } = argumentsOf(process.argv.slice(2))
|
|
103
|
+
if (command === 'capabilities') {
|
|
104
|
+
const schema = getRichTextDocumentSchema()
|
|
105
|
+
return { schemaVersion: '1', protocolVersion: 1, streamProtocolVersion: 1,
|
|
106
|
+
nodes: Object.keys(schema.nodes), marks: Object.keys(schema.marks), tools: getRichTextToolDefinitions().map(tool => tool.name) }
|
|
107
|
+
}
|
|
108
|
+
if (command === 'validate-document') {
|
|
109
|
+
const bytes = await inputBytes(options.input)
|
|
110
|
+
if (extname(options.input ?? '').toLowerCase() === '.nordox' || bytes[0] === 0x50 && bytes[1] === 0x4b) {
|
|
111
|
+
const unpacked = await unpackNordox(bytes)
|
|
112
|
+
return { valid: true, format: 'nordox', title: unpacked.manifest.title, resources: unpacked.manifest.resources.length }
|
|
113
|
+
}
|
|
114
|
+
const result = validateRichTextDocument(JSON.parse(bytes.toString('utf8')))
|
|
115
|
+
if (!result.valid) throw new Error(JSON.stringify(result.errors))
|
|
116
|
+
return { valid: true, format: 'json', schemaVersion: result.schemaVersion }
|
|
117
|
+
}
|
|
118
|
+
if (command === 'validate-patch') {
|
|
119
|
+
const value = JSON.parse((await inputBytes(options.input)).toString('utf8'))
|
|
120
|
+
const result = validateRichTextPatch(value)
|
|
121
|
+
if (!result.valid) throw new Error(JSON.stringify(result.errors))
|
|
122
|
+
return { valid: true, structuralOnly: true, operations: result.patch.operations.length }
|
|
123
|
+
}
|
|
124
|
+
if (command === 'validate-stream') {
|
|
125
|
+
const result = await streamDocument(options.input, options.mode ?? 'createDocument')
|
|
126
|
+
return { valid: true, mode: result.mode, events: result.seq, nodes: result.nodes.length, operations: result.operations.length }
|
|
127
|
+
}
|
|
128
|
+
if (command === 'create') {
|
|
129
|
+
let document
|
|
130
|
+
if (options.stream) {
|
|
131
|
+
const result = await streamDocument(options.input)
|
|
132
|
+
document = { type: 'doc', content: result.nodes }
|
|
133
|
+
} else {
|
|
134
|
+
document = JSON.parse((await inputBytes(options.input)).toString('utf8'))
|
|
135
|
+
}
|
|
136
|
+
const bytes = await packNordox(document, {
|
|
137
|
+
title: options.title ?? 'Untitled',
|
|
138
|
+
assetPackaging: assetPackaging(options),
|
|
139
|
+
resourceResolver: await resourceResolver(options['resource-map']),
|
|
140
|
+
})
|
|
141
|
+
await writeNewFile(options.output, bytes)
|
|
142
|
+
return { created: true, format: 'nordox', output: options.output, byteLength: bytes.byteLength }
|
|
143
|
+
}
|
|
144
|
+
throw new Error('Usage: rich-text.mjs capabilities | validate-document | validate-patch | validate-stream | create [--input path|-] [--output path] [--stream]')
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
main().then(result => {
|
|
148
|
+
process.stdout.write(`${JSON.stringify(result)}\n`)
|
|
149
|
+
}).catch(error => {
|
|
150
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`)
|
|
151
|
+
process.exitCode = 1
|
|
152
|
+
})
|