|
1 | 1 | """Agent command for creating remote agent runs."""
|
2 | 2 |
|
| 3 | +import json |
| 4 | + |
3 | 5 | import requests
|
4 | 6 | import typer
|
5 | 7 | from rich import box
|
6 | 8 | from rich.console import Console
|
7 | 9 | from rich.panel import Panel
|
| 10 | +from rich.syntax import Syntax |
8 | 11 |
|
9 | 12 | from codegen.cli.api.endpoints import API_ENDPOINT
|
10 |
| -from codegen.cli.auth.token_manager import get_current_token |
| 13 | +from codegen.cli.auth.token_manager import get_current_org_name, get_current_token |
11 | 14 | from codegen.cli.rich.spinners import create_spinner
|
12 | 15 | from codegen.cli.utils.org import resolve_org_id
|
13 | 16 |
|
@@ -141,19 +144,91 @@ def agent_callback(ctx: typer.Context):
|
141 | 144 | raise typer.Exit()
|
142 | 145 |
|
143 | 146 |
|
144 |
| -# For backward compatibility, also allow `codegen agent --prompt "..."` |
| 147 | +# For backward compatibility, also allow `codegen agent --prompt "..."` and `codegen agent --id X --json` |
145 | 148 | def agent(
|
146 |
| - prompt: str = typer.Option(None, "--prompt", "-p", help="The prompt to send to the agent"), |
| 149 | + prompt: str | None = typer.Option(None, "--prompt", "-p", help="The prompt to send to the agent"), |
| 150 | + agent_id: int | None = typer.Option(None, "--id", help="Agent run ID to fetch"), |
| 151 | + as_json: bool = typer.Option(False, "--json", help="Output raw JSON response"), |
147 | 152 | org_id: int | None = typer.Option(None, help="Organization ID (defaults to CODEGEN_ORG_ID/REPOSITORY_ORG_ID or auto-detect)"),
|
148 | 153 | model: str | None = typer.Option(None, help="Model to use for this agent run (optional)"),
|
149 | 154 | repo_id: int | None = typer.Option(None, help="Repository ID to use for this agent run (optional)"),
|
150 | 155 | ):
|
151 |
| - """Create a new agent run with the given prompt.""" |
| 156 | + """Create a new agent run with the given prompt, or fetch an existing agent run by ID.""" |
152 | 157 | if prompt:
|
153 | 158 | # If prompt is provided, create the agent run
|
154 | 159 | create(prompt=prompt, org_id=org_id, model=model, repo_id=repo_id)
|
| 160 | + elif agent_id: |
| 161 | + # If agent ID is provided, fetch the agent run |
| 162 | + get(agent_id=agent_id, as_json=as_json, org_id=org_id) |
155 | 163 | else:
|
156 |
| - # If no prompt, show help |
157 |
| - console.print("[red]Error:[/red] --prompt is required") |
158 |
| - console.print("Usage: [cyan]codegen agent --prompt 'Your prompt here'[/cyan]") |
| 164 | + # If neither prompt nor agent_id, show help |
| 165 | + console.print("[red]Error:[/red] Either --prompt or --id is required") |
| 166 | + console.print("Usage:") |
| 167 | + console.print(" [cyan]codegen agent --prompt 'Your prompt here'[/cyan] # Create agent run") |
| 168 | + console.print(" [cyan]codegen agent --id 123 --json[/cyan] # Fetch agent run as JSON") |
| 169 | + raise typer.Exit(1) |
| 170 | + |
| 171 | + |
| 172 | +@agent_app.command() |
| 173 | +def get( |
| 174 | + agent_id: int = typer.Option(..., "--id", help="Agent run ID to fetch"), |
| 175 | + as_json: bool = typer.Option(False, "--json", help="Output raw JSON response"), |
| 176 | + org_id: int | None = typer.Option(None, help="Organization ID (defaults to CODEGEN_ORG_ID/REPOSITORY_ORG_ID or auto-detect)"), |
| 177 | +): |
| 178 | + """Fetch and display details for a specific agent run.""" |
| 179 | + # Get the current token |
| 180 | + token = get_current_token() |
| 181 | + if not token: |
| 182 | + console.print("[red]Error:[/red] Not authenticated. Please run 'codegen login' first.") |
| 183 | + raise typer.Exit(1) |
| 184 | + |
| 185 | + try: |
| 186 | + # Resolve org id (fast, uses stored data) |
| 187 | + resolved_org_id = resolve_org_id(org_id) |
| 188 | + if resolved_org_id is None: |
| 189 | + console.print("[red]Error:[/red] Organization ID not provided. Pass --org-id, set CODEGEN_ORG_ID, or REPOSITORY_ORG_ID.") |
| 190 | + raise typer.Exit(1) |
| 191 | + |
| 192 | + spinner = create_spinner(f"Fetching agent run {agent_id}...") |
| 193 | + spinner.start() |
| 194 | + |
| 195 | + try: |
| 196 | + headers = {"Authorization": f"Bearer {token}"} |
| 197 | + # Fixed: Use /agent/run/{id} not /agent/runs/{id} |
| 198 | + url = f"{API_ENDPOINT.rstrip('/')}/v1/organizations/{resolved_org_id}/agent/run/{agent_id}" |
| 199 | + response = requests.get(url, headers=headers) |
| 200 | + response.raise_for_status() |
| 201 | + agent_data = response.json() |
| 202 | + finally: |
| 203 | + spinner.stop() |
| 204 | + |
| 205 | + # Output the data |
| 206 | + if as_json: |
| 207 | + # Pretty print JSON with syntax highlighting |
| 208 | + formatted_json = json.dumps(agent_data, indent=2, sort_keys=True) |
| 209 | + syntax = Syntax(formatted_json, "json", theme="monokai", line_numbers=False) |
| 210 | + console.print(syntax) |
| 211 | + else: |
| 212 | + # Display formatted information (fallback for future enhancement) |
| 213 | + formatted_json = json.dumps(agent_data, indent=2, sort_keys=True) |
| 214 | + syntax = Syntax(formatted_json, "json", theme="monokai", line_numbers=False) |
| 215 | + console.print(syntax) |
| 216 | + |
| 217 | + except requests.HTTPError as e: |
| 218 | + # Get organization name for better error messages |
| 219 | + org_name = get_current_org_name() |
| 220 | + org_display = f"{org_name} ({resolved_org_id})" if org_name else f"organization {resolved_org_id}" |
| 221 | + |
| 222 | + if e.response.status_code == 404: |
| 223 | + console.print(f"[red]Error:[/red] Agent run {agent_id} not found in {org_display}.") |
| 224 | + elif e.response.status_code == 403: |
| 225 | + console.print(f"[red]Error:[/red] Access denied to agent run {agent_id} in {org_display}. Check your permissions.") |
| 226 | + else: |
| 227 | + console.print(f"[red]Error:[/red] HTTP {e.response.status_code}: {e}") |
| 228 | + raise typer.Exit(1) |
| 229 | + except requests.RequestException as e: |
| 230 | + console.print(f"[red]Error fetching agent run:[/red] {e}") |
| 231 | + raise typer.Exit(1) |
| 232 | + except Exception as e: |
| 233 | + console.print(f"[red]Unexpected error:[/red] {e}") |
159 | 234 | raise typer.Exit(1)
|
0 commit comments