MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / handleAddFile

Method handleAddFile

src/core/tools/ApplyPatchTool.ts:141–231  ·  view source on GitHub ↗
(
		change: ApplyPatchFileChange,
		absolutePath: string,
		relPath: string,
		task: Task,
		callbacks: ToolCallbacks,
		isWriteProtected: boolean,
	)

Source from the content-addressed store, hash-verified

139 }
140
141 private async handleAddFile(
142 change: ApplyPatchFileChange,
143 absolutePath: string,
144 relPath: string,
145 task: Task,
146 callbacks: ToolCallbacks,
147 isWriteProtected: boolean,
148 ): Promise<void> {
149 const { askApproval, pushToolResult } = callbacks
150
151 // Check if file already exists
152 const fileExists = await fileExistsAtPath(absolutePath)
153 if (fileExists) {
154 task.consecutiveMistakeCount++
155 task.recordToolError("apply_patch")
156 const errorMessage = `File already exists: ${relPath}. Use Update File instead.`
157 await task.say("error", errorMessage)
158 pushToolResult(formatResponse.toolError(errorMessage))
159 return
160 }
161
162 const newContent = change.newContent || ""
163 const isOutsideWorkspace = isPathOutsideWorkspace(absolutePath)
164
165 // Initialize diff view for new file
166 task.diffViewProvider.editType = "create"
167 task.diffViewProvider.originalContent = undefined
168
169 const diff = formatResponse.createPrettyPatch(relPath, "", newContent)
170
171 // Check experiment settings
172 const provider = task.providerRef.deref()
173 const state = await provider?.getState()
174 const diagnosticsEnabled = state?.diagnosticsEnabled ?? true
175 const writeDelayMs = state?.writeDelayMs ?? DEFAULT_WRITE_DELAY_MS
176 const isPreventFocusDisruptionEnabled = experiments.isEnabled(
177 state?.experiments ?? {},
178 EXPERIMENT_IDS.PREVENT_FOCUS_DISRUPTION,
179 )
180
181 const sanitizedDiff = sanitizeUnifiedDiff(diff || "")
182 const diffStats = computeDiffStats(sanitizedDiff) || undefined
183
184 const sharedMessageProps: ClineSayTool = {
185 tool: "appliedDiff",
186 path: getReadablePath(task.cwd, relPath),
187 diff: sanitizedDiff,
188 isOutsideWorkspace,
189 }
190
191 const completeMessage = JSON.stringify({
192 ...sharedMessageProps,
193 content: sanitizedDiff,
194 isProtected: isWriteProtected,
195 diffStats,
196 } satisfies ClineSayTool)
197
198 // Show diff view if focus disruption prevention is disabled

Callers 1

executeMethod · 0.95

Calls 15

fileExistsAtPathFunction · 0.90
isPathOutsideWorkspaceFunction · 0.90
sanitizeUnifiedDiffFunction · 0.90
computeDiffStatsFunction · 0.90
getReadablePathFunction · 0.90
pushToolResultFunction · 0.85
askApprovalFunction · 0.85
recordToolErrorMethod · 0.80
sayMethod · 0.80
isEnabledMethod · 0.80
openMethod · 0.80
scrollToFirstDiffMethod · 0.80

Tested by

no test coverage detected