-
Notifications
You must be signed in to change notification settings - Fork 5
feat: context assembly -- per-task context packaging via MCP (OPE-172) #291
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
DevanshuNEU
merged 3 commits into
OpenCodeIntel:main
from
DevanshuNEU:feat/context-assembler-ope-172
Mar 10, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """Context assembly endpoint. | ||
|
|
||
| Provides per-task context packaging via POST /api/v1/context/assemble. | ||
| Uses semantic search + dependency graph + project rules to build a | ||
| minimal, precise context package for AI coding assistants. | ||
| """ | ||
| import time | ||
| from typing import Any | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from dependencies import verify_repo_access | ||
| from middleware.auth import AuthContext, require_auth | ||
| from services.observability import ( | ||
| add_breadcrumb, | ||
| capture_exception, | ||
| logger, | ||
| metrics, | ||
| set_operation_context, | ||
| ) | ||
|
|
||
| router = APIRouter(tags=["context"]) | ||
|
|
||
|
|
||
| class AssembleRequest(BaseModel): | ||
| task: str = Field(..., min_length=3, max_length=1000) | ||
| repo_id: str | ||
| token_budget: int = Field(default=1500, ge=100, le=10000) | ||
|
|
||
|
|
||
| @router.post("/context/assemble") | ||
| async def assemble_context( | ||
| request: AssembleRequest, | ||
| auth: AuthContext = Depends(require_auth), | ||
| ) -> dict[str, Any]: | ||
| """Assemble task-specific context from semantic search + deps + rules. | ||
|
|
||
| Returns a markdown context package sized to fit within token_budget, | ||
| containing only the files, dependencies, and project rules relevant | ||
| to the given task description. | ||
| """ | ||
| set_operation_context( | ||
| "context_assemble", | ||
| user_id=auth.user_id, | ||
| repo_id=request.repo_id, | ||
| ) | ||
| add_breadcrumb("Context assembly requested", category="context", repo_id=request.repo_id) | ||
|
|
||
| verify_repo_access(request.repo_id, auth.user_id) | ||
|
|
||
| from dependencies import context_assembler | ||
|
|
||
| start = time.time() | ||
| try: | ||
| result = await context_assembler.assemble( | ||
| task=request.task, | ||
| repo_id=request.repo_id, | ||
| user_id=auth.user_id, | ||
| token_budget=request.token_budget, | ||
| ) | ||
|
|
||
| elapsed = time.time() - start | ||
| logger.info( | ||
| "Context assembled", | ||
| repo_id=request.repo_id, | ||
| files=result["files_found"], | ||
| tokens=result["tokens_used"], | ||
| budget=request.token_budget, | ||
| duration_ms=round(elapsed * 1000), | ||
| ) | ||
| metrics.timing("context_assemble_ms", elapsed * 1000) | ||
|
|
||
| return result | ||
|
|
||
| except HTTPException: | ||
| raise | ||
| except Exception as exc: | ||
| capture_exception(exc, operation="context_assemble", repo_id=request.repo_id) | ||
| logger.error("Context assembly failed: %s", exc) | ||
| raise HTTPException(status_code=500, detail="Context assembly failed") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.