-
Notifications
You must be signed in to change notification settings - Fork 91
Feature/rag service extraction #927
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
Open
kissghosts
wants to merge
9
commits into
wecode-ai:main
Choose a base branch
from
kissghosts:feature/rag-service-extraction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
32a6ec0
docs(knowledge): add rag service extraction design
kissghosts edda1f2
refactor(knowledge): extract local indexing engine kernel
kissghosts 35de16b
refactor(knowledge): add remote runtime contracts and transport scaffold
kissghosts 3807b64
refactor(knowledge): route indexing through runtime gateway factory
kissghosts a96729f
refactor(knowledge): extract rag execution into knowledge engine
kissghosts d4b97ec
test(knowledge): move engine storage tests out of backend
kissghosts 4b1dcc6
docs(knowledge): converge runtime split documentation
kissghosts 5014e28
refactor(knowledge): remove backend rag shim modules
kissghosts 743ee7e
fix(knowledge): address runtime extraction review feedback
kissghosts 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
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,90 @@ | ||
| # SPDX-FileCopyrightText: 2026 Weibo, Inc. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Internal attachment streaming endpoint for knowledge_runtime.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from collections.abc import Iterator | ||
|
|
||
| from fastapi import APIRouter, Depends, Header, HTTPException, status | ||
| from fastapi.responses import StreamingResponse | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from app.api.dependencies import get_db | ||
| from app.api.endpoints.adapter.attachments import _build_content_disposition | ||
| from app.models.subtask_context import ContextStatus, ContextType | ||
| from app.services.auth import extract_token_from_header | ||
| from app.services.auth.rag_download_token import verify_rag_download_token | ||
| from app.services.context import context_service | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| router = APIRouter(prefix="/rag/content", tags=["internal-rag-content"]) | ||
|
|
||
|
|
||
| def _binary_stream(binary_data: bytes) -> Iterator[bytes]: | ||
| yield binary_data | ||
|
|
||
|
|
||
| def _verify_rag_download_authorization( | ||
| attachment_id: int, | ||
| authorization: str = Header(default=""), | ||
| ) -> None: | ||
| """Validate Bearer token for internal RAG attachment download.""" | ||
|
|
||
| token = extract_token_from_header(authorization) | ||
| if not token: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||
| detail="Missing or invalid Authorization header", | ||
| ) | ||
|
|
||
| token_info = verify_rag_download_token(token) | ||
| if token_info is None or token_info.attachment_id != attachment_id: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||
| detail="Invalid RAG download token", | ||
| ) | ||
|
|
||
|
|
||
| @router.get("/{attachment_id}") | ||
| async def stream_rag_attachment_content( | ||
| attachment_id: int, | ||
| _: None = Depends(_verify_rag_download_authorization), | ||
| db: Session = Depends(get_db), | ||
| ): | ||
| """Stream attachment binary content for knowledge_runtime.""" | ||
|
|
||
| context = context_service.get_context_optional( | ||
| db=db, | ||
| context_id=attachment_id, | ||
| ) | ||
| if context is None or context.context_type != ContextType.ATTACHMENT.value: | ||
| raise HTTPException(status_code=404, detail="Attachment not found") | ||
| if context.status != ContextStatus.READY.value: | ||
| raise HTTPException(status_code=409, detail="Attachment is not ready") | ||
|
|
||
| binary_data = context_service.get_attachment_binary_data( | ||
| db=db, | ||
| context=context, | ||
| ) | ||
| if binary_data is None: | ||
| logger.error( | ||
| "Failed to retrieve binary data for internal RAG attachment %s", | ||
| attachment_id, | ||
| ) | ||
| raise HTTPException( | ||
| status_code=500, | ||
| detail="Failed to retrieve attachment data", | ||
| ) | ||
|
|
||
| return StreamingResponse( | ||
| _binary_stream(binary_data), | ||
| media_type=context.mime_type, | ||
| headers={ | ||
| "Content-Disposition": _build_content_disposition(context.original_filename) | ||
| }, | ||
| ) | ||
Oops, something went wrong.
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.
This isn't actually streaming yet.
backend/app/services/context/context_service.pyreturnsbytesfromget_attachment_binary_data(), so Line 70 has already materialized the whole attachment and Line 85 just yields that buffer once. Large attachments will therefore spike Backend memory on the new remote-indexing path; this needs a chunked/file-like reader from the storage layer instead of wrapping a singlebytesobject.Also applies to: 70-85
🤖 Prompt for AI Agents