Files
Agent-Workbench/src/core/context/read-budget.mjs
T

65 lines
4.5 KiB
JavaScript

import { createHash } from 'node:crypto';
const messageBytes = (result, callId) => Buffer.byteLength(JSON.stringify({ role: 'tool', tool_call_id: callId, content: JSON.stringify(result) }), 'utf8');
const boundary = (text, end) => end > 0 && end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1]) ? end - 1 : end;
// This is the actual tool page, not a rewrite of a historical result. Count
// the escaped JSON tool message which will be present in the next request.
export function artifactPage(artifact, text, args, { maximumMessageBytes = 16000, callId = '' } = {}) {
const requestedOffset = args.offset ?? 0, requestedLimit = args.limit ?? 2000;
let offset = Math.min(requestedOffset, text.length);
if (offset > 0 && /[\uDC00-\uDFFF]/.test(text[offset]) && /[\uD800-\uDBFF]/.test(text[offset - 1])) offset--;
let maximumEnd = boundary(text, Math.min(offset + requestedLimit, text.length));
if (maximumEnd === offset && offset < text.length) maximumEnd = boundary(text, Math.min(offset + 2, text.length));
const make = end => ({ artifact_id: artifact.id, kind: artifact.kind, sha256: artifact.sha256,
content: text.slice(offset, end), offset, next_offset: end, returned_characters: end - offset,
total_characters: text.length, truncated: end < text.length, requested_offset: requestedOffset, requested_limit: requestedLimit,
budget_limited: end < maximumEnd, maximum_message_bytes: maximumMessageBytes });
let low = offset, high = maximumEnd;
if (messageBytes(make(low), callId) > maximumMessageBytes) throw new Error('当前请求预算无法容纳工件读取元数据,请先核对任务预算。');
while (low < high) {
const middle = Math.ceil((low + high) / 2), end = boundary(text, middle);
if (messageBytes(make(end), callId) <= maximumMessageBytes) low = middle; else high = middle - 1;
}
const end = boundary(text, low), result = make(end);
if (offset < maximumEnd && end <= offset) throw new Error('当前请求预算无法容纳下一个完整字符,请先核对任务预算。');
return result;
}
export function readMessageBudget(plan, message) {
const calls = message.tool_calls?.length ?? 1;
const assistantTokens = Math.ceil(Buffer.byteLength(JSON.stringify(message), 'utf8') / 3) + 8;
// Reserve space for the current call's framing and the next state/notice.
const tokens = plan.budget.inputBudget - plan.mandatoryInputTokens - assistantTokens - calls * 8 - 512;
return Math.max(1024, Math.floor(tokens * 3 / calls));
}
export function recordArtifactRead(task, artifact, result, invocationId) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
const identity = artifact.id + ':' + artifact.sha256, start = result.offset, end = result.next_offset;
let read = progress.reads.find(value => value.identity === identity);
if (!read) { read = { identity, ranges: [] }; progress.reads.push(read); if (progress.reads.length > 128) progress.reads.shift(); }
const covered = read.ranges.some(range => start >= range[0] && end <= range[1]);
progress.repeated = covered ? progress.repeated + 1 : 0;
if (!covered) {
const ranges = [...read.ranges, [start, end]].sort((a, b) => a[0] - b[0]), merged = [];
for (const range of ranges) { const previous = merged.at(-1); if (previous && range[0] <= previous[1]) previous[1] = Math.max(previous[1], range[1]); else merged.push(range); }
read.ranges = merged.slice(-64);
}
if (artifact.kind === 'context-history-index') {
if (!progress.changingIndexes.includes(identity)) progress.changingIndexes.push(identity);
progress.changingIndexes = progress.changingIndexes.slice(-8);
} else progress.changingIndexes = [];
progress.warning = progress.repeated >= 1 ? '刚才读取的工件区间已完整读取,请使用已返回内容,或按next_offset读取新的区间。'
: progress.changingIndexes.length >= 3 ? '连续读取更新的历史索引没有推进工程;请直接读取实际结果或项目文件,依据已有信息继续任务。' : null;
progress.pause = progress.repeated >= 3 || progress.changingIndexes.length >= 6;
progress.last = { artifactId: artifact.id, sha256: artifact.sha256, offset: start, nextOffset: end, invocationId,
contentSha256: createHash('sha256').update(result.content, 'utf8').digest('hex') };
return progress;
}
export function clearReadStreak(task) {
if (!task.readProgress) return;
task.readProgress.repeated = 0; task.readProgress.changingIndexes = []; task.readProgress.warning = null; task.readProgress.pause = false;
}