@@ -37,7 +37,7 @@ import { TabTitleIndex, createTabTitleIndex } from "./tabTitleIndex.mjs";
37
37
import { ChildProcess , fork } from "child_process" ;
38
38
import { fileURLToPath } from "node:url" ;
39
39
import path from "node:path" ;
40
- import fs from "node:fs" ;
40
+ import fs , { readFileSync } from "node:fs" ;
41
41
42
42
import {
43
43
CommandHandler ,
@@ -84,7 +84,7 @@ import { ShoppingActions } from "./commerce/schema/userActions.mjs";
84
84
import { SchemaDiscoveryActions } from "./discovery/schema/discoveryActions.mjs" ;
85
85
import { ExternalBrowserActions } from "./externalBrowserActionSchema.mjs" ;
86
86
import { BrowserControl } from "../common/browserControl.mjs" ;
87
- import { openai , TextEmbeddingModel } from "aiclient" ;
87
+ import { openai , TextEmbeddingModel , wikipedia } from "aiclient" ;
88
88
import { urlResolver , bingWithGrounding } from "azure-ai-foundry" ;
89
89
import { createExternalBrowserClient } from "./rpc/externalBrowserControlClient.mjs" ;
90
90
import { deleteCachedSchema } from "./crossword/cachedSchema.mjs" ;
@@ -123,6 +123,12 @@ export type BrowserActionContext = {
123
123
localHostPort : number ;
124
124
macrosStore ?: MacroStore | undefined ; // Add MacroStore instance
125
125
currentWebSearchResults ?: Map < string , any [ ] > | undefined ; // Store search results for follow-up actions
126
+ resolverSettings : {
127
+ searchResolver ?: boolean | undefined ;
128
+ keywordResolver ?: boolean | undefined ;
129
+ wikipediaResolver ?: boolean | undefined ;
130
+ historyResolver ?: boolean | undefined ;
131
+ } ;
126
132
} ;
127
133
128
134
export interface urlResolutionAction {
@@ -151,6 +157,12 @@ async function initializeBrowserContext(
151
157
index : undefined ,
152
158
localHostPort,
153
159
macrosStore : undefined , // Will be initialized in updateBrowserContext
160
+ resolverSettings : {
161
+ searchResolver : true ,
162
+ keywordResolver : true ,
163
+ wikipediaResolver : true ,
164
+ historyResolver : false ,
165
+ } ,
154
166
} ;
155
167
}
156
168
@@ -244,6 +256,26 @@ async function updateBrowserContext(
244
256
}
245
257
} ) ;
246
258
}
259
+
260
+ // rehydrate resolver settings
261
+ const sessionDir : string | undefined =
262
+ await getSessionFolderPath ( context ) ;
263
+ const contents : string = await readFileSync (
264
+ path . join ( sessionDir ! , "settings.json" ) ,
265
+ "utf-8" ,
266
+ ) ;
267
+
268
+ if ( contents . length > 0 ) {
269
+ const config = JSON . parse ( contents ) ;
270
+ context . agentContext . resolverSettings . searchResolver =
271
+ config . searchResolver ;
272
+ context . agentContext . resolverSettings . keywordResolver =
273
+ config . keywordResolver ;
274
+ context . agentContext . resolverSettings . wikipediaResolver =
275
+ config . wikipediaResolver ;
276
+ context . agentContext . resolverSettings . historyResolver =
277
+ config . historyResolver ;
278
+ }
247
279
} else {
248
280
const webSocket = context . agentContext . webSocket ;
249
281
if ( webSocket ) {
@@ -614,6 +646,13 @@ async function getSessionFolderPath(
614
646
return sessionDir ;
615
647
}
616
648
649
+ async function saveSettings ( context : SessionContext < BrowserActionContext > ) {
650
+ await context . sessionStorage ?. write (
651
+ "settings.json" ,
652
+ JSON . stringify ( context . agentContext . resolverSettings ) ,
653
+ ) ;
654
+ }
655
+
617
656
async function resolveEntity (
618
657
type : string ,
619
658
name : string ,
@@ -701,43 +740,55 @@ async function resolveWebPage(
701
740
}
702
741
703
742
// try to resolve URL using website visit history first
704
- const historyUrl = await resolveURLWithHistory ( context , site ) ;
705
- if ( historyUrl ) {
706
- debug ( `Resolved URL from history: ${ historyUrl } ` ) ;
707
- return historyUrl ;
743
+ if ( context . agentContext . resolverSettings . historyResolver ) {
744
+ const historyUrl = await resolveURLWithHistory ( context , site ) ;
745
+ if ( historyUrl ) {
746
+ debug ( `Resolved URL from history: ${ historyUrl } ` ) ;
747
+ return historyUrl ;
748
+ }
708
749
}
709
- const cachehitUrl = await urlResolver . resolveURLByKeyword ( site ) ;
710
- if ( cachehitUrl ) {
711
- debug ( `Resolved URL from cache: ${ cachehitUrl } ` ) ;
712
-
713
- if (
714
- cachehitUrl . indexOf ( "https://" ) !== 0 &&
715
- cachehitUrl . indexOf ( "http://" ) !== 0
716
- ) {
717
- return "https://" + cachehitUrl ;
718
- } else {
719
- return cachehitUrl ;
750
+
751
+ // try to resolve URL string using known keyword matching
752
+ if ( context . agentContext . resolverSettings . keywordResolver ) {
753
+ const cachehitUrl = await urlResolver . resolveURLByKeyword ( site ) ;
754
+ if ( cachehitUrl ) {
755
+ debug ( `Resolved URL from cache: ${ cachehitUrl } ` ) ;
756
+
757
+ if (
758
+ cachehitUrl . indexOf ( "https://" ) !== 0 &&
759
+ cachehitUrl . indexOf ( "http://" ) !== 0
760
+ ) {
761
+ return "https://" + cachehitUrl ;
762
+ } else {
763
+ return cachehitUrl ;
764
+ }
765
+ }
766
+ }
767
+
768
+ // try to resolve URL by using the Wikipedia API
769
+ if ( context . agentContext . resolverSettings . wikipediaResolver ) {
770
+ debug ( `Resolving URL using Wikipedia for: ${ site } ` ) ;
771
+ const wikiPediaUrl = await urlResolver . resolveURLWithWikipedia (
772
+ site ,
773
+ wikipedia . apiSettingsFromEnv ( ) ,
774
+ ) ;
775
+ if ( wikiPediaUrl ) {
776
+ debug ( `Resolved URL using Wikipedia: ${ wikiPediaUrl } ` ) ;
777
+ return wikiPediaUrl ;
720
778
}
721
779
}
722
- // TODO: reenable
723
- // const wikiPediaUrl = await urlResolver.resolveURLWithWikipedia(
724
- // site,
725
- // wikipedia.apiSettingsFromEnv(),
726
- // );
727
- // if (wikiPediaUrl) {
728
- // debug(`Resolved URL using Wikipedia: ${wikiPediaUrl}`);
729
- // return wikiPediaUrl;
730
- // }
731
780
732
781
// try to resolve URL using LLM + internet search
733
- const url = await urlResolver . resolveURLWithSearch (
734
- site ,
735
- bingWithGrounding . apiSettingsFromEnv ( ) ,
736
- ) ;
782
+ if ( context . agentContext . resolverSettings . searchResolver ) {
783
+ const url = await urlResolver . resolveURLWithSearch (
784
+ site ,
785
+ bingWithGrounding . apiSettingsFromEnv ( ) ,
786
+ ) ;
737
787
738
- if ( url ) {
739
- debug ( `Resolved URL using Bing with Grounding: ${ url } ` ) ;
740
- return url ;
788
+ if ( url ) {
789
+ debug ( `Resolved URL using Bing with Grounding: ${ url } ` ) ;
790
+ return url ;
791
+ }
741
792
}
742
793
743
794
// can't get a URL
@@ -2086,5 +2137,112 @@ export const handlers: CommandHandlerTable = {
2086
2137
} ,
2087
2138
} ,
2088
2139
} ,
2140
+ resolver : {
2141
+ description : "Toggle URL resolver methods" ,
2142
+ defaultSubCommand : "list" ,
2143
+ commands : {
2144
+ list : {
2145
+ description : "List all available URL resolvers" ,
2146
+ run : async (
2147
+ context : ActionContext < BrowserActionContext > ,
2148
+ ) => {
2149
+ const agentContext =
2150
+ context . sessionContext . agentContext ;
2151
+ const resolvers = Object . entries (
2152
+ agentContext . resolverSettings ,
2153
+ )
2154
+ . filter ( ( [ , enabled ] ) => enabled !== undefined )
2155
+ . map ( ( [ name , enabled ] ) => ( {
2156
+ name,
2157
+ enabled,
2158
+ } ) ) ;
2159
+ displaySuccess (
2160
+ `Available resolvers: ${ JSON . stringify ( resolvers ) } ` ,
2161
+ context ,
2162
+ ) ;
2163
+ } ,
2164
+ } ,
2165
+ search : {
2166
+ description : "Toggle search resolver" ,
2167
+ run : async (
2168
+ context : ActionContext < BrowserActionContext > ,
2169
+ ) => {
2170
+ const agentContext =
2171
+ context . sessionContext . agentContext ;
2172
+ agentContext . resolverSettings . searchResolver =
2173
+ ! agentContext . resolverSettings . searchResolver ;
2174
+ displaySuccess (
2175
+ `Search resolver is now ${
2176
+ agentContext . resolverSettings . searchResolver
2177
+ ? "enabled"
2178
+ : "disabled"
2179
+ } `,
2180
+ context ,
2181
+ ) ;
2182
+ saveSettings ( context . sessionContext ) ;
2183
+ } ,
2184
+ } ,
2185
+ keyword : {
2186
+ description : "Toggle keyword resolver" ,
2187
+ run : async (
2188
+ context : ActionContext < BrowserActionContext > ,
2189
+ ) => {
2190
+ const agentContext =
2191
+ context . sessionContext . agentContext ;
2192
+ agentContext . resolverSettings . keywordResolver =
2193
+ ! agentContext . resolverSettings . keywordResolver ;
2194
+ displaySuccess (
2195
+ `Keyword resolver is now ${
2196
+ agentContext . resolverSettings . keywordResolver
2197
+ ? "enabled"
2198
+ : "disabled"
2199
+ } `,
2200
+ context ,
2201
+ ) ;
2202
+ saveSettings ( context . sessionContext ) ;
2203
+ } ,
2204
+ } ,
2205
+ wikipedia : {
2206
+ description : "Toggle Wikipedia resolver" ,
2207
+ run : async (
2208
+ context : ActionContext < BrowserActionContext > ,
2209
+ ) => {
2210
+ const agentContext =
2211
+ context . sessionContext . agentContext ;
2212
+ agentContext . resolverSettings . wikipediaResolver =
2213
+ ! agentContext . resolverSettings . wikipediaResolver ;
2214
+ displaySuccess (
2215
+ `Wikipedia resolver is now ${
2216
+ agentContext . resolverSettings . wikipediaResolver
2217
+ ? "enabled"
2218
+ : "disabled"
2219
+ } `,
2220
+ context ,
2221
+ ) ;
2222
+ saveSettings ( context . sessionContext ) ;
2223
+ } ,
2224
+ } ,
2225
+ history : {
2226
+ description : "Toggle history resolver" ,
2227
+ run : async (
2228
+ context : ActionContext < BrowserActionContext > ,
2229
+ ) => {
2230
+ const agentContext =
2231
+ context . sessionContext . agentContext ;
2232
+ agentContext . resolverSettings . historyResolver =
2233
+ ! agentContext . resolverSettings . historyResolver ;
2234
+ displaySuccess (
2235
+ `History resolver is now ${
2236
+ agentContext . resolverSettings . historyResolver
2237
+ ? "enabled"
2238
+ : "disabled"
2239
+ } `,
2240
+ context ,
2241
+ ) ;
2242
+ saveSettings ( context . sessionContext ) ;
2243
+ } ,
2244
+ } ,
2245
+ } ,
2246
+ } ,
2089
2247
} ,
2090
2248
} ;
0 commit comments