feat: ship bounded tool feedback and Python engineering checks in 1.7.0
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import fs from 'node:fs';
|
||||
import { parse } from 'smol-toml';
|
||||
import { safePath } from '../server/files.mjs';
|
||||
|
||||
export function inspectPythonProject(project, files, readDeclaration) {
|
||||
const sources = files.filter(file => file.language === 'Python' && file.sha256).map(file => ({ path: file.path, sha256: file.sha256 }));
|
||||
const pyproject = readDeclaration('pyproject.toml'), pytestIni = readDeclaration('pytest.ini');
|
||||
if (!sources.length && !pyproject && !pytestIni) return null;
|
||||
const declarationHashes = Object.fromEntries([['pyproject.toml', pyproject], ['pytest.ini', pytestIni]].filter(([, file]) => file).map(([name, file]) => [name, file.sha256]));
|
||||
let document = {}, settings, framework, testPaths, pattern;
|
||||
try {
|
||||
if (pyproject) document = parse(pyproject.content);
|
||||
const own = document.tool?.['agent-workbench']?.python ?? {};
|
||||
if (!own || typeof own !== 'object' || Array.isArray(own)) throw new Error('tool.agent-workbench.python需要一个表。');
|
||||
const pytest = document.tool?.pytest?.ini_options;
|
||||
const dependencies = [...(document.project?.dependencies ?? []), ...Object.values(document.project?.['optional-dependencies'] ?? {}).flat(),
|
||||
...Object.values(document['dependency-groups'] ?? {}).flat()].filter(item => typeof item === 'string');
|
||||
framework = own.framework ?? (pytest || pytestIni || dependencies.some(item => /^pytest(?:[<>=!~\s;\[]|$)/i.test(item)) ? 'pytest' : 'unittest');
|
||||
if (!['pytest', 'unittest'].includes(framework)) throw new Error('Python检查框架只支持unittest或pytest。');
|
||||
const common = ['tests', 'test'].find(folder => sources.some(file => file.path.startsWith(folder + '/')));
|
||||
testPaths = own['test-paths'] ?? (framework === 'pytest' ? pytest?.testpaths : undefined) ?? [common || '.'];
|
||||
if (!Array.isArray(testPaths) || !testPaths.length || testPaths.length > 6 || testPaths.some(item => typeof item !== 'string' || !item || item.length > 500)) throw new Error('Python test-paths需要1到6个项目相对路径。');
|
||||
testPaths = [...new Set(testPaths)];
|
||||
for (const relative of testPaths) {
|
||||
const absolute = safePath(project, relative);
|
||||
if (fs.lstatSync(absolute).isSymbolicLink() || !fs.statSync(absolute).isDirectory() && !(framework === 'pytest' && fs.statSync(absolute).isFile() && relative.endsWith('.py'))) throw new Error('Python检查目标需要常规目录或pytest测试文件。');
|
||||
}
|
||||
if (framework === 'unittest' && testPaths.length !== 1) throw new Error('unittest当前支持一个显式发现目录。');
|
||||
pattern = own.pattern ?? 'test*.py';
|
||||
if (typeof pattern !== 'string' || pattern.length > 128 || !/^[a-z0-9_.*?\[\]-]+\.py$/i.test(pattern)) throw new Error('unittest pattern需要文件名模式,不能包含目录或命令。');
|
||||
settings = framework === 'unittest' ? { startDirectory: testPaths[0], pattern } : { testPaths,
|
||||
configPath: pytestIni ? 'pytest.ini' : pytest ? 'pyproject.toml' : null, neutralizeAddopts: true };
|
||||
} catch (error) {
|
||||
return { status: 'invalid', declarationHashes, sources, checks: [], message: 'Python检查声明无效:' + error.message };
|
||||
}
|
||||
const hasTests = sources.some(file => /(^|\/)(test[^/]*|[^/]*_test)\.py$/i.test(file.path)) || !!document.tool?.pytest || !!pytestIni || !!document.tool?.['agent-workbench']?.python;
|
||||
const checks = [];
|
||||
if (hasTests) checks.push({ name: 'python:test', kind: 'test', framework, engine: 'python', command: framework + ' · ' + testPaths.join(', '),
|
||||
script: framework === 'unittest' ? 'unittest发现目录:' + testPaths[0] + '\n文件模式:' + pattern : 'pytest测试目标:' + testPaths.join(', ') + '\n配置:' + (settings.configPath || '默认') + '\n使用所选目标;不追加环境或配置中的addopts。',
|
||||
lifecycle: [], python: { mode: framework, settings, files: sources } });
|
||||
if (sources.length) checks.push({ name: 'python:compile', kind: 'check', framework: null, engine: 'python', command: 'Python源码编译 · ' + sources.length + ' 个文件',
|
||||
script: '按已登记源码字节进行语法编译,不执行模块。', lifecycle: [], python: { mode: 'compile', settings: {}, files: sources } });
|
||||
const candidates = ['.venv/Scripts/python.exe', 'venv/Scripts/python.exe', '.venv/bin/python', 'venv/bin/python'];
|
||||
const preferredExecutable = candidates.find(relative => {
|
||||
try { const absolute = safePath(project, relative); return fs.statSync(absolute).isFile(); } catch { return false; }
|
||||
}) ?? null;
|
||||
return { status: 'detected', framework, declarationHashes, settings, sources, checks, preferredExecutable,
|
||||
note: '静态识别源码、测试目标和声明;未导入项目或执行项目解释器。' };
|
||||
}
|
||||
Reference in New Issue
Block a user