mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 19:04:23 +00:00
feat(agent): integrate sandboxed bash tool and sandbox settings
This commit is contained in:
+105
-2
@@ -10,14 +10,17 @@ import {
|
||||
SessionManager,
|
||||
SettingsManager,
|
||||
defineTool,
|
||||
createBashTool,
|
||||
} from '@mariozechner/pi-coding-agent'
|
||||
import { Type, type TSchema } from '@sinclair/typebox'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { DatabaseSessionManager } from './database-session-manager'
|
||||
import { analyzeAssetMediaTool } from './tools/analyze-asset-media'
|
||||
import { readSkillTool } from './tools/read-skill'
|
||||
import { createReadSkillTool } from './tools/read-skill'
|
||||
import { Usage } from '@/services/ai/provider/provider'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { spawn } from 'node:child_process'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type Tool = any
|
||||
@@ -37,6 +40,12 @@ export class AgentExecutor {
|
||||
})
|
||||
}
|
||||
|
||||
private async getSandbox(teamId: string) {
|
||||
return this.prismaClient.sandbox.findUnique({
|
||||
where: { teamId },
|
||||
})
|
||||
}
|
||||
|
||||
async chat(teamId: string, prompt: string): Promise<{ text: string; usage: Usage }> {
|
||||
const agent = await this.prismaClient.agent.findFirst({
|
||||
where: {
|
||||
@@ -163,6 +172,95 @@ export class AgentExecutor {
|
||||
cwd: process.cwd(),
|
||||
})
|
||||
|
||||
const sandbox = await this.getSandbox(teamId)
|
||||
const allowedDomains = sandbox?.allowedDomains || [
|
||||
'github.com',
|
||||
'api.github.com',
|
||||
'raw.githubusercontent.com',
|
||||
]
|
||||
|
||||
// Initialize SandboxManager for this session
|
||||
await SandboxManager.initialize({
|
||||
network: {
|
||||
allowedDomains,
|
||||
deniedDomains: [],
|
||||
},
|
||||
filesystem: {
|
||||
allowWrite: ['.pi', '/tmp'],
|
||||
denyRead: ['*'], // Deny everything else
|
||||
denyWrite: [],
|
||||
allowRead: ['.pi', '/tmp'],
|
||||
},
|
||||
})
|
||||
|
||||
const sandboxedBash = createBashTool(process.cwd(), {
|
||||
operations: {
|
||||
async exec(command, cwd, { onData, signal, timeout }) {
|
||||
const wrappedCommand = await SandboxManager.wrapWithSandbox(command)
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('bash', ['-c', wrappedCommand], {
|
||||
cwd,
|
||||
detached: true,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...process.env,
|
||||
...sessionManager.getSkillEnvs(),
|
||||
},
|
||||
})
|
||||
|
||||
let timedOut = false
|
||||
let timeoutHandle: NodeJS.Timeout | undefined
|
||||
|
||||
if (timeout !== undefined && timeout > 0) {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
timedOut = true
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
} catch {
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}, timeout * 1000)
|
||||
}
|
||||
|
||||
child.stdout?.on('data', onData)
|
||||
child.stderr?.on('data', onData)
|
||||
|
||||
child.on('error', (err) => {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle)
|
||||
reject(err)
|
||||
})
|
||||
|
||||
const onAbort = () => {
|
||||
if (child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, 'SIGKILL')
|
||||
} catch {
|
||||
child.kill('SIGKILL')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const { session } = await createAgentSession({
|
||||
cwd: process.cwd(),
|
||||
agentDir,
|
||||
@@ -172,7 +270,12 @@ export class AgentExecutor {
|
||||
resourceLoader,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
sessionManager: sessionManager as any as SessionManager,
|
||||
customTools: [analyzeAssetMediaTool, readSkillTool, ...tools],
|
||||
customTools: [
|
||||
analyzeAssetMediaTool,
|
||||
createReadSkillTool(sessionManager),
|
||||
sandboxedBash,
|
||||
...tools,
|
||||
],
|
||||
})
|
||||
const content: (TextContent | ImageContent)[] = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user