Compare commits

..

1 Commits

Author SHA1 Message Date
Zamil Majdy
1750c833ee fix(frontend): upgrade Docker Node.js from v21 (EOL) to v22 LTS (#12561)
## Summary
Upgrade the frontend **Docker image** from **Node.js v21** (EOL since
June 2024) to **Node.js v22 LTS** (supported through April 2027).

> **Scope:** This only affects the **Dockerfile** used for local
development (`docker compose`) and CI. It does **not** affect Vercel
(which manages its own Node.js runtime) or Kubernetes (the frontend Helm
chart was removed in Dec 2025 — the frontend is deployed exclusively via
Vercel).

## Why
- Node v21.7.3 has a **known TransformStream race condition bug**
causing `TypeError: controller[kState].transformAlgorithm is not a
function` — this is
[BUILDER-3KF](https://significant-gravitas.sentry.io/issues/BUILDER-3KF)
with **567,000+ Sentry events**
- The error is entirely in Node.js internals
(`node:internal/webstreams/transformstream`), zero first-party code
- Node 21 is **not an LTS release** and has been EOL since June 2024
- `package.json` already declares `"engines": { "node": "22.x" }` — the
Dockerfile was inconsistent
- Node 22.x LTS (v22.22.1) fixes the TransformStream bug
- Next.js 15.4.x requires Node 18.18+, so Node 22 is fully compatible

## Changes
- `autogpt_platform/frontend/Dockerfile`: `node:21-alpine` →
`node:22.22-alpine3.23` (both `base` and `prod` stages)

## Test plan
- [ ] Verify frontend Docker image builds successfully via `docker
compose`
- [ ] Verify frontend starts and serves pages correctly in local Docker
environment
- [ ] Monitor Sentry for BUILDER-3KF — should drop to zero for
Docker-based runs
2026-03-27 13:11:23 +07:00
83 changed files with 412 additions and 4846 deletions

View File

@@ -1,146 +0,0 @@
"""Admin endpoints for checking and resetting user CoPilot rate limit usage."""
import logging
from typing import Optional
from autogpt_libs.auth import get_user_id, requires_admin_user
from fastapi import APIRouter, Body, HTTPException, Security
from pydantic import BaseModel
from backend.copilot.config import ChatConfig
from backend.copilot.rate_limit import (
get_global_rate_limits,
get_usage_status,
reset_user_usage,
)
from backend.data.user import get_user_by_email, get_user_email_by_id
logger = logging.getLogger(__name__)
config = ChatConfig()
router = APIRouter(
prefix="/admin",
tags=["copilot", "admin"],
dependencies=[Security(requires_admin_user)],
)
class UserRateLimitResponse(BaseModel):
user_id: str
user_email: Optional[str] = None
daily_token_limit: int
weekly_token_limit: int
daily_tokens_used: int
weekly_tokens_used: int
async def _resolve_user_id(
user_id: Optional[str], email: Optional[str]
) -> tuple[str, Optional[str]]:
"""Resolve a user_id and email from the provided parameters.
Returns (user_id, email). Accepts either user_id or email; at least one
must be provided. When both are provided, ``email`` takes precedence.
"""
if email:
user = await get_user_by_email(email)
if not user:
raise HTTPException(
status_code=404, detail="No user found with the provided email."
)
return user.id, email
if not user_id:
raise HTTPException(
status_code=400,
detail="Either user_id or email query parameter is required.",
)
# We have a user_id; try to look up their email for display purposes.
# This is non-critical -- a failure should not block the response.
try:
resolved_email = await get_user_email_by_id(user_id)
except Exception:
logger.warning("Failed to resolve email for user %s", user_id, exc_info=True)
resolved_email = None
return user_id, resolved_email
@router.get(
"/rate_limit",
response_model=UserRateLimitResponse,
summary="Get User Rate Limit",
)
async def get_user_rate_limit(
user_id: Optional[str] = None,
email: Optional[str] = None,
admin_user_id: str = Security(get_user_id),
) -> UserRateLimitResponse:
"""Get a user's current usage and effective rate limits. Admin-only.
Accepts either ``user_id`` or ``email`` as a query parameter.
When ``email`` is provided the user is looked up by email first.
"""
resolved_id, resolved_email = await _resolve_user_id(user_id, email)
logger.info("Admin %s checking rate limit for user %s", admin_user_id, resolved_id)
daily_limit, weekly_limit = await get_global_rate_limits(
resolved_id, config.daily_token_limit, config.weekly_token_limit
)
usage = await get_usage_status(resolved_id, daily_limit, weekly_limit)
return UserRateLimitResponse(
user_id=resolved_id,
user_email=resolved_email,
daily_token_limit=daily_limit,
weekly_token_limit=weekly_limit,
daily_tokens_used=usage.daily.used,
weekly_tokens_used=usage.weekly.used,
)
@router.post(
"/rate_limit/reset",
response_model=UserRateLimitResponse,
summary="Reset User Rate Limit Usage",
)
async def reset_user_rate_limit(
user_id: str = Body(embed=True),
reset_weekly: bool = Body(False, embed=True),
admin_user_id: str = Security(get_user_id),
) -> UserRateLimitResponse:
"""Reset a user's daily usage counter (and optionally weekly). Admin-only."""
logger.info(
"Admin %s resetting rate limit for user %s (reset_weekly=%s)",
admin_user_id,
user_id,
reset_weekly,
)
try:
await reset_user_usage(user_id, reset_weekly=reset_weekly)
except Exception as e:
logger.exception("Failed to reset user usage")
raise HTTPException(status_code=500, detail="Failed to reset usage") from e
daily_limit, weekly_limit = await get_global_rate_limits(
user_id, config.daily_token_limit, config.weekly_token_limit
)
usage = await get_usage_status(user_id, daily_limit, weekly_limit)
try:
resolved_email = await get_user_email_by_id(user_id)
except Exception:
logger.warning("Failed to resolve email for user %s", user_id, exc_info=True)
resolved_email = None
return UserRateLimitResponse(
user_id=user_id,
user_email=resolved_email,
daily_token_limit=daily_limit,
weekly_token_limit=weekly_limit,
daily_tokens_used=usage.daily.used,
weekly_tokens_used=usage.weekly.used,
)

View File

@@ -1,263 +0,0 @@
import json
from types import SimpleNamespace
from unittest.mock import AsyncMock
import fastapi
import fastapi.testclient
import pytest
import pytest_mock
from autogpt_libs.auth.jwt_utils import get_jwt_payload
from pytest_snapshot.plugin import Snapshot
from backend.copilot.rate_limit import CoPilotUsageStatus, UsageWindow
from .rate_limit_admin_routes import router as rate_limit_admin_router
app = fastapi.FastAPI()
app.include_router(rate_limit_admin_router)
client = fastapi.testclient.TestClient(app)
_MOCK_MODULE = "backend.api.features.admin.rate_limit_admin_routes"
_TARGET_EMAIL = "target@example.com"
@pytest.fixture(autouse=True)
def setup_app_admin_auth(mock_jwt_admin):
"""Setup admin auth overrides for all tests in this module"""
app.dependency_overrides[get_jwt_payload] = mock_jwt_admin["get_jwt_payload"]
yield
app.dependency_overrides.clear()
def _mock_usage_status(
daily_used: int = 500_000, weekly_used: int = 3_000_000
) -> CoPilotUsageStatus:
from datetime import UTC, datetime, timedelta
now = datetime.now(UTC)
return CoPilotUsageStatus(
daily=UsageWindow(
used=daily_used, limit=2_500_000, resets_at=now + timedelta(hours=6)
),
weekly=UsageWindow(
used=weekly_used, limit=12_500_000, resets_at=now + timedelta(days=3)
),
)
def _patch_rate_limit_deps(
mocker: pytest_mock.MockerFixture,
target_user_id: str,
daily_used: int = 500_000,
weekly_used: int = 3_000_000,
):
"""Patch the common rate-limit + user-lookup dependencies."""
mocker.patch(
f"{_MOCK_MODULE}.get_global_rate_limits",
new_callable=AsyncMock,
return_value=(2_500_000, 12_500_000),
)
mocker.patch(
f"{_MOCK_MODULE}.get_usage_status",
new_callable=AsyncMock,
return_value=_mock_usage_status(daily_used=daily_used, weekly_used=weekly_used),
)
mocker.patch(
f"{_MOCK_MODULE}.get_user_email_by_id",
new_callable=AsyncMock,
return_value=_TARGET_EMAIL,
)
def test_get_rate_limit(
mocker: pytest_mock.MockerFixture,
configured_snapshot: Snapshot,
target_user_id: str,
) -> None:
"""Test getting rate limit and usage for a user."""
_patch_rate_limit_deps(mocker, target_user_id)
response = client.get("/admin/rate_limit", params={"user_id": target_user_id})
assert response.status_code == 200
data = response.json()
assert data["user_id"] == target_user_id
assert data["user_email"] == _TARGET_EMAIL
assert data["daily_token_limit"] == 2_500_000
assert data["weekly_token_limit"] == 12_500_000
assert data["daily_tokens_used"] == 500_000
assert data["weekly_tokens_used"] == 3_000_000
configured_snapshot.assert_match(
json.dumps(data, indent=2, sort_keys=True) + "\n",
"get_rate_limit",
)
def test_get_rate_limit_by_email(
mocker: pytest_mock.MockerFixture,
target_user_id: str,
) -> None:
"""Test looking up rate limits via email instead of user_id."""
_patch_rate_limit_deps(mocker, target_user_id)
mock_user = SimpleNamespace(id=target_user_id, email=_TARGET_EMAIL)
mocker.patch(
f"{_MOCK_MODULE}.get_user_by_email",
new_callable=AsyncMock,
return_value=mock_user,
)
response = client.get("/admin/rate_limit", params={"email": _TARGET_EMAIL})
assert response.status_code == 200
data = response.json()
assert data["user_id"] == target_user_id
assert data["user_email"] == _TARGET_EMAIL
assert data["daily_token_limit"] == 2_500_000
def test_get_rate_limit_by_email_not_found(
mocker: pytest_mock.MockerFixture,
) -> None:
"""Test that looking up a non-existent email returns 404."""
mocker.patch(
f"{_MOCK_MODULE}.get_user_by_email",
new_callable=AsyncMock,
return_value=None,
)
response = client.get("/admin/rate_limit", params={"email": "nobody@example.com"})
assert response.status_code == 404
def test_get_rate_limit_no_params() -> None:
"""Test that omitting both user_id and email returns 400."""
response = client.get("/admin/rate_limit")
assert response.status_code == 400
def test_reset_user_usage_daily_only(
mocker: pytest_mock.MockerFixture,
configured_snapshot: Snapshot,
target_user_id: str,
) -> None:
"""Test resetting only daily usage (default behaviour)."""
mock_reset = mocker.patch(
f"{_MOCK_MODULE}.reset_user_usage",
new_callable=AsyncMock,
)
_patch_rate_limit_deps(mocker, target_user_id, daily_used=0, weekly_used=3_000_000)
response = client.post(
"/admin/rate_limit/reset",
json={"user_id": target_user_id},
)
assert response.status_code == 200
data = response.json()
assert data["daily_tokens_used"] == 0
# Weekly is untouched
assert data["weekly_tokens_used"] == 3_000_000
mock_reset.assert_awaited_once_with(target_user_id, reset_weekly=False)
configured_snapshot.assert_match(
json.dumps(data, indent=2, sort_keys=True) + "\n",
"reset_user_usage_daily_only",
)
def test_reset_user_usage_daily_and_weekly(
mocker: pytest_mock.MockerFixture,
configured_snapshot: Snapshot,
target_user_id: str,
) -> None:
"""Test resetting both daily and weekly usage."""
mock_reset = mocker.patch(
f"{_MOCK_MODULE}.reset_user_usage",
new_callable=AsyncMock,
)
_patch_rate_limit_deps(mocker, target_user_id, daily_used=0, weekly_used=0)
response = client.post(
"/admin/rate_limit/reset",
json={"user_id": target_user_id, "reset_weekly": True},
)
assert response.status_code == 200
data = response.json()
assert data["daily_tokens_used"] == 0
assert data["weekly_tokens_used"] == 0
mock_reset.assert_awaited_once_with(target_user_id, reset_weekly=True)
configured_snapshot.assert_match(
json.dumps(data, indent=2, sort_keys=True) + "\n",
"reset_user_usage_daily_and_weekly",
)
def test_reset_user_usage_redis_failure(
mocker: pytest_mock.MockerFixture,
target_user_id: str,
) -> None:
"""Test that Redis failure on reset returns 500."""
mocker.patch(
f"{_MOCK_MODULE}.reset_user_usage",
new_callable=AsyncMock,
side_effect=Exception("Redis connection refused"),
)
response = client.post(
"/admin/rate_limit/reset",
json={"user_id": target_user_id},
)
assert response.status_code == 500
def test_get_rate_limit_email_lookup_failure(
mocker: pytest_mock.MockerFixture,
target_user_id: str,
) -> None:
"""Test that failing to resolve a user email degrades gracefully."""
mocker.patch(
f"{_MOCK_MODULE}.get_global_rate_limits",
new_callable=AsyncMock,
return_value=(2_500_000, 12_500_000),
)
mocker.patch(
f"{_MOCK_MODULE}.get_usage_status",
new_callable=AsyncMock,
return_value=_mock_usage_status(),
)
mocker.patch(
f"{_MOCK_MODULE}.get_user_email_by_id",
new_callable=AsyncMock,
side_effect=Exception("DB connection lost"),
)
response = client.get("/admin/rate_limit", params={"user_id": target_user_id})
assert response.status_code == 200
data = response.json()
assert data["user_id"] == target_user_id
assert data["user_email"] is None
def test_admin_endpoints_require_admin_role(mock_jwt_user) -> None:
"""Test that rate limit admin endpoints require admin role."""
app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]
response = client.get("/admin/rate_limit", params={"user_id": "test"})
assert response.status_code == 403
response = client.post(
"/admin/rate_limit/reset",
json={"user_id": "test"},
)
assert response.status_code == 403

View File

@@ -30,14 +30,8 @@ from backend.copilot.model import (
from backend.copilot.rate_limit import (
CoPilotUsageStatus,
RateLimitExceeded,
acquire_reset_lock,
check_rate_limit,
get_daily_reset_count,
get_global_rate_limits,
get_usage_status,
increment_daily_reset_count,
release_reset_lock,
reset_daily_usage,
)
from backend.copilot.response_model import StreamError, StreamFinish, StreamHeartbeat
from backend.copilot.tools.e2b_sandbox import kill_sandbox
@@ -65,16 +59,9 @@ from backend.copilot.tools.models import (
UnderstandingUpdatedResponse,
)
from backend.copilot.tracking import track_user_message
from backend.data.credit import UsageTransactionMetadata, get_user_credit_model
from backend.data.redis_client import get_redis_async
from backend.data.understanding import get_business_understanding
from backend.data.workspace import get_or_create_workspace
from backend.util.exceptions import InsufficientBalanceError, NotFoundError
from backend.util.settings import Settings
settings = Settings()
logger = logging.getLogger(__name__)
from backend.util.exceptions import NotFoundError
config = ChatConfig()
@@ -82,6 +69,8 @@ _UUID_RE = re.compile(
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I
)
logger = logging.getLogger(__name__)
async def _validate_and_get_session(
session_id: str,
@@ -432,187 +421,11 @@ async def get_copilot_usage(
"""Get CoPilot usage status for the authenticated user.
Returns current token usage vs limits for daily and weekly windows.
Global defaults sourced from LaunchDarkly (falling back to config).
"""
daily_limit, weekly_limit = await get_global_rate_limits(
user_id, config.daily_token_limit, config.weekly_token_limit
)
return await get_usage_status(
user_id=user_id,
daily_token_limit=daily_limit,
weekly_token_limit=weekly_limit,
rate_limit_reset_cost=config.rate_limit_reset_cost,
)
class RateLimitResetResponse(BaseModel):
"""Response from resetting the daily rate limit."""
success: bool
credits_charged: int = Field(description="Credits charged (in cents)")
remaining_balance: int = Field(description="Credit balance after charge (in cents)")
usage: CoPilotUsageStatus = Field(description="Updated usage status after reset")
@router.post(
"/usage/reset",
status_code=200,
responses={
400: {
"description": "Bad Request (feature disabled or daily limit not reached)"
},
402: {"description": "Payment Required (insufficient credits)"},
429: {
"description": "Too Many Requests (max daily resets exceeded or reset in progress)"
},
503: {
"description": "Service Unavailable (Redis reset failed; credits refunded or support needed)"
},
},
)
async def reset_copilot_usage(
user_id: Annotated[str, Security(auth.get_user_id)],
) -> RateLimitResetResponse:
"""Reset the daily CoPilot rate limit by spending credits.
Allows users who have hit their daily token limit to spend credits
to reset their daily usage counter and continue working.
Returns 400 if the feature is disabled or the user is not over the limit.
Returns 402 if the user has insufficient credits.
"""
cost = config.rate_limit_reset_cost
if cost <= 0:
raise HTTPException(
status_code=400,
detail="Rate limit reset is not available.",
)
if not settings.config.enable_credit:
raise HTTPException(
status_code=400,
detail="Rate limit reset is not available (credit system is disabled).",
)
daily_limit, weekly_limit = await get_global_rate_limits(
user_id, config.daily_token_limit, config.weekly_token_limit
)
if daily_limit <= 0:
raise HTTPException(
status_code=400,
detail="No daily limit is configured — nothing to reset.",
)
# Check max daily resets. get_daily_reset_count returns None when Redis
# is unavailable; reject the reset in that case to prevent unlimited
# free resets when the counter store is down.
reset_count = await get_daily_reset_count(user_id)
if reset_count is None:
raise HTTPException(
status_code=503,
detail="Unable to verify reset eligibility — please try again later.",
)
if config.max_daily_resets > 0 and reset_count >= config.max_daily_resets:
raise HTTPException(
status_code=429,
detail=f"You've used all {config.max_daily_resets} resets for today.",
)
# Acquire a per-user lock to prevent TOCTOU races (concurrent resets).
if not await acquire_reset_lock(user_id):
raise HTTPException(
status_code=429,
detail="A reset is already in progress. Please try again.",
)
try:
# Verify the user is actually at or over their daily limit.
usage_status = await get_usage_status(
user_id=user_id,
daily_token_limit=daily_limit,
weekly_token_limit=weekly_limit,
)
if daily_limit > 0 and usage_status.daily.used < daily_limit:
raise HTTPException(
status_code=400,
detail="You have not reached your daily limit yet.",
)
# If the weekly limit is also exhausted, resetting the daily counter
# won't help — the user would still be blocked by the weekly limit.
if weekly_limit > 0 and usage_status.weekly.used >= weekly_limit:
raise HTTPException(
status_code=400,
detail="Your weekly limit is also reached. Resetting the daily limit won't help.",
)
# Charge credits.
credit_model = await get_user_credit_model(user_id)
try:
remaining = await credit_model.spend_credits(
user_id=user_id,
cost=cost,
metadata=UsageTransactionMetadata(
reason="CoPilot daily rate limit reset",
),
)
except InsufficientBalanceError as e:
raise HTTPException(
status_code=402,
detail="Insufficient credits to reset your rate limit.",
) from e
# Reset daily usage in Redis. If this fails, refund the credits
# so the user is not charged for a service they did not receive.
if not await reset_daily_usage(user_id, daily_token_limit=daily_limit):
# Compensate: refund the charged credits.
refunded = False
try:
await credit_model.top_up_credits(user_id, cost)
refunded = True
logger.warning(
"Refunded %d credits to user %s after Redis reset failure",
cost,
user_id[:8],
)
except Exception:
logger.error(
"CRITICAL: Failed to refund %d credits to user %s "
"after Redis reset failure — manual intervention required",
cost,
user_id[:8],
exc_info=True,
)
if refunded:
raise HTTPException(
status_code=503,
detail="Rate limit reset failed — please try again later. "
"Your credits have not been charged.",
)
raise HTTPException(
status_code=503,
detail="Rate limit reset failed and the automatic refund "
"also failed. Please contact support for assistance.",
)
# Track the reset count for daily cap enforcement.
await increment_daily_reset_count(user_id)
finally:
await release_reset_lock(user_id)
# Return updated usage status.
updated_usage = await get_usage_status(
user_id=user_id,
daily_token_limit=daily_limit,
weekly_token_limit=weekly_limit,
rate_limit_reset_cost=config.rate_limit_reset_cost,
)
return RateLimitResetResponse(
success=True,
credits_charged=cost,
remaining_balance=remaining,
usage=updated_usage,
daily_token_limit=config.daily_token_limit,
weekly_token_limit=config.weekly_token_limit,
)
@@ -713,16 +526,12 @@ async def stream_chat_post(
# Pre-turn rate limit check (token-based).
# check_rate_limit short-circuits internally when both limits are 0.
# Global defaults sourced from LaunchDarkly, falling back to config.
if user_id:
try:
daily_limit, weekly_limit = await get_global_rate_limits(
user_id, config.daily_token_limit, config.weekly_token_limit
)
await check_rate_limit(
user_id=user_id,
daily_token_limit=daily_limit,
weekly_token_limit=weekly_limit,
daily_token_limit=config.daily_token_limit,
weekly_token_limit=config.weekly_token_limit,
)
except RateLimitExceeded as e:
raise HTTPException(status_code=429, detail=str(e)) from e
@@ -1085,47 +894,6 @@ async def session_assign_user(
return {"status": "ok"}
# ========== Suggested Prompts ==========
class SuggestedTheme(BaseModel):
"""A themed group of suggested prompts."""
name: str
prompts: list[str]
class SuggestedPromptsResponse(BaseModel):
"""Response model for user-specific suggested prompts grouped by theme."""
themes: list[SuggestedTheme]
@router.get(
"/suggested-prompts",
dependencies=[Security(auth.requires_user)],
)
async def get_suggested_prompts(
user_id: Annotated[str, Security(auth.get_user_id)],
) -> SuggestedPromptsResponse:
"""
Get LLM-generated suggested prompts grouped by theme.
Returns personalized quick-action prompts based on the user's
business understanding. Returns empty themes list if no custom
prompts are available.
"""
understanding = await get_business_understanding(user_id)
if understanding is None or not understanding.suggested_prompts:
return SuggestedPromptsResponse(themes=[])
themes = [
SuggestedTheme(name=name, prompts=prompts)
for name, prompts in understanding.suggested_prompts.items()
]
return SuggestedPromptsResponse(themes=themes)
# ========== Configuration ==========

View File

@@ -1,7 +1,7 @@
"""Tests for chat API routes: session title update, file attachment validation, usage, and rate limiting."""
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock
import fastapi
import fastapi.testclient
@@ -368,7 +368,6 @@ def test_usage_returns_daily_and_weekly(
user_id=test_user_id,
daily_token_limit=10000,
weekly_token_limit=50000,
rate_limit_reset_cost=chat_routes.config.rate_limit_reset_cost,
)
@@ -381,7 +380,6 @@ def test_usage_uses_config_limits(
mocker.patch.object(chat_routes.config, "daily_token_limit", 99999)
mocker.patch.object(chat_routes.config, "weekly_token_limit", 77777)
mocker.patch.object(chat_routes.config, "rate_limit_reset_cost", 500)
response = client.get("/usage")
@@ -390,7 +388,6 @@ def test_usage_uses_config_limits(
user_id=test_user_id,
daily_token_limit=99999,
weekly_token_limit=77777,
rate_limit_reset_cost=500,
)
@@ -403,69 +400,3 @@ def test_usage_rejects_unauthenticated_request() -> None:
response = unauthenticated_client.get("/usage")
assert response.status_code == 401
# ─── Suggested prompts endpoint ──────────────────────────────────────
def _mock_get_business_understanding(
mocker: pytest_mock.MockerFixture,
*,
return_value=None,
):
"""Mock get_business_understanding."""
return mocker.patch(
"backend.api.features.chat.routes.get_business_understanding",
new_callable=AsyncMock,
return_value=return_value,
)
def test_suggested_prompts_returns_themes(
mocker: pytest_mock.MockerFixture,
test_user_id: str,
) -> None:
"""User with themed prompts gets them back as themes list."""
mock_understanding = MagicMock()
mock_understanding.suggested_prompts = {
"Learn": ["L1", "L2"],
"Create": ["C1"],
}
_mock_get_business_understanding(mocker, return_value=mock_understanding)
response = client.get("/suggested-prompts")
assert response.status_code == 200
data = response.json()
assert "themes" in data
themes_by_name = {t["name"]: t["prompts"] for t in data["themes"]}
assert themes_by_name["Learn"] == ["L1", "L2"]
assert themes_by_name["Create"] == ["C1"]
def test_suggested_prompts_no_understanding(
mocker: pytest_mock.MockerFixture,
test_user_id: str,
) -> None:
"""User with no understanding gets empty themes list."""
_mock_get_business_understanding(mocker, return_value=None)
response = client.get("/suggested-prompts")
assert response.status_code == 200
assert response.json() == {"themes": []}
def test_suggested_prompts_empty_prompts(
mocker: pytest_mock.MockerFixture,
test_user_id: str,
) -> None:
"""User with understanding but empty prompts gets empty themes list."""
mock_understanding = MagicMock()
mock_understanding.suggested_prompts = {}
_mock_get_business_understanding(mocker, return_value=mock_understanding)
response = client.get("/suggested-prompts")
assert response.status_code == 200
assert response.json() == {"themes": []}

View File

@@ -18,7 +18,6 @@ from prisma.errors import PrismaError
import backend.api.features.admin.credit_admin_routes
import backend.api.features.admin.execution_analytics_routes
import backend.api.features.admin.rate_limit_admin_routes
import backend.api.features.admin.store_admin_routes
import backend.api.features.builder
import backend.api.features.builder.routes
@@ -319,11 +318,6 @@ app.include_router(
tags=["v2", "admin"],
prefix="/api/executions",
)
app.include_router(
backend.api.features.admin.rate_limit_admin_routes.router,
tags=["v2", "admin"],
prefix="/api/copilot",
)
app.include_router(
backend.api.features.executions.review.routes.router,
tags=["v2", "executions", "review"],

View File

@@ -73,7 +73,7 @@ class ReadDiscordMessagesBlock(Block):
id="df06086a-d5ac-4abb-9996-2ad0acb2eff7",
input_schema=ReadDiscordMessagesBlock.Input, # Assign input schema
output_schema=ReadDiscordMessagesBlock.Output, # Assign output schema
description="Reads new messages from a Discord channel using a bot token and triggers when a new message is posted",
description="Reads messages from a Discord channel using a bot token.",
categories={BlockCategory.SOCIAL},
test_input={
"continuous_read": False,

View File

@@ -28,9 +28,9 @@ class AgentInputBlock(Block):
"""
This block is used to provide input to the graph.
It takes in a value, name, and description.
It takes in a value, name, description, default values list and bool to limit selection to default values.
It outputs the value passed as input.
It Outputs the value passed as input.
"""
class Input(BlockSchemaInput):
@@ -47,6 +47,12 @@ class AgentInputBlock(Block):
default=None,
advanced=True,
)
placeholder_values: list = SchemaField(
description="The placeholder values to be passed as input.",
default_factory=list,
advanced=True,
hidden=True,
)
advanced: bool = SchemaField(
description="Whether to show the input in the advanced section, if the field is not required.",
default=False,
@@ -59,7 +65,10 @@ class AgentInputBlock(Block):
)
def generate_schema(self):
return copy.deepcopy(self.get_field_schema("value"))
schema = copy.deepcopy(self.get_field_schema("value"))
if possible_values := self.placeholder_values:
schema["enum"] = possible_values
return schema
class Output(BlockSchema):
# Use BlockSchema to avoid automatic error field for interface definition
@@ -77,16 +86,18 @@ class AgentInputBlock(Block):
"value": "Hello, World!",
"name": "input_1",
"description": "Example test input.",
"placeholder_values": [],
},
{
"value": 42,
"value": "Hello, World!",
"name": "input_2",
"description": "Example numeric input.",
"description": "Example test input with placeholders.",
"placeholder_values": ["Hello, World!"],
},
],
"test_output": [
("result", "Hello, World!"),
("result", 42),
("result", "Hello, World!"),
],
"categories": {BlockCategory.INPUT, BlockCategory.BASIC},
"block_type": BlockType.INPUT,
@@ -234,11 +245,13 @@ class AgentShortTextInputBlock(AgentInputBlock):
"value": "Hello",
"name": "short_text_1",
"description": "Short text example 1",
"placeholder_values": [],
},
{
"value": "Quick test",
"name": "short_text_2",
"description": "Short text example 2",
"placeholder_values": ["Quick test", "Another option"],
},
],
test_output=[
@@ -272,11 +285,13 @@ class AgentLongTextInputBlock(AgentInputBlock):
"value": "Lorem ipsum dolor sit amet...",
"name": "long_text_1",
"description": "Long text example 1",
"placeholder_values": [],
},
{
"value": "Another multiline text input.",
"name": "long_text_2",
"description": "Long text example 2",
"placeholder_values": ["Another multiline text input."],
},
],
test_output=[
@@ -310,11 +325,13 @@ class AgentNumberInputBlock(AgentInputBlock):
"value": 42,
"name": "number_input_1",
"description": "Number example 1",
"placeholder_values": [],
},
{
"value": 314,
"name": "number_input_2",
"description": "Number example 2",
"placeholder_values": [314, 2718],
},
],
test_output=[
@@ -484,12 +501,6 @@ class AgentDropdownInputBlock(AgentInputBlock):
title="Dropdown Options",
)
def generate_schema(self):
schema = super().generate_schema()
if possible_values := self.placeholder_values:
schema["enum"] = possible_values
return schema
class Output(AgentInputBlock.Output):
result: str = SchemaField(description="Selected dropdown value.")

View File

@@ -4,8 +4,6 @@ import pytest
from backend.blocks import get_blocks
from backend.blocks._base import Block, BlockSchemaInput
from backend.blocks.io import AgentDropdownInputBlock, AgentInputBlock
from backend.data.graph import BaseGraph
from backend.data.model import SchemaField
from backend.util.test import execute_block_test
@@ -281,66 +279,3 @@ class TestAutoCredentialsFieldsValidation:
assert "Duplicate auto_credentials kwarg_name 'credentials'" in str(
exc_info.value
)
def test_agent_input_block_ignores_legacy_placeholder_values():
"""Verify AgentInputBlock.Input.model_construct tolerates extra placeholder_values
for backward compatibility with existing agent JSON."""
legacy_data = {
"name": "url",
"value": "",
"description": "Enter a URL",
"placeholder_values": ["https://example.com"],
}
instance = AgentInputBlock.Input.model_construct(**legacy_data)
schema = instance.generate_schema()
assert (
"enum" not in schema
), "AgentInputBlock should not produce enum from legacy placeholder_values"
def test_dropdown_input_block_produces_enum():
"""Verify AgentDropdownInputBlock.Input.generate_schema() produces enum."""
options = ["Option A", "Option B"]
instance = AgentDropdownInputBlock.Input.model_construct(
name="choice", value=None, placeholder_values=options
)
schema = instance.generate_schema()
assert schema.get("enum") == options
def test_generate_schema_integration_legacy_placeholder_values():
"""Test the full Graph._generate_schema path with legacy placeholder_values
on AgentInputBlock — verifies no enum leaks through the graph loading path."""
legacy_input_default = {
"name": "url",
"value": "",
"description": "Enter a URL",
"placeholder_values": ["https://example.com"],
}
result = BaseGraph._generate_schema(
(AgentInputBlock.Input, legacy_input_default),
)
url_props = result["properties"]["url"]
assert (
"enum" not in url_props
), "Graph schema should not contain enum from AgentInputBlock placeholder_values"
def test_generate_schema_integration_dropdown_produces_enum():
"""Test the full Graph._generate_schema path with AgentDropdownInputBlock
— verifies enum IS produced for dropdown blocks."""
dropdown_input_default = {
"name": "color",
"value": None,
"placeholder_values": ["Red", "Green", "Blue"],
}
result = BaseGraph._generate_schema(
(AgentDropdownInputBlock.Input, dropdown_input_default),
)
color_props = result["properties"]["color"]
assert color_props.get("enum") == [
"Red",
"Green",
"Blue",
], "Graph schema should contain enum from AgentDropdownInputBlock"

View File

@@ -91,20 +91,6 @@ class ChatConfig(BaseSettings):
description="Max tokens per week, resets Monday 00:00 UTC (0 = unlimited)",
)
# Cost (in credits / cents) to reset the daily rate limit using credits.
# When a user hits their daily limit, they can spend this amount to reset
# the daily counter and keep working. Set to 0 to disable the feature.
rate_limit_reset_cost: int = Field(
default=500,
ge=0,
description="Credit cost (in cents) for resetting the daily rate limit. 0 = disabled.",
)
max_daily_resets: int = Field(
default=5,
ge=0,
description="Maximum number of credit-based rate limit resets per user per day. 0 = unlimited.",
)
# Claude Agent SDK Configuration
use_claude_agent_sdk: bool = Field(
default=True,

View File

@@ -205,10 +205,9 @@ Important files (code, configs, outputs) should be saved to workspace to ensure
### SDK tool-result files
When tool outputs are large, the SDK truncates them and saves the full output to
a local file under `~/.claude/projects/.../tool-results/`. To read these files,
always use `Read` (NOT `bash_exec`, NOT `read_workspace_file`).
These files are on the host filesystem — `bash_exec` runs in the sandbox and
CANNOT access them. `read_workspace_file` reads from cloud workspace storage,
where SDK tool-results are NOT stored.
always use `read_file` or `Read` (NOT `read_workspace_file`).
`read_workspace_file` reads from cloud workspace storage, where SDK
tool-results are NOT stored.
{_SHARED_TOOL_NOTES}{extra_notes}"""

View File

@@ -36,10 +36,6 @@ class CoPilotUsageStatus(BaseModel):
daily: UsageWindow
weekly: UsageWindow
reset_cost: int = Field(
default=0,
description="Credit cost (in cents) to reset the daily limit. 0 = feature disabled.",
)
class RateLimitExceeded(Exception):
@@ -65,7 +61,6 @@ async def get_usage_status(
user_id: str,
daily_token_limit: int,
weekly_token_limit: int,
rate_limit_reset_cost: int = 0,
) -> CoPilotUsageStatus:
"""Get current usage status for a user.
@@ -73,7 +68,6 @@ async def get_usage_status(
user_id: The user's ID.
daily_token_limit: Max tokens per day (0 = unlimited).
weekly_token_limit: Max tokens per week (0 = unlimited).
rate_limit_reset_cost: Credit cost (cents) to reset daily limit (0 = disabled).
Returns:
CoPilotUsageStatus with current usage and limits.
@@ -103,7 +97,6 @@ async def get_usage_status(
limit=weekly_token_limit,
resets_at=_weekly_reset_time(now=now),
),
reset_cost=rate_limit_reset_cost,
)
@@ -148,110 +141,6 @@ async def check_rate_limit(
raise RateLimitExceeded("weekly", _weekly_reset_time(now=now))
async def reset_daily_usage(user_id: str, daily_token_limit: int = 0) -> bool:
"""Reset a user's daily token usage counter in Redis.
Called after a user pays credits to extend their daily limit.
Also reduces the weekly usage counter by ``daily_token_limit`` tokens
(clamped to 0) so the user effectively gets one extra day's worth of
weekly capacity.
Args:
user_id: The user's ID.
daily_token_limit: The configured daily token limit. When positive,
the weekly counter is reduced by this amount.
Fails open: returns False if Redis is unavailable (consistent with
the fail-open design of this module).
"""
now = datetime.now(UTC)
try:
redis = await get_redis_async()
# Use a MULTI/EXEC transaction so that DELETE (daily) and DECRBY
# (weekly) either both execute or neither does. This prevents the
# scenario where the daily counter is cleared but the weekly
# counter is not decremented — which would let the caller refund
# credits even though the daily limit was already reset.
d_key = _daily_key(user_id, now=now)
w_key = _weekly_key(user_id, now=now) if daily_token_limit > 0 else None
pipe = redis.pipeline(transaction=True)
pipe.delete(d_key)
if w_key is not None:
pipe.decrby(w_key, daily_token_limit)
results = await pipe.execute()
# Clamp negative weekly counter to 0 (best-effort; not critical).
if w_key is not None:
new_val = results[1] # DECRBY result
if new_val < 0:
await redis.set(w_key, 0, keepttl=True)
logger.info("Reset daily usage for user %s", user_id[:8])
return True
except (RedisError, ConnectionError, OSError):
logger.warning("Redis unavailable for resetting daily usage")
return False
_RESET_LOCK_PREFIX = "copilot:reset_lock"
_RESET_COUNT_PREFIX = "copilot:reset_count"
async def acquire_reset_lock(user_id: str, ttl_seconds: int = 10) -> bool:
"""Acquire a short-lived lock to serialize rate limit resets per user."""
try:
redis = await get_redis_async()
key = f"{_RESET_LOCK_PREFIX}:{user_id}"
return bool(await redis.set(key, "1", nx=True, ex=ttl_seconds))
except (RedisError, ConnectionError, OSError) as exc:
logger.warning("Redis unavailable for reset lock, rejecting reset: %s", exc)
return False
async def release_reset_lock(user_id: str) -> None:
"""Release the per-user reset lock."""
try:
redis = await get_redis_async()
await redis.delete(f"{_RESET_LOCK_PREFIX}:{user_id}")
except (RedisError, ConnectionError, OSError):
pass # Lock will expire via TTL
async def get_daily_reset_count(user_id: str) -> int | None:
"""Get how many times the user has reset today.
Returns None when Redis is unavailable so callers can fail-closed
for billed operations (as opposed to failing open for read-only
rate-limit checks).
"""
now = datetime.now(UTC)
try:
redis = await get_redis_async()
key = f"{_RESET_COUNT_PREFIX}:{user_id}:{now.strftime('%Y-%m-%d')}"
val = await redis.get(key)
return int(val or 0)
except (RedisError, ConnectionError, OSError):
logger.warning("Redis unavailable for reading daily reset count")
return None
async def increment_daily_reset_count(user_id: str) -> None:
"""Increment and track how many resets this user has done today."""
now = datetime.now(UTC)
try:
redis = await get_redis_async()
key = f"{_RESET_COUNT_PREFIX}:{user_id}:{now.strftime('%Y-%m-%d')}"
pipe = redis.pipeline(transaction=True)
pipe.incr(key)
seconds_until_reset = int((_daily_reset_time(now=now) - now).total_seconds())
pipe.expire(key, max(seconds_until_reset, 1))
await pipe.execute()
except (RedisError, ConnectionError, OSError):
logger.warning("Redis unavailable for tracking reset count")
async def record_token_usage(
user_id: str,
prompt_tokens: int,
@@ -342,67 +231,6 @@ async def record_token_usage(
)
async def get_global_rate_limits(
user_id: str,
config_daily: int,
config_weekly: int,
) -> tuple[int, int]:
"""Resolve global rate limits from LaunchDarkly, falling back to config.
Args:
user_id: User ID for LD flag evaluation context.
config_daily: Fallback daily limit from ChatConfig.
config_weekly: Fallback weekly limit from ChatConfig.
Returns:
(daily_token_limit, weekly_token_limit) tuple.
"""
# Lazy import to avoid circular dependency:
# rate_limit -> feature_flag -> settings -> ... -> rate_limit
from backend.util.feature_flag import Flag, get_feature_flag_value
daily_raw = await get_feature_flag_value(
Flag.COPILOT_DAILY_TOKEN_LIMIT.value, user_id, config_daily
)
weekly_raw = await get_feature_flag_value(
Flag.COPILOT_WEEKLY_TOKEN_LIMIT.value, user_id, config_weekly
)
try:
daily = max(0, int(daily_raw))
except (TypeError, ValueError):
logger.warning("Invalid LD value for daily token limit: %r", daily_raw)
daily = config_daily
try:
weekly = max(0, int(weekly_raw))
except (TypeError, ValueError):
logger.warning("Invalid LD value for weekly token limit: %r", weekly_raw)
weekly = config_weekly
return daily, weekly
async def reset_user_usage(user_id: str, *, reset_weekly: bool = False) -> None:
"""Reset a user's usage counters.
Always deletes the daily Redis key. When *reset_weekly* is ``True``,
the weekly key is deleted as well.
Unlike read paths (``get_usage_status``, ``check_rate_limit``) which
fail-open on Redis errors, resets intentionally re-raise so the caller
knows the operation did not succeed. A silent failure here would leave
the admin believing the counters were zeroed when they were not.
"""
now = datetime.now(UTC)
keys_to_delete = [_daily_key(user_id, now=now)]
if reset_weekly:
keys_to_delete.append(_weekly_key(user_id, now=now))
try:
redis = await get_redis_async()
await redis.delete(*keys_to_delete)
except (RedisError, ConnectionError, OSError):
logger.warning("Redis unavailable for resetting user usage")
raise
# ---------------------------------------------------------------------------
# Private helpers
# ---------------------------------------------------------------------------

View File

@@ -12,7 +12,6 @@ from .rate_limit import (
check_rate_limit,
get_usage_status,
record_token_usage,
reset_daily_usage,
)
_USER = "test-user-rl"
@@ -333,91 +332,3 @@ class TestRecordTokenUsage:
):
# Should not raise — fail-open
await record_token_usage(_USER, prompt_tokens=100, completion_tokens=50)
# ---------------------------------------------------------------------------
# reset_daily_usage
# ---------------------------------------------------------------------------
class TestResetDailyUsage:
@staticmethod
def _make_pipeline_mock(decrby_result: int = 0) -> MagicMock:
"""Create a pipeline mock that returns [delete_result, decrby_result]."""
pipe = MagicMock()
pipe.execute = AsyncMock(return_value=[1, decrby_result])
return pipe
@pytest.mark.asyncio
async def test_deletes_daily_key(self):
mock_pipe = self._make_pipeline_mock(decrby_result=0)
mock_redis = AsyncMock()
mock_redis.pipeline = lambda **_kw: mock_pipe
with patch(
"backend.copilot.rate_limit.get_redis_async",
return_value=mock_redis,
):
result = await reset_daily_usage(_USER, daily_token_limit=10000)
assert result is True
mock_pipe.delete.assert_called_once()
@pytest.mark.asyncio
async def test_reduces_weekly_usage_via_decrby(self):
"""Weekly counter should be reduced via DECRBY in the pipeline."""
mock_pipe = self._make_pipeline_mock(decrby_result=35000)
mock_redis = AsyncMock()
mock_redis.pipeline = lambda **_kw: mock_pipe
with patch(
"backend.copilot.rate_limit.get_redis_async",
return_value=mock_redis,
):
await reset_daily_usage(_USER, daily_token_limit=10000)
mock_pipe.decrby.assert_called_once()
mock_redis.set.assert_not_called() # 35000 > 0, no clamp needed
@pytest.mark.asyncio
async def test_clamps_negative_weekly_to_zero(self):
"""If DECRBY goes negative, SET to 0 (outside the pipeline)."""
mock_pipe = self._make_pipeline_mock(decrby_result=-5000)
mock_redis = AsyncMock()
mock_redis.pipeline = lambda **_kw: mock_pipe
with patch(
"backend.copilot.rate_limit.get_redis_async",
return_value=mock_redis,
):
await reset_daily_usage(_USER, daily_token_limit=10000)
mock_pipe.decrby.assert_called_once()
mock_redis.set.assert_called_once()
@pytest.mark.asyncio
async def test_no_weekly_reduction_when_daily_limit_zero(self):
"""When daily_token_limit is 0, weekly counter should not be touched."""
mock_pipe = self._make_pipeline_mock()
mock_pipe.execute = AsyncMock(return_value=[1]) # only delete result
mock_redis = AsyncMock()
mock_redis.pipeline = lambda **_kw: mock_pipe
with patch(
"backend.copilot.rate_limit.get_redis_async",
return_value=mock_redis,
):
await reset_daily_usage(_USER, daily_token_limit=0)
mock_pipe.delete.assert_called_once()
mock_pipe.decrby.assert_not_called()
@pytest.mark.asyncio
async def test_returns_false_when_redis_unavailable(self):
with patch(
"backend.copilot.rate_limit.get_redis_async",
side_effect=ConnectionError("Redis down"),
):
result = await reset_daily_usage(_USER, daily_token_limit=10000)
assert result is False

View File

@@ -1,294 +0,0 @@
"""Unit tests for the POST /usage/reset endpoint."""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import HTTPException
from backend.api.features.chat.routes import reset_copilot_usage
from backend.copilot.rate_limit import CoPilotUsageStatus, UsageWindow
from backend.util.exceptions import InsufficientBalanceError
# Minimal config mock matching ChatConfig fields used by the endpoint.
def _make_config(
rate_limit_reset_cost: int = 500,
daily_token_limit: int = 2_500_000,
weekly_token_limit: int = 12_500_000,
max_daily_resets: int = 5,
):
cfg = MagicMock()
cfg.rate_limit_reset_cost = rate_limit_reset_cost
cfg.daily_token_limit = daily_token_limit
cfg.weekly_token_limit = weekly_token_limit
cfg.max_daily_resets = max_daily_resets
return cfg
def _usage(daily_used: int = 3_000_000, daily_limit: int = 2_500_000):
return CoPilotUsageStatus(
daily=UsageWindow(
used=daily_used,
limit=daily_limit,
resets_at=datetime.now(UTC) + timedelta(hours=6),
),
weekly=UsageWindow(
used=5_000_000,
limit=12_500_000,
resets_at=datetime.now(UTC) + timedelta(days=3),
),
)
_MODULE = "backend.api.features.chat.routes"
def _mock_settings(enable_credit: bool = True):
"""Return a mock Settings object with the given enable_credit flag."""
mock = MagicMock()
mock.config.enable_credit = enable_credit
return mock
@pytest.mark.asyncio
class TestResetCopilotUsage:
async def test_feature_disabled_returns_400(self):
"""When rate_limit_reset_cost=0, endpoint returns 400."""
with patch(f"{_MODULE}.config", _make_config(rate_limit_reset_cost=0)):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 400
assert "not available" in exc_info.value.detail
async def test_no_daily_limit_returns_400(self):
"""When daily_token_limit=0 (unlimited), endpoint returns 400."""
with (
patch(f"{_MODULE}.config", _make_config(daily_token_limit=0)),
patch(f"{_MODULE}.settings", _mock_settings()),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 400
assert "nothing to reset" in exc_info.value.detail.lower()
async def test_not_at_limit_returns_400(self):
"""When user hasn't hit their daily limit, returns 400."""
cfg = _make_config()
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=0)),
patch(f"{_MODULE}.acquire_reset_lock", AsyncMock(return_value=True)),
patch(f"{_MODULE}.release_reset_lock", AsyncMock()) as mock_release,
patch(
f"{_MODULE}.get_usage_status",
AsyncMock(return_value=_usage(daily_used=1_000_000)),
),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 400
assert "not reached" in exc_info.value.detail
mock_release.assert_awaited_once()
async def test_insufficient_credits_returns_402(self):
"""When user doesn't have enough credits, returns 402."""
mock_credit_model = AsyncMock()
mock_credit_model.spend_credits.side_effect = InsufficientBalanceError(
message="Insufficient balance",
user_id="user-1",
balance=50,
amount=200,
)
cfg = _make_config()
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=0)),
patch(f"{_MODULE}.acquire_reset_lock", AsyncMock(return_value=True)),
patch(f"{_MODULE}.release_reset_lock", AsyncMock()) as mock_release,
patch(
f"{_MODULE}.get_usage_status",
AsyncMock(return_value=_usage()),
),
patch(
f"{_MODULE}.get_user_credit_model",
AsyncMock(return_value=mock_credit_model),
),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 402
mock_release.assert_awaited_once()
async def test_happy_path(self):
"""Successful reset: charges credits, resets usage, returns response."""
mock_credit_model = AsyncMock()
mock_credit_model.spend_credits.return_value = 1500 # remaining balance
cfg = _make_config()
updated_usage = _usage(daily_used=0)
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=0)),
patch(f"{_MODULE}.acquire_reset_lock", AsyncMock(return_value=True)),
patch(f"{_MODULE}.release_reset_lock", AsyncMock()),
patch(
f"{_MODULE}.get_usage_status",
AsyncMock(side_effect=[_usage(), updated_usage]),
),
patch(
f"{_MODULE}.get_user_credit_model",
AsyncMock(return_value=mock_credit_model),
),
patch(
f"{_MODULE}.reset_daily_usage", AsyncMock(return_value=True)
) as mock_reset,
patch(f"{_MODULE}.increment_daily_reset_count", AsyncMock()) as mock_incr,
):
result = await reset_copilot_usage(user_id="user-1")
assert result.success is True
assert result.credits_charged == 500
assert result.remaining_balance == 1500
mock_reset.assert_awaited_once()
mock_incr.assert_awaited_once()
async def test_max_daily_resets_exceeded(self):
"""When user has exhausted daily resets, returns 429."""
cfg = _make_config(max_daily_resets=3)
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=3)),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 429
async def test_credit_system_disabled_returns_400(self):
"""When enable_credit=False, endpoint returns 400."""
with (
patch(f"{_MODULE}.config", _make_config()),
patch(f"{_MODULE}.settings", _mock_settings(enable_credit=False)),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 400
assert "credit system is disabled" in exc_info.value.detail.lower()
async def test_weekly_limit_exhausted_returns_400(self):
"""When the weekly limit is also exhausted, resetting daily won't help."""
cfg = _make_config()
weekly_exhausted = CoPilotUsageStatus(
daily=UsageWindow(
used=3_000_000,
limit=2_500_000,
resets_at=datetime.now(UTC) + timedelta(hours=6),
),
weekly=UsageWindow(
used=12_500_000,
limit=12_500_000,
resets_at=datetime.now(UTC) + timedelta(days=3),
),
)
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=0)),
patch(f"{_MODULE}.acquire_reset_lock", AsyncMock(return_value=True)),
patch(f"{_MODULE}.release_reset_lock", AsyncMock()) as mock_release,
patch(
f"{_MODULE}.get_usage_status",
AsyncMock(return_value=weekly_exhausted),
),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 400
assert "weekly" in exc_info.value.detail.lower()
mock_release.assert_awaited_once()
async def test_redis_failure_for_reset_count_returns_503(self):
"""When Redis is unavailable for get_daily_reset_count, returns 503."""
with (
patch(f"{_MODULE}.config", _make_config()),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=None)),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 503
assert "verify" in exc_info.value.detail.lower()
async def test_redis_reset_failure_refunds_credits(self):
"""When reset_daily_usage fails, credits are refunded and 503 returned."""
mock_credit_model = AsyncMock()
mock_credit_model.spend_credits.return_value = 1500
cfg = _make_config()
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=0)),
patch(f"{_MODULE}.acquire_reset_lock", AsyncMock(return_value=True)),
patch(f"{_MODULE}.release_reset_lock", AsyncMock()),
patch(
f"{_MODULE}.get_usage_status",
AsyncMock(return_value=_usage()),
),
patch(
f"{_MODULE}.get_user_credit_model",
AsyncMock(return_value=mock_credit_model),
),
patch(f"{_MODULE}.reset_daily_usage", AsyncMock(return_value=False)),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 503
assert "not been charged" in exc_info.value.detail
mock_credit_model.top_up_credits.assert_awaited_once()
async def test_redis_reset_failure_refund_also_fails(self):
"""When both reset and refund fail, error message reflects the truth."""
mock_credit_model = AsyncMock()
mock_credit_model.spend_credits.return_value = 1500
mock_credit_model.top_up_credits.side_effect = RuntimeError("db down")
cfg = _make_config()
with (
patch(f"{_MODULE}.config", cfg),
patch(f"{_MODULE}.settings", _mock_settings()),
patch(f"{_MODULE}.get_daily_reset_count", AsyncMock(return_value=0)),
patch(f"{_MODULE}.acquire_reset_lock", AsyncMock(return_value=True)),
patch(f"{_MODULE}.release_reset_lock", AsyncMock()),
patch(
f"{_MODULE}.get_usage_status",
AsyncMock(return_value=_usage()),
),
patch(
f"{_MODULE}.get_user_credit_model",
AsyncMock(return_value=mock_credit_model),
),
patch(f"{_MODULE}.reset_daily_usage", AsyncMock(return_value=False)),
):
with pytest.raises(HTTPException) as exc_info:
await reset_copilot_usage(user_id="user-1")
assert exc_info.value.status_code == 503
assert "contact support" in exc_info.value.detail.lower()

