40 lines
3.6 KiB
JavaScript
40 lines
3.6 KiB
JavaScript
import { isId } from './execution.mjs';
|
|
import { ceiling } from './execution-policy.mjs';
|
|
import { isContinuation, taskContract } from './task-control.mjs';
|
|
import { planHash, planError, taskRequirements } from './task-plans.mjs';
|
|
export { requestedFilePaths, changeRequirement } from './file-intent.mjs';
|
|
|
|
export function fileResultInput(input, { source = 'agent', config = {} } = {}) {
|
|
if (!input || typeof input !== 'object' || Array.isArray(input) || Object.keys(input).some(key => !['files', 'reason'].includes(key))
|
|
|| typeof input.reason !== 'string' || !input.reason.trim() || input.reason.length > ceiling(config, 600) || input.reason.includes('\0')
|
|
|| !Array.isArray(input.files) || !input.files.length || input.files.length > ceiling(config, 8)) throw planError('成果核对需要文件和采用原因。', 'input', 400);
|
|
const files = input.files.map(file => {
|
|
if (!file || typeof file !== 'object' || Object.keys(file).some(key => !['path', 'sha256', 'read_call_ids', 'source_result_id'].includes(key))
|
|
|| typeof file.path !== 'string' || !file.path || file.path.length > ceiling(config, 240) || /[\\:\0]/.test(file.path) || file.path.startsWith('/')
|
|
|| file.path.split('/').some(part => !part || part === '.' || part === '..') || !/^[a-f0-9]{64}$/.test(file.sha256)
|
|
|| file.source_result_id !== undefined && !isId(file.source_result_id)) throw planError('需要项目内文件路径、当前SHA256和有效来源ID。', 'input', 400);
|
|
const calls = file.read_call_ids ?? [];
|
|
if (!Array.isArray(calls) || calls.length > ceiling(config, 128) || calls.some(id => typeof id !== 'string' || !id || id.length > ceiling(config, 160)) || new Set(calls).size !== calls.length
|
|
|| source === 'agent' && !calls.length || source === 'user' && calls.length) throw planError('模型需引用本Task实际read_file调用;用户核对由宿主直接读取。', 'input', 400);
|
|
return { path: file.path, sha256: file.sha256, read_call_ids: calls, ...(file.source_result_id ? { source_result_id: file.source_result_id } : {}) };
|
|
});
|
|
if (new Set(files.map(file => file.path.toLowerCase())).size !== files.length) throw planError('核对文件不能重复。', 'input', 400);
|
|
return { files, reason: input.reason.trim() };
|
|
}
|
|
|
|
export function fileResultRequirements(task) {
|
|
const value = taskRequirements(task);
|
|
value.instructions = value.instructions.filter(item => !isContinuation(item.content));
|
|
return structuredClone({ ...value, contract: taskContract(task) });
|
|
}
|
|
export const fileResultRequirementsHash = task => planHash(fileResultRequirements(task));
|
|
|
|
export const fileResultToolDefinitions = [{ type: 'function', function: {
|
|
name: 'verify_existing_files', description: '把已存在且满足当前要求的文本文件登记为当前成果,不改写文件、不代替测试或业务验收。先用本Task的read_file完整分页读取,再传当前SHA与原read_call_ids;跨Task采用须显式source_result_id。文件/要求变化凭据失效。明确修改要求仍需本Task实际修改。返回原成果ID,可用于results类计划步骤;相同文件不重复登记。',
|
|
parameters: { type: 'object', additionalProperties: false, required: ['files', 'reason'], properties: {
|
|
reason: { type: 'string', minLength: 1, maxLength: 600 }, files: { type: 'array', minItems: 1, maxItems: 8, items: { type: 'object', additionalProperties: false, required: ['path', 'sha256', 'read_call_ids'], properties: {
|
|
path: { type: 'string' }, sha256: { type: 'string', pattern: '^[a-f0-9]{64}$' }, read_call_ids: { type: 'array', minItems: 1, maxItems: 128, items: { type: 'string' } }, source_result_id: { type: 'string' },
|
|
} } },
|
|
} },
|
|
} }];
|