# Project export: latentra

This document was generated by HackStack to give an AI agent context about a hackathon project. Sections are labeled with their provenance; content marked as truncated was cut to keep this document small.

## Project metadata

- Hackathon: Cal Hacks 12.0
- Tagline: Air Drop for AI Compute
- Devpost: https://devpost.com/software/latentra
- GitHub: https://github.com/yordanoskassa/latentra
- Video: https://www.youtube.com/embed/please-check-the-looma-link-above-wifi-bad-for-youtube?enablejsapi=1&hl=en_US&rel=0&start=&version=3&wmode=transparent
- Team: 2 GitHub contributor(s) — Yordanos (10 commits), Claude (1 commits)

## Devpost submission (written by the team)

### Overview

Turn college campuses into AI supercomputers 💡

### Inspiration

It's 2 AM. A grad student is waiting for GPU access. Position 34 in queue. Wait time: 6 days. Meanwhile, 10,000+ computers sit idle across campus. That's enough compute to run GPT-4 level models. But nobody can access it. We built latentra to fix this. 🎯

### What it does

latentra is a peer-to-peer AI agent platform that pools idle campus computers to run massive AI models with powerful tool-calling capabilities. Students: Build and run AI agents without expensive GPUs or cloud bills Researchers: Deploy complex multi-step agents that use real tools (APIs, databases, scripts) Universities: Cut compute costs by 60-80% Planet: 80% less energy vs cloud computing (MIT research) How it works: Install desktop app on your laptop When idle, your device joins the campus compute pool Build AI agents with drag-and-drop interface Agents run distributed across campus using LocalAI's P2P inference Composio enables agents to call 100+ tools (GitHub, Slack, Gmail, etc.) Chroma vector DB stores agent memory across the network Contribute compute → earn credits → use for research 🛠️

### How we built it

Tech Stack: Electron - Cross-platform desktop app React + TypeScript - Agent builder UI LocalAI - P2P distributed inference across campus devices Composio - Tool calling integration (100+ APIs and tools) Chroma - Distributed vector database for agent memory Python + PyTorch - Inference engine libp2p + WebRTC - P2P networking layer OAuth 2.0 - Campus SSO authentication Architecture: Key innovations: Agent builder with P2P inference: Visual interface to create agents that run distributed across campus Composio integration: Agents can actually DO things (send emails, query databases, execute code) Distributed memory with Chroma: Agent context and embeddings stored across network nodes Fault tolerance: Auto-recovers when students close laptops mid-agent-execution Campus-aware routing: Optimizes for network topology (same building = faster) Security: Sandboxed tool execution, encrypted everything, IT admin controls 🚧 Challenges P2P + Vector DB = Hard - Syncing Chroma embeddings across distributed nodes while maintaining consistency. Built custom replication protocol. P2P + Vector DB = Hard - Syncing Chroma embeddings across distributed nodes while maintaining consistency. Built custom replication protocol. Tool calling security - Students could potentially execute dangerous API calls. Implemented permission system and sandboxing with Composio. Tool calling security - Students could potentially execute dangerous API calls. Implemented permission system and sandboxing with Composio. LocalAI coordination - Managing inference across heterogeneous devices (different specs, availability). Built adaptive scheduling. LocalAI coordination - Managing inference across heterogeneous devices (different specs, availability). Built adaptive scheduling. Agent state management - When a node drops mid-execution, agent needs to resume. Added checkpointing at every tool call. Agent state management - When a node drops mid-execution, agent needs to resume. Added checkpointing at every tool call. Network instability - WiFi drops, laptops close. Built aggressive fault tolerance and state recovery. Network instability - WiFi drops, laptops close. Built aggressive fault tolerance and state recovery. 🏆 Accomplishments ✅ Built working AI agent that uses Composio to query GitHub, analyze code, and send Slack notifications - all running distributed ✅ Chroma vector DB successfully replicated across 10+ campus laptops ✅ Agent survived 3 laptops disconnecting mid-execution (fault tolerance works!) ✅ 240ms average inference latency with LocalAI P2P ✅ 73% energy savings vs cloud (measured with actual wattmeters) ✅ Successfully demonstrated agent making real API calls through Composio ✅ UC Berkeley Research IT interested in piloting 📚

### What we learned

