cc 3 months ago
parent
commit
752fbc4faa

+ 1347 - 0
src/main/resources/static/css/chat.css

@@ -0,0 +1,1347 @@
+:root {
+    --sidebar-width: 260px;
+}
+
+*, *::before, *::after {
+    box-sizing: border-box;
+    margin: 0;
+    padding: 0;
+}
+
+html, body {
+    height: 100%;
+    overflow: hidden;
+}
+
+body {
+    font-family: var(--font-sans);
+    background: var(--bg-body);
+    color: var(--text-primary);
+    display: flex;
+    font-size: 14px;
+    transition: background 0.3s, color 0.3s;
+}
+
+/* ===== Sidebar ===== */
+.sidebar {
+    width: var(--sidebar-width);
+    background: var(--bg-sidebar);
+    border-right: 1px solid var(--border-color);
+    display: flex;
+    flex-direction: column;
+    flex-shrink: 0;
+    height: 100vh;
+    transition: background 0.3s;
+}
+
+.sidebar-header {
+    padding: 20px 16px 16px;
+}
+
+.new-chat-btn {
+    width: 100%;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    gap: 8px;
+    padding: 11px 0;
+    border: 1px dashed var(--border-color);
+    border-radius: 10px;
+    background: transparent;
+    color: var(--text-primary);
+    cursor: pointer;
+    font-size: 14px;
+    transition: all 0.2s;
+}
+
+.new-chat-btn:hover {
+    background: var(--accent-light);
+    border-color: var(--accent);
+    color: var(--accent);
+}
+
+.sidebar-title {
+    padding: 16px 16px 8px;
+    font-size: 11px;
+    color: var(--text-secondary);
+    font-weight: 600;
+    letter-spacing: 1px;
+    text-transform: uppercase;
+}
+
+.sidebar-list {
+    flex: 1;
+    overflow-y: auto;
+    padding: 4px 10px;
+}
+
+.sidebar-item {
+    padding: 10px 12px;
+    border-radius: 10px;
+    cursor: pointer;
+    color: var(--text-sidebar);
+    font-size: 13px;
+    display: flex;
+    align-items: center;
+    transition: all 0.15s;
+    margin-bottom: 2px;
+}
+
+.sidebar-item:hover {
+    background: var(--bg-hover);
+}
+
+.sidebar-item.active {
+    background: var(--accent-light);
+    color: var(--accent);
+    font-weight: 500;
+}
+
+.sidebar-item-label {
+    flex: 1;
+    min-width: 0;
+    white-space: nowrap;
+    overflow: hidden;
+    text-overflow: ellipsis;
+}
+
+.sidebar-item-del {
+    display: none;
+    flex-shrink: 0;
+    width: 22px;
+    height: 22px;
+    border: none;
+    background: none;
+    border-radius: 6px;
+    cursor: pointer;
+    color: var(--text-secondary);
+    font-size: 14px;
+    align-items: center;
+    justify-content: center;
+    margin-left: 4px;
+    transition: all 0.15s;
+}
+
+.sidebar-item:hover .sidebar-item-del {
+    display: flex;
+}
+
+.sidebar-item-del:hover {
+    background: #ef444422;
+    color: #ef4444;
+}
+
+.sidebar-footer {
+    padding: 14px 16px;
+    border-top: 1px solid var(--border-color);
+    display: flex;
+    flex-direction: column;
+    align-items: stretch;
+    gap: 12px;
+}
+
+.sidebar-footer-meta {
+    display: flex;
+    align-items: center;
+    gap: 10px;
+}
+
+.sidebar-config-btn {
+    width: 100%;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    gap: 8px;
+    padding: 10px 12px;
+    border: 1px solid var(--border-color);
+    border-radius: 10px;
+    background: var(--bg-main);
+    color: var(--text-primary);
+    cursor: pointer;
+    font-size: 13px;
+    transition: all 0.2s;
+}
+
+.sidebar-config-btn:hover {
+    border-color: var(--accent);
+    background: var(--accent-light);
+    color: var(--accent);
+}
+
+.model-modal {
+    position: fixed;
+    inset: 0;
+    display: none;
+    align-items: center;
+    justify-content: center;
+    padding: 24px;
+    background: rgba(15, 23, 42, 0.45);
+    z-index: 999;
+}
+
+.model-modal.active {
+    display: flex;
+}
+
+.model-modal-card {
+    width: min(680px, 100%);
+    max-height: 90vh;
+    overflow: hidden;
+    display: flex;
+    flex-direction: column;
+    border: 1px solid var(--border-color);
+    border-radius: 18px;
+    background: var(--bg-input-box);
+    box-shadow: var(--shadow-lg);
+}
+
+.model-modal-header {
+    display: flex;
+    align-items: center;
+    justify-content: space-between;
+    padding: 18px 20px 14px;
+    border-bottom: 1px solid var(--border-color);
+}
+
+.model-modal-title {
+    font-size: 18px;
+    font-weight: 700;
+    color: var(--text-primary);
+}
+
+.model-modal-close {
+    width: 34px;
+    height: 34px;
+    border: none;
+    border-radius: 10px;
+    background: transparent;
+    color: var(--text-secondary);
+    cursor: pointer;
+    transition: all 0.2s;
+}
+
+.model-modal-close:hover {
+    background: var(--bg-hover);
+    color: var(--text-primary);
+}
+
+.model-modal-body {
+    padding: 18px 20px;
+    overflow-y: auto;
+}
+
+.model-form-grid {
+    display: grid;
+    grid-template-columns: repeat(2, minmax(0, 1fr));
+    gap: 16px;
+}
+
+.model-field {
+    display: flex;
+    flex-direction: column;
+    gap: 8px;
+}
+
+.model-field.full {
+    grid-column: 1 / -1;
+}
+
+.model-field label {
+    font-size: 13px;
+    font-weight: 600;
+    color: var(--text-primary);
+}
+
+.model-field input,
+.model-field select,
+.model-field textarea {
+    width: 100%;
+    border: 1px solid var(--border-input);
+    border-radius: 12px;
+    background: var(--bg-main);
+    color: var(--text-primary);
+    padding: 11px 12px;
+    font: inherit;
+    outline: none;
+    transition: all 0.2s;
+}
+
+.model-field input:focus,
+.model-field select:focus,
+.model-field textarea:focus {
+    border-color: var(--accent);
+    box-shadow: 0 0 0 4px rgba(79, 110, 247, 0.08);
+}
+
+.model-field textarea {
+    min-height: 84px;
+    resize: vertical;
+}
+
+.model-save-btn,
+.model-cancel-btn {
+    height: 44px;
+    border-radius: 12px;
+    font-size: 14px;
+    cursor: pointer;
+    transition: all 0.2s;
+}
+
+.model-modal-footer {
+    padding: 16px 20px 20px;
+    border-top: 1px solid var(--border-color);
+    display: flex;
+    justify-content: flex-end;
+    gap: 12px;
+}
+
+.model-cancel-btn {
+    min-width: 92px;
+    border: 1px solid var(--border-color);
+    background: transparent;
+    color: var(--text-secondary);
+}
+
+.model-cancel-btn:hover {
+    background: var(--bg-hover);
+    color: var(--text-primary);
+}
+
+.model-save-btn {
+    min-width: 120px;
+    border: none;
+    background: var(--accent);
+    color: #fff;
+}
+
+.model-save-btn:hover {
+    background: var(--accent-hover);
+}
+
+.model-switch {
+    display: flex;
+    align-items: center;
+    gap: 10px;
+    padding-top: 8px;
+    color: var(--text-primary);
+}
+
+.model-switch input {
+    width: 16px;
+    height: 16px;
+}
+
+.model-hint {
+    margin-top: 4px;
+    font-size: 12px;
+    color: var(--text-secondary);
+}
+
+/* Streaming indicator for sidebar items */
+.sidebar-item.streaming {
+    border-left: 3px solid var(--accent);
+    padding-left: 9px;
+}
+
+.sidebar-item.streaming:hover .sidebar-item-del {
+    display: none;
+}
+
+.sidebar-item-spinner {
+    flex-shrink: 0;
+    width: 16px;
+    height: 16px;
+    border: 2px solid var(--border-color);
+    border-top-color: var(--accent);
+    border-radius: 50%;
+    animation: blob-spin 0.8s linear infinite;
+    margin-left: 6px;
+}
+
+/* ===== Main ===== */
+.main-area {
+    flex: 1;
+    display: flex;
+    flex-direction: column;
+    height: 100vh;
+    min-width: 0;
+    background: var(--bg-main);
+    transition: background 0.3s;
+}
+
+/* Header */
+.main-header {
+    padding: 14px 28px;
+    display: flex;
+    align-items: center;
+    justify-content: flex-end;
+    flex-shrink: 0;
+}
+
+.theme-toggle {
+    background: none;
+    border: 1px solid var(--border-color);
+    border-radius: 10px;
+    padding: 6px 10px;
+    cursor: pointer;
+    color: var(--text-secondary);
+    font-size: 16px;
+    transition: all 0.2s;
+    display: flex;
+    align-items: center;
+}
+
+.theme-toggle:hover {
+    background: var(--bg-hover);
+    color: var(--text-primary);
+}
+
+/* ===== Welcome ===== */
+.welcome-view {
+    flex: 1;
+    display: flex;
+    flex-direction: column;
+    align-items: center;
+    justify-content: center;
+    padding: 0 24px 80px;
+}
+
+.welcome-avatar {
+    width: 120px;
+    height: 120px;
+    position: relative;
+    margin-bottom: 28px;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+}
+
+.welcome-avatar .blob {
+    position: absolute;
+}
+
+.welcome-avatar .blob-outer {
+    width: 110px;
+    height: 110px;
+    background: conic-gradient(from 0deg, #4f6ef7, #818cf8 15%, #a78bfa 30%, #c084fc 50%, #818cf8 70%, #4f6ef7 85%, #4f6ef7);
+    border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%;
+    animation: blob-morph1 8s ease-in-out infinite, blob-spin 6s linear infinite;
+    opacity: 0.82;
+    filter: blur(1.5px) drop-shadow(0 0 14px rgba(79, 110, 247, 0.4));
+}
+
+.welcome-avatar .blob-core {
+    width: 38px;
+    height: 38px;
+    z-index: 1;
+    border-radius: 50% 40% 60% 35% / 40% 55% 35% 60%;
+    background: linear-gradient(135deg, var(--accent), #a78bfa, #c084fc, var(--accent));
+    background-size: 300% 300%;
+    animation: core-flow 4s ease infinite, core-morph 5s ease-in-out infinite, core-pulse 3s ease-in-out infinite;
+    box-shadow: 0 0 20px rgba(79, 110, 247, 0.45), 0 0 44px rgba(167, 139, 250, 0.2);
+    filter: blur(0.5px);
+}
+
+@keyframes blob-morph1 {
+    0%, 100% {
+        border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%;
+    }
+    25% {
+        border-radius: 30% 60% 70% 40% / 50% 60% 30% 60%;
+    }
+    50% {
+        border-radius: 50% 60% 30% 60% / 30% 50% 70% 60%;
+    }
+    75% {
+        border-radius: 40% 60% 50% 40% / 60% 40% 60% 50%;
+    }
+}
+
+@keyframes blob-spin {
+    to {
+        transform: rotate(360deg);
+    }
+}
+
+@keyframes core-flow {
+    0% {
+        background-position: 0% 50%;
+    }
+    50% {
+        background-position: 100% 50%;
+    }
+    100% {
+        background-position: 0% 50%;
+    }
+}
+
+@keyframes core-morph {
+    0%, 100% {
+        border-radius: 50% 40% 60% 35% / 40% 55% 35% 60%;
+    }
+    20% {
+        border-radius: 35% 55% 40% 60% / 55% 35% 60% 40%;
+    }
+    40% {
+        border-radius: 60% 35% 50% 45% / 35% 60% 45% 55%;
+    }
+    60% {
+        border-radius: 40% 60% 35% 55% / 60% 40% 55% 35%;
+    }
+    80% {
+        border-radius: 55% 45% 55% 40% / 45% 50% 40% 60%;
+    }
+}
+
+@keyframes core-pulse {
+    0%, 100% {
+        transform: scale(1);
+        box-shadow: 0 0 20px rgba(79, 110, 247, 0.45), 0 0 44px rgba(167, 139, 250, 0.2);
+    }
+    50% {
+        transform: scale(1.1);
+        box-shadow: 0 0 28px rgba(79, 110, 247, 0.55), 0 0 56px rgba(167, 139, 250, 0.3);
+    }
+}
+
+.welcome-title {
+    font-size: 26px;
+    font-weight: 700;
+    margin-bottom: 10px;
+}
+
+.welcome-sub {
+    color: var(--text-secondary);
+    font-size: 15px;
+    margin-bottom: 24px;
+}
+
+/* Quick Questions */
+.quick-questions {
+    width: 100%;
+    max-width: 680px;
+    margin-bottom: 16px;
+    display: flex;
+    flex-wrap: wrap;
+    gap: 8px;
+}
+
+.quick-q-chip {
+    padding: 6px 14px;
+    border: 1px solid var(--border-color);
+    border-radius: 20px;
+    background: var(--bg-input-box);
+    color: var(--text-secondary);
+    font-size: 13px;
+    cursor: pointer;
+    transition: all 0.2s;
+    white-space: nowrap;
+    max-width: 320px;
+    overflow: hidden;
+    text-overflow: ellipsis;
+}
+
+.quick-q-chip:hover {
+    border-color: var(--accent);
+    color: var(--accent);
+    background: var(--accent-light);
+}
+
+.welcome-input-box {
+    width: 100%;
+    max-width: 680px;
+    background: var(--bg-input-box);
+    border: 1px solid var(--border-input);
+    border-radius: 16px;
+    box-shadow: var(--shadow-lg);
+    display: flex;
+    flex-direction: column;
+    transition: all 0.25s;
+}
+
+.welcome-input-box:focus-within {
+    border-color: var(--accent);
+    box-shadow: var(--shadow-lg), 0 0 0 4px rgba(79, 110, 247, 0.08);
+}
+
+.welcome-input-box textarea {
+    border: none;
+    outline: none;
+    resize: none;
+    background: transparent;
+    padding: 18px 20px 8px;
+    font-family: var(--font-sans);
+    font-size: 15px;
+    color: var(--text-primary);
+    min-height: 54px;
+    max-height: 140px;
+    line-height: 1.6;
+}
+
+.welcome-input-box textarea::placeholder {
+    color: var(--text-secondary);
+}
+
+.welcome-toolbar {
+    display: flex;
+    align-items: center;
+    justify-content: flex-end;
+    padding: 8px 14px;
+}
+
+.welcome-send-btn {
+    width: 40px;
+    height: 40px;
+    border-radius: 50%;
+    border: none;
+    background: var(--accent);
+    color: #fff;
+    cursor: pointer;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    font-size: 18px;
+    transition: all 0.2s;
+}
+
+.welcome-send-btn:hover {
+    background: var(--accent-hover);
+    transform: scale(1.06);
+}
+
+/* ===== Chat View ===== */
+.chat-view {
+    flex: 1;
+    display: none;
+    flex-direction: column;
+    min-height: 0;
+}
+
+.chat-view.active {
+    display: flex;
+}
+
+.messages-wrap {
+    flex: 1;
+    overflow-y: auto;
+}
+
+.messages-inner {
+    max-width: 1024px;
+    margin: 0 auto;
+    padding: 28px 24px 12px;
+}
+
+/* Message */
+.msg-row {
+    display: flex;
+    margin-bottom: 28px;
+    gap: 14px;
+    animation: msg-in 0.25s ease-out;
+}
+
+@keyframes msg-in {
+    from {
+        opacity: 0;
+        transform: translateY(8px);
+    }
+    to {
+        opacity: 1;
+        transform: none;
+    }
+}
+
+.msg-row.user {
+    justify-content: flex-end;
+}
+
+.msg-row.assistant {
+    justify-content: flex-start;
+}
+
+.msg-avatar {
+    width: 38px;
+    height: 38px;
+    border-radius: 12px;
+    flex-shrink: 0;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    font-size: 14px;
+    font-weight: 600;
+}
+
+.msg-row.assistant .msg-avatar {
+    background: linear-gradient(135deg, var(--accent), #a78bfa);
+    color: #fff;
+}
+
+.msg-row.user .msg-avatar {
+    background: var(--bg-hover);
+    color: var(--text-secondary);
+}
+
+.msg-bubble {
+    max-width: 75%;
+    line-height: 1.75;
+    word-break: break-word;
+}
+
+.msg-row.user .msg-bubble {
+    background: var(--bg-user-msg);
+    color: var(--text-user-msg);
+    padding: 12px 18px;
+    border-radius: 20px 20px 4px 20px;
+    font-size: 14px;
+}
+
+.msg-row.assistant .msg-bubble {
+    max-width: 88%;
+    min-width: 88%;
+}
+
+/* Markdown */
+.md-content {
+    padding: 2px 0;
+    color: var(--text-ai-msg);
+    font-size: 14px;
+}
+
+.md-content > :first-child {
+    margin-top: 0;
+}
+
+.md-content > :last-child {
+    margin-bottom: 0;
+}
+
+.md-content p {
+    margin: 7px 0;
+}
+
+.md-content h1, .md-content h2, .md-content h3, .md-content h4 {
+    margin: 16px 0 8px;
+    font-weight: 600;
+    color: var(--text-primary);
+}
+
+.md-content h1 {
+    font-size: 1.35em;
+}
+
+.md-content h2 {
+    font-size: 1.2em;
+}
+
+.md-content h3 {
+    font-size: 1.08em;
+}
+
+.md-content strong {
+    font-weight: 600;
+}
+
+.md-content code {
+    background: var(--bg-code);
+    padding: 2px 7px;
+    border-radius: 5px;
+    font-family: var(--font-mono);
+    font-size: 0.87em;
+}
+
+.md-content pre {
+    background: var(--bg-code-block);
+    padding: 16px 18px;
+    border-radius: 10px;
+    overflow-x: auto;
+    margin: 10px 0;
+}
+
+.md-content pre code {
+    background: none;
+    padding: 0;
+    color: var(--text-code);
+    font-size: 13px;
+}
+
+.md-content ul, .md-content ol {
+    padding-left: 22px;
+    margin: 7px 0;
+}
+
+.md-content li {
+    margin: 4px 0;
+}
+
+.md-content blockquote {
+    border-left: 3px solid var(--accent);
+    padding-left: 14px;
+    color: var(--text-secondary);
+    margin: 10px 0;
+}
+
+.md-content a {
+    color: var(--accent);
+    text-decoration: none;
+}
+
+.md-content a:hover {
+    text-decoration: underline;
+}
+
+.md-content table {
+    border-collapse: collapse;
+    margin: 10px 0;
+    width: 100%;
+    font-size: 13px;
+}
+
+.md-content th, .md-content td {
+    border: 1px solid var(--border-color);
+    padding: 7px 12px;
+    text-align: left;
+}
+
+.md-content th {
+    background: var(--bg-hover);
+    font-weight: 600;
+}
+
+.md-content img {
+    max-width: 100%;
+    border-radius: 8px;
+}
+
+.md-content hr {
+    border: none;
+    border-top: 1px solid var(--border-color);
+    margin: 14px 0;
+}
+
+/* Tool Card */
+.tool-card {
+    margin: 10px 0;
+    border: 1px solid var(--border-color);
+    border-radius: 10px;
+    overflow: hidden;
+    transition: border-color 0.2s;
+}
+
+.tool-card-header {
+    display: flex;
+    align-items: center;
+    gap: 10px;
+    padding: 10px 14px;
+    cursor: pointer;
+    transition: background 0.15s;
+    font-size: 13px;
+}
+
+.tool-card-header:hover {
+    background: var(--bg-hover);
+}
+
+.tool-status-icon {
+    width: 22px;
+    height: 22px;
+    border-radius: 50%;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    flex-shrink: 0;
+    font-size: 12px;
+    transition: all 0.3s;
+}
+
+.tool-status-icon.loading {
+    border: 2px solid var(--border-color);
+    border-top-color: var(--accent);
+    animation: blob-spin 0.8s linear infinite;
+}
+
+.tool-status-icon.done {
+    background: #22c55e;
+    color: #fff;
+    border: none;
+    animation: none;
+}
+
+.tool-name {
+    font-weight: 600;
+    color: var(--text-primary);
+}
+
+.tool-label {
+    color: var(--text-secondary);
+    font-size: 12px;
+}
+
+.tool-toggle {
+    margin-left: auto;
+    color: var(--text-secondary);
+    font-size: 14px;
+    transition: transform 0.2s;
+}
+
+.tool-card.expanded .tool-toggle {
+    transform: rotate(90deg);
+}
+
+.tool-card-body {
+    display: none;
+    padding: 0 14px 12px;
+    font-family: var(--font-mono);
+    font-size: 12px;
+    color: var(--text-secondary);
+    white-space: pre-wrap;
+    word-break: break-all;
+    max-height: 200px;
+    overflow-y: auto;
+    border-top: 1px solid var(--border-color);
+}
+
+.tool-card.expanded .tool-card-body {
+    display: block;
+}
+
+.chunk-error {
+    margin: 8px 0;
+    padding: 10px 14px;
+    background: var(--error-bg);
+    border-left: 3px solid var(--error-border);
+    border-radius: 6px;
+    color: var(--error-text);
+    font-size: 13px;
+}
+
+/* HITL Card */
+.hitl-card {
+    margin: 10px 0;
+    border: 1px solid #f59e0b;
+    border-radius: 10px;
+    overflow: hidden;
+    background: var(--bg-input-box);
+    animation: msg-in 0.25s ease-out;
+}
+
+.hitl-card-header {
+    display: flex;
+    align-items: center;
+    gap: 10px;
+    padding: 12px 14px;
+    font-size: 14px;
+    font-weight: 600;
+    color: #f59e0b;
+    border-bottom: 1px solid var(--border-color);
+}
+
+.hitl-card-header i {
+    font-size: 18px;
+}
+
+.hitl-card-body {
+    padding: 12px 14px;
+    font-size: 13px;
+    color: var(--text-primary);
+}
+
+.hitl-card-body .hitl-tool {
+    margin-bottom: 6px;
+    color: var(--text-secondary);
+}
+
+.hitl-card-body .hitl-tool strong {
+    color: var(--text-primary);
+}
+
+.hitl-card-body .hitl-command {
+    background: var(--bg-code-block);
+    padding: 10px 14px;
+    border-radius: 8px;
+    font-family: var(--font-mono);
+    font-size: 13px;
+    color: var(--text-code);
+    word-break: break-all;
+    margin: 8px 0 12px;
+}
+
+.hitl-card-actions {
+    display: flex;
+    gap: 10px;
+    padding: 0 14px 14px;
+}
+
+.hitl-btn {
+    padding: 8px 20px;
+    border-radius: 8px;
+    border: none;
+    cursor: pointer;
+    font-size: 13px;
+    font-weight: 500;
+    transition: all 0.2s;
+    display: flex;
+    align-items: center;
+    gap: 6px;
+}
+
+.hitl-btn-approve {
+    background: #22c55e;
+    color: #fff;
+}
+
+.hitl-btn-approve:hover {
+    background: #16a34a;
+}
+
+.hitl-btn-reject {
+    background: #ef4444;
+    color: #fff;
+}
+
+.hitl-btn-reject:hover {
+    background: #dc2626;
+}
+
+.hitl-btn:disabled {
+    opacity: 0.5;
+    cursor: not-allowed;
+}
+
+/* Msg Actions */
+.msg-actions {
+    display: flex;
+    gap: 6px;
+    margin-top: 10px;
+    opacity: 0;
+    transition: opacity 0.2s;
+}
+
+.msg-row.assistant:hover .msg-actions {
+    opacity: 1;
+}
+
+.msg-action-btn {
+    background: none;
+    border: 1px solid var(--border-color);
+    border-radius: 8px;
+    padding: 5px 10px;
+    cursor: pointer;
+    color: var(--text-secondary);
+    font-size: 12px;
+    transition: all 0.15s;
+    display: flex;
+    align-items: center;
+    gap: 4px;
+}
+
+.msg-action-btn:hover {
+    background: var(--bg-hover);
+    color: var(--text-primary);
+    border-color: var(--accent);
+}
+
+/* Thinking */
+.inline-thinking {
+    display: flex;
+    align-items: center;
+    gap: 8px;
+    padding: 8px 0;
+    color: var(--text-secondary);
+    font-size: 13px;
+}
+
+.thinking-row {
+    display: flex;
+    gap: 14px;
+    margin-bottom: 28px;
+    animation: msg-in 0.25s ease-out;
+}
+
+.thinking-bubble {
+    display: flex;
+    align-items: center;
+    gap: 10px;
+    color: var(--text-secondary);
+    font-size: 14px;
+    padding: 10px 0;
+}
+
+.thinking-dots {
+    display: flex;
+    gap: 5px;
+}
+
+.thinking-dots span {
+    width: 7px;
+    height: 7px;
+    border-radius: 50%;
+    background: var(--thinking-dot);
+    animation: dot-bounce 1.4s ease-in-out infinite;
+}
+
+.thinking-dots span:nth-child(2) {
+    animation-delay: 0.2s;
+}
+
+.thinking-dots span:nth-child(3) {
+    animation-delay: 0.4s;
+}
+
+@keyframes dot-bounce {
+    0%, 80%, 100% {
+        opacity: 0.25;
+        transform: scale(0.85);
+    }
+    40% {
+        opacity: 1;
+        transform: scale(1.15);
+    }
+}
+
+/* Thinking Block (collapsible reasoning content) */
+.thinking-block {
+    margin: 0 0 12px;
+    border: 1px solid var(--thinking-block-border);
+    border-radius: 10px;
+    overflow: hidden;
+    background: var(--thinking-block-bg);
+}
+
+.thinking-block-header {
+    display: flex;
+    align-items: center;
+    gap: 8px;
+    padding: 10px 14px;
+    cursor: pointer;
+    font-size: 13px;
+    user-select: none;
+    transition: background 0.15s;
+}
+
+.thinking-block-header:hover {
+    background: var(--bg-hover);
+}
+
+.thinking-block-label {
+    color: var(--text-secondary);
+    font-weight: 500;
+}
+
+.thinking-block-toggle {
+    margin-left: auto;
+    color: var(--text-secondary);
+    font-size: 12px;
+    transition: transform 0.25s;
+}
+
+.thinking-block.expanded .thinking-block-toggle {
+    transform: rotate(90deg);
+}
+
+.thinking-block-body {
+    padding: 0 14px 12px;
+    font-size: 13px;
+    color: var(--text-secondary);
+    line-height: 1.7;
+    border-top: 1px solid var(--thinking-block-border);
+    max-height: 300px;
+    overflow-y: auto;
+}
+
+.thinking-block-body .md-content {
+    color: var(--text-secondary);
+    font-size: 13px;
+}
+
+.thinking-block:not(.expanded) .thinking-block-body {
+    display: none;
+}
+
+.thinking-block.streaming {
+    border-color: var(--accent);
+}
+
+.thinking-block.streaming .thinking-block-label {
+    color: var(--accent);
+}
+
+.thinking-block-dots {
+    display: inline-flex;
+    gap: 4px;
+    margin-left: 4px;
+    vertical-align: middle;
+}
+
+.thinking-block-dots span {
+    width: 5px;
+    height: 5px;
+    border-radius: 50%;
+    background: var(--accent);
+    animation: dot-bounce 1.4s ease-in-out infinite;
+}
+
+.thinking-block-dots span:nth-child(2) {
+    animation-delay: 0.2s;
+}
+
+.thinking-block-dots span:nth-child(3) {
+    animation-delay: 0.4s;
+}
+
+/* Bottom Input */
+.input-wrap {
+    padding: 12px 24px 22px;
+    flex-shrink: 0;
+}
+
+.input-box {
+    max-width: 820px;
+    margin: 0 auto;
+    background: var(--bg-input-box);
+    border: 1px solid var(--border-input);
+    border-radius: 14px;
+    box-shadow: var(--shadow);
+    display: flex;
+    flex-direction: column;
+    transition: all 0.25s;
+}
+
+.input-box:focus-within {
+    border-color: var(--accent);
+    box-shadow: var(--shadow), 0 0 0 3px rgba(79, 110, 247, 0.06);
+}
+
+.input-box textarea {
+    border: none;
+    outline: none;
+    resize: none;
+    background: transparent;
+    padding: 14px 16px 6px;
+    font-family: var(--font-sans);
+    font-size: 14px;
+    color: var(--text-primary);
+    min-height: 44px;
+    max-height: 140px;
+    line-height: 1.6;
+}
+
+.input-box textarea::placeholder {
+    color: var(--text-secondary);
+}
+
+.input-toolbar {
+    display: flex;
+    align-items: center;
+    justify-content: flex-end;
+    padding: 6px 10px;
+}
+
+.send-btn {
+    width: 36px;
+    height: 36px;
+    border-radius: 50%;
+    border: none;
+    background: var(--accent);
+    color: #fff;
+    cursor: pointer;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    font-size: 18px;
+    transition: all 0.2s;
+}
+
+.send-btn:hover {
+    background: var(--accent-hover);
+}
+
+.send-btn:disabled {
+    opacity: 0.4;
+    cursor: not-allowed;
+}
+
+.send-btn.stop-mode {
+    background: #ef4444;
+}
+
+.send-btn.stop-mode:hover {
+    background: #dc2626;
+}
+
+.send-btn.stop-mode .stop-icon {
+    width: 14px;
+    height: 14px;
+    background: #fff;
+    border-radius: 3px;
+}
+
+/* Image Preview */
+.img-preview-wrap {
+    padding: 8px 14px 0;
+    display: none;
+}
+
+.img-preview-wrap.has-img {
+    display: flex;
+    align-items: flex-start;
+    gap: 8px;
+}
+
+.img-preview {
+    position: relative;
+    display: inline-block;
+}
+
+.img-preview img {
+    max-height: 80px;
+    max-width: 200px;
+    border-radius: 8px;
+    border: 1px solid var(--border-color);
+    object-fit: cover;
+}
+
+.img-preview-remove {
+    position: absolute;
+    top: -6px;
+    right: -6px;
+    width: 20px;
+    height: 20px;
+    border-radius: 50%;
+    background: #ef4444;
+    color: #fff;
+    border: 2px solid var(--bg-input-box);
+    cursor: pointer;
+    display: flex;
+    align-items: center;
+    justify-content: center;
+    font-size: 12px;
+    line-height: 1;
+}
+
+.img-preview-remove:hover {
+    background: #dc2626;
+}
+
+/* Scrollbar */
+.messages-wrap::-webkit-scrollbar, .sidebar-list::-webkit-scrollbar, .thinking-block-body::-webkit-scrollbar {
+    width: 5px;
+}
+
+.messages-wrap::-webkit-scrollbar-thumb, .sidebar-list::-webkit-scrollbar-thumb, .thinking-block-body::-webkit-scrollbar-thumb {
+    background: var(--border-color);
+    border-radius: 3px;
+}
+
+.messages-wrap::-webkit-scrollbar-track, .sidebar-list::-webkit-scrollbar-track, .thinking-block-body::-webkit-scrollbar-track {
+    background: transparent;
+}
+
+@media (max-width: 768px) {
+    .sidebar {
+        display: none;
+    }
+
+    .model-form-grid {
+        grid-template-columns: 1fr;
+    }
+}

+ 1054 - 0
src/main/resources/static/js/chat.js

@@ -0,0 +1,1054 @@
+/* ===== DOM ===== */
+var welcomeView = document.getElementById('welcomeView');
+var chatView = document.getElementById('chatView');
+var messagesWrap = document.getElementById('messagesWrap');
+var welcomeInput = document.getElementById('welcomeInput');
+var welcomeSendBtn = document.getElementById('welcomeSendBtn');
+var chatInput = document.getElementById('chatInput');
+var chatSendBtn = document.getElementById('chatSendBtn');
+// var themeBtn = document.getElementById('themeBtn');
+var themeIcon = document.getElementById('themeIcon');
+var newChatBtn = document.getElementById('newChatBtn');
+var historyList = document.getElementById('historyList');
+var modelConfigBtn = document.getElementById('modelConfigBtn');
+var modelConfigModal = document.getElementById('modelConfigModal');
+var modelConfigCloseBtn = document.getElementById('modelConfigCloseBtn');
+var modelCancelBtn = document.getElementById('modelCancelBtn');
+var modelSaveBtn = document.getElementById('modelSaveBtn');
+var providerCodeSelect = document.getElementById('providerCode');
+var modelConfigForm = document.getElementById('modelConfigForm');
+var modelIdInput = document.getElementById('modelId');
+var modelNameInput = document.getElementById('modelName');
+var apiHostInput = document.getElementById('apiHost');
+var apiKeyInput = document.getElementById('apiKey');
+var hasUseInput = document.getElementById('hasUse');
+
+/* ===== Constants ===== */
+var DOTS_HTML = '<span class="thinking-dots"><span></span><span></span><span></span></span>';
+var CHAT_MODEL_CATEGORY = 'chat';
+var SINGLE_MODEL_ID = '1';
+
+/* ===== Per-Session State ===== */
+function SessionState(sessionId) {
+    this.sessionId = sessionId;
+    this.container = document.createElement('div');
+    this.container.className = 'messages-inner';
+    this.container.style.display = 'none';
+    messagesWrap.appendChild(this.container);
+    this.eventSource = null;
+    this.isStreaming = false;
+    this.currentBubbleEl = null;
+    this.reasonBuffer = '';
+    this.thinkingBlockEl = null;
+    this.thinkingBodyMdEl = null;
+    this.thinkingBodyWrapEl = null;
+    this.thinkingBuffer = '';
+    this.pendingToolCard = null;
+    this.thinkingEl = null;
+    this.inlineThinkingEl = null;
+    this.silenceTimer = null;
+    this.contentRafId = null;
+    this.reasonRafId = null;
+}
+
+var sessionMap = {};
+var activeSessionId = null;
+
+/* ===== Global State ===== */
+var SESSION_ID = 'web-' + Date.now().toString(36);
+var isStreaming = false;
+var inChatMode = false;
+var chatHistory = [];
+var currentChatIndex = -1;
+var pendingImageBase64 = null;
+var pendingImageMime = null;
+var userScrolledUp = false;
+var providerOptions = [
+    {providerCode: 'ollama', providerName: 'Ollama'},
+    {providerCode: 'qwen', providerName: '千问'},
+    {providerCode: 'DeepSeek', providerName: 'DeepSeek'}
+];
+var modelConfigLoading = false;
+
+/* ===== Session Helpers ===== */
+function getOrCreateSession(sessionId) {
+    if (!sessionMap[sessionId]) {
+        sessionMap[sessionId] = new SessionState(sessionId);
+    }
+    return sessionMap[sessionId];
+}
+
+function setActiveSession(sessionId) {
+    if (activeSessionId && sessionMap[activeSessionId]) {
+        sessionMap[activeSessionId].container.style.display = 'none';
+    }
+    var sess = getOrCreateSession(sessionId);
+    sess.container.style.display = '';
+    activeSessionId = sessionId;
+    SESSION_ID = sessionId;
+    isStreaming = sess.isStreaming;
+    userScrolledUp = false;
+    if (isStreaming) setBtnStopMode();
+    else setBtnSendMode();
+}
+
+function deactivateSession() {
+    if (activeSessionId && sessionMap[activeSessionId]) {
+        sessionMap[activeSessionId].container.style.display = 'none';
+    }
+    activeSessionId = null;
+    isStreaming = false;
+    setBtnSendMode();
+}
+
+function showMessage(message, type) {
+    if (window.layui && layui.layer) {
+        layui.layer.msg(message, {icon: type === 'error' ? 2 : 1, time: 1800});
+        return;
+    }
+    alert(message);
+}
+
+function toggleModelModal(show) {
+    modelConfigModal.classList.toggle('active', !!show);
+    if (show) {
+        document.body.style.overflow = 'hidden';
+    } else {
+        document.body.style.overflow = '';
+    }
+}
+
+function resetModelForm() {
+    modelConfigForm.reset();
+    modelIdInput.value = SINGLE_MODEL_ID;
+    document.getElementById('modelCategory').value = CHAT_MODEL_CATEGORY;
+    hasUseInput.checked = true;
+    if (providerCodeSelect.options.length > 0) {
+        providerCodeSelect.selectedIndex = 0;
+    }
+}
+
+function fillModelForm(model) {
+    modelIdInput.value = model && model.id ? model.id : '';
+    document.getElementById('modelCategory').value = model && model.category ? model.category : CHAT_MODEL_CATEGORY;
+    modelNameInput.value = model && model.modelName ? model.modelName : '';
+    apiHostInput.value = model && model.apiHost ? model.apiHost : '';
+    apiKeyInput.value = model && model.apiKey ? model.apiKey : '';
+    hasUseInput.checked = !model || model.hasUse == null ? true : Number(model.hasUse) === 1;
+    providerCodeSelect.value = model && model.providerCode ? model.providerCode : '';
+}
+
+function renderProviderSelect() {
+    var html = '<option value="">请选择服务商</option>';
+    for (var i = 0; i < providerOptions.length; i++) {
+        var item = providerOptions[i];
+        html += '<option value="' + escapeHtml(item.providerCode || '') + '">' + escapeHtml(item.providerName || item.providerCode || '') + '</option>';
+    }
+    providerCodeSelect.innerHTML = html;
+}
+
+function httpGetJson(url, callback, onError) {
+    var xhr = new XMLHttpRequest();
+    xhr.open('GET', url, true);
+    xhr.onreadystatechange = function () {
+        if (xhr.readyState !== 4) return;
+        if (xhr.status >= 200 && xhr.status < 300) {
+            try {
+                callback(JSON.parse(xhr.responseText));
+            } catch (e) {
+                console.log(e)
+                if (onError) onError(e);
+            }
+            return;
+        }
+        if (onError) onError(xhr);
+    };
+    xhr.send();
+}
+
+function unwrapResponse(payload) {
+    if (!payload || typeof payload !== 'object') {
+        return payload;
+    }
+    if (Object.prototype.hasOwnProperty.call(payload, 'data')) {
+        return payload.data;
+    }
+    return payload;
+}
+
+function loadModelConfigData(done) {
+    if (modelConfigLoading) return;
+    modelConfigLoading = true;
+    var pending = 1;
+    var failed = false;
+
+    function finish() {
+        pending--;
+        if (pending > 0) return;
+        modelConfigLoading = false;
+        if (!failed && done) done();
+    }
+
+    function handleError() {
+        failed = true;
+        modelConfigLoading = false;
+        showMessage('加载模型配置失败', 'error');
+    }
+
+    renderProviderSelect();
+
+    httpGetJson('/js/a/ai/model/getInfo?id=' + encodeURIComponent(SINGLE_MODEL_ID), function (data) {
+        resetModelForm();
+        fillModelForm(unwrapResponse(data) || {});
+        finish();
+    }, handleError);
+}
+
+function openModelConfigModal() {
+    loadModelConfigData(function () {
+        toggleModelModal(true);
+    });
+}
+
+function saveModelConfig() {
+    var providerCode = providerCodeSelect.value.trim();
+    var modelName = modelNameInput.value.trim();
+    var apiHost = apiHostInput.value.trim();
+    var apiKey = apiKeyInput.value.trim();
+    if (!providerCode || !modelName || !apiHost || !apiKey) {
+        showMessage('请完整填写服务商、模型名称、API Host 和 API Key', 'error');
+        return;
+    }
+
+    modelSaveBtn.disabled = true;
+    var params = new URLSearchParams();
+    params.append('id', modelIdInput.value || SINGLE_MODEL_ID);
+    params.append('category', CHAT_MODEL_CATEGORY);
+    params.append('providerCode', providerCode);
+    params.append('modelName', modelName);
+    params.append('apiHost', apiHost);
+    params.append('apiKey', apiKey);
+    params.append('hasUse', hasUseInput.checked ? '1' : '0');
+
+    fetch('/js/a/ai/create', {
+        method: 'POST',
+        headers: {'Content-Type': 'application/json; charset=UTF-8'},
+        body: JSON.stringify(Object.fromEntries(params))
+    }).then(function (resp) {
+        if (!resp.ok) {
+            throw new Error('save failed');
+        }
+        return resp.text();
+    }).then(function () {
+        showMessage('模型配置已保存');
+        toggleModelModal(false);
+    }).catch(function () {
+        showMessage('保存模型配置失败', 'error');
+    }).finally(function () {
+        modelSaveBtn.disabled = false;
+    });
+}
+
+/* ===== Image Paste ===== */
+var welcomeImgWrap = document.getElementById('welcomeImgWrap');
+var welcomeImgPreview = document.getElementById('welcomeImgPreview');
+var welcomeImgRemove = document.getElementById('welcomeImgRemove');
+var chatImgWrap = document.getElementById('chatImgWrap');
+var chatImgPreview = document.getElementById('chatImgPreview');
+var chatImgRemove = document.getElementById('chatImgRemove');
+
+function handlePasteImage(e) {
+    var items = (e.clipboardData || e.originalEvent.clipboardData).items;
+    for (var i = 0; i < items.length; i++) {
+        if (items[i].type.indexOf('image') !== -1) {
+            e.preventDefault();
+            var file = items[i].getAsFile();
+            var reader = new FileReader();
+            reader.onload = function(evt) {
+                var dataUrl = evt.target.result;
+                var parts = dataUrl.split(',');
+                var meta = parts[0];
+                pendingImageBase64 = parts[1];
+                pendingImageMime = meta.match(/data:(.*?);/)[1];
+                showImagePreview(dataUrl);
+            };
+            reader.readAsDataURL(file);
+            return;
+        }
+    }
+}
+
+function showImagePreview(dataUrl) {
+    if (inChatMode) {
+        chatImgPreview.src = dataUrl;
+        chatImgWrap.classList.add('has-img');
+    } else {
+        welcomeImgPreview.src = dataUrl;
+        welcomeImgWrap.classList.add('has-img');
+    }
+}
+
+function clearImagePreview() {
+    pendingImageBase64 = null;
+    pendingImageMime = null;
+    welcomeImgPreview.src = '';
+    welcomeImgWrap.classList.remove('has-img');
+    chatImgPreview.src = '';
+    chatImgWrap.classList.remove('has-img');
+}
+
+modelConfigBtn.addEventListener('click', openModelConfigModal);
+modelConfigCloseBtn.addEventListener('click', function () {
+    toggleModelModal(false);
+});
+modelCancelBtn.addEventListener('click', function () {
+    toggleModelModal(false);
+});
+modelSaveBtn.addEventListener('click', saveModelConfig);
+// modelConfigModal.addEventListener('click', function (e) {
+//     if (e.target === modelConfigModal) {
+//         toggleModelModal(false);
+//     }
+// });
+document.addEventListener('keydown', function (e) {
+    if (e.key === 'Escape' && modelConfigModal.classList.contains('active')) {
+        toggleModelModal(false);
+    }
+});
+
+welcomeInput.addEventListener('paste', handlePasteImage);
+chatInput.addEventListener('paste', handlePasteImage);
+welcomeImgRemove.addEventListener('click', clearImagePreview);
+chatImgRemove.addEventListener('click', clearImagePreview);
+
+/* ===== Quick Questions ===== */
+var welcomeQuickQs = document.getElementById('welcomeQuickQs');
+var chatQuickQs = document.getElementById('chatQuickQs');
+
+function loadQuickQuestions() {
+    httpGetJson('/js/a/ai/qs', function (data) {
+        var questions = unwrapResponse(data);
+        if (!Array.isArray(questions) || questions.length === 0) return;
+        renderQuickQChips(welcomeQuickQs, questions);
+        renderQuickQChips(chatQuickQs, questions);
+    });
+}
+
+function renderQuickQChips(container, questions) {
+    container.innerHTML = '';
+    for (var i = 0; i < questions.length; i++) {
+        var chip = document.createElement('span');
+        chip.className = 'quick-q-chip';
+        chip.textContent = questions[i];
+        chip.title = questions[i];
+        chip.addEventListener('click', (function(q) {
+            return function() {
+                if (inChatMode) {
+                    chatInput.value = q;
+                    chatInput.focus();
+                    autoResize(chatInput);
+                } else {
+                    welcomeInput.value = q;
+                    welcomeInput.focus();
+                    autoResize(welcomeInput);
+                }
+            };
+        })(questions[i]));
+        container.appendChild(chip);
+    }
+}
+
+loadQuickQuestions();
+
+/* ===== Marked ===== */
+if (typeof marked !== 'undefined') { marked.setOptions({ breaks: true, gfm: true }); }
+function renderMd(text) {
+    if (typeof marked !== 'undefined') return marked.parse(text);
+    return text.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br>');
+}
+
+/* ===== Theme ===== */
+var currentTheme = localStorage.getItem('chat-theme') || 'light';
+document.body.setAttribute('data-theme', currentTheme);
+/*    updateThemeIcon();
+    themeBtn.addEventListener('click', function() {
+        currentTheme = currentTheme === 'light' ? 'dark' : 'light';
+        document.body.setAttribute('data-theme', currentTheme);
+        localStorage.setItem('chat-theme', currentTheme);
+        updateThemeIcon();
+    });
+    function updateThemeIcon() {
+        themeIcon.innerHTML = currentTheme === 'light' ? '&#xe6c2;' : '&#xe748;';
+        themeBtn.title = currentTheme === 'light' ? '切换至暗色' : '切换至浅色';
+    }*/
+
+/* ===== View Switch ===== */
+function switchToChatMode() {
+    if (inChatMode) return;
+    inChatMode = true;
+    welcomeView.style.display = 'none';
+    chatView.classList.add('active');
+    chatInput.focus();
+}
+function switchToWelcomeMode() {
+    inChatMode = false;
+    deactivateSession();
+    welcomeView.style.display = '';
+    chatView.classList.remove('active');
+    welcomeInput.focus();
+}
+
+/* ===== Auto-resize ===== */
+function autoResize(el) {
+    el.style.height = 'auto';
+    el.style.height = Math.min(el.scrollHeight, 140) + 'px';
+}
+welcomeInput.addEventListener('input', function() { autoResize(this); });
+chatInput.addEventListener('input', function() { autoResize(this); });
+
+/* ===== Send from both inputs ===== */
+function getInputText() {
+    if (inChatMode) return chatInput.value.trim();
+    return welcomeInput.value.trim();
+}
+function clearInput() {
+    if (inChatMode) { chatInput.value = ''; chatInput.style.height = 'auto'; }
+    else { welcomeInput.value = ''; welcomeInput.style.height = 'auto'; }
+}
+
+welcomeInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } });
+chatInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } });
+welcomeSendBtn.addEventListener('click', function() { sendMessage(); });
+chatSendBtn.addEventListener('click', function() {
+    if (isStreaming && activeSessionId && sessionMap[activeSessionId]) {
+        finishStream(sessionMap[activeSessionId]);
+    } else {
+        sendMessage();
+    }
+});
+
+/* ===== Click to focus ===== */
+document.querySelector('.welcome-input-box').addEventListener('click', function(e) {
+    if (!e.target.closest('button')) welcomeInput.focus();
+});
+document.querySelector('.input-box').addEventListener('click', function(e) {
+    if (!e.target.closest('button')) chatInput.focus();
+});
+
+/* ===== New Chat ===== */
+newChatBtn.addEventListener('click', function() {
+    SESSION_ID = 'web-' + Date.now().toString(36);
+    currentChatIndex = -1;
+    switchToWelcomeMode();
+    updateHistoryUI();
+});
+
+/* ===== Helpers ===== */
+messagesWrap.addEventListener('scroll', function() {
+    var gap = messagesWrap.scrollHeight - messagesWrap.scrollTop - messagesWrap.clientHeight;
+    userScrolledUp = gap > 80;
+});
+var scrollRafPending = false;
+function scrollToBottom(force) {
+    if (!force && userScrolledUp) return;
+    if (force) userScrolledUp = false;
+    if (scrollRafPending) return;
+    scrollRafPending = true;
+    requestAnimationFrame(function() {
+        scrollRafPending = false;
+        messagesWrap.scrollTop = messagesWrap.scrollHeight;
+    });
+}
+
+function resetStreamState(sess) {
+    sess.currentBubbleEl = null;
+    sess.reasonBuffer = '';
+    sess.thinkingBlockEl = null;
+    sess.thinkingBodyMdEl = null;
+    sess.thinkingBodyWrapEl = null;
+    sess.thinkingBuffer = '';
+    if (sess.contentRafId) { cancelAnimationFrame(sess.contentRafId); sess.contentRafId = null; }
+    if (sess.reasonRafId) { cancelAnimationFrame(sess.reasonRafId); sess.reasonRafId = null; }
+}
+
+function setBtnStopMode() {
+    chatSendBtn.disabled = false;
+    chatSendBtn.classList.add('stop-mode');
+    chatSendBtn.innerHTML = '<div class="stop-icon"></div>';
+    chatSendBtn.title = '停止生成';
+}
+function setBtnSendMode() {
+    chatSendBtn.classList.remove('stop-mode');
+    chatSendBtn.innerHTML = '<i class="layui-icon layui-icon-release"></i>';
+    chatSendBtn.title = '发送';
+    chatSendBtn.disabled = false;
+}
+
+/* ===== History ===== */
+function loadSessionHistory() {
+    var xhr = new XMLHttpRequest();
+    xhr.open('GET', '/js/a/chat/sessions', true);
+    xhr.onreadystatechange = function() {
+        if (xhr.readyState === 4 && xhr.status === 200) {
+            try {
+                var result = JSON.parse(xhr.responseText);
+                var list = result.data;
+                chatHistory = [];
+                for (var i = 0; i < list.length; i++) {
+                    chatHistory.push({ label: list[i].label, sessionId: list[i].sessionId });
+                }
+                updateHistoryUI();
+            } catch (e) {}
+        }
+    };
+    xhr.send();
+}
+
+function saveChatToHistory(firstMsg) {
+    var label = firstMsg.length > 30 ? firstMsg.substring(0, 30) + '...' : firstMsg;
+    for (var i = 0; i < chatHistory.length; i++) {
+        if (chatHistory[i].sessionId === SESSION_ID) {
+            currentChatIndex = i;
+            updateHistoryUI();
+            return;
+        }
+    }
+    chatHistory.unshift({ label: label, sessionId: SESSION_ID });
+    if (chatHistory.length > 50) chatHistory.pop();
+    currentChatIndex = 0;
+    updateHistoryUI();
+}
+
+/* Sidebar event delegation — single listener instead of per-item binding */
+historyList.addEventListener('click', function(e) {
+    var delBtn = e.target.closest('.sidebar-item-del');
+    if (delBtn) {
+        e.stopPropagation();
+        var idx = parseInt(delBtn.closest('.sidebar-item').getAttribute('data-idx'));
+        if (!isNaN(idx)) deleteSession(idx);
+        return;
+    }
+    var item = e.target.closest('.sidebar-item');
+    if (item) {
+        var idx = parseInt(item.getAttribute('data-idx'));
+        if (!isNaN(idx)) selectSession(idx);
+    }
+});
+
+function updateHistoryUI() {
+    var frag = document.createDocumentFragment();
+    for (var i = 0; i < chatHistory.length; i++) {
+        var item = document.createElement('div');
+        var sess = sessionMap[chatHistory[i].sessionId];
+        var streaming = sess && sess.isStreaming;
+        item.className = 'sidebar-item' + (i === currentChatIndex ? ' active' : '') + (streaming ? ' streaming' : '');
+        item.setAttribute('data-idx', i);
+
+        var label = document.createElement('span');
+        label.className = 'sidebar-item-label';
+        label.textContent = chatHistory[i].label;
+
+        var delBtn = document.createElement('button');
+        delBtn.className = 'sidebar-item-del';
+        delBtn.title = '删除对话';
+        delBtn.innerHTML = '<i class="layui-icon layui-icon-close"></i>';
+
+        item.appendChild(label);
+        if (streaming) {
+            var spinner = document.createElement('span');
+            spinner.className = 'sidebar-item-spinner';
+            spinner.title = '对话进行中...';
+            item.appendChild(spinner);
+        }
+        item.appendChild(delBtn);
+        frag.appendChild(item);
+    }
+    historyList.innerHTML = '';
+    historyList.appendChild(frag);
+}
+
+function deleteSession(idx) {
+    var entry = chatHistory[idx];
+    if (!entry) return;
+
+    var xhr = new XMLHttpRequest();
+    xhr.open('POST', '/js/a/chat/sessions/delete?sessionId=' + encodeURIComponent(entry.sessionId), true);
+    xhr.onreadystatechange = function() {
+        if (xhr.readyState === 4) {
+            /* Clean up session state after server confirms */
+            var sess = sessionMap[entry.sessionId];
+            if (sess) {
+                if (sess.eventSource) sess.eventSource.close();
+                if (sess.silenceTimer) clearTimeout(sess.silenceTimer);
+                if (sess.contentRafId) cancelAnimationFrame(sess.contentRafId);
+                if (sess.reasonRafId) cancelAnimationFrame(sess.reasonRafId);
+                if (sess.container.parentNode) sess.container.parentNode.removeChild(sess.container);
+                delete sessionMap[entry.sessionId];
+            }
+
+            chatHistory.splice(idx, 1);
+
+            if (idx === currentChatIndex) {
+                currentChatIndex = -1;
+                SESSION_ID = 'web-' + Date.now().toString(36);
+                switchToWelcomeMode();
+            } else if (idx < currentChatIndex) {
+                currentChatIndex--;
+            }
+
+            updateHistoryUI();
+        }
+    };
+    xhr.send();
+}
+
+function selectSession(idx) {
+    if (idx === currentChatIndex && inChatMode) return;
+    var entry = chatHistory[idx];
+    if (!entry) return;
+
+    currentChatIndex = idx;
+    SESSION_ID = entry.sessionId;
+    if (!inChatMode) switchToChatMode();
+    setActiveSession(entry.sessionId);
+    updateHistoryUI();
+
+    var sess = sessionMap[entry.sessionId];
+    /* Only load from server if not streaming and container has no content */
+    if (!sess.isStreaming && sess.container.children.length === 0) {
+        loadMessages(sess);
+    } else {
+        scrollToBottom(true);
+    }
+}
+
+function loadMessages(sess) {
+    var xhr = new XMLHttpRequest();
+    xhr.open('GET', '/js/a/chat/messages?sessionId=' + encodeURIComponent(sess.sessionId), true);
+    xhr.onreadystatechange = function() {
+        if (xhr.readyState === 4 && xhr.status === 200) {
+            try {
+                var result = JSON.parse(xhr.responseText);
+                var msgs = result.data;
+                sess.container.innerHTML = '';
+                resetStreamState(sess);
+                for (var i = 0; i < msgs.length; i++) {
+                    var m = msgs[i];
+                    if (m.role === 'USER') {
+                        resetStreamState(sess);
+                        appendUserMessage(sess, m.content);
+                    } else if (m.role === 'ASSISTANT') {
+                        var isConsecutive = (i > 0 && msgs[i - 1].role === 'ASSISTANT');
+                        if (!isConsecutive) resetStreamState(sess);
+                        var el = ensureAssistantBubble(sess);
+                        sess.reasonBuffer = isConsecutive ? sess.reasonBuffer + '\n\n' + m.content : m.content;
+                        el.innerHTML = renderMd(sess.reasonBuffer);
+                    }
+                }
+                resetStreamState(sess);
+                if (sess.sessionId === activeSessionId) scrollToBottom(true);
+            } catch (e) {}
+        }
+    };
+    xhr.send();
+}
+
+/* Load on startup */
+loadSessionHistory();
+
+/* ===== Message Rendering (Session-Aware) ===== */
+function appendUserMessage(sess, text, imageDataUrl) {
+    var row = document.createElement('div');
+    row.className = 'msg-row user';
+    row.innerHTML = '<div class="msg-bubble"></div><div class="msg-avatar">我</div>';
+    var bubble = row.querySelector('.msg-bubble');
+    if (imageDataUrl) {
+        var img = document.createElement('img');
+        img.src = imageDataUrl;
+        img.style.cssText = 'max-height:120px;max-width:240px;border-radius:8px;display:block;margin-bottom:8px;';
+        bubble.appendChild(img);
+    }
+    var span = document.createElement('span');
+    span.textContent = text;
+    bubble.appendChild(span);
+    sess.container.appendChild(row);
+    if (sess.sessionId === activeSessionId) scrollToBottom(true);
+}
+
+function ensureAssistantBubble(sess) {
+    if (!sess.currentBubbleEl) {
+        removeThinking(sess);
+        var row = document.createElement('div');
+        row.className = 'msg-row assistant';
+        row.innerHTML = '<div class="msg-avatar"><i class="layui-icon layui-icon-bot" style="font-size:18px"></i></div>'
+            + '<div class="msg-bubble"><div class="md-content"></div>'
+            + '<div class="msg-actions">'
+            + '<button class="msg-action-btn copy-btn" title="复制"><i class="layui-icon layui-icon-file"></i> 复制</button>'
+            + '</div></div>';
+        sess.container.appendChild(row);
+        sess.currentBubbleEl = row.querySelector('.md-content');
+        var copyBtn = row.querySelector('.copy-btn');
+        var mdRef = sess.currentBubbleEl;
+        copyBtn.addEventListener('click', function() {
+            var txt = mdRef.innerText || '';
+            if (navigator.clipboard) { navigator.clipboard.writeText(txt); }
+        });
+    }
+    return sess.currentBubbleEl;
+}
+
+function ensureThinkingBlock(sess) {
+    if (!sess.thinkingBlockEl) {
+        ensureAssistantBubble(sess);
+        var parent = sess.currentBubbleEl.parentNode;
+        var block = document.createElement('div');
+        block.className = 'thinking-block streaming expanded';
+        block.innerHTML = '<div class="thinking-block-header">'
+            + '<span class="thinking-block-label">思考中...</span>'
+            + '<span class="thinking-block-dots"><span></span><span></span><span></span></span>'
+            + '<i class="layui-icon layui-icon-right thinking-block-toggle"></i>'
+            + '</div>'
+            + '<div class="thinking-block-body"><div class="md-content"></div></div>';
+        parent.insertBefore(block, sess.currentBubbleEl);
+        block.querySelector('.thinking-block-header').addEventListener('click', function() {
+            block.classList.toggle('expanded');
+        });
+        sess.thinkingBlockEl = block;
+        sess.thinkingBodyMdEl = block.querySelector('.thinking-block-body .md-content');
+        sess.thinkingBodyWrapEl = block.querySelector('.thinking-block-body');
+        sess.thinkingBuffer = '';
+    }
+    return sess.thinkingBlockEl;
+}
+
+function insertBeforeActions(sess, el) {
+    var parent = sess.currentBubbleEl.parentNode;
+    parent.insertBefore(el, parent.querySelector('.msg-actions'));
+}
+
+function finishThinkingBlock(sess) {
+    if (sess.thinkingBlockEl) {
+        if (sess.reasonRafId) {
+            cancelAnimationFrame(sess.reasonRafId);
+            sess.reasonRafId = null;
+            if (sess.thinkingBodyMdEl) {
+                sess.thinkingBodyMdEl.innerHTML = renderMd(sess.thinkingBuffer);
+            }
+        }
+        sess.thinkingBlockEl.classList.remove('streaming', 'expanded');
+        var label = sess.thinkingBlockEl.querySelector('.thinking-block-label');
+        if (label) label.textContent = '思考结束';
+        var dots = sess.thinkingBlockEl.querySelector('.thinking-block-dots');
+        if (dots) dots.remove();
+        sess.thinkingBlockEl = null;
+        sess.thinkingBodyMdEl = null;
+        sess.thinkingBodyWrapEl = null;
+        sess.thinkingBuffer = '';
+    }
+}
+
+function clearThinkTags(text) {
+    return text.replace(/<\s*\/?think\s*>/gi, '');
+}
+
+function appendReasonChunk(sess, text) {
+    removeThinking(sess);
+    ensureThinkingBlock(sess);
+    sess.thinkingBuffer += clearThinkTags(text);
+    if (!sess.reasonRafId) {
+        sess.reasonRafId = requestAnimationFrame(function() {
+            sess.reasonRafId = null;
+            if (!sess.thinkingBlockEl) return;
+            if (sess.thinkingBodyMdEl) {
+                sess.thinkingBodyMdEl.innerHTML = renderMd(sess.thinkingBuffer);
+            }
+            if (sess.thinkingBodyWrapEl) {
+                sess.thinkingBodyWrapEl.scrollTop = sess.thinkingBodyWrapEl.scrollHeight;
+            }
+            if (sess.sessionId === activeSessionId) scrollToBottom();
+        });
+    }
+}
+
+function finishPendingTool(sess) {
+    if (sess.pendingToolCard) {
+        var icon = sess.pendingToolCard.querySelector('.tool-status-icon');
+        if (icon) { icon.className = 'tool-status-icon done'; icon.innerHTML = '<i class="layui-icon layui-icon-ok" style="font-size:12px"></i>'; }
+        var label = sess.pendingToolCard.querySelector('.tool-label');
+        if (label) label.textContent = '已完成';
+        sess.pendingToolCard = null;
+    }
+}
+
+function appendActionEndChunk(sess, toolName, text) {
+    finishPendingTool(sess);
+    ensureAssistantBubble(sess);
+
+    var card = document.createElement('div');
+    card.className = 'tool-card';
+    card.innerHTML = '<div class="tool-card-header">'
+        + '<span class="tool-status-icon loading"></span>'
+        + '<span class="tool-name">' + escapeHtml(toolName || 'tool') + '</span>'
+        + '<span class="tool-label">执行中...</span>'
+        + '<i class="layui-icon layui-icon-right tool-toggle"></i>'
+        + '</div>'
+        + '<div class="tool-card-body"></div>';
+
+    card.querySelector('.tool-card-body').textContent = text || '';
+    card.querySelector('.tool-card-header').addEventListener('click', function() {
+        card.classList.toggle('expanded');
+    });
+
+    insertBeforeActions(sess, card);
+    sess.pendingToolCard = card;
+
+    sess.reasonBuffer = '';
+    var newMd = document.createElement('div');
+    newMd.className = 'md-content';
+    insertBeforeActions(sess, newMd);
+    sess.currentBubbleEl = newMd;
+    if (sess.sessionId === activeSessionId) scrollToBottom();
+}
+
+function appendContentChunk(sess, text, append) {
+    var clean = clearThinkTags(text);
+    sess.reasonBuffer = append ? sess.reasonBuffer + clean : clean;
+    if (!sess.contentRafId) {
+        sess.contentRafId = requestAnimationFrame(function() {
+            sess.contentRafId = null;
+            var el = ensureAssistantBubble(sess);
+            el.innerHTML = renderMd(sess.reasonBuffer);
+            if (sess.sessionId === activeSessionId) scrollToBottom();
+        });
+    }
+}
+
+function appendErrorChunk(sess, text) {
+    ensureAssistantBubble(sess);
+    var errEl = document.createElement('div');
+    errEl.className = 'chunk-error';
+    errEl.textContent = text;
+    insertBeforeActions(sess, errEl);
+    if (sess.sessionId === activeSessionId) scrollToBottom();
+}
+
+/* ===== Thinking Indicators ===== */
+function showThinking(sess) {
+    removeThinking(sess);
+    sess.thinkingEl = document.createElement('div');
+    sess.thinkingEl.className = 'thinking-row';
+    sess.thinkingEl.innerHTML = '<div class="msg-avatar" style="background:linear-gradient(135deg,var(--accent),#a78bfa);color:#fff">'
+        + '<i class="layui-icon layui-icon-bot" style="font-size:18px"></i></div>'
+        + '<div class="thinking-bubble">思考中' + DOTS_HTML + '</div>';
+    sess.container.appendChild(sess.thinkingEl);
+    if (sess.sessionId === activeSessionId) scrollToBottom(true);
+}
+function removeThinking(sess) {
+    if (sess.thinkingEl && sess.thinkingEl.parentNode) { sess.thinkingEl.parentNode.removeChild(sess.thinkingEl); sess.thinkingEl = null; }
+}
+
+function showInlineThinking(sess) {
+    if (sess.inlineThinkingEl || !sess.currentBubbleEl) return;
+    sess.inlineThinkingEl = document.createElement('div');
+    sess.inlineThinkingEl.className = 'inline-thinking';
+    sess.inlineThinkingEl.innerHTML = '思考中 ' + DOTS_HTML;
+    insertBeforeActions(sess, sess.inlineThinkingEl);
+    if (sess.sessionId === activeSessionId) scrollToBottom();
+}
+function removeInlineThinking(sess) {
+    if (sess.inlineThinkingEl && sess.inlineThinkingEl.parentNode) { sess.inlineThinkingEl.parentNode.removeChild(sess.inlineThinkingEl); }
+    sess.inlineThinkingEl = null;
+}
+
+/* ===== HITL ===== */
+function appendHitlCard(sess, toolName, command) {
+    ensureAssistantBubble(sess);
+
+    var card = document.createElement('div');
+    card.className = 'hitl-card';
+    card.innerHTML = '<div class="hitl-card-header">'
+        + '<i class="layui-icon layui-icon-tips"></i> \u9700\u8981\u6388\u6743'
+        + '</div>'
+        + '<div class="hitl-card-body">'
+        + '<div class="hitl-tool">\u5de5\u5177: <strong>' + escapeHtml(toolName || 'unknown') + '</strong></div>'
+        + (command ? '<div class="hitl-command">' + escapeHtml(command) + '</div>' : '')
+        + '</div>'
+        + '<div class="hitl-card-actions">'
+        + '<button class="hitl-btn hitl-btn-approve">\u6279\u51c6</button>'
+        + '<button class="hitl-btn hitl-btn-reject">\u62d2\u7edd</button>'
+        + '</div>';
+
+    insertBeforeActions(sess, card);
+
+    var approveBtn = card.querySelector('.hitl-btn-approve');
+    var rejectBtn = card.querySelector('.hitl-btn-reject');
+
+    approveBtn.addEventListener('click', function() {
+        approveBtn.disabled = true;
+        rejectBtn.disabled = true;
+        approveBtn.textContent = '\u5df2\u6279\u51c6';
+        rejectBtn.style.display = 'none';
+        card.style.borderColor = '#22c55e';
+        handleHitlResponse(sess, 'approve');
+    });
+
+    rejectBtn.addEventListener('click', function() {
+        approveBtn.disabled = true;
+        rejectBtn.disabled = true;
+        rejectBtn.textContent = '\u5df2\u62d2\u7edd';
+        approveBtn.style.display = 'none';
+        card.style.borderColor = '#ef4444';
+        handleHitlResponse(sess, 'reject');
+    });
+
+    if (sess.sessionId === activeSessionId) scrollToBottom();
+}
+
+function escapeHtml(str) {
+    var div = document.createElement('div');
+    div.textContent = str;
+    return div.innerHTML;
+}
+
+function handleHitlResponse(sess, action) {
+    if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
+    resetStreamState(sess);
+
+    sess.isStreaming = true;
+    if (sess.sessionId === activeSessionId) {
+        isStreaming = true;
+        setBtnStopMode();
+    }
+
+    var url = SSE_ENDPOINT + '?hitlAction=' + encodeURIComponent(action)
+        + '&sessionId=' + encodeURIComponent(sess.sessionId);
+
+    sess.eventSource = new EventSource(url);
+    sess.eventSource.onmessage = function(event) { handleSSEData(sess, event.data); };
+    sess.eventSource.onerror = function() { finishStream(sess); };
+}
+
+/* ===== Send ===== */
+function sendMessage() {
+    var text = getInputText();
+    if (!text && !pendingImageBase64) return;
+    /* Block only if the active session is currently streaming */
+    if (activeSessionId && sessionMap[activeSessionId] && sessionMap[activeSessionId].isStreaming) return;
+    if (!text) text = '请描述这张图片';
+
+    var imageDataUrl = pendingImageBase64 ? ('data:' + pendingImageMime + ';base64,' + pendingImageBase64) : null;
+    var imageBase64 = pendingImageBase64;
+    var imageMime = pendingImageMime;
+
+    if (currentChatIndex === -1) {
+        SESSION_ID = 'web-' + Date.now().toString(36);
+        saveChatToHistory(text);
+    }
+
+    clearInput();
+    clearImagePreview();
+    if (!inChatMode) switchToChatMode();
+    setActiveSession(SESSION_ID);
+
+    var sess = sessionMap[SESSION_ID];
+
+    appendUserMessage(sess, text, imageDataUrl);
+
+    sess.isStreaming = true;
+    isStreaming = true;
+    setBtnStopMode();
+    resetStreamState(sess);
+    showThinking(sess);
+
+    if (imageBase64) {
+        sendWithFetch(sess, text, imageBase64, imageMime);
+    } else {
+        sendWithEventSource(sess, text);
+    }
+}
+
+function sendWithEventSource(sess, text) {
+    if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
+    var url = SSE_ENDPOINT + '?input=' + encodeURIComponent(text)
+        + '&sessionId=' + encodeURIComponent(sess.sessionId);
+
+    sess.eventSource = new EventSource(url);
+    sess.eventSource.onmessage = function(event) { handleSSEData(sess, event.data); };
+    sess.eventSource.onerror = function() { finishStream(sess); };
+}
+
+function sendWithFetch(sess, text, imageBase64, imageMime) {
+    if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
+    var body = JSON.stringify({ input: text, sessionId: sess.sessionId, imageBase64: imageBase64, imageMime: imageMime });
+    var abortCtrl = new AbortController();
+    sess.eventSource = { close: function() { abortCtrl.abort(); } };
+
+    fetch(SSE_ENDPOINT, {
+        method: 'POST',
+        headers: { 'Content-Type': 'application/json' },
+        signal: abortCtrl.signal,
+        body: body
+    }).then(function(resp) {
+        var reader = resp.body.getReader();
+        var decoder = new TextDecoder();
+        var buffer = '';
+
+        function pump() {
+            reader.read().then(function(result) {
+                if (result.done) { finishStream(sess); return; }
+                buffer += decoder.decode(result.value, { stream: true });
+                var lines = buffer.split('\n');
+                buffer = lines.pop();
+                for (var i = 0; i < lines.length; i++) {
+                    var line = lines[i].trim();
+                    if (line.startsWith('data:')) {
+                        var raw = line.substring(5).trim();
+                        handleSSEData(sess, raw);
+                    }
+                }
+                pump();
+            }).catch(function() { finishStream(sess); });
+        }
+        pump();
+    }).catch(function() { finishStream(sess); });
+}
+
+/* ===== SSE Data Handling (Session-Aware) ===== */
+function handleSSEData(sess, raw) {
+    if (raw === '[DONE]') { finishStream(sess); return; }
+    try {
+        var chunk = JSON.parse(raw);
+        if (sess.silenceTimer) clearTimeout(sess.silenceTimer);
+        removeInlineThinking(sess);
+        switch (chunk.type) {
+            case 'reason': finishPendingTool(sess); appendReasonChunk(sess, chunk.text); break;
+            case 'text':   finishThinkingBlock(sess); finishPendingTool(sess); appendContentChunk(sess, chunk.text, true); break;
+            case 'action': finishThinkingBlock(sess); appendActionEndChunk(sess, chunk.toolName, chunk.text); break;
+            case 'agent':  finishThinkingBlock(sess); finishPendingTool(sess); appendContentChunk(sess, chunk.text, false); break;
+            case 'error':  finishThinkingBlock(sess); appendErrorChunk(sess, chunk.text); break;
+            case 'hitl':   finishThinkingBlock(sess); finishPendingTool(sess); appendHitlCard(sess, chunk.toolName, chunk.command); break;
+        }
+        sess.silenceTimer = setTimeout(function() {
+            if (sess.isStreaming && !sess.thinkingBlockEl) showInlineThinking(sess);
+        }, 1000);
+    } catch (e) {
+        console.error('SSE数据解析失败:', raw, e);
+    }
+}
+
+function finishStream(sess) {
+    sess.isStreaming = false;
+    if (sess.silenceTimer) { clearTimeout(sess.silenceTimer); sess.silenceTimer = null; }
+    removeThinking(sess);
+    removeInlineThinking(sess);
+    finishThinkingBlock(sess);
+    finishPendingTool(sess);
+    if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
+    resetStreamState(sess);
+    if (sess.sessionId === activeSessionId) {
+        isStreaming = false;
+        setBtnSendMode();
+        chatInput.focus();
+    }
+    loadSessionHistory();
+}

File diff suppressed because it is too large
+ 1 - 1349
src/main/resources/templates/chat.html


Some files were not shown because too many files changed in this diff