1
- import { $ , fs , path , chalk } from 'zx' ;
2
- import { DEFAULT_AGENT_ID , DEFAULT_CHARACTER } from './test1.mjs' ;
3
- import { spawn } from 'node:child_process' ;
4
- $ . verbose = false ; // Suppress command output unless there's an error
1
+ import { spawn } from "node:child_process" ;
2
+ import { stringToUuid } from "../packages/core/dist/index.js" ;
3
+ import path from "path" ;
4
+
5
+ export const DEFAULT_CHARACTER = "trump"
6
+ export const DEFAULT_AGENT_ID = stringToUuid ( DEFAULT_CHARACTER ?? uuidv4 ( ) ) ;
5
7
6
8
function projectRoot ( ) {
7
9
return path . join ( import . meta. dirname , ".." ) ;
8
10
}
9
11
12
+ function log ( message ) {
13
+ console . log ( message ) ;
14
+ }
15
+
16
+ function logError ( error ) {
17
+ log ( "ERROR: " + error . message ) ;
18
+ log ( error ) ; // Print stack trace
19
+ }
20
+
10
21
async function runProcess ( command , args = [ ] , directory = projectRoot ( ) ) {
11
22
try {
12
- const result = await $ `cd ${ directory } && ${ command } ${ args } ` ;
23
+ throw new Exception ( "Not implemented yet" ) ; // TODO
24
+ // const result = await $`cd ${directory} && ${command} ${args}`;
13
25
return result . stdout . trim ( ) ;
14
26
} catch ( error ) {
15
27
throw new Error ( `Command failed: ${ error . message } ` ) ;
16
28
}
17
29
}
18
30
19
31
async function installProjectDependencies ( ) {
20
- console . log ( chalk . blue ( 'Installing dependencies...' ) ) ;
32
+ log ( 'Installing dependencies...' ) ;
21
33
return await runProcess ( 'pnpm' , [ 'install' , '-r' ] ) ;
22
34
}
23
35
24
36
async function buildProject ( ) {
25
- console . log ( chalk . blue ( 'Building project...' ) ) ;
37
+ log ( 'Building project...' ) ;
26
38
return await runProcess ( 'pnpm' , [ 'build' ] ) ;
27
39
}
28
40
@@ -34,50 +46,81 @@ async function writeEnvFile(entries) {
34
46
}
35
47
36
48
async function startAgent ( character = DEFAULT_CHARACTER ) {
37
- console . log ( chalk . blue ( `Starting agent for character: ${ character } ` ) ) ;
38
- const proc = spawn ( 'pnpm' , [ 'start' , `--character=characters/${ character } .character.json` , '--non-interactive' ] , { shell : true , "stdio" : "inherit" } ) ;
39
- log ( `proc=${ JSON . stringify ( proc ) } ` ) ;
40
-
41
- // Wait for server to be ready
42
- await new Promise ( resolve => setTimeout ( resolve , 20000 ) ) ;
49
+ log ( `Starting agent for character: ${ character } ` ) ;
50
+ const proc = spawn ( "node" , [ "--loader" , "ts-node/esm" , "src/index.ts" , "--isRoot" , `--character=characters/${ character } .character.json` , "--non-interactive" ] , {
51
+ cwd : path . join ( projectRoot ( ) , "agent" ) ,
52
+ shell : false ,
53
+ stdio : "inherit"
54
+ } ) ;
55
+ const startTime = Date . now ( ) ;
56
+ while ( true ) {
57
+ try {
58
+ const response = await fetch ( "http://127.0.0.1:3000/" , { method : "GET" } ) ;
59
+ if ( response . ok ) break ;
60
+ } catch ( error ) { }
61
+ if ( Date . now ( ) - startTime > 120000 ) {
62
+ throw new Error ( "Timeout waiting for process to start" ) ;
63
+ } else {
64
+ await sleep ( 1000 ) ;
65
+ }
66
+ }
67
+ await sleep ( 1000 ) ;
43
68
return proc ;
44
69
}
45
70
46
71
async function stopAgent ( proc ) {
47
- console . log ( chalk . blue ( 'Stopping agent...' ) ) ;
48
- proc . kill ( 'SIGTERM' )
72
+ log ( "Stopping agent..." + JSON . stringify ( proc . pid ) ) ;
73
+ proc . kill ( ) ;
74
+ const startTime = Date . now ( ) ;
75
+ while ( true ) {
76
+ if ( proc . killed ) break ;
77
+ if ( Date . now ( ) - startTime > 60000 ) {
78
+ throw new Error ( "Timeout waiting for the process to terminate" ) ;
79
+ }
80
+ await sleep ( 1000 ) ;
81
+ }
82
+ await sleep ( 1000 ) ;
49
83
}
50
84
51
- async function send ( message ) {
52
- const endpoint = `http://127.0.0.1:3000/${ DEFAULT_AGENT_ID } /message` ;
53
- const payload = {
54
- text : message ,
55
- userId : "user" ,
56
- userName : "User"
57
- } ;
85
+ async function sleep ( ms ) {
86
+ await new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
87
+ }
58
88
89
+ async function sendPostRequest ( url , method , payload ) {
59
90
try {
60
- const response = await fetch ( endpoint , {
61
- method : 'POST' ,
62
- headers : {
63
- 'Content-Type' : 'application/json'
64
- } ,
91
+ const response = await fetch ( url , {
92
+ method : method ,
93
+ headers : { "Content-Type" : "application/json" } ,
65
94
body : JSON . stringify ( payload )
66
95
} ) ;
67
-
68
- if ( ! response . ok ) {
69
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
70
- }
71
-
96
+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
72
97
const data = await response . json ( ) ;
73
98
return data [ 0 ] . text ;
74
99
} catch ( error ) {
75
100
throw new Error ( `Failed to send message: ${ error . message } ` ) ;
76
101
}
77
102
}
78
103
79
- function log ( message ) {
80
- console . log ( message ) ;
104
+ async function send ( message ) {
105
+ const url = `http://127.0.0.1:3000/${ DEFAULT_AGENT_ID } /message` ;
106
+ return await sendPostRequest ( url , "POST" , {
107
+ text : message ,
108
+ userId : "user" ,
109
+ userName : "User"
110
+ } ) ;
111
+ }
112
+
113
+ async function runIntegrationTest ( fn ) {
114
+ const proc = await startAgent ( ) ;
115
+ try {
116
+ await fn ( ) ;
117
+ log ( "✓ Test passed" ) ;
118
+ } catch ( error ) {
119
+ log ( "✗ Test failed" ) ;
120
+ logError ( error ) ;
121
+ } finally {
122
+ await stopAgent ( proc ) ;
123
+ }
81
124
}
82
125
83
126
export {
@@ -89,5 +132,7 @@ export {
89
132
startAgent ,
90
133
stopAgent ,
91
134
send ,
92
- log
93
- }
135
+ runIntegrationTest ,
136
+ log ,
137
+ logError
138
+ }
0 commit comments