Technical: LocalAI's P2P capabilities are powerful but need careful orchestration Composio's tool integration is amazing - 100+ tools out of the box Distributed vector databases (Chroma) need smart replication strategies Agent state management across unreliable networks is genuinely hard Product: Researchers want AGENTS, not just inference - they need tools to interact with their workflow The combination of P2P inference + tool calling + memory is uniquely powerful Students love the visual agent builder - makes AI accessible Process: Integration is key - LocalAI + Composio + Chroma work beautifully together Demo with real agents doing real tasks is WAY more impressive than just running models Building for campus first (not general cloud) gives us unique advantages 🌟 Impact If adopted at UC Berkeley: 💰 Financial $200-2,000 saved per student researcher/semester $1-5M campus-wide annual savings Free access to agent tools that normally cost $50/month per user 🎓 Academic Every student can build AI agents with real capabilities Research automation: agents that run experiments, analyze data, write reports Democratizes AI research access 🌍 Environmental 450 tons CO₂ saved annually 80% energy reduction vs cloud Uses energy already being consumed (idle devices) 🤖 Agent Capabilities Students can build research assistants that actually interact with GitHub, Notion, Slack Multi-step workflows automated across distributed compute Persistent memory through Chroma for long-running research projects 🚀

### What's next

Short term: Pilot at UC Berkeley with 3 research labs Deploy to 5 computer labs (2,000+ devices) Expand Composio integration to more research tools (LaTeX, Jupyter, ArXiv) Medium term: Expand to all 10 UC campuses (280k students) Multi-campus agent federation (agents that span Berkeley + Stanford networks) Add agent marketplace - students share and remix agents Long term: 50+ universities nationwide Open source the agent builder and P2P orchestration layer Work with NSF to standardize distributed AI agent infrastructure Technical roadmap: More Chroma optimization for faster retrieval across P2P network Composio enterprise tools (databases, cloud platforms) Agent-to-agent communication for multi-agent systems LocalAI model switching based on task complexity Built at Cal Hacks 2025 Democratizing AI agents. One campus at a time. 🤖🌍 🔧 Tech Details Why these tools? LocalAI: Enables P2P inference without centralized servers - perfect for campus networks Composio: Gives agents real-world capabilities - 100+ integrations out of the box Chroma: Lightweight vector DB that works distributed - agent memory across devices What makes this powerful: Traditional cloud agent platforms = expensive + centralized + energy-hungry latentra = free + distributed + sustainable + runs on campus hardware you already have DEMO VIDEO : link

## README (from the GitHub repository)

No README available.

## Detected evidence (automated analysis)

Indexed codebase: 64 recognized source files, 425 KB.
- CSS (language) — detected in the code
- HTML (language) — detected in the code
- JavaScript (language) — detected in the code
- LangChain (technology) — detected in the code
- React (technology) — detected in the code
- Tailwind CSS (technology) — detected in the code
- TypeScript (language) — detected in the code
- AI coding agent: Claude Code — evidence: commit authorship or trailers

## Codebase structure (from repository index)

### Files (81 of 81)

```
.DS_Store
.env.example
.github/ISSUE_TEMPLATE/bug_report.yml
.github/ISSUE_TEMPLATE/config.yml
.github/ISSUE_TEMPLATE/feature_request.yml
.github/workflows/main.yml
.gitignore
assistant-ui.txt
auto-test.js
components.json
COMPOSIO_INTEGRATION.md
composio-docs.txt
download-model.js
electron/agent-service.ts
electron/distributed-service.ts
electron/langchain-agent-service.ts
electron/llm-config.ts
electron/llm-service.ts
electron/localai-manager.ts
electron/main.ts
electron/native-p2p.ts
electron/preload.ts
electron/wsl-manager.ts
find-latentra.cjs
index.html
LICENSE
mdns-test.cjs
package.json
postcss.config.js
resources/bin/.gitignore
resources/bin/.gitkeep
scripts/download-localai.js
src/App.tsx
src/components/assistant-ui/model-selector.tsx
src/components/assistant-ui/runtime-provider.tsx
src/components/assistant-ui/thread.tsx
src/components/chat/AgentBuilder.tsx
src/components/chat/ChatInterface.tsx
src/components/chat/ComposioSettings.tsx
src/components/chat/ComputeVisualization.tsx
src/components/chat/DistributedSettings.tsx
src/components/chat/LocalAIControl.tsx
src/components/chat/ModelManager.tsx
src/components/chat/ModernChatInterface.tsx
src/components/chat/NetworkStatus.tsx
src/components/chat/P2PSettings.tsx
src/components/chat/PeerMonitor.tsx
src/components/chat/PerformanceSettings.tsx
src/components/chat/SimpleAirDropUI.tsx
src/components/chat/SimpleP2PPage.tsx
src/components/chat/SocialNetworkInference.tsx
src/components/chat/SwarmControl.tsx
src/components/chat/ToolRouterManager.tsx
src/components/chat/UserProfileDialog.tsx
src/components/ui/ai-input.tsx
src/components/ui/avatar.tsx
src/components/ui/badge.tsx
src/components/ui/button.tsx
src/components/ui/card.tsx
src/components/ui/dialog.tsx
src/components/ui/input.tsx
src/components/ui/select.tsx
src/components/ui/switch.tsx
src/components/ui/tabs.tsx
src/components/ui/textarea.tsx
src/components/ui/tooltip.tsx
src/globals.css
src/lib/composio-client.ts
src/lib/utils.ts
src/main.tsx
src/types/electron.d.ts
tailwind.config.js
test-p2p.sh
test-peer.cjs
tsconfig.json
tsconfig.main.json
tsconfig.node.json
tsconfig.preload.json
vite.config.ts
WSL-DISTRIBUTED-INFERENCE.md
WSL-FIX-SUMMARY.md
```

