Files
Agent-Workbench/src/shared/contracts/file-intent.mjs
T

27 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// This recognizes explicit file actions only; it does not certify business semantics.
export const negatedFileAction = prefix => /(?:不要|无需|不需要|不用|禁止|不得|不必|勿|不允许|不能|不再|避免|不)(?:\s|再|去|进行|执行|直接|自动|擅自|任何|做|你)*(?:.{0,24}(?:或|和|以及)\s*)?$|\b(?:do not|don't|without|never|no need to|must not|cannot|can't)\b.{0,40}$|(?:解释|说明|分析|告诉我).{0,24}(?:怎么|如何)\s*$|\bhow\s+to\s*$/i.test(prefix);
export function requestedFilePaths(text) {
const paths = [];
for (const clause of String(text).split(/[,,;;。\n]/)) {
const action = /(?:创建|新建|保存|写入|修改|重写|修复|调整|替换)|\b(?:create|save|write|modify|rewrite|fix|change|replace)\b/i.exec(clause);
if (!action || negatedFileAction(clause.slice(0, action.index))) continue;
const suffix = clause.slice(action.index + action[0].length);
// Stop at a reference or prohibition instead of treating its input file as
// another required output. Ambiguous wording stays outside this small gate.
const boundary = /\b(?:from|using|based on)\b|(?:根据|参考|参照|不要|无需|不用|禁止|不得)/i.exec(suffix);
const outputs = boundary ? suffix.slice(0, boundary.index) : suffix;
for (const match of outputs.matchAll(/(?:[\w\u3400-\u9fff@.-]+\/)*[\w\u3400-\u9fff@-]+\.(?:html?|svg|txt|md|json|[cm]?[jt]sx?|css|py|ya?ml)\b/ig)) paths.push(match[0]);
}
return [...new Set(paths)];
}
export function changeRequirement(task) {
const positive = text => String(text).split(/[,,;;。\n]/).some(clause => {
const match = /(?:修改|重写|修复|改写|调整|新增功能|增加功能|删除|替换)|\b(?:modify|rewrite|fix|change|delete|replace)\b/i.exec(clause);
return match && !negatedFileAction(clause.slice(0, match.index));
});
const latest = (task.instructions ?? []).findLast(item => item.source !== 'review-action' && positive(item.content));
const goal = task.contract?.goal ?? task.goal;
return latest ? { required: true, after: latest.createdAt, text: latest.content, paths: requestedFilePaths(latest.content) }
: { required: positive(goal), after: task.createdAt, text: goal, paths: requestedFilePaths(goal) };
}