File size: 41,704 Bytes
564aab6 67b16c6 79b2fcc 67b16c6 d7f2a7c 564aab6 79b2fcc 5de6e8f 79b2fcc 82bad21 564aab6 2b4c539 82bad21 2b4c539 880b591 67b16c6 2b4c539 67b16c6 79b2fcc 67b16c6 6155b26 67b16c6 77324b8 67b16c6 0611031 67b16c6 754345f 540437a 564aab6 ff8c636 7a76ad1 0a9e96d 67b16c6 8615c28 67b16c6 5db99fa 092f909 d7637ba 5db99fa 2b4c539 5db99fa 1b922dd 5db99fa d7637ba 092f909 d7637ba 092f909 d7637ba 092f909 d7637ba 092f909 d7637ba 092f909 540437a d7637ba 540437a d7637ba 540437a d7637ba 5db99fa 540437a d7637ba 540437a d7637ba 540437a d7637ba d9d9785 d7637ba 540437a 5db99fa d7637ba 540437a d7637ba 540437a d9d9785 540437a 5de6e8f 2b4c539 d9d9785 7636865 d9d9785 754345f d9d9785 71e1892 7636865 d9d9785 79b2fcc 754345f 79b2fcc d9d9785 79b2fcc 67b16c6 79b2fcc 67b16c6 79b2fcc 0a9e96d 79b2fcc 849c88c 79b2fcc 849c88c f30ed48 849c88c 5de6e8f 79b2fcc 849c88c f30ed48 849c88c 79b2fcc 849c88c 79b2fcc f30ed48 79b2fcc 849c88c f30ed48 79b2fcc 849c88c 79b2fcc d9d9785 754345f 79b2fcc d9d9785 754345f 79b2fcc 67b16c6 79b2fcc 540437a d7637ba 5db99fa 540437a 79b2fcc 7872152 7a76ad1 79b2fcc 540437a d7637ba 092f909 540437a 79b2fcc 2715896 71e1892 2715896 79b2fcc 092f909 67b16c6 962191f 540437a d7637ba 962191f 7a76ad1 962191f 540437a 092f909 540437a 962191f 2715896 71e1892 2715896 962191f 092f909 962191f 67b16c6 79b2fcc d9d9785 67b16c6 bf564b6 540437a bf564b6 540437a d7637ba bf564b6 d9d9785 bf564b6 d9d9785 bf564b6 6155b26 d9d9785 6155b26 d9d9785 6155b26 2b4c539 77324b8 540437a 5db99fa 540437a 5db99fa 540437a 5db99fa 540437a ff8c636 754345f bed4e38 7a76ad1 ff8c636 bed4e38 ff8c636 67b16c6 79b2fcc d9d9785 67b16c6 8615c28 7636865 8615c28 67b16c6 79b2fcc 7636865 67b16c6 79b2fcc 82bad21 79b2fcc 82bad21 d7637ba 82bad21 67b16c6 82bad21 67b16c6 79b2fcc 754345f 67b16c6 79b2fcc ff8c636 67b16c6 564aab6 d9d9785 564aab6 d7637ba 540437a d7637ba 540437a 564aab6 ff8c636 564aab6 754345f 564aab6 ff8c636 564aab6 d7f2a7c ff8c636 d9d9785 ff8c636 754345f ff8c636 d7f2a7c 754345f d7f2a7c d9d9785 754345f d9d9785 d7f2a7c 564aab6 d9d9785 564aab6 d7f2a7c 564aab6 d9d9785 d7f2a7c 564aab6 d7f2a7c d9d9785 d7f2a7c d9d9785 d7f2a7c d9d9785 754345f d7f2a7c d9d9785 d7f2a7c 67b16c6 79b2fcc 67b16c6 d9d9785 67b16c6 7d17616 d9d9785 7d17616 754345f 7d17616 67b16c6 79b2fcc 67b16c6 d9d9785 67b16c6 0611031 82bad21 0611031 82bad21 d9d9785 82bad21 0611031 754345f 0611031 67b16c6 79b2fcc 67b16c6 d9d9785 67b16c6 79b2fcc 67b16c6 d9d9785 67b16c6 754345f 2a2e170 d9d9785 2a2e170 754345f 2a2e170 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 | """Agent API routes — REST + SSE endpoints.
All routes (except /health) require authentication via the get_current_user
dependency. In dev mode (no OAUTH_CLIENT_ID), auth is bypassed automatically.
"""
import asyncio
import json
import logging
from typing import Any
from dependencies import (
INTERNAL_HF_TOKEN_KEY,
get_current_user,
)
from fastapi import (
APIRouter,
Depends,
HTTPException,
Request,
)
from fastapi.exceptions import RequestValidationError
from fastapi.responses import StreamingResponse
from huggingface_hub.errors import HfHubHTTPError
from litellm import Message, acompletion
from pydantic import ValidationError
from starlette.datastructures import FormData, UploadFile
from dataset_uploads import (
MAX_DATASET_UPLOAD_BYTES,
dataset_context_note,
push_dataset_upload_to_hub,
)
from models import (
ApprovalRequest,
DatasetUploadResponse,
HealthResponse,
LLMHealthResponse,
SessionInfo,
SessionNotificationsRequest,
SessionResponse,
SessionYoloRequest,
SubmitRequest,
TruncateRequest,
)
from session_manager import (
MAX_SESSIONS,
AgentSession,
SessionCapacityError,
session_manager,
)
import user_quotas
from agent.core.hf_access import get_jobs_access
from agent.core.hf_tokens import resolve_hf_request_token, resolve_hf_router_token
from agent.core.llm_params import _resolve_llm_params
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api", tags=["agent"])
_background_teardown_tasks: set[asyncio.Task] = set()
DEFAULT_CLAUDE_MODEL_ID = "bedrock/us.anthropic.claude-opus-4-6-v1"
DEFAULT_FREE_MODEL_ID = "moonshotai/Kimi-K2.6"
PREMIUM_MODEL_IDS = {
DEFAULT_CLAUDE_MODEL_ID,
"openai/gpt-5.5",
}
DATASET_UPLOAD_MULTIPART_SLACK_BYTES = 1024 * 1024
def _claude_picker_model_id() -> str:
"""Return the model ID used by the Claude option in the UI.
The frontend config sets ``session_manager.config.model_name`` from
``ML_INTERN_CLAUDE_MODEL_ID`` when that env var is present, otherwise it
falls back to the production Bedrock Claude model. This function only
exposes that resolved config value for the Claude picker; non-Claude models
are listed separately in the model switcher.
"""
return session_manager.config.model_name
def _available_models() -> list[dict[str, Any]]:
models = [
{
"id": "moonshotai/Kimi-K2.6",
"label": "Kimi K2.6",
"provider": "huggingface",
"tier": "free",
"recommended": True,
},
{
"id": _claude_picker_model_id(),
"label": "Claude Opus 4.6",
"provider": "anthropic",
"tier": "pro",
"recommended": True,
},
{
"id": "openai/gpt-5.5",
"label": "GPT-5.5",
"provider": "openai",
"tier": "pro",
},
{
"id": "MiniMaxAI/MiniMax-M2.7",
"label": "MiniMax M2.7",
"provider": "huggingface",
"tier": "free",
},
{
"id": "zai-org/GLM-5.1",
"label": "GLM 5.1",
"provider": "huggingface",
"tier": "free",
},
{
"id": "deepseek-ai/DeepSeek-V4-Pro:deepinfra",
"label": "DeepSeek V4 Pro",
"provider": "huggingface",
"tier": "free",
},
]
return models
AVAILABLE_MODELS = _available_models()
def _is_premium_model(model_id: str) -> bool:
return model_id in PREMIUM_MODEL_IDS
async def _model_override_for_new_session(
request: Request,
requested_model: str | None,
) -> str | None:
"""Return the model override to use when creating a new session.
Explicit premium model requests are allowed and charged at message-submit
time. Implicit default sessions are more forgiving: when the configured
default is premium, start them on the first free model instead of spending
premium quota accidentally.
"""
resolved_model = requested_model or session_manager.config.model_name
if not _is_premium_model(resolved_model):
return requested_model
if requested_model:
return requested_model
logger.info(
"Default premium model %s would spend quota; "
"creating session with free fallback %s",
resolved_model,
DEFAULT_FREE_MODEL_ID,
)
return DEFAULT_FREE_MODEL_ID
async def _enforce_premium_model_quota(
user: dict[str, Any],
agent_session: AgentSession,
) -> None:
"""Charge the user's daily premium-model quota on first use in a session.
Runs at *message-submit* time, not session-create time — so spinning up a
premium-model session to look around doesn't burn quota. The
``claude_counted`` flag on ``AgentSession`` guards against re-counting the
same session; the stored field name is kept for persistence compatibility.
No-ops when the session's current model isn't premium, or when this
session has already been charged. Raises 429 when the user has hit
their daily cap.
"""
if agent_session.claude_counted:
return
model_name = agent_session.session.config.model_name
if not _is_premium_model(model_name):
return
user_id = user["user_id"]
plan = user.get("plan", "free")
cap = user_quotas.daily_cap_for(plan)
new_count = await user_quotas.try_increment_claude(user_id, cap)
if new_count is None:
if plan == "pro":
message = (
"Daily premium model limit reached. Use a free model and try "
"premium models again tomorrow."
)
else:
message = (
"Daily premium model limit reached. Upgrade to HF Pro for "
f"{user_quotas.CLAUDE_PRO_DAILY}/day or use a free model."
)
raise HTTPException(
status_code=429,
detail={
"error": "premium_model_daily_cap",
"plan": plan,
"cap": cap,
"message": message,
},
)
agent_session.claude_counted = True
await session_manager.persist_session_snapshot(agent_session)
def _user_hf_token(user: dict[str, Any] | None) -> str | None:
if not isinstance(user, dict):
return None
return user.get(INTERNAL_HF_TOKEN_KEY)
def _reject_oversize_dataset_upload(request: Request) -> None:
raw_content_length = request.headers.get("content-length")
if raw_content_length is None:
return
try:
content_length = int(raw_content_length)
except (TypeError, ValueError):
return
if content_length > MAX_DATASET_UPLOAD_BYTES + DATASET_UPLOAD_MULTIPART_SLACK_BYTES:
raise HTTPException(
status_code=413,
detail="Dataset upload exceeds the 100 MB limit.",
)
def _dataset_upload_file_from_form(form: FormData) -> UploadFile:
uploaded_files = [
(key, value)
for key, value in form.multi_items()
if isinstance(value, UploadFile)
]
if len(uploaded_files) != 1:
raise HTTPException(
status_code=400,
detail="Upload exactly one dataset file.",
)
field_name, upload = uploaded_files[0]
if field_name != "file":
raise HTTPException(
status_code=400,
detail="Missing 'file' upload field.",
)
return upload
def _dataset_upload_hub_http_exception(error: HfHubHTTPError) -> HTTPException:
status_code = getattr(error.response, "status_code", None)
if status_code == 401:
detail = "Hugging Face rejected the token used for the dataset upload."
return HTTPException(status_code=401, detail=detail)
if status_code == 403:
detail = (
"Hugging Face denied permission to create or write to the dataset repo."
)
return HTTPException(status_code=403, detail=detail)
if status_code == 404:
detail = "Could not find the Hugging Face namespace or dataset repo."
return HTTPException(status_code=404, detail=detail)
if status_code == 429:
detail = "Hugging Face Hub rate limit reached while uploading the dataset."
return HTTPException(status_code=429, detail=detail)
return HTTPException(
status_code=502,
detail="Hugging Face Hub upload failed. Please try again.",
)
async def _check_session_access(
session_id: str,
user: dict[str, Any],
request: Request | None = None,
preload_sandbox: bool = True,
) -> AgentSession:
"""Verify and lazily load the user's session. Raises 403 or 404."""
hf_token = (
resolve_hf_request_token(request)
if request is not None
else _user_hf_token(user)
)
agent_session = await session_manager.ensure_session_loaded(
session_id,
user["user_id"],
hf_token=hf_token,
hf_username=user.get("username"),
preload_sandbox=preload_sandbox,
)
if not agent_session:
raise HTTPException(status_code=404, detail="Session not found")
if user["user_id"] != "dev" and agent_session.user_id not in {
user["user_id"],
"dev",
}:
raise HTTPException(status_code=403, detail="Access denied to this session")
return agent_session
@router.get("/health", response_model=HealthResponse)
async def health_check() -> HealthResponse:
"""Health check endpoint."""
return HealthResponse(
status="ok",
active_sessions=session_manager.active_session_count,
max_sessions=MAX_SESSIONS,
)
@router.get("/health/llm", response_model=LLMHealthResponse)
async def llm_health_check() -> LLMHealthResponse:
"""Check if the LLM provider is reachable and the API key is valid.
Makes a minimal 1-token completion call. Catches common errors:
- 401 → invalid API key
- 402/insufficient_quota → out of credits
- 429 → rate limited
- timeout / network → provider unreachable
"""
model = session_manager.config.model_name
try:
llm_params = _resolve_llm_params(model, reasoning_effort="high")
await acompletion(
messages=[{"role": "user", "content": "hi"}],
max_tokens=1,
timeout=10,
**llm_params,
)
return LLMHealthResponse(status="ok", model=model)
except Exception as e:
err_str = str(e).lower()
error_type = "unknown"
if (
"401" in err_str
or "auth" in err_str
or "invalid" in err_str
or "api key" in err_str
):
error_type = "auth"
elif (
"402" in err_str
or "credit" in err_str
or "quota" in err_str
or "insufficient" in err_str
or "billing" in err_str
):
error_type = "credits"
elif "429" in err_str or "rate" in err_str:
error_type = "rate_limit"
elif "timeout" in err_str or "connect" in err_str or "network" in err_str:
error_type = "network"
logger.warning(f"LLM health check failed ({error_type}): {e}")
return LLMHealthResponse(
status="error",
model=model,
error=str(e)[:500],
error_type=error_type,
)
@router.get("/config/model")
async def get_model() -> dict:
"""Get current model and available models. No auth required."""
return {
"current": session_manager.config.model_name,
"available": AVAILABLE_MODELS,
}
_TITLE_STRIP_CHARS = str.maketrans("", "", "`*_~#[]()")
@router.post("/title")
async def generate_title(
request: SubmitRequest, user: dict = Depends(get_current_user)
) -> dict:
"""Generate a short title for a chat session based on the first user message.
Always uses gpt-oss-120b via Cerebras on the HF router. The tab headline
renders as plain text, so the model is told to avoid markdown and any
stray formatting characters are stripped before returning. gpt-oss is a
reasoning model — reasoning_effort=low keeps the reasoning budget small
so the 60-token output budget isn't consumed before the title is written.
"""
api_key = resolve_hf_router_token(_user_hf_token(user))
try:
response = await acompletion(
# Double openai/ prefix: LiteLLM strips the first as its provider
# prefix, leaving the HF model id on the wire for the router.
model="openai/openai/gpt-oss-120b:cerebras",
api_base="https://router.huggingface.co/v1",
api_key=api_key,
messages=[
{
"role": "system",
"content": (
"Generate a very short title (max 6 words) for a chat conversation "
"that starts with the following user message. "
"Reply with ONLY the title in plain text. "
"Do NOT use markdown, backticks, asterisks, quotes, brackets, or any "
"formatting characters. No punctuation at the end."
),
},
{"role": "user", "content": request.text[:500]},
],
max_tokens=60,
temperature=0.3,
timeout=10,
reasoning_effort="low",
)
title = response.choices[0].message.content.strip().strip('"').strip("'")
title = title.translate(_TITLE_STRIP_CHARS).strip()
if len(title) > 50:
title = title[:50].rstrip() + "…"
try:
await _check_session_access(request.session_id, user)
await session_manager.update_session_title(request.session_id, title)
except Exception:
logger.debug(
"Skipping title persistence for missing session %s", request.session_id
)
return {"title": title}
except Exception as e:
logger.warning(f"Title generation failed: {e}")
fallback = request.text.strip()
title = fallback[:40].rstrip() + "…" if len(fallback) > 40 else fallback
try:
await _check_session_access(request.session_id, user)
await session_manager.update_session_title(request.session_id, title)
except Exception:
logger.debug(
"Skipping fallback title persistence for missing session %s",
request.session_id,
)
return {"title": title}
@router.post("/session", response_model=SessionResponse)
async def create_session(
request: Request, user: dict = Depends(get_current_user)
) -> SessionResponse:
"""Create a new agent session bound to the authenticated user.
The user's HF access token is extracted from the Authorization header
and stored in the session so that tools (e.g. hf_jobs) can act on
behalf of the user.
Optional body ``{"model"?: <id>}`` selects the session's LLM; unknown
ids are rejected (400). The premium-model quota runs at message-submit
time, not here — spinning up a session to look around is free.
Returns 503 if the server or user has reached the session limit.
"""
# Extract the user's HF token (Bearer header, HttpOnly cookie, or env var)
hf_token = resolve_hf_request_token(request)
# Optional model override. Empty body falls back to the config default.
model: str | None = None
try:
body = await request.json()
except Exception:
body = None
if isinstance(body, dict):
model = body.get("model")
valid_ids = {m["id"] for m in AVAILABLE_MODELS}
if model and model not in valid_ids:
raise HTTPException(status_code=400, detail=f"Unknown model: {model}")
# Explicit premium selections are allowed. If the implicit configured
# default is premium, start the session on a free model instead.
model = await _model_override_for_new_session(request, model)
try:
session_id = await session_manager.create_session(
user_id=user["user_id"],
hf_username=user.get("username"),
hf_token=hf_token,
model=model,
is_pro=user.get("plan") == "pro",
)
except SessionCapacityError as e:
raise HTTPException(status_code=503, detail=str(e))
return SessionResponse(
session_id=session_id,
ready=True,
model=model or session_manager.config.model_name,
)
@router.post("/session/restore-summary", response_model=SessionResponse)
async def restore_session_summary(
request: Request, body: dict, user: dict = Depends(get_current_user)
) -> SessionResponse:
"""Create a new session seeded with a summary of the caller's prior
conversation. The client sends its cached messages; we run the standard
summarization prompt on them and drop the result into the new
session's context as a user-role system note.
Optional ``"model"`` in the body overrides the session's LLM. The
premium-model quota runs at message-submit time, not here.
"""
messages = body.get("messages")
if not isinstance(messages, list) or not messages:
raise HTTPException(status_code=400, detail="Missing 'messages' array")
hf_token = resolve_hf_request_token(request)
model = body.get("model")
valid_ids = {m["id"] for m in AVAILABLE_MODELS}
if model and model not in valid_ids:
raise HTTPException(status_code=400, detail=f"Unknown model: {model}")
model = await _model_override_for_new_session(request, model)
try:
session_id = await session_manager.create_session(
user_id=user["user_id"],
hf_username=user.get("username"),
hf_token=hf_token,
model=model,
is_pro=user.get("plan") == "pro",
)
except SessionCapacityError as e:
raise HTTPException(status_code=503, detail=str(e))
try:
summarized = await session_manager.seed_from_summary(session_id, messages)
except ValueError as e:
raise HTTPException(status_code=500, detail=str(e))
except Exception as e:
logger.exception("seed_from_summary failed")
raise HTTPException(status_code=500, detail=f"Summary failed: {e}")
logger.info(
f"Seeded session {session_id} for {user.get('username', 'unknown')} "
f"(summary of {summarized} messages)"
)
return SessionResponse(
session_id=session_id,
ready=True,
model=model or session_manager.config.model_name,
)
@router.get("/session/{session_id}", response_model=SessionInfo)
async def get_session(
session_id: str, user: dict = Depends(get_current_user)
) -> SessionInfo:
"""Get session information. Only accessible by the session owner."""
await _check_session_access(session_id, user)
info = session_manager.get_session_info(session_id)
return SessionInfo(**info)
@router.post("/session/{session_id}/model")
async def set_session_model(
session_id: str,
body: dict,
request: Request,
user: dict = Depends(get_current_user),
) -> dict:
"""Switch the active model for a single session (tab-scoped).
Takes effect on the next LLM call in that session — other sessions
(including other browser tabs) are unaffected. Model switches don't
charge quota — the premium-model quota only fires at message-submit time.
"""
agent_session = await _check_session_access(session_id, user, request)
model_id = body.get("model")
if not model_id:
raise HTTPException(status_code=400, detail="Missing 'model' field")
valid_ids = {m["id"] for m in AVAILABLE_MODELS}
if model_id not in valid_ids:
raise HTTPException(status_code=400, detail=f"Unknown model: {model_id}")
if not agent_session:
raise HTTPException(status_code=404, detail="Session not found")
await session_manager.update_session_model(session_id, model_id)
logger.info(
f"Session {session_id} model → {model_id} "
f"(by {user.get('username', 'unknown')})"
)
return {"session_id": session_id, "model": model_id}
@router.post("/session/{session_id}/notifications")
async def set_session_notifications(
session_id: str,
body: SessionNotificationsRequest,
user: dict = Depends(get_current_user),
) -> dict:
"""Replace the session's auto-notification destinations."""
agent_session = await _check_session_access(session_id, user)
try:
destinations = session_manager.set_notification_destinations(
session_id, body.destinations
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
await session_manager.persist_session_snapshot(agent_session)
return {
"session_id": session_id,
"notification_destinations": destinations,
}
@router.post("/session/{session_id}/datasets", response_model=DatasetUploadResponse)
async def upload_session_dataset(
session_id: str,
request: Request,
user: dict = Depends(get_current_user),
) -> DatasetUploadResponse:
"""Upload a CSV/JSON dataset file to a private Hub dataset for this session."""
file: UploadFile | None = None
try:
_reject_oversize_dataset_upload(request)
agent_session = await _check_session_access(session_id, user, request)
if not agent_session or not agent_session.is_active:
raise HTTPException(status_code=404, detail="Session not found")
if agent_session.is_processing:
raise HTTPException(
status_code=409,
detail="Cannot upload a dataset while the agent is processing.",
)
if agent_session.session.pending_approval:
raise HTTPException(
status_code=409,
detail="Approve or reject pending tools before uploading a dataset.",
)
hf_token = (
resolve_hf_request_token(request, include_env_fallback=False)
or _user_hf_token(user)
or resolve_hf_request_token(request)
)
if not hf_token:
raise HTTPException(
status_code=401,
detail="A Hugging Face token is required to upload datasets.",
)
form = await request.form(
max_files=1,
max_fields=1,
max_part_size=MAX_DATASET_UPLOAD_BYTES,
)
file = _dataset_upload_file_from_form(form)
hf_username = user.get("username") or agent_session.hf_username
uploaded = await push_dataset_upload_to_hub(
upload=file,
session_id=session_id,
hf_username=hf_username,
hf_token=hf_token,
)
agent_session.session.context_manager.add_message(
Message(role="user", content=dataset_context_note(uploaded))
)
await session_manager.persist_session_snapshot(agent_session)
logger.info(
"Uploaded dataset file %s to %s for session %s",
uploaded.filename,
uploaded.repo_id,
session_id,
)
return DatasetUploadResponse(**uploaded.response_payload())
except HTTPException:
raise
except HfHubHTTPError as e:
logger.warning(
"Hub rejected dataset upload for session %s: status=%s request_id=%s",
session_id,
getattr(e.response, "status_code", None),
getattr(e, "request_id", None),
)
raise _dataset_upload_hub_http_exception(e)
except Exception:
logger.exception("Dataset upload failed for session %s", session_id)
raise HTTPException(
status_code=502,
detail="Dataset upload failed. Please try again.",
)
finally:
if file is not None:
await file.close()
@router.patch("/session/{session_id}/yolo")
async def set_session_yolo(
session_id: str,
body: SessionYoloRequest,
user: dict = Depends(get_current_user),
) -> dict:
"""Update the session-scoped auto-approval policy."""
await _check_session_access(session_id, user)
try:
summary = await session_manager.update_session_auto_approval(
session_id,
enabled=body.enabled,
cost_cap_usd=body.cost_cap_usd,
cap_provided="cost_cap_usd" in body.model_fields_set,
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
return {"session_id": session_id, **summary}
@router.get("/user/quota")
async def get_user_quota(user: dict = Depends(get_current_user)) -> dict:
"""Return the user's plan tier and today's premium-model quota state."""
plan = user.get("plan", "free")
used = await user_quotas.get_claude_used_today(user["user_id"])
cap = user_quotas.daily_cap_for(plan)
remaining = max(0, cap - used)
return {
"plan": plan,
"premium_used_today": used,
"premium_daily_cap": cap,
"premium_remaining": remaining,
}
@router.get("/user/jobs-access")
async def get_jobs_access_info(
request: Request, user: dict = Depends(get_current_user)
) -> dict:
"""Return the namespaces the current token can run HF Jobs under.
Credits are enforced by the HF API at job-creation time, not here —
the response only describes which wallets the caller is allowed to
pick from. Pro is irrelevant.
"""
token = resolve_hf_request_token(request)
access = await get_jobs_access(token or "")
return {
"eligible_namespaces": access.eligible_namespaces if access else [],
"default_namespace": access.default_namespace if access else None,
"billing_url": "https://huggingface.co/settings/billing",
}
@router.get("/sessions", response_model=list[SessionInfo])
async def list_sessions(user: dict = Depends(get_current_user)) -> list[SessionInfo]:
"""List sessions belonging to the authenticated user."""
sessions = await session_manager.list_sessions(user_id=user["user_id"])
return [SessionInfo(**s) for s in sessions]
@router.post("/session/{session_id}/sandbox/teardown")
async def teardown_session_sandbox(
session_id: str, user: dict = Depends(get_current_user)
) -> dict:
"""Best-effort sandbox teardown that preserves durable chat history."""
await _check_session_access(session_id, user, preload_sandbox=False)
task = asyncio.create_task(session_manager.teardown_sandbox(session_id))
_background_teardown_tasks.add(task)
task.add_done_callback(_background_teardown_tasks.discard)
return {"status": "teardown_requested", "session_id": session_id}
@router.delete("/session/{session_id}")
async def delete_session(
session_id: str, user: dict = Depends(get_current_user)
) -> dict:
"""Delete a session. Only accessible by the session owner."""
await _check_session_access(session_id, user, preload_sandbox=False)
success = await session_manager.delete_session(session_id)
if not success:
raise HTTPException(status_code=404, detail="Session not found")
return {"status": "deleted", "session_id": session_id}
@router.post("/submit")
async def submit_input(
request: Request, user: dict = Depends(get_current_user)
) -> dict:
"""Submit user input to a session. Only accessible by the session owner."""
# Parse the body manually so session ownership can be checked before the
# text-length constraints fire — otherwise a non-owner sending an empty
# or oversized text gets a 422 leaking the constraint instead of the 404
# they'd get for any other access to a session they don't own.
try:
payload = await request.json()
except (json.JSONDecodeError, TypeError) as exc:
raise HTTPException(status_code=422, detail=str(exc))
if not isinstance(payload, dict):
raise HTTPException(status_code=422, detail="Body must be a JSON object")
raw_session_id = payload.get("session_id")
if not isinstance(raw_session_id, str) or not raw_session_id:
raise RequestValidationError(
[
{
"type": "missing",
"loc": ("body", "session_id"),
"msg": "Field required",
"input": payload,
}
]
)
agent_session = await _check_session_access(raw_session_id, user)
try:
body = SubmitRequest(**payload)
except ValidationError as exc:
raise RequestValidationError(exc.errors()) from exc
await _enforce_premium_model_quota(user, agent_session)
success = await session_manager.submit_user_input(body.session_id, body.text)
if not success:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return {"status": "submitted", "session_id": body.session_id}
@router.post("/approve")
async def submit_approval(
request: ApprovalRequest, user: dict = Depends(get_current_user)
) -> dict:
"""Submit tool approvals to a session. Only accessible by the session owner."""
await _check_session_access(request.session_id, user)
approvals = [
{
"tool_call_id": a.tool_call_id,
"approved": a.approved,
"feedback": a.feedback,
"edited_script": a.edited_script,
"namespace": a.namespace,
}
for a in request.approvals
]
success = await session_manager.submit_approval(request.session_id, approvals)
if not success:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return {"status": "submitted", "session_id": request.session_id}
@router.post("/chat/{session_id}")
async def chat_sse(
session_id: str,
request: Request,
user: dict = Depends(get_current_user),
) -> StreamingResponse:
"""SSE endpoint: submit input or approval, then stream events until turn ends."""
agent_session = await _check_session_access(session_id, user, request)
if not agent_session or not agent_session.is_active:
raise HTTPException(status_code=404, detail="Session not found or inactive")
# Parse body
body = await request.json()
# Subscribe BEFORE submitting so we never miss events — even if the
# agent loop processes the submission before this coroutine continues.
broadcaster = agent_session.broadcaster
sub_id, event_queue = broadcaster.subscribe()
# Submit the operation
text = body.get("text")
approvals = body.get("approvals")
# Gate user-message sends against the daily premium-model quota. Approvals are
# continuations of an in-progress turn — the session was already charged
# on its first message, so we skip the gate there.
if text is not None and not approvals:
try:
await _enforce_premium_model_quota(user, agent_session)
except HTTPException:
broadcaster.unsubscribe(sub_id)
raise
try:
if approvals:
formatted = [
{
"tool_call_id": a["tool_call_id"],
"approved": a["approved"],
"feedback": a.get("feedback"),
"edited_script": a.get("edited_script"),
"namespace": a.get("namespace"),
}
for a in approvals
]
success = await session_manager.submit_approval(session_id, formatted)
elif text is not None:
success = await session_manager.submit_user_input(session_id, text)
else:
broadcaster.unsubscribe(sub_id)
raise HTTPException(
status_code=400, detail="Must provide 'text' or 'approvals'"
)
if not success:
broadcaster.unsubscribe(sub_id)
raise HTTPException(status_code=404, detail="Session not found or inactive")
except HTTPException:
broadcaster.unsubscribe(sub_id)
raise
except Exception:
broadcaster.unsubscribe(sub_id)
raise
return _sse_response(broadcaster, event_queue, sub_id)
@router.post("/pro-click/{session_id}")
async def record_pro_click(
session_id: str,
body: dict,
user: dict = Depends(get_current_user),
) -> dict:
"""Record a click on a Pro upgrade CTA shown from inside a session."""
agent_session = await _check_session_access(session_id, user)
from agent.core import telemetry
await telemetry.record_pro_cta_click(
agent_session.session,
source=str(body.get("source") or "unknown"),
target=str(body.get("target") or "pro_pricing"),
)
if agent_session.session.config.save_sessions:
agent_session.session.save_and_upload_detached(
agent_session.session.config.session_dataset_repo
)
return {"status": "ok"}
# ---------------------------------------------------------------------------
# Shared SSE helpers
# ---------------------------------------------------------------------------
_TERMINAL_EVENTS = {
"turn_complete",
"approval_required",
"error",
"interrupted",
"shutdown",
}
_SSE_KEEPALIVE_SECONDS = 15
def _last_event_seq(request: Request) -> int:
raw = (
request.headers.get("last-event-id") or request.query_params.get("after") or "0"
)
try:
return max(0, int(raw))
except (TypeError, ValueError):
return 0
def _format_sse(msg: dict[str, Any]) -> str:
seq = msg.get("seq")
body = {"event_type": msg.get("event_type"), "data": msg.get("data") or {}}
if seq is not None:
body["seq"] = seq
return f"id: {seq}\ndata: {json.dumps(body)}\n\n"
return f"data: {json.dumps(body)}\n\n"
def _event_doc_to_msg(doc: dict[str, Any]) -> dict[str, Any]:
return {
"event_type": doc.get("event_type"),
"data": doc.get("data") or {},
"seq": doc.get("seq"),
}
def _sse_response(
broadcaster,
event_queue,
sub_id,
*,
replay_events: list[dict[str, Any]] | None = None,
after_seq: int = 0,
) -> StreamingResponse:
"""Build a StreamingResponse that drains *event_queue* as SSE,
sending keepalive comments every 15 s to prevent proxy timeouts."""
async def event_generator():
try:
for doc in replay_events or []:
msg = _event_doc_to_msg(doc)
seq = msg.get("seq")
if isinstance(seq, int) and seq <= after_seq:
continue
yield _format_sse(msg)
if msg.get("event_type", "") in _TERMINAL_EVENTS:
return
while True:
try:
msg = await asyncio.wait_for(
event_queue.get(), timeout=_SSE_KEEPALIVE_SECONDS
)
except asyncio.TimeoutError:
# SSE comment — ignored by parsers, keeps connection alive
yield ": keepalive\n\n"
continue
event_type = msg.get("event_type", "")
yield _format_sse(msg)
if event_type in _TERMINAL_EVENTS:
break
finally:
broadcaster.unsubscribe(sub_id)
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
},
)
@router.get("/events/{session_id}")
async def subscribe_events(
session_id: str,
request: Request,
user: dict = Depends(get_current_user),
) -> StreamingResponse:
"""Subscribe to events for a running session without submitting new input.
Used by the frontend to re-attach after a connection drop (e.g. screen
sleep). Returns 404 if the session isn't active or isn't processing.
"""
agent_session = await _check_session_access(session_id, user, request)
if not agent_session or not agent_session.is_active:
raise HTTPException(status_code=404, detail="Session not found or inactive")
after_seq = _last_event_seq(request)
replay_events = await session_manager._store().load_events_after(
session_id, after_seq
)
broadcaster = agent_session.broadcaster
sub_id, event_queue = broadcaster.subscribe()
return _sse_response(
broadcaster,
event_queue,
sub_id,
replay_events=replay_events,
after_seq=after_seq,
)
@router.post("/interrupt/{session_id}")
async def interrupt_session(
session_id: str, user: dict = Depends(get_current_user)
) -> dict:
"""Interrupt the current operation in a session."""
await _check_session_access(session_id, user)
success = await session_manager.interrupt(session_id)
if not success:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return {"status": "interrupted", "session_id": session_id}
@router.get("/session/{session_id}/messages")
async def get_session_messages(
session_id: str, user: dict = Depends(get_current_user)
) -> list[dict]:
"""Return the session's message history from memory."""
agent_session = await _check_session_access(session_id, user)
if not agent_session or not agent_session.is_active:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return [
msg.model_dump(mode="json")
for msg in agent_session.session.context_manager.items
]
@router.post("/undo/{session_id}")
async def undo_session(session_id: str, user: dict = Depends(get_current_user)) -> dict:
"""Undo the last turn in a session."""
await _check_session_access(session_id, user)
success = await session_manager.undo(session_id)
if not success:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return {"status": "undo_requested", "session_id": session_id}
@router.post("/truncate/{session_id}")
async def truncate_session(
session_id: str,
request: Request,
user: dict = Depends(get_current_user),
) -> dict:
"""Truncate conversation to before a specific user message."""
# Check session ownership before parsing the request body so a 404 on a
# non-existent / non-owned session_id beats the 422 schema-validation error
# (otherwise the response leaks the required field name to non-owners).
await _check_session_access(session_id, user)
try:
body = TruncateRequest(**(await request.json()))
except ValidationError as exc:
# Re-raise as RequestValidationError so FastAPI returns its standard
# structured 422 schema (`{"detail": [{"type":..., "loc":..., ...}]}`)
# instead of a string-stringified Pydantic dump.
raise RequestValidationError(exc.errors()) from exc
except (json.JSONDecodeError, TypeError) as exc:
raise HTTPException(status_code=422, detail=str(exc))
success = await session_manager.truncate(session_id, body.user_message_index)
if not success:
raise HTTPException(
status_code=404,
detail="Session not found, inactive, or message index out of range",
)
return {"status": "truncated", "session_id": session_id}
@router.post("/compact/{session_id}")
async def compact_session(
session_id: str, user: dict = Depends(get_current_user)
) -> dict:
"""Compact the context in a session."""
await _check_session_access(session_id, user)
success = await session_manager.compact(session_id)
if not success:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return {"status": "compact_requested", "session_id": session_id}
@router.post("/shutdown/{session_id}")
async def shutdown_session(
session_id: str, user: dict = Depends(get_current_user)
) -> dict:
"""Shutdown a session."""
await _check_session_access(session_id, user)
success = await session_manager.shutdown_session(session_id)
if not success:
raise HTTPException(status_code=404, detail="Session not found or inactive")
return {"status": "shutdown_requested", "session_id": session_id}
@router.post("/feedback/{session_id}")
async def submit_feedback(
session_id: str,
body: dict,
user: dict = Depends(get_current_user),
) -> dict:
"""Attach a user feedback signal to a session's event log.
Body: {rating: "up"|"down"|"outcome_success"|"outcome_fail",
turn_index?: int, comment?: str, message_id?: str}
Appended as a `feedback` event and saved with the session trajectory.
"""
agent_session = await _check_session_access(session_id, user)
rating = body.get("rating")
if rating not in {"up", "down", "outcome_success", "outcome_fail"}:
raise HTTPException(status_code=400, detail="invalid rating")
from agent.core import telemetry
await telemetry.record_feedback(
agent_session.session,
rating=rating,
turn_index=body.get("turn_index"),
message_id=body.get("message_id"),
comment=body.get("comment"),
)
# Fire-and-forget save so feedback reaches the dataset even if the user
# closes the tab right after clicking.
if agent_session.session.config.save_sessions:
agent_session.session.save_and_upload_detached(
agent_session.session.config.session_dataset_repo
)
return {"status": "ok"}
|