fix rules not being included in agent mode
This commit is contained in:
parent
bfb4d41324
commit
afd7244548
9 changed files with 90 additions and 55 deletions
|
|
@ -4,6 +4,7 @@ import {
|
|||
ILLM,
|
||||
Prediction,
|
||||
RuleWithSource,
|
||||
ToolResultChatMessage,
|
||||
UserChatMessage,
|
||||
} from "../";
|
||||
import {
|
||||
|
|
@ -104,15 +105,16 @@ export async function* streamDiffLines({
|
|||
|
||||
// Rules will be included with edit prompt
|
||||
// If any rules are present this will result in using chat instead of legacy completion
|
||||
const lastUserMessage: UserChatMessage | undefined =
|
||||
const lastUserMessage =
|
||||
typeof prompt === "string"
|
||||
? {
|
||||
? ({
|
||||
role: "user",
|
||||
content: prompt,
|
||||
}
|
||||
: (findLast(prompt, (msg) => msg.role === "user") as
|
||||
| UserChatMessage
|
||||
| undefined);
|
||||
} as UserChatMessage)
|
||||
: (findLast(
|
||||
prompt,
|
||||
(msg) => msg.role === "user" || msg.role === "tool",
|
||||
) as UserChatMessage | ToolResultChatMessage | undefined);
|
||||
|
||||
const systemMessage = getSystemMessageWithRules({
|
||||
currentModel: llm.model,
|
||||
|
|
|
|||
|
|
@ -3,10 +3,12 @@ import {
|
|||
ChatMessage,
|
||||
RuleWithSource,
|
||||
TextMessagePart,
|
||||
ToolResultChatMessage,
|
||||
UserChatMessage,
|
||||
} from "../";
|
||||
import { findLast } from "../util/findLast";
|
||||
import { normalizeToMessageParts } from "../util/messageContent";
|
||||
import { isUserOrToolMsg } from "./messages";
|
||||
import { getSystemMessageWithRules } from "./rules/getSystemMessageWithRules";
|
||||
|
||||
export const DEFAULT_CHAT_SYSTEM_MESSAGE_URL =
|
||||
|
|
@ -96,13 +98,15 @@ export function constructMessages(
|
|||
}
|
||||
}
|
||||
|
||||
const userMessage = findLast(msgs, (msg) => msg.role === "user") as
|
||||
const lastUserMsg = findLast(msgs, (msg) => isUserOrToolMsg) as
|
||||
| UserChatMessage
|
||||
| ToolResultChatMessage
|
||||
| undefined;
|
||||
|
||||
const systemMessage = getSystemMessageWithRules({
|
||||
baseSystemMessage: baseChatSystemMessage ?? DEFAULT_CHAT_SYSTEM_MESSAGE,
|
||||
rules,
|
||||
userMessage,
|
||||
userMessage: lastUserMsg,
|
||||
currentModel: modelName,
|
||||
});
|
||||
if (systemMessage.trim()) {
|
||||
|
|
@ -112,8 +116,6 @@ export function constructMessages(
|
|||
});
|
||||
}
|
||||
|
||||
// Note, last assistant message is removed on core side
|
||||
|
||||
// Remove the "id" from all of the messages
|
||||
return msgs.map((msg) => {
|
||||
const { id, ...rest } = msg as any;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { Tiktoken, encodingForModel as _encodingForModel } from "js-tiktoken";
|
|||
|
||||
import { ChatMessage, MessageContent, MessagePart, Tool } from "../index.js";
|
||||
|
||||
import { findLast } from "../util/findLast.js";
|
||||
import { renderChatMessage } from "../util/messageContent.js";
|
||||
import {
|
||||
AsyncEncoder,
|
||||
|
|
@ -15,6 +14,8 @@ import {
|
|||
addSpaceToAnyEmptyMessages,
|
||||
chatMessageIsEmpty,
|
||||
flattenMessages,
|
||||
isUserOrToolMsg,
|
||||
messageHasToolCallId,
|
||||
} from "./messages.js";
|
||||
interface Encoding {
|
||||
encode: Tiktoken["encode"];
|
||||
|
|
@ -328,29 +329,47 @@ function compileChatMessages({
|
|||
}
|
||||
}
|
||||
|
||||
const lastUserMsg = findLast(msgsCopy, (msg) => msg.role === "user");
|
||||
if (!lastUserMsg) {
|
||||
throw new Error("Error parsing chat history: no user message found"); // should never happen
|
||||
}
|
||||
// Extract system message
|
||||
const systemMsg = msgsCopy.find((msg) => msg.role === "system");
|
||||
msgsCopy = msgsCopy.filter((msg) => msg.role !== "system");
|
||||
|
||||
// Remove any empty messages or non-user/tool trailing messages
|
||||
msgsCopy = msgsCopy.filter((msg) => !chatMessageIsEmpty(msg));
|
||||
|
||||
msgsCopy = addSpaceToAnyEmptyMessages(msgsCopy);
|
||||
|
||||
// Remove any last messages that aren't user
|
||||
// Then pop the user message too since we've already grabbed it
|
||||
while (msgsCopy.at(-1)?.role !== "user") {
|
||||
while (msgsCopy.length > 1) {
|
||||
if (isUserOrToolMsg(msgsCopy.at(-1))) {
|
||||
break;
|
||||
}
|
||||
msgsCopy.pop();
|
||||
}
|
||||
msgsCopy.pop();
|
||||
|
||||
const lastUserOrToolMsg = msgsCopy.pop();
|
||||
if (!lastUserOrToolMsg || !isUserOrToolMsg(lastUserOrToolMsg)) {
|
||||
throw new Error("Error parsing chat history: no user/tool message found"); // should never happen
|
||||
}
|
||||
|
||||
let lastToolCallsMsg: ChatMessage | undefined = undefined;
|
||||
if (lastUserOrToolMsg.role === "tool") {
|
||||
lastToolCallsMsg = msgsCopy.pop();
|
||||
if (!messageHasToolCallId(lastToolCallsMsg, lastUserOrToolMsg.toolCallId)) {
|
||||
throw new Error(
|
||||
`Error parsing chat history: tool call found to match tool output for id ${lastUserOrToolMsg.toolCallId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let lastMessagesTokens = countChatMessageTokens(modelName, lastUserOrToolMsg);
|
||||
if (lastToolCallsMsg) {
|
||||
lastMessagesTokens += countChatMessageTokens(modelName, lastToolCallsMsg);
|
||||
}
|
||||
|
||||
// System message
|
||||
let systemMsgTokens = 0;
|
||||
const systemMsg = msgsCopy.find((msg) => msg.role === "system");
|
||||
if (systemMsg) {
|
||||
systemMsgTokens = countChatMessageTokens(modelName, systemMsg);
|
||||
}
|
||||
msgsCopy = msgsCopy.filter((msg) => msg.role !== "system");
|
||||
|
||||
// Tools
|
||||
let toolTokens = 0;
|
||||
|
|
@ -358,9 +377,6 @@ function compileChatMessages({
|
|||
toolTokens = countToolsTokens(tools, modelName);
|
||||
}
|
||||
|
||||
// Last user message
|
||||
const lastUserMsgTokens = countChatMessageTokens(modelName, lastUserMsg);
|
||||
|
||||
const countingSafetyBuffer = getTokenCountingBufferSafety(contextLength);
|
||||
const minOutputTokens = getMinResponseTokens(maxTokens);
|
||||
let inputTokensAvailable =
|
||||
|
|
@ -368,20 +384,20 @@ function compileChatMessages({
|
|||
|
||||
inputTokensAvailable -= toolTokens;
|
||||
inputTokensAvailable -= systemMsgTokens;
|
||||
inputTokensAvailable -= lastUserMsgTokens;
|
||||
inputTokensAvailable -= lastMessagesTokens;
|
||||
|
||||
// Make sure there's enough context for the non-excludable items
|
||||
if (inputTokensAvailable < 0) {
|
||||
throw new Error(
|
||||
`Not enough context available to include the system message, last user message, and tools.
|
||||
There must be at least ${minOutputTokens} tokens remaining for output.
|
||||
Request had the following properties:
|
||||
Request had the following token counts:
|
||||
- contextLength: ${contextLength}
|
||||
- token counting safety buffer: ${countingSafetyBuffer}
|
||||
- toolTokens: ~${toolTokens}
|
||||
- systemMsgTokens: ~${systemMsgTokens}
|
||||
- lastUserMsgTokens: ~${lastUserMsgTokens}
|
||||
- maxTokens: ${maxTokens}`,
|
||||
- counting safety buffer: ${countingSafetyBuffer}
|
||||
- tools: ~${toolTokens}
|
||||
- system message: ~${systemMsgTokens}
|
||||
- last user or tool + tool call message tokens: ~${lastMessagesTokens}
|
||||
- max output tokens: ${maxTokens}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -398,27 +414,28 @@ function compileChatMessages({
|
|||
|
||||
while (historyWithTokens.length > 0 && currentTotal > inputTokensAvailable) {
|
||||
const message = historyWithTokens.shift()!;
|
||||
currentTotal -= message.tokens;
|
||||
|
||||
// At this point make sure no latent tool response without corresponding call
|
||||
while (historyWithTokens[0]?.role === "tool") {
|
||||
const message = historyWithTokens.shift()!;
|
||||
currentTotal -= message.tokens;
|
||||
}
|
||||
|
||||
currentTotal -= message.tokens;
|
||||
}
|
||||
|
||||
// Now reassemble
|
||||
const reassembled: ChatMessage[] = [
|
||||
...historyWithTokens.map(({ tokens, ...rest }) => rest),
|
||||
lastUserMsg,
|
||||
];
|
||||
const reassembled: ChatMessage[] = [];
|
||||
if (systemMsg) {
|
||||
reassembled.unshift(systemMsg);
|
||||
reassembled.push(systemMsg);
|
||||
}
|
||||
reassembled.push(...historyWithTokens.map(({ tokens, ...rest }) => rest));
|
||||
if (lastToolCallsMsg) {
|
||||
reassembled.push(lastToolCallsMsg);
|
||||
}
|
||||
reassembled.push(lastUserOrToolMsg);
|
||||
|
||||
// Flatten the messages (combines adjacent similar messages)
|
||||
const flattenedHistory = flattenMessages(reassembled);
|
||||
|
||||
return flattenedHistory;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,6 +49,31 @@ export function addSpaceToAnyEmptyMessages(
|
|||
});
|
||||
}
|
||||
|
||||
export function isUserOrToolMsg(msg: ChatMessage | undefined): boolean {
|
||||
if (!msg) {
|
||||
return false;
|
||||
}
|
||||
return msg.role === "user" || msg.role === "tool";
|
||||
}
|
||||
|
||||
export function isToolMessageForId(
|
||||
msg: ChatMessage | undefined,
|
||||
toolCallId: string,
|
||||
): boolean {
|
||||
return !!msg && msg.role === "tool" && msg.toolCallId === toolCallId;
|
||||
}
|
||||
|
||||
export function messageHasToolCallId(
|
||||
msg: ChatMessage | undefined,
|
||||
toolCallId: string,
|
||||
): boolean {
|
||||
return (
|
||||
!!msg &&
|
||||
msg.role === "assistant" &&
|
||||
!!msg.toolCalls?.find((call) => call.id === toolCallId)
|
||||
);
|
||||
}
|
||||
|
||||
export function chatMessageIsEmpty(message: ChatMessage): boolean {
|
||||
switch (message.role) {
|
||||
case "system":
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { RuleWithSource, UserChatMessage } from "../..";
|
||||
import { RuleWithSource, ToolResultChatMessage, UserChatMessage } from "../..";
|
||||
import { renderChatMessage } from "../../util/messageContent";
|
||||
import { extractPathsFromCodeBlocks } from "../utils/extractPathsFromCodeBlocks";
|
||||
import { isRuleActive } from "./isRuleActive";
|
||||
|
|
@ -10,7 +10,7 @@ export const getSystemMessageWithRules = ({
|
|||
currentModel,
|
||||
}: {
|
||||
baseSystemMessage?: string;
|
||||
userMessage: UserChatMessage | undefined;
|
||||
userMessage: UserChatMessage | ToolResultChatMessage | undefined;
|
||||
rules: RuleWithSource[];
|
||||
currentModel: string;
|
||||
}) => {
|
||||
|
|
|
|||
12
extensions/vscode/package-lock.json
generated
12
extensions/vscode/package-lock.json
generated
|
|
@ -1,20 +1,12 @@
|
|||
{
|
||||
"name": "continue",
|
||||
<<<<<<< HEAD
|
||||
"version": "1.1.22",
|
||||
=======
|
||||
"version": "1.1.24",
|
||||
>>>>>>> b8c92f26cad325ff79eca6b5d01fd4ac47b4a88f
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "continue",
|
||||
<<<<<<< HEAD
|
||||
"version": "1.1.22",
|
||||
=======
|
||||
"version": "1.1.24",
|
||||
>>>>>>> b8c92f26cad325ff79eca6b5d01fd4ac47b4a88f
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@continuedev/fetch": "^1.0.3",
|
||||
|
|
@ -114,11 +106,7 @@
|
|||
"@aws-sdk/client-sagemaker-runtime": "^3.777.0",
|
||||
"@aws-sdk/credential-providers": "^3.778.0",
|
||||
"@continuedev/config-types": "^1.0.13",
|
||||
<<<<<<< HEAD
|
||||
"@continuedev/config-yaml": "^1.0.77",
|
||||
=======
|
||||
"@continuedev/config-yaml": "file:../packages/config-yaml",
|
||||
>>>>>>> b8c92f26cad325ff79eca6b5d01fd4ac47b4a88f
|
||||
"@continuedev/fetch": "^1.0.4",
|
||||
"@continuedev/llm-info": "^1.0.8",
|
||||
"@continuedev/openai-adapters": "^1.0.18",
|
||||
|
|
|
|||
|
|
@ -181,7 +181,7 @@ export function RulesSection() {
|
|||
}
|
||||
}
|
||||
// Add a displayed rule for the base system chat message when in chat mode
|
||||
if (mode === "chat") {
|
||||
if (mode === "chat" || mode === "agent") {
|
||||
const baseMessage =
|
||||
config.selectedModelByRole.chat?.baseChatSystemMessage;
|
||||
if (baseMessage) {
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ export function ToolCallDiv(props: ToolCallDivProps) {
|
|||
const icon =
|
||||
props.toolCall.function?.name &&
|
||||
toolCallIcons[props.toolCall.function?.name];
|
||||
|
||||
if (icon && props.toolCall.id) {
|
||||
return (
|
||||
<div className="ml-4 mt-2">
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ export const streamThunkWrapper = createAsyncThunk<
|
|||
} finally {
|
||||
dispatch(setInactive());
|
||||
const state = getState();
|
||||
if (state.session.mode === "chat") {
|
||||
if (state.session.mode === "chat" || state.session.mode === "agent") {
|
||||
await dispatch(
|
||||
saveCurrentSession({
|
||||
openNewSession: false,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue