Files
Agent-Workbench/src/core/project-inspector.mjs
T

135 lines
11 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { listFiles, safePath, readText, excludedFromInventory } from '../server/files.mjs';
import { runGit } from './git-process.mjs';
import { inspectPythonProject } from './python-project.mjs';
const hash = value => createHash('sha256').update(value).digest('hex');
const languages = { '.js': 'JavaScript', '.mjs': 'JavaScript', '.cjs': 'JavaScript', '.ts': 'TypeScript', '.tsx': 'TypeScript', '.mts': 'TypeScript', '.cts': 'TypeScript', '.jsx': 'JavaScript', '.py': 'Python', '.go': 'Go', '.rs': 'Rust', '.cs': 'C#', '.vue': 'Vue', '.html': 'HTML', '.css': 'CSS' };
const recognizedScripts = /^(test(?::[a-z0-9_-]+)?|typecheck|check|lint|build)$/i;
async function gitCommand(root, args) {
try {
const result = await runGit(root, args, { timeoutMs: 5000, maximumBytes: 512000 });
return { ok: result.ok, output: result.text, error: result.ok ? null : result.code };
} catch (error) { return { ok: false, output: '', error: error.code ?? 'git-unavailable' }; }
}
export async function gitBaseline(project) {
const root = safePath(project);
const probe = await gitCommand(root, ['rev-parse', '--is-inside-work-tree']);
if (!probe.ok || probe.output.trim() !== 'true') return { available: false, head: null, branch: null, dirty: false, status: '', complete: true, paths: null };
const top = await gitCommand(root, ['rev-parse', '--show-toplevel']);
const normalize = value => process.platform === 'win32' ? path.resolve(value).toLowerCase() : path.resolve(value);
if (!top.ok || normalize(top.output.trim()) !== normalize(root)) return { available: false, parentRepository: true,
note: '上级目录的Git仓库不作为当前项目独立基线。', head: null, branch: null, dirty: false, status: '', complete: true, paths: null };
// Even status may run a repository's clean/process filter. Read only the
// configuration key names, then disable those commands for this inspection.
const filterConfig = await gitCommand(root, ['config', '--null', '--name-only', '--get-regexp', '^filter\\..*\\.(clean|process|required)$']);
const filtersKnown = filterConfig.ok || filterConfig.error === 1;
const drivers = [...new Set(filterConfig.output.split('\0').filter(key => /^filter\..+\.(clean|process|required)$/i.test(key)).map(key => key.replace(/\.(clean|process|required)$/i, '')))];
const filterOverrides = drivers.flatMap(key => ['-c', key + '.clean=', '-c', key + '.process=', '-c', key + '.required=false']);
const [status, head, branch, files] = await Promise.all([
filtersKnown ? gitCommand(root, [...filterOverrides, 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--ignore-submodules=all', '--', '.'])
: Promise.resolve({ ok: false, output: '', error: 'filter-config-unavailable' }),
gitCommand(root, ['rev-parse', '--verify', 'HEAD']),
gitCommand(root, ['symbolic-ref', '--quiet', '--short', 'HEAD']),
gitCommand(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', '.']),
]);
return { available: true, head: head.ok ? head.output.trim() : null, branch: branch.ok ? branch.output.trim() : null,
dirty: !!status.output, status: status.output.replaceAll('\0', '\n').slice(0, 8000),
statusSha256: hash(status.output), complete: status.ok && files.ok,
paths: files.ok ? [...new Set(files.output.split('\0').filter(Boolean))] : null,
contentFilters: filtersKnown ? drivers.length ? 'disabled-for-inspection' : 'none' : 'unverified', submoduleWorktrees: 'not-inspected' };
}
function declaration(project, relative, maximum = 64000) {
const file = safePath(project, relative, true);
if (!fs.existsSync(file)) return null;
if (!fs.statSync(file).isFile() || fs.statSync(file).size > maximum) throw new Error(relative + ' 超过工程声明的读取上限。');
return readText(project, relative);
}
function framework(script) {
if (/^\s*(?:node|node\.exe)\s+(?:--[^\s]+\s+)*--test(?:\s|$)/.test(script)) return 'node';
if (/^\s*(?:npx\s+)?vitest(?:\s|$)/.test(script)) return 'vitest';
if (/^\s*(?:npx\s+)?jest(?:\s|$)/.test(script)) return 'jest';
return null;
}
export async function inspectProject(project, { includeFiles = false } = {}) {
const root = safePath(project), git = await gitBaseline(project);
const inventory = git.paths ? null : listFiles(project, '', 8, 1500);
const candidates = (git.paths ?? inventory.files.filter(file => file.kind === 'file').map(file => file.path)).slice().sort();
const fingerprints = [], names = [], excluded = [], detected = new Set(), sourceFiles = [];
let bytes = 0, complete = git.complete && !inventory?.truncated && candidates.length <= 1000;
for (const relative of candidates.slice(0, 1000)) {
if (excludedFromInventory(relative)) { excluded.push(relative); continue; }
try {
const file = safePath(project, relative, true);
if (!fs.existsSync(file)) { fingerprints.push([relative, 'missing']); sourceFiles.push({ path: relative, missing: true, sha256: null, bytes: 0 }); continue; }
const info = fs.lstatSync(file);
if (!info.isFile() || info.isSymbolicLink()) { excluded.push(relative); continue; }
names.push(relative);
const language = languages[path.extname(relative).toLowerCase()]; if (language) detected.add(language);
if (info.size > 1024 * 1024 || bytes + info.size > 16 * 1024 * 1024) { complete = false; fingerprints.push([relative, info.size, 'outside-read-budget']); continue; }
const contents = fs.readFileSync(file), sha256 = hash(contents); bytes += contents.length; fingerprints.push([relative, sha256]);
sourceFiles.push({ path: relative, sha256, bytes: contents.length, language: language ?? null });
} catch (error) {
excluded.push(relative);
if (error.message !== '该路径不在可读取的项目范围内。') { complete = false; fingerprints.push([relative, 'unreadable']); }
}
}
const packageFile = declaration(project, 'package.json');
let manifest = null;
if (packageFile) {
try { manifest = JSON.parse(packageFile.content); } catch { throw new Error('项目package.json不是有效JSON,先修复工程声明。'); }
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('项目package.json需要一个JSON对象。');
}
const fileExists = relative => fs.existsSync(safePath(project, relative, true));
const packageManager = (typeof manifest?.packageManager === 'string' ? manifest.packageManager.split('@')[0] : null) || (fileExists('pnpm-lock.yaml') ? 'pnpm' : fileExists('yarn.lock') ? 'yarn' : fileExists('bun.lock') || fileExists('bun.lockb') ? 'bun' : packageFile ? 'npm' : 'unknown');
const scripts = Object.entries(manifest?.scripts ?? {}).filter(([name, script]) => recognizedScripts.test(name) && typeof script === 'string' && script.length <= 6000)
.map(([name, script]) => ({ name, script, kind: /^test(?::|$)/i.test(name) ? 'test' : name === 'build' ? 'build' : 'check', framework: framework(script),
lifecycle: ['pre' + name, 'post' + name].filter(key => typeof manifest.scripts[key] === 'string').map(key => ({ name: key, script: manifest.scripts[key].slice(0, 6000) })) }));
const rules = [];
for (const relative of names.filter(name => /(^|\/)AGENTS\.md$/i.test(name)).slice(0, 8)) {
const file = declaration(project, relative); if (file) rules.push({ path: relative, sha256: file.sha256, excerpt: file.content.slice(0, 1800), interpretation: 'project-data' });
}
const declarations = {};
for (const relative of ['pyproject.toml', 'Cargo.toml', 'go.mod', 'tsconfig.json']) {
const file = declaration(project, relative); if (file) declarations[relative] = { sha256: file.sha256, excerpt: file.content.slice(0, 1000) };
}
const python = inspectPythonProject(project, sourceFiles, relative => declaration(project, relative));
const fingerprint = hash(JSON.stringify({ projectId: project.id, root, files: fingerprints, packageSha256: packageFile?.sha256 ?? null,
...(python ? { pythonDeclarationHashes: python.declarationHashes } : {}), head: git.head, gitStatus: git.statusSha256 ?? null, complete }));
const sourceFingerprint = hash(JSON.stringify({ projectId: project.id, root, files: fingerprints,
packageSha256: packageFile?.sha256 ?? null, ...(python ? { pythonDeclarationHashes: python.declarationHashes } : {}), complete }));
const { paths, ...visibleGit } = git;
return { projectId: project.id, name: typeof manifest?.name === 'string' ? manifest.name.slice(0, 150) : project.name,
languages: [...detected].sort(), packageManager, scripts, rules, declarations, ...(python ? { python } : {}),
entryPoints: [manifest?.main, typeof manifest?.bin === 'string' ? manifest.bin : null, names.includes('src/index.ts') ? 'src/index.ts' : null, names.includes('src/index.mjs') ? 'src/index.mjs' : null].filter(value => typeof value === 'string').slice(0, 5),
git: visibleGit, fingerprint, sourceFingerprint, inspectedAt: new Date().toISOString(),
...(includeFiles ? { files: sourceFiles } : {}),
scope: { complete, scannedFiles: fingerprints.length, bytes, excludedFiles: excluded.length, maximumFiles: 1000, maximumBytes: 16 * 1024 * 1024,
note: '有界快照;Git工程遵循ignore并包含未跟踪源码,跳过依赖、链接和常见凭据路径。' } };
}
export function createCheckList(inspection, names) {
if (names !== undefined && (!Array.isArray(names) || !names.length || names.length > 6 || names.some(name => typeof name !== 'string'))) throw new Error('检查脚本选择需要1到6个已识别名称。');
if (inspection.python?.status === 'invalid' && (inspection.packageManager !== 'npm' || names?.some(name => name.startsWith('python:')))) throw new Error(inspection.python.message);
const npmScripts = inspection.packageManager === 'npm' ? inspection.scripts : [];
const pythonChecks = inspection.python?.checks ?? [];
if (!npmScripts.length && !pythonChecks.length) return [];
const preferred = ['test', 'typecheck', 'check', 'lint', 'build'];
const selected = names ?? (inspection.packageManager === 'npm'
? [...(npmScripts.some(script => script.name === 'test') ? ['test'] : npmScripts.filter(script => script.kind === 'test').map(script => script.name).slice(0, 2)), ...preferred.slice(1).filter(name => npmScripts.some(script => script.name === name))].slice(0, 6)
: pythonChecks.map(check => check.name));
return [...new Set(selected)].map(name => {
const python = pythonChecks.find(item => item.name === name); if (python) return structuredClone(python);
const script = npmScripts.find(item => item.name === name);
if (!script) throw new Error('这个脚本不在已识别的检查能力中:' + name);
return { ...script, command: 'npm run ' + name };
});
}