View File

@@ -67,17 +67,9 @@ These define the agent's interface — what it accepts and what it produces.
**AgentInputBlock** (ID: `c0a8e994-ebf1-4a9c-a4d8-89d09c86741b`):
- Defines a user-facing input field on the agent
- Required `input_default` fields: `name` (str), `value` (default: null)
- Optional: `title`, `description`
- Optional: `title`, `description`, `placeholder_values` (for dropdowns)
- Output: `result` — the user-provided value at runtime
- Create one AgentInputBlock per distinct input the agent needs
- For dropdown/select inputs, use **AgentDropdownInputBlock** instead (see below)
**AgentDropdownInputBlock** (ID: `655d6fdf-a334-421c-b733-520549c07cd1`):
- Specialized input block that presents a dropdown/select to the user
- Required `input_default` fields: `name` (str), `placeholder_values` (list of options, must have at least one)
- Optional: `title`, `description`, `value` (default selection)
- Output: `result` — the user-selected value at runtime
- Use this instead of AgentInputBlock when the user should pick from a fixed set of options
**AgentOutputBlock** (ID: `363ae599-353e-4804-937e-b2ee3cef3da4`):
- Defines a user-facing output displayed after the agent runs

View File

@@ -102,6 +102,7 @@ async def setup_test_data(server):
"value": "",
"advanced": False,
"description": "Test input field",
"placeholder_values": [],
},
metadata={"position": {"x": 0, "y": 0}},
)
@@ -241,6 +242,7 @@ async def setup_llm_test_data(server):
"value": "",
"advanced": False,
"description": "Prompt for the LLM",
"placeholder_values": [],
},
metadata={"position": {"x": 0, "y": 0}},
)
@@ -394,6 +396,7 @@ async def setup_firecrawl_test_data(server):
"value": "",
"advanced": False,
"description": "URL for Firecrawl to scrape",
"placeholder_values": [],
},
metadata={"position": {"x": 0, "y": 0}},
)

