|
| 1 | +import { LanguageModelChatMessage, LanguageModelChatUserMessage, LanguageModelChatAssistantMessage, lm, Disposable, CancellationToken, LanguageModelChatRequestOptions } from "vscode"; |
| 2 | +import { instrumentSimpleOperation, sendInfo } from "vscode-extension-telemetry-wrapper"; |
| 3 | +import { logger } from "./utils"; |
| 4 | + |
| 5 | +export default class Copilot { |
| 6 | + public static readonly DEFAULT_END_MARK = '<|endofresponse|>'; |
| 7 | + public static readonly DEFAULT_MAX_ROUNDS = 10; |
| 8 | + public static readonly DEFAULT_MODEL = 'copilot-gpt-4'; |
| 9 | + public static readonly DEFAULT_MODEL_OPTIONS: LanguageModelChatRequestOptions = { modelOptions: {} }; |
| 10 | + public static readonly NOT_CANCELLABEL: CancellationToken = { isCancellationRequested: false, onCancellationRequested: () => Disposable.from() }; |
| 11 | + |
| 12 | + public constructor( |
| 13 | + private readonly systemMessagesOrSamples: LanguageModelChatMessage[], |
| 14 | + private readonly model: string = Copilot.DEFAULT_MODEL, |
| 15 | + private readonly modelOptions: LanguageModelChatRequestOptions = Copilot.DEFAULT_MODEL_OPTIONS, |
| 16 | + private readonly maxRounds: number = Copilot.DEFAULT_MAX_ROUNDS, |
| 17 | + private readonly endMark: string = Copilot.DEFAULT_END_MARK |
| 18 | + ) { |
| 19 | + } |
| 20 | + |
| 21 | + private async doSend( |
| 22 | + userMessage: string, |
| 23 | + modelOptions: LanguageModelChatRequestOptions = Copilot.DEFAULT_MODEL_OPTIONS, |
| 24 | + cancellationToken: CancellationToken = Copilot.NOT_CANCELLABEL |
| 25 | + ): Promise<string> { |
| 26 | + let answer: string = ''; |
| 27 | + let rounds: number = 0; |
| 28 | + const messages = [...this.systemMessagesOrSamples]; |
| 29 | + const _send = async (message: string): Promise<boolean> => { |
| 30 | + rounds++; |
| 31 | + logger.info(`User: \n`, message); |
| 32 | + messages.push(new LanguageModelChatUserMessage(message)); |
| 33 | + logger.info('Copilot: thinking...'); |
| 34 | + |
| 35 | + let rawAnswer: string = ''; |
| 36 | + try { |
| 37 | + const response = await lm.sendChatRequest(this.model, messages, modelOptions ?? this.modelOptions, cancellationToken); |
| 38 | + for await (const item of response.stream) { |
| 39 | + rawAnswer += item; |
| 40 | + } |
| 41 | + } catch (e) { |
| 42 | + logger.error(`Failed to send request to copilot`, e); |
| 43 | + throw new Error(`Failed to send request to copilot: ${e}`); |
| 44 | + } |
| 45 | + messages.push(new LanguageModelChatAssistantMessage(rawAnswer)); |
| 46 | + logger.info(`Copilot: \n`, rawAnswer); |
| 47 | + answer += rawAnswer; |
| 48 | + return answer.trim().endsWith(this.endMark); |
| 49 | + }; |
| 50 | + let complete: boolean = await _send(userMessage); |
| 51 | + while (!complete && rounds < this.maxRounds) { |
| 52 | + complete = await _send('continue where you left off.'); |
| 53 | + } |
| 54 | + logger.debug('rounds', rounds); |
| 55 | + sendInfo('java.copilot.sendRequest.rounds', { rounds: rounds }); |
| 56 | + return answer.replace(this.endMark, ""); |
| 57 | + } |
| 58 | + |
| 59 | + public async send( |
| 60 | + userMessage: string, |
| 61 | + modelOptions: LanguageModelChatRequestOptions = Copilot.DEFAULT_MODEL_OPTIONS, |
| 62 | + cancellationToken: CancellationToken = Copilot.NOT_CANCELLABEL |
| 63 | + ): Promise<string> { |
| 64 | + return instrumentSimpleOperation("java.copilot.sendRequest", this.doSend.bind(this))(userMessage, modelOptions, cancellationToken); |
| 65 | + } |
| 66 | +} |
0 commit comments