Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Handle single-line actions

Vasek Mlejnsky81ce3974f348
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 0c70f1560..079386e3a 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -64,4 +64,5 @@
"./**",
"./api-service/.venv/bin/python"
],
+ "python.linting.enabled": true,
}
\ No newline at end of file
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.py
index d338968f5..6a61e7648 100644
--- a/api-service/codegen/agent/base.py
+++ b/api-service/codegen/agent/base.py
@@ -1,6 +1,7 @@
from typing import Optional, Tuple, List, Dict, Union
from html import unescape
import xml.etree.ElementTree as ET
+import re
from langchain.agents.tools import InvalidTool
from langchain.agents import AgentExecutor
@@ -27,6 +28,19 @@ def xml_escape(text: str) -> str:
return text
+def actions_from_xml_string(xml_string: str) -> str:
+ """The `xml_string` must be escaped"""
+ # Parse the escaped string as an XML tree.
+ root = ET.fromstring(f"<root>{xml_string}</root>")
+ actions = root.findall("action")
+
+ # Because we XML-escaped action element's body (= tool input) so we could XML-parse it,
+ # we need to unescape it now to get the actual tool input.
+ for a in actions:
+ a.text = unescape(a.text)
+ return actions
+
+
def parse_action_string(action_string: str):
lines = action_string.strip().splitlines()
@@ -34,12 +48,41 @@ def parse_action_string(action_string: str):
# TODO: What to do here?
pass
+ # === SINGLE-LINE CASE
+ #
if len(lines) == 1:
- # Possible format: <action tool="">content</action><action tool="">content</action>
+ # We assume that the XML is in valid format, just on a single line.
+ # Possible format: <action tool="name">content</action><action tool="name">content</action>
line = lines[0]
- # TODO
- pass
+ action_count = line.count("</action>")
+ if action_count == 0:
+ # TODO: What to do here?
+ pass
+ else:
+ parts = line.split("</action>")
+ # We have an array that looks like this:
+ # ['<action tool="name">content', '<action tool="name">content', ..., '']
+ # We will use regex to match content after the `<action tool="name">` part.
+ for idx, part in enumerate(parts):
+ if part:
+ action_name, action_content = re.search(
+ r"<action tool=\"(.*)\">(.*)$", part
+ ).groups()
+ escaped_content = xml_escape(action_content)
+ # Assemble the action with escaped content back togeter.
+ parts[
+ idx
+ ] = f'<action tool="{action_name}">{escaped_content}</action>'
+
+ escaped_xml_string = "\n".join(parts)
+
+ # Parse the escaped string as an XML tree.
+ actions = actions_from_xml_string(escaped_xml_string)
+ return actions
+
+ # === MULTI-LINE CASE
+ #
# The action is in the XML format, we need to try to a XML escape
# the body of an XML element. We'll use a bit of heuristics here.
# We assume the action looks like this:
@@ -69,14 +112,8 @@ def parse_action_string(action_string: str):
# Join the lines back into a single string.
escaped = "\n".join(lines)
- # Parse the escaped string as a XML tree.
- root = ET.fromstring(f"<root>{escaped}</root>")
- actions = root.findall("action")
-
- # Because we XML-escaped action element's body (= tool input) so we could XML parse it,
- # we need to unescape it now to get the actual tool input.
- for a in actions:
- a.text = unescape(a.text)
+ # Parse the escaped string as an XML tree.
+ actions = actions_from_xml_string(escaped)
return actions
diff --git a/components/Editor/RightSidebar/Logs.tsx b/components/Editor/RightSidebar/Logs.tsx
index 3695a2e94..dc0a636bc 100644
--- a/components/Editor/RightSidebar/Logs.tsx
+++ b/components/Editor/RightSidebar/Logs.tsx
@@ -89,9 +89,11 @@ function Logs({
{selectedTab === 0 && (
<>
{logs?.map((l, i) =>
- <ReactMarkdown key={i}>
- {l}
- </ReactMarkdown>
+ <>
+ {l.type === 'thought' && <div key={l.id}>{l.content}</div>}
+ {l.type === 'tool' && !l.output && <div key={l.id}>{l.name}</div>}
+ {l.type === 'tool' && l.output && <div key={l.id}>{l.name}{': '}{l.output}</div>}
+ </>
)}
</>
)}
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index c0d4cca1f..c87d7a6df 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -209,9 +209,9 @@ function Editor({ project }: Props) {
<Link
href={deployment.url}
className="
- mt-6
- underline
- "
+ mt-6
+ underline
+ "
target="_blank" rel="noopener noreferrer"
>
<Text
diff --git a/db/types.ts b/db/types.ts
index a85390a29..19b42970d 100644
--- a/db/types.ts
+++ b/db/types.ts
@@ -1 +1,19 @@
-export type Log = string
+export interface ThoughtLog {
+ id: string
+ type: 'thought'
+ content: string
+ created_at: Date
+}
+
+export interface ToolLog {
+ id: string
+ type: 'tool'
+ name: string
+ input: string
+ start_at: Date
+ finish_at?: Date
+ output?: string
+}
+
+export type Log = ThoughtLog | ToolLog
+