-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbasic.ts
More file actions
81 lines (66 loc) · 2.39 KB
/
basic.ts
File metadata and controls
81 lines (66 loc) · 2.39 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
/**
* Basic usage example - environment lifecycle and code execution
*/
import {
AgentDiff,
TypeScriptExecutorProxy,
BashExecutorProxy,
} from 'agent-diff';
async function main() {
// Initialize client (defaults to http://localhost:8000)
const client = new AgentDiff();
// 1. Initialize isolated environment from template
console.log('Initializing environment...');
const env = await client.initEnv({
templateService: 'slack',
templateName: 'slack_default',
impersonateUserId: 'U01AGENBOT9', // Seeded user ID from template
});
console.log(`Environment created: ${env.environmentId}`);
console.log(`Service URL: ${env.environmentUrl}`);
console.log(`Expires at: ${env.expiresAt}`);
// 2. Take before snapshot
console.log('\nTaking before snapshot...');
const run = await client.startRun({ envId: env.environmentId });
console.log(`Run started: ${run.runId}`);
// 3. Execute TypeScript code with network interception
console.log('\nExecuting TypeScript code...');
const tsExecutor = new TypeScriptExecutorProxy(
env.environmentId,
client.getBaseUrl()
);
const tsResult = await tsExecutor.execute(`
const response = await fetch('https://slack.com/api/conversations.list', {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
const data = await response.json();
console.log('Channels:', data.channels?.map(c => c.name).join(', '));
`);
console.log('TypeScript result:', tsResult.stdout);
// 4. Execute Bash code with curl interception
console.log('\nExecuting Bash code...');
const bashExecutor = new BashExecutorProxy(
env.environmentId,
client.getBaseUrl()
);
const bashResult = await bashExecutor.execute(`
curl -X POST https://slack.com/api/chat.postMessage \\
-H "Content-Type: application/json" \\
-d '{"channel": "C01ABCD1234", "text": "Hello from Agent Diff!"}'
`);
console.log('Bash result:', bashResult.stdout);
// 5. Get diff of changes
console.log('\nComputing diff...');
const diff = await client.diffRun({
runId: run.runId,
});
console.log('Before snapshot:', diff.beforeSnapshot);
console.log('After snapshot:', diff.afterSnapshot);
console.log('Diff:', JSON.stringify(diff.diff, null, 2));
// 6. Cleanup
console.log('\nCleaning up...');
await client.deleteEnv(env.environmentId);
console.log('Environment deleted');
}
main().catch(console.error);