@yuridulin/pointer 0.0.0-stage → 0.1.0
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 +54 -2
- package/package.json +51 -4
- package/server/acp/catalog.ts +111 -0
- package/server/acp/connection.ts +233 -0
- package/server/acp/fake-agent.ts +408 -0
- package/server/acp/protocol.ts +167 -0
- package/server/acp/run.ts +139 -0
- package/server/acp/session.ts +1249 -0
- package/server/attachments.ts +115 -0
- package/server/changes.ts +239 -0
- package/server/cli.ts +362 -0
- package/server/compact.ts +361 -0
- package/server/config.ts +176 -0
- package/server/db.ts +462 -0
- package/server/dev.ts +8 -0
- package/server/diff.ts +190 -0
- package/server/engines.ts +63 -0
- package/server/files.ts +254 -0
- package/server/git.ts +319 -0
- package/server/history.ts +425 -0
- package/server/http/attachments.ts +63 -0
- package/server/http/browse.ts +108 -0
- package/server/http/chats.ts +627 -0
- package/server/http/closing.ts +14 -0
- package/server/http/files.ts +115 -0
- package/server/http/git.ts +84 -0
- package/server/http/preview.ts +21 -0
- package/server/http/providers.ts +200 -0
- package/server/http/reply.ts +38 -0
- package/server/hub.ts +521 -0
- package/server/install.ts +169 -0
- package/server/jobs.ts +664 -0
- package/server/linkPreview.ts +172 -0
- package/server/loop.ts +328 -0
- package/server/main.ts +353 -0
- package/server/probe.ts +34 -0
- package/server/process.ts +333 -0
- package/server/prompt.ts +196 -0
- package/server/providers/fake.ts +100 -0
- package/server/providers/opencode.ts +468 -0
- package/server/providers/registry.ts +149 -0
- package/server/providers/store.ts +210 -0
- package/server/segments.ts +248 -0
- package/server/sse.ts +93 -0
- package/server/tools/bash.ts +222 -0
- package/server/tools/edit.ts +254 -0
- package/server/tools/glob.ts +63 -0
- package/server/tools/grep.ts +122 -0
- package/server/tools/ignore.ts +54 -0
- package/server/tools/index.ts +129 -0
- package/server/tools/jail.ts +85 -0
- package/server/tools/jobs.ts +293 -0
- package/server/tools/read.ts +248 -0
- package/server/tools/write.ts +70 -0
- package/server/tray.ps1 +191 -0
- package/server/turn.ts +663 -0
- package/server/watch.ts +94 -0
- package/shared/acpConfig.ts +113 -0
- package/shared/links.ts +122 -0
- package/shared/types.ts +392 -0
- package/web/dist/app.css +1 -0
- package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
- package/web/dist/index.html +28 -0
- package/web/dist/main.css +1 -0
- package/web/dist/main.js +1366 -0
- package/web/dist/tokens.css +1 -0
- package/web/dist/workers/css.worker.js +97 -0
- package/web/dist/workers/editor.worker.js +30 -0
- package/web/dist/workers/html.worker.js +300 -0
- package/web/dist/workers/json.worker.js +62 -0
- package/web/dist/workers/ts.worker.js +67733 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, statSync } from 'node:fs'
|
|
2
|
+
import { basename, extname, join, resolve } from 'node:path'
|
|
3
|
+
import { IMAGE_EXTENSIONS, imageMime, isImageFile } from '../shared/links'
|
|
4
|
+
import { writeAtomic } from './changes'
|
|
5
|
+
import { pointerHome } from './config'
|
|
6
|
+
import { FileError } from './files'
|
|
7
|
+
import { resolveInRoot } from './tools/jail'
|
|
8
|
+
|
|
9
|
+
export const ATTACHMENT_LIMIT = 10 * 1024 * 1024
|
|
10
|
+
|
|
11
|
+
const MIME_EXTENSION: Record<string, string> = {
|
|
12
|
+
'image/png': '.png',
|
|
13
|
+
'image/jpeg': '.jpg',
|
|
14
|
+
'image/gif': '.gif',
|
|
15
|
+
'image/webp': '.webp',
|
|
16
|
+
'image/bmp': '.bmp',
|
|
17
|
+
'image/x-icon': '.ico',
|
|
18
|
+
'image/vnd.microsoft.icon': '.ico',
|
|
19
|
+
'image/svg+xml': '.svg',
|
|
20
|
+
'image/avif': '.avif',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type SavedAttachment = { path: string; name: string; size: number; mime: string }
|
|
24
|
+
|
|
25
|
+
export function attachmentsDir(base = pointerHome()): string {
|
|
26
|
+
const dir = join(base, 'attachments')
|
|
27
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
28
|
+
return dir
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function safeName(raw: string): string {
|
|
32
|
+
const name = basename(raw.replaceAll('\\', '/'))
|
|
33
|
+
.replace(/[<>:"/\\|?*\u0000-\u001f]/g, '-')
|
|
34
|
+
.replace(/[. ]+$/, '')
|
|
35
|
+
.trim()
|
|
36
|
+
return name === '' || name === '.' || name === '..' ? 'image' : name
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function imageExtension(name: string, mime: string): string {
|
|
40
|
+
const extension = extname(name).toLowerCase()
|
|
41
|
+
if (IMAGE_EXTENSIONS[extension.slice(1)] === true) return extension
|
|
42
|
+
if (extension === '' && MIME_EXTENSION[mime] !== undefined) return MIME_EXTENSION[mime]!
|
|
43
|
+
throw new FileError(400, 'Поддерживаются только изображения')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function uniquePath(dir: string, name: string, extension: string): { abs: string; name: string } {
|
|
47
|
+
const stem = extension !== '' && name.toLowerCase().endsWith(extension) ? name.slice(0, -extension.length) : name
|
|
48
|
+
for (let index = 0; ; index += 1) {
|
|
49
|
+
const candidate = index === 0 ? `${stem}${extension}` : `${stem}-${index}${extension}`
|
|
50
|
+
const abs = resolve(dir, candidate)
|
|
51
|
+
if (!existsSync(abs)) return { abs, name: candidate }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function saveImageAttachments(
|
|
56
|
+
files: File[],
|
|
57
|
+
dir = attachmentsDir(),
|
|
58
|
+
): Promise<SavedAttachment[]> {
|
|
59
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
60
|
+
const saved: SavedAttachment[] = []
|
|
61
|
+
for (const file of files) {
|
|
62
|
+
const name = safeName(file.name)
|
|
63
|
+
const extension = imageExtension(name, file.type)
|
|
64
|
+
if (file.size > ATTACHMENT_LIMIT) {
|
|
65
|
+
throw new FileError(400, 'Изображение больше 10 МБ')
|
|
66
|
+
}
|
|
67
|
+
const target = uniquePath(dir, name, extension)
|
|
68
|
+
const bytes = new Uint8Array(await file.arrayBuffer())
|
|
69
|
+
try {
|
|
70
|
+
writeAtomic(target.abs, bytes)
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw new FileError(400, error instanceof Error ? error.message : String(error))
|
|
73
|
+
}
|
|
74
|
+
saved.push({
|
|
75
|
+
path: target.abs,
|
|
76
|
+
name: target.name,
|
|
77
|
+
size: file.size,
|
|
78
|
+
mime: imageMime(target.name) ?? file.type,
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
return saved
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function readImageFile(
|
|
85
|
+
input: string,
|
|
86
|
+
workspaceRoot: string | null,
|
|
87
|
+
): { abs: string; mime: string } {
|
|
88
|
+
const path = input.trim()
|
|
89
|
+
if (path === '') throw new FileError(400, 'Нужен путь к изображению')
|
|
90
|
+
if (/^file:/i.test(path)) throw new FileError(400, 'Ожидается путь к изображению')
|
|
91
|
+
let abs: string
|
|
92
|
+
if (path.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(path) || path.startsWith('\\\\')) {
|
|
93
|
+
abs = resolve(path)
|
|
94
|
+
} else {
|
|
95
|
+
if (workspaceRoot === null) throw new FileError(400, 'Нужен workspaceId')
|
|
96
|
+
try {
|
|
97
|
+
abs = resolveInRoot(workspaceRoot, path).abs
|
|
98
|
+
} catch (error) {
|
|
99
|
+
throw new FileError(400, error instanceof Error ? error.message : String(error))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
let stat
|
|
103
|
+
try {
|
|
104
|
+
stat = statSync(abs)
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
107
|
+
throw new FileError(404, 'Изображение не найдено')
|
|
108
|
+
}
|
|
109
|
+
throw new FileError(400, 'Изображение не читается')
|
|
110
|
+
}
|
|
111
|
+
if (!stat.isFile()) throw new FileError(404, 'Изображение не найдено')
|
|
112
|
+
if (!isImageFile(abs)) throw new FileError(400, 'Поддерживаются только изображения')
|
|
113
|
+
if (stat.size > ATTACHMENT_LIMIT) throw new FileError(400, 'Изображение больше 10 МБ')
|
|
114
|
+
return { abs, mime: imageMime(abs) ?? 'application/octet-stream' }
|
|
115
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import {
|
|
2
|
+
closeSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
openSync,
|
|
5
|
+
readSync,
|
|
6
|
+
renameSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs'
|
|
10
|
+
import { basename, dirname, join } from 'node:path'
|
|
11
|
+
import type { Database } from 'bun:sqlite'
|
|
12
|
+
import { resolveInRoot } from './tools/jail'
|
|
13
|
+
|
|
14
|
+
export const BLOB_LIMIT = 2 * 1024 * 1024
|
|
15
|
+
|
|
16
|
+
export type Revision = { path: string; before: Uint8Array | null; after: Uint8Array }
|
|
17
|
+
|
|
18
|
+
export type RevisionRow = {
|
|
19
|
+
id: string
|
|
20
|
+
chat_id: string
|
|
21
|
+
message_id: string | null
|
|
22
|
+
path: string
|
|
23
|
+
before_hash: string | null
|
|
24
|
+
after_hash: string
|
|
25
|
+
reviewed_at: number | null
|
|
26
|
+
created_at: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const revisionColumns = `id, chat_id, message_id, path, before_hash, after_hash, reviewed_at, created_at`
|
|
30
|
+
|
|
31
|
+
export function hashBytes(bytes: Uint8Array): string {
|
|
32
|
+
return new Bun.CryptoHasher('sha256').update(bytes).digest('hex')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function putBlob(db: Database, bytes: Uint8Array): string {
|
|
36
|
+
if (bytes.byteLength > BLOB_LIMIT) {
|
|
37
|
+
throw new Error('file exceeds the 2 MB blob limit; narrow the work to smaller files')
|
|
38
|
+
}
|
|
39
|
+
const hash = hashBytes(bytes)
|
|
40
|
+
db.run('INSERT OR IGNORE INTO blobs (hash, size, content) VALUES (?, ?, ?)', [
|
|
41
|
+
hash,
|
|
42
|
+
bytes.byteLength,
|
|
43
|
+
bytes,
|
|
44
|
+
])
|
|
45
|
+
return hash
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getBlob(db: Database, hash: string): Uint8Array | null {
|
|
49
|
+
const row = db
|
|
50
|
+
.query<{ content: Uint8Array }, [string]>('SELECT content FROM blobs WHERE hash = ?')
|
|
51
|
+
.get(hash)
|
|
52
|
+
return row?.content ?? null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function writeAtomic(abs: string, bytes: Uint8Array): void {
|
|
56
|
+
const dir = dirname(abs)
|
|
57
|
+
mkdirSync(dir, { recursive: true })
|
|
58
|
+
const temp = join(dir, `.${basename(abs)}.${crypto.randomUUID()}.tmp`)
|
|
59
|
+
try {
|
|
60
|
+
writeFileSync(temp, bytes)
|
|
61
|
+
renameSync(temp, abs)
|
|
62
|
+
} catch (error) {
|
|
63
|
+
rmSync(temp, { force: true })
|
|
64
|
+
throw error
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function recordRevisions(
|
|
69
|
+
db: Database,
|
|
70
|
+
chatId: string,
|
|
71
|
+
messageId: string | null,
|
|
72
|
+
revisions: Revision[],
|
|
73
|
+
): RevisionRow[] {
|
|
74
|
+
return db.transaction((): RevisionRow[] => {
|
|
75
|
+
const rows: RevisionRow[] = []
|
|
76
|
+
for (const revision of revisions) {
|
|
77
|
+
const beforeHash = revision.before === null ? null : putBlob(db, revision.before)
|
|
78
|
+
const afterHash = putBlob(db, revision.after)
|
|
79
|
+
const row: RevisionRow = {
|
|
80
|
+
id: crypto.randomUUID(),
|
|
81
|
+
chat_id: chatId,
|
|
82
|
+
message_id: messageId,
|
|
83
|
+
path: revision.path,
|
|
84
|
+
before_hash: beforeHash,
|
|
85
|
+
after_hash: afterHash,
|
|
86
|
+
reviewed_at: null,
|
|
87
|
+
created_at: Date.now(),
|
|
88
|
+
}
|
|
89
|
+
db.run(
|
|
90
|
+
`INSERT INTO file_revisions (id, chat_id, message_id, path, before_hash, after_hash, reviewed_at, created_at)
|
|
91
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
92
|
+
[
|
|
93
|
+
row.id,
|
|
94
|
+
row.chat_id,
|
|
95
|
+
row.message_id,
|
|
96
|
+
row.path,
|
|
97
|
+
row.before_hash,
|
|
98
|
+
row.after_hash,
|
|
99
|
+
row.reviewed_at,
|
|
100
|
+
row.created_at,
|
|
101
|
+
],
|
|
102
|
+
)
|
|
103
|
+
rows.push(row)
|
|
104
|
+
}
|
|
105
|
+
return rows
|
|
106
|
+
})()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function listRevisions(db: Database, chatId: string): RevisionRow[] {
|
|
110
|
+
return db
|
|
111
|
+
.query<RevisionRow, [string]>(
|
|
112
|
+
`SELECT ${revisionColumns} FROM file_revisions WHERE chat_id = ? ORDER BY rowid`,
|
|
113
|
+
)
|
|
114
|
+
.all(chatId)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Revisions written by the given messages, oldest first. */
|
|
118
|
+
export function revisionsForMessages(
|
|
119
|
+
db: Database,
|
|
120
|
+
chatId: string,
|
|
121
|
+
messageIds: string[],
|
|
122
|
+
): RevisionRow[] {
|
|
123
|
+
const wanted = new Set(messageIds)
|
|
124
|
+
if (wanted.size === 0) return []
|
|
125
|
+
return listRevisions(db, chatId).filter(
|
|
126
|
+
(row) => row.message_id !== null && wanted.has(row.message_id),
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The latest revision of one path in the chat, or null when the path is untouched. */
|
|
131
|
+
export function lastRevision(db: Database, chatId: string, path: string): RevisionRow | null {
|
|
132
|
+
return (
|
|
133
|
+
db
|
|
134
|
+
.query<RevisionRow, [string, string]>(
|
|
135
|
+
`SELECT ${revisionColumns} FROM file_revisions
|
|
136
|
+
WHERE chat_id = ? AND path = ? ORDER BY rowid DESC LIMIT 1`,
|
|
137
|
+
)
|
|
138
|
+
.get(chatId, path) ?? null
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Одна ревизия чата; id из другого чата не находится. */
|
|
143
|
+
export function getRevision(db: Database, chatId: string, revisionId: string): RevisionRow | null {
|
|
144
|
+
return (
|
|
145
|
+
db
|
|
146
|
+
.query<RevisionRow, [string, string]>(
|
|
147
|
+
`SELECT ${revisionColumns} FROM file_revisions WHERE id = ? AND chat_id = ?`,
|
|
148
|
+
)
|
|
149
|
+
.get(revisionId, chatId) ?? null
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function markReviewed(db: Database, chatId: string, path: string, now = Date.now()): void {
|
|
154
|
+
db.run('UPDATE file_revisions SET reviewed_at = ? WHERE chat_id = ? AND path = ?', [
|
|
155
|
+
now,
|
|
156
|
+
chatId,
|
|
157
|
+
path,
|
|
158
|
+
])
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function revertRevision(
|
|
162
|
+
db: Database,
|
|
163
|
+
root: string,
|
|
164
|
+
chatId: string,
|
|
165
|
+
revisionId: string,
|
|
166
|
+
options?: { force?: boolean },
|
|
167
|
+
):
|
|
168
|
+
| { status: 'reverted'; path: string; removed: number }
|
|
169
|
+
| {
|
|
170
|
+
status: 'conflict'
|
|
171
|
+
path: string
|
|
172
|
+
beforeHash: string | null
|
|
173
|
+
agentHash: string
|
|
174
|
+
diskHash: string | null
|
|
175
|
+
}
|
|
176
|
+
| { status: 'not_found' } {
|
|
177
|
+
const revision = db
|
|
178
|
+
.query<RevisionRow & { rowid: number }, [string, string]>(
|
|
179
|
+
`SELECT rowid, ${revisionColumns} FROM file_revisions WHERE id = ? AND chat_id = ?`,
|
|
180
|
+
)
|
|
181
|
+
.get(revisionId, chatId)
|
|
182
|
+
if (!revision) return { status: 'not_found' }
|
|
183
|
+
|
|
184
|
+
const last = lastRevision(db, chatId, revision.path)
|
|
185
|
+
const agentHash = last?.after_hash ?? revision.after_hash
|
|
186
|
+
const abs = resolveInRoot(root, revision.path).abs
|
|
187
|
+
const diskHash = hashOnDisk(abs)
|
|
188
|
+
|
|
189
|
+
if (!options?.force && diskHash !== agentHash) {
|
|
190
|
+
return {
|
|
191
|
+
status: 'conflict',
|
|
192
|
+
path: revision.path,
|
|
193
|
+
beforeHash: revision.before_hash,
|
|
194
|
+
agentHash,
|
|
195
|
+
diskHash,
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (revision.before_hash === null) {
|
|
200
|
+
rmSync(abs, { force: true })
|
|
201
|
+
} else {
|
|
202
|
+
const bytes = getBlob(db, revision.before_hash)
|
|
203
|
+
if (!bytes) throw new Error(`blob ${revision.before_hash} is missing`)
|
|
204
|
+
writeAtomic(abs, bytes)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const removed = db.transaction((): number => {
|
|
208
|
+
return db.run('DELETE FROM file_revisions WHERE chat_id = ? AND path = ? AND rowid >= ?', [
|
|
209
|
+
chatId,
|
|
210
|
+
revision.path,
|
|
211
|
+
revision.rowid,
|
|
212
|
+
]).changes
|
|
213
|
+
})()
|
|
214
|
+
|
|
215
|
+
return { status: 'reverted', path: revision.path, removed }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Hashes in fixed-size chunks so a large file never has to fit in memory at once. */
|
|
219
|
+
export function hashOnDisk(abs: string): string | null {
|
|
220
|
+
let fd: number
|
|
221
|
+
try {
|
|
222
|
+
fd = openSync(abs, 'r')
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null
|
|
225
|
+
throw error
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const hasher = new Bun.CryptoHasher('sha256')
|
|
229
|
+
const buffer = Buffer.allocUnsafe(64 * 1024)
|
|
230
|
+
for (;;) {
|
|
231
|
+
const read = readSync(fd, buffer, 0, buffer.length, null)
|
|
232
|
+
if (read === 0) break
|
|
233
|
+
hasher.update(buffer.subarray(0, read))
|
|
234
|
+
}
|
|
235
|
+
return hasher.digest('hex')
|
|
236
|
+
} finally {
|
|
237
|
+
closeSync(fd)
|
|
238
|
+
}
|
|
239
|
+
}
|