Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add `id` and `created_at` timestamp to logs; Add stream parsing comments

Tomas Valenta320afac0dc7e
diff --git a/api-service/codegen/callbacks/logs.py b/api-service/codegen/callbacks/logs.py
index 41d668d16..e432a8157 100644
--- a/api-service/codegen/callbacks/logs.py
+++ b/api-service/codegen/callbacks/logs.py
@@ -4,15 +4,27 @@ import uuid
import asyncio
from asyncio import Queue, ensure_future
from typing import Coroutine, Dict, Any, List, Union
-from pydantic import PrivateAttr, BaseModel
from langchain.callbacks.base import AsyncCallbackHandler
from langchain.schema import AgentAction, AgentFinish, LLMResult
+from pydantic import PrivateAttr
from codegen.agent.base import parse_action_string, separate_thought_and_action
from database import Database
class LogStreamParser:
+ """
+ To allow continuous streaming of partially finished logs we parse the logs every time we receive new token or tool output.
+ We reparse only the tokens that corresponds to the buffered logs and not all the tokens that we have ever received.
+
+ Buffered logs are partially finished logs that are either unfinished (still being streamed)
+ or they are tools' logs for which we haven't received outputs yet.
+ We can keep track of the buffered logs because there will always be more tool logs than tool outputs - we must parse the actions from tokens before they executed.
+ Because of that all buffered logs can be flushed to finished logs when we receive output for the last tool in the buffered logs.
+
+ We are matching the tools' logs and tools' outputs by the order in which they are parsed and ingested - if we need to support asynchronous execution of tools we need to change this matching.
+ """
+
def __init__(self) -> None:
# All "finished" logs that the parser saved.
self._logs: List[Dict[str, str]] = []
@@ -26,20 +38,23 @@ class LogStreamParser:
# This is a list of tools' outputs that corresponds to the tool logs in the self._logs.buffer.
self._tools_output_buffer: List[Dict[str, str]] = []
+ # List of ids corresponding to the buffered logs.
self._id_buffer: List[str] = []
+ # List of timestamps corresponding to the buffered logs.
+ self._start_timestamp_buffer: List[str] = []
def _parse(self):
"""This function should be called only after ingesting new token or tool output to update the buffered logs."""
thought, action_string = separate_thought_and_action(self._token_buffer)
- action_logs = [
+ tools_logs = [
{"type": "tool", "name": action.attrib["tool"], "input": action.text or ""}
for action in parse_action_string(action_string)
]
- # Update action logs with the information from ingested tools' outputs.
+ # Update tools' logs with the information from ingested tools' outputs.
for i in range(len(self._tools_output_buffer)):
- action_logs[i] = {
- **action_logs[i],
+ tools_logs[i] = {
+ **tools_logs[i],
**self._tools_output_buffer[i],
}
@@ -52,10 +67,34 @@ class LogStreamParser:
.replace("Action:", "")
.strip(),
},
- *action_logs,
+ *tools_logs,
]
- # self._id_buffer.extend([str(uuid.uuid4()) for a in range(1)])
+ # Add a new uuids to id buffer if we have less ids that there are logs in the logs buffer.
+ self._id_buffer.extend(
+ (
+ str(uuid.uuid4())
+ for _ in range(len(self._logs_buffer) - len(self._id_buffer))
+ )
+ )
+
+ # Assign the stable ids to logs
+ for log, id in zip(self._logs_buffer, self._id_buffer):
+ log["id"] = id
+
+ # Add a new timestamps to timestamps buffer if we have less timestamps that there are logs in the logs buffer.
+ self._start_timestamp_buffer.extend(
+ (
+ str(datetime.datetime.now())
+ for _ in range(
+ len(self._logs_buffer) - len(self._start_timestamp_buffer)
+ )
+ )
+ )
+
+ # Assign the corresponding timestamps to logs
+ for log, timestamp in zip(self._logs_buffer, self._start_timestamp_buffer):
+ log["created_at"] = timestamp
def ingest_token(self, token: str):
"""Ingest token and update the logs."""
diff --git a/components/Editor/RightSidebar/Logs/index.tsx b/components/Editor/RightSidebar/Logs/index.tsx
index 831cf253f..caf9a2d02 100644
--- a/components/Editor/RightSidebar/Logs/index.tsx
+++ b/components/Editor/RightSidebar/Logs/index.tsx
@@ -51,6 +51,8 @@ function Logs({
ref.current.scrollIntoView({ behavior: 'smooth' })
}, [logs])
+ console.log(logs)
+
return (
<div className="
max-w-full
diff --git a/db/types.ts b/db/types.ts
index 1e2308ebb..7f29c7353 100644
--- a/db/types.ts
+++ b/db/types.ts
@@ -12,6 +12,7 @@ export enum LogName {
interface BaseLog {
id: string
type: LogType
+ created_at: Date
}
export interface ThoughtLog extends BaseLog {