forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude-code-cli.ts
More file actions
109 lines (94 loc) · 3.81 KB
/
Copy pathclaude-code-cli.ts
File metadata and controls
109 lines (94 loc) · 3.81 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
import { createCliAgent } from './lib/create-cli-agent'
import type { AgentDefinition } from './types/agent-definition'
const baseDefinition = createCliAgent({
id: 'claude-code-cli',
displayName: 'Claude Code CLI',
cliName: 'Claude Code',
shortName: 'claude-code',
startCommand: 'claude --dangerously-skip-permissions',
permissionNote:
'Always use `--dangerously-skip-permissions` when testing to avoid permission prompts that would block automated tests.',
model: 'anthropic/claude-opus-4.7',
})
// Constants must be inside handleSteps since it gets serialized via .toString()
const definition: AgentDefinition = {
...baseDefinition,
handleSteps: function* ({ prompt, params, logger }) {
const START_COMMAND = 'claude --dangerously-skip-permissions'
const CLI_NAME = 'Claude Code'
yield {
toolName: 'add_message',
input: {
role: 'user',
content: 'Before starting the ' + CLI_NAME + ' CLI session, gather context by reading relevant files and understanding the task to provide better guidance to the CLI.',
},
includeToolCall: false,
}
yield 'STEP'
logger.info('Starting ' + CLI_NAME + ' tmux session...')
const { toolResult } = yield {
toolName: 'run_terminal_command',
input: {
command: './scripts/tmux/tmux-cli.sh start --command "' + START_COMMAND + '"',
timeout_seconds: 30,
},
}
// Parse response from tmux-cli.sh (outputs plain session name on success, error to stderr on failure)
let sessionName = ''
let parseError = ''
const result = toolResult?.[0]
if (result && result.type === 'json') {
const value = result.value as Record<string, unknown>
const stdout = typeof value?.stdout === 'string' ? value.stdout.trim() : ''
const stderr = typeof value?.stderr === 'string' ? value.stderr.trim() : ''
const exitCode = typeof value?.exitCode === 'number' ? value.exitCode : undefined
if (!stdout && !stderr) {
parseError = 'tmux-cli.sh returned empty output'
} else if (exitCode !== 0 || !stdout) {
parseError = stderr || 'tmux-cli.sh failed with no error message'
} else {
sessionName = stdout
}
} else {
parseError = 'Unexpected result type from run_terminal_command'
}
if (!sessionName) {
const errorMsg = parseError || 'Session name was empty'
logger.error({ parseError: errorMsg }, 'Failed to start tmux session')
yield {
toolName: 'set_output',
input: {
overallStatus: 'failure',
summary: 'Failed to start ' + CLI_NAME + ' tmux session. ' + errorMsg,
sessionName: '',
scriptIssues: [
{
script: 'tmux-cli.sh',
issue: errorMsg,
errorOutput: JSON.stringify(toolResult),
suggestedFix: 'Ensure tmux-cli.sh outputs the session name to stdout and exits with code 0. Check that tmux is installed.',
},
],
captures: [],
},
}
return
}
logger.info('Successfully started tmux session: ' + sessionName)
yield {
toolName: 'add_message',
input: {
role: 'user',
content: 'A ' + CLI_NAME + ' tmux session has been started: `' + sessionName + '`\n\n' +
'Use this session for all CLI interactions. The session name must be included in your final output.\n\n' +
'Proceed with the task using the helper scripts:\n' +
'- Send commands: `./scripts/tmux/tmux-cli.sh send "' + sessionName + '" "..."`\n' +
'- Capture output: `./scripts/tmux/tmux-cli.sh capture "' + sessionName + '" --label "..."`\n' +
'- Stop when done: `./scripts/tmux/tmux-cli.sh stop "' + sessionName + '"`',
},
includeToolCall: false,
}
yield 'STEP_ALL'
},
}
export default definition