Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCODE-640: Adapt message content access to latest vscode API #857

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
},
{
"name": "Run Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceFolder}/out/test/suite", // TODO: VSCODE-641 - remove suite
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite"
],
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"preLaunchTask": "npm: compile:extension",
}
],
"compounds": [
Expand Down
21 changes: 12 additions & 9 deletions src/participant/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { LoadedConnection } from '../storage/connectionStorage';
import EXTENSION_COMMANDS from '../commands';
import type { StorageController } from '../storage';
import { StorageVariables } from '../storage';
import { Prompts } from './prompts';
import { getContentLength, Prompts } from './prompts';
import type { ChatResult } from './constants';
import {
askToConnectChatResult,
Expand Down Expand Up @@ -189,7 +189,7 @@ export default class ParticipantController {
(message: vscode.LanguageModelChatMessage) =>
util.inspect({
role: message.role,
contentLength: message.content.length,
contentLength: getContentLength(message),
})
),
});
Expand Down Expand Up @@ -790,15 +790,18 @@ export default class ParticipantController {
// it currently errors (not on insiders, only main VSCode).
// Here we're defaulting to have some content as a workaround.
// TODO: Remove this when the issue is fixed.
messagesWithNamespace.messages[
messagesWithNamespace.messages.length - 1
// eslint-disable-next-line new-cap
] = vscode.LanguageModelChatMessage.User(
if (
!Prompts.doMessagesContainUserInput([
messagesWithNamespace.messages[
messagesWithNamespace.messages.length - 1
],
])
) {
messagesWithNamespace.messages[
messagesWithNamespace.messages.length - 1
].content.trim() || 'see previous messages'
);

// eslint-disable-next-line new-cap
] = vscode.LanguageModelChatMessage.User('see previous messages');
}
const responseContentWithNamespace = await this.getChatResponseContent({
modelInput: messagesWithNamespace,
token,
Expand Down
5 changes: 4 additions & 1 deletion src/participant/prompts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { IntentPrompt } from './intent';
import { NamespacePrompt } from './namespace';
import { QueryPrompt } from './query';
import { SchemaPrompt } from './schema';
import { isContentEmpty } from './promptBase';

export { getContentLength } from './promptBase';

export class Prompts {
public static generic = new GenericPrompt();
Expand All @@ -26,7 +29,7 @@ export class Prompts {
for (const message of messages) {
if (
message.role === vscode.LanguageModelChatMessageRole.User &&
message.content.trim().length > 0
!isContentEmpty(message)
) {
return true;
}
Expand Down
48 changes: 47 additions & 1 deletion src/participant/prompts/promptBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@ export interface ModelInput {
stats: ParticipantPromptProperties;
}

export function getContentLength(
message: vscode.LanguageModelChatMessage
): number {
const content = message.content as any;
if (typeof content === 'string') {
return content.trim().length;
}

// TODO: https://github.com/microsoft/vscode/pull/231788 made it so message.content is no longer a string,
// but an array of things that a message can contain. This will eventually be reflected in the type definitions
// but until then, we're manually checking the array contents to ensure we don't break when this PR gets released
// in the stable channel.
if (Array.isArray(content)) {
return content.reduce((acc: number, element) => {
const value = element?.value ?? element?.content?.value;
if (typeof value === 'string') {
return acc + value.length;
}

return acc;
}, 0);
}

return 0;
}

export function isContentEmpty(
message: vscode.LanguageModelChatMessage
): boolean {
const content = message.content as any;
if (typeof content === 'string') {
return content.trim().length === 0;
}

if (Array.isArray(content)) {
for (const element of content) {
const value = element?.value ?? element?.content?.value;
if (typeof value === 'string' && value.trim().length > 0) {
return false;
}
}
}

return true;
}

export abstract class PromptBase<TArgs extends PromptArgsBase> {
protected abstract getAssistantPrompt(args: TArgs): string;

Expand Down Expand Up @@ -92,7 +138,7 @@ export abstract class PromptBase<TArgs extends PromptArgsBase> {
): ParticipantPromptProperties {
return {
total_message_length: messages.reduce(
(acc, message) => acc + message.content.length,
(acc, message) => acc + getContentLength(message),
0
),
user_input_length: request.prompt.length,
Expand Down
Loading
Loading