





















@@ -4,6 +4,10 @@ import { deriveSessionTotalTokens, hasNonzeroUsage, normalizeUsage } from "../ag
44import { jsonUtf8Bytes } from "../infra/json-utf8-bytes.js";
55import { hasInterSessionUserProvenance } from "../sessions/input-provenance.js";
66import { extractAssistantVisibleText } from "../shared/chat-message-content.js";
7+import {
8+resolveIntegerOption,
9+resolveNonNegativeIntegerOption,
10+} from "../shared/number-coercion.js";
711import { escapeRegExp } from "../shared/regexp.js";
812import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
913import { estimateStringChars, estimateTokensFromChars } from "../utils/cjk-chars.js";
@@ -181,13 +185,24 @@ type TailTranscriptRecord = {
181185record: Record<string, unknown>;
182186};
183187188+function normalizeRecentSessionReadOptions(opts?: Partial<ReadRecentSessionMessagesOptions>) {
189+const maxMessages = resolveNonNegativeIntegerOption(opts?.maxMessages, 0);
190+const maxBytes = resolveIntegerOption(opts?.maxBytes, RECENT_SESSION_MESSAGES_DEFAULT_MAX_BYTES, {
191+min: 1024,
192+});
193+const maxLines = resolveIntegerOption(opts?.maxLines, maxMessages * 20 + 20, {
194+min: maxMessages,
195+});
196+return { maxMessages, maxBytes, maxLines };
197+}
198+184199export function readRecentSessionMessages(
185200sessionId: string,
186201storePath: string | undefined,
187202sessionFile?: string,
188203opts?: ReadRecentSessionMessagesOptions,
189204): unknown[] {
190-const maxMessages = Math.max(0, Math.floor(opts?.maxMessages ?? 0));
205+const { maxMessages, maxBytes, maxLines } = normalizeRecentSessionReadOptions(opts);
191206if (maxMessages === 0) {
192207return [];
193208}
@@ -207,13 +222,8 @@ export function readRecentSessionMessages(
207222return [];
208223}
209224210-const maxBytes = Math.max(
211-1024,
212-Math.floor(opts?.maxBytes ?? RECENT_SESSION_MESSAGES_DEFAULT_MAX_BYTES),
213-);
214225const readLen = Math.min(stat.size, maxBytes);
215226const readStart = Math.max(0, stat.size - readLen);
216-const maxLines = Math.max(maxMessages, Math.floor(opts?.maxLines ?? maxMessages * 20 + 20));
217227218228return (
219229withOpenTranscriptFd(filePath, (fd) => {
@@ -239,14 +249,9 @@ async function readRecentTranscriptTailLinesAsync(
239249stat: fs.Stats,
240250opts: ReadRecentSessionMessagesOptions,
241251): Promise<string[]> {
242-const maxMessages = Math.max(0, Math.floor(opts.maxMessages));
243-const maxBytes = Math.max(
244-1024,
245-Math.floor(opts.maxBytes ?? RECENT_SESSION_MESSAGES_DEFAULT_MAX_BYTES),
246-);
252+const { maxMessages, maxBytes, maxLines } = normalizeRecentSessionReadOptions(opts);
247253const readLen = Math.min(stat.size, maxBytes);
248254const readStart = Math.max(0, stat.size - readLen);
249-const maxLines = Math.max(maxMessages, Math.floor(opts.maxLines ?? maxMessages * 20 + 20));
250255const handle = await fs.promises.open(filePath, "r");
251256try {
252257const buffer = Buffer.alloc(readLen);
@@ -655,7 +660,8 @@ export async function readRecentSessionMessagesAsync(
655660sessionFile?: string,
656661opts?: ReadRecentSessionMessagesOptions,
657662): Promise<unknown[]> {
658-const maxMessages = Math.max(0, Math.floor(opts?.maxMessages ?? 0));
663+const normalized = normalizeRecentSessionReadOptions(opts);
664+const { maxMessages } = normalized;
659665if (maxMessages === 0) {
660666return [];
661667}
@@ -675,8 +681,7 @@ export async function readRecentSessionMessagesAsync(
675681return [];
676682}
677683const lines = await readRecentTranscriptTailLinesAsync(filePath, stat, {
678- ...opts,
679- maxMessages,
684+ ...normalized,
680685});
681686return parseRecentTranscriptTailMessages(lines, maxMessages);
682687}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。