@@ -10,7 +10,11 @@ import { formatEnvelopeTimestamp } from "../envelope.js";
1010import type { TemplateContext } from "../templating.js" ;
1111
1212const MAX_UNTRUSTED_JSON_STRING_CHARS = 2_000 ;
13- const MAX_UNTRUSTED_HISTORY_ENTRIES = 20 ;
13+ const DEFAULT_UNTRUSTED_HISTORY_ENTRIES = 20 ;
14+
15+ type InboundUserContextPrefixOptions = {
16+ historyLimit ?: number ;
17+ } ;
1418
1519function stripNullBytes ( value : string ) : string {
1620 return value . replaceAll ( "\u0000" , "" ) ;
@@ -44,6 +48,13 @@ function truncateUntrustedJsonString(value: string): string {
4448 return `${ truncateUtf16Safe ( value , Math . max ( 0 , MAX_UNTRUSTED_JSON_STRING_CHARS - 14 ) ) . trimEnd ( ) } …[truncated]` ;
4549}
4650
51+ function resolveUntrustedHistoryEntryLimit ( value : unknown ) : number {
52+ if ( typeof value !== "number" || ! Number . isFinite ( value ) ) {
53+ return DEFAULT_UNTRUSTED_HISTORY_ENTRIES ;
54+ }
55+ return Math . max ( 0 , Math . floor ( value ) ) ;
56+ }
57+
4758function sanitizeUntrustedJsonValue ( value : unknown ) : unknown {
4859 if ( typeof value === "string" ) {
4960 return neutralizeMarkdownFences ( truncateUntrustedJsonString ( value ) ) ;
@@ -178,6 +189,7 @@ export function buildInboundMetaSystemPrompt(
178189export function buildInboundUserContextPrefix (
179190 ctx : TemplateContext ,
180191 envelope ?: EnvelopeFormatOptions ,
192+ options ?: InboundUserContextPrefixOptions ,
181193) : string {
182194 const blocks : string [ ] = [ ] ;
183195 const chatType = normalizeChatType ( ctx . ChatType ) ;
@@ -193,7 +205,8 @@ export function buildInboundUserContextPrefix(
193205 const resolvedMessageId = messageId ?? messageIdFull ;
194206 const timestampStr = formatConversationTimestamp ( ctx . Timestamp , envelope ) ;
195207 const inboundHistory = Array . isArray ( ctx . InboundHistory ) ? ctx . InboundHistory : [ ] ;
196- const boundedHistory = inboundHistory . slice ( - MAX_UNTRUSTED_HISTORY_ENTRIES ) ;
208+ const historyLimit = resolveUntrustedHistoryEntryLimit ( options ?. historyLimit ) ;
209+ const boundedHistory = historyLimit > 0 ? inboundHistory . slice ( - historyLimit ) : [ ] ;
197210
198211 // Keep volatile conversation/message identifiers in the user-role block so the system
199212 // prompt stays byte-stable across task-scoped sessions and reply turns.
@@ -231,7 +244,7 @@ export function buildInboundUserContextPrefix(
231244 has_forwarded_context : normalizePromptMetadataString ( ctx . ForwardedFrom ) ? true : undefined ,
232245 has_thread_starter : sanitizePromptBody ( ctx . ThreadStarterBody ) ? true : undefined ,
233246 history_count : boundedHistory . length > 0 ? boundedHistory . length : undefined ,
234- history_truncated : inboundHistory . length > MAX_UNTRUSTED_HISTORY_ENTRIES ? true : undefined ,
247+ history_truncated : inboundHistory . length > historyLimit ? true : undefined ,
235248 } ;
236249 if ( Object . values ( conversationInfo ) . some ( ( v ) => v !== undefined ) ) {
237250 blocks . push (
0 commit comments