Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/inlineChatWorkspaceSearch.tsx
13404 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*--------------------------------------------------------------------------------------------*/45import { BasePromptElementProps, PromptElement, PromptSizing } from '@vscode/prompt-tsx';6import { TelemetryCorrelationId } from '../../../../util/common/telemetryCorrelationId';7import { Diagnostic } from '../../../../vscodeTypes';8import { IDocumentContext } from '../../../prompt/node/documentContext';9import { ChunksToolProps, WorkspaceChunks } from '../panel/workspace/workspaceContext';1011interface InlineChatWorkspaceSearchProps extends BasePromptElementProps {12readonly documentContext: IDocumentContext;13readonly diagnostics: Diagnostic[];14readonly useWorkspaceChunksFromSelection?: boolean;15readonly useWorkspaceChunksFromDiagnostics?: boolean;16}1718export class InlineChatWorkspaceSearch extends PromptElement<InlineChatWorkspaceSearchProps> {1920render(state: void, sizing: PromptSizing) {21const { useWorkspaceChunksFromSelection, useWorkspaceChunksFromDiagnostics } = this.props;2223if (!useWorkspaceChunksFromSelection && !useWorkspaceChunksFromDiagnostics) {24return null;25}2627let tokenBudget = sizing.tokenBudget;28if (useWorkspaceChunksFromSelection && useWorkspaceChunksFromDiagnostics) {29tokenBudget = tokenBudget / 2;30}31return (32<>33{useWorkspaceChunksFromSelection &&34<WorkspaceChunks {...this.getChunkSearchPropsForSelection()} />}35{useWorkspaceChunksFromDiagnostics &&36<WorkspaceChunks {...this.getChunkSearchPropsForDiagnostics(tokenBudget)} />}37</>38);39}4041private getChunkSearchPropsForSelection(): ChunksToolProps {42const { document, wholeRange } = this.props.documentContext;43let range = document.validateRange(wholeRange);44this.props.diagnostics.forEach(d => {45range = range.union(d.range);46});47const selectedText = document.getText(range);4849const query = [50`Please find code that is similar to the following code block:\n`,51'```',52selectedText,53'```'54].join('\n');55return {56telemetryInfo: new TelemetryCorrelationId('InlineChatWorkspaceSearch::getChunkSearchPropsForSelection'),57query: {58queryText: query59},60// do not return matches in the current file61globPatterns: { exclude: [document.uri.fsPath] }, // TODO: use relativePattern once supported62maxResults: 3,63};64}6566private getChunkSearchPropsForDiagnostics(tokenBudget: number): ChunksToolProps {67const document = this.props.documentContext.document;68const messages = this.props.diagnostics.map(d => d.message).join(' ');69const query = `Please find code that can help me fix the following problems: ${messages}`;70return {71telemetryInfo: new TelemetryCorrelationId('InlineChatWorkspaceSearch::getChunkSearchPropsForDiagnostics'),72query: {73queryText: query74},75// do not return matches in the current file76globPatterns: { exclude: [document.uri.fsPath] },77maxResults: 3,78};79}80}818283