feat(sandbox): add pending domains support for network sandbox

This commit is contained in:
yiling
2026-06-15 00:01:55 +08:00
parent 9c3b808809
commit 6a63e99044
14 changed files with 242 additions and 31 deletions
+42 -5
View File
@@ -97,7 +97,10 @@ If you need to create files in the local filesystem (for example, a temporary fi
systemPrompt += `\n\nYour current model supports the following input types: ${modelConfig.input.join(', ')}.`
}
let blockedHost = ''
const { session, harness } = await createAgentSession({
teamId: params.teamId,
agentId: params.agentId,
providerName,
modelId,
@@ -121,6 +124,9 @@ If you need to create files in the local filesystem (for example, a temporary fi
config: m.config,
})),
})),
onNetworkBlocked: (host) => {
blockedHost = host
},
})
const imagesToPass: ImageContent[] = []
@@ -142,6 +148,24 @@ If you need to create files in the local filesystem (for example, a temporary fi
try {
const assistantMessage = await harness.prompt(params.prompt, { images: imagesToPass })
const storage = session.getStorage()
let sessionId = ''
if (storage instanceof DatabaseSessionStorage) {
sessionId = storage.sessionId
}
if (blockedHost) {
return {
text: `Network request to ${blockedHost} is blocked, please ask admin to allow it in sandbox settings.`,
usage: {
model: modelId,
inputTokens: assistantMessage.usage?.input || 0,
outputTokens: assistantMessage.usage?.output || 0,
},
sessionId,
}
}
const sessionEntries = await session.getEntries()
sessionEntries.forEach((entry) => {
if (entry.type === 'message') {
@@ -178,12 +202,25 @@ If you need to create files in the local filesystem (for example, a temporary fi
outputTokens: assistantMessage.usage?.output || 0,
}
const storage = session.getStorage()
let sessionId = ''
if (storage instanceof DatabaseSessionStorage) {
sessionId = storage.sessionId
}
return { text, usage, sessionId }
} catch (err) {
if (blockedHost) {
const storage = session.getStorage()
let sessionId = ''
if (storage instanceof DatabaseSessionStorage) {
sessionId = storage.sessionId
}
return {
text: `Network request to ${blockedHost} is blocked, please ask admin to allow it in sandbox settings.`,
usage: {
model: modelId,
inputTokens: 0,
outputTokens: 0,
},
sessionId,
}
}
throw err
} finally {
// Cleanup handled by agent session if needed
}
@@ -316,6 +316,7 @@ describe('DatabaseSessionStorage', () => {
// 6. Resume the session via createAgentSession
await createAgentSession({
teamId: 't1',
agentId: agent.id,
providerName: 'test-provider',
modelId: 'test-model',
@@ -424,6 +425,7 @@ describe('DatabaseSessionStorage', () => {
// 3. Resume the session via createAgentSession with a NEW comment ID
await createAgentSession({
teamId: 't1',
agentId: agent.id,
providerName: 'test-provider',
modelId: 'test-model',
+50 -10
View File
@@ -5,6 +5,7 @@ import { getModel } from '@earendil-works/pi-ai'
import { Type, type TSchema } from '@sinclair/typebox'
import * as fs from 'fs'
import * as path from 'path'
import { prisma } from '@shumai/db'
import { agentService } from '@shumai/core/src/agent/agent'
import { DatabaseSessionStorage } from './database-session-storage'
import { createAnalyzeImageTool } from './tools/analyze-image'
@@ -17,6 +18,7 @@ import { createReadSkillTool } from './tools/read-skill'
import { createSandboxedBashTool } from './tools/sandboxed-bash'
export interface CreateAgentSessionParams {
teamId: string
agentId: string
providerName: string
modelId: string
@@ -36,10 +38,12 @@ export interface CreateAgentSessionParams {
config: PrismaJson.ModelConfig
}>
}>
onNetworkBlocked?: (host: string) => void
}
export async function createAgentSession(params: CreateAgentSessionParams) {
const {
teamId,
agentId,
providerName,
modelId,
@@ -115,18 +119,54 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
const allowWrite = [piDir]
const sandboxAskCallback = async ({ host, port }: { host: string; port?: number }) => {
const hostPort = port ? `${host}:${port}` : host
try {
const sandbox = await prisma.sandbox.findUnique({
where: { teamId },
})
const pendingDomains = sandbox?.pendingDomains || []
if (!pendingDomains.includes(hostPort)) {
await prisma.sandbox.upsert({
where: { teamId },
create: {
teamId,
pendingDomains: [hostPort],
},
update: {
pendingDomains: {
push: hostPort,
},
},
})
}
} catch (err) {
console.error('Failed to update sandbox pending domains:', err)
}
if (params.onNetworkBlocked) {
params.onNetworkBlocked(hostPort)
}
return false
}
// SandboxManager.initialize is a global operation that applies to the entire process.
await SandboxManager.initialize({
network: {
allowedDomains,
deniedDomains: [],
await SandboxManager.initialize(
{
network: {
allowedDomains,
deniedDomains: [],
},
filesystem: {
denyRead: ['.env', '.env.*', '*.pem', '*.key'],
allowWrite,
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
},
},
filesystem: {
denyRead: ['.env', '.env.*', '*.pem', '*.key'],
allowWrite,
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
},
})
sandboxAskCallback,
)
const skillEnvs: Record<string, string> = {}
const onEnvsAdded = (envs: Record<string, string>) => {