feat: add FastAPI RESTful server with SSE streaming and ark_apikey support#80
Conversation
- Created gui_agents/restful_app.py with FastAPI implementation - Implemented all required endpoints from mini-agent OpenAPI spec - Added ark_apikey parameter support for model configuration - Updated pyproject.toml with restful optional dependencies - Updated Dockerfile to support RESTful server - Added lybic-guiagent-restful entry point Co-authored-by: AEnjoy <37976919+AEnjoy@users.noreply.github.com>
- Created comprehensive test suite in tests/test_restful_api.py - All 9 tests pass successfully - Added detailed documentation in docs/restful_api.md - Documentation includes API endpoints, examples, and usage guide Co-authored-by: AEnjoy <37976919+AEnjoy@users.noreply.github.com>
- Created comprehensive Python client example in examples/restful_client_example.py - Added RESTful API server information to main README.md - Included usage examples and documentation links - Client example demonstrates async submission, streaming, and task management Co-authored-by: AEnjoy <37976919+AEnjoy@users.noreply.github.com>
- Add comment explaining shared dependencies between mcp and restful - Improve clarity of comments about sandbox object removal - Explain why sandbox needs to be removed from backend_kwargs Co-authored-by: AEnjoy <37976919+AEnjoy@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR implements a FastAPI-based RESTful API server as an HTTP/REST alternative to the existing gRPC service. The implementation provides 8 endpoints matching the mini-agent OpenAPI specification, with Server-Sent Events (SSE) for real-time streaming and ark_apikey parameter support for per-request LLM model configuration.
Key Changes:
- New RESTful server implementation in
gui_agents/restful_app.py(997 lines) with endpoints for agent operations, task management, and sandbox creation - SSE-based streaming via
EventSourceResponsefor real-time task execution updates - Per-request LLM configuration through
ark_apikeyparameter, enabling multi-tenant scenarios with custom API keys
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
gui_agents/restful_app.py |
Core RESTful server implementation with 8 API endpoints, RestfulAgentService class mirroring gRPC architecture, SSE streaming support, and ark_apikey-based model configuration |
tests/test_restful_api.py |
Syntax and structure validation tests for RESTful implementation covering models, endpoints, imports, and streaming support |
pyproject.toml |
Added optional [restful] dependency group with FastAPI, sse-starlette, uvicorn, and pydantic; added lybic-guiagent-restful entry point |
Dockerfile |
Updated to include restful extras in build and expose port 8080 for RESTful API |
docs/restful_api.md |
Comprehensive documentation covering installation, configuration, all 8 endpoints, ark_apikey usage, examples, and troubleshooting |
examples/restful_client_example.py |
Example client implementation demonstrating async submission, status polling, streaming, and custom model configuration |
README.md |
Added documentation for running RESTful server via Docker with reference to detailed usage guide |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| class SandboxInfo: | ||
| def __init__(self, id, shape_name): | ||
| self.id = id | ||
| self.shapeName = shape_name |
There was a problem hiding this comment.
The SandboxInfo class is duplicated in both _create_sandbox (lines 294-297) and _get_sandbox_pb (lines 316-319). Consider defining this class once at the module or class level to avoid code duplication and improve maintainability.
| task_id = str(uuid.uuid4()) | ||
| logger.info(f"Received /api/agent/run request, assigning task_id: {task_id}") | ||
|
|
||
| service.metrics.record_grpc_request("RunAgent") |
There was a problem hiding this comment.
The method names record_grpc_request and record_grpc_error suggest gRPC-specific metrics, but this is a RESTful API. While this may be intentional for metric compatibility, consider whether these method names accurately reflect their use in a REST context. If the metrics system is shared between gRPC and REST implementations, this is acceptable, but the naming could be misleading.
- Replace direct LybicClient instantiation with async context manager - Simplify sandbox creation and retrieval logic - Update task cancellation handling to use 'canceled' instead of 'cancelled' - Fix module import naming from app to cli_app - Adjust logging and metrics recording for consistency - Update backend compatibility checks to use cli_app module
…trol - Introduce StageModelConfig and LLMConfig Pydantic models - Replace single ark_apikey field with structured stage_model_config - Support per-component LLM configuration (grounding, action generation, etc.) - Allow custom API endpoints and provider-specific settings - Update REST API documentation with new parameter and examples - Modify agent creation logic to apply stage-specific configurations - Maintain backward compatibility through action_generator_model fallback - Add support for platform-specific agent instantiation
|
/gemini review |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a significant and well-structured feature: a FastAPI-based RESTful server that mirrors the existing gRPC service. The implementation includes SSE for real-time streaming, comprehensive documentation, and an example client, which are all excellent additions. The code is generally clean and follows modern asynchronous Python practices. My review focuses on a few key areas for improvement: ensuring the ark_apikey feature is fully implemented as described, enhancing error handling for web clients, and a couple of refactoring suggestions for better maintainability and dependency management.
| sandbox = await lybic_client.sandbox.get(result.id) | ||
|
|
||
| # Create simple namespace object to mimic protobuf | ||
| class SandboxInfo: |
There was a problem hiding this comment.
The nested class SandboxInfo is used here and in _get_sandbox_pb to mimic a protobuf structure. In a FastAPI application, it's more idiomatic and maintainable to define this as a Pydantic BaseModel at the top level of the file. This improves type safety, consistency with other models, and allows for easier serialization (e.g., using .model_dump()).
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Implemented MigrationManager class to handle SQL migrations - Added automatic migration execution during PostgreSQL storage initialization - Created migration tracking table (schema_migrations) to record applied versions - Added support for running individual migration files - Included convenience function migrate_database for easy migration execution - Added CLI script (run_migrations.py) for manual migration management - Created README.md with migration documentation and troubleshooting guide - Added first migration (001_add_conversation_history.sql) to extend agent_tasks table - Implemented idempotent migration operations using IF NOT EXISTS clauses - Added proper logging and error handling for migration processes
- Add logic to retrieve and validate sandbox from previous tasks - Handle sandbox expiration and not-found errors with proper messaging - Ensure sandbox ID consistency when both previous and new IDs are provided - Update all three apps (grpc, mcp, restful) to support continuing tasks - Introduce final_sandbox_id variable for clearer sandbox management - Log sandbox usage and creation for better traceability - Import LybicAPIError for more precise error handling
Summary of your change / What this PR does / why we need it?
Implements a RESTful API server using FastAPI as an HTTP/REST alternative to the existing gRPC service. Provides 8 endpoints matching the mini-agent OpenAPI specification with Server-Sent Events for real-time streaming and
ark_apikeyparameter for per-request LLM model configuration.Implementation Details:
Server (
gui_agents/restful_app.py, 997 lines)/api/agent/{info,run,submit,status,cancel,tasks},/api/sandbox/createEventSourceResponsefor task executionRestfulAgentServicemirrorsAgentServicerarchitecture from grpc_app.pyModel Configuration
ark_apikeyparameter applies custom API keys to all LLM toolsLLMConfigbut via request bodyDependencies (
pyproject.toml)[restful]group: fastapi, sse-starlette, uvicorn, pydanticlybic-guiagent-restfulDocker (
Dockerfile)Usage:
Interactive docs at
/docs(Swagger UI).Please indicate you've done the following:
Special notes for your reviewer:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.