Merge pull request #475 from rahthakor/feature/ui-enhancements

feat(ui): refactor chat layout with sidebar, message grouping, and nav improvements
This commit is contained in:
Peter Steinberger
2026-01-09 18:54:44 +00:00
committed by GitHub
23 changed files with 2560 additions and 314 deletions

View File

@@ -69,6 +69,7 @@
- Sessions: support session `label` in store/list/UI and allow `sessions_send` lookup by label. (#570) — thanks @azade-c
- Control UI: show/patch per-session reasoning level and render extracted reasoning in chat.
- Control UI: queue outgoing chat messages, add Enter-to-send, and show queued items. (#527) — thanks @YuriNachos
- Control UI: refactor chat layout with tool sidebar, grouped messages, and nav improvements. (#475) — thanks @rahthakor
- Control UI: drop explicit `ui:install` step; `ui:build` now auto-installs UI deps (docs + update flow).
- Telegram: retry long-polling conflicts with backoff to avoid fatal exits.
- Telegram: fix grammY fetch type mismatch when injecting `fetch`. (#512) — thanks @YuriNachos

View File

@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { resetProcessRegistryForTests } from "./bash-process-registry.js";
import {
bashTool,
@@ -50,6 +50,16 @@ beforeEach(() => {
});
describe("bash tool backgrounding", () => {
const originalShell = process.env.SHELL;
beforeEach(() => {
if (!isWin) process.env.SHELL = "/bin/bash";
});
afterEach(() => {
if (!isWin) process.env.SHELL = originalShell;
});
it(
"backgrounds after yield and can be polled",
async () => {

View File

@@ -12,4 +12,3 @@
<script type="module" src="/src/main.ts"></script>
</body>
</html>

View File

@@ -94,29 +94,7 @@ body::before {
z-index: 0;
}
body::after {
content: "";
position: fixed;
inset: 0;
background:
repeating-linear-gradient(
90deg,
var(--grid-line) 0,
var(--grid-line) 1px,
transparent 1px,
transparent 140px
),
repeating-linear-gradient(
0deg,
var(--grid-line) 0,
var(--grid-line) 1px,
transparent 1px,
transparent 140px
);
opacity: 0.45;
pointer-events: none;
z-index: 0;
}
/* Grid overlay removed for cleaner look */
@keyframes theme-circle-transition {
0% {

789
ui/src/styles/chat.css Normal file
View File

@@ -0,0 +1,789 @@
/* =============================================
CHAT CARD LAYOUT - Flex container with sticky compose
============================================= */
/* Main chat card - flex column layout, transparent background */
.chat {
position: relative;
display: flex;
flex-direction: column;
flex: 1 1 0;
height: 100%;
min-height: 0; /* Allow flex shrinking */
overflow: hidden;
background: transparent !important;
border: none !important;
box-shadow: none !important;
}
/* Chat header - fixed at top, transparent */
.chat-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
flex-wrap: nowrap;
flex-shrink: 0;
padding-bottom: 12px;
margin-bottom: 12px;
background: transparent;
}
.chat-header__left {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
min-width: 0;
}
.chat-header__right {
display: flex;
align-items: center;
gap: 8px;
}
.chat-session {
min-width: 180px;
}
/* Chat thread - scrollable middle section, transparent */
.chat-thread {
flex: 1 1 0; /* Grow, shrink, and use 0 base for proper scrolling */
overflow-y: auto;
overflow-x: hidden;
padding: 12px;
margin: 0 -12px;
min-height: 0; /* Allow shrinking for flex scroll behavior */
border-radius: 12px;
background: transparent;
}
/* Focus mode exit button */
.chat-focus-exit {
position: absolute;
top: 12px;
right: 12px;
z-index: 100;
width: 32px;
height: 32px;
border-radius: 50%;
border: 1px solid var(--border);
background: var(--panel);
color: var(--muted);
font-size: 20px;
line-height: 1;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: background 150ms ease-out, color 150ms ease-out, border-color 150ms ease-out;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.chat-focus-exit:hover {
background: var(--panel-strong);
color: var(--text);
border-color: var(--accent);
}
/* Chat compose - sticky at bottom */
.chat-compose {
position: sticky;
bottom: 0;
flex-shrink: 0;
display: flex;
align-items: flex-end;
gap: 12px;
margin-top: auto; /* Push to bottom of flex container */
padding: 16px 0 4px;
background: linear-gradient(to bottom, transparent, var(--bg) 20%);
z-index: 10;
}
.chat-compose__field {
flex: 1 1 auto;
min-width: 0;
}
/* Hide the "Message" label - keep textarea only */
.chat-compose__field > span {
display: none;
}
/* Override .field textarea min-height (180px) from components.css */
.chat-compose .chat-compose__field textarea {
width: 100%;
min-height: 36px;
max-height: 150px;
padding: 8px 12px;
border-radius: 10px;
resize: vertical;
white-space: pre-wrap;
font-family: var(--font-body);
font-size: 14px;
line-height: 1.45;
}
.chat-compose__field textarea:disabled {
opacity: 0.7;
cursor: not-allowed;
}
.chat-compose__actions {
flex-shrink: 0;
display: flex;
align-items: stretch;
}
.chat-compose .chat-compose__actions .btn {
padding: 8px 16px;
font-size: 13px;
min-height: 36px;
white-space: nowrap;
}
/* Chat controls - moved to content-header area, left aligned */
.chat-controls {
display: flex;
align-items: center;
justify-content: flex-start;
gap: 12px;
flex-wrap: wrap;
}
.chat-controls__session {
min-width: 140px;
}
.chat-controls__thinking {
display: flex;
align-items: center;
gap: 6px;
font-size: 13px;
}
/* Icon button style */
.btn--icon {
padding: 8px !important;
min-width: 36px;
height: 36px;
display: inline-flex;
align-items: center;
justify-content: center;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.06);
}
/* Controls separator */
.chat-controls__separator {
color: rgba(255, 255, 255, 0.4);
font-size: 18px;
margin: 0 8px;
font-weight: 300;
}
:root[data-theme="light"] .chat-controls__separator {
color: rgba(16, 24, 40, 0.3);
}
.btn--icon:hover {
background: rgba(255, 255, 255, 0.12);
border-color: rgba(255, 255, 255, 0.2);
}
/* Light theme icon button overrides */
:root[data-theme="light"] .btn--icon {
background: rgba(255, 255, 255, 0.9);
border-color: rgba(16, 24, 40, 0.2);
box-shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
color: rgba(16, 24, 40, 0.7);
}
:root[data-theme="light"] .btn--icon:hover {
background: rgba(255, 255, 255, 1);
border-color: rgba(16, 24, 40, 0.3);
color: rgba(16, 24, 40, 0.9);
}
.btn--icon svg {
display: block;
}
.chat-controls__session select {
padding: 6px 10px;
font-size: 13px;
}
.chat-controls__thinking {
display: flex;
align-items: center;
gap: 4px;
font-size: 12px;
padding: 4px 10px;
background: rgba(255, 255, 255, 0.04);
border-radius: 6px;
border: 1px solid var(--border);
}
/* Light theme thinking indicator override */
:root[data-theme="light"] .chat-controls__thinking {
background: rgba(255, 255, 255, 0.9);
border-color: rgba(16, 24, 40, 0.15);
}
@media (max-width: 640px) {
.chat-session {
min-width: 140px;
}
.chat-compose {
grid-template-columns: 1fr;
}
.chat-controls {
flex-wrap: wrap;
gap: 8px;
}
.chat-controls__session {
min-width: 120px;
}
}
/* =============================================
LEGACY CHAT LINE LAYOUT (non-grouped)
============================================= */
.chat-line {
display: flex;
margin-bottom: 12px;
}
.chat-line.user {
justify-content: flex-end;
}
.chat-line.assistant,
.chat-line.other {
justify-content: flex-start;
}
.chat-msg {
display: grid;
gap: 6px;
max-width: min(900px, 95%);
}
.chat-line.user .chat-msg {
justify-items: end;
}
.chat-stamp {
font-size: 11px;
color: var(--muted);
}
.chat-line.user .chat-stamp {
text-align: right;
}
/* =============================================
CHAT TEXT STYLING
============================================= */
.chat-text {
font-size: 14px;
line-height: 1.5;
word-wrap: break-word;
overflow-wrap: break-word;
}
.chat-text :where(p, ul, ol, pre, blockquote, table) {
margin: 0;
}
.chat-text :where(p + p, p + ul, p + ol, p + pre, p + blockquote) {
margin-top: 0.75em;
}
.chat-text :where(ul, ol) {
padding-left: 1.5em;
}
.chat-text :where(li + li) {
margin-top: 0.25em;
}
.chat-text :where(a) {
color: var(--accent);
text-decoration: underline;
text-underline-offset: 2px;
}
.chat-text :where(a:hover) {
opacity: 0.8;
}
.chat-text :where(code) {
font-family: var(--mono);
font-size: 0.9em;
}
.chat-text :where(:not(pre) > code) {
background: rgba(0, 0, 0, 0.15);
padding: 0.15em 0.4em;
border-radius: 4px;
}
.chat-text :where(pre) {
background: rgba(0, 0, 0, 0.15);
border-radius: 6px;
padding: 10px 12px;
overflow-x: auto;
}
.chat-text :where(pre code) {
background: none;
padding: 0;
}
.chat-text :where(blockquote) {
border-left: 3px solid var(--border);
padding-left: 12px;
color: var(--muted);
}
.chat-text :where(hr) {
border: none;
border-top: 1px solid var(--border);
margin: 1em 0;
}
/* =============================================
GROUPED CHAT LAYOUT (Slack-style)
============================================= */
/* Chat Group Layout - default (assistant/other on left) */
.chat-group {
display: flex;
gap: 12px;
align-items: flex-start;
margin-bottom: 16px;
margin-left: 16px;
margin-right: 16px;
}
/* User messages on right */
.chat-group.user {
flex-direction: row-reverse;
justify-content: flex-start;
}
.chat-group-messages {
display: flex;
flex-direction: column;
gap: 2px;
max-width: min(900px, calc(100% - 60px));
}
/* User messages align content right */
.chat-group.user .chat-group-messages {
align-items: flex-end;
}
.chat-group.user .chat-group-footer {
justify-content: flex-end;
}
/* Footer at bottom of message group (role + time) */
.chat-group-footer {
display: flex;
gap: 8px;
align-items: baseline;
margin-top: 6px;
}
.chat-sender-name {
font-weight: 500;
font-size: 12px;
color: var(--muted);
}
.chat-group-timestamp {
font-size: 11px;
color: var(--muted);
opacity: 0.7;
}
/* Avatar Styles */
.chat-avatar {
width: 40px;
height: 40px;
border-radius: 8px;
background: var(--panel-strong);
display: grid;
place-items: center;
font-weight: 600;
font-size: 14px;
flex-shrink: 0;
align-self: flex-end; /* Align with last message in group */
margin-bottom: 4px; /* Optical alignment */
}
.chat-avatar.user {
background: rgba(245, 159, 74, 0.2);
color: rgba(245, 159, 74, 1);
}
.chat-avatar.assistant {
background: rgba(52, 199, 183, 0.2);
color: rgba(52, 199, 183, 1);
}
.chat-avatar.other {
background: rgba(150, 150, 150, 0.2);
color: rgba(150, 150, 150, 1);
}
/* Minimal Bubble Design - dynamic width based on content */
.chat-bubble {
display: inline-block;
border: 1px solid var(--border);
background: rgba(0, 0, 0, 0.12);
border-radius: 12px;
padding: 10px 14px;
box-shadow: none;
transition: background 150ms ease-out, border-color 150ms ease-out;
max-width: 100%;
word-wrap: break-word;
}
.chat-bubble:hover {
background: rgba(0, 0, 0, 0.18);
}
/* User bubbles have different styling */
.chat-group.user .chat-bubble {
background: rgba(245, 159, 74, 0.15);
border-color: rgba(245, 159, 74, 0.3);
}
.chat-group.user .chat-bubble:hover {
background: rgba(245, 159, 74, 0.22);
}
/* Streaming animation */
.chat-bubble.streaming {
animation: pulsing-border 1.5s ease-out infinite;
}
@keyframes pulsing-border {
0%, 100% {
border-color: var(--border);
}
50% {
border-color: var(--accent);
}
}
/* Fade-in animation for new messages */
.chat-bubble.fade-in {
animation: fade-in 200ms ease-out;
}
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(4px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Tool Card Styles */
.chat-tool-card {
border: 1px solid var(--border);
border-radius: 8px;
padding: 12px;
margin-top: 8px;
transition: border-color 150ms ease-out, background 150ms ease-out;
/* Fixed max-height to ensure cards don't expand too much */
max-height: 120px;
overflow: hidden;
}
.chat-tool-card:hover {
border-color: var(--accent);
background: rgba(0, 0, 0, 0.06);
}
/* First tool card in a group - no top margin */
.chat-tool-card:first-child {
margin-top: 0;
}
.chat-tool-card--clickable {
cursor: pointer;
}
.chat-tool-card--clickable:focus {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
/* Header with title and chevron */
.chat-tool-card__header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 8px;
}
.chat-tool-card__title {
display: inline-flex;
align-items: center;
gap: 6px;
font-weight: 600;
font-size: 13px;
line-height: 1.2;
}
.chat-tool-card__icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
font-size: 14px;
line-height: 1;
font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
vertical-align: middle;
flex-shrink: 0;
}
/* "View >" action link */
.chat-tool-card__action {
font-size: 12px;
color: var(--accent);
opacity: 0.8;
transition: opacity 150ms ease-out;
}
.chat-tool-card--clickable:hover .chat-tool-card__action {
opacity: 1;
}
/* Status indicator for completed/empty results */
.chat-tool-card__status {
font-size: 14px;
color: var(--ok);
}
.chat-tool-card__status-text {
font-size: 11px;
margin-top: 4px;
}
.chat-tool-card__detail {
font-size: 12px;
color: var(--muted);
margin-top: 4px;
}
/* Collapsed preview - fixed height with truncation */
.chat-tool-card__preview {
font-size: 11px;
color: var(--muted);
margin-top: 8px;
padding: 8px 10px;
background: rgba(0, 0, 0, 0.08);
border-radius: 6px;
white-space: pre-wrap;
overflow: hidden;
max-height: 44px;
line-height: 1.4;
border: 1px solid rgba(255, 255, 255, 0.04);
}
.chat-tool-card--clickable:hover .chat-tool-card__preview {
background: rgba(0, 0, 0, 0.12);
border-color: rgba(255, 255, 255, 0.08);
}
/* Short inline output */
.chat-tool-card__inline {
font-size: 11px;
color: var(--text);
margin-top: 6px;
padding: 6px 8px;
background: rgba(0, 0, 0, 0.06);
border-radius: 4px;
white-space: pre-wrap;
word-break: break-word;
}
/* Reading Indicator */
.chat-reading-indicator {
background: transparent;
border: 1px solid var(--border);
padding: 12px;
display: inline-flex;
}
.chat-reading-indicator__dots {
display: flex;
gap: 6px;
align-items: center;
}
.chat-reading-indicator__dots span {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--muted);
animation: reading-pulse 1.4s ease-in-out infinite;
}
.chat-reading-indicator__dots span:nth-child(1) {
animation-delay: 0s;
}
.chat-reading-indicator__dots span:nth-child(2) {
animation-delay: 0.2s;
}
.chat-reading-indicator__dots span:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes reading-pulse {
0%, 60%, 100% {
opacity: 0.3;
transform: scale(0.8);
}
30% {
opacity: 1;
transform: scale(1);
}
}
/* Split View Layout */
.chat-split-container {
display: flex;
gap: 0;
flex: 1;
min-height: 0;
height: 100%;
}
.chat-main {
min-width: 400px;
display: flex;
flex-direction: column;
overflow: hidden;
/* Smooth transition when sidebar opens/closes */
transition: flex 250ms ease-out;
}
.chat-sidebar {
flex: 1;
min-width: 300px;
border-left: 1px solid var(--border);
display: flex;
flex-direction: column;
overflow: hidden;
animation: slide-in 200ms ease-out;
}
@keyframes slide-in {
from {
opacity: 0;
transform: translateX(20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
/* Sidebar Panel */
.sidebar-panel {
display: flex;
flex-direction: column;
height: 100%;
background: var(--panel);
}
.sidebar-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
border-bottom: 1px solid var(--border);
flex-shrink: 0;
position: sticky;
top: 0;
z-index: 10;
background: var(--panel);
}
/* Smaller close button for sidebar */
.sidebar-header .btn {
padding: 4px 8px;
font-size: 14px;
min-width: auto;
line-height: 1;
}
.sidebar-title {
font-weight: 600;
font-size: 14px;
}
.sidebar-content {
flex: 1;
overflow: auto;
padding: 16px;
}
.sidebar-markdown {
font-size: 14px;
line-height: 1.5;
}
.sidebar-markdown pre {
background: rgba(0, 0, 0, 0.12);
border-radius: 4px;
padding: 12px;
overflow-x: auto;
}
.sidebar-markdown code {
font-family: var(--mono);
font-size: 13px;
}
/* Mobile: Full-screen modal */
@media (max-width: 768px) {
.chat-split-container {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1000;
}
.chat-main {
display: none; /* Hide chat on mobile when sidebar open */
}
.chat-sidebar {
width: 100%;
min-width: 0;
border-left: none;
}
}

View File

@@ -1,3 +1,5 @@
@import './chat.css';
.card {
border: 1px solid var(--border);
background: linear-gradient(160deg, rgba(255, 255, 255, 0.04), transparent 65%),
@@ -210,6 +212,17 @@
background: rgba(255, 107, 107, 0.18);
}
.btn--sm {
padding: 5px 10px;
font-size: 12px;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.field {
display: grid;
gap: 6px;
@@ -287,8 +300,9 @@
:root[data-theme="light"] .field input,
:root[data-theme="light"] .field textarea,
:root[data-theme="light"] .field select {
background: rgba(255, 255, 255, 0.9);
border-color: var(--border-strong);
background: rgba(255, 255, 255, 1);
border-color: rgba(16, 24, 40, 0.25);
box-shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}
:root[data-theme="light"] .field input:focus,
@@ -297,6 +311,26 @@
background: #ffffff;
}
/* Light theme button overrides */
:root[data-theme="light"] .btn {
background: rgba(255, 255, 255, 0.9);
border-color: rgba(16, 24, 40, 0.2);
box-shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
}
:root[data-theme="light"] .btn:hover {
background: rgba(255, 255, 255, 1);
border-color: rgba(16, 24, 40, 0.3);
}
:root[data-theme="light"] .btn.primary {
background: rgba(245, 159, 74, 0.15);
}
:root[data-theme="light"] .btn.active {
background: rgba(245, 159, 74, 0.12);
}
.muted {
color: var(--muted);
}
@@ -569,6 +603,7 @@
.shell--chat .chat {
flex: 1;
max-height: calc(100vh - 180px); /* Constrain height for sticky compose */
}
.chat-header {
@@ -603,26 +638,18 @@
flex-direction: column;
gap: 12px;
flex: 1;
max-height: none;
overflow: visible;
min-height: 0; /* Allow flex shrinking for scroll behavior */
overflow-y: auto; /* Enable scrolling */
overflow-x: hidden;
padding: 14px 12px;
min-width: 0;
border-radius: 16px;
border: 1px solid var(--border);
background: linear-gradient(
180deg,
rgba(0, 0, 0, 0.2) 0%,
rgba(0, 0, 0, 0.3) 100%
);
border-radius: 0;
border: none;
background: transparent;
}
:root[data-theme="light"] .chat-thread {
border-color: rgba(16, 24, 40, 0.12);
background: linear-gradient(
180deg,
rgba(16, 24, 40, 0.03) 0%,
rgba(16, 24, 40, 0.06) 100%
);
background: transparent;
}
.chat-queue {

View File

@@ -1,28 +1,28 @@
.shell {
--shell-pad: 18px;
--shell-gap: 18px;
--shell-nav-col: minmax(220px, 280px);
--shell-topbar-row: auto;
--shell-pad: 16px;
--shell-gap: 16px;
--shell-nav-width: 220px;
--shell-topbar-height: 56px;
--shell-focus-duration: 220ms;
--shell-focus-ease: cubic-bezier(0.2, 0.85, 0.25, 1);
min-height: 100vh;
display: grid;
grid-template-columns: var(--shell-nav-col) minmax(0, 1fr);
grid-template-rows: var(--shell-topbar-row) 1fr;
grid-template-columns: var(--shell-nav-width) minmax(0, 1fr);
grid-template-rows: var(--shell-topbar-height) 1fr;
grid-template-areas:
"topbar topbar"
"nav content";
gap: var(--shell-gap);
padding: var(--shell-pad);
gap: 0;
animation: dashboard-enter 0.6s ease-out;
transition: padding var(--shell-focus-duration) var(--shell-focus-ease);
transition: grid-template-columns var(--shell-focus-duration) var(--shell-focus-ease);
}
.shell--nav-collapsed {
grid-template-columns: 0px minmax(0, 1fr);
}
.shell--chat-focus {
--shell-pad: 8px;
--shell-gap: 0px;
--shell-nav-col: 0px;
--shell-topbar-row: auto;
grid-template-columns: 0px minmax(0, 1fr);
}
.shell--chat-focus .content {
@@ -33,89 +33,147 @@
.topbar {
grid-area: topbar;
position: sticky;
top: var(--shell-pad);
z-index: 20;
top: 0;
z-index: 40;
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px;
border: 1px solid var(--border);
border-radius: 18px;
background: linear-gradient(135deg, var(--chrome), rgba(255, 255, 255, 0.02));
gap: 16px;
padding: 0 20px;
height: var(--shell-topbar-height);
border-bottom: 1px solid var(--border);
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 18px 40px rgba(0, 0, 0, 0.28);
overflow: hidden;
transform-origin: top center;
transition: opacity var(--shell-focus-duration) var(--shell-focus-ease),
transform var(--shell-focus-duration) var(--shell-focus-ease),
max-height var(--shell-focus-duration) var(--shell-focus-ease),
padding var(--shell-focus-duration) var(--shell-focus-ease),
border-width var(--shell-focus-duration) var(--shell-focus-ease);
max-height: max(0px, var(--topbar-height, 92px));
}
.topbar-left {
display: flex;
align-items: center;
gap: 12px;
}
.topbar .nav-collapse-toggle {
width: 44px;
height: 44px;
margin-bottom: 0;
}
.topbar .nav-collapse-toggle__icon {
font-size: 22px;
}
.brand {
display: grid;
gap: 4px;
display: flex;
flex-direction: column;
gap: 2px;
}
.brand-title {
font-family: var(--font-display);
font-size: 20px;
letter-spacing: 0.6px;
font-size: 16px;
letter-spacing: 1px;
text-transform: uppercase;
font-weight: 600;
line-height: 1.1;
}
.brand-sub {
font-size: 10px;
color: var(--muted);
font-size: 12px;
letter-spacing: 1.2px;
letter-spacing: 0.8px;
text-transform: uppercase;
line-height: 1;
}
.topbar-status {
display: flex;
align-items: center;
gap: 10px;
gap: 8px;
}
/* Smaller pill and theme toggle in topbar */
.topbar-status .pill {
padding: 4px 10px;
gap: 6px;
font-size: 11px;
}
.topbar-status .statusDot {
width: 6px;
height: 6px;
}
.topbar-status .theme-toggle {
--theme-item: 22px;
--theme-gap: 4px;
--theme-pad: 4px;
}
.topbar-status .theme-icon {
width: 12px;
height: 12px;
}
.nav {
grid-area: nav;
position: sticky;
top: calc(
var(--shell-pad) + var(--topbar-height, 0px) + var(--shell-gap)
);
align-self: start;
max-height: calc(
100vh - var(--topbar-height, 0px) - var(--shell-gap) -
var(--shell-pad) - var(--shell-pad)
);
overflow: auto;
overflow-y: auto;
overflow-x: hidden;
padding: 16px;
border: 1px solid var(--border);
border-radius: 20px;
border-right: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 18px 40px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(18px);
transform-origin: left center;
transition: opacity var(--shell-focus-duration) var(--shell-focus-ease),
transform var(--shell-focus-duration) var(--shell-focus-ease),
max-width var(--shell-focus-duration) var(--shell-focus-ease),
padding var(--shell-focus-duration) var(--shell-focus-ease),
border-width var(--shell-focus-duration) var(--shell-focus-ease);
max-width: 320px;
transition: width var(--shell-focus-duration) var(--shell-focus-ease),
padding var(--shell-focus-duration) var(--shell-focus-ease);
}
.shell--chat-focus .nav {
opacity: 0;
transform: translateX(-12px);
max-width: 0px;
width: 0;
padding: 0;
border-width: 0;
overflow: hidden;
pointer-events: none;
}
/* Collapsed nav sidebar - completely hidden */
.nav--collapsed {
width: 0;
min-width: 0;
padding: 0;
overflow: hidden;
border: none;
opacity: 0;
pointer-events: none;
}
/* Nav collapse toggle button */
.nav-collapse-toggle {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
background: transparent;
border: 1px solid transparent;
border-radius: 6px;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease;
margin-bottom: 16px;
}
.nav-collapse-toggle:hover {
background: rgba(255, 255, 255, 0.08);
border-color: var(--border);
}
:root[data-theme="light"] .nav-collapse-toggle:hover {
background: rgba(0, 0, 0, 0.06);
}
.nav-collapse-toggle__icon {
font-size: 16px;
color: var(--muted);
}
.nav-group {
margin-bottom: 18px;
display: grid;
@@ -130,27 +188,77 @@
border-bottom: none;
}
.nav-group__items {
display: grid;
gap: 4px;
}
.nav-group--collapsed .nav-group__items {
display: none;
}
.nav-label {
font-size: 10px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
width: 100%;
padding: 4px 0;
font-size: 11px;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 1.6px;
color: var(--muted);
letter-spacing: 1.4px;
color: var(--text);
opacity: 0.7;
margin-bottom: 4px;
background: transparent;
border: none;
cursor: pointer;
text-align: left;
}
.nav-label:hover {
opacity: 1;
}
.nav-label__text {
flex: 1;
}
.nav-label__chevron {
font-size: 12px;
opacity: 0.6;
}
.nav-item {
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
justify-content: flex-start;
gap: 8px;
padding: 10px 12px 10px 14px;
border-radius: 12px;
border: 1px solid transparent;
background: rgba(255, 255, 255, 0.02);
background: transparent;
color: var(--muted);
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, color 160ms ease,
transform 160ms ease;
text-decoration: none;
transition: border-color 160ms ease, background 160ms ease, color 160ms ease;
}
.nav-item__icon {
font-size: 16px;
width: 18px;
height: 18px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.nav-item__text {
font-size: 13px;
white-space: nowrap;
}
.nav-item:hover {
@@ -162,11 +270,11 @@
.nav-item::before {
content: "";
position: absolute;
left: 6px;
left: 0;
top: 50%;
width: 4px;
height: 60%;
border-radius: 999px;
border-radius: 0 999px 999px 0;
transform: translateY(-50%);
background: transparent;
}
@@ -174,8 +282,7 @@
.nav-item.active {
color: var(--text);
border-color: rgba(245, 159, 74, 0.45);
background: rgba(245, 159, 74, 0.16);
transform: translateX(2px);
background: rgba(245, 159, 74, 0.12);
}
.nav-item.active::before {
@@ -190,13 +297,12 @@
flex-direction: column;
gap: 20px;
min-height: 0;
overflow-y: auto; /* Enable vertical scrolling for pages with long content */
overflow-x: hidden;
}
.shell--chat .content {
min-height: calc(
100vh - var(--topbar-height, 0px) - var(--shell-gap) -
var(--shell-pad) - var(--shell-pad)
);
/* No-op: keep chat layout consistent with other tabs */
}
.docs-link {
@@ -264,6 +370,26 @@
gap: 10px;
}
/* Chat view: header and controls side by side */
.content--chat .content-header {
flex-direction: row;
align-items: center;
justify-content: space-between;
gap: 16px;
}
.content--chat .content-header > div:first-child {
text-align: left;
}
.content--chat .page-meta {
justify-content: flex-start;
}
.content--chat .chat-controls {
flex-shrink: 0;
}
.grid {
display: grid;
gap: 18px;

View File

@@ -3,6 +3,7 @@ import { html, nothing } from "lit";
import type { GatewayBrowserClient, GatewayHelloOk } from "./gateway";
import {
TAB_GROUPS,
iconForTab,
pathForTab,
subtitleForTab,
titleForTab,
@@ -215,11 +216,25 @@ export function renderApp(state: AppViewState) {
const chatFocus = isChat && state.settings.chatFocusMode;
return html`
<div class="shell ${isChat ? "shell--chat" : ""} ${chatFocus ? "shell--chat-focus" : ""}">
<div class="shell ${isChat ? "shell--chat" : ""} ${chatFocus ? "shell--chat-focus" : ""} ${state.settings.navCollapsed ? "shell--nav-collapsed" : ""}">
<header class="topbar">
<div class="brand">
<div class="brand-title">Clawdbot Control</div>
<div class="brand-sub">Gateway dashboard</div>
<div class="topbar-left">
<button
class="nav-collapse-toggle"
@click=${() =>
state.applySettings({
...state.settings,
navCollapsed: !state.settings.navCollapsed,
})}
title="${state.settings.navCollapsed ? "Expand sidebar" : "Collapse sidebar"}"
aria-label="${state.settings.navCollapsed ? "Expand sidebar" : "Collapse sidebar"}"
>
<span class="nav-collapse-toggle__icon">☰</span>
</button>
<div class="brand">
<div class="brand-title">CLAWDBOT</div>
<div class="brand-sub">Gateway Dashboard</div>
</div>
</div>
<div class="topbar-status">
<div class="pill">
@@ -227,28 +242,36 @@ export function renderApp(state: AppViewState) {
<span>Health</span>
<span class="mono">${state.connected ? "OK" : "Offline"}</span>
</div>
${isChat
? renderChatFocusToggle(
state.settings.chatFocusMode,
() =>
state.applySettings({
...state.settings,
chatFocusMode: !state.settings.chatFocusMode,
}),
)
: nothing}
${renderThemeToggle(state)}
</div>
</header>
<aside class="nav">
${TAB_GROUPS.map(
(group) => html`
<div class="nav-group">
<div class="nav-label">${group.label}</div>
${group.tabs.map((tab) => renderTab(state, tab))}
<aside class="nav ${state.settings.navCollapsed ? "nav--collapsed" : ""}">
${TAB_GROUPS.map((group) => {
const isGroupCollapsed = state.settings.navGroupsCollapsed[group.label] ?? false;
const hasActiveTab = group.tabs.some((tab) => tab === state.tab);
return html`
<div class="nav-group ${isGroupCollapsed && !hasActiveTab ? "nav-group--collapsed" : ""}">
<button
class="nav-label"
@click=${() => {
const next = { ...state.settings.navGroupsCollapsed };
next[group.label] = !isGroupCollapsed;
state.applySettings({
...state.settings,
navGroupsCollapsed: next,
});
}}
aria-expanded=${!isGroupCollapsed}
>
<span class="nav-label__text">${group.label}</span>
<span class="nav-label__chevron">${isGroupCollapsed ? "+" : ""}</span>
</button>
<div class="nav-group__items">
${group.tabs.map((tab) => renderTab(state, tab))}
</div>
</div>
`,
)}
`;
})}
</aside>
<main class="content ${isChat ? "content--chat" : ""}">
<section class="content-header">
@@ -260,6 +283,7 @@ export function renderApp(state: AppViewState) {
${state.lastError
? html`<div class="pill danger">${state.lastError}</div>`
: nothing}
${isChat ? renderChatControls(state) : nothing}
</div>
</section>
@@ -453,15 +477,35 @@ export function renderApp(state: AppViewState) {
isToolOutputExpanded: (id) => state.toolOutputExpanded.has(id),
onToolOutputToggle: (id, expanded) =>
state.toggleToolOutput(id, expanded),
focusMode: state.settings.chatFocusMode,
useNewChatLayout: state.settings.useNewChatLayout,
onRefresh: () => {
state.resetToolStream();
return loadChatHistory(state);
},
onToggleFocusMode: () =>
state.applySettings({
...state.settings,
chatFocusMode: !state.settings.chatFocusMode,
}),
onToggleLayout: () =>
state.applySettings({
...state.settings,
useNewChatLayout: !state.settings.useNewChatLayout,
}),
onDraftChange: (next) => (state.chatMessage = next),
onSend: () => state.handleSendChat(),
onQueueRemove: (id) => state.removeQueuedMessage(id),
onNewSession: () =>
state.handleSendChat("/new", { restoreDraft: true }),
// Sidebar props for tool output viewing
sidebarOpen: state.sidebarOpen,
sidebarContent: state.sidebarContent,
sidebarError: state.sidebarError,
splitRatio: state.splitRatio,
onOpenSidebar: (content: string) => state.handleOpenSidebar(content),
onCloseSidebar: () => state.handleCloseSidebar(),
onSplitRatioChange: (ratio: number) => state.handleSplitRatioChange(ratio),
})
: nothing}
@@ -562,12 +606,115 @@ function renderTab(state: AppViewState, tab: Tab) {
event.preventDefault();
state.setTab(tab);
}}
title=${titleForTab(tab)}
>
<span>${titleForTab(tab)}</span>
<span class="nav-item__icon" aria-hidden="true">${iconForTab(tab)}</span>
<span class="nav-item__text">${titleForTab(tab)}</span>
</a>
`;
}
function renderChatControls(state: AppViewState) {
const sessionOptions = resolveSessionOptions(state.sessionKey, state.sessionsResult);
// Icon for list view (legacy)
const listIcon = html`<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="8" y1="6" x2="21" y2="6"></line><line x1="8" y1="12" x2="21" y2="12"></line><line x1="8" y1="18" x2="21" y2="18"></line><line x1="3" y1="6" x2="3.01" y2="6"></line><line x1="3" y1="12" x2="3.01" y2="12"></line><line x1="3" y1="18" x2="3.01" y2="18"></line></svg>`;
// Icon for grouped view
const groupIcon = html`<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect></svg>`;
// Refresh icon
const refreshIcon = html`<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"></path><path d="M21 3v5h-5"></path></svg>`;
const focusIcon = html`<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 7V4h3"></path><path d="M20 7V4h-3"></path><path d="M4 17v3h3"></path><path d="M20 17v3h-3"></path><circle cx="12" cy="12" r="3"></circle></svg>`;
return html`
<div class="chat-controls">
<label class="field chat-controls__session">
<select
.value=${state.sessionKey}
?disabled=${!state.connected}
@change=${(e: Event) => {
const next = (e.target as HTMLSelectElement).value;
state.sessionKey = next;
state.chatMessage = "";
state.chatStream = null;
state.chatStreamStartedAt = null;
state.chatRunId = null;
state.resetToolStream();
state.resetChatScroll();
state.applySettings({
...state.settings,
sessionKey: next,
lastActiveSessionKey: next,
});
void loadChatHistory(state);
}}
>
${sessionOptions.map(
(entry) =>
html`<option value=${entry.key}>
${entry.displayName ?? entry.key}
</option>`,
)}
</select>
</label>
<button
class="btn btn--sm btn--icon"
?disabled=${state.chatLoading || !state.connected}
@click=${() => {
state.resetToolStream();
void loadChatHistory(state);
}}
title="Refresh chat history"
>
${refreshIcon}
</button>
<span class="chat-controls__separator">|</span>
<button
class="btn btn--sm btn--icon ${state.settings.chatFocusMode ? "active" : ""}"
@click=${() =>
state.applySettings({
...state.settings,
chatFocusMode: !state.settings.chatFocusMode,
})}
aria-pressed=${state.settings.chatFocusMode}
title="Toggle focus mode (hide sidebar + page header)"
>
${focusIcon}
</button>
<button
class="btn btn--sm btn--icon ${state.settings.useNewChatLayout ? "active" : ""}"
@click=${() =>
state.applySettings({
...state.settings,
useNewChatLayout: !state.settings.useNewChatLayout,
})}
aria-pressed=${state.settings.useNewChatLayout}
title="${state.settings.useNewChatLayout ? "Switch to list view" : "Switch to grouped view"}"
>
${state.settings.useNewChatLayout ? groupIcon : listIcon}
</button>
</div>
`;
}
function resolveSessionOptions(sessionKey: string, sessions: SessionsListResult | null) {
const seen = new Set<string>();
const options: Array<{ key: string; displayName?: string }> = [];
// Add current session key first
seen.add(sessionKey);
options.push({ key: sessionKey });
// Add sessions from the result
if (sessions?.sessions) {
for (const s of sessions.sessions) {
if (!seen.has(s.key)) {
seen.add(s.key);
options.push({ key: s.key, displayName: s.displayName });
}
}
}
return options;
}
const THEME_ORDER: ThemeMode[] = ["system", "light", "dark"];
function renderThemeToggle(state: AppViewState) {
@@ -618,19 +765,6 @@ function renderThemeToggle(state: AppViewState) {
`;
}
function renderChatFocusToggle(focusMode: boolean, onToggle: () => void) {
return html`
<button
class="btn ${focusMode ? "active" : ""}"
@click=${onToggle}
aria-pressed=${focusMode}
title="Toggle focus mode (hide sidebar + page header)"
>
Focus
</button>
`;
}
function renderSunIcon() {
return html`
<svg class="theme-icon" viewBox="0 0 24 24" aria-hidden="true">

View File

@@ -205,6 +205,7 @@ export class ClawdbotApp extends LitElement {
@state() eventLog: EventLogEntry[] = [];
private eventLogBuffer: EventLogEntry[] = [];
private toolStreamSyncTimer: number | null = null;
private sidebarCloseTimer: number | null = null;
@state() sessionKey = this.settings.sessionKey;
@state() chatLoading = false;
@@ -218,6 +219,11 @@ export class ClawdbotApp extends LitElement {
@state() chatThinkingLevel: string | null = null;
@state() chatQueue: ChatQueueItem[] = [];
@state() toolOutputExpanded = new Set<string>();
// Sidebar state for tool output viewing
@state() sidebarOpen = false;
@state() sidebarContent: string | null = null;
@state() sidebarError: string | null = null;
@state() splitRatio = this.settings.splitRatio;
@state() nodesLoading = false;
@state() nodes: Array<Record<string, unknown>> = [];
@@ -1149,6 +1155,37 @@ export class ClawdbotApp extends LitElement {
await loadProviders(this, true);
}
// Sidebar handlers for tool output viewing
handleOpenSidebar(content: string) {
if (this.sidebarCloseTimer != null) {
window.clearTimeout(this.sidebarCloseTimer);
this.sidebarCloseTimer = null;
}
this.sidebarContent = content;
this.sidebarError = null;
this.sidebarOpen = true;
}
handleCloseSidebar() {
this.sidebarOpen = false;
// Clear content after transition
if (this.sidebarCloseTimer != null) {
window.clearTimeout(this.sidebarCloseTimer);
}
this.sidebarCloseTimer = window.setTimeout(() => {
if (this.sidebarOpen) return;
this.sidebarContent = null;
this.sidebarError = null;
this.sidebarCloseTimer = null;
}, 200);
}
handleSplitRatioChange(ratio: number) {
const newRatio = Math.max(0.4, Math.min(0.7, ratio));
this.splitRatio = newRatio;
this.applySettings({ ...this.settings, splitRatio: newRatio });
}
render() {
return renderApp(this);
}

View File

@@ -16,17 +16,24 @@ beforeEach(() => {
// no-op: avoid real gateway WS connections in browser tests
};
window.__CLAWDBOT_CONTROL_UI_BASE_PATH__ = undefined;
localStorage.clear();
document.body.innerHTML = "";
});
afterEach(() => {
ClawdbotApp.prototype.connect = originalConnect;
window.__CLAWDBOT_CONTROL_UI_BASE_PATH__ = undefined;
localStorage.clear();
document.body.innerHTML = "";
});
describe("chat markdown rendering", () => {
it("renders markdown inside tool result cards", async () => {
localStorage.setItem(
"clawdbot.control.settings.v1",
JSON.stringify({ useNewChatLayout: false }),
);
const app = mountApp("/chat");
await app.updateComplete;

View File

@@ -0,0 +1,12 @@
/**
* Chat-related constants for the UI layer.
*/
/** Character threshold for showing tool output inline vs collapsed */
export const TOOL_INLINE_THRESHOLD = 80;
/** Maximum lines to show in collapsed preview */
export const PREVIEW_MAX_LINES = 2;
/** Maximum characters to show in collapsed preview */
export const PREVIEW_MAX_CHARS = 100;

View File

@@ -0,0 +1,169 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import {
normalizeMessage,
normalizeRoleForGrouping,
isToolResultMessage,
} from "./message-normalizer";
describe("message-normalizer", () => {
describe("normalizeMessage", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2024-01-01T00:00:00Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("normalizes message with string content", () => {
const result = normalizeMessage({
role: "user",
content: "Hello world",
timestamp: 1000,
id: "msg-1",
});
expect(result).toEqual({
role: "user",
content: [{ type: "text", text: "Hello world" }],
timestamp: 1000,
id: "msg-1",
});
});
it("normalizes message with array content", () => {
const result = normalizeMessage({
role: "assistant",
content: [
{ type: "text", text: "Here is the result" },
{ type: "tool_use", name: "bash", args: { command: "ls" } },
],
timestamp: 2000,
});
expect(result.role).toBe("assistant");
expect(result.content).toHaveLength(2);
expect(result.content[0]).toEqual({ type: "text", text: "Here is the result", name: undefined, args: undefined });
expect(result.content[1]).toEqual({ type: "tool_use", text: undefined, name: "bash", args: { command: "ls" } });
});
it("normalizes message with text field (alternative format)", () => {
const result = normalizeMessage({
role: "user",
text: "Alternative format",
});
expect(result.content).toEqual([{ type: "text", text: "Alternative format" }]);
});
it("detects tool result by toolCallId", () => {
const result = normalizeMessage({
role: "assistant",
toolCallId: "call-123",
content: "Tool output",
});
expect(result.role).toBe("toolResult");
});
it("detects tool result by tool_call_id (snake_case)", () => {
const result = normalizeMessage({
role: "assistant",
tool_call_id: "call-456",
content: "Tool output",
});
expect(result.role).toBe("toolResult");
});
it("handles missing role", () => {
const result = normalizeMessage({ content: "No role" });
expect(result.role).toBe("unknown");
});
it("handles missing content", () => {
const result = normalizeMessage({ role: "user" });
expect(result.content).toEqual([]);
});
it("uses current timestamp when not provided", () => {
const result = normalizeMessage({ role: "user", content: "Test" });
expect(result.timestamp).toBe(Date.now());
});
it("handles arguments field (alternative to args)", () => {
const result = normalizeMessage({
role: "assistant",
content: [{ type: "tool_use", name: "test", arguments: { foo: "bar" } }],
});
expect(result.content[0].args).toEqual({ foo: "bar" });
});
});
describe("normalizeRoleForGrouping", () => {
it("returns assistant for toolresult", () => {
expect(normalizeRoleForGrouping("toolresult")).toBe("assistant");
expect(normalizeRoleForGrouping("toolResult")).toBe("assistant");
expect(normalizeRoleForGrouping("TOOLRESULT")).toBe("assistant");
});
it("returns assistant for tool_result", () => {
expect(normalizeRoleForGrouping("tool_result")).toBe("assistant");
expect(normalizeRoleForGrouping("TOOL_RESULT")).toBe("assistant");
});
it("returns assistant for tool", () => {
expect(normalizeRoleForGrouping("tool")).toBe("assistant");
expect(normalizeRoleForGrouping("Tool")).toBe("assistant");
});
it("returns assistant for function", () => {
expect(normalizeRoleForGrouping("function")).toBe("assistant");
expect(normalizeRoleForGrouping("Function")).toBe("assistant");
});
it("preserves user role", () => {
expect(normalizeRoleForGrouping("user")).toBe("user");
expect(normalizeRoleForGrouping("User")).toBe("User");
});
it("preserves assistant role", () => {
expect(normalizeRoleForGrouping("assistant")).toBe("assistant");
});
it("preserves system role", () => {
expect(normalizeRoleForGrouping("system")).toBe("system");
});
});
describe("isToolResultMessage", () => {
it("returns true for toolresult role", () => {
expect(isToolResultMessage({ role: "toolresult" })).toBe(true);
expect(isToolResultMessage({ role: "toolResult" })).toBe(true);
expect(isToolResultMessage({ role: "TOOLRESULT" })).toBe(true);
});
it("returns true for tool_result role", () => {
expect(isToolResultMessage({ role: "tool_result" })).toBe(true);
expect(isToolResultMessage({ role: "TOOL_RESULT" })).toBe(true);
});
it("returns false for other roles", () => {
expect(isToolResultMessage({ role: "user" })).toBe(false);
expect(isToolResultMessage({ role: "assistant" })).toBe(false);
expect(isToolResultMessage({ role: "tool" })).toBe(false);
});
it("returns false for missing role", () => {
expect(isToolResultMessage({})).toBe(false);
expect(isToolResultMessage({ content: "test" })).toBe(false);
});
it("returns false for non-string role", () => {
expect(isToolResultMessage({ role: 123 })).toBe(false);
expect(isToolResultMessage({ role: null })).toBe(false);
});
});
});

View File

@@ -0,0 +1,69 @@
/**
* Message normalization utilities for chat rendering.
*/
import type {
NormalizedMessage,
MessageContentItem,
} from "../types/chat-types";
/**
* Normalize a raw message object into a consistent structure.
*/
export function normalizeMessage(message: unknown): NormalizedMessage {
const m = message as Record<string, unknown>;
let role = typeof m.role === "string" ? m.role : "unknown";
// Detect tool result messages by presence of toolCallId or tool_call_id
if (typeof m.toolCallId === "string" || typeof m.tool_call_id === "string") {
role = "toolResult";
}
// Extract content
let content: MessageContentItem[] = [];
if (typeof m.content === "string") {
content = [{ type: "text", text: m.content }];
} else if (Array.isArray(m.content)) {
content = m.content.map((item: Record<string, unknown>) => ({
type: (item.type as MessageContentItem["type"]) || "text",
text: item.text as string | undefined,
name: item.name as string | undefined,
args: item.args || item.arguments,
}));
} else if (typeof m.text === "string") {
content = [{ type: "text", text: m.text }];
}
const timestamp = typeof m.timestamp === "number" ? m.timestamp : Date.now();
const id = typeof m.id === "string" ? m.id : undefined;
return { role, content, timestamp, id };
}
/**
* Normalize role for grouping purposes.
* Tool results should be grouped with assistant messages.
*/
export function normalizeRoleForGrouping(role: string): string {
const lower = role.toLowerCase();
// All tool-related roles should display as assistant
if (
lower === "toolresult" ||
lower === "tool_result" ||
lower === "tool" ||
lower === "function"
) {
return "assistant";
}
return role;
}
/**
* Check if a message is a tool result message based on its role.
*/
export function isToolResultMessage(message: unknown): boolean {
const m = message as Record<string, unknown>;
const role = typeof m.role === "string" ? m.role.toLowerCase() : "";
return role === "toolresult" || role === "tool_result";
}

View File

@@ -0,0 +1,141 @@
import { describe, it, expect } from "vitest";
import { formatToolOutputForSidebar, getTruncatedPreview } from "./tool-helpers";
describe("tool-helpers", () => {
describe("formatToolOutputForSidebar", () => {
it("formats valid JSON object as code block", () => {
const input = '{"name":"test","value":123}';
const result = formatToolOutputForSidebar(input);
expect(result).toBe(`\`\`\`json
{
"name": "test",
"value": 123
}
\`\`\``);
});
it("formats valid JSON array as code block", () => {
const input = '[1, 2, 3]';
const result = formatToolOutputForSidebar(input);
expect(result).toBe(`\`\`\`json
[
1,
2,
3
]
\`\`\``);
});
it("handles nested JSON objects", () => {
const input = '{"outer":{"inner":"value"}}';
const result = formatToolOutputForSidebar(input);
expect(result).toContain("```json");
expect(result).toContain('"outer"');
expect(result).toContain('"inner"');
});
it("returns plain text for non-JSON content", () => {
const input = "This is plain text output";
const result = formatToolOutputForSidebar(input);
expect(result).toBe("This is plain text output");
});
it("returns as-is for invalid JSON starting with {", () => {
const input = "{not valid json";
const result = formatToolOutputForSidebar(input);
expect(result).toBe("{not valid json");
});
it("returns as-is for invalid JSON starting with [", () => {
const input = "[not valid json";
const result = formatToolOutputForSidebar(input);
expect(result).toBe("[not valid json");
});
it("trims whitespace before detecting JSON", () => {
const input = ' {"trimmed": true} ';
const result = formatToolOutputForSidebar(input);
expect(result).toContain("```json");
expect(result).toContain('"trimmed"');
});
it("handles empty string", () => {
const result = formatToolOutputForSidebar("");
expect(result).toBe("");
});
it("handles whitespace-only string", () => {
const result = formatToolOutputForSidebar(" ");
expect(result).toBe(" ");
});
});
describe("getTruncatedPreview", () => {
it("returns short text unchanged", () => {
const input = "Short text";
const result = getTruncatedPreview(input);
expect(result).toBe("Short text");
});
it("truncates text longer than max chars", () => {
const input = "a".repeat(150);
const result = getTruncatedPreview(input);
expect(result.length).toBe(101); // 100 chars + ellipsis
expect(result.endsWith("…")).toBe(true);
});
it("truncates to max lines", () => {
const input = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5";
const result = getTruncatedPreview(input);
// Should only show first 2 lines (PREVIEW_MAX_LINES = 2)
expect(result).toBe("Line 1\nLine 2…");
});
it("adds ellipsis when lines are truncated", () => {
const input = "Line 1\nLine 2\nLine 3";
const result = getTruncatedPreview(input);
expect(result.endsWith("…")).toBe(true);
});
it("does not add ellipsis when all lines fit", () => {
const input = "Line 1\nLine 2";
const result = getTruncatedPreview(input);
expect(result).toBe("Line 1\nLine 2");
expect(result.endsWith("…")).toBe(false);
});
it("handles single line within limits", () => {
const input = "Single line";
const result = getTruncatedPreview(input);
expect(result).toBe("Single line");
});
it("handles empty string", () => {
const result = getTruncatedPreview("");
expect(result).toBe("");
});
it("truncates by chars even within line limit", () => {
// Two lines but very long content
const longLine = "x".repeat(80);
const input = `${longLine}\n${longLine}`;
const result = getTruncatedPreview(input);
expect(result.length).toBe(101); // 100 + ellipsis
expect(result.endsWith("…")).toBe(true);
});
});
});

View File

@@ -0,0 +1,36 @@
/**
* Helper functions for tool card rendering.
*/
import { PREVIEW_MAX_CHARS, PREVIEW_MAX_LINES } from "./constants";
/**
* Format tool output content for display in the sidebar.
* Detects JSON and wraps it in a code block with formatting.
*/
export function formatToolOutputForSidebar(text: string): string {
const trimmed = text.trim();
// Try to detect and format JSON
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
try {
const parsed = JSON.parse(trimmed);
return "```json\n" + JSON.stringify(parsed, null, 2) + "\n```";
} catch {
// Not valid JSON, return as-is
}
}
return text;
}
/**
* Get a truncated preview of tool output text.
* Truncates to first N lines or first N characters, whichever is shorter.
*/
export function getTruncatedPreview(text: string): string {
const lines = text.split("\n").slice(0, PREVIEW_MAX_LINES);
const preview = lines.join("\n");
if (preview.length > PREVIEW_MAX_CHARS) {
return preview.slice(0, PREVIEW_MAX_CHARS) + "…";
}
return lines.length < text.split("\n").length ? preview + "…" : preview;
}

View File

@@ -0,0 +1,109 @@
import { LitElement, html, css } from "lit";
import { customElement, property } from "lit/decorators.js";
/**
* A draggable divider for resizable split views.
* Dispatches 'resize' events with { splitRatio: number } detail.
*/
@customElement("resizable-divider")
export class ResizableDivider extends LitElement {
@property({ type: Number }) splitRatio = 0.6;
@property({ type: Number }) minRatio = 0.4;
@property({ type: Number }) maxRatio = 0.7;
private isDragging = false;
private startX = 0;
private startRatio = 0;
static styles = css`
:host {
width: 4px;
cursor: col-resize;
background: var(--border, #333);
transition: background 150ms ease-out;
flex-shrink: 0;
position: relative;
}
:host::before {
content: "";
position: absolute;
top: 0;
left: -4px;
right: -4px;
bottom: 0;
}
:host(:hover) {
background: var(--accent, #007bff);
}
:host(.dragging) {
background: var(--accent, #007bff);
}
`;
render() {
return html``;
}
connectedCallback() {
super.connectedCallback();
this.addEventListener("mousedown", this.handleMouseDown);
}
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener("mousedown", this.handleMouseDown);
document.removeEventListener("mousemove", this.handleMouseMove);
document.removeEventListener("mouseup", this.handleMouseUp);
}
private handleMouseDown = (e: MouseEvent) => {
this.isDragging = true;
this.startX = e.clientX;
this.startRatio = this.splitRatio;
this.classList.add("dragging");
document.addEventListener("mousemove", this.handleMouseMove);
document.addEventListener("mouseup", this.handleMouseUp);
e.preventDefault();
};
private handleMouseMove = (e: MouseEvent) => {
if (!this.isDragging) return;
const container = this.parentElement;
if (!container) return;
const containerWidth = container.getBoundingClientRect().width;
const deltaX = e.clientX - this.startX;
const deltaRatio = deltaX / containerWidth;
let newRatio = this.startRatio + deltaRatio;
newRatio = Math.max(this.minRatio, Math.min(this.maxRatio, newRatio));
this.dispatchEvent(
new CustomEvent("resize", {
detail: { splitRatio: newRatio },
bubbles: true,
composed: true,
})
);
};
private handleMouseUp = () => {
this.isDragging = false;
this.classList.remove("dragging");
document.removeEventListener("mousemove", this.handleMouseMove);
document.removeEventListener("mouseup", this.handleMouseUp);
};
}
declare global {
interface HTMLElementTagNameMap {
"resizable-divider": ResizableDivider;
}
}

View File

@@ -65,4 +65,3 @@ describe("chat focus mode", () => {
expect(shell?.classList.contains("shell--chat-focus")).toBe(true);
});
});

View File

@@ -0,0 +1,190 @@
import { describe, expect, it } from "vitest";
import {
TAB_GROUPS,
iconForTab,
inferBasePathFromPathname,
normalizeBasePath,
normalizePath,
pathForTab,
subtitleForTab,
tabFromPath,
titleForTab,
type Tab,
} from "./navigation";
/** All valid tab identifiers derived from TAB_GROUPS */
const ALL_TABS: Tab[] = TAB_GROUPS.flatMap((group) => group.tabs) as Tab[];
describe("iconForTab", () => {
it("returns a non-empty string for every tab", () => {
for (const tab of ALL_TABS) {
const icon = iconForTab(tab);
expect(icon).toBeTruthy();
expect(typeof icon).toBe("string");
expect(icon.length).toBeGreaterThan(0);
}
});
it("returns stable icons for known tabs", () => {
expect(iconForTab("chat")).toBe("💬");
expect(iconForTab("overview")).toBe("📊");
expect(iconForTab("connections")).toBe("🔗");
expect(iconForTab("instances")).toBe("📡");
expect(iconForTab("sessions")).toBe("📄");
expect(iconForTab("cron")).toBe("⏰");
expect(iconForTab("skills")).toBe("⚡️");
expect(iconForTab("nodes")).toBe("🖥️");
expect(iconForTab("config")).toBe("⚙️");
expect(iconForTab("debug")).toBe("🐞");
expect(iconForTab("logs")).toBe("🧾");
});
it("returns a fallback icon for unknown tab", () => {
// TypeScript won't allow this normally, but runtime could receive unexpected values
const unknownTab = "unknown" as Tab;
expect(iconForTab(unknownTab)).toBe("📁");
});
});
describe("titleForTab", () => {
it("returns a non-empty string for every tab", () => {
for (const tab of ALL_TABS) {
const title = titleForTab(tab);
expect(title).toBeTruthy();
expect(typeof title).toBe("string");
}
});
it("returns expected titles", () => {
expect(titleForTab("chat")).toBe("Chat");
expect(titleForTab("overview")).toBe("Overview");
expect(titleForTab("cron")).toBe("Cron Jobs");
});
});
describe("subtitleForTab", () => {
it("returns a string for every tab", () => {
for (const tab of ALL_TABS) {
const subtitle = subtitleForTab(tab);
expect(typeof subtitle).toBe("string");
}
});
it("returns descriptive subtitles", () => {
expect(subtitleForTab("chat")).toContain("chat session");
expect(subtitleForTab("config")).toContain("clawdbot.json");
});
});
describe("normalizeBasePath", () => {
it("returns empty string for falsy input", () => {
expect(normalizeBasePath("")).toBe("");
});
it("adds leading slash if missing", () => {
expect(normalizeBasePath("ui")).toBe("/ui");
});
it("removes trailing slash", () => {
expect(normalizeBasePath("/ui/")).toBe("/ui");
});
it("returns empty string for root path", () => {
expect(normalizeBasePath("/")).toBe("");
});
it("handles nested paths", () => {
expect(normalizeBasePath("/apps/clawdbot")).toBe("/apps/clawdbot");
});
});
describe("normalizePath", () => {
it("returns / for falsy input", () => {
expect(normalizePath("")).toBe("/");
});
it("adds leading slash if missing", () => {
expect(normalizePath("chat")).toBe("/chat");
});
it("removes trailing slash except for root", () => {
expect(normalizePath("/chat/")).toBe("/chat");
expect(normalizePath("/")).toBe("/");
});
});
describe("pathForTab", () => {
it("returns correct path without base", () => {
expect(pathForTab("chat")).toBe("/chat");
expect(pathForTab("overview")).toBe("/overview");
});
it("prepends base path", () => {
expect(pathForTab("chat", "/ui")).toBe("/ui/chat");
expect(pathForTab("sessions", "/apps/clawdbot")).toBe("/apps/clawdbot/sessions");
});
});
describe("tabFromPath", () => {
it("returns tab for valid path", () => {
expect(tabFromPath("/chat")).toBe("chat");
expect(tabFromPath("/overview")).toBe("overview");
expect(tabFromPath("/sessions")).toBe("sessions");
});
it("returns chat for root path", () => {
expect(tabFromPath("/")).toBe("chat");
});
it("handles base paths", () => {
expect(tabFromPath("/ui/chat", "/ui")).toBe("chat");
expect(tabFromPath("/apps/clawdbot/sessions", "/apps/clawdbot")).toBe("sessions");
});
it("returns null for unknown path", () => {
expect(tabFromPath("/unknown")).toBeNull();
});
it("is case-insensitive", () => {
expect(tabFromPath("/CHAT")).toBe("chat");
expect(tabFromPath("/Overview")).toBe("overview");
});
});
describe("inferBasePathFromPathname", () => {
it("returns empty string for root", () => {
expect(inferBasePathFromPathname("/")).toBe("");
});
it("returns empty string for direct tab path", () => {
expect(inferBasePathFromPathname("/chat")).toBe("");
expect(inferBasePathFromPathname("/overview")).toBe("");
});
it("infers base path from nested paths", () => {
expect(inferBasePathFromPathname("/ui/chat")).toBe("/ui");
expect(inferBasePathFromPathname("/apps/clawdbot/sessions")).toBe("/apps/clawdbot");
});
it("handles index.html suffix", () => {
expect(inferBasePathFromPathname("/index.html")).toBe("");
expect(inferBasePathFromPathname("/ui/index.html")).toBe("/ui");
});
});
describe("TAB_GROUPS", () => {
it("contains all expected groups", () => {
const labels = TAB_GROUPS.map((g) => g.label);
expect(labels).toContain("Chat");
expect(labels).toContain("Control");
expect(labels).toContain("Agent");
expect(labels).toContain("Settings");
});
it("all tabs are unique", () => {
const allTabs = TAB_GROUPS.flatMap((g) => g.tabs);
const uniqueTabs = new Set(allTabs);
expect(uniqueTabs.size).toBe(allTabs.length);
});
});

View File

@@ -98,6 +98,35 @@ export function inferBasePathFromPathname(pathname: string): string {
return `/${segments.join("/")}`;
}
export function iconForTab(tab: Tab): string {
switch (tab) {
case "chat":
return "💬";
case "overview":
return "📊";
case "connections":
return "🔗";
case "instances":
return "📡";
case "sessions":
return "📄";
case "cron":
return "⏰";
case "skills":
return "⚡️";
case "nodes":
return "🖥️";
case "config":
return "⚙️";
case "debug":
return "🐞";
case "logs":
return "🧾";
default:
return "📁";
}
}
export function titleForTab(tab: Tab) {
switch (tab) {
case "overview":

View File

@@ -9,6 +9,10 @@ export type UiSettings = {
lastActiveSessionKey: string;
theme: ThemeMode;
chatFocusMode: boolean;
splitRatio: number; // Sidebar split ratio (0.4 to 0.7, default 0.6)
useNewChatLayout: boolean; // Slack-style grouped messages layout
navCollapsed: boolean; // Collapsible sidebar state
navGroupsCollapsed: Record<string, boolean>; // Which nav groups are collapsed
};
export function loadSettings(): UiSettings {
@@ -24,6 +28,10 @@ export function loadSettings(): UiSettings {
lastActiveSessionKey: "main",
theme: "system",
chatFocusMode: false,
splitRatio: 0.6,
useNewChatLayout: true, // Enabled by default
navCollapsed: false,
navGroupsCollapsed: {},
};
try {
@@ -57,6 +65,25 @@ export function loadSettings(): UiSettings {
typeof parsed.chatFocusMode === "boolean"
? parsed.chatFocusMode
: defaults.chatFocusMode,
splitRatio:
typeof parsed.splitRatio === "number" &&
parsed.splitRatio >= 0.4 &&
parsed.splitRatio <= 0.7
? parsed.splitRatio
: defaults.splitRatio,
useNewChatLayout:
typeof parsed.useNewChatLayout === "boolean"
? parsed.useNewChatLayout
: defaults.useNewChatLayout,
navCollapsed:
typeof parsed.navCollapsed === "boolean"
? parsed.navCollapsed
: defaults.navCollapsed,
navGroupsCollapsed:
typeof parsed.navGroupsCollapsed === "object" &&
parsed.navGroupsCollapsed !== null
? parsed.navGroupsCollapsed
: defaults.navGroupsCollapsed,
};
} catch {
return defaults;

View File

@@ -0,0 +1,43 @@
/**
* Chat message types for the UI layer.
*/
/** Union type for items in the chat thread */
export type ChatItem =
| { kind: "message"; key: string; message: unknown }
| { kind: "stream"; key: string; text: string; startedAt: number }
| { kind: "reading-indicator"; key: string };
/** A group of consecutive messages from the same role (Slack-style layout) */
export type MessageGroup = {
kind: "group";
key: string;
role: string;
messages: Array<{ message: unknown; key: string }>;
timestamp: number;
isStreaming: boolean;
};
/** Content item types in a normalized message */
export type MessageContentItem = {
type: "text" | "tool_call" | "tool_result";
text?: string;
name?: string;
args?: unknown;
};
/** Normalized message structure for rendering */
export type NormalizedMessage = {
role: string;
content: MessageContentItem[];
timestamp: number;
id?: string;
};
/** Tool card representation for tool calls and results */
export type ToolCard = {
kind: "call" | "result";
name: string;
args?: unknown;
text?: string;
};

View File

@@ -7,6 +7,19 @@ import { toSanitizedMarkdownHtml } from "../markdown";
import { formatToolDetail, resolveToolDisplay } from "../tool-display";
import type { SessionsListResult } from "../types";
import type { ChatQueueItem } from "../ui-types";
import type { ChatItem, MessageGroup, ToolCard } from "../types/chat-types";
import { TOOL_INLINE_THRESHOLD } from "../chat/constants";
import {
formatToolOutputForSidebar,
getTruncatedPreview,
} from "../chat/tool-helpers";
import {
normalizeMessage,
normalizeRoleForGrouping,
isToolResultMessage,
} from "../chat/message-normalizer";
import { renderMarkdownSidebar } from "./markdown-sidebar";
import "../components/resizable-divider";
export type ChatProps = {
sessionKey: string;
@@ -25,97 +38,140 @@ export type ChatProps = {
disabledReason: string | null;
error: string | null;
sessions: SessionsListResult | null;
// Legacy tool output expand/collapse (used when useNewChatLayout is false)
isToolOutputExpanded: (id: string) => boolean;
onToolOutputToggle: (id: string, expanded: boolean) => void;
// Focus mode
focusMode: boolean;
// Feature flag for new Slack-style layout with sidebar
useNewChatLayout?: boolean;
// Sidebar state (used when useNewChatLayout is true)
sidebarOpen?: boolean;
sidebarContent?: string | null;
sidebarError?: string | null;
splitRatio?: number;
// Event handlers
onRefresh: () => void;
onToggleFocusMode: () => void;
onToggleLayout?: () => void;
onDraftChange: (next: string) => void;
onSend: () => void;
onQueueRemove: (id: string) => void;
onNewSession: () => void;
onOpenSidebar?: (content: string) => void;
onCloseSidebar?: () => void;
onSplitRatioChange?: (ratio: number) => void;
};
export function renderChat(props: ChatProps) {
const canCompose = props.connected;
const isBusy = props.sending || Boolean(props.stream);
const sessionOptions = resolveSessionOptions(props.sessionKey, props.sessions);
const activeSession = props.sessions?.sessions?.find(
(row) => row.key === props.sessionKey,
);
const reasoningLevel = activeSession?.reasoningLevel ?? "off";
const showReasoning = reasoningLevel !== "off";
const composePlaceholder = props.connected
? "Message (↩ to send, Shift+↩ for line breaks)"
: "Connect to the gateway to start chatting…";
const splitRatio = props.splitRatio ?? 0.6;
const sidebarOpen = Boolean(props.sidebarOpen && props.onCloseSidebar);
const useNewLayout = props.useNewChatLayout ?? false;
return html`
<section class="card chat">
<div class="chat-header">
<div class="chat-header__left">
<label class="field chat-session">
<span>Session Key</span>
<select
.value=${props.sessionKey}
?disabled=${!props.connected}
@change=${(e: Event) =>
props.onSessionKeyChange((e.target as HTMLSelectElement).value)}
${props.disabledReason
? html`<div class="callout">${props.disabledReason}</div>`
: nothing}
${props.error
? html`<div class="callout danger">${props.error}</div>`
: nothing}
${props.focusMode
? html`
<button
class="chat-focus-exit"
type="button"
@click=${props.onToggleFocusMode}
aria-label="Exit focus mode"
title="Exit focus mode"
>
${sessionOptions.map(
(entry) =>
html`<option value=${entry.key}>
${entry.displayName ?? entry.key}
</option>`
)}
</select>
</label>
<button
class="btn"
?disabled=${props.loading || !props.connected}
@click=${props.onRefresh}
>
${props.loading ? "Loading…" : "Refresh"}
</button>
</button>
`
: nothing}
<div
class="chat-split-container ${sidebarOpen ? "chat-split-container--open" : ""}"
>
<div
class="chat-main"
style="flex: ${sidebarOpen ? `0 0 ${splitRatio * 100}%` : "1 1 100%"}"
>
<div class="chat-thread" role="log" aria-live="polite">
${props.loading
? html`<div class="muted">Loading chat…</div>`
: nothing}
${repeat(buildChatItems(props), (item) => item.key, (item) => {
if (item.kind === "reading-indicator") {
return useNewLayout
? renderReadingIndicatorGroup()
: renderReadingIndicator();
}
if (item.kind === "stream") {
return useNewLayout
? renderStreamingGroup(
item.text,
item.startedAt,
props.onOpenSidebar,
)
: renderMessage(
{
role: "assistant",
content: [{ type: "text", text: item.text }],
timestamp: item.startedAt,
},
props,
{ streaming: true, showReasoning },
);
}
if (item.kind === "group") {
return renderMessageGroup(item, {
onOpenSidebar: props.onOpenSidebar,
showReasoning,
});
}
return renderMessage(item.message, props, { showReasoning });
})}
</div>
</div>
<div class="chat-header__right">
<div class="muted">Thinking: ${props.thinkingLevel ?? "inherit"}</div>
<div class="muted">Reasoning: ${reasoningLevel}</div>
</div>
</div>
${
props.disabledReason
? html`<div class="callout" style="margin-top: 12px;">
${props.disabledReason}
</div>`
: nothing
}
${
props.error
? html`<div class="callout danger" style="margin-top: 12px;">${props.error}</div>`
: nothing
}
<div class="chat-thread" role="log" aria-live="polite">
${props.loading ? html`<div class="muted">Loading chat…</div>` : nothing}
${repeat(
buildChatItems(props),
(item) => item.key,
(item) => {
if (item.kind === "reading-indicator") return renderReadingIndicator();
if (item.kind === "stream") {
return renderMessage(
{
role: "assistant",
content: [{ type: "text", text: item.text }],
timestamp: item.startedAt,
},
props,
{ streaming: true }
);
}
return renderMessage(item.message, props, { showReasoning });
}
)}
${useNewLayout && sidebarOpen
? html`
<resizable-divider
.splitRatio=${splitRatio}
@resize=${(e: CustomEvent) =>
props.onSplitRatioChange?.(e.detail.splitRatio)}
></resizable-divider>
<div class="chat-sidebar">
${renderMarkdownSidebar({
content: props.sidebarContent ?? null,
error: props.sidebarError ?? null,
onClose: props.onCloseSidebar!,
onViewRawText: () => {
if (!props.sidebarContent || !props.onOpenSidebar) return;
props.onOpenSidebar(`\`\`\`\n${props.sidebarContent}\n\`\`\``);
},
})}
</div>
`
: nothing}
</div>
${props.queue.length
@@ -157,11 +213,12 @@ export function renderChat(props: ChatProps) {
e.preventDefault();
if (canCompose) props.onSend();
}}
@input=${(e: Event) => props.onDraftChange((e.target as HTMLTextAreaElement).value)}
@input=${(e: Event) =>
props.onDraftChange((e.target as HTMLTextAreaElement).value)}
placeholder=${composePlaceholder}
></textarea>
</label>
<div class="row chat-compose__actions">
<div class="chat-compose__actions">
<button
class="btn"
?disabled=${!props.connected || props.sending}
@@ -182,14 +239,46 @@ export function renderChat(props: ChatProps) {
`;
}
type ChatItem =
| { kind: "message"; key: string; message: unknown }
| { kind: "stream"; key: string; text: string; startedAt: number }
| { kind: "reading-indicator"; key: string };
const CHAT_HISTORY_RENDER_LIMIT = 200;
function buildChatItems(props: ChatProps): ChatItem[] {
function groupMessages(items: ChatItem[]): Array<ChatItem | MessageGroup> {
const result: Array<ChatItem | MessageGroup> = [];
let currentGroup: MessageGroup | null = null;
for (const item of items) {
if (item.kind !== "message") {
if (currentGroup) {
result.push(currentGroup);
currentGroup = null;
}
result.push(item);
continue;
}
const normalized = normalizeMessage(item.message);
const role = normalizeRoleForGrouping(normalized.role);
const timestamp = normalized.timestamp || Date.now();
if (!currentGroup || currentGroup.role !== role) {
if (currentGroup) result.push(currentGroup);
currentGroup = {
kind: "group",
key: `group:${role}:${item.key}`,
role,
messages: [{ message: item.message, key: item.key }],
timestamp,
isStreaming: false,
};
} else {
currentGroup.messages.push({ message: item.message, key: item.key });
}
}
if (currentGroup) result.push(currentGroup);
return result;
}
function buildChatItems(props: ChatProps): Array<ChatItem | MessageGroup> {
const items: ChatItem[] = [];
const history = Array.isArray(props.messages) ? props.messages : [];
const tools = Array.isArray(props.toolMessages) ? props.toolMessages : [];
@@ -234,6 +323,7 @@ function buildChatItems(props: ChatProps): ChatItem[] {
}
}
if (props.useNewChatLayout) return groupMessages(items);
return items;
}
@@ -247,7 +337,8 @@ function messageKey(message: unknown, index: number): string {
if (messageId) return `msg:${messageId}`;
const timestamp = typeof m.timestamp === "number" ? m.timestamp : null;
const role = typeof m.role === "string" ? m.role : "unknown";
const fingerprint = extractText(message) ?? (typeof m.content === "string" ? m.content : null);
const fingerprint =
extractText(message) ?? (typeof m.content === "string" ? m.content : null);
const seed = fingerprint ?? safeJson(message) ?? String(index);
const hash = fnv1a(seed);
return timestamp ? `msg:${role}:${timestamp}:${hash}` : `msg:${role}:${hash}`;
@@ -270,51 +361,6 @@ function fnv1a(input: string): string {
return (hash >>> 0).toString(36);
}
type SessionOption = {
key: string;
updatedAt?: number | null;
displayName?: string;
};
function resolveSessionOptions(currentKey: string, sessions: SessionsListResult | null) {
const now = Date.now();
const cutoff = now - 24 * 60 * 60 * 1000;
const entries = Array.isArray(sessions?.sessions) ? (sessions?.sessions ?? []) : [];
const sorted = [...entries].sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0));
const recent: SessionOption[] = [];
const seen = new Set<string>();
for (const entry of sorted) {
if (seen.has(entry.key)) continue;
seen.add(entry.key);
if ((entry.updatedAt ?? 0) < cutoff) continue;
recent.push(entry);
}
const result: SessionOption[] = [];
const included = new Set<string>();
const mainKey = "main";
const mainEntry = sorted.find((entry) => entry.key === mainKey);
if (mainEntry) {
result.push(mainEntry);
included.add(mainKey);
} else if (currentKey === mainKey) {
result.push({ key: mainKey, updatedAt: null });
included.add(mainKey);
}
for (const entry of recent) {
if (included.has(entry.key)) continue;
result.push(entry);
included.add(entry.key);
}
if (!included.has(currentKey)) {
result.push({ key: currentKey, updatedAt: null });
}
return result;
}
function renderReadingIndicator() {
return html`
<div class="chat-line assistant">
@@ -329,21 +375,37 @@ function renderReadingIndicator() {
`;
}
function renderReadingIndicatorGroup() {
return html`
<div class="chat-group assistant">
${renderAvatar("assistant")}
<div class="chat-group-messages">
<div class="chat-bubble chat-reading-indicator" aria-hidden="true">
<span class="chat-reading-indicator__dots">
<span></span><span></span><span></span>
</span>
</div>
</div>
</div>
`;
}
function renderMessage(
message: unknown,
props?: Pick<ChatProps, "isToolOutputExpanded" | "onToolOutputToggle">,
opts?: { streaming?: boolean; showReasoning?: boolean }
opts?: { streaming?: boolean; showReasoning?: boolean },
) {
const m = message as Record<string, unknown>;
const role = typeof m.role === "string" ? m.role : "unknown";
const toolCards = extractToolCards(message);
const hasToolCards = toolCards.length > 0;
const isToolResult = isToolResultMessage(message);
const isToolResult =
isToolResultMessage(message) ||
typeof m.toolCallId === "string" ||
typeof m.tool_call_id === "string";
const extractedText = extractText(message);
const extractedThinking =
opts?.showReasoning && role === "assistant"
? extractThinking(message)
: null;
opts?.showReasoning && role === "assistant" ? extractThinking(message) : null;
const contentText = typeof m.content === "string" ? m.content : null;
const fallback = hasToolCards ? null : JSON.stringify(message, null, 2);
@@ -355,6 +417,7 @@ function renderMessage(
: !isToolResult && fallback
? { kind: "json" as const, value: fallback }
: null;
const markdownBase =
display?.kind === "json"
? ["```json", display.value, "```"].join("\n")
@@ -367,31 +430,43 @@ function renderMessage(
const timestamp =
typeof m.timestamp === "number" ? new Date(m.timestamp).toLocaleTimeString() : "";
const klass = role === "assistant" ? "assistant" : role === "user" ? "user" : "other";
const who = role === "assistant" ? "Assistant" : role === "user" ? "You" : role;
const normalizedRole = normalizeRoleForGrouping(role);
const klass =
normalizedRole === "assistant"
? "assistant"
: normalizedRole === "user"
? "user"
: "other";
const who =
normalizedRole === "assistant"
? "Assistant"
: normalizedRole === "user"
? "You"
: normalizedRole;
const toolCallId = typeof m.toolCallId === "string" ? m.toolCallId : "";
const toolCardBase =
toolCallId ||
(typeof m.id === "string" ? m.id : "") ||
(typeof m.messageId === "string" ? m.messageId : "") ||
(typeof m.timestamp === "number" ? String(m.timestamp) : "tool-card");
return html`
<div class="chat-line ${klass}">
<div class="chat-msg">
<div class="chat-bubble ${opts?.streaming ? "streaming" : ""}">
${
markdown
? html`<div class="chat-text">${unsafeHTML(toSanitizedMarkdownHtml(markdown))}</div>`
: nothing
}
${markdown
? html`<div class="chat-text">${unsafeHTML(toSanitizedMarkdownHtml(markdown))}</div>`
: nothing}
${toolCards.map((card, index) =>
renderToolCard(card, {
renderToolCardLegacy(card, {
id: `${toolCardBase}:${index}`,
expanded: props?.isToolOutputExpanded
? props.isToolOutputExpanded(`${toolCardBase}:${index}`)
: false,
onToggle: props?.onToolOutputToggle,
})
}),
)}
</div>
<div class="chat-stamp mono">
@@ -446,7 +521,11 @@ function extractThinking(message: unknown): string | null {
// Back-compat: older logs may still have <think> tags inside text blocks.
const rawText = extractRawText(message);
if (!rawText) return null;
const matches = [...rawText.matchAll(/<\s*think(?:ing)?\s*>([\s\S]*?)<\s*\/\s*think(?:ing)?\s*>/gi)];
const matches = [
...rawText.matchAll(
/<\s*think(?:ing)?\s*>([\s\S]*?)<\s*\/\s*think(?:ing)?\s*>/gi,
),
];
const extracted = matches
.map((m) => (m[1] ?? "").trim())
.filter(Boolean);
@@ -482,13 +561,6 @@ function formatReasoningMarkdown(text: string): string {
return lines.length ? ["_Reasoning:_", ...lines].join("\n") : "";
}
type ToolCard = {
kind: "call" | "result";
name: string;
args?: unknown;
text?: string;
};
function extractToolCards(message: unknown): ToolCard[] {
const m = message as Record<string, unknown>;
const content = normalizeContent(m.content);
@@ -516,7 +588,10 @@ function extractToolCards(message: unknown): ToolCard[] {
cards.push({ kind: "result", name, text });
}
if (isToolResultMessage(message) && !cards.some((card) => card.kind === "result")) {
if (
isToolResultMessage(message) &&
!cards.some((card) => card.kind === "result")
) {
const name =
(typeof m.toolName === "string" && m.toolName) ||
(typeof m.tool_name === "string" && m.tool_name) ||
@@ -528,13 +603,13 @@ function extractToolCards(message: unknown): ToolCard[] {
return cards;
}
function renderToolCard(
function renderToolCardLegacy(
card: ToolCard,
opts?: {
id: string;
expanded: boolean;
onToggle?: (id: string, expanded: boolean) => void;
}
},
) {
const display = resolveToolDisplay({ name: card.name, args: card.args });
const detail = formatToolDetail(display);
@@ -543,11 +618,18 @@ function renderToolCard(
const id = opts?.id ?? `${card.name}-${Math.random()}`;
return html`
<div class="chat-tool-card">
<div class="chat-tool-card__title">${display.emoji} ${display.label}</div>
${detail ? html`<div class="chat-tool-card__detail">${detail}</div>` : nothing}
${
hasOutput
? html`
<div class="chat-tool-card__header">
<div class="chat-tool-card__title">
<span class="chat-tool-card__icon">${display.emoji}</span>
<span>${display.label}</span>
</div>
${!hasOutput ? html`<span class="chat-tool-card__status">✓</span>` : nothing}
</div>
${detail
? html`<div class="chat-tool-card__detail">${detail}</div>`
: nothing}
${hasOutput
? html`
<details
class="chat-tool-card__details"
?open=${expanded}
@@ -563,17 +645,219 @@ function renderToolCard(
(${card.text?.length ?? 0} chars)
</span>
</summary>
${
expanded
? html`<div class="chat-tool-card__output chat-text">
${expanded
? html`<div class="chat-tool-card__output chat-text">
${unsafeHTML(toSanitizedMarkdownHtml(card.text ?? ""))}
</div>`
: nothing
}
: nothing}
</details>
`
: nothing
: nothing}
</div>
`;
}
function renderToolCardSidebar(
card: ToolCard,
onOpenSidebar?: (content: string) => void,
) {
const display = resolveToolDisplay({ name: card.name, args: card.args });
const detail = formatToolDetail(display);
const hasText = Boolean(card.text?.trim());
const canClick = Boolean(onOpenSidebar);
const handleClick = canClick
? () => {
if (hasText) {
onOpenSidebar!(formatToolOutputForSidebar(card.text!));
return;
}
const info = `## ${display.label}\n\n${
detail ? `**Command:** \`${detail}\`\n\n` : ""
}*No output — tool completed successfully.*`;
onOpenSidebar!(info);
}
: undefined;
const isShort = hasText && (card.text?.length ?? 0) <= TOOL_INLINE_THRESHOLD;
const showCollapsed = hasText && !isShort;
const showInline = hasText && isShort;
const isEmpty = !hasText;
return html`
<div
class="chat-tool-card ${canClick ? "chat-tool-card--clickable" : ""}"
@click=${handleClick}
role=${canClick ? "button" : nothing}
tabindex=${canClick ? "0" : nothing}
@keydown=${canClick
? (e: KeyboardEvent) => {
if (e.key !== "Enter" && e.key !== " ") return;
e.preventDefault();
handleClick?.();
}
: nothing}
>
<div class="chat-tool-card__header">
<div class="chat-tool-card__title">
<span class="chat-tool-card__icon">${display.emoji}</span>
<span>${display.label}</span>
</div>
${canClick
? html`<span class="chat-tool-card__action">${hasText ? "View " : ""}</span>`
: nothing}
${isEmpty && !canClick ? html`<span class="chat-tool-card__status">✓</span>` : nothing}
</div>
${detail
? html`<div class="chat-tool-card__detail">${detail}</div>`
: nothing}
${isEmpty
? html`<div class="chat-tool-card__status-text muted">Completed</div>`
: nothing}
${showCollapsed
? html`<div class="chat-tool-card__preview mono">${getTruncatedPreview(card.text!)}</div>`
: nothing}
${showInline
? html`<div class="chat-tool-card__inline mono">${card.text}</div>`
: nothing}
</div>
`;
}
function renderAvatar(role: string) {
const normalized = normalizeRoleForGrouping(role);
const initial = normalized === "user" ? "U" : normalized === "assistant" ? "A" : "?";
const className = normalized === "user" ? "user" : normalized === "assistant" ? "assistant" : "other";
return html`<div class="chat-avatar ${className}">${initial}</div>`;
}
function renderStreamingGroup(
text: string,
startedAt: number,
onOpenSidebar?: (content: string) => void,
) {
const timestamp = new Date(startedAt).toLocaleTimeString([], {
hour: "numeric",
minute: "2-digit",
});
return html`
<div class="chat-group assistant">
${renderAvatar("assistant")}
<div class="chat-group-messages">
${renderGroupedMessage(
{
role: "assistant",
content: [{ type: "text", text }],
timestamp: startedAt,
},
{ isStreaming: true, showReasoning: false },
onOpenSidebar,
)}
<div class="chat-group-footer">
<span class="chat-sender-name">Assistant</span>
<span class="chat-group-timestamp">${timestamp}</span>
</div>
</div>
</div>
`;
}
function renderMessageGroup(
group: MessageGroup,
opts: { onOpenSidebar?: (content: string) => void; showReasoning: boolean },
) {
const normalizedRole = normalizeRoleForGrouping(group.role);
const who =
normalizedRole === "user"
? "You"
: normalizedRole === "assistant"
? "Assistant"
: normalizedRole;
const roleClass =
normalizedRole === "user"
? "user"
: normalizedRole === "assistant"
? "assistant"
: "other";
const timestamp = new Date(group.timestamp).toLocaleTimeString([], {
hour: "numeric",
minute: "2-digit",
});
return html`
<div class="chat-group ${roleClass}">
${renderAvatar(group.role)}
<div class="chat-group-messages">
${group.messages.map((item, index) =>
renderGroupedMessage(
item.message,
{
isStreaming:
group.isStreaming && index === group.messages.length - 1,
showReasoning: opts.showReasoning,
},
opts.onOpenSidebar,
),
)}
<div class="chat-group-footer">
<span class="chat-sender-name">${who}</span>
<span class="chat-group-timestamp">${timestamp}</span>
</div>
</div>
</div>
`;
}
function renderGroupedMessage(
message: unknown,
opts: { isStreaming: boolean; showReasoning: boolean },
onOpenSidebar?: (content: string) => void,
) {
const m = message as Record<string, unknown>;
const role = typeof m.role === "string" ? m.role : "unknown";
const isToolResult =
isToolResultMessage(message) ||
role.toLowerCase() === "toolresult" ||
role.toLowerCase() === "tool_result" ||
typeof m.toolCallId === "string" ||
typeof m.tool_call_id === "string";
const toolCards = extractToolCards(message);
const hasToolCards = toolCards.length > 0;
const extractedText = extractText(message);
const extractedThinking =
opts.showReasoning && role === "assistant" ? extractThinking(message) : null;
const markdownBase = extractedText?.trim() ? extractedText : null;
const markdown = extractedThinking
? [formatReasoningMarkdown(extractedThinking), markdownBase]
.filter(Boolean)
.join("\n\n")
: markdownBase;
const bubbleClasses = [
"chat-bubble",
opts.isStreaming ? "streaming" : "",
"fade-in",
]
.filter(Boolean)
.join(" ");
if (!markdown && hasToolCards && isToolResult) {
return html`${toolCards.map((card) =>
renderToolCardSidebar(card, onOpenSidebar),
)}`;
}
if (!markdown && !hasToolCards) return nothing;
return html`
<div class="${bubbleClasses}">
${markdown
? html`<div class="chat-text">${unsafeHTML(toSanitizedMarkdownHtml(markdown))}</div>`
: nothing}
${toolCards.map((card) => renderToolCardSidebar(card, onOpenSidebar))}
</div>
`;
}
@@ -600,9 +884,3 @@ function extractToolText(item: Record<string, unknown>): string | undefined {
if (typeof item.content === "string") return item.content;
return undefined;
}
function isToolResultMessage(message: unknown): boolean {
const m = message as Record<string, unknown>;
const role = typeof m.role === "string" ? m.role.toLowerCase() : "";
return role === "toolresult" || role === "tool_result";
}

View File

@@ -0,0 +1,36 @@
import { html, nothing } from "lit";
import { unsafeHTML } from "lit/directives/unsafe-html.js";
import { toSanitizedMarkdownHtml } from "../markdown";
export type MarkdownSidebarProps = {
content: string | null;
error: string | null;
onClose: () => void;
onViewRawText: () => void;
};
export function renderMarkdownSidebar(props: MarkdownSidebarProps) {
return html`
<div class="sidebar-panel">
<div class="sidebar-header">
<div class="sidebar-title">Tool Output</div>
<button @click=${props.onClose} class="btn" title="Close sidebar">
</button>
</div>
<div class="sidebar-content">
${props.error
? html`
<div class="callout danger">${props.error}</div>
<button @click=${props.onViewRawText} class="btn" style="margin-top: 12px;">
View Raw Text
</button>
`
: props.content
? html`<div class="sidebar-markdown">${unsafeHTML(toSanitizedMarkdownHtml(props.content))}</div>`
: html`<div class="muted">No content available</div>`}
</div>
</div>
`;
}