forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilotRemoteAgentUtils.ts
More file actions
64 lines (57 loc) · 2.68 KB
/
Copy pathcopilotRemoteAgentUtils.ts
File metadata and controls
64 lines (57 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { MAX_PROBLEM_STATEMENT_LENGTH } from './copilotApi';
import Logger from '../common/logger';
/**
* Truncation utility to ensure the problem statement sent to Copilot API is under the maximum length.
* Truncation is not ideal. The caller providing the prompt/context should be summarizing so this is a no-op whenever possible.
*
* @param prompt The final message submitted by the user
* @param context Any additional context collected by the caller (chat history, open files, etc...)
* @returns A complete 'problem statement' string that is under the maximum length, and a flag indicating if truncation occurred
*/
export function truncatePrompt(prompt: string, context?: string): { problemStatement: string; isTruncated: boolean } {
// Prioritize the userPrompt
// Take the last n characters that fit within the limit
if (prompt.length >= MAX_PROBLEM_STATEMENT_LENGTH) {
Logger.warn(`Truncation: Prompt length ${prompt.length} exceeds max of ${MAX_PROBLEM_STATEMENT_LENGTH}`);
prompt = prompt.slice(-MAX_PROBLEM_STATEMENT_LENGTH);
return { problemStatement: prompt, isTruncated: true };
}
if (context && (prompt.length + context.length >= MAX_PROBLEM_STATEMENT_LENGTH)) {
const availableLength = MAX_PROBLEM_STATEMENT_LENGTH - prompt.length - 2 /* new lines */;
Logger.warn(`Truncation: Combined prompt and context length ${prompt.length + context.length} exceeds max of ${MAX_PROBLEM_STATEMENT_LENGTH}`);
context = context.slice(-availableLength);
return {
problemStatement: prompt + (context ? `\n\n${context}` : ''),
isTruncated: true
};
}
// No truncation occurred
return {
problemStatement: prompt + (context ? `\n\n${context}` : ''),
isTruncated: false
};
}
export function extractTitle(prompt: string, context: string | undefined): string | undefined {
const fromTitle = () => {
if (!prompt) {
return;
}
if (prompt.length <= 20) {
return prompt;
}
return prompt.substring(0, 20) + '...';
};
const titleMatch = context?.match(/TITLE: \s*(.*)/i);
if (titleMatch && titleMatch[1]) {
return titleMatch[1].trim();
}
return fromTitle();
}
export function formatBodyPlaceholder(title: string | undefined): string {
return vscode.l10n.t('Coding agent has begun work on **{0}** and will update this pull request as work progresses.', title || vscode.l10n.t('your request'));
}