Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(chat): guard against sending a window with no user query

Akarshan Biswasbe37ed282015
diff --git a/web-app/src/lib/__tests__/custom-chat-transport.test.ts b/web-app/src/lib/__tests__/custom-chat-transport.test.ts
index 681360534..53a424784 100644
--- a/web-app/src/lib/__tests__/custom-chat-transport.test.ts
+++ b/web-app/src/lib/__tests__/custom-chat-transport.test.ts
@@ -4,6 +4,7 @@ import type { UIMessage } from '@ai-sdk/react'
import {
buildLlamacppReasoningParams,
coalesceMessagesForAlternation,
+ hasGenuineUserQuery,
extractContextInfoFromError,
normalizeToolInputSchema,
resolveOrphanToolCalls,
@@ -715,3 +716,43 @@ describe('unwrapRetryError', () => {
expect(unwrapRetryError('boom')).toBe('boom')
})
})
+
+describe('hasGenuineUserQuery', () => {
+ const toolResponseUser = (id: string, text: string): UIMessage =>
+ ({ id, role: 'user', parts: [{ type: 'text', text }] }) as UIMessage
+
+ it('returns true when a real user query is present', () => {
+ expect(
+ hasGenuineUserQuery([userMsg('u1', 'What is the capital of France?')])
+ ).toBe(true)
+ })
+
+ it('returns false for an empty message list', () => {
+ expect(hasGenuineUserQuery([])).toBe(false)
+ })
+
+ it('returns false when only assistant turns survive (user message deleted)', () => {
+ expect(hasGenuineUserQuery([assistantMsg('a1', [])])).toBe(false)
+ })
+
+ it('returns false when the only user turn is a tool_response wrapper', () => {
+ expect(
+ hasGenuineUserQuery([
+ toolResponseUser('u1', '<tool_response>{"ok":true}</tool_response>'),
+ ])
+ ).toBe(false)
+ })
+
+ it('ignores blank user turns', () => {
+ expect(hasGenuineUserQuery([userMsg('u1', ' ')])).toBe(false)
+ })
+
+ it('returns true when a real query coexists with tool_response turns', () => {
+ expect(
+ hasGenuineUserQuery([
+ toolResponseUser('u1', '<tool_response>x</tool_response>'),
+ userMsg('u2', 'Summarize the result'),
+ ])
+ ).toBe(true)
+ })
+})
diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.ts
index d4720b003..f16b7d2ab 100644
--- a/web-app/src/lib/custom-chat-transport.ts
+++ b/web-app/src/lib/custom-chat-transport.ts
@@ -453,6 +453,26 @@ export function coalesceMessagesForAlternation(
return out
}
+const TOOL_RESPONSE_ONLY = /^<tool_response>[\s\S]*<\/tool_response>$/
+
+/**
+ * A "genuine" user query is a user-role message with non-empty text that isn't
+ * entirely a <tool_response> wrapper. Qwen3.5+ chat templates raise
+ * "No user query found in messages" when none survives — e.g. the user deletes
+ * the only real user turn (leaving orphaned assistant/tool turns) or token
+ * eviction drops it. Guard the send so we fail with a clear message instead.
+ */
+export function hasGenuineUserQuery(messages: UIMessage[]): boolean {
+ return messages.some((m) => {
+ if (m.role !== 'user') return false
+ const text = (m.parts ?? [])
+ .map((p) => (p.type === 'text' ? (p.text ?? '') : ''))
+ .join('')
+ .trim()
+ return text.length > 0 && !TOOL_RESPONSE_ONLY.test(text)
+ })
+}
+
type ToolInputSchema = Record<string, unknown>
// Keep this behavior aligned with `normalize_openai_tool_parameters_schema` in Rust.
@@ -1044,6 +1064,15 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
}
}
+ // Many chat templates (Qwen3.5+) reject a window with no genuine user query
+ // and throw a cryptic Jinja error. Fail early with a clear message when
+ // deletion/eviction has left no real user turn to respond to.
+ if (!hasGenuineUserQuery(effectiveMessages)) {
+ throw new Error(
+ 'This conversation has no user message to respond to. Add a message, or regenerate from a turn that includes your question.'
+ )
+ }
+
const modelSupportsVision =
selectedModel?.capabilities?.includes('vision') ?? false
const baseMessages = await convertToModelMessages(