Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/types/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const toolNames = [
"codebase_search",
"update_todo_list",
"generate_image",
"edit_and_execute",
] as const

export const toolNamesSchema = z.enum(toolNames)
Expand Down
6 changes: 6 additions & 0 deletions src/core/assistant-message/presentAssistantMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { newTaskTool } from "../tools/newTaskTool"

import { updateTodoListTool } from "../tools/updateTodoListTool"
import { generateImageTool } from "../tools/generateImageTool"
import { editAndExecuteTool } from "../tools/editAndExecuteTool"

import { formatResponse } from "../prompts/responses"
import { validateToolUse } from "../tools/validateToolUse"
Expand Down Expand Up @@ -224,6 +225,8 @@ export async function presentAssistantMessage(cline: Task) {
}
case "generate_image":
return `[${block.name} for '${block.params.path}']`
case "edit_and_execute":
return `[${block.name}]`
}
}

Expand Down Expand Up @@ -552,6 +555,9 @@ export async function presentAssistantMessage(cline: Task) {
case "generate_image":
await generateImageTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
break
case "edit_and_execute":
await editAndExecuteTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
break
}

break
Expand Down
144 changes: 144 additions & 0 deletions src/core/tools/__tests__/editAndExecuteTool.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { describe, it, expect, vi, beforeEach } from "vitest"
import { editAndExecuteTool } from "../editAndExecuteTool"
import { Task } from "../../task/Task"
import { ToolUse } from "../../../shared/tools"
import * as writeToFileToolModule from "../writeToFileTool"
import * as executeCommandToolModule from "../executeCommandTool"

// Mock the imported tool modules
vi.mock("../writeToFileTool")
vi.mock("../executeCommandTool")
vi.mock("../applyDiffTool")
vi.mock("../insertContentTool")
vi.mock("../searchAndReplaceTool")

describe("editAndExecuteTool", () => {
let mockCline: Task
let mockAskApproval: any
let mockHandleError: any
let mockPushToolResult: any
let mockRemoveClosingTag: any

beforeEach(() => {
// Reset all mocks
vi.clearAllMocks()

// Create mock Cline instance
mockCline = {
consecutiveMistakeCount: 0,
recordToolError: vi.fn(),
recordToolUsage: vi.fn(),
sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing parameter error"),
ask: vi.fn().mockResolvedValue({}),
cwd: "/test/workspace",
providerRef: {
deref: vi.fn().mockReturnValue({
getState: vi.fn().mockResolvedValue({ experiments: {} }),
}),
},
} as any

// Create mock functions
mockAskApproval = vi.fn().mockResolvedValue(true)
mockHandleError = vi.fn()
mockPushToolResult = vi.fn()
mockRemoveClosingTag = vi.fn((tag, content) => content)
})

it("should handle missing args parameter", async () => {
const block: ToolUse = {
type: "tool_use",
name: "edit_and_execute",
params: {},
partial: false,
}

await editAndExecuteTool(
mockCline,
block,
mockAskApproval,
mockHandleError,
mockPushToolResult,
mockRemoveClosingTag,
)

expect(mockCline.consecutiveMistakeCount).toBe(1)
expect(mockCline.recordToolError).toHaveBeenCalledWith("edit_and_execute")
expect(mockPushToolResult).toHaveBeenCalledWith("Missing parameter error")
})

it("should handle missing edit block", async () => {
const block: ToolUse = {
type: "tool_use",
name: "edit_and_execute",
params: {
args: "<execute><execute_command><command>ls</command></execute_command></execute>",
},
partial: false,
}

await editAndExecuteTool(
mockCline,
block,
mockAskApproval,
mockHandleError,
mockPushToolResult,
mockRemoveClosingTag,
)

expect(mockCline.consecutiveMistakeCount).toBe(1)
expect(mockCline.recordToolError).toHaveBeenCalledWith("edit_and_execute")
expect(mockPushToolResult).toHaveBeenCalledWith(expect.stringContaining("Missing or invalid <edit> block"))
})

it("should handle missing execute block", async () => {
const block: ToolUse = {
type: "tool_use",
name: "edit_and_execute",
params: {
args: "<edit><write_to_file><path>test.txt</path><content>hello</content></write_to_file></edit>",
},
partial: false,
}

await editAndExecuteTool(
mockCline,
block,
mockAskApproval,
mockHandleError,
mockPushToolResult,
mockRemoveClosingTag,
)

expect(mockCline.consecutiveMistakeCount).toBe(1)
expect(mockCline.recordToolError).toHaveBeenCalledWith("edit_and_execute")
expect(mockPushToolResult).toHaveBeenCalledWith(expect.stringContaining("Missing or invalid <execute> block"))
})

it("should handle partial blocks", async () => {
const block: ToolUse = {
type: "tool_use",
name: "edit_and_execute",
params: {
args: "partial content",
},
partial: true,
}

await editAndExecuteTool(
mockCline,
block,
mockAskApproval,
mockHandleError,
mockPushToolResult,
mockRemoveClosingTag,
)

expect(mockCline.ask).toHaveBeenCalledWith(
"tool",
expect.stringContaining("Processing edit and execute operation"),
true,
)
expect(mockPushToolResult).not.toHaveBeenCalled()
})
})
Loading
Loading