File size: 1,531 Bytes
ce25387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { getPlatform } from '../platform'
import { Type } from '@sinclair/typebox'
import type { AgentTool } from '@mariozechner/pi-agent-core'
import { logAction } from '../services/action-logger'

export function createBashTool(): AgentTool<any, any> {
  return {
    name: 'bash',
    label: 'Execute Command',
    description: 'Execute a shell command and return its output.',
    parameters: Type.Object({
      command: Type.String({ description: 'The shell command to execute' }),
    }),
    execute: async (toolCallId, params) => {
      try {
        const { process: proc } = getPlatform()
        const result = await proc.exec(params.command, { timeout: 30000, maxBuffer: 1024 * 1024 })
        logAction('tool.bash', {
          command: params.command,
          exitCode: result.exitCode,
          outputLength: result.stdout.length,
        }, { actor: 'agent' })

        if (result.exitCode !== 0) {
          return {
            content: [{ type: 'text', text: `stdout:\n${result.stdout}\nstderr:\n${result.stderr}` }],
            details: { command: params.command, exitCode: result.exitCode, error: result.stderr },
          }
        }
        return {
          content: [{ type: 'text', text: result.stdout }],
          details: { command: params.command, exitCode: 0 },
        }
      } catch (e: any) {
        return {
          content: [{ type: 'text', text: `Error: ${e.message}` }],
          details: { command: params.command, exitCode: 1, error: e.message },
        }
      }
    },
  }
}