Skip to content

Commit 211c1aa

Browse files
fix(plugin-chatbot): the record-write card header badge follows the decision the card body shows (#39303)
Fixes #39265 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:在 AI 助手的写入确认卡上点了确认之后,卡片标题旁的状态标签改为跟着卡片内容走:提交中显示「正在写入」,写好后显示「已写入」,取消后显示「已取消」,不再一直停在「等待确认」。 - 为什么做:替点确认的人解决同一张卡上标题说「等待确认」、内容却说「已写入」的矛盾。 - 出错会怎样:只影响确认卡标题上的状态文字,写入的数据和按钮都不变;有问题可以直接撤回这次改动。 - 要你做什么:无。 ## What changes - `ui/packages/plugin-chatbot/src/chat/toolRecordWrite.ts`: new `recordWriteOutcomeOf(decision)` reads the host's decision for the tool call (`toolDecisions[toolCallId]`, the one source the card body already used) and names what the card says about it: `submitting`, `written`, `cancelled`, `held`, `expired` or `error`. With no decision it is absent. - `ui/packages/plugin-chatbot/src/chat/toolDetail.tsx`: the record-write card reads that one outcome twice. - The body's decision line now branches on it, with the same words as before. - The header badge also reads it. Each outcome maps to the AI-SDK badge state whose icon fits and to the label the body uses for it. In flight reads `recordWriteSubmittingLabel`, written reads `recordWriteWrittenLabel`, and cancelled reads `recordWriteCancelledLabel`. Held reads the inbox's held word, expired reads the expired word, and a refused write reads the existing `Error` badge word, with the server's reason in the body. - With no decision, the badge still follows the tool part's state ("Awaiting approval"). - Before this change the badge read only the tool part's state. A Confirm or Cancel on the card calls the pending-action door and never changes that state, so the badge kept reading "Awaiting approval" until a reload. - No new strings. No change to `ChatbotEnhanced.tsx`, the vendored `elements/tool.tsx` or any locale pack. - Changeset `.changeset/39265-write-card-badge.md`: a `patch` for `@object-ui/plugin-chatbot`. Read first, by risk: 1. `chat/toolDetail.tsx`, the `recordWriteBadge` table and the `ToolHeader` props. These choose the icon state and label per outcome. 2. `chat/toolRecordWrite.ts`, `recordWriteOutcomeOf`. Its branch order is the body's old order: error, held, expired, pending, then success split by `decision.decision`. 3. The new `describe` at the end of `ChatbotEnhanced.recordWriteCard-3565.test.tsx`. ## Tests The pins are a new top-level `describe` in the existing harness file `ChatbotEnhanced.recordWriteCard-3565.test.tsx`. Each test re-renders one card through the decision states with `rerender`, with no remount and no reload: - create and update (`it.each`): the badge reads "Awaiting approval", then the submitting label while the Confirm is in flight, then "Written" after a successful Confirm. At each step the badge reads the same word as the body. - Cancel: the badge reads the submitting label, then "Cancelled", never "Written". - held, expired and refused: the badge reads the held word, then the expired word, then "Error". It never reads "Written" or "Awaiting approval". <details><summary>Runs on fa63a25 (the final commit)</summary> ```text pnpm --dir ui exec vitest run --maxWorkers=2 packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.recordWriteCard-3565.test.tsx packages/plugin-chatbot/src/__tests__/recordWrite-3565.test.ts Test Files 2 passed (2) Tests 53 passed (53) VERDICT command-exit 0 pnpm --dir ui exec vitest run --maxWorkers=2 packages/plugin-chatbot/ Test Files 89 passed (89) Tests 882 passed (882) VERDICT command-exit 0 pnpm --filter @object-ui/plugin-chatbot type-check (tsc --noEmit && tsc -p tsconfig.test.json) VERDICT command-exit 0; tsconfig.test.json --listFiles counts the three edited files (3) ``` </details> <details><summary>Ablation and restore</summary> `node scripts/ablation-replace.mjs` replaced the line that selects the badge from the outcome with a constant `undefined`. The body still read the outcome. The anchor went from 1 to 0, and the blob changed from 09c401c to 8169fe87afbe. ```text × create: awaiting, then in flight, then written after 确认 × update: awaiting, then in flight, then written after 确认 × after 取消 the badge reads cancelled, as the body does × a held, lapsed or refused decision reads on the badge as it does in the body, never as written Tests 4 failed | 25 passed (29) Expected element to have text content: 正在写入… Received: Create recordAwaiting approval ``` The mutated tree reproduces the reported defect: the header reads "Awaiting approval" while the body reads the decision. The restore leg matched the blob against HEAD (09c401c == 09c401c), and `git diff HEAD` is empty. </details> ## Acceptance notes - The generic approval cards (the plain Approve or Deny strip, the sample-data cleanup card and the package-removal card) also draw their header badge from the tool part's state. They show the decision only in their own decision line. This PR does not change them, because the card scopes the fix to the record-write card. This is an inference from reading the code and was not measured in a browser. It is not filed. - The badge for a refused write reads the existing `Error` word, not the server's sentence. The body carries the sentence. ## Verification Local gate subset on `fa63a255d5`. The derivation is `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack`, computed against the merge base `c7478b37b` with the 4 changed paths. Each exit code was captured before any pipe. <details><summary>38 derived commands plus `check:changeset-fixed`: every one exit 0</summary> ```text node scripts/check-adr-0087-registration.mjs --base origin/main :: exit 0 node scripts/check-adr-0087-registration.mjs --self-test :: exit 0 node scripts/check-changeset-no-major.mjs --base origin/main :: exit 0 node scripts/check-closing-keyword-parity.mjs :: exit 0 node scripts/check-closing-keyword-parity.mjs --self-test :: exit 0 node scripts/check-comment-mask-corpus.mjs :: exit 0 node scripts/check-empty-changeset.mjs --base origin/main :: exit 0 node scripts/check-empty-changeset.mjs --self-test :: exit 0 node scripts/check-sdui-manifest.mjs :: exit 0 node scripts/check-sdui-manifest.mjs --self-test :: exit 0 node scripts/ci/snapshot-next.mjs --self-test :: exit 0 node scripts/pm/check-governed-queue-guard.mjs --self-test :: exit 0 node scripts/pm/release-rehearsal-clone.mjs --self-test :: exit 0 node scripts/release-pending-publish.mjs --self-test :: exit 0 pnpm --dir ui exec vitest run scripts/ :: exit 0 pnpm --filter @objectstack/spec run check:duration-unit-keys :: exit 0 pnpm check:adr-anchors :: exit 0 pnpm check:changeset-gate-self-tests :: exit 0 pnpm check:closed-ceremony-text :: exit 0 pnpm check:cross-package-test-inputs :: exit 0 pnpm check:dispatcher-error-vocabulary :: exit 0 pnpm check:driver-memory-census :: exit 0 pnpm check:engine-double-contract :: exit 0 pnpm check:gitlink-declared :: exit 0 pnpm check:nul-bytes :: exit 0 pnpm check:published-files :: exit 0 pnpm check:query-options-erasure :: exit 0 pnpm check:refd-timer-probe :: exit 0 pnpm check:registry-log-declared :: exit 0 pnpm check:rest-log-spy-declared :: exit 0 pnpm check:skill-identifier-liveness :: exit 0 pnpm check:test-source-alias :: exit 0 pnpm check:tier-file-adoption :: exit 0 pnpm check:type-check-coverage :: exit 0 pnpm check:type-check-debt :: exit 0 pnpm check:undeclared-dep-imports :: exit 0 pnpm check:watch-hint-literal :: exit 0 pnpm check:where-matcher :: exit 0 pnpm check:changeset-fixed :: exit 0 ``` </details> `node scripts/pm/dispatch-gates.mjs --ran` printed: ```text Run reconciliation — 38 derived, 38 run, 0 NOT-MEASURED, 0 UNRUN. ✓ dispatch-gates --ran: 38 derived famil(ies) accounted for — 38 run, 0 NOT-MEASURED (a DERIVED zero — all 38 recorded an exit code and none of them is 3). ``` `check:changeset-fixed` was run as well, because its roster lives in `.changeset/`; `--ran` lists it as outside the derivation. Type-check lanes, the whole-tree families and the repo-wide lint are left to CI and are not repeated here. Before opening, `origin/main` was 3 commits ahead of the base, and none of them touch `ui/packages/plugin-chatbot`, so the branch was not merged; CI runs on the merge ref. Two-minute check: run `pnpm --dir ui exec vitest run packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.recordWriteCard-3565.test.tsx` from the repository root. Expect 29 passed, including the four tests under "the record-write card header badge follows the decision". --- _Generated by [Claude Code](https://claude.ai/code/session_01TsNV8Bsjqmw7aXCo2QuLU1)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 14680d0 commit 211c1aa

4 files changed

Lines changed: 155 additions & 9 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/plugin-chatbot': patch
3+
---
4+
5+
fix(console): the chat's record-write card header badge says what the card says about the decision, not "Awaiting approval" after a Confirm
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
After a person confirmed the record the chat proposed and the write succeeded, the card body read "Written" with a link to the record, but the badge in the card header still read "Awaiting approval" until the page was reloaded. The badge followed the tool call's pending state, which a Confirm or Cancel on the card does not change. The badge now reads the same decision the body reads: "Writing…" while the Confirm or Cancel is being sent, "Written" after a successful write, and "Cancelled" after a Cancel or "Open the full form". A write held for another approver reads "Waiting for approval", a lapsed one "Expired", and a refused one "Error", with the reason in the body. Before any decision, the badge still reads "Awaiting approval". No new strings: the badge uses the card's existing labels.

‎ui/packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.recordWriteCard-3565.test.tsx‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,90 @@ describe('ChatbotEnhanced — a file or image field on the record-write card rea
632632
expect(phone.querySelector('.line-through')).toHaveTextContent('13812345678');
633633
});
634634
});
635+
636+
/*
637+
* The card header's status badge says what the card body says about the
638+
* decision: the host's decision for the tool call is the one source both
639+
* read, so after a Confirm the header never still reads "Awaiting approval".
640+
* Driven by re-rendering one card through the decision states, no reload.
641+
*/
642+
describe('ChatbotEnhanced — the record-write card header badge follows the decision', () => {
643+
const BADGE_LABELS = { ...LABELS, recordWriteSubmittingLabel: '正在写入…' };
644+
// The card's header: the collapsible trigger, which holds the title and the badge.
645+
const header = () => {
646+
const trigger = document.querySelector('button[aria-expanded]');
647+
if (!trigger) throw new Error('no tool card header');
648+
return trigger as HTMLElement;
649+
};
650+
const card = (messages: ReturnType<typeof transcript>, toolDecisions?: Record<string, ToolDecisionState>) => (
651+
<ChatbotEnhanced
652+
messages={messages}
653+
onSendMessage={vi.fn()}
654+
onToolApprove={vi.fn()}
655+
onOpenRecord={vi.fn()}
656+
toolDecisions={toolDecisions}
657+
{...BADGE_LABELS}
658+
/>
659+
);
660+
661+
it.each([
662+
['create', CREATE, 'create_record'],
663+
['update', UPDATE, 'update_record'],
664+
] as const)('%s: awaiting, then in flight, then written after 确认', (_op, result, toolName) => {
665+
const messages = transcript(result, toolName);
666+
const { rerender } = render(card(messages));
667+
// CONTROL: no decision yet, the badge follows the pending tool part.
668+
expect(header()).toHaveTextContent('Awaiting approval');
669+
670+
rerender(card(messages, { c1: { state: 'pending', decision: 'approved' } }));
671+
expect(screen.getByTestId('record-write-decision')).toHaveTextContent('正在写入…');
672+
expect(header()).toHaveTextContent('正在写入…');
673+
expect(header()).not.toHaveTextContent('Awaiting approval');
674+
675+
rerender(
676+
card(messages, {
677+
c1: {
678+
state: 'success',
679+
decision: 'approved',
680+
message: 'Approved — action executed.',
681+
result: { status: 'written', objectName: 'crm_lead', recordId: 'new_1', label: '张伟' },
682+
},
683+
}),
684+
);
685+
expect(screen.getByTestId('record-write-decision')).toHaveTextContent('已写入');
686+
expect(header()).toHaveTextContent('已写入');
687+
expect(header()).not.toHaveTextContent('Awaiting approval');
688+
});
689+
690+
it('after 取消 the badge reads cancelled, as the body does', () => {
691+
const messages = transcript(CREATE);
692+
const { rerender } = render(card(messages));
693+
expect(header()).toHaveTextContent('Awaiting approval');
694+
695+
rerender(card(messages, { c1: { state: 'pending', decision: 'rejected' } }));
696+
expect(header()).toHaveTextContent('正在写入…');
697+
698+
rerender(card(messages, { c1: { state: 'success', decision: 'rejected', message: 'Rejected.' } }));
699+
expect(screen.getByTestId('record-write-decision')).toHaveTextContent('已取消');
700+
expect(header()).toHaveTextContent('已取消');
701+
expect(header()).not.toHaveTextContent('Awaiting approval');
702+
expect(header()).not.toHaveTextContent('已写入');
703+
});
704+
705+
it('a held, lapsed or refused decision reads on the badge as it does in the body, never as written', () => {
706+
const messages = transcript(CREATE);
707+
const { rerender } = render(
708+
card(messages, { c1: { state: 'held', decision: 'approved', result: { code: 'AI_WRITE_HELD' } } }),
709+
);
710+
expect(header()).toHaveTextContent('Waiting for approval');
711+
712+
rerender(card(messages, { c1: { state: 'expired', decision: 'approved' } }));
713+
expect(header()).toHaveTextContent('Expired');
714+
715+
rerender(card(messages, { c1: { state: 'error', decision: 'approved', message: 'Not written.' } }));
716+
expect(screen.getByTestId('record-write-decision')).toHaveTextContent('Not written.');
717+
expect(header()).toHaveTextContent('Error');
718+
expect(header()).not.toHaveTextContent('已写入');
719+
expect(header()).not.toHaveTextContent('Awaiting approval');
720+
});
721+
});

