Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(sampling): Max tokens ersetzt die Eingabe statt sie anzuhaengen

purple6c991f00bc64
diff --git a/src/api/__tests__/sampling-reaches-the-cloud-body.test.ts b/src/api/__tests__/sampling-reaches-the-cloud-body.test.ts
index 2c8a75a4..de9ac2dd 100644
--- a/src/api/__tests__/sampling-reaches-the-cloud-body.test.ts
+++ b/src/api/__tests__/sampling-reaches-the-cloud-body.test.ts
@@ -8,7 +8,7 @@
* lib/constants.ts:12-15, and `OpenAIChatRequest` in openai-provider.ts:86-100
* has no member it could be assigned to), and temperature goes on the wire.
* SamplingControls writes exactly four keys, all through one setter
- * (SamplingControls.tsx:57 for the three sliders, :72 for Max tokens), and
+ * (SamplingControls.tsx:77 for the three sliders, :96 for Max tokens), and
* useChat.ts:825-828 hands the same four to the provider.
*
* The value that really has no effect in the cloud is TOP K, and that is not a
@@ -33,6 +33,7 @@ import type { OpenAIChatRequest } from '../providers/openai-provider'
import type { ProviderConfig } from '../providers/types'
import { sentJson } from './provider-test-support'
import { useSettingsStore } from '../../stores/settingsStore'
+import { normalizeMaxTokens } from '../../components/chat/SamplingControls'
import { DEFAULT_SETTINGS } from '../../lib/constants'
vi.mock('../cloud/supabase', () => ({ getAccessToken: async () => 'session-token-abc' }))
@@ -133,6 +134,17 @@ describe('what the sliders put on the wire', () => {
expect(DEFAULT_SETTINGS.maxTokens).toBe(0)
expect((await cloudBody()).max_tokens).toBeUndefined()
})
+
+ it('puts the NUMBER on the wire for the text the box used to keep', async () => {
+ // The field kept `0512` on screen while the store already held 512 (T1
+ // nebenfund 5), so the one thing worth proving on a real body is that what
+ // leaves the app is a number and not the text somebody typed. `0512` is
+ // the exact string the old field produced.
+ sliders({ maxTokens: normalizeMaxTokens('0512') })
+ const body = await cloudBody()
+ expect(body.max_tokens).toBe(512)
+ expect(typeof body.max_tokens).toBe('number')
+ })
})
describe('the panel writes the keys the send reads', () => {
diff --git a/src/components/chat/SamplingControls.tsx b/src/components/chat/SamplingControls.tsx
index b67a79c1..033667a1 100644
--- a/src/components/chat/SamplingControls.tsx
+++ b/src/components/chat/SamplingControls.tsx
@@ -20,8 +20,28 @@ const FIELDS = [
{ key: 'topK', label: 'Top K', min: 0, max: 200, step: 1 },
] as const
+/**
+ * Why "Max tokens" carries a draft string instead of the plain number.
+ *
+ * React keeps a controlled `<input type="number">` in step with a LOOSE
+ * comparison (`node.value != value`). With a number on the prop side that
+ * comparison coerces, so "0512" and 512 count as equal and the DOM keeps the
+ * string it already had. Typing 512 into a field showing 0 therefore left
+ * `0512` on screen forever, and the field looked like it appended instead of
+ * replacing (measured on the box, T1 nebenfund 5). Handing React a STRING
+ * turns the same comparison into a string comparison, so the normalised text
+ * really lands in the DOM. The draft covers the other half: while the field is
+ * focused an empty box must stay empty, otherwise nobody can clear it to type
+ * a new number.
+ */
+export function normalizeMaxTokens(raw: string): number {
+ if (raw.trim() === '') return DEFAULT_SETTINGS.maxTokens
+ return Math.max(0, Math.trunc(Number(raw)) || 0)
+}
+
export function SamplingControls() {
const [open, setOpen] = useState(false)
+ const [draft, setDraft] = useState<string | null>(null)
const settings = useSettingsStore((s) => s.settings)
const update = useSettingsStore((s) => s.updateSettings)
const changed = FIELDS.some((f) => settings[f.key] !== DEFAULT_SETTINGS[f.key])
@@ -67,9 +87,15 @@ export function SamplingControls() {
className="flex-1 rounded border border-gray-700 bg-transparent px-1 py-0.5"
min={0}
step={128}
- value={settings.maxTokens}
+ value={draft ?? String(settings.maxTokens)}
aria-label="Max tokens"
- onChange={(e) => update({ maxTokens: Math.max(0, Number(e.target.value) || 0) })}
+ onChange={(e) => {
+ const raw = e.target.value
+ const next = normalizeMaxTokens(raw)
+ setDraft(raw.trim() === '' ? raw : String(next))
+ update({ maxTokens: next })
+ }}
+ onBlur={() => setDraft(null)}
/>
<span className="w-10 text-right text-gray-500">{settings.maxTokens === 0 ? 'auto' : ''}</span>
</label>
@@ -80,12 +106,15 @@ export function SamplingControls() {
type="button"
className="text-gray-400 underline disabled:opacity-40"
disabled={!changed}
- onClick={() => update({
- temperature: DEFAULT_SETTINGS.temperature,
- topP: DEFAULT_SETTINGS.topP,
- topK: DEFAULT_SETTINGS.topK,
- maxTokens: DEFAULT_SETTINGS.maxTokens,
- })}
+ onClick={() => {
+ setDraft(null)
+ update({
+ temperature: DEFAULT_SETTINGS.temperature,
+ topP: DEFAULT_SETTINGS.topP,
+ topK: DEFAULT_SETTINGS.topK,
+ maxTokens: DEFAULT_SETTINGS.maxTokens,
+ })
+ }}
>
Reset
</button>
diff --git a/src/components/chat/__tests__/sampling-controls.test.tsx b/src/components/chat/__tests__/sampling-controls.test.tsx
index 97cd9f0a..786221c4 100644
--- a/src/components/chat/__tests__/sampling-controls.test.tsx
+++ b/src/components/chat/__tests__/sampling-controls.test.tsx
@@ -50,6 +50,50 @@ describe('SamplingControls', () => {
expect(settings().maxTokens).toBe(0)
})
+ /**
+ * Max tokens used to APPEND what you typed instead of replacing it: the box
+ * showed 0, you typed 512, and `0512` stayed on screen (measured on the box,
+ * T1 nebenfund 5). React keeps a controlled number input in step with a
+ * loose comparison, so "0512" and the number 512 counted as equal and the
+ * DOM was never corrected. Typed here one keystroke at a time, each one
+ * appended to whatever the field really shows, because that is the gesture
+ * that produced the wrong number.
+ */
+ const maxTokens = () => screen.getByLabelText('Max tokens') as HTMLInputElement
+ const typeInto = (field: HTMLInputElement, chars: string) => {
+ for (const c of chars) fireEvent.change(field, { target: { value: field.value + c } })
+ }
+
+ it('replaces what stands in max tokens instead of appending to it', () => {
+ render(<SamplingControls />)
+ open()
+ const field = maxTokens()
+ expect(field.value).toBe('0')
+ typeInto(field, '512')
+ expect(field.value).toBe('512')
+ expect(settings().maxTokens).toBe(512)
+ })
+
+ it('falls back to the default when max tokens is cleared, and shows it again on blur', () => {
+ useSettingsStore.getState().updateSettings({ maxTokens: 512 })
+ render(<SamplingControls />)
+ open()
+ const field = maxTokens()
+ fireEvent.change(field, { target: { value: '' } })
+ expect(settings().maxTokens).toBe(DEFAULT_SETTINGS.maxTokens)
+ expect(field.value).toBe('')
+ fireEvent.blur(field)
+ expect(field.value).toBe(String(DEFAULT_SETTINGS.maxTokens))
+ })
+
+ it('keeps max tokens a whole number, because a fraction is not a token count', () => {
+ render(<SamplingControls />)
+ open()
+ fireEvent.change(maxTokens(), { target: { value: '512.7' } })
+ expect(settings().maxTokens).toBe(512)
+ expect(Number.isInteger(settings().maxTokens)).toBe(true)
+ })
+
it('marks a changed setup and resets every field at once', () => {
useSettingsStore.getState().updateSettings({ temperature: 1.9, topK: 5 })
render(<SamplingControls />)