* fix: improve chat compression and tool display Context Compression Fixes: - Remove duplicate token calculation in compress() - Simplify compress() to only execute compression, not judge - Add buildConversationHistory() to preserve tool calls in LLM context - Remove unused estimateMessagesTokens() and contextLength parameter - Move all judgment logic to chat-run-socket.ts (uses accurate DB tokens) Tool Call Display Improvements: - Add tool execution duration display (format: 1.272s) - Add success/error status icons with circular backgrounds - Replace text error with SVG icon (X in red circle) - Replace old checkmark with polished green checkmark icon - Add i18n key 'chat.executionDuration' for all locales Bug Fixes: - Fix streaming-indicator stuck by adding try-finally in handleEvent - Add debug logging for compression flow diagnosis - Fix template syntax error in MessageList.vue Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(chat): convert conversation history to Anthropic format before sending to Gateway - Add convertToAnthropicFormat() to transform OpenAI format to Anthropic format - Handle DeepSeek reasoning_content in thinking blocks - Properly convert tool_use and tool_result blocks - Add convertFromAnthropicFormat() for parsing SSE responses - Handle stringified Python arrays in resume messages - Record debug history files for troubleshooting (original vs converted) - Fix tool_call_id validation to prevent empty ID errors - Clean internal Hermes fields (call_id, response_item_id) from tool_calls Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(chat): optimize message parsing and add debug logging - Only check for stringified arrays in assistant messages (performance) - Improve parsing error handling: keep original content on parse failure - Add debug logging for upstream events (reasoning/thinking tracking) - Log run.completed event keys for troubleshooting Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(chat): add message pagination and reasoning sync improvements **Message Pagination:** - Add getSessionDetailPaginated() for paginated message loading - Query with DESC order then reverse in code for optimal performance - Remove listSessionsPaginated() (not needed) **Reasoning Sync:** - Add bidirectional reasoning merge in syncFromHermes - Memory → DB: preserve streamed reasoning from SSE events - DB → Memory: restore reasoning if Hermes Gateway fixes storage - Send resumed event after sync completes with complete messages - Fix reasoning field inconsistency: use unified 'reasoning' field **Message Parsing:** - Only parse stringified arrays for assistant messages (performance) - Improve parse error handling: keep original content on failure - Add debug logging for upstream reasoning/thinking events **Bug Fixes:** - Fix reasoning content display: now works on both SSE and resume - Ensure reasoning is preserved across page refreshes via sync + resumed event Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: increase default pagination limit for messages to 500 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: remove auto-resumed event trigger and clean up debug code - Remove automatic resumed event trigger in syncFromHermes to avoid timing issues - Clean up unused imports (fs, join) - Remove debug history file logging code - Fix socket parameter passing in handleAbort, markCompleted, and syncFromHermes - Change usage emit from room broadcast to socket-only emit - Remove console.log debug statement Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: use reasoning field in convertToAnthropicFormat Change convertToAnthropicFormat to read from reasoning field instead of reasoning_content for consistency with database schema and frontend. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat: parse stringified array content and improve logs - Parse stringified array format in run.completed to extract thinking/text/tool_use - Send parsed content to frontend via parsed_content/parsed_reasoning/parsed_tool_calls - Frontend updates last assistant message with parsed content - Remove ellipsis from log messages, show full content - Add detailed logging for conversion and parsing Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: move finalOutputTrimmed outside else block * fix(chat): handle double-serialized content in resumeSession - Remove outer quotes before parsing stringified array format - Updated changelog for v0.5.2 and v0.5.3 with multilingual support - Fixed message pagination with DESC query + array reverse Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(chat): improve error logging for resume parsing - Add detailed logging for double-serialized content parsing - Log content preview when parsing fails to diagnose issues Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * revert(chat): use simple Python-to-JSON replacement - Revert to simple .replace(/'/g, '"') approach - Parsing failures will keep original content as-is Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
416 lines
10 KiB
Vue
416 lines
10 KiB
Vue
<script setup lang="ts">
|
|
import { ref, computed, watch, nextTick } from "vue";
|
|
import { useI18n } from "vue-i18n";
|
|
import MessageItem from "./MessageItem.vue";
|
|
import { useChatStore } from "@/stores/hermes/chat";
|
|
import thinkingVideoLight from "@/assets/thinking-light.mp4";
|
|
import thinkingVideoDark from "@/assets/thinking-dark.mp4";
|
|
import { useTheme } from "@/composables/useTheme";
|
|
|
|
const chatStore = useChatStore();
|
|
const { t } = useI18n();
|
|
const { isDark } = useTheme();
|
|
const listRef = ref<HTMLElement>();
|
|
|
|
function formatTokens(n: number): string {
|
|
if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + 'M'
|
|
if (n >= 1_000) return (n / 1_000).toFixed(1) + 'K'
|
|
return String(n)
|
|
}
|
|
|
|
function formatToolDuration(seconds: number): string {
|
|
if (seconds < 1) return `${Math.round(seconds * 1000)}ms`
|
|
if (seconds < 60) return `${Math.round(seconds * 10) / 10}s`
|
|
const mins = Math.floor(seconds / 60)
|
|
const secs = Math.round(seconds % 60)
|
|
return `${mins}m ${secs}s`
|
|
}
|
|
|
|
const displayMessages = computed(() =>
|
|
chatStore.messages.filter((m) => m.role !== "tool"),
|
|
);
|
|
|
|
const currentToolCalls = computed(() => {
|
|
const msgs = chatStore.messages;
|
|
// Find the last user message index
|
|
let lastUserIdx = -1;
|
|
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
if (msgs[i].role === "user") {
|
|
lastUserIdx = i;
|
|
break;
|
|
}
|
|
}
|
|
// Only tool calls after the last user message, newest on top
|
|
const tools = msgs.filter((m, i) => m.role === "tool" && i > lastUserIdx);
|
|
return [...tools].reverse();
|
|
});
|
|
|
|
function isNearBottom(threshold = 200): boolean {
|
|
const el = listRef.value;
|
|
if (!el) return true;
|
|
return el.scrollHeight - el.scrollTop - el.clientHeight < threshold;
|
|
}
|
|
|
|
function scrollToBottom() {
|
|
nextTick(() => {
|
|
if (listRef.value) {
|
|
listRef.value.scrollTop = listRef.value.scrollHeight;
|
|
}
|
|
});
|
|
}
|
|
|
|
function scrollToMessage(messageId: string) {
|
|
nextTick(() => {
|
|
const el = document.getElementById(`message-${messageId}`);
|
|
if (el) {
|
|
el.scrollIntoView({ block: 'center' });
|
|
}
|
|
});
|
|
}
|
|
|
|
// Scroll to bottom on session switch
|
|
watch(
|
|
() => chatStore.activeSessionId,
|
|
(id) => {
|
|
if (!id) return;
|
|
if (chatStore.focusMessageId) {
|
|
nextTick(() => scrollToMessage(chatStore.focusMessageId!));
|
|
return;
|
|
}
|
|
nextTick(() => scrollToBottom());
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
watch(
|
|
() => chatStore.focusMessageId,
|
|
(messageId) => {
|
|
if (!messageId) return;
|
|
scrollToMessage(messageId);
|
|
},
|
|
);
|
|
|
|
// When a run starts (user just sent a message), always scroll to bottom once
|
|
watch(
|
|
() => chatStore.isRunActive,
|
|
(v) => {
|
|
if (v) scrollToBottom();
|
|
},
|
|
);
|
|
|
|
// During streaming, only auto-scroll if the user is already near the bottom
|
|
watch(
|
|
() => chatStore.messages[chatStore.messages.length - 1]?.content,
|
|
() => {
|
|
if (chatStore.focusMessageId) {
|
|
scrollToMessage(chatStore.focusMessageId);
|
|
return;
|
|
}
|
|
if (!isNearBottom()) return;
|
|
scrollToBottom();
|
|
},
|
|
);
|
|
watch(currentToolCalls, () => {
|
|
if (chatStore.focusMessageId) {
|
|
scrollToMessage(chatStore.focusMessageId);
|
|
return;
|
|
}
|
|
if (!isNearBottom()) return;
|
|
scrollToBottom();
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<div ref="listRef" class="message-list">
|
|
<div v-if="chatStore.messages.length === 0" class="empty-state">
|
|
<img src="/logo.png" alt="Hermes" class="empty-logo" />
|
|
<p>{{ t("chat.emptyState") }}</p>
|
|
</div>
|
|
<MessageItem
|
|
v-for="msg in displayMessages"
|
|
:key="msg.id"
|
|
:message="msg"
|
|
:highlight="chatStore.focusMessageId === msg.id"
|
|
/>
|
|
<Transition name="fade">
|
|
<div v-if="chatStore.isRunActive" class="streaming-indicator">
|
|
<video
|
|
:src="isDark ? thinkingVideoDark : thinkingVideoLight"
|
|
autoplay
|
|
loop
|
|
muted
|
|
playsinline
|
|
class="thinking-video"
|
|
/>
|
|
<div v-if="currentToolCalls.length > 0 || chatStore.compressionState" class="tool-calls-panel">
|
|
<!-- Compression indicator -->
|
|
<div v-if="chatStore.compressionState" class="tool-call-item compression-item">
|
|
<svg
|
|
v-if="chatStore.compressionState.compressing"
|
|
width="12"
|
|
height="12"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="1.5"
|
|
class="tool-call-icon"
|
|
>
|
|
<path d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
|
|
</svg>
|
|
<svg
|
|
v-else-if="chatStore.compressionState.compressed"
|
|
width="12"
|
|
height="12"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="1.5"
|
|
class="tool-call-icon"
|
|
>
|
|
<path d="M5 13l4 4L19 7" />
|
|
</svg>
|
|
<span class="tool-call-name">
|
|
{{
|
|
chatStore.compressionState.compressing
|
|
? `Compressing... (${chatStore.compressionState.messageCount} msgs, ~${formatTokens(chatStore.compressionState.beforeTokens)} tokens)`
|
|
: chatStore.compressionState.compressed
|
|
? `Compressed ${chatStore.compressionState.messageCount} msgs: ~${formatTokens(chatStore.compressionState.beforeTokens)} → ~${formatTokens(chatStore.compressionState.afterTokens)} tokens`
|
|
: `Compression skipped`
|
|
}}
|
|
</span>
|
|
<span
|
|
v-if="chatStore.compressionState.compressing"
|
|
class="tool-call-spinner"
|
|
></span>
|
|
</div>
|
|
<!-- Tool calls -->
|
|
<div
|
|
v-for="tc in currentToolCalls"
|
|
:key="tc.id"
|
|
class="tool-call-item"
|
|
>
|
|
<svg
|
|
width="12"
|
|
height="12"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="1.5"
|
|
class="tool-call-icon"
|
|
>
|
|
<path
|
|
d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"
|
|
/>
|
|
</svg>
|
|
<span class="tool-call-name">{{ tc.toolName }}</span>
|
|
<span v-if="tc.toolPreview" class="tool-call-preview">{{
|
|
tc.toolPreview
|
|
}}</span>
|
|
<span
|
|
v-if="tc.toolDuration && tc.toolStatus !== 'running'"
|
|
class="tool-call-duration"
|
|
:title="$t('chat.executionDuration')"
|
|
>{{ formatToolDuration(tc.toolDuration) }}</span
|
|
>
|
|
<svg
|
|
v-if="tc.toolStatus === 'done'"
|
|
width="16"
|
|
height="16"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
class="tool-call-success-icon"
|
|
>
|
|
<circle cx="12" cy="12" r="10" fill="currentColor" fill-opacity="0.15"/>
|
|
<path
|
|
d="M8 12L11 15L16 9"
|
|
stroke="currentColor"
|
|
stroke-width="2"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
fill="none"
|
|
/>
|
|
</svg>
|
|
<span
|
|
v-if="tc.toolStatus === 'running'"
|
|
class="tool-call-spinner"
|
|
></span>
|
|
<svg
|
|
v-if="tc.toolStatus === 'error'"
|
|
width="16"
|
|
height="16"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
class="tool-call-error-icon"
|
|
>
|
|
<circle cx="12" cy="12" r="10" fill="currentColor" fill-opacity="0.15"/>
|
|
<path
|
|
d="M15 9L9 15M9 9L15 15"
|
|
stroke="currentColor"
|
|
stroke-width="2"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
fill="none"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Transition>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped lang="scss">
|
|
@use "@/styles/variables" as *;
|
|
|
|
.message-list {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 20px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 16px;
|
|
background-color: $bg-card;
|
|
|
|
.dark & {
|
|
background-color: #333333;
|
|
}
|
|
}
|
|
|
|
.empty-state {
|
|
flex: 1;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: $text-muted;
|
|
gap: 12px;
|
|
|
|
.empty-logo {
|
|
width: 48px;
|
|
height: 48px;
|
|
opacity: 0.25;
|
|
}
|
|
|
|
p {
|
|
font-size: 14px;
|
|
}
|
|
}
|
|
|
|
.fade-enter-active,
|
|
.fade-leave-active {
|
|
transition: opacity 0.4s ease;
|
|
}
|
|
.fade-enter-from,
|
|
.fade-leave-to {
|
|
opacity: 0;
|
|
}
|
|
|
|
.streaming-indicator {
|
|
display: flex;
|
|
align-items: flex-start;
|
|
gap: 12px;
|
|
padding: 4px;
|
|
.thinking-video {
|
|
width: 120px;
|
|
height: 213px;
|
|
border-radius: $radius-md;
|
|
object-fit: contain;
|
|
flex-shrink: 0;
|
|
}
|
|
}
|
|
|
|
.tool-calls-panel {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 4px;
|
|
max-height: 213px;
|
|
overflow-y: auto;
|
|
padding-top: 4px;
|
|
scrollbar-width: none;
|
|
-ms-overflow-style: none;
|
|
&::-webkit-scrollbar {
|
|
display: none;
|
|
}
|
|
}
|
|
|
|
.tool-call-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
font-size: 11px;
|
|
color: $text-secondary;
|
|
padding: 3px 8px;
|
|
background: rgba(0, 0, 0, 0.03);
|
|
border-radius: $radius-sm;
|
|
|
|
.dark & {
|
|
background: rgba(255, 255, 255, 0.06);
|
|
}
|
|
|
|
&.compression-item {
|
|
color: $text-muted;
|
|
font-size: 10px;
|
|
}
|
|
|
|
.tool-call-icon {
|
|
flex-shrink: 0;
|
|
color: $text-muted;
|
|
}
|
|
|
|
.tool-call-name {
|
|
font-family: $font-code;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.tool-call-preview {
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
max-width: 300px;
|
|
color: $text-muted;
|
|
}
|
|
}
|
|
|
|
.tool-call-spinner {
|
|
width: 10px;
|
|
height: 10px;
|
|
border: 1.5px solid $text-muted;
|
|
border-top-color: transparent;
|
|
border-radius: 50%;
|
|
animation: spin 0.6s linear infinite;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.tool-call-error-icon {
|
|
color: #ff4d4f;
|
|
flex-shrink: 0;
|
|
margin-left: 6px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.tool-call-duration {
|
|
font-size: 10px;
|
|
color: $text-muted;
|
|
font-family: $font-code;
|
|
margin-left: 4px;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.tool-call-success-icon {
|
|
color: #52c41a;
|
|
flex-shrink: 0;
|
|
margin-left: 6px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
@keyframes spin {
|
|
to {
|
|
transform: rotate(360deg);
|
|
}
|
|
}
|
|
</style>
|