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(group): say so when the pinned first message went out capped

purple69b5e06e447e
diff --git a/src/hooks/__tests__/group-long-answer-in-the-round.test.ts b/src/hooks/__tests__/group-long-answer-in-the-round.test.ts
index 71dccadc..60e14706 100644
--- a/src/hooks/__tests__/group-long-answer-in-the-round.test.ts
+++ b/src/hooks/__tests__/group-long-answer-in-the-round.test.ts
@@ -185,3 +185,31 @@ describe('shortenLongTurns', () => {
expect(estimateMessageTokens(out.messages as never)).toBeGreaterThan(5000)
})
})
+
+describe('the pinned first message', () => {
+ it('a long first line that went out capped is said next to the answer', async () => {
+ reply = (model) => sse(`${model} answer.`)
+ const convId = openGroup()
+ useChatStore.setState((s) => ({
+ conversations: s.conversations.map((c) => (c.id === convId ? {
+ ...c,
+ messages: [
+ { id: 'u0', role: 'user' as const, content: 'Please read this. ' + 'The quick brown fox jumps. '.repeat(2700), timestamp: 1 },
+ { id: 'a0', role: 'assistant' as const, content: 'qwen05 read it.', modelId: A, timestamp: 2 },
+ { id: 'a1', role: 'assistant' as const, content: 'qwen05-zwei read it.', modelId: B, timestamp: 3 },
+ { id: 'a2', role: 'assistant' as const, content: 'coder15 read it.', modelId: C, timestamp: 4 },
+ { id: 'u1', role: 'user' as const, content: 'hallo', timestamp: 5 },
+ { id: 'a3', role: 'assistant' as const, content: 'qwen05 hallo.', modelId: A, timestamp: 6 },
+ { id: 'a4', role: 'assistant' as const, content: 'qwen05-zwei hallo.', modelId: B, timestamp: 7 },
+ { id: 'a5', role: 'assistant' as const, content: 'coder15 hallo.', modelId: C, timestamp: 8 },
+ ],
+ } : c)),
+ }))
+ await round('und noch einmal')
+
+ const budget = chatSendBudget({ providerId: 'openai', modelWindow: 16384, contextDecay: true, localBackend: true })!
+ for (const r of requests) expect(wireSize(r)).toBeLessThanOrEqual(budget)
+ const said = turns(convId).slice(-3)
+ expect(said.every((m) => (m.historyTrim?.shortened ?? 0) >= 1)).toBe(true)
+ })
+})
diff --git a/src/hooks/useChat.ts b/src/hooks/useChat.ts
index 9b19f4be..93938747 100644
--- a/src/hooks/useChat.ts
+++ b/src/hooks/useChat.ts
@@ -230,7 +230,7 @@ async function runGroupTurn(convId: string, model: string, allModels: string[],
let messages = fitted.messages
// Said in the chat, next to this answer: older turns did not go out.
let dropped = full.length - counted.length + sent.droppedMessages
- let shortened = fitted.shortened
+ let shortened = (sent.shortenedMessages ?? 0) + fitted.shortened
const noteTrim = () => useChatStore.getState().updateMessageHistoryTrim(
convId, assistantMessage.id,
dropped > 0 || shortened > 0 ? { dropped, window: modelWindow, ...(shortened ? { shortened } : {}) } : null,
@@ -324,7 +324,8 @@ async function runGroupTurn(convId: string, model: string, allModels: string[],
log.info('chat.group_context_overflow_retry', { sent: size, next, window: overflow.window, promptTokens: overflow.promptTokens, kept: cut.messages.length, shortened: fit.shortened })
messages = fit.messages
dropped += cut.dropped
- shortened += fit.shortened
+ // The pin is capped again on the retry: the same message, not a second one.
+ shortened = Math.max(shortened, cut.shortened) + fit.shortened
noteTrim()
yield* provider.chatStream(modelId, messages, streamOpts)
}
diff --git a/src/lib/chat-send-budget.ts b/src/lib/chat-send-budget.ts
index 1e69e5ed..7ea6f7e6 100644
--- a/src/lib/chat-send-budget.ts
+++ b/src/lib/chat-send-budget.ts
@@ -194,6 +194,8 @@ export interface ChatSendResult<T> {
droppedImages: number
/** Older messages the token budget left out of this payload (GH #201). */
droppedMessages: number
+ /** Messages that went out cut to fit (the pinned first user message). */
+ shortenedMessages?: number
}
export interface ApplyBudgetOptions {
@@ -249,6 +251,7 @@ export function applySendBudget<T extends { role: string; content?: unknown }>(
promptTokens: estimateMessageTokens(out as unknown as Estimated),
droppedImages: aged.strippedImages,
droppedMessages: compacted.dropped,
+ shortenedMessages: compacted.shortened,
}
}
diff --git a/src/lib/context-compaction.ts b/src/lib/context-compaction.ts
index 99936816..78a77992 100644
--- a/src/lib/context-compaction.ts
+++ b/src/lib/context-compaction.ts
@@ -355,13 +355,15 @@ export function compactMessages(
* compactMessages, and how many of the older messages it left out. The count
* is what the chat shows under an answer whose request was trimmed (GH #201),
* so nobody has to guess from a token counter that turns were dropped.
+ * `shortened` is 1 when the pinned first user message went out capped: it is
+ * not counted as dropped, and without the count it was cut without a word.
*/
export function compactMessagesWithCount(
messages: OllamaChatMessage[],
maxTokens: number,
opts: CompactOptions = {},
-): { messages: OllamaChatMessage[]; dropped: number } {
- const unchanged = { messages, dropped: 0 }
+): { messages: OllamaChatMessage[]; dropped: number; shortened: number } {
+ const unchanged = { messages, dropped: 0, shortened: 0 }
const currentTokens = estimateMessageTokens(messages)
const triggerTokens = opts.hysteresis ? Math.floor(maxTokens * COMPACT_TRIGGER_RATIO) : maxTokens
const targetTokens = opts.hysteresis ? Math.floor(maxTokens * COMPACT_TARGET_RATIO) : maxTokens
@@ -503,6 +505,7 @@ export function compactMessagesWithCount(
strip: stripTrimNotice,
}),
dropped: Math.max(0, droppedCount),
+ shortened: pinNeeded && rawTask !== null && rawTask.length > PINNED_TASK_MAX_CHARS ? 1 : 0,
}
}