@@ -38,6 +38,7 @@ export type TemplateOpts = {
38
38
sections ?: {
39
39
flags : { [ key in Section ] ?: boolean }
40
40
sections : { [ key in Section ] : string [ ] }
41
+ done : boolean
41
42
}
42
43
43
44
/**
@@ -184,6 +185,7 @@ export async function parseTemplate(
184
185
const sections : TemplateOpts [ 'sections' ] = {
185
186
flags : { } ,
186
187
sections : { system : [ ] , history : [ ] , post : [ ] } ,
188
+ done : false ,
187
189
}
188
190
189
191
opts . sections = sections
@@ -205,6 +207,7 @@ export async function parseTemplate(
205
207
const ast = parser . parse ( template , { } ) as PNode [ ]
206
208
readInserts ( opts , ast )
207
209
let output = render ( template , opts , ast )
210
+ opts . sections . done = true
208
211
let unusedTokens = 0
209
212
let linesAddedCount = 0
210
213
@@ -333,16 +336,23 @@ function render(template: string, opts: TemplateOpts, existingAst?: PNode[]) {
333
336
}
334
337
335
338
const output : string [ ] = [ ]
339
+ let prevMarker : Section = 'system'
336
340
337
341
for ( let i = 0 ; i < ast . length ; i ++ ) {
338
342
const parent = ast [ i ]
339
343
340
344
const result = renderNode ( parent , opts )
341
345
342
- const marker = getMarker ( parent )
343
- fillSection ( opts , marker , result )
346
+ const marker = getMarker ( opts , parent , prevMarker )
347
+ prevMarker = marker
344
348
345
- if ( result ) output . push ( result )
349
+ if ( ! opts . sections ?. done ) {
350
+ fillSection ( opts , marker , result )
351
+ }
352
+
353
+ if ( result ) {
354
+ output . push ( result )
355
+ }
346
356
}
347
357
return output . join ( '' ) . replace ( / \n \n + / g, '\n\n' )
348
358
} catch ( err ) {
@@ -623,6 +633,9 @@ function renderIterator(holder: IterableHolder, children: CNode[], opts: Templat
623
633
if ( isHistory && opts . limit ?. output ) {
624
634
const id = HISTORY_MARKER
625
635
opts . limit . output [ id ] = { src : holder , lines : output }
636
+ if ( opts . sections ) {
637
+ opts . sections . flags . history = true
638
+ }
626
639
return id
627
640
}
628
641
@@ -788,38 +801,40 @@ function fillSection(opts: TemplateOpts, marker: Section | undefined, result: st
788
801
const flags = opts . sections . flags
789
802
const sections = opts . sections . sections
790
803
791
- if ( ! flags . system ) {
804
+ if ( ! flags . system && marker === 'system' ) {
792
805
sections . system . push ( result )
793
806
return
794
807
}
795
808
796
809
if ( marker === 'history' ) {
797
810
flags . system = true
798
- flags . history = true
799
811
return
800
812
}
801
813
802
814
sections . post . push ( result )
803
815
}
804
816
805
- function getMarker ( node : PNode ) : Section | undefined {
806
- if ( typeof node === 'string' ) return
817
+ function getMarker ( opts : TemplateOpts , node : PNode , previous : Section ) : Section {
818
+ if ( ! opts . sections ) return previous
819
+ if ( opts . sections . flags . history ) return 'post'
820
+
821
+ if ( typeof node === 'string' ) return previous
807
822
808
823
switch ( node . kind ) {
809
824
case 'placeholder' : {
810
825
if ( node . value === 'history' ) return 'history'
811
826
if ( node . value === 'system_prompt' ) return 'system'
812
- return
827
+ return previous
813
828
}
814
829
815
830
case 'each' :
816
831
if ( node . value === 'history' ) return 'history'
817
- return
832
+ return previous
818
833
819
834
case 'if' :
820
835
if ( node . value === 'system_prompt' ) return 'system'
821
- return
836
+ return previous
822
837
}
823
838
824
- return
839
+ return previous
825
840
}
0 commit comments