### Dependencies

- package.json: @assistant-ui/react@^0.11.34, @assistant-ui/react-markdown@^0.11.3, @composio/core@^0.2.0, @langchain/community@^1.0.0, @langchain/openai@^1.0.0, @radix-ui/react-avatar@^1.1.10, @radix-ui/react-dialog@^1.1.15, @radix-ui/react-label@^2.0.2, @radix-ui/react-select@^2.2.6, @radix-ui/react-separator@^1.0.3, @radix-ui/react-slot@^1.2.3, @radix-ui/react-switch@^1.2.6, @radix-ui/react-tabs@^1.0.4, @radix-ui/react-tooltip@^1.2.8, @types/better-sqlite3@^7.6.13, @types/multicast-dns@^7.2.4, @types/node@^20.10.0, @types/react@^18.2.43, @types/react-dom@^18.2.17, @types/uuid@^10.0.0, @typescript-eslint/eslint-plugin@^6.14.0, @typescript-eslint/parser@^6.14.0, @vitejs/plugin-react@^4.2.1, autoprefixer@^10.4.16, better-sqlite3@^12.4.1, chromadb@^3.0.17, class-variance-authority@^0.7.1, clsx@^2.1.1, concurrently@^8.2.2, cross-env@^10.1.0, dotenv@^17.2.3, electron@^28.0.0, electron-builder@^24.9.1, eslint@^8.55.0, eslint-plugin-react-hooks@^4.6.0, eslint-plugin-react-refresh@^0.4.5, langchain@^1.0.1, lucide-react@^0.294.0, motion@^12.23.24, multicast-dns@^7.2.5, node-llama-cpp@^3.0.0, postcss@^8.4.32, react@^18.2.0, react-dom@^18.2.0, remark-gfm@^4.0.1, tailwind-merge@^2.6.0, tailwindcss@^3.3.6, tailwindcss-animate@^1.0.7, typescript@^5.2.2, uuid@^13.0.0, vite@^5.0.8, wait-on@^7.2.0, zustand@^5.0.8

### Recent commits (newest first)

- chore(github): add issue templates
- chore(license): add MIT license
- ci(cd): add GitHub Actions workflow for automated builds
- feat: Lenovo distributed inference simulation
- Add VRAM display and fix peer discovery
- P2P-lite: start local relay server (/:health, /api/relay/chat) and add 'Add Friend' UI in PeerMonitor to send test chat via relay; no LocalAI required for simulation
- Composio v3: use nested payload {auth_config:{id}, connection:{user_id}} and pass auth_scheme; auto-create auth config when missing
- Fix TLS connection errors: Add timeout handling and better error messages for Composio API requests
- Add @types/multicast-dns to fix build error
- Fix Composio integration: Use v3 API endpoints with hardcoded auth configs and proper error handling
- Add Windows compatibility with WSL fallback for distributed inference
- Add P2P compute sharing, fix LocalAI integration, package app (binaries in release/ folder locally only)
- Update P2P and distributed features
- Initial commit for latentra project

## Key source files (fetched from GitHub, selected and truncated for size)

### WSL-FIX-SUMMARY.md

