-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path39c-serverless-code-execution.ts
More file actions
109 lines (94 loc) · 3.36 KB
/
Copy path39c-serverless-code-execution.ts
File metadata and controls
109 lines (94 loc) · 3.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 39c - Serverless Code Execution
*
* The ServerlessCodeExecutor sends code to an HTTP endpoint and returns
* the result. Use this to offload execution to a hosted sandbox, AWS Lambda,
* Google Cloud Functions, or any service that accepts a JSON payload.
*
* This example starts a tiny local HTTP server to simulate the remote service,
* then runs an agent that executes code through it.
*
* Requirements:
* - Conductor server with LLM support
* - CONDUCTOR_SERVER_URL=http://localhost:8080/api as environment variable
* - CONDUCTOR_AGENT_LLM_MODEL=openai/gpt-4o-mini as environment variable
*/
import { createServer } from 'http';
import { execSync } from 'child_process';
import { Agent, AgentRuntime, ServerlessCodeExecutor } from '@io-orkes/conductor-javascript/agents';
import { llmModel } from './settings';
// -- Tiny mock execution server -----------------------------------------------
const PORT = 9753;
const server = createServer((req, res) => {
if (req.method !== 'POST') {
res.writeHead(405);
res.end();
return;
}
let body = '';
req.on('data', (chunk: Buffer) => { body += chunk.toString(); });
req.on('end', () => {
const parsed = JSON.parse(body) as { code?: string; timeout?: number };
const code = parsed.code ?? '';
const timeout = (parsed.timeout ?? 10) * 1000;
let result: { output: string; error: string; exit_code: number };
try {
const output = execSync(`python3 -c ${JSON.stringify(code)}`, {
timeout,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
result = { output, error: '', exit_code: 0 };
} catch (err: unknown) {
const e = err as { stdout?: string; stderr?: string; status?: number };
result = {
output: e.stdout ?? '',
error: e.stderr ?? String(err),
exit_code: e.status ?? 1,
};
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(result));
});
});
server.listen(PORT, '127.0.0.1');
// -- Agent setup --------------------------------------------------------------
const serverlessExecutor = new ServerlessCodeExecutor({
endpoint: `http://127.0.0.1:${PORT}/execute`,
timeout: 15,
});
export const serverlessCoder = new Agent({
name: 'serverless_coder',
model: llmModel,
tools: [serverlessExecutor.asTool('execute_code')],
codeExecutionConfig: {
enabled: true,
},
instructions:
'You write Python code that runs on a remote execution service. ' +
'Use the execute_code tool to run code remotely.',
});
// -- Run ----------------------------------------------------------------------
async function main() {
const runtime = new AgentRuntime();
try {
console.log('--- Serverless Code Execution ---');
const result = await runtime.run(
serverlessCoder,
'Calculate 2**100 and print the result.',
);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(serverlessCoder);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples --agents serverless_coder
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(serverlessCoder);
} finally {
server.close();
await runtime.shutdown();
}
}
main().catch(console.error);