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
fix(engine): fit the reasoning rung onto the rungs the chat template renders
purple67cdb1c47aff
diff --git a/src/api/providers/__tests__/template-effort-levels.test.ts b/src/api/providers/__tests__/template-effort-levels.test.tsnew file mode 100644index 00000000..9b986e0a--- /dev/null+++ b/src/api/providers/__tests__/template-effort-levels.test.ts@@ -0,0 +1,244 @@+/**+ * 3.0.11: a chat template with rungs of its own (bug report on 3.0.10).+ *+ * Qwen3.8-27B-Uncensored-HauhauCS-Aggressive-IQ2_M on the LU Engine: every+ * message died with+ *+ * Jinja Exception: Unexpected reasoning effort high. Supported types are+ * xhigh (default), medium, and low.+ *+ * LU offers Low, Medium and High for every engine model, and b11960 (pin+ * since 3.0.9) hands the template both `chat_template_kwargs` and a top-level+ * `reasoning_effort`. The fix reads the rungs the template renders from+ * /props (`chat_template_caps.reasoning_effort_levels`, llama.cpp+ * common/jinja/caps.cpp) and fits every rung onto them before the send.+ *+ * The fake engine below behaves like that template: it renders low, medium+ * and xhigh, defaults to xhigh, and answers anything else with the exact+ * error from the report.+ *+ * Run: npx vitest run src/api/providers/__tests__/template-effort-levels.test.ts+ */+import { describe, it, expect, vi, afterEach } from 'vitest'+import type { ProviderConfig, ChatStreamChunk } from '../types'+import type { OpenAIChatRequest } from '../openai-provider'+import { fitRungToTemplate, templateEffortLevels } from '../../../lib/local-effort'++const REPORTED_ERROR =+ 'While executing CallExpression at line 49, column 28 in source:\n' ++ "...raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.')\n" ++ 'Error: Jinja Exception: Unexpected reasoning effort high. Supported types are xhigh (default), medium, and low.'++/** The part of the template that matters here, as the model ships it. */+const QWEN_TEMPLATE =+ "{%- if reasoning_effort is not defined %}{%- set reasoning_effort = 'xhigh' %}{%- endif %}" ++ "{%- if reasoning_effort not in ['xhigh', 'medium', 'low'] %}" ++ "{{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.') }}" ++ '{%- endif %}'++/** What b11960 reports in /props for that template. */+const QWEN_PROPS = {+ chat_template: QWEN_TEMPLATE,+ chat_template_caps: { supports_reasoning_effort: true, reasoning_effort_levels: ['low', 'medium', 'xhigh'] },+}++const SUPPORTED = ['low', 'medium', 'xhigh']+const streamBody = 'data: {"choices":[{"delta":{"content":"hi"}}]}\n\ndata: [DONE]\n\n'++type Init = { body?: string }+let sent: OpenAIChatRequest[]+let props: unknown++/** The rung b11960 hands the template: top-level wins over the kwargs+ * (tools/server/server-common.cpp), absent means the template default. */+function templateRung(body: OpenAIChatRequest): string | undefined {+ if (typeof body.reasoning_effort === 'string' && body.reasoning_effort !== 'none') return body.reasoning_effort+ const kw = body.chat_template_kwargs?.reasoning_effort+ return typeof kw === 'string' ? kw : undefined+}++function answer(url: string, init: Init | undefined): Response {+ if (typeof init?.body === 'string') {+ const body = JSON.parse(init.body) as OpenAIChatRequest+ sent.push(body)+ const rung = templateRung(body)+ if (rung !== undefined && !SUPPORTED.includes(rung)) {+ const msg = REPORTED_ERROR.replace(/effort high\./, `effort ${rung}.`)+ return new Response(JSON.stringify({ error: { code: 500, message: msg, type: 'server_error' } }), { status: 500 })+ }+ if (body.stream) return new Response(streamBody, { status: 200 })+ return new Response(JSON.stringify({ choices: [{ message: { content: 'ok' } }] }), { status: 200 })+ }+ if (url.endsWith('/props') && props !== undefined) return new Response(JSON.stringify(props), { status: 200 })+ return new Response('not found', { status: 404 })+}++async function makeOpenAI(config: ProviderConfig, serverProps: unknown) {+ vi.resetModules()+ sent = []+ props = serverProps+ vi.doMock('../../backend', () => ({+ localFetch: vi.fn(async (url: string, init?: Init) => answer(url, init)),+ localFetchStream: vi.fn(async (url: string, init?: Init) => answer(url, init)),+ backendCall: vi.fn(),+ isPrivateOrLanHost: (host: string) => host === 'localhost' || host === '127.0.0.1',+ isDirectFetchAllowed: () => true,+ hostnameOf: (url: string) => new URL(url).hostname,+ ensureProxyAllowsHost: vi.fn(),+ isTauri: () => false,+ }))+ vi.doMock('../../builtin-ensure', () => ({+ ensureBuiltinEngineAlive: vi.fn(),+ explainDeadEngine: (e: unknown) => e,+ explainEngineTransportMessage: (m: string) => m,+ isManagedBuiltinSlot: () => false,+ }))+ const mod = await import('../openai-provider')+ return new mod.OpenAIProvider(config)+}++async function drain(gen: AsyncGenerator<ChatStreamChunk>) {+ for await (const _ of gen) { /* the fetch fires on the first next() */ }+}++const ENGINE: ProviderConfig = {+ id: 'openai', name: 'LU Engine', apiKey: '', enabled: true,+ baseUrl: 'http://127.0.0.1:8127/v1', isLocal: true, managed: true,+}+const LLAMACPP: ProviderConfig = {+ id: 'openai', name: 'llama.cpp', apiKey: '', enabled: true, baseUrl: 'http://localhost:8080/v1', isLocal: true,+}+const MODEL = 'Qwen3.8-27B-Uncensored-HauhauCS-Aggressive-IQ2_M.gguf'+const ENGINE_LEVELS = ['low', 'medium', 'high']+const hi = [{ role: 'user' as const, content: 'hi' }]++afterEach(() => {+ vi.doUnmock('../../backend')+ vi.doUnmock('../../builtin-ensure')+ vi.restoreAllMocks()+ vi.resetModules()+})++describe('templateEffortLevels: the rungs b11960 reports in /props', () => {+ it('reads chat_template_caps.reasoning_effort_levels', () => {+ expect(templateEffortLevels(QWEN_PROPS)).toEqual(['low', 'medium', 'xhigh'])+ })++ it('an older server without the list, or an empty one, says nothing', () => {+ expect(templateEffortLevels({ chat_template: QWEN_TEMPLATE })).toBeUndefined()+ expect(templateEffortLevels({ chat_template_caps: { reasoning_effort_levels: [] } })).toBeUndefined()+ expect(templateEffortLevels(undefined)).toBeUndefined()+ })+})++describe('fitRungToTemplate', () => {+ const qwen = ['low', 'medium', 'xhigh']++ it('a listed rung stays', () => {+ expect(fitRungToTemplate('low', qwen)).toBe('low')+ expect(fitRungToTemplate('medium', qwen)).toBe('medium')+ expect(fitRungToTemplate('xhigh', qwen)).toBe('xhigh')+ })++ it('high on the reported template becomes xhigh, the next rung it has', () => {+ expect(fitRungToTemplate('high', qwen)).toBe('xhigh')+ })++ it('a rung above everything the template has is dropped: the template default', () => {+ expect(fitRungToTemplate('max', qwen)).toBeUndefined()+ expect(fitRungToTemplate('high', ['low', 'medium'])).toBeUndefined()+ })++ it('an alias listed under its higher name is found', () => {+ expect(fitRungToTemplate('high', ['low', 'medium', 'max'])).toBe('max')+ })++ it('below the cheapest rung goes up to it, never to minimal', () => {+ expect(fitRungToTemplate('low', ['medium', 'high'])).toBe('medium')+ expect(fitRungToTemplate('low', ['minimal', 'high'])).toBe('high')+ })++ it('no list keeps the rung, a word llama.cpp never tries is dropped', () => {+ expect(fitRungToTemplate('high', undefined)).toBe('high')+ expect(fitRungToTemplate('high', [])).toBe('high')+ expect(fitRungToTemplate('turbo', qwen)).toBeUndefined()+ })++ it('gpt-oss keeps all three rungs it lists', () => {+ for (const r of ['low', 'medium', 'high']) expect(fitRungToTemplate(r, ['low', 'medium', 'high'])).toBe(r)+ })+})++describe('LU Engine with the reported template', () => {+ it('High picked: the template gets xhigh instead of high and the message goes through', async () => {+ const p = await makeOpenAI(ENGINE, QWEN_PROPS)+ await drain(p.chatStream(MODEL, hi, {+ thinking: true, reasoningEffort: 'high', localEffortChosen: true, effortLevels: ENGINE_LEVELS,+ }))+ expect(sent).toHaveLength(1)+ expect(sent[0].chat_template_kwargs).toEqual({ enable_thinking: true, reasoning_effort: 'xhigh' })+ expect('reasoning_effort' in sent[0]).toBe(false)+ })++ it('Max picked: the high applyLocalEffort chose becomes xhigh too', async () => {+ const p = await makeOpenAI(ENGINE, QWEN_PROPS)+ await drain(p.chatStream(MODEL, hi, {+ thinking: true, reasoningEffort: 'max', localEffortChosen: true, effortLevels: [...ENGINE_LEVELS, 'max'],+ }))+ expect(sent[0].chat_template_kwargs?.reasoning_effort).toBe('xhigh')+ })++ it('Low and Medium are rungs the template has and stay', async () => {+ const p = await makeOpenAI(ENGINE, QWEN_PROPS)+ await drain(p.chatStream(MODEL, hi, {+ thinking: true, reasoningEffort: 'medium', localEffortChosen: true, effortLevels: ENGINE_LEVELS,+ }))+ expect(sent[0].chat_template_kwargs?.reasoning_effort).toBe('medium')+ })++ it('the tool turn is fitted the same way', async () => {+ const p = await makeOpenAI(ENGINE, QWEN_PROPS)+ await p.chatWithTools(MODEL, hi, [], {+ thinking: true, reasoningEffort: 'high', localEffortChosen: true, effortLevels: ENGINE_LEVELS,+ })+ expect(sent).toHaveLength(1)+ expect(sent[0].chat_template_kwargs?.reasoning_effort).toBe('xhigh')+ })++ it('no rung picked: the request stays the one from before, nothing in the template', async () => {+ const p = await makeOpenAI(ENGINE, QWEN_PROPS)+ await drain(p.chatStream(MODEL, hi, { thinking: true, effortLevels: ENGINE_LEVELS }))+ expect(sent).toHaveLength(1)+ expect(sent[0].chat_template_kwargs).toEqual({ enable_thinking: true })+ expect('reasoning_effort' in sent[0]).toBe(false)+ })++ it('without the fix this template refuses high with the reported error (fake engine check)', async () => {+ // The same request with an engine that reports no levels, which is what+ // LU saw before: the rung goes out as high and the template raises.+ const p = await makeOpenAI(ENGINE, { chat_template: QWEN_TEMPLATE })+ let err: unknown+ try {+ await drain(p.chatStream(MODEL, hi, {+ thinking: true, reasoningEffort: 'high', localEffortChosen: true, effortLevels: ENGINE_LEVELS,+ }))+ } catch (e) { err = e }+ expect(String((err as Error)?.message ?? err)).toMatch(/Unexpected reasoning effort high\. Supported types are xhigh \(default\), medium, and low\./)+ expect(sent[0].chat_template_kwargs?.reasoning_effort).toBe('high')+ })+})++describe('self-hosted llama.cpp with the reported template', () => {+ it('the top-level high every Think-on request carries is fitted as well', async () => {+ const p = await makeOpenAI(LLAMACPP, QWEN_PROPS)+ await drain(p.chatStream(MODEL, hi, { thinking: true }))+ expect(sent).toHaveLength(1)+ expect(sent[0].reasoning_effort).toBe('xhigh')+ })++ it('Think off keeps its none, that is the off switch and not a rung', async () => {+ const p = await makeOpenAI(LLAMACPP, QWEN_PROPS)+ await drain(p.chatStream(MODEL, hi, { thinking: false }))+ expect(sent[0].reasoning_effort).toBe('none')+ })+})diff --git a/src/api/providers/openai-provider.ts b/src/api/providers/openai-provider.tsindex a639a380..08fde9bf 100644--- a/src/api/providers/openai-provider.ts+++ b/src/api/providers/openai-provider.ts@@ -39,7 +39,7 @@ import { thinkBudgetForRequest } from '../../lib/think-budget'import { chatFirstReplyMs, FIRST_REPLY_TIMEOUT } from '../../lib/chat-reply-timeout'import {applyLocalEffort, isKoboldVersion, isLmStudioV1Models, lmStudioEffortLevels, localEffortNote,- templateReadsEffort, KOBOLD_EFFORT_LEVELS, LLAMACPP_EFFORT_LEVELS, type LocalEffortServer,+ templateReadsEffort, templateEffortLevels, fitRungToTemplate, KOBOLD_EFFORT_LEVELS, LLAMACPP_EFFORT_LEVELS, type LocalEffortServer,} from '../../lib/local-effort'/** The server-wide facts cached per endpoint, see `serverFact`. */@@ -798,6 +798,70 @@ export class OpenAIProvider implements ProviderClient {* retry without `chat_template_kwargs` that needs it back. */private static engineEffortHeld = new WeakMap<OpenAIChatRequest, string>()+ /**+ * 3.0.11: every rung that can reach the template is fitted onto the rungs+ * the template renders, read from /props (lib/local-effort.ts,+ * templateEffortLevels). b11960 hands the template both+ * `chat_template_kwargs.reasoning_effort` and a top-level+ * `reasoning_effort`, and a template that raises on a rung kills the whole+ * request before a token is made: "Unexpected reasoning effort high.+ * Supported types are xhigh (default), medium, and low." (bug report on+ * 3.0.10, LU Engine). The fixed ladder low, medium, high cannot know such+ * a template; the server does. Runs here, after the engine is up, so+ * /props describes the model this request goes to. The held top-level rung+ * of the retry without kwargs is fitted too. 'none' and 'minimal' are the+ * off switch and stay as they are.+ */+ private async fitEffortToTemplate(body: OpenAIChatRequest, model: string): Promise<void> {+ if (!this.isLocalEffortSlot) return+ const isRung = (v: unknown): v is string => typeof v === 'string' && v !== '' && v !== 'none' && v !== 'minimal'+ const kwRung = body.chat_template_kwargs?.reasoning_effort+ const held = OpenAIProvider.engineEffortHeld.get(body)+ if (!isRung(body.reasoning_effort) && !isRung(kwRung) && !isRung(held)) return+ const levels = await this.templateLevels(model)+ if (!levels) return+ if (isRung(body.reasoning_effort)) {+ const fit = fitRungToTemplate(body.reasoning_effort, levels)+ if (fit === undefined) delete body.reasoning_effort+ else body.reasoning_effort = fit+ }+ if (isRung(kwRung) && body.chat_template_kwargs) {+ const fit = fitRungToTemplate(kwRung, levels)+ if (fit === undefined) {+ const rest = { ...body.chat_template_kwargs }+ delete rest.reasoning_effort+ body.chat_template_kwargs = rest+ } else {+ body.chat_template_kwargs = { ...body.chat_template_kwargs, reasoning_effort: fit }+ }+ }+ if (isRung(held)) {+ const fit = fitRungToTemplate(held, levels)+ if (fit === undefined) OpenAIProvider.engineEffortHeld.delete(body)+ else OpenAIProvider.engineEffortHeld.set(body, fit)+ }+ }++ /** The template rungs per endpoint and model, five minutes like the other+ * server facts. Keyed by model as well: /props describes the model that+ * is loaded, and the LU Engine swaps models behind one port. */+ private templateLevels(model: string): Promise<string[] | undefined> {+ const key = `${this.baseUrl}|${model}`+ const now = Date.now()+ const hit = OpenAIProvider.templateLevelsCache.get(key)+ if (hit && now - hit.at < 300_000) return hit.levels+ // No caller signal: a Stop must not leave an empty answer cached. The+ // probe has its own short timeout (probeInit).+ const entry = { at: now, levels: this.probeJson(`${serverRoot(this.baseUrl)}/props`).then(templateEffortLevels) }+ // No list is asked again after 30 s instead of 5 min: an engine that+ // was still loading must not keep a template's rungs away for long.+ void entry.levels.then((l) => { if (!l) entry.at = now - 270_000 })+ OpenAIProvider.templateLevelsCache.set(key, entry)+ return entry.levels+ }++ private static templateLevelsCache = new Map<string, { at: number; levels: Promise<string[] | undefined> }>()+/** The LU Engine or a server on this machine or the LAN, LU Cloud aside. */private get isLocalEffortSlot(): boolean {return this.config.managed === true || (this.isLanBackend && this.config.id !== 'lu-cloud')@@ -868,6 +932,8 @@ export class OpenAIProvider implements ProviderClient {)const refused = (res: Response) => !res.ok && (res.status === 400 || res.status === 422)+ await this.fitEffortToTemplate(body, model)+const asked = body.reasoning_effort// 'none' and 'minimal' are the two ways of saying off; every other rung is// the ON lane. Reading it off the value rather than off 'high' is what letsdiff --git a/src/lib/local-effort.ts b/src/lib/local-effort.tsindex ff68032c..d1c20ef7 100644--- a/src/lib/local-effort.ts+++ b/src/lib/local-effort.ts@@ -134,3 +134,47 @@ export function applyLocalEffort(body: EffortBody, server: LocalEffortServer, owreturn}}++/**+ * 3.0.11: the rungs this model's chat template really renders.+ *+ * llama.cpp b11960 (the LU Engine pin since 3.0.9) tries every rung on the+ * template once at load time and reports the ones that render distinctly in+ * /props, `chat_template_caps.reasoning_effort_levels`, lowest first+ * (common/jinja/caps.cpp, tools/server/README.md). A template that raises on+ * a rung leaves it out; rungs that render the same are aliases and only the+ * highest name is listed. Undefined when the server reports no such list+ * (an older llama.cpp, LM Studio, KoboldCpp) or the template has no rungs.+ */+export function templateEffortLevels(props: unknown): string[] | undefined {+ const levels = asStringArray(prop(prop(props, 'chat_template_caps'), 'reasoning_effort_levels'))+ return levels.length > 0 ? levels : undefined+}++/** The order llama.cpp tries the rungs in (common/jinja/caps.cpp, b11960). */+const TEMPLATE_RUNG_ORDER: readonly string[] = ['minimal', 'low', 'medium', 'high', 'xhigh', 'max']++/**+ * A rung, fitted onto the rungs the template renders (bug report 3.0.10:+ * a Qwen3.8 27B template knows only low, medium and xhigh and raises+ * "Unexpected reasoning effort high", so every message died).+ *+ * - Listed: unchanged.+ * - Not listed: the next listed rung ABOVE it. Aliases are listed under+ * their highest name, so a rung that is an alias is always found above+ * itself, and for a template that raised on it the next one up is the+ * nearest rung the template has. 'minimal' is never a target, it is a+ * way of saying off (lib/effort.ts).+ * - Nothing above it, or a word llama.cpp does not try: undefined, the+ * caller drops the field and the template keeps its own default. A+ * template that falls back silently lists its default nowhere, because+ * it renders like an unknown word, so dropping is what that rung meant.+ *+ * No list at all: unchanged, which is the request from before this fix.+ */+export function fitRungToTemplate(rung: string, levels: readonly string[] | undefined): string | undefined {+ if (!levels || levels.length === 0 || levels.includes(rung)) return rung+ const at = TEMPLATE_RUNG_ORDER.indexOf(rung)+ if (at < 0) return undefined+ return TEMPLATE_RUNG_ORDER.slice(at + 1).find((r) => r !== 'minimal' && levels.includes(r))+}