Skip to content

Commit bd89500

Browse files
committed
fix: cleanup
1 parent de53d1f commit bd89500

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+5993
-553
lines changed

src/codegen/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from codegen.extensions.events.codegen_app import CodegenApp
44
from codegen.sdk.core.codebase import Codebase
55
from codegen.shared.enums.programming_language import ProgrammingLanguage
6+
from codegen.agents.agent import Agent
67

7-
__all__ = ["Codebase", "CodegenApp", "Function", "ProgrammingLanguage", "function"]
8+
__all__ = ["Agent", "Codebase", "CodegenApp", "Function", "ProgrammingLanguage", "function"]

src/codegen/agents/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Codegen Agent API module.
3+
"""
4+
from codegen.agents.agent import Agent
5+
6+
__all__ = ["Agent"]

src/codegen/agents/agent.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from typing import Optional, Dict, Any
2+
import os
3+
4+
from codegen.agents.client.openapi_client.api.agents_api import AgentsApi
5+
from codegen.agents.client.openapi_client.api_client import ApiClient
6+
from codegen.agents.client.openapi_client.models.agent_run_response import AgentRunResponse
7+
from codegen.agents.client.openapi_client.models.create_agent_run_input import CreateAgentRunInput
8+
from codegen.agents.client.openapi_client.configuration import Configuration
9+
from codegen.agents.constants import CODEGEN_BASE_API_URL
10+
11+
12+
class AgentTask:
13+
"""Represents an agent run job."""
14+
15+
def __init__(self, task_data: AgentRunResponse, api_client: ApiClient, org_id: int):
16+
self.id = task_data.id
17+
self.org_id = org_id
18+
self.status = task_data.status
19+
self.result = task_data.result
20+
self._api_client = api_client
21+
self._agents_api = AgentsApi(api_client)
22+
23+
def refresh(self) -> None:
24+
"""Refresh the job status from the API."""
25+
if self.id is None:
26+
return
27+
28+
job_data = self._agents_api.get_agent_run_v1_organizations_org_id_agent_run_agent_run_id_get(
29+
agent_run_id=int(self.id),
30+
org_id=int(self.org_id),
31+
authorization=f"Bearer {self._api_client.configuration.access_token}"
32+
)
33+
34+
# Convert API response to dict for attribute access
35+
job_dict = {}
36+
if hasattr(job_data, '__dict__'):
37+
job_dict = job_data.__dict__
38+
elif isinstance(job_data, dict):
39+
job_dict = job_data
40+
41+
self.status = job_dict.get("status")
42+
self.result = job_dict.get("result")
43+
44+
45+
class Agent:
46+
"""API client for interacting with Codegen AI agents."""
47+
48+
def __init__(self, token: str, org_id: Optional[int] = None, base_url: Optional[str] = CODEGEN_BASE_API_URL):
49+
"""
50+
Initialize a new Agent client.
51+
52+
Args:
53+
token: API authentication token
54+
org_id: Optional organization ID. If not provided, default org will be used.
55+
"""
56+
self.token = token
57+
self.org_id = org_id or int(os.environ.get("CODEGEN_ORG_ID", "1")) # Default to org ID 1 if not specified
58+
59+
# Configure API client
60+
config = Configuration(host=base_url, access_token=token)
61+
self.api_client = ApiClient(configuration=config)
62+
self.agents_api = AgentsApi(self.api_client)
63+
64+
# Current job
65+
self.current_job = None
66+
67+
def run(self, prompt: str) -> AgentTask:
68+
"""
69+
Run an agent with the given prompt.
70+
71+
Args:
72+
prompt: The instruction for the agent to execute
73+
74+
Returns:
75+
Job: A job object representing the agent run
76+
"""
77+
run_input = CreateAgentRunInput(prompt=prompt)
78+
agent_run_response = self.agents_api.create_agent_run_v1_organizations_org_id_agent_run_post(
79+
org_id=int(self.org_id),
80+
create_agent_run_input=run_input,
81+
authorization=f"Bearer {self.token}",
82+
_headers={"Content-Type": "application/json"}
83+
)
84+
85+
# Convert API response to dict for Job initialization
86+
87+
job = AgentTask(agent_run_response, self.api_client, self.org_id)
88+
self.current_job = job
89+
return job
90+
91+
def get_status(self) -> Optional[Dict[str, Any]]:
92+
"""
93+
Get the status of the current job.
94+
95+
Returns:
96+
dict: A dictionary containing job status information,
97+
or None if no job has been run.
98+
"""
99+
if self.current_job:
100+
self.current_job.refresh()
101+
return {
102+
"id": self.current_job.id,
103+
"status": self.current_job.status,
104+
"result": self.current_job.result
105+
}
106+
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.github/workflows/python.yml
2+
.gitignore
3+
.gitlab-ci.yml
4+
.openapi-generator-ignore
5+
.travis.yml
6+
README.md
7+
docs/AgentRunResponse.md
8+
docs/AgentsApi.md
9+
docs/CreateAgentRunInput.md
10+
docs/HTTPValidationError.md
11+
docs/OrganizationResponse.md
12+
docs/OrganizationSettings.md
13+
docs/OrganizationsApi.md
14+
docs/PageOrganizationResponse.md
15+
docs/PageUserResponse.md
16+
docs/UserResponse.md
17+
docs/UsersApi.md
18+
docs/ValidationError.md
19+
docs/ValidationErrorLocInner.md
20+
git_push.sh
21+
openapi_client/__init__.py
22+
openapi_client/api/__init__.py
23+
openapi_client/api/agents_api.py
24+
openapi_client/api/organizations_api.py
25+
openapi_client/api/users_api.py
26+
openapi_client/api_client.py
27+
openapi_client/api_response.py
28+
openapi_client/configuration.py
29+
openapi_client/exceptions.py
30+
openapi_client/models/__init__.py
31+
openapi_client/models/agent_run_response.py
32+
openapi_client/models/create_agent_run_input.py
33+
openapi_client/models/http_validation_error.py
34+
openapi_client/models/organization_response.py
35+
openapi_client/models/organization_settings.py
36+
openapi_client/models/page_organization_response.py
37+
openapi_client/models/page_user_response.py
38+
openapi_client/models/user_response.py
39+
openapi_client/models/validation_error.py
40+
openapi_client/models/validation_error_loc_inner.py
41+
openapi_client/py.typed
42+
openapi_client/rest.py
43+
pyproject.toml
44+
requirements.txt
45+
setup.cfg
46+
setup.py
47+
test-requirements.txt
48+
test/__init__.py
49+
test/test_agent_run_response.py
50+
test/test_agents_api.py
51+
test/test_create_agent_run_input.py
52+
test/test_http_validation_error.py
53+
test/test_organization_response.py
54+
test/test_organization_settings.py
55+
test/test_organizations_api.py
56+
test/test_page_organization_response.py
57+
test/test_page_user_response.py
58+
test/test_user_response.py
59+
test/test_users_api.py
60+
test/test_validation_error.py
61+
test/test_validation_error_loc_inner.py
62+
tox.ini

src/codegen/agents/client/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# openapi-client
2+
API for application developers
3+
4+
This Python directory was automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project. However it the generated code was altered to make compatible with the rest of the project.
5+
6+
- API version: 1.0.0
7+
8+
9+
10+
### Steps to update client directory
11+
12+
1. Fetch the api schema from the API endpoint [https://codegen-sh--rest-api.modal.run/api/openapi.json](schema file)
13+
2. generate the client code with the following command:
14+
15+
```bash
16+
openapi-generator generate -i openapi.yaml -g python -o ./client
17+
```
18+
3. This command will generate a lot of unused files we just need to include the files in the `openapi_client` directory to the project.
19+
20+
4. May need to fix the imports for `openapi_client` to be fully qualified import paths.
21+
22+
23+
5. TODO: make updates more streamlined. Ideally setup this api client as it's own package so all it takes is to generate the new code, no addtional manual steps are needed.
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# coding: utf-8
2+
3+
# flake8: noqa
4+
5+
"""
6+
Developer API
7+
8+
API for application developers
9+
10+
The version of the OpenAPI document: 1.0.0
11+
Generated by OpenAPI Generator (https://openapi-generator.tech)
12+
13+
Do not edit the class manually.
14+
""" # noqa: E501
15+
16+
17+
__version__ = "1.0.0"
18+
19+
# import apis into sdk package
20+
from codegen.agents.client.openapi_client.api.agents_api import AgentsApi
21+
from codegen.agents.client.openapi_client.api.organizations_api import OrganizationsApi
22+
from codegen.agents.client.openapi_client.api.users_api import UsersApi
23+
24+
# import ApiClient
25+
from codegen.agents.client.openapi_client.api_response import ApiResponse
26+
from codegen.agents.client.openapi_client.api_client import ApiClient
27+
from codegen.agents.client.openapi_client.configuration import Configuration
28+
from codegen.agents.client.openapi_client.exceptions import OpenApiException
29+
from codegen.agents.client.openapi_client.exceptions import ApiTypeError
30+
from codegen.agents.client.openapi_client.exceptions import ApiValueError
31+
from codegen.agents.client.openapi_client.exceptions import ApiKeyError
32+
from codegen.agents.client.openapi_client.exceptions import ApiAttributeError
33+
from codegen.agents.client.openapi_client.exceptions import ApiException
34+
35+
# import models into sdk package
36+
from codegen.agents.client.openapi_client.models.agent_run_response import AgentRunResponse
37+
from codegen.agents.client.openapi_client.models.create_agent_run_input import CreateAgentRunInput
38+
from codegen.agents.client.openapi_client.models.http_validation_error import HTTPValidationError
39+
from codegen.agents.client.openapi_client.models.organization_response import OrganizationResponse
40+
from codegen.agents.client.openapi_client.models.organization_settings import OrganizationSettings
41+
from codegen.agents.client.openapi_client.models.page_organization_response import PageOrganizationResponse
42+
from codegen.agents.client.openapi_client.models.page_user_response import PageUserResponse
43+
from codegen.agents.client.openapi_client.models.user_response import UserResponse
44+
from codegen.agents.client.openapi_client.models.validation_error import ValidationError
45+
from codegen.agents.client.openapi_client.models.validation_error_loc_inner import ValidationErrorLocInner
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# flake8: noqa
2+
3+
# import apis into api package
4+
from codegen.agents.client.openapi_client.api.agents_api import AgentsApi
5+
from codegen.agents.client.openapi_client.api.organizations_api import OrganizationsApi
6+
from codegen.agents.client.openapi_client.api.users_api import UsersApi
7+

0 commit comments

Comments
 (0)