@lincyaw/workspace 1.0.0-rc.16 → 1.0.0-rc.161
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/README.md +6 -4
- package/package.json +11 -6
- package/src/domain/paths.ts +0 -15
- package/src/node/agent-activity.ts +46 -0
- package/src/node/archive.ts +33 -0
- package/src/node/authoring-clients.ts +73 -20
- package/src/node/authoring-windows.ts +165 -0
- package/src/node/build.ts +16 -6
- package/src/node/capture.ts +40 -13
- package/src/node/config.ts +135 -50
- package/src/node/document-assets.ts +2 -2
- package/src/node/document-audio.ts +66 -47
- package/src/node/document-authoring.ts +30 -15
- package/src/node/document-check.ts +36 -0
- package/src/node/document-history.ts +34 -6
- package/src/node/document-images.ts +24 -14
- package/src/node/document-import.ts +123 -0
- package/src/node/document-index.ts +654 -0
- package/src/node/document-materials.ts +80 -0
- package/src/node/document-operations.ts +4 -2
- package/src/node/document-transcription.ts +2 -1
- package/src/node/document-voice.ts +121 -13
- package/src/node/git-runner.mjs +19 -0
- package/src/node/git.ts +141 -22
- package/src/node/host-activity.ts +17 -0
- package/src/node/host-upgrade.ts +28 -0
- package/src/node/jobs.ts +40 -8
- package/src/node/local-history.ts +85 -0
- package/src/node/locks.ts +12 -0
- package/src/node/mcp-settings.ts +68 -0
- package/src/node/media-upload-messages.ts +4 -0
- package/src/node/media-upload-page.ts +57 -0
- package/src/node/media-upload.css +4 -0
- package/src/node/media-uploads.ts +116 -0
- package/src/node/page-delete.ts +0 -1
- package/src/node/project-files.ts +2 -2
- package/src/node/projects.ts +20 -5
- package/src/node/review/agent-session-reviews.ts +211 -68
- package/src/node/review/agent-workspace.ts +151 -0
- package/src/node/review/git-review.ts +72 -29
- package/src/node/review/record.ts +41 -9
- package/src/node/review/routes.ts +9 -1
- package/src/node/review/windows-dependencies.ts +163 -0
- package/src/node/review/workspace-review.ts +87 -0
- package/src/node/revision-files.ts +54 -0
- package/src/node/revisions.ts +318 -80
- package/src/node/routes.ts +52 -59
- package/src/node/serve.ts +216 -46
- package/src/node/site.ts +5 -5
- package/src/node/source-editor.ts +66 -26
- package/src/node/speech-settings.ts +6 -8
- package/src/node/templates.ts +2 -2
- package/src/node/tree-operations.ts +425 -0
- package/src/node/tree-routes.ts +107 -0
- package/src/node/workbench-api.ts +14 -3
- package/src/node/workspace-changes.ts +130 -0
- package/src/node/workspace-migrate.ts +54 -0
- package/src/node/workspace-source.ts +133 -0
- package/src/node/writer.ts +21 -8
- package/templates/document/default/{lecture.json → index.pmd} +5 -1
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { dirname, join } from 'node:path'
|
|
5
|
+
import { importDocuments } from '@lincyaw/compiler'
|
|
6
|
+
import { DOCUMENT_ENTRY, DOCUMENT_IMPORT_MAX_BYTES, DOCUMENT_IMPORT_MAX_FILES, hasRole, jsonResponse, validateDocument } from '@lincyaw/kernel'
|
|
7
|
+
import type { DocumentImportAnswer, Route } from '@lincyaw/kernel'
|
|
8
|
+
import { readWorkspaceConfig } from './config.ts'
|
|
9
|
+
import type { WorkspaceConfig } from './config.ts'
|
|
10
|
+
import { catchRequestErrors, RequestError } from './errors.ts'
|
|
11
|
+
import type { DocumentLocks } from './locks.ts'
|
|
12
|
+
import type { Revisions } from './revisions.ts'
|
|
13
|
+
import type { DocumentWriter } from './writer.ts'
|
|
14
|
+
import type { DocumentsChange } from './document-operations.ts'
|
|
15
|
+
|
|
16
|
+
const bad = (status: number, code: string): never => { throw new RequestError(status, `document-import-${code}`) }
|
|
17
|
+
/** Word and PowerPoint files from before 2007, which the converter cannot read. */
|
|
18
|
+
const LEGACY = /\.(?:doc|ppt|pps)$/i
|
|
19
|
+
const plainPath = (path: unknown): path is string => typeof path === 'string' && path.length > 0 && path.length <= 500
|
|
20
|
+
&& !path.startsWith('/') && !/[\\\0:]/.test(path) && !path.split('/').some(part => !part || part === '.' || part === '..' || ['.git', '.presmium', 'node_modules', 'dist'].includes(part))
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Where a batch goes in the workspace: its folders at the top level under their own names when none is taken (one
|
|
24
|
+
* file imports as `<name>/`); a single folder whose name is taken gets the first free `<name>-<n>`; several folders, any
|
|
25
|
+
* of them taken, go together into the first free `imported`, `imported-<n>` (links between them stay relative).
|
|
26
|
+
*/
|
|
27
|
+
function placement(root: string, tops: string[]): { directory: string, place(path: string): string } {
|
|
28
|
+
const free = (name: string) => { let candidate = name; for (let n = 2; existsSync(join(root, candidate)); n++) candidate = `${name}-${n}`; return candidate }
|
|
29
|
+
if (!tops.some(top => existsSync(join(root, top)))) return { directory: tops.length === 1 ? tops[0]! : '', place: path => path }
|
|
30
|
+
if (tops.length === 1) {
|
|
31
|
+
const directory = free(tops[0]!)
|
|
32
|
+
return { directory, place: path => `${directory}${path.slice(tops[0]!.length)}` }
|
|
33
|
+
}
|
|
34
|
+
const directory = free('imported')
|
|
35
|
+
return { directory, place: path => `${directory}/${path}` }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Uploaded sources are converted before a single locked, rollback-capable workspace publication. */
|
|
39
|
+
export function documentImportRoute(options: { config: WorkspaceConfig, writer: DocumentWriter, locks: DocumentLocks, revisions?: Revisions, changed(change: DocumentsChange): Promise<void> }): Route {
|
|
40
|
+
const { config, writer, locks, revisions } = options
|
|
41
|
+
return {
|
|
42
|
+
role: 'owner', maxBody: DOCUMENT_IMPORT_MAX_BYTES + (1 << 20),
|
|
43
|
+
handle: (request, ctx) => catchRequestErrors(async () => {
|
|
44
|
+
if (request.method !== 'POST') return bad(405, 'method')
|
|
45
|
+
if (!hasRole(ctx.member, 'owner') || ctx.document !== '') return bad(403, 'forbidden')
|
|
46
|
+
if (config.singleDeck || !revisions) return bad(409, 'unavailable')
|
|
47
|
+
const form = await request.formData().catch(() => bad(400, 'invalid'))
|
|
48
|
+
const raw = form.get('paths')
|
|
49
|
+
if (typeof raw !== 'string' || raw.length > 512_000) return bad(400, 'invalid')
|
|
50
|
+
let paths: unknown
|
|
51
|
+
try { paths = JSON.parse(raw) } catch { return bad(400, 'invalid') }
|
|
52
|
+
const uploads = form.getAll('files')
|
|
53
|
+
if (!Array.isArray(paths) || paths.length === 0 || paths.length > DOCUMENT_IMPORT_MAX_FILES || paths.length !== uploads.length || !paths.every(plainPath)) return bad(400, 'invalid')
|
|
54
|
+
if (new Set(paths.map(path => path.toLowerCase())).size !== paths.length) return bad(400, 'invalid')
|
|
55
|
+
if (uploads.some(file => typeof file === 'string') || uploads.reduce((sum, file) => sum + (typeof file === 'string' ? 0 : file.size), 0) > DOCUMENT_IMPORT_MAX_BYTES) return bad(413, 'limit')
|
|
56
|
+
const temporary = mkdtempSync(join(tmpdir(), 'presmium-import-'))
|
|
57
|
+
try {
|
|
58
|
+
const source = join(temporary, 'source'), output = join(temporary, 'output')
|
|
59
|
+
mkdirSync(source)
|
|
60
|
+
for (const [index, path] of paths.entries()) {
|
|
61
|
+
const file = uploads[index] as File
|
|
62
|
+
request.signal.throwIfAborted()
|
|
63
|
+
mkdirSync(dirname(join(source, path)), { recursive: true })
|
|
64
|
+
writeFileSync(join(source, path), Buffer.from(await file.arrayBuffer()), { flag: 'wx' })
|
|
65
|
+
}
|
|
66
|
+
const report = await importDocuments(source, { out: output, preserveOriginal: true, workspaceRoot: config.root }).catch(() => bad(422, 'conversion'))
|
|
67
|
+
if (report.documents.length === 0) return bad(422, paths.some(path => LEGACY.test(path)) ? 'legacy' : 'empty')
|
|
68
|
+
const files: { path: string, content: Buffer }[] = []
|
|
69
|
+
let bytes = 0
|
|
70
|
+
const walk = (dir: string, path = '') => {
|
|
71
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
72
|
+
const rel = path ? `${path}/${entry.name}` : entry.name
|
|
73
|
+
if (entry.isDirectory()) walk(join(dir, entry.name), rel)
|
|
74
|
+
else {
|
|
75
|
+
const content = readFileSync(join(dir, entry.name))
|
|
76
|
+
if (files.length >= DOCUMENT_IMPORT_MAX_FILES || (bytes += content.byteLength) > DOCUMENT_IMPORT_MAX_BYTES) bad(413, 'limit')
|
|
77
|
+
files.push({ path: rel, content })
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
walk(output)
|
|
82
|
+
const documents = report.documents.map(dir => {
|
|
83
|
+
const checked = validateDocument(JSON.parse(readFileSync(join(output, dir, DOCUMENT_ENTRY), 'utf8')))
|
|
84
|
+
if (!checked.document || checked.issues.some(issue => issue.severity === 'error')) return bad(422, 'conversion')
|
|
85
|
+
checked.document.id = randomUUID()
|
|
86
|
+
files.find(file => file.path === `${dir}/${DOCUMENT_ENTRY}`)!.content = Buffer.from(`${JSON.stringify(checked.document, null, 2)}\n`)
|
|
87
|
+
return { dir, title: checked.document.metadata.title }
|
|
88
|
+
})
|
|
89
|
+
const release = await locks.acquire('', 'document:import')
|
|
90
|
+
try {
|
|
91
|
+
const answer = await revisions.transaction(async (): Promise<DocumentImportAnswer> => {
|
|
92
|
+
request.signal.throwIfAborted()
|
|
93
|
+
const { directory, place } = placement(config.root, [...new Set(files.map(item => item.path.split('/')[0]!))])
|
|
94
|
+
const placed = files.map(item => ({ ...item, path: place(item.path) }))
|
|
95
|
+
const tops = [...new Set(placed.map(item => item.path.split('/')[0]!))]
|
|
96
|
+
if (tops.some(top => existsSync(join(config.root, top)))) return bad(409, 'changed')
|
|
97
|
+
if (readWorkspaceConfig(config.root).singleDeck) return bad(409, 'unavailable')
|
|
98
|
+
if (await revisions.ignoresAny(placed.map(item => item.path))) return bad(403, 'forbidden')
|
|
99
|
+
await revisions.checkpoint(tops)
|
|
100
|
+
const checkpoint = await writer.checkpoint(tops)
|
|
101
|
+
const before = await revisions.head()
|
|
102
|
+
let committed = false
|
|
103
|
+
try {
|
|
104
|
+
for (const item of placed) { request.signal.throwIfAborted(); await writer.create(item.path, item.content, true) }
|
|
105
|
+
request.signal.throwIfAborted()
|
|
106
|
+
const revision = await revisions.commitWhileLocked({ paths: tops, summary: `Import ${documents.length} documents`, body: '', origin: { kind: 'document', member: ctx.member, operation: { op: 'create', dir: directory || tops[0]! } } })
|
|
107
|
+
if (!revision) return bad(409, 'changed')
|
|
108
|
+
committed = true
|
|
109
|
+
return { directory, documents: documents.map(doc => ({ ...doc, dir: place(doc.dir) })), files: files.length, unresolved: report.unresolved.length, warnings: report.warnings ?? 0, ...revision ? { revision } : {} }
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (await revisions.head() !== before) { committed = true; throw new RequestError(409, 'document-import-changed', { committed: true }) }
|
|
112
|
+
await checkpoint.restore()
|
|
113
|
+
throw error
|
|
114
|
+
} finally { if (committed || await revisions.head() === before) checkpoint.dispose() }
|
|
115
|
+
})
|
|
116
|
+
try { await options.changed({ removed: [], added: answer.documents.map(doc => doc.dir) }) }
|
|
117
|
+
catch { throw new RequestError(409, 'document-import-changed', { committed: true }) }
|
|
118
|
+
return jsonResponse(answer, 201)
|
|
119
|
+
} finally { release() }
|
|
120
|
+
} finally { rmSync(temporary, { recursive: true, force: true }) }
|
|
121
|
+
}),
|
|
122
|
+
}
|
|
123
|
+
}
|