```markdown
# WSL LocalAI Auto-Fallback Fix

## Problem
LocalAI was not working on Windows because:
1. The system only tried to auto-start LocalAI if a native binary was available
2. On Windows without a native binary, `binaryAvailable` was false
3. The WSL fallback existed but was never automatically triggered
4. Users had to manually enable WSL mode, which wasn't obvious
5. WSL detection was using cached results (failing permanently after first failure)
6. WSL commands were failing due to PATH issues in Electron's child_process

## Solution

### Changes Made

#### 1. **electron/localai-manager.ts**
- Modified `_start()` method to automatically try WSL fallback on Windows when native binary is not found
- Now when `start()` is called, it automatically detects Windows and attempts WSL if the native binary is missing
- This makes WSL transparent to the user

```typescript
// Before: Threw an error if binary not found
if (!this.isBinaryAvailable()) {
  throw new Error(`LocalAI binary not found...`)
}

// After: Automatically tries WSL on Windows
if (!this.isBinaryAvailable()) {
  if (process.platform === 'win32') {
    console.log('Native LocalAI binary not found, attempting WSL fallback...')
    return this._startViaWSL()
  }
  throw new Error(`LocalAI binary not found...`)
}
```

#### 2. **electron/distributed-service.ts**
- Removed the `status.binaryAvailable` check from auto-start logic
- Now attempts to start LocalAI even if native binary isn't available
- WSL fallback happens automatically through the LocalAIManager

```typescript
// Before: Only auto-started if binary available
} else if (this.config.enableP2P && status.binaryAvailable && !this.manuallyStopped) {

// After: Always tries to auto-start (WSL fallback is automatic)
} else if (this.config.enableP2P && !this.manuallyStopped) {
```

#### 3. **src/components/chat/LocalAIControl.tsx**
- Added `usingWSL` property to status interface
- Added visual indicator when running via WSL
- Updated warning message to mention WSL auto-fallback
- Removed `binaryAvailable` requirement from start button

#### 4. **src/components/chat/P2PSettings.tsx**
- Removed `binaryAvailable` check from start button visibility
- Added hint that WSL will be used on Windows if needed
- Button is now always visible when LocalAI is not running

## How It Works Now

### On Windows (without native binary):

1. User starts the app
2. Distributed service initializes
3. LocalAI auto-start attempts:
   - Checks for native binary (not found)
   - Detects Windows platform
   - Automatically calls `_startViaWSL()`
4. WSL Manager:
   - Checks if WSL is available
   - Downloads LocalAI Linux binary if needed
   - Syncs models to WSL
   - Starts LocalAI in WSL
5. App connects to LocalAI at `localhost:8080`
6. UI shows "running via wsl" indicator

### Benefits

✅ **Automatic** - No manual configuration needed  
✅ **Transparent** - Works seamlessly in the background  
✅ **Clear UI** - Shows when WSL is being used  
✅ **Fallback Chain** - Nati
[truncated — 1422 more characters]
```

### WSL-DISTRIBUTED-INFERENCE.md

```markdown
# Distributed Inference on Windows via WSL

## Overview

Windows doesn't have native LocalAI binaries, but we've implemented **automatic WSL2 fallback** to enable full distributed inference on Windows!

## How It Works

1. **Automatic Detection**: When you start the app on Windows, it checks for a native LocalAI binary
2. **WSL Fallback**: If not found, it automatically uses WSL2 to run the Linux version
3. **Seamless Integration**: Your Windows app connects to `localhost:8080` - WSL2's networking makes this transparent
4. **Full Features**: You get complete distributed inference, P2P discovery, and swarm capabilities

## Setup Requirements

### Install WSL2

If you don't have WSL2 installed:

```powershell
# Run in PowerShell as Administrator
wsl --install
```

Then restart your computer.

### Verify WSL2

```powershell
wsl --status
```

You should see "Default Version: 2"

## What Happens Automatically

When you start Latentra on Windows:

1. ✅ Checks for native Windows LocalAI binary
2. ✅ Falls back to WSL2 if not available
3. ✅ Downloads LocalAI Linux binary to WSL (`~/.latentra/local-ai`)
4. ✅ Syncs your models from Windows to WSL (`~/.latentra/models`)
5. ✅ Starts LocalAI in WSL with P2P enabled
6. ✅ Your app connects via `localhost:8080`

## First Run

The first time you enable distributed inference on Windows:

- LocalAI binary (~400MB) downloads to WSL
- Your models sync to WSL (if not already there)
- This may take a few minutes depending on model sizes

Subsequent starts are much faster!

## Benefits

### ✅ Full Distributed Inference
- Share computation across devices on your network
- Mac, Linux, and Windows devices can all participate
- Automatic load balancing

### ✅ P2P Discovery
- Automatically finds other Latentra instances
- mDNS-based discovery (no configuration needed)
- Real-time peer status

### ✅ Performance
- WSL2 has near-native Linux performance
- GPU passthrough support (if you have WSL2 GPU drivers)
- Efficient localhost networking

## Architecture

```
┌─────────────────────────────────────────┐
│         Windows (Electron App)          │
│                                         │
│  ┌─────────────────────────────────┐  │
│  │   Latentra UI                   │  │
│  │   (React + TypeScript)          │  │
│  └──────────────┬──────────────────┘  │
│                 │ IPC                  │
│  ┌──────────────▼──────────────────┐  │
│  │   LocalAI Manager               │  │
│  │   (Detects: No Windows binary)  │  │
│  └──────────────┬──────────────────┘  │
│                 │                      │
│  ┌──────────────▼──────────────────┐  │
│  │   WSL Manager                   │  │
│  │   • Check WSL availability      │  │
│  │   • Download LocalAI Linux      │  │
│  │   • Sync models                 │  │
│  │   • Start via wsl command       │  │
│  └──────────────┬──────────────────┘  │
└─────────────────┼────────────────────┘
                  │
                  │ localhost:8080
                  │
┌─────────────────▼─
[truncated — 2090 more characters]
```

### package.json

```
{
  "name": "latentra",
  "version": "1.0.0",
  "description": "Local LLM Studio - Download, manage, and chat with AI models",
  "type": "module",
  "main": "dist/main.js",
  "scripts": {
    "dev": "concurrently \"npm run dev:renderer\" \"npm run dev:electron\"",
    "dev:renderer": "vite",
    "dev:electron": "wait-on http://localhost:5176 && cross-env NODE_ENV=development electron .",
    "build": "npm run build:renderer && npm run build:electron",
    "build:renderer": "vite build",
    "build:electron": "tsc -p tsconfig.main.json && tsc -p tsconfig.preload.json",
    "dist": "npm run download-localai && npm run build && electron-builder",
    "postinstall": "electron-builder install-app-deps",
    "download-model": "node download-model.js",
    "download-localai": "node scripts/download-localai.js",
    "test": "node auto-test.js",
    "test:peer": "node test-peer.cjs"
  },
  "keywords": [
    "electron",
    "llm",
    "ai",
    "local",
    "llama.cpp",
    "hugging-face",
    "chat",
    "studio"
  ],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@types/multicast-dns": "^7.2.4",
    "@types/node": "^20.10.0",
    "@types/react": "^18.2.43",
    "@types/react-dom": "^18.2.17",
    "@typescript-eslint/eslint-plugin": "^6.14.0",
    "@typescript-eslint/parser": "^6.14.0",
    "@vitejs/plugin-react": "^4.2.1",
    "autoprefixer": "^10.4.16",
    "concurrently": "^8.2.2",
    "cross-env": "^10.1.0",
    "electron": "^28.0.0",
    "electron-builder": "^24.9.1",
    "eslint": "^8.55.0",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.5",
    "postcss": "^8.4.32",
    "tailwindcss": "^3.3.6",
    "typescript": "^5.2.2",
    "vite": "^5.0.8",
    "wait-on": "^7.2.0"
  },
  "dependencies": {
    "@assistant-ui/react": "^0.11.34",
    "@assistant-ui/react-markdown": "^0.11.3",
    "@composio/core": "^0.2.0",
    "@langchain/community": "^1.0.0",
    "@langchain/openai": "^1.0.0",
    "@radix-ui/react-avatar": "^1.1.10",
    "@radix-ui/react-dialog": "^1.1.15",
    "@radix-ui/react-label": "^2.0.2",
    "@radix-ui/react-select": "^2.2.6",
    "@radix-ui/react-separator": "^1.0.3",
    "@radix-ui/react-slot": "^1.2.3",
    "@radix-ui/react-switch": "^1.2.6",
    "@radix-ui/react-tabs": "^1.0.4",
    "@radix-ui/react-tooltip": "^1.2.8",
    "@types/better-sqlite3": "^7.6.13",
    "@types/uuid": "^10.0.0",
    "better-sqlite3": "^12.4.1",
    "chromadb": "^3.0.17",
    "class-variance-authority": "^0.7.1",
    "clsx": "^2.1.1",
    "dotenv": "^17.2.3",
    "langchain": "^1.0.1",
    "lucide-react": "^0.294.0",
    "motion": "^12.23.24",
    "multicast-dns": "^7.2.5",
    "node-llama-cpp": "^3.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "remark-gfm": "^4.0.1",
    "tailwind-merge": "^2.6.0",
    "tailwindcss-animate": "^1.0.7",
    "uuid": "^13.0.0",
    "zustand": "^5.0.8"
  },
  "build": {
    "appId": "com.latentra.app",
    "productName": "latentra",
    "directories": {
      "output": "release"
    },
    "files": [
      "dist/**/*",
      "node_modules/**/*",
      "src/assets/icon.png",
      "resources/bin/**/*"
    ],
    "extraResources": [
      {
        "from": "resources/bin",
        "to": "bin",
        "filter": [
          "**/*"
        ]
      }
    ],
    "mac": {
      "category": "public.app-category.productivity",
      "icon": "src/assets/icon.png"
    },
    "win": {
      "target": "nsis",
      "icon": "src/assets/icon.png"
    },
    "linux": {
      "target": "AppImage",
      "icon": "src/assets/icon.png"
    }
  }
}

