A local-first multi-agent app development platform, built on PenguinHarness
fix(web): 生成 id 的请求被拒时,原因留在 id 框下面
lmliheng52058dc5c30b
diff --git a/changelog/unreleased/2026-10-31-semantic-id-suggest-refusal.md b/changelog/unreleased/2026-10-31-semantic-id-suggest-refusal.mdnew file mode 100644index 00000000..a0f34b55--- /dev/null+++ b/changelog/unreleased/2026-10-31-semantic-id-suggest-refusal.md@@ -0,0 +1,48 @@+# A refused id proposal says why under the id box, instead of only in a toast++- **Date:** 2026-10-31+- **Type:** fix+- **Scope:** `web`+- **Breaking:** no++[中文](2026-10-31-semantic-id-suggest-refusal.zh.md)++Every create dialog that names an object with a semantic id (Project, Agent, Benchmark,+organization, channel) wears the same id field, whose "generate with AI" button asks the server for+an id derived from the display name. The server always answers with an id, so what the field had to+say was only how much that id is worth. **The request itself failing** is another matter — no access+to that Project, company mode switched off under an org or channel dialog, the connection gone —+and then the box is left exactly as it was while the reason arrived as a toast and nothing else: six+seconds after a reader looked away, a button that had done nothing and no sentence saying why.++That sentence now sits under the id box, in the same place as "this id is a placeholder" and "this+one is a transliteration": the shared lead-in `S.common.refused`, in the danger ink, as+`role="alert"` so it is announced when it arrives. The next edit or the next click clears it — it is+this click's answer, not the field's standing state. All five create dialogs share this body, so all+five get it, and no new copy was written.++## Details++- `packages/web/src/features/semantic-id/semantic-id-field.tsx`: a `refusal` state, filled from the+ failed request via `refusalText` (the server's own words when it has them, this app's own sentence+ for the conditions it raises itself) instead of `toastError`; cleared by a change in the box and+ by the next attempt. The box itself is untouched — the click obtained nothing.+- `packages/web/src/features/semantic-id/id-suggest-notice.ts`: a new pure function `idSuggestLine`+ decides which line goes under the box in one place: refusal, then the attention-toned placeholder+ note, then the quiet transliteration one. The field never holds two at once (it clears the note+ before asking); the order is written down so a later edit cannot quietly let a note hide why the+ button did nothing.++## Tests++- `packages/web/test/id-suggest-notice.test.ts`: four new cases pinning the combinations (neither, a+ note alone, a refusal alone, and a refusal outranking a leftover note).+- `packages/web/e2e/semantic-id-suggest-refusal.spec.mjs` (new, two browser cases against the real+ server): a refused proposal leaves the reason under the id box, the box still empty and the+ notification region without a toast, and the line is still there seven seconds after a toast would+ have gone; then, with the refusal lifted, the same click lands an id and the line goes with it.++## Compatibility++- No API, on-disk or request-shape change. The three notes a successful proposal draws are word for+ word as before; only the failed path stopped using a toast.diff --git a/changelog/unreleased/2026-10-31-semantic-id-suggest-refusal.zh.md b/changelog/unreleased/2026-10-31-semantic-id-suggest-refusal.zh.mdnew file mode 100644index 00000000..4c0e6e6d--- /dev/null+++ b/changelog/unreleased/2026-10-31-semantic-id-suggest-refusal.zh.md@@ -0,0 +1,40 @@+# 生成 ID 的请求被拒时,原因留在 ID 框下面,不再只在一条六秒的提示里++- **日期:** 2026-10-31+- **类型:** fix+- **范围:** `web`+- **破坏性:** 无++[English](2026-10-31-semantic-id-suggest-refusal.md)++每个「用语义 id 命名对象」的创建对话框(项目、Agent、Benchmark、组织、渠道)都戴着同一个 id+字段,其中那颗「用 AI 生成」按钮向服务端要一个从显示名推出来的 id。服务端一定会给出一个 id,+所以框里原本只差一句「这个 id 值多少」的说明。**请求本身失败**是另一回事:没有那个项目的访问+权、组织 / 渠道对话框下公司模式被关掉、连接断了 —— 框里什么都不会变,而原因此前只有一条 toast,+用户看向别处的那六秒之后,屏幕上一个什么都没做的按钮,一句已经消失的解释。++现在这句话就写在 id 框下面,与「这个 id 是占位」「这是按名称转写」的说明同一个位置:共享的引子+`S.common.refused`、danger 色、`role="alert"`(出现时会被读屏念到)。下一次编辑或下一次点击都+会把它清掉 —— 它是这一次点击的答复,不是这个框的长期状态。五个创建对话框共用这一份正文,因此+五处同时生效;没有新增文案。++## 细节++- `packages/web/src/features/semantic-id/semantic-id-field.tsx`:多一个 `refusal` 状态,由失败的+ 请求经 `refusalText` 填上(服务端有原话就用原话,正文自己举的错也有自己的句子),不再走+ `toastError`;输入变化与下一次生成都清空它。id 框本身保持不变 —— 这次点击什么都没有得到。+- `packages/web/src/features/semantic-id/id-suggest-notice.ts`:新增纯函数 `idSuggestLine`,把+ 「框下面画哪一行」收成一处:拒绝 > 占位提示(attention)> 安静的转写说明。字段目前不会同时+ 持有两者(生成前先清说明),把顺序写在这里是为了以后改动不会悄悄让说明盖住「按钮为什么没动」。++## 测试++- `packages/web/test/id-suggest-notice.test.ts`:新增四条,钉住这四种组合(都没有 / 一条说明 /+ 一条拒绝 / 两者同时存在时拒绝在前)。+- `packages/web/e2e/semantic-id-suggest-refusal.spec.mjs`(新,真服务端两条):被拒的生成把原因留+ 在 id 框下面、框里仍是空的、通知区里没有可点的 toast,并且 toast 该消失的七秒之后那一行还在;+ 随后放开拒绝,同一次点击真的拿到 id,那一行随之消失。++## 兼容++- 接口、磁盘格式、请求体都没变。生成成功时的三种说明一字未改,只有请求失败那条路不再用 toast。diff --git a/packages/web/e2e/semantic-id-suggest-refusal.spec.mjs b/packages/web/e2e/semantic-id-suggest-refusal.spec.mjsnew file mode 100644index 00000000..a5bda264--- /dev/null+++ b/packages/web/e2e/semantic-id-suggest-refusal.spec.mjs@@ -0,0 +1,117 @@+/**+ * Regression: a create dialog's "generate an id" button says why it did nothing, under the id+ * field, and the sentence outlives the six seconds a toast lives.+ *+ * The button asks the server for an id built from the display name; a model-made id needs no+ * note, a transliteration gets a quiet one and a placeholder an attention-toned one — all three+ * are lines under the field (features/semantic-id/id-suggest-notice.ts). The fourth case is the+ * request itself failing, and there the box is left exactly as it was: that used to arrive as a+ * toast and nothing else, so a reader who looked away from the notification region was left with+ * a button that had done nothing and no reason. It is now the field's own refusal line+ * (`role="alert"`, danger ink, the shared "操作被拒绝:" lead-in), cleared by the next edit or the+ * next attempt.+ *+ * Both tests run against the real server with only the suggest-id POST taken over, exactly the+ * way agent-config-refusal.spec.mjs takes over the writes it refuses: everything else — the+ * login, the Project, the Agents page, the dialog and its validation — is the app's own.+ */+import { test, expect } from "@playwright/test";+import { provisionAndLogin } from "./auth.mjs";++const BASE = process.env.BASE_URL;+const P = "password123";++/** The proposal route, as the web app reaches it. */+const SUGGEST = /\/api\/projects\/[^/]+\/suggest-id$/;++/** What a refused proposal answers: the route's own 404 for a Project the caller cannot reach. */+const REFUSAL = {+ code: "not_found",+ message: "This project does not exist, or you have no access to it.",+};+/** The line the field draws: the shared lead-in plus the app's sentence for that code. */+const REFUSED_LINE = "操作被拒绝:资源不存在,或你没有访问权限。";+/** How long a toast lives (the notification stack's own lifetime), plus a second of slack. */+const TOAST_LIFE = 7_000;++/** The notification region: a refusal must not be announced there (that is the old toast). */+const notifications = (page) => page.getByRole("region", { name: "通知" });++/**+ * Take over the proposal POST: every request is counted, and while `state.refuse` is set it is+ * answered with the refusal instead of reaching the real route.+ */+async function interceptSuggest(page, state) {+ await page.route(SUGGEST, async (route) => {+ if (route.request().method() !== "POST") return route.fallback();+ state.seen.push(route.request().url());+ if (!state.refuse) return route.fallback();+ await route.fulfill({+ status: 404,+ contentType: "application/json",+ body: JSON.stringify({ error: REFUSAL }),+ });+ });+}++/** The create-Agent dialog, opened from the Agents page, with a name typed in and no id yet. */+async function openCreateDialog(page, user, name) {+ await provisionAndLogin(page.request, user, P);+ await page.goto(`${BASE}/agents`);+ await page.getByRole("button", { name: "手动创建", exact: true }).first().click();+ const dialog = page.getByRole("dialog", { name: "创建 Agent" });+ await expect(dialog).toBeVisible();+ // The id box starts empty, which is what makes "the box was left alone" readable below.+ const id = dialog.getByRole("textbox", { name: /^Agent id/ });+ await expect(id).toHaveValue("");+ // The name box's accessible name carries its hint as well ("名称 留空则使用 Agent id 作为名称").+ await dialog.getByRole("textbox", { name: /^名称/ }).fill(name);+ return { dialog, id, generate: dialog.getByRole("button", { name: "用 AI 生成" }) };+}++test("a refused id proposal says why under the field, and the line outlives the toast", async ({+ page,+}) => {+ const { dialog, id, generate } = await openCreateDialog(page, "probesuggesta", "研究助理");+ const state = { seen: [], refuse: true };+ await interceptSuggest(page, state);++ await generate.click();++ // The reason is under the box that asked, and it is not a toast.+ const line = dialog.getByRole("alert");+ await expect(line).toHaveText(REFUSED_LINE);+ await expect(notifications(page).getByRole("button")).toHaveCount(0);+ expect(state.seen).toHaveLength(1);+ // Nothing was proposed into the box: the request never answered.+ await expect(id).toHaveValue("");++ // Past the lifetime of a toast, the same line is still there — this is the whole point.+ await page.waitForTimeout(TOAST_LIFE);+ await expect(line).toHaveText(REFUSED_LINE);+ await expect(id).toHaveValue("");++ // And it is cleared the moment the user edits the box, not carried over to the next name.+ await id.fill("x");+ await expect(dialog.getByRole("alert")).toHaveCount(0);+});++test("the next attempt after a refusal lands: the line goes and the box gets the id", async ({+ page,+}) => {+ const { dialog, id, generate } = await openCreateDialog(page, "probesuggestb", "研究助理");+ const state = { seen: [], refuse: true };+ await interceptSuggest(page, state);++ await generate.click();+ await expect(dialog.getByRole("alert")).toHaveText(REFUSED_LINE);++ // Let the same button through: the real route answers (a transliterated id here — this data+ // root has no model), the refusal line is gone and what it says about the id takes its place.+ state.refuse = false;+ await generate.click();+ await expect(dialog.getByRole("alert")).toHaveCount(0);+ await expect(id).not.toHaveValue("");+ expect(state.seen).toHaveLength(2);+ await expect(notifications(page).getByRole("button")).toHaveCount(0);+});diff --git a/packages/web/src/features/semantic-id/id-suggest-notice.ts b/packages/web/src/features/semantic-id/id-suggest-notice.tsindex 7ae76ee0..e9e903d4 100644--- a/packages/web/src/features/semantic-id/id-suggest-notice.ts+++ b/packages/web/src/features/semantic-id/id-suggest-notice.ts@@ -38,6 +38,30 @@ export function idSuggestNotice(return null;}+/**+ * The single line the field draws under the box, and which of the three it is: the shared refusal+ * lead-in (the request itself failed), the attention-toned note (the box holds a placeholder), or+ * the quiet one (the id is a transliteration).+ *+ * The refusal is asked for FIRST even though the field never holds both at once: `generate`+ * clears the note before it asks, so a verdict about an id in the box can only be older than the+ * answer about the click that asked for another one. Spelling the order out here means a later+ * edit that keeps a note around cannot quietly hide why the button did nothing.+ */+export type IdSuggestLine =+ | { kind: "refusal"; text: string }+ | { kind: "attention"; text: string }+ | { kind: "muted"; text: string };++export function idSuggestLine(+ notice: IdSuggestNotice | null,+ refusal: string | null,+): IdSuggestLine | null {+ if (refusal !== null) return { kind: "refusal", text: refusal };+ if (notice === null) return null;+ return { kind: notice.tone, text: notice.text };+}+/*** What goes in the box: the proposal, less the part the field draws in front of the box. The* server answers a non-admin's Project id whole (`alice-research_lab`) while the box holds onlydiff --git a/packages/web/src/features/semantic-id/semantic-id-field.tsx b/packages/web/src/features/semantic-id/semantic-id-field.tsxindex 621843b8..35552041 100644--- a/packages/web/src/features/semantic-id/semantic-id-field.tsx+++ b/packages/web/src/features/semantic-id/semantic-id-field.tsx@@ -13,6 +13,13 @@* for a real name, rather than a toast that would be gone by the time the user looks down.* Nothing is proposed on its own: typing a name never rewrites the id.*+ * THE REQUEST ITSELF CAN FAIL — no access to the Project the model is spent from, company mode+ * switched off under an org or channel dialog, the connection gone — and then the box is left+ * exactly as it was. That failure is drawn in the same place as the note (the shared refusal+ * lead-in, in danger ink, `role="alert"`), because the button is the thing that just did nothing+ * and it is where the reader is looking: a toast explains a button that still sits there, and+ * only for six seconds.+ ** The button says what it does in words — a model is asked for the id, which is not something* a sparkles glyph on its own tells anyone — so it sits BESIDE the box rather than inside it,* where a label would crowd the value it is meant to leave room for. What it must stay outside@@ -27,21 +34,12 @@import { useId, useState } from "react";import type { KeyboardEvent } from "react";import type { SemanticIdKind } from "@adelie/server/api";-import {- Button,- FieldError,- FieldHint,- FieldLabel,- GlyphIcon,- ICON_SIZE,- Input,- toastError,-} from "@adelie/ui";+import { Button, FieldError, FieldHint, FieldLabel, GlyphIcon, ICON_SIZE, Input } from "@adelie/ui";import * as api from "../../api/endpoints";import { S } from "../../lib/strings";-import { apiErrorText } from "../../lib/api-error";+import { refusalText } from "../../lib/api-error";import { toneInk } from "../../lib/tone";-import { idSuggestNotice, proposalValue } from "./id-suggest-notice";+import { idSuggestLine, idSuggestNotice, proposalValue } from "./id-suggest-notice";import type { IdSuggestNotice } from "./id-suggest-notice";/** Generate (lucide sparkles): the four-pointed star with its two smaller companions. */@@ -94,16 +92,23 @@ export function SemanticIdField({const [busy, setBusy] = useState(false);/** What the last proposal has to say about the id it filled in; cleared by the next edit or attempt. */const [notice, setNotice] = useState<IdSuggestNotice | null>(null);+ /**+ * Why the last request never answered — the reason the button did nothing at all. It stays+ * until the next edit or attempt, which is what makes it readable after the click.+ */+ const [refusal, setRefusal] = useState<string | null>(null);const derivable = source.trim() !== "";// The caller's validation outranks the proposal's own note: a rejected id is about what is// in the box, which is what the user is looking at.const showError = error !== undefined;- const below = showError || notice !== null;+ const line = idSuggestLine(notice, refusal);+ const below = showError || line !== null;const generate = async () => {if (projectId === null) return;setBusy(true);setNotice(null);+ setRefusal(null);try {const res = await api.suggestSemanticId(projectId, {name: source.trim(),@@ -113,9 +118,10 @@ export function SemanticIdField({onChange(proposalValue(res.id, lockedPrefix));setNotice(idSuggestNotice(res, S.semanticId.idSuggest));} catch (e) {- // The request itself failed (offline, no permission, the mode switched off): the id is- // unchanged and there is nothing to say under the field.- toastError(apiErrorText(e));+ // The request never answered (offline, no permission, the mode switched off): the box is+ // untouched and the button did nothing, so the reason goes under the field — `refusalText`+ // because the sentence may be the app's own rather than the server's.+ setRefusal(refusalText(e));} finally {setBusy(false);}@@ -133,6 +139,7 @@ export function SemanticIdField({{...(below ? { "aria-describedby": messageId } : {})}onChange={(e) => {setNotice(null);+ setRefusal(null);onChange(e.target.value);}}{...(onEnter !== undefined@@ -179,11 +186,11 @@ export function SemanticIdField({</div>{showError ? (<FieldError id={messageId}>{error}</FieldError>- ) : notice?.tone === "attention" ? (+ ) : line?.kind === "attention" ? (// A placeholder id: what the user must act on, so it takes the slot the error would.// `status` rather than `alert` — the box holds a valid id, nothing was rejected.<span id={messageId} role="status" className={`mt-1 block text-xs ${toneInk.attention}`}>- {notice.text}+ {line.text}</span>) : (<>@@ -191,11 +198,22 @@ export function SemanticIdField({is punctuation, and punctuation is part of the language. The id rule stays onscreen beside a quiet note — it is what the user reads while typing. */}<FieldHint>{`${hint}${generateHint ?? S.semanticId.idGenerateHint}`}</FieldHint>- {notice !== null && (+ {line?.kind === "refusal" ? (+ // The button did nothing and the box still holds what it held: the reason belongs+ // under it, in the shared refusal lead-in. `alert`, because a toast was announced+ // once and then left, while the button stays.+ <p+ id={messageId}+ role="alert"+ className={`mt-1 block text-xs break-words ${toneInk.danger}`}+ >+ {S.common.refused(line.text)}+ </p>+ ) : line?.kind === "muted" ? (<span id={messageId} role="status" className={`mt-1 block text-xs ${toneInk.muted}`}>- {notice.text}+ {line.text}</span>- )}+ ) : null}</>)}</div>diff --git a/packages/web/test/id-suggest-notice.test.ts b/packages/web/test/id-suggest-notice.test.tsindex efd8ce45..df84ac76 100644--- a/packages/web/test/id-suggest-notice.test.ts+++ b/packages/web/test/id-suggest-notice.test.ts@@ -6,7 +6,8 @@*/import { describe, expect, it } from "vitest";import type { SemanticIdSuggestReason, SemanticIdSuggestResponse } from "@adelie/server/api";-import { idSuggestNotice } from "../src/features/semantic-id/id-suggest-notice";+import { idSuggestLine, idSuggestNotice } from "../src/features/semantic-id/id-suggest-notice";+import type { IdSuggestNotice } from "../src/features/semantic-id/id-suggest-notice";import { zh } from "../src/lib/strings";import { en } from "../src/lib/strings-en";@@ -77,3 +78,27 @@ describe("idSuggestNotice", () => {);});});++describe("idSuggestLine", () => {+ const QUIET: IdSuggestNotice = { tone: "muted", text: "按名称转写生成" };++ it("says nothing while the box speaks for itself", () => {+ expect(idSuggestLine(null, null)).toBeNull();+ });++ it("passes a note through with the tone it was given", () => {+ expect(idSuggestLine(QUIET, null)).toEqual({ kind: "muted", text: "按名称转写生成" });+ expect(idSuggestLine({ tone: "attention", text: "请填写名称" }, null)).toEqual({+ kind: "attention",+ text: "请填写名称",+ });+ });++ it("draws a failed request as a refusal of its own — the reason the button did nothing", () => {+ expect(idSuggestLine(null, "not_found")).toEqual({ kind: "refusal", text: "not_found" });+ });++ it("lets a refusal outrank a note left over from an earlier proposal", () => {+ expect(idSuggestLine(QUIET, "not_found")).toEqual({ kind: "refusal", text: "not_found" });+ });+});