# Investibook Project Structure

## Overview

Investibook is a workspace-based AI research platform built on Next.js 16 with a three-column layout inspired by NotebookLM.

## Key Files Created/Modified

### New Files

1. **`lib/workspaces.ts`** - Workspace management utilities
   - CRUD operations for workspaces
   - localStorage integration
   - Data cleanup on deletion

2. **`app/page.tsx`** - Home page (workspace list)
   - Grid layout of workspace cards
   - Create workspace modal
   - Delete confirmation
   - Gradient background design

3. **`app/workspace/[id]/page.tsx`** - Workspace detail page
   - Dynamic routing by workspace ID
   - Workspace header with navigation
   - Three-column layout integration
   - Session management per workspace

4. **`CHANGELOG.md`** - Project changelog
5. **`PROJECT_STRUCTURE.md`** - This file

### Modified Files

1. **`lib/workspace.tsx`** - Enhanced WorkspaceProvider
   - Added `workspaceId` prop
   - Workspace-specific data loading/saving
   - localStorage persistence

2. **`package.json`** - Updated project name to "investibook"

3. **`README.md`** - Updated documentation
   - Workspace management features
   - Usage flow
   - Data storage details

## Application Flow

```
┌─────────────────────────────────────────────────────────────┐
│                         User Opens App                       │
│                     http://localhost:3000                    │
└────────────────────────────┬────────────────────────────────┘
                             │
                             ▼
┌─────────────────────────────────────────────────────────────┐
│                    Home Page (app/page.tsx)                  │
│  ┌──────────────────────────────────────────────────────┐   │
│  │  Load workspaces from localStorage                   │   │
│  │  Display workspace cards in grid                     │   │
│  │  Show "Create Workspace" card                        │   │
│  └──────────────────────────────────────────────────────┘   │
└────────────┬────────────────────────────┬───────────────────┘
             │                            │
    User clicks                    User clicks
  "Create Workspace"              workspace card
             │                            │
             ▼                            ▼
┌──────────────────────┐    ┌────────────────────────────────┐
│  Create Modal Opens  │    │  Navigate to /workspace/[id]   │
│  Enter name & desc   │    │                                │
│  Save to localStorage│    │  Workspace Detail Page         │
│  Navigate to new WS  │    │  (app/workspace/[id]/page.tsx) │
└──────────┬───────────┘    └────────────┬───────────────────┘
           │                             │
           └─────────────┬───────────────┘
                         ▼
┌─────────────────────────────────────────────────────────────┐
│              Workspace Detail Page                           │
│  ┌──────────────────────────────────────────────────────┐   │
│  │  1. Load workspace metadata                          │   │
│  │  2. Initialize WorkspaceProvider with workspaceId    │   │
│  │  3. Load workspace-specific data from localStorage   │   │
│  │     - Sources                                        │   │
│  │     - Artifacts                                      │   │
│  │     - Session ID                                     │   │
│  └──────────────────────────────────────────────────────┘   │
│                                                              │
│  ┌──────────┬─────────────────────┬──────────────────┐     │
│  │ Sources  │       Chat          │     Studio       │     │
│  │  Panel   │       Panel         │     Panel        │     │
│  │          │                     │                  │     │
│  │ Add/Edit │  AI Conversation    │  View Artifacts  │     │
│  │ Sources  │  with Context       │  Reports/Charts  │     │
│  └──────────┴─────────────────────┴──────────────────┘     │
│                                                              │
│  [Back to Workspaces] button returns to home page           │
└─────────────────────────────────────────────────────────────┘
```

## Data Flow

```
┌─────────────────────────────────────────────────────────────┐
│                    Browser localStorage                      │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  investibook_workspaces: [                                  │
│    {                                                         │
│      id: "ws-123...",                                       │
│      name: "Q1 2024 Analysis",                              │
│      description: "...",                                    │
│      createdAt: "...",                                      │
│      updatedAt: "..."                                       │
│    },                                                        │
│    ...                                                       │
│  ]                                                           │
│                                                              │
│  workspace_ws-123_sources: [...]                            │
│  workspace_ws-123_artifacts: [...]                          │
│  workspace_ws-123_session: "ws-123-456..."                  │
│                                                              │
└─────────────────────────────────────────────────────────────┘
         ▲                                    │
         │                                    │
         │ Save on change                     │ Load on mount
         │                                    ▼
┌─────────────────────────────────────────────────────────────┐
│              WorkspaceProvider (React Context)               │
├─────────────────────────────────────────────────────────────┤
│  State:                                                      │
│  - sources: Source[]                                         │
│  - artifacts: Artifact[]                                     │
│  - selectedSourceId                                          │
│  - selectedArtifactId                                        │
│  - studioView                                                │
│  - openFilePath                                              │
│                                                              │
│  Methods:                                                    │
│  - addSource()                                               │
│  - removeSource()                                            │
│  - addArtifact()                                             │
│  - removeArtifact()                                          │
└─────────────────────────────────────────────────────────────┘
         │                    │                    │
         ▼                    ▼                    ▼
┌──────────────┐   ┌──────────────────┐   ┌──────────────┐
│ SourcesPanel │   │   ChatPanel      │   │ StudioPanel  │
└──────────────┘   └──────────────────┘   └──────────────┘
```

## Component Hierarchy

```
app/page.tsx (Home)
└── Workspace List
    ├── Create Workspace Card
    └── Workspace Cards (map)

app/workspace/[id]/page.tsx (Workspace Detail)
└── WorkspaceProvider (with workspaceId)
    ├── Header
    │   ├── Back Button
    │   └── Workspace Info
    └── Three-Column Layout
        ├── SourcesPanel
        ├── ChatPanel
        └── StudioPanel
```

## localStorage Keys

| Key | Description | Data Type |
|-----|-------------|-----------|
| `investibook_workspaces` | Master list of all workspaces | `Workspace[]` |
| `workspace_{id}_sources` | Sources for workspace {id} | `Source[]` |
| `workspace_{id}_artifacts` | Artifacts for workspace {id} | `Artifact[]` |
| `workspace_{id}_session` | Session ID for workspace {id} | `string` |

## API Endpoints (Backend)

Backend API endpoints:

- `POST /chat/stream` - Stream chat responses via SSE
- Session management via query parameters
- ACP protocol for agent communication

## Next Steps for Development

1. **Chat History Persistence**: Save chat messages to localStorage
2. **Workspace Export/Import**: Allow users to export/import workspace data
3. **Workspace Templates**: Pre-configured workspace templates
4. **Search**: Search across workspaces
5. **Workspace Settings**: Per-workspace configuration
6. **Collaboration**: Share workspaces between users (requires backend)
7. **Cloud Sync**: Sync workspaces to cloud storage (requires backend)
