File tree Expand file tree Collapse file tree
packages/magnitude-core/examples Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " magnitude-core " : patch
3+ ---
4+
5+ pause resume api
6+
7+ author: @ddwang
Original file line number Diff line number Diff line change 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 ( ) ;
You can’t perform that action at this time.
0 commit comments