feat: preserve bounded model requests and complete task history
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
export function completionBody(config, messages, tools) {
|
||||
return { model: config.model, messages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream: false };
|
||||
}
|
||||
|
||||
export function requestMetrics(body) {
|
||||
const serialized = JSON.stringify(body);
|
||||
const bytes = Buffer.byteLength(serialized, 'utf8');
|
||||
return { bytes, sha256: createHash('sha256').update(serialized, 'utf8').digest('hex'),
|
||||
estimatedInputTokens: Math.ceil(bytes / 3) + body.messages.length * 8 + (body.tools?.length ?? 0) * 16,
|
||||
estimateMethod: 'utf8-third-plus-framing-v1' };
|
||||
}
|
||||
|
||||
export function assertPlannedRequest(body, plan) {
|
||||
if (!plan) return;
|
||||
const metrics = requestMetrics(body);
|
||||
if (metrics.sha256 !== plan.requestSha256 || metrics.bytes !== plan.requestBytes) throw new Error('模型请求正文与已保存计划不一致,未发送请求。');
|
||||
}
|
||||
Reference in New Issue
Block a user