Skip to content

Commit ee7bea7

Browse files
committed
renderChatMessage, return ContextItem[] from tools, and other refactoring
1 parent a7cd760 commit ee7bea7

53 files changed

Lines changed: 499 additions & 320 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/commands/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CustomCommand, SlashCommand, SlashCommandDescription } from "../";
2-
import { stripImages } from "../llm/images";
32
import { renderTemplatedString } from "../promptFiles/v1/renderTemplatedString";
3+
import { renderChatMessage } from "../util/messageContent";
44

55
import SlashCommands from "./slash";
66

@@ -29,7 +29,8 @@ export function slashFromCustomCommand(
2929
const messages = [...history];
3030
// Find the last chat message with this slash command and replace it with the user input
3131
for (let i = messages.length - 1; i >= 0; i--) {
32-
const { role, content } = messages[i];
32+
const message = messages[i];
33+
const { role, content } = message;
3334
if (role !== "user") {
3435
continue;
3536
}
@@ -41,7 +42,7 @@ export function slashFromCustomCommand(
4142
)
4243
) {
4344
messages[i] = {
44-
...messages[i],
45+
...message,
4546
content: content.map((part) => {
4647
return part.text?.startsWith(`/${customCommand.name}`)
4748
? { ...part, text: promptUserInput }
@@ -53,13 +54,16 @@ export function slashFromCustomCommand(
5354
typeof content === "string" &&
5455
content.startsWith(`/${customCommand.name}`)
5556
) {
56-
messages[i] = { ...messages[i], content: promptUserInput };
57+
messages[i] = { ...message, content: promptUserInput };
5758
break;
5859
}
5960
}
6061

61-
for await (const chunk of llm.streamChat(messages, new AbortController().signal)) {
62-
yield stripImages(chunk.content);
62+
for await (const chunk of llm.streamChat(
63+
messages,
64+
new AbortController().signal,
65+
)) {
66+
yield renderChatMessage(chunk);
6367
}
6468
},
6569
};

core/commands/slash/comment.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

core/commands/slash/commit.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SlashCommand } from "../../index.js";
2-
import { stripImages } from "../../llm/images.js";
2+
import { renderChatMessage } from "../../util/messageContent.js";
33

44
const CommitMessageCommand: SlashCommand = {
55
name: "commit",
@@ -14,10 +14,11 @@ const CommitMessageCommand: SlashCommand = {
1414
}
1515

1616
const prompt = `${diff}\n\nGenerate a commit message for the above set of changes. First, give a single sentence, no more than 80 characters. Then, after 2 line breaks, give a list of no more than 5 short bullet points, each no more than 40 characters. Output nothing except for the commit message, and don't surround it in quotes.`;
17-
for await (const chunk of llm.streamChat([
18-
{ role: "user", content: prompt },
19-
], new AbortController().signal)) {
20-
yield stripImages(chunk.content);
17+
for await (const chunk of llm.streamChat(
18+
[{ role: "user", content: prompt }],
19+
new AbortController().signal,
20+
)) {
21+
yield renderChatMessage(chunk);
2122
}
2223
},
2324
};

core/commands/slash/draftIssue.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChatMessage, SlashCommand } from "../../index.js";
2-
import { stripImages } from "../../llm/images.js";
32
import { removeQuotesAndEscapes } from "../../util/index.js";
3+
import { renderChatMessage } from "../../util/messageContent.js";
44

55
const PROMPT = (
66
input: string,
@@ -30,7 +30,8 @@ const DraftIssueCommand: SlashCommand = {
3030
return;
3131
}
3232
let title = await llm.complete(
33-
`Generate a title for the GitHub issue requested in this user input: '${input}'. Use no more than 20 words and output nothing other than the title. Do not surround it with quotes. The title is: `, new AbortController().signal,
33+
`Generate a title for the GitHub issue requested in this user input: '${input}'. Use no more than 20 words and output nothing other than the title. Do not surround it with quotes. The title is: `,
34+
new AbortController().signal,
3435
{ maxTokens: 20 },
3536
);
3637

@@ -43,9 +44,12 @@ const DraftIssueCommand: SlashCommand = {
4344
{ role: "user", content: PROMPT(input, title) },
4445
];
4546

46-
for await (const chunk of llm.streamChat(messages, new AbortController().signal)) {
47+
for await (const chunk of llm.streamChat(
48+
messages,
49+
new AbortController().signal,
50+
)) {
4751
body += chunk.content;
48-
yield stripImages(chunk.content);
52+
yield renderChatMessage(chunk);
4953
}
5054

5155
const url = `${params.repositoryUrl}/issues/new?title=${encodeURIComponent(

core/commands/slash/multifileEdit.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SlashCommand } from "../..";
2-
import { stripImages } from "../../llm/images";
2+
import { renderChatMessage } from "../../util/messageContent";
33

44
const MultiFileEditSlashCommand: SlashCommand = {
55
name: "multifile-edit",
@@ -39,8 +39,11 @@ const MultiFileEditSlashCommand: SlashCommand = {
3939

4040
const content = createPrompt(filesToEditStr, additionalContextStr, input);
4141

42-
for await (const chunk of llm.streamChat([{ role: "user", content }], new AbortController().signal)) {
43-
yield stripImages(chunk.content);
42+
for await (const chunk of llm.streamChat(
43+
[{ role: "user", content }],
44+
new AbortController().signal,
45+
)) {
46+
yield renderChatMessage(chunk);
4447
}
4548
},
4649
};

core/commands/slash/onboard.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
defaultIgnoreFile,
1010
gitIgArrayFromFile,
1111
} from "../../indexing/ignore";
12-
import { stripImages } from "../../llm/images";
12+
import { renderChatMessage } from "../../util/messageContent";
1313

1414
const LANGUAGE_DEP_MGMT_FILENAMES = [
1515
"package.json", // JavaScript (Node.js)
@@ -45,10 +45,11 @@ const OnboardSlashCommand: SlashCommand = {
4545
const context = await gatherProjectContext(workspaceDir, ide);
4646
const prompt = createOnboardingPrompt(context);
4747

48-
for await (const chunk of llm.streamChat([
49-
{ role: "user", content: prompt },
50-
], new AbortController().signal)) {
51-
yield stripImages(chunk.content);
48+
for await (const chunk of llm.streamChat(
49+
[{ role: "user", content: prompt }],
50+
new AbortController().signal,
51+
)) {
52+
yield renderChatMessage(chunk);
5253
}
5354
},
5455
};

core/commands/slash/review.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChatMessage, SlashCommand } from "../../index.js";
2-
import { stripImages } from "../../llm/images.js";
2+
import { renderChatMessage } from "../../util/messageContent.js";
33

44
const prompt = `
55
Review the following code, focusing on Readability, Maintainability, Code Smells, Speed, and Memory Performance. Provide feedback with these guidelines:
@@ -43,10 +43,11 @@ const ReviewMessageCommand: SlashCommand = {
4343

4444
const content = `${prompt} \r\n ${reviewText}`;
4545

46-
for await (const chunk of llm.streamChat([
47-
{ role: "user", content: content },
48-
], new AbortController().signal)) {
49-
yield stripImages(chunk.content);
46+
for await (const chunk of llm.streamChat(
47+
[{ role: "user", content: content }],
48+
new AbortController().signal,
49+
)) {
50+
yield renderChatMessage(chunk);
5051
}
5152
},
5253
};

core/commands/slash/share.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "path";
44

55
import { languageForFilepath } from "../../autocomplete/constants/AutocompleteLanguageInfo.js";
66
import { SlashCommand } from "../../index.js";
7-
import { stripImages } from "../../llm/images.js";
7+
import { renderChatMessage } from "../../util/messageContent.js";
88

99
// If useful elsewhere, helper funcs should move to core/util/index.ts or similar
1010
function getOffsetDatetime(date: Date): Date {
@@ -48,7 +48,7 @@ const ShareSlashCommand: SlashCommand = {
4848
// message in the chat history, this will omit it
4949
for (const msg of history.slice(0, history.length - 1)) {
5050
let msgText = msg.content;
51-
msgText = stripImages(msg.content);
51+
msgText = renderChatMessage(msg);
5252

5353
if (msg.role === "user" && msgText.search("```") > -1) {
5454
msgText = reformatCodeBlocks(msgText);

core/config/promptFile.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import Handlebars from "handlebars";
44
import * as YAML from "yaml";
55

66
import { walkDir } from "../indexing/walkDir";
7-
import { stripImages } from "../llm/images";
87
import { renderTemplatedString } from "../promptFiles/v1/renderTemplatedString";
98
import { getBasename } from "../util/index";
9+
import { renderChatMessage } from "../util/messageContent";
1010

1111
import type {
1212
ChatMessage,
@@ -125,8 +125,11 @@ export function slashCommandFromPromptFile(
125125
systemMessage,
126126
);
127127

128-
for await (const chunk of context.llm.streamChat(messages, new AbortController().signal)) {
129-
yield stripImages(chunk.content);
128+
for await (const chunk of context.llm.streamChat(
129+
messages,
130+
new AbortController().signal,
131+
)) {
132+
yield renderChatMessage(chunk);
130133
}
131134

132135
context.llm.systemMessage = originalSystemMessage;
@@ -233,7 +236,8 @@ function updateChatHistory(
233236
const messages = [...history];
234237

235238
for (let i = messages.length - 1; i >= 0; i--) {
236-
const { role, content } = messages[i];
239+
const message = messages[i];
240+
const { role, content } = message;
237241
if (role !== "user") {
238242
continue;
239243
}
@@ -251,7 +255,7 @@ function updateChatHistory(
251255
typeof content === "string" &&
252256
content.startsWith(`/${commandName}`)
253257
) {
254-
messages[i] = { ...messages[i], content: renderedPrompt };
258+
messages[i] = { ...message, content: renderedPrompt };
255259
break;
256260
}
257261
}

core/context/retrieval/repoMapRequest.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Chunk, ContinueConfig, IDE, ILLM } from "../..";
22
import { getModelByRole } from "../../config/util";
3-
import { stripImages } from "../../llm/images";
43
import generateRepoMap from "../../util/generateRepoMap";
4+
import { renderChatMessage } from "../../util/messageContent";
55

66
const SUPPORTED_MODEL_TITLE_FAMILIES = [
77
"claude-3",
@@ -58,11 +58,14 @@ After this, your response should begin with a <results> tag, followed by a list
5858
5959
This is the question that you should select relevant files for: "${input}"`;
6060

61-
const response = await llm.chat([
62-
{ role: "user", content: prompt },
63-
{ role: "assistant", content: "<reasoning>" },
64-
], new AbortController().signal);
65-
const content = stripImages(response.content);
61+
const response = await llm.chat(
62+
[
63+
{ role: "user", content: prompt },
64+
{ role: "assistant", content: "<reasoning>" },
65+
],
66+
new AbortController().signal,
67+
);
68+
const content = renderChatMessage(response);
6669
console.debug("Repo map retrieval response: ", content);
6770

6871
if (!content.includes("\n")) {

0 commit comments

Comments
 (0)