feat(agent): integrate sandboxed bash tool and sandbox settings

This commit is contained in:
yiling
2026-05-20 10:36:47 +08:00
parent 9cad96210c
commit 53594194cc
32 changed files with 2205 additions and 110 deletions
@@ -0,0 +1,150 @@
import { useState } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { client } from '@/ui/api/client'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card'
import { Input } from '@/ui/components/ui/input'
import { Button } from '@/ui/components/ui/button'
import { Loader2, Plus, X, Globe } from 'lucide-react'
import { toast } from 'sonner'
export function SandboxSettings({ teamId }: { teamId: string }) {
const queryClient = useQueryClient()
const [newDomain, setNewDomain] = useState('')
const { data: sandbox, isLoading } = useQuery({
queryKey: ['teams', teamId, 'sandbox'],
queryFn: async () => {
const res = await client.api.teams[':teamId'].sandbox.$get({
param: { teamId },
})
if (!res.ok) throw new Error('Failed to fetch sandbox settings')
return await res.json()
},
})
const { mutate: updateSandbox, isPending: isUpdating } = useMutation({
mutationFn: async (allowedDomains: string[]) => {
const res = await client.api.teams[':teamId'].sandbox.$put({
param: { teamId },
json: { allowedDomains },
})
if (!res.ok) throw new Error('Failed to update sandbox settings')
return await res.json()
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'sandbox'] })
toast.success('Sandbox settings updated')
},
onError: (err) => {
toast.error(err instanceof Error ? err.message : 'Failed to update sandbox settings')
},
})
const handleAddDomain = (e: React.FormEvent) => {
e.preventDefault()
if (!newDomain.trim()) return
if (sandbox?.allowedDomains.includes(newDomain.trim())) {
toast.error('Domain already exists')
return
}
const updated = [...(sandbox?.allowedDomains || []), newDomain.trim()]
updateSandbox(updated)
setNewDomain('')
}
const handleRemoveDomain = (domain: string) => {
const updated = (sandbox?.allowedDomains || []).filter((d) => d !== domain)
updateSandbox(updated)
}
if (isLoading) {
return (
<div className="flex items-center justify-center p-8">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
)
}
return (
<div className="h-full overflow-y-auto space-y-6 pr-1">
<Card>
<CardHeader>
<div className="flex items-center gap-2">
<Globe className="w-5 h-5 text-blue-500" />
<CardTitle>Network Sandbox</CardTitle>
</div>
<CardDescription>
Configure allowed domains for the sandboxed agent. By default, only essential domains
are allowed.
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleAddDomain} className="flex gap-2 mb-6">
<Input
placeholder="e.g. api.openai.com"
value={newDomain}
onChange={(e) => setNewDomain(e.target.value)}
disabled={isUpdating}
/>
<Button type="submit" disabled={isUpdating}>
{isUpdating ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Plus className="w-4 h-4 mr-2" />
)}
Add Domain
</Button>
</form>
<div className="space-y-2">
<h4 className="text-sm font-medium text-slate-500 mb-3">Allowed Domains</h4>
<div className="flex flex-wrap gap-2">
{sandbox?.allowedDomains.map((domain) => (
<div
key={domain}
className="flex items-center gap-1 px-3 py-1.5 bg-slate-100 dark:bg-slate-800 rounded-full text-sm border border-slate-200 dark:border-slate-700 transition-all hover:border-slate-300 dark:hover:border-slate-600"
>
<span className="font-medium text-slate-700 dark:text-slate-300">{domain}</span>
<button
onClick={() => handleRemoveDomain(domain)}
className="ml-1 text-slate-400 hover:text-red-500 transition-colors"
disabled={isUpdating}
>
<X className="w-3.5 h-3.5" />
</button>
</div>
))}
{sandbox?.allowedDomains.length === 0 && (
<div className="text-sm text-slate-400 italic">No custom domains allowed</div>
)}
</div>
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Filesystem Restriction</CardTitle>
<CardDescription>
The agent is restricted to reading and writing only within the <code>.pi</code> and{' '}
<code>/tmp</code> folders. These settings are currently hardcoded for security.
</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-2">
<div className="flex items-center gap-2 text-sm text-slate-600 dark:text-slate-400">
<div className="w-1.5 h-1.5 rounded-full bg-green-500" />
<span>
Allow Write: <code>.pi</code>, <code>/tmp</code>
</span>
</div>
<div className="flex items-center gap-2 text-sm text-slate-600 dark:text-slate-400">
<div className="w-1.5 h-1.5 rounded-full bg-red-500" />
<span>Deny Read: All other paths</span>
</div>
</div>
</CardContent>
</Card>
</div>
)
}
+28 -2
View File
@@ -10,9 +10,10 @@ import { useState } from 'react'
import { ProvidersSettings } from '@/ui/components/settings/ProvidersSettings'
import { SkillsConfigCard } from '@/ui/components/settings/SkillsConfigCard'
import { AgentsSettings } from '@/ui/components/settings/AgentsSettings'
import { Bot } from 'lucide-react'
import { SandboxSettings } from '@/ui/components/settings/SandboxSettings'
import { Bot, ShieldCheck } from 'lucide-react'
type SettingsTab = 'general' | 'transcode' | 'skills' | 'providers' | 'agents'
type SettingsTab = 'general' | 'transcode' | 'skills' | 'providers' | 'agents' | 'sandbox'
function TeamSettingsPage() {
const { teamId } = Route.useParams()
@@ -196,6 +197,22 @@ function TeamSettingsPage() {
<div className="ml-auto w-1.5 h-1.5 rounded-full bg-blue-600 dark:bg-blue-400" />
)}
</button>
<button
onClick={() => setActiveTab('sandbox')}
className={cn(
'w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-lg transition-all',
activeTab === 'sandbox'
? 'bg-blue-50 dark:bg-blue-900/20 text-blue-700 dark:text-blue-300 ring-1 ring-blue-200 dark:ring-blue-800'
: 'text-slate-600 dark:text-slate-400 hover:bg-slate-50 dark:hover:bg-slate-800 hover:text-slate-900 dark:hover:text-white',
)}
>
<ShieldCheck className="w-5 h-5" />
Sandbox
{activeTab === 'sandbox' && (
<div className="ml-auto w-1.5 h-1.5 rounded-full bg-blue-600 dark:bg-blue-400" />
)}
</button>
</nav>
</aside>
@@ -210,6 +227,7 @@ function TeamSettingsPage() {
{activeTab === 'skills' && 'Skills Management'}
{activeTab === 'providers' && 'AI Providers'}
{activeTab === 'agents' && 'AI Agents'}
{activeTab === 'sandbox' && 'AI Sandbox Settings'}
</h2>
<p className="text-sm text-slate-500 dark:text-slate-400 mt-1">
{activeTab === 'general' && 'View your personal information and team role.'}
@@ -219,6 +237,8 @@ function TeamSettingsPage() {
{activeTab === 'providers' &&
'Configure AI providers and their models for this team.'}
{activeTab === 'agents' && 'Manage AI agents and their personalities.'}
{activeTab === 'sandbox' &&
'Configure security and network restrictions for the AI agent.'}
</p>
</div>
</div>
@@ -397,6 +417,12 @@ function TeamSettingsPage() {
<AgentsSettings teamId={teamId} />
</div>
)}
{activeTab === 'sandbox' && (
<div className="h-full overflow-y-auto pr-1">
<SandboxSettings teamId={teamId} />
</div>
)}
</div>
</div>
</main>