Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Added chat history #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ rl.on("SIGINT", () => {
process.exit(0);
});

const chatHistory: string[] = [];

async function handleUserInput(input, agentId) {
if (input.toLowerCase() === "exit") {
rl.close();
Expand All @@ -34,7 +36,11 @@ async function handleUserInput(input, agentId) {
);

const data = await response.json();
data.forEach((message) => console.log(`${"Agent"}: ${message.text}`));
data.forEach((message) => {
const formattedMessage = `${"Agent"}: ${message.text}`;
console.log(formattedMessage);
chatHistory.push(formattedMessage);
});
Comment on lines +39 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve message structure and consistency

Messages lack metadata and consistent formatting.

Implement a structured message format:

-      const formattedMessage = `${"Agent"}: ${message.text}`;
-      console.log(formattedMessage);
-      chatHistory.push(formattedMessage);
+      const historyEntry = {
+        timestamp: new Date().toISOString(),
+        sender: "Agent",
+        text: message.text
+      };
+      console.log(`${historyEntry.sender}: ${historyEntry.text}`);
+      chatHistory.push(historyEntry);

Committable suggestion skipped: line range outside the PR's diff.

} catch (error) {
console.error("Error fetching response:", error);
}
Expand All @@ -44,6 +50,13 @@ export function startChat(characters) {
function chat() {
const agentId = characters[0].name ?? "Agent";
rl.question("You: ", async (input) => {
if (input.toLocaleLowerCase() === "history") {
showChatHistory();
chat();
return;
}

chatHistory.push(`You: ${input}`);
Comment on lines +53 to +59
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve command handling robustness

The current implementation has reliability concerns.

Apply these improvements:

  1. Fix case sensitivity
  2. Add command prefix
  3. Handle async flow properly
-      if (input.toLocaleLowerCase() === "history") {
+      const normalizedInput = input.trim().toLowerCase();
+      if (normalizedInput === "/history") {
+        await showChatHistory();
         chat();
         return;
       }
       
-      chatHistory.push(`You: ${input}`);
+      addToChatHistory({
+        timestamp: new Date().toISOString(),
+        sender: "You",
+        text: input
+      });

Committable suggestion skipped: line range outside the PR's diff.

await handleUserInput(input, agentId);
if (input.toLowerCase() !== "exit") {
chat(); // Loop back to ask another question
Expand All @@ -53,3 +66,10 @@ export function startChat(characters) {

return chat;
}

export function showChatHistory() {
console.log('Chat History:');
chatHistory.forEach((message, index) => {
console.log(`${index + 1}: ${message}`);
});
}