Files
Agent-Workbench/src/server/files.mjs
T

160 lines
9.6 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { createHash, randomUUID } from 'node:crypto';
import { diffLines } from 'diff';
import { now } from './store.mjs';
export const hash = bytes => createHash('sha256').update(bytes).digest('hex');
const ignored = new Set(['node_modules', '.git', '.next', '.data', 'bin', 'obj', 'dist', 'dist-client', 'dist-server', '__pycache__']);
const secretName = /(^|\/)(\.env($|\.)|id_rsa($|\.)|id_ed25519($|\.)|credentials($|\.)|\.ssh($|\/))/i;
const normalize = value => process.platform === 'win32' ? value.toLowerCase() : value;
const inside = (root, target) => normalize(target) === normalize(root) || normalize(target).startsWith(normalize(root + path.sep));
export function safePath(project, relative = '', allowMissing = false) {
if (typeof relative !== 'string' || relative.includes('\0') || relative.includes(':') || path.isAbsolute(relative) || path.win32.isAbsolute(relative)) throw new Error('路径必须相对于项目文件夹。');
const portable = relative.replaceAll('\\', '/');
if (portable.split('/').includes('..') || secretName.test(portable) || portable.split('/').some(part => part.toLowerCase() === '.git')) throw new Error('该路径不在可读取的项目范围内。');
const root = fs.realpathSync.native(project.root);
if (normalize(root) !== normalize(project.root)) throw new Error('项目位置已变化,请重新打开项目。');
const target = path.resolve(root, portable || '.');
if (!inside(root, target)) throw new Error('路径超出项目范围。');
let existing = target;
while (!fs.existsSync(existing)) {
try { if (fs.lstatSync(existing).isSymbolicLink()) throw new Error('目标经过一个失效的链接,请先核对项目路径。'); }
catch (error) { if (error.code !== 'ENOENT') throw error; }
if (!allowMissing) throw new Error('文件或文件夹不存在。');
const parent = path.dirname(existing);
if (parent === existing) throw new Error('无法确定目标文件夹。');
existing = parent;
}
const real = fs.realpathSync.native(existing);
if (!inside(root, real)) throw new Error('链接指向项目以外的位置。');
return target;
}
export function listFiles(project, relative = '', depth = 4, limit = 700) {
const start = safePath(project, relative);
if (!fs.statSync(start).isDirectory()) throw new Error('请选择文件夹。');
const files = [];
const queue = [{ absolute: start, depth: 0 }];
let truncated = false;
while (queue.length) {
const current = queue.shift();
const entries = fs.readdirSync(current.absolute, { withFileTypes: true })
.sort((a, b) => Number(b.isDirectory()) - Number(a.isDirectory()) || a.name.localeCompare(b.name));
for (const entry of entries) {
if (ignored.has(entry.name) || entry.name.startsWith('.awb-') || secretName.test(entry.name)) continue;
const absolute = path.join(current.absolute, entry.name);
const relativePath = path.relative(project.root, absolute).replaceAll('\\', '/');
if (entry.isSymbolicLink()) continue;
const directory = entry.isDirectory();
files.push({ path: relativePath, name: entry.name, kind: directory ? 'directory' : 'file',
bytes: directory ? 0 : fs.statSync(absolute).size });
if (files.length >= limit) { truncated = true; break; }
if (directory && current.depth < Math.min(12, Math.max(0, depth))) queue.push({ absolute, depth: current.depth + 1 });
}
if (truncated) break;
}
return { files, truncated, scope: '按深度和数量上限读取,跳过依赖、版本目录、链接与常见凭据文件。' };
}
export function readText(project, relative) {
const target = safePath(project, relative);
if (!fs.statSync(target).isFile()) throw new Error('这不是常规文件。');
if (fs.statSync(target).size > 1024 * 1024) throw new Error('文件超过 1 MB 的读取上限。');
const bytes = fs.readFileSync(target);
if (bytes.includes(0)) throw new Error('这是二进制文件,无法作为文本打开。');
const text = new TextDecoder('utf8', { fatal: true }).decode(bytes);
return { path: relative, content: text, sha256: hash(bytes), bytes: bytes.length };
}
export function readLines(project, args) {
const file = readText(project, args.path);
const lines = file.content.split('\n');
const start = Math.max(1, Number(args.start_line) || 1);
const end = Math.min(lines.length, start + 299, Math.max(start, Number(args.end_line) || start + 199));
return { path: file.path, start_line: start, end_line: end, total_lines: lines.length,
content: lines.slice(start - 1, end).join('\n'), sha256: file.sha256, truncated: start > 1 || end < lines.length };
}
export function searchFiles(project, args) {
const query = String(args.query ?? '').slice(0, 200);
if (!query.trim()) throw new Error('请输入搜索内容。');
const inventory = listFiles(project, '', 10, 2500);
const matches = [];
let scanned = 0, budget = 0;
for (const file of inventory.files) {
if (file.kind !== 'file' || file.bytes > 256000) continue;
if (scanned >= 400 || budget + file.bytes > 8 * 1024 * 1024) { inventory.truncated = true; break; }
try {
const content = readText(project, file.path).content;
scanned++; budget += file.bytes;
const lines = content.split('\n');
for (let index = 0; index < lines.length; index++) {
if ((args.case_sensitive ? lines[index] : lines[index].toLocaleLowerCase()).includes(args.case_sensitive ? query : query.toLocaleLowerCase())) {
matches.push({ path: file.path, line: index + 1, text: lines[index].slice(0, 500) });
if (matches.length >= 80) break;
}
}
} catch { continue; }
if (matches.length >= 80) break;
}
return { query, matches, scanned_files: scanned, truncated: inventory.truncated || scanned >= 400 || budget >= 8 * 1024 * 1024 || matches.length >= 80,
scope: '有数量/体积上限的文本搜索;无结果不表示所有文件都已搜索。' };
}
export function proposeFile(store, session, args) {
if (typeof args.path !== 'string' || typeof args.content !== 'string') throw new Error('文件提案需要文本路径和完整文本内容。');
const project = store.project(session.projectId);
const target = safePath(project, args.path, true);
let original = null;
if (fs.existsSync(target)) original = readText(project, args.path);
if (Object.hasOwn(args, 'base_sha256') && args.base_sha256 !== (original?.sha256 ?? null)) throw Object.assign(new Error('文件已经在读取后发生变化,请重新读取并生成提案。'), { status: 409 });
const content = String(args.content ?? '');
if (Buffer.byteLength(content, 'utf8') > 512000) throw new Error('单次提案不能超过 512 KB。');
if (content.includes('\0')) throw new Error('只能提出文本改动。');
if (original?.content === content) return { status: 'unchanged', path: args.path };
const parts = diffLines(original?.content ?? '', content);
const change = {
taskId: session.taskId ?? null, runId: session.runId ?? null,
id: randomUUID(), path: args.path, reason: String(args.reason ?? '建议修改').slice(0, 600), status: 'pending',
baseExists: !!original, baseSha256: original?.sha256 ?? null, baseContent: original?.content ?? '',
content, sha256: hash(Buffer.from(content, 'utf8')), diff: parts, createdAt: now(),
additions: parts.filter(part => part.added).reduce((sum, part) => sum + (part.count ?? 0), 0),
deletions: parts.filter(part => part.removed).reduce((sum, part) => sum + (part.count ?? 0), 0),
};
session.changes.push(change);
if (!['running', 'awaiting_approval', 'stopping'].includes(session.status)) session.status = 'needs_review';
store.save(session);
return { status: 'awaiting_review', change_id: change.id, path: change.path,
additions: change.additions, deletions: change.deletions, message: '提案已保存,项目文件尚未改变。等待用户查看差异并应用。' };
}
export function applyChange(store, session, id) {
const change = session.changes.find(item => item.id === id);
if (!change || change.status !== 'pending') throw new Error('这项改动不处于待审查状态。');
const project = store.project(session.projectId);
const target = safePath(project, change.path, true);
const exists = fs.existsSync(target);
const actualHash = exists ? readText(project, change.path).sha256 : null;
if (exists !== change.baseExists || actualHash !== change.baseSha256) {
change.status = 'conflict'; store.save(session);
throw Object.assign(new Error('文件已经在其他地方修改。请重新生成提案并核对差异。'), { status: 409 });
}
const backupRoot = path.join(store.directory, 'backups', session.id);
fs.mkdirSync(backupRoot, { recursive: true });
if (exists) fs.copyFileSync(target, path.join(backupRoot, change.id + '.txt'));
fs.mkdirSync(path.dirname(target), { recursive: true });
safePath(project, change.path, true);
const temporary = path.join(path.dirname(target), '.awb-' + change.id + '.tmp');
try {
fs.writeFileSync(temporary, change.content, { encoding: 'utf8', flag: 'wx' });
fs.renameSync(temporary, target);
} finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
change.status = 'applied'; change.appliedAt = now();
store.invalidateChecks(project.id);
if (session.status === 'needs_review' && !session.changes.some(item => ['pending', 'conflict'].includes(item.status))) session.status = 'completed';
store.event(session, { kind: 'change', label: '已应用 ' + change.path, phase: 'completed', output: exists ? '写入前基线检查通过,原文已备份。' : '写入前基线检查通过,已创建新文件。' });
return change;
}