|
| 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 | + |
| 6 | +import { timeout } from '../../../../../base/common/async.js'; |
| 7 | +import { CancellationToken } from '../../../../../base/common/cancellation.js'; |
| 8 | +import { MarkdownString } from '../../../../../base/common/htmlContent.js'; |
| 9 | +import { localize } from '../../../../../nls.js'; |
| 10 | +import { ExtensionIdentifier } from '../../../../../platform/extensions/common/extensions.js'; |
| 11 | +import { ChatElicitationRequestPart } from '../../../chat/browser/chatElicitationRequestPart.js'; |
| 12 | +import { ChatModel } from '../../../chat/common/chatModel.js'; |
| 13 | +import { IChatService } from '../../../chat/common/chatService.js'; |
| 14 | +import { ChatMessageRole, ILanguageModelsService } from '../../../chat/common/languageModels.js'; |
| 15 | +import { IToolInvocationContext } from '../../../chat/common/languageModelToolsService.js'; |
| 16 | +import { ITerminalInstance } from '../../../terminal/browser/terminal.js'; |
| 17 | +import type { IMarker as IXtermMarker } from '@xterm/xterm'; |
| 18 | + |
| 19 | +const enum PollingConsts { |
| 20 | + MinNoDataEvents = 2, // Minimum number of no data checks before considering the terminal idle |
| 21 | + MinPollingDuration = 500, |
| 22 | + FirstPollingMaxDuration = 20000, // 20 seconds |
| 23 | + ExtendedPollingMaxDuration = 120000, // 2 minutes |
| 24 | + MaxPollingIntervalDuration = 2000, // 2 seconds |
| 25 | +} |
| 26 | + |
| 27 | +export function getOutput(instance: ITerminalInstance, startMarker?: IXtermMarker): string { |
| 28 | + if (!instance.xterm || !instance.xterm.raw) { |
| 29 | + return ''; |
| 30 | + } |
| 31 | + const lines: string[] = []; |
| 32 | + for (let y = Math.min(startMarker?.line ?? 0, 0); y < instance.xterm!.raw.buffer.active.length; y++) { |
| 33 | + const line = instance.xterm!.raw.buffer.active.getLine(y); |
| 34 | + if (!line) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + lines.push(line.translateToString(true)); |
| 38 | + } |
| 39 | + return lines.join('\n'); |
| 40 | +} |
| 41 | + |
| 42 | +export async function pollForOutputAndIdle( |
| 43 | + execution: { getOutput: () => string }, |
| 44 | + extendedPolling: boolean, |
| 45 | + token: CancellationToken, |
| 46 | + languageModelsService: ILanguageModelsService, |
| 47 | +): Promise<{ terminalExecutionIdleBeforeTimeout: boolean; output: string; pollDurationMs?: number }> { |
| 48 | + const maxWaitMs = extendedPolling ? PollingConsts.ExtendedPollingMaxDuration : PollingConsts.FirstPollingMaxDuration; |
| 49 | + const maxInterval = PollingConsts.MaxPollingIntervalDuration; |
| 50 | + let currentInterval = PollingConsts.MinPollingDuration; |
| 51 | + const pollStartTime = Date.now(); |
| 52 | + |
| 53 | + let lastBufferLength = 0; |
| 54 | + let noNewDataCount = 0; |
| 55 | + let buffer = ''; |
| 56 | + let terminalExecutionIdleBeforeTimeout = false; |
| 57 | + |
| 58 | + while (true) { |
| 59 | + if (token.isCancellationRequested) { |
| 60 | + break; |
| 61 | + } |
| 62 | + const now = Date.now(); |
| 63 | + const elapsed = now - pollStartTime; |
| 64 | + const timeLeft = maxWaitMs - elapsed; |
| 65 | + |
| 66 | + if (timeLeft <= 0) { |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + // Cap the wait so we never overshoot timeLeft |
| 71 | + const waitTime = Math.min(currentInterval, timeLeft); |
| 72 | + await timeout(waitTime); |
| 73 | + |
| 74 | + // Check again immediately after waking |
| 75 | + if (Date.now() - pollStartTime >= maxWaitMs) { |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + currentInterval = Math.min(currentInterval * 2, maxInterval); |
| 80 | + |
| 81 | + buffer = execution.getOutput(); |
| 82 | + const currentBufferLength = buffer.length; |
| 83 | + |
| 84 | + if (currentBufferLength === lastBufferLength) { |
| 85 | + noNewDataCount++; |
| 86 | + } else { |
| 87 | + noNewDataCount = 0; |
| 88 | + lastBufferLength = currentBufferLength; |
| 89 | + } |
| 90 | + const isLikelyFinished = await assessOutputForFinishedState(buffer, token, languageModelsService); |
| 91 | + terminalExecutionIdleBeforeTimeout = isLikelyFinished && noNewDataCount >= PollingConsts.MinNoDataEvents; |
| 92 | + if (terminalExecutionIdleBeforeTimeout) { |
| 93 | + return { terminalExecutionIdleBeforeTimeout, output: buffer, pollDurationMs: Date.now() - pollStartTime + (extendedPolling ? PollingConsts.FirstPollingMaxDuration : 0) }; |
| 94 | + } |
| 95 | + } |
| 96 | + return { terminalExecutionIdleBeforeTimeout, output: buffer, pollDurationMs: Date.now() - pollStartTime + (extendedPolling ? PollingConsts.FirstPollingMaxDuration : 0) }; |
| 97 | +} |
| 98 | + |
| 99 | +export async function promptForMorePolling(command: string, execution: { getOutput: () => string }, token: CancellationToken, context: IToolInvocationContext, chatService: IChatService): Promise<boolean> { |
| 100 | + const chatModel = chatService.getSession(context.sessionId); |
| 101 | + if (chatModel instanceof ChatModel) { |
| 102 | + const request = chatModel.getRequests().at(-1); |
| 103 | + if (request) { |
| 104 | + const waitPromise = new Promise<boolean>(resolve => { |
| 105 | + const part = new ChatElicitationRequestPart( |
| 106 | + new MarkdownString(localize('poll.terminal.waiting', "Continue waiting for `{0}` to finish?", command)), |
| 107 | + new MarkdownString(localize('poll.terminal.polling', "Copilot will continue to poll for output to determine when the terminal becomes idle for up to 2 minutes.")), |
| 108 | + '', |
| 109 | + localize('poll.terminal.accept', 'Yes'), |
| 110 | + localize('poll.terminal.reject', 'No'), |
| 111 | + async () => { |
| 112 | + resolve(true); |
| 113 | + }, |
| 114 | + async () => { |
| 115 | + resolve(false); |
| 116 | + } |
| 117 | + ); |
| 118 | + chatModel.acceptResponseProgress(request, part); |
| 119 | + }); |
| 120 | + return waitPromise; |
| 121 | + } |
| 122 | + } |
| 123 | + return false; // Fallback to not waiting if we can't prompt the user |
| 124 | +} |
| 125 | + |
| 126 | +export async function assessOutputForFinishedState(buffer: string, token: CancellationToken, languageModelsService: ILanguageModelsService): Promise<boolean> { |
| 127 | + const models = await languageModelsService.selectLanguageModels({ vendor: 'copilot', family: 'gpt-4o-mini' }); |
| 128 | + if (!models.length) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + const response = await languageModelsService.sendChatRequest(models[0], new ExtensionIdentifier('Github.copilot-chat'), [{ role: ChatMessageRole.Assistant, content: [{ type: 'text', value: `Evaluate this terminal output to determine if the command is finished or still in process: ${buffer}. Return the word true if finished and false if still in process.` }] }], {}, token); |
| 133 | + |
| 134 | + let responseText = ''; |
| 135 | + |
| 136 | + const streaming = (async () => { |
| 137 | + for await (const part of response.stream) { |
| 138 | + if (Array.isArray(part)) { |
| 139 | + for (const p of part) { |
| 140 | + if (p.part.type === 'text') { |
| 141 | + responseText += p.part.value; |
| 142 | + } |
| 143 | + } |
| 144 | + } else if (part.part.type === 'text') { |
| 145 | + responseText += part.part.value; |
| 146 | + } |
| 147 | + } |
| 148 | + })(); |
| 149 | + |
| 150 | + try { |
| 151 | + await Promise.all([response.result, streaming]); |
| 152 | + return responseText.includes('true'); |
| 153 | + } catch (err) { |
| 154 | + return false; |
| 155 | + } |
| 156 | +} |
0 commit comments