Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/nodejs-project-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function main() {
2. A package.json with necessary dependencies
3. A README.md with setup instructions
4. A simple route that returns "Hello World"
5. Include a console log message "Listening on port 3000"
5. Include a console log message "Listening on port 1234"

Please format your response as follows:
FILE: filename
Expand Down Expand Up @@ -83,6 +83,7 @@ Separate each file with a blank line.`
cwd: '/project'
},
dependencies: ['express'],
ports: [1234],
streamOutput: {
dependencyStdout: (data) => {
console.log('Dependency stdout:', data);
Expand Down
5 changes: 4 additions & 1 deletion src/code-execution-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function createCodeExecutionTool(config: CodeExecutionToolConfig = {}) {
entryFile: z.string().describe('Path to the entry file relative to the mounted directory'),
cwd: z.string().describe('Working directory path that should be mounted')
}).optional().describe('Optional configuration for running an entire application'),
ports: z.array(z.number()).optional().describe('Ports to expose from the container to the host'),
streamOutput: z.object({
stdout: z.function().args(z.string()).optional(),
stderr: z.function().args(z.string()).optional(),
Expand All @@ -67,6 +68,7 @@ export function createCodeExecutionTool(config: CodeExecutionToolConfig = {}) {
// sessionId,
environment = {},
runApp,
ports,
streamOutput
}: z.infer<typeof codeExecutionSchema>): Promise<CodeExecutionResult> => {
const strategy = config.defaultStrategy ?? 'per_execution';
Expand All @@ -77,7 +79,8 @@ export function createCodeExecutionTool(config: CodeExecutionToolConfig = {}) {
containerConfig: {
image: getImageForLanguage(language),
environment,
mounts: config.mounts
mounts: config.mounts,
ports
}
});

Expand Down
13 changes: 13 additions & 0 deletions src/container-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ export class ContainerManager {
mountsWithWorkspace.push({ type: 'directory', source: workspaceDir, target: '/workspace' });
}

// Handle port bindings if provided
const exposedPorts: Record<string, {}> = {};
const portBindings: Record<string, Array<{ HostPort: string }>> = {};
if (config.ports && config.ports.length > 0) {
for (const p of config.ports) {
const key = `${p}/tcp`;
exposedPorts[key] = {};
portBindings[key] = [{ HostPort: p.toString() }];
}
}

const container = await this.docker.createContainer({
name: containerName,
Image: config.image,
Expand All @@ -122,6 +133,7 @@ export class ContainerManager {
CpuPeriod: 100000,
CpuQuota: 50000,
NetworkMode: 'bridge',
PortBindings: Object.keys(portBindings).length > 0 ? portBindings : undefined,
Mounts: mountsWithWorkspace.map(mount => ({
Target: mount.target,
Source: mount.source,
Expand All @@ -130,6 +142,7 @@ export class ContainerManager {
}))
},
WorkingDir: '/workspace',
ExposedPorts: Object.keys(exposedPorts).length > 0 ? exposedPorts : undefined,
Cmd: ['sh', '-c', 'mkdir -p /workspace && tail -f /dev/null']
});

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ContainerConfig {
mounts?: MountOptions[];
environment?: Record<string, string>;
name?: string;
ports?: number[]; // Host ports to publish (TCP)
}

export interface ExecutionResult {
Expand Down