-
Notifications
You must be signed in to change notification settings - Fork 7
feat(config): improve config hot reload handling and introduce mock sse endpoint for ui testing as not all providers support prompt caching #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
161 changes: 161 additions & 0 deletions
161
unoplat-code-confluence-query-engine/experiments/mock_sse_server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| """Mock SSE server that replays events from docs/sse-agent-results.txt. | ||
|
|
||
| This standalone FastAPI app exposes an endpoint compatible with the real | ||
| `/v1/codebase-agent-rules` route and streams Server-Sent Events in the exact | ||
| order captured in the reference log file. Useful for UI/dev testing without | ||
| running the full agent pipeline or external services. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from pathlib import Path | ||
| from typing import AsyncGenerator, Dict, List, Optional | ||
|
|
||
| from fastapi import FastAPI, Query, Request | ||
| from fastapi.middleware.cors import CORSMiddleware | ||
| from loguru import logger | ||
| from sse_starlette.sse import EventSourceResponse | ||
|
|
||
| APP_TITLE: str = "Mock Codebase Agent Rules SSE Server" | ||
| DEFAULT_DELAY_SECONDS: float = 0.05 | ||
|
|
||
| # Resolve repository root and reference SSE log file | ||
| REPO_ROOT: Path = Path(__file__).resolve().parents[1] | ||
| SSE_LOG_PATH: Path = REPO_ROOT / "docs" / "sse-agent-results.txt" | ||
|
|
||
|
|
||
| # In-memory cache of parsed SSE events | ||
| LOADED_EVENTS: List[Dict[str, str]] = [] | ||
|
|
||
|
|
||
| def parse_sse_log_file(file_path: Path) -> List[Dict[str, str]]: | ||
| """Parse a newline-delimited SSE capture file into event dicts. | ||
|
|
||
| The expected format mirrors what `curl -N` would show: | ||
| id: 1\n | ||
| event: repo:agent:event_type\n | ||
| data: {"json": "object"}\n | ||
| \n | ||
| - Blank line separates events | ||
| - Lines starting with "::" are treated as comments/keepalive pings and skipped | ||
|
|
||
| Returns a list of dicts each containing keys: "id", "event", "data". | ||
| Data is preserved as a raw JSON string to match the real endpoint behavior. | ||
| """ | ||
| events: List[Dict[str, str]] = [] | ||
| current: Dict[str, str] = {} | ||
|
|
||
| try: | ||
| raw_text: str = file_path.read_text(encoding="utf-8") | ||
| except FileNotFoundError as exc: | ||
| logger.error("SSE log file not found at {}", file_path) | ||
| raise exc | ||
|
|
||
| for raw_line in raw_text.splitlines(): | ||
| line: str = raw_line.strip() | ||
|
|
||
| if not line: | ||
| if "event" in current and "data" in current: | ||
| if "id" not in current: | ||
| current["id"] = str(len(events)) | ||
| events.append(current) | ||
| current = {} | ||
| continue | ||
|
|
||
| if line.startswith("::"): | ||
| # Skip comment/ping lines | ||
| continue | ||
|
|
||
| if line.startswith("id:"): | ||
| current["id"] = line.partition("id:")[2].strip() | ||
| continue | ||
|
|
||
| if line.startswith("event:"): | ||
| current["event"] = line.partition("event:")[2].strip() | ||
| continue | ||
|
|
||
| if line.startswith("data:"): | ||
| # Preserve raw JSON string as-is | ||
| current["data"] = line.partition("data:")[2].strip() | ||
| continue | ||
|
|
||
| # Finalize trailing event if file doesn't end with a blank line | ||
| if current and "event" in current and "data" in current: | ||
| if "id" not in current: | ||
| current["id"] = str(len(events)) | ||
| events.append(current) | ||
|
|
||
| return events | ||
|
|
||
|
|
||
| async def stream_mock_events( | ||
| request: Request, *, delay_seconds: float | ||
| ) -> AsyncGenerator[Dict[str, str], None]: | ||
| """Yield parsed SSE events to the client with an optional delay between them. | ||
|
|
||
| The yielded dicts follow sse-starlette's expected structure: keys "id", | ||
| "event", and "data". `data` remains a JSON string, matching the real | ||
| endpoint which serializes payloads prior to emission. | ||
| """ | ||
| for event in LOADED_EVENTS: | ||
| if await request.is_disconnected(): | ||
| logger.info("Mock SSE client disconnected early") | ||
| break | ||
|
|
||
| yield event | ||
|
|
||
| if delay_seconds > 0: | ||
| await asyncio.sleep(delay_seconds) | ||
|
|
||
|
|
||
| app: FastAPI = FastAPI(title=APP_TITLE) | ||
|
|
||
| # Add CORS middleware to allow cross-origin requests (matches main.py configuration) | ||
| app.add_middleware( | ||
| CORSMiddleware, | ||
| allow_origins=["*"], # Configure appropriately for production | ||
| allow_credentials=True, | ||
| allow_methods=["*"], | ||
| allow_headers=["*"], | ||
| expose_headers=["*"], # Important for SSE headers | ||
| ) | ||
|
|
||
|
|
||
| @app.on_event("startup") | ||
| async def on_startup() -> None: | ||
| """Load and cache SSE events from the reference file on startup.""" | ||
| global LOADED_EVENTS | ||
| LOADED_EVENTS = parse_sse_log_file(SSE_LOG_PATH) | ||
| logger.info("Loaded {} mock SSE events from {}", len(LOADED_EVENTS), SSE_LOG_PATH) | ||
|
|
||
|
|
||
| @app.get("/v1/codebase-agent-rules") | ||
| async def get_mock_codebase_agent_rules( | ||
| request: Request, | ||
| owner_name: str = Query(..., description="Repository owner name"), | ||
| repo_name: str = Query(..., description="Repository name"), | ||
| delay_seconds: Optional[float] = Query( | ||
| default=DEFAULT_DELAY_SECONDS, | ||
| ge=0.0, | ||
| le=2.0, | ||
| description="Artificial delay between events to simulate streaming", | ||
| ), | ||
| ) -> EventSourceResponse: | ||
| """Stream mock SSE events in the same order as the captured session. | ||
|
|
||
| Query params are accepted for API compatibility, but do not affect the | ||
| mocked stream content. | ||
| """ | ||
| headers: Dict[str, str] = { | ||
| "Cache-Control": "no-cache", | ||
| "Connection": "keep-alive", | ||
| "X-Accel-Buffering": "no", | ||
| } | ||
| return EventSourceResponse( | ||
| stream_mock_events(request, delay_seconds=delay_seconds or 0.0), | ||
| ping=10, | ||
| headers=headers, | ||
| ) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| type: http_request | ||
| model: http_request | ||
| id: rq_ZCT8QECJLF | ||
| createdAt: 2025-09-15T11:23:47.226515 | ||
| updatedAt: 2025-09-15T11:24:59.927451 | ||
| workspaceId: wk_KULjx4bEa8 | ||
| folderId: fl_zukJtBcFCC | ||
| authentication: {} | ||
| authenticationType: null | ||
| body: {} | ||
| bodyType: null | ||
| description: '' | ||
| headers: [] | ||
| method: GET | ||
| name: mock-test-agent-pc-dw-bl-xai-grok-code-fast-1-or-all-codebases | ||
| sortPriority: 4000.001 | ||
| url: http://localhost:8002/v1/codebase-agent-rules | ||
| urlParameters: | ||
| - enabled: true | ||
| name: owner_name | ||
| value: unoplat | ||
| id: XVEctDb8wq | ||
| - enabled: true | ||
| name: repo_name | ||
| value: unoplat-code-confluence | ||
| id: 3apksU6Xei | ||
| - enabled: true | ||
| name: '' | ||
| value: '' | ||
| id: zfTZmKzX25 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Handle missing SSE log file in mock server
The mock SSE app loads
REPO_ROOT / "docs" / "sse-agent-results.txt"during startup, but no such file exists anywhere in the repository (seefind . -name "sse-agent-results.txt"returning nothing). Becauseparse_sse_log_fileimmediately raisesFileNotFoundErrorwhen the file is missing, the FastAPI application never finishes starting and the newtask mock-sseor Docker usage will crash before serving any events. Either ship the reference file with the repo or guard the startup logic so the server can run without it.Useful? React with 👍 / 👎.