@@ -4,6 +4,7 @@ import type {
44 PineconeSearchTextParams ,
55} from '@/tools/pinecone/types'
66import type { ToolConfig } from '@/tools/types'
7+ import { safeUrlPathSegment } from '@/tools/url-path'
78
89function parseRerank (
910 value : PineconeSearchTextParams [ 'rerank' ]
@@ -134,7 +135,7 @@ export const searchTextTool: ToolConfig<PineconeSearchTextParams, PineconeRespon
134135 } ,
135136 method : 'POST' ,
136137 url : ( params ) =>
137- `${ params . indexHost } /records/namespaces/${ encodeURIComponent ( params . namespace ?. trim ( ) || '__default__' ) } /search` ,
138+ `${ params . indexHost } /records/namespaces/${ safeUrlPathSegment ( params . namespace ?? '' , 'namespace' , { allowSeparators : true , preserveWhitespace : true } ) } /search` ,
138139 headers : ( params ) => ( {
139140 'Api-Key' : params . apiKey ,
140141 'Content-Type' : 'application/json' ,
@@ -164,12 +165,15 @@ export const searchTextTool: ToolConfig<PineconeSearchTextParams, PineconeRespon
164165 }
165166
166167 if ( params . rerank ) {
167- const rerank = parseRerank ( params . rerank )
168- const rerankQuery = rerank ?. query
169- // Pinecone takes `rerank.query` as a string and falls back to the search query when unset.
170- body . rerank =
168+ const { query : rerankQuery , ...rerank } = parseRerank ( params . rerank ) ?? { }
169+ const queryText =
171170 rerankQuery !== null && typeof rerankQuery === 'object' && ! Array . isArray ( rerankQuery )
172- ? { ...rerank , query : ( rerankQuery as Record < string , unknown > ) . text }
171+ ? ( rerankQuery as Record < string , unknown > ) . text
172+ : rerankQuery
173+ // Pinecone takes `rerank.query` as a string and reranks against the search query when unset.
174+ body . rerank =
175+ typeof queryText === 'string' && queryText . trim ( )
176+ ? { ...rerank , query : queryText }
173177 : rerank
174178 }
175179
0 commit comments