Skip to content

feat(ai/settings): surface, reject, and recover AI provider misconfiguration#1788

Merged
xuyushun441-sys merged 1 commit into
mainfrom
feat/ai-settings-diagnostics
Jun 12, 2026
Merged

feat(ai/settings): surface, reject, and recover AI provider misconfiguration#1788
xuyushun441-sys merged 1 commit into
mainfrom
feat/ai-settings-diagnostics

Conversation

@xuyushun441-sys

Copy link
Copy Markdown
Contributor

背景

本地 showcase 曾出现:Setup UI 半填的 AI 设置(provider=cloudflare、API key 为空)被存进 sys_setting,静默覆盖了 env 自动检测出的 adapter;唯一症状是聊天流里一句裸的 Bad Request。排查只能靠 sqlite 手术。本 PR 把这条链路上的四个缺口全部补上(服务端部分;前端按钮/错误展示由 manifest 与现有 toast 机制驱动,objectui 零改动)。

改动

1. 生效配置可见 — GET /api/v1/ai/status

AIServicePlugin 全程追踪 adapter 来源(boot 检测 / settings 重建 / reset),新接口返回:

{ "adapter": "vercel", "description": "Vercel AI Gateway (model: …)",
  "source": "env | settings | explicit | fallback",
  "provider": "gateway", "model": "",
  "settingsProvider": "", "settingsError": null }

settingsError 非空 = 已保存的 settings 没有生效及其原因(此前只有一行服务端 warn)。

2. 保存时服务端校验(spec 早已承诺,首次实现)

SettingsService.setMany 现在按 manifest 校验:对"当前可见(visible 表达式服务端求值)+ required + 被本次写入触及"的字段拒绝空值;pattern 不匹配同样拒绝。整批原子拒绝,400 SETTINGS_VALIDATION 携带字段级错误,UI toast 直接可读。受限文法求值器见 visibility-eval.ts(data.x === 'y'&&/||/!、括号、字面量);不可解析表达式与全空 patch(重置)保持宽松。gateway_model/cloudflare_model 增加 provider/model 格式 pattern。

3. 一键恢复 — 内置 reset action + manifest 按钮

每个 settings 命名空间获得内置 reset(清持久化行,回落 env/默认,留审计);ai 命名空间的覆盖版还会立即重跑 env adapter 检测。AI manifest 增加 "Reset to environment defaults" 按钮,前端自动渲染。

4. chat 流错误可诊断

chat/agents/assistant 三条流式路由的 provider 错误经 error-hints.ts 增强:带 adapter 描述 + 按状态码映射的下一步提示(400→model id 格式、401/403→凭据、404→模型不存在、429→限流、网络错误)。Bad Request 变成:

Model 'claude/sonnet-4.6' not found (HTTP 404) — adapter: Vercel AI Gateway (model: claude/sonnet-4.6) · The model id does not exist for this provider. Check the model name in Setup → AI.

测试

  • 新增 adapter-status.test.ts(8)、error-hints.test.ts(6)、visibility-eval.test.ts(6)+ settings reset/校验用例;service-ai 359、service-settings 113 全绿。
  • 两个旧测试因新校验按预期拦截"只存 provider 不带必填字段"而补全为完整配置。
  • 浏览器实测(showcase :3000 → Setup → AI 与 Embedder):坏 cloudflare 配置保存被拒(字段级 toast,DB 零写入);gpt-4o 无斜杠被 pattern 拒;存入合规但错误的 model 后 status 立即显示 source: settings + 该 model;chat 错误带完整诊断;点 Reset 按钮 toast 报告 "Cleared 2 saved value(s). Active adapter: …" 且 chat 即刻恢复。

🤖 Generated with Claude Code

…uration

A half-saved `ai` settings row (provider=cloudflare, empty key) silently
overrode env auto-detection and the only symptom was a bare "Bad Request"
in chat. Close every gap in that chain:

- GET /api/v1/ai/status — active adapter provenance (source: explicit/
  env/settings/fallback, provider, model) plus settingsError explaining
  why saved settings were NOT applied.
- Server-side save validation in SettingsService.setMany: visible+required
  empty fields and pattern mismatches reject the batch with field-level
  errors (400 SETTINGS_VALIDATION). Visibility expressions are evaluated
  by a restricted-grammar parser; resets and unparseable expressions stay
  lenient. gateway_model/cloudflare_model gain provider/model patterns.
- Built-in `reset` settings action per namespace; the ai override also
  re-runs env adapter detection. Manifest ships a "Reset to environment
  defaults" button — no more hand-editing sys_setting.
- Chat/agent/assistant stream errors now carry the adapter description
  and actionable hints (400 → model-id format, 401 → credential, …).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jun 12, 2026 1:31pm

Request Review

it('enriches thrown errors too', async () => {
async function* throwingStream(): AsyncIterable<any> {
throw Object.assign(new Error('Unauthorized'), { statusCode: 401 });
yield undefined; // unreachable — makes this a generator
@xuyushun441-sys
xuyushun441-sys merged commit d100707 into main Jun 12, 2026
12 checks passed
@xuyushun441-sys
xuyushun441-sys deleted the feat/ai-settings-diagnostics branch June 12, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/xl tests tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants