Skip to content

Commit bbe4258

Browse files
fix(plugin-chatbot): a send clears only the composer's message box, so the picked model survives it (#38106)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} --> _Requested by **Tesla** · [project thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6Jao2F5jFXXMeaD95BAtSDpyiW)_ Fixes #37831 repo:objectstack#3 · os-tesla · session_01L9qibxaAB2c4Epy2EEZxDf Clause-②: no Metadata-protocol: none ## 维护者速读 - 在 AI 侧栏里换了模型再提问,以后的问题会一直用这个模型,直到你自己再换。以前每次发送都会把选择悄悄改回默认模型,下一个问题就用默认模型回答、按默认倍率计费。 - 属于问 AI 对话体验这条线:用户选定的设置不能被悄悄丢掉。全页聊天用的是同一个输入框,同一处修复覆盖它,但没有单独测。 - 只改输入框发送时清空文字的方式:以前重置整张表单,现在只清空输入框本身,其他表单控件不再被一起重置。改错的话,最坏是发送后输入框里的文字没清掉,可以直接撤回。 - 无 ## What changes - `ui/packages/plugin-chatbot/src/elements/prompt-input.tsx`: `handleSubmit` no longer calls `form.reset()` to clear the composer. It sets the `message` field back to its `defaultValue`, which is what the reset gave that field, and leaves every other control in the form alone. - `patch` changeset for `@object-ui/plugin-chatbot`. ## Why The composer's model picker (`chat/ModelPicker.tsx`) is the shared Radix `Select`, and it sits inside the composer's `<form>`. Radix Select 2.3.7 listens for the `reset` event of the form it belongs to and answers it with `setValue(initialValueRef.current)`, the value it had on its FIRST render (`@radix-ui/react-select` `dist/index.mjs`, the effect that adds the `reset` listener). For the picker that is the default model's index. Because the picker is controlled, `setValue` calls `onValueChange`, so `ModelPicker` calls `onPick(defaultId)` and the host that holds the pick (`ChatDockConversation`, ADR-0028) records the default model. Every send therefore put the pick back to the default: the send itself still carried the picked model (read before the reset landed), and the next one carried the default. The earlier attempt on this card (report 7213606537) mocked `ChatbotEnhanced` and called `onSendMessage` directly, so the form submit, and with it the reset, never ran. Its other finding still holds: `ChatDockConversation` is not remounted around a turn. ## Tests `ui/packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.modelPicker-11865.test.tsx`, new top-level `describe` "a picked model survives the composer's send (objectstack#37831)". The host holds the pick, as every chat host does, and each send goes through the real form submit (Enter in the box): - picked: after each of two sends the picker still shows the picked model; `onModelChange` received only the user's pick; both sends carried the picked model; the box is empty after the send; - CONTROL, no pick: the first model is in force before and after a send, nothing is written, the box is empty. <details><summary>Readings at eb2e26d (base origin/main f72f335)</summary> - Premise at f72f335 through a throwaway probe (deleted; copy kept in the project folder): the real `ChatbotEnhanced`, a pick made in the UI, then a form submit. `onModelChange` received `["m_picked","m_default"]` and the picker showed Default after the send; CONTROL (no pick) wrote nothing; with the `reset` event swallowed in the window's capture phase and nothing else changed, the pick survived (`writes: ["m_picked"]`). - Ablation on the committed fix: `form.reset()` put back in `handleSubmit`, then `vitest run packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.modelPicker-11865.test.tsx`: `Tests 1 failed | 21 passed (22)`, the failure being the picked row (`expected 'GPT-4o mini' to be 'Claude 3.5'`), the CONTROL row green. Restored with `git restore --source=HEAD --staged --worktree`, `git status --porcelain` empty, `Tests 22 passed (22)`. - `pnpm --filter @object-ui/plugin-chatbot test`: `Test Files 76 passed (76)`, `Tests 731 passed (731)`. `type-check` exit 0. `eslint` on the two changed files: 0 errors, and the same 11 warnings as the file on `main` (all pre-existing, none on a changed line). - `vitest run packages/app-shell/src/console/ai packages/app-shell/src/layout` (the hosts that render this composer): `Test Files 210 passed (210)`, `Tests 1796 passed (1796)`. - `dispatch-gates --commands` at eb2e26d: 39 derived, 39 run, all exit 0 (`pnpm --dir ui exec vitest run scripts/` was killed once by the container's memory limit while another run was going, then re-run alone: `Test Files 172 passed | 2 skipped (174)`, exit 0). `origin/main` is 2 commits ahead, neither touching `ui/packages/plugin-chatbot`. </details> ## Acceptance notes - Not changed, file surface fenced by the claim: two comments in `ChatbotEnhanced.tsx` (in `sendTyped` and above the send-failure restore effect) still say "the composer's own form.reset clears the box optimistically". The box is still cleared optimistically at the same moment, now by the field reset, so both comments stay true in effect; the word `form.reset` in them is stale. A follow-up can reword them when that file is next touched. - The full-page chat (`AiChatPage`) and the app Ask page render the same composer, so they take the same fix. Neither was measured separately. ## Verification Read first (by risk): 1. `ui/packages/plugin-chatbot/src/elements/prompt-input.tsx`: `handleSubmit`, the clear before the blob conversion. 2. The new `describe` at the end of `ChatbotEnhanced.modelPicker-11865.test.tsx`. Two-minute check: `cd ui && pnpm exec vitest run packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.modelPicker-11865.test.tsx` shows 22 passing; put `form.reset();` back in place of the field clear in `handleSubmit` and the same command shows the picked row failing. ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (not touched) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes. - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: its generated artifacts are regenerated and committed. (not touched) - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. - [x] The *Before Submitting* list in CONTRIBUTING.md is done. --- _Generated by [Claude Code](https://claude.ai/code/session_01L9qibxaAB2c4Epy2EEZxDf)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 630a609 commit bbe4258

3 files changed

Lines changed: 91 additions & 4 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@object-ui/plugin-chatbot': patch
3+
---
4+
5+
Clause-②: no
6+
Metadata-protocol: none
7+
8+
A model picked in the chat composer now stays picked after you send. The composer cleared its message box by resetting the whole form, and the model picker in that form answered the reset by switching back to the default model, so the next question silently ran on the default. Sending now clears only the message box.

‎ui/packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.modelPicker-11865.test.tsx‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import '@testing-library/jest-dom/vitest';
4343
import React from 'react';
4444
import { describe, it, expect, vi, afterEach } from 'vitest';
45-
import { render, screen, fireEvent, cleanup, within } from '@testing-library/react';
45+
import { render, screen, fireEvent, cleanup, within, waitFor } from '@testing-library/react';
4646
import { ChatbotEnhanced } from '../ChatbotEnhanced';
4747

4848
afterEach(() => cleanup());
@@ -204,3 +204,73 @@ describe('the closed picker shows the tier name alone when the model carries one
204204
expect(onModelChange.mock.calls).toEqual([['sonnet']]);
205205
});
206206
});
207+
208+
describe('a picked model survives the composer\'s send (objectstack#37831)', () => {
209+
/**
210+
* The host holds the pick, as every chat host does (ADR-0028): what the
211+
* picker writes comes back as `selectedModelId`, and the send reads the
212+
* model in force at that moment. Each send goes through the composer's real
213+
* form submit (Enter in the box), never a direct `onSendMessage` call: the
214+
* submit is where the pick was lost. The composer used to clear its box with
215+
* `form.reset()`, and the Radix `Select` in the same form answers a reset by
216+
* writing its first-render value (the default model) back through
217+
* `onValueChange`. Ablation: restoring `form.reset()` in `handleSubmit`
218+
* (`elements/prompt-input.tsx`) reds the picked row; the CONTROL row stays green.
219+
*/
220+
function mountHeld() {
221+
const writes: string[] = [];
222+
const sends: Array<[string, string]> = [];
223+
function Host() {
224+
const [held, setHeld] = React.useState<string | undefined>(undefined);
225+
const inForce = held ?? MODELS[0].id;
226+
return (
227+
<ChatbotEnhanced
228+
models={MODELS}
229+
selectedModelId={inForce}
230+
onModelChange={(id: string) => {
231+
writes.push(id);
232+
setHeld(id);
233+
}}
234+
onSendMessage={(text: string) => {
235+
sends.push([text, inForce]);
236+
}}
237+
/>
238+
);
239+
}
240+
render(<Host />);
241+
return { writes, sends, trigger: () => screen.getByRole('combobox', { name: 'Model' }) };
242+
}
243+
244+
async function sendTyped(text: string, sentBefore: number, sends: unknown[]): Promise<void> {
245+
const box = screen.getByRole('textbox') as HTMLTextAreaElement;
246+
fireEvent.change(box, { target: { value: text } });
247+
fireEvent.keyDown(box, { key: 'Enter' });
248+
await waitFor(() => expect(sends).toHaveLength(sentBefore + 1));
249+
}
250+
251+
it('picked: the picker still shows the pick after each send, and every later send carries it', async () => {
252+
const { writes, sends, trigger } = mountHeld();
253+
await pick(trigger(), 'Claude 3.5 · anthropic');
254+
await sendTyped('first', 0, sends);
255+
expect(within(trigger()).getByTestId('model-picker-value').textContent).toBe('Claude 3.5');
256+
await sendTyped('second', 1, sends);
257+
expect(within(trigger()).getByTestId('model-picker-value').textContent).toBe('Claude 3.5');
258+
// The one write is the user's pick: the sends wrote nothing.
259+
expect(writes).toEqual(['claude-3-5-sonnet']);
260+
expect(sends).toEqual([
261+
['first', 'claude-3-5-sonnet'],
262+
['second', 'claude-3-5-sonnet'],
263+
]);
264+
// The send still clears the box.
265+
expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe('');
266+
});
267+
268+
it('CONTROL — no pick: the first model is in force before and after a send, and nothing is written', async () => {
269+
const { writes, sends, trigger } = mountHeld();
270+
await sendTyped('first', 0, sends);
271+
expect(within(trigger()).getByTestId('model-picker-value').textContent).toBe('GPT-4o mini');
272+
expect(writes).toEqual([]);
273+
expect(sends).toEqual([['first', 'gpt-4o-mini']]);
274+
expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe('');
275+
});
276+
});

‎ui/packages/plugin-chatbot/src/elements/prompt-input.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,19 @@ export const PromptInput = ({
791791
return (formData.get("message") as string) || "";
792792
})();
793793

794-
// Reset form immediately after capturing text to avoid race condition
795-
// where user input during async blob conversion would be lost
794+
// Clear the message field immediately after capturing text to avoid race
795+
// condition where user input during async blob conversion would be lost.
796+
// objectstack#37831 — only the composer's OWN field, never `form.reset()`:
797+
// a reset resets every control in the form, and a Radix `Select` there
798+
// (the composer's model picker) answers the form's `reset` event by
799+
// writing its first-render value back through `onValueChange`, so every
800+
// send silently put the picked model back to the default. The field goes
801+
// back to exactly what a reset gave it: its default value.
796802
if (!usingProvider) {
797-
form.reset();
803+
const field = form.elements.namedItem("message");
804+
if (field instanceof HTMLTextAreaElement) {
805+
field.value = field.defaultValue;
806+
}
798807
}
799808

800809
// Convert blob URLs to data URLs asynchronously

0 commit comments

Comments
 (0)