-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
717 lines (607 loc) · 19.2 KB
/
index.ts
File metadata and controls
717 lines (607 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
* OpenCode Fast Apply Plugin
*
* Integrates OpenAI-compatible Fast Apply API for 10x faster code editing.
* Supports LM Studio, Ollama, and other OpenAI-compatible endpoints.
* Uses lazy edit markers (// ... existing code ...) for partial file updates.
*
* @see https://github.com/tickernelz/opencode-fast-apply
*/
import { type Plugin, tool } from "@opencode-ai/plugin"
import { createTwoFilesPatch } from "diff"
import { readFile, writeFile, access } from "fs/promises"
import { constants } from "fs"
type SessionParams = {
agent?: string
providerId?: string
modelId?: string
variant?: string
}
const sessionParamsCache = new Map<string, SessionParams>()
// Get API key from environment (set in mcpm/jarvis config)
const FAST_APPLY_API_KEY = process.env.FAST_APPLY_API_KEY || "optional-api-key"
const FAST_APPLY_URL = (process.env.FAST_APPLY_URL || "http://localhost:1234/v1").replace(/\/v1\/?$/, "")
const FAST_APPLY_MODEL = process.env.FAST_APPLY_MODEL || "fastapply-1.5b"
const FAST_APPLY_SYSTEM_PROMPT = `You are a precise code merging assistant. Your role:
1. Merge updates while preserving all original structure and formatting
2. Output ONLY valid code within specified XML tags
3. Never add explanations or text outside tags
4. Preserve all escaped XML entities exactly as provided
5. Ensure output is syntactically valid and complete`
const FAST_APPLY_USER_PROMPT = `Merge <update> into <code> and output the complete merged file.
RULES:
- Preserve: structure, order, comments, indentation, escaped entities
- Output: entire file within <updated-code></updated-code> tags only
- Forbidden: explanations, placeholders, ellipses, code fences, nested tags
- Validation: ensure syntactically valid, complete code
<code>{original_code}</code>
<update>{update_snippet}</update>
Output complete merged code:`
const TOOL_INSTRUCTIONS = `DEFAULT tool for editing existing files. Use INSTEAD of native 'edit' tool.
CRITICAL: For EXISTING files ONLY. Use 'write' for new files.
WORKFLOW:
1. Read the file to understand current content
2. Extract relevant section (50-500 lines with context)
3. Call fast_apply_edit with original_code (partial) and code_edit
PARTIAL EDITING:
- You DON'T need to provide the entire file
- Provide 50-500 lines of context around the area you want to change
- Include 2-5 lines before and after the target section
- Tool will automatically find and replace that section in the file
PRIORITY:
1. fast_apply_edit - ALL file edits (default, 10x faster)
2. edit - Fallback if API fails
3. write - NEW files only
FORMAT:
Use \`// ... existing code ...\` markers for unchanged sections:
\`\`\`
// ... existing code ...
function updated() { return "modified"; }
// ... existing code ...
\`\`\`
RULES:
- MANDATORY: Read file first to get original_code
- Provide 50-500 lines of context (not entire file unless small)
- Use \`// ... existing code ...\` markers in code_edit
- Include 2-5 lines context before/after edits
- Preserve exact indentation and whitespace
- ONE edit block per call (multiple blocks = suboptimal)
EXAMPLE:
\`\`\`typescript
// 1. Read file
const content = await read("src/app.ts", { offset: 100, limit: 50 })
// 2. Call fast_apply_edit with partial context
fast_apply_edit({
target_filepath: "src/app.ts",
original_code: content, // Just 50 lines, not entire file!
code_edit: "... updated code ..."
})
\`\`\`
FALLBACK: If API fails, use native 'edit' tool.`
function escapeXmlTags(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
}
function unescapeXmlTags(text: string): string {
return text
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&/g, "&")
}
function validateNoNestedTags(content: string): void {
const unescaped = content
.replace(/</g, "<")
.replace(/>/g, ">")
if (unescaped.includes("<updated-code>") || unescaped.includes("</updated-code>")) {
throw new Error("Content contains unescaped tag-like sequences that could break parsing")
}
if (unescaped.includes("<code>") || unescaped.includes("</code>")) {
throw new Error("Content contains unescaped <code> tags that could break prompt structure")
}
if (unescaped.includes("<update>") || unescaped.includes("</update>")) {
throw new Error("Content contains unescaped <update> tags that could break prompt structure")
}
}
function extractUpdatedCode(raw: string): string {
const stripped = raw.trim()
const startRegex = /<updated-code\s*>/i
const endRegex = /<\/updated-code\s*>/i
const startMatch = stripped.match(startRegex)
if (!startMatch || startMatch.index === undefined) {
throw new Error("Missing or malformed <updated-code> start tag in AI response")
}
const startIdx = startMatch.index + startMatch[0].length
const remaining = stripped.slice(startIdx)
const endMatch = remaining.match(endRegex)
if (!endMatch || endMatch.index === undefined) {
throw new Error("Missing or malformed </updated-code> end tag in AI response")
}
const endIdx = endMatch.index
const inner = remaining.slice(0, endIdx)
if (!inner.trim()) {
throw new Error("Empty updated-code block in AI response")
}
const unescaped = unescapeXmlTags(inner)
validateNoNestedTags(unescaped)
return unescaped
}
function generateUnifiedDiff(
filepath: string,
original: string,
modified: string
): string {
const patch = createTwoFilesPatch(
`a/${filepath}`,
`b/${filepath}`,
original,
modified,
"",
"",
{ context: 3 }
)
if (!patch.includes("@@")) {
return "No changes detected"
}
return patch
}
/**
* Count additions and deletions from a unified diff
*/
function countChanges(diff: string): { added: number; removed: number } {
const lines = diff.split("\n")
let added = 0
let removed = 0
for (const line of lines) {
if (line.startsWith("+") && !line.startsWith("+++")) {
added++
} else if (line.startsWith("-") && !line.startsWith("---")) {
removed++
}
}
return { added, removed }
}
function formatTokenCount(tokens: number): string {
if (tokens >= 1000) {
return `${(tokens / 1000).toFixed(1)}K`.replace(".0K", "K")
}
return tokens.toString()
}
function shortenPath(filePath: string, workingDir: string): string {
if (filePath.startsWith(workingDir + "/")) {
return filePath.slice(workingDir.length + 1)
}
if (filePath === workingDir) {
return "."
}
return filePath
}
function estimateTokens(text: string): number {
return Math.ceil(text.length / 4)
}
function normalizeWhitespace(text: string): string {
return text
.split('\n')
.map(line => line.trimEnd())
.join('\n')
.replace(/\r\n/g, '\n')
.trim()
}
function findExactMatch(haystack: string, needle: string): number {
return haystack.indexOf(needle)
}
function findNormalizedMatch(haystack: string, needle: string): number {
const normalizedHaystack = normalizeWhitespace(haystack)
const normalizedNeedle = normalizeWhitespace(needle)
const index = normalizedHaystack.indexOf(normalizedNeedle)
if (index === -1) return -1
let actualIndex = 0
let normalizedIndex = 0
while (normalizedIndex < index && actualIndex < haystack.length) {
const char = haystack[actualIndex]
const normalizedChar = normalizedHaystack[normalizedIndex]
if (char === '\r' && haystack[actualIndex + 1] === '\n') {
actualIndex += 2
normalizedIndex += 1
} else if (char === normalizedChar) {
actualIndex++
normalizedIndex++
} else {
actualIndex++
}
}
return actualIndex
}
async function applyPartialEdit(
filepath: string,
original_code: string,
merged_code: string
): Promise<{ success: boolean; newFileContent?: string; error?: string }> {
const currentFile = await readFile(filepath, "utf-8")
if (currentFile.includes('\0')) {
return {
success: false,
error: "Cannot edit binary files"
}
}
let index = findExactMatch(currentFile, original_code)
let matchType = "exact"
if (index === -1) {
index = findNormalizedMatch(currentFile, original_code)
matchType = "normalized"
}
if (index === -1) {
return {
success: false,
error: `Cannot locate original_code in ${filepath}.
The content you provided doesn't match the current file.
POSSIBLE CAUSES:
- File was modified since you read it
- Whitespace or indentation differs
- Wrong section provided
- File encoding issues
SOLUTIONS:
1. Re-read the file to get current content
2. Verify exact whitespace and indentation
3. Provide more context (more surrounding lines)
4. Use native 'edit' tool for exact string matching`
}
}
const occurrences = currentFile.split(original_code).length - 1
if (occurrences > 1) {
return {
success: false,
error: `original_code appears ${occurrences} times in ${filepath}.
Please provide more context (more surrounding lines) to uniquely identify the section you want to edit.`
}
}
const newFileContent =
currentFile.substring(0, index) +
merged_code +
currentFile.substring(index + original_code.length)
return {
success: true,
newFileContent
}
}
function formatFastApplyResult(
filePath: string,
workingDir: string,
insertions: number,
deletions: number,
diffPreview: string,
modifiedTokens: number
): string {
const shortPath = shortenPath(filePath, workingDir)
const tokenStr = formatTokenCount(modifiedTokens)
const lines = [
"✓ Fast Apply complete",
"",
`File: ${shortPath}`,
`Changes: +${insertions} -${deletions} (~${tokenStr} tokens)`,
"",
"Unified diff:",
diffPreview
]
return lines.join("\n")
}
function formatErrorOutput(error: string, filePath: string, workingDir: string): string {
const shortPath = shortenPath(filePath, workingDir)
return [
"✗ Fast Apply failed",
"",
`File: ${shortPath}`,
`Error: ${error}`,
"",
"Fallback: Use native 'edit' tool with exact string matching"
].join("\n")
}
/**
* Call OpenAI's Fast Apply API to merge code edits
*/
async function callFastApply(
originalCode: string,
codeEdit: string
): Promise<{ success: boolean; content?: string; error?: string }> {
if (!FAST_APPLY_API_KEY) {
return {
success: false,
error:
"FAST_APPLY_API_KEY not set. Get one at https://openai.com/api",
}
}
try {
const escapedOriginalCode = escapeXmlTags(originalCode)
const escapedCodeEdit = escapeXmlTags(codeEdit)
const userContent = FAST_APPLY_USER_PROMPT
.replace("{original_code}", escapedOriginalCode)
.replace("{update_snippet}", escapedCodeEdit)
const response = await fetch(`${FAST_APPLY_URL}/v1/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${FAST_APPLY_API_KEY}`,
},
body: JSON.stringify({
model: FAST_APPLY_MODEL,
messages: [
{
role: "system",
content: FAST_APPLY_SYSTEM_PROMPT,
},
{
role: "user",
content: userContent,
},
],
temperature: 0,
}),
})
if (!response.ok) {
const errorText = await response.text()
return {
success: false,
error: `Fast Apply API error (${response.status}): ${errorText}`,
}
}
const result = (await response.json()) as {
choices: Array<{ message: { content: string } }>
}
const rawResponse = result.choices?.[0]?.message?.content
if (!rawResponse) {
return {
success: false,
error: "Fast Apply API returned empty response",
}
}
try {
const mergedCode = extractUpdatedCode(rawResponse)
return {
success: true,
content: mergedCode,
}
} catch (parseError) {
const error = parseError as Error
return {
success: false,
error: `Failed to parse AI response: ${error.message}`,
}
}
} catch (err) {
const error = err as Error
return {
success: false,
error: `Fast Apply API request failed: ${error.message}`,
}
}
}
async function sendTUIMessage(
client: any,
sessionID: string,
message: string,
params: SessionParams
): Promise<void> {
const agent = params.agent || undefined
const variant = params.variant || undefined
const model = params.providerId && params.modelId
? {
providerID: params.providerId,
modelID: params.modelId,
}
: undefined
try {
await client.session.prompt({
path: { id: sessionID },
body: {
noReply: true,
agent: agent,
model: model,
variant: variant,
parts: [
{
type: "text",
text: message,
ignored: true,
},
],
},
})
} catch (error: any) {
// Silently fail if notification cannot be sent
}
}
async function sendTUINotification(
client: any,
sessionID: string,
filePath: string,
workingDir: string,
insertions: number,
deletions: number,
modifiedTokens: number,
diff: string,
params: SessionParams
): Promise<void> {
const shortPath = shortenPath(filePath, workingDir)
const tokenStr = formatTokenCount(modifiedTokens)
const message = [
`▣ Fast Apply | ~${tokenStr} tokens modified`,
"",
`Applied changes to ${shortPath} (+${insertions} -${deletions}):`,
"",
diff
].join("\n")
await sendTUIMessage(client, sessionID, message, params)
}
async function sendTUIErrorNotification(
client: any,
sessionID: string,
filePath: string,
workingDir: string,
errorMessage: string,
params: SessionParams
): Promise<void> {
const shortPath = shortenPath(filePath, workingDir)
const message = [
`✗ Fast Apply Error`,
"",
`File: ${shortPath}`,
`Error: ${errorMessage}`,
"",
"Fallback: Use native 'edit' tool"
].join("\n")
await sendTUIMessage(client, sessionID, message, params)
}
export const FastApplyPlugin: Plugin = async ({ directory, client }) => {
return {
"chat.message": async (input: {
sessionID: string
agent?: string
model?: { providerID: string; modelID: string }
variant?: string
}) => {
sessionParamsCache.set(input.sessionID, {
agent: input.agent,
providerId: input.model?.providerID,
modelId: input.model?.modelID,
variant: input.variant,
})
},
tool: {
fast_apply_edit: tool({
description: TOOL_INSTRUCTIONS,
args: {
target_filepath: tool.schema
.string()
.describe("Path of the file to modify (relative to project root)"),
original_code: tool.schema
.string()
.describe(`The original code section to be modified.
IMPORTANT:
- Provide 50-500 lines of context around the area you want to change
- Include 2-5 lines before and after the target section
- Must match the current file content exactly (whitespace matters)
- Can be partial (doesn't need to be entire file)
WORKFLOW:
1. Read the file first to get current content
2. Extract the relevant section with context
3. Provide that section as original_code`),
code_edit: tool.schema
.string()
.describe(
'The updated code with changes applied. Use "// ... existing code ..." markers for unchanged sections within this context.'
),
},
async execute(args, toolCtx) {
const { target_filepath, original_code, code_edit } = args
const params = sessionParamsCache.get(toolCtx.sessionID) || {}
// Resolve file path relative to project directory
const filepath = target_filepath.startsWith("/")
? target_filepath
: `${directory}/${target_filepath}`
// Check if API key is available
if (!FAST_APPLY_API_KEY) {
return `Error: FAST_APPLY_API_KEY not configured.
To use fast_apply_edit, set the FAST_APPLY_API_KEY environment variable.
Get your API key at: https://openai.com/api
Alternatively, use the native 'edit' tool for this change.`
}
// Check if file exists and is writable
try {
await access(filepath, constants.R_OK | constants.W_OK)
} catch (err) {
return `Error: File not found or not writable: ${target_filepath}
This tool is for EDITING EXISTING FILES ONLY.
For new file creation, use the 'write' tool instead.
Example:
write({
filePath: "${target_filepath}",
content: "your file content here"
})`
}
// Call Fast Apply API to merge the edit
const result = await callFastApply(
original_code,
code_edit
)
if (!result.success || !result.content) {
const errorMsg = result.error || "Unknown error"
await sendTUIErrorNotification(
client,
toolCtx.sessionID,
target_filepath,
directory,
errorMsg,
params
)
return formatErrorOutput(errorMsg, target_filepath, directory)
}
const mergedCode = result.content
// Read original file content BEFORE applying changes (for diff comparison)
const originalFileContent = await readFile(filepath, "utf-8")
// Apply partial edit with smart matching
const applyResult = await applyPartialEdit(filepath, original_code, mergedCode)
if (!applyResult.success) {
await sendTUIErrorNotification(
client,
toolCtx.sessionID,
target_filepath,
directory,
applyResult.error!,
params
)
return formatErrorOutput(applyResult.error!, target_filepath, directory)
}
// Write merged file back
try {
await writeFile(filepath, applyResult.newFileContent!, "utf-8")
} catch (err) {
const error = err as Error
await sendTUIErrorNotification(
client,
toolCtx.sessionID,
target_filepath,
directory,
error.message,
params
)
return formatErrorOutput(error.message, target_filepath, directory)
}
// Generate diff comparing original content with merged content
const diff = generateUnifiedDiff(
target_filepath,
originalFileContent,
applyResult.newFileContent!
)
const { added, removed } = countChanges(diff)
const modifiedTokens = estimateTokens(diff)
await sendTUINotification(
client,
toolCtx.sessionID,
target_filepath,
directory,
added,
removed,
modifiedTokens,
diff,
params
)
return formatFastApplyResult(
target_filepath,
directory,
added,
removed,
diff,
modifiedTokens
)
},
}),
},
}
}
// Default export for OpenCode plugin loader
export default FastApplyPlugin