Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Web: Use agent hooks

Kilian Lieretc600c376938b
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index cbade019..65933165 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -224,6 +224,9 @@ class AgentHook:
def on_actions_generated(self, *, thought: str, action: str, output: str):
...
+
+ def on_sub_action_started(self, *, sub_action: str):
+ ...
def on_sub_action_started(self, *, sub_action: str):
...
diff --git a/sweagent/web/server.py b/sweagent/web/server.py
index 96855a83..5abb99dc 100644
--- a/sweagent/web/server.py
+++ b/sweagent/web/server.py
@@ -4,7 +4,7 @@ import threading
import sys
from sweagent import CONFIG_DIR, PACKAGE_DIR
-from sweagent.agent.agents import AgentArguments
+from sweagent.agent.agents import AgentArguments, AgentHook
from sweagent.agent.models import ModelArguments
from sweagent.environment.swe_env import EnvironmentArguments
from flask_socketio import SocketIO
@@ -26,7 +26,7 @@ def index():
def handle_connect():
print('Client connected')
-class WebUpdateRunHook(MainHook):
+class MainUpdateHook(MainHook):
def on_start(self):
socketio.emit('update', {'event': 'started'})
@@ -37,6 +37,17 @@ class WebUpdateRunHook(MainHook):
socketio.emit('update', {'event': 'instance_completed', **info})
+class AgentUpdateHook(AgentHook):
+ def on_actions_generated(self, *, thought: str, action: str, output: str):
+ socketio.emit('update', {'event': 'actions_generated', 'thought': thought, 'action': action})
+
+ def on_sub_action_started(self, *, sub_action: str):
+ socketio.emit('update', {'event': 'sub_action_started', 'sub_action': sub_action})
+
+ def on_sub_action_executed(self, *, obs: str, done: bool):
+ socketio.emit('update', {'event': 'sub_action_executed', 'obs': obs, 'done': done})
+
+
@app.route('/run', methods=['GET'])
def run():
data_path = request.args.get("data_path", "")
@@ -68,11 +79,12 @@ def run():
actions=ActionsArguments(open_pr=False, skip_if_commits_reference_issue=True),
)
main = Main(defaults)
- main.add_hook(WebUpdateRunHook())
+ main.add_hook(MainUpdateHook())
+ main.agent.add_hook(AgentUpdateHook())
thread = threading.Thread(target=main.main)
thread.start()
return 'Commands are being executed', 202
if __name__ == "__main__":
- socketio.run(app, port=5001)
\ No newline at end of file
+ socketio.run(app, port=5001, debug=True)
\ No newline at end of file
diff --git a/sweagent/web/static/scripts/mainscript.js b/sweagent/web/static/scripts/mainscript.js
new file mode 100644
index 00000000..227abdae
--- /dev/null
+++ b/sweagent/web/static/scripts/mainscript.js
@@ -0,0 +1,27 @@
+function addMessageToFeed(feedID, title, text) {
+ console.log("in addMessageToFeed");
+ const feed = document.getElementById(feedID);
+ const messageBlock = document.createElement('div');
+ messageBlock.className = 'message';
+
+ const messageTitle = document.createElement('h4');
+ messageTitle.textContent = title;
+
+ const messageText = document.createElement('p');
+ messageText.textContent = text;
+
+ messageBlock.appendChild(messageTitle);
+ messageBlock.appendChild(messageText);
+
+ feed.appendChild(messageBlock);
+}
+
+
+document.addEventListener('DOMContentLoaded', function() {
+ const socket = io();
+ socket.on('update', function(data) {
+ console.log("update");
+ addMessageToFeed('agentFeed', data["event"], JSON.stringify(data));
+ addMessageToFeed('envFeed', data["event"], JSON.stringify(data));
+ });
+});
\ No newline at end of file
diff --git a/sweagent/web/static/styles/mainpage.css b/sweagent/web/static/styles/mainpage.css
index b6e49232..be2ac3b7 100644
--- a/sweagent/web/static/styles/mainpage.css
+++ b/sweagent/web/static/styles/mainpage.css
@@ -18,5 +18,7 @@
max-width: 50%; /* Optionally limit the feed width */
overflow-y: auto; /* Adds scroll bar if feed gets too long */
height: 500px; /* Fixed height for feed */
- border: 1px solid black;
+ border: 1px solid #ccc;
+ padding: 10px;
+ border-radius: 5px;
}
\ No newline at end of file
diff --git a/sweagent/web/templates/index.html b/sweagent/web/templates/index.html
index 0a3066d6..f0960e1e 100644
--- a/sweagent/web/templates/index.html
+++ b/sweagent/web/templates/index.html
@@ -2,37 +2,9 @@
<html>
<head>
<title>Run Command</title>
- <link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">
+ <link rel= "stylesheet" type= "text/css" href="{{ url_for('static',filename='styles/mainpage.css') }}">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
- <script>
- function addMessageToFeed(feedID, title, text) {
- console.log("in addMessageToFeed");
- const feed = document.getElementById(feedID);
- const messageBlock = document.createElement('div');
- messageBlock.className = 'message';
-
- const messageTitle = document.createElement('h2');
- messageTitle.textContent = title;
-
- const messageText = document.createElement('p');
- messageText.textContent = text;
-
- messageBlock.appendChild(messageTitle);
- messageBlock.appendChild(messageText);
-
- feed.appendChild(messageBlock);
- }
-
-
- document.addEventListener('DOMContentLoaded', function() {
- const socket = io();
- socket.on('update', function(data) {
- console.log("update");
- addMessageToFeed('agentFeed', data["event"], JSON.stringify(data));
- addMessageToFeed('envFeed', data["event"], JSON.stringify(data));
- });
- });
- </script>
+ <script src="{{ url_for('static',filename='scripts/mainscript.js') }}"> </script>
</head>
<body>
<h1>Start run</h1>
@@ -59,10 +31,14 @@
.catch(error => console.error('Error:', error));
});
</script>
- <h1>Trajectory</h1>
+ <h2>Trajectory</h2>
<div id="container">
- <div id="agentFeed"></div>
- <div id="envFeed"></div>
+ <div id="agentFeed">
+ <h3>Agent Feed</h3>
+ </div>
+ <div id="envFeed">
+ <h3>Environment Feed</h3>
+ </div>
</div>
</body>
</html>
\ No newline at end of file