‎ui/packages/plugin-chatbot/src/chat/toolDetail.tsx‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import {
3131
formatRecordWriteFile,
3232
formatRecordWriteValue,
3333
recordWriteFileChange,
34+
recordWriteOutcomeOf,
3435
writtenRecordOf,
3536
type RecordWriteFile,
37+
type RecordWriteOutcome,
3638
} from './toolRecordWrite';
3739

3840
/**
@@ -189,6 +191,20 @@ export const renderToolDetail = (tool: ChatbotEnhancedToolInvocation, ctx: ToolD
189191
// three buttons; the generic Approve / Deny strip, its decision line and
190192
// the raw payload stay out of the way.
191193
const isRecordWriteCard = Boolean(tool.recordWrite);
194+
// The record-write card's decision, read once: its body line and its header
195+
// badge both say this, so the two can never disagree.
196+
const recordWriteOutcome = isRecordWriteCard ? recordWriteOutcomeOf(decision) : undefined;
197+
// Each outcome as a badge: the AI-SDK state whose icon fits it, and the word
198+
// the body uses for it. Absent outcome → the badge follows the part's state.
199+
const recordWriteBadge: Record<RecordWriteOutcome, { state: keyof ToolStatusLabels; label?: string }> = {
200+
submitting: { state: 'input-available', label: recordWriteSubmittingLabel },
201+
written: { state: 'output-available', label: recordWriteWrittenLabel },
202+
cancelled: { state: 'output-denied', label: recordWriteCancelledLabel },
203+
held: { state: 'approval-requested', label: heldForApprovalLabel },
204+
expired: { state: 'output-denied', label: expiredLabel },
205+
error: { state: 'output-error', label: toolStatusLabels['output-error'] },
206+
};
207+
const badge = recordWriteOutcome ? recordWriteBadge[recordWriteOutcome] : undefined;
192208
const hidePendingPayload =
193209
state === 'approval-requested' && Boolean(tool.pendingActionId);
194210
// objectui#7254 / cloud#1658 — `humanizeToolName`'s translator seam has
@@ -237,9 +253,9 @@ export const renderToolDetail = (tool: ChatbotEnhancedToolInvocation, ctx: ToolD
237253
>
238254
<ToolHeader
239255
type={partType}
240-
state={state}
256+
state={badge?.state ?? state}
241257
title={titleNode}
242-
statusLabels={toolStatusLabels}
258+
statusLabels={badge ? { ...toolStatusLabels, [badge.state]: badge.label } : toolStatusLabels}
243259
/>
244260
<ToolContent>
245261
{showPayload && tool.args !== undefined ? (
@@ -526,7 +542,7 @@ export const renderToolDetail = (tool: ChatbotEnhancedToolInvocation, ctx: ToolD
526542
.join(', ')}
527543
</p>
528544
) : null}
529-
{decision ? (
545+
{decision && recordWriteOutcome ? (
530546
<p
531547
data-testid="record-write-decision"
532548
data-state={decision.state}
@@ -541,34 +557,34 @@ export const renderToolDetail = (tool: ChatbotEnhancedToolInvocation, ctx: ToolD
541557
: 'text-muted-foreground')
542558
}
543559
>
544-
{decision.state === 'error' ? (
560+
{recordWriteOutcome === 'error' ? (
545561
<>
546562
<XCircle className="size-3.5" />
547563
{decision.message}
548564
</>
549-
) : decision.state === 'held' ? (
565+
) : recordWriteOutcome === 'held' ? (
550566
<>
551567
<Hourglass className="size-3.5" />
552568
{heldForApprovalLabel}
553569
</>
554-
) : decision.state === 'expired' ? (
570+
) : recordWriteOutcome === 'expired' ? (
555571
<>
556572
<Clock className="size-3.5" />
557573
{`${expiredLabel} · ${expiredDetailLabel}`}
558574
</>
559-
) : decision.state === 'pending' ? (
575+
) : recordWriteOutcome === 'submitting' ? (
560576
<>
561577
<Loader2 className="size-3.5 animate-spin" />
562578
{recordWriteSubmittingLabel}
563579
</>
564-
) : decision.decision === 'rejected' ? (
580+
) : recordWriteOutcome === 'cancelled' ? (
565581
recordWriteCancelledLabel
566582
) : (
567583
<>
568584
<CheckCircle2 className="size-3.5" />
569585
{recordWriteWrittenLabel}
570586
{(() => {
571-
const written = writtenRecordOf(decision.result);
587+
const written = writtenRecordOf(decision.state === 'success' ? decision.result : undefined);
572588
return written && onOpenRecord ? (
573589
<button
574590
type="button"

‎ui/packages/plugin-chatbot/src/chat/toolRecordWrite.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@
1111
*/
1212
import { FILE_REFERENCE_TYPES, isFileIdToken } from '@objectstack/spec/data';
1313
import type { RecordWriteField } from '../mapMessages';
14+
import type { ToolDecisionState } from './types';
15+
16+
/**
17+
* What a record-write card says about the person's decision, read from the
18+
* host's decision for the tool call and nothing else. The card body and its
19+
* header badge both read this one answer, so the badge can never still say
20+
* "Awaiting approval" while the body says the record was written. Absent
21+
* while no decision is made: the badge then follows the tool part's state.
22+
*
23+
* - `submitting`: the approve or reject is in flight.
24+
* - `written`: the write was confirmed and done.
25+
* - `cancelled`: the person rejected it (Cancel, or Open the full form).
26+
* - `held`: a policy holds the confirmed write for another approver.
27+
* - `expired`: the pending action lapsed before the decision reached it.
28+
* - `error`: the decision failed; the body shows the server's reason.
29+
*/
30+
export type RecordWriteOutcome = 'submitting' | 'written' | 'cancelled' | 'held' | 'expired' | 'error';
31+
32+
export function recordWriteOutcomeOf(decision: ToolDecisionState | undefined): RecordWriteOutcome | undefined {
33+
if (!decision) return undefined;
34+
switch (decision.state) {
35+
case 'error':
36+
return 'error';
37+
case 'held':
38+
return 'held';
39+
case 'expired':
40+
return 'expired';
41+
case 'pending':
42+
return 'submitting';
43+
case 'success':
44+
return decision.decision === 'rejected' ? 'cancelled' : 'written';
45+
}
46+
}
1447

1548
/** A record-write card value as a person reads it: nothing, a list, or the value itself. */
1649
export function formatRecordWriteValue(value: unknown): string {

0 commit comments

Comments
 (0)