Skip to content

Commit 4744ba4

Browse files
committed
Refactor session handling in autonomais.ts
The code has been updated to improve session handling on the Autonomais program. We added an import for console and corrected the exit command message. The exit condition logic has been removed from the replServer eval, and a 'reset' event listener has been added to reinitialize the session.
1 parent 6bf0be8 commit 4744ba4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/autonomais.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";
4+
import * as console from "node:console";
45
import * as fs from "node:fs";
56
import * as repl from "node:repl";
67
import * as yargs from "yargs";
@@ -19,7 +20,7 @@ import { enableVerboseLogging, parseWorkflow, runWorkflow } from "./index";
1920
async function run(path: string | number, prompt?: string): Promise<void> {
2021
console.log(`Hello, ${process.env.USER}!`);
2122
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'.");
2324
console.log("────────────────────────────────────────────────────────────────────────");
2425
console.log();
2526

@@ -31,13 +32,19 @@ async function run(path: string | number, prompt?: string): Promise<void> {
3132
const contents = fs.readFileSync(path.toString(), "utf-8");
3233
const nodes = parseWorkflow(contents);
3334

34-
const messages: BaseMessage[] = [];
35-
if (prompt) messages.push(new HumanMessage(prompt));
36-
3735
console.log(`Running workflow ${path}.`);
3836
console.log(prompt ? `Using prompt: ${prompt}.` : "No prompt provided.");
3937
console.log();
4038

39+
let messages: BaseMessage[] = [];
40+
41+
function initializeContext() {
42+
messages = [];
43+
if (prompt) messages.push(new HumanMessage(prompt));
44+
}
45+
46+
initializeContext();
47+
4148
const completion = await runWorkflow(nodes, messages);
4249
messages.push(new AIMessage(completion));
4350
console.log(colorize(`AI: ${completion}`));
@@ -46,13 +53,8 @@ async function run(path: string | number, prompt?: string): Promise<void> {
4653
prompt: "→ ",
4754
useColors: true,
4855
eval: async (cmd, _, __, callback) => {
49-
const trimmedCmd = cmd.trim();
50-
if (trimmedCmd === '/quit' || trimmedCmd === '/exit') {
51-
replServer.close();
52-
return;
53-
}
5456
try {
55-
messages.push(new HumanMessage(trimmedCmd));
57+
messages.push(new HumanMessage(cmd));
5658
const completion = await runWorkflow(nodes, messages);
5759
messages.push(new AIMessage(completion));
5860
callback(null, `AI: ${completion}`);
@@ -62,6 +64,8 @@ async function run(path: string | number, prompt?: string): Promise<void> {
6264
},
6365
});
6466

67+
replServer.on("reset", initializeContext);
68+
6569
replServer.on("exit", () => {
6670
console.log("Interactive session ended.");
6771
process.exit();

0 commit comments

Comments
 (0)