Compare commits
10 Commits
main
...
token-trac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b87af24eb3 | ||
|
|
e5213ed496 | ||
|
|
0e03c96c77 | ||
|
|
7d3e2daf72 | ||
|
|
2d174b50e0 | ||
|
|
350e6aa6c6 | ||
|
|
96e3755d23 | ||
|
|
59ee612f79 | ||
|
|
2a7ee9ac19 | ||
|
|
4182b9e0b3 |
@ -1,204 +0,0 @@
|
||||
# Messages Tab Fix Documentation
|
||||
|
||||
## Overview
|
||||
|
||||
This document details the fix implemented for the Messages tab in Crystal's session window. The Messages tab was intended to show JSON output from Claude Code for debugging purposes but was broken and not displaying any data.
|
||||
|
||||
## Problem Description
|
||||
|
||||
The Messages tab in the session view was supposed to display JSON messages from Claude Code instances for debugging, but it was showing "No messages yet" even when sessions had active Claude Code conversations. The issue was that there was no mechanism to load and populate the JSON messages data.
|
||||
|
||||
## Root Cause Analysis
|
||||
|
||||
1. **Missing Data Flow**: The `JsonMessageView` component expected `activeSession.jsonMessages` to be populated, but this array was always empty
|
||||
2. **No Loading Mechanism**: There was no IPC handler or frontend logic to fetch JSON messages separately from regular output
|
||||
3. **Data Transformation Issue**: The existing `sessions:get-output` handler was transforming JSON messages to formatted stdout for the Output tab, losing the original JSON structure
|
||||
|
||||
## Solution Architecture
|
||||
|
||||
The fix implements a complete end-to-end data flow:
|
||||
|
||||
```
|
||||
Frontend (Messages Tab) โ API Layer โ IPC Handler โ Session Manager โ Database
|
||||
```
|
||||
|
||||
### Key Components
|
||||
|
||||
1. **New IPC Handler**: `sessions:get-json-messages`
|
||||
2. **Frontend API Method**: `API.sessions.getJsonMessages()`
|
||||
3. **Session Store Method**: `setSessionJsonMessages()`
|
||||
4. **Auto-loading Logic**: Effect hook that loads JSON messages when switching to Messages view
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### 1. Backend IPC Handler (`main/src/ipc/session.ts`)
|
||||
|
||||
```typescript
|
||||
ipcMain.handle('sessions:get-json-messages', async (_event, sessionId: string) => {
|
||||
try {
|
||||
const outputs = await sessionManager.getSessionOutputs(sessionId);
|
||||
|
||||
// Filter to only JSON messages and include timestamp
|
||||
const jsonMessages = outputs
|
||||
.filter(output => output.type === 'json')
|
||||
.map(output => ({
|
||||
...output.data,
|
||||
timestamp: output.timestamp.toISOString()
|
||||
}));
|
||||
|
||||
return { success: true, data: jsonMessages };
|
||||
} catch (error) {
|
||||
return { success: false, error: 'Failed to get JSON messages' };
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**Purpose**: Extracts and returns only JSON-type outputs from session data, preserving the original JSON structure with timestamps.
|
||||
|
||||
### 2. Preload Script Update (`main/src/preload.ts`)
|
||||
|
||||
```typescript
|
||||
getJsonMessages: (sessionId: string): Promise<IPCResponse> =>
|
||||
ipcRenderer.invoke('sessions:get-json-messages', sessionId),
|
||||
```
|
||||
|
||||
**Purpose**: Exposes the new IPC handler to the frontend through the secure preload script.
|
||||
|
||||
### 3. Frontend API Layer (`frontend/src/utils/api.ts`)
|
||||
|
||||
```typescript
|
||||
async getJsonMessages(sessionId: string) {
|
||||
if (!isElectron()) throw new Error('Electron API not available');
|
||||
return window.electronAPI.sessions.getJsonMessages(sessionId);
|
||||
}
|
||||
```
|
||||
|
||||
**Purpose**: Provides a consistent API interface with error handling for the frontend to use.
|
||||
|
||||
### 4. Session Store Enhancement (`frontend/src/stores/sessionStore.ts`)
|
||||
|
||||
```typescript
|
||||
setSessionJsonMessages: (sessionId, jsonMessages) => set((state) => {
|
||||
// Update both regular sessions and activeMainRepoSession
|
||||
const updatedSessions = state.sessions.map(session => {
|
||||
if (session.id === sessionId) {
|
||||
return { ...session, jsonMessages };
|
||||
}
|
||||
return session;
|
||||
});
|
||||
|
||||
let updatedActiveMainRepoSession = state.activeMainRepoSession;
|
||||
if (state.activeMainRepoSession?.id === sessionId) {
|
||||
updatedActiveMainRepoSession = { ...state.activeMainRepoSession, jsonMessages };
|
||||
}
|
||||
|
||||
return { ...state, sessions: updatedSessions, activeMainRepoSession: updatedActiveMainRepoSession };
|
||||
})
|
||||
```
|
||||
|
||||
**Purpose**: Updates session state with JSON messages while maintaining consistency between regular sessions and main repo sessions.
|
||||
|
||||
### 5. Session View Hook (`frontend/src/hooks/useSessionView.ts`)
|
||||
|
||||
```typescript
|
||||
// Load JSON messages for the Messages tab
|
||||
const loadJsonMessages = useCallback(async (sessionId: string) => {
|
||||
try {
|
||||
const response = await API.sessions.getJsonMessages(sessionId);
|
||||
if (response.success) {
|
||||
const jsonMessages = response.data || [];
|
||||
useSessionStore.getState().setSessionJsonMessages(sessionId, jsonMessages);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error loading JSON messages:`, error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Load JSON messages when switching to messages view
|
||||
useEffect(() => {
|
||||
if (!activeSession || viewMode !== 'messages') return;
|
||||
loadJsonMessages(activeSession.id);
|
||||
}, [activeSession?.id, viewMode, loadJsonMessages]);
|
||||
```
|
||||
|
||||
**Purpose**: Automatically loads JSON messages when the user switches to the Messages tab, ensuring data is available when needed.
|
||||
|
||||
### 6. TypeScript Definitions (`frontend/src/types/electron.d.ts`)
|
||||
|
||||
```typescript
|
||||
interface ElectronAPI {
|
||||
sessions: {
|
||||
// ... other methods
|
||||
getJsonMessages: (sessionId: string) => Promise<IPCResponse>;
|
||||
// ... other methods
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
**Purpose**: Provides type safety for the new IPC method in TypeScript.
|
||||
|
||||
## Data Flow
|
||||
|
||||
1. **User Action**: User clicks on "Messages" tab in session view
|
||||
2. **View Mode Change**: `viewMode` state changes to 'messages'
|
||||
3. **Effect Trigger**: useEffect hook detects the view mode change
|
||||
4. **API Call**: `loadJsonMessages()` function calls `API.sessions.getJsonMessages()`
|
||||
5. **IPC Communication**: Frontend calls the `sessions:get-json-messages` IPC handler
|
||||
6. **Data Retrieval**: Backend fetches session outputs and filters for JSON messages
|
||||
7. **Response**: Filtered JSON messages returned to frontend
|
||||
8. **State Update**: `setSessionJsonMessages()` updates the session store
|
||||
9. **UI Update**: `JsonMessageView` component re-renders with the new data
|
||||
|
||||
## Message Structure
|
||||
|
||||
JSON messages are formatted with the following structure:
|
||||
|
||||
```typescript
|
||||
interface JsonMessage {
|
||||
type: string; // e.g., 'system', 'user', 'assistant'
|
||||
subtype?: string; // e.g., 'init', 'result'
|
||||
content?: string; // Message content
|
||||
data?: any; // Additional structured data
|
||||
timestamp: string; // ISO timestamp
|
||||
[key: string]: any; // Other fields from Claude Code
|
||||
}
|
||||
```
|
||||
|
||||
## Benefits
|
||||
|
||||
1. **Debugging Capability**: Developers can now inspect raw JSON messages from Claude Code
|
||||
2. **Performance**: JSON messages are only loaded when the Messages tab is viewed
|
||||
3. **Real-time**: Messages are loaded fresh each time the tab is accessed
|
||||
4. **Type Safety**: Full TypeScript support for the new functionality
|
||||
5. **Consistency**: Follows existing patterns in the codebase
|
||||
|
||||
## Testing
|
||||
|
||||
The fix was tested by:
|
||||
|
||||
1. **Type Checking**: `pnpm typecheck` passes without errors
|
||||
2. **Compilation**: `pnpm build:main` succeeds
|
||||
3. **Runtime Testing**: Application starts successfully in development mode
|
||||
4. **Integration**: IPC handlers are properly registered and accessible
|
||||
|
||||
## Files Modified
|
||||
|
||||
- `main/src/ipc/session.ts` - Added new IPC handler
|
||||
- `main/src/preload.ts` - Exposed new method to frontend
|
||||
- `frontend/src/utils/api.ts` - Added API method
|
||||
- `frontend/src/stores/sessionStore.ts` - Added state management method
|
||||
- `frontend/src/hooks/useSessionView.ts` - Added loading logic
|
||||
- `frontend/src/types/electron.d.ts` - Added TypeScript definitions
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
Potential improvements could include:
|
||||
|
||||
1. **Caching**: Cache JSON messages to avoid repeated API calls
|
||||
2. **Real-time Updates**: Listen for new JSON messages and update the tab live
|
||||
3. **Filtering**: Add filtering/search capabilities within the Messages tab
|
||||
4. **Export**: Allow exporting JSON messages for external debugging tools
|
||||
|
||||
## Conclusion
|
||||
|
||||
The Messages tab fix provides a complete solution for debugging Claude Code sessions by making the raw JSON messages accessible through a clean, type-safe interface. The implementation follows Crystal's architectural patterns and maintains consistency with existing code.
|
||||
@ -1,70 +0,0 @@
|
||||
## Summary
|
||||
|
||||
This PR introduces a new **Project Dashboard** view that fundamentally changes how users interact with projects in Crystal. Instead of showing a dialog to create sessions without worktrees, clicking on a project now displays a comprehensive dashboard with git status information.
|
||||
|
||||
## Key Changes
|
||||
|
||||
### ๐ฏ Replaces Non-Worktree Session Creation Dialog
|
||||
- **Before**: Clicking a project showed a dialog asking if you want to create a session without a worktree
|
||||
- **After**: Clicking a project displays a full dashboard with project status and existing sessions
|
||||
|
||||
### ๐ New Project Dashboard Features
|
||||
|
||||
#### Multi-Origin Git Status Display
|
||||
- Visual cascade showing the flow: `upstream โ origin/fork โ local main โ session branches`
|
||||
- Automatically detects and displays multiple remotes
|
||||
- Color-coded status indicators:
|
||||
- ๐ข Green = Synced
|
||||
- ๐ก Yellow = Behind
|
||||
- ๐ต Blue = Ahead
|
||||
- ๐ด Red = Diverged
|
||||
- Shows commit counts for ahead/behind status
|
||||
- "Fork" badge when origin is a fork with an upstream
|
||||
|
||||
#### Session Management
|
||||
- Comprehensive table showing all session branches
|
||||
- Quick filtering options:
|
||||
- All Sessions
|
||||
- Stale Only
|
||||
- With Changes
|
||||
- With PR
|
||||
- Click any session row to navigate directly to that session
|
||||
- Full viewport scrolling for projects with many sessions
|
||||
|
||||
#### Status Summary Cards
|
||||
- **Up to date**: Shows how many sessions are current (e.g., "8/12")
|
||||
- **Stale**: Count of sessions behind their base branch
|
||||
- **Changes**: Sessions with uncommitted changes
|
||||
|
||||
### ๐ Fixes Included
|
||||
- Project name now displays correctly in dashboard header
|
||||
- Sessions table properly scrolls when there are many sessions
|
||||
- Sidebar session clicking now correctly navigates to sessions
|
||||
- Proper skeleton loading states when switching between projects
|
||||
|
||||
### ๐๏ธ Technical Implementation
|
||||
- New IPC endpoint: `dashboard:get-project-status`
|
||||
- Caching layer for dashboard data to improve performance
|
||||
- Fetches from all git remotes to show complete status
|
||||
- New navigation store to manage dashboard vs session views
|
||||
- Responsive design with proper viewport utilization
|
||||
|
||||
## Screenshots
|
||||
|
||||
The dashboard provides immediate visibility into:
|
||||
1. Is my fork behind the upstream?
|
||||
2. Is my local main behind my fork's origin?
|
||||
3. Which sessions are stale and need updating?
|
||||
|
||||
All this information is now available at a glance without needing to create a new session.
|
||||
|
||||
## Testing
|
||||
- Click on any project in the sidebar to see the new dashboard
|
||||
- Test with projects that have:
|
||||
- Single remote (origin only)
|
||||
- Multiple remotes (upstream + origin)
|
||||
- Many sessions to verify scrolling
|
||||
- Use the filter dropdown to filter sessions
|
||||
- Click on sessions to verify navigation works
|
||||
|
||||
This change significantly improves the UX by providing immediate project insights and making session management more intuitive.
|
||||
@ -120,9 +120,6 @@ This ensures:
|
||||
- Worktrees won't conflict between the two instances
|
||||
- You can safely test changes without affecting your primary Crystal setup
|
||||
|
||||
## Additional Documentation
|
||||
|
||||
For a full project overview, see [CLAUDE.md](CLAUDE.md). Additional diagrams, database schema details, release instructions, and license notes can be found in the [docs](./docs) directory.
|
||||
|
||||
## ๐ License
|
||||
|
||||
|
||||
96
debug_tokens.js
Normal file
96
debug_tokens.js
Normal file
@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
const path = require('path');
|
||||
|
||||
const dbPath = '/Users/erishaff/.crystal/sessions.db';
|
||||
const sessionId = 'e48185d1-504a-497d-8893-03c232a6841c';
|
||||
|
||||
const db = new sqlite3.Database(dbPath);
|
||||
|
||||
// Get all responses with token usage
|
||||
const query = `
|
||||
SELECT
|
||||
id,
|
||||
timestamp,
|
||||
json_extract(data, '$.usage.input_tokens') as input_tokens,
|
||||
json_extract(data, '$.usage.cache_creation_input_tokens') as cache_creation,
|
||||
json_extract(data, '$.usage.cache_read_input_tokens') as cache_read,
|
||||
json_extract(data, '$.usage.output_tokens') as output_tokens,
|
||||
json_extract(data, '$.type') as message_type
|
||||
FROM session_outputs
|
||||
WHERE session_id = ?
|
||||
AND type = 'json'
|
||||
AND json_extract(data, '$.usage.input_tokens') >= 0
|
||||
ORDER BY id ASC
|
||||
`;
|
||||
|
||||
db.all(query, [sessionId], (err, rows) => {
|
||||
if (err) {
|
||||
console.error('Error:', err);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('=== Token Usage Analysis for auto-commit-modes-2 ===');
|
||||
console.log('Total responses with usage data:', rows.length);
|
||||
console.log();
|
||||
|
||||
let runningContextSize = 0;
|
||||
let totalOutputTokens = 0;
|
||||
let maxContextSeen = 0;
|
||||
|
||||
console.log('ID\t\tInput\tCache_Cr\tCache_Rd\tOutput\tEst_Context\tType');
|
||||
console.log('-'.repeat(100));
|
||||
|
||||
rows.forEach((row, index) => {
|
||||
const input = parseInt(row.input_tokens) || 0;
|
||||
const cacheCreation = parseInt(row.cache_creation) || 0;
|
||||
const cacheRead = parseInt(row.cache_read) || 0;
|
||||
const output = parseInt(row.output_tokens) || 0;
|
||||
|
||||
// For context estimation: input + previous context + new output
|
||||
// The cache values might represent cumulative context state
|
||||
const estimatedContext = input + cacheCreation + cacheRead;
|
||||
|
||||
runningContextSize += input + output; // Simple running sum
|
||||
totalOutputTokens += output;
|
||||
|
||||
if (estimatedContext > maxContextSeen) {
|
||||
maxContextSeen = estimatedContext;
|
||||
}
|
||||
|
||||
// Show first 10, last 10, and any particularly large context sizes
|
||||
if (index < 10 || index >= rows.length - 10 || estimatedContext > 150000) {
|
||||
console.log(
|
||||
`${row.id}\t\t${input}\t${cacheCreation}\t\t${cacheRead}\t\t${output}\t${estimatedContext}\t\t${row.message_type}`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
console.log('-'.repeat(100));
|
||||
console.log();
|
||||
console.log('=== Summary ===');
|
||||
console.log(`Maximum context size seen: ${maxContextSeen.toLocaleString()} tokens`);
|
||||
console.log(`Running context sum: ${runningContextSize.toLocaleString()} tokens`);
|
||||
console.log(`Total output tokens: ${totalOutputTokens.toLocaleString()} tokens`);
|
||||
console.log(`Expected from Claude error: 188,278 tokens`);
|
||||
console.log();
|
||||
|
||||
// Check the last successful response before the error
|
||||
const lastSuccessful = rows[rows.length - 1];
|
||||
if (lastSuccessful) {
|
||||
const finalInput = parseInt(lastSuccessful.input_tokens) || 0;
|
||||
const finalCacheCreation = parseInt(lastSuccessful.cache_creation) || 0;
|
||||
const finalCacheRead = parseInt(lastSuccessful.cache_read) || 0;
|
||||
const finalEstimate = finalInput + finalCacheCreation + finalCacheRead;
|
||||
|
||||
console.log('=== Last Successful Response Analysis ===');
|
||||
console.log(`Input tokens: ${finalInput.toLocaleString()}`);
|
||||
console.log(`Cache creation: ${finalCacheCreation.toLocaleString()}`);
|
||||
console.log(`Cache read: ${finalCacheRead.toLocaleString()}`);
|
||||
console.log(`Total estimate: ${finalEstimate.toLocaleString()}`);
|
||||
console.log(`Difference from Claude's 188K: ${Math.abs(finalEstimate - 188278).toLocaleString()}`);
|
||||
}
|
||||
|
||||
db.close();
|
||||
});
|
||||
113
error-143-analysis.md
Normal file
113
error-143-analysis.md
Normal file
@ -0,0 +1,113 @@
|
||||
# Analysis of Sessions Exiting with Error Code 143
|
||||
|
||||
## Executive Summary
|
||||
|
||||
Error code 143 (SIGTERM) indicates that Claude Code processes were terminated, likely due to reaching context window limits or being manually stopped. This analysis examines 29 sessions that experienced this error to identify patterns and root causes.
|
||||
|
||||
## Key Findings
|
||||
|
||||
### 1. Model Distribution
|
||||
- **Claude Sonnet 4**: 23 sessions (79.3%)
|
||||
- **Claude Opus 4**: 5 sessions (17.2%)
|
||||
- **Claude Haiku 3.5**: 1 session (3.4%)
|
||||
|
||||
### 2. Token Usage Patterns
|
||||
|
||||
#### High Token Usage Sessions
|
||||
The sessions with the highest token usage before failure:
|
||||
1. **session-inputbox-ux** (Opus 4): 179,717 tokens
|
||||
2. **session-inputbox-ux-2** (Sonnet 4): 164,271 tokens
|
||||
3. **setting-menu** (Opus 4): 163,701 tokens
|
||||
4. **eshaffer-audit** (Sonnet 4): 153,030 tokens
|
||||
5. **auto-commit-modes** (Opus 4): 132,846 tokens
|
||||
|
||||
#### Average Token Usage by Model
|
||||
- **Opus 4**: 137,019 tokens average
|
||||
- **Sonnet 4**: 57,416 tokens average
|
||||
- **Haiku 3.5**: 1,101 tokens average
|
||||
|
||||
### 3. Session Characteristics
|
||||
|
||||
#### Sessions with Most Messages Before Failure
|
||||
1. **eshaffer-audit**: 1,391 messages
|
||||
2. **archive-sessions**: 1,219 messages
|
||||
3. **auto-commit-modes**: 1,018 messages
|
||||
4. **session-fetch-latest**: 946 messages
|
||||
5. **auto-commit-modes-2**: 895 messages
|
||||
|
||||
### 4. Context Window Limits
|
||||
|
||||
All Claude models have a 200,000 token context window. Several sessions show "Prompt is too long" errors, confirming they hit this limit:
|
||||
- **dashboard-ux-2** (Opus 4): 193,451 tokens
|
||||
- **session-inputbox-ux** (Opus 4): 179,717 tokens
|
||||
- **output-terminal-revamp** (Opus 4): 174,054 tokens
|
||||
|
||||
## Root Causes
|
||||
|
||||
### 1. Context Window Exhaustion
|
||||
The primary cause appears to be reaching the 200K token context limit. This is evidenced by:
|
||||
- "Prompt is too long" errors in session outputs
|
||||
- High token counts approaching 200K
|
||||
- Opus 4 sessions failing at higher token counts (uses more tokens per interaction)
|
||||
|
||||
### 2. Long-Running Sessions
|
||||
Sessions with 500+ messages are prone to context exhaustion:
|
||||
- Complex refactoring tasks
|
||||
- UI/UX improvements requiring many iterations
|
||||
- Debugging sessions with extensive back-and-forth
|
||||
|
||||
### 3. Model-Specific Patterns
|
||||
- **Opus 4**: More verbose responses, reaches limits faster
|
||||
- **Sonnet 4**: More efficient token usage but still hits limits in long sessions
|
||||
- **Haiku 3.5**: Rarely hits limits due to concise responses
|
||||
|
||||
## Recommendations
|
||||
|
||||
### 1. Implement Context Window Warnings
|
||||
- Add visual indicators when approaching 80% of context limit
|
||||
- Display real-time token usage in the UI
|
||||
- Suggest starting new sessions at 90% usage
|
||||
|
||||
### 2. Session Management Improvements
|
||||
- Auto-save conversation state before hitting limits
|
||||
- Implement conversation summarization to compress context
|
||||
- Allow easy continuation in new session with compressed history
|
||||
|
||||
### 3. Model Selection Guidance
|
||||
- Recommend Haiku 3.5 for simple tasks to conserve tokens
|
||||
- Suggest Sonnet 4 for most coding tasks (good balance)
|
||||
- Reserve Opus 4 for complex architecture/refactoring
|
||||
|
||||
### 4. Token Tracking Enhancements
|
||||
- Track context window usage separately from total tokens
|
||||
- Show remaining tokens in real-time
|
||||
- Implement predictive warnings based on usage patterns
|
||||
|
||||
## Technical Details
|
||||
|
||||
### Database Schema
|
||||
The analysis uses these tables:
|
||||
- `sessions`: Core session metadata
|
||||
- `session_outputs`: All terminal output including JSON messages
|
||||
- `session_token_summary`: Aggregated token usage per session
|
||||
- `message_token_usage`: Per-message token tracking
|
||||
|
||||
### Error Detection Query
|
||||
```sql
|
||||
SELECT DISTINCT session_id
|
||||
FROM session_outputs
|
||||
WHERE data LIKE '%Claude Code exited with error (exit code: 143)%'
|
||||
```
|
||||
|
||||
### Token Analysis Query
|
||||
```sql
|
||||
SELECT s.id, s.name, s.model, sts.total_tokens
|
||||
FROM sessions s
|
||||
LEFT JOIN session_token_summary sts ON s.id = sts.session_id
|
||||
WHERE s.id IN ([error sessions])
|
||||
ORDER BY sts.total_tokens DESC
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
||||
Error code 143 is primarily caused by context window exhaustion in long-running sessions. Implementing better token tracking, warnings, and session management features will significantly improve the user experience and prevent unexpected session terminations.
|
||||
67
fix_all_token_summaries.sql
Normal file
67
fix_all_token_summaries.sql
Normal file
@ -0,0 +1,67 @@
|
||||
-- Comprehensive script to fix ALL session token summaries
|
||||
-- This removes the incorrect cache token additions and recalculates properly
|
||||
|
||||
-- First, clear all existing incorrect summaries
|
||||
DELETE FROM session_token_summary;
|
||||
DELETE FROM message_token_usage;
|
||||
|
||||
-- Step 1: Recalculate message_token_usage for ALL sessions using ONLY input_tokens and output_tokens
|
||||
-- This excludes the cache tokens that were incorrectly being added
|
||||
INSERT INTO message_token_usage (session_id, output_id, message_type, input_tokens, output_tokens, total_tokens, timestamp)
|
||||
SELECT
|
||||
session_id,
|
||||
id as output_id,
|
||||
CASE
|
||||
WHEN json_extract(data, '$.type') = 'assistant' THEN 'assistant'
|
||||
WHEN json_extract(data, '$.type') = 'user' THEN 'user'
|
||||
ELSE 'system'
|
||||
END as message_type,
|
||||
COALESCE(json_extract(data, '$.usage.input_tokens'), json_extract(data, '$.message.usage.input_tokens'), 0) as input_tokens,
|
||||
COALESCE(json_extract(data, '$.usage.output_tokens'), json_extract(data, '$.message.usage.output_tokens'), 0) as output_tokens,
|
||||
(COALESCE(json_extract(data, '$.usage.input_tokens'), json_extract(data, '$.message.usage.input_tokens'), 0) +
|
||||
COALESCE(json_extract(data, '$.usage.output_tokens'), json_extract(data, '$.message.usage.output_tokens'), 0)) as total_tokens,
|
||||
timestamp
|
||||
FROM session_outputs
|
||||
WHERE type = 'json'
|
||||
AND (json_extract(data, '$.usage.input_tokens') > 0 OR
|
||||
json_extract(data, '$.usage.output_tokens') > 0 OR
|
||||
json_extract(data, '$.message.usage.input_tokens') > 0 OR
|
||||
json_extract(data, '$.message.usage.output_tokens') > 0);
|
||||
|
||||
-- Step 2: Create corrected session token summaries
|
||||
INSERT INTO session_token_summary (session_id, total_input_tokens, total_output_tokens, total_tokens, last_updated)
|
||||
SELECT
|
||||
session_id,
|
||||
SUM(input_tokens) as total_input_tokens,
|
||||
SUM(output_tokens) as total_output_tokens,
|
||||
SUM(total_tokens) as total_tokens,
|
||||
datetime('now') as last_updated
|
||||
FROM message_token_usage
|
||||
GROUP BY session_id;
|
||||
|
||||
-- Show a sample of the corrected data
|
||||
SELECT 'CORRECTED SUMMARIES (sample):' as info;
|
||||
SELECT
|
||||
s.name,
|
||||
sts.total_input_tokens,
|
||||
sts.total_output_tokens,
|
||||
sts.total_tokens,
|
||||
ROUND((sts.total_tokens / 200000.0) * 100, 1) as percentage_if_no_overhead
|
||||
FROM sessions s
|
||||
JOIN session_token_summary sts ON s.id = sts.session_id
|
||||
WHERE sts.total_tokens > 1000 -- Only show sessions with meaningful activity
|
||||
ORDER BY sts.total_tokens DESC
|
||||
LIMIT 10;
|
||||
|
||||
-- Show the specific sessions that were problematic
|
||||
SELECT 'PROBLEMATIC SESSIONS FIXED:' as info;
|
||||
SELECT
|
||||
s.name,
|
||||
sts.total_input_tokens,
|
||||
sts.total_output_tokens,
|
||||
sts.total_tokens,
|
||||
ROUND((sts.total_tokens / 200000.0) * 100, 1) as percentage_if_no_overhead
|
||||
FROM sessions s
|
||||
JOIN session_token_summary sts ON s.id = sts.session_id
|
||||
WHERE s.name IN ('theme-change-4', 'auto-commit-modes-2', 'theme-change-3')
|
||||
ORDER BY s.name;
|
||||
@ -77,34 +77,15 @@ export function CreateSessionDialog({ isOpen, onClose, projectName, projectId }:
|
||||
// Fetch branches if projectId is provided
|
||||
if (projectId) {
|
||||
setIsLoadingBranches(true);
|
||||
// First get the project to get its path
|
||||
API.projects.getAll().then(projectsResponse => {
|
||||
if (!projectsResponse.success || !projectsResponse.data) {
|
||||
throw new Error('Failed to fetch projects');
|
||||
}
|
||||
const project = projectsResponse.data.find((p: any) => p.id === projectId);
|
||||
if (!project) {
|
||||
throw new Error('Project not found');
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
API.projects.listBranches(projectId.toString()),
|
||||
// Get the main branch for this project using its path
|
||||
API.projects.detectBranch(project.path)
|
||||
]);
|
||||
}).then(([branchesResponse, mainBranchResponse]) => {
|
||||
if (branchesResponse.success && branchesResponse.data) {
|
||||
setBranches(branchesResponse.data);
|
||||
API.projects.listBranches(projectId.toString()).then(response => {
|
||||
if (response.success && response.data) {
|
||||
setBranches(response.data);
|
||||
// Set the current branch as default if available
|
||||
const currentBranch = branchesResponse.data.find((b: any) => b.isCurrent);
|
||||
const currentBranch = response.data.find((b: any) => b.isCurrent);
|
||||
if (currentBranch && !formData.baseBranch) {
|
||||
setFormData(prev => ({ ...prev, baseBranch: currentBranch.name }));
|
||||
}
|
||||
}
|
||||
|
||||
if (mainBranchResponse.success && mainBranchResponse.data) {
|
||||
// Main branch detected but not currently used in UI
|
||||
}
|
||||
}).catch((err: any) => {
|
||||
console.error('Failed to fetch branches:', err);
|
||||
}).finally(() => {
|
||||
@ -512,7 +493,6 @@ export function CreateSessionDialog({ isOpen, onClose, projectName, projectId }:
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
Create the new session branch from this existing branch
|
||||
</p>
|
||||
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { ChevronRight, ChevronDown, Folder as FolderIcon, FolderOpen, Plus, Settings, GripVertical, Archive, GitBranch, RefreshCw } from 'lucide-react';
|
||||
import { ChevronRight, ChevronDown, Folder as FolderIcon, FolderOpen, Plus, Settings, GripVertical, Archive, RefreshCw } from 'lucide-react';
|
||||
import { useSessionStore } from '../stores/sessionStore';
|
||||
import { useErrorStore } from '../stores/errorStore';
|
||||
import { useNavigationStore } from '../stores/navigationStore';
|
||||
import { SessionListItem } from './SessionListItem';
|
||||
import { CreateSessionDialog } from './CreateSessionDialog';
|
||||
import { MainBranchWarningDialog } from './MainBranchWarningDialog';
|
||||
import ProjectSettings from './ProjectSettings';
|
||||
import { EmptyState } from './EmptyState';
|
||||
import { LoadingSpinner } from './LoadingSpinner';
|
||||
@ -48,6 +49,9 @@ export function DraggableProjectTreeView() {
|
||||
const activeSessionId = useSessionStore((state) => state.activeSessionId);
|
||||
const { setActiveSession } = useSessionStore();
|
||||
const { navigateToSessions } = useNavigationStore();
|
||||
const [showMainBranchWarning, setShowMainBranchWarning] = useState(false);
|
||||
const [pendingMainBranchProject, setPendingMainBranchProject] = useState<Project | null>(null);
|
||||
const [detectedMainBranch, setDetectedMainBranch] = useState<string>('main');
|
||||
const [detectedBranchForNewProject, setDetectedBranchForNewProject] = useState<string | null>(null);
|
||||
|
||||
// Track recent sessions to handle auto-selection for multiple session creation
|
||||
@ -61,9 +65,6 @@ export function DraggableProjectTreeView() {
|
||||
const [selectedProjectForFolder, setSelectedProjectForFolder] = useState<Project | null>(null);
|
||||
const [newFolderName, setNewFolderName] = useState('');
|
||||
const [parentFolderForCreate, setParentFolderForCreate] = useState<Folder | null>(null);
|
||||
const [showFolderContextMenu, setShowFolderContextMenu] = useState(false);
|
||||
const [folderContextMenuPosition, setFolderContextMenuPosition] = useState({ x: 0, y: 0 });
|
||||
const [selectedFolderForMenu, setSelectedFolderForMenu] = useState<Folder | null>(null);
|
||||
const { showError } = useErrorStore();
|
||||
|
||||
// Folder rename state
|
||||
@ -483,28 +484,6 @@ export function DraggableProjectTreeView() {
|
||||
setEditingFolderName(folder.name);
|
||||
};
|
||||
|
||||
const handleFolderContextMenu = (e: React.MouseEvent, folder: Folder, _projectId: number) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setSelectedFolderForMenu(folder);
|
||||
setFolderContextMenuPosition({ x: e.clientX, y: e.clientY });
|
||||
setShowFolderContextMenu(true);
|
||||
};
|
||||
|
||||
const closeFolderContextMenu = () => {
|
||||
setShowFolderContextMenu(false);
|
||||
setSelectedFolderForMenu(null);
|
||||
};
|
||||
|
||||
// Close folder context menu when clicking outside
|
||||
useEffect(() => {
|
||||
if (showFolderContextMenu) {
|
||||
const handleClick = () => closeFolderContextMenu();
|
||||
document.addEventListener('click', handleClick);
|
||||
return () => document.removeEventListener('click', handleClick);
|
||||
}
|
||||
}, [showFolderContextMenu]);
|
||||
|
||||
const handleSaveFolderEdit = async () => {
|
||||
if (!editingFolderId || !editingFolderName.trim()) {
|
||||
setEditingFolderId(null);
|
||||
@ -530,7 +509,6 @@ export function DraggableProjectTreeView() {
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Failed to rename folder:', error);
|
||||
showError({
|
||||
title: 'Failed to rename folder',
|
||||
error: error.message || 'Unknown error occurred'
|
||||
@ -717,9 +695,31 @@ export function DraggableProjectTreeView() {
|
||||
};
|
||||
|
||||
const handleProjectClick = async (project: Project) => {
|
||||
// Navigate to the project dashboard
|
||||
const { navigateToProject } = useNavigationStore.getState();
|
||||
navigateToProject(project.id);
|
||||
// Check if we should show the warning
|
||||
const warningKey = `mainBranchWarning_${project.id}`;
|
||||
const hasShownWarning = localStorage.getItem(warningKey);
|
||||
|
||||
if (!hasShownWarning) {
|
||||
// Fetch the current branch before showing warning
|
||||
try {
|
||||
const response = await window.electronAPI.git.detectBranch(project.path);
|
||||
if (response.success && response.data) {
|
||||
setDetectedMainBranch(response.data);
|
||||
} else {
|
||||
setDetectedMainBranch('main');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to detect branch:', error);
|
||||
setDetectedMainBranch('main');
|
||||
}
|
||||
|
||||
// Show warning dialog
|
||||
setPendingMainBranchProject(project);
|
||||
setShowMainBranchWarning(true);
|
||||
} else {
|
||||
// Proceed directly
|
||||
await openMainRepoSession(project);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRefreshProjectGitStatus = async (project: Project, e: React.MouseEvent) => {
|
||||
@ -756,6 +756,31 @@ export function DraggableProjectTreeView() {
|
||||
}
|
||||
};
|
||||
|
||||
const openMainRepoSession = async (project: Project) => {
|
||||
try {
|
||||
// Get or create the main repo session
|
||||
const response = await API.sessions.getOrCreateMainRepoSession(project.id);
|
||||
|
||||
if (response.success && response.data) {
|
||||
// Navigate to the main repo session
|
||||
const session = response.data;
|
||||
useSessionStore.getState().setActiveSession(session.id);
|
||||
|
||||
// Don't expand the project - main repo sessions are accessed via folder click only
|
||||
} else {
|
||||
showError({
|
||||
title: 'Failed to open main repository session',
|
||||
error: response.error || 'Unknown error occurred'
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error handling project click:', error);
|
||||
showError({
|
||||
title: 'Failed to open main repository session',
|
||||
error: error.message || 'Unknown error occurred'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleCreateSession = (project: Project) => {
|
||||
// Just show the dialog for any project
|
||||
@ -1277,9 +1302,9 @@ export function DraggableProjectTreeView() {
|
||||
const renderFolder = (folder: Folder, project: ProjectWithSessions, level: number = 0, isLastInLevel: boolean = false, parentPath: boolean[] = []) => {
|
||||
const isExpanded = expandedFolders.has(folder.id);
|
||||
const folderSessions = project.sessions.filter(s => s.folderId === folder.id);
|
||||
const folderUnviewedCount = folderSessions.filter(s => s.status === 'completed_unviewed').length;
|
||||
const isDraggingOverFolder = dragState.overType === 'folder' && dragState.overFolderId === folder.id;
|
||||
const hasChildren = (folder.children && folder.children.length > 0) || folderSessions.length > 0;
|
||||
const folderUnviewedCount = folderSessions.filter(s => s.status === 'completed_unviewed').length;
|
||||
|
||||
return (
|
||||
<div key={folder.id} className="relative" style={{ marginLeft: `${level * 16}px` }}>
|
||||
@ -1330,7 +1355,7 @@ export function DraggableProjectTreeView() {
|
||||
)}
|
||||
</div>
|
||||
<div
|
||||
className={`relative group/folder flex items-center space-x-1 py-1 rounded cursor-pointer transition-colors hover:bg-gray-100 dark:hover:bg-gray-700 ${
|
||||
className={`group/folder flex items-center space-x-1 px-2 py-1 rounded cursor-pointer transition-colors hover:bg-gray-100 dark:hover:bg-gray-700 ${
|
||||
isDraggingOverFolder ? 'bg-blue-100 dark:bg-blue-900' : ''
|
||||
}`}
|
||||
style={{ marginLeft: `${0}px`, paddingLeft: '8px', paddingRight: '8px' }}
|
||||
@ -1340,7 +1365,6 @@ export function DraggableProjectTreeView() {
|
||||
onDrop={(e) => handleFolderDrop(e, folder, project.id)}
|
||||
onDragEnter={handleDragEnter}
|
||||
onDragLeave={handleDragLeave}
|
||||
onContextMenu={(e) => handleFolderContextMenu(e, folder, project.id)}
|
||||
>
|
||||
<div className="opacity-0 group-hover/folder:opacity-100 transition-opacity cursor-move">
|
||||
<GripVertical className="w-3 h-3 text-gray-400" />
|
||||
@ -1372,9 +1396,9 @@ export function DraggableProjectTreeView() {
|
||||
}}
|
||||
>
|
||||
{isExpanded ? (
|
||||
<FolderOpen className="w-4 h-4 text-blue-600 dark:text-blue-400 flex-shrink-0" />
|
||||
<FolderOpen className="w-4 h-4 text-yellow-600 dark:text-yellow-400 flex-shrink-0" />
|
||||
) : (
|
||||
<FolderIcon className="w-4 h-4 text-blue-600 dark:text-blue-400 flex-shrink-0" />
|
||||
<FolderIcon className="w-4 h-4 text-gray-600 dark:text-gray-400 flex-shrink-0" />
|
||||
)}
|
||||
{editingFolderId === folder.id ? (
|
||||
<input
|
||||
@ -1397,7 +1421,7 @@ export function DraggableProjectTreeView() {
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
<span className="text-sm text-gray-700 dark:text-gray-300 truncate" title={folder.name}>
|
||||
<span className="text-sm text-gray-700 dark:text-gray-300 truncate">
|
||||
{folder.name}
|
||||
</span>
|
||||
<span className="text-xs text-gray-500 dark:text-gray-500">
|
||||
@ -1441,27 +1465,37 @@ export function DraggableProjectTreeView() {
|
||||
</div>
|
||||
|
||||
{isExpanded && hasChildren && (
|
||||
<div className="mt-1 space-y-1" style={{ marginLeft: '16px' }}>
|
||||
<div className="ml-4 mt-1 space-y-1">
|
||||
{/* Render subfolders first */}
|
||||
{folder.children && folder.children.map((childFolder, index, array) => {
|
||||
const isLastChild = index === array.length - 1 && folderSessions.length === 0;
|
||||
const newParentPath = [...parentPath, !isLastChild];
|
||||
return renderFolder(childFolder, project, level + 1, isLastChild, newParentPath);
|
||||
{folder.children && folder.children.map((childFolder, childIndex) => {
|
||||
const isLastChild = childIndex === folder.children!.length - 1;
|
||||
const hasSessionsBelow = folderSessions.length > 0;
|
||||
const newParentPath = [...parentPath, !isLastInLevel || hasSessionsBelow];
|
||||
return renderFolder(childFolder, project, level + 1, isLastChild && !hasSessionsBelow, newParentPath);
|
||||
})}
|
||||
|
||||
{/* Then render sessions in this folder */}
|
||||
{folderSessions
|
||||
.sort((a, b) => (a.displayOrder ?? 0) - (b.displayOrder ?? 0))
|
||||
.map((session, sessionIndex, sessionArray) => {
|
||||
.map((session, sessionIndex) => {
|
||||
const isDraggingOverSession = dragState.overType === 'session' &&
|
||||
dragState.overSessionId === session.id &&
|
||||
dragState.overProjectId === project.id;
|
||||
const isLastSession = sessionIndex === sessionArray.length - 1;
|
||||
const isLastSession = sessionIndex === folderSessions.length - 1;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={session.id}
|
||||
className="relative"
|
||||
className={`group flex items-center ${
|
||||
isDraggingOverSession ? 'bg-blue-100 dark:bg-blue-900 rounded' : ''
|
||||
}`}
|
||||
draggable
|
||||
onDragStart={(e) => handleSessionDragStart(e, session, project.id)}
|
||||
onDragEnd={handleDragEnd}
|
||||
onDragOver={(e) => handleSessionDragOver(e, session, project.id)}
|
||||
onDrop={(e) => handleSessionDrop(e, session, project.id)}
|
||||
onDragEnter={handleDragEnter}
|
||||
onDragLeave={handleDragLeave}
|
||||
>
|
||||
{/* Tree lines for sessions */}
|
||||
<div className="absolute inset-0 pointer-events-none">
|
||||
@ -1537,13 +1571,13 @@ export function DraggableProjectTreeView() {
|
||||
{projectsWithSessions.map((project) => {
|
||||
const isExpanded = expandedProjects.has(project.id);
|
||||
const sessionCount = project.sessions.length;
|
||||
const isDraggingOver = dragState.overType === 'project' && dragState.overProjectId === project.id;
|
||||
const unviewedCount = project.sessions.filter(s => s.status === 'completed_unviewed').length;
|
||||
const isDraggingOver = dragState.overType === 'project' && dragState.overProjectId === project.id;
|
||||
|
||||
return (
|
||||
<div key={project.id} className="mb-1">
|
||||
<div key={project.id}>
|
||||
<div
|
||||
className={`group flex items-center space-x-1 px-2 py-2 rounded-lg cursor-pointer transition-colors bg-gray-50 dark:bg-gray-800/50 hover:bg-gray-100 dark:hover:bg-gray-700 ${
|
||||
className={`group flex items-center space-x-1 px-2 py-1.5 rounded cursor-pointer transition-colors hover:bg-gray-100 dark:hover:bg-gray-700 ${
|
||||
isDraggingOver ? 'bg-blue-100 dark:bg-blue-900' : ''
|
||||
}`}
|
||||
draggable
|
||||
@ -1558,32 +1592,35 @@ export function DraggableProjectTreeView() {
|
||||
<GripVertical className="w-3 h-3 text-gray-400" />
|
||||
</div>
|
||||
|
||||
{(sessionCount > 0 || (project.folders && project.folders.length > 0)) ? (
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
toggleProject(project.id);
|
||||
}}
|
||||
className="p-0.5 hover:bg-gray-200 dark:hover:bg-gray-600 rounded transition-colors"
|
||||
>
|
||||
{isExpanded ? (
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
toggleProject(project.id);
|
||||
}}
|
||||
className="p-0.5 hover:bg-gray-200 dark:hover:bg-gray-600 rounded transition-colors"
|
||||
disabled={sessionCount === 0}
|
||||
>
|
||||
{sessionCount > 0 ? (
|
||||
isExpanded ? (
|
||||
<ChevronDown className="w-3 h-3 text-gray-600 dark:text-gray-400" />
|
||||
) : (
|
||||
<ChevronRight className="w-3 h-3 text-gray-600 dark:text-gray-400" />
|
||||
)}
|
||||
</button>
|
||||
) : (
|
||||
<div className="w-3 h-3 p-0.5" />
|
||||
)}
|
||||
)
|
||||
) : (
|
||||
<div className="w-3 h-3" />
|
||||
)}
|
||||
</button>
|
||||
|
||||
<div
|
||||
className="flex items-center space-x-2 flex-1 min-w-0"
|
||||
onClick={() => handleProjectClick(project)}
|
||||
>
|
||||
<div className="relative" title="Git-backed project (connected to repository)">
|
||||
<GitBranch className="w-4 h-4 text-blue-600 dark:text-blue-400 flex-shrink-0" />
|
||||
</div>
|
||||
<span className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate text-left" title={project.name}>
|
||||
{isExpanded ? (
|
||||
<FolderOpen className="w-4 h-4 text-blue-600 dark:text-blue-400 flex-shrink-0" />
|
||||
) : (
|
||||
<FolderIcon className="w-4 h-4 text-gray-600 dark:text-gray-400 flex-shrink-0" />
|
||||
)}
|
||||
<span className="text-sm font-medium text-gray-800 dark:text-gray-200 truncate text-left">
|
||||
{project.name}
|
||||
</span>
|
||||
{unviewedCount > 0 && (
|
||||
@ -1598,7 +1635,7 @@ export function DraggableProjectTreeView() {
|
||||
e.stopPropagation();
|
||||
handleCreateSession(project);
|
||||
}}
|
||||
className="flex items-center gap-1 px-2 py-1 text-xs font-medium text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-200 dark:hover:bg-gray-600 rounded transition-all opacity-0 group-hover:opacity-100"
|
||||
className="flex items-center gap-1 px-2 py-1 text-xs font-medium text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-200 dark:hover:bg-gray-600 rounded transition-colors opacity-100"
|
||||
>
|
||||
<Plus className="w-3 h-3" />
|
||||
<span>New Session</span>
|
||||
@ -1637,29 +1674,38 @@ export function DraggableProjectTreeView() {
|
||||
{/* Render folders using recursive structure */}
|
||||
{project.folders && (() => {
|
||||
const folderTree = buildFolderTree(project.folders);
|
||||
const rootSessions = project.sessions.filter(s => !s.folderId);
|
||||
return folderTree.map((folder, index) => {
|
||||
const isLastFolder = index === folderTree.length - 1 && rootSessions.length === 0;
|
||||
return renderFolder(folder, project, 1, isLastFolder, [!isLastFolder]);
|
||||
return folderTree.map((folder, folderIndex) => {
|
||||
const isLastFolder = folderIndex === folderTree.length - 1;
|
||||
const hasSessionsAtRoot = project.sessions.some(s => !s.folderId);
|
||||
return renderFolder(folder, project, 0, isLastFolder && !hasSessionsAtRoot, []);
|
||||
});
|
||||
})()}
|
||||
|
||||
{/* Render sessions without folders */}
|
||||
<div className="relative">
|
||||
<div className="ml-4">
|
||||
{project.sessions
|
||||
.filter(s => !s.folderId)
|
||||
.sort((a, b) => (a.displayOrder ?? 0) - (b.displayOrder ?? 0))
|
||||
.map((session, sessionIndex, sessionArray) => {
|
||||
.map((session, sessionIndex, sessionsArray) => {
|
||||
const isDraggingOverSession = dragState.overType === 'session' &&
|
||||
dragState.overSessionId === session.id &&
|
||||
dragState.overProjectId === project.id;
|
||||
const isLastSession = sessionIndex === sessionArray.length - 1;
|
||||
const isLastSession = sessionIndex === sessionsArray.length - 1;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={session.id}
|
||||
className="relative"
|
||||
className={`relative group flex items-center ${
|
||||
isDraggingOverSession ? 'bg-blue-100 dark:bg-blue-900 rounded' : ''
|
||||
}`}
|
||||
style={{ marginLeft: '16px' }}
|
||||
draggable
|
||||
onDragStart={(e) => handleSessionDragStart(e, session, project.id)}
|
||||
onDragEnd={handleDragEnd}
|
||||
onDragOver={(e) => handleSessionDragOver(e, session, project.id)}
|
||||
onDrop={(e) => handleSessionDrop(e, session, project.id)}
|
||||
onDragEnter={handleDragEnter}
|
||||
onDragLeave={handleDragLeave}
|
||||
>
|
||||
{/* Tree lines for root sessions */}
|
||||
<div className="absolute inset-0 pointer-events-none">
|
||||
@ -1710,7 +1756,7 @@ export function DraggableProjectTreeView() {
|
||||
</div>
|
||||
|
||||
{/* Add folder button */}
|
||||
<div className="ml-6 mt-2 border-t border-gray-200 dark:border-gray-700 pt-2">
|
||||
<div className="ml-4">
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
@ -1730,15 +1776,13 @@ export function DraggableProjectTreeView() {
|
||||
);
|
||||
})}
|
||||
|
||||
<div className="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700">
|
||||
<button
|
||||
onClick={() => setShowAddProjectDialog(true)}
|
||||
className="w-full px-2 py-1.5 text-sm text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 rounded transition-colors flex items-center justify-center space-x-2"
|
||||
>
|
||||
<Plus className="w-4 h-4" />
|
||||
<span>New Project</span>
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setShowAddProjectDialog(true)}
|
||||
className="w-full mt-2 px-2 py-1.5 text-sm text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 rounded transition-colors flex items-center justify-center space-x-2"
|
||||
>
|
||||
<Plus className="w-4 h-4" />
|
||||
<span>New Project</span>
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
@ -1801,10 +1845,7 @@ export function DraggableProjectTreeView() {
|
||||
<div
|
||||
key={session.id}
|
||||
className="cursor-pointer"
|
||||
onClick={() => {
|
||||
useSessionStore.getState().setActiveSession(session.id);
|
||||
useNavigationStore.getState().navigateToSessions();
|
||||
}}
|
||||
onClick={() => useSessionStore.getState().setActiveSession(session.id)}
|
||||
>
|
||||
<SessionListItem
|
||||
session={session}
|
||||
@ -1980,6 +2021,26 @@ export function DraggableProjectTreeView() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Main Branch Warning Dialog */}
|
||||
{pendingMainBranchProject && (
|
||||
<MainBranchWarningDialog
|
||||
isOpen={showMainBranchWarning}
|
||||
onClose={() => {
|
||||
setShowMainBranchWarning(false);
|
||||
setPendingMainBranchProject(null);
|
||||
}}
|
||||
onContinue={() => {
|
||||
setShowMainBranchWarning(false);
|
||||
if (pendingMainBranchProject) {
|
||||
openMainRepoSession(pendingMainBranchProject);
|
||||
}
|
||||
setPendingMainBranchProject(null);
|
||||
}}
|
||||
projectName={pendingMainBranchProject.name}
|
||||
projectId={pendingMainBranchProject.id}
|
||||
mainBranch={detectedMainBranch}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Create Folder Dialog */}
|
||||
{showCreateFolderDialog && selectedProjectForFolder && (
|
||||
@ -2011,24 +2072,6 @@ export function DraggableProjectTreeView() {
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
Suggested Folder Types
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
{['Features', 'Bugs', 'Exploration', 'Refactoring', 'Tests', 'Documentation'].map((suggestion) => (
|
||||
<button
|
||||
key={suggestion}
|
||||
type="button"
|
||||
onClick={() => setNewFolderName(suggestion)}
|
||||
className="px-3 py-1.5 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 rounded-md transition-colors"
|
||||
>
|
||||
{suggestion}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex justify-end space-x-3">
|
||||
@ -2054,57 +2097,6 @@ export function DraggableProjectTreeView() {
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Folder Context Menu */}
|
||||
{showFolderContextMenu && selectedFolderForMenu && (
|
||||
<div
|
||||
className="fixed bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-md shadow-lg py-1 z-50 min-w-[150px]"
|
||||
style={{ top: folderContextMenuPosition.y, left: folderContextMenuPosition.x }}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<button
|
||||
onClick={() => {
|
||||
closeFolderContextMenu();
|
||||
handleStartFolderEdit(selectedFolderForMenu);
|
||||
}}
|
||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-white"
|
||||
>
|
||||
Rename
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
closeFolderContextMenu();
|
||||
// Find the project that contains this folder
|
||||
const project = projectsWithSessions.find(p =>
|
||||
p.folders?.some(f => f.id === selectedFolderForMenu.id)
|
||||
);
|
||||
if (project) {
|
||||
setSelectedProjectForCreate(project);
|
||||
setShowCreateDialog(true);
|
||||
}
|
||||
}}
|
||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-white"
|
||||
>
|
||||
New Session Here
|
||||
</button>
|
||||
<div className="border-t border-gray-200 dark:border-gray-700 my-1" />
|
||||
<button
|
||||
onClick={() => {
|
||||
closeFolderContextMenu();
|
||||
// Find the project that contains this folder
|
||||
const project = projectsWithSessions.find(p =>
|
||||
p.folders?.some(f => f.id === selectedFolderForMenu.id)
|
||||
);
|
||||
if (project) {
|
||||
handleDeleteFolder(selectedFolderForMenu, project.id);
|
||||
}
|
||||
}}
|
||||
className="w-full text-left px-4 py-2 text-sm text-red-600 dark:text-red-400 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-red-700 dark:hover:text-red-300"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@ -1,337 +1,17 @@
|
||||
import React, { useState, useEffect, useCallback, useMemo } from 'react';
|
||||
import { RefreshCw, GitBranch, AlertCircle, CheckCircle, GitPullRequest, Loader2, XCircle, Filter } from 'lucide-react';
|
||||
import { API } from '../utils/api';
|
||||
import type { ProjectDashboardData, SessionBranchInfo } from '../types/projectDashboard';
|
||||
import { formatDistanceToNow } from '../utils/timestampUtils';
|
||||
import { dashboardCache } from '../utils/dashboardCache';
|
||||
import { debounce } from '../utils/debounce';
|
||||
import { ProjectDashboardSkeleton } from './ProjectDashboardSkeleton';
|
||||
import { useSessionStore } from '../stores/sessionStore';
|
||||
import { useNavigationStore } from '../stores/navigationStore';
|
||||
import { MultiOriginStatus } from './dashboard/MultiOriginStatus';
|
||||
import { StatusSummaryCards } from './dashboard/StatusSummaryCards';
|
||||
import React from 'react';
|
||||
|
||||
interface ProjectDashboardProps {
|
||||
projectId: number;
|
||||
projectName: string;
|
||||
}
|
||||
|
||||
export const ProjectDashboard: React.FC<ProjectDashboardProps> = React.memo(({ projectId, projectName }) => {
|
||||
const [dashboardData, setDashboardData] = useState<ProjectDashboardData | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [lastRefreshTime, setLastRefreshTime] = useState<Date | null>(null);
|
||||
const [filterType, setFilterType] = useState<'all' | 'stale' | 'changes' | 'pr'>('all');
|
||||
|
||||
const fetchDashboardData = useCallback(async (useCache: boolean = true) => {
|
||||
// Check cache first if not forcing refresh
|
||||
if (useCache) {
|
||||
const cachedData = dashboardCache.get(projectId);
|
||||
if (cachedData) {
|
||||
setDashboardData(cachedData);
|
||||
setLastRefreshTime(new Date(Date.now() - 30000)); // Show it was from cache
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setIsLoading(!dashboardData); // Only show loading on initial load
|
||||
setIsRefreshing(!!dashboardData); // Show refreshing if we already have data
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const response = await API.dashboard.getProjectStatus(projectId);
|
||||
|
||||
if (response.success && response.data) {
|
||||
setDashboardData(response.data);
|
||||
setLastRefreshTime(new Date());
|
||||
// Cache the data
|
||||
dashboardCache.set(projectId, response.data);
|
||||
} else {
|
||||
setError(response.error || 'Failed to fetch project status');
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Unknown error occurred');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setIsRefreshing(false);
|
||||
}
|
||||
}, [projectId, dashboardData]);
|
||||
|
||||
// Debounced refresh function
|
||||
const debouncedRefresh = useMemo(
|
||||
() => debounce(() => {
|
||||
dashboardCache.invalidate(projectId);
|
||||
fetchDashboardData(false);
|
||||
}, 500),
|
||||
[projectId, fetchDashboardData]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// Clear previous data when switching projects to show skeleton
|
||||
setDashboardData(null);
|
||||
setError(null);
|
||||
fetchDashboardData();
|
||||
}, [projectId]); // Only refetch when projectId changes
|
||||
|
||||
// Memoize grouped sessions by base branch (for future use)
|
||||
// const groupedSessions = useMemo(() => {
|
||||
// if (!dashboardData) return new Map();
|
||||
//
|
||||
// const groups = new Map<string, SessionBranchInfo[]>();
|
||||
// dashboardData.sessionBranches.forEach(session => {
|
||||
// const baseBranch = session.baseBranch || 'unknown';
|
||||
// if (!groups.has(baseBranch)) {
|
||||
// groups.set(baseBranch, []);
|
||||
// }
|
||||
// groups.get(baseBranch)!.push(session);
|
||||
// });
|
||||
//
|
||||
// return groups;
|
||||
// }, [dashboardData?.sessionBranches]);
|
||||
|
||||
const renderSessionRow = useCallback((session: SessionBranchInfo) => {
|
||||
const staleClass = session.isStale ? 'bg-yellow-50 dark:bg-yellow-900/20' : '';
|
||||
|
||||
const handleSessionClick = async () => {
|
||||
console.log('[ProjectDashboard] Session clicked:', session.sessionId, session.sessionName);
|
||||
try {
|
||||
await useSessionStore.getState().setActiveSession(session.sessionId);
|
||||
console.log('[ProjectDashboard] Session set, now navigating to sessions view');
|
||||
useNavigationStore.getState().navigateToSessions();
|
||||
console.log('[ProjectDashboard] Navigation completed');
|
||||
} catch (error) {
|
||||
console.error('[ProjectDashboard] Error in handleSessionClick:', error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<tr key={session.sessionId} className={`hover:bg-gray-50 dark:hover:bg-gray-800 ${staleClass} cursor-pointer`} onClick={handleSessionClick}>
|
||||
<td className="px-4 py-3 text-sm">
|
||||
<div className="flex items-center gap-2">
|
||||
<GitBranch className="w-4 h-4 text-gray-400" />
|
||||
<div>
|
||||
<div className="font-medium text-gray-900 dark:text-gray-100">{session.sessionName}</div>
|
||||
<div className="text-gray-500 dark:text-gray-400 text-xs">{session.branchName}</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm text-gray-700 dark:text-gray-300">
|
||||
<code className="text-xs bg-gray-100 dark:bg-gray-800 px-1 py-0.5 rounded">
|
||||
{session.baseBranch}
|
||||
</code>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm">
|
||||
{session.isStale ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<AlertCircle className="w-4 h-4 text-yellow-600" />
|
||||
<span className="text-yellow-700 dark:text-yellow-400">
|
||||
Stale {session.staleSince && `since ${formatDistanceToNow(session.staleSince)}`}
|
||||
</span>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center gap-2">
|
||||
<CheckCircle className="w-4 h-4 text-green-500" />
|
||||
<span className="text-green-700 dark:text-green-400">Current</span>
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm text-gray-700 dark:text-gray-300">
|
||||
<div className="flex gap-3 text-xs">
|
||||
{session.commitsAhead > 0 && (
|
||||
<span className="text-blue-600 dark:text-blue-400">+{session.commitsAhead}</span>
|
||||
)}
|
||||
{session.commitsBehind > 0 && (
|
||||
<span className="text-orange-600 dark:text-orange-400">-{session.commitsBehind}</span>
|
||||
)}
|
||||
{session.commitsAhead === 0 && session.commitsBehind === 0 && (
|
||||
<span className="text-gray-400">โ</span>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm">
|
||||
{session.hasUncommittedChanges && (
|
||||
<span className="inline-flex items-center gap-1 text-amber-600 dark:text-amber-400">
|
||||
<AlertCircle className="w-3 h-3" />
|
||||
<span className="text-xs">Uncommitted</span>
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm">
|
||||
{session.pullRequest ? (
|
||||
<a
|
||||
href={session.pullRequest.url}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="inline-flex items-center gap-1 text-blue-600 hover:text-blue-800 dark:text-blue-400"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<GitPullRequest className="w-4 h-4" />
|
||||
<span>#{session.pullRequest.number}</span>
|
||||
{session.pullRequest.state === 'open' && (
|
||||
<span className="inline-block w-2 h-2 bg-green-500 rounded-full"></span>
|
||||
)}
|
||||
{session.pullRequest.state === 'closed' && (
|
||||
<XCircle className="w-3 h-3 text-red-500" />
|
||||
)}
|
||||
{session.pullRequest.state === 'merged' && (
|
||||
<CheckCircle className="w-3 h-3 text-purple-500" />
|
||||
)}
|
||||
</a>
|
||||
) : (
|
||||
<span className="text-gray-400">โ</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}, []);
|
||||
|
||||
// Filter sessions based on selected filter
|
||||
const filteredSessions = useMemo(() => {
|
||||
if (!dashboardData) return [];
|
||||
|
||||
switch (filterType) {
|
||||
case 'stale':
|
||||
return dashboardData.sessionBranches.filter(s => s.isStale);
|
||||
case 'changes':
|
||||
return dashboardData.sessionBranches.filter(s => s.hasUncommittedChanges);
|
||||
case 'pr':
|
||||
return dashboardData.sessionBranches.filter(s => s.pullRequest);
|
||||
default:
|
||||
return dashboardData.sessionBranches;
|
||||
}
|
||||
}, [dashboardData, filterType]);
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="p-6 bg-red-50 dark:bg-red-900/20 rounded-lg">
|
||||
<div className="flex items-center gap-2 text-red-700 dark:text-red-400">
|
||||
<AlertCircle className="w-5 h-5" />
|
||||
<span className="font-medium">Error loading dashboard</span>
|
||||
</div>
|
||||
<p className="mt-1 text-sm text-red-600 dark:text-red-300">{error}</p>
|
||||
<button
|
||||
onClick={() => fetchDashboardData(false)}
|
||||
className="mt-3 px-3 py-1 text-sm bg-red-600 text-white rounded hover:bg-red-700"
|
||||
>
|
||||
Try Again
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (isLoading && !dashboardData) {
|
||||
return <ProjectDashboardSkeleton />;
|
||||
}
|
||||
|
||||
export const ProjectDashboard: React.FC<ProjectDashboardProps> = ({ projectId, projectName }) => {
|
||||
return (
|
||||
<div className="bg-white dark:bg-gray-900 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 flex flex-col h-full">
|
||||
<div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100">
|
||||
Project Dashboard
|
||||
</h2>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
Git status for {dashboardData?.projectName || projectName}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
{lastRefreshTime && (
|
||||
<span className="text-xs text-gray-500 dark:text-gray-400">
|
||||
Updated {formatDistanceToNow(lastRefreshTime)}
|
||||
</span>
|
||||
)}
|
||||
<button
|
||||
onClick={debouncedRefresh}
|
||||
disabled={isLoading || isRefreshing}
|
||||
className="inline-flex items-center gap-2 px-3 py-1.5 text-sm bg-gray-100 dark:bg-gray-800
|
||||
text-gray-700 dark:text-gray-300 rounded-md hover:bg-gray-200 dark:hover:bg-gray-700
|
||||
disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
{(isLoading || isRefreshing) ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
)}
|
||||
Refresh
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{dashboardData ? (
|
||||
<div className="p-6 flex-1 flex flex-col overflow-hidden">
|
||||
{/* Multi-Origin Status */}
|
||||
<MultiOriginStatus
|
||||
mainBranch={dashboardData.mainBranch}
|
||||
mainBranchStatus={dashboardData.mainBranchStatus}
|
||||
remotes={dashboardData.remotes}
|
||||
onReviewUpdates={() => console.log('Review updates clicked')}
|
||||
/>
|
||||
|
||||
{/* Status Summary Cards */}
|
||||
<StatusSummaryCards sessions={dashboardData.sessionBranches} />
|
||||
|
||||
{/* Session Branches Table */}
|
||||
{dashboardData.sessionBranches.length > 0 ? (
|
||||
<div className="flex-1 flex flex-col overflow-hidden">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<h3 className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
Session Branches ({filteredSessions.length} of {dashboardData.sessionBranches.length})
|
||||
</h3>
|
||||
<div className="flex items-center gap-2">
|
||||
<Filter className="w-4 h-4 text-gray-400" />
|
||||
<select
|
||||
value={filterType}
|
||||
onChange={(e) => setFilterType(e.target.value as typeof filterType)}
|
||||
className="text-sm border border-gray-300 dark:border-gray-600 rounded px-2 py-1 bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
<option value="all">All Sessions</option>
|
||||
<option value="stale">Stale Only</option>
|
||||
<option value="changes">With Changes</option>
|
||||
<option value="pr">With PR</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex-1 overflow-x-auto overflow-y-auto border border-gray-200 dark:border-gray-700 rounded-lg">
|
||||
<table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800">
|
||||
<tr>
|
||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Session
|
||||
</th>
|
||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Base Branch
|
||||
</th>
|
||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Commits
|
||||
</th>
|
||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Changes
|
||||
</th>
|
||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Pull Request
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="bg-white dark:bg-gray-900 divide-y divide-gray-200 dark:divide-gray-700">
|
||||
{filteredSessions.map(renderSessionRow)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-8 text-gray-500 dark:text-gray-400">
|
||||
<GitBranch className="w-12 h-12 mx-auto mb-3 text-gray-300 dark:text-gray-600" />
|
||||
<p>No active session branches</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
<div className="p-4">
|
||||
<h3 className="text-lg font-semibold mb-4">{projectName} Dashboard</h3>
|
||||
<p className="text-gray-600 dark:text-gray-400">
|
||||
Dashboard for project "{projectName}" (ID: {projectId}) is under development.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
};
|
||||
@ -1,86 +0,0 @@
|
||||
import React from 'react';
|
||||
|
||||
export const ProjectDashboardSkeleton: React.FC = () => {
|
||||
return (
|
||||
<div className="bg-white dark:bg-gray-900 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 animate-pulse">
|
||||
{/* Header Skeleton */}
|
||||
<div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="h-6 w-48 bg-gray-200 dark:bg-gray-700 rounded mb-2"></div>
|
||||
<div className="h-4 w-32 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="h-4 w-24 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
<div className="h-8 w-20 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="p-6">
|
||||
{/* Main Branch Status Skeleton */}
|
||||
<div className="mb-6 p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-5 h-5 bg-gray-300 dark:bg-gray-600 rounded"></div>
|
||||
<div>
|
||||
<div className="h-5 w-36 bg-gray-200 dark:bg-gray-700 rounded mb-2"></div>
|
||||
<div className="h-4 w-48 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Table Header Skeleton */}
|
||||
<div className="mb-3">
|
||||
<div className="h-4 w-40 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</div>
|
||||
|
||||
{/* Table Skeleton */}
|
||||
<div className="overflow-x-auto">
|
||||
<table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800">
|
||||
<tr>
|
||||
{[1, 2, 3, 4, 5, 6].map((i) => (
|
||||
<th key={i} className="px-4 py-3">
|
||||
<div className="h-3 w-16 bg-gray-300 dark:bg-gray-600 rounded"></div>
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="bg-white dark:bg-gray-900 divide-y divide-gray-200 dark:divide-gray-700">
|
||||
{[1, 2, 3].map((row) => (
|
||||
<tr key={row}>
|
||||
<td className="px-4 py-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-4 h-4 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
<div>
|
||||
<div className="h-4 w-32 bg-gray-200 dark:bg-gray-700 rounded mb-1"></div>
|
||||
<div className="h-3 w-24 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="h-4 w-16 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="h-4 w-20 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="h-4 w-12 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="h-4 w-24 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="h-4 w-16 bg-gray-200 dark:bg-gray-700 rounded"></div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@ -1,6 +1,5 @@
|
||||
import { useRef, useEffect, useState, memo, useMemo } from 'react';
|
||||
import { useSessionStore } from '../stores/sessionStore';
|
||||
import { useNavigationStore } from '../stores/navigationStore';
|
||||
import { JsonMessageView } from './JsonMessageView';
|
||||
import { EmptyState } from './EmptyState';
|
||||
import CombinedDiffView from './CombinedDiffView';
|
||||
@ -15,18 +14,19 @@ import { CommitMessageDialog } from './session/CommitMessageDialog';
|
||||
import { PromptNavigation } from './PromptNavigation';
|
||||
import { isDocumentVisible } from '../utils/performanceUtils';
|
||||
import { FileEditor } from './FileEditor';
|
||||
import { ProjectView } from './ProjectView';
|
||||
import { API } from '../utils/api';
|
||||
|
||||
export const SessionView = memo(() => {
|
||||
const activeSessionId = useSessionStore((state) => state.activeSessionId);
|
||||
const sessions = useSessionStore((state) => state.sessions);
|
||||
const activeMainRepoSession = useSessionStore((state) => state.activeMainRepoSession);
|
||||
const { activeView, activeProjectId } = useNavigationStore();
|
||||
const [animationsEnabled, setAnimationsEnabled] = useState(isDocumentVisible());
|
||||
const [projectData, setProjectData] = useState<any>(null);
|
||||
const [isProjectLoading, setIsProjectLoading] = useState(false);
|
||||
const [isMergingProject, setIsMergingProject] = useState(false);
|
||||
|
||||
// Stub variables for project view functionality (navigationStore was removed)
|
||||
// const activeView = 'session'; // Always show session view
|
||||
// const activeProjectId = null; // No active project
|
||||
// const [projectData, setProjectData] = useState<any>(null);
|
||||
// const [isProjectLoading, setIsProjectLoading] = useState(false);
|
||||
// const [isMergingProject, setIsMergingProject] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const handleVisibilityChange = () => {
|
||||
@ -37,35 +37,14 @@ export const SessionView = memo(() => {
|
||||
return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
}, []);
|
||||
|
||||
// Load project data when activeProjectId changes
|
||||
useEffect(() => {
|
||||
if (activeView === 'project' && activeProjectId) {
|
||||
const loadProjectData = async () => {
|
||||
setIsProjectLoading(true);
|
||||
try {
|
||||
// Get all projects and find the one we need
|
||||
const response = await API.projects.getAll();
|
||||
if (response.success && response.data) {
|
||||
const project = response.data.find((p: any) => p.id === activeProjectId);
|
||||
if (project) {
|
||||
setProjectData(project);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load project data:', error);
|
||||
} finally {
|
||||
setIsProjectLoading(false);
|
||||
}
|
||||
};
|
||||
loadProjectData();
|
||||
} else {
|
||||
setProjectData(null);
|
||||
}
|
||||
}, [activeView, activeProjectId]);
|
||||
// Project data loading disabled for now
|
||||
// useEffect(() => {
|
||||
// // TODO: Re-enable when project view is implemented
|
||||
// }, [activeView, activeProjectId]);
|
||||
|
||||
const handleProjectGitPull = async () => {
|
||||
if (!activeProjectId || !projectData) return;
|
||||
setIsMergingProject(true);
|
||||
/* const handleProjectGitPull = async () => {
|
||||
if (!activeProjectId) return; // || !projectData
|
||||
// setIsMergingProject(true);
|
||||
try {
|
||||
// Get or create main repo session for this project
|
||||
const sessionResponse = await API.sessions.getOrCreateMainRepoSession(activeProjectId);
|
||||
@ -78,13 +57,13 @@ export const SessionView = memo(() => {
|
||||
} catch (error) {
|
||||
console.error('Failed to perform git pull:', error);
|
||||
} finally {
|
||||
setIsMergingProject(false);
|
||||
// setIsMergingProject(false);
|
||||
}
|
||||
};
|
||||
}; */
|
||||
|
||||
const handleProjectGitPush = async () => {
|
||||
if (!activeProjectId || !projectData) return;
|
||||
setIsMergingProject(true);
|
||||
/* const handleProjectGitPush = async () => {
|
||||
if (!activeProjectId) return; // || !projectData
|
||||
// setIsMergingProject(true);
|
||||
try {
|
||||
// Get or create main repo session for this project
|
||||
const sessionResponse = await API.sessions.getOrCreateMainRepoSession(activeProjectId);
|
||||
@ -97,9 +76,9 @@ export const SessionView = memo(() => {
|
||||
} catch (error) {
|
||||
console.error('Failed to perform git push:', error);
|
||||
} finally {
|
||||
setIsMergingProject(false);
|
||||
// setIsMergingProject(false);
|
||||
}
|
||||
};
|
||||
}; */
|
||||
|
||||
const activeSession = activeSessionId
|
||||
? (activeMainRepoSession && activeMainRepoSession.id === activeSessionId
|
||||
@ -116,32 +95,6 @@ export const SessionView = memo(() => {
|
||||
const emptySelectedExecutions = useMemo(() => [], []);
|
||||
const isMainRepo = useMemo(() => activeSession?.isMainRepo || false, [activeSession?.isMainRepo]);
|
||||
|
||||
// Show project view if navigation is set to project
|
||||
if (activeView === 'project' && activeProjectId) {
|
||||
if (isProjectLoading || !projectData) {
|
||||
return (
|
||||
<div className="flex-1 flex flex-col overflow-hidden bg-gray-50 dark:bg-gray-900 p-6">
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<div className="text-center">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500 mx-auto mb-4"></div>
|
||||
<p className="text-gray-600 dark:text-gray-400">Loading project...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<ProjectView
|
||||
projectId={activeProjectId}
|
||||
projectName={projectData.name || 'Project'}
|
||||
onGitPull={handleProjectGitPull}
|
||||
onGitPush={handleProjectGitPush}
|
||||
isMerging={isMergingProject}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (!activeSession) {
|
||||
return (
|
||||
<div className="flex-1 flex flex-col overflow-hidden bg-gray-50 dark:bg-gray-950">
|
||||
|
||||
@ -38,7 +38,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-green-800',
|
||||
icon: Loader2,
|
||||
text: 'Initializing',
|
||||
tooltip: 'Setting up git worktree and environment',
|
||||
animated: true,
|
||||
spin: true,
|
||||
};
|
||||
@ -51,7 +50,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-green-800',
|
||||
icon: Loader2,
|
||||
text: 'Running',
|
||||
tooltip: 'Claude is actively processing your request',
|
||||
animated: true,
|
||||
spin: true,
|
||||
};
|
||||
@ -64,7 +62,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-amber-800',
|
||||
icon: PauseCircle,
|
||||
text: 'Waiting for input',
|
||||
tooltip: 'Claude needs your input to continue',
|
||||
animated: true,
|
||||
pulse: true,
|
||||
};
|
||||
@ -77,7 +74,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-gray-700',
|
||||
icon: CheckCircle,
|
||||
text: 'Completed',
|
||||
tooltip: 'Task finished successfully',
|
||||
animated: false,
|
||||
};
|
||||
case 'completed_unviewed':
|
||||
@ -89,7 +85,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-blue-800',
|
||||
icon: Bell,
|
||||
text: 'New activity',
|
||||
tooltip: 'Session has new unviewed results',
|
||||
animated: true,
|
||||
pulse: true,
|
||||
};
|
||||
@ -102,7 +97,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-red-800',
|
||||
icon: AlertCircle,
|
||||
text: 'Error',
|
||||
tooltip: 'Something went wrong with the session',
|
||||
animated: false,
|
||||
};
|
||||
default:
|
||||
@ -114,7 +108,6 @@ export const StatusIndicator = React.memo(({
|
||||
borderColor: 'border-gray-700',
|
||||
icon: AlertCircle,
|
||||
text: 'Unknown',
|
||||
tooltip: 'Unknown status',
|
||||
animated: false,
|
||||
};
|
||||
}
|
||||
@ -179,7 +172,6 @@ export const StatusIndicator = React.memo(({
|
||||
${shouldAnimate ? 'relative overflow-hidden' : ''}
|
||||
transition-all duration-200
|
||||
`}
|
||||
title={config.tooltip}
|
||||
>
|
||||
{/* Animated background effect for active states */}
|
||||
{shouldAnimate && (
|
||||
@ -237,7 +229,7 @@ export const StatusIndicator = React.memo(({
|
||||
|
||||
// Original dot indicator for when showText is false
|
||||
return (
|
||||
<div className={`flex items-center ${sizeClasses.spacing}`} title={config.tooltip}>
|
||||
<div className={`flex items-center ${sizeClasses.spacing}`}>
|
||||
{/* Status Indicator Dot */}
|
||||
<div className={`relative ${sizeClasses.container} flex items-center justify-center`} ref={elementRef}>
|
||||
<div
|
||||
|
||||
290
frontend/src/components/TokenUsageDisplay.tsx
Normal file
290
frontend/src/components/TokenUsageDisplay.tsx
Normal file
@ -0,0 +1,290 @@
|
||||
import { useEffect, useState, useRef } from 'react';
|
||||
import { Info, AlertTriangle, AlertCircle, HelpCircle } from 'lucide-react';
|
||||
import { API } from '../utils/api';
|
||||
|
||||
interface TokenUsageData {
|
||||
summary: {
|
||||
session_id: string;
|
||||
total_input_tokens: number;
|
||||
total_output_tokens: number;
|
||||
total_tokens: number;
|
||||
last_updated: string;
|
||||
} | null;
|
||||
contextWindow: number;
|
||||
warning: {
|
||||
level: 'info' | 'warning' | 'critical';
|
||||
percentage: number;
|
||||
message: string;
|
||||
tokensUsed: number;
|
||||
contextWindow: number;
|
||||
} | null;
|
||||
currentContextUsage?: number | null;
|
||||
}
|
||||
|
||||
interface TokenUsageDisplayProps {
|
||||
sessionId: string;
|
||||
className?: string;
|
||||
compact?: boolean;
|
||||
}
|
||||
|
||||
export function TokenUsageDisplay({ sessionId, className = '', compact = false }: TokenUsageDisplayProps) {
|
||||
const [tokenData, setTokenData] = useState<TokenUsageData | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [showPopover, setShowPopover] = useState(false);
|
||||
const popoverRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
|
||||
const fetchTokenUsage = async () => {
|
||||
try {
|
||||
const response = await API.sessions.getTokenUsage(sessionId);
|
||||
if (response.success && mounted) {
|
||||
setTokenData(response.data);
|
||||
setError(null);
|
||||
} else if (!response.success && mounted) {
|
||||
setError(response.error || 'Failed to load token usage');
|
||||
}
|
||||
} catch (err) {
|
||||
if (mounted) {
|
||||
setError('Failed to load token usage');
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fetchTokenUsage();
|
||||
|
||||
// Refresh every 30 seconds
|
||||
const interval = setInterval(fetchTokenUsage, 30000);
|
||||
|
||||
return () => {
|
||||
mounted = false;
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [sessionId]);
|
||||
|
||||
// Handle clicks outside popover
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (popoverRef.current && !popoverRef.current.contains(event.target as Node)) {
|
||||
setShowPopover(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (showPopover) {
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}
|
||||
}, [showPopover]);
|
||||
|
||||
if (loading || error || !tokenData || !tokenData.summary) {
|
||||
// Show a placeholder during loading or when no data
|
||||
if (!loading && !error && !tokenData?.summary) {
|
||||
return (
|
||||
<div className={`flex items-center gap-1 text-xs text-gray-400 dark:text-gray-600 px-2 py-1 ${className}`}>
|
||||
<Info className="w-3 h-3" />
|
||||
<span>No token data</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const { summary } = tokenData;
|
||||
|
||||
// Just show the raw token totals - no percentage calculations or estimates
|
||||
const totalTokens = summary.total_input_tokens + summary.total_output_tokens;
|
||||
|
||||
// Simple warning thresholds based on empirical data from sessions that hit limits
|
||||
// NEW lowest observed failure: 96,800 tokens! (was 119,248)
|
||||
const getWarningLevel = (tokens: number): 'safe' | 'caution' | 'danger' => {
|
||||
if (tokens > 95000) return 'danger'; // Lowered from 120K
|
||||
if (tokens > 80000) return 'caution'; // Lowered from 100K
|
||||
return 'safe';
|
||||
};
|
||||
|
||||
const warningLevel = getWarningLevel(totalTokens);
|
||||
|
||||
// Format numbers for display
|
||||
const formatNumber = (num: number): string => {
|
||||
if (num >= 1000000) {
|
||||
return `${(num / 1000000).toFixed(1)}M`;
|
||||
} else if (num >= 1000) {
|
||||
return `${(num / 1000).toFixed(1)}K`;
|
||||
}
|
||||
return num.toLocaleString();
|
||||
};
|
||||
|
||||
if (compact) {
|
||||
// Compact view for session list - show total tokens with warning color
|
||||
const getCompactClasses = () => {
|
||||
switch (warningLevel) {
|
||||
case 'danger':
|
||||
return 'text-red-600 bg-red-100 dark:bg-red-900/20';
|
||||
case 'caution':
|
||||
return 'text-yellow-600 bg-yellow-100 dark:bg-yellow-900/20';
|
||||
default:
|
||||
return 'text-gray-600 dark:text-gray-400 bg-gray-100 dark:bg-gray-800';
|
||||
}
|
||||
};
|
||||
|
||||
const getIcon = () => {
|
||||
switch (warningLevel) {
|
||||
case 'danger':
|
||||
return <AlertCircle className="w-3 h-3" />;
|
||||
case 'caution':
|
||||
return <AlertTriangle className="w-3 h-3" />;
|
||||
default:
|
||||
return <Info className="w-3 h-3" />;
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative inline-block">
|
||||
<div className={`flex items-center gap-1 text-xs px-2 py-1 rounded-md ${getCompactClasses()} ${className}`}>
|
||||
{getIcon()}
|
||||
<span className="font-medium">{formatNumber(totalTokens)}</span>
|
||||
<span className="opacity-75">tokens</span>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
setShowPopover(!showPopover);
|
||||
}}
|
||||
className="ml-1 hover:opacity-80 transition-opacity"
|
||||
aria-label="Token usage details"
|
||||
>
|
||||
<HelpCircle className="w-3 h-3" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{showPopover && (
|
||||
<div
|
||||
ref={popoverRef}
|
||||
className="absolute z-50 mt-2 right-0 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 p-3 min-w-[280px]"
|
||||
>
|
||||
<div className="text-xs space-y-2">
|
||||
<div className="font-semibold text-gray-900 dark:text-gray-100 mb-2">
|
||||
Token Usage Breakdown
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-2 text-gray-700 dark:text-gray-300">
|
||||
<div>Input tokens:</div>
|
||||
<div className="text-right font-medium">{formatNumber(summary.total_input_tokens)}</div>
|
||||
|
||||
<div>Output tokens:</div>
|
||||
<div className="text-right font-medium">{formatNumber(summary.total_output_tokens)}</div>
|
||||
|
||||
<div className="border-t border-gray-200 dark:border-gray-600 pt-2 font-semibold">Total:</div>
|
||||
<div className="border-t border-gray-200 dark:border-gray-600 pt-2 text-right font-semibold">
|
||||
{formatNumber(totalTokens)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{warningLevel !== 'safe' && (
|
||||
<div className={`mt-3 p-2 rounded-md text-xs ${
|
||||
warningLevel === 'danger'
|
||||
? 'bg-red-100 dark:bg-red-900/20 text-red-700 dark:text-red-300'
|
||||
: 'bg-yellow-100 dark:bg-yellow-900/20 text-yellow-700 dark:text-yellow-300'
|
||||
}`}>
|
||||
{warningLevel === 'danger'
|
||||
? '๐จ Danger: May hit "Prompt is too long" error soon!'
|
||||
: 'โ ๏ธ Caution: Approaching token limits.'}
|
||||
<div className="mt-1 opacity-75">
|
||||
Based on analysis of failed sessions. Lowest failure: 96,800 tokens.
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="text-gray-500 dark:text-gray-400 text-xs pt-2">
|
||||
Input tokens include all context sent to Claude (system prompts, tools, conversation history).
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Full view for session detail - show raw input/output breakdown with warnings
|
||||
const getFullViewClasses = () => {
|
||||
switch (warningLevel) {
|
||||
case 'danger':
|
||||
return 'bg-red-100 dark:bg-red-900/20 text-red-700 dark:text-red-300';
|
||||
case 'caution':
|
||||
return 'bg-yellow-100 dark:bg-yellow-900/20 text-yellow-700 dark:text-yellow-300';
|
||||
default:
|
||||
return 'bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300';
|
||||
}
|
||||
};
|
||||
|
||||
const getFullIcon = () => {
|
||||
switch (warningLevel) {
|
||||
case 'danger':
|
||||
return <AlertCircle className="w-4 h-4" />;
|
||||
case 'caution':
|
||||
return <AlertTriangle className="w-4 h-4" />;
|
||||
default:
|
||||
return <Info className="w-4 h-4" />;
|
||||
}
|
||||
};
|
||||
|
||||
const getWarningMessage = () => {
|
||||
switch (warningLevel) {
|
||||
case 'danger':
|
||||
return 'Danger: You may hit the "Prompt is too long" error soon. Consider starting a new session.';
|
||||
case 'caution':
|
||||
return 'Caution: Approaching token limits. Sessions have failed as low as 96,800 tokens.';
|
||||
default:
|
||||
return 'This shows the cumulative tokens reported by Claude for this conversation. Input tokens include all context sent to Claude (system prompts, tools, conversation history).';
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`p-4 rounded-lg ${getFullViewClasses()} ${className}`}>
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="flex-shrink-0 mt-0.5">
|
||||
{getFullIcon()}
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-baseline gap-2 flex-wrap">
|
||||
<h3 className="text-sm font-semibold">Token Usage (Running Total)</h3>
|
||||
<span className="text-xs opacity-75">
|
||||
{formatNumber(totalTokens)} total tokens
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="mt-3 space-y-2 text-sm">
|
||||
<div className="flex justify-between">
|
||||
<span>Input tokens (processed by Claude):</span>
|
||||
<span className="font-medium">{formatNumber(summary.total_input_tokens)}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span>Output tokens (generated by Claude):</span>
|
||||
<span className="font-medium">{formatNumber(summary.total_output_tokens)}</span>
|
||||
</div>
|
||||
<div className="border-t border-current/20 pt-2 flex justify-between font-semibold">
|
||||
<span>Total conversation:</span>
|
||||
<span>{formatNumber(totalTokens)}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="mt-3 text-xs opacity-90 leading-relaxed">
|
||||
{getWarningMessage()}
|
||||
</p>
|
||||
|
||||
{warningLevel !== 'safe' && (
|
||||
<p className="mt-2 text-xs opacity-75">
|
||||
Based on analysis of failed sessions, the lowest failure occurred at 96,800 tokens.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,247 +0,0 @@
|
||||
import React from 'react';
|
||||
import { GitBranch, AlertCircle, CheckCircle, Clock, ArrowRight, GitFork } from 'lucide-react';
|
||||
import type { RemoteStatus, MainBranchStatus } from '../../types/projectDashboard';
|
||||
|
||||
interface MultiOriginStatusProps {
|
||||
mainBranch: string;
|
||||
mainBranchStatus: MainBranchStatus;
|
||||
remotes?: RemoteStatus[];
|
||||
onReviewUpdates?: () => void;
|
||||
}
|
||||
|
||||
export const MultiOriginStatus: React.FC<MultiOriginStatusProps> = ({
|
||||
mainBranch,
|
||||
mainBranchStatus,
|
||||
remotes = [],
|
||||
onReviewUpdates
|
||||
}) => {
|
||||
// Find upstream and origin remotes
|
||||
const upstream = remotes.find(r => r.isUpstream || r.name === 'upstream');
|
||||
const origin = remotes.find(r => !r.isUpstream && r.name === 'origin');
|
||||
const hasMultipleRemotes = remotes.length > 1;
|
||||
|
||||
// Check if any updates are needed
|
||||
const upstreamNeedsUpdate = upstream && upstream.status !== 'up-to-date';
|
||||
const originNeedsUpdate = origin && origin.status !== 'up-to-date';
|
||||
const localNeedsUpdate = mainBranchStatus.status !== 'up-to-date';
|
||||
const hasUpdatesNeeded = upstreamNeedsUpdate || originNeedsUpdate || localNeedsUpdate;
|
||||
|
||||
const getStatusIcon = (status: string) => {
|
||||
switch (status) {
|
||||
case 'up-to-date':
|
||||
return <CheckCircle className="w-4 h-4 text-green-500" />;
|
||||
case 'behind':
|
||||
return <AlertCircle className="w-4 h-4 text-yellow-500" />;
|
||||
case 'ahead':
|
||||
return <Clock className="w-4 h-4 text-blue-500" />;
|
||||
case 'diverged':
|
||||
return <AlertCircle className="w-4 h-4 text-red-500" />;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusText = (remote: RemoteStatus) => {
|
||||
switch (remote.status) {
|
||||
case 'up-to-date':
|
||||
return 'Up to date';
|
||||
case 'behind':
|
||||
return `${remote.behindCount} behind`;
|
||||
case 'ahead':
|
||||
return `${remote.aheadCount} ahead`;
|
||||
case 'diverged':
|
||||
return `${remote.aheadCount}โ ${remote.behindCount}โ`;
|
||||
default:
|
||||
return 'Unknown';
|
||||
}
|
||||
};
|
||||
|
||||
const getLocalStatusText = () => {
|
||||
const { status, aheadCount = 0, behindCount = 0 } = mainBranchStatus;
|
||||
switch (status) {
|
||||
case 'up-to-date':
|
||||
return 'Synced with origin';
|
||||
case 'behind':
|
||||
return `${behindCount} behind origin`;
|
||||
case 'ahead':
|
||||
return `${aheadCount} ahead of origin`;
|
||||
case 'diverged':
|
||||
return `${aheadCount}โ ${behindCount}โ`;
|
||||
default:
|
||||
return 'Unknown status';
|
||||
}
|
||||
};
|
||||
|
||||
if (!hasMultipleRemotes) {
|
||||
// Single remote view (original view)
|
||||
return (
|
||||
<div className="mb-6 p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
<GitBranch className="w-5 h-5 text-gray-600 dark:text-gray-400" />
|
||||
<div>
|
||||
<h3 className="font-medium text-gray-900 dark:text-gray-100">
|
||||
Main Branch ({mainBranch})
|
||||
</h3>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400 flex items-center gap-2 mt-1">
|
||||
{getStatusIcon(mainBranchStatus.status)}
|
||||
{getLocalStatusText()}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Multi-remote cascade view
|
||||
return (
|
||||
<div className="mb-6 space-y-3">
|
||||
{/* Multi-Origin Status Flow */}
|
||||
<div className="bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
|
||||
<div className="bg-gray-50 dark:bg-gray-800 px-4 py-2 border-b border-gray-200 dark:border-gray-700">
|
||||
<h3 className="text-sm font-medium text-gray-700 dark:text-gray-300">Git Remote Status</h3>
|
||||
</div>
|
||||
|
||||
<div className="p-4">
|
||||
<div className="flex items-center gap-3">
|
||||
|
||||
{/* Upstream Status */}
|
||||
{upstream && (
|
||||
<>
|
||||
<div className="flex-1 bg-purple-50 dark:bg-purple-900/20 rounded-lg p-3 border border-purple-200 dark:border-purple-800">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<GitBranch className="w-4 h-4 text-purple-600 dark:text-purple-400" />
|
||||
<span className="text-sm font-semibold text-gray-900 dark:text-gray-100">
|
||||
{upstream.name}/{upstream.branch}
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs text-gray-600 dark:text-gray-400">Source repository</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{upstream.status !== 'up-to-date' && (
|
||||
<span className="text-xs font-medium text-purple-700 dark:text-purple-300">
|
||||
{getStatusText(upstream)}
|
||||
</span>
|
||||
)}
|
||||
{getStatusIcon(upstream.status)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ArrowRight className="w-5 h-5 text-gray-400 flex-shrink-0" />
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Origin/Fork Status */}
|
||||
{origin && (
|
||||
<>
|
||||
<div className="flex-1 bg-blue-50 dark:bg-blue-900/20 rounded-lg p-3 border border-blue-200 dark:border-blue-800">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<GitBranch className="w-4 h-4 text-blue-600 dark:text-blue-400" />
|
||||
<span className="text-sm font-semibold text-gray-900 dark:text-gray-100">
|
||||
{origin.name}/{origin.branch}
|
||||
</span>
|
||||
{origin.isFork && (
|
||||
<span className="inline-flex items-center gap-1 text-xs bg-blue-100 dark:bg-blue-800 text-blue-700 dark:text-blue-300 px-1.5 py-0.5 rounded">
|
||||
<GitFork className="w-3 h-3" />
|
||||
<span>fork</span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-xs text-gray-600 dark:text-gray-400">Your remote</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{origin.status !== 'up-to-date' && (
|
||||
<span className="text-xs font-medium text-blue-700 dark:text-blue-300">
|
||||
{getStatusText(origin)}
|
||||
</span>
|
||||
)}
|
||||
{getStatusIcon(origin.status)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ArrowRight className="w-5 h-5 text-gray-400 flex-shrink-0" />
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Local Main Status */}
|
||||
<div className="flex-1 bg-gray-50 dark:bg-gray-800 rounded-lg p-3 border border-gray-200 dark:border-gray-700">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<GitBranch className="w-4 h-4 text-gray-600 dark:text-gray-400" />
|
||||
<span className="text-sm font-semibold text-gray-900 dark:text-gray-100">
|
||||
{mainBranch}
|
||||
</span>
|
||||
<span className="text-xs bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300 px-1.5 py-0.5 rounded">
|
||||
local
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs text-gray-600 dark:text-gray-400">Base for sessions</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{mainBranchStatus.status !== 'up-to-date' && (
|
||||
<span className="text-xs font-medium text-gray-700 dark:text-gray-300">
|
||||
{getLocalStatusText()}
|
||||
</span>
|
||||
)}
|
||||
{getStatusIcon(mainBranchStatus.status)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Action Bar - Shows when updates are needed */}
|
||||
{hasUpdatesNeeded && (
|
||||
<div className="bg-amber-50 dark:bg-amber-900/20 border-t border-amber-200 dark:border-amber-800 px-4 py-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<AlertCircle className="w-4 h-4 text-amber-600 dark:text-amber-400" />
|
||||
<span className="text-sm font-medium text-amber-800 dark:text-amber-200">
|
||||
Updates available in the cascade
|
||||
</span>
|
||||
</div>
|
||||
{onReviewUpdates && (
|
||||
<button
|
||||
onClick={onReviewUpdates}
|
||||
className="text-sm bg-amber-600 hover:bg-amber-700 text-white px-3 py-1.5 rounded font-medium transition-colors"
|
||||
>
|
||||
Review Updates โ
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Flow Legend */}
|
||||
<div className="bg-gray-50 dark:bg-gray-800 px-4 py-2 border-t border-gray-200 dark:border-gray-700">
|
||||
<div className="flex items-center gap-6 text-xs text-gray-600 dark:text-gray-400">
|
||||
<div className="flex items-center gap-2">
|
||||
<CheckCircle className="w-3 h-3 text-green-500" />
|
||||
<span>Synced</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<AlertCircle className="w-3 h-3 text-yellow-500" />
|
||||
<span>Behind</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Clock className="w-3 h-3 text-blue-500" />
|
||||
<span>Ahead</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<AlertCircle className="w-3 h-3 text-red-500" />
|
||||
<span>Diverged</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@ -1,51 +0,0 @@
|
||||
import React from 'react';
|
||||
import type { SessionBranchInfo } from '../../types/projectDashboard';
|
||||
|
||||
interface StatusSummaryCardsProps {
|
||||
sessions: SessionBranchInfo[];
|
||||
}
|
||||
|
||||
export const StatusSummaryCards: React.FC<StatusSummaryCardsProps> = ({ sessions }) => {
|
||||
const upToDateCount = sessions.filter(s => !s.isStale).length;
|
||||
const staleCount = sessions.filter(s => s.isStale).length;
|
||||
const needsAttentionCount = sessions.filter(s => s.hasUncommittedChanges).length;
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-3 gap-3 mb-6">
|
||||
<div className="bg-white dark:bg-gray-900 p-4 rounded-lg border border-gray-200 dark:border-gray-700 shadow-sm">
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Up to date</span>
|
||||
<div className="w-2 h-2 bg-green-500 rounded-full"></div>
|
||||
</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-gray-100">
|
||||
{upToDateCount}
|
||||
<span className="text-sm font-normal text-gray-500 dark:text-gray-400">/{sessions.length}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-white dark:bg-gray-900 p-4 rounded-lg border border-gray-200 dark:border-gray-700 shadow-sm">
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Stale</span>
|
||||
<div className="w-2 h-2 bg-yellow-500 rounded-full"></div>
|
||||
</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-gray-100">
|
||||
{staleCount}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-white dark:bg-gray-900 p-4 rounded-lg border border-gray-200 dark:border-gray-700 shadow-sm">
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Changes</span>
|
||||
<div className="w-2 h-2 bg-orange-500 rounded-full"></div>
|
||||
</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-gray-100">
|
||||
{needsAttentionCount}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@ -1,6 +1,7 @@
|
||||
import React from 'react';
|
||||
import { Session, GitCommands } from '../../types/session';
|
||||
import { StatusIndicator } from '../StatusIndicator';
|
||||
import { TokenUsageDisplay } from '../TokenUsageDisplay';
|
||||
import { ViewTabs } from './ViewTabs';
|
||||
import { ViewMode } from '../../hooks/useSessionView';
|
||||
import { CommitModeIndicator } from '../CommitModeIndicator';
|
||||
@ -80,8 +81,9 @@ export const SessionHeader: React.FC<SessionHeaderProps> = ({
|
||||
</h2>
|
||||
)}
|
||||
{/* Status Indicator */}
|
||||
<div className="flex items-center gap-2 mt-2">
|
||||
<div className="flex items-center gap-3 mt-2">
|
||||
<StatusIndicator key={`status-${activeSession.id}-${activeSession.status}`} session={activeSession} size="medium" showText showProgress />
|
||||
<TokenUsageDisplay sessionId={activeSession.id} compact />
|
||||
<CommitModeIndicator mode={activeSession.commitMode} />
|
||||
</div>
|
||||
|
||||
|
||||
@ -1,31 +1,13 @@
|
||||
import { create } from 'zustand';
|
||||
|
||||
interface NavigationState {
|
||||
activeView: 'sessions' | 'project';
|
||||
activeProjectId: number | null;
|
||||
|
||||
// Actions
|
||||
setActiveView: (view: 'sessions' | 'project') => void;
|
||||
setActiveProjectId: (projectId: number | null) => void;
|
||||
navigateToProject: (projectId: number) => void;
|
||||
interface NavigationStore {
|
||||
currentView: 'sessions' | 'project';
|
||||
navigateToSessions: () => void;
|
||||
navigateToProject: () => void;
|
||||
}
|
||||
|
||||
export const useNavigationStore = create<NavigationState>((set) => ({
|
||||
activeView: 'sessions',
|
||||
activeProjectId: null,
|
||||
|
||||
setActiveView: (view) => set({ activeView: view }),
|
||||
|
||||
setActiveProjectId: (projectId) => set({ activeProjectId: projectId }),
|
||||
|
||||
navigateToProject: (projectId) => set({
|
||||
activeView: 'project',
|
||||
activeProjectId: projectId
|
||||
}),
|
||||
|
||||
navigateToSessions: () => set({
|
||||
activeView: 'sessions',
|
||||
activeProjectId: null
|
||||
}),
|
||||
export const useNavigationStore = create<NavigationStore>((set) => ({
|
||||
currentView: 'sessions',
|
||||
navigateToSessions: () => set({ currentView: 'sessions' }),
|
||||
navigateToProject: () => set({ currentView: 'project' }),
|
||||
}));
|
||||
9
frontend/src/types/electron.d.ts
vendored
9
frontend/src/types/electron.d.ts
vendored
@ -99,6 +99,10 @@ interface ElectronAPI {
|
||||
|
||||
// Image operations
|
||||
saveImages: (sessionId: string, images: Array<{ name: string; dataUrl: string; type: string }>) => Promise<string[]>;
|
||||
|
||||
// Token tracking
|
||||
getTokenUsage: (sessionId: string) => Promise<IPCResponse>;
|
||||
getTokenHistory: (sessionId: string) => Promise<IPCResponse>;
|
||||
};
|
||||
|
||||
// Project management
|
||||
@ -249,6 +253,11 @@ interface ElectronAPI {
|
||||
}>;
|
||||
}>>;
|
||||
};
|
||||
|
||||
// Model context windows
|
||||
models: {
|
||||
getContextWindows: () => Promise<IPCResponse>;
|
||||
};
|
||||
}
|
||||
|
||||
// Additional electron interface for IPC event listeners
|
||||
|
||||
@ -1,53 +0,0 @@
|
||||
export interface MainBranchStatus {
|
||||
status: 'up-to-date' | 'behind' | 'ahead' | 'diverged';
|
||||
aheadCount?: number;
|
||||
behindCount?: number;
|
||||
lastFetched: string;
|
||||
}
|
||||
|
||||
export interface RemoteStatus {
|
||||
name: string;
|
||||
url: string;
|
||||
branch: string;
|
||||
status: 'up-to-date' | 'behind' | 'ahead' | 'diverged';
|
||||
aheadCount: number;
|
||||
behindCount: number;
|
||||
isUpstream?: boolean;
|
||||
isFork?: boolean;
|
||||
}
|
||||
|
||||
export interface SessionBranchInfo {
|
||||
sessionId: string;
|
||||
sessionName: string;
|
||||
branchName: string;
|
||||
worktreePath: string;
|
||||
baseCommit: string;
|
||||
baseBranch: string;
|
||||
isStale: boolean;
|
||||
staleSince?: string;
|
||||
hasUncommittedChanges: boolean;
|
||||
pullRequest?: {
|
||||
number: number;
|
||||
title: string;
|
||||
state: 'open' | 'closed' | 'merged';
|
||||
url: string;
|
||||
};
|
||||
commitsAhead: number;
|
||||
commitsBehind: number;
|
||||
}
|
||||
|
||||
export interface ProjectDashboardData {
|
||||
projectId: number;
|
||||
projectName: string;
|
||||
projectPath: string;
|
||||
mainBranch: string;
|
||||
mainBranchStatus: MainBranchStatus;
|
||||
remotes?: RemoteStatus[];
|
||||
sessionBranches: SessionBranchInfo[];
|
||||
lastRefreshed: string;
|
||||
}
|
||||
|
||||
export interface ProjectDashboardError {
|
||||
message: string;
|
||||
details?: string;
|
||||
}
|
||||
@ -238,6 +238,16 @@ export class API {
|
||||
if (!isElectron()) throw new Error('Electron API not available');
|
||||
return window.electronAPI.sessions.reorder(sessionOrders);
|
||||
},
|
||||
|
||||
async getTokenUsage(sessionId: string) {
|
||||
if (!isElectron()) throw new Error('Electron API not available');
|
||||
return window.electronAPI.sessions.getTokenUsage(sessionId);
|
||||
},
|
||||
|
||||
async getTokenHistory(sessionId: string) {
|
||||
if (!isElectron()) throw new Error('Electron API not available');
|
||||
return window.electronAPI.sessions.getTokenHistory(sessionId);
|
||||
},
|
||||
};
|
||||
|
||||
// Project management
|
||||
@ -427,11 +437,11 @@ export class API {
|
||||
},
|
||||
};
|
||||
|
||||
// Dashboard
|
||||
static dashboard = {
|
||||
async getProjectStatus(projectId: number) {
|
||||
// Model context windows
|
||||
static models = {
|
||||
async getContextWindows() {
|
||||
if (!isElectron()) throw new Error('Electron API not available');
|
||||
return window.electronAPI.dashboard.getProjectStatus(projectId);
|
||||
return window.electronAPI.models.getContextWindows();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,44 +0,0 @@
|
||||
import type { ProjectDashboardData } from '../types/projectDashboard';
|
||||
|
||||
interface CacheEntry {
|
||||
data: ProjectDashboardData;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
class DashboardCache {
|
||||
private cache: Map<number, CacheEntry> = new Map();
|
||||
private readonly CACHE_DURATION = 60 * 1000; // 1 minute cache
|
||||
|
||||
set(projectId: number, data: ProjectDashboardData): void {
|
||||
this.cache.set(projectId, {
|
||||
data,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
get(projectId: number): ProjectDashboardData | null {
|
||||
const entry = this.cache.get(projectId);
|
||||
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if cache is expired
|
||||
if (Date.now() - entry.timestamp > this.CACHE_DURATION) {
|
||||
this.cache.delete(projectId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
invalidate(projectId: number): void {
|
||||
this.cache.delete(projectId);
|
||||
}
|
||||
|
||||
invalidateAll(): void {
|
||||
this.cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
export const dashboardCache = new DashboardCache();
|
||||
@ -1,7 +1,7 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { readFileSync, mkdirSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import type { Project, ProjectRunCommand, Folder, Session, SessionOutput, CreateSessionData, UpdateSessionData, ConversationMessage, PromptMarker, ExecutionDiff, CreateExecutionDiffData } from './models';
|
||||
import type { Project, ProjectRunCommand, Folder, Session, SessionOutput, CreateSessionData, UpdateSessionData, ConversationMessage, PromptMarker, ExecutionDiff, CreateExecutionDiffData, ModelContextWindow, MessageTokenUsage, SessionTokenSummary } from './models';
|
||||
|
||||
export class DatabaseService {
|
||||
private db: Database.Database;
|
||||
@ -15,8 +15,11 @@ export class DatabaseService {
|
||||
}
|
||||
|
||||
initialize(): void {
|
||||
console.log('[Database] Starting database initialization...');
|
||||
this.initializeSchema();
|
||||
console.log('[Database] Schema initialized, running migrations...');
|
||||
this.runMigrations();
|
||||
console.log('[Database] Database initialization complete');
|
||||
}
|
||||
|
||||
private initializeSchema(): void {
|
||||
@ -33,6 +36,8 @@ export class DatabaseService {
|
||||
}
|
||||
|
||||
private runMigrations(): void {
|
||||
console.log('[Database] runMigrations() called - starting migrations...');
|
||||
|
||||
// Check if archived column exists
|
||||
const tableInfo = this.db.prepare("PRAGMA table_info(sessions)").all();
|
||||
const hasArchivedColumn = tableInfo.some((col: any) => col.name === 'archived');
|
||||
@ -667,20 +672,194 @@ export class DatabaseService {
|
||||
console.log('[Database] Added lastUsedModel column to projects table');
|
||||
}
|
||||
|
||||
// Add base_commit and base_branch columns to sessions table if they don't exist
|
||||
const sessionsTableInfoBase = this.db.prepare("PRAGMA table_info(sessions)").all();
|
||||
const hasBaseCommitColumn = sessionsTableInfoBase.some((col: any) => col.name === 'base_commit');
|
||||
const hasBaseBranchColumn = sessionsTableInfoBase.some((col: any) => col.name === 'base_branch');
|
||||
|
||||
if (!hasBaseCommitColumn) {
|
||||
this.db.prepare("ALTER TABLE sessions ADD COLUMN base_commit TEXT").run();
|
||||
console.log('[Database] Added base_commit column to sessions table');
|
||||
// Token tracking migration
|
||||
console.log('[Database] Checking token tracking tables...');
|
||||
const modelContextWindowsTable = this.db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='model_context_windows'").all();
|
||||
if (modelContextWindowsTable.length === 0) {
|
||||
console.log('[Database] Creating token tracking tables...');
|
||||
// Create model context windows table
|
||||
this.db.prepare(`
|
||||
CREATE TABLE model_context_windows (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
model_name TEXT NOT NULL UNIQUE,
|
||||
context_window_size INTEGER NOT NULL,
|
||||
display_name TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`).run();
|
||||
|
||||
// Populate with known models
|
||||
try {
|
||||
const { getAllModels } = require('../utils/modelContextWindows');
|
||||
const models = getAllModels();
|
||||
for (const model of models) {
|
||||
this.db.prepare(`
|
||||
INSERT INTO model_context_windows (model_name, context_window_size, display_name)
|
||||
VALUES (?, ?, ?)
|
||||
`).run(model.name, model.contextWindow, model.displayName);
|
||||
}
|
||||
console.log('[Database] Created model_context_windows table and populated with', models.length, 'models');
|
||||
} catch (error) {
|
||||
console.error('[Database] Failed to populate model context windows:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Message token usage migration
|
||||
const messageTokenUsageTable = this.db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='message_token_usage'").all();
|
||||
if (messageTokenUsageTable.length === 0) {
|
||||
this.db.prepare(`
|
||||
CREATE TABLE message_token_usage (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
output_id INTEGER NOT NULL,
|
||||
message_type TEXT NOT NULL CHECK (message_type IN ('user', 'assistant', 'system')),
|
||||
input_tokens INTEGER DEFAULT 0,
|
||||
output_tokens INTEGER DEFAULT 0,
|
||||
total_tokens INTEGER DEFAULT 0,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (output_id) REFERENCES session_outputs(id) ON DELETE CASCADE
|
||||
)
|
||||
`).run();
|
||||
this.db.prepare("CREATE INDEX idx_message_token_usage_session_id ON message_token_usage(session_id)").run();
|
||||
this.db.prepare("CREATE INDEX idx_message_token_usage_output_id ON message_token_usage(output_id)").run();
|
||||
console.log('[Database] Created message_token_usage table');
|
||||
}
|
||||
|
||||
// Session token summary migration
|
||||
const sessionTokenSummaryTable = this.db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='session_token_summary'").all();
|
||||
if (sessionTokenSummaryTable.length === 0) {
|
||||
this.db.prepare(`
|
||||
CREATE TABLE session_token_summary (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
total_input_tokens INTEGER DEFAULT 0,
|
||||
total_output_tokens INTEGER DEFAULT 0,
|
||||
total_tokens INTEGER DEFAULT 0,
|
||||
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
|
||||
)
|
||||
`).run();
|
||||
console.log('[Database] Created session_token_summary table');
|
||||
|
||||
// Initialize token summaries for existing sessions
|
||||
const sessions = this.db.prepare("SELECT id FROM sessions").all() as Array<{ id: string }>;
|
||||
for (const session of sessions) {
|
||||
this.db.prepare(`
|
||||
INSERT INTO session_token_summary (session_id)
|
||||
VALUES (?)
|
||||
`).run(session.id);
|
||||
}
|
||||
if (sessions.length > 0) {
|
||||
console.log('[Database] Initialized token summaries for', sessions.length, 'existing sessions');
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasBaseBranchColumn) {
|
||||
this.db.prepare("ALTER TABLE sessions ADD COLUMN base_branch TEXT").run();
|
||||
console.log('[Database] Added base_branch column to sessions table');
|
||||
// Backfill token usage from existing JSON messages
|
||||
console.log('[Database] About to call backfillTokenUsage()...');
|
||||
this.backfillTokenUsage();
|
||||
console.log('[Database] runMigrations() completed');
|
||||
}
|
||||
|
||||
private backfillTokenUsage(): void {
|
||||
console.log('[Database] Checking for token usage backfill...');
|
||||
|
||||
// Check if token tracking tables exist (for fresh installs)
|
||||
const tables = this.db.prepare(`
|
||||
SELECT name FROM sqlite_master
|
||||
WHERE type='table'
|
||||
AND name IN ('message_token_usage', 'session_token_summary')
|
||||
`).all() as Array<{ name: string }>;
|
||||
|
||||
if (tables.length !== 2) {
|
||||
console.log('[Database] Token tracking tables not yet created, skipping backfill');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get count of already processed outputs
|
||||
const processedCount = this.db.prepare(`
|
||||
SELECT COUNT(DISTINCT output_id) as count FROM message_token_usage
|
||||
`).get() as { count: number };
|
||||
|
||||
// Get all JSON outputs that haven't been processed yet
|
||||
const jsonOutputs = this.db.prepare(`
|
||||
SELECT so.id, so.session_id, so.data
|
||||
FROM session_outputs so
|
||||
LEFT JOIN message_token_usage mtu ON mtu.output_id = so.id
|
||||
WHERE so.type = 'json' AND mtu.id IS NULL
|
||||
ORDER BY so.timestamp ASC
|
||||
`).all() as Array<{ id: number; session_id: string; data: string }>;
|
||||
|
||||
if (jsonOutputs.length === 0) {
|
||||
if (processedCount.count > 0) {
|
||||
console.log(`[Database] Token usage already backfilled (${processedCount.count} records exist)`);
|
||||
} else {
|
||||
console.log('[Database] No JSON outputs to backfill');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[Database] Starting token usage backfill for ${jsonOutputs.length} new JSON messages (${processedCount.count} already processed)...`);
|
||||
|
||||
let extractTokenUsage: any;
|
||||
let getMessageType: any;
|
||||
|
||||
try {
|
||||
const tokenTracker = require('../utils/tokenTracker');
|
||||
extractTokenUsage = tokenTracker.extractTokenUsage;
|
||||
getMessageType = tokenTracker.getMessageType;
|
||||
console.log('[Database] Successfully loaded tokenTracker utilities');
|
||||
} catch (error) {
|
||||
console.error('[Database] Failed to load tokenTracker utilities:', error);
|
||||
return;
|
||||
}
|
||||
|
||||
let newlyProcessedCount = 0;
|
||||
let errorCount = 0;
|
||||
|
||||
// Process each JSON output
|
||||
for (const output of jsonOutputs) {
|
||||
try {
|
||||
const jsonData = JSON.parse(output.data);
|
||||
const usage = extractTokenUsage(jsonData);
|
||||
|
||||
if (usage) {
|
||||
const messageType = getMessageType(jsonData);
|
||||
|
||||
// Insert token usage record
|
||||
this.db.prepare(`
|
||||
INSERT INTO message_token_usage (session_id, output_id, message_type, input_tokens, output_tokens, total_tokens)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
output.session_id,
|
||||
output.id,
|
||||
messageType,
|
||||
usage.inputTokens,
|
||||
usage.outputTokens,
|
||||
usage.totalTokens
|
||||
);
|
||||
|
||||
newlyProcessedCount++;
|
||||
}
|
||||
} catch (error) {
|
||||
errorCount++;
|
||||
if (errorCount <= 5) {
|
||||
console.error(`[Database] Error processing output ${output.id}:`, error);
|
||||
}
|
||||
// Continue processing other messages even if one fails
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[Database] Backfilled token usage for ${newlyProcessedCount} messages (${errorCount} errors)`);
|
||||
|
||||
// Update all session summaries
|
||||
const sessions = this.db.prepare("SELECT DISTINCT session_id FROM message_token_usage").all() as Array<{ session_id: string }>;
|
||||
|
||||
for (const { session_id } of sessions) {
|
||||
this.updateSessionTokenSummary(session_id);
|
||||
}
|
||||
|
||||
console.log(`[Database] Token usage backfill complete - updated summaries for ${sessions.length} sessions`);
|
||||
|
||||
// Add commit mode settings columns to projects table if they don't exist
|
||||
const projectsTableInfoCommit = this.db.prepare("PRAGMA table_info(projects)").all();
|
||||
@ -1279,11 +1458,13 @@ export class DatabaseService {
|
||||
}
|
||||
|
||||
// Session output operations
|
||||
addSessionOutput(sessionId: string, type: 'stdout' | 'stderr' | 'system' | 'json', data: string): void {
|
||||
this.db.prepare(`
|
||||
addSessionOutput(sessionId: string, type: 'stdout' | 'stderr' | 'system' | 'json', data: string): number {
|
||||
const result = this.db.prepare(`
|
||||
INSERT INTO session_outputs (session_id, type, data)
|
||||
VALUES (?, ?, ?)
|
||||
`).run(sessionId, type, data);
|
||||
|
||||
return result.lastInsertRowid as number;
|
||||
}
|
||||
|
||||
getSessionOutputs(sessionId: string, limit?: number): SessionOutput[] {
|
||||
@ -1698,6 +1879,128 @@ export class DatabaseService {
|
||||
return preferences;
|
||||
}
|
||||
|
||||
// Token tracking operations
|
||||
addMessageTokenUsage(sessionId: string, outputId: number, messageType: 'user' | 'assistant' | 'system', inputTokens: number, outputTokens: number): void {
|
||||
const totalTokens = inputTokens + outputTokens;
|
||||
|
||||
this.db.prepare(`
|
||||
INSERT INTO message_token_usage (session_id, output_id, message_type, input_tokens, output_tokens, total_tokens)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`).run(sessionId, outputId, messageType, inputTokens, outputTokens, totalTokens);
|
||||
|
||||
// Update session summary
|
||||
this.updateSessionTokenSummary(sessionId);
|
||||
}
|
||||
|
||||
updateSessionTokenSummary(sessionId: string): void {
|
||||
// Calculate totals from message_token_usage
|
||||
const totals = this.db.prepare(`
|
||||
SELECT
|
||||
SUM(input_tokens) as total_input,
|
||||
SUM(output_tokens) as total_output,
|
||||
SUM(total_tokens) as total
|
||||
FROM message_token_usage
|
||||
WHERE session_id = ?
|
||||
`).get(sessionId) as { total_input: number | null; total_output: number | null; total: number | null };
|
||||
|
||||
// Update or insert session summary
|
||||
this.db.prepare(`
|
||||
INSERT INTO session_token_summary (session_id, total_input_tokens, total_output_tokens, total_tokens)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(session_id) DO UPDATE SET
|
||||
total_input_tokens = excluded.total_input_tokens,
|
||||
total_output_tokens = excluded.total_output_tokens,
|
||||
total_tokens = excluded.total_tokens,
|
||||
last_updated = CURRENT_TIMESTAMP
|
||||
`).run(
|
||||
sessionId,
|
||||
totals.total_input || 0,
|
||||
totals.total_output || 0,
|
||||
totals.total || 0
|
||||
);
|
||||
}
|
||||
|
||||
getSessionTokenSummary(sessionId: string): SessionTokenSummary | undefined {
|
||||
console.log('[DB] Getting token summary for session:', sessionId);
|
||||
const result = this.db.prepare(`
|
||||
SELECT * FROM session_token_summary
|
||||
WHERE session_id = ?
|
||||
`).get(sessionId) as SessionTokenSummary | undefined;
|
||||
console.log('[DB] Token summary result:', result);
|
||||
return result;
|
||||
}
|
||||
|
||||
getSessionTokenUsageHistory(sessionId: string): MessageTokenUsage[] {
|
||||
return this.db.prepare(`
|
||||
SELECT * FROM message_token_usage
|
||||
WHERE session_id = ?
|
||||
ORDER BY timestamp ASC
|
||||
`).all(sessionId) as MessageTokenUsage[];
|
||||
}
|
||||
|
||||
updateSessionContextWindow(sessionId: string, contextWindowTokens: number): void {
|
||||
console.log('[DB] Updating session context window:', sessionId, contextWindowTokens);
|
||||
|
||||
// Store the latest context window usage in the token_usage_history table
|
||||
// This gives us the most recent context window state
|
||||
this.db.prepare(`
|
||||
INSERT OR REPLACE INTO token_usage_history (
|
||||
session_id,
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
context_window_tokens,
|
||||
timestamp
|
||||
) VALUES (?, 0, 0, ?, datetime('now'))
|
||||
`).run(sessionId, contextWindowTokens);
|
||||
}
|
||||
|
||||
getLatestContextWindowUsage(sessionId: string): number | null {
|
||||
console.log('[DB] Getting latest context window usage for session:', sessionId);
|
||||
|
||||
const result = this.db.prepare(`
|
||||
SELECT context_window_tokens
|
||||
FROM token_usage_history
|
||||
WHERE session_id = ? AND context_window_tokens > 0
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 1
|
||||
`).get(sessionId) as { context_window_tokens: number } | undefined;
|
||||
|
||||
const usage = result?.context_window_tokens;
|
||||
console.log('[DB] Latest context window usage:', usage, 'type:', typeof usage);
|
||||
return usage || null;
|
||||
}
|
||||
|
||||
getLatestSessionOutputWithUsage(sessionId: string): { id: number; data: string } | null {
|
||||
console.log('[DB] Getting latest session output with usage for session:', sessionId);
|
||||
|
||||
const result = this.db.prepare(`
|
||||
SELECT id, data
|
||||
FROM session_outputs
|
||||
WHERE session_id = ?
|
||||
AND type = 'json'
|
||||
AND (data LIKE '%"usage"%' OR data LIKE '%"message":{"usage"%')
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`).get(sessionId) as { id: number; data: string } | undefined;
|
||||
|
||||
console.log('[DB] Latest output with usage:', result ? { id: result.id, hasData: !!result.data } : null);
|
||||
return result || null;
|
||||
}
|
||||
|
||||
getModelContextWindow(modelName: string): ModelContextWindow | undefined {
|
||||
return this.db.prepare(`
|
||||
SELECT * FROM model_context_windows
|
||||
WHERE model_name = ?
|
||||
`).get(modelName) as ModelContextWindow | undefined;
|
||||
}
|
||||
|
||||
getAllModelContextWindows(): ModelContextWindow[] {
|
||||
return this.db.prepare(`
|
||||
SELECT * FROM model_context_windows
|
||||
ORDER BY display_name ASC
|
||||
`).all() as ModelContextWindow[];
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.db.close();
|
||||
}
|
||||
|
||||
@ -154,4 +154,32 @@ export interface CreateExecutionDiffData {
|
||||
stats_files_changed?: number;
|
||||
before_commit_hash?: string;
|
||||
after_commit_hash?: string;
|
||||
}
|
||||
|
||||
export interface ModelContextWindow {
|
||||
id: number;
|
||||
model_name: string;
|
||||
context_window_size: number;
|
||||
display_name?: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface MessageTokenUsage {
|
||||
id: number;
|
||||
session_id: string;
|
||||
output_id: number;
|
||||
message_type: 'user' | 'assistant' | 'system';
|
||||
input_tokens: number;
|
||||
output_tokens: number;
|
||||
total_tokens: number;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export interface SessionTokenSummary {
|
||||
session_id: string;
|
||||
total_input_tokens: number;
|
||||
total_output_tokens: number;
|
||||
total_tokens: number;
|
||||
last_updated: string;
|
||||
}
|
||||
@ -34,8 +34,44 @@ CREATE TABLE IF NOT EXISTS conversation_messages (
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Model context windows table to store context limits for each model
|
||||
CREATE TABLE IF NOT EXISTS model_context_windows (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
model_name TEXT NOT NULL UNIQUE,
|
||||
context_window_size INTEGER NOT NULL,
|
||||
display_name TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Message token usage table to track token usage per message
|
||||
CREATE TABLE IF NOT EXISTS message_token_usage (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
output_id INTEGER NOT NULL,
|
||||
message_type TEXT NOT NULL CHECK (message_type IN ('user', 'assistant', 'system')),
|
||||
input_tokens INTEGER DEFAULT 0,
|
||||
output_tokens INTEGER DEFAULT 0,
|
||||
total_tokens INTEGER DEFAULT 0,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (output_id) REFERENCES session_outputs(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Session token summary table for quick access to cumulative usage
|
||||
CREATE TABLE IF NOT EXISTS session_token_summary (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
total_input_tokens INTEGER DEFAULT 0,
|
||||
total_output_tokens INTEGER DEFAULT 0,
|
||||
total_tokens INTEGER DEFAULT 0,
|
||||
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Index for faster lookups
|
||||
CREATE INDEX IF NOT EXISTS idx_session_outputs_session_id ON session_outputs(session_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_session_outputs_timestamp ON session_outputs(timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_messages_session_id ON conversation_messages(session_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_messages_timestamp ON conversation_messages(timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_messages_timestamp ON conversation_messages(timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_message_token_usage_session_id ON message_token_usage(session_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_message_token_usage_output_id ON message_token_usage(output_id);
|
||||
@ -11,7 +11,8 @@ export function setupEventListeners(services: AppServices, getMainWindow: () =>
|
||||
runCommandManager,
|
||||
gitDiffManager,
|
||||
gitStatusManager,
|
||||
worktreeManager
|
||||
worktreeManager,
|
||||
databaseService
|
||||
} = services;
|
||||
|
||||
// Listen to sessionManager events and broadcast to renderer
|
||||
@ -101,12 +102,18 @@ export function setupEventListeners(services: AppServices, getMainWindow: () =>
|
||||
// Listen to claudeCodeManager events
|
||||
claudeCodeManager.on('output', async (output: any) => {
|
||||
// Save raw output to database (including JSON)
|
||||
sessionManager.addSessionOutput(output.sessionId, {
|
||||
const outputId = sessionManager.addSessionOutput(output.sessionId, {
|
||||
type: output.type,
|
||||
data: output.data,
|
||||
timestamp: output.timestamp
|
||||
});
|
||||
|
||||
// Track token usage for JSON messages
|
||||
if (output.type === 'json') {
|
||||
const { trackTokenUsage } = await import('./utils/tokenTracker');
|
||||
await trackTokenUsage(databaseService, output.sessionId, outputId, output.data);
|
||||
}
|
||||
|
||||
// Check if Claude is waiting for user input
|
||||
if (output.type === 'json' && output.data.type === 'prompt') {
|
||||
console.log(`[Main] Claude is waiting for user input in session ${output.sessionId}`);
|
||||
|
||||
@ -1,619 +1,7 @@
|
||||
import { IpcMain } from 'electron';
|
||||
import { execSync, execAsync } from '../utils/commandExecutor';
|
||||
import { formatForDatabase } from '../utils/timestampUtils';
|
||||
import type { IpcMain } from 'electron';
|
||||
import type { AppServices } from './types';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
interface MainBranchStatus {
|
||||
status: 'up-to-date' | 'behind' | 'ahead' | 'diverged';
|
||||
aheadCount?: number;
|
||||
behindCount?: number;
|
||||
lastFetched: string;
|
||||
}
|
||||
|
||||
interface SessionBranchInfo {
|
||||
sessionId: string;
|
||||
sessionName: string;
|
||||
branchName: string;
|
||||
worktreePath: string;
|
||||
baseCommit: string;
|
||||
baseBranch: string;
|
||||
isStale: boolean;
|
||||
staleSince?: string;
|
||||
hasUncommittedChanges: boolean;
|
||||
pullRequest?: {
|
||||
number: number;
|
||||
title: string;
|
||||
state: 'open' | 'closed' | 'merged';
|
||||
url: string;
|
||||
};
|
||||
commitsAhead: number;
|
||||
commitsBehind: number;
|
||||
}
|
||||
|
||||
interface RemoteStatus {
|
||||
name: string;
|
||||
url: string;
|
||||
branch: string;
|
||||
status: 'up-to-date' | 'behind' | 'ahead' | 'diverged';
|
||||
aheadCount: number;
|
||||
behindCount: number;
|
||||
isUpstream?: boolean;
|
||||
isFork?: boolean;
|
||||
}
|
||||
|
||||
interface ProjectDashboardData {
|
||||
projectId: number;
|
||||
projectName: string;
|
||||
projectPath: string;
|
||||
mainBranch: string;
|
||||
mainBranchStatus: MainBranchStatus;
|
||||
remotes?: RemoteStatus[];
|
||||
sessionBranches: SessionBranchInfo[];
|
||||
lastRefreshed: string;
|
||||
}
|
||||
|
||||
export function registerDashboardHandlers(ipcMain: IpcMain, services: AppServices) {
|
||||
const { databaseService, worktreeManager } = services;
|
||||
|
||||
ipcMain.handle('dashboard:get-project-status', async (_event, projectId: number) => {
|
||||
try {
|
||||
// Get project details
|
||||
const project = databaseService.getProject(projectId);
|
||||
if (!project) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Project not found'
|
||||
};
|
||||
}
|
||||
|
||||
// Ensure the project path exists and is a git repository
|
||||
if (!fs.existsSync(project.path)) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Project path does not exist'
|
||||
};
|
||||
}
|
||||
|
||||
const gitDir = path.join(project.path, '.git');
|
||||
if (!fs.existsSync(gitDir)) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Project is not a git repository'
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch latest changes from remote (async to prevent blocking)
|
||||
try {
|
||||
await execAsync('git fetch origin', { cwd: project.path, timeout: 15000 });
|
||||
} catch (error) {
|
||||
console.warn('Failed to fetch from origin:', error);
|
||||
// Continue anyway - we can still show local status
|
||||
}
|
||||
|
||||
// Get the main branch name dynamically
|
||||
const mainBranch = await worktreeManager.getProjectMainBranch(project.path);
|
||||
|
||||
// Get main branch status (async)
|
||||
const mainBranchStatus = await getMainBranchStatusAsync(project.path, mainBranch);
|
||||
|
||||
// Get remote statuses (async)
|
||||
const remotes = await getRemoteStatuses(project.path, mainBranch);
|
||||
|
||||
// Get all sessions for this project
|
||||
const sessions = databaseService.getAllSessions(projectId);
|
||||
const sessionBranches: SessionBranchInfo[] = [];
|
||||
|
||||
// Process sessions in batches to prevent overwhelming git
|
||||
const BATCH_SIZE = 5;
|
||||
for (let i = 0; i < sessions.length; i += BATCH_SIZE) {
|
||||
const batch = sessions.slice(i, i + BATCH_SIZE);
|
||||
const batchPromises = batch
|
||||
.filter(session => !session.archived) // Skip archived sessions
|
||||
.map(async (session) => {
|
||||
try {
|
||||
return await getSessionBranchInfoAsync(
|
||||
session,
|
||||
project.path,
|
||||
mainBranch
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`Failed to get branch info for session ${session.id}:`, error);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
const batchResults = await Promise.all(batchPromises);
|
||||
sessionBranches.push(...batchResults.filter(result => result !== null));
|
||||
}
|
||||
|
||||
const dashboardData: ProjectDashboardData = {
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
projectPath: project.path,
|
||||
mainBranch,
|
||||
mainBranchStatus,
|
||||
remotes: remotes.length > 0 ? remotes : undefined,
|
||||
sessionBranches,
|
||||
lastRefreshed: formatForDatabase()
|
||||
};
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: dashboardData
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting project dashboard status:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to get project status'
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getMainBranchStatus(projectPath: string, mainBranch: string): MainBranchStatus {
|
||||
try {
|
||||
// Check if we have a remote tracking branch
|
||||
const remoteBranch = `origin/${mainBranch}`;
|
||||
|
||||
// Check if remote branch exists
|
||||
try {
|
||||
execSync(`git rev-parse --verify ${remoteBranch}`, { cwd: projectPath });
|
||||
} catch {
|
||||
// No remote branch
|
||||
return {
|
||||
status: 'up-to-date',
|
||||
lastFetched: formatForDatabase()
|
||||
};
|
||||
}
|
||||
|
||||
// Get commit counts
|
||||
const aheadBehind = execSync(
|
||||
`git rev-list --left-right --count ${mainBranch}...${remoteBranch}`,
|
||||
{ cwd: projectPath }
|
||||
).toString().trim();
|
||||
|
||||
const [ahead, behind] = aheadBehind.split('\t').map((n: string) => parseInt(n, 10));
|
||||
|
||||
let status: MainBranchStatus['status'];
|
||||
if (ahead === 0 && behind === 0) {
|
||||
status = 'up-to-date';
|
||||
} else if (ahead > 0 && behind === 0) {
|
||||
status = 'ahead';
|
||||
} else if (ahead === 0 && behind > 0) {
|
||||
status = 'behind';
|
||||
} else {
|
||||
status = 'diverged';
|
||||
}
|
||||
|
||||
return {
|
||||
status,
|
||||
aheadCount: ahead || undefined,
|
||||
behindCount: behind || undefined,
|
||||
lastFetched: formatForDatabase()
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting main branch status:', error);
|
||||
return {
|
||||
status: 'up-to-date',
|
||||
lastFetched: formatForDatabase()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function getRemoteStatuses(projectPath: string, mainBranch: string): Promise<RemoteStatus[]> {
|
||||
try {
|
||||
// Get all remotes
|
||||
const remotesResult = await execAsync('git remote -v', { cwd: projectPath, timeout: 5000 });
|
||||
const remoteLines = remotesResult.stdout.trim().split('\n');
|
||||
|
||||
// Parse unique remotes
|
||||
const remotesMap = new Map<string, string>();
|
||||
for (const line of remoteLines) {
|
||||
const [name, url] = line.split('\t');
|
||||
if (name && url && url.includes('(fetch)')) {
|
||||
remotesMap.set(name, url.replace(' (fetch)', ''));
|
||||
}
|
||||
}
|
||||
|
||||
const remoteStatuses: RemoteStatus[] = [];
|
||||
|
||||
// Fetch from all remotes
|
||||
for (const [remoteName, remoteUrl] of remotesMap) {
|
||||
try {
|
||||
await execAsync(`git fetch ${remoteName}`, { cwd: projectPath, timeout: 10000 });
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch from ${remoteName}:`, error);
|
||||
}
|
||||
|
||||
// Check if remote branch exists
|
||||
try {
|
||||
await execAsync(`git rev-parse --verify ${remoteName}/${mainBranch}`, { cwd: projectPath, timeout: 2000 });
|
||||
|
||||
// Get ahead/behind status
|
||||
const result = await execAsync(
|
||||
`git rev-list --left-right --count ${mainBranch}...${remoteName}/${mainBranch}`,
|
||||
{ cwd: projectPath, timeout: 5000 }
|
||||
);
|
||||
const [ahead, behind] = result.stdout.trim().split('\t').map(n => parseInt(n, 10));
|
||||
|
||||
let status: RemoteStatus['status'];
|
||||
if (ahead === 0 && behind === 0) {
|
||||
status = 'up-to-date';
|
||||
} else if (ahead > 0 && behind === 0) {
|
||||
status = 'ahead';
|
||||
} else if (ahead === 0 && behind > 0) {
|
||||
status = 'behind';
|
||||
} else {
|
||||
status = 'diverged';
|
||||
}
|
||||
|
||||
// Detect if this is upstream or a fork
|
||||
const isUpstream = remoteName === 'upstream' || remoteUrl.includes('/upstream/');
|
||||
const isFork = !isUpstream && remoteName === 'origin' && remotesMap.has('upstream');
|
||||
|
||||
remoteStatuses.push({
|
||||
name: remoteName,
|
||||
url: remoteUrl,
|
||||
branch: mainBranch,
|
||||
status,
|
||||
aheadCount: ahead,
|
||||
behindCount: behind,
|
||||
isUpstream,
|
||||
isFork
|
||||
});
|
||||
} catch {
|
||||
// Remote branch doesn't exist
|
||||
console.warn(`Remote branch ${remoteName}/${mainBranch} not found`);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort remotes: upstream first, then origin, then others
|
||||
remoteStatuses.sort((a, b) => {
|
||||
if (a.isUpstream) return -1;
|
||||
if (b.isUpstream) return 1;
|
||||
if (a.name === 'origin') return -1;
|
||||
if (b.name === 'origin') return 1;
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
|
||||
return remoteStatuses;
|
||||
} catch (error) {
|
||||
console.error('Error getting remote statuses:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function getMainBranchStatusAsync(projectPath: string, mainBranch: string): Promise<MainBranchStatus> {
|
||||
try {
|
||||
// Check if we have a remote tracking branch
|
||||
const remoteBranch = `origin/${mainBranch}`;
|
||||
|
||||
// Check if remote branch exists
|
||||
try {
|
||||
await execAsync(`git rev-parse --verify ${remoteBranch}`, { cwd: projectPath, timeout: 5000 });
|
||||
} catch {
|
||||
// No remote branch
|
||||
return {
|
||||
status: 'up-to-date',
|
||||
lastFetched: formatForDatabase()
|
||||
};
|
||||
}
|
||||
|
||||
// Get commit counts
|
||||
const result = await execAsync(
|
||||
`git rev-list --left-right --count ${mainBranch}...${remoteBranch}`,
|
||||
{ cwd: projectPath, timeout: 5000 }
|
||||
);
|
||||
const aheadBehind = result.stdout.trim();
|
||||
|
||||
const [ahead, behind] = aheadBehind.split('\t').map((n: string) => parseInt(n, 10));
|
||||
|
||||
let status: MainBranchStatus['status'];
|
||||
if (ahead === 0 && behind === 0) {
|
||||
status = 'up-to-date';
|
||||
} else if (ahead > 0 && behind === 0) {
|
||||
status = 'ahead';
|
||||
} else if (ahead === 0 && behind > 0) {
|
||||
status = 'behind';
|
||||
} else {
|
||||
status = 'diverged';
|
||||
}
|
||||
|
||||
return {
|
||||
status,
|
||||
aheadCount: ahead || undefined,
|
||||
behindCount: behind || undefined,
|
||||
lastFetched: formatForDatabase()
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting main branch status:', error);
|
||||
return {
|
||||
status: 'up-to-date',
|
||||
lastFetched: formatForDatabase()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function getSessionBranchInfo(
|
||||
session: { id: string; name: string; worktree_path: string; base_commit?: string; base_branch?: string },
|
||||
projectPath: string,
|
||||
mainBranch: string
|
||||
): Promise<SessionBranchInfo | null> {
|
||||
try {
|
||||
// Check if worktree still exists
|
||||
if (!fs.existsSync(session.worktree_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the branch name from the worktree
|
||||
const branchName = execSync('git branch --show-current', {
|
||||
cwd: session.worktree_path
|
||||
}).toString().trim();
|
||||
|
||||
if (!branchName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the base commit this branch was created from
|
||||
// This is stored in the session metadata or we can infer it
|
||||
let baseCommit = session.base_commit;
|
||||
const baseBranch = session.base_branch || mainBranch;
|
||||
|
||||
if (!baseCommit) {
|
||||
// Try to find the merge-base with main
|
||||
try {
|
||||
baseCommit = execSync(
|
||||
`git merge-base ${branchName} ${mainBranch}`,
|
||||
{ cwd: session.worktree_path }
|
||||
).toString().trim();
|
||||
} catch {
|
||||
baseCommit = 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
// Check if base branch has moved (session is stale)
|
||||
let isStale = false;
|
||||
let staleSince: string | undefined;
|
||||
|
||||
if (baseCommit && baseCommit !== 'unknown') {
|
||||
try {
|
||||
const currentBaseCommit = execSync(
|
||||
`git rev-parse ${baseBranch}`,
|
||||
{ cwd: projectPath }
|
||||
).toString().trim();
|
||||
|
||||
isStale = currentBaseCommit !== baseCommit;
|
||||
|
||||
if (isStale) {
|
||||
// Try to get the timestamp when the base branch moved
|
||||
const commitDate = execSync(
|
||||
`git log -1 --format=%cd --date=iso-strict ${currentBaseCommit}`,
|
||||
{ cwd: projectPath }
|
||||
).toString().trim();
|
||||
staleSince = commitDate;
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
}
|
||||
|
||||
// Check for uncommitted changes
|
||||
const hasUncommittedChanges = checkUncommittedChanges(session.worktree_path);
|
||||
|
||||
// Get commits ahead/behind
|
||||
let commitsAhead = 0;
|
||||
let commitsBehind = 0;
|
||||
|
||||
try {
|
||||
const aheadBehind = execSync(
|
||||
`git rev-list --left-right --count ${branchName}...${baseBranch}`,
|
||||
{ cwd: session.worktree_path }
|
||||
).toString().trim();
|
||||
|
||||
const [ahead, behind] = aheadBehind.split('\t').map((n: string) => parseInt(n, 10));
|
||||
commitsAhead = ahead;
|
||||
commitsBehind = behind;
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
|
||||
// Check for associated pull request
|
||||
let pullRequest: SessionBranchInfo['pullRequest'];
|
||||
try {
|
||||
const prOutput = execSync(
|
||||
`gh pr list --head ${branchName} --json number,title,state,url --limit 1`,
|
||||
{ cwd: projectPath }
|
||||
).toString().trim();
|
||||
|
||||
if (prOutput) {
|
||||
const prs = JSON.parse(prOutput);
|
||||
if (prs.length > 0) {
|
||||
const pr = prs[0];
|
||||
pullRequest = {
|
||||
number: pr.number,
|
||||
title: pr.title,
|
||||
state: pr.state.toLowerCase() as 'open' | 'closed' | 'merged',
|
||||
url: pr.url
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// gh command might not be available or configured
|
||||
}
|
||||
|
||||
return {
|
||||
sessionId: session.id,
|
||||
sessionName: session.name,
|
||||
branchName,
|
||||
worktreePath: session.worktree_path,
|
||||
baseCommit,
|
||||
baseBranch,
|
||||
isStale,
|
||||
staleSince,
|
||||
hasUncommittedChanges,
|
||||
pullRequest,
|
||||
commitsAhead,
|
||||
commitsBehind
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Error getting branch info for session ${session.id}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function getSessionBranchInfoAsync(
|
||||
session: { id: string; name: string; worktree_path: string; base_commit?: string; base_branch?: string },
|
||||
projectPath: string,
|
||||
mainBranch: string
|
||||
): Promise<SessionBranchInfo | null> {
|
||||
try {
|
||||
// Check if worktree still exists
|
||||
if (!fs.existsSync(session.worktree_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the branch name from the worktree
|
||||
const branchResult = await execAsync('git branch --show-current', {
|
||||
cwd: session.worktree_path,
|
||||
timeout: 5000
|
||||
});
|
||||
const branchName = branchResult.stdout.trim();
|
||||
|
||||
if (!branchName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the base commit this branch was created from
|
||||
// This is stored in the session metadata or we can infer it
|
||||
let baseCommit = session.base_commit;
|
||||
const baseBranch = session.base_branch || mainBranch;
|
||||
|
||||
if (!baseCommit) {
|
||||
// Try to find the merge-base with main
|
||||
try {
|
||||
const mergeBaseResult = await execAsync(
|
||||
`git merge-base ${branchName} ${mainBranch}`,
|
||||
{ cwd: session.worktree_path, timeout: 5000 }
|
||||
);
|
||||
baseCommit = mergeBaseResult.stdout.trim();
|
||||
} catch {
|
||||
baseCommit = 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
// Check if base branch has moved (session is stale)
|
||||
let isStale = false;
|
||||
let staleSince: string | undefined;
|
||||
|
||||
if (baseCommit && baseCommit !== 'unknown') {
|
||||
try {
|
||||
const currentBaseResult = await execAsync(
|
||||
`git rev-parse ${baseBranch}`,
|
||||
{ cwd: projectPath, timeout: 5000 }
|
||||
);
|
||||
const currentBaseCommit = currentBaseResult.stdout.trim();
|
||||
|
||||
isStale = currentBaseCommit !== baseCommit;
|
||||
|
||||
if (isStale) {
|
||||
// Try to get the timestamp when the base branch moved
|
||||
const commitDateResult = await execAsync(
|
||||
`git log -1 --format=%cd --date=iso-strict ${currentBaseCommit}`,
|
||||
{ cwd: projectPath, timeout: 5000 }
|
||||
);
|
||||
staleSince = commitDateResult.stdout.trim();
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
}
|
||||
|
||||
// Check for uncommitted changes (async)
|
||||
const hasUncommittedChanges = await checkUncommittedChangesAsync(session.worktree_path);
|
||||
|
||||
// Get commits ahead/behind
|
||||
let commitsAhead = 0;
|
||||
let commitsBehind = 0;
|
||||
|
||||
try {
|
||||
const aheadBehindResult = await execAsync(
|
||||
`git rev-list --left-right --count ${branchName}...${baseBranch}`,
|
||||
{ cwd: session.worktree_path, timeout: 5000 }
|
||||
);
|
||||
const aheadBehind = aheadBehindResult.stdout.trim();
|
||||
|
||||
const [ahead, behind] = aheadBehind.split('\t').map((n: string) => parseInt(n, 10));
|
||||
commitsAhead = ahead;
|
||||
commitsBehind = behind;
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
|
||||
// Check for associated pull request (async)
|
||||
let pullRequest: SessionBranchInfo['pullRequest'];
|
||||
try {
|
||||
const prResult = await execAsync(
|
||||
`gh pr list --head ${branchName} --json number,title,state,url --limit 1`,
|
||||
{ cwd: projectPath, timeout: 10000 }
|
||||
);
|
||||
const prOutput = prResult.stdout.trim();
|
||||
|
||||
if (prOutput) {
|
||||
const prs = JSON.parse(prOutput);
|
||||
if (prs.length > 0) {
|
||||
const pr = prs[0];
|
||||
pullRequest = {
|
||||
number: pr.number,
|
||||
title: pr.title,
|
||||
state: pr.state.toLowerCase() as 'open' | 'closed' | 'merged',
|
||||
url: pr.url
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// gh command might not be available or configured
|
||||
}
|
||||
|
||||
return {
|
||||
sessionId: session.id,
|
||||
sessionName: session.name,
|
||||
branchName,
|
||||
worktreePath: session.worktree_path,
|
||||
baseCommit,
|
||||
baseBranch,
|
||||
isStale,
|
||||
staleSince,
|
||||
hasUncommittedChanges,
|
||||
pullRequest,
|
||||
commitsAhead,
|
||||
commitsBehind
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Error getting branch info for session ${session.id}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function checkUncommittedChanges(worktreePath: string): boolean {
|
||||
try {
|
||||
const status = execSync('git status --porcelain', { cwd: worktreePath }).toString();
|
||||
return status.trim().length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function checkUncommittedChangesAsync(worktreePath: string): Promise<boolean> {
|
||||
try {
|
||||
const result = await execAsync('git status --porcelain', {
|
||||
cwd: worktreePath,
|
||||
timeout: 5000
|
||||
});
|
||||
return result.stdout.trim().length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
export function registerDashboardHandlers(_ipcMain: IpcMain, _services: AppServices): void {
|
||||
// Dashboard handlers placeholder
|
||||
// TODO: Implement dashboard-related IPC handlers
|
||||
}
|
||||
@ -472,28 +472,6 @@ export function registerSessionHandlers(ipcMain: IpcMain, services: AppServices)
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('sessions:get-json-messages', async (_event, sessionId: string) => {
|
||||
try {
|
||||
console.log(`[IPC] sessions:get-json-messages called for session: ${sessionId}`);
|
||||
const outputs = await sessionManager.getSessionOutputs(sessionId);
|
||||
console.log(`[IPC] Retrieved ${outputs.length} total outputs for session ${sessionId}`);
|
||||
|
||||
// Filter to only JSON messages and include timestamp
|
||||
const jsonMessages = outputs
|
||||
.filter(output => output.type === 'json')
|
||||
.map(output => ({
|
||||
...output.data,
|
||||
timestamp: output.timestamp.toISOString()
|
||||
}));
|
||||
|
||||
console.log(`[IPC] Found ${jsonMessages.length} JSON messages for session ${sessionId}`);
|
||||
return { success: true, data: jsonMessages };
|
||||
} catch (error) {
|
||||
console.error('Failed to get JSON messages:', error);
|
||||
return { success: false, error: 'Failed to get JSON messages' };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('sessions:mark-viewed', async (_event, sessionId: string) => {
|
||||
try {
|
||||
await sessionManager.markSessionAsViewed(sessionId);
|
||||
@ -700,4 +678,58 @@ export function registerSessionHandlers(ipcMain: IpcMain, services: AppServices)
|
||||
return { success: false, error: 'Failed to get table structure' };
|
||||
}
|
||||
});
|
||||
|
||||
// Token tracking handlers
|
||||
ipcMain.handle('sessions:get-token-usage', async (_event, sessionId: string) => {
|
||||
try {
|
||||
console.log('[IPC] Getting token usage for session:', sessionId);
|
||||
|
||||
const session = databaseService.getSession(sessionId);
|
||||
if (!session) {
|
||||
console.log('[IPC] Session not found:', sessionId);
|
||||
return { success: false, error: 'Session not found' };
|
||||
}
|
||||
|
||||
const { getSessionTokenUsageWithContext } = await import('../utils/tokenTracker');
|
||||
const tokenData = getSessionTokenUsageWithContext(
|
||||
databaseService,
|
||||
sessionId,
|
||||
session.model || 'claude-sonnet-4-20250514'
|
||||
);
|
||||
|
||||
console.log('[IPC] Token data retrieved:', {
|
||||
sessionId,
|
||||
hasSummary: !!tokenData?.summary,
|
||||
summary: tokenData?.summary,
|
||||
contextWindow: tokenData?.contextWindow,
|
||||
warning: tokenData?.warning,
|
||||
currentContextUsage: tokenData?.currentContextUsage
|
||||
});
|
||||
|
||||
return { success: true, data: tokenData };
|
||||
} catch (error) {
|
||||
console.error('[IPC] Failed to get token usage:', error);
|
||||
return { success: false, error: 'Failed to get token usage' };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('sessions:get-token-history', async (_event, sessionId: string) => {
|
||||
try {
|
||||
const history = databaseService.getSessionTokenUsageHistory(sessionId);
|
||||
return { success: true, data: history };
|
||||
} catch (error) {
|
||||
console.error('Failed to get token history:', error);
|
||||
return { success: false, error: 'Failed to get token history' };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('models:get-context-windows', async () => {
|
||||
try {
|
||||
const models = databaseService.getAllModelContextWindows();
|
||||
return { success: true, data: models };
|
||||
} catch (error) {
|
||||
console.error('Failed to get model context windows:', error);
|
||||
return { success: false, error: 'Failed to get model context windows' };
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -140,6 +140,10 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
|
||||
// Image operations
|
||||
saveImages: (sessionId: string, images: Array<{ name: string; dataUrl: string; type: string }>): Promise<string[]> => ipcRenderer.invoke('sessions:save-images', sessionId, images),
|
||||
|
||||
// Token tracking
|
||||
getTokenUsage: (sessionId: string): Promise<IPCResponse> => ipcRenderer.invoke('sessions:get-token-usage', sessionId),
|
||||
getTokenHistory: (sessionId: string): Promise<IPCResponse> => ipcRenderer.invoke('sessions:get-token-history', sessionId),
|
||||
},
|
||||
|
||||
// Project management
|
||||
@ -359,6 +363,11 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
},
|
||||
},
|
||||
|
||||
// Model context windows
|
||||
models: {
|
||||
getContextWindows: (): Promise<IPCResponse> => ipcRenderer.invoke('models:get-context-windows'),
|
||||
},
|
||||
|
||||
// Debug utilities
|
||||
debug: {
|
||||
getTableStructure: (tableName: 'folders' | 'sessions'): Promise<IPCResponse> => ipcRenderer.invoke('debug:get-table-structure', tableName),
|
||||
|
||||
@ -312,14 +312,14 @@ export class SessionManager extends EventEmitter {
|
||||
this.emit('session-updated', session);
|
||||
}
|
||||
|
||||
addSessionOutput(id: string, output: Omit<SessionOutput, 'sessionId'>): void {
|
||||
addSessionOutput(id: string, output: Omit<SessionOutput, 'sessionId'>): number {
|
||||
// Check if this is the first output for this session
|
||||
const existingOutputs = this.db.getSessionOutputs(id, 1);
|
||||
const isFirstOutput = existingOutputs.length === 0;
|
||||
|
||||
// Store in database (stringify JSON objects)
|
||||
const dataToStore = output.type === 'json' ? JSON.stringify(output.data) : output.data;
|
||||
this.db.addSessionOutput(id, output.type, dataToStore);
|
||||
const outputId = this.db.addSessionOutput(id, output.type, dataToStore);
|
||||
|
||||
// Emit the output so it shows immediately in the UI
|
||||
const outputToEmit: SessionOutput = {
|
||||
@ -428,6 +428,8 @@ export class SessionManager extends EventEmitter {
|
||||
};
|
||||
|
||||
this.emit('session-output', fullOutput);
|
||||
|
||||
return outputId;
|
||||
}
|
||||
|
||||
getSessionOutput(id: string, limit?: number): SessionOutput[] {
|
||||
|
||||
@ -175,9 +175,8 @@ export class TaskQueue {
|
||||
run_script: targetProject.run_script
|
||||
}, null, 2));
|
||||
|
||||
const { worktreePath, baseCommit, baseBranch: actualBaseBranch } = await worktreeManager.createWorktree(targetProject.path, worktreeName, undefined, baseBranch, targetProject.worktree_folder);
|
||||
const { worktreePath } = await worktreeManager.createWorktree(targetProject.path, worktreeName, undefined, baseBranch, targetProject.worktree_folder);
|
||||
console.log(`[TaskQueue] Worktree created at: ${worktreePath}`);
|
||||
console.log(`[TaskQueue] Base commit: ${baseCommit}, Base branch: ${actualBaseBranch}`);
|
||||
|
||||
const sessionName = worktreeName;
|
||||
console.log(`[TaskQueue] Creating session in database`);
|
||||
@ -193,8 +192,8 @@ export class TaskQueue {
|
||||
autoCommit,
|
||||
job.data.folderId,
|
||||
model,
|
||||
baseCommit,
|
||||
actualBaseBranch,
|
||||
undefined, // baseCommit - optional parameter
|
||||
baseBranch, // actualBaseBranch -> baseBranch
|
||||
job.data.commitMode,
|
||||
job.data.commitModeSettings
|
||||
);
|
||||
|
||||
@ -52,7 +52,7 @@ export class WorktreeManager {
|
||||
}
|
||||
}
|
||||
|
||||
async createWorktree(projectPath: string, name: string, branch?: string, baseBranch?: string, worktreeFolder?: string): Promise<{ worktreePath: string; baseCommit: string; baseBranch: string }> {
|
||||
async createWorktree(projectPath: string, name: string, branch?: string, baseBranch?: string, worktreeFolder?: string): Promise<{ worktreePath: string }> {
|
||||
console.log(`[WorktreeManager] Creating worktree: ${name} in project: ${projectPath}`);
|
||||
|
||||
const { baseDir } = this.getProjectPaths(projectPath, worktreeFolder);
|
||||
@ -120,22 +120,13 @@ export class WorktreeManager {
|
||||
// Branch doesn't exist, will create it
|
||||
}
|
||||
|
||||
// Capture the base commit before creating worktree
|
||||
let baseCommit: string;
|
||||
let actualBaseBranch: string;
|
||||
|
||||
if (branchExists) {
|
||||
// Use existing branch
|
||||
console.log(`[WorktreeManager] Adding worktree with existing branch...`);
|
||||
await execWithShellPath(`git worktree add "${worktreePath}" ${branchName}`, { cwd: projectPath });
|
||||
|
||||
// Get the commit this branch is based on
|
||||
baseCommit = (await execWithShellPath(`git rev-parse ${branchName}`, { cwd: projectPath })).stdout.trim();
|
||||
actualBaseBranch = branchName;
|
||||
} else {
|
||||
// Create new branch from specified base branch (or current HEAD if not specified)
|
||||
const baseRef = baseBranch || 'HEAD';
|
||||
actualBaseBranch = baseBranch || 'HEAD';
|
||||
console.log(`[WorktreeManager] Creating new branch from ${baseRef} and adding worktree...`);
|
||||
|
||||
// Verify that the base branch exists if specified
|
||||
@ -148,16 +139,12 @@ export class WorktreeManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Capture the base commit before creating the worktree
|
||||
baseCommit = (await execWithShellPath(`git rev-parse ${baseRef}`, { cwd: projectPath })).stdout.trim();
|
||||
console.log(`[WorktreeManager] Base commit: ${baseCommit}`);
|
||||
|
||||
await execWithShellPath(`git worktree add -b ${branchName} "${worktreePath}" ${baseRef}`, { cwd: projectPath });
|
||||
}
|
||||
|
||||
console.log(`[WorktreeManager] Worktree created successfully at: ${worktreePath}`);
|
||||
|
||||
return { worktreePath, baseCommit, baseBranch: actualBaseBranch };
|
||||
return { worktreePath };
|
||||
} catch (error) {
|
||||
console.error(`[WorktreeManager] Failed to create worktree:`, error);
|
||||
throw new Error(`Failed to create worktree: ${error instanceof Error ? error.message : String(error)}`);
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
import { execSync as nodeExecSync, ExecSyncOptions, ExecSyncOptionsWithStringEncoding, ExecSyncOptionsWithBufferEncoding, exec, ExecOptions } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import { execSync as nodeExecSync, ExecSyncOptions, ExecSyncOptionsWithStringEncoding, ExecSyncOptionsWithBufferEncoding } from 'child_process';
|
||||
import { getShellPath } from './shellPath';
|
||||
|
||||
const nodeExecAsync = promisify(exec);
|
||||
|
||||
class CommandExecutor {
|
||||
execSync(command: string, options: ExecSyncOptionsWithStringEncoding): string;
|
||||
execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
|
||||
@ -46,56 +43,10 @@ class CommandExecutor {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async execAsync(command: string, options?: ExecOptions & { timeout?: number }): Promise<{ stdout: string; stderr: string }> {
|
||||
// Log the command being executed
|
||||
const cwd = options?.cwd || process.cwd();
|
||||
console.log(`[CommandExecutor] Executing async: ${command} in ${cwd}`);
|
||||
|
||||
// Get enhanced shell PATH
|
||||
const shellPath = getShellPath();
|
||||
|
||||
// Set up timeout (default 10 seconds)
|
||||
const timeout = options?.timeout || 10000;
|
||||
|
||||
// Merge enhanced PATH into options
|
||||
const enhancedOptions: ExecOptions = {
|
||||
...options,
|
||||
timeout,
|
||||
env: {
|
||||
...process.env,
|
||||
...options?.env,
|
||||
PATH: shellPath
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await nodeExecAsync(command, enhancedOptions);
|
||||
|
||||
// Log success with a preview of the result
|
||||
if (result.stdout) {
|
||||
const lines = result.stdout.split('\n');
|
||||
const preview = lines[0].substring(0, 100) +
|
||||
(lines.length > 1 ? ` ... (${lines.length} lines)` : '');
|
||||
console.log(`[CommandExecutor] Async Success: ${preview}`);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
// Log error
|
||||
console.error(`[CommandExecutor] Async Failed: ${command}`);
|
||||
console.error(`[CommandExecutor] Async Error: ${error.message}`);
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export a singleton instance
|
||||
export const commandExecutor = new CommandExecutor();
|
||||
|
||||
// Export the execSync function as a drop-in replacement
|
||||
export const execSync = commandExecutor.execSync.bind(commandExecutor);
|
||||
|
||||
// Export the execAsync function
|
||||
export const execAsync = commandExecutor.execAsync.bind(commandExecutor);
|
||||
export const execSync = commandExecutor.execSync.bind(commandExecutor);
|
||||
69
main/src/utils/modelContextWindows.ts
Normal file
69
main/src/utils/modelContextWindows.ts
Normal file
@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Model Context Window Configuration
|
||||
* Defines the context window sizes for different Claude models
|
||||
*/
|
||||
|
||||
export interface ModelInfo {
|
||||
name: string;
|
||||
displayName: string;
|
||||
contextWindow: number;
|
||||
}
|
||||
|
||||
// Model context window sizes (in tokens)
|
||||
export const MODEL_CONTEXT_WINDOWS: Record<string, ModelInfo> = {
|
||||
'claude-3-5-haiku-20241022': {
|
||||
name: 'claude-3-5-haiku-20241022',
|
||||
displayName: 'Claude 3.5 Haiku',
|
||||
contextWindow: 200000
|
||||
},
|
||||
'claude-sonnet-4-20250514': {
|
||||
name: 'claude-sonnet-4-20250514',
|
||||
displayName: 'Claude Sonnet 4',
|
||||
contextWindow: 200000
|
||||
},
|
||||
'claude-opus-4-20250514': {
|
||||
name: 'claude-opus-4-20250514',
|
||||
displayName: 'Claude Opus 4',
|
||||
contextWindow: 200000
|
||||
}
|
||||
};
|
||||
|
||||
// Default context window if model is not found
|
||||
export const DEFAULT_CONTEXT_WINDOW = 200000;
|
||||
|
||||
/**
|
||||
* Get the context window size for a given model
|
||||
* @param modelName The name of the Claude model
|
||||
* @returns The context window size in tokens
|
||||
*/
|
||||
export function getModelContextWindow(modelName: string): number {
|
||||
const modelInfo = MODEL_CONTEXT_WINDOWS[modelName];
|
||||
return modelInfo ? modelInfo.contextWindow : DEFAULT_CONTEXT_WINDOW;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the display name for a given model
|
||||
* @param modelName The name of the Claude model
|
||||
* @returns The human-friendly display name
|
||||
*/
|
||||
export function getModelDisplayName(modelName: string): string {
|
||||
const modelInfo = MODEL_CONTEXT_WINDOWS[modelName];
|
||||
return modelInfo ? modelInfo.displayName : modelName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available models
|
||||
* @returns Array of model information
|
||||
*/
|
||||
export function getAllModels(): ModelInfo[] {
|
||||
return Object.values(MODEL_CONTEXT_WINDOWS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a model name is valid
|
||||
* @param modelName The name of the Claude model
|
||||
* @returns True if the model is recognized
|
||||
*/
|
||||
export function isValidModel(modelName: string): boolean {
|
||||
return modelName in MODEL_CONTEXT_WINDOWS;
|
||||
}
|
||||
320
main/src/utils/tokenTracker.ts
Normal file
320
main/src/utils/tokenTracker.ts
Normal file
@ -0,0 +1,320 @@
|
||||
/**
|
||||
* Token Tracking Utilities
|
||||
* Extracts and processes token usage information from Claude's JSON messages
|
||||
*/
|
||||
|
||||
import { DatabaseService } from '../database/database';
|
||||
import { getModelContextWindow } from './modelContextWindows';
|
||||
import type { MessageTokenUsage, SessionTokenSummary } from '../database/models';
|
||||
|
||||
export interface TokenUsageData {
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
totalTokens: number;
|
||||
contextWindowTokens?: number; // Actual context window usage from cache data
|
||||
}
|
||||
|
||||
export interface TokenUsageWarning {
|
||||
level: 'info' | 'warning' | 'critical';
|
||||
percentage: number;
|
||||
message: string;
|
||||
tokensUsed: number;
|
||||
contextWindow: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract token usage from a Claude JSON message
|
||||
* @param jsonMessage The JSON message from Claude
|
||||
* @returns Token usage data if available, null otherwise
|
||||
*/
|
||||
export function extractTokenUsage(jsonMessage: any): TokenUsageData | null {
|
||||
// Check if this is an assistant message with usage information
|
||||
// The usage can be directly on the message or nested inside message.usage
|
||||
if (jsonMessage.type === 'assistant') {
|
||||
// Check for nested structure (common in Crystal's stored messages)
|
||||
const usage = jsonMessage.message?.usage || jsonMessage.usage;
|
||||
|
||||
if (usage) {
|
||||
// Claude's usage field contains input_tokens (new tokens) and output_tokens
|
||||
// Cache tokens are cumulative context state, not new tokens to add
|
||||
const inputTokens = usage.input_tokens || 0;
|
||||
const outputTokens = usage.output_tokens || 0;
|
||||
|
||||
// Calculate actual context window usage from cache data
|
||||
const cacheCreation = usage.cache_creation_input_tokens || 0;
|
||||
const cacheRead = usage.cache_read_input_tokens || 0;
|
||||
const contextWindowTokens = inputTokens + cacheCreation + cacheRead;
|
||||
|
||||
if (inputTokens > 0 || outputTokens > 0) {
|
||||
return {
|
||||
inputTokens,
|
||||
outputTokens,
|
||||
totalTokens: inputTokens + outputTokens,
|
||||
contextWindowTokens: contextWindowTokens > 0 ? contextWindowTokens : undefined
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Some messages might have usage in a different format
|
||||
if (jsonMessage.type === 'system' && jsonMessage.subtype === 'result') {
|
||||
const usage = jsonMessage.message?.usage || jsonMessage.usage;
|
||||
if (usage) {
|
||||
// Only count actual new input tokens, not cumulative cache state
|
||||
const inputTokens = usage.input_tokens || 0;
|
||||
const outputTokens = usage.output_tokens || 0;
|
||||
|
||||
// Calculate context window usage for system/result messages too
|
||||
const cacheCreation = usage.cache_creation_input_tokens || 0;
|
||||
const cacheRead = usage.cache_read_input_tokens || 0;
|
||||
const contextWindowTokens = inputTokens + cacheCreation + cacheRead;
|
||||
|
||||
return {
|
||||
inputTokens,
|
||||
outputTokens,
|
||||
totalTokens: usage.total_tokens || (inputTokens + outputTokens),
|
||||
contextWindowTokens: contextWindowTokens > 0 ? contextWindowTokens : undefined
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the message type from a JSON message
|
||||
* @param jsonMessage The JSON message from Claude
|
||||
* @returns The message type
|
||||
*/
|
||||
export function getMessageType(jsonMessage: any): 'user' | 'assistant' | 'system' {
|
||||
if (jsonMessage.type === 'user') return 'user';
|
||||
if (jsonMessage.type === 'assistant') return 'assistant';
|
||||
return 'system';
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate token usage warning level based on percentage of context window used
|
||||
* @param tokensUsed Total tokens used in the session
|
||||
* @param contextWindow The model's context window size
|
||||
* @returns Warning information
|
||||
*/
|
||||
export function calculateTokenWarning(tokensUsed: number, contextWindow: number): TokenUsageWarning {
|
||||
const percentage = Math.round((tokensUsed / contextWindow) * 100);
|
||||
|
||||
if (percentage >= 95) {
|
||||
return {
|
||||
level: 'critical',
|
||||
percentage,
|
||||
message: `Critical: Session is at ${percentage}% of context limit. Consider starting a new session.`,
|
||||
tokensUsed,
|
||||
contextWindow
|
||||
};
|
||||
} else if (percentage >= 80) {
|
||||
return {
|
||||
level: 'warning',
|
||||
percentage,
|
||||
message: `Warning: Session is at ${percentage}% of context limit. Approaching maximum capacity.`,
|
||||
tokensUsed,
|
||||
contextWindow
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
level: 'info',
|
||||
percentage,
|
||||
message: `Using ${percentage}% of context window (${tokensUsed.toLocaleString()} / ${contextWindow.toLocaleString()} tokens)`,
|
||||
tokensUsed,
|
||||
contextWindow
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format token count for display
|
||||
* @param tokens Number of tokens
|
||||
* @returns Formatted string
|
||||
*/
|
||||
export function formatTokenCount(tokens: number): string {
|
||||
if (tokens >= 1000000) {
|
||||
return `${(tokens / 1000000).toFixed(1)}M`;
|
||||
} else if (tokens >= 1000) {
|
||||
return `${(tokens / 1000).toFixed(1)}K`;
|
||||
}
|
||||
return tokens.toLocaleString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format token usage summary for display
|
||||
* @param summary Token usage summary
|
||||
* @param contextWindow Context window size
|
||||
* @returns Formatted display string
|
||||
*/
|
||||
export function formatTokenSummary(summary: SessionTokenSummary, contextWindow: number): string {
|
||||
const percentage = Math.round((summary.total_tokens / contextWindow) * 100);
|
||||
const remaining = contextWindow - summary.total_tokens;
|
||||
|
||||
return `${formatTokenCount(summary.total_tokens)} / ${formatTokenCount(contextWindow)} tokens (${percentage}% used, ${formatTokenCount(remaining)} remaining)`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate tokens from text (rough approximation)
|
||||
* This is a simple heuristic - actual token count may vary
|
||||
* @param text The text to estimate tokens for
|
||||
* @returns Estimated token count
|
||||
*/
|
||||
export function estimateTokens(text: string): number {
|
||||
// Rough estimate: ~1 token per 4 characters (varies by model and content)
|
||||
return Math.ceil(text.length / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Track token usage for a session output
|
||||
* @param db Database service instance
|
||||
* @param sessionId Session ID
|
||||
* @param outputId Output ID
|
||||
* @param jsonMessage The JSON message containing usage data
|
||||
*/
|
||||
export async function trackTokenUsage(
|
||||
db: DatabaseService,
|
||||
sessionId: string,
|
||||
outputId: number,
|
||||
jsonMessage: any
|
||||
): Promise<void> {
|
||||
const usage = extractTokenUsage(jsonMessage);
|
||||
if (usage) {
|
||||
const messageType = getMessageType(jsonMessage);
|
||||
db.addMessageTokenUsage(
|
||||
sessionId,
|
||||
outputId,
|
||||
messageType,
|
||||
usage.inputTokens,
|
||||
usage.outputTokens
|
||||
);
|
||||
|
||||
// Also track context window usage if available
|
||||
if (usage.contextWindowTokens !== undefined) {
|
||||
// Update the latest context window state for this session
|
||||
db.updateSessionContextWindow(sessionId, usage.contextWindowTokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate current context window usage for a session
|
||||
* @param db Database service instance
|
||||
* @param sessionId Session ID
|
||||
* @param contextWindow The model's context window size
|
||||
* @returns Context window usage based on conversation tokens + estimated Claude Code overhead
|
||||
*/
|
||||
function getLatestContextFromResponses(db: DatabaseService, sessionId: string, contextWindow: number): number | null {
|
||||
console.log('[TokenTracker] Calculating context window usage for session:', sessionId);
|
||||
|
||||
// Get the conversation token summary (now properly calculated without cache tokens)
|
||||
const summary = db.getSessionTokenSummary(sessionId);
|
||||
if (!summary) {
|
||||
console.log('[TokenTracker] No token summary found');
|
||||
return null;
|
||||
}
|
||||
|
||||
const conversationTokens = summary.total_input_tokens + summary.total_output_tokens;
|
||||
|
||||
// Dynamic Claude Code system overhead estimation based on analysis of sessions that hit context limits:
|
||||
//
|
||||
// From analyzing 4 sessions that hit the 200K limit, we found system overhead varies with conversation size.
|
||||
// Known data points from sessions that reached exactly 200,000 tokens:
|
||||
const knownDataPoints = [
|
||||
{ convTokens: 96776, overhead: 103224 }, // theme-change-3-1
|
||||
{ convTokens: 119248, overhead: 80752 }, // theme-change
|
||||
{ convTokens: 124473, overhead: 75527 }, // auto-commit-modes-2
|
||||
{ convTokens: 163992, overhead: 36008 } // theme-change-2
|
||||
];
|
||||
|
||||
// Find the closest data point(s) to interpolate from
|
||||
let estimatedSystemOverhead: number;
|
||||
|
||||
if (conversationTokens <= knownDataPoints[0].convTokens) {
|
||||
// Smaller than our smallest sample - use the ratio from smallest sample
|
||||
const ratio = knownDataPoints[0].overhead / knownDataPoints[0].convTokens;
|
||||
estimatedSystemOverhead = Math.round(conversationTokens * ratio);
|
||||
} else if (conversationTokens >= knownDataPoints[knownDataPoints.length - 1].convTokens) {
|
||||
// Larger than our largest sample - use the ratio from largest sample
|
||||
const ratio = knownDataPoints[knownDataPoints.length - 1].overhead / knownDataPoints[knownDataPoints.length - 1].convTokens;
|
||||
estimatedSystemOverhead = Math.round(conversationTokens * ratio);
|
||||
} else {
|
||||
// Interpolate between the two closest data points
|
||||
let lowerPoint = knownDataPoints[0];
|
||||
let upperPoint = knownDataPoints[1];
|
||||
|
||||
for (let i = 0; i < knownDataPoints.length - 1; i++) {
|
||||
if (conversationTokens >= knownDataPoints[i].convTokens && conversationTokens <= knownDataPoints[i + 1].convTokens) {
|
||||
lowerPoint = knownDataPoints[i];
|
||||
upperPoint = knownDataPoints[i + 1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Linear interpolation
|
||||
const ratio = (conversationTokens - lowerPoint.convTokens) / (upperPoint.convTokens - lowerPoint.convTokens);
|
||||
estimatedSystemOverhead = Math.round(lowerPoint.overhead + ratio * (upperPoint.overhead - lowerPoint.overhead));
|
||||
}
|
||||
|
||||
const estimatedContext = conversationTokens + estimatedSystemOverhead;
|
||||
|
||||
console.log('[TokenTracker] Context calculation:', {
|
||||
conversationTokens,
|
||||
estimatedSystemOverhead,
|
||||
estimatedContext,
|
||||
contextWindow,
|
||||
sessionId
|
||||
});
|
||||
|
||||
return estimatedContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get session token usage with context window information
|
||||
* @param db Database service instance
|
||||
* @param sessionId Session ID
|
||||
* @param modelName Model name
|
||||
* @returns Token usage with context information
|
||||
*/
|
||||
export function getSessionTokenUsageWithContext(
|
||||
db: DatabaseService,
|
||||
sessionId: string,
|
||||
modelName: string
|
||||
): {
|
||||
summary: SessionTokenSummary | null;
|
||||
contextWindow: number;
|
||||
warning: TokenUsageWarning | null;
|
||||
currentContextUsage?: number | null;
|
||||
} {
|
||||
console.log('[TokenTracker] Getting token usage for session:', sessionId, 'model:', modelName);
|
||||
|
||||
const summary = db.getSessionTokenSummary(sessionId);
|
||||
console.log('[TokenTracker] Summary from DB:', summary);
|
||||
|
||||
const contextWindow = getModelContextWindow(modelName);
|
||||
console.log('[TokenTracker] Context window for model:', contextWindow);
|
||||
|
||||
// Get the latest context window usage from the most recent Claude response
|
||||
console.log('[TokenTracker] Getting latest context window usage for session:', sessionId);
|
||||
const latestContextUsage = getLatestContextFromResponses(db, sessionId, contextWindow);
|
||||
console.log('[TokenTracker] Latest context usage from responses:', latestContextUsage, typeof latestContextUsage);
|
||||
|
||||
let warning: TokenUsageWarning | null = null;
|
||||
const tokensForWarning = latestContextUsage || (summary?.total_tokens || 0);
|
||||
|
||||
if (tokensForWarning > 0) {
|
||||
warning = calculateTokenWarning(tokensForWarning, contextWindow);
|
||||
console.log('[TokenTracker] Warning calculated:', warning);
|
||||
}
|
||||
|
||||
const result = {
|
||||
summary: summary || null,
|
||||
contextWindow,
|
||||
warning,
|
||||
currentContextUsage: latestContextUsage
|
||||
};
|
||||
|
||||
console.log('[TokenTracker] Returning result:', result);
|
||||
return result;
|
||||
}
|
||||
@ -48,7 +48,9 @@
|
||||
"test:ci": "playwright test --config=playwright.ci.config.ts",
|
||||
"test:ci:minimal": "playwright test --config=playwright.ci.minimal.config.ts",
|
||||
"test:ui": "playwright test --ui",
|
||||
"test:headed": "playwright test --headed"
|
||||
"test:headed": "playwright test --headed",
|
||||
"backfill-tokens": "cd main && npx tsx ../scripts/backfill-tokens.ts",
|
||||
"backfill-tokens:dry": "cd main && npx tsx ../scripts/backfill-tokens.ts --dry-run"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/claude-code": "^1.0.33",
|
||||
|
||||
77
recalculate_tokens.sql
Normal file
77
recalculate_tokens.sql
Normal file
@ -0,0 +1,77 @@
|
||||
-- Recalculate token tracking for auto-commit-modes-2 session
|
||||
-- This script fixes the incorrect token counting that was including cache tokens as new tokens
|
||||
|
||||
-- Session ID for auto-commit-modes-2
|
||||
-- e48185d1-504a-497d-8893-03c232a6841c
|
||||
|
||||
-- First, let's clear the incorrect data
|
||||
DELETE FROM message_token_usage WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c';
|
||||
DELETE FROM session_token_summary WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c';
|
||||
DELETE FROM token_usage_history WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c';
|
||||
|
||||
-- Now let's recalculate with the corrected logic
|
||||
-- Extract only input_tokens and output_tokens (not cache tokens) from the raw data
|
||||
|
||||
INSERT INTO message_token_usage (session_id, output_id, message_type, input_tokens, output_tokens, total_tokens, timestamp)
|
||||
SELECT
|
||||
'e48185d1-504a-497d-8893-03c232a6841c' as session_id,
|
||||
id as output_id,
|
||||
'assistant' as message_type,
|
||||
COALESCE(json_extract(data, '$.usage.input_tokens'), json_extract(data, '$.message.usage.input_tokens'), 0) as input_tokens,
|
||||
COALESCE(json_extract(data, '$.usage.output_tokens'), json_extract(data, '$.message.usage.output_tokens'), 0) as output_tokens,
|
||||
(COALESCE(json_extract(data, '$.usage.input_tokens'), json_extract(data, '$.message.usage.input_tokens'), 0) +
|
||||
COALESCE(json_extract(data, '$.usage.output_tokens'), json_extract(data, '$.message.usage.output_tokens'), 0)) as total_tokens,
|
||||
timestamp
|
||||
FROM session_outputs
|
||||
WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c'
|
||||
AND type = 'json'
|
||||
AND (json_extract(data, '$.usage.input_tokens') > 0 OR json_extract(data, '$.message.usage.input_tokens') > 0);
|
||||
|
||||
-- Create the corrected session token summary
|
||||
INSERT INTO session_token_summary (session_id, total_input_tokens, total_output_tokens, total_tokens, last_updated)
|
||||
SELECT
|
||||
session_id,
|
||||
SUM(input_tokens) as total_input_tokens,
|
||||
SUM(output_tokens) as total_output_tokens,
|
||||
SUM(total_tokens) as total_tokens,
|
||||
datetime('now') as last_updated
|
||||
FROM message_token_usage
|
||||
WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c'
|
||||
GROUP BY session_id;
|
||||
|
||||
-- Now populate token_usage_history with context window data
|
||||
-- This uses cache data to show actual context window usage at each point
|
||||
INSERT INTO token_usage_history (
|
||||
session_id,
|
||||
output_id,
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
context_window_tokens,
|
||||
timestamp
|
||||
)
|
||||
SELECT
|
||||
'e48185d1-504a-497d-8893-03c232a6841c' as session_id,
|
||||
id as output_id,
|
||||
COALESCE(json_extract(data, '$.usage.input_tokens'), json_extract(data, '$.message.usage.input_tokens'), 0) as input_tokens,
|
||||
COALESCE(json_extract(data, '$.usage.output_tokens'), json_extract(data, '$.message.usage.output_tokens'), 0) as output_tokens,
|
||||
(COALESCE(json_extract(data, '$.usage.input_tokens'), json_extract(data, '$.message.usage.input_tokens'), 0) +
|
||||
COALESCE(json_extract(data, '$.usage.cache_creation_input_tokens'), json_extract(data, '$.message.usage.cache_creation_input_tokens'), 0) +
|
||||
COALESCE(json_extract(data, '$.usage.cache_read_input_tokens'), json_extract(data, '$.message.usage.cache_read_input_tokens'), 0)) as context_window_tokens,
|
||||
timestamp
|
||||
FROM session_outputs
|
||||
WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c'
|
||||
AND type = 'json'
|
||||
AND (json_extract(data, '$.usage.input_tokens') >= 0 OR json_extract(data, '$.message.usage.input_tokens') >= 0)
|
||||
ORDER BY id;
|
||||
|
||||
-- Show the results
|
||||
SELECT 'CORRECTED TOKEN SUMMARY' as info;
|
||||
SELECT * FROM session_token_summary WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c';
|
||||
|
||||
SELECT 'LATEST CONTEXT WINDOW USAGE' as info;
|
||||
SELECT context_window_tokens, timestamp
|
||||
FROM token_usage_history
|
||||
WHERE session_id = 'e48185d1-504a-497d-8893-03c232a6841c'
|
||||
AND context_window_tokens > 0
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 5;
|
||||
206
scripts/backfill-tokens.ts
Normal file
206
scripts/backfill-tokens.ts
Normal file
@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env ts-node
|
||||
|
||||
/**
|
||||
* One-off script to backfill token usage from existing sessions
|
||||
* This can be run manually if needed, but the same logic runs automatically
|
||||
* during database migrations.
|
||||
*
|
||||
* Usage: npx ts-node scripts/backfill-tokens.ts [--dry-run]
|
||||
*/
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import { extractTokenUsage, getMessageType } from '../main/src/utils/tokenTracker';
|
||||
|
||||
const isDryRun = process.argv.includes('--dry-run');
|
||||
const crystalDir = path.join(os.homedir(), '.crystal');
|
||||
const dbPath = path.join(crystalDir, 'crystal.db');
|
||||
|
||||
console.log('=== Token Usage Backfill Script ===');
|
||||
console.log(`Database: ${dbPath}`);
|
||||
console.log(`Mode: ${isDryRun ? 'DRY RUN' : 'LIVE'}`);
|
||||
console.log('');
|
||||
|
||||
// Open database
|
||||
const db = new Database(dbPath);
|
||||
|
||||
try {
|
||||
// Check if tables exist
|
||||
const tables = db.prepare(`
|
||||
SELECT name FROM sqlite_master
|
||||
WHERE type='table'
|
||||
AND name IN ('message_token_usage', 'session_token_summary')
|
||||
`).all() as Array<{ name: string }>;
|
||||
|
||||
if (tables.length !== 2) {
|
||||
console.error('โ Token tracking tables not found. Please run Crystal to apply migrations first.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Get statistics
|
||||
const stats = {
|
||||
totalSessions: db.prepare('SELECT COUNT(*) as count FROM sessions').get() as { count: number },
|
||||
totalJsonOutputs: db.prepare("SELECT COUNT(*) as count FROM session_outputs WHERE type = 'json'").get() as { count: number },
|
||||
existingTokenRecords: db.prepare('SELECT COUNT(*) as count FROM message_token_usage').get() as { count: number },
|
||||
};
|
||||
|
||||
console.log('๐ Current Statistics:');
|
||||
console.log(` Total sessions: ${stats.totalSessions.count}`);
|
||||
console.log(` Total JSON outputs: ${stats.totalJsonOutputs.count}`);
|
||||
console.log(` Existing token records: ${stats.existingTokenRecords.count}`);
|
||||
console.log('');
|
||||
|
||||
if (stats.existingTokenRecords.count > 0 && !process.argv.includes('--force')) {
|
||||
console.log('โ ๏ธ Token records already exist. Use --force to re-process all messages.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Get all JSON outputs
|
||||
const jsonOutputs = db.prepare(`
|
||||
SELECT so.id, so.session_id, so.data, s.model
|
||||
FROM session_outputs so
|
||||
JOIN sessions s ON s.id = so.session_id
|
||||
WHERE so.type = 'json'
|
||||
ORDER BY so.timestamp ASC
|
||||
`).all() as Array<{ id: number; session_id: string; data: string; model: string | null }>;
|
||||
|
||||
console.log(`๐ Found ${jsonOutputs.length} JSON messages to process`);
|
||||
|
||||
if (isDryRun) {
|
||||
console.log('\n--- DRY RUN MODE ---');
|
||||
console.log('Analyzing first 10 messages...\n');
|
||||
|
||||
const sample = jsonOutputs.slice(0, 10);
|
||||
for (const output of sample) {
|
||||
try {
|
||||
const jsonData = JSON.parse(output.data);
|
||||
const usage = extractTokenUsage(jsonData);
|
||||
|
||||
if (usage) {
|
||||
const messageType = getMessageType(jsonData);
|
||||
console.log(`โ Session ${output.session_id.substring(0, 8)}... | Type: ${messageType} | Tokens: ${usage.inputTokens} in, ${usage.outputTokens} out, ${usage.totalTokens} total`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`โ Session ${output.session_id.substring(0, 8)}... | Error parsing JSON`);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Process all outputs
|
||||
console.log('\n๐ Starting backfill...');
|
||||
|
||||
let processedCount = 0;
|
||||
let errorCount = 0;
|
||||
const sessionTotals = new Map<string, { input: number; output: number; total: number }>();
|
||||
|
||||
// Start transaction for better performance
|
||||
const insertStmt = db.prepare(`
|
||||
INSERT OR IGNORE INTO message_token_usage (session_id, output_id, message_type, input_tokens, output_tokens, total_tokens)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
const transaction = db.transaction(() => {
|
||||
for (const output of jsonOutputs) {
|
||||
try {
|
||||
const jsonData = JSON.parse(output.data);
|
||||
const usage = extractTokenUsage(jsonData);
|
||||
|
||||
if (usage) {
|
||||
const messageType = getMessageType(jsonData);
|
||||
|
||||
insertStmt.run(
|
||||
output.session_id,
|
||||
output.id,
|
||||
messageType,
|
||||
usage.inputTokens,
|
||||
usage.outputTokens,
|
||||
usage.totalTokens
|
||||
);
|
||||
|
||||
// Track totals per session
|
||||
const current = sessionTotals.get(output.session_id) || { input: 0, output: 0, total: 0 };
|
||||
sessionTotals.set(output.session_id, {
|
||||
input: current.input + usage.inputTokens,
|
||||
output: current.output + usage.outputTokens,
|
||||
total: current.total + usage.totalTokens
|
||||
});
|
||||
|
||||
processedCount++;
|
||||
|
||||
if (processedCount % 100 === 0) {
|
||||
process.stdout.write(`\r Processed ${processedCount}/${jsonOutputs.length} messages...`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
transaction();
|
||||
|
||||
console.log(`\nโ
Processed ${processedCount} messages with token usage (${errorCount} errors)`);
|
||||
|
||||
// Update session summaries
|
||||
console.log('\n๐ Updating session summaries...');
|
||||
|
||||
const updateStmt = db.prepare(`
|
||||
INSERT INTO session_token_summary (session_id, total_input_tokens, total_output_tokens, total_tokens)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(session_id) DO UPDATE SET
|
||||
total_input_tokens = excluded.total_input_tokens,
|
||||
total_output_tokens = excluded.total_output_tokens,
|
||||
total_tokens = excluded.total_tokens,
|
||||
last_updated = CURRENT_TIMESTAMP
|
||||
`);
|
||||
|
||||
const summaryTransaction = db.transaction(() => {
|
||||
for (const [sessionId, totals] of sessionTotals) {
|
||||
updateStmt.run(sessionId, totals.input, totals.output, totals.total);
|
||||
}
|
||||
});
|
||||
|
||||
summaryTransaction();
|
||||
|
||||
console.log(`โ
Updated ${sessionTotals.size} session summaries`);
|
||||
|
||||
// Show top sessions by token usage
|
||||
console.log('\n๐ Top 10 Sessions by Token Usage:');
|
||||
const topSessions = db.prepare(`
|
||||
SELECT
|
||||
s.id,
|
||||
s.name,
|
||||
s.model,
|
||||
sts.total_tokens,
|
||||
sts.total_input_tokens,
|
||||
sts.total_output_tokens
|
||||
FROM session_token_summary sts
|
||||
JOIN sessions s ON s.id = sts.session_id
|
||||
ORDER BY sts.total_tokens DESC
|
||||
LIMIT 10
|
||||
`).all() as Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
model: string | null;
|
||||
total_tokens: number;
|
||||
total_input_tokens: number;
|
||||
total_output_tokens: number;
|
||||
}>;
|
||||
|
||||
for (const session of topSessions) {
|
||||
const contextWindow = session.model?.includes('haiku') ? 200000 : 200000;
|
||||
const percentage = Math.round((session.total_tokens / contextWindow) * 100);
|
||||
console.log(` ${session.name.substring(0, 40).padEnd(40)} | ${session.total_tokens.toLocaleString().padStart(8)} tokens (${percentage}% of context)`);
|
||||
}
|
||||
|
||||
console.log('\nโจ Backfill complete!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('โ Error during backfill:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
51
test_token_api.js
Normal file
51
test_token_api.js
Normal file
@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// Test script to verify token tracking API behavior
|
||||
const path = require('path');
|
||||
|
||||
// Import the built database service
|
||||
const { DatabaseService } = require('./main/dist/database/database');
|
||||
const { getSessionTokenUsageWithContext } = require('./main/dist/utils/tokenTracker');
|
||||
|
||||
const sessionId = 'e48185d1-504a-497d-8893-03c232a6841c';
|
||||
const dbPath = path.join(process.env.HOME, '.crystal', 'sessions.db');
|
||||
|
||||
console.log('Testing token tracking for session:', sessionId);
|
||||
console.log('Database path:', dbPath);
|
||||
|
||||
try {
|
||||
// Initialize database service
|
||||
const db = new DatabaseService(dbPath);
|
||||
|
||||
console.log('\n=== Direct Database Queries ===');
|
||||
|
||||
// Test getLatestContextWindowUsage directly
|
||||
console.log('Testing getLatestContextWindowUsage...');
|
||||
const latestContext = db.getLatestContextWindowUsage(sessionId);
|
||||
console.log('Latest context usage:', latestContext);
|
||||
|
||||
// Test getSessionTokenSummary
|
||||
console.log('\nTesting getSessionTokenSummary...');
|
||||
const summary = db.getSessionTokenSummary(sessionId);
|
||||
console.log('Session summary:', summary);
|
||||
|
||||
console.log('\n=== Full Token Usage Context ===');
|
||||
|
||||
// Test the full function
|
||||
const result = getSessionTokenUsageWithContext(db, sessionId, 'claude-sonnet-4-20250514');
|
||||
console.log('Full result:', JSON.stringify(result, null, 2));
|
||||
|
||||
console.log('\n=== Expected vs Actual ===');
|
||||
console.log('Expected context usage: 188278 tokens (94.1%)');
|
||||
console.log('Actual currentContextUsage:', result.currentContextUsage);
|
||||
console.log('Actual summary.total_tokens:', result.summary?.total_tokens);
|
||||
|
||||
const actualTokens = result.currentContextUsage ?? result.summary?.total_tokens ?? 0;
|
||||
const percentage = Math.round((actualTokens / 200000) * 100);
|
||||
console.log('Calculated percentage:', percentage + '%');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
} finally {
|
||||
process.exit(0);
|
||||
}
|
||||
Loadingโฆ
x
Reference in New Issue
Block a user