-
Notifications
You must be signed in to change notification settings - Fork 9.5k
feat: add public answer sharing #3306
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
LittleChenLiya
wants to merge
5
commits into
bytedance:main
Choose a base branch
from
LittleChenLiya:fix/issue-3288-share-answer
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.
+1,154
−12
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1643112
feat: add public answer sharing
LittleChenLiya a68dc5b
fix: address share review feedback
LittleChenLiya 2febe3f
fix: harden public share snapshots
LittleChenLiya d78c0fb
fix: address share edge cases
LittleChenLiya 903bcf9
fix: persist share authorization metadata
LittleChenLiya 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| from . import artifacts, assistants_compat, mcp, models, skills, suggestions, thread_runs, threads, uploads | ||
| from . import artifacts, assistants_compat, mcp, models, shares, skills, suggestions, thread_runs, threads, uploads | ||
|
|
||
| __all__ = ["artifacts", "assistants_compat", "mcp", "models", "skills", "suggestions", "threads", "thread_runs", "uploads"] | ||
| __all__ = ["artifacts", "assistants_compat", "mcp", "models", "shares", "skills", "suggestions", "threads", "thread_runs", "uploads"] |
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 @@ | ||
| """Public conversation share endpoints.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import secrets | ||
| from typing import Any | ||
|
|
||
| from fastapi import APIRouter, HTTPException, Request | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from app.gateway.authz import require_permission | ||
| from app.gateway.deps import get_checkpointer, get_store | ||
| from app.gateway.utils import sanitize_log_param | ||
| from deerflow.runtime import serialize_channel_values | ||
| from deerflow.utils.time import now_iso | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| router = APIRouter(prefix="/api/shares", tags=["shares"]) | ||
|
|
||
| _SHARES_NS = ("shares",) | ||
| _SHARE_ID_BYTES = 16 | ||
|
|
||
|
|
||
| class ShareCreateRequest(BaseModel): | ||
| """Request body for creating a public share snapshot.""" | ||
|
|
||
| message_ids: list[str] = Field( | ||
| min_length=1, | ||
| description="Message IDs to include in the public share.", | ||
| ) | ||
| title: str | None = Field(default=None, max_length=256, description="Optional share title") | ||
|
|
||
|
|
||
| class ShareCreateResponse(BaseModel): | ||
| share_id: str | ||
| title: str | None = None | ||
| created_at: str | ||
|
|
||
|
|
||
| class ShareResponse(BaseModel): | ||
| share_id: str | ||
| title: str | None = None | ||
| messages: list[dict[str, Any]] = Field(default_factory=list) | ||
| created_at: str | ||
|
|
||
|
|
||
| def _extract_message_id(message: dict[str, Any]) -> str | None: | ||
| message_id = message.get("id") | ||
| return message_id if isinstance(message_id, str) and message_id else None | ||
|
|
||
|
|
||
| def _has_displayable_content(message: dict[str, Any]) -> bool: | ||
| content = message.get("content") | ||
| if isinstance(content, str): | ||
| return bool(content.strip()) | ||
| if isinstance(content, list): | ||
| return len(content) > 0 | ||
| return content is not None | ||
|
|
||
|
|
||
| def _is_shareable_message(message: dict[str, Any]) -> bool: | ||
| message_type = message.get("type") | ||
| if message_type == "human": | ||
| return _has_displayable_content(message) | ||
| if message_type == "ai": | ||
| return _has_displayable_content(message) and not message.get("tool_calls") and not message.get("invalid_tool_calls") | ||
| return False | ||
|
|
||
|
|
||
| async def _put_unique_share(store, value: dict[str, Any]) -> str: | ||
| for _ in range(4): | ||
| share_id = secrets.token_urlsafe(_SHARE_ID_BYTES) | ||
| if await store.aget(_SHARES_NS, share_id) is None: | ||
| await store.aput(_SHARES_NS, share_id, value) | ||
| return share_id | ||
| raise HTTPException(status_code=500, detail="Failed to create share") | ||
|
LittleChenLiya marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @router.post("/threads/{thread_id}", response_model=ShareCreateResponse) | ||
| @require_permission("threads", "read", owner_check=True, require_existing=True) | ||
| async def create_thread_share(thread_id: str, body: ShareCreateRequest, request: Request) -> ShareCreateResponse: | ||
| """Create a public immutable snapshot from an owned thread.""" | ||
| store = get_store(request) | ||
| if store is None: | ||
| raise HTTPException(status_code=503, detail="Store not available") | ||
|
|
||
| checkpointer = get_checkpointer(request) | ||
| if checkpointer is None: | ||
| raise HTTPException(status_code=503, detail="Checkpointer not available") | ||
|
|
||
| config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} | ||
| try: | ||
| checkpoint_tuple = await checkpointer.aget_tuple(config) | ||
| except Exception: | ||
|
LittleChenLiya marked this conversation as resolved.
|
||
| logger.exception("Failed to get state for share source thread %s", sanitize_log_param(thread_id)) | ||
| raise HTTPException(status_code=500, detail="Failed to create share") | ||
|
|
||
| if checkpoint_tuple is None: | ||
| raise HTTPException(status_code=404, detail=f"Thread {thread_id} not found") | ||
|
|
||
| checkpoint = getattr(checkpoint_tuple, "checkpoint", {}) or {} | ||
| channel_values = checkpoint.get("channel_values", {}) or {} | ||
| serialized_values = serialize_channel_values(channel_values) | ||
| all_messages = serialized_values.get("messages", []) | ||
| if not isinstance(all_messages, list) or not all_messages: | ||
| raise HTTPException(status_code=400, detail="Thread has no messages to share") | ||
|
|
||
| requested_ids = [message_id for message_id in body.message_ids if message_id] | ||
| if not requested_ids: | ||
| raise HTTPException(status_code=400, detail="No message IDs selected") | ||
|
|
||
| requested_id_set = set(requested_ids) | ||
| selected_messages = [message for message in all_messages if isinstance(message, dict) and _extract_message_id(message) in requested_id_set] | ||
|
LittleChenLiya marked this conversation as resolved.
Outdated
|
||
| selected_id_set = {message_id for message in selected_messages if (message_id := _extract_message_id(message)) is not None} | ||
| missing_ids = [message_id for message_id in requested_ids if message_id not in selected_id_set] | ||
| if missing_ids: | ||
| raise HTTPException(status_code=400, detail=f"Message IDs not found: {', '.join(missing_ids)}") | ||
|
|
||
| non_shareable_ids = [message_id for message in selected_messages if (message_id := _extract_message_id(message)) is not None and not _is_shareable_message(message)] | ||
|
LittleChenLiya marked this conversation as resolved.
Outdated
|
||
| if non_shareable_ids: | ||
| raise HTTPException(status_code=400, detail=f"Message IDs are not shareable: {', '.join(non_shareable_ids)}") | ||
|
|
||
| created_at = now_iso() | ||
| title = body.title or serialized_values.get("title") | ||
| if not isinstance(title, str): | ||
| title = None | ||
|
LittleChenLiya marked this conversation as resolved.
|
||
|
|
||
| share_id = await _put_unique_share( | ||
| store, | ||
| { | ||
| "title": title, | ||
| "messages": selected_messages, | ||
| "created_at": created_at, | ||
| }, | ||
| ) | ||
| return ShareCreateResponse(share_id=share_id, title=title, created_at=created_at) | ||
|
|
||
|
|
||
| @router.get("/{share_id}", response_model=ShareResponse) | ||
| async def get_share(share_id: str, request: Request) -> ShareResponse: | ||
| """Read a public share snapshot without requiring authentication.""" | ||
| store = get_store(request) | ||
| if store is None: | ||
| raise HTTPException(status_code=503, detail="Store not available") | ||
|
|
||
| item = await store.aget(_SHARES_NS, share_id) | ||
| if item is None: | ||
| raise HTTPException(status_code=404, detail="Share not found") | ||
|
|
||
| value = item.value or {} | ||
| messages = value.get("messages", []) | ||
| if not isinstance(messages, list): | ||
| messages = [] | ||
| title = value.get("title") | ||
| return ShareResponse( | ||
| share_id=share_id, | ||
| title=title if isinstance(title, str) else None, | ||
| messages=messages, | ||
| created_at=value.get("created_at", ""), | ||
| ) | ||
|
LittleChenLiya marked this conversation as resolved.
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.