Skip to content

Commit 177b33f

Browse files
committed
Add apply patch to tool registry
1 parent c2f5235 commit 177b33f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, expect, test } from 'bun:test'
2+
import React from 'react'
3+
import { renderToStaticMarkup } from 'react-dom/server'
4+
5+
import { initializeThemeStore } from '../../../hooks/use-theme'
6+
import { chatThemes } from '../../../utils/theme-system'
7+
import { getToolComponent, renderToolComponent } from '../registry'
8+
9+
import type { ToolBlock } from '../types'
10+
11+
initializeThemeStore()
12+
13+
const createToolBlock = (
14+
operation: Record<string, unknown>,
15+
): ToolBlock & { toolName: 'apply_patch' } => ({
16+
type: 'tool',
17+
toolName: 'apply_patch',
18+
toolCallId: 'apply-patch-test-id',
19+
input: { operation },
20+
})
21+
22+
const renderOptions = {
23+
availableWidth: 80,
24+
indentationOffset: 0,
25+
labelWidth: 0,
26+
}
27+
28+
describe('ApplyPatchComponent', () => {
29+
test('is registered for apply_patch tool calls', () => {
30+
expect(getToolComponent('apply_patch')).toBeDefined()
31+
})
32+
33+
test('renders create_file operation', () => {
34+
const toolBlock = createToolBlock({
35+
type: 'create_file',
36+
path: 'src/new-file.ts',
37+
diff: '@@\n+export const value = 1\n',
38+
})
39+
40+
const result = renderToolComponent(toolBlock, chatThemes.dark, renderOptions)
41+
42+
expect(result).toBeDefined()
43+
expect(result?.content).toBeDefined()
44+
45+
const markup = renderToStaticMarkup(result?.content as React.ReactElement)
46+
expect(markup).toContain('Create')
47+
expect(markup).toContain('src/new-file.ts')
48+
})
49+
50+
test('renders update_file operation with diff content', () => {
51+
const toolBlock = createToolBlock({
52+
type: 'update_file',
53+
path: 'src/existing.ts',
54+
diff: '@@\n-oldLine\n+newLine\n',
55+
})
56+
57+
const result = renderToolComponent(toolBlock, chatThemes.dark, renderOptions)
58+
59+
expect(result).toBeDefined()
60+
expect(result?.content).toBeDefined()
61+
62+
const markup = renderToStaticMarkup(result?.content as React.ReactElement)
63+
expect(markup).toContain('Edit')
64+
expect(markup).toContain('src/existing.ts')
65+
expect(markup).toContain('-oldLine')
66+
expect(markup).toContain('+newLine')
67+
})
68+
69+
test('renders delete_file operation', () => {
70+
const toolBlock = createToolBlock({
71+
type: 'delete_file',
72+
path: 'src/remove-me.ts',
73+
})
74+
75+
const result = renderToolComponent(toolBlock, chatThemes.dark, renderOptions)
76+
77+
expect(result).toBeDefined()
78+
expect(result?.content).toBeDefined()
79+
80+
const markup = renderToStaticMarkup(result?.content as React.ReactElement)
81+
expect(markup).toContain('Delete')
82+
expect(markup).toContain('src/remove-me.ts')
83+
})
84+
})

cli/src/components/tools/registry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ApplyPatchComponent } from './apply-patch'
12
import { CodeSearchComponent } from './code-search'
23
import { GlobComponent } from './glob'
34
import { ListDirectoryComponent } from './list-directory'
@@ -26,6 +27,7 @@ import type { ToolName } from '@codebuff/sdk'
2627
* Add new tool components here to make them available in the CLI.
2728
*/
2829
const toolComponentRegistry = new Map<ToolName, ToolComponent>([
30+
[ApplyPatchComponent.toolName, ApplyPatchComponent],
2931
[CodeSearchComponent.toolName, CodeSearchComponent],
3032
[GlobComponent.toolName, GlobComponent],
3133
[ListDirectoryComponent.toolName, ListDirectoryComponent],

0 commit comments

Comments
 (0)