1
1
#!/usr/bin/env node
2
2
3
3
import { AIMessage , BaseMessage , HumanMessage } from "@langchain/core/messages" ;
4
+ import * as console from "node:console" ;
4
5
import * as fs from "node:fs" ;
5
6
import * as repl from "node:repl" ;
6
7
import * as yargs from "yargs" ;
@@ -19,7 +20,7 @@ import { enableVerboseLogging, parseWorkflow, runWorkflow } from "./index";
19
20
async function run ( path : string | number , prompt ?: string) : Promise < void > {
20
21
console. log ( `Hello, ${ process . env . USER } !` ) ;
21
22
console . log ( `You're running Autonomais in ${ process . cwd ( ) } .` ) ;
22
- console . log ( "You can exit the interactive session by typing '/quit' or '/ exit'." ) ;
23
+ console . log ( "You can exit the interactive session by typing '. exit'." ) ;
23
24
console . log ( "────────────────────────────────────────────────────────────────────────" ) ;
24
25
console . log ( ) ;
25
26
@@ -31,13 +32,19 @@ async function run(path: string | number, prompt?: string): Promise<void> {
31
32
const contents = fs . readFileSync ( path . toString ( ) , "utf-8" ) ;
32
33
const nodes = parseWorkflow ( contents ) ;
33
34
34
- const messages : BaseMessage [ ] = [ ] ;
35
- if ( prompt ) messages . push ( new HumanMessage ( prompt ) ) ;
36
-
37
35
console . log ( `Running workflow ${ path } .` ) ;
38
36
console . log ( prompt ? `Using prompt: ${ prompt } .` : "No prompt provided." ) ;
39
37
console . log ( ) ;
40
38
39
+ let messages : BaseMessage [ ] = [ ] ;
40
+
41
+ function initializeContext ( ) {
42
+ messages = [ ] ;
43
+ if ( prompt ) messages . push ( new HumanMessage ( prompt ) ) ;
44
+ }
45
+
46
+ initializeContext ( ) ;
47
+
41
48
const completion = await runWorkflow ( nodes , messages ) ;
42
49
messages . push ( new AIMessage ( completion ) ) ;
43
50
console . log ( colorize ( `AI: ${ completion } ` ) ) ;
@@ -46,13 +53,8 @@ async function run(path: string | number, prompt?: string): Promise<void> {
46
53
prompt : "→ " ,
47
54
useColors : true ,
48
55
eval : async ( cmd , _ , __ , callback ) => {
49
- const trimmedCmd = cmd . trim ( ) ;
50
- if ( trimmedCmd === '/quit' || trimmedCmd === '/exit' ) {
51
- replServer . close ( ) ;
52
- return ;
53
- }
54
56
try {
55
- messages . push ( new HumanMessage ( trimmedCmd ) ) ;
57
+ messages . push ( new HumanMessage ( cmd ) ) ;
56
58
const completion = await runWorkflow ( nodes , messages ) ;
57
59
messages . push ( new AIMessage ( completion ) ) ;
58
60
callback ( null , `AI: ${ completion } ` ) ;
@@ -62,6 +64,8 @@ async function run(path: string | number, prompt?: string): Promise<void> {
62
64
} ,
63
65
} ) ;
64
66
67
+ replServer . on ( "reset" , initializeContext ) ;
68
+
65
69
replServer . on ( "exit" , ( ) => {
66
70
console . log ( "Interactive session ended." ) ;
67
71
process . exit ( ) ;
0 commit comments