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

Adopt chat-extension-utils #86

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 13 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"displayName": "vscode-mermAId",
"description": "Exploration into Copilot Chat-powered Diagram Generation",
"repository": {
"type": "git",
"url": "https://github.com/microsoft/vscode-mermAId.git"
},
"type": "git",
"url": "https://github.com/microsoft/vscode-mermAId.git"
},
"version": "0.0.1",
"icon": "assets/logo.png",
"engines": {
Expand Down Expand Up @@ -91,7 +91,8 @@
"name": "mermAId_get_symbol_definition",
"tags": [
"code",
"symbols"
"symbols",
"mermaid"
],
"displayName": "Symbol Definitions from File",
"modelDescription": "Given a file path string and a list of symbols, this model returns the definitions of the specified symbols. For example, if the file 'x.py' is provided and the symbol 'abc' is requested, the model will find 'abc' in 'x.py' and return its definition from the file where it is actually defined, such as 'y.py'.",
Expand Down Expand Up @@ -124,7 +125,8 @@
"name": "mermAId_gather_symbols",
"tags": [
"code",
"symbols"
"symbols",
"mermaid"
],
"displayName": "Gather Workspace Symbols",
"modelDescription": "This tool will accept a list of symbols and attempt to gather information about those symbols from the entire workspace. It can provide the location, parent symbol, and the the symbol's content.",
Expand Down Expand Up @@ -290,6 +292,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@vscode/chat-extension-utils": "^0.0.0-alpha.1",
"@vscode/codicons": "^0.0.36",
"groq-sdk": "^0.7.0",
"mermaid": "^11.3.0"
Expand Down
70 changes: 0 additions & 70 deletions src/chat/chatHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,7 @@
import * as vscode from 'vscode';
import { MermaidPrompt, MermaidProps } from './mermaidPrompt';
import { ChatMessage, ChatRole, HTMLTracer, PromptRenderer } from '@vscode/prompt-tsx';

export interface IToolCall {
tool: vscode.LanguageModelToolInformation;
call: vscode.LanguageModelToolCallPart;
result: Thenable<vscode.LanguageModelToolResult>;
}


export async function getContextMessage(references: ReadonlyArray<vscode.ChatPromptReference>): Promise<string> {
const contextParts = (await Promise.all(references.map(async ref => {
if (ref.value instanceof vscode.Uri) {
const fileContents = (await vscode.workspace.fs.readFile(ref.value)).toString();
return `${ref.value.fsPath}:\n\`\`\`\n${fileContents}\n\`\`\``;
} else if (ref.value instanceof vscode.Location) {
const rangeText = (await vscode.workspace.openTextDocument(ref.value.uri)).getText(ref.value.range);
return `${ref.value.uri.fsPath}:${ref.value.range.start.line + 1}-${ref.value.range.end.line + 1}\n\`\`\`${rangeText}\`\`\``;
} else if (typeof ref.value === 'string') {
return ref.value;
}
return null;
}))).filter(part => part !== null) as string[];

const context = contextParts
.map(part => `<context>\n${part}\n</context>`)
.join('\n');
return `The user has provided these references:\n${context}`;
}

export async function getHistoryMessages(context: vscode.ChatContext): Promise<vscode.LanguageModelChatMessage[]> {
const messages: vscode.LanguageModelChatMessage[] = [];
for (const message of context.history) {
if (message instanceof vscode.ChatRequestTurn) {
if (message.references.length) {
messages.push(vscode.LanguageModelChatMessage.User(await getContextMessage(message.references)));
}
messages.push(vscode.LanguageModelChatMessage.User(message.prompt));
} else if (message instanceof vscode.ChatResponseTurn) {
const strResponse = message.response.map(part => {
if (part instanceof vscode.ChatResponseMarkdownPart) {
return part.value.value;
} else if (part instanceof vscode.ChatResponseAnchorPart) {
if (part.value instanceof vscode.Location) {
return ` ${part.value.uri.fsPath}:${part.value.range.start.line}-${part.value.range.end.line} `;
} else if (part.value instanceof vscode.Uri) {
return ` ${part.value.fsPath} `;
}
}
}).join('');
messages.push(vscode.LanguageModelChatMessage.Assistant(strResponse));
}
}

return messages;
}

export async function renderMessages(chat: vscode.LanguageModelChat, props: MermaidProps, stream: vscode.ChatResponseStream, serveTrace: boolean) {
const renderer = new PromptRenderer({ modelMaxPromptTokens: chat.maxInputTokens }, MermaidPrompt, props, {
tokenLength: async (text, _token) => {
return chat.countTokens(text);
},
countMessageTokens: async (message: ChatMessage) => {
return chat.countTokens(message.content);
}
});
const tracer = new HTMLTracer();
renderer.tracer = tracer;
const result = await renderer.render();
if (serveTrace) {
const server = await tracer.serveHTML();
console.log('Server address:', server.address);
const serverUri = vscode.Uri.parse(server.address);
}
return result;
}
Loading