1- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent" ;
2- import { StringEnum , Type } from "@mariozechner/pi-ai" ;
1+ import { StringEnum } from "@earendil-works/pi-ai" ;
2+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent" ;
3+ import { Type } from "typebox" ;
34
45import { loadConfig , normalizeDomains , resolveSearchProviders } from "../src/config.js" ;
56import {
@@ -50,15 +51,15 @@ export default function (pi: ExtensionAPI) {
5051 parameters : Type . Object ( {
5152 query : Type . String ( { description : "Search query" } ) ,
5253 depth : Type . Optional (
53- StringEnum ( [ "basic" , "thorough" ] , {
54+ StringEnum ( [ "basic" , "thorough" ] as const , {
5455 default : "basic" ,
5556 description :
5657 "basic (default): fast search that returns snippets. " +
5758 "thorough: content-enriched search that may include one inline content excerpt." ,
5859 } ) ,
5960 ) ,
6061 freshness : Type . Optional (
61- StringEnum ( [ "day" , "week" , "month" , "year" ] , {
62+ StringEnum ( [ "day" , "week" , "month" , "year" ] as const , {
6263 description : "Optional recency filter for time-sensitive searches." ,
6364 } ) ,
6465 ) ,
@@ -79,19 +80,22 @@ export default function (pi: ExtensionAPI) {
7980 ) ,
8081 } ) ,
8182 async execute ( _toolCallId , params , signal , onUpdate ) {
83+ const abortSignal = signal ?? new AbortController ( ) . signal ;
84+
8285 if ( ! providers . hasAnySearchProvider ) {
8386 throw new Error (
8487 "No search provider configured. Set one of BRAVE_API_KEY, TAVILY_API_KEY, or EXA_API_KEY to enable web_search." ,
8588 ) ;
8689 }
8790
8891 const depth = params . depth ?? "basic" ;
92+ const freshness = params . freshness ;
8993 const maxResults = params . max_results ?? 5 ;
9094 const domains = normalizeDomains ( params . domains ) ;
9195 const resolution = resolveSearchProviders (
9296 {
9397 depth,
94- freshness : params . freshness ,
98+ freshness,
9599 domains,
96100 } ,
97101 providers . search ,
@@ -107,7 +111,7 @@ export default function (pi: ExtensionAPI) {
107111 let lastError : Error | undefined ;
108112
109113 for ( const provider of resolution . providers ) {
110- if ( signal . aborted ) throw new Error ( "Search aborted." ) ;
114+ if ( abortSignal . aborted ) throw new Error ( "Search aborted." ) ;
111115
112116 onUpdate ?.( {
113117 content : [
@@ -124,9 +128,9 @@ export default function (pi: ExtensionAPI) {
124128 query : params . query ,
125129 maxResults,
126130 includeContent : resolution . servedDepth === "thorough" ,
127- freshness : params . freshness ,
131+ freshness,
128132 domains,
129- signal,
133+ signal : abortSignal ,
130134 } ) ;
131135
132136 const notes = [ ...resolution . notes , ...( response . notes ?? [ ] ) ] ;
@@ -140,7 +144,7 @@ export default function (pi: ExtensionAPI) {
140144 provider : provider . name ,
141145 requestedDepth : depth ,
142146 servedDepth : resolution . servedDepth ,
143- freshness : params . freshness ,
147+ freshness,
144148 domains,
145149 appliedFilters : response . appliedFilters ,
146150 notes,
@@ -153,14 +157,14 @@ export default function (pi: ExtensionAPI) {
153157 servedDepth : resolution . servedDepth ,
154158 degraded : resolution . servedDepth !== depth ,
155159 warnings : config . warnings ,
156- freshness : params . freshness ?? null ,
160+ freshness : freshness ?? null ,
157161 domains : domains ?? [ ] ,
158162 appliedFilters : response . appliedFilters ?? null ,
159163 resultCount : response . results . length ,
160164 } ,
161165 } ;
162166 } catch ( error ) {
163- if ( signal . aborted ) throw error ;
167+ if ( abortSignal . aborted ) throw error ;
164168
165169 lastError = error instanceof Error ? error : new Error ( String ( error ) ) ;
166170 if ( ! isTransientProviderError ( lastError ) ) {
@@ -199,6 +203,7 @@ export default function (pi: ExtensionAPI) {
199203 ) ,
200204 } ) ,
201205 async execute ( _toolCallId , params , signal ) {
206+ const abortSignal = signal ?? new AbortController ( ) . signal ;
202207 const url = validateFetchUrl ( params . url ) ;
203208 const offset = params . offset ?? 0 ;
204209 const maxChars = params . max_chars ?? FETCH_DEFAULT_MAX_CHARS ;
@@ -214,11 +219,11 @@ export default function (pi: ExtensionAPI) {
214219 }
215220
216221 try {
217- content = await provider . fetch ( url , signal ) ;
222+ content = await provider . fetch ( url , abortSignal ) ;
218223 providerName = provider . name ;
219224 pageCache . set ( url , content , provider . name ) ;
220225 } catch ( error ) {
221- if ( signal . aborted ) throw error ;
226+ if ( abortSignal . aborted ) throw error ;
222227
223228 const providerError = error instanceof Error ? error : new Error ( String ( error ) ) ;
224229 if ( ! isTransientProviderError ( providerError ) ) {
0 commit comments