```

### src/main.tsx

```typescript
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './globals.css'

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)

```

### src/App.tsx

```typescript
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Input } from '@/components/ui/input'
import { ModelManager } from '@/components/chat/ModelManager'
import { ModernChatInterface } from '@/components/chat/ModernChatInterface'
import { PerformanceSettings } from '@/components/chat/PerformanceSettings'
import { DistributedSettings } from '@/components/chat/DistributedSettings'
import { SimpleP2PPage } from '@/components/chat/SimpleP2PPage'
import { P2PSettings } from '@/components/chat/P2PSettings'
import { AgentBuilder } from '@/components/chat/AgentBuilder'
import { ComposioSettings } from '@/components/chat/ComposioSettings'
import { Settings, HardDrive, Brain, Search, MessageSquare, Network, Bot, Link as LinkIcon } from 'lucide-react'
import { TooltipProvider } from '@/components/ui/tooltip'

function App() {
  const [activeTab, setActiveTab] = useState('chat')

  return (
    <div className="min-h-screen bg-background">
      <div className="flex">
        {/* Sidebar */}
        <div className="w-64 bg-card border-r border-border h-screen">
          <div className="p-6">
            <div className="mb-2 animate-fade-in">
              <h1 className="text-2xl font-semibold text-foreground lowercase tracking-tight">latentra</h1>
            </div>
            <p className="text-sm text-muted-foreground lowercase">local llm studio</p>
          </div>
          <nav className="px-4 space-y-1.5">
            <Button
              variant={activeTab === 'chat' ? 'default' : 'ghost'}
              className="w-full justify-start h-10 rounded-xl transition-all duration-200"
              onClick={() => setActiveTab('chat')}
            >
              <MessageSquare className="mr-2 h-4 w-4" />
              <span className="lowercase">chat</span>
            </Button>
            <Button
              variant={activeTab === 'models' ? 'default' : 'ghost'}
              className="w-full justify-start h-10 rounded-xl transition-all duration-200"
              onClick={() => setActiveTab('models')}
            >
              <HardDrive className="mr-2 h-4 w-4" />
              <span className="lowercase">models</span>
            </Button>
            <Button
              variant={activeTab === 'agents' ? 'default' : 'ghost'}
              className="w-full justify-start h-10 rounded-xl transition-all duration-200"
              onClick={() => setActiveTab('agents')}
            >
              <Bot className="mr-2 h-4 w-4" />
              <span className="lowercase">agents</span>
            </Button>
            <Button
              variant={activeTab === 'network' ? 'default' : 'ghost'}
              className="w-full justify-start h-10 rounded-xl transition-all duration-200"
              onClick={() => setActiveTab('network')}
            >
              <Network className="mr-2 h-4 w-4" />
              <span className="lowercase">network</span>
            </Button>
            <Button
              variant={activeTab === 'composio' ? 'default' : 'ghost'}
              className="w-full justify-start h-10 rounded-xl transition-all duration-200"
              onClick={() => setActiveTab('composio')}
            >
              <LinkIcon className="mr-2 h-4 w-4" />
              <span className="lowercase">composio</span>
            </Button>
            <Button
              variant={activeTab === 'settings' ? 'default' : 'ghost'}
              className="w-full justify-start h-10 rounded-xl transition-all duration-200"
              onClick={() => setActiveTab('settings')}
            >
              <Settings className="mr-2 h-4 w-4" />
              <span className="lowercase">settings</span>
            </Button>
          </nav>
        </div>

        {/* Main Content */}
        <div className="flex-1 p-8">
          <div className="mb-8">
            <div className="flex items-center justify-between animate-fade-in">
              <div>
                <h2 className="text-3xl font-semibold text-foreground lowercase tracking-tight">
                  {activeTab}
                </h2>
                <p className="text-muted-foreground lowercase">
                  {activeTab === 'chat' && 'chat with your local ai models'}
                  {activeTab === 'models' && 'browse, download, and manage ai models'}
                  {activeTab === 'agents' && 'create custom ai assistants for specific tasks'}
                  {activeTab === 'network' && 'share ai power with nearby devices'}
                  {activeTab === 'composio' && 'manage tool integrations and authentication'}
                  {activeTab === 'settings' && 'configure your llm preferences'}
                </p>
              </div>
              {activeTab === 'models' && (
                <div className="flex items-center space-x-4">
                  <div className="relative">
                    <Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
                    <Input placeholder="Search models..." className="pl-8 w-64" />
                  </div>
                </div>
              )}
            </div>
          </div>

          {/* Content based on active tab */}
          {activeTab === 'chat' && (
            <div className="h-[calc(100vh-12rem)]">
              <TooltipProvider>
                <ModernChatInterface />
              </TooltipProvider>
            </div>
          )}

          {activeTab === 'models' && (
            <div className="h-[calc(100vh-12rem)] overflow-y-auto">
              <ModelManager />
            </div>
          )}

          {activeTab === 'agents' && (
            <div className="h-[calc(100vh-12rem)] overflow-y-auto">
              <AgentBuilder />
            </div>
          )}

          {activeTab === 'network' && (
            <div className="w-full">
              <SimpleP2PPage />
           
[truncated — 1459 more characters]
```

### electron/main.ts

```typescript
import { app, BrowserWindow, Menu, ipcMain, dialog } from 'electron'
import path from 'path'
import { fileURLToPath } from 'url'
import { config as dotenvConfig } from 'dotenv'
import { LLMService } from './llm-service.js'
import { DistributedInferenceService } from './distributed-service.js'
import { AgentDatabaseService } from './agent-service.js'
import fs from 'fs/promises'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const isDev = process.env.NODE_ENV === 'development'

// Load environment variables from .env file
dotenvConfig({ path: path.join(__dirname, '../.env') })
const llmService = new LLMService()
const distributedService = new DistributedInferenceService(llmService)
const agentService = new AgentDatabaseService()

function createWindow(): void {
  const mainWindow = new BrowserWindow({
    height: 800,
    width: 1200,
    icon: path.join(__dirname, '../src/assets/icon.png'),
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      preload: path.join(__dirname, 'preload.js'),
    },
    titleBarStyle: 'hiddenInset',
    show: false,
  })

  mainWindow.once('ready-to-show', () => {
    mainWindow.show()
  })

  if (isDev) {
    mainWindow.loadURL('http://localhost:5176')
    mainWindow.webContents.openDevTools()
  } else {
    mainWindow.loadFile(path.join(__dirname, 'renderer/index.html'))
  }
}

app.whenReady().then(async () => {
  createWindow()

  // Initialize LLM service (optional - can be done on first chat)
  try {
    await llmService.initialize()
  } catch (error) {
    console.log('LLM service initialization failed, will try again on first chat:', error)
  }

  // Initialize distributed service
  try {
    await distributedService.initialize()
  } catch (error) {
    console.log('Distributed service initialization failed:', error)
  }

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })

  // Start lightweight relay server for peer UI -> master inference
  startRelayServer()
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

if (isDev) {
  Menu.setApplicationMenu(null)
}

// Simple relay HTTP server to allow other devices to call this device for inference
// Exposes:
//  - GET /health -> { ok: true }
//  - POST /api/relay/chat { message } -> { success, response }
async function startRelayServer() {
  try {
    const http = await import('http')
    const relayPort = Number(process.env.RELAY_PORT || 5123)

    const server = http.createServer(async (req, res) => {
      // Basic CORS for convenience
      res.setHeader('Access-Control-Allow-Origin', '*')
      res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
      res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
      if (req.method === 'OPTIONS') {
        res.writeHead(204)
        res.end()
        return
      }

      if (req.method === 'GET' && req.url === '/health') {
        res.writeHead(200, { 'Content-Type': 'application/json' })
        res.end(JSON.stringify({ ok: true }))
        return
      }

      if (req.method === 'POST' && req.url === '/api/relay/chat') {
        try {
          let body = ''
          req.on('data', chunk => { body += chunk })
          req.on('end', async () => {
            try {
              const parsed = JSON.parse(body || '{}')
              const message: string = parsed.message || ''
              if (!message) {
                res.writeHead(400, { 'Content-Type': 'application/json' })
                res.end(JSON.stringify({ success: false, error: 'Missing message' }))
                return
              }
              const result = await llmService.chat(message)
              res.writeHead(200, { 'Content-Type': 'application/json' })
              res.end(JSON.stringify({ success: true, response: result }))
            } catch (err: any) {
              res.writeHead(500, { 'Content-Type': 'application/json' })
              res.end(JSON.stringify({ success: false, error: err?.message || 'Unknown error' }))
            }
          })
        } catch (error: any) {
          res.writeHead(500, { 'Content-Type': 'application/json' })
          res.end(JSON.stringify({ success: false, error: error?.message || 'Unknown error' }))
        }
        return
      }

      res.writeHead(404, { 'Content-Type': 'application/json' })
      res.end(JSON.stringify({ success: false, error: 'Not Found' }))
    })

    server.listen(relayPort, () => {
      console.log(`Relay server listening on http://0.0.0.0:${relayPort}`)
    })
  } catch (error) {
    console.error('Failed to start relay server:', error)
  }
}

// IPC handler for environment variables
ipcMain.handle('app:getEnv', async (event, key: string) => {
  try {
    return process.env[key] || undefined
  } catch (error) {
    console.error('Failed to get environment variable:', error)
    return undefined
  }
})

// IPC handlers for Composio API calls (to avoid CORS issues) - Using v3 API
ipcMain.handle('composio:createAuthConfig', async (event, data: { toolkit: string, authScheme: string, scopes?: string[] }) => {
  try {
    const apiKey = process.env.COMPOSIO_API_KEY
    if (!apiKey) {
      return { success: false, error: 'No API key configured' }
    }

    const https = await import('https')
    
    // Build payload for creating auth config using v3 format
    const requestBody: any = {
      toolkit: data.toolkit.toUpperCase(),
      auth_scheme: data.authScheme,
    }
    
    if (data.scopes && data.scopes.length > 0) {
      requestBody.scopes = data.scopes
    }
    
    const payload = JSON.stringify(requestBody)
    console.log('[Composio] Creating auth config with payload:', requestBody)

    return new Promise((resolve) => {
      const req = https.request({
        hostname: 'backend.composio.dev',
        path: '/api/v3/auth_configs',
        method: 'POST',
        headers: {
          'X-API-Key': apiKey,
          'Content-Ty
[truncated — 29082 more characters]
```

### postcss.config.js

```javascript
export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

```

### vite.config.ts

```typescript
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  base: './',
  build: {
    outDir: 'dist/renderer',
    emptyOutDir: true,
  },
  server: {
    port: 5176,
    strictPort: true,
    host: true,
  },
})

```

### index.html

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/png" href="/src/assets/icon.png" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="preconnect" href="https://rsms.me">
    <link rel="stylesheet" href="https://rsms.me/inter/inter.css">
    <title>latentra</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

```

### auto-test.js

```javascript
#!/usr/bin/env node

import { spawn } from 'child_process'
import path from 'path'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

console.log('🚀 Starting Latentra Auto Test Suite...')

// Start the test peer
console.log('📡 Starting test peer...')
const testPeer = spawn('node', ['test-peer.cjs'], {
  cwd: __dirname,
  stdio: 'inherit'
})

// Handle cleanup
const cleanup = () => {
  console.log('\n🧹 Cleaning up...')
  testPeer.kill()
  process.exit(0)
}

process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup)

testPeer.on('error', (error) => {
  console.error('❌ Test peer error:', error)
})

testPeer.on('exit', (code) => {
  console.log(`📡 Test peer exited with code ${code}`)
})

console.log('✅ Auto test suite running!')
console.log('💡 The test peer is now discoverable as "Lenovo"')
console.log('🔍 Start discovery in the app to see the simulation')
console.log('⚡ Click on Lenovo devices to see combined VRAM')
console.log('🛑 Press Ctrl+C to stop')

```

[55 more indexed source files omitted to keep this export small. The full file list is in the Codebase structure section above.]