fix: include full conversation history in AI chat requests

- Add trimHistory() to keep most recent messages within 100k token budget
- Add formatMessageWithHistory() to prepend conversation context to each message
- Wire up in useAIChat.ts: sendMessage now includes full history
- Keep --resume as belt-and-suspenders for same-session continuity
- 14 new tests in ai-chat.test.ts and useAIChat.test.ts
This commit is contained in:
lucaronin
2026-03-06 23:24:07 +01:00
parent 7610f1b89a
commit 21bfaa8c30
4 changed files with 250 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import type { VaultEntry } from '../types'
import {
type ChatMessage, nextMessageId,
buildSystemPrompt, streamClaudeChat,
trimHistory, formatMessageWithHistory, MAX_HISTORY_TOKENS,
} from '../utils/ai-chat'
export function useAIChat(
@@ -29,9 +30,11 @@ export function useAIChat(
abortRef.current = false
const { prompt: systemPrompt } = buildSystemPrompt(contextNotes, allContent)
const history = trimHistory(messages, MAX_HISTORY_TOKENS)
const messageWithHistory = formatMessageWithHistory(history, text.trim())
let accumulated = ''
streamClaudeChat(text.trim(), systemPrompt || undefined, sessionIdRef.current, {
streamClaudeChat(messageWithHistory, systemPrompt || undefined, sessionIdRef.current, {
onInit: (sid) => { sessionIdRef.current = sid },
onText: (chunk) => {
@@ -58,7 +61,7 @@ export function useAIChat(
}).then(sid => {
if (sid) sessionIdRef.current = sid
})
}, [isStreaming, allContent, contextNotes])
}, [isStreaming, allContent, contextNotes, messages])
const clearConversation = useCallback(() => {
abortRef.current = true