Skip to content

Commit 550f4f5

Browse files
committed
feat: Removed GitHub sign in requirement for non-AI assisted usage. Users will only be prompted to sign in to GitHub if the extension needs to use GitHub API's for search.
1 parent de418f2 commit 550f4f5

File tree

4 files changed

+180
-122
lines changed

4 files changed

+180
-122
lines changed

src/McpAgent.ts

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SCOPES,
77
} from "./utilities/const";
88
import { 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";
1010
import { getReadme, searchMcpServers2 } from "./utilities/repoSearch";
1111
import {
1212
logChatSearch,
@@ -20,6 +20,22 @@ import {
2020
import { TelemetryEvents } from "./telemetry/types";
2121
import { 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+
2339
const 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

478467
export 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."

src/extension.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,38 @@ export async function activate(context: vscode.ExtensionContext) {
5353
context.subscriptions.push(logger, { dispose: shutdownLogs });
5454
context.subscriptions.push(telemetryReporter);
5555
// console.dir(await vscode.authentication.getAccounts('github'), {depth: null});
56-
outputLogger.debug("Getting GitHub authentication session");
57-
const session = await vscode.authentication.getSession(
58-
GITHUB_AUTH_PROVIDER_ID,
59-
SCOPES,
60-
{ createIfNone: true }
61-
);
56+
outputLogger.debug("Checking for existing GitHub authentication session");
57+
let session: vscode.AuthenticationSession | undefined;
58+
try {
59+
session = await vscode.authentication.getSession(
60+
GITHUB_AUTH_PROVIDER_ID,
61+
SCOPES,
62+
{ createIfNone: false }
63+
);
64+
} catch (error) {
65+
outputLogger.warn("Failed to retrieve existing GitHub session", error as Error);
66+
}
6267

63-
outputLogger.info("Initializing Copilot MCP LM Provider");
64-
const copilot = await CopilotChatProvider.initialize(context);
65-
const models = await copilot.getModels();
66-
outputLogger.info("Available Copilot models", models.map((m: any) => m.id));
68+
outputLogger.info("Configuring Copilot MCP LM Provider");
69+
const copilot = await CopilotChatProvider.configure(context);
70+
const initialized = await copilot.tryEnsureInitialized();
71+
if (initialized) {
72+
try {
73+
const models = await copilot.getModels();
74+
outputLogger.info("Available Copilot models", models.map((m: any) => m.id));
75+
} catch (modelError) {
76+
outputLogger.warn("Failed to retrieve Copilot models", modelError as Error);
77+
}
78+
} else {
79+
outputLogger.debug("Copilot provider not initialized yet; will initialize on demand");
80+
}
6781

6882
// Initialize standardized telemetry context
6983
const extensionVersion = vscode.extensions.getExtension('AutomataLabs.copilot-mcp')?.packageJSON?.version || 'unknown';
70-
initializeContext(session, extensionVersion);
71-
setCommonLogAttributes({ ...session.account });
84+
initializeContext(session, extensionVersion);
85+
if (session) {
86+
setCommonLogAttributes({ ...session.account });
87+
}
7288

7389
// Initialize CloudMCP indexer
7490
outputLogger.info("Initializing CloudMCP indexer");
@@ -106,10 +122,9 @@ export async function activate(context: vscode.ExtensionContext) {
106122
context.subscriptions.push(disposable, showLogsCommand);
107123

108124
outputLogger.debug("Creating CopilotMcpViewProvider");
109-
const provider = new CopilotMcpViewProvider(
110-
context.extensionUri,
111-
session.accessToken
112-
);
125+
const provider = new CopilotMcpViewProvider(
126+
context.extensionUri
127+
);
113128
context.subscriptions.push(
114129
vscode.window.registerWebviewViewProvider(
115130
CopilotMcpViewProvider.viewType,

src/panels/ExtensionPanel.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import type {
4040
} from "../shared/types/rpcTypes";
4141
import axios from "axios";
4242
import { outputLogger } from "../utilities/outputLogger";
43-
import { resolve } from "dns";
43+
import { GITHUB_AUTH_PROVIDER_ID, SCOPES } from "../utilities/const";
4444

4545
// Helper function to read servers from .vscode/mcp.json
4646
async function getServersFromMcpJsonFile(
@@ -231,8 +231,8 @@ export class CopilotMcpViewProvider implements vscode.WebviewViewProvider {
231231
octokit: any;
232232

233233
constructor(
234-
private readonly _extensionUri: vscode.Uri,
235-
private readonly _accessToken: string ) {}
234+
private readonly _extensionUri: vscode.Uri
235+
) {}
236236

237237
resolveWebviewView(
238238
webviewView: vscode.WebviewView,
@@ -529,9 +529,27 @@ export class CopilotMcpViewProvider implements vscode.WebviewViewProvider {
529529
}
530530

531531
async getOctokit() {
532+
if (this.octokit) {
533+
return this.octokit;
534+
}
535+
let session: vscode.AuthenticationSession | undefined;
536+
try {
537+
session = await vscode.authentication.getSession(
538+
GITHUB_AUTH_PROVIDER_ID,
539+
SCOPES,
540+
{ createIfNone: true }
541+
);
542+
} catch (error) {
543+
outputLogger.warn("Failed to acquire GitHub session for Octokit", error as Error);
544+
throw new Error("GitHub authentication is required to continue.");
545+
}
546+
const accessToken = session?.accessToken;
547+
if (!accessToken) {
548+
throw new Error("GitHub authentication is required to continue.");
549+
}
532550
const Octokit = await import("octokit");
533551
this.octokit = new Octokit.Octokit({
534-
auth: this._accessToken,
552+
auth: accessToken,
535553
});
536554
return this.octokit;
537555
}

0 commit comments

Comments
 (0)