Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/simuliationWorkspaceChunkSearch.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import type { ChatResponsePart, Progress } from 'vscode';
6
import { EmbeddingType } from '../../src/platform/embeddings/common/embeddingsComputer';
7
import { GithubRepoId } from '../../src/platform/git/common/gitService';
8
import { IIgnoreService } from '../../src/platform/ignore/common/ignoreService';
9
import { ILogService } from '../../src/platform/log/common/logService';
10
import { GithubCodeSearchScope, IGithubCodeSearchService, parseGithubCodeSearchResponse } from '../../src/platform/remoteCodeSearch/common/githubCodeSearchService';
11
import { LexicalCodeSearchResult, RemoteCodeSearchError, RemoteCodeSearchIndexState, RemoteCodeSearchIndexStatus, SemanticCodeSearchResult } from '../../src/platform/remoteCodeSearch/common/remoteCodeSearch';
12
import { WorkspaceChunkQuery, WorkspaceChunkSearchOptions } from '../../src/platform/workspaceChunkSearch/common/workspaceChunkSearch';
13
import { BuildIndexTriggerReason, TriggerIndexingError } from '../../src/platform/workspaceChunkSearch/node/codeSearch/codeSearchRepo';
14
import { IWorkspaceChunkSearchService, WorkspaceChunkSearchResult, WorkspaceChunkSearchSizing, WorkspaceIndexState } from '../../src/platform/workspaceChunkSearch/node/workspaceChunkSearchService';
15
import { Result } from '../../src/util/common/result';
16
import { TelemetryCorrelationId } from '../../src/util/common/telemetryCorrelationId';
17
import { CancellationToken } from '../../src/util/vs/base/common/cancellation';
18
import { Event } from '../../src/util/vs/base/common/event';
19
import { Disposable } from '../../src/util/vs/base/common/lifecycle';
20
import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';
21
22
const searchEndpoint = 'http://localhost:4443/api/embeddings/code/search';
23
24
25
class SimulationGithubCodeSearchService extends Disposable implements IGithubCodeSearchService {
26
27
declare readonly _serviceBrand: undefined;
28
29
30
constructor(
31
@IIgnoreService private readonly _ignoreService: IIgnoreService,
32
@ILogService private readonly _logService: ILogService,
33
) {
34
super();
35
}
36
37
async lexicalSearch(_authOptions: { silent: boolean }, _scope: GithubCodeSearchScope, _query: string, _maxResults: number, _options: WorkspaceChunkSearchOptions, _telemetryInfo: TelemetryCorrelationId, _token: CancellationToken): Promise<LexicalCodeSearchResult> {
38
throw new Error('Method not implemented.');
39
}
40
41
async semanticSearch(authOptions: { silent: boolean }, embeddingType: EmbeddingType, repo: GithubCodeSearchScope & { kind: 'repo' }, query: string, maxResults: number, options: WorkspaceChunkSearchOptions, _telemetryInfo: TelemetryCorrelationId, token: CancellationToken): Promise<SemanticCodeSearchResult> {
42
this._logService.trace(`SimulationGithubCodeSearchService::searchRepo(${repo.githubRepoId}, ${query})`);
43
const response = await fetch(searchEndpoint, {
44
method: 'POST',
45
headers: {
46
'Content-Type': 'application/json'
47
},
48
body: JSON.stringify({
49
scoping_query: `repo:msbench/workspace`,
50
prompt: query,
51
limit: maxResults
52
})
53
});
54
55
if (!response.ok) {
56
this._logService.trace(`SimulationGithubCodeSearchService::searchRepo(${repo.githubRepoId}, ${query}) failed. status: ${response.status}`);
57
const body = await response.text();
58
throw new Error(`Error fetching index status: ${response.status} - ${body}`);
59
}
60
61
const json: any = await response.json();
62
63
const result = await parseGithubCodeSearchResponse(json, repo, { ...options, skipVerifyRepo: true }, this._ignoreService);
64
this._logService.trace(`SimulationGithubCodeSearchService::searchRepo(${repo.githubRepoId}, ${query}) success. Found ${result.chunks.length} chunks`);
65
return result;
66
}
67
68
async getRemoteIndexState(authOptions: { silent: boolean }, githubRepoId: GithubRepoId, _telemetryInfo: TelemetryCorrelationId, token: CancellationToken): Promise<Result<RemoteCodeSearchIndexState, RemoteCodeSearchError>> {
69
return Result.ok({ status: RemoteCodeSearchIndexStatus.Ready, indexedCommit: 'HEAD' });
70
}
71
72
triggerIndexing(authOptions: { silent: boolean }, triggerReason: 'auto' | 'manual' | 'tool', githubRepoId: GithubRepoId): Promise<Result<true, RemoteCodeSearchError>> {
73
throw new Error('Method not implemented.');
74
}
75
}
76
77
78
export class SimulationCodeSearchChunkSearchService extends Disposable implements IWorkspaceChunkSearchService {
79
declare readonly _serviceBrand: undefined;
80
81
private readonly _githubCodeSearchService: IGithubCodeSearchService;
82
83
constructor(
84
@IInstantiationService instantiationService: IInstantiationService
85
) {
86
super();
87
88
this._githubCodeSearchService = instantiationService.createInstance(SimulationGithubCodeSearchService);
89
}
90
91
readonly onDidChangeIndexState = Event.None;
92
93
getIndexState(): Promise<WorkspaceIndexState> {
94
throw new Error('Method not implemented.');
95
}
96
97
async isAvailable(): Promise<boolean> {
98
return true;
99
}
100
101
async searchFileChunks(sizing: WorkspaceChunkSearchSizing, query: WorkspaceChunkQuery, options: WorkspaceChunkSearchOptions, telemetryInfo: TelemetryCorrelationId, progress: Progress<ChatResponsePart> | undefined, token: CancellationToken): Promise<WorkspaceChunkSearchResult> {
102
const repo = new GithubRepoId('test-org', 'test-repo');
103
try {
104
const results = await this._githubCodeSearchService.semanticSearch({ silent: true }, EmbeddingType.text3small_512, {
105
kind: 'repo',
106
githubRepoId: repo,
107
indexedCommit: undefined,
108
localRepoRoot: undefined,
109
}, query.queryText, sizing.maxResults ?? 128, options, telemetryInfo, token);
110
return {
111
chunks: results.chunks,
112
};
113
} catch (error) {
114
console.error('Error searching repo:', error);
115
}
116
117
return {
118
chunks: [],
119
};
120
}
121
122
triggerIndexing(trigger: BuildIndexTriggerReason, _onProgress?: (message: string) => void, _telemetryInfo?: TelemetryCorrelationId, _token?: CancellationToken): Promise<Result<true, TriggerIndexingError>> {
123
throw new Error('Method not implemented.');
124
}
125
126
deleteExternalIngestWorkspaceIndex(): Promise<void> {
127
return Promise.resolve();
128
}
129
130
async *getDiagnosticsDump(): AsyncIterable<string> {
131
yield 'Simulation mode — no diagnostics available.';
132
}
133
}
134
135