Skip to content

Commit ef6665e

Browse files
committed
fix: websurfer not accessing network
1 parent a8bfe3e commit ef6665e

File tree

9 files changed

+780
-203
lines changed

9 files changed

+780
-203
lines changed

api/Dockerfile

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
FROM python:3.12-slim
1+
FROM python:3.11-slim
22

33
# Set environment variables
44
ENV PYTHONUNBUFFERED=1
55
ENV OPENBLAS_NUM_THREADS=4
66
ENV MKL_NUM_THREADS=4
77
ENV OMP_NUM_THREADS=4
88
ENV NUMEXPR_NUM_THREADS=4
9+
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
910

1011
# Install system dependencies
1112
RUN apt-get update && apt-get install -y \
1213
build-essential \
1314
curl \
15+
git \
16+
# Playwright system dependencies
17+
libnss3 \
18+
libnspr4 \
19+
libatk1.0-0 \
20+
libatk-bridge2.0-0 \
21+
libcups2 \
22+
libdrm2 \
23+
libdbus-1-3 \
24+
libxkbcommon0 \
25+
libatspi2.0-0 \
26+
libxcomposite1 \
27+
libxdamage1 \
28+
libxfixes3 \
29+
libxrandr2 \
30+
libgbm1 \
31+
libpango-1.0-0 \
32+
libcairo2 \
33+
libasound2 \
1434
&& rm -rf /var/lib/apt/lists/*
1535

1636
# Set working directory
@@ -28,6 +48,9 @@ RUN poetry config virtualenvs.create true \
2848
&& poetry config virtualenvs.in-project true \
2949
&& poetry install --no-interaction --no-ansi
3050

51+
# Install Playwright browsers
52+
RUN poetry run playwright install --with-deps chromium firefox webkit
53+
3154
# Copy application code
3255
COPY . .
3356

api/agentok_api/services/codegen.py

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,26 @@ def generate_project(self, project: Project) -> str:
5858
# Extend config_list with settings['models'] if available
5959
user_settings = self.supabase.fetch_general_settings()
6060
if user_settings and "general" in user_settings and 'models' in user_settings["general"]:
61-
config_list.extend(user_settings["general"]["models"])
61+
config_list.extend([model for model in user_settings["general"]["models"] if model["enabled"] == True])
62+
63+
print('config_list 1', config_list)
6264

6365
# Extend config_list with OAI_CONFIG_LIST
6466
config_list.extend(autogen.config_list_from_json(
6567
env_or_file="OAI_CONFIG_LIST",
6668
))
6769

70+
print('config_list 2', config_list)
71+
6872
# Apply filters if any
69-
if user_settings and 'filters' in user_settings:
70-
config_list = autogen.filter_config(
71-
config_list,
72-
filter_dict={
73-
"model": user_settings['filters'].get('name', '').split(','),
74-
"tags": user_settings['filters'].get('tags', '').split(','),
75-
}
76-
)
73+
# if user_settings and 'filters' in user_settings:
74+
# config_list = autogen.filter_config(
75+
# config_list,
76+
# filter_dict={
77+
# "model": user_settings['filters'].get('name', '').split(','),
78+
# "tags": user_settings['filters'].get('tags', '').split(','),
79+
# }
80+
# )
7781

7882
# Check cache first
7983
cache_key = self._get_cache_key(project)
@@ -182,7 +186,7 @@ def generate_project(self, project: Project) -> str:
182186

183187
# Generate tool assignments
184188
tool_dict = self.build_tool_dict(project)
185-
tool_assignments = self.generate_tool_assignments(flow.edges, tool_dict)
189+
tool_assignments = self.generate_tool_assignments(flow.edges, tool_dict, flow.nodes)
186190
print(tool_assignments)
187191
tool_dict = {
188192
tool_id: tool
@@ -290,10 +294,11 @@ def build_tool_dict(self, project: Project):
290294
tools = self.supabase.fetch_tools(tool_ids)
291295
return {tool["id"]: tool for tool in tools}
292296

293-
def generate_tool_assignments(self, edges, tool_dict):
297+
def generate_tool_assignments(self, edges, tool_dict, nodes):
294298
# Prepare the tool assignments
295299
tool_assignments = {}
296300

301+
# First, process the regular tool assignments from edges
297302
for edge in edges:
298303
if edge.get("data") is not None and edge["data"].get("tools") is not None:
299304
# Iterate over edge.data.tools.llm and append node.id to the respective tool_id in llm
@@ -310,7 +315,40 @@ def generate_tool_assignments(self, edges, tool_dict):
310315
)
311316
)
312317

313-
# Now tool_assignments contains the desired structure
318+
# Now process WebSurferAgent tools
319+
for edge in edges:
320+
target_node = next(
321+
(node for node in nodes if node["id"] == edge["target"]),
322+
None
323+
)
324+
if target_node and target_node.get("data", {}).get("class_type") == "WebSurferAgent":
325+
# Get the source node (usually a UserProxyAgent) for execution
326+
source_node = next(
327+
(node for node in nodes if node["id"] == edge["source"]),
328+
None
329+
)
330+
if source_node:
331+
# Get the web tool type from the node's data
332+
web_tool = target_node.get("data", {}).get("web_tool", "browser_use")
333+
334+
# Add the tool registration to the generated code
335+
# We don't need to create a mock function or add to tool_dict
336+
# Instead, we'll handle this in the template by registering websurfer.tools
337+
tool_id = f"websurfer_{target_node['id']}"
338+
if tool_id not in tool_assignments:
339+
tool_assignments[tool_id] = {
340+
"execution": [],
341+
"llm": [],
342+
"is_websurfer": True, # Flag to indicate this is a WebSurferAgent tool
343+
"web_tool": web_tool, # Store the web tool type
344+
"websurfer_node_id": target_node["id"] # Store the WebSurferAgent node ID
345+
}
346+
347+
if source_node["id"] not in tool_assignments[tool_id]["execution"]:
348+
tool_assignments[tool_id]["execution"].append(source_node["id"])
349+
if target_node["id"] not in tool_assignments[tool_id]["llm"]:
350+
tool_assignments[tool_id]["llm"].append(target_node["id"])
351+
314352
return tool_assignments
315353

316354
def generate_rag_assignments(self, edges):
@@ -324,35 +362,32 @@ def generate_rag_assignments(self, edges):
324362

325363
return rag_assignments
326364

327-
def generate_tool_envs(self, project: Project, tool_dict: dict):
365+
def generate_tool_envs(self, project: Project, tool_dict: dict) -> dict:
328366
"""Generate tool environment files for the provided project.
329367
330368
Args:
331369
project (Project): The project metadata.
370+
tool_dict (dict): Dictionary of tools with their metadata.
332371
333372
Returns:
334373
dict: A dictionary containing the tool IDs as keys and the environment file contents as values.
335374
"""
336-
for tool in tool_dict.values():
337-
meta = self.extract_tool_meta(tool["code"])
338-
tool_dict[tool["id"]]["func_name"] = meta["func_name"]
339-
tool_assignments = self.generate_tool_assignments(project.flow.edges, tool_dict)
340-
375+
# Fetch tool settings from Supabase
341376
tool_settings = self.supabase.fetch_tool_settings()
342377

343-
# Clean unused tools from tool_settings that not included in tool_assignments
378+
# Filter tool_settings to only include tools in tool_dict
344379
tool_settings = {
345380
int(tool_id): settings
346381
for tool_id, settings in tool_settings.items()
347-
if int(tool_id) in tool_assignments
382+
if int(tool_id) in tool_dict
348383
}
349384

385+
# Render the template for each tool
350386
template = self.env.get_template("tool_env.j2")
351-
tool_envs = {}
352-
for tool_id, tool_settings in tool_settings.items():
353-
tool_envs[tool_id] = template.render(tool_settings=tool_settings)
354-
355-
print("tool_envs", tool_envs)
387+
tool_envs = {
388+
tool_id: template.render(tool_settings=tool_settings[tool_id])
389+
for tool_id in tool_settings
390+
}
356391

357392
return tool_envs
358393

api/agentok_api/templates/assistant.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ node_{{ node.id }} = {{ node.data.class_type }}(
5252
llm_config=llm_config,
5353
{%- endif %}
5454
)
55-
5655
{%- endfor %}
56+
5757
{%- endmacro %}

api/agentok_api/templates/tool_binding.j2

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,35 @@
33
{# Iterate over each tool_id in tool_assignments #}
44
{%- for tool_id, assignments in tool_assignments.items() %}
55

6+
{%- if assignments.is_websurfer %}
7+
# Register WebSurferAgent tools
8+
{%- for node_id in assignments.execution %}
9+
# Register all tools from WebSurferAgent to UserProxyAgent for execution
10+
for tool in node_{{ assignments.websurfer_node_id }}.tools:
11+
tool.register_for_execution(node_{{ node_id }})
12+
{%- endfor %}
13+
{%- else %}
614
{# Register LLM tools #}
7-
{%- if assignments.llm -%}
15+
{%- if assignments.llm -%}
816
# Register LLM tools to agents
9-
{%- set tool = tool_dict[tool_id] %}
10-
{%- for node_id in assignments.llm %}
17+
{%- set tool = tool_dict[tool_id] %}
18+
{%- for node_id in assignments.llm %}
1119
node_{{ node_id }}.register_for_llm(
1220
name="{{ tool['func_name'] }}",
1321
description="{{ tool['description'] }}" # This field is only required when registering to LLM
1422
)({{ tool['func_name'] }})
15-
{%- endfor %}
16-
{%- endif %}
23+
{%- endfor %}
24+
{%- endif %}
1725

1826
{# Register Execution tools #}
19-
{%- if assignments.execution -%}
27+
{%- if assignments.execution -%}
2028
# Register Execution tools to agents
21-
{%- set tool = tool_dict[tool_id] %}
22-
{%- for node_id in assignments.execution %}
29+
{%- set tool = tool_dict[tool_id] %}
30+
{%- for node_id in assignments.execution %}
2331
node_{{ node_id }}.register_for_execution(name="{{ tool['func_name'] }}")({{ tool['func_name'] }})
24-
{%- endfor %}
25-
{%- endif -%}
32+
{%- endfor %}
33+
{%- endif -%}
34+
{%- endif %}
2635

2736
{%- endfor -%}
2837
{%- endif -%}

0 commit comments

Comments
 (0)