@@ -27,8 +27,18 @@ import type {
2727 BehaviorFlagKey ,
2828 SecretType ,
2929} from "../types" ;
30+ import {
31+ type AvailableToolsFilter ,
32+ filterTools ,
33+ } from "./available-tools-filter" ;
3034import { normalizeScopes } from "./scopes-display" ;
3135
36+ export {
37+ type AvailableToolsFilter ,
38+ type FilterToolsOptions ,
39+ filterTools ,
40+ } from "./available-tools-filter" ;
41+
3242const DEFAULT_PAGE_SIZE = 25 ;
3343const PAGE_SIZE_OPTIONS = [ 25 , 50 , 100 , 200 ] as const ;
3444
@@ -226,13 +236,6 @@ export function toToolAnchorId(value: string): string {
226236 return value . toLowerCase ( ) . replace ( / \s + / g, "-" ) . replace ( / \. / g, "" ) ;
227237}
228238
229- export type AvailableToolsFilter =
230- | "all"
231- | "has_scopes"
232- | "no_scopes"
233- | "has_secrets"
234- | "no_secrets" ;
235-
236239export type AvailableToolsSort =
237240 | "name_asc"
238241 | "name_desc"
@@ -692,88 +695,6 @@ export function sortTools(
692695 }
693696}
694697
695- export type FilterToolsOptions = {
696- activeOperations ?: Set < string > ;
697- behaviorFlags ?: Partial < Record < BehaviorFlagKey , boolean > > ;
698- } ;
699-
700- function matchesFilterCategory (
701- tool : AvailableToolsTableProps [ "tools" ] [ number ] ,
702- filter : AvailableToolsFilter
703- ) : boolean {
704- const hasScopes = buildScopeDisplayItems ( tool . scopes ?? [ ] ) . length > 0 ;
705- const hasSecrets =
706- ( tool . secretsInfo ?. length ?? 0 ) > 0 || ( tool . secrets ?. length ?? 0 ) > 0 ;
707-
708- switch ( filter ) {
709- case "has_scopes" :
710- return hasScopes ;
711- case "no_scopes" :
712- return ! hasScopes ;
713- case "has_secrets" :
714- return hasSecrets ;
715- case "no_secrets" :
716- return ! hasSecrets ;
717- default :
718- return true ;
719- }
720- }
721-
722- function matchesOperations (
723- tool : AvailableToolsTableProps [ "tools" ] [ number ] ,
724- activeOperations : Set < string >
725- ) : boolean {
726- if ( activeOperations . size === 0 ) {
727- return true ;
728- }
729- const toolOps = tool . metadata ?. behavior ?. operations ?? [ ] ;
730- return toolOps . some ( ( op ) => activeOperations . has ( op ) ) ;
731- }
732-
733- function matchesBehaviorFlags (
734- tool : AvailableToolsTableProps [ "tools" ] [ number ] ,
735- behaviorFlags : Partial < Record < BehaviorFlagKey , boolean > >
736- ) : boolean {
737- for ( const [ key , expected ] of Object . entries ( behaviorFlags ) as [
738- BehaviorFlagKey ,
739- boolean | undefined ,
740- ] [ ] ) {
741- if ( expected === undefined ) {
742- continue ;
743- }
744- if ( tool . metadata ?. behavior ?. [ key ] !== expected ) {
745- return false ;
746- }
747- }
748- return true ;
749- }
750-
751- export function filterTools (
752- tools : AvailableToolsTableProps [ "tools" ] ,
753- query : string ,
754- filter : AvailableToolsFilter ,
755- options : FilterToolsOptions = { }
756- ) : AvailableToolsTableProps [ "tools" ] {
757- const { activeOperations = new Set ( ) , behaviorFlags = { } } = options ;
758- const normalizedQuery = query . trim ( ) . toLowerCase ( ) ;
759-
760- return tools . filter ( ( tool ) => {
761- const haystack = [ tool . name , tool . qualifiedName , tool . description ?? "" ]
762- . join ( " " )
763- . toLowerCase ( ) ;
764- if ( normalizedQuery . length > 0 && ! haystack . includes ( normalizedQuery ) ) {
765- return false ;
766- }
767- if ( ! matchesFilterCategory ( tool , filter ) ) {
768- return false ;
769- }
770- if ( ! matchesOperations ( tool , activeOperations ) ) {
771- return false ;
772- }
773- return matchesBehaviorFlags ( tool , behaviorFlags ) ;
774- } ) ;
775- }
776-
777698/**
778699 * AvailableToolsTable
779700 *
0 commit comments