Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Handle single-line actions
Vasek Mlejnsky81ce3974f348
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 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 filediff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.pyindex 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, Unionfrom html import unescapeimport xml.etree.ElementTree as ET+import refrom langchain.agents.tools import InvalidToolfrom 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 actionsdiff --git a/components/Editor/RightSidebar/Logs.tsx b/components/Editor/RightSidebar/Logs.tsxindex 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.tsxindex c0d4cca1f..c87d7a6df 100644--- a/components/Editor/index.tsx+++ b/components/Editor/index.tsx@@ -209,9 +209,9 @@ function Editor({ project }: Props) {<Linkhref={deployment.url}className="- mt-6- underline- "+ mt-6+ underline+ "target="_blank" rel="noopener noreferrer"><Textdiff --git a/db/types.ts b/db/types.tsindex 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+