Skip to content

Commit 62534a9

Browse files
authored
fix(api): support auto-generating agent path from name (#646)
* fix: support auto-generating agent path from name * fix: return agent path on agent creation
1 parent d6c9fc6 commit 62534a9

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

gptme/server/api_v2_agents.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import logging
8+
import re
89
import shlex
910
import shutil
1011
import subprocess
@@ -29,6 +30,18 @@
2930

3031
logger = logging.getLogger(__name__)
3132

33+
# Store the initial working directory when the module is imported
34+
INITIAL_WORKING_DIRECTORY = Path.cwd()
35+
36+
37+
def slugify_name(name: str) -> str:
38+
"""Convert agent name to a filesystem-safe slug."""
39+
# Convert to lowercase and replace spaces/special chars with hyphens
40+
slug = re.sub(r"[^\w\s-]", "", name.lower())
41+
slug = re.sub(r"[-\s]+", "-", slug)
42+
return slug.strip("-")
43+
44+
3245
agents_api = flask.Blueprint("agents_api", __name__)
3346

3447

@@ -64,10 +77,15 @@ def api_agents_put():
6477

6578
path = req_json.get("path")
6679
if not path:
67-
return flask.jsonify({"error": "path is required"}), 400
80+
# Auto-generate path from initial directory + slugified agent name
81+
agent_slug = slugify_name(agent_name)
82+
path = INITIAL_WORKING_DIRECTORY / agent_slug
6883
else:
6984
path = Path(path).expanduser().resolve()
7085

86+
# Ensure path is a Path object and resolved
87+
path = Path(path).expanduser().resolve()
88+
7189
# Ensure the folder is empty
7290
if path.exists():
7391
return flask.jsonify({"error": f"Folder/path already exists: {path}"}), 400
@@ -189,5 +207,6 @@ def api_agents_put():
189207
"status": "ok",
190208
"message": "Agent created",
191209
"initial_conversation_id": conversation_id,
210+
"agent_path": str(path),
192211
}
193212
)

0 commit comments

Comments
 (0)