Skip to content

Commit f16c70e

Browse files
extract isClaude4ModelFamily helper (RooCodeInc#4121)
1 parent ddca841 commit f16c70e

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

src/core/task/index.ts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ import { isInTestMode } from "../../services/test/TestMode"
113113
import { processFilesIntoText } from "@integrations/misc/extract-text"
114114
import { featureFlagsService } from "@services/posthog/feature-flags/FeatureFlagsService"
115115
import { StreamingJsonReplacer, ChangeLocation } from "@core/assistant-message/diff-json"
116+
import { isClaude4ModelFamily } from "@/utils/model-utils"
116117

117118
export const USE_EXPERIMENTAL_CLAUDE4_FEATURES = false
118119

@@ -1629,12 +1630,6 @@ export class Task {
16291630
}
16301631
}
16311632

1632-
private async isClaude4ModelFamily(): Promise<boolean> {
1633-
const model = this.api.getModel()
1634-
const modelId = model.id
1635-
return modelId.includes("sonnet-4") || modelId.includes("opus-4")
1636-
}
1637-
16381633
async *attemptApiRequest(previousApiReqIndex: number): ApiStream {
16391634
// Wait for MCP servers to be connected before generating system prompt
16401635
await pWaitFor(() => this.mcpHub.isConnecting !== true, { timeout: 10_000 }).catch(() => {
@@ -1648,8 +1643,8 @@ export class Task {
16481643

16491644
const supportsBrowserUse = modelSupportsBrowserUse && !disableBrowserTool // only enable browser use if the model supports it and the user hasn't disabled it
16501645

1651-
const isClaude4ModelFamily = await this.isClaude4ModelFamily()
1652-
let systemPrompt = await SYSTEM_PROMPT(cwd, supportsBrowserUse, this.mcpHub, this.browserSettings, isClaude4ModelFamily)
1646+
const isClaude4Model = isClaude4ModelFamily(this.api)
1647+
let systemPrompt = await SYSTEM_PROMPT(cwd, supportsBrowserUse, this.mcpHub, this.browserSettings, isClaude4Model)
16531648

16541649
let settingsCustomInstructions = this.customInstructions?.trim()
16551650
await this.migratePreferredLanguageToolSetting()
@@ -1930,7 +1925,6 @@ export class Task {
19301925

19311926
// Get final list of replacements
19321927
const allReplacements = this.streamingJsonReplacer.getSuccessfullyParsedItems()
1933-
// console.log(`Total replacements applied: ${allReplacements.length}`)
19341928

19351929
// Cleanup
19361930
this.streamingJsonReplacer = undefined
@@ -1960,7 +1954,6 @@ export class Task {
19601954
if (this.didCompleteReadingStream) {
19611955
this.userMessageContentReady = true
19621956
}
1963-
// console.log("no more content blocks to stream! this shouldn't happen?")
19641957
this.presentAssistantMessageLocked = false
19651958
return
19661959
//throw new Error("No more content blocks to stream! This shouldn't happen...") // remove and just return after testing
@@ -2096,11 +2089,11 @@ export class Task {
20962089
break
20972090
}
20982091

2099-
const pushToolResult = (content: ToolResponse, isClaude4ModelFamily: boolean = false) => {
2092+
const pushToolResult = (content: ToolResponse, isClaude4Model: boolean = false) => {
21002093
if (typeof content === "string") {
21012094
const resultText = content || "(tool did not return anything)"
21022095

2103-
if (isClaude4ModelFamily && USE_EXPERIMENTAL_CLAUDE4_FEATURES) {
2096+
if (isClaude4Model && USE_EXPERIMENTAL_CLAUDE4_FEATURES) {
21042097
// Claude 4 family: Use function_results format
21052098
this.userMessageContent.push({
21062099
type: "text",
@@ -2186,7 +2179,7 @@ export class Task {
21862179
}
21872180
}
21882181

2189-
const handleError = async (action: string, error: Error, isClaude4ModelFamily: boolean = false) => {
2182+
const handleError = async (action: string, error: Error, isClaude4Model: boolean = false) => {
21902183
if (this.abandoned) {
21912184
console.log("Ignoring error since task was abandoned (i.e. from task cancellation after resetting)")
21922185
return
@@ -2197,7 +2190,7 @@ export class Task {
21972190
`Error ${action}:\n${error.message ?? JSON.stringify(serializeError(error), null, 2)}`,
21982191
)
21992192

2200-
pushToolResult(formatResponse.toolError(errorString), isClaude4ModelFamily)
2193+
pushToolResult(formatResponse.toolError(errorString), isClaude4Model)
22012194
}
22022195

22032196
// If block is partial, remove partial closing tag so its not presented to user
@@ -2275,9 +2268,9 @@ export class Task {
22752268

22762269
const currentFullJson = block.params.diff
22772270
// Check if we should use streaming (e.g., for specific models)
2278-
const isClaude4ModelFamily = await this.isClaude4ModelFamily()
2271+
const isClaude4Model = isClaude4ModelFamily(this.api)
22792272
// Going through claude family of models
2280-
if (isClaude4ModelFamily && USE_EXPERIMENTAL_CLAUDE4_FEATURES && currentFullJson) {
2273+
if (isClaude4Model && USE_EXPERIMENTAL_CLAUDE4_FEATURES && currentFullJson) {
22812274
console.log("[EDIT] Streaming JSON replacement")
22822275
const streamingResult = await this.handleStreamingJsonReplacement(
22832276
block,
@@ -2669,7 +2662,7 @@ export class Task {
26692662
}
26702663
}
26712664
case "list_files": {
2672-
const isClaude4ModelFamily = await this.isClaude4ModelFamily()
2665+
const isClaude4Model = isClaude4ModelFamily(this.api)
26732666
const relDirPath: string | undefined = block.params.path
26742667
const recursiveRaw: string | undefined = block.params.recursive
26752668
const recursive = recursiveRaw?.toLowerCase() === "true"
@@ -2695,10 +2688,7 @@ export class Task {
26952688
} else {
26962689
if (!relDirPath) {
26972690
this.consecutiveMistakeCount++
2698-
pushToolResult(
2699-
await this.sayAndCreateMissingParamError("list_files", "path"),
2700-
isClaude4ModelFamily,
2701-
)
2691+
pushToolResult(await this.sayAndCreateMissingParamError("list_files", "path"), isClaude4Model)
27022692
await this.saveCheckpoint()
27032693
break
27042694
}
@@ -2749,12 +2739,12 @@ export class Task {
27492739
true,
27502740
)
27512741
}
2752-
pushToolResult(result, isClaude4ModelFamily)
2742+
pushToolResult(result, isClaude4Model)
27532743
await this.saveCheckpoint()
27542744
break
27552745
}
27562746
} catch (error) {
2757-
await handleError("listing files", error, isClaude4ModelFamily)
2747+
await handleError("listing files", error, isClaude4Model)
27582748
await this.saveCheckpoint()
27592749
break
27602750
}
@@ -2842,7 +2832,7 @@ export class Task {
28422832
}
28432833
}
28442834
case "search_files": {
2845-
const isClaude4ModelFamily = await this.isClaude4ModelFamily()
2835+
const isClaude4Model = isClaude4ModelFamily(this.api)
28462836
const relDirPath: string | undefined = block.params.path
28472837
const regex: string | undefined = block.params.regex
28482838
const filePattern: string | undefined = block.params.file_pattern
@@ -2872,7 +2862,7 @@ export class Task {
28722862
this.consecutiveMistakeCount++
28732863
pushToolResult(
28742864
await this.sayAndCreateMissingParamError("search_files", "path"),
2875-
isClaude4ModelFamily,
2865+
isClaude4Model,
28762866
)
28772867
await this.saveCheckpoint()
28782868
break
@@ -2881,7 +2871,7 @@ export class Task {
28812871
this.consecutiveMistakeCount++
28822872
pushToolResult(
28832873
await this.sayAndCreateMissingParamError("search_files", "regex"),
2884-
isClaude4ModelFamily,
2874+
isClaude4Model,
28852875
)
28862876
await this.saveCheckpoint()
28872877
break
@@ -2932,12 +2922,12 @@ export class Task {
29322922
true,
29332923
)
29342924
}
2935-
pushToolResult(results, isClaude4ModelFamily)
2925+
pushToolResult(results, isClaude4Model)
29362926
await this.saveCheckpoint()
29372927
break
29382928
}
29392929
} catch (error) {
2940-
await handleError("searching files", error, isClaude4ModelFamily)
2930+
await handleError("searching files", error, isClaude4Model)
29412931
await this.saveCheckpoint()
29422932
break
29432933
}
@@ -4525,9 +4515,8 @@ export class Task {
45254515
assistantMessage += chunk.text
45264516
// parse raw assistant message into content blocks
45274517
const prevLength = this.assistantMessageContent.length
4528-
const isClaude4ModelFamily = await this.isClaude4ModelFamily()
4529-
4530-
if (isClaude4ModelFamily && USE_EXPERIMENTAL_CLAUDE4_FEATURES) {
4518+
const isClaude4Model = isClaude4ModelFamily(this.api)
4519+
if (isClaude4Model && USE_EXPERIMENTAL_CLAUDE4_FEATURES) {
45314520
this.assistantMessageContent = parseAssistantMessageV3(assistantMessage)
45324521
} else {
45334522
this.assistantMessageContent = parseAssistantMessageV2(assistantMessage)

src/utils/model-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ApiHandler } from "@api/index"
2+
3+
export function isClaude4ModelFamily(api: ApiHandler): boolean {
4+
const model = api.getModel()
5+
const modelId = model.id
6+
return modelId.includes("sonnet-4") || modelId.includes("opus-4")
7+
}

0 commit comments

Comments
 (0)