Path: blob/main/extensions/copilot/test/base/simuliationWorkspaceChunkSearch.ts
13388 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import type { ChatResponsePart, Progress } from 'vscode';5import { EmbeddingType } from '../../src/platform/embeddings/common/embeddingsComputer';6import { GithubRepoId } from '../../src/platform/git/common/gitService';7import { IIgnoreService } from '../../src/platform/ignore/common/ignoreService';8import { ILogService } from '../../src/platform/log/common/logService';9import { GithubCodeSearchScope, IGithubCodeSearchService, parseGithubCodeSearchResponse } from '../../src/platform/remoteCodeSearch/common/githubCodeSearchService';10import { LexicalCodeSearchResult, RemoteCodeSearchError, RemoteCodeSearchIndexState, RemoteCodeSearchIndexStatus, SemanticCodeSearchResult } from '../../src/platform/remoteCodeSearch/common/remoteCodeSearch';11import { WorkspaceChunkQuery, WorkspaceChunkSearchOptions } from '../../src/platform/workspaceChunkSearch/common/workspaceChunkSearch';12import { BuildIndexTriggerReason, TriggerIndexingError } from '../../src/platform/workspaceChunkSearch/node/codeSearch/codeSearchRepo';13import { IWorkspaceChunkSearchService, WorkspaceChunkSearchResult, WorkspaceChunkSearchSizing, WorkspaceIndexState } from '../../src/platform/workspaceChunkSearch/node/workspaceChunkSearchService';14import { Result } from '../../src/util/common/result';15import { TelemetryCorrelationId } from '../../src/util/common/telemetryCorrelationId';16import { CancellationToken } from '../../src/util/vs/base/common/cancellation';17import { Event } from '../../src/util/vs/base/common/event';18import { Disposable } from '../../src/util/vs/base/common/lifecycle';19import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';2021const searchEndpoint = 'http://localhost:4443/api/embeddings/code/search';222324class SimulationGithubCodeSearchService extends Disposable implements IGithubCodeSearchService {2526declare readonly _serviceBrand: undefined;272829constructor(30@IIgnoreService private readonly _ignoreService: IIgnoreService,31@ILogService private readonly _logService: ILogService,32) {33super();34}3536async lexicalSearch(_authOptions: { silent: boolean }, _scope: GithubCodeSearchScope, _query: string, _maxResults: number, _options: WorkspaceChunkSearchOptions, _telemetryInfo: TelemetryCorrelationId, _token: CancellationToken): Promise<LexicalCodeSearchResult> {37throw new Error('Method not implemented.');38}3940async semanticSearch(authOptions: { silent: boolean }, embeddingType: EmbeddingType, repo: GithubCodeSearchScope & { kind: 'repo' }, query: string, maxResults: number, options: WorkspaceChunkSearchOptions, _telemetryInfo: TelemetryCorrelationId, token: CancellationToken): Promise<SemanticCodeSearchResult> {41this._logService.trace(`SimulationGithubCodeSearchService::searchRepo(${repo.githubRepoId}, ${query})`);42const response = await fetch(searchEndpoint, {43method: 'POST',44headers: {45'Content-Type': 'application/json'46},47body: JSON.stringify({48scoping_query: `repo:msbench/workspace`,49prompt: query,50limit: maxResults51})52});5354if (!response.ok) {55this._logService.trace(`SimulationGithubCodeSearchService::searchRepo(${repo.githubRepoId}, ${query}) failed. status: ${response.status}`);56const body = await response.text();57throw new Error(`Error fetching index status: ${response.status} - ${body}`);58}5960const json: any = await response.json();6162const result = await parseGithubCodeSearchResponse(json, repo, { ...options, skipVerifyRepo: true }, this._ignoreService);63this._logService.trace(`SimulationGithubCodeSearchService::searchRepo(${repo.githubRepoId}, ${query}) success. Found ${result.chunks.length} chunks`);64return result;65}6667async getRemoteIndexState(authOptions: { silent: boolean }, githubRepoId: GithubRepoId, _telemetryInfo: TelemetryCorrelationId, token: CancellationToken): Promise<Result<RemoteCodeSearchIndexState, RemoteCodeSearchError>> {68return Result.ok({ status: RemoteCodeSearchIndexStatus.Ready, indexedCommit: 'HEAD' });69}7071triggerIndexing(authOptions: { silent: boolean }, triggerReason: 'auto' | 'manual' | 'tool', githubRepoId: GithubRepoId): Promise<Result<true, RemoteCodeSearchError>> {72throw new Error('Method not implemented.');73}74}757677export class SimulationCodeSearchChunkSearchService extends Disposable implements IWorkspaceChunkSearchService {78declare readonly _serviceBrand: undefined;7980private readonly _githubCodeSearchService: IGithubCodeSearchService;8182constructor(83@IInstantiationService instantiationService: IInstantiationService84) {85super();8687this._githubCodeSearchService = instantiationService.createInstance(SimulationGithubCodeSearchService);88}8990readonly onDidChangeIndexState = Event.None;9192getIndexState(): Promise<WorkspaceIndexState> {93throw new Error('Method not implemented.');94}9596async isAvailable(): Promise<boolean> {97return true;98}99100async searchFileChunks(sizing: WorkspaceChunkSearchSizing, query: WorkspaceChunkQuery, options: WorkspaceChunkSearchOptions, telemetryInfo: TelemetryCorrelationId, progress: Progress<ChatResponsePart> | undefined, token: CancellationToken): Promise<WorkspaceChunkSearchResult> {101const repo = new GithubRepoId('test-org', 'test-repo');102try {103const results = await this._githubCodeSearchService.semanticSearch({ silent: true }, EmbeddingType.text3small_512, {104kind: 'repo',105githubRepoId: repo,106indexedCommit: undefined,107localRepoRoot: undefined,108}, query.queryText, sizing.maxResults ?? 128, options, telemetryInfo, token);109return {110chunks: results.chunks,111};112} catch (error) {113console.error('Error searching repo:', error);114}115116return {117chunks: [],118};119}120121triggerIndexing(trigger: BuildIndexTriggerReason, _onProgress?: (message: string) => void, _telemetryInfo?: TelemetryCorrelationId, _token?: CancellationToken): Promise<Result<true, TriggerIndexingError>> {122throw new Error('Method not implemented.');123}124125deleteExternalIngestWorkspaceIndex(): Promise<void> {126return Promise.resolve();127}128129async *getDiagnosticsDump(): AsyncIterable<string> {130yield 'Simulation mode — no diagnostics available.';131}132}133134135