mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
feat(agent): migrate to pi-agent-core harness and implement DatabaseSessionStorage
This commit is contained in:
@@ -1,48 +1,54 @@
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { createBashTool } from '@mariozechner/pi-coding-agent'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { spawn } from 'node:child_process'
|
||||
import type { DatabaseSessionManager } from '../database-session-manager'
|
||||
|
||||
export const createSandboxedBashTool = (cwd: string, sessionManager: DatabaseSessionManager) => {
|
||||
return createBashTool(cwd, {
|
||||
operations: {
|
||||
async exec(command, executeCwd, { onData, signal, timeout }) {
|
||||
const wrappedCommand = await SandboxManager.wrapWithSandbox(command)
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('bash', ['-c', wrappedCommand], {
|
||||
cwd: executeCwd,
|
||||
detached: true,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...sessionManager.getSkillEnvs(),
|
||||
},
|
||||
})
|
||||
const BashParameters = Type.Object({
|
||||
command: Type.String({ description: 'The bash command to execute.' }),
|
||||
timeout: Type.Optional(Type.Number({ description: 'Maximum execution time in seconds.' })),
|
||||
})
|
||||
|
||||
let timedOut = false
|
||||
let timeoutHandle: NodeJS.Timeout | undefined
|
||||
type BashDetails = {
|
||||
exitCode: number | null
|
||||
stdout: string
|
||||
stderr: string
|
||||
}
|
||||
|
||||
if (timeout !== undefined && timeout > 0) {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
timedOut = true
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
} catch {
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}, timeout * 1000)
|
||||
}
|
||||
export const createSandboxedBashTool = (
|
||||
cwd: string,
|
||||
skillEnvs: Record<string, string> = {},
|
||||
): AgentTool<typeof BashParameters, BashDetails> => {
|
||||
return {
|
||||
name: 'bash',
|
||||
description: 'Execute a bash command in a sandboxed environment.',
|
||||
label: 'Executing bash command',
|
||||
parameters: BashParameters,
|
||||
execute: async (
|
||||
toolCallId,
|
||||
{ command, timeout },
|
||||
signal,
|
||||
): Promise<AgentToolResult<BashDetails>> => {
|
||||
const wrappedCommand = await SandboxManager.wrapWithSandbox(command)
|
||||
|
||||
child.stdout?.on('data', onData)
|
||||
child.stderr?.on('data', onData)
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('bash', ['-c', wrappedCommand], {
|
||||
cwd,
|
||||
detached: true,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...process.env,
|
||||
...skillEnvs,
|
||||
},
|
||||
})
|
||||
|
||||
child.on('error', (err) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
reject(err)
|
||||
})
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
let timedOut = false
|
||||
let timeoutHandle: NodeJS.Timeout | undefined
|
||||
|
||||
const onAbort = () => {
|
||||
if (timeout !== undefined && timeout > 0) {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
timedOut = true
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
@@ -50,24 +56,62 @@ export const createSandboxedBashTool = (cwd: string, sessionManager: DatabaseSes
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}
|
||||
}, timeout * 1000)
|
||||
}
|
||||
|
||||
signal?.addEventListener('abort', onAbort, { once: true })
|
||||
|
||||
child.on('close', (code) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
|
||||
if (signal?.aborted) {
|
||||
reject(new Error('aborted'))
|
||||
} else if (timedOut) {
|
||||
reject(new Error(`timeout:${timeout}`))
|
||||
} else {
|
||||
resolve({ exitCode: code })
|
||||
}
|
||||
})
|
||||
child.stdout?.on('data', (data) => {
|
||||
stdout += data.toString()
|
||||
})
|
||||
},
|
||||
child.stderr?.on('data', (data) => {
|
||||
stderr += data.toString()
|
||||
})
|
||||
|
||||
const onAbort = () => {
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
} catch {
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signal?.addEventListener('abort', onAbort, { once: true })
|
||||
|
||||
child.on('error', (err) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
reject(err)
|
||||
})
|
||||
|
||||
child.on('close', (code) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
|
||||
if (signal?.aborted) {
|
||||
reject(new Error('aborted'))
|
||||
} else if (timedOut) {
|
||||
reject(new Error(`bash command timed out after ${timeout} seconds`))
|
||||
} else if (code !== 0 && code !== null) {
|
||||
reject(new Error(`bash command exited with code ${code}`))
|
||||
} else {
|
||||
const output = stdout + stderr
|
||||
resolve({
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: output || (code === 0 ? 'Success' : `Failed with exit code ${code}`),
|
||||
},
|
||||
],
|
||||
details: {
|
||||
exitCode: code,
|
||||
stdout,
|
||||
stderr,
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user