Skip to content

Commit da4f300

Browse files
committed
fix: fix AgentFileInfo model and agent variable detection logic
- Rename AgentFileInfo fields to be more descriptive (path→file_path, etc.) - Add new fields: imports, has_runner, has_entrypoint, detected_tools - Update AgentFileInfo docstring to clarify its purpose in project initialization - Simplify agent variable detection to only match direct Agent(...) instantiation - Relax agent variable validation to accept any assignment when user specifies --agent-var - Remove SSE "data: " prefix from wrapper (cherry picked from commit 0acbbbf4a722f6a0e12c7ec07eac655e8b4dcdda)
1 parent 376c725 commit da4f300

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

agentkit/toolkit/models.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -448,24 +448,37 @@ def __str__(self) -> str:
448448

449449
@dataclass
450450
class AgentFileInfo:
451-
"""Information about a file generated by Agent.
451+
"""Parsed information about a user's Agent definition file.
452452
453-
Used to record details about generated files.
453+
Used during project initialization to understand the structure
454+
of the user's Agent file and generate appropriate wrappers.
454455
"""
455-
path: str
456-
"""File path"""
456+
file_path: str
457+
"""Absolute path to the Agent definition file"""
457458

458-
content: Optional[str] = None
459-
"""File content"""
459+
agent_var_name: str
460+
"""Name of the Agent variable in the file (e.g., 'agent', 'my_agent')"""
460461

461-
file_type: Optional[str] = None
462-
"""File type"""
462+
module_name: str
463+
"""Python module name derived from filename (without .py extension)"""
463464

464-
created: bool = False
465-
"""Whether this is a newly created file"""
465+
file_name: str
466+
"""Filename with extension (e.g., 'websearch_agent.py')"""
467+
468+
imports: Optional[List[str]] = None
469+
"""List of import statements found in the file"""
470+
471+
has_runner: bool = False
472+
"""Whether the file already contains a Runner definition"""
473+
474+
has_entrypoint: bool = False
475+
"""Whether the file already has an entrypoint decorator"""
476+
477+
detected_tools: Optional[List[str]] = None
478+
"""List of detected tool names used in the Agent"""
466479

467480
def __str__(self) -> str:
468-
return f"AgentFileInfo(path={self.path}, created={self.created})"
481+
return f"AgentFileInfo(file={self.file_name}, var={self.agent_var_name})"
469482

470483

471484
@dataclass

agentkit/toolkit/resources/wrappers/wrapper_stream.py.jinja2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ async def run(payload: dict, headers: dict):
8686
# Format as SSE data
8787
sse_event = event.model_dump_json(exclude_none=True, by_alias=True)
8888
logger.debug("Generated event in agent run streaming: %s", sse_event)
89-
yield f"data: {sse_event}\n\n"
89+
yield sse_event
9090
except Exception as e:
9191
logger.exception("Error in event_generator: %s", e)
9292
# Yield error event
9393
error_data = json.dumps({"error": str(e)})
94-
yield f'data: {error_data}\n\n'
94+
yield error_data
9595

9696

9797
@app.ping

agentkit/toolkit/utils/agent_parser.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,13 @@ def _detect_agent_variable(self, content: str) -> Optional[str]:
130130
"""
131131
Detect Agent variable name using pattern matching.
132132
133-
Patterns to match:
134-
1. variable_name = Agent(...)
135-
2. variable_name = Agent(
133+
Only matches direct instantiation: variable_name = Agent(...)
134+
For other patterns (e.g., agent = generate_agent_from_config()),
135+
user should specify --agent-var explicitly.
136136
"""
137-
# Pattern 1: Single line assignment
138137
# Match: agent = Agent(...) or my_agent = Agent(...)
139-
pattern1 = r'(\w+)\s*=\s*Agent\s*\('
140-
141-
matches = re.findall(pattern1, content, re.MULTILINE)
138+
pattern = r'(\w+)\s*=\s*Agent\s*\('
139+
matches = re.findall(pattern, content, re.MULTILINE)
142140

143141
if matches:
144142
# If multiple matches, prefer common names or last one
@@ -156,10 +154,17 @@ def _detect_agent_variable(self, content: str) -> Optional[str]:
156154

157155
def _validate_agent_var(self, content: str, var_name: str) -> bool:
158156
"""
159-
Validate that the specified variable is an Agent instance.
157+
Validate that the specified variable exists and is assigned a value.
158+
159+
When user explicitly specifies --agent-var, we trust them and only check
160+
that the variable is assigned something. This handles cases like:
161+
- agent = Agent(...)
162+
- agent = generate_agent_from_config(config_path)
163+
- agent = create_custom_agent()
160164
"""
161-
pattern = rf'\b{re.escape(var_name)}\s*=\s*Agent\s*\('
162-
return bool(re.search(pattern, content))
165+
# Check if variable is assigned to anything: var_name = ...
166+
pattern = rf'^\s*{re.escape(var_name)}\s*=\s*\S'
167+
return bool(re.search(pattern, content, re.MULTILINE))
163168

164169
def _extract_imports(self, content: str) -> List[str]:
165170
"""

agentkit/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
VERSION = "0.1.7"
15+
VERSION = "0.1.9"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "agentkit-sdk-python"
3-
version = "0.1.7"
3+
version = "0.1.9"
44
description = "Python SDK for transforming any AI agent into a production-ready application. Framework-agnostic primitives for runtime, memory, authentication, and tools with volcengine-managed infrastructure."
55
readme = "README.md"
66
requires-python = ">=3.10"

0 commit comments

Comments
 (0)