Roo-Code/src/core/webview/generateSystemPrompt.ts
Sam Hoang Van 7bed94454a
feat: Enhance apply_diff with XML for multi-file/multi-diff operations & batch UI (#3342)
* feat: add BatchDiffApproval component for multi-file diff application

- Introduced a new component `BatchDiffApproval` to handle the approval of batch changes across multiple files.
- Integrated the `BatchDiffApproval` component into `ChatRow` to display batch diff requests.
- Updated experimental settings to include a toggle for multi-file apply diff functionality.
- Enhanced localization files to support new strings related to batch changes in multiple languages.
- Updated tests to cover the new multi-file apply diff feature.

* revert this

* fix: update applyDiff parameter type to accept string or DiffItem

* refactor: keep original file name for apply diff tool

* revert this

* Update src/core/webview/__tests__/ClineProvider.test.ts

* revert this

* fix: keep the original path if the experiment is disabled

* test: add dynamic strategy selection tests for MultiSearchReplaceDiffStrategy and MultiFileSearchReplaceDiffStrategy

* fix: mock applyDiffTool module and ensure legacy tool resolves successfully in tests

* remove this

* ellipsis suggestion

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

* refactor: mirror concurrent file reads

---------

Co-authored-by: Daniel Riccio <ricciodaniel98@gmail.com>
Co-authored-by: Daniel <57051444+daniel-lxs@users.noreply.github.com>
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
2025-06-12 12:36:34 -04:00

89 lines
3 KiB
TypeScript

import { WebviewMessage } from "../../shared/WebviewMessage"
import { defaultModeSlug, getModeBySlug, getGroupName } from "../../shared/modes"
import { buildApiHandler } from "../../api"
import { experiments as experimentsModule, EXPERIMENT_IDS } from "../../shared/experiments"
import { SYSTEM_PROMPT } from "../prompts/system"
import { MultiSearchReplaceDiffStrategy } from "../diff/strategies/multi-search-replace"
import { MultiFileSearchReplaceDiffStrategy } from "../diff/strategies/multi-file-search-replace"
import { ClineProvider } from "./ClineProvider"
export const generateSystemPrompt = async (provider: ClineProvider, message: WebviewMessage) => {
const {
apiConfiguration,
customModePrompts,
customInstructions,
browserViewportSize,
diffEnabled,
mcpEnabled,
fuzzyMatchThreshold,
experiments,
enableMcpServerCreation,
browserToolEnabled,
language,
maxReadFileLine,
maxConcurrentFileReads,
} = await provider.getState()
// Check experiment to determine which diff strategy to use
const isMultiFileApplyDiffEnabled = experimentsModule.isEnabled(
experiments ?? {},
EXPERIMENT_IDS.MULTI_FILE_APPLY_DIFF,
)
const diffStrategy = isMultiFileApplyDiffEnabled
? new MultiFileSearchReplaceDiffStrategy(fuzzyMatchThreshold)
: new MultiSearchReplaceDiffStrategy(fuzzyMatchThreshold)
const cwd = provider.cwd
const mode = message.mode ?? defaultModeSlug
const customModes = await provider.customModesManager.getCustomModes()
const rooIgnoreInstructions = provider.getCurrentCline()?.rooIgnoreController?.getInstructions()
// Determine if browser tools can be used based on model support, mode, and user settings
let modelSupportsComputerUse = false
// Create a temporary API handler to check if the model supports computer use
// This avoids relying on an active Cline instance which might not exist during preview
try {
const tempApiHandler = buildApiHandler(apiConfiguration)
modelSupportsComputerUse = tempApiHandler.getModel().info.supportsComputerUse ?? false
} catch (error) {
console.error("Error checking if model supports computer use:", error)
}
// Check if the current mode includes the browser tool group
const modeConfig = getModeBySlug(mode, customModes)
const modeSupportsBrowser = modeConfig?.groups.some((group) => getGroupName(group) === "browser") ?? false
// Only enable browser tools if the model supports it, the mode includes browser tools,
// and browser tools are enabled in settings
const canUseBrowserTool = modelSupportsComputerUse && modeSupportsBrowser && (browserToolEnabled ?? true)
const systemPrompt = await SYSTEM_PROMPT(
provider.context,
cwd,
canUseBrowserTool,
mcpEnabled ? provider.getMcpHub() : undefined,
diffStrategy,
browserViewportSize ?? "900x600",
mode,
customModePrompts,
customModes,
customInstructions,
diffEnabled,
experiments,
enableMcpServerCreation,
language,
rooIgnoreInstructions,
maxReadFileLine !== -1,
{
maxConcurrentFileReads,
},
)
return systemPrompt
}