Merge pull request #6841 from continuedev/nate/sys-msg-tools-by-default

System message tools by default for OpenRouter
This commit is contained in:
Nate Sesti 2025-07-28 18:35:36 -07:00 • committed by GitHub
commit 07a0e511ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 275 additions and 8 deletions

View file

@ -0,0 +1,31 @@
import { ModelDescription } from "../index.d";
/**
* Determines if system message tools should be auto-enabled for the given model.
* This is used to automatically enable system message tools for certain provider/model combinations
* where we have a strong preference, taking priority over user manual settings.
*
* System message tools are an alternative to native tool calling where tool definitions and calls
* are embedded in the system message as text rather than using the provider's native tool calling API.
* This can be beneficial for providers/models that don't support native tool calling or have
* poor tool calling performance.
*
* Current auto-enable rules:
* - OpenRouter provider: true for all models except those containing "claude" (since Claude models
* generally have good native tool calling support)
* - All other providers: undefined (no auto-preference, use manual setting)
*
* @param model The model description to check
* @returns true to force enable, false to force disable, undefined for no auto-preference
*/
export function shouldAutoEnableSystemMessageTools(
model: ModelDescription,
): boolean | undefined {
// Auto-enable for OpenRouter, but exclude Claude models which have good native tool calling
if (model.provider === "openrouter") {
return !model.model.toLowerCase().includes("claude");
}
// No auto-preference for all other providers - use manual setting
return undefined;
}

View file

@ -0,0 +1,85 @@
import { describe, expect, test } from "vitest";
import { ModelDescription } from "../index.d";
import { shouldAutoEnableSystemMessageTools } from "./shouldAutoEnableSystemMessageTools";
describe("shouldAutoEnableSystemMessageTools", () => {
const createModel = (provider: string, model: string): ModelDescription => ({
title: "Test Model",
provider,
underlyingProviderName: provider,
model,
});
test("should return true for OpenRouter models that don't contain 'claude'", () => {
expect(
shouldAutoEnableSystemMessageTools(createModel("openrouter", "gpt-4o")),
).toBe(true);
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "llama-3.3-70b-instruct"),
),
).toBe(true);
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "qwen/qwen-2.5-72b-instruct"),
),
).toBe(true);
});
test("should return false for OpenRouter Claude models", () => {
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "anthropic/claude-3-sonnet"),
),
).toBe(false);
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "anthropic/claude-3.5-sonnet"),
),
).toBe(false);
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "claude-3-haiku"),
),
).toBe(false);
});
test("should return undefined for all other providers", () => {
expect(
shouldAutoEnableSystemMessageTools(createModel("openai", "gpt-4o")),
).toBe(undefined);
expect(
shouldAutoEnableSystemMessageTools(
createModel("anthropic", "claude-3.5-sonnet"),
),
).toBe(undefined);
expect(
shouldAutoEnableSystemMessageTools(
createModel("groq", "llama-3.3-70b-versatile"),
),
).toBe(undefined);
expect(
shouldAutoEnableSystemMessageTools(
createModel("together", "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"),
),
).toBe(undefined);
});
test("should be case insensitive for 'claude' detection", () => {
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "CLAUDE-models"),
),
).toBe(false);
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "Claude-3.5"),
),
).toBe(false);
expect(
shouldAutoEnableSystemMessageTools(
createModel("openrouter", "some-model-Claude"),
),
).toBe(false);
});
});

View file

