@@ -54,6 +54,8 @@ export async function createKnowproTestCommands(
54
54
commands . kpTestChoices = testMultipleChoice ;
55
55
commands . kpTestSearch = testSearchScope ;
56
56
commands . kpTestScoped = setScoped ;
57
+ commands . kpTestSearchCompare = testSearchCompare ;
58
+ commands . kpTestSearchCompareBatch = testSearchCompareBatch ;
57
59
58
60
async function testHtml ( args : string [ ] ) {
59
61
const html = await readAllText ( args [ 0 ] ) ;
@@ -473,75 +475,118 @@ export async function createKnowproTestCommands(
473
475
474
476
function testSearchDef ( ) : CommandMetadata {
475
477
const def = searchDef ( ) ;
476
- def . options ??= { } ;
477
- def . options ! . compare = argBool ( "Compare to V1 mode" , true ) ;
478
+ def . description = "Test v2 (scoped) search" ;
478
479
return def ;
479
480
}
480
481
commands . kpTestSearch . metadata = testSearchDef ( ) ;
481
482
async function testSearchScope ( args : string [ ] ) {
482
483
const namedArgs = parseNamedArguments ( args , testSearchDef ( ) ) ;
483
- const queryTranslator =
484
- context . conversation ! instanceof cm . Memory
485
- ? context . conversation ! . settings . queryTranslator
486
- : context . queryTranslator ;
487
- const result = namedArgs . compare
488
- ? await queryTranslator . translate ( namedArgs . query )
489
- : undefined ;
490
- try {
491
- context . printer . pushColor ( chalk . gray ) ;
492
- if ( result ) {
493
- context . printer . writeTranslation ( result ) ;
494
- }
495
- } finally {
496
- context . printer . popColor ( ) ;
497
- }
498
-
499
- context . printer . writeHeading ( "Scope" ) ;
484
+ const queryTranslator = context . getConversationQueryTranslator ( ) ;
500
485
const resultScope = await queryTranslator . translate2 ! ( namedArgs . query ) ;
501
486
context . printer . writeTranslation ( resultScope ) ;
502
- if ( ( result === undefined || result . success ) && resultScope . success ) {
503
- if ( result !== undefined ) {
504
- const error = kpTest . compareSearchQueryScope (
505
- result . data ,
506
- resultScope . data ,
507
- ) ;
508
- if ( error !== undefined ) {
509
- context . printer . writeError ( error ) ;
510
- }
511
- }
512
- if ( ! context . conversation ) {
513
- return ;
514
- }
515
- const request = parseTypedArguments < kpTest . SearchRequest > (
516
- args ,
517
- searchDef ( ) ,
518
- ) ;
519
- const options : kp . LanguageSearchOptions = {
520
- ... kpTest . createSearchOptions ( request ) ,
521
- compileOptions : {
522
- exactScope : request . exactScope ,
523
- applyScope : request . applyScope ,
524
- } ,
525
- } ;
526
- const query = kp . compileSearchQuery2 (
487
+ if ( ! resultScope . success || ! ensureConversationLoaded ( ) ) {
488
+ return ;
489
+ }
490
+ const request = parseTypedArguments < kpTest . SearchRequest > (
491
+ args ,
492
+ searchDef ( ) ,
493
+ ) ;
494
+ const options : kp . LanguageSearchOptions = {
495
+ ... kpTest . createSearchOptions ( request ) ,
496
+ compileOptions : {
497
+ exactScope : request . exactScope ,
498
+ applyScope : request . applyScope ,
499
+ } ,
500
+ } ;
501
+ const query = kp . compileSearchQuery2 (
502
+ context . conversation ! ,
503
+ resultScope . data ,
504
+ options . compileOptions ,
505
+ ) ;
506
+
507
+ const results = (
508
+ await kp . runSearchQueries ( context . conversation ! , query , options )
509
+ ) . flat ( ) ;
510
+ for ( let i = 0 ; i < results . length ; ++ i ) {
511
+ context . printer . writeConversationSearchResult (
527
512
context . conversation ! ,
528
- resultScope . data ,
529
- options . compileOptions ,
513
+ results [ i ] ,
514
+ namedArgs . showKnowledge ,
515
+ namedArgs . showMessages ,
516
+ namedArgs . maxToDisplay ,
517
+ namedArgs . distinct ,
530
518
) ;
519
+ }
520
+ }
521
+
522
+ commands . kpTestSearchCompare . metadata = testSearchDef ( ) ;
523
+ async function testSearchCompare ( args : string [ ] ) {
524
+ const namedArgs = parseNamedArguments ( args , testSearchDef ( ) ) ;
525
+ const queryTranslator = context . getConversationQueryTranslator ( ) ;
526
+ const result = await kpTest . compareSearchQueryTranslations (
527
+ queryTranslator ,
528
+ namedArgs . query ,
529
+ ) ;
530
+ if ( ! result . success ) {
531
+ context . printer . writeError ( result . message ) ;
532
+ return ;
533
+ }
534
+ writeSearchComparison ( result . data ) ;
535
+ }
531
536
532
- const results = (
533
- await kp . runSearchQueries ( context . conversation ! , query , options )
534
- ) . flat ( ) ;
535
- for ( let i = 0 ; i < results . length ; ++ i ) {
536
- context . printer . writeConversationSearchResult (
537
- context . conversation ! ,
538
- results [ i ] ,
539
- namedArgs . showKnowledge ,
540
- namedArgs . showMessages ,
541
- namedArgs . maxToDisplay ,
542
- namedArgs . distinct ,
537
+ function compareSearchScopeBatchDef ( ) : CommandMetadata {
538
+ return {
539
+ description : "Compare query translations in a batch" ,
540
+ args : {
541
+ srcPath : argSourceFile ( ) ,
542
+ } ,
543
+ options : {
544
+ verbose : argBool ( "Verbose error output" , false ) ,
545
+ } ,
546
+ } ;
547
+ }
548
+ commands . kpTestSearchCompareBatch . metadata = compareSearchScopeBatchDef ( ) ;
549
+ async function testSearchCompareBatch ( args : string [ ] ) {
550
+ const namedArgs = parseNamedArguments ( args , verifySearchBatchDef ( ) ) ;
551
+ const prevOptions = beginTestBatch ( namedArgs ) ;
552
+ try {
553
+ const srcPath = namedArgs . srcPath ;
554
+ const results = await kpTest . compareSearchQueryTranslationsBatch (
555
+ context ,
556
+ srcPath ,
557
+ ( result , index , total ) => {
558
+ context . printer . writeProgress ( index + 1 , total ) ;
559
+ if ( result . success ) {
560
+ const cmp = result . data ;
561
+ if ( cmp . error ) {
562
+ writeSearchComparison ( cmp ) ;
563
+ } else {
564
+ context . printer . writeLineInColor (
565
+ chalk . green ,
566
+ cmp . query ,
567
+ ) ;
568
+ }
569
+ } else {
570
+ context . printer . writeError ( result . message ) ;
571
+ }
572
+ } ,
573
+ ) ;
574
+ if ( ! results . success ) {
575
+ context . printer . writeError ( results . message ) ;
576
+ return ;
577
+ }
578
+ let errorResults = results . data . filter (
579
+ ( r ) => r . error !== undefined && r . error . length > 0 ,
580
+ ) ;
581
+ if ( errorResults . length > 0 ) {
582
+ context . printer . writeLine ( `${ errorResults . length } errors` ) ;
583
+ context . printer . writeList (
584
+ errorResults . map ( ( e ) => e . query ) ,
585
+ { type : "ol" } ,
543
586
) ;
544
587
}
588
+ } finally {
589
+ endTestBatch ( prevOptions ) ;
545
590
}
546
591
}
547
592
@@ -644,6 +689,15 @@ export async function createKnowproTestCommands(
644
689
}
645
690
}
646
691
692
+ function writeSearchComparison ( cmp : kpTest . ScopeQueryComparison ) : void {
693
+ context . printer . writeJsonInColor ( chalk . gray , cmp . expected ) ;
694
+ context . printer . writeHeading ( "Scope" ) ;
695
+ context . printer . writeJson ( cmp . actual ) ;
696
+ if ( cmp . error ) {
697
+ context . printer . writeError ( cmp . error ) ;
698
+ }
699
+ }
700
+
647
701
function beginTestBatch (
648
702
namedArgs : NamedArgs ,
649
703
) : kpTest . KnowproContextOptions {
0 commit comments