




















11import fs from "node:fs";
2+import { readTextFileTail, tailText } from "./text-file-utils.mjs";
2334const ERROR_DETAIL_TAIL_BYTES = 64 * 1024;
45const REPLY_TEXT_PREVIEW_BYTES = 8 * 1024;
@@ -15,13 +16,6 @@ function textByteLength(text) {
1516return Buffer.byteLength(text, "utf8");
1617}
171818-function tailText(text, maxBytes = ERROR_DETAIL_TAIL_BYTES) {
19-if (textByteLength(text) <= maxBytes) {
20-return text;
21-}
22-return Buffer.from(text, "utf8").subarray(-maxBytes).toString("utf8");
23-}
24-2519function summarizeReplyTexts(replyTexts) {
2620const previewStart = Math.max(0, replyTexts.length - REPLY_TEXT_PREVIEW_COUNT);
2721const recent = replyTexts.slice(previewStart).map((text, index) => ({
@@ -32,29 +26,6 @@ function summarizeReplyTexts(replyTexts) {
3226return JSON.stringify({ count: replyTexts.length, recent });
3327}
342835-function readTextFileTail(file, maxBytes = ERROR_DETAIL_TAIL_BYTES) {
36-let stat;
37-try {
38-stat = fs.statSync(file);
39-} catch {
40-return "";
41-}
42-if (!stat.isFile() || stat.size <= 0) {
43-return "";
44-}
45-46-const length = Math.min(maxBytes, stat.size);
47-const start = stat.size - length;
48-const fd = fs.openSync(file, "r");
49-try {
50-const buffer = Buffer.alloc(length);
51-const bytesRead = fs.readSync(fd, buffer, 0, length, start);
52-return buffer.subarray(0, bytesRead).toString("utf8");
53-} finally {
54-fs.closeSync(fd);
55-}
56-}
57-5829function fileContainsPattern(file, pattern) {
5930let stat;
6031try {
@@ -198,7 +169,7 @@ export function assertAgentReplyContainsMarker(marker, outputPath) {
198169if (replyTexts.some((text) => text.includes(marker))) {
199170return;
200171}
201-const outputTail = tailText(output);
172+const outputTail = tailText(output, ERROR_DETAIL_TAIL_BYTES);
202173throw new Error(
203174`agent reply payload did not contain marker ${marker}. Reply payload summary: ${summarizeReplyTexts(replyTexts)}. Output tail: ${outputTail}`,
204175);
@@ -208,6 +179,6 @@ export function assertOpenAiRequestLogUsed(requestLogPath, label = "mock OpenAI
208179if (fileContainsPattern(requestLogPath, OPENAI_REQUEST_PATH_PATTERN)) {
209180return;
210181}
211-const requestLogTail = readTextFileTail(requestLogPath);
182+const requestLogTail = readTextFileTail(requestLogPath, ERROR_DETAIL_TAIL_BYTES);
212183throw new Error(`${label} was not used. Request log tail: ${requestLogTail}`);
213184}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。