Skip to content

Commit b1771a4

Browse files
committed
changeset for pause resume api and example
1 parent c0b4519 commit b1771a4

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

.changeset/dirty-drinks-serve.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"magnitude-core": patch
3+
---
4+
5+
pause resume api
6+
7+
author: @ddwang
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Example: Using agent.pause() / agent.resume() for human-in-the-loop control
3+
*
4+
* Pauses the agent after every action so you can review and decide
5+
* whether to continue or stop.
6+
*/
7+
import { startBrowserAgent } from '../src/agent/browserAgent';
8+
import * as readline from 'readline';
9+
10+
function askUser(question: string): Promise<string> {
11+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
12+
return new Promise((resolve) => {
13+
rl.question(question, (answer) => {
14+
rl.close();
15+
resolve(answer.trim().toLowerCase());
16+
});
17+
});
18+
}
19+
20+
async function main() {
21+
const agent = await startBrowserAgent({
22+
url: 'https://magnitodo.com',
23+
narrate: true,
24+
});
25+
26+
// Pause after every action for human review
27+
agent.events.on('actionDone', () => {
28+
agent.pause();
29+
});
30+
31+
// On each pause, prompt the user
32+
agent.events.on('pause', async () => {
33+
const answer = await askUser('Continue? (y = resume, n = stop): ');
34+
if (answer === 'n') {
35+
await agent.stop();
36+
} else {
37+
agent.resume();
38+
}
39+
});
40+
41+
await agent.act('create 3 todos');
42+
await agent.act('check off each todo');
43+
await agent.stop();
44+
}
45+
46+
main();

0 commit comments

Comments
 (0)