Skip to content

Commit a7fdafd

Browse files
committed
refactor(api): Improve OpenAPI endpoint name generation
- Enhance path-to-function name conversion logic - Add robust handling for path names with special characters - Implement regex to clean and standardize endpoint names - Prevent invalid function names by adding prefix for numeric paths - Ensure fallback to generic name if path cleaning results in empty string - Update version to 0.9.25 in uv.lock
1 parent cd89552 commit a7fdafd

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

agentle/agents/apis/api.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import annotations
1515

1616
import logging
17+
import re
1718
from collections.abc import (
1819
Coroutine,
1920
Mapping,
@@ -513,13 +514,30 @@ def _parse_openapi_paths(
513514
continue
514515

515516
# Create endpoint
516-
endpoint_name: str = cast(
517-
str,
518-
(
519-
operation_id
520-
or f"{method}_{path.replace('/', '_').replace('{', '').replace('}', '')}"
521-
),
522-
)
517+
# Generate a valid function name from the path
518+
if operation_id:
519+
endpoint_name = operation_id
520+
else:
521+
# Clean the path to create a valid function name
522+
# Remove leading/trailing slashes and replace special chars
523+
clean_path = (
524+
path.strip("/")
525+
.replace("/", "_")
526+
.replace("{", "")
527+
.replace("}", "")
528+
.replace("-", "_")
529+
)
530+
# Remove any consecutive underscores
531+
clean_path = re.sub(r"_+", "_", clean_path)
532+
# Ensure it doesn't start with a number
533+
if clean_path and clean_path[0].isdigit():
534+
clean_path = f"n{clean_path}"
535+
# If empty after cleaning, use a generic name
536+
if not clean_path:
537+
clean_path = "root"
538+
endpoint_name = f"{method.lower()}_{clean_path}"
539+
540+
endpoint_name = cast(str, endpoint_name)
523541

524542
endpoint_description: str = cast(
525543
str,

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)