66 SCOPES ,
77} from "./utilities/const" ;
88import { CopilotChatProvider } from "./utilities/CopilotChat" ;
9- import { AxAgent , AxFunction , ax , f , AxFunctionProcessor , agent } from "@ax-llm/ax" ;
9+ import { AxAgent , AxFunction , AxAI , ax , f , AxFunctionProcessor , agent } from "@ax-llm/ax" ;
1010import { getReadme , searchMcpServers2 } from "./utilities/repoSearch" ;
1111import {
1212 logChatSearch ,
@@ -20,6 +20,22 @@ import {
2020import { TelemetryEvents } from "./telemetry/types" ;
2121import { outputLogger } from "./utilities/outputLogger" ;
2222
23+ async function configureCopilotProvider ( debug : boolean ) {
24+ const copilot = CopilotChatProvider . getInstance ( ) ;
25+ const provider = await copilot . getProvider ( { interactive : true } ) ;
26+ provider . setOptions ( {
27+ debug,
28+ fetch : ( input : RequestInfo | URL , init ?: RequestInit ) => {
29+ init ! . headers = {
30+ ...init ?. headers ,
31+ ...copilot . headers ,
32+ } ;
33+ return fetch ( input , init ) ;
34+ } ,
35+ } ) ;
36+ return { provider, copilot } ;
37+ }
38+
2339const getRepoReadme : AxFunction = {
2440 func : getReadme ,
2541 name : "getReadme" ,
@@ -165,19 +181,13 @@ export const handler: vscode.ChatRequestHandler = async (
165181 prompt : request . prompt
166182 } ) ;
167183
168- const copilot = CopilotChatProvider . getInstance ( ) ;
169- const provider = copilot . provider ;
170- provider . setOptions (
171- {
172- debug : false ,
173- fetch : ( input : RequestInfo | URL , init ?: RequestInit ) => {
174- init ! . headers = {
175- ...init ?. headers ,
176- ...copilot . headers ,
177- } ;
178- return fetch ( input , init ) ;
179- }
180- } ) ;
184+ let provider : AxAI ;
185+ try {
186+ ( { provider } = await configureCopilotProvider ( false ) ) ;
187+ } catch ( error ) {
188+ outputLogger . warn ( "Copilot provider unavailable for chat handler" , error as Error ) ;
189+ throw new Error ( "GitHub authentication is required to use AI-assisted chat features." ) ;
190+ }
181191
182192 const session = await vscode . authentication . getSession (
183193 GITHUB_AUTH_PROVIDER_ID ,
@@ -329,25 +339,16 @@ class GitHubSearchTool
329339 // Create the search coordinator agent with composition
330340 this . searchCoordinatorAgent = this . createSearchCoordinatorAgent ( ) ;
331341 }
332-
342+
333343 private createSearchCoordinatorAgent ( ) : ReturnType < typeof agent > {
334344 // Create query generator agent
335345 const queryGeneratorAgent = createQueryGeneratorAgent ( ) ;
336- const copilot = CopilotChatProvider . getInstance ( ) ;
337- const provider = copilot . provider ;
338- outputLogger . info ( "headers: " , copilot . headers ) ;
339- provider . setOptions ( { debug : true , fetch : ( input : RequestInfo | URL , init ?: RequestInit ) => {
340- init ! . headers = {
341- ...init ?. headers ,
342- ...copilot . headers ,
343- } ;
344- return fetch ( input , init ) ;
345- } } ) ;
346346 // Create the search and process function
347347 const generateAndSearchRepos : AxFunction = {
348348 name : "generateAndSearchRepos" ,
349349 description : "Generate a search query and search for MCP repositories" ,
350350 func : async ( args : { originalUserMessage : string } ) => {
351+ const { provider } = await configureCopilotProvider ( true ) ;
351352
352353 // Use the query generator agent
353354 const queryResult = await queryGeneratorAgent . forward (
@@ -387,19 +388,7 @@ class GitHubSearchTool
387388
388389 async invoke ( options : vscode . LanguageModelToolInvocationOptions < { userQuery : string ; } > ) : Promise < vscode . LanguageModelToolResult > {
389390 this . stream . progress ( "Beginning search for installable MCP servers..." ) ;
390- const copilot = CopilotChatProvider . getInstance ( ) ;
391- const provider = copilot . provider ;
392- provider . setOptions (
393- {
394- debug : false ,
395- fetch : ( input : RequestInfo | URL , init ?: RequestInit ) => {
396- init ! . headers = {
397- ...init ?. headers ,
398- ...copilot . headers ,
399- } ;
400- return fetch ( input , init ) ;
401- }
402- } ) ;
391+ const { provider } = await configureCopilotProvider ( false ) ;
403392 outputLogger . info ( "GitHubSearchTool invoked" , { options } ) ;
404393
405394 try {
@@ -476,25 +465,13 @@ class GitHubSearchTool
476465}
477466
478467export async function readmeExtractionRequest ( readme : string ) {
479- // Try to get GitHub token from VSCode authentication API using the same scopes as extension activation
480- const session = await vscode . authentication . getSession ( GITHUB_AUTH_PROVIDER_ID , SCOPES , { createIfNone : false } ) ;
481- const accessToken = session ?. accessToken ;
482- if ( ! accessToken ) {
483- throw new Error ( "Copilot not set up." ) ;
468+ let provider : AxAI ;
469+ try {
470+ ( { provider } = await configureCopilotProvider ( false ) ) ;
471+ } catch ( error ) {
472+ outputLogger . warn ( "Copilot provider unavailable for README extraction" , error as Error ) ;
473+ throw new Error ( "GitHub authentication is required to use AI-assisted setup." ) ;
484474 }
485- const copilot = CopilotChatProvider . getInstance ( ) ;
486- const provider = copilot . provider ;
487- provider . setOptions (
488- {
489- debug : false ,
490- fetch : ( input : RequestInfo | URL , init ?: RequestInit ) => {
491- init ! . headers = {
492- ...init ?. headers ,
493- ...copilot . headers ,
494- } ;
495- return fetch ( input , init ) ;
496- }
497- } ) ;
498475
499476 const extractor = ax ( `
500477 "Extracts the MCP server configuration from a README.md file and returns the necessary information to run it."
0 commit comments