@lincyaw/workspace 1.0.0-rc.6 → 1.0.0-rc.61
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/package.json +7 -5
- package/src/node/authoring-clients.ts +73 -20
- package/src/node/authoring-windows.ts +165 -0
- package/src/node/build.ts +58 -22
- package/src/node/capture.ts +356 -0
- package/src/node/config.ts +1 -0
- package/src/node/document-audio.ts +60 -41
- package/src/node/document-authoring.ts +7 -1
- package/src/node/document-images.ts +20 -10
- package/src/node/document-voice.ts +62 -5
- package/src/node/locks.ts +12 -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/projects.ts +18 -3
- package/src/node/review/agent-session-reviews.ts +167 -60
- package/src/node/review/agent-workspace.ts +107 -0
- package/src/node/review/git-review.ts +41 -11
- package/src/node/review/record.ts +4 -1
- package/src/node/review/routes.ts +7 -0
- package/src/node/review/workspace-review.ts +87 -0
- package/src/node/revision-files.ts +54 -0
- package/src/node/revisions.ts +217 -56
- package/src/node/routes.ts +8 -57
- package/src/node/serve.ts +92 -19
- package/src/node/site.ts +1 -1
- package/src/node/source-editor.ts +35 -19
- package/src/node/speech-settings.ts +6 -8
- package/src/node/templates.ts +2 -2
- package/src/node/workbench-api.ts +14 -3
- package/src/node/workspace-changes.ts +130 -0
- package/src/node/workspace-source.ts +133 -0
- package/src/node/writer.ts +3 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { CHANGES_MAX_REQUEST_CHARS, CHANGES_MAX_RESPONSE_CHARS, DOCUMENT_ENTRY, hasRole, jsonResponse, problemResponse } from '@lincyaw/kernel'
|
|
2
|
+
import type { ChangesCaller, WorkspaceChanges, WorkspaceChangesResult } from '@lincyaw/kernel'
|
|
3
|
+
import { validateChangesQuery, validateWorkspaceChangesRequest } from '@lincyaw/kernel/node'
|
|
4
|
+
import { listDocuments } from './config.ts'
|
|
5
|
+
import type { WorkspaceConfig } from './config.ts'
|
|
6
|
+
import { catchRequestErrors, RequestError } from './errors.ts'
|
|
7
|
+
import { documentsDiff } from './document-operations.ts'
|
|
8
|
+
import type { DocumentsChange } from './document-operations.ts'
|
|
9
|
+
import type { DocumentLocks } from './locks.ts'
|
|
10
|
+
import type { Revisions } from './revisions.ts'
|
|
11
|
+
import { MAX_SOURCE, validateDocumentSource } from './source-editor.ts'
|
|
12
|
+
import type { SourceEditor } from './source-editor.ts'
|
|
13
|
+
import { documentDirOf, isGeneratedPath } from './writer.ts'
|
|
14
|
+
import type { Route } from '@lincyaw/kernel'
|
|
15
|
+
|
|
16
|
+
const size = (value: unknown) => JSON.stringify(value).length
|
|
17
|
+
async function bounded<T>(work: () => Promise<T>): Promise<T> {
|
|
18
|
+
try { return await work() }
|
|
19
|
+
catch (error) {
|
|
20
|
+
const value = error as { message?: string, status?: number }
|
|
21
|
+
if (value.message?.startsWith('changes-') || value.message === 'authoring-review-required') throw error
|
|
22
|
+
throw new RequestError(value.status ?? 409, value.message === 'document-invalid' ? 'changes-document-invalid' : 'changes-operation-failed')
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Web and MCP share the same capture, policy, document locks and frozen Revisions application. */
|
|
26
|
+
export function createWorkspaceChanges(options: { config: WorkspaceConfig, editor: SourceEditor, locks: DocumentLocks, revisions?: Revisions, changed(change: DocumentsChange): Promise<void> | void }) {
|
|
27
|
+
const { config, editor, locks, revisions } = options
|
|
28
|
+
const scope = (document: string | undefined, caller: ChangesCaller) => {
|
|
29
|
+
if (!hasRole(caller.member, 'editor')) throw new RequestError(403, 'changes-permission-denied')
|
|
30
|
+
if (caller.boundDocument !== undefined) {
|
|
31
|
+
if (document !== undefined && document !== caller.boundDocument) throw new RequestError(403, 'changes-permission-denied')
|
|
32
|
+
return { document: caller.boundDocument, home: false }
|
|
33
|
+
}
|
|
34
|
+
return editor.scope(document ?? '')
|
|
35
|
+
}
|
|
36
|
+
const writer = (caller: ChangesCaller) => {
|
|
37
|
+
if (!hasRole(caller.member, 'editor') || caller.origin === 'agent' && (!hasRole(caller.member, 'owner') || caller.boundDocument !== undefined || caller.confirmed)) throw new RequestError(403, 'changes-permission-denied')
|
|
38
|
+
if (caller.origin === 'agent' && config.settings['presmium.assistant']?.review === 'required') throw new RequestError(409, 'authoring-review-required')
|
|
39
|
+
}
|
|
40
|
+
const budget = (value: unknown) => { if (size(value) > CHANGES_MAX_REQUEST_CHARS) throw new RequestError(413, 'changes-request-too-large') }
|
|
41
|
+
const service: WorkspaceChanges = {
|
|
42
|
+
list: (query, caller) => bounded(async () => {
|
|
43
|
+
budget(query)
|
|
44
|
+
if (!validateChangesQuery(query)) throw new RequestError(400, 'changes-request-invalid')
|
|
45
|
+
const owned = scope(query.document, caller)
|
|
46
|
+
if (!revisions) return { available: false, files: [] }
|
|
47
|
+
const state = await revisions.observeChanges(editor.roots(owned)), offset = query.offset ?? 0
|
|
48
|
+
if (query.snapshot !== undefined && query.snapshot !== state.snapshot || offset > 0 && !query.snapshot) throw new RequestError(409, 'changes-state-changed')
|
|
49
|
+
if (offset > state.files.length) throw new RequestError(400, 'changes-request-invalid')
|
|
50
|
+
const raw = new Map(state.raw.map(file => [file.path, file]))
|
|
51
|
+
const files = [] as import('@lincyaw/kernel').WorkspaceChangesAnswer['files']
|
|
52
|
+
for (const file of state.files.slice(offset, offset + 20)) {
|
|
53
|
+
if (!editor.canEdit(owned, file.path, caller.member)) throw new RequestError(403, 'changes-permission-denied')
|
|
54
|
+
const row = { ...file, ...raw.get(file.path)?.mode === 'unsupported' ? { unsupported: true as const } : {} }
|
|
55
|
+
if (size({ available: true, snapshot: state.snapshot, files: [...files, row], nextOffset: offset + files.length + 1 }) > CHANGES_MAX_RESPONSE_CHARS) break
|
|
56
|
+
files.push(row)
|
|
57
|
+
}
|
|
58
|
+
if (!files.length && offset < state.files.length) throw new RequestError(413, 'changes-response-too-large')
|
|
59
|
+
return { available: true, snapshot: state.snapshot, files, ...offset + files.length < state.files.length ? { nextOffset: offset + files.length } : {} }
|
|
60
|
+
}),
|
|
61
|
+
prepare: (request, caller) => bounded(async () => {
|
|
62
|
+
budget(request); writer(caller)
|
|
63
|
+
if (!validateWorkspaceChangesRequest(request) || request.action === 'commit' && !request.message.trim()) throw new RequestError(400, 'changes-request-invalid')
|
|
64
|
+
const action = request.action, message = request.action === 'commit' ? request.message.trim() : undefined
|
|
65
|
+
const owned = scope(request.document, caller)
|
|
66
|
+
if (!revisions) throw new RequestError(409, 'changes-git-unavailable')
|
|
67
|
+
const observed = await revisions.observeChanges(editor.roots(owned))
|
|
68
|
+
if (request.snapshot !== undefined && request.snapshot !== observed.snapshot) throw new RequestError(409, 'changes-state-changed')
|
|
69
|
+
const chosen = new Set(request.paths ?? observed.files.map(file => file.path))
|
|
70
|
+
if (!chosen.size) throw new RequestError(409, 'changes-empty')
|
|
71
|
+
const known = new Set(observed.files.map(file => file.path))
|
|
72
|
+
if ([...chosen].some(path => !known.has(path))) throw new RequestError(409, 'changes-selection-changed')
|
|
73
|
+
const paths = observed.files.filter(file => chosen.has(file.path)).map(file => file.path)
|
|
74
|
+
let captured = { ...observed, files: observed.files.filter(file => chosen.has(file.path)), raw: observed.raw.filter(file => chosen.has(file.path)) }
|
|
75
|
+
if (captured.raw.some(file => !file.change)) throw new RequestError(409, 'changes-file-unsupported')
|
|
76
|
+
const owners = paths.map(path => documentDirOf(config, path))
|
|
77
|
+
const check = () => {
|
|
78
|
+
writer(caller)
|
|
79
|
+
for (const [index, path] of paths.entries()) {
|
|
80
|
+
if (!editor.canEdit(owned, path, caller.member) || isGeneratedPath(path)) throw new RequestError(403, 'changes-permission-denied')
|
|
81
|
+
if (documentDirOf(config, path) !== owners[index]) throw new RequestError(409, 'changes-state-changed')
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
check()
|
|
85
|
+
if (action === 'commit') for (const file of captured.raw) {
|
|
86
|
+
if (file.path === DOCUMENT_ENTRY || file.path.endsWith(`/${DOCUMENT_ENTRY}`)) {
|
|
87
|
+
if (!file.change || !('content' in file.change)) throw new RequestError(409, 'changes-document-invalid')
|
|
88
|
+
if (Buffer.byteLength(file.change.content) > MAX_SOURCE) throw new RequestError(413, 'changes-selection-too-large')
|
|
89
|
+
let text: string
|
|
90
|
+
try { text = new TextDecoder('utf-8', { fatal: true }).decode(file.change.content) } catch { throw new RequestError(422, 'changes-document-invalid') }
|
|
91
|
+
validateDocumentSource(file.path, text)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (action === 'commit') captured = await revisions.freezeCommit(captured)
|
|
95
|
+
let pending: Promise<WorkspaceChangesResult> | undefined
|
|
96
|
+
return { paths: Object.freeze([...paths]), change: signal => pending ??= bounded(async () => {
|
|
97
|
+
signal?.throwIfAborted(); check()
|
|
98
|
+
const claim = locks.claimAll(owners.map(owner => owner ?? ''), `editor:${action}`)
|
|
99
|
+
if ('holder' in claim) throw new RequestError(409, 'changes-workspace-busy')
|
|
100
|
+
const before = listDocuments(config).map(document => document.dir)
|
|
101
|
+
try {
|
|
102
|
+
const revision = await revisions.applyChanges(action, captured, action === 'commit' ? { paths, summary: message!.split('\n')[0]!, body: message!.split('\n').slice(1).join('\n'), origin: { kind: caller.origin === 'agent' ? 'agent' : 'editor', member: caller.member } } : undefined, () => { signal?.throwIfAborted(); check() })
|
|
103
|
+
let refreshed: false | undefined
|
|
104
|
+
if (action === 'discard') {
|
|
105
|
+
try { await options.changed(documentsDiff(before, listDocuments(config).map(document => document.dir))) } catch { refreshed = false }
|
|
106
|
+
}
|
|
107
|
+
return { ...refreshed === false ? { refreshed } : {}, action, files: paths.length, ...action === 'commit' ? { committed: Boolean(revision), ...revision ? { revision } : {} } : {} }
|
|
108
|
+
} finally { claim.release() }
|
|
109
|
+
}) }
|
|
110
|
+
}),
|
|
111
|
+
}
|
|
112
|
+
const routes: Record<string, Route> = {
|
|
113
|
+
changes: { role: 'editor', handle: (request, ctx) => catchRequestErrors(async () => {
|
|
114
|
+
if (request.method !== 'GET') return problemResponse(405, 'GET')
|
|
115
|
+
if (!revisions) return jsonResponse({ files: [] })
|
|
116
|
+
const files = await revisions.uncommittedFiles(editor.roots(editor.scope(ctx.document)))
|
|
117
|
+
return jsonResponse({ files })
|
|
118
|
+
}) },
|
|
119
|
+
...Object.fromEntries((['commit', 'discard'] as const).map(action => [`changes/${action}`, { role: 'editor', handle: (request, ctx) => catchRequestErrors(async () => {
|
|
120
|
+
if (request.method !== 'POST') return problemResponse(405, 'POST')
|
|
121
|
+
const body: unknown = await request.json().catch(() => undefined)
|
|
122
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) throw new RequestError(400, 'changes-request-invalid')
|
|
123
|
+
const input = body as Record<string, unknown>
|
|
124
|
+
const prepared = await service.prepare({ ...input, document: ctx.document, action, ...action === 'discard' ? { confirm: true } : {} } as import('@lincyaw/kernel').WorkspaceChangesRequest, { member: ctx.member, origin: 'editor' })
|
|
125
|
+
const result = await prepared.change(request.signal)
|
|
126
|
+
return jsonResponse({ paths: prepared.paths, ...result.refreshed === false ? { refreshed: false } : {}, ...result.revision ? { revision: result.revision } : {} })
|
|
127
|
+
}) } satisfies Route])),
|
|
128
|
+
}
|
|
129
|
+
return { service, routes }
|
|
130
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { DOCUMENT_ENTRY, hasRole, SOURCE_FRAGMENT_CHARS, SOURCE_MAX_REQUEST_CHARS, SOURCE_MAX_RESPONSE_CHARS } from '@lincyaw/kernel'
|
|
3
|
+
import type { SourceCaller, SourceSaved, WorkspaceSource } from '@lincyaw/kernel'
|
|
4
|
+
import { validateSourceEdit, validateSourceList, validateSourceRead } from '@lincyaw/kernel/node'
|
|
5
|
+
import { listDocumentEntries } from './config.ts'
|
|
6
|
+
import type { WorkspaceConfig } from './config.ts'
|
|
7
|
+
import { RequestError } from './errors.ts'
|
|
8
|
+
import { saveSummary } from '../domain/commit-message.ts'
|
|
9
|
+
import type { Revisions } from './revisions.ts'
|
|
10
|
+
import { validateDocumentSource } from './source-editor.ts'
|
|
11
|
+
import type { SourceEditor } from './source-editor.ts'
|
|
12
|
+
|
|
13
|
+
const size = (value: unknown) => JSON.stringify(value).length
|
|
14
|
+
const fits = (value: unknown) => size(value) <= SOURCE_MAX_RESPONSE_CHARS
|
|
15
|
+
const digest = (value: string) => createHash('sha256').update(value).digest('hex')
|
|
16
|
+
const canonical = (path: string) => path === DOCUMENT_ENTRY || path.endsWith(`/${DOCUMENT_ENTRY}`)
|
|
17
|
+
/** SourceEditor keeps detailed conflicts for Web; MCP never returns full conflicting file contents. */
|
|
18
|
+
async function bounded<T>(work: () => Promise<T>): Promise<T> {
|
|
19
|
+
try { return await work() }
|
|
20
|
+
catch (error) {
|
|
21
|
+
const value = error as { message?: string, status?: number, code?: string, detail?: { conflict?: { version?: string }, exists?: boolean, issues?: { path?: string, code?: string, message?: string, severity?: string }[] } }
|
|
22
|
+
if (error instanceof Error && error.name === 'AbortError') throw error
|
|
23
|
+
if (value.message?.startsWith('source-') || value.message === 'authoring-review-required') throw error
|
|
24
|
+
if (value.message === 'document-invalid') {
|
|
25
|
+
const issues = value.detail?.issues?.slice(0, 4).map(issue => ({ path: issue.path?.slice(0, 500) ?? '', code: issue.code?.slice(0, 100) ?? 'document-invalid', message: issue.message?.slice(0, 240) ?? '', severity: issue.severity === 'warning' ? 'warning' : 'error' })) ?? []
|
|
26
|
+
while (size({ issues }) > SOURCE_MAX_RESPONSE_CHARS - 1024 && issues.length) issues.pop()
|
|
27
|
+
throw new RequestError(422, 'document-invalid', { issues })
|
|
28
|
+
}
|
|
29
|
+
if (value.message === 'document-identity-changed') throw new RequestError(422, 'document-identity-changed')
|
|
30
|
+
if (value.status === 409) throw new RequestError(409, value.detail?.exists ? 'source-file-exists' : 'source-version-changed', value.detail?.conflict?.version ? { version: value.detail.conflict.version } : undefined)
|
|
31
|
+
const status = value.status ?? ({ ENOENT: 404, EEXIST: 409, ELOOP: 403, EACCES: 403, EISDIR: 415 } as Record<string, number>)[value.code ?? ''] ?? 500
|
|
32
|
+
throw new RequestError(status, status === 404 ? 'source-file-not-found' : status === 409 ? 'source-file-exists' : status === 403 ? 'source-file-forbidden' : status === 413 ? 'source-file-too-large' : status === 415 ? 'source-text-required' : status < 500 ? 'source-request-invalid' : 'source-operation-failed')
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A bounded adapter over the native source scope, preflight, transaction and history owner. */
|
|
37
|
+
export function createWorkspaceSource(options: { config: WorkspaceConfig, editor: SourceEditor, revisions?: Revisions }): WorkspaceSource {
|
|
38
|
+
const { config, editor, revisions } = options
|
|
39
|
+
const scope = (document: string | undefined, caller: SourceCaller) => {
|
|
40
|
+
if (!hasRole(caller.member, 'editor')) throw new RequestError(403, 'source-file-forbidden')
|
|
41
|
+
if (caller.boundDocument !== undefined) {
|
|
42
|
+
if (document !== undefined && document !== caller.boundDocument) throw new RequestError(403, 'source-file-forbidden')
|
|
43
|
+
return { document: caller.boundDocument, home: false }
|
|
44
|
+
}
|
|
45
|
+
if (document && !listDocumentEntries(config).some(entry => entry.dir === document)) throw new RequestError(404, 'source-file-not-found')
|
|
46
|
+
return editor.scope(document ?? '')
|
|
47
|
+
}
|
|
48
|
+
const request = (value: unknown) => { if (size(value) > SOURCE_MAX_REQUEST_CHARS) throw new RequestError(413, 'source-request-too-large') }
|
|
49
|
+
const writer = (caller: SourceCaller) => {
|
|
50
|
+
if (!hasRole(caller.member, 'owner') || caller.boundDocument !== undefined || caller.confirmed) throw new RequestError(403, 'source-file-forbidden')
|
|
51
|
+
if (caller.origin === 'agent' && config.settings['presmium.assistant']?.review === 'required') throw new RequestError(409, 'authoring-review-required')
|
|
52
|
+
}
|
|
53
|
+
const repair = (path: string, before: Uint8Array | undefined) => {
|
|
54
|
+
if (!canonical(path)) return
|
|
55
|
+
if (!before || !listDocumentEntries(config).some(entry => entry.entry === path)) throw new RequestError(409, 'source-canonical-edit-required')
|
|
56
|
+
try { validateDocumentSource(path, Buffer.from(before).toString('utf8')) }
|
|
57
|
+
catch (error) { if ((error as Error).message === 'document-invalid') return; throw error }
|
|
58
|
+
throw new RequestError(409, 'source-canonical-edit-required')
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
list: (query, caller) => bounded(async () => {
|
|
62
|
+
request(query)
|
|
63
|
+
if (!validateSourceList(query)) throw new RequestError(400, 'source-request-invalid')
|
|
64
|
+
const owned = scope(query.document, caller), listed = editor.files(owned, caller.member)
|
|
65
|
+
const files = listed.files.filter(file => !query.query || file.path.toLowerCase().includes(query.query.toLowerCase()))
|
|
66
|
+
const snapshot = digest(JSON.stringify([owned, query.query ?? '', files, listed.truncated ?? false])), offset = query.offset ?? 0
|
|
67
|
+
if (offset > 0 && !query.snapshot || query.snapshot !== undefined && query.snapshot !== snapshot) throw new RequestError(409, 'source-list-changed')
|
|
68
|
+
if (offset > files.length) throw new RequestError(400, 'source-request-invalid')
|
|
69
|
+
const answer = { files: [] as typeof files, snapshot, ...listed.truncated ? { truncated: true as const } : {} }
|
|
70
|
+
for (const file of files.slice(offset, offset + 20)) {
|
|
71
|
+
if (!fits({ ...answer, files: [...answer.files, file], nextOffset: offset + answer.files.length + 1 })) break
|
|
72
|
+
answer.files.push(file)
|
|
73
|
+
}
|
|
74
|
+
if (!answer.files.length && offset < files.length) throw new RequestError(413, 'source-response-too-large')
|
|
75
|
+
return { ...answer, ...offset + answer.files.length < files.length ? { nextOffset: offset + answer.files.length } : {} }
|
|
76
|
+
}),
|
|
77
|
+
read: (query, caller) => bounded(async () => {
|
|
78
|
+
request(query)
|
|
79
|
+
if (!validateSourceRead(query)) throw new RequestError(400, 'source-request-invalid')
|
|
80
|
+
const value = editor.read(scope(query.document, caller), query.path, caller.member), offset = query.offset ?? 0
|
|
81
|
+
if (offset > 0 && !query.version || query.version !== undefined && query.version !== value.version) throw new RequestError(409, 'source-version-changed')
|
|
82
|
+
if (offset > value.content.length) throw new RequestError(400, 'source-request-invalid')
|
|
83
|
+
const answer = { path: value.path, version: value.version, text: value.content.slice(offset, offset + SOURCE_FRAGMENT_CHARS), offset, totalChars: value.content.length, nextOffset: 0 }
|
|
84
|
+
const cursor = () => answer.nextOffset = offset + answer.text.length
|
|
85
|
+
cursor()
|
|
86
|
+
while (!fits(answer) && answer.text.length) { answer.text = answer.text.slice(0, Math.floor(answer.text.length / 2)); cursor() }
|
|
87
|
+
if (!fits(answer) || !answer.text.length && offset < value.content.length) throw new RequestError(413, 'source-response-too-large')
|
|
88
|
+
const { nextOffset, ...part } = answer
|
|
89
|
+
return { ...part, ...nextOffset < value.content.length ? { nextOffset } : {} }
|
|
90
|
+
}),
|
|
91
|
+
prepare: (edit, caller) => bounded(async () => {
|
|
92
|
+
request(edit); writer(caller)
|
|
93
|
+
if (!validateSourceEdit(edit)) throw new RequestError(400, 'source-request-invalid')
|
|
94
|
+
const document = edit.document, owned = scope(document, caller), edits = 'edits' in edit ? edit.edits : [edit]
|
|
95
|
+
const captured = edits.map(file => {
|
|
96
|
+
const creating = 'create' in file && file.create === true
|
|
97
|
+
if (creating && (canonical(file.path) || file.version !== undefined)) throw new RequestError(409, 'source-canonical-edit-required')
|
|
98
|
+
const before = creating ? undefined : editor.read(owned, file.path, caller.member)
|
|
99
|
+
if (file.version !== undefined && before?.version !== file.version) throw new RequestError(409, 'source-version-changed')
|
|
100
|
+
repair(file.path, before && Buffer.from(before.content))
|
|
101
|
+
let content: string
|
|
102
|
+
if ('content' in file) content = file.content
|
|
103
|
+
else {
|
|
104
|
+
const first = before!.content.indexOf(file.find)
|
|
105
|
+
if (first < 0 || before!.content.indexOf(file.find, first + 1) >= 0) throw new RequestError(409, 'source-find-not-unique')
|
|
106
|
+
content = before!.content.slice(0, first) + file.replace + before!.content.slice(first + file.find.length)
|
|
107
|
+
}
|
|
108
|
+
return { path: file.path, content, base: before?.version ?? null, changed: creating || before!.content !== content }
|
|
109
|
+
})
|
|
110
|
+
const changes = captured.map(({ changed: _changed, ...file }) => file)
|
|
111
|
+
await editor.planTransaction(owned, changes, caller.member)
|
|
112
|
+
let work: Promise<SourceSaved> | undefined
|
|
113
|
+
return { change: (signal?: AbortSignal) => work ??= bounded(async () => {
|
|
114
|
+
const result = await editor.transaction(owned, changes, caller.member, {
|
|
115
|
+
check: files => {
|
|
116
|
+
signal?.throwIfAborted(); writer(caller)
|
|
117
|
+
if (JSON.stringify(scope(document, caller)) !== JSON.stringify(owned)) throw new RequestError(409, 'source-scope-changed')
|
|
118
|
+
for (const file of files) repair(file.path, file.before)
|
|
119
|
+
},
|
|
120
|
+
run: task => revisions ? revisions.transaction(task) : task(),
|
|
121
|
+
commit: async (paths, lang) => {
|
|
122
|
+
if (!revisions) return
|
|
123
|
+
if ((await Promise.all(paths.map(path => revisions.ignores(path)))).some(Boolean)) throw new RequestError(409, 'source-ignored-file')
|
|
124
|
+
return revisions.commitWhileLocked({ paths, summary: saveSummary(paths[0]!, lang), body: '', origin: { kind: caller.origin === 'agent' ? 'agent' : 'editor', member: caller.member } })
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
let current = false
|
|
128
|
+
try { current = result.versions.every(value => editor.read(owned, value.path, caller.member).version === value.version) } catch { /* A successful write is not safely retryable when readback changes or fails. */ }
|
|
129
|
+
return { saved: true as const, changed: captured.some(file => file.changed), ...captured.length === 1 ? { path: captured[0]!.path } : { files: captured.length }, ...result.revision ? { revision: result.revision.id } : {}, ...!current ? { current: false as const } : {} }
|
|
130
|
+
}) }
|
|
131
|
+
}),
|
|
132
|
+
}
|
|
133
|
+
}
|
package/src/node/writer.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { closeSync, constants, cpSync, fstatSync, lstatSync, mkdirSync, mkdtempS
|
|
|
3
3
|
import type { Stats } from 'node:fs'
|
|
4
4
|
import { tmpdir } from 'node:os'
|
|
5
5
|
import { dirname, join } from 'node:path'
|
|
6
|
-
import { DOCUMENT_ENTRY, validateDocument } from '@lincyaw/kernel'
|
|
6
|
+
import { CHANGES_MAX_BYTES, CHANGES_MAX_FILES, DOCUMENT_ENTRY, validateDocument } from '@lincyaw/kernel'
|
|
7
7
|
import type { DocumentFileChange, DocumentFiles } from '@lincyaw/kernel'
|
|
8
8
|
import { isWithin, rewriteLinks } from '../domain/paths.ts'
|
|
9
9
|
import { isDocumentDir, listDocuments } from './config.ts'
|
|
@@ -11,8 +11,8 @@ import type { WorkspaceConfig } from './config.ts'
|
|
|
11
11
|
import { findTemplate } from './templates.ts'
|
|
12
12
|
|
|
13
13
|
const NOT_COPIED = new Set(['node_modules', 'dist', '.git'])
|
|
14
|
-
export const DOCUMENT_OPERATION_MAX_BACKUP_ENTRIES =
|
|
15
|
-
export const DOCUMENT_OPERATION_MAX_BACKUP_BYTES =
|
|
14
|
+
export const DOCUMENT_OPERATION_MAX_BACKUP_ENTRIES = CHANGES_MAX_FILES
|
|
15
|
+
export const DOCUMENT_OPERATION_MAX_BACKUP_BYTES = CHANGES_MAX_BYTES
|
|
16
16
|
|
|
17
17
|
export class FilesError extends Error {}
|
|
18
18
|
export class DocumentCheckpointRefusal extends FilesError {
|