@@ -82,6 +82,7 @@ export type PromptOpts = {
82
82
resolvedScenario : string
83
83
modelFormat ?: ModelFormat
84
84
jsonValues : Record < string , any > | undefined
85
+ contextBuffer ?: number
85
86
}
86
87
87
88
export type BuildPromptOpts = {
@@ -231,7 +232,6 @@ export async function createPromptParts(opts: PromptOpts, encoder: TokenCounter)
231
232
* The lines from `getLinesForPrompt` are returned in time-descending order
232
233
*/
233
234
let template = getTemplate ( opts )
234
- const templateSize = await encoder ( template )
235
235
236
236
if ( opts . modelFormat ) {
237
237
template = replaceTags ( template , opts . modelFormat )
@@ -244,10 +244,9 @@ export async function createPromptParts(opts: PromptOpts, encoder: TokenCounter)
244
244
* If we ambitiously include the entire history then embeddings will never be included.
245
245
* The queryable embeddings are messages that are _NOT_ included in the context
246
246
*/
247
- const maxContext = opts . settings
248
- ? getContextLimit ( opts . user , opts . settings ) - templateSize - opts . settings . maxTokens !
249
- : undefined
250
- const lines = await getLinesForPrompt ( opts , encoder , maxContext )
247
+ const contextBuffer = opts . contextBuffer ?? 0
248
+ const maxContext = opts . settings ? getContextLimit ( opts . user , opts . settings ) : undefined
249
+ const lines = await getLinesForPrompt ( opts , encoder , ( maxContext || 0 ) + contextBuffer )
251
250
const parts = await buildPromptParts ( opts , lines , encoder )
252
251
253
252
const prompt = await injectPlaceholders ( template , {
@@ -697,6 +696,7 @@ export async function fillPromptWithLines(opts: {
697
696
/** Nodes to be inserted at a particular depth in the `lines` */
698
697
inserts ?: Map < number , string >
699
698
optional ?: Array < { id : string ; content : string } >
699
+ marker ?: string
700
700
} ) {
701
701
const { encoder, tokenLimit, context, lines, inserts = new Map ( ) , optional = [ ] } = opts
702
702
const insertsCost = await encoder ( Array . from ( inserts . values ( ) ) . join ( ' ' ) )
@@ -706,7 +706,11 @@ export async function fillPromptWithLines(opts: {
706
706
* Optional placeholders do not count towards token counts.
707
707
* They are optional after everything else has been inserted therefore we remove them from the prompt
708
708
*/
709
- const cleanContext = optional . reduce ( ( amble , { id } ) => amble . replace ( id , '' ) , context )
709
+ let cleanContext = optional . reduce ( ( amble , { id } ) => amble . replace ( id , '' ) , context )
710
+ if ( opts . marker ) {
711
+ cleanContext . replace ( opts . marker , '' )
712
+ }
713
+
710
714
let count = await encoder ( cleanContext )
711
715
const adding : string [ ] = [ ]
712
716
0 commit comments