feat: ai chat added, login flow still broken
This commit is contained in:
@@ -28,6 +28,15 @@ const TagsIcon = () => (
|
||||
</svg>
|
||||
);
|
||||
|
||||
const ChatIcon = () => (
|
||||
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
|
||||
<path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H6l-2 2V4h16v12z"/>
|
||||
<circle cx="8" cy="10" r="1.5"/>
|
||||
<circle cx="12" cy="10" r="1.5"/>
|
||||
<circle cx="16" cy="10" r="1.5"/>
|
||||
</svg>
|
||||
);
|
||||
|
||||
const SyncIcon = () => (
|
||||
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
|
||||
<path d="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01-.25 1.97-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0-4.42-3.58-8-8-8zm0 14c-3.31 0-6-2.69-6-6 0-1.01.25-1.97.7-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4-4-4-4v3z"/>
|
||||
@@ -45,8 +54,11 @@ export const ActivityBar: React.FC = () => {
|
||||
// Check if tags tab is currently active
|
||||
const isTagsTabActive = tabs.some(t => t.type === 'tags' && t.id === activeTabId);
|
||||
|
||||
// Check if chat sidebar is active (activeView === 'chat' and sidebar is visible)
|
||||
const isChatActive = activeView === 'chat' && sidebarVisible;
|
||||
|
||||
// Handle view click - toggle sidebar if clicking on active view, otherwise switch view
|
||||
const handleViewClick = (view: 'posts' | 'media') => {
|
||||
const handleViewClick = (view: 'posts' | 'media' | 'chat') => {
|
||||
if (activeView === view && sidebarVisible) {
|
||||
// Clicking on active view toggles sidebar off
|
||||
toggleSidebar();
|
||||
@@ -96,6 +108,13 @@ export const ActivityBar: React.FC = () => {
|
||||
>
|
||||
<TagsIcon />
|
||||
</button>
|
||||
<button
|
||||
className={`activity-bar-item ${isChatActive ? 'active' : ''}`}
|
||||
onClick={() => handleViewClick('chat')}
|
||||
title="AI Assistant (click again to toggle sidebar)"
|
||||
>
|
||||
<ChatIcon />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="activity-bar-bottom">
|
||||
|
||||
346
src/renderer/components/ChatPanel/ChatPanel.css
Normal file
346
src/renderer/components/ChatPanel/ChatPanel.css
Normal file
@@ -0,0 +1,346 @@
|
||||
.chat-panel {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
background-color: var(--vscode-editor-background);
|
||||
}
|
||||
|
||||
.chat-panel-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 16px;
|
||||
border-bottom: 1px solid var(--vscode-editorGroup-border);
|
||||
background-color: var(--vscode-sideBar-background);
|
||||
}
|
||||
|
||||
.chat-panel-title {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: var(--vscode-foreground);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.chat-panel-model {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.model-selector-button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 4px 8px;
|
||||
font-size: 12px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
background: transparent;
|
||||
border: 1px solid var(--vscode-input-border);
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.model-selector-button:hover {
|
||||
background-color: var(--vscode-list-hoverBackground);
|
||||
}
|
||||
|
||||
.model-dropdown-icon {
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.model-dropdown {
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
right: 0;
|
||||
margin-top: 4px;
|
||||
min-width: 160px;
|
||||
background-color: var(--vscode-dropdown-background);
|
||||
border: 1px solid var(--vscode-dropdown-border);
|
||||
border-radius: 4px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.model-option {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
font-size: 12px;
|
||||
text-align: left;
|
||||
color: var(--vscode-foreground);
|
||||
background: transparent;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.model-option:hover {
|
||||
background-color: var(--vscode-list-hoverBackground);
|
||||
}
|
||||
|
||||
.model-option.active {
|
||||
background-color: var(--vscode-list-activeSelectionBackground);
|
||||
color: var(--vscode-list-activeSelectionForeground);
|
||||
}
|
||||
|
||||
.chat-messages {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.chat-welcome {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
padding: 32px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
|
||||
.chat-welcome-icon {
|
||||
font-size: 48px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.chat-welcome h2 {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 18px;
|
||||
font-weight: 500;
|
||||
color: var(--vscode-foreground);
|
||||
}
|
||||
|
||||
.chat-welcome p {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.chat-welcome ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.chat-welcome li {
|
||||
padding: 4px 0;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.chat-welcome li::before {
|
||||
content: '•';
|
||||
margin-right: 8px;
|
||||
color: var(--vscode-textLink-foreground);
|
||||
}
|
||||
|
||||
.chat-message {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.chat-message.user {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.chat-message-avatar {
|
||||
flex-shrink: 0;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 18px;
|
||||
border-radius: 50%;
|
||||
background-color: var(--vscode-input-background);
|
||||
}
|
||||
|
||||
.chat-message.user .chat-message-avatar {
|
||||
background-color: var(--vscode-button-background);
|
||||
}
|
||||
|
||||
.chat-message-content {
|
||||
max-width: 80%;
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.chat-message.user .chat-message-content {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.chat-message-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.chat-message.user .chat-message-header {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.chat-message-role {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
|
||||
.streaming-indicator {
|
||||
color: var(--vscode-button-background);
|
||||
animation: pulse 1s infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.3; }
|
||||
}
|
||||
|
||||
.chat-message-text {
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
color: var(--vscode-foreground);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
padding: 10px 14px;
|
||||
border-radius: 12px;
|
||||
background-color: var(--vscode-input-background);
|
||||
}
|
||||
|
||||
.chat-message.user .chat-message-text {
|
||||
background-color: var(--vscode-button-background);
|
||||
color: var(--vscode-button-foreground);
|
||||
border-radius: 12px 12px 2px 12px;
|
||||
}
|
||||
|
||||
.chat-message.assistant .chat-message-text {
|
||||
border-radius: 12px 12px 12px 2px;
|
||||
}
|
||||
|
||||
.chat-message.streaming .chat-message-text {
|
||||
background: linear-gradient(
|
||||
90deg,
|
||||
var(--vscode-input-background) 0%,
|
||||
var(--vscode-list-hoverBackground) 50%,
|
||||
var(--vscode-input-background) 100%
|
||||
);
|
||||
background-size: 200% 100%;
|
||||
animation: shimmer 2s infinite;
|
||||
}
|
||||
|
||||
@keyframes shimmer {
|
||||
0% { background-position: 200% 0; }
|
||||
100% { background-position: -200% 0; }
|
||||
}
|
||||
|
||||
.chat-thinking-indicator {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
padding: 12px 16px;
|
||||
}
|
||||
|
||||
.chat-thinking-indicator span {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
background-color: var(--vscode-descriptionForeground);
|
||||
animation: bounce 1.4s infinite ease-in-out both;
|
||||
}
|
||||
|
||||
.chat-thinking-indicator span:nth-child(1) {
|
||||
animation-delay: -0.32s;
|
||||
}
|
||||
|
||||
.chat-thinking-indicator span:nth-child(2) {
|
||||
animation-delay: -0.16s;
|
||||
}
|
||||
|
||||
@keyframes bounce {
|
||||
0%, 80%, 100% {
|
||||
transform: scale(0);
|
||||
}
|
||||
40% {
|
||||
transform: scale(1);
|
||||
}
|
||||
}
|
||||
|
||||
.chat-input-container {
|
||||
padding: 16px;
|
||||
border-top: 1px solid var(--vscode-editorGroup-border);
|
||||
background-color: var(--vscode-sideBar-background);
|
||||
}
|
||||
|
||||
.chat-abort-button {
|
||||
display: block;
|
||||
width: 100%;
|
||||
margin-bottom: 8px;
|
||||
padding: 8px;
|
||||
font-size: 13px;
|
||||
color: var(--vscode-errorForeground);
|
||||
background-color: transparent;
|
||||
border: 1px solid var(--vscode-errorForeground);
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.chat-abort-button:hover {
|
||||
background-color: var(--vscode-inputValidation-errorBackground);
|
||||
}
|
||||
|
||||
.chat-input-wrapper {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 8px;
|
||||
background-color: var(--vscode-input-background);
|
||||
border: 1px solid var(--vscode-input-border);
|
||||
border-radius: 8px;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.chat-input-wrapper:focus-within {
|
||||
border-color: var(--vscode-focusBorder);
|
||||
}
|
||||
|
||||
.chat-input {
|
||||
flex: 1;
|
||||
min-height: 24px;
|
||||
max-height: 120px;
|
||||
padding: 0;
|
||||
font-size: 14px;
|
||||
font-family: inherit;
|
||||
line-height: 1.5;
|
||||
color: var(--vscode-input-foreground);
|
||||
background: transparent;
|
||||
border: none;
|
||||
outline: none;
|
||||
resize: none;
|
||||
}
|
||||
|
||||
.chat-input::placeholder {
|
||||
color: var(--vscode-input-placeholderForeground);
|
||||
}
|
||||
|
||||
.chat-send-button {
|
||||
flex-shrink: 0;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 18px;
|
||||
color: var(--vscode-button-foreground);
|
||||
background-color: var(--vscode-button-background);
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.15s;
|
||||
}
|
||||
|
||||
.chat-send-button:hover:not(:disabled) {
|
||||
background-color: var(--vscode-button-hoverBackground);
|
||||
}
|
||||
|
||||
.chat-send-button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
263
src/renderer/components/ChatPanel/ChatPanel.tsx
Normal file
263
src/renderer/components/ChatPanel/ChatPanel.tsx
Normal file
@@ -0,0 +1,263 @@
|
||||
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import type { ChatMessage, ChatConversation, ChatModel } from '../../types/electron';
|
||||
import './ChatPanel.css';
|
||||
|
||||
interface ChatPanelProps {
|
||||
conversationId: string;
|
||||
}
|
||||
|
||||
export const ChatPanel: React.FC<ChatPanelProps> = ({ conversationId }) => {
|
||||
const [conversation, setConversation] = useState<ChatConversation | null>(null);
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||
const [inputValue, setInputValue] = useState('');
|
||||
const [isStreaming, setIsStreaming] = useState(false);
|
||||
const [streamingContent, setStreamingContent] = useState('');
|
||||
const [availableModels, setAvailableModels] = useState<ChatModel[]>([]);
|
||||
const [showModelSelector, setShowModelSelector] = useState(false);
|
||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||
const inputRef = useRef<HTMLTextAreaElement>(null);
|
||||
const streamingRef = useRef('');
|
||||
|
||||
// Scroll to bottom when messages change
|
||||
const scrollToBottom = useCallback(() => {
|
||||
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
||||
}, []);
|
||||
|
||||
// Load conversation and messages
|
||||
const loadData = useCallback(async () => {
|
||||
try {
|
||||
const [conv, msgs, models] = await Promise.all([
|
||||
window.electronAPI?.chat.getConversation(conversationId),
|
||||
window.electronAPI?.chat.getHistory(conversationId),
|
||||
window.electronAPI?.chat.getAvailableModels()
|
||||
]);
|
||||
|
||||
if (conv) setConversation(conv);
|
||||
if (msgs) setMessages(msgs);
|
||||
if (models) setAvailableModels(models);
|
||||
} catch (error) {
|
||||
console.error('Failed to load chat data:', error);
|
||||
}
|
||||
}, [conversationId]);
|
||||
|
||||
useEffect(() => {
|
||||
loadData();
|
||||
|
||||
// Subscribe to stream events
|
||||
const unsubDelta = window.electronAPI?.chat.onStreamDelta((data) => {
|
||||
if (data.conversationId === conversationId) {
|
||||
streamingRef.current += data.delta;
|
||||
setStreamingContent(streamingRef.current);
|
||||
scrollToBottom();
|
||||
}
|
||||
});
|
||||
|
||||
const unsubTitle = window.electronAPI?.chat.onTitleUpdated((data) => {
|
||||
if (data.conversationId === conversationId) {
|
||||
setConversation(prev => prev ? { ...prev, title: data.title } : null);
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsubDelta?.();
|
||||
unsubTitle?.();
|
||||
};
|
||||
}, [conversationId, loadData, scrollToBottom]);
|
||||
|
||||
// Scroll on new messages or streaming content
|
||||
useEffect(() => {
|
||||
scrollToBottom();
|
||||
}, [messages, streamingContent, scrollToBottom]);
|
||||
|
||||
const handleSend = async () => {
|
||||
const message = inputValue.trim();
|
||||
if (!message || isStreaming) return;
|
||||
|
||||
setInputValue('');
|
||||
setIsStreaming(true);
|
||||
streamingRef.current = '';
|
||||
setStreamingContent('');
|
||||
|
||||
// Add user message optimistically
|
||||
const userMessage: ChatMessage = {
|
||||
id: `temp-${Date.now()}`,
|
||||
conversationId,
|
||||
role: 'user',
|
||||
content: message,
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
setMessages(prev => [...prev, userMessage]);
|
||||
|
||||
try {
|
||||
// Send message and wait for complete response
|
||||
await window.electronAPI?.chat.sendMessage(conversationId, message);
|
||||
|
||||
// Reload messages to get the saved assistant response
|
||||
const msgs = await window.electronAPI?.chat.getHistory(conversationId);
|
||||
if (msgs) setMessages(msgs);
|
||||
} catch (error) {
|
||||
console.error('Failed to send message:', error);
|
||||
// Add error message
|
||||
const errorMessage: ChatMessage = {
|
||||
id: `error-${Date.now()}`,
|
||||
conversationId,
|
||||
role: 'assistant',
|
||||
content: 'Sorry, an error occurred while processing your message.',
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
setMessages(prev => [...prev, errorMessage]);
|
||||
} finally {
|
||||
setIsStreaming(false);
|
||||
setStreamingContent('');
|
||||
streamingRef.current = '';
|
||||
}
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSend();
|
||||
}
|
||||
};
|
||||
|
||||
const handleAbort = async () => {
|
||||
try {
|
||||
await window.electronAPI?.chat.abortMessage(conversationId);
|
||||
} catch (error) {
|
||||
console.error('Failed to abort:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleModelChange = async (modelId: string) => {
|
||||
try {
|
||||
await window.electronAPI?.chat.setConversationModel(conversationId, modelId);
|
||||
setConversation(prev => prev ? { ...prev, model: modelId } : null);
|
||||
setShowModelSelector(false);
|
||||
} catch (error) {
|
||||
console.error('Failed to change model:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const renderMessage = (msg: ChatMessage) => {
|
||||
if (msg.role === 'system' || msg.role === 'tool') return null;
|
||||
|
||||
return (
|
||||
<div key={msg.id} className={`chat-message ${msg.role}`}>
|
||||
<div className="chat-message-avatar">
|
||||
{msg.role === 'user' ? '👤' : '🤖'}
|
||||
</div>
|
||||
<div className="chat-message-content">
|
||||
<div className="chat-message-header">
|
||||
<span className="chat-message-role">
|
||||
{msg.role === 'user' ? 'You' : 'Assistant'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="chat-message-text">{msg.content}</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="chat-panel">
|
||||
<div className="chat-panel-header">
|
||||
<div className="chat-panel-title">
|
||||
{conversation?.title || 'New Chat'}
|
||||
</div>
|
||||
<div className="chat-panel-model">
|
||||
<button
|
||||
className="model-selector-button"
|
||||
onClick={() => setShowModelSelector(!showModelSelector)}
|
||||
>
|
||||
{conversation?.model || 'gpt-4o'}
|
||||
<span className="model-dropdown-icon">▾</span>
|
||||
</button>
|
||||
{showModelSelector && (
|
||||
<div className="model-dropdown">
|
||||
{availableModels.map(model => (
|
||||
<button
|
||||
key={model.id}
|
||||
className={`model-option ${conversation?.model === model.id ? 'active' : ''}`}
|
||||
onClick={() => handleModelChange(model.id)}
|
||||
>
|
||||
{model.name}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="chat-messages">
|
||||
{messages.length === 0 && !isStreaming && (
|
||||
<div className="chat-welcome">
|
||||
<div className="chat-welcome-icon">🤖</div>
|
||||
<h2>Welcome to the AI Assistant</h2>
|
||||
<p>I can help you manage your posts and media. Try asking me to:</p>
|
||||
<ul>
|
||||
<li>Search for posts about a specific topic</li>
|
||||
<li>Get details about a specific post</li>
|
||||
<li>Update metadata for posts or media</li>
|
||||
<li>List all images in your media library</li>
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{messages.map(renderMessage)}
|
||||
|
||||
{isStreaming && streamingContent && (
|
||||
<div className="chat-message assistant streaming">
|
||||
<div className="chat-message-avatar">🤖</div>
|
||||
<div className="chat-message-content">
|
||||
<div className="chat-message-header">
|
||||
<span className="chat-message-role">Assistant</span>
|
||||
<span className="streaming-indicator">●</span>
|
||||
</div>
|
||||
<div className="chat-message-text">{streamingContent}</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isStreaming && !streamingContent && (
|
||||
<div className="chat-message assistant thinking">
|
||||
<div className="chat-message-avatar">🤖</div>
|
||||
<div className="chat-message-content">
|
||||
<div className="chat-thinking-indicator">
|
||||
<span></span><span></span><span></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div ref={messagesEndRef} />
|
||||
</div>
|
||||
|
||||
<div className="chat-input-container">
|
||||
{isStreaming && (
|
||||
<button className="chat-abort-button" onClick={handleAbort}>
|
||||
◼ Stop
|
||||
</button>
|
||||
)}
|
||||
<div className="chat-input-wrapper">
|
||||
<textarea
|
||||
ref={inputRef}
|
||||
className="chat-input"
|
||||
value={inputValue}
|
||||
onChange={(e) => setInputValue(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder="Type a message..."
|
||||
rows={1}
|
||||
disabled={isStreaming}
|
||||
/>
|
||||
<button
|
||||
className="chat-send-button"
|
||||
onClick={handleSend}
|
||||
disabled={!inputValue.trim() || isStreaming}
|
||||
>
|
||||
↑
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
1
src/renderer/components/ChatPanel/index.ts
Normal file
1
src/renderer/components/ChatPanel/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { ChatPanel } from './ChatPanel';
|
||||
@@ -9,6 +9,7 @@ import { ErrorModal } from '../ErrorModal';
|
||||
import { SettingsView } from '../SettingsView';
|
||||
import { TagsView } from '../TagsView';
|
||||
import { TagInput } from '../TagInput';
|
||||
import { ChatPanel } from '../ChatPanel';
|
||||
import { AutoSaveManager } from '../../utils';
|
||||
import './Editor.css';
|
||||
|
||||
@@ -1005,6 +1006,7 @@ export const Editor: React.FC = () => {
|
||||
const showMedia = activeTab?.type === 'media';
|
||||
const showSettings = activeTab?.type === 'settings' || (activeView === 'settings' && !activeTab);
|
||||
const showTags = activeTab?.type === 'tags' || (activeView === 'tags' && !activeTab);
|
||||
const showChat = activeTab?.type === 'chat';
|
||||
|
||||
// Clear selectedPostId if the post doesn't exist (e.g., after project switch)
|
||||
useEffect(() => {
|
||||
@@ -1068,6 +1070,16 @@ export const Editor: React.FC = () => {
|
||||
);
|
||||
}
|
||||
|
||||
// Show chat if chat tab is active
|
||||
if (showChat && activeTabId) {
|
||||
return (
|
||||
<>
|
||||
<ChatPanel key={activeTabId} conversationId={activeTabId} />
|
||||
{renderErrorModal()}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
// Show post editor if a post tab is active
|
||||
if (showPost && activeTabId) {
|
||||
const post = posts.find(p => p.id === activeTabId);
|
||||
|
||||
@@ -606,3 +606,180 @@
|
||||
.sidebar-item.unsaved .sidebar-item-meta {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* Chat List Styles */
|
||||
.chat-list {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.chat-list-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 8px 12px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
color: var(--vscode-sideBar-foreground);
|
||||
border-bottom: 1px solid var(--vscode-sideBar-border);
|
||||
}
|
||||
|
||||
.chat-new-button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: var(--vscode-textLink-foreground);
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
padding: 2px 6px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.chat-new-button:hover {
|
||||
background-color: var(--vscode-list-hoverBackground);
|
||||
}
|
||||
|
||||
.chat-user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 8px 12px;
|
||||
font-size: 12px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
border-bottom: 1px solid var(--vscode-sideBar-border);
|
||||
}
|
||||
|
||||
.chat-user-icon {
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.chat-username {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.chat-list-items {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.chat-list-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 8px 12px;
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid var(--vscode-sideBar-border);
|
||||
}
|
||||
|
||||
.chat-list-item:hover {
|
||||
background-color: var(--vscode-list-hoverBackground);
|
||||
}
|
||||
|
||||
.chat-item-content {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat-item-title {
|
||||
font-size: 13px;
|
||||
color: var(--vscode-foreground);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.chat-item-date {
|
||||
font-size: 11px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
|
||||
.chat-item-delete {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
padding: 0 4px;
|
||||
opacity: 0;
|
||||
transition: opacity 0.15s;
|
||||
}
|
||||
|
||||
.chat-list-item:hover .chat-item-delete {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.chat-item-delete:hover {
|
||||
color: var(--vscode-errorForeground);
|
||||
}
|
||||
|
||||
.chat-loading,
|
||||
.chat-empty {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.chat-start-button,
|
||||
.chat-login-button {
|
||||
margin-top: 12px;
|
||||
padding: 8px 16px;
|
||||
font-size: 13px;
|
||||
color: var(--vscode-button-foreground);
|
||||
background-color: var(--vscode-button-background);
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.chat-start-button:hover,
|
||||
.chat-login-button:hover {
|
||||
background-color: var(--vscode-button-hoverBackground);
|
||||
}
|
||||
|
||||
.chat-auth-prompt {
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
|
||||
.chat-auth-prompt p {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.device-code-prompt {
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
.device-code-prompt a {
|
||||
color: var(--vscode-textLink-foreground);
|
||||
text-decoration: none;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.device-code-prompt a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.device-code {
|
||||
margin-top: 8px;
|
||||
padding: 8px 16px;
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
font-family: var(--vscode-editor-font-family, monospace);
|
||||
letter-spacing: 3px;
|
||||
color: var(--vscode-foreground);
|
||||
background-color: var(--vscode-input-background);
|
||||
border: 1px solid var(--vscode-input-border);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import React, { useState, useEffect, useCallback } from 'react';
|
||||
import { useAppStore, PostData } from '../../store';
|
||||
import { showToast } from '../Toast';
|
||||
import type { ChatConversation } from '../../types/electron';
|
||||
import './Sidebar.css';
|
||||
|
||||
const formatDate = (dateString: string) => {
|
||||
@@ -747,6 +748,210 @@ const SettingsNav: React.FC = () => {
|
||||
);
|
||||
};
|
||||
|
||||
// Chat conversations list
|
||||
const ChatList: React.FC = () => {
|
||||
const { openTab } = useAppStore();
|
||||
const [conversations, setConversations] = useState<ChatConversation[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [authStatus, setAuthStatus] = useState<{ authenticated: boolean; username?: string } | null>(null);
|
||||
const [deviceCode, setDeviceCode] = useState<{ verificationUri: string; userCode: string } | null>(null);
|
||||
|
||||
// Load conversations
|
||||
const loadConversations = useCallback(async () => {
|
||||
try {
|
||||
const convs = await window.electronAPI?.chat.getConversations();
|
||||
if (convs) {
|
||||
setConversations(convs);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load conversations:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Check auth status
|
||||
const checkAuth = useCallback(async () => {
|
||||
try {
|
||||
const status = await window.electronAPI?.chat.copilotAuthStatus();
|
||||
setAuthStatus(status ?? null);
|
||||
} catch (error) {
|
||||
console.error('Failed to check auth:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
setIsLoading(true);
|
||||
await checkAuth();
|
||||
await loadConversations();
|
||||
setIsLoading(false);
|
||||
};
|
||||
init();
|
||||
|
||||
// Subscribe to title updates
|
||||
const unsubTitle = window.electronAPI?.chat.onTitleUpdated((data) => {
|
||||
setConversations(prev =>
|
||||
prev.map(c => c.id === data.conversationId ? { ...c, title: data.title } : c)
|
||||
);
|
||||
});
|
||||
|
||||
// Subscribe to device code for login flow
|
||||
const unsubDevice = window.electronAPI?.chat.onDeviceCode((data) => {
|
||||
setDeviceCode(data);
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsubTitle?.();
|
||||
unsubDevice?.();
|
||||
};
|
||||
}, [loadConversations, checkAuth]);
|
||||
|
||||
const handleNewChat = async () => {
|
||||
try {
|
||||
const conversation = await window.electronAPI?.chat.createConversation();
|
||||
if (conversation) {
|
||||
setConversations(prev => [conversation, ...prev]);
|
||||
openTab({ type: 'chat', id: conversation.id, isTransient: false });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to create conversation:', error);
|
||||
showToast.error('Failed to create new chat');
|
||||
}
|
||||
};
|
||||
|
||||
const handleOpenChat = (conversationId: string) => {
|
||||
openTab({ type: 'chat', id: conversationId, isTransient: false });
|
||||
};
|
||||
|
||||
const handleDeleteChat = async (conversationId: string) => {
|
||||
try {
|
||||
await window.electronAPI?.chat.deleteConversation(conversationId);
|
||||
setConversations(prev => prev.filter(c => c.id !== conversationId));
|
||||
} catch (error) {
|
||||
console.error('Failed to delete conversation:', error);
|
||||
showToast.error('Failed to delete chat');
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogin = async () => {
|
||||
try {
|
||||
const result = await window.electronAPI?.chat.copilotLogin();
|
||||
if (result?.success) {
|
||||
setDeviceCode(null);
|
||||
await checkAuth();
|
||||
} else if (result?.error) {
|
||||
console.error('Login failed:', result.error);
|
||||
showToast.error(result.error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
showToast.error('Login failed');
|
||||
}
|
||||
};
|
||||
|
||||
const formatChatDate = (dateString: string) => {
|
||||
const date = new Date(dateString);
|
||||
const now = new Date();
|
||||
const diffDays = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (diffDays === 0) {
|
||||
return date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit' });
|
||||
} else if (diffDays === 1) {
|
||||
return 'Yesterday';
|
||||
} else if (diffDays < 7) {
|
||||
return date.toLocaleDateString('en-US', { weekday: 'short' });
|
||||
}
|
||||
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="chat-list">
|
||||
<div className="chat-list-header">
|
||||
<span>AI ASSISTANT</span>
|
||||
</div>
|
||||
<div className="chat-loading">Loading...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Show login prompt if not authenticated
|
||||
if (!authStatus?.authenticated) {
|
||||
return (
|
||||
<div className="chat-list">
|
||||
<div className="chat-list-header">
|
||||
<span>AI ASSISTANT</span>
|
||||
</div>
|
||||
<div className="chat-auth-prompt">
|
||||
<p>Sign in to GitHub Copilot to start chatting</p>
|
||||
{deviceCode ? (
|
||||
<div className="device-code-prompt">
|
||||
<p>Enter this code at:</p>
|
||||
<a href={deviceCode.verificationUri} target="_blank" rel="noopener noreferrer">
|
||||
{deviceCode.verificationUri}
|
||||
</a>
|
||||
<div className="device-code">{deviceCode.userCode}</div>
|
||||
</div>
|
||||
) : (
|
||||
<button className="chat-login-button" onClick={handleLogin}>
|
||||
Sign in with GitHub
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="chat-list">
|
||||
<div className="chat-list-header">
|
||||
<span>AI ASSISTANT</span>
|
||||
<button className="chat-new-button" onClick={handleNewChat} title="New Chat">
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
{authStatus.username && (
|
||||
<div className="chat-user-info">
|
||||
<span className="chat-user-icon">👤</span>
|
||||
<span className="chat-username">{authStatus.username}</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="chat-list-items">
|
||||
{conversations.length === 0 ? (
|
||||
<div className="chat-empty">
|
||||
<p>No conversations yet</p>
|
||||
<button className="chat-start-button" onClick={handleNewChat}>
|
||||
Start a new chat
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
conversations.map(conv => (
|
||||
<div
|
||||
key={conv.id}
|
||||
className="chat-list-item"
|
||||
onClick={() => handleOpenChat(conv.id)}
|
||||
>
|
||||
<div className="chat-item-content">
|
||||
<div className="chat-item-title">{conv.title}</div>
|
||||
<div className="chat-item-date">{formatChatDate(conv.updatedAt)}</div>
|
||||
</div>
|
||||
<button
|
||||
className="chat-item-delete"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleDeleteChat(conv.id);
|
||||
}}
|
||||
title="Delete conversation"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const Sidebar: React.FC = () => {
|
||||
const { activeView, sidebarVisible } = useAppStore();
|
||||
|
||||
@@ -760,6 +965,7 @@ export const Sidebar: React.FC = () => {
|
||||
{activeView === 'media' && <MediaList />}
|
||||
{activeView === 'settings' && <SettingsNav />}
|
||||
{activeView === 'tags' && <TagsNav />}
|
||||
{activeView === 'chat' && <ChatList />}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -16,3 +16,4 @@ export { TagsView, scrollToTagsSection, type TagsCategory } from './TagsView';
|
||||
export { TagInput } from './TagInput';
|
||||
export { PostLinks } from './PostLinks';
|
||||
export { ErrorModal, type ErrorDetails } from './ErrorModal';
|
||||
export { ChatPanel } from './ChatPanel';
|
||||
|
||||
@@ -5,7 +5,7 @@ import { persist } from 'zustand/middleware';
|
||||
const STORAGE_KEY = 'bds-app-state';
|
||||
|
||||
// Tab types
|
||||
export type TabType = 'post' | 'media' | 'settings' | 'tags';
|
||||
export type TabType = 'post' | 'media' | 'settings' | 'tags' | 'chat';
|
||||
|
||||
export interface Tab {
|
||||
type: TabType;
|
||||
@@ -88,7 +88,7 @@ interface AppState {
|
||||
activeTabId: string | null;
|
||||
|
||||
// UI State
|
||||
activeView: 'posts' | 'media' | 'settings' | 'tags';
|
||||
activeView: 'posts' | 'media' | 'settings' | 'tags' | 'chat';
|
||||
sidebarVisible: boolean;
|
||||
panelVisible: boolean;
|
||||
selectedPostId: string | null;
|
||||
@@ -136,7 +136,7 @@ interface AppState {
|
||||
restoreTabState: (state: TabState) => void;
|
||||
|
||||
// Actions
|
||||
setActiveView: (view: 'posts' | 'media' | 'settings' | 'tags') => void;
|
||||
setActiveView: (view: 'posts' | 'media' | 'settings' | 'tags' | 'chat') => void;
|
||||
toggleSidebar: () => void;
|
||||
togglePanel: () => void;
|
||||
setSelectedPost: (id: string | null) => void;
|
||||
|
||||
98
src/renderer/types/electron.d.ts
vendored
98
src/renderer/types/electron.d.ts
vendored
@@ -170,6 +170,70 @@ export interface SyncTagsResult {
|
||||
added: string[];
|
||||
}
|
||||
|
||||
// Chat/AI types
|
||||
export interface ChatConversation {
|
||||
id: string;
|
||||
projectId: string;
|
||||
title: string;
|
||||
model?: string;
|
||||
copilotSessionId?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface ChatMessage {
|
||||
id: string;
|
||||
conversationId: string;
|
||||
role: 'user' | 'assistant' | 'system' | 'tool';
|
||||
content: string;
|
||||
toolCallId?: string;
|
||||
toolCalls?: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface ChatModel {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ChatAuthStatus {
|
||||
authenticated: boolean;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
export interface ChatReadyStatus {
|
||||
ready: boolean;
|
||||
authenticated: boolean;
|
||||
}
|
||||
|
||||
export interface ChatStreamDelta {
|
||||
conversationId: string;
|
||||
delta: string;
|
||||
}
|
||||
|
||||
export interface ChatToolCall {
|
||||
conversationId: string;
|
||||
toolCall: {
|
||||
name: string;
|
||||
arguments: Record<string, unknown>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ChatToolResult {
|
||||
conversationId: string;
|
||||
result: unknown;
|
||||
}
|
||||
|
||||
export interface ChatTitleUpdate {
|
||||
conversationId: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
export interface ChatDeviceCode {
|
||||
verificationUri: string;
|
||||
userCode: string;
|
||||
}
|
||||
|
||||
export interface ElectronAPI {
|
||||
projects: {
|
||||
create: (data: { name: string; description?: string; slug?: string }) => Promise<ProjectData>;
|
||||
@@ -274,6 +338,40 @@ export interface ElectronAPI {
|
||||
getPostsWithTag: (tagId: string) => Promise<string[]>;
|
||||
syncFromPosts: () => Promise<SyncTagsResult>;
|
||||
};
|
||||
chat: {
|
||||
// Authentication
|
||||
checkReady: () => Promise<ChatReadyStatus>;
|
||||
copilotAuthStatus: () => Promise<ChatAuthStatus>;
|
||||
copilotLogin: () => Promise<{ success: boolean; error?: string; login?: string }>;
|
||||
copilotLogout: () => Promise<void>;
|
||||
|
||||
// Settings
|
||||
getAvailableModels: () => Promise<ChatModel[]>;
|
||||
setDefaultModel: (modelId: string) => Promise<void>;
|
||||
getSystemPrompt: () => Promise<string | null>;
|
||||
setSystemPrompt: (prompt: string) => Promise<void>;
|
||||
|
||||
// Conversations
|
||||
getConversations: () => Promise<ChatConversation[]>;
|
||||
createConversation: (title?: string, model?: string) => Promise<ChatConversation>;
|
||||
getConversation: (id: string) => Promise<ChatConversation | null>;
|
||||
updateConversation: (id: string, updates: { title?: string; model?: string }) => Promise<ChatConversation | null>;
|
||||
deleteConversation: (id: string) => Promise<boolean>;
|
||||
|
||||
// Messaging
|
||||
sendMessage: (conversationId: string, message: string) => Promise<string>;
|
||||
abortMessage: (conversationId: string) => Promise<void>;
|
||||
getHistory: (conversationId: string) => Promise<ChatMessage[]>;
|
||||
clearMessages: (conversationId: string) => Promise<void>;
|
||||
setConversationModel: (conversationId: string, modelId: string) => Promise<void>;
|
||||
|
||||
// Event listeners for streaming/progress
|
||||
onStreamDelta: (callback: (data: ChatStreamDelta) => void) => () => void;
|
||||
onToolCall: (callback: (data: ChatToolCall) => void) => () => void;
|
||||
onToolResult: (callback: (data: ChatToolResult) => void) => () => void;
|
||||
onTitleUpdated: (callback: (data: ChatTitleUpdate) => void) => () => void;
|
||||
onDeviceCode: (callback: (data: ChatDeviceCode) => void) => () => void;
|
||||
};
|
||||
on: (channel: string, callback: (...args: unknown[]) => void) => () => void;
|
||||
once: (channel: string, callback: (...args: unknown[]) => void) => void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user