From 4706d91bd7f7ce4d5ac09860b030bb6deec8b80d Mon Sep 17 00:00:00 2001 From: Chenyme <118253778+chenyme@users.noreply.github.com> Date: Sun, 1 Mar 2026 16:54:19 +0800 Subject: [PATCH] chore: bump version to 1.5.4 and update API key description in configuration files --- app/core/auth.py | 28 +++++++- app/services/grok/services/chat.py | 14 +++- app/services/token/manager.py | 94 ++++++++++++++++++++++++++- app/static/admin/js/config.js | 2 +- app/static/admin/pages/cache.html | 14 ++-- app/static/admin/pages/config.html | 12 ++-- app/static/admin/pages/login.html | 10 +-- app/static/admin/pages/token.html | 14 ++-- app/static/common/js/footer.js | 2 +- app/static/common/js/header.js | 2 +- app/static/common/js/public-header.js | 2 +- app/static/public/css/chat.css | 30 ++++++--- app/static/public/pages/chat.html | 12 ++-- app/static/public/pages/imagine.html | 12 ++-- app/static/public/pages/login.html | 10 +-- app/static/public/pages/video.html | 12 ++-- app/static/public/pages/voice.html | 12 ++-- config.defaults.toml | 2 +- docs/README.en.md | 2 +- pyproject.toml | 2 +- readme.md | 2 +- uv.lock | 2 +- 22 files changed, 215 insertions(+), 77 deletions(-) diff --git a/app/core/auth.py b/app/core/auth.py index e2e31297..e6258f0d 100644 --- a/app/core/auth.py +++ b/app/core/auth.py @@ -3,7 +3,7 @@ API 认证模块 """ import hashlib -from typing import Optional +from typing import Optional, Iterable from fastapi import HTTPException, status, Security from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials @@ -31,6 +31,27 @@ def get_admin_api_key() -> str: api_key = get_config("app.api_key", DEFAULT_API_KEY) return api_key or "" + +def _normalize_api_keys(value: Optional[object]) -> list[str]: + if not value: + return [] + if isinstance(value, str): + raw = value.strip() + if not raw: + return [] + return [part.strip() for part in raw.split(",") if part.strip()] + if isinstance(value, Iterable): + keys: list[str] = [] + for item in value: + if not item: + continue + if isinstance(item, str): + stripped = item.strip() + if stripped: + keys.append(stripped) + return keys + return [] + def get_app_key() -> str: """ 获取 App Key(后台管理密码)。 @@ -84,7 +105,8 @@ async def verify_api_key( 如果 config.toml 中未配置 api_key,则不启用认证。 """ api_key = get_admin_api_key() - if not api_key: + api_keys = _normalize_api_keys(api_key) + if not api_keys: return None if not auth: @@ -95,7 +117,7 @@ async def verify_api_key( ) # 标准 api_key 验证 - if auth.credentials == api_key: + if auth.credentials in api_keys: return auth.credentials raise HTTPException( diff --git a/app/services/grok/services/chat.py b/app/services/grok/services/chat.py index e54b7a07..a8086531 100644 --- a/app/services/grok/services/chat.py +++ b/app/services/grok/services/chat.py @@ -529,6 +529,16 @@ class StreamProcessor(proc_base.BaseProcessor): self._tool_buffer = "" self._tool_partial = "" self._tool_calls_seen = False + self._tool_call_index = 0 + + def _with_tool_index(self, tool_call: Any) -> Any: + if not isinstance(tool_call, dict): + return tool_call + if tool_call.get("index") is None: + tool_call = dict(tool_call) + tool_call["index"] = self._tool_call_index + self._tool_call_index += 1 + return tool_call def _filter_tool_card(self, token: str) -> str: if not token or not self.tool_usage_enabled: @@ -653,7 +663,7 @@ class StreamProcessor(proc_base.BaseProcessor): data = data[end_idx + len(end_tag) :] tool_call = parse_tool_call_block(self._tool_buffer, self.tools) if tool_call: - events.append(("tool", tool_call)) + events.append(("tool", self._with_tool_index(tool_call))) self._tool_calls_seen = True self._tool_buffer = "" self._tool_state = "text" @@ -671,7 +681,7 @@ class StreamProcessor(proc_base.BaseProcessor): raw = f"{self._tool_buffer}{self._tool_partial}" tool_call = parse_tool_call_block(raw, self.tools) if tool_call: - events.append(("tool", tool_call)) + events.append(("tool", self._with_tool_index(tool_call))) self._tool_calls_seen = True elif raw: events.append(("text", f"{raw}")) diff --git a/app/services/token/manager.py b/app/services/token/manager.py index 317ab64c..c5e435da 100644 --- a/app/services/token/manager.py +++ b/app/services/token/manager.py @@ -28,6 +28,7 @@ DEFAULT_REFRESH_INTERVAL_HOURS = 8 DEFAULT_RELOAD_INTERVAL_SEC = 30 DEFAULT_SAVE_DELAY_MS = 500 DEFAULT_USAGE_FLUSH_INTERVAL_SEC = 5 +SUPER_WINDOW_THRESHOLD_SECONDS = 14400 SUPER_POOL_NAME = "ssoSuper" BASIC_POOL_NAME = "ssoBasic" @@ -182,6 +183,48 @@ class TokenManager: del self._dirty_tokens[token_key] self._mark_state_change() + def _extract_window_size_seconds(self, result: dict) -> Optional[int]: + if not isinstance(result, dict): + return None + for key in ("windowSizeSeconds", "window_size_seconds"): + if key in result: + try: + return int(result.get(key)) + except (TypeError, ValueError): + return None + limits = result.get("limits") or result.get("rateLimits") + if isinstance(limits, dict): + for key in ("windowSizeSeconds", "window_size_seconds"): + if key in limits: + try: + return int(limits.get(key)) + except (TypeError, ValueError): + return None + return None + + def _move_token_pool( + self, + token: TokenInfo, + from_pool: str, + to_pool: str, + reason: str = "", + ) -> str: + if from_pool == to_pool: + return from_pool + if to_pool not in self.pools: + self.pools[to_pool] = TokenPool(to_pool) + logger.info(f"Pool '{to_pool}': created") + if from_pool in self.pools: + self.pools[from_pool].remove(token.token) + self.pools[to_pool].add(token) + self._track_token_change(token, to_pool, "state") + self._schedule_save() + extra = f" ({reason})" if reason else "" + logger.warning( + f"Token {token.token[:10]}... moved pool {from_pool} -> {to_pool}{extra}" + ) + return to_pool + async def _save(self, force: bool = False): """保存变更""" async with self._save_lock: @@ -491,6 +534,30 @@ class TokenManager: target_token.update_quota(new_quota) target_token.record_success(is_usage=is_usage) + target_token.mark_synced() + + window_size = self._extract_window_size_seconds(result) + if window_size is not None: + if ( + target_pool_name == SUPER_POOL_NAME + and window_size >= SUPER_WINDOW_THRESHOLD_SECONDS + ): + target_pool_name = self._move_token_pool( + target_token, + SUPER_POOL_NAME, + BASIC_POOL_NAME, + reason=f"windowSizeSeconds={window_size}", + ) + elif ( + target_pool_name == BASIC_POOL_NAME + and window_size < SUPER_WINDOW_THRESHOLD_SECONDS + ): + target_pool_name = self._move_token_pool( + target_token, + BASIC_POOL_NAME, + SUPER_POOL_NAME, + reason=f"windowSizeSeconds={window_size}", + ) consumed = max(0, old_quota - new_quota) logger.info( @@ -837,6 +904,30 @@ class TokenManager: token_info.update_quota(new_quota) token_info.mark_synced() + window_size = self._extract_window_size_seconds(result) + if window_size is not None: + current_pool = self.get_pool_name_for_token(token_info.token) + if ( + current_pool == SUPER_POOL_NAME + and window_size >= SUPER_WINDOW_THRESHOLD_SECONDS + ): + self._move_token_pool( + token_info, + SUPER_POOL_NAME, + BASIC_POOL_NAME, + reason=f"windowSizeSeconds={window_size}", + ) + elif ( + current_pool == BASIC_POOL_NAME + and window_size < SUPER_WINDOW_THRESHOLD_SECONDS + ): + self._move_token_pool( + token_info, + BASIC_POOL_NAME, + SUPER_POOL_NAME, + reason=f"windowSizeSeconds={window_size}", + ) + logger.info( f"Token {token_info.token[:10]}...: refreshed " f"{old_quota} -> {new_quota}, status: {old_status} -> {token_info.status}" @@ -890,7 +981,8 @@ class TokenManager: await asyncio.sleep(1) for pool_name, token_info in to_refresh: - self._track_token_change(token_info, pool_name, "state") + current_pool = self.get_pool_name_for_token(token_info.token) or pool_name + self._track_token_change(token_info, current_pool, "state") await self._save(force=True) logger.info( diff --git a/app/static/admin/js/config.js b/app/static/admin/js/config.js index 6b3de1c4..c1c97266 100644 --- a/app/static/admin/js/config.js +++ b/app/static/admin/js/config.js @@ -38,7 +38,7 @@ const NUMERIC_FIELDS = new Set([ const LOCALE_MAP = { "app": { "label": "应用设置", - "api_key": { title: "API 密钥", desc: "调用 Grok2API 服务的 Token(可选)。" }, + "api_key": { title: "API 密钥", desc: "调用 Grok2API 服务的 Token(可选,支持多个,逗号分隔或数组)。" }, "app_key": { title: "后台密码", desc: "登录 Grok2API 管理后台的密码(必填)。" }, "public_enabled": { title: "启用功能玩法", desc: "是否启用功能玩法入口(关闭则功能玩法页面不可访问)。" }, "public_key": { title: "Public 密码", desc: "功能玩法页面的访问密码(可选)。" }, diff --git a/app/static/admin/pages/cache.html b/app/static/admin/pages/cache.html index 2babd4b7..b5905286 100644 --- a/app/static/admin/pages/cache.html +++ b/app/static/admin/pages/cache.html @@ -9,8 +9,8 @@ - - + + @@ -196,12 +196,12 @@ - - - - + + + + - + diff --git a/app/static/admin/pages/config.html b/app/static/admin/pages/config.html index 811f1b62..ee550b39 100644 --- a/app/static/admin/pages/config.html +++ b/app/static/admin/pages/config.html @@ -9,8 +9,8 @@ - - + + @@ -46,11 +46,11 @@ - - - + + + - + diff --git a/app/static/admin/pages/login.html b/app/static/admin/pages/login.html index 8e00f80b..55240da7 100644 --- a/app/static/admin/pages/login.html +++ b/app/static/admin/pages/login.html @@ -24,8 +24,8 @@ } } - - + + @@ -55,10 +55,10 @@ - + - - + + diff --git a/app/static/admin/pages/token.html b/app/static/admin/pages/token.html index fe326692..be5d0ba3 100644 --- a/app/static/admin/pages/token.html +++ b/app/static/admin/pages/token.html @@ -9,8 +9,8 @@ - - + + @@ -295,12 +295,12 @@ - - - - + + + + - + diff --git a/app/static/common/js/footer.js b/app/static/common/js/footer.js index 2bd11c2e..9b0ba239 100644 --- a/app/static/common/js/footer.js +++ b/app/static/common/js/footer.js @@ -2,7 +2,7 @@ async function loadAdminFooter() { const container = document.getElementById('app-footer'); if (!container) return; try { - const res = await fetch('/static/common/html/footer.html?v=1.5.3'); + const res = await fetch('/static/common/html/footer.html?v=1.5.4'); if (!res.ok) return; container.innerHTML = await res.text(); } catch (e) { diff --git a/app/static/common/js/header.js b/app/static/common/js/header.js index 80387a29..b0b088c4 100644 --- a/app/static/common/js/header.js +++ b/app/static/common/js/header.js @@ -2,7 +2,7 @@ async function loadAdminHeader() { const container = document.getElementById('app-header'); if (!container) return; try { - const res = await fetch('/static/common/html/header.html?v=1.5.3'); + const res = await fetch('/static/common/html/header.html?v=1.5.4'); if (!res.ok) return; container.innerHTML = await res.text(); const path = window.location.pathname; diff --git a/app/static/common/js/public-header.js b/app/static/common/js/public-header.js index cb976dd0..f086d29b 100644 --- a/app/static/common/js/public-header.js +++ b/app/static/common/js/public-header.js @@ -2,7 +2,7 @@ async function loadPublicHeader() { const container = document.getElementById('app-header'); if (!container) return; try { - const res = await fetch('/static/common/html/public-header.html?v=1.5.3'); + const res = await fetch('/static/common/html/public-header.html?v=1.5.4'); if (!res.ok) return; container.innerHTML = await res.text(); const logoutBtn = container.querySelector('#public-logout-btn'); diff --git a/app/static/public/css/chat.css b/app/static/public/css/chat.css index 40fac00a..7b2346a7 100644 --- a/app/static/public/css/chat.css +++ b/app/static/public/css/chat.css @@ -13,6 +13,17 @@ body { radial-gradient(circle at 85% 0%, rgba(17, 17, 17, 0.03), transparent 50%); } +/* Hide scrollbars while keeping scroll behavior */ +* { + scrollbar-width: none; + -ms-overflow-style: none; +} + +*::-webkit-scrollbar { + width: 0; + height: 0; +} + .chat-main { padding-bottom: 16px; } @@ -85,7 +96,7 @@ body { .message-row { display: flex; flex-direction: column; - gap: 8px; + gap: 4px; } .message-row.user { @@ -100,7 +111,7 @@ body { .message-actions { display: inline-flex; align-items: center; - gap: 10px; + gap: 6px; color: var(--accents-4); font-size: 12px; margin-left: 2px; @@ -141,7 +152,8 @@ body { .message-row.user .message-bubble { background: #efeeea; - padding: 12px 16px; + padding: 6px 10px; + line-height: 1.45; border-radius: 16px; border: 1px solid var(--chat-border); } @@ -821,16 +833,16 @@ body { /* --- Extensions for multi-session UI --- */ .chat-layout { display: flex; - gap: 20px; + gap: 6px; align-items: flex-start; } .chat-sidebar { width: 180px; flex-shrink: 0; - border: 1px solid var(--chat-border); + border: none; border-radius: 16px; - background: var(--chat-surface); + background: transparent; display: flex; flex-direction: column; overflow: hidden; @@ -844,7 +856,7 @@ body { align-items: center; justify-content: space-between; padding: 4px 12px; - border-bottom: 1px solid var(--chat-border); + border-bottom: none; min-height: 26px; } @@ -917,7 +929,7 @@ body { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; - line-height: 1.1; + line-height: 1; } .session-delete { @@ -1032,6 +1044,8 @@ body { .chat-shell { flex: 1; min-width: 0; + border-left: 1px solid var(--chat-border); + padding-left: 16px; } .model-chip { diff --git a/app/static/public/pages/chat.html b/app/static/public/pages/chat.html index cc82b861..a01c7aa4 100644 --- a/app/static/public/pages/chat.html +++ b/app/static/public/pages/chat.html @@ -9,8 +9,8 @@ - - + + @@ -147,11 +147,11 @@ - - - + + + - + diff --git a/app/static/public/pages/imagine.html b/app/static/public/pages/imagine.html index 46832efd..df8d9d99 100644 --- a/app/static/public/pages/imagine.html +++ b/app/static/public/pages/imagine.html @@ -9,8 +9,8 @@ - - + + @@ -237,12 +237,12 @@ - - - + + + - + diff --git a/app/static/public/pages/login.html b/app/static/public/pages/login.html index 8f70260e..5c098e23 100644 --- a/app/static/public/pages/login.html +++ b/app/static/public/pages/login.html @@ -24,8 +24,8 @@ } } - - + + @@ -59,10 +59,10 @@ - + - - + + diff --git a/app/static/public/pages/video.html b/app/static/public/pages/video.html index 52eca7e2..ec154ba4 100644 --- a/app/static/public/pages/video.html +++ b/app/static/public/pages/video.html @@ -9,8 +9,8 @@ - - + + @@ -158,11 +158,11 @@ - - - + + + - + diff --git a/app/static/public/pages/voice.html b/app/static/public/pages/voice.html index 32e63e81..40f1990f 100644 --- a/app/static/public/pages/voice.html +++ b/app/static/public/pages/voice.html @@ -9,8 +9,8 @@ - - + + @@ -147,11 +147,11 @@ - - - + + + - + diff --git a/config.defaults.toml b/config.defaults.toml index 9ef56d87..2619d6f3 100644 --- a/config.defaults.toml +++ b/config.defaults.toml @@ -4,7 +4,7 @@ app_url = "" # 后台管理密码 app_key = "grok2api" -# API 调用密钥(可选) +# API 调用密钥(可选,支持列表) api_key = "" # 是否启用 public 功能玩法 public_enabled = false diff --git a/docs/README.en.md b/docs/README.en.md index 019dcef3..e739a456 100644 --- a/docs/README.en.md +++ b/docs/README.en.md @@ -334,7 +334,7 @@ Config file: `data/config.toml` | :-- | :-- | :-- | :-- | :-- | | **app** | `app_url` | App URL | External base URL used for file links. | `""` | | | `app_key` | Admin password | Login password for admin panel. | `grok2api` | -| | `api_key` | API key | Optional API key for access. | `""` | +| | `api_key` | API key | Optional API key for access (comma-separated string or array). | `""` | | | `public_enabled` | Public mode | Enable public features. | `false` | | | `public_key` | Public key | Public access key (optional). | `""` | | | `image_format` | Image format | `url` or `base64`. | `url` | diff --git a/pyproject.toml b/pyproject.toml index 22666620..ae732684 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "grok2api" -version = "1.5.3" +version = "1.5.4" description = "Grok2API rebuilt with FastAPI, fully aligned with the latest web call format. Supports streaming and non-streaming chat, image generation/editing, deep thinking, token pool concurrency, and automatic load balancing." readme = "README.md" requires-python = ">=3.13" diff --git a/readme.md b/readme.md index a4711616..be6321c1 100644 --- a/readme.md +++ b/readme.md @@ -335,7 +335,7 @@ curl http://localhost:8000/v1/images/edits \ | :-- | :-- | :-- | :-- | :-- | | **app** | `app_url` | 应用地址 | 当前 Grok2API 服务的外部访问 URL,用于文件链接访问。 | `""` | | | `app_key` | 后台密码 | 登录 Grok2API 管理后台的密码(必填)。 | `grok2api` | -| | `api_key` | API 密钥 | 调用 Grok2API 服务的 Token(可选)。 | `""` | +| | `api_key` | API 密钥 | 调用 Grok2API 服务的 Token(可选,支持逗号分隔或数组)。 | `""` | | | `public_enabled` | Public 开关 | 是否启用 public 功能玩法。 | `false` | | | `public_key` | Public 密钥 | Public 调用密钥(可选)。 | `""` | | | `image_format` | 图片格式 | 生成的图片格式(url 或 base64)。 | `url` | diff --git a/uv.lock b/uv.lock index c0a70e63..fd85b5b8 100644 --- a/uv.lock +++ b/uv.lock @@ -471,7 +471,7 @@ wheels = [ [[package]] name = "grok2api" -version = "1.5.3" +version = "1.5.4" source = { virtual = "." } dependencies = [ { name = "aiofiles" },