-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path24-code-execution.ts
More file actions
82 lines (71 loc) · 2.53 KB
/
Copy path24-code-execution.ts
File metadata and controls
82 lines (71 loc) · 2.53 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
/**
* Code Execution -- sandboxed environments for running LLM-generated code.
*
* Demonstrates code executor types:
*
* 1. LocalCodeExecutor -- runs code in a local subprocess (no sandbox)
* 2. DockerCodeExecutor -- runs code inside a Docker container (sandboxed)
* 3. JupyterCodeExecutor -- runs code in a persistent Jupyter kernel
*
* Each executor is attached to an agent as a tool via `executor.asTool()`.
*
* Requirements:
* - Conductor server with LLM support
* - Docker (for DockerCodeExecutor example)
* - AGENTSPAN_SERVER_URL=http://localhost:8080/api as environment variable
* - AGENTSPAN_LLM_MODEL=openai/gpt-4o-mini as environment variable
*/
import {
Agent,
AgentRuntime,
LocalCodeExecutor,
DockerCodeExecutor,
} from '@io-orkes/conductor-javascript/agents';
import { llmModel } from './settings';
// -- Example 1: Local code execution ---------------------------------------
const localExecutor = new LocalCodeExecutor({ timeout: 10 });
export const coder = new Agent({
name: 'local_coder',
model: llmModel,
tools: [localExecutor.asTool()],
instructions:
'You are a Python developer. Write and execute code to solve problems. ' +
'Always use the code_executor tool to run your code and show results.',
});
// -- Example 2: Docker-sandboxed execution ---------------------------------
const dockerExecutor = new DockerCodeExecutor({
image: 'python:3.12-slim',
timeout: 15,
memoryLimit: '256m',
});
export const sandboxedCoder = new Agent({
name: 'sandboxed_coder',
model: llmModel,
tools: [dockerExecutor.asTool('run_sandboxed')],
instructions:
'You write Python code that runs in a sandboxed Docker container. ' +
'Use the run_sandboxed tool to execute code safely.',
});
// -- Run -------------------------------------------------------------------
async function main() {
const runtime = new AgentRuntime();
try {
console.log('--- Local Code Execution ---');
const result = await runtime.run(
coder,
'Write a Python function to find the first 10 Fibonacci numbers and print them.',
);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(coder);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples --agents local_coder
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(coder);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);