mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-01 18:05:32 +08:00
fix(claude): preserve local command artifacts in session history
Claude writes slash-command metadata, local stdout, and compaction summaries into the same JSONL stream as normal chat messages. The existing normalization path treated those rows as internal content and dropped them entirely. That made the web UI diverge from the CLI transcript and removed important context. Commands like /compact appeared to have never happened, the stdout status line disappeared, and the continuation summary after compaction was filtered out even though it best describes the post-boundary session state. This change keeps the distinction between truly internal transcript rows and user-visible local command artifacts. Command wrapper tags are parsed into structured metadata without exposing the raw tags, local command stdout is remapped to assistant text, and compact summaries are preserved as assistant-authored content instead of being mislabeled as user input. Search and session-summary parsing are updated for the same reason. If history normalization preserved these rows but search still ignored them, rendered conversation state and searchable conversation state would continue to disagree, and session summaries would fall back to stale user text instead of Claude's actual compaction summary. The shared message and store typings are extended so this metadata survives the full backend-to-frontend pipeline. That avoids reconstructing meaning later and keeps the transcript faithful to Claude's persisted history while still hiding genuinely internal control content.
This commit is contained in:
@@ -89,13 +89,8 @@ const RIPGREP_CHUNK_CONCURRENCY = 6;
|
||||
const UNKNOWN_PROJECT_KEY = '__unknown_project__';
|
||||
|
||||
const INTERNAL_CONTENT_PREFIXES = [
|
||||
'<command-name>',
|
||||
'<command-message>',
|
||||
'<command-args>',
|
||||
'<local-command-stdout>',
|
||||
'<system-reminder>',
|
||||
'Caveat:',
|
||||
'This session is being continued from a previous',
|
||||
'Invalid API key',
|
||||
'[Request interrupted',
|
||||
] as const;
|
||||
@@ -302,6 +297,135 @@ function extractClaudeText(content: unknown): string {
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
function extractTaggedContent(content: string, tagName: string): string | null {
|
||||
const escapedTagName = tagName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const match = new RegExp(`<${escapedTagName}>([\\s\\S]*?)<\\/${escapedTagName}>`).exec(content);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
type ClaudeLocalCommandPayload = {
|
||||
commandName: string;
|
||||
commandMessage: string;
|
||||
commandArgs: string;
|
||||
};
|
||||
|
||||
function parseClaudeLocalCommandPayload(content: string): ClaudeLocalCommandPayload | null {
|
||||
const commandName = extractTaggedContent(content, 'command-name');
|
||||
const commandMessage = extractTaggedContent(content, 'command-message');
|
||||
const commandArgs = extractTaggedContent(content, 'command-args');
|
||||
|
||||
if (commandName === null && commandMessage === null && commandArgs === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
commandName: commandName ?? '',
|
||||
commandMessage: commandMessage ?? '',
|
||||
commandArgs: commandArgs ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
function buildClaudeLocalCommandDisplayText(payload: ClaudeLocalCommandPayload): string {
|
||||
const commandName = payload.commandName.trim();
|
||||
const commandMessage = payload.commandMessage.trim();
|
||||
const commandArgs = payload.commandArgs.trim();
|
||||
const baseCommand = commandName || commandMessage;
|
||||
|
||||
if (!baseCommand) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return commandArgs ? `${baseCommand} ${commandArgs}` : baseCommand;
|
||||
}
|
||||
|
||||
function stripAnsiFormatting(text: string): string {
|
||||
return text.replace(/\u001B\[[0-9;?]*[ -/]*[@-~]/g, '');
|
||||
}
|
||||
|
||||
type ClaudeSearchableMessage = {
|
||||
text: string;
|
||||
role: 'user' | 'assistant';
|
||||
};
|
||||
|
||||
/**
|
||||
* Claude mixes visible chat, compact summaries, and local command wrappers into
|
||||
* the same transcript stream. Search should operate on the user-visible meaning
|
||||
* of those rows rather than the raw wrapper syntax.
|
||||
*/
|
||||
function extractClaudeSearchableMessage(entry: AnyRecord): ClaudeSearchableMessage | null {
|
||||
if (!entry.message?.content || entry.isApiErrorMessage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawRole = entry.message.role;
|
||||
if (rawRole !== 'user' && rawRole !== 'assistant') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof entry.message.content === 'string') {
|
||||
const content = String(entry.message.content);
|
||||
|
||||
if (entry.isCompactSummary === true && content.trim()) {
|
||||
return {
|
||||
text: content,
|
||||
role: 'assistant',
|
||||
};
|
||||
}
|
||||
|
||||
const localCommand = parseClaudeLocalCommandPayload(content);
|
||||
if (localCommand) {
|
||||
const displayText = buildClaudeLocalCommandDisplayText(localCommand);
|
||||
return displayText
|
||||
? {
|
||||
text: displayText,
|
||||
role: 'user',
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
const localCommandStdout = extractTaggedContent(content, 'local-command-stdout');
|
||||
if (localCommandStdout !== null) {
|
||||
const stdoutText = stripAnsiFormatting(localCommandStdout).trim();
|
||||
return stdoutText
|
||||
? {
|
||||
text: stdoutText,
|
||||
role: 'assistant',
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
if (!content || isInternalContent(content)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
text: content,
|
||||
role: rawRole,
|
||||
};
|
||||
}
|
||||
|
||||
const text = extractClaudeText(entry.message.content);
|
||||
if (!text) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (entry.isCompactSummary === true) {
|
||||
return {
|
||||
text,
|
||||
role: 'assistant',
|
||||
};
|
||||
}
|
||||
|
||||
if (isInternalContent(text)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
text,
|
||||
role: rawRole,
|
||||
};
|
||||
}
|
||||
|
||||
function extractCodexText(content: unknown): string {
|
||||
if (typeof content === 'string') {
|
||||
return content;
|
||||
@@ -733,18 +857,21 @@ async function parseClaudeSessionMatches(
|
||||
}
|
||||
}
|
||||
|
||||
if (!entry.message?.content || entry.isApiErrorMessage) {
|
||||
const searchableMessage = extractClaudeSearchableMessage(entry);
|
||||
if (!searchableMessage) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const role = entry.message.role;
|
||||
if (role !== 'user' && role !== 'assistant') {
|
||||
continue;
|
||||
}
|
||||
const { text, role } = searchableMessage;
|
||||
|
||||
const text = extractClaudeText(entry.message.content);
|
||||
if (!text || isInternalContent(text)) {
|
||||
continue;
|
||||
/**
|
||||
* Claude compact summaries are the most faithful session-summary source
|
||||
* after a `/compact` because they describe the post-compaction state that
|
||||
* the resumed session actually continues from. Prefer them over generic
|
||||
* fallback user text when present.
|
||||
*/
|
||||
if (entry.isCompactSummary === true) {
|
||||
state.resolvedSummary = text;
|
||||
}
|
||||
|
||||
if (role === 'user') {
|
||||
|
||||
Reference in New Issue
Block a user