View File

@@ -4,8 +4,6 @@ import logging
import re
from typing import Any
from backend.data.dynamic_fields import DICT_SPLIT
from .helpers import (
AGENT_EXECUTOR_BLOCK_ID,
MCP_TOOL_BLOCK_ID,
@@ -1538,8 +1536,8 @@ class AgentFixer:
for link in links:
sink_name = link.get("sink_name", "")
if DICT_SPLIT in sink_name:
parent, child = sink_name.split(DICT_SPLIT, 1)
if "_#_" in sink_name:
parent, child = sink_name.split("_#_", 1)
# Check if child is a numeric index (invalid for _#_ notation)
if child.isdigit():

View File

@@ -4,8 +4,6 @@ import re
import uuid
from typing import Any
from backend.data.dynamic_fields import DICT_SPLIT
from .blocks import get_blocks_as_dicts
__all__ = [
@@ -53,8 +51,8 @@ def generate_uuid() -> str:
def get_defined_property_type(schema: dict[str, Any], name: str) -> str | None:
"""Get property type from a schema, handling nested `_#_` notation."""
if DICT_SPLIT in name:
parent, child = name.split(DICT_SPLIT, 1)
if "_#_" in name:
parent, child = name.split("_#_", 1)
parent_schema = schema.get(parent, {})
if "properties" in parent_schema and isinstance(
parent_schema["properties"], dict

View File

@@ -5,8 +5,6 @@ import logging
import re
from typing import Any
from backend.data.dynamic_fields import DICT_SPLIT
from .helpers import (
AGENT_EXECUTOR_BLOCK_ID,
AGENT_INPUT_BLOCK_ID,
@@ -258,6 +256,95 @@ class AgentValidator:
return valid
def validate_nested_sink_links(
self,
agent: AgentDict,
blocks: list[dict[str, Any]],
node_lookup: dict[str, dict[str, Any]] | None = None,
) -> bool:
"""
Validate nested sink links (links with _#_ notation).
Returns True if all nested links are valid, False otherwise.
"""
valid = True
block_input_schemas = {
block.get("id", ""): block.get("inputSchema", {}).get("properties", {})
for block in blocks
}
block_names = {
block.get("id", ""): block.get("name", "Unknown Block") for block in blocks
}
if node_lookup is None:
node_lookup = self._build_node_lookup(agent)
for link in agent.get("links", []):
sink_name = link.get("sink_name", "")
sink_id = link.get("sink_id")
if not sink_name or not sink_id:
continue
if "_#_" in sink_name:
parent, child = sink_name.split("_#_", 1)
sink_node = node_lookup.get(sink_id)
if not sink_node:
continue
block_id = sink_node.get("block_id")
input_props = block_input_schemas.get(block_id, {})
parent_schema = input_props.get(parent)
if not parent_schema:
block_name = block_names.get(block_id, "Unknown Block")
self.add_error(
f"Invalid nested sink link '{sink_name}' for "
f"node '{sink_id}' (block "
f"'{block_name}' - {block_id}): Parent property "
f"'{parent}' does not exist in the block's "
f"input schema."
)
valid = False
continue
# Check if additionalProperties is allowed either directly
# or via anyOf
allows_additional_properties = parent_schema.get(
"additionalProperties", False
)
# Check anyOf for additionalProperties
if not allows_additional_properties and "anyOf" in parent_schema:
any_of_schemas = parent_schema.get("anyOf", [])
if isinstance(any_of_schemas, list):
for schema_option in any_of_schemas:
if isinstance(schema_option, dict) and schema_option.get(
"additionalProperties"
):
allows_additional_properties = True
break
if not allows_additional_properties:
if not (
isinstance(parent_schema, dict)
and "properties" in parent_schema
and isinstance(parent_schema["properties"], dict)
and child in parent_schema["properties"]
):
block_name = block_names.get(block_id, "Unknown Block")
self.add_error(
f"Invalid nested sink link '{sink_name}' "
f"for node '{link.get('sink_id', '')}' (block "
f"'{block_name}' - {block_id}): Child "
f"property '{child}' does not exist in "
f"parent '{parent}' schema. Available "
f"properties: "
f"{list(parent_schema.get('properties', {}).keys())}"
)
valid = False
return valid
def validate_prompt_double_curly_braces_spaces(self, agent: AgentDict) -> bool:
"""
Validate that prompt parameters do not contain spaces in double curly
@@ -384,8 +471,8 @@ class AgentValidator:
output_props = block_output_schemas.get(block_id, {})
# Handle nested source names (with _#_ notation)
if DICT_SPLIT in source_name:
parent, child = source_name.split(DICT_SPLIT, 1)
if "_#_" in source_name:
parent, child = source_name.split("_#_", 1)
parent_schema = output_props.get(parent)
if not parent_schema:
@@ -466,195 +553,6 @@ class AgentValidator:
return valid
def validate_sink_input_existence(
self,
agent: AgentDict,
blocks: list[dict[str, Any]],
node_lookup: dict[str, dict[str, Any]] | None = None,
) -> bool:
"""
Validate that all sink_names in links and input_default keys in nodes
exist in the corresponding block's input schema.
Checks that for each link the sink_name references a valid input
property in the sink block's inputSchema, and that every key in a
node's input_default is a recognised input property. Also handles
nested inputs with _#_ notation and dynamic schemas for
AgentExecutorBlock.
Args:
agent: The agent dictionary to validate
blocks: List of available blocks with their schemas
node_lookup: Optional pre-built node-id → node dict
Returns:
True if all sink input fields exist, False otherwise
"""
valid = True
block_input_schemas = {
block.get("id", ""): block.get("inputSchema", {}).get("properties", {})
for block in blocks
}
block_names = {
block.get("id", ""): block.get("name", "Unknown Block") for block in blocks
}
if node_lookup is None:
node_lookup = self._build_node_lookup(agent)
def get_input_props(node: dict[str, Any]) -> dict[str, Any]:
block_id = node.get("block_id", "")
if block_id == AGENT_EXECUTOR_BLOCK_ID:
input_default = node.get("input_default", {})
dynamic_input_schema = input_default.get("input_schema", {})
if not isinstance(dynamic_input_schema, dict):
dynamic_input_schema = {}
dynamic_props = dynamic_input_schema.get("properties", {})
if not isinstance(dynamic_props, dict):
dynamic_props = {}
static_props = block_input_schemas.get(block_id, {})
return {**static_props, **dynamic_props}
return block_input_schemas.get(block_id, {})
def check_nested_input(
input_props: dict[str, Any],
field_name: str,
context: str,
block_name: str,
block_id: str,
) -> bool:
parent, child = field_name.split(DICT_SPLIT, 1)
parent_schema = input_props.get(parent)
if not parent_schema:
self.add_error(
f"{context}: Parent property '{parent}' does not "
f"exist in block '{block_name}' ({block_id}) input "
f"schema."
)
return False
allows_additional = parent_schema.get("additionalProperties", False)
# Only anyOf is checked here because Pydantic's JSON schema
# emits optional/union fields via anyOf. allOf and oneOf are
# not currently used by any block's dict-typed inputs, so
# false positives from them are not a concern in practice.
if not allows_additional and "anyOf" in parent_schema:
for schema_option in parent_schema.get("anyOf", []):
if not isinstance(schema_option, dict):
continue
if schema_option.get("additionalProperties"):
allows_additional = True
break
items_schema = schema_option.get("items")
if isinstance(items_schema, dict) and items_schema.get(
"additionalProperties"
):
allows_additional = True
break
if not allows_additional:
if not (
isinstance(parent_schema, dict)
and "properties" in parent_schema
and isinstance(parent_schema["properties"], dict)
and child in parent_schema["properties"]
):
available = (
list(parent_schema.get("properties", {}).keys())
if isinstance(parent_schema, dict)
else []
)
self.add_error(
f"{context}: Child property '{child}' does not "
f"exist in parent '{parent}' of block "
f"'{block_name}' ({block_id}) input schema. "
f"Available properties: {available}"
)
return False
return True
for link in agent.get("links", []):
sink_id = link.get("sink_id")
sink_name = link.get("sink_name", "")
link_id = link.get("id", "Unknown")
if not sink_name:
# Missing sink_name is caught by validate_data_type_compatibility
continue
sink_node = node_lookup.get(sink_id)
if not sink_node:
# Already caught by validate_link_node_references
continue
block_id = sink_node.get("block_id", "")
block_name = block_names.get(block_id, "Unknown Block")
input_props = get_input_props(sink_node)
context = (
f"Invalid sink input field '{sink_name}' in link "
f"'{link_id}' to node '{sink_id}'"
)
if DICT_SPLIT in sink_name:
if not check_nested_input(
input_props, sink_name, context, block_name, block_id
):
valid = False
else:
if sink_name not in input_props:
available_inputs = list(input_props.keys())
self.add_error(
f"{context} (block '{block_name}' - {block_id}): "
f"Input property '{sink_name}' does not exist in "
f"the block's input schema. "
f"Available inputs: {available_inputs}"
)
valid = False
for node in agent.get("nodes", []):
node_id = node.get("id")
block_id = node.get("block_id", "")
block_name = block_names.get(block_id, "Unknown Block")
input_default = node.get("input_default", {})
if not isinstance(input_default, dict) or not input_default:
continue
if (
block_id not in block_input_schemas
and block_id != AGENT_EXECUTOR_BLOCK_ID
):
continue
input_props = get_input_props(node)
for key in input_default:
if key == "credentials":
continue
context = (
f"Node '{node_id}' (block '{block_name}' - {block_id}) "
f"has unknown input_default key '{key}'"
)
if DICT_SPLIT in key:
if not check_nested_input(
input_props, key, context, block_name, block_id
):
valid = False
else:
if key not in input_props:
available_inputs = list(input_props.keys())
self.add_error(
f"{context} which does not exist in the "
f"block's input schema. "
f"Available inputs: {available_inputs}"
)
valid = False
return valid
def validate_io_blocks(self, agent: AgentDict) -> bool:
"""
Validate that the agent has at least one AgentInputBlock and one
@@ -1101,12 +999,12 @@ class AgentValidator:
self.validate_data_type_compatibility(agent, blocks, node_lookup),
),
(
"Source output existence",
self.validate_source_output_existence(agent, blocks, node_lookup),
"Nested sink links",
self.validate_nested_sink_links(agent, blocks, node_lookup),
),
(
"Sink input existence",
self.validate_sink_input_existence(agent, blocks, node_lookup),
"Source output existence",
self.validate_source_output_existence(agent, blocks, node_lookup),
),
(
"Prompt double curly braces spaces",

View File

@@ -331,6 +331,43 @@ class TestValidatePromptDoubleCurlyBracesSpaces:
assert any("spaces" in e for e in v.errors)
# ============================================================================
# validate_nested_sink_links
# ============================================================================
class TestValidateNestedSinkLinks:
def test_valid_nested_link_passes(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={
"properties": {
"config": {
"type": "object",
"properties": {"key": {"type": "string"}},
}
},
"required": [],
},
)
node = _make_node(node_id="n1", block_id="b1")
link = _make_link(sink_id="n1", sink_name="config_#_key", source_id="n2")
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_nested_sink_links(agent, [block]) is True
def test_invalid_parent_fails(self):
v = AgentValidator()
block = _make_block(block_id="b1")
node = _make_node(node_id="n1", block_id="b1")
link = _make_link(sink_id="n1", sink_name="nonexistent_#_key", source_id="n2")
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_nested_sink_links(agent, [block]) is False
assert any("does not exist" in e for e in v.errors)
# ============================================================================
# validate_agent_executor_block_schemas
# ============================================================================
@@ -558,28 +595,11 @@ class TestValidate:
input_block = _make_block(
block_id=AGENT_INPUT_BLOCK_ID,
name="AgentInputBlock",
input_schema={
"properties": {
"name": {"type": "string"},
"title": {"type": "string"},
"value": {},
"description": {"type": "string"},
},
"required": ["name"],
},
output_schema={"properties": {"result": {}}},
)
output_block = _make_block(
block_id=AGENT_OUTPUT_BLOCK_ID,
name="AgentOutputBlock",
input_schema={
"properties": {
"name": {"type": "string"},
"title": {"type": "string"},
"value": {},
},
"required": ["name"],
},
)
input_node = _make_node(
node_id="n-in",
@@ -630,201 +650,6 @@ class TestValidate:
assert "AgentOutputBlock" in error_message
class TestValidateSinkInputExistence:
"""Tests for validate_sink_input_existence."""
def test_valid_sink_name_passes(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={"properties": {"url": {"type": "string"}}, "required": []},
)
node = _make_node(node_id="n1", block_id="b1")
link = _make_link(
source_id="src", source_name="out", sink_id="n1", sink_name="url"
)
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_sink_input_existence(agent, [block]) is True
def test_invalid_sink_name_fails(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={"properties": {"url": {"type": "string"}}, "required": []},
)
node = _make_node(node_id="n1", block_id="b1")
link = _make_link(
source_id="src", source_name="out", sink_id="n1", sink_name="nonexistent"
)
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_sink_input_existence(agent, [block]) is False
assert any("nonexistent" in e for e in v.errors)
def test_valid_nested_link_passes(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={
"properties": {
"config": {
"type": "object",
"properties": {"key": {"type": "string"}},
}
},
"required": [],
},
)
node = _make_node(node_id="n1", block_id="b1")
link = _make_link(
source_id="src",
source_name="out",
sink_id="n1",
sink_name="config_#_key",
)
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_sink_input_existence(agent, [block]) is True
def test_invalid_nested_child_fails(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={
"properties": {
"config": {
"type": "object",
"properties": {"key": {"type": "string"}},
}
},
"required": [],
},
)
node = _make_node(node_id="n1", block_id="b1")
link = _make_link(
source_id="src",
source_name="out",
sink_id="n1",
sink_name="config_#_missing",
)
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_sink_input_existence(agent, [block]) is False
def test_unknown_input_default_key_fails(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={"properties": {"url": {"type": "string"}}, "required": []},
)
node = _make_node(
node_id="n1", block_id="b1", input_default={"nonexistent_key": "value"}
)
agent = _make_agent(nodes=[node])
assert v.validate_sink_input_existence(agent, [block]) is False
assert any("nonexistent_key" in e for e in v.errors)
def test_credentials_key_skipped(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={"properties": {"url": {"type": "string"}}, "required": []},
)
node = _make_node(
node_id="n1",
block_id="b1",
input_default={
"url": "http://example.com",
"credentials": {"api_key": "x"},
},
)
agent = _make_agent(nodes=[node])
assert v.validate_sink_input_existence(agent, [block]) is True
def test_agent_executor_dynamic_schema_passes(self):
v = AgentValidator()
block = _make_block(
block_id=AGENT_EXECUTOR_BLOCK_ID,
input_schema={
"properties": {
"graph_id": {"type": "string"},
"input_schema": {"type": "object"},
},
"required": ["graph_id"],
},
)
node = _make_node(
node_id="n1",
block_id=AGENT_EXECUTOR_BLOCK_ID,
input_default={
"graph_id": "abc",
"input_schema": {
"properties": {"query": {"type": "string"}},
"required": [],
},
},
)
link = _make_link(
source_id="src",
source_name="out",
sink_id="n1",
sink_name="query",
)
agent = _make_agent(nodes=[node], links=[link])
assert v.validate_sink_input_existence(agent, [block]) is True
def test_input_default_nested_invalid_child_fails(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={
"properties": {
"config": {
"type": "object",
"properties": {"key": {"type": "string"}},
}
},
"required": [],
},
)
node = _make_node(
node_id="n1",
block_id="b1",
input_default={"config_#_invalid_child": "value"},
)
agent = _make_agent(nodes=[node])
assert v.validate_sink_input_existence(agent, [block]) is False
assert any("invalid_child" in e for e in v.errors)
def test_input_default_nested_valid_child_passes(self):
v = AgentValidator()
block = _make_block(
block_id="b1",
input_schema={
"properties": {
"config": {
"type": "object",
"properties": {"key": {"type": "string"}},
}
},
"required": [],
},
)
node = _make_node(
node_id="n1",
block_id="b1",
input_default={"config_#_key": "value"},
)
agent = _make_agent(nodes=[node])
assert v.validate_sink_input_existence(agent, [block]) is True
class TestValidateMCPToolBlocks:
"""Tests for validate_mcp_tool_blocks."""

View File

@@ -342,7 +342,6 @@ class GraphExecution(GraphExecutionMeta):
if (
(block := get_block(exec.block_id))
and block.block_type == BlockType.INPUT
and "name" in exec.input_data
)
}
),
@@ -361,10 +360,8 @@ class GraphExecution(GraphExecutionMeta):
outputs: CompletedBlockOutput = defaultdict(list)
for exec in complete_node_executions:
if (
(block := get_block(exec.block_id))
and block.block_type == BlockType.OUTPUT
and "name" in exec.input_data
):
block := get_block(exec.block_id)
) and block.block_type == BlockType.OUTPUT:
outputs[exec.input_data["name"]].append(exec.input_data.get("value"))
return GraphExecution(

View File

@@ -40,9 +40,6 @@ _MAX_PAGES = 100
# LLM extraction timeout (seconds)
_LLM_TIMEOUT = 30
SUGGESTION_THEMES = ["Learn", "Create", "Automate", "Organize"]
PROMPTS_PER_THEME = 5
def _mask_email(email: str) -> str:
"""Mask an email for safe logging: 'alice@example.com' -> 'a***e@example.com'."""
@@ -335,11 +332,6 @@ Fields:
- current_software (list of strings): software/tools currently used
- existing_automation (list of strings): existing automations
- additional_notes (string): any additional context
- suggested_prompts (object with keys "Learn", "Create", "Automate", "Organize"): for each key, \
provide a list of 5 short action prompts (each under 20 words) that would help this person. \
"Learn" = questions about AutoGPT features; "Create" = content/document generation tasks; \
"Automate" = recurring workflow automation ideas; "Organize" = structuring/prioritizing tasks. \
Should be specific to their industry, role, and pain points; actionable and conversational in tone.
Form data:
"""
@@ -386,29 +378,6 @@ async def extract_business_understanding(
# Filter out null values before constructing
cleaned = {k: v for k, v in data.items() if v is not None}
# Validate suggested_prompts: themed dict, filter >20 words, cap at 5 per theme
raw_prompts = cleaned.get("suggested_prompts", {})
if isinstance(raw_prompts, dict):
themed: dict[str, list[str]] = {}
for theme in SUGGESTION_THEMES:
theme_prompts = raw_prompts.get(theme, [])
if not isinstance(theme_prompts, list):
continue
valid = [
s
for p in theme_prompts
if isinstance(p, str) and (s := p.strip()) and len(s.split()) <= 20
]
if valid:
themed[theme] = valid[:PROMPTS_PER_THEME]
if themed:
cleaned["suggested_prompts"] = themed
else:
cleaned.pop("suggested_prompts", None)
else:
cleaned.pop("suggested_prompts", None)
return BusinessUnderstandingInput(**cleaned)

View File

@@ -284,7 +284,6 @@ async def test_populate_understanding_full_flow():
],
}
mock_input = MagicMock()
mock_input.suggested_prompts = {"Learn": ["P1"], "Create": ["P2"]}
with (
patch(
@@ -398,25 +397,15 @@ def test_extraction_prompt_no_format_placeholders():
@pytest.mark.asyncio
async def test_extract_business_understanding_themed_prompts():
"""Happy path: LLM returns themed prompts as dict."""
async def test_extract_business_understanding_success():
"""Happy path: LLM returns valid JSON that maps to BusinessUnderstandingInput."""
mock_choice = MagicMock()
mock_choice.message.content = json.dumps(
{
"user_name": "Alice",
"business_name": "Acme Corp",
"suggested_prompts": {
"Learn": ["Learn 1", "Learn 2", "Learn 3", "Learn 4", "Learn 5"],
"Create": [
"Create 1",
"Create 2",
"Create 3",
"Create 4",
"Create 5",
],
"Automate": ["Auto 1", "Auto 2", "Auto 3", "Auto 4", "Auto 5"],
"Organize": ["Org 1", "Org 2", "Org 3", "Org 4", "Org 5"],
},
"industry": "Technology",
"pain_points": ["manual reporting"],
}
)
mock_response = MagicMock()
@@ -429,42 +418,9 @@ async def test_extract_business_understanding_themed_prompts():
result = await extract_business_understanding("Q: Name?\nA: Alice")
assert result.user_name == "Alice"
assert result.suggested_prompts is not None
assert len(result.suggested_prompts) == 4
assert len(result.suggested_prompts["Learn"]) == 5
@pytest.mark.asyncio
async def test_extract_themed_prompts_filters_long_and_unknown_keys():
"""Long prompts are filtered, unknown keys are dropped, each theme capped at 5."""
long_prompt = " ".join(["word"] * 21)
mock_choice = MagicMock()
mock_choice.message.content = json.dumps(
{
"user_name": "Alice",
"suggested_prompts": {
"Learn": [long_prompt, "Valid learn 1", "Valid learn 2"],
"UnknownTheme": ["Should be dropped"],
"Automate": ["A1", "A2", "A3", "A4", "A5", "A6"],
},
}
)
mock_response = MagicMock()
mock_response.choices = [mock_choice]
mock_client = AsyncMock()
mock_client.chat.completions.create.return_value = mock_response
with patch("backend.data.tally.AsyncOpenAI", return_value=mock_client):
result = await extract_business_understanding("Q: Name?\nA: Alice")
assert result.suggested_prompts is not None
# Unknown key dropped
assert "UnknownTheme" not in result.suggested_prompts
# Long prompt filtered
assert result.suggested_prompts["Learn"] == ["Valid learn 1", "Valid learn 2"]
# Capped at 5
assert result.suggested_prompts["Automate"] == ["A1", "A2", "A3", "A4", "A5"]
assert result.business_name == "Acme Corp"
assert result.industry == "Technology"
assert result.pain_points == ["manual reporting"]
@pytest.mark.asyncio

View File

@@ -49,25 +49,6 @@ def _json_to_list(value: Any) -> list[str]:
return []
def _json_to_themed_prompts(value: Any) -> dict[str, list[str]]:
"""Convert Json field to themed prompts dict.
Handles both the new ``dict[str, list[str]]`` format and the legacy
``list[str]`` format. Legacy rows are placed under a ``"General"`` key so
existing personalised prompts remain readable until a backfill regenerates
them into the proper themed shape.
"""
if isinstance(value, dict):
return {
k: [i for i in v if isinstance(i, str)]
for k, v in value.items()
if isinstance(k, str) and isinstance(v, list)
}
if isinstance(value, list) and value:
return {"General": [str(p) for p in value if isinstance(p, str)]}
return {}
class BusinessUnderstandingInput(pydantic.BaseModel):
"""Input model for updating business understanding - all fields optional for incremental updates."""
@@ -123,11 +104,6 @@ class BusinessUnderstandingInput(pydantic.BaseModel):
None, description="Any additional context"
)
# Suggested prompts (UI-only, not included in system prompt)
suggested_prompts: Optional[dict[str, list[str]]] = pydantic.Field(
None, description="LLM-generated suggested prompts grouped by theme"
)
class BusinessUnderstanding(pydantic.BaseModel):
"""Full business understanding model returned from database."""
@@ -164,9 +140,6 @@ class BusinessUnderstanding(pydantic.BaseModel):
# Additional context
additional_notes: Optional[str] = None
# Suggested prompts (UI-only, not included in system prompt)
suggested_prompts: dict[str, list[str]] = pydantic.Field(default_factory=dict)
@classmethod
def from_db(cls, db_record: CoPilotUnderstanding) -> "BusinessUnderstanding":
"""Convert database record to Pydantic model."""
@@ -194,7 +167,6 @@ class BusinessUnderstanding(pydantic.BaseModel):
current_software=_json_to_list(business.get("current_software")),
existing_automation=_json_to_list(business.get("existing_automation")),
additional_notes=business.get("additional_notes"),
suggested_prompts=_json_to_themed_prompts(data.get("suggested_prompts")),
)
@@ -274,22 +246,33 @@ async def get_business_understanding(
return understanding
def merge_business_understanding_data(
existing_data: dict[str, Any],
async def upsert_business_understanding(
user_id: str,
input_data: BusinessUnderstandingInput,
) -> dict[str, Any]:
"""Merge new input into existing data dict using incremental strategy.
) -> BusinessUnderstanding:
"""
Create or update business understanding with incremental merge strategy.
- String fields: new value overwrites if provided (not None)
- List fields: new items are appended to existing (deduplicated)
- suggested_prompts: fully replaced if provided (not None)
Returns the merged data dict (mutates and returns *existing_data*).
Data is stored as: {name: ..., business: {version: 1, ...}}
"""
# Get existing record for merge
existing = await CoPilotUnderstanding.prisma().find_unique(
where={"userId": user_id}
)
# Get existing data structure or start fresh
existing_data: dict[str, Any] = {}
if existing and isinstance(existing.data, dict):
existing_data = dict(existing.data)
existing_business: dict[str, Any] = {}
if isinstance(existing_data.get("business"), dict):
existing_business = dict(existing_data["business"])
# Business fields (stored inside business object)
business_string_fields = [
"job_title",
"business_name",
@@ -327,48 +310,16 @@ def merge_business_understanding_data(
merged = _merge_lists(existing_list, value)
existing_business[field] = merged
# Suggested prompts - fully replace if provided
if input_data.suggested_prompts is not None:
existing_data["suggested_prompts"] = input_data.suggested_prompts
# Set version and nest business data
existing_business["version"] = 1
existing_data["business"] = existing_business
return existing_data
async def upsert_business_understanding(
user_id: str,
input_data: BusinessUnderstandingInput,
) -> BusinessUnderstanding:
"""
Create or update business understanding with incremental merge strategy.
- String fields: new value overwrites if provided (not None)
- List fields: new items are appended to existing (deduplicated)
- suggested_prompts: fully replaced if provided (not None)
Data is stored as: {name: ..., business: {version: 1, ...}}
"""
# Get existing record for merge
existing = await CoPilotUnderstanding.prisma().find_unique(
where={"userId": user_id}
)
# Get existing data structure or start fresh
existing_data: dict[str, Any] = {}
if existing and isinstance(existing.data, dict):
existing_data = dict(existing.data)
merged_data = merge_business_understanding_data(existing_data, input_data)
# Upsert with the merged data
record = await CoPilotUnderstanding.prisma().upsert(
where={"userId": user_id},
data={
"create": {"userId": user_id, "data": SafeJson(merged_data)},
"update": {"data": SafeJson(merged_data)},
"create": {"userId": user_id, "data": SafeJson(existing_data)},
"update": {"data": SafeJson(existing_data)},
},
)

View File

@@ -1,148 +0,0 @@
"""Tests for business understanding merge and format logic."""
from datetime import datetime, timezone
from typing import Any
from unittest.mock import MagicMock
from backend.data.understanding import (
BusinessUnderstanding,
BusinessUnderstandingInput,
_json_to_themed_prompts,
format_understanding_for_prompt,
merge_business_understanding_data,
)
def _make_input(**kwargs: Any) -> BusinessUnderstandingInput:
"""Create a BusinessUnderstandingInput with only the specified fields."""
return BusinessUnderstandingInput.model_validate(kwargs)
# ─── merge_business_understanding_data: themed prompts ─────────────────
def test_merge_themed_prompts_overwrites_existing():
"""New themed prompts should fully replace existing ones (not merge)."""
existing = {
"name": "Alice",
"business": {"industry": "Tech", "version": 1},
"suggested_prompts": {
"Learn": ["Old learn prompt"],
"Create": ["Old create prompt"],
},
}
new_prompts = {
"Automate": ["Schedule daily reports", "Set up email alerts"],
"Organize": ["Sort inbox by priority"],
}
input_data = _make_input(suggested_prompts=new_prompts)
result = merge_business_understanding_data(existing, input_data)
assert result["suggested_prompts"] == new_prompts
def test_merge_themed_prompts_none_preserves_existing():
"""When input has suggested_prompts=None, existing themed prompts are preserved."""
existing_prompts = {
"Learn": ["How to automate?"],
"Create": ["Build a chatbot"],
}
existing = {
"name": "Alice",
"business": {"industry": "Tech", "version": 1},
"suggested_prompts": existing_prompts,
}
input_data = _make_input(industry="Finance")
result = merge_business_understanding_data(existing, input_data)
assert result["suggested_prompts"] == existing_prompts
assert result["business"]["industry"] == "Finance"
# ─── from_db: themed prompts deserialization ───────────────────────────
def test_from_db_themed_prompts():
"""from_db correctly deserializes a themed dict for suggested_prompts."""
themed = {
"Learn": ["What can I automate?"],
"Create": ["Build a workflow"],
}
db_record = MagicMock()
db_record.id = "test-id"
db_record.userId = "user-1"
db_record.createdAt = datetime.now(tz=timezone.utc)
db_record.updatedAt = datetime.now(tz=timezone.utc)
db_record.data = {
"name": "Alice",
"business": {"industry": "Tech", "version": 1},
"suggested_prompts": themed,
}
result = BusinessUnderstanding.from_db(db_record)
assert result.suggested_prompts == themed
def test_from_db_legacy_list_prompts_preserved_under_general():
"""from_db preserves legacy list[str] prompts under a 'General' key."""
db_record = MagicMock()
db_record.id = "test-id"
db_record.userId = "user-1"
db_record.createdAt = datetime.now(tz=timezone.utc)
db_record.updatedAt = datetime.now(tz=timezone.utc)
db_record.data = {
"name": "Alice",
"business": {"industry": "Tech", "version": 1},
"suggested_prompts": ["Old prompt 1", "Old prompt 2"],
}
result = BusinessUnderstanding.from_db(db_record)
assert result.suggested_prompts == {"General": ["Old prompt 1", "Old prompt 2"]}
# ─── _json_to_themed_prompts helper ───────────────────────────────────
def test_json_to_themed_prompts_with_dict():
value = {"Learn": ["a", "b"], "Create": ["c"]}
assert _json_to_themed_prompts(value) == {"Learn": ["a", "b"], "Create": ["c"]}
def test_json_to_themed_prompts_with_list_returns_general():
assert _json_to_themed_prompts(["a", "b"]) == {"General": ["a", "b"]}
def test_json_to_themed_prompts_with_none_returns_empty():
assert _json_to_themed_prompts(None) == {}
# ─── format_understanding_for_prompt: excludes themed prompts ──────────
def test_format_understanding_excludes_themed_prompts():
"""Themed suggested_prompts are UI-only and must NOT appear in the system prompt."""
understanding = BusinessUnderstanding(
id="test-id",
user_id="user-1",
created_at=datetime.now(tz=timezone.utc),
updated_at=datetime.now(tz=timezone.utc),
user_name="Alice",
industry="Technology",
suggested_prompts={
"Learn": ["Automate reports"],
"Create": ["Set up alerts", "Track KPIs"],
},
)
formatted = format_understanding_for_prompt(understanding)
assert "Alice" in formatted
assert "Technology" in formatted
assert "suggested_prompts" not in formatted
assert "Automate reports" not in formatted
assert "Set up alerts" not in formatted
assert "Track KPIs" not in formatted

View File

@@ -39,8 +39,6 @@ class Flag(str, Enum):
ENABLE_PLATFORM_PAYMENT = "enable-platform-payment"
CHAT = "chat"
COPILOT_SDK = "copilot-sdk"
COPILOT_DAILY_TOKEN_LIMIT = "copilot-daily-token-limit"
COPILOT_WEEKLY_TOKEN_LIMIT = "copilot-weekly-token-limit"
def is_configured() -> bool:

View File

@@ -1,8 +0,0 @@
{
"daily_token_limit": 2500000,
"daily_tokens_used": 500000,
"user_email": "target@example.com",
"user_id": "5e53486c-cf57-477e-ba2a-cb02dc828e1c",
"weekly_token_limit": 12500000,
"weekly_tokens_used": 3000000
}

View File

@@ -1,8 +0,0 @@
{
"daily_token_limit": 2500000,
"daily_tokens_used": 0,
"user_email": "target@example.com",
"user_id": "5e53486c-cf57-477e-ba2a-cb02dc828e1c",
"weekly_token_limit": 12500000,
"weekly_tokens_used": 0
}

View File

@@ -1,8 +0,0 @@
{
"daily_token_limit": 2500000,
"daily_tokens_used": 0,
"user_email": "target@example.com",
"user_id": "5e53486c-cf57-477e-ba2a-cb02dc828e1c",
"weekly_token_limit": 12500000,
"weekly_tokens_used": 3000000
}

View File

@@ -526,12 +526,7 @@ class TestValidateOrchestratorBlocks:
"id": AGENT_INPUT_BLOCK_ID,
"name": "AgentInputBlock",
"inputSchema": {
"properties": {
"name": {"type": "string"},
"title": {"type": "string"},
"value": {},
"description": {"type": "string"},
},
"properties": {"name": {"type": "string"}},
"required": ["name"],
},
"outputSchema": {"properties": {"result": {}}},
@@ -542,7 +537,6 @@ class TestValidateOrchestratorBlocks:
"inputSchema": {
"properties": {
"name": {"type": "string"},
"title": {"type": "string"},
"value": {},
},
"required": ["name"],
@@ -689,12 +683,7 @@ class TestOrchestratorE2EPipeline:
"id": AGENT_INPUT_BLOCK_ID,
"name": "AgentInputBlock",
"inputSchema": {
"properties": {
"name": {"type": "string"},
"title": {"type": "string"},
"value": {},
"description": {"type": "string"},
},
"properties": {"name": {"type": "string"}},
"required": ["name"],
},
"outputSchema": {"properties": {"result": {}}},
@@ -705,7 +694,6 @@ class TestOrchestratorE2EPipeline:
"inputSchema": {
"properties": {
"name": {"type": "string"},
"title": {"type": "string"},
"value": {},
},
"required": ["name"],

View File

@@ -254,6 +254,7 @@ class TestDataCreator:
"value": "",
"advanced": False,
"description": None,
"placeholder_values": [],
},
metadata={"position": {"x": -1012, "y": 674}},
)
@@ -273,6 +274,7 @@ class TestDataCreator:
"value": "",
"advanced": False,
"description": None,
"placeholder_values": [],
},
metadata={"position": {"x": -1117, "y": 78}},
)

View File

@@ -7,7 +7,6 @@ const config: StorybookConfig = {
"../src/components/atoms/**/*.stories.@(js|jsx|mjs|ts|tsx)",
"../src/components/molecules/**/*.stories.@(js|jsx|mjs|ts|tsx)",
"../src/components/ai-elements/**/*.stories.@(js|jsx|mjs|ts|tsx)",
"../src/components/renderers/**/*.stories.@(js|jsx|mjs|ts|tsx)",
],
addons: [
"@storybook/addon-a11y",

View File

@@ -1,5 +1,5 @@
# Base stage for both dev and prod
FROM node:21-alpine AS base
FROM node:22.22-alpine3.23 AS base
WORKDIR /app
RUN corepack enable
COPY autogpt_platform/frontend/package.json autogpt_platform/frontend/pnpm-lock.yaml ./
@@ -33,7 +33,7 @@ ENV NEXT_PUBLIC_SOURCEMAPS="false"
RUN if [ "$NEXT_PUBLIC_PW_TEST" = "true" ]; then NEXT_PUBLIC_PW_TEST=true NODE_OPTIONS="--max-old-space-size=8192" pnpm build; else NODE_OPTIONS="--max-old-space-size=8192" pnpm build; fi
# Prod stage - based on NextJS reference Dockerfile https://github.com/vercel/next.js/blob/64271354533ed16da51be5dce85f0dbd15f17517/examples/with-docker/Dockerfile
FROM node:21-alpine AS prod
FROM node:22.22-alpine3.23 AS prod
ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
WORKDIR /app

View File

@@ -1,71 +0,0 @@
"use client";
import { useState } from "react";
import { Input } from "@/components/__legacy__/ui/input";
import { Button } from "@/components/atoms/Button/Button";
import { MagnifyingGlass } from "@phosphor-icons/react";
export interface AdminUserSearchProps {
/** Current search query value (controlled). Falls back to internal state if omitted. */
value?: string;
/** Called when the input text changes */
onChange?: (value: string) => void;
/** Called when the user presses Enter or clicks the search button */
onSearch: (query: string) => void;
/** Placeholder text for the input */
placeholder?: string;
/** Disables the input and button while a search is in progress */
isLoading?: boolean;
}
/**
* Shared admin user search input.
* Supports searching users by name, email, or partial/fuzzy text.
* Can be used as controlled (value + onChange) or uncontrolled (internal state).
*/
export function AdminUserSearch({
value: controlledValue,
onChange,
onSearch,
placeholder = "Search users by Name or Email...",
isLoading = false,
}: AdminUserSearchProps) {
const [internalValue, setInternalValue] = useState("");
const isControlled = controlledValue !== undefined;
const currentValue = isControlled ? controlledValue : internalValue;
function handleChange(newValue: string) {
if (isControlled) {
onChange?.(newValue);
} else {
setInternalValue(newValue);
}
}
function handleSearch() {
onSearch(currentValue.trim());
}
return (
<div className="flex w-full items-center gap-2">
<Input
placeholder={placeholder}
aria-label={placeholder}
value={currentValue}
onChange={(e) => handleChange(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleSearch()}
disabled={isLoading}
/>
<Button
variant="outline"
size="small"
onClick={handleSearch}
disabled={isLoading || !currentValue.trim()}
loading={isLoading}
>
{isLoading ? "Searching..." : <MagnifyingGlass size={16} />}
</Button>
</div>
);
}

View File

@@ -1,34 +0,0 @@
"use client";
export function formatTokens(tokens: number): string {
if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`;
if (tokens >= 1_000) return `${(tokens / 1_000).toFixed(0)}K`;
return tokens.toString();
}
export function UsageBar({ used, limit }: { used: number; limit: number }) {
if (limit === 0) {
return <span className="text-sm text-gray-500">Unlimited</span>;
}
const pct = Math.min(Math.max(0, (used / limit) * 100), 100);
const color =
pct >= 90 ? "bg-red-500" : pct >= 70 ? "bg-yellow-500" : "bg-green-500";
return (
<div className="space-y-1">
<div className="flex justify-between text-sm">
<span>{formatTokens(used)} used</span>
<span>{formatTokens(limit)} limit</span>
</div>
<div className="h-2 w-full rounded-full bg-gray-200">
<div
className={`h-2 rounded-full ${color}`}
style={{ width: `${pct}%` }}
/>
</div>
<div className="text-right text-xs text-gray-500">
{pct.toFixed(1)}% used
</div>
</div>
);
}

View File

@@ -1,6 +1,5 @@
import { Sidebar } from "@/components/__legacy__/Sidebar";
import { Users, DollarSign, UserSearch, FileText } from "lucide-react";
import { Gauge } from "@phosphor-icons/react/dist/ssr";
import { IconSliders } from "@/components/__legacy__/ui/icons";
@@ -22,11 +21,6 @@ const sidebarLinkGroups = [
href: "/admin/impersonation",
icon: <UserSearch className="h-6 w-6" />,
},
{
text: "Rate Limits",
href: "/admin/rate-limits",
icon: <Gauge className="h-6 w-6" />,
},
{
text: "Execution Analytics",
href: "/admin/execution-analytics",

View File

@@ -1,85 +0,0 @@
"use client";
import { useState } from "react";
import { Button } from "@/components/atoms/Button/Button";
import type { UserRateLimitResponse } from "@/app/api/__generated__/models/userRateLimitResponse";
import { UsageBar } from "../../components/UsageBar";
interface Props {
data: UserRateLimitResponse;
onReset: (resetWeekly: boolean) => Promise<void>;
/** Override the outer container classes (default: bordered card). */
className?: string;
}
export function RateLimitDisplay({ data, onReset, className }: Props) {
const [isResetting, setIsResetting] = useState(false);
const [resetWeekly, setResetWeekly] = useState(false);
async function handleReset() {
const msg = resetWeekly
? "Reset both daily and weekly usage counters to zero?"
: "Reset daily usage counter to zero?";
if (!window.confirm(msg)) return;
setIsResetting(true);
try {
await onReset(resetWeekly);
} finally {
setIsResetting(false);
}
}
const nothingToReset = resetWeekly
? data.daily_tokens_used === 0 && data.weekly_tokens_used === 0
: data.daily_tokens_used === 0;
return (
<div className={className ?? "rounded-md border bg-white p-6"}>
<h2 className="mb-1 text-lg font-semibold">
Rate Limits for {data.user_email ?? data.user_id}
</h2>
{data.user_email && (
<p className="mb-4 text-xs text-gray-500">User ID: {data.user_id}</p>
)}
{!data.user_email && <div className="mb-4" />}
<div className="grid grid-cols-2 gap-6">
<div className="space-y-2">
<h3 className="text-sm font-medium text-gray-700">Daily Usage</h3>
<UsageBar
used={data.daily_tokens_used}
limit={data.daily_token_limit}
/>
</div>
<div className="space-y-2">
<h3 className="text-sm font-medium text-gray-700">Weekly Usage</h3>
<UsageBar
used={data.weekly_tokens_used}
limit={data.weekly_token_limit}
/>
</div>
</div>
<div className="mt-6 flex items-center gap-3 border-t pt-4">
<select
aria-label="Reset scope"
value={resetWeekly ? "both" : "daily"}
onChange={(e) => setResetWeekly(e.target.value === "both")}
className="rounded-md border bg-white px-3 py-1.5 text-sm"
disabled={isResetting}
>
<option value="daily">Reset daily only</option>
<option value="both">Reset daily + weekly</option>
</select>
<Button
variant="outline"
onClick={handleReset}
disabled={isResetting || nothingToReset}
>
{isResetting ? "Resetting..." : "Reset Usage"}
</Button>
</div>
</div>
);
}

View File

@@ -1,81 +0,0 @@
"use client";
import { AdminUserSearch } from "../../components/AdminUserSearch";
import { RateLimitDisplay } from "./RateLimitDisplay";
import { useRateLimitManager } from "./useRateLimitManager";
export function RateLimitManager() {
const {
isSearching,
isLoadingRateLimit,
searchResults,
selectedUser,
rateLimitData,
handleSearch,
handleSelectUser,
handleReset,
} = useRateLimitManager();
return (
<div className="space-y-6">
<div className="rounded-md border bg-white p-6">
<label className="mb-2 block text-sm font-medium">Search User</label>
<AdminUserSearch
onSearch={handleSearch}
placeholder="Search by name, email, or user ID..."
isLoading={isSearching}
/>
<p className="mt-1.5 text-xs text-gray-500">
Exact email or user ID does a direct lookup. Partial text searches
user history.
</p>
</div>
{/* User selection list -- always require explicit selection */}
{searchResults.length >= 1 && !selectedUser && (
<div className="rounded-md border bg-white p-4">
<h3 className="mb-2 text-sm font-medium text-gray-700">
Select a user ({searchResults.length}{" "}
{searchResults.length === 1 ? "result" : "results"})
</h3>
<ul className="divide-y">
{searchResults.map((user) => (
<li key={user.user_id}>
<button
className="w-full px-2 py-2 text-left text-sm hover:bg-gray-100"
onClick={() => handleSelectUser(user)}
>
<span className="font-medium">{user.user_email}</span>
<span className="ml-2 text-xs text-gray-500">
{user.user_id}
</span>
</button>
</li>
))}
</ul>
</div>
)}
{/* Show selected user */}
{selectedUser && searchResults.length >= 1 && (
<div className="rounded-md border border-blue-200 bg-blue-50 px-4 py-2 text-sm">
Selected:{" "}
<span className="font-medium">{selectedUser.user_email}</span>
<span className="ml-2 text-xs text-gray-500">
{selectedUser.user_id}
</span>
</div>
)}
{isLoadingRateLimit && (
<div className="py-4 text-center text-sm text-gray-500">
Loading rate limits...
</div>
)}
{rateLimitData && (
<RateLimitDisplay data={rateLimitData} onReset={handleReset} />
)}
</div>
);
}

View File

@@ -1,212 +0,0 @@
"use client";
import { useState } from "react";
import { useToast } from "@/components/molecules/Toast/use-toast";
import type { UserRateLimitResponse } from "@/app/api/__generated__/models/userRateLimitResponse";
import {
getV2GetUserRateLimit,
getV2GetAllUsersHistory,
postV2ResetUserRateLimitUsage,
} from "@/app/api/__generated__/endpoints/admin/admin";
export interface UserOption {
user_id: string;
user_email: string;
}
/**
* Returns true when the input looks like a complete email address.
* Used to decide whether to call the direct email lookup endpoint
* vs. the broader user-history search.
*/
function looksLikeEmail(input: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(input);
}
/**
* Returns true when the input looks like a UUID (user ID).
*/
function looksLikeUuid(input: string): boolean {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
input,
);
}
export function useRateLimitManager() {
const { toast } = useToast();
const [isSearching, setIsSearching] = useState(false);
const [isLoadingRateLimit, setIsLoadingRateLimit] = useState(false);
const [searchResults, setSearchResults] = useState<UserOption[]>([]);
const [selectedUser, setSelectedUser] = useState<UserOption | null>(null);
const [rateLimitData, setRateLimitData] =
useState<UserRateLimitResponse | null>(null);
/** Direct lookup by email or user ID via the rate-limit endpoint. */
async function handleDirectLookup(trimmed: string) {
setIsSearching(true);
setSearchResults([]);
setSelectedUser(null);
setRateLimitData(null);
try {
const params = looksLikeEmail(trimmed)
? { email: trimmed }
: { user_id: trimmed };
const response = await getV2GetUserRateLimit(params);
if (response.status !== 200) {
throw new Error("Failed to fetch rate limit");
}
setRateLimitData(response.data);
setSelectedUser({
user_id: response.data.user_id,
user_email: response.data.user_email ?? response.data.user_id,
});
} catch (error) {
console.error("Error fetching rate limit:", error);
const hint = looksLikeEmail(trimmed)
? "No user found with that email address."
: "Check the user ID and try again.";
toast({
title: "Error",
description: `Failed to fetch rate limits. ${hint}`,
variant: "destructive",
});
setRateLimitData(null);
} finally {
setIsSearching(false);
}
}
/** Fuzzy name/email search via the spending-history endpoint. */
async function handleFuzzySearch(trimmed: string) {
setIsSearching(true);
setSearchResults([]);
setSelectedUser(null);
setRateLimitData(null);
try {
const response = await getV2GetAllUsersHistory({
search: trimmed,
page: 1,
page_size: 50,
});
if (response.status !== 200) {
throw new Error("Failed to search users");
}
// Deduplicate by user_id to get unique users
const seen = new Set<string>();
const users: UserOption[] = [];
for (const tx of response.data.history) {
if (!seen.has(tx.user_id)) {
seen.add(tx.user_id);
users.push({
user_id: tx.user_id,
user_email: String(tx.user_email ?? tx.user_id),
});
}
}
if (users.length === 0) {
toast({
title: "No results",
description: "No users found matching your search.",
});
}
// Always show the result list so the user explicitly picks a match.
// The history endpoint paginates transactions, not users, so a single
// page may not be authoritative -- avoid auto-selecting.
setSearchResults(users);
} catch (error) {
console.error("Error searching users:", error);
toast({
title: "Error",
description: "Failed to search users.",
variant: "destructive",
});
} finally {
setIsSearching(false);
}
}
async function handleSearch(query: string) {
const trimmed = query.trim();
if (!trimmed) return;
// Direct lookup when the input is a full email or UUID.
// This avoids the spending-history indirection and works even for
// users who have no credit transaction history.
if (looksLikeEmail(trimmed) || looksLikeUuid(trimmed)) {
await handleDirectLookup(trimmed);
} else {
await handleFuzzySearch(trimmed);
}
}
async function fetchRateLimit(userId: string) {
setIsLoadingRateLimit(true);
try {
const response = await getV2GetUserRateLimit({ user_id: userId });
if (response.status !== 200) {
throw new Error("Failed to fetch rate limit");
}
setRateLimitData(response.data);
} catch (error) {
console.error("Error fetching rate limit:", error);
toast({
title: "Error",
description: "Failed to fetch user rate limit.",
variant: "destructive",
});
setRateLimitData(null);
} finally {
setIsLoadingRateLimit(false);
}
}
async function handleSelectUser(user: UserOption) {
setSelectedUser(user);
setRateLimitData(null);
await fetchRateLimit(user.user_id);
}
async function handleReset(resetWeekly: boolean) {
if (!rateLimitData) return;
try {
const response = await postV2ResetUserRateLimitUsage({
user_id: rateLimitData.user_id,
reset_weekly: resetWeekly,
});
if (response.status !== 200) {
throw new Error("Failed to reset usage");
}
setRateLimitData(response.data);
toast({
title: "Success",
description: resetWeekly
? "Daily and weekly usage reset to zero."
: "Daily usage reset to zero.",
});
} catch (error) {
console.error("Error resetting rate limit:", error);
toast({
title: "Error",
description: "Failed to reset rate limit usage.",
variant: "destructive",
});
}
}
return {
isSearching,
isLoadingRateLimit,
searchResults,
selectedUser,
rateLimitData,
handleSearch,
handleSelectUser,
handleReset,
};
}

View File

@@ -1,25 +0,0 @@
import { withRoleAccess } from "@/lib/withRoleAccess";
import { RateLimitManager } from "./components/RateLimitManager";
function RateLimitsDashboard() {
return (
<div className="mx-auto p-6">
<div className="flex flex-col gap-4">
<div>
<h1 className="text-3xl font-bold">User Rate Limits</h1>
<p className="text-gray-500">
Check and manage CoPilot rate limits per user
</p>
</div>
<RateLimitManager />
</div>
</div>
);
}
export default async function RateLimitsDashboardPage() {
"use server";
const withAdminAccess = await withRoleAccess(["admin"]);
const ProtectedDashboard = await withAdminAccess(RateLimitsDashboard);
return <ProtectedDashboard />;
}

View File

@@ -11,7 +11,6 @@ import { PaginationControls } from "../../../../../components/__legacy__/ui/pagi
import { SearchAndFilterAdminSpending } from "./SearchAndFilterAdminSpending";
import { getUsersTransactionHistory } from "@/app/(platform)/admin/spending/actions";
import { AdminAddMoneyButton } from "./AddMoneyButton";
import { RateLimitModal } from "./RateLimitModal";
import { CreditTransactionType } from "@/lib/autogpt-server-api";
export async function AdminUserGrantHistory({
@@ -81,7 +80,10 @@ export async function AdminUserGrantHistory({
return (
<div className="space-y-4">
<SearchAndFilterAdminSpending initialSearch={initialSearch} />
<SearchAndFilterAdminSpending
initialStatus={initialStatus}
initialSearch={initialSearch}
/>
<div className="rounded-md border bg-white">
<Table>
@@ -103,7 +105,7 @@ export async function AdminUserGrantHistory({
{history.length === 0 ? (
<TableRow>
<TableCell
colSpan={9}
colSpan={8}
className="py-10 text-center text-gray-500"
>
No transactions found
@@ -112,7 +114,7 @@ export async function AdminUserGrantHistory({
) : (
history.map((transaction) => (
<TableRow
key={`${transaction.user_id}-${transaction.transaction_time}`}
key={transaction.user_id}
className="hover:bg-gray-50"
>
<TableCell className="font-medium">
@@ -145,29 +147,25 @@ export async function AdminUserGrantHistory({
${transaction.current_balance / 100}
</TableCell> */}
<TableCell className="text-right">
<div className="flex items-center justify-end gap-2">
<RateLimitModal
userId={transaction.user_id}
userEmail={transaction.user_email ?? ""}
/>
<AdminAddMoneyButton
userId={transaction.user_id}
userEmail={transaction.user_email ?? ""}
currentBalance={transaction.current_balance}
defaultAmount={
transaction.transaction_type ===
CreditTransactionType.USAGE
? -transaction.amount
: undefined
}
defaultComments={
transaction.transaction_type ===
CreditTransactionType.USAGE
? "Refund for usage"
: undefined
}
/>
</div>
<AdminAddMoneyButton
userId={transaction.user_id}
userEmail={
transaction.user_email ?? "User Email wasn't attached"
}
currentBalance={transaction.current_balance}
defaultAmount={
transaction.transaction_type ===
CreditTransactionType.USAGE
? -transaction.amount
: undefined
}
defaultComments={
transaction.transaction_type ===
CreditTransactionType.USAGE
? "Refund for usage"
: undefined
}
/>
</TableCell>
</TableRow>
))

View File

@@ -1,138 +0,0 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "@/components/atoms/Button/Button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
} from "@/components/__legacy__/ui/dialog";
import { useToast } from "@/components/molecules/Toast/use-toast";
import type { UserRateLimitResponse } from "@/app/api/__generated__/models/userRateLimitResponse";
import {
getV2GetUserRateLimit,
postV2ResetUserRateLimitUsage,
} from "@/app/api/__generated__/endpoints/admin/admin";
import { Gauge } from "@phosphor-icons/react";
import { RateLimitDisplay } from "../../rate-limits/components/RateLimitDisplay";
export function RateLimitModal({
userId,
userEmail,
}: {
userId: string;
userEmail: string;
}) {
const { toast } = useToast();
const [open, setOpen] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [rateLimitData, setRateLimitData] =
useState<UserRateLimitResponse | null>(null);
useEffect(() => {
if (!open) {
setRateLimitData(null);
return;
}
async function fetchRateLimit() {
setIsLoading(true);
try {
const response = await getV2GetUserRateLimit({ user_id: userId });
if (response.status !== 200) {
throw new Error("Failed to fetch rate limit");
}
setRateLimitData(response.data);
} catch (error) {
console.error("Error fetching rate limit:", error);
toast({
title: "Error",
description: "Failed to fetch user rate limit.",
variant: "destructive",
});
setRateLimitData(null);
} finally {
setIsLoading(false);
}
}
fetchRateLimit();
}, [open, userId, toast]);
async function handleReset(resetWeekly: boolean) {
if (!rateLimitData) return;
try {
const response = await postV2ResetUserRateLimitUsage({
user_id: rateLimitData.user_id,
reset_weekly: resetWeekly,
});
if (response.status !== 200) {
throw new Error("Failed to reset usage");
}
setRateLimitData(response.data);
toast({
title: "Success",
description: resetWeekly
? "Daily and weekly usage reset to zero."
: "Daily usage reset to zero.",
});
} catch (error) {
console.error("Error resetting rate limit:", error);
toast({
title: "Error",
description: "Failed to reset rate limit usage.",
variant: "destructive",
});
}
}
return (
<>
<Button
size="small"
variant="outline"
onClick={(e) => {
e.stopPropagation();
setOpen(true);
}}
>
<Gauge size={16} className="mr-1" />
Rate Limits
</Button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>Rate Limits</DialogTitle>
<DialogDescription>
CoPilot rate limits for {userEmail || userId}
</DialogDescription>
</DialogHeader>
{isLoading && (
<div className="py-8 text-center text-gray-500">
Loading rate limits...
</div>
)}
{!isLoading && rateLimitData && (
<RateLimitDisplay
data={rateLimitData}
onReset={handleReset}
className="space-y-4"
/>
)}
{!isLoading && !rateLimitData && (
<div className="py-8 text-center text-gray-500">
No rate limit data available for this user.
</div>
)}
</DialogContent>
</Dialog>
</>
);
}

View File

@@ -2,6 +2,9 @@
import { useState, useEffect } from "react";
import { useRouter, usePathname, useSearchParams } from "next/navigation";
import { Input } from "@/components/__legacy__/ui/input";
import { Button } from "@/components/__legacy__/ui/button";
import { Search } from "lucide-react";
import { CreditTransactionType } from "@/lib/autogpt-server-api";
import {
Select,
@@ -10,11 +13,11 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/__legacy__/ui/select";
import { AdminUserSearch } from "../../components/AdminUserSearch";
export function SearchAndFilterAdminSpending({
initialSearch,
}: {
initialStatus?: CreditTransactionType;
initialSearch?: string;
}) {
const router = useRouter();
@@ -34,11 +37,11 @@ export function SearchAndFilterAdminSpending({
setSearchQuery(searchParams.get("search") || "");
}, [searchParams]);
function handleSearch(query: string) {
const handleSearch = () => {
const params = new URLSearchParams(searchParams.toString());
if (query) {
params.set("search", query);
if (searchQuery) {
params.set("search", searchQuery);
} else {
params.delete("search");
}
@@ -52,15 +55,21 @@ export function SearchAndFilterAdminSpending({
params.set("page", "1"); // Reset to first page on new search
router.push(`${pathname}?${params.toString()}`);
}
};
return (
<div className="flex items-center justify-between">
<AdminUserSearch
value={searchQuery}
onChange={setSearchQuery}
onSearch={handleSearch}
/>
<div className="flex w-full items-center gap-2">
<Input
placeholder="Search users by Name or Email..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleSearch()}
/>
<Button variant="outline" onClick={handleSearch}>
<Search className="h-4 w-4" />
</Button>
</div>
<Select
value={selectedStatus}

View File

@@ -1,14 +1,9 @@
"use client";
import type { CoPilotUsageStatus } from "@/app/api/__generated__/models/coPilotUsageStatus";
import { useGetV2GetCopilotUsage } from "@/app/api/__generated__/endpoints/chat/chat";
import { toast } from "@/components/molecules/Toast/use-toast";
import useCredits from "@/hooks/useCredits";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import { SidebarProvider } from "@/components/ui/sidebar";
import { cn } from "@/lib/utils";
import { UploadSimple } from "@phosphor-icons/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { useCallback, useRef, useState } from "react";
import { ChatContainer } from "./components/ChatContainer/ChatContainer";
import { ChatSidebar } from "./components/ChatSidebar/ChatSidebar";
import { DeleteChatDialog } from "./components/DeleteChatDialog/DeleteChatDialog";
@@ -16,7 +11,6 @@ import { MobileDrawer } from "./components/MobileDrawer/MobileDrawer";
import { MobileHeader } from "./components/MobileHeader/MobileHeader";
import { NotificationBanner } from "./components/NotificationBanner/NotificationBanner";
import { NotificationDialog } from "./components/NotificationDialog/NotificationDialog";
import { RateLimitResetDialog } from "./components/RateLimitResetDialog/RateLimitResetDialog";
import { ScaleLoader } from "./components/ScaleLoader/ScaleLoader";
import { useCopilotPage } from "./useCopilotPage";
@@ -71,7 +65,6 @@ export function CopilotPage() {
error,
stop,
isReconnecting,
isSyncing,
createSession,
onSend,
isLoadingSession,
@@ -95,45 +88,8 @@ export function CopilotPage() {
isDeleting,
handleConfirmDelete,
handleCancelDelete,
// Rate limit reset
rateLimitMessage,
dismissRateLimit,
} = useCopilotPage();
const {
data: usage,
isSuccess: hasUsage,
isError: usageError,
} = useGetV2GetCopilotUsage({
query: {
select: (res) => res.data as CoPilotUsageStatus,
refetchInterval: 30000,
staleTime: 10000,
},
});
const resetCost = usage?.reset_cost;
const isBillingEnabled = useGetFlag(Flag.ENABLE_PLATFORM_PAYMENT);
const { credits, fetchCredits } = useCredits({ fetchInitialCredits: true });
const hasInsufficientCredits =
credits !== null && resetCost != null && credits < resetCost;
// Fall back to a toast when the credit-based reset feature is disabled or
// when the usage query fails (so the user still gets feedback).
useEffect(() => {
if (
rateLimitMessage &&
(usageError || (hasUsage && (resetCost ?? 0) <= 0))
) {
toast({
title: "Usage limit reached",
description: rateLimitMessage,
variant: "destructive",
});
dismissRateLimit();
}
}, [rateLimitMessage, resetCost, hasUsage, usageError, dismissRateLimit]);
if (isUserLoading || !isLoggedIn) {
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-[#f8f8f9]">
@@ -179,7 +135,6 @@ export function CopilotPage() {
isSessionError={isSessionError}
isCreatingSession={isCreatingSession}
isReconnecting={isReconnecting}
isSyncing={isSyncing}
onCreateSession={createSession}
onSend={onSend}
onStop={stop}
@@ -211,20 +166,6 @@ export function CopilotPage() {
/>
)}
<NotificationDialog />
<RateLimitResetDialog
isOpen={!!rateLimitMessage && hasUsage && (resetCost ?? 0) > 0}
onClose={dismissRateLimit}
resetCost={resetCost ?? 0}
resetMessage={rateLimitMessage ?? ""}
isWeeklyExhausted={
hasUsage &&
usage.weekly.limit > 0 &&
usage.weekly.used >= usage.weekly.limit
}
hasInsufficientCredits={hasInsufficientCredits}
isBillingEnabled={isBillingEnabled}
onCreditChange={fetchCredits}
/>
</SidebarProvider>
);
}

View File

@@ -17,8 +17,6 @@ export interface ChatContainerProps {
isCreatingSession: boolean;
/** True when backend has an active stream but we haven't reconnected yet. */
isReconnecting?: boolean;
/** True while re-syncing session state after device wake. */
isSyncing?: boolean;
onCreateSession: () => void | Promise<string>;
onSend: (message: string, files?: File[]) => void | Promise<void>;
onStop: () => void;
@@ -37,7 +35,6 @@ export const ChatContainer = ({
isSessionError,
isCreatingSession,
isReconnecting,
isSyncing,
onCreateSession,
onSend,
onStop,
@@ -49,7 +46,6 @@ export const ChatContainer = ({
status === "streaming" ||
status === "submitted" ||
!!isReconnecting ||
!!isSyncing ||
isLoadingSession ||
!!isSessionError;
const inputLayoutId = "copilot-2-chat-input";

View File

@@ -93,12 +93,6 @@ export function ChatInput({
baseHandleChange(e);
}
const resolvedPlaceholder = isRecording
? ""
: isTranscribing
? "Transcribing..."
: placeholder;
const canSend =
!disabled &&
(!!value.trim() || hasFiles) &&
@@ -135,7 +129,7 @@ export function ChatInput({
onChange={handleChange}
onKeyDown={handleKeyDown}
disabled={isInputDisabled}
placeholder={resolvedPlaceholder}
placeholder={isTranscribing ? "Transcribing..." : placeholder}
/>
{isRecording && !value && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">

View File

@@ -4,11 +4,7 @@ import {
ConversationContent,
ConversationScrollButton,
} from "@/components/ai-elements/conversation";
import {
Message,
MessageActions,
MessageContent,
} from "@/components/ai-elements/message";
import { Message, MessageContent } from "@/components/ai-elements/message";
import { LoadingSpinner } from "@/components/atoms/LoadingSpinner/LoadingSpinner";
import { FileUIPart, UIDataTypes, UIMessage, UITools } from "ai";
import { TOOL_PART_PREFIX } from "../JobStatsBar/constants";
@@ -23,7 +19,6 @@ import {
splitReasoningAndResponse,
} from "./helpers";
import { AssistantMessageActions } from "./components/AssistantMessageActions";
import { CopyButton } from "./components/CopyButton";
import { CollapsedToolGroup } from "./components/CollapsedToolGroup";
import { MessageAttachments } from "./components/MessageAttachments";
import { MessagePartRenderer } from "./components/MessagePartRenderer";
@@ -245,11 +240,6 @@ export function ChatMessagesContainer({
<ThinkingIndicator active={showThinking} />
)}
</MessageContent>
{message.role === "user" && textParts.length > 0 && (
<MessageActions className="mt-1 justify-end opacity-0 transition-opacity group-focus-within:opacity-100 group-hover:opacity-100">
<CopyButton text={textParts.map((p) => p.text).join("\n")} />
</MessageActions>
)}
{fileParts.length > 0 && (
<MessageAttachments
files={fileParts}

View File

@@ -2,7 +2,7 @@
import { MessageAction } from "@/components/ai-elements/message";
import { toast } from "@/components/molecules/Toast/use-toast";
import { Check, CopySimple } from "@phosphor-icons/react";
import { Check, Copy } from "@phosphor-icons/react";
import { useState } from "react";
interface Props {
@@ -31,12 +31,10 @@ export function CopyButton({ text }: Props) {
return (
<MessageAction
tooltip={copied ? "Copied!" : "Copy"}
tooltip={copied ? "Copied!" : "Copy to clipboard"}
onClick={handleCopy}
variant="ghost"
size="icon-sm"
>
{copied ? <Check size={16} /> : <CopySimple size={16} weight="regular" />}
{copied ? <Check size={16} /> : <Copy size={16} />}
</MessageAction>
);
}

View File

@@ -25,7 +25,6 @@ import {
import { cn } from "@/lib/utils";
import {
CheckCircle,
CircleNotch,
DotsThree,
PlusCircleIcon,
PlusIcon,
@@ -37,6 +36,7 @@ import { useEffect, useRef, useState } from "react";
import { useCopilotUIStore } from "../../store";
import { NotificationToggle } from "./components/NotificationToggle/NotificationToggle";
import { DeleteChatDialog } from "../DeleteChatDialog/DeleteChatDialog";
import { PulseLoader } from "../PulseLoader/PulseLoader";
import { UsageLimits } from "../UsageLimits/UsageLimits";
export function ChatSidebar() {
@@ -367,10 +367,7 @@ export function ChatSidebar() {
{session.is_processing &&
session.id !== sessionId &&
!completedSessionIDs.has(session.id) && (
<CircleNotch
className="h-4 w-4 shrink-0 animate-spin text-zinc-400"
weight="bold"
/>
<PulseLoader size={16} className="shrink-0" />
)}
{completedSessionIDs.has(session.id) &&
session.id !== sessionId && (

View File

@@ -1,18 +1,17 @@
"use client";
import { ChatInput } from "@/app/(platform)/copilot/components/ChatInput/ChatInput";
import { useGetV2GetSuggestedPrompts } from "@/app/api/__generated__/endpoints/chat/chat";
import { Skeleton } from "@/components/atoms/Skeleton/Skeleton";
import { Button } from "@/components/atoms/Button/Button";
import { Text } from "@/components/atoms/Text/Text";
import { useSupabase } from "@/lib/supabase/hooks/useSupabase";
import { SpinnerGapIcon } from "@phosphor-icons/react";
import { motion } from "framer-motion";
import { useEffect, useState } from "react";
import {
getGreetingName,
getInputPlaceholder,
getSuggestionThemes,
getQuickActions,
} from "./helpers";
import { SuggestionThemes } from "./components/SuggestionThemes/SuggestionThemes";
interface Props {
inputLayoutId: string;
@@ -34,35 +33,25 @@ export function EmptySession({
}: Props) {
const { user } = useSupabase();
const greetingName = getGreetingName(user);
const { data: suggestedPromptsResponse, isLoading: isLoadingPrompts } =
useGetV2GetSuggestedPrompts({
query: { staleTime: Infinity, gcTime: Infinity, refetchOnMount: false },
});
const themes = getSuggestionThemes(
suggestedPromptsResponse?.status === 200
? suggestedPromptsResponse.data.themes
: undefined,
);
const quickActions = getQuickActions();
const [loadingAction, setLoadingAction] = useState<string | null>(null);
const [inputPlaceholder, setInputPlaceholder] = useState(
getInputPlaceholder(),
);
useEffect(() => {
function handleResize() {
setInputPlaceholder(getInputPlaceholder(window.innerWidth));
setInputPlaceholder(getInputPlaceholder(window.innerWidth));
}, [window.innerWidth]);
async function handleQuickActionClick(action: string) {
if (isCreatingSession || loadingAction !== null) return;
setLoadingAction(action);
try {
await onSend(action);
} finally {
setLoadingAction(null);
}
handleResize();
const mql = window.matchMedia("(max-width: 500px)");
mql.addEventListener("change", handleResize);
const mql2 = window.matchMedia("(max-width: 1080px)");
mql2.addEventListener("change", handleResize);
return () => {
mql.removeEventListener("change", handleResize);
mql2.removeEventListener("change", handleResize);
};
}, []);
}
return (
<div className="flex h-full flex-1 items-center justify-center overflow-y-auto bg-[#f8f8f9] px-0 py-5 md:px-6 md:py-10">
@@ -100,19 +89,30 @@ export function EmptySession({
</div>
</div>
{isLoadingPrompts ? (
<div className="flex flex-wrap items-center justify-center gap-3">
{Array.from({ length: 4 }, (_, i) => (
<Skeleton key={i} className="h-10 w-28 shrink-0 rounded-full" />
))}
</div>
) : (
<SuggestionThemes
themes={themes}
onSend={onSend}
disabled={isCreatingSession}
/>
)}
<div className="flex flex-wrap items-center justify-center gap-3 overflow-x-auto [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
{quickActions.map((action) => (
<Button
key={action}
type="button"
variant="outline"
size="small"
onClick={() => void handleQuickActionClick(action)}
disabled={isCreatingSession || loadingAction !== null}
aria-busy={loadingAction === action}
leftIcon={
loadingAction === action ? (
<SpinnerGapIcon
className="h-4 w-4 animate-spin"
weight="bold"
/>
) : null
}
className="h-auto shrink-0 border-zinc-300 px-3 py-2 text-[.9rem] text-zinc-600"
>
{action}
</Button>
))}
</div>
</motion.div>
</div>
);

View File

@@ -1,100 +0,0 @@
"use client";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/molecules/Popover/Popover";
import { Button } from "@/components/atoms/Button/Button";
import {
BookOpenIcon,
PaintBrushIcon,
LightningIcon,
ListChecksIcon,
SpinnerGapIcon,
} from "@phosphor-icons/react";
import { useState } from "react";
import type { SuggestionTheme } from "../../helpers";
const THEME_ICONS: Record<string, typeof BookOpenIcon> = {
Learn: BookOpenIcon,
Create: PaintBrushIcon,
Automate: LightningIcon,
Organize: ListChecksIcon,
};
interface Props {
themes: SuggestionTheme[];
onSend: (prompt: string) => void | Promise<void>;
disabled?: boolean;
}
export function SuggestionThemes({ themes, onSend, disabled }: Props) {
const [openTheme, setOpenTheme] = useState<string | null>(null);
const [loadingPrompt, setLoadingPrompt] = useState<string | null>(null);
async function handlePromptClick(theme: string, prompt: string) {
if (disabled || loadingPrompt) return;
setLoadingPrompt(`${theme}:${prompt}`);
try {
await onSend(prompt);
} finally {
setLoadingPrompt(null);
setOpenTheme(null);
}
}
return (
<div className="flex flex-wrap items-center justify-center gap-3">
{themes.map((theme) => {
const Icon = THEME_ICONS[theme.name];
return (
<Popover
key={theme.name}
open={openTheme === theme.name}
onOpenChange={(open) => setOpenTheme(open ? theme.name : null)}
>
<PopoverTrigger asChild>
<Button
type="button"
variant="outline"
size="small"
disabled={disabled || loadingPrompt !== null}
className="shrink-0 gap-2 border-zinc-300 px-3 py-2 text-[.9rem] text-zinc-600"
>
{Icon && <Icon size={16} weight="regular" />}
{theme.name}
</Button>
</PopoverTrigger>
<PopoverContent align="center" className="w-80 p-2">
<ul className="grid gap-0.5">
{theme.prompts.map((prompt) => (
<li key={prompt}>
<button
type="button"
disabled={disabled || loadingPrompt !== null}
onClick={() => void handlePromptClick(theme.name, prompt)}
className="w-full rounded-md px-3 py-2 text-left text-sm text-zinc-700 transition-colors hover:bg-zinc-100 disabled:opacity-50"
>
{loadingPrompt === `${theme.name}:${prompt}` ? (
<span className="flex items-center gap-2">
<SpinnerGapIcon
className="h-4 w-4 animate-spin"
weight="bold"
/>
{prompt}
</span>
) : (
prompt
)}
</button>
</li>
))}
</ul>
</PopoverContent>
</Popover>
);
})}
</div>
);
}

View File

@@ -12,87 +12,12 @@ export function getInputPlaceholder(width?: number) {
return "What's your role and what eats up most of your day? e.g. 'I'm a recruiter and I hate...'";
}
export interface SuggestionTheme {
name: string;
prompts: string[];
}
export const DEFAULT_THEMES: SuggestionTheme[] = [
{
name: "Learn",
prompts: [
"What can AutoGPT do for me?",
"Show me how agents work",
"What integrations are available?",
"How do I schedule an agent?",
"What are the most popular agents?",
],
},
{
name: "Create",
prompts: [
"Draft a weekly status report",
"Generate social media posts for my business",
"Create a competitive analysis summary",
"Write onboarding emails for new hires",
"Build a content calendar for next month",
],
},
{
name: "Automate",
prompts: [
"Monitor relevant websites for changes",
"Send me a daily news digest on my industry",
"Auto-reply to common customer questions",
"Track price changes on products I sell",
"Summarize my emails every morning",
],
},
{
name: "Organize",
prompts: [
"Summarize my unread emails",
"Create a project timeline from my notes",
"Prioritize my task list by urgency",
"Build a decision matrix for vendor selection",
"Organize my meeting notes into action items",
],
},
];
export function getSuggestionThemes(
apiThemes?: SuggestionTheme[],
): SuggestionTheme[] {
if (!apiThemes?.length) {
return DEFAULT_THEMES;
}
const promptsByTheme = new Map(
apiThemes.map((theme) => [theme.name, theme.prompts] as const),
);
// Legacy users have prompts under "General" — distribute them across themes
const generalPrompts = (promptsByTheme.get("General") ?? []).filter(
(p) => p.trim().length > 0,
);
return DEFAULT_THEMES.map((theme, idx) => {
const personalized = (promptsByTheme.get(theme.name) ?? []).filter(
(p) => p.trim().length > 0,
);
// Spread legacy "General" prompts round-robin across themes
const legacySlice = generalPrompts.filter(
(_, i) => i % DEFAULT_THEMES.length === idx,
);
return {
name: theme.name,
prompts: Array.from(
new Set([...personalized, ...legacySlice, ...theme.prompts]),
).slice(0, theme.prompts.length),
};
});
export function getQuickActions() {
return [
"I don't know where to start, just ask me stuff",
"I do the same thing every week and it's killing me",
"Help me find where I'm wasting my time",
];
}
export function getGreetingName(user?: User | null) {

View File

@@ -5,7 +5,6 @@ import { scrollbarStyles } from "@/components/styles/scrollbars";
import { cn } from "@/lib/utils";
import {
CheckCircle,
CircleNotch,
PlusIcon,
SpeakerHigh,
SpeakerSlash,
@@ -14,6 +13,7 @@ import {
} from "@phosphor-icons/react";
import { Drawer } from "vaul";
import { useCopilotUIStore } from "../../store";
import { PulseLoader } from "../PulseLoader/PulseLoader";
interface Props {
isOpen: boolean;
@@ -165,10 +165,7 @@ export function MobileDrawer({
{session.is_processing &&
!completedSessionIDs.has(session.id) &&
session.id !== currentSessionId && (
<CircleNotch
className="h-4 w-4 shrink-0 animate-spin text-zinc-400"
weight="bold"
/>
<PulseLoader size={8} className="shrink-0" />
)}
{completedSessionIDs.has(session.id) &&
session.id !== currentSessionId && (

View File

@@ -0,0 +1,39 @@
.loader {
position: relative;
display: inline-block;
flex-shrink: 0;
}
.loader::before,
.loader::after {
content: "";
box-sizing: border-box;
width: 100%;
height: 100%;
border-radius: 50%;
background: currentColor;
position: absolute;
left: 0;
top: 0;
transform: scale(0);
opacity: 0;
animation: ripple 2s linear infinite;
}
.loader::after {
animation-delay: 1s;
}
@keyframes ripple {
0% {
transform: scale(0);
opacity: 0.6;
}
50% {
opacity: 0.3;
}
100% {
transform: scale(1);
opacity: 0;
}
}

View File

@@ -0,0 +1,16 @@
import { cn } from "@/lib/utils";
import styles from "./PulseLoader.module.css";
interface Props {
size?: number;
className?: string;
}
export function PulseLoader({ size = 24, className }: Props) {
return (
<div
className={cn(styles.loader, className)}
style={{ width: size, height: size }}
/>
);
}

View File

@@ -1,114 +0,0 @@
"use client";
import { Button } from "@/components/atoms/Button/Button";
import { Text } from "@/components/atoms/Text/Text";
import { Dialog } from "@/components/molecules/Dialog/Dialog";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { useResetRateLimit } from "../../hooks/useResetRateLimit";
interface Props {
isOpen: boolean;
onClose: () => void;
resetCost: number;
resetMessage: string;
isWeeklyExhausted?: boolean;
hasInsufficientCredits?: boolean;
isBillingEnabled?: boolean;
onCreditChange?: () => void;
}
function formatCents(cents: number): string {
return `$${(cents / 100).toFixed(2)}`;
}
export function RateLimitResetDialog({
isOpen,
onClose,
resetCost,
resetMessage,
isWeeklyExhausted = false,
hasInsufficientCredits = false,
isBillingEnabled = false,
onCreditChange,
}: Props) {
const { resetUsage, isPending } = useResetRateLimit({
onSuccess: onClose,
onCreditChange,
});
const router = useRouter();
// Refresh the credit balance each time the dialog opens so we never
// block a valid reset due to a stale client-side balance.
useEffect(() => {
if (isOpen) onCreditChange?.();
}, [isOpen]); // eslint-disable-line react-hooks/exhaustive-deps
// Whether to hide the reset button entirely
const cannotReset = isWeeklyExhausted || hasInsufficientCredits;
return (
<Dialog
title="Usage limit reached"
styling={{ maxWidth: "28rem", minWidth: "auto" }}
controlled={{
isOpen,
set: async (open) => {
if (!open) onClose();
},
}}
>
<Dialog.Content>
<div className="flex flex-col gap-3">
<Text variant="body">{resetMessage}</Text>
{isWeeklyExhausted ? (
<Text variant="body">
Your weekly limit is also reached, so resetting the daily limit
won&apos;t help. Please wait for your limits to reset.
</Text>
) : hasInsufficientCredits ? (
<Text variant="body">
You don&apos;t have enough credits to reset your daily limit.
{isBillingEnabled
? " Add credits to continue working."
: " Please wait for your limits to reset."}
</Text>
) : (
<Text variant="body">
You can spend{" "}
<Text variant="body-medium" as="span">
{formatCents(resetCost)}
</Text>{" "}
in credits to reset your daily limit and continue working.
</Text>
)}
</div>
<Dialog.Footer className="!justify-center">
<Button variant="secondary" onClick={onClose} disabled={isPending}>
{cannotReset ? "OK" : "Wait for reset"}
</Button>
{hasInsufficientCredits && isBillingEnabled && (
<Button
variant="primary"
onClick={() => {
onClose();
router.push("/profile/credits");
}}
>
Add credits
</Button>
)}
{!cannotReset && (
<Button
variant="primary"
onClick={() => resetUsage()}
loading={isPending}
>
Reset for {formatCents(resetCost)}
</Button>
)}
</Dialog.Footer>
</Dialog.Content>
</Dialog>
);
}

View File

@@ -1,7 +1,5 @@
import type { CoPilotUsageStatus } from "@/app/api/__generated__/models/coPilotUsageStatus";
import { useGetV2GetCopilotUsage } from "@/app/api/__generated__/endpoints/chat/chat";
import useCredits from "@/hooks/useCredits";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import {
Popover,
PopoverContent,
@@ -22,12 +20,6 @@ export function UsageLimits() {
},
});
const isBillingEnabled = useGetFlag(Flag.ENABLE_PLATFORM_PAYMENT);
const { credits, fetchCredits } = useCredits({ fetchInitialCredits: true });
const resetCost = usage?.reset_cost;
const hasInsufficientCredits =
credits !== null && resetCost != null && credits < resetCost;
if (isLoading || !usage?.daily || !usage?.weekly) return null;
if (usage.daily.limit <= 0 && usage.weekly.limit <= 0) return null;
@@ -39,12 +31,7 @@ export function UsageLimits() {
</Button>
</PopoverTrigger>
<PopoverContent align="start" className="w-64 p-3">
<UsagePanelContent
usage={usage}
hasInsufficientCredits={hasInsufficientCredits}
isBillingEnabled={isBillingEnabled}
onCreditChange={fetchCredits}
/>
<UsagePanelContent usage={usage} />
</PopoverContent>
</Popover>
);

View File

@@ -1,7 +1,5 @@
import type { CoPilotUsageStatus } from "@/app/api/__generated__/models/coPilotUsageStatus";
import { Button } from "@/components/atoms/Button/Button";
import Link from "next/link";
import { useResetRateLimit } from "../../hooks/useResetRateLimit";
export function formatResetTime(
resetsAt: Date | string,
@@ -72,50 +70,15 @@ function UsageBar({
);
}
function ResetButton({
cost,
onCreditChange,
}: {
cost: number;
onCreditChange?: () => void;
}) {
const { resetUsage, isPending } = useResetRateLimit({ onCreditChange });
return (
<Button
variant="primary"
size="small"
onClick={() => resetUsage()}
loading={isPending}
className="mt-1 w-full text-[11px]"
>
{isPending
? "Resetting..."
: `Reset daily limit for $${(cost / 100).toFixed(2)}`}
</Button>
);
}
export function UsagePanelContent({
usage,
showBillingLink = true,
hasInsufficientCredits = false,
isBillingEnabled = false,
onCreditChange,
}: {
usage: CoPilotUsageStatus;
showBillingLink?: boolean;
hasInsufficientCredits?: boolean;
isBillingEnabled?: boolean;
onCreditChange?: () => void;
}) {
const hasDailyLimit = usage.daily.limit > 0;
const hasWeeklyLimit = usage.weekly.limit > 0;
const isDailyExhausted =
hasDailyLimit && usage.daily.used >= usage.daily.limit;
const isWeeklyExhausted =
hasWeeklyLimit && usage.weekly.used >= usage.weekly.limit;
const resetCost = usage.reset_cost ?? 0;
if (!hasDailyLimit && !hasWeeklyLimit) {
return (
@@ -142,23 +105,6 @@ export function UsagePanelContent({
resetsAt={usage.weekly.resets_at}
/>
)}
{isDailyExhausted &&
!isWeeklyExhausted &&
resetCost > 0 &&
!hasInsufficientCredits && (
<ResetButton cost={resetCost} onCreditChange={onCreditChange} />
)}
{isDailyExhausted &&
!isWeeklyExhausted &&
hasInsufficientCredits &&
isBillingEnabled && (
<Link
href="/profile/credits"
className="mt-1 inline-flex w-full items-center justify-center rounded-md bg-primary px-3 py-1.5 text-[11px] font-medium text-primary-foreground hover:bg-primary/90"
>
Add credits to reset
</Link>
)}
{showBillingLink && (
<Link
href="/profile/credits"

View File

@@ -1,24 +1,5 @@
import type { UIMessage } from "ai";
/**
* Check whether a refetchSession result indicates the backend still has an
* active SSE stream for this session.
*/
export function hasActiveBackendStream(result: { data?: unknown }): boolean {
const d = result.data;
return (
d != null &&
typeof d === "object" &&
"status" in d &&
d.status === 200 &&
"data" in d &&
d.data != null &&
typeof d.data === "object" &&
"active_stream" in d.data &&
!!d.data.active_stream
);
}
/** Mark any in-progress tool parts as completed/errored so spinners stop. */
export function resolveInProgressTools(
messages: UIMessage[],

View File

@@ -1,48 +0,0 @@
import {
usePostV2ResetCopilotUsage,
getGetV2GetCopilotUsageQueryKey,
} from "@/app/api/__generated__/endpoints/chat/chat";
import { toast } from "@/components/molecules/Toast/use-toast";
import { ApiError } from "@/lib/autogpt-server-api";
import { useQueryClient } from "@tanstack/react-query";
export function useResetRateLimit(options?: {
onSuccess?: () => void;
onCreditChange?: () => void;
}) {
const queryClient = useQueryClient();
const { mutate: resetUsage, isPending } = usePostV2ResetCopilotUsage({
mutation: {
onSuccess: async () => {
// Await the usage refetch so the UI shows updated limits before
// closing the dialog or re-enabling the reset CTA.
// invalidateQueries already triggers a refetch for active queries.
await queryClient.invalidateQueries({
queryKey: getGetV2GetCopilotUsageQueryKey(),
});
options?.onCreditChange?.();
toast({
title: "Rate limit reset",
description:
"Your daily usage limit has been reset. You can continue working.",
});
options?.onSuccess?.();
},
onError: (error: unknown) => {
const message =
error instanceof ApiError
? (error.response?.detail ?? error.message)
: error instanceof Error
? error.message
: "Failed to reset limit.";
toast({
title: "Reset failed",
description: message,
variant: "destructive",
});
},
},
});
return { resetUsage, isPending };
}

View File

@@ -54,10 +54,7 @@ export function useCopilotPage() {
status,
error,
isReconnecting,
isSyncing,
isUserStoppingRef,
rateLimitMessage,
dismissRateLimit,
} = useCopilotStream({
sessionId,
hydratedMessages,
@@ -352,7 +349,6 @@ export function useCopilotPage() {
error,
stop,
isReconnecting,
isSyncing,
isLoadingSession,
isSessionError,
isCreatingSession,
@@ -377,8 +373,5 @@ export function useCopilotPage() {
handleDeleteClick,
handleConfirmDelete,
handleCancelDelete,
// Rate limit reset
rateLimitMessage,
dismissRateLimit,
};
}

View File

@@ -10,19 +10,12 @@ import { useChat } from "@ai-sdk/react";
import { useQueryClient } from "@tanstack/react-query";
import { DefaultChatTransport } from "ai";
import type { FileUIPart, UIMessage } from "ai";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
deduplicateMessages,
hasActiveBackendStream,
resolveInProgressTools,
} from "./helpers";
import { useEffect, useMemo, useRef, useState } from "react";
import { deduplicateMessages, resolveInProgressTools } from "./helpers";
const RECONNECT_BASE_DELAY_MS = 1_000;
const RECONNECT_MAX_ATTEMPTS = 3;
/** Minimum time the page must have been hidden to trigger a wake re-sync. */
const WAKE_RESYNC_THRESHOLD_MS = 30_000;
/** Fetch a fresh JWT for direct backend requests (same pattern as WebSocket). */
async function getAuthHeaders(): Promise<Record<string, string>> {
const { token, error } = await getWebSocketToken();
@@ -47,8 +40,6 @@ export function useCopilotStream({
refetchSession,
}: UseCopilotStreamArgs) {
const queryClient = useQueryClient();
const [rateLimitMessage, setRateLimitMessage] = useState<string | null>(null);
const dismissRateLimit = useCallback(() => setRateLimitMessage(null), []);
// Connect directly to the Python backend for SSE, bypassing the Next.js
// serverless proxy. This eliminates the Vercel 800s function timeout that
@@ -107,10 +98,6 @@ export function useCopilotStream({
// Must be state (not ref) so that setting it triggers a re-render and
// recomputes `isReconnecting`.
const [reconnectExhausted, setReconnectExhausted] = useState(false);
// True while performing a wake re-sync (blocks chat input).
const [isSyncing, setIsSyncing] = useState(false);
// Tracks the last time the page was hidden — used to detect sleep/wake gaps.
const lastHiddenAtRef = useRef(Date.now());
function handleReconnect(sid: string) {
if (isReconnectScheduledRef.current || !sid) return;
@@ -172,7 +159,19 @@ export function useCopilotStream({
// unnecessary reconnect cycles.
await new Promise((r) => setTimeout(r, 500));
const result = await refetchSession();
if (hasActiveBackendStream(result)) {
const d = result.data;
const backendActive =
d != null &&
typeof d === "object" &&
"status" in d &&
d.status === 200 &&
"data" in d &&
d.data != null &&
typeof d.data === "object" &&
"active_stream" in d.data &&
!!d.data.active_stream;
if (backendActive) {
handleReconnect(sessionId);
}
},
@@ -198,10 +197,13 @@ export function useCopilotStream({
}
const isRateLimited = errorDetail.toLowerCase().includes("usage limit");
if (isRateLimited) {
setRateLimitMessage(
errorDetail ||
toast({
title: "Usage limit reached",
description:
errorDetail ||
"You've reached your usage limit. Please try again later.",
);
variant: "destructive",
});
return;
}
@@ -296,67 +298,6 @@ export function useCopilotStream({
}
}
// Keep a ref to sessionId so the async wake handler can detect staleness.
const sessionIdRef = useRef(sessionId);
sessionIdRef.current = sessionId;
// ---------------------------------------------------------------------------
// Wake detection: when the page becomes visible after being hidden for >30s
// (device sleep, tab backgrounded for a long time), refetch the session to
// pick up any messages the backend produced while the SSE was dead.
// ---------------------------------------------------------------------------
useEffect(() => {
async function handleWakeResync() {
const sid = sessionIdRef.current;
if (!sid) return;
const elapsed = Date.now() - lastHiddenAtRef.current;
lastHiddenAtRef.current = Date.now();
if (document.visibilityState !== "visible") return;
if (elapsed < WAKE_RESYNC_THRESHOLD_MS) return;
setIsSyncing(true);
try {
const result = await refetchSession();
// Bail out if the session changed while the refetch was in flight.
if (sessionIdRef.current !== sid) return;
if (hasActiveBackendStream(result)) {
// Stream is still running — resume SSE to pick up live chunks.
// Remove stale in-progress assistant message first (backend replays
// from "0-0").
setMessages((prev) => {
if (prev.length > 0 && prev[prev.length - 1].role === "assistant") {
return prev.slice(0, -1);
}
return prev;
});
await resumeStream();
}
// If !backendActive, the refetch will update hydratedMessages via
// React Query, and the hydration effect below will merge them in.
} catch (err) {
console.warn("[copilot] wake re-sync failed", err);
} finally {
setIsSyncing(false);
}
}
function onVisibilityChange() {
if (document.visibilityState === "hidden") {
lastHiddenAtRef.current = Date.now();
} else {
handleWakeResync();
}
}
document.addEventListener("visibilitychange", onVisibilityChange);
return () => {
document.removeEventListener("visibilitychange", onVisibilityChange);
};
}, [refetchSession, setMessages, resumeStream]);
// Hydrate messages from REST API when not actively streaming
useEffect(() => {
if (!hydratedMessages || hydratedMessages.length === 0) return;
@@ -378,11 +319,9 @@ export function useCopilotStream({
reconnectAttemptsRef.current = 0;
isReconnectScheduledRef.current = false;
setIsReconnectScheduled(false);
setRateLimitMessage(null);
hasShownDisconnectToast.current = false;
isUserStoppingRef.current = false;
setReconnectExhausted(false);
setIsSyncing(false);
hasResumedRef.current.clear();
return () => {
clearTimeout(reconnectTimerRef.current);
@@ -485,9 +424,6 @@ export function useCopilotStream({
status,
error: isReconnecting || isUserStoppingRef.current ? undefined : error,
isReconnecting,
isSyncing,
isUserStoppingRef,
rateLimitMessage,
dismissRateLimit,
};
}

View File

@@ -79,10 +79,7 @@ export function StoreCard({
/>
</>
) : (
<div
className="absolute inset-0 rounded-xl"
style={{ backgroundColor: "rgb(216, 208, 255)" }}
/>
<div className="absolute inset-0 rounded-xl bg-violet-50" />
)}
</div>
@@ -116,7 +113,7 @@ export function StoreCard({
{/* Third Section: Description */}
<div className="mt-2.5 flex w-full flex-col">
<Text variant="body" className="line-clamp-3 leading-normal">
<Text variant="body" className="line-clamp-2 leading-normal">
{description}
</Text>
</div>

View File

@@ -1,15 +0,0 @@
/**
* Generated by orval v7.13.0 🍺
* Do not edit manually.
* AutoGPT Agent Server
* This server is used to execute agents that are created by the AutoGPT system.
* OpenAPI spec version: 0.1
*/
import type { SuggestedTheme } from "./suggestedTheme";
/**
* Response model for user-specific suggested prompts grouped by theme.
*/
export interface SuggestedPromptsResponse {
themes: SuggestedTheme[];
}

View File

@@ -1,15 +0,0 @@
/**
* Generated by orval v7.13.0 🍺
* Do not edit manually.
* AutoGPT Agent Server
* This server is used to execute agents that are created by the AutoGPT system.
* OpenAPI spec version: 0.1
*/
/**
* A themed group of suggested prompts.
*/
export interface SuggestedTheme {
name: string;
prompts: string[];
}

View File

@@ -1358,35 +1358,11 @@
}
}
},
"/api/chat/suggested-prompts": {
"get": {
"tags": ["v2", "chat", "chat"],
"summary": "Get Suggested Prompts",
"description": "Get LLM-generated suggested prompts grouped by theme.\n\nReturns personalized quick-action prompts based on the user's\nbusiness understanding. Returns empty themes list if no custom\nprompts are available.",
"operationId": "getV2GetSuggestedPrompts",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SuggestedPromptsResponse"
}
}
}
},
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
}
},
"security": [{ "HTTPBearerJWT": [] }]
}
},
"/api/chat/usage": {
"get": {
"tags": ["v2", "chat", "chat"],
"summary": "Get Copilot Usage",
"description": "Get CoPilot usage status for the authenticated user.\n\nReturns current token usage vs limits for daily and weekly windows.\nGlobal defaults sourced from LaunchDarkly (falling back to config).",
"description": "Get CoPilot usage status for the authenticated user.\n\nReturns current token usage vs limits for daily and weekly windows.",
"operationId": "getV2GetCopilotUsage",
"responses": {
"200": {
@@ -1404,134 +1380,6 @@
"security": [{ "HTTPBearerJWT": [] }]
}
},
"/api/chat/usage/reset": {
"post": {
"tags": ["v2", "chat", "chat"],
"summary": "Reset Copilot Usage",
"description": "Reset the daily CoPilot rate limit by spending credits.\n\nAllows users who have hit their daily token limit to spend credits\nto reset their daily usage counter and continue working.\nReturns 400 if the feature is disabled or the user is not over the limit.\nReturns 402 if the user has insufficient credits.",
"operationId": "postV2ResetCopilotUsage",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/RateLimitResetResponse"
}
}
}
},
"400": {
"description": "Bad Request (feature disabled or daily limit not reached)"
},
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
},
"402": { "description": "Payment Required (insufficient credits)" },
"429": {
"description": "Too Many Requests (max daily resets exceeded or reset in progress)"
},
"503": {
"description": "Service Unavailable (Redis reset failed; credits refunded or support needed)"
}
},
"security": [{ "HTTPBearerJWT": [] }]
}
},
"/api/copilot/admin/rate_limit": {
"get": {
"tags": ["v2", "admin", "copilot", "admin"],
"summary": "Get User Rate Limit",
"description": "Get a user's current usage and effective rate limits. Admin-only.\n\nAccepts either ``user_id`` or ``email`` as a query parameter.\nWhen ``email`` is provided the user is looked up by email first.",
"operationId": "getV2Get user rate limit",
"security": [{ "HTTPBearerJWT": [] }],
"parameters": [
{
"name": "user_id",
"in": "query",
"required": false,
"schema": {
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "User Id"
}
},
{
"name": "email",
"in": "query",
"required": false,
"schema": {
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Email"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserRateLimitResponse"
}
}
}
},
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/api/copilot/admin/rate_limit/reset": {
"post": {
"tags": ["v2", "admin", "copilot", "admin"],
"summary": "Reset User Rate Limit Usage",
"description": "Reset a user's daily usage counter (and optionally weekly). Admin-only.",
"operationId": "postV2Reset user rate limit usage",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Body_postV2Reset_user_rate_limit_usage"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserRateLimitResponse"
}
}
}
},
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
},
"security": [{ "HTTPBearerJWT": [] }]
}
},
"/api/credits": {
"get": {
"tags": ["v1", "credits"],
@@ -8283,19 +8131,6 @@
"type": "object",
"title": "Body_postV2Execute a preset"
},
"Body_postV2Reset_user_rate_limit_usage": {
"properties": {
"user_id": { "type": "string", "title": "User Id" },
"reset_weekly": {
"type": "boolean",
"title": "Reset Weekly",
"default": false
}
},
"type": "object",
"required": ["user_id"],
"title": "Body_postV2Reset user rate limit usage"
},
"Body_postV2Upload_submission_media": {
"properties": {
"file": { "type": "string", "format": "binary", "title": "File" }
@@ -8414,13 +8249,7 @@
"CoPilotUsageStatus": {
"properties": {
"daily": { "$ref": "#/components/schemas/UsageWindow" },
"weekly": { "$ref": "#/components/schemas/UsageWindow" },
"reset_cost": {
"type": "integer",
"title": "Reset Cost",
"description": "Credit cost (in cents) to reset the daily limit. 0 = feature disabled.",
"default": 0
}
"weekly": { "$ref": "#/components/schemas/UsageWindow" }
},
"type": "object",
"required": ["daily", "weekly"],
@@ -11729,34 +11558,6 @@
"required": ["providers", "pagination"],
"title": "ProviderResponse"
},
"RateLimitResetResponse": {
"properties": {
"success": { "type": "boolean", "title": "Success" },
"credits_charged": {
"type": "integer",
"title": "Credits Charged",
"description": "Credits charged (in cents)"
},
"remaining_balance": {
"type": "integer",
"title": "Remaining Balance",
"description": "Credit balance after charge (in cents)"
},
"usage": {
"$ref": "#/components/schemas/CoPilotUsageStatus",
"description": "Updated usage status after reset"
}
},
"type": "object",
"required": [
"success",
"credits_charged",
"remaining_balance",
"usage"
],
"title": "RateLimitResetResponse",
"description": "Response from resetting the daily rate limit."
},
"RecentExecution": {
"properties": {
"status": { "type": "string", "title": "Status" },
@@ -12953,33 +12754,6 @@
"title": "SuggestedGoalResponse",
"description": "Response when the goal needs refinement with a suggested alternative."
},
"SuggestedPromptsResponse": {
"properties": {
"themes": {
"items": { "$ref": "#/components/schemas/SuggestedTheme" },
"type": "array",
"title": "Themes"
}
},
"type": "object",
"required": ["themes"],
"title": "SuggestedPromptsResponse",
"description": "Response model for user-specific suggested prompts grouped by theme."
},
"SuggestedTheme": {
"properties": {
"name": { "type": "string", "title": "Name" },
"prompts": {
"items": { "type": "string" },
"type": "array",
"title": "Prompts"
}
},
"type": "object",
"required": ["name", "prompts"],
"title": "SuggestedTheme",
"description": "A themed group of suggested prompts."
},
"SuggestionsResponse": {
"properties": {
"recent_searches": {
@@ -14708,40 +14482,6 @@
"required": ["provider", "username", "password"],
"title": "UserPasswordCredentials"
},
"UserRateLimitResponse": {
"properties": {
"user_id": { "type": "string", "title": "User Id" },
"user_email": {
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "User Email"
},
"daily_token_limit": {
"type": "integer",
"title": "Daily Token Limit"
},
"weekly_token_limit": {
"type": "integer",
"title": "Weekly Token Limit"
},
"daily_tokens_used": {
"type": "integer",
"title": "Daily Tokens Used"
},
"weekly_tokens_used": {
"type": "integer",
"title": "Weekly Tokens Used"
}
},
"type": "object",
"required": [
"user_id",
"daily_token_limit",
"weekly_token_limit",
"daily_tokens_used",
"weekly_tokens_used"
],
"title": "UserRateLimitResponse"
},
"UserReadiness": {
"properties": {
"has_all_credentials": {

View File

@@ -16,13 +16,19 @@ export default function ArrayFieldItemTemplate(
);
return (
<div className="mb-4 flex flex-col">
<div className="w-full">{children}</div>
{hasToolbar && (
<div className="-mt-2 flex justify-start gap-2">
<ArrayFieldItemButtonsTemplate {...buttonsProps} />
<div>
<div className="mb-2 flex flex-row flex-wrap items-center">
<div className="shrink grow">
<div className="shrink grow">{children}</div>
</div>
)}
<div className="flex items-end justify-end">
{hasToolbar && (
<div className="-mt-4 mb-2 flex gap-2">
<ArrayFieldItemButtonsTemplate {...buttonsProps} />
</div>
)}
</div>
</div>
</div>
);
}

View File

@@ -80,6 +80,11 @@ Root (type: object)
│ └── FieldTemplate → AnyOfField
│ └── String → TextWidget OR Null → nothing
├── placeholder_values (array of strings)
│ └── FieldTemplate → ArrayFieldTemplate
│ └── ArrayFieldItemTemplate (per item)
│ └── TextWidget
├── advanced (boolean)
│ └── FieldTemplate → CheckboxWidget

View File

@@ -1,126 +0,0 @@
import type { Meta, StoryObj } from "@storybook/nextjs";
import { FormRendererStory, storyDecorator } from "./FormRendererStoryWrapper";
const meta: Meta = {
title: "Renderers/FormRenderer/Array Fields",
tags: ["autodocs"],
decorators: [storyDecorator],
parameters: {
layout: "centered",
docs: {
description: {
component:
"Array field types: list[str], list[int], list[Enum], list[bool], and list[object].",
},
},
},
};
export default meta;
type Story = StoryObj<typeof meta>;
export const ListOfStrings: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
tags: {
type: "array",
title: "Tags",
items: { type: "string" },
description: "list[str] - A list of text items",
},
},
}}
initialValues={{ tags: ["tag1", "tag2"] }}
/>
),
};
export const ListOfIntegers: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
numbers: {
type: "array",
title: "Numbers",
items: { type: "integer" },
description: "list[int] - A list of integers",
},
},
}}
initialValues={{ numbers: [1, 2, 3] }}
/>
),
};
export const ListOfEnums: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
formats: {
type: "array",
title: "Formats",
items: {
type: "string",
enum: ["markdown", "html", "screenshot", "rawHtml", "links"],
},
description: "list[Enum] - e.g. Firecrawl ScrapeFormat",
},
},
}}
initialValues={{ formats: ["markdown", "screenshot"] }}
/>
),
};
export const ListOfBooleans: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
flags: {
type: "array",
title: "Flags",
items: { type: "boolean" },
description: "list[bool] - A list of boolean flags",
},
},
}}
initialValues={{ flags: [true, false] }}
/>
),
};
export const ListOfObjects: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
headers: {
type: "array",
title: "Headers",
items: {
type: "object",
properties: {
key: { type: "string", title: "Key" },
value: { type: "string", title: "Value" },
},
},
description: "list[dict] - Key-value pairs",
},
},
}}
initialValues={{
headers: [{ key: "Authorization", value: "Bearer token" }],
}}
/>
),
};

View File

@@ -1,593 +0,0 @@
import type { Meta, StoryObj } from "@storybook/nextjs";
import type { RJSFSchema } from "@rjsf/utils";
import { FormRendererStory, storyDecorator } from "./FormRendererStoryWrapper";
const meta: Meta = {
title: "Renderers/FormRenderer/Complex Schemas",
tags: ["autodocs"],
decorators: [storyDecorator],
parameters: {
layout: "centered",
docs: {
description: {
component:
"Complex schemas: nested objects, unions/anyOf, oneOf discriminated unions, multi-select, required fields, and kitchen sink.",
},
},
},
};
export default meta;
type Story = StoryObj<typeof meta>;
// --- Object / Nested Types ---
export const ObjectField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
config: {
type: "object",
title: "Config",
properties: {
host: { type: "string", title: "Host" },
port: { type: "integer", title: "Port" },
ssl: { type: "boolean", title: "SSL" },
},
description: "A nested object with multiple fields",
},
},
}}
/>
),
};
export const NestedObjectWithEnum: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
settings: {
type: "object",
title: "Settings",
properties: {
mode: {
type: "string",
title: "Mode",
enum: ["fast", "balanced", "quality"],
},
max_retries: { type: "integer", title: "Max Retries" },
verbose: { type: "boolean", title: "Verbose" },
},
},
},
}}
/>
),
};
// --- Optional / AnyOf ---
export const OptionalString: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
nickname: {
anyOf: [{ type: "string" }, { type: "null" }],
title: "Nickname",
description: "Optional[str] - can be a string or null",
},
},
}}
/>
),
};
export const OptionalInteger: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
max_tokens: {
anyOf: [{ type: "integer" }, { type: "null" }],
title: "Max Tokens",
description: "Optional[int] - can be an integer or null",
},
},
}}
/>
),
};
// --- Union / AnyOf (multiple types) ---
export const UnionStringOrInteger: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
value: {
anyOf: [{ type: "string" }, { type: "integer" }],
title: "Value",
description: "str | int - Union of string and integer",
},
},
}}
/>
),
};
// --- TwitterGetUserBlock (exact schema from model_json_schema(), minus credentials) ---
// Schema uses custom properties (advanced, secret, placeholder) that the backend
// emits but aren't part of JSONSchema7, so we keep the object untyped and cast
// to RJSFSchema at the component boundary.
const twitterGetUserSchema = {
type: "object",
$defs: {
UserId: {
type: "object",
title: "UserId",
properties: {
discriminator: {
const: "user_id",
title: "Discriminator",
type: "string",
},
user_id: {
advanced: true,
default: "",
description: "The ID of the user to lookup",
secret: false,
title: "User Id",
type: "string",
},
},
required: ["discriminator"],
},
Username: {
type: "object",
title: "Username",
properties: {
discriminator: {
const: "username",
title: "Discriminator",
type: "string",
},
username: {
advanced: true,
default: "",
description: "The Twitter username (handle) of the user",
secret: false,
title: "Username",
type: "string",
},
},
required: ["discriminator"],
},
UserExpansionsFilter: {
type: "object",
title: "UserExpansionsFilter",
properties: {
pinned_tweet_id: {
default: false,
title: "Pinned Tweet Id",
type: "boolean",
},
},
},
TweetFieldsFilter: {
type: "object",
title: "TweetFieldsFilter",
properties: {
Tweet_Attachments: {
default: false,
title: "Tweet Attachments",
type: "boolean",
},
Author_ID: {
default: false,
title: "Author Id",
type: "boolean",
},
Context_Annotations: {
default: false,
title: "Context Annotations",
type: "boolean",
},
Conversation_ID: {
default: false,
title: "Conversation Id",
type: "boolean",
},
Creation_Time: {
default: false,
title: "Creation Time",
type: "boolean",
},
Edit_Controls: {
default: false,
title: "Edit Controls",
type: "boolean",
},
Tweet_Entities: {
default: false,
title: "Tweet Entities",
type: "boolean",
},
Geographic_Location: {
default: false,
title: "Geographic Location",
type: "boolean",
},
Tweet_ID: {
default: false,
title: "Tweet Id",
type: "boolean",
},
Reply_To_User_ID: {
default: false,
title: "Reply To User Id",
type: "boolean",
},
Language: {
default: false,
title: "Language",
type: "boolean",
},
Public_Metrics: {
default: false,
title: "Public Metrics",
type: "boolean",
},
Sensitive_Content_Flag: {
default: false,
title: "Sensitive Content Flag",
type: "boolean",
},
Referenced_Tweets: {
default: false,
title: "Referenced Tweets",
type: "boolean",
},
Reply_Settings: {
default: false,
title: "Reply Settings",
type: "boolean",
},
Tweet_Source: {
default: false,
title: "Tweet Source",
type: "boolean",
},
Tweet_Text: {
default: false,
title: "Tweet Text",
type: "boolean",
},
Withheld_Content: {
default: false,
title: "Withheld Content",
type: "boolean",
},
},
},
TweetUserFieldsFilter: {
type: "object",
title: "TweetUserFieldsFilter",
properties: {
Account_Creation_Date: {
default: false,
title: "Account Creation Date",
type: "boolean",
},
User_Bio: {
default: false,
title: "User Bio",
type: "boolean",
},
User_Entities: {
default: false,
title: "User Entities",
type: "boolean",
},
User_ID: {
default: false,
title: "User Id",
type: "boolean",
},
User_Location: {
default: false,
title: "User Location",
type: "boolean",
},
Latest_Tweet_ID: {
default: false,
title: "Latest Tweet Id",
type: "boolean",
},
Display_Name: {
default: false,
title: "Display Name",
type: "boolean",
},
Pinned_Tweet_ID: {
default: false,
title: "Pinned Tweet Id",
type: "boolean",
},
Profile_Picture_URL: {
default: false,
title: "Profile Picture Url",
type: "boolean",
},
Is_Protected_Account: {
default: false,
title: "Is Protected Account",
type: "boolean",
},
Account_Statistics: {
default: false,
title: "Account Statistics",
type: "boolean",
},
Profile_URL: {
default: false,
title: "Profile Url",
type: "boolean",
},
Username: {
default: false,
title: "Username",
type: "boolean",
},
Is_Verified: {
default: false,
title: "Is Verified",
type: "boolean",
},
Verification_Type: {
default: false,
title: "Verification Type",
type: "boolean",
},
Content_Withholding_Info: {
default: false,
title: "Content Withholding Info",
type: "boolean",
},
},
},
},
required: ["identifier"],
properties: {
identifier: {
advanced: false,
description:
"Choose whether to identify the user by their unique Twitter ID or by their username",
discriminator: {
mapping: {
user_id: "#/$defs/UserId",
username: "#/$defs/Username",
},
propertyName: "discriminator",
},
oneOf: [{ $ref: "#/$defs/UserId" }, { $ref: "#/$defs/Username" }],
secret: false,
title: "Identifier",
},
expansions: {
advanced: true,
anyOf: [{ $ref: "#/$defs/UserExpansionsFilter" }, { type: "null" }],
default: null,
description:
"Choose what extra information you want to get with user data. Currently only 'pinned_tweet_id' is available to see a user's pinned tweet.",
placeholder: "Select extra user information to include",
secret: false,
},
tweet_fields: {
advanced: true,
anyOf: [{ $ref: "#/$defs/TweetFieldsFilter" }, { type: "null" }],
default: null,
description:
"Select what tweet information you want to see in pinned tweets. This only works if you select 'pinned_tweet_id' in expansions above.",
placeholder: "Choose what details to see in pinned tweets",
secret: false,
},
user_fields: {
advanced: true,
anyOf: [{ $ref: "#/$defs/TweetUserFieldsFilter" }, { type: "null" }],
default: null,
description:
"Select what user information you want to see, like username, bio, profile picture, etc.",
placeholder: "Choose what user details you want to see",
secret: false,
},
},
};
export const TwitterGetUserBlock: Story = {
render: () => (
<FormRendererStory
jsonSchema={twitterGetUserSchema as RJSFSchema}
initialValues={{
identifier: { discriminator: "user_id", user_id: "" },
}}
/>
),
};
// --- Multi-select (all-boolean object, exact Twitter TweetFieldsFilter schema) ---
const multiSelectSchema = {
type: "object",
$defs: {
TweetFieldsFilter: {
type: "object",
title: "TweetFieldsFilter",
properties: {
Tweet_Attachments: {
default: false,
title: "Tweet Attachments",
type: "boolean",
},
Author_ID: {
default: false,
title: "Author Id",
type: "boolean",
},
Context_Annotations: {
default: false,
title: "Context Annotations",
type: "boolean",
},
Conversation_ID: {
default: false,
title: "Conversation Id",
type: "boolean",
},
Creation_Time: {
default: false,
title: "Creation Time",
type: "boolean",
},
Tweet_Entities: {
default: false,
title: "Tweet Entities",
type: "boolean",
},
Language: {
default: false,
title: "Language",
type: "boolean",
},
Public_Metrics: {
default: false,
title: "Public Metrics",
type: "boolean",
},
Tweet_Text: {
default: false,
title: "Tweet Text",
type: "boolean",
},
},
},
},
properties: {
tweet_fields: {
anyOf: [{ $ref: "#/$defs/TweetFieldsFilter" }, { type: "null" }],
default: null,
description: "Select what tweet information you want to see.",
placeholder: "Choose what details to see in tweets",
},
},
};
export const MultiSelectField: Story = {
render: () => (
<FormRendererStory jsonSchema={multiSelectSchema as RJSFSchema} />
),
};
// --- Required vs Optional fields ---
export const RequiredFields: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
required: ["email", "role"],
properties: {
email: { type: "string", title: "Email" },
role: {
type: "string",
title: "Role",
enum: ["admin", "editor", "viewer"],
},
bio: { type: "string", title: "Bio" },
},
}}
/>
),
};
// --- Kitchen Sink ---
export const KitchenSink: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
required: ["url", "method"],
properties: {
url: {
type: "string",
title: "URL",
description: "The target URL",
},
method: {
type: "string",
title: "Method",
enum: ["GET", "POST", "PUT", "DELETE", "PATCH"],
},
timeout: {
type: "number",
title: "Timeout (seconds)",
description: "Request timeout",
},
follow_redirects: {
type: "boolean",
title: "Follow Redirects",
},
headers: {
type: "array",
title: "Headers",
items: {
type: "object",
properties: {
key: { type: "string", title: "Key" },
value: { type: "string", title: "Value" },
},
},
},
body_format: {
type: "string",
title: "Body Format",
enum: ["json", "form", "raw", "none"],
},
tags: {
type: "array",
title: "Tags",
items: { type: "string" },
},
auth: {
anyOf: [
{
type: "object",
title: "Bearer Token",
properties: {
token: { type: "string", title: "Token" },
},
},
{ type: "null" },
],
title: "Authentication",
},
},
}}
initialValues={{
method: "GET",
follow_redirects: true,
body_format: "json",
}}
/>
),
};

View File

@@ -1,42 +0,0 @@
import { useState } from "react";
import { TooltipProvider } from "@/components/atoms/Tooltip/BaseTooltip";
import { FormRenderer } from "../FormRenderer";
import type { RJSFSchema } from "@rjsf/utils";
import type { ExtendedFormContextType } from "../types";
const defaultFormContext: ExtendedFormContextType = {
nodeId: "story-node",
showHandles: false,
size: "medium",
showOptionalToggle: true,
};
export function FormRendererStory({
jsonSchema,
initialValues,
}: {
jsonSchema: RJSFSchema;
initialValues?: Record<string, unknown>;
}) {
const [formData, setFormData] = useState(initialValues ?? {});
return (
<div className="w-[400px]">
<FormRenderer
jsonSchema={jsonSchema}
handleChange={(e) => setFormData(e.formData ?? {})}
uiSchema={{}}
initialValues={formData}
formContext={defaultFormContext}
/>
</div>
);
}
export function storyDecorator(Story: React.ComponentType) {
return (
<TooltipProvider>
<Story />
</TooltipProvider>
);
}

View File

@@ -1,214 +0,0 @@
import type { Meta, StoryObj } from "@storybook/nextjs";
import { FormRendererStory, storyDecorator } from "./FormRendererStoryWrapper";
const meta: Meta = {
title: "Renderers/FormRenderer/Primitive Fields",
tags: ["autodocs"],
decorators: [storyDecorator],
parameters: {
layout: "centered",
docs: {
description: {
component:
"Primitive field types: strings, numbers, booleans, enums, and date/time formats.",
},
},
},
};
export default meta;
type Story = StoryObj<typeof meta>;
// --- String ---
export const StringField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
name: { type: "string", title: "Name", description: "Enter a name" },
},
}}
/>
),
};
export const StringWithDefault: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
greeting: {
type: "string",
title: "Greeting",
default: "Hello, world!",
},
},
}}
initialValues={{ greeting: "Hello, world!" }}
/>
),
};
// --- Number ---
export const IntegerField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
count: { type: "integer", title: "Count", description: "A number" },
},
}}
/>
),
};
export const NumberField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
temperature: {
type: "number",
title: "Temperature",
description: "A float value",
},
},
}}
/>
),
};
export const NumberWithConstraints: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
score: {
type: "number",
title: "Score",
minimum: 0,
maximum: 100,
description: "Value between 0 and 100",
},
},
}}
/>
),
};
// --- Boolean ---
export const BooleanField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
enabled: {
type: "boolean",
title: "Enabled",
description: "Toggle this on or off",
},
},
}}
/>
),
};
// --- Enum ---
export const EnumField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
color: {
type: "string",
title: "Color",
enum: ["red", "green", "blue", "yellow"],
},
},
}}
/>
),
};
export const EnumWithDefault: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
priority: {
type: "string",
title: "Priority",
enum: ["low", "medium", "high", "critical"],
default: "medium",
},
},
}}
initialValues={{ priority: "medium" }}
/>
),
};
// --- Date / Time ---
export const DateField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
start_date: {
type: "string",
title: "Start Date",
format: "date",
},
},
}}
/>
),
};
export const TimeField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
alarm_time: {
type: "string",
title: "Alarm Time",
format: "time",
},
},
}}
/>
),
};
export const DateTimeField: Story = {
render: () => (
<FormRendererStory
jsonSchema={{
type: "object",
properties: {
scheduled_at: {
type: "string",
title: "Scheduled At",
format: "date-time",
},
},
}}
/>
),
};

View File

@@ -964,7 +964,6 @@ export type AddUserCreditsResponse = {
new_balance: number;
transaction_key: string;
};
const _stringFormatToDataTypeMap: Partial<Record<string, DataType>> = {
date: DataType.DATE,
time: DataType.TIME,

View File

@@ -342,7 +342,7 @@ Below is a comprehensive list of all available blocks, categorized by their prim
| [Post To X](block-integrations/ayrshare/post_to_x.md#post-to-x) | Post to X / Twitter using Ayrshare |
| [Post To YouTube](block-integrations/ayrshare/post_to_youtube.md#post-to-youtube) | Post to YouTube using Ayrshare |
| [Publish To Medium](block-integrations/misc.md#publish-to-medium) | Publishes a post to Medium |
| [Read Discord Messages](block-integrations/discord/bot_blocks.md#read-discord-messages) | Reads new messages from a Discord channel using a bot token and triggers when a new message is posted |
| [Read Discord Messages](block-integrations/discord/bot_blocks.md#read-discord-messages) | Reads messages from a Discord channel using a bot token |
| [Reddit Get My Posts](block-integrations/misc.md#reddit-get-my-posts) | Fetch posts created by the authenticated Reddit user (you) |
| [Reply To Discord Message](block-integrations/discord/bot_blocks.md#reply-to-discord-message) | Replies to a specific Discord message |
| [Reply To Reddit Comment](block-integrations/misc.md#reply-to-reddit-comment) | Reply to a specific Reddit comment |

View File

@@ -182,9 +182,9 @@ This is ideal when you want to constrain user input to a predefined set of choic
| value | Text selected from a dropdown. | str | No |
| title | The title of the input. | str | No |
| description | The description of the input. | str | No |
| placeholder_values | Possible values for the dropdown. | List[Any] | No |
| advanced | Whether to show the input in the advanced section, if the field is not required. | bool | No |
| secret | Whether the input should be treated as a secret. | bool | No |
| placeholder_values | Possible values for the dropdown. | List[Any] | No |
### Outputs
@@ -293,7 +293,7 @@ A block that accepts and processes user input values within a workflow, supporti
### How it works
<!-- MANUAL: how_it_works -->
It accepts a value from the user, along with metadata such as name and description. The block then outputs the provided value.
It accepts a value from the user, along with metadata such as name, description, and optional placeholder values. The block then outputs the provided value.
<!-- END MANUAL -->
### Inputs

View File

@@ -1,7 +1,7 @@
## Read Discord Messages
### What it is
A block that reads new messages from a Discord channel using a bot token and triggers when a new message is posted.
A block that reads messages from a Discord channel using a bot token.
### What it does
This block connects to Discord using a bot token and retrieves messages from a specified channel. It can operate continuously or retrieve a single message.

View File

@@ -132,7 +132,7 @@ The user must be visible to your bot (share a server with your bot).
## Read Discord Messages
### What it is
Reads new messages from a Discord channel using a bot token and triggers when a new message is posted
Reads messages from a Discord channel using a bot token.
### How it works
<!-- MANUAL: how_it_works -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

View File

@@ -1,67 +0,0 @@
# Test Report: PR #12577 - feat/admin-rate-limit-management
**Date:** 2026-03-27
**Tester:** Automated (Claude)
**Test user:** test@test.com (non-admin)
**Backend:** http://localhost:8006
**Frontend:** http://localhost:3000
## Summary
All tests PASSED. The PR correctly implements admin-only rate limit management endpoints and UI, with proper authorization gating.
## Test Results
### API Endpoint Tests
| # | Test | Expected | Actual | Status |
|---|------|----------|--------|--------|
| 1 | GET `/api/copilot/admin/rate_limit?user_id=test-user` (non-admin) | 403 | 403 `{"detail":"Admin access required"}` | PASS |
| 2 | POST `/api/copilot/admin/rate_limit/reset` (non-admin) | 403 | 403 `{"detail":"Admin access required"}` | PASS |
| 3 | OpenAPI spec includes new endpoints | Endpoints listed | Found 3 endpoints: `/api/copilot/admin/rate_limit`, `/api/copilot/admin/rate_limit/reset`, `/api/copilot/admin/rate_limit/tier` | PASS |
| 4 | GET `/api/copilot/admin/rate_limit` without user_id (non-admin) | 403 (auth check before validation) | 403 `{"detail":"Admin access required"}` | PASS |
| 5 | GET `/api/copilot/admin/rate_limit` without auth header | 401 | 401 `{"detail":"Authorization header is missing"}` | PASS |
| 6 | OpenAPI spec endpoint details | Summaries and params present | GET supports `user_id` and `email` params; POST reset takes `user_id` + `reset_weekly` body | PASS |
| 7 | GET `/api/copilot/admin/rate_limit?email=test@test.com` (non-admin) | 403 | 403 `{"detail":"Admin access required"}` | PASS |
| 8 | POST reset with missing user_id (non-admin) | 403 (auth check before validation) | 403 `{"detail":"Admin access required"}` | PASS |
| 9 | GET `/api/copilot/admin/rate_limit/tier` (non-admin) | 403 | 403 `{"detail":"Admin access required"}` | PASS |
| 10 | POST `/api/copilot/admin/rate_limit/tier` (non-admin) | 403 | 403 `{"detail":"Admin access required"}` | PASS |
### Frontend Tests
| # | Test | Expected | Actual | Status |
|---|------|----------|--------|--------|
| 11 | Navigate to `/admin/rate-limits` as non-admin | Redirect away | Redirected to `/copilot` | PASS |
| 12 | Admin sidebar includes Rate Limits link | Link present in layout | `layout.tsx` contains `{ text: "Rate Limits", href: "/admin/rate-limits" }` | PASS |
| 13 | Rate limits page uses `withRoleAccess(["admin"])` | Admin-only gating | Confirmed in `page.tsx` | PASS |
### OpenAPI Schema Verification
**Endpoints discovered (3 total, exceeding the 2 mentioned in PR):**
1. **GET `/api/copilot/admin/rate_limit`** - "Get User Rate Limit"
- Query params: `user_id`, `email` (lookup by either)
2. **POST `/api/copilot/admin/rate_limit/reset`** - "Reset User Rate Limit Usage"
- Body: `{ user_id: string (required), reset_weekly: boolean (default: false) }`
3. **GET/POST `/api/copilot/admin/rate_limit/tier`** - "Get/Set User Rate Limit Tier"
- GET params: `user_id`
- POST body: `{ user_id: string, tier: SubscriptionTier }`
## Observations
1. **Authorization is enforced before input validation** - all endpoints return 403 before checking query params or body, which is the correct security pattern (prevents information leakage about valid/invalid inputs).
2. **Consistent error messages** - all admin endpoints return `{"detail":"Admin access required"}` for non-admin users.
3. **Unauthenticated requests** return 401 with `{"detail":"Authorization header is missing"}` - correct separation of authn vs authz.
4. **The PR includes a bonus tier endpoint** (`/api/copilot/admin/rate_limit/tier`) not mentioned in the original PR description but visible in the OpenAPI spec.
5. **Frontend properly gated** - the admin page uses server-side `withRoleAccess(["admin"])` and redirects non-admin users.
6. **Admin layout updated** - includes "Rate Limits" link with Gauge icon in sidebar navigation.
## Screenshots
- `01-login-page.png` - User already logged in, showing Build page
- `02-admin-rate-limits-redirect.png` - After navigating to /admin/rate-limits, redirected to main page
- `03-admin-redirect-confirmed.png` - Confirmed redirect to /copilot for non-admin user
## Verdict
**PASS** - All authorization gates work correctly. Endpoints exist in OpenAPI spec with proper schemas. Non-admin users are denied at both API and UI levels. No issues found.