-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcreate-sandbox.ts
More file actions
83 lines (74 loc) · 2.36 KB
/
create-sandbox.ts
File metadata and controls
83 lines (74 loc) · 2.36 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { UIMessageChunk } from 'ai'
import type { DataPart } from '../messages/data-parts'
import { Sandbox } from '@vercel/sandbox'
import { getRichError } from './get-rich-error'
import { tool } from 'ai'
import description from './create-sandbox.prompt'
import z from 'zod/v3'
import { getWritable } from 'workflow'
const inputSchema = z.object({
timeout: z
.number()
.min(600000)
.max(2700000)
.optional()
.describe(
'Maximum time in milliseconds the Vercel Sandbox will remain active before automatically shutting down. Minimum 600000ms (10 minutes), maximum 2700000ms (45 minutes). Defaults to 600000ms (10 minutes). The sandbox will terminate all running processes when this timeout is reached.'
),
ports: z
.array(z.number())
.max(2)
.optional()
.describe(
'Array of network ports to expose and make accessible from outside the Vercel Sandbox. These ports allow web servers, APIs, or other services running inside the Vercel Sandbox to be reached externally. Common ports include 3000 (Next.js), 8000 (Python servers), 5000 (Flask), etc.'
),
})
async function executeCreateSandbox(
{ timeout, ports }: z.infer<typeof inputSchema>,
{ toolCallId }: { toolCallId: string }
) {
'use step'
const writeable = getWritable<UIMessageChunk<never, DataPart>>()
const writer = writeable.getWriter()
writer.write({
id: toolCallId,
type: 'data-create-sandbox',
data: { status: 'loading' },
})
try {
const sandbox = await Sandbox.create({
timeout: timeout ?? 600000,
ports,
})
writer.write({
id: toolCallId,
type: 'data-create-sandbox',
data: { sandboxId: sandbox.sandboxId, status: 'done' },
})
return (
`Sandbox created with ID: ${sandbox.sandboxId}.` +
`\nYou can now upload files, run commands, and access services on the exposed ports.`
)
} catch (error) {
const richError = getRichError({
action: 'Creating Sandbox',
error,
})
writer.write({
id: toolCallId,
type: 'data-create-sandbox',
data: {
error: { message: richError.error.message },
status: 'error',
},
})
console.log('Error creating Sandbox:', richError.error)
return richError.message
}
}
export const createSandbox = () =>
tool({
description,
inputSchema,
execute: executeCreateSandbox,
})