@@ -21,7 +21,7 @@ import {
2121 createKnowledgeTools ,
2222 getKnowledgeToolDefinitions ,
2323} from '../knowledge-tools.js' ;
24- import type { IDataEngine , IKnowledgeAdapter } from '@objectstack/spec/contracts' ;
24+ import type { IDataEngine , IKnowledgeAdapter , IKnowledgeService } from '@objectstack/spec/contracts' ;
2525import type { KnowledgeHit , KnowledgeSource , ObjectKnowledgeSource } from '@objectstack/spec/ai' ;
2626import type { ExecutionContext } from '@objectstack/spec/kernel' ;
2727import { PLATFORM_TOOLS_BY_PACKAGE } from '@objectstack/spec/system' ;
@@ -304,7 +304,7 @@ describe('search_knowledge — scope, granularity and groupBy: the definition',
304304
305305 it ( 'names each argument, and each hit member the new modes add, in the description' , ( ) => {
306306 const [ { description } ] = getKnowledgeToolDefinitions ( ) ;
307- for ( const name of [ '`scope`' , '`granularity: "record"`' , '`groupBy`' , '`groupValue`' , '`headingPath`' ] ) {
307+ for ( const name of [ '`scope`' , '`granularity: "record"`' , '`groupBy`' , '`groupValue`' , '`headingPath`' , '`metadata`' ] ) {
308308 expect ( description ) . toContain ( name ) ;
309309 }
310310 } ) ;
@@ -423,7 +423,16 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
423423 } ;
424424 }
425425
426- function setupModes ( readable : readonly string [ ] = [ 'c1' , 'c2' , 'c3' , 'n1' ] ) {
426+ /**
427+ * `declared` is what the registered clause source lists in `metadataFields`
428+ * now; the corpus was indexed under `['policy', 'status']` whatever it says.
429+ * `queryable` answers the caller's queryable fields per object (absent: no
430+ * security service, so every unmasked field is queryable).
431+ */
432+ function setupModes (
433+ readable : readonly string [ ] = [ 'c1' , 'c2' , 'c3' , 'n1' ] ,
434+ opts : { declared ?: string [ ] ; queryable ?: ( object : string , ctx ?: ExecutionContext ) => string [ ] } = { } ,
435+ ) {
427436 const tables : Record < string , Row [ ] > = { clause : CLAUSES , note : NOTES } ;
428437 const corpus : Record < string , KnowledgeHit [ ] > = {
429438 clauses : [
@@ -458,9 +467,19 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
458467 return page . map ( ( r ) => ( q . fields ? Object . fromEntries ( q . fields . filter ( ( f ) => f in r ) . map ( ( f ) => [ f , r [ f ] ] ) ) : { ...r } ) ) ;
459468 } ) ,
460469 } as unknown as IDataEngine ;
461- const service = new KnowledgeService ( { dataEngine : engine , logger : { warn : ( ) => undefined , debug : ( ) => undefined } } ) ;
470+ const service = new KnowledgeService ( {
471+ dataEngine : engine ,
472+ logger : { warn : ( ) => undefined , debug : ( ) => undefined } ,
473+ ...( opts . queryable
474+ ? { resolveSecurity : ( ) => ( { getQueryableFields : async ( object : string , ctx ?: ExecutionContext ) => opts . queryable ! ( object , ctx ) } ) }
475+ : { } ) ,
476+ } ) ;
462477 service . registerAdapter ( 'memory' , adapter ) ;
463- service . registerSource ( clauses ) ;
478+ service . registerSource (
479+ opts . declared
480+ ? { ...clauses , source : { ...( clauses . source as ObjectKnowledgeSource ) , metadataFields : opts . declared } }
481+ : clauses ,
482+ ) ;
464483 service . registerSource ( notes ) ;
465484 return { service, tools : service . knowledgeTools ( ) , asked } ;
466485 }
@@ -490,11 +509,10 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
490509 expect ( byChunk . hits . map ( ( h ) => h . record ?. id ) ) . toEqual ( [ 'c1' , 'c1' , 'c2' , 'c3' ] ) ;
491510 } ) ;
492511
493- it ( " groupBy: one hit per group, carrying its groupValue and no other metadata" , async ( ) => {
512+ it ( ' groupBy: one hit per group, carrying its groupValue' , async ( ) => {
494513 const { tools } = setupModes ( ) ;
495514 const answer = await tools . searchKnowledge ( { query : 'q' , groupBy : 'policy' } , caller ) ;
496515 expect ( answer . hits . map ( ( h ) => [ h . record ?. id , h . groupValue ] ) ) . toEqual ( [ [ 'c1' , 'P_leave' ] , [ 'c3' , 'P_travel' ] ] ) ;
497- expect ( JSON . stringify ( answer ) ) . not . toContain ( 'draft' ) ;
498516 } ) ;
499517
500518 it ( 'scope: only hits from the named records' , async ( ) => {
@@ -503,7 +521,7 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
503521 expect ( answer . hits . map ( ( h ) => h . record ?. id ) ) . toEqual ( [ 'c2' , 'c3' ] ) ;
504522 } ) ;
505523
506- it ( 'the hit is a whitelist: no chunkId, no docHash, no metadata; headingPath where the passage has one' , async ( ) => {
524+ it ( 'the hit is a whitelist: no chunkId, no docHash, only the listed metadata; headingPath where the passage has one' , async ( ) => {
507525 const { tools } = setupModes ( ) ;
508526 const answer = await tools . searchKnowledge ( { query : 'q' , groupBy : 'policy' } , caller ) ;
509527 expect ( answer . hits [ 0 ] ) . toEqual ( {
@@ -514,11 +532,11 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
514532 record : { object : 'clause' , id : 'c1' } ,
515533 headingPath : [ 'Leave' , 'Annual' ] ,
516534 groupValue : 'P_leave' ,
535+ metadata : { policy : 'P_leave' , status : 'draft' } ,
517536 } ) ;
518537 for ( const hit of answer . hits ) {
519538 expect ( hit ) . not . toHaveProperty ( 'docHash' ) ;
520539 expect ( hit ) . not . toHaveProperty ( 'chunkId' ) ;
521- expect ( hit ) . not . toHaveProperty ( 'metadata' ) ;
522540 }
523541 } ) ;
524542
@@ -530,7 +548,14 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
530548 caller ,
531549 ) ;
532550 expect ( answer ) . toEqual ( {
533- hits : [ { sourceId : 'clauses' , documentId : 'clauses:c2' , snippet : 'sick leave (0)' , score : 0.7 , record : { object : 'clause' , id : 'c2' } } ] ,
551+ hits : [ {
552+ sourceId : 'clauses' ,
553+ documentId : 'clauses:c2' ,
554+ snippet : 'sick leave (0)' ,
555+ score : 0.7 ,
556+ record : { object : 'clause' , id : 'c2' } ,
557+ metadata : { policy : 'P_leave' , status : 'live' } ,
558+ } ] ,
534559 sources : SOURCES ,
535560 } ) ;
536561 } ) ;
@@ -555,4 +580,68 @@ describe('search_knowledge — scope, granularity and groupBy over the knowledge
555580 expect ( a ) . toEqual ( b ) ;
556581 expect ( withheld . asked . map ( ( x ) => x . source ) ) . not . toContain ( 'notes' ) ;
557582 } ) ;
583+
584+ describe ( 'a hit carries the values of the fields its source lists in `metadataFields`' , ( ) => {
585+ it ( 'every hit of an indexed record carries exactly the listed fields, so a clause names its policy with no record read' , async ( ) => {
586+ const { tools } = setupModes ( ) ;
587+ const answer = await tools . searchKnowledge ( { query : 'q' } , caller ) ;
588+ expect ( answer . hits . map ( ( h ) => [ h . record ?. id , h . metadata ] ) ) . toEqual ( [
589+ [ 'c1' , { policy : 'P_leave' , status : 'draft' } ] ,
590+ [ 'c1' , { policy : 'P_leave' , status : 'draft' } ] ,
591+ [ 'c2' , { policy : 'P_leave' , status : 'live' } ] ,
592+ [ 'c3' , { policy : 'P_travel' , status : 'live' } ] ,
593+ ] ) ;
594+ // In the order the source lists them, and only those.
595+ for ( const hit of answer . hits ) expect ( Object . keys ( hit . metadata ?? { } ) ) . toEqual ( [ 'policy' , 'status' ] ) ;
596+ } ) ;
597+
598+ it ( 'a field the source no longer lists is not answered, though the index still stores it' , async ( ) => {
599+ // The corpus was indexed under ['policy', 'status']; the source lists only `policy` now.
600+ const { tools } = setupModes ( undefined , { declared : [ 'policy' ] } ) ;
601+ const answer = await tools . searchKnowledge ( { query : 'q' } , caller ) ;
602+ expect ( answer . hits . map ( ( h ) => h . metadata ) ) . toEqual ( [
603+ { policy : 'P_leave' } , { policy : 'P_leave' } , { policy : 'P_leave' } , { policy : 'P_travel' } ,
604+ ] ) ;
605+ expect ( JSON . stringify ( answer ) ) . not . toContain ( 'draft' ) ;
606+ // Listing none answers no `metadata` member at all.
607+ const none = await setupModes ( undefined , { declared : [ ] } ) . tools . searchKnowledge ( { query : 'q' } , caller ) ;
608+ expect ( none . hits . length ) . toBe ( 4 ) ;
609+ for ( const hit of none . hits ) expect ( hit ) . not . toHaveProperty ( 'metadata' ) ;
610+ } ) ;
611+
612+ it ( 'a listed field whose stored value is null or absent is left out; a stored key no listed field names never is answered' , async ( ) => {
613+ const source : KnowledgeSource = objectSource ( 'clauses' , 'clause' , {
614+ source : { kind : 'object' , object : 'clause' , content : [ 'body' ] , metadataFields : [ 'policy' , 'owner' , 'kind' , 'constructor' ] } as ObjectKnowledgeSource ,
615+ } ) ;
616+ const stored = ( metadata : Record < string , unknown > ) : KnowledgeHit => ( { ...hit ( 'clauses' , 'c1' , 0.9 ) , metadata } ) ;
617+ const service = {
618+ listSources : ( ) => [ source ] ,
619+ search : async ( ) => [
620+ stored ( { policy : 'P_leave' , owner : null , secret : 's3cr3t' } ) ,
621+ stored ( { owner : null } ) ,
622+ ] ,
623+ } as unknown as IKnowledgeService ;
624+ const answer = await createKnowledgeTools ( service ) . searchKnowledge ( { query : 'q' } , caller ) ;
625+ expect ( answer . hits [ 0 ] . metadata ) . toEqual ( { policy : 'P_leave' } ) ;
626+ expect ( answer . hits [ 1 ] ) . not . toHaveProperty ( 'metadata' ) ;
627+ expect ( JSON . stringify ( answer ) ) . not . toContain ( 's3cr3t' ) ;
628+ } ) ;
629+
630+ it ( 'field gate: a caller who may not query one listed field gets no hit from that source; one who may, gets its values' , async ( ) => {
631+ const member = { ...caller , userId : 'u_member' } as ExecutionContext ;
632+ const manager = { ...caller , userId : 'u_manager' } as ExecutionContext ;
633+ // Without a schema the title is read from a field named `title`, a served field the gate asks about too.
634+ const all = [ 'id' , 'title' , 'body' , 'policy' , 'status' , 'author' ] ;
635+ const queryable = ( _object : string , ctx ?: ExecutionContext ) =>
636+ ctx ?. userId === 'u_member' ? all . filter ( ( f ) => f !== 'status' ) : all ;
637+ const gated = setupModes ( undefined , { queryable } ) ;
638+ const refused = await gated . tools . searchKnowledge ( { query : 'q' } , member ) ;
639+ expect ( refused ) . toEqual ( { hits : [ ] , sources : SOURCES } ) ;
640+ expect ( gated . asked . map ( ( x ) => x . source ) ) . not . toContain ( 'clauses' ) ; // never searched, not searched-then-dropped
641+ const served = await gated . tools . searchKnowledge ( { query : 'q' , granularity : 'record' } , manager ) ;
642+ expect ( served . hits . map ( ( h ) => h . metadata ) ) . toEqual ( [
643+ { policy : 'P_leave' , status : 'draft' } , { policy : 'P_leave' , status : 'live' } , { policy : 'P_travel' , status : 'live' } ,
644+ ] ) ;
645+ } ) ;
646+ } ) ;
558647} ) ;
0 commit comments