fix-mode-switch-broken-tools

This commit is contained in:
chezsmithy 2025-05-06 00:16:52 -07:00
parent 8665a08360
commit c0b26d3302
6 changed files with 104 additions and 12 deletions

View file

@ -8,14 +8,73 @@ import {
} from "../";
import { findLast } from "../util/findLast";
import { normalizeToMessageParts } from "../util/messageContent";
import { isUserOrToolMsg } from "./messages";
import { isUserOrToolMsg, messageHasToolCallId } from "./messages";
import { getSystemMessageWithRules } from "./rules/getSystemMessageWithRules";
import { CHAT_UNSAFE_TOOLS } from "../tools/builtIn";
export const DEFAULT_CHAT_SYSTEM_MESSAGE_URL =
"https://github.com/continuedev/continue/blob/main/core/llm/constructMessages.ts";
export const DEFAULT_AGENT_SYSTEM_MESSAGE_URL =
"https://github.com/continuedev/continue/blob/main/core/llm/constructMessages.ts";
export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\
<important_rules>
You are in chat mode.
Only use functions that are explicitly listed in the toolsConfig provided to you. Ignore any previous tools that might have been used.
Before making any function call, verify that the function name exists in your current toolsConfig.
Never attempt to use functions you've observed in context if they aren't in your current toolsConfig.
Ignore any examples that use unavailable functions.
In chat mode, only suggest changes rather than attempting to make them directly. and can't actually edit or create files.
You can only suggest modifictions.
If the user asks to make changes offer that they can switch to Agent Mode to make the suggested updates automatically.
If needed consisely explain to the user they can switch to agent mode using the Mode Selector dropdown and provide no other details.
Always include the language and file name in the info string when you write code blocks.
If you are editing "src/main.py" for example, your code block should start with '\`\`\`python src/main.py'
When addressing code modification requests, present a concise code snippet that
emphasizes only the necessary changes and uses abbreviated placeholders for
unmodified sections. For example:
\`\`\`language /path/to/file
// ... existing code ...
{{ modified code here }}
// ... existing code ...
{{ another modification }}
// ... rest of code ...
\`\`\`
In existing files, you should always restate the function or class that the snippet belongs to:
\`\`\`language /path/to/file
// ... existing code ...
function exampleFunction() {
// ... existing code ...
{{ modified code here }}
// ... rest of function ...
}
// ... rest of code ...
\`\`\`
Since users have access to their complete file, they prefer reading only the
relevant modifications. It's perfectly acceptable to omit unmodified portions
at the beginning, middle, or end of files using these "lazy" comments. Only
provide the complete file when explicitly requested. Include a concise explanation
of changes unless the user specifically asks for code only.
</important_rules>`;
export const DEFAULT_AGENT_SYSTEM_MESSAGE = `\
<important_rules>
You are in agent mode.
Always include the language and file name in the info string when you write code blocks.
If you are editing "src/main.py" for example, your code block should start with '\`\`\`python src/main.py'
@ -59,6 +118,7 @@ export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\
</important_rules>`;
export function constructMessages(
messageMode: string,
history: ChatHistoryItem[],
baseChatOrAgentSystemMessage: string | undefined,
rules: RuleWithSource[],
@ -67,10 +127,26 @@ export function constructMessages(
(item) => item.message.role !== "system",
);
const msgs: ChatMessage[] = [];
const toolCallIdsToRemove: string[] = [];
for (let i = 0; i < filteredHistory.length; i++) {
const historyItem = filteredHistory[i];
if (messageMode === "chat" && CHAT_UNSAFE_TOOLS.includes(historyItem.toolCallState?.toolCall.function.name || "")) {
// remove unsafe tools calls to avoid using them in chat mode.
// Some models ignore toolsConfig and instead use tools use in history.
if (historyItem.toolCallState?.toolCallId) {
toolCallIdsToRemove.push(historyItem.toolCallState.toolCall.id);
}
continue;
}
const toolMessage: ToolResultChatMessage = historyItem.message as ToolResultChatMessage;
if (messageMode === "chat" && toolCallIdsToRemove.includes(toolMessage.toolCallId || "")) {
// remove toolcall responses from the history for unsafe tools.
continue;
}
if (historyItem.message.role === "user") {
// Gather context items for user messages
let content = normalizeToMessageParts(historyItem.message);
@ -100,8 +176,7 @@ export function constructMessages(
| undefined;
const systemMessage = getSystemMessageWithRules({
baseSystemMessage:
baseChatOrAgentSystemMessage ?? DEFAULT_CHAT_SYSTEM_MESSAGE,
baseSystemMessage: baseChatOrAgentSystemMessage,
rules,
userMessage: lastUserMsg,
});

View file

@ -16,6 +16,8 @@ export enum BuiltInToolNames {
ViewSubdirectory = "builtin_view_subdirectory",
}
export const CHAT_UNSAFE_TOOLS: string[] = [ BuiltInToolNames.EditExistingFile, BuiltInToolNames.CreateNewFile, BuiltInToolNames.RunTerminalCommand ];
export const BUILT_IN_GROUP_NAME = "Built-In";
export const CLIENT_TOOLS = [BuiltInToolNames.EditExistingFile];

View file

@ -1,6 +1,7 @@
import { createSelector } from "@reduxjs/toolkit";
import { Tool } from "core";
import { RootState } from "../store";
import { CHAT_UNSAFE_TOOLS } from "core/tools/builtIn";
export const selectActiveTools = createSelector(
[
@ -10,13 +11,24 @@ export const selectActiveTools = createSelector(
(store: RootState) => store.ui.toolGroupSettings,
],
(mode, tools, policies, groupPolicies): Tool[] => {
if (mode !== "agent") {
if (mode === "chat") {
const chatSafeTools: Tool[] = tools.filter(
(tool) =>
!CHAT_UNSAFE_TOOLS.includes(tool.function.name) &&
policies[tool.function.name] !== "disabled" &&
groupPolicies[tool.group] !== "exclude",
);
return chatSafeTools;
}
else if (mode === "agent") {
return tools.filter(
(tool) =>
policies[tool.function.name] !== "disabled" &&
groupPolicies[tool.group] !== "exclude",
);
}
else {
return [];
}
return tools.filter(
(tool) =>
policies[tool.function.name] !== "disabled" &&
groupPolicies[tool.group] !== "exclude",
);
},
);

View file

@ -89,8 +89,9 @@ export const streamResponseThunk = createAsyncThunk<
const messageMode = getState().session.mode
const baseChatOrAgentSystemMessage = getBaseSystemMessage(selectedChatModel, messageMode)
const messages = constructMessages(
messageMode,
[...updatedHistory],
baseChatOrAgentSystemMessage,
state.config.config.rules,

View file

@ -72,6 +72,7 @@ export const streamResponseAfterToolCall = createAsyncThunk<
const baseChatOrAgentSystemMessage = getBaseSystemMessage(selectedChatModel, messageMode)
const messages = constructMessages(
messageMode,
[...updatedHistory],
baseChatOrAgentSystemMessage,
state.config.config.rules,

View file

@ -3,6 +3,7 @@ import { ProfileDescription } from "core/config/ProfileLifecycleManager";
import _ from "lodash";
import { KeyboardEvent } from "react";
import { getLocalStorage } from "./localStorage";
import { DEFAULT_CHAT_SYSTEM_MESSAGE, DEFAULT_AGENT_SYSTEM_MESSAGE } from "core/llm/constructMessages";
export type Platform = "mac" | "linux" | "windows" | "unknown";
@ -122,9 +123,9 @@ export function isLocalProfile(profile: ProfileDescription): boolean {
export function getBaseSystemMessage(modelDetails: ModelDescription | null, mode: MessageModes) {
let baseChatOrAgentSystemMessage: string|undefined
if(mode === 'agent') {
baseChatOrAgentSystemMessage = modelDetails?.baseAgentSystemMessage ?? modelDetails?.baseChatSystemMessage // fallback to chat system message if agent system message is unavailable
baseChatOrAgentSystemMessage = modelDetails?.baseAgentSystemMessage ?? DEFAULT_AGENT_SYSTEM_MESSAGE;
} else {
baseChatOrAgentSystemMessage = modelDetails?.baseChatSystemMessage
baseChatOrAgentSystemMessage = modelDetails?.baseChatSystemMessage ?? DEFAULT_CHAT_SYSTEM_MESSAGE;
}
return baseChatOrAgentSystemMessage
}