@ -213,7 +213,7 @@ describe("LLM", () => {
testToolCall: true,
});
testLLM(
new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o1-preview" }),
new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o3-mini" }),
{ skip: false, timeout: 60000 },
);
testLLM(new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o1" }), {

View file

@ -275,7 +275,7 @@ describe("OpenAI", () => {
test("should handle O1 models correctly", async () => {
const openai = new OpenAI({
apiKey: "test-api-key",
model: "o1-preview",
model: "o3-mini",
apiBase: "https://api.openai.com/v1/",
});
@ -296,7 +296,7 @@ describe("OpenAI", () => {
"api-key": "test-api-key",
},
body: {
model: "o1-preview",
model: "o3-mini",
messages: [{ role: "user", content: "hello" }],
stream: true,
max_completion_tokens: 100,

View file

@ -12,6 +12,7 @@ import {
import { capitalize } from "lodash";
import { useCallback, useEffect, useMemo } from "react";
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import { selectUseSystemMessageTools } from "../../redux/selectors/selectUseSystemMessageTools";
import { selectSelectedChatModel } from "../../redux/slices/configSlice";
import { setMode } from "../../redux/slices/sessionSlice";
import { getFontSize, getMetaKeyLabel } from "../../util";
@ -24,9 +25,8 @@ export function ModeSelect() {
const dispatch = useAppDispatch();
const mode = useAppSelector((store) => store.session.mode);
const selectedModel = useAppSelector(selectSelectedChatModel);
const useSystemTools = useAppSelector(
(state) => state.config.config.experimental?.onlyUseSystemMessageTools,
);
const useSystemTools = useAppSelector(selectUseSystemMessageTools);
const isAgentSupported = useMemo(() => {
if (!selectedModel) {
return undefined;

View file

@ -0,0 +1,112 @@
import { ModelDescription } from "core";
import { describe, expect, test, vi } from "vitest";
import { RootState } from "../store";
// Mock the shouldAutoEnableSystemMessageTools function
vi.mock("core/config/shouldAutoEnableSystemMessageTools", () => ({
shouldAutoEnableSystemMessageTools: vi.fn((model: ModelDescription) => {
if (model.provider === "openrouter") {
return !model.model.toLowerCase().includes("claude");
}
return undefined; // No auto-preference for other providers
}),
}));
// Mock the selectSelectedChatModel selector - we'll control its return value in tests
vi.mock("../slices/configSlice", () => ({
selectSelectedChatModel: vi.fn(),
}));
// Import after mocking
import { selectSelectedChatModel } from "../slices/configSlice";
import { selectUseSystemMessageTools } from "./selectUseSystemMessageTools";
describe("selectUseSystemMessageTools", () => {
const mockSelectSelectedChatModel = selectSelectedChatModel as any;
const createMockState = (manualSetting?: boolean): RootState => {
return {
config: {
config: {
experimental: {
onlyUseSystemMessageTools: manualSetting,
},
},
},
} as any;
};
const createModel = (provider: string, model: string): ModelDescription => ({
title: "Test Model",
provider,
underlyingProviderName: provider,
model,
});
test("auto-detection takes priority: OpenRouter non-Claude returns true even with manual false", () => {
mockSelectSelectedChatModel.mockReturnValue(
createModel("openrouter", "gpt-4"),
);
const state = createMockState(false);
expect(selectUseSystemMessageTools(state)).toBe(true);
});
test("auto-detection takes priority: OpenRouter Claude returns false even with manual true", () => {
mockSelectSelectedChatModel.mockReturnValue(
createModel("openrouter", "claude-3.5-sonnet"),
);
const state = createMockState(true);
expect(selectUseSystemMessageTools(state)).toBe(false);
});
test("falls back to manual setting when no auto-preference: manual true", () => {
mockSelectSelectedChatModel.mockReturnValue(createModel("openai", "gpt-4"));
const state = createMockState(true);
expect(selectUseSystemMessageTools(state)).toBe(true);
});
test("falls back to manual setting when no auto-preference: manual false", () => {
mockSelectSelectedChatModel.mockReturnValue(
createModel("anthropic", "claude-3.5-sonnet"),
);
const state = createMockState(false);
expect(selectUseSystemMessageTools(state)).toBe(false);
});
test("defaults to false when no auto-preference and no manual setting", () => {
mockSelectSelectedChatModel.mockReturnValue(
createModel("groq", "llama-3.3-70b"),
);
const state = createMockState(undefined);
expect(selectUseSystemMessageTools(state)).toBe(false);
});
test("handles missing selected model: uses manual setting", () => {
mockSelectSelectedChatModel.mockReturnValue(undefined);
const state = createMockState(true);
expect(selectUseSystemMessageTools(state)).toBe(true);
});
test("handles missing selected model: defaults to false when no manual setting", () => {
mockSelectSelectedChatModel.mockReturnValue(undefined);
const state = createMockState(undefined);
expect(selectUseSystemMessageTools(state)).toBe(false);
});
test("auto-enable works for various OpenRouter models", () => {
mockSelectSelectedChatModel.mockReturnValue(
createModel("openrouter", "gpt-4o"),
);
expect(selectUseSystemMessageTools(createMockState(false))).toBe(true);
mockSelectSelectedChatModel.mockReturnValue(
createModel("openrouter", "llama-3.3-70b"),
);
expect(selectUseSystemMessageTools(createMockState(false))).toBe(true);
mockSelectSelectedChatModel.mockReturnValue(
createModel("openrouter", "claude-haiku"),
);
expect(selectUseSystemMessageTools(createMockState(true))).toBe(false);
});
});

View file

@ -0,0 +1,37 @@
import { shouldAutoEnableSystemMessageTools } from "core/config/shouldAutoEnableSystemMessageTools";
import { selectSelectedChatModel } from "../slices/configSlice";
import { RootState } from "../store";
/**
* Selector that determines if system message tools should be used.
* This prioritizes auto-detection over manual configuration where we have a strong preference.
*
* Priority order:
* 1. Auto-detection (if it returns true/false)
* 2. Manual user configuration (if auto-detection returns undefined)
* 3. Default to false
*
* @param state The Redux root state
* @returns true if system message tools should be used, false otherwise
*/
export function selectUseSystemMessageTools(state: RootState): boolean {
const selectedModel = selectSelectedChatModel(state);
const manualSetting =
state.config.config.experimental?.onlyUseSystemMessageTools;
// If no model is selected, fall back to manual setting or default
if (!selectedModel) {
return manualSetting ?? false;
}
// Check auto-detection first
const autoSetting = shouldAutoEnableSystemMessageTools(selectedModel);
// If auto-detection has a preference, use it (takes priority)
if (autoSetting !== undefined) {
return autoSetting;
}
// If no auto-preference, use manual setting or default to false
return manualSetting ?? false;
}

View file

@ -4,6 +4,7 @@ import { getRuleId } from "core/llm/rules/getSystemMessageWithRules";
import { ToCoreProtocol } from "core/protocol";
import { BuiltInToolNames } from "core/tools/builtIn";
import { selectActiveTools } from "../selectors/selectActiveTools";
import { selectUseSystemMessageTools } from "../selectors/selectUseSystemMessageTools";
import { selectSelectedChatModel } from "../slices/configSlice";
import {
abortStream,
@ -141,8 +142,9 @@ export const streamNormalInput = createAsyncThunk<
const allActiveTools = selectActiveTools(state);
const activeTools = filterToolsForModel(allActiveTools, selectedChatModel);
const supportsNativeTools = modelSupportsNativeTools(selectedChatModel);
const useSystemTools =
!!state.config.config.experimental?.onlyUseSystemMessageTools;
// Use the centralized selector to determine if system message tools should be used
const useSystemTools = selectUseSystemMessageTools(state);
const useNativeTools = !useSystemTools && supportsNativeTools;
// Construct completion options