Skip to content

Commit e3eacc9

Browse files
committed
clean
1 parent 7ebc91e commit e3eacc9

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

src/extension/agents/vscode-node/organizationAndEnterpriseAgentProvider.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class OrganizationAndEnterpriseAgentProvider extends Disposable implement
2626
readonly onDidChangeCustomAgents = this._onDidChangeCustomAgents.event;
2727

2828
private isFetching = false;
29-
private memoryCache = new Map<string, vscode.CustomAgentResource[]>();
29+
private memoryCache: vscode.CustomAgentResource[] | undefined;
3030

3131
constructor(
3232
@IOctoKitService private readonly octoKitService: IOctoKitService,
@@ -35,36 +35,29 @@ export class OrganizationAndEnterpriseAgentProvider extends Disposable implement
3535
@IFileSystemService private readonly fileSystem: IFileSystemService,
3636
) {
3737
super();
38+
39+
// Trigger async fetch to update cache. Note: this provider is re-created each time
40+
// the user signs in, so this will re-fetch on sign-in. See logic in conversationFeature.ts.
41+
this.fetchAndUpdateCache().catch(error => {
42+
this.logService.error(`[OrganizationAndEnterpriseAgentProvider] Error in background fetch: ${error}`);
43+
});
3844
}
3945

4046
private getCacheDir(): vscode.Uri {
4147
return vscode.Uri.joinPath(this.extensionContext.globalStorageUri, 'githubAgentsCache');
4248
}
4349

4450
async provideCustomAgents(
45-
options: vscode.CustomAgentQueryOptions,
51+
_options: vscode.CustomAgentQueryOptions,
4652
_token: vscode.CancellationToken
4753
): Promise<vscode.CustomAgentResource[]> {
4854
try {
49-
const user = await this.octoKitService.getCurrentAuthedUser();
50-
if (!user) {
51-
return [];
55+
if (this.memoryCache !== undefined) {
56+
return this.memoryCache;
5257
}
5358

54-
// If we have successfully fetched and cached in memory, return from memory
55-
if (this.memoryCache.has(user.login)) {
56-
return this.memoryCache.get(user.login)!;
57-
}
58-
59-
// Read from file cache first
60-
const fileCachedAgents = await this.readFromCache();
61-
62-
// Trigger async fetch to update cache
63-
this.fetchAndUpdateCache(options).catch(error => {
64-
this.logService.error(`[OrganizationAndEnterpriseAgentProvider] Error in background fetch: ${error}`);
65-
});
66-
67-
return fileCachedAgents;
59+
// Return results from file cache
60+
return await this.readFromCache();
6861
} catch (error) {
6962
this.logService.error(`[OrganizationAndEnterpriseAgentProvider] Error in provideCustomAgents: ${error}`);
7063
return [];
@@ -129,9 +122,7 @@ export class OrganizationAndEnterpriseAgentProvider extends Disposable implement
129122
return operation();
130123
}
131124

132-
private async fetchAndUpdateCache(
133-
options: vscode.CustomAgentQueryOptions
134-
): Promise<void> {
125+
private async fetchAndUpdateCache(): Promise<void> {
135126
// Prevent concurrent fetches
136127
if (this.isFetching) {
137128
this.logService.trace('[OrganizationAndEnterpriseAgentProvider] Fetch already in progress, skipping');
@@ -158,9 +149,9 @@ export class OrganizationAndEnterpriseAgentProvider extends Disposable implement
158149
this.logService.trace(`[OrganizationAndEnterpriseAgentProvider] Found ${organizations.length} organizations: ${organizations.join(', ')}`);
159150

160151
// Convert VS Code API options to internal options
161-
const internalOptions = options ? {
152+
const internalOptions = {
162153
includeSources: ['org', 'enterprise'] // don't include 'repo'
163-
} satisfies CustomAgentListOptions : undefined;
154+
} satisfies CustomAgentListOptions;
164155

165156
// Fetch agents from all organizations
166157
const agentsByOrg = new Map<string, Map<string, CustomAgentListItem>>();
@@ -320,8 +311,8 @@ export class OrganizationAndEnterpriseAgentProvider extends Disposable implement
320311
this.logService.trace(`[OrganizationAndEnterpriseAgentProvider] Updated cache with ${totalAgents} agents from ${organizations.length} organizations`);
321312

322313
// If all fetch operations succeeded, populate memory cache
323-
if (!hadAnyFetchErrors && !this.memoryCache.has(user.login)) {
324-
this.memoryCache.set(user.login, await this.readFromCache());
314+
if (!hadAnyFetchErrors && this.memoryCache === undefined) {
315+
this.memoryCache = await this.readFromCache();
325316
this.logService.trace('[OrganizationAndEnterpriseAgentProvider] Successfully populated memory cache');
326317
}
327318

0 commit comments

Comments
 (0)