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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
feat(effort): die Stufenleiter als eine reine, getestete Stelle
purple140c09765363
diff --git a/src/lib/__tests__/effort.test.ts b/src/lib/__tests__/effort.test.tsnew file mode 100644index 00000000..0eb6af44--- /dev/null+++ b/src/lib/__tests__/effort.test.ts@@ -0,0 +1,157 @@+/**+ * The effort ladder, the arithmetic of it.+ *+ * Three things have to hold, and each one has a live measurement behind it+ * (ops/wissen/deepinfra-modellmatrix-2026-09-02.md, 52 models against the real+ * API on 2026-09-02):+ *+ * 1. CLAMPING. Qwen/Qwen3.8-27B answers 400 to 'high' AND 'max' and names+ * 'low' and 'medium' as its rungs. A composer that sends one fixed scale to+ * every model breaks every request on that one model.+ * 2. NO LADDER, NO CHANGE. A server that predates the field sends nothing, and+ * nothing has to mean the 'high' this client has always sent. Otherwise an+ * update moves an existing customer's token bill without them touching a+ * thing.+ * 3. 'none' IS NOT A RUNG. On GLM 5.3 'none' does not stop the thinking, it+ * only stops the upstream from separating it: the monologue lands in the+ * customer's chat window and burns MORE tokens than sending no parameter at+ * all (53/125/130 tokens clean against 102/113/135 with none, and no answer+ * inside the budget). A model that always reasons must therefore never be+ * offered 'none' by anything here.+ *+ * Run: npx vitest run src/lib/__tests__/effort.test.ts+ */+import { describe, it, expect } from 'vitest'+import {+ clampEffort,+ effortChoices,+ effortLabel,+ hasEffortLadder,+ lowerEffort,+ nextEffort,+ isEffortLevel,+ DEFAULT_EFFORT,+ EFFORT_STEPS,+} from '../effort'++const GLM53 = ['low', 'medium', 'high', 'max']+const MOST = ['low', 'medium', 'high']+const QWEN38_27B = ['minimal', 'low', 'medium']++describe('clamping the wish onto the rungs a model really has', () => {+ it('leaves a wish the model knows exactly as it is', () => {+ expect(clampEffort(GLM53, 'medium')).toBe('medium')+ expect(clampEffort(GLM53, 'max')).toBe('max')+ })++ it('THE MEASUREMENT: max on a model without max becomes its top rung, not a 400', () => {+ expect(clampEffort(MOST, 'max')).toBe('high')+ expect(clampEffort(QWEN38_27B, 'max')).toBe('medium')+ expect(clampEffort(QWEN38_27B, 'high')).toBe('medium')+ })++ it('rounds UP only when there is nothing at or below the wish', () => {+ expect(clampEffort(['medium', 'high'], 'low')).toBe('medium')+ })++ it('takes the rungs in any order the server happens to send them', () => {+ expect(clampEffort(['max', 'low', 'high'], 'medium')).toBe('low')+ })++ it('drops words it does not know instead of forwarding them', () => {+ // 'xhigh' shows up in one upstream error message and in no documentation.+ // Forwarding a word we cannot rank is how an invalid value reaches a model+ // that reads invalid as "think as hard as you can".+ expect(clampEffort(['low', 'xhigh'], 'high')).toBe('low')+ expect(clampEffort(['xhigh', 'ludicrous'], 'high')).toBe(DEFAULT_EFFORT)+ })++ it('treats a wish it does not know as the default wish, never as the top rung', () => {+ expect(clampEffort(GLM53, 'ludicrous')).toBe('high')+ expect(clampEffort(GLM53, 'ludicrous')).not.toBe('max')+ })+})++describe('no ladder means no change', () => {+ it('an absent ladder answers with the rung this client has always sent', () => {+ expect(clampEffort(undefined, 'low')).toBe('high')+ expect(clampEffort([], 'max')).toBe('high')+ expect(DEFAULT_EFFORT).toBe('high')+ })++ it('and offers no rungs to the composer, so no control is drawn', () => {+ expect(effortChoices(undefined)).toEqual([])+ expect(effortChoices([])).toEqual([])+ expect(hasEffortLadder(undefined)).toBe(false)+ expect(hasEffortLadder(['nonsense'])).toBe(false)+ expect(hasEffortLadder(MOST)).toBe(true)+ })+})++describe("'none' is the off switch and never a rung", () => {+ it('a server that sends none among the levels does not get it offered', () => {+ expect(effortChoices(['none', 'low', 'high'])).toEqual(['low', 'high'])+ expect(clampEffort(['none', 'low'], 'max')).toBe('low')+ })++ it('nothing on this ladder ever answers none, on any wish', () => {+ for (const wish of ['none', 'minimal', 'low', 'medium', 'high', 'max', 'ludicrous']) {+ expect(clampEffort(GLM53, wish)).not.toBe('none')+ expect(nextEffort(GLM53, wish)).not.toBe('none')+ expect(lowerEffort(GLM53, wish)).not.toBe('none')+ }+ })++ it('and minimal is not offered either, the Think button already says off', () => {+ expect(effortChoices(QWEN38_27B)).toEqual(['low', 'medium'])+ expect(EFFORT_STEPS).toEqual(['low', 'medium', 'high', 'max'])+ })+})++describe('walking one rung down for the retry ladder', () => {+ it('finds the next rung below', () => {+ expect(lowerEffort(GLM53, 'max')).toBe('high')+ expect(lowerEffort(GLM53, 'high')).toBe('medium')+ expect(lowerEffort(QWEN38_27B, 'high')).toBe('medium')+ })++ it('runs out at the cheapest rung instead of inventing one', () => {+ expect(lowerEffort(GLM53, 'low')).toBeUndefined()+ expect(lowerEffort(undefined, 'high')).toBeUndefined()+ expect(lowerEffort(GLM53, 'ludicrous')).toBeUndefined()+ })+})++describe('the cycling button', () => {+ it('walks the rungs the model has and wraps at the top', () => {+ expect(nextEffort(GLM53, 'low')).toBe('medium')+ expect(nextEffort(GLM53, 'high')).toBe('max')+ expect(nextEffort(GLM53, 'max')).toBe('low')+ })++ it('wraps on a short ladder without ever leaving it', () => {+ expect(nextEffort(QWEN38_27B, 'medium')).toBe('low')+ expect(nextEffort(MOST, 'high')).toBe('low')+ })++ it('starts from the clamped rung, so the display and the next click agree', () => {+ // The user's global wish is 'max', this model tops out at 'high': the+ // button shows High and one click has to give Low, not Low-via-Max.+ expect(nextEffort(MOST, 'max')).toBe('low')+ })+})++describe('labels and the type guard', () => {+ it('says the rung in one short word', () => {+ expect(effortLabel('low')).toBe('Low')+ expect(effortLabel('medium')).toBe('Medium')+ expect(effortLabel('high')).toBe('High')+ expect(effortLabel('max')).toBe('Max')+ })++ it('knows a real rung from a stored typo', () => {+ expect(isEffortLevel('high')).toBe(true)+ expect(isEffortLevel('none')).toBe(false)+ expect(isEffortLevel('xhigh')).toBe(false)+ })+})diff --git a/src/lib/effort.ts b/src/lib/effort.tsnew file mode 100644index 00000000..b20aa3a5--- /dev/null+++ b/src/lib/effort.ts@@ -0,0 +1,112 @@+/**+ * The reasoning-effort ladder: one place that decides which rung a request+ * actually asks for.+ *+ * Why a ladder per model instead of one fixed scale. Measured against the live+ * DeepInfra API on 2026-09-02 (ops/wissen/deepinfra-modellmatrix-2026-09-02.md):+ * most reasoners take low, medium and high, GLM 5.3 adds max, and+ * Qwen/Qwen3.8-27B answers 400 to BOTH high and max, naming low and medium as+ * the only rungs it has. A composer that sends one fixed scale to every model+ * breaks every single request on that last one.+ *+ * So the server declares the rungs per model (`reasoning_effort_levels` on+ * /api/inference/v1/models) and everything here clamps the user's wish onto+ * them. An older server sends no field, the ladder is empty, and an empty+ * ladder has to mean "exactly like before": 'high' while thinking is on, and+ * no effort control in the composer at all.+ *+ * 'none' is deliberately NOT a rung here. It is the off switch, the Think+ * button already says that, and on GLM 5.3 it is worse than useless: the model+ * keeps thinking and only stops separating the thought, so the monologue lands+ * in the customer's chat window and costs more tokens than sending nothing.+ */++/** Ascending, cheapest first. The off switch is not part of it. */+const RANK: Record<string, number> = { minimal: 0, low: 1, medium: 2, high: 3, max: 4 }++/**+ * The rungs the composer offers. 'minimal' is not one of them: it is another+ * way of saying off, and two controls that both claim to turn thinking off is+ * how a user ends up disbelieving both.+ */+export const EFFORT_STEPS = ['low', 'medium', 'high', 'max'] as const++export type EffortLevel = (typeof EFFORT_STEPS)[number]++/**+ * What we ask for when nothing else is known. It is 'high' because that is+ * what the client has always sent for thinking ON, and a release that quietly+ * changes the rung changes every existing customer's bill without asking.+ */+export const DEFAULT_EFFORT: EffortLevel = 'high'++export function isEffortLevel(value: string): value is EffortLevel {+ return (EFFORT_STEPS as readonly string[]).includes(value)+}++/** The declared rungs we understand, ascending. Unknown words are dropped. */+function known(levels: readonly string[] | undefined): string[] {+ return (levels ?? []).filter((l) => l in RANK).sort((a, b) => RANK[a] - RANK[b])+}++/** Does this model declare any rung we understand? */+export function hasEffortLadder(levels: readonly string[] | undefined): boolean {+ return known(levels).length > 0+}++/** The rungs to show in the composer, in ladder order. Empty = show nothing. */+export function effortChoices(levels: readonly string[] | undefined): EffortLevel[] {+ const ladder = known(levels)+ return EFFORT_STEPS.filter((step) => ladder.includes(step))+}++/**+ * The wish, clamped onto the rungs this model really has.+ *+ * Down before up: a wish above the model's top rung becomes that top rung+ * (asking Qwen3.8 27B for 'max' would 400), and only a wish below the cheapest+ * rung is rounded up. No ladder at all returns DEFAULT_EFFORT, which is what+ * the client sent before any of this existed.+ */+export function clampEffort(levels: readonly string[] | undefined, wanted: string): string {+ const ladder = known(levels)+ if (ladder.length === 0) return DEFAULT_EFFORT+ if (ladder.includes(wanted)) return wanted+ const rank = RANK[wanted]+ // A word we do not know is treated as the default wish, never as "the most+ // the model can do": on GLM 5.3 an invalid value silently means max, and+ // guessing upward is the one guess the customer pays for.+ if (rank === undefined) return clampEffort(ladder, DEFAULT_EFFORT)+ const below = ladder.filter((l) => RANK[l] < rank).pop()+ return below ?? ladder[0]+}++/**+ * The next rung DOWN, for the retry ladder. A model that refuses 'high' still+ * reasons at 'medium'; dropping the field instead hands the turn to the+ * upstream default, which is the most expensive setting there is.+ */+export function lowerEffort(+ levels: readonly string[] | undefined,+ current: string,+): string | undefined {+ const rank = RANK[current]+ if (rank === undefined) return undefined+ return known(levels).filter((l) => RANK[l] < rank).pop()+}++/** The next rung for the cycling button, wrapping at the top of the ladder. */+export function nextEffort(+ levels: readonly string[] | undefined,+ current: string,+): EffortLevel {+ const choices = effortChoices(levels)+ if (choices.length === 0) return DEFAULT_EFFORT+ const at = choices.indexOf(clampEffort(choices, current) as EffortLevel)+ return choices[(at + 1) % choices.length]+}++/** Short label for the composer button. */+export function effortLabel(level: string): string {+ return level.charAt(0).toUpperCase() + level.slice(1)+}