-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path10-code-execution.ts
More file actions
64 lines (55 loc) · 1.74 KB
/
Copy path10-code-execution.ts
File metadata and controls
64 lines (55 loc) · 1.74 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
/**
* 10 - Code Execution
*
* Demonstrates LocalCodeExecutor.asTool() attached to an agent.
* The agent can execute code to answer questions.
*/
import {
Agent,
AgentRuntime,
LocalCodeExecutor,
} from '@io-orkes/conductor-javascript/agents';
const MODEL = process.env.CONDUCTOR_AGENT_LLM_MODEL ?? 'openai/gpt-4o';
// -- Create a local code executor --
const executor = new LocalCodeExecutor({ timeout: 10 });
// -- Wrap as a tool --
const codeTool = executor.asTool('run_code');
// -- Agent with code execution --
export const codeAgent = new Agent({
name: 'code_agent',
model: MODEL,
instructions:
'You can execute code to solve problems. ' +
'Use the run_code tool to execute JavaScript code.',
tools: [codeTool],
codeExecutionConfig: {
enabled: true,
allowedLanguages: ['javascript', 'python'],
timeout: 10,
},
});
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(
codeAgent,
'Calculate the first 10 Fibonacci numbers using code.',
);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(codeAgent);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples --agents code_agent
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(codeAgent);
} finally {
await runtime.shutdown();
}
}
// Test executor directly
const directResult = executor.execute('console.log("Hello from code executor!")', 'javascript');
console.log('Direct execution:', directResult.output);
console.log('Success:', directResult.success);
main().catch(console.error);