diff --git a/DATA_CONVERSION_GUIDE.md b/DATA_CONVERSION_GUIDE.md new file mode 100644 index 0000000..43fc62f --- /dev/null +++ b/DATA_CONVERSION_GUIDE.md @@ -0,0 +1,186 @@ +# Data Conversion Guide: TLDraw Sync to Automerge Sync + +This guide explains the data conversion process from the old TLDraw sync format to the new Automerge sync format, and how to verify the conversion is working correctly. + +## Data Format Changes + +### Old Format (TLDraw Sync) +```json +{ + "documents": [ + { "state": { "id": "shape:abc123", "typeName": "shape", ... } }, + { "state": { "id": "page:page", "typeName": "page", ... } } + ], + "schema": { ... } +} +``` + +### New Format (Automerge Sync) +```json +{ + "store": { + "shape:abc123": { "id": "shape:abc123", "typeName": "shape", ... }, + "page:page": { "id": "page:page", "typeName": "page", ... } + }, + "schema": { ... } +} +``` + +## Conversion Process + +The conversion happens automatically when a document is loaded from R2. The `AutomergeDurableObject.getDocument()` method detects the format and converts it: + +1. **Automerge Array Format**: Detected by `Array.isArray(rawDoc)` + - Converts via `convertAutomergeToStore()` + - Extracts `record.state` and uses it as the store record + +2. **Store Format**: Detected by `rawDoc.store` existing + - Already in correct format, uses as-is + - No conversion needed + +3. **Old Documents Format**: Detected by `rawDoc.documents` existing but no `store` + - Converts via `migrateDocumentsToStore()` + - Maps `doc.state.id` to `store[doc.state.id] = doc.state` + +4. **Shape Property Migration**: After format conversion, all shapes are migrated via `migrateShapeProperties()` + - Ensures required properties exist (x, y, rotation, isLocked, opacity, meta, index) + - Moves `w`/`h` from top-level to `props` for geo shapes + - Fixes richText structure + - Preserves custom shape properties + +## Validation & Error Handling + +The conversion functions now include comprehensive validation: + +- **Missing state.id**: Skipped with warning +- **Missing state.typeName**: Skipped with warning +- **Null/undefined records**: Skipped with warning +- **Invalid ID types**: Skipped with warning +- **Malformed shapes**: Fixed during shape migration + +All validation errors are logged with detailed statistics. + +## Custom Records + +Custom record types (like `obsidian_vault:`) are preserved during conversion: +- Tracked during conversion +- Verified in logs +- Preserved in the final store + +## Custom Shapes + +Custom shape types are preserved: +- ObsNote +- Holon +- FathomMeetingsBrowser +- FathomTranscript +- HolonBrowser +- LocationShare +- ObsidianBrowser + +All custom shape properties are preserved during migration. + +## Logging + +The conversion process logs comprehensive statistics: + +``` +๐Ÿ“Š Automerge to Store conversion statistics: + - total: Number of records processed + - converted: Number successfully converted + - skipped: Number skipped (invalid) + - errors: Number of errors + - customRecordCount: Number of custom records + - errorCount: Number of error details +``` + +Similar statistics are logged for: +- Documents to Store migration +- Shape property migration + +## Testing + +### Test Edge Cases + +Run the test script to verify edge case handling: + +```bash +npx tsx test-data-conversion.ts +``` + +This tests: +- Missing state.id +- Missing state.typeName +- Null/undefined records +- Missing state property +- Invalid ID types +- Custom records +- Malformed shapes +- Empty documents +- Mixed valid/invalid records + +### Test with Real R2 Data + +To test with actual R2 data: + +1. **Check Worker Logs**: When a document is loaded, check the Cloudflare Worker logs for conversion statistics +2. **Verify Data Integrity**: After conversion, verify: + - All shapes appear correctly + - All properties are preserved + - No validation errors in TLDraw + - Custom records are present + - Custom shapes work correctly + +3. **Monitor Conversion**: Watch for: + - High skip counts (may indicate data issues) + - Errors during conversion + - Missing custom records + - Shape migration issues + +## Migration Checklist + +- [x] Format detection (Automerge array, store format, old documents format) +- [x] Validation for malformed records +- [x] Error handling and logging +- [x] Custom record preservation +- [x] Custom shape preservation +- [x] Shape property migration +- [x] Comprehensive logging +- [x] Edge case testing + +## Troubleshooting + +### High Skip Counts +If many records are being skipped: +1. Check error details in logs +2. Verify data format in R2 +3. Check for missing required fields + +### Missing Custom Records +If custom records are missing: +1. Check logs for custom record count +2. Verify records start with expected prefix (e.g., `obsidian_vault:`) +3. Check if records were filtered during conversion + +### Shape Validation Errors +If shapes have validation errors: +1. Check shape migration logs +2. Verify required properties are present +3. Check for w/h in wrong location (should be in props for geo shapes) + +## Backward Compatibility + +The conversion is backward compatible: +- Old format documents are automatically converted +- New format documents are used as-is +- No data loss during conversion +- All properties are preserved + +## Future Improvements + +Potential improvements: +1. Add migration flag to track converted documents +2. Add backup before conversion +3. Add rollback mechanism +4. Add conversion progress tracking for large documents + diff --git a/DATA_CONVERSION_SUMMARY.md b/DATA_CONVERSION_SUMMARY.md new file mode 100644 index 0000000..d90dc14 --- /dev/null +++ b/DATA_CONVERSION_SUMMARY.md @@ -0,0 +1,141 @@ +# Data Conversion Summary + +## Overview + +This document summarizes the data conversion implementation from the old tldraw sync format to the new automerge sync format. + +## Conversion Paths + +The system handles three data formats automatically: + +### 1. Automerge Array Format +- **Format**: `[{ state: { id: "...", ... } }, ...]` +- **Conversion**: `convertAutomergeToStore()` +- **Handles**: Raw Automerge document format + +### 2. Store Format (Already Converted) +- **Format**: `{ store: { "recordId": {...}, ... }, schema: {...} }` +- **Conversion**: None needed - already in correct format +- **Handles**: Previously converted documents + +### 3. Old Documents Format (Legacy) +- **Format**: `{ documents: [{ state: {...} }, ...] }` +- **Conversion**: `migrateDocumentsToStore()` +- **Handles**: Old tldraw sync format + +## Validation & Error Handling + +### Record Validation +- โœ… Validates `state` property exists +- โœ… Validates `state.id` exists and is a string +- โœ… Validates `state.typeName` exists (for documents format) +- โœ… Skips invalid records with detailed logging +- โœ… Preserves valid records + +### Shape Migration +- โœ… Ensures required properties (x, y, rotation, opacity, isLocked, meta, index) +- โœ… Moves `w`/`h` from top-level to `props` for geo shapes +- โœ… Fixes richText structure +- โœ… Preserves custom shape properties (ObsNote, Holon, etc.) +- โœ… Tracks and verifies custom shapes + +### Custom Records +- โœ… Preserves `obsidian_vault:` records +- โœ… Tracks custom record count +- โœ… Logs custom record IDs for verification + +## Logging & Statistics + +All conversion functions now provide comprehensive statistics: + +### Conversion Statistics Include: +- Total records processed +- Successfully converted count +- Skipped records (with reasons) +- Errors encountered +- Custom records preserved +- Shape types distribution +- Custom shapes preserved + +### Log Levels: +- **Info**: Conversion statistics, successful conversions +- **Warn**: Skipped records, warnings (first 10 shown) +- **Error**: Conversion errors with details + +## Data Preservation Guarantees + +### What is Preserved: +- โœ… All valid shape data +- โœ… All custom shape properties (ObsNote, Holon, etc.) +- โœ… All custom records (obsidian_vault) +- โœ… All metadata +- โœ… All text content +- โœ… All richText content (structure fixed, content preserved) + +### What is Fixed: +- ๐Ÿ”ง Missing required properties (defaults added) +- ๐Ÿ”ง Invalid property locations (w/h moved to props) +- ๐Ÿ”ง Malformed richText structure +- ๐Ÿ”ง Missing typeName (inferred where possible) + +### What is Skipped: +- โš ๏ธ Records with missing `state` property +- โš ๏ธ Records with missing `state.id` +- โš ๏ธ Records with invalid `state.id` type +- โš ๏ธ Records with missing `state.typeName` (for documents format) + +## Testing + +### Unit Tests +- `test-data-conversion.ts`: Tests edge cases with malformed data +- Covers: missing fields, null records, invalid types, custom records + +### Integration Testing +- Test with real R2 data (see `test-r2-conversion.md`) +- Verify data integrity after conversion +- Check logs for warnings/errors + +## Migration Safety + +### Safety Features: +1. **Non-destructive**: Original R2 data is not modified until first save +2. **Error handling**: Invalid records are skipped, not lost +3. **Comprehensive logging**: All actions are logged for debugging +4. **Fallback**: Creates empty document if conversion fails completely + +### Rollback: +- Original data remains in R2 until overwritten +- Can restore from backup if needed +- Conversion errors don't corrupt existing data + +## Performance + +- Conversion happens once per room (cached) +- Statistics logging is efficient (limited to first 10 errors) +- Shape migration only processes shapes (not all records) +- Custom record tracking is lightweight + +## Next Steps + +1. โœ… Conversion logic implemented and validated +2. โœ… Comprehensive logging added +3. โœ… Custom records/shapes preservation verified +4. โœ… Edge case handling implemented +5. โณ Test with real R2 data (manual process) +6. โณ Monitor production conversions + +## Files Modified + +- `worker/AutomergeDurableObject.ts`: Main conversion logic + - `getDocument()`: Format detection and routing + - `convertAutomergeToStore()`: Automerge array conversion + - `migrateDocumentsToStore()`: Old documents format conversion + - `migrateShapeProperties()`: Shape property migration + +## Key Improvements + +1. **Validation**: All records are validated before conversion +2. **Logging**: Comprehensive statistics for debugging +3. **Error Handling**: Graceful handling of malformed data +4. **Preservation**: Custom records and shapes are tracked and verified +5. **Safety**: Non-destructive conversion with fallbacks diff --git a/FATHOM_INTEGRATION.md b/FATHOM_INTEGRATION.md new file mode 100644 index 0000000..9ed4e70 --- /dev/null +++ b/FATHOM_INTEGRATION.md @@ -0,0 +1,134 @@ +# Fathom API Integration for tldraw Canvas + +This integration allows you to import Fathom meeting transcripts directly into your tldraw canvas at jeffemmett.com/board/test. + +## Features + +- ๐ŸŽฅ **Import Fathom Meetings**: Browse and import your Fathom meeting recordings +- ๐Ÿ“ **Rich Transcript Display**: View full transcripts with speaker identification and timestamps +- โœ… **Action Items**: See extracted action items from meetings +- ๐Ÿ“‹ **AI Summaries**: Display AI-generated meeting summaries +- ๐Ÿ”— **Direct Links**: Click to view meetings in Fathom +- ๐ŸŽจ **Customizable Display**: Toggle between compact and expanded views + +## Setup Instructions + +### 1. Get Your Fathom API Key + +1. Go to your [Fathom User Settings](https://app.usefathom.com/settings/integrations) +2. Navigate to the "Integrations" section +3. Generate an API key +4. Copy the API key for use in the canvas + +### 2. Using the Integration + +1. **Open the Canvas**: Navigate to `jeffemmett.com/board/test` +2. **Access Fathom Meetings**: Click the "Fathom Meetings" button in the toolbar (calendar icon) +3. **Enter API Key**: When prompted, enter your Fathom API key +4. **Browse Meetings**: The panel will load your recent Fathom meetings +5. **Add to Canvas**: Click "Add to Canvas" on any meeting to create a transcript shape + +### 3. Customizing Transcript Shapes + +Once added to the canvas, you can: + +- **Toggle Transcript View**: Click the "๐Ÿ“ Transcript" button to show/hide the full transcript +- **Toggle Action Items**: Click the "โœ… Actions" button to show/hide action items +- **Expand/Collapse**: Click the "๐Ÿ“„ Expanded/Compact" button to change the view +- **Resize**: Drag the corners to resize the shape +- **Move**: Click and drag to reposition the shape + +## API Endpoints + +The integration includes these backend endpoints: + +- `GET /api/fathom/meetings` - List all meetings +- `GET /api/fathom/meetings/:id` - Get specific meeting details +- `POST /api/fathom/webhook` - Receive webhook notifications (for future real-time updates) + +## Webhook Setup (Optional) + +For real-time updates when new meetings are recorded: + +1. **Get Webhook URL**: Your webhook endpoint is `https://jeffemmett-canvas.jeffemmett.workers.dev/api/fathom/webhook` +2. **Configure in Fathom**: Add this URL in your Fathom webhook settings +3. **Enable Notifications**: Turn on webhook notifications for new meetings + +## Data Structure + +The Fathom transcript shape includes: + +```typescript +{ + meetingId: string + meetingTitle: string + meetingUrl: string + summary: string + transcript: Array<{ + speaker: string + text: string + timestamp: string + }> + actionItems: Array<{ + text: string + assignee?: string + dueDate?: string + }> +} +``` + +## Troubleshooting + +### Common Issues + +1. **"No API key provided"**: Make sure you've entered your Fathom API key correctly +2. **"Failed to fetch meetings"**: Check that your API key is valid and has the correct permissions +3. **Empty transcript**: Some meetings may not have transcripts if they were recorded without transcription enabled + +### Getting Help + +- Check the browser console for error messages +- Verify your Fathom API key is correct +- Ensure you have recorded meetings in Fathom +- Contact support if issues persist + +## Security Notes + +- API keys are stored locally in your browser +- Webhook endpoints are currently not signature-verified (TODO for production) +- All data is processed client-side for privacy + +## Future Enhancements + +- [ ] Real-time webhook notifications +- [ ] Search and filter meetings +- [ ] Export transcript data +- [ ] Integration with other meeting tools +- [ ] Advanced transcript formatting options + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SANITIZATION_EXPLANATION.md b/SANITIZATION_EXPLANATION.md new file mode 100644 index 0000000..a8c31fe --- /dev/null +++ b/SANITIZATION_EXPLANATION.md @@ -0,0 +1,91 @@ +# Sanitization Explanation + +## Why Sanitization Exists + +Sanitization is **necessary** because TLDraw has strict schema requirements that must be met for shapes to render correctly. Without sanitization, we get validation errors and broken shapes. + +## Critical Fixes (MUST KEEP) + +These fixes are **required** for TLDraw to work: + +1. **Move w/h/geo from top-level to props for geo shapes** + - TLDraw schema requires `w`, `h`, and `geo` to be in `props`, not at the top level + - Without this, TLDraw throws validation errors + +2. **Remove w/h from group shapes** + - Group shapes don't have `w`/`h` properties + - Having them causes validation errors + +3. **Remove w/h from line shapes** + - Line shapes use `points`, not `w`/`h` + - Having them causes validation errors + +4. **Fix richText structure** + - TLDraw requires `richText` to be `{ content: [...], type: 'doc' }` + - Old data might have it as an array or missing structure + - We preserve all content, just fix the structure + +5. **Fix crop structure for image/video** + - TLDraw requires `crop` to be `{ topLeft: {x,y}, bottomRight: {x,y} }` or `null` + - Old data might have `{ x, y, w, h }` format + - We convert the format, preserving the crop area + +6. **Remove h/geo from text shapes** + - Text shapes don't have `h` or `geo` properties + - Having them causes validation errors + +7. **Ensure required properties exist** + - Some shapes require certain properties (e.g., `points` for line shapes) + - We only add defaults if truly missing + +## What We Preserve + +We **preserve all user data**: +- โœ… `richText` content (we only fix structure, never delete content) +- โœ… `text` property on arrows +- โœ… All metadata (`meta` object) +- โœ… All valid shape properties +- โœ… Custom shape properties + +## What We Remove (Only When Necessary) + +We only remove properties that: +1. **Cause validation errors** (e.g., `w`/`h` on groups/lines) +2. **Are invalid for the shape type** (e.g., `geo` on text shapes) + +We **never** remove: +- User-created content (text, richText) +- Valid metadata +- Properties that don't cause errors + +## Current Sanitization Locations + +1. **TLStoreToAutomerge.ts** - When saving from TLDraw to Automerge + - Minimal fixes only + - Preserves all data + +2. **AutomergeToTLStore.ts** - When loading from Automerge to TLDraw + - Minimal fixes only + - Preserves all data + +3. **useAutomergeStoreV2.ts** - Initial load processing + - More extensive (handles migration from old formats) + - Still preserves all user data + +## Can We Simplify? + +**Yes, but carefully:** + +1. โœ… We can remove property deletions that don't cause validation errors +2. โœ… We can consolidate duplicate logic +3. โŒ We **cannot** remove schema fixes (w/h/geo movement, richText structure) +4. โŒ We **cannot** remove property deletions that cause validation errors + +## Recommendation + +Keep sanitization but: +1. Only delete properties that **actually cause validation errors** +2. Preserve all user data (text, richText, metadata) +3. Consolidate duplicate logic between files +4. Add comments explaining why each fix is necessary + diff --git a/TRANSCRIPTION_SETUP.md b/TRANSCRIPTION_SETUP.md new file mode 100644 index 0000000..7dadb0e --- /dev/null +++ b/TRANSCRIPTION_SETUP.md @@ -0,0 +1,60 @@ +# Transcription Setup Guide + +## Why the Start Button Doesn't Work + +The transcription start button is likely disabled because the **OpenAI API key is not configured**. The button will be disabled and show a tooltip "OpenAI API key not configured - Please set your API key in settings" when this is the case. + +## How to Fix It + +### Step 1: Get an OpenAI API Key +1. Go to [OpenAI API Keys](https://platform.openai.com/api-keys) +2. Sign in to your OpenAI account +3. Click "Create new secret key" +4. Copy the API key (it starts with `sk-`) + +### Step 2: Configure the API Key in Canvas +1. In your Canvas application, look for the **Settings** button (usually a gear icon) +2. Open the settings dialog +3. Find the **OpenAI API Key** field +4. Paste your API key +5. Save the settings + +### Step 3: Test the Transcription +1. Create a transcription shape on the canvas +2. Click the "Start" button +3. Allow microphone access when prompted +4. Start speaking - you should see the transcription appear in real-time + +## Debugging Information + +The application now includes debug logging to help identify issues: + +- **Console Logs**: Check the browser console for messages starting with `๐Ÿ”ง OpenAI Config Debug:` +- **Visual Indicators**: The transcription window will show "(API Key Required)" if not configured +- **Button State**: The start button will be disabled and grayed out if the API key is missing + +## Troubleshooting + +### Button Still Disabled After Adding API Key +1. Refresh the page to reload the configuration +2. Check the browser console for any error messages +3. Verify the API key is correctly saved in settings + +### Microphone Permission Issues +1. Make sure you've granted microphone access to the browser +2. Check that your microphone is working in other applications +3. Try refreshing the page and granting permission again + +### No Audio Being Recorded +1. Check the browser console for audio-related error messages +2. Verify your microphone is not being used by another application +3. Try using a different browser if issues persist + +## Technical Details + +The transcription system: +- Uses the device microphone directly (not Daily room audio) +- Records audio in WebM format +- Sends audio chunks to OpenAI's Whisper API +- Updates the transcription shape in real-time +- Requires a valid OpenAI API key to function diff --git a/WORKER_ENV_GUIDE.md b/WORKER_ENV_GUIDE.md new file mode 100644 index 0000000..0a8100e --- /dev/null +++ b/WORKER_ENV_GUIDE.md @@ -0,0 +1,85 @@ +# Worker Environment Switching Guide + +## Quick Switch Commands + +### Switch to Dev Environment (Default) +```bash +./switch-worker-env.sh dev +``` + +### Switch to Production Environment +```bash +./switch-worker-env.sh production +``` + +### Switch to Local Environment +```bash +./switch-worker-env.sh local +``` + +## Manual Switching + +You can also manually edit the environment by: + +1. **Option 1**: Set environment variable + ```bash + export VITE_WORKER_ENV=dev + ``` + +2. **Option 2**: Edit `.env.local` file + ``` + VITE_WORKER_ENV=dev + ``` + +3. **Option 3**: Edit `src/constants/workerUrl.ts` directly + ```typescript + const WORKER_ENV = 'dev' // Change this line + ``` + +## Available Environments + +| Environment | URL | Description | +|-------------|-----|-------------| +| `local` | `http://localhost:5172` | Local worker (requires `npm run dev:worker:local`) | +| `dev` | `https://jeffemmett-canvas-automerge-dev.jeffemmett.workers.dev` | Cloudflare dev environment | +| `production` | `https://jeffemmett-canvas.jeffemmett.workers.dev` | Production environment | + +## Current Status + +- โœ… **Dev Environment**: Working with AutomergeDurableObject +- โœ… **R2 Data Loading**: Fixed format conversion +- โœ… **WebSocket**: Improved with keep-alive and reconnection +- ๐Ÿ”„ **Production**: Ready to deploy when testing is complete + +## Testing the Fix + +1. Switch to dev environment: `./switch-worker-env.sh dev` +2. Start your frontend: `npm run dev` +3. Check browser console for environment logs +4. Test R2 data loading in your canvas app +5. Verify WebSocket connections are stable + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/package-lock.json b/package-lock.json index f4d857c..21ecdfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,10 +24,16 @@ "@types/marked": "^5.0.2", "@uiw/react-md-editor": "^4.0.5", "@vercel/analytics": "^1.2.2", + "@xenova/transformers": "^2.17.2", "ai": "^4.1.0", + "ajv": "^8.17.1", "cherry-markdown": "^0.8.57", "cloudflare-workers-unfurl": "^0.0.7", + "fathom-typescript": "^0.0.36", "gray-matter": "^4.0.3", + "gun": "^0.2020.1241", + "h3-js": "^4.3.0", + "holosphere": "^1.1.20", "html2canvas": "^1.4.1", "itty-router": "^5.0.17", "jotai": "^2.6.0", @@ -1423,6 +1429,15 @@ "react": ">= 16 || ^19.0.0-rc" } }, + "node_modules/@huggingface/jinja": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@huggingface/jinja/-/jinja-0.2.2.tgz", + "integrity": "sha512-/KPde26khDUIPkTGU82jdtTW9UAuvUTumCAbFs/7giR0SxsvZC4hru51PBvpijH6BVkHcROcvZM/lpy5h1jRRA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/@img/sharp-darwin-arm64": { "version": "0.33.5", "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.33.5.tgz", @@ -2603,6 +2618,48 @@ "node": ">=8.0.0" } }, + "node_modules/@peculiar/asn1-schema": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@peculiar/asn1-schema/-/asn1-schema-2.5.0.tgz", + "integrity": "sha512-YM/nFfskFJSlHqv59ed6dZlLZqtZQwjRVJ4bBAiWV08Oc+1rSd5lDZcBEx0lGDHfSoH3UziI2pXt2UM33KerPQ==", + "license": "MIT", + "optional": true, + "dependencies": { + "asn1js": "^3.0.6", + "pvtsutils": "^1.3.6", + "tslib": "^2.8.1" + } + }, + "node_modules/@peculiar/json-schema": { + "version": "1.1.12", + "resolved": "https://registry.npmjs.org/@peculiar/json-schema/-/json-schema-1.1.12.tgz", + "integrity": "sha512-coUfuoMeIB7B8/NMekxaDzLhaYmp0HZNPEjYRm9goRou8UZIC3z21s0sL9AWoCw4EG876QyO3kYrc61WNF9B/w==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.0.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/@peculiar/webcrypto": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@peculiar/webcrypto/-/webcrypto-1.5.0.tgz", + "integrity": "sha512-BRs5XUAwiyCDQMsVA9IDvDa7UBR9gAvPHgugOeGng3YN6vJ9JYonyDc0lNczErgtCWtucjR5N7VtaonboD/ezg==", + "license": "MIT", + "optional": true, + "dependencies": { + "@peculiar/asn1-schema": "^2.3.8", + "@peculiar/json-schema": "^1.1.12", + "pvtsutils": "^1.3.5", + "tslib": "^2.6.2", + "webcrypto-core": "^1.8.0" + }, + "engines": { + "node": ">=10.12.0" + } + }, "node_modules/@popperjs/core": { "version": "2.11.8", "resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.8.tgz", @@ -4716,6 +4773,12 @@ "dev": true, "license": "CC0-1.0" }, + "node_modules/@stablelib/base64": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@stablelib/base64/-/base64-1.0.1.tgz", + "integrity": "sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==", + "license": "MIT" + }, "node_modules/@swc/core": { "version": "1.13.5", "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.13.5.tgz", @@ -5730,6 +5793,12 @@ "@types/lodash": "*" } }, + "node_modules/@types/long": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.2.tgz", + "integrity": "sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==", + "license": "MIT" + }, "node_modules/@types/markdown-it": { "version": "14.1.2", "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-14.1.2.tgz", @@ -6437,6 +6506,30 @@ "ajv": "^6.12.3" } }, + "node_modules/@vercel/routing-utils/node_modules/ajv": { + "version": "6.12.6", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", + "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "license": "MIT", + "optional": true, + "dependencies": { + "fast-deep-equal": "^3.1.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/@vercel/routing-utils/node_modules/json-schema-traverse": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", + "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", + "license": "MIT", + "optional": true + }, "node_modules/@vercel/routing-utils/node_modules/path-to-regexp": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.1.0.tgz", @@ -6488,12 +6581,6 @@ "url": "https://github.com/sponsors/epoberezkin" } }, - "node_modules/@vercel/static-config/node_modules/json-schema-traverse": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", - "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", - "license": "MIT" - }, "node_modules/@vitejs/plugin-react": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.7.0.tgz", @@ -6515,6 +6602,55 @@ "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0" } }, + "node_modules/@xenova/transformers": { + "version": "2.17.2", + "resolved": "https://registry.npmjs.org/@xenova/transformers/-/transformers-2.17.2.tgz", + "integrity": "sha512-lZmHqzrVIkSvZdKZEx7IYY51TK0WDrC8eR0c5IMnBsO8di8are1zzw8BlLhyO2TklZKLN5UffNGs1IJwT6oOqQ==", + "license": "Apache-2.0", + "dependencies": { + "@huggingface/jinja": "^0.2.2", + "onnxruntime-web": "1.14.0", + "sharp": "^0.32.0" + }, + "optionalDependencies": { + "onnxruntime-node": "1.14.0" + } + }, + "node_modules/@xenova/transformers/node_modules/semver": { + "version": "7.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz", + "integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@xenova/transformers/node_modules/sharp": { + "version": "0.32.6", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.32.6.tgz", + "integrity": "sha512-KyLTWwgcR9Oe4d9HwCwNM2l7+J0dUQwn/yf7S0EnTtb0eVS4RxO0eUSvxPtzT4F3SY+C4K6fqdv/DO27sJ/v/w==", + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "color": "^4.2.3", + "detect-libc": "^2.0.2", + "node-addon-api": "^6.1.0", + "prebuild-install": "^7.1.1", + "semver": "^7.5.4", + "simple-get": "^4.0.1", + "tar-fs": "^3.0.4", + "tunnel-agent": "^0.6.0" + }, + "engines": { + "node": ">=14.15.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, "node_modules/abab": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.6.tgz", @@ -6670,16 +6806,15 @@ } }, "node_modules/ajv": { - "version": "6.12.6", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", - "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "version": "8.17.1", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz", + "integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==", "license": "MIT", - "optional": true, "dependencies": { - "fast-deep-equal": "^3.1.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.2" + "fast-deep-equal": "^3.1.3", + "fast-uri": "^3.0.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2" }, "funding": { "type": "github", @@ -6744,6 +6879,21 @@ "node": ">=10" } }, + "node_modules/asn1js": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/asn1js/-/asn1js-3.0.6.tgz", + "integrity": "sha512-UOCGPYbl0tv8+006qks/dTgV9ajs97X2p0FAbyS2iyCRrmLSRolDaHdp+v/CLgnzHc3fVB+CwYiUmei7ndFcgA==", + "license": "BSD-3-Clause", + "optional": true, + "dependencies": { + "pvtsutils": "^1.3.6", + "pvutils": "^1.1.3", + "tslib": "^2.8.1" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/async-listen": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/async-listen/-/async-listen-1.2.0.tgz", @@ -6785,6 +6935,20 @@ "proxy-from-env": "^1.1.0" } }, + "node_modules/b4a": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.7.3.tgz", + "integrity": "sha512-5Q2mfq2WfGuFp3uS//0s6baOJLMoVduPYVeNmDYxu5OUA1/cBfvr2RIS7vi62LdNj/urk1hfmj867I3qt6uZ7Q==", + "license": "Apache-2.0", + "peerDependencies": { + "react-native-b4a": "*" + }, + "peerDependenciesMeta": { + "react-native-b4a": { + "optional": true + } + } + }, "node_modules/bail": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/bail/-/bail-2.0.2.tgz", @@ -6801,6 +6965,89 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "license": "MIT" }, + "node_modules/bare-events": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.7.0.tgz", + "integrity": "sha512-b3N5eTW1g7vXkw+0CXh/HazGTcO5KYuu/RCNaJbDMPI6LHDi+7qe8EmxKUVe1sUbY2KZOVZFyj62x0OEz9qyAA==", + "license": "Apache-2.0" + }, + "node_modules/bare-fs": { + "version": "4.4.5", + "resolved": "https://registry.npmjs.org/bare-fs/-/bare-fs-4.4.5.tgz", + "integrity": "sha512-TCtu93KGLu6/aiGWzMr12TmSRS6nKdfhAnzTQRbXoSWxkbb9eRd53jQ51jG7g1gYjjtto3hbBrrhzg6djcgiKg==", + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "bare-events": "^2.5.4", + "bare-path": "^3.0.0", + "bare-stream": "^2.6.4", + "bare-url": "^2.2.2", + "fast-fifo": "^1.3.2" + }, + "engines": { + "bare": ">=1.16.0" + }, + "peerDependencies": { + "bare-buffer": "*" + }, + "peerDependenciesMeta": { + "bare-buffer": { + "optional": true + } + } + }, + "node_modules/bare-os": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/bare-os/-/bare-os-3.6.2.tgz", + "integrity": "sha512-T+V1+1srU2qYNBmJCXZkUY5vQ0B4FSlL3QDROnKQYOqeiQR8UbjNHlPa+TIbM4cuidiN9GaTaOZgSEgsvPbh5A==", + "license": "Apache-2.0", + "optional": true, + "engines": { + "bare": ">=1.14.0" + } + }, + "node_modules/bare-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bare-path/-/bare-path-3.0.0.tgz", + "integrity": "sha512-tyfW2cQcB5NN8Saijrhqn0Zh7AnFNsnczRcuWODH0eYAXBsJ5gVxAUuNr7tsHSC6IZ77cA0SitzT+s47kot8Mw==", + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "bare-os": "^3.0.1" + } + }, + "node_modules/bare-stream": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/bare-stream/-/bare-stream-2.7.0.tgz", + "integrity": "sha512-oyXQNicV1y8nc2aKffH+BUHFRXmx6VrPzlnaEvMhram0nPBrKcEdcyBg5r08D0i8VxngHFAiVyn1QKXpSG0B8A==", + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "streamx": "^2.21.0" + }, + "peerDependencies": { + "bare-buffer": "*", + "bare-events": "*" + }, + "peerDependenciesMeta": { + "bare-buffer": { + "optional": true + }, + "bare-events": { + "optional": true + } + } + }, + "node_modules/bare-url": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/bare-url/-/bare-url-2.2.2.tgz", + "integrity": "sha512-g+ueNGKkrjMazDG3elZO1pNs3HY5+mMmOet1jtKyhOaCnkLzitxf26z7hoAEkDNgdNmnc1KIlt/dw6Po6xZMpA==", + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "bare-path": "^3.0.0" + } + }, "node_modules/base-x": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.1.tgz", @@ -6855,6 +7102,41 @@ "file-uri-to-path": "1.0.0" } }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "license": "MIT", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "node_modules/bl/node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/blake3-wasm": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/blake3-wasm/-/blake3-wasm-2.1.5.tgz", @@ -7385,7 +7667,6 @@ "version": "4.2.3", "resolved": "https://registry.npmjs.org/color/-/color-4.2.3.tgz", "integrity": "sha512-1rXeuUUiGGrykh+CeBdu5Ie7OJwinCgQY0bc7GCRxy5xVHy+moaqkpL/jqQq0MtQOeYcrqEz4abc5f0KtU7W4A==", - "dev": true, "license": "MIT", "dependencies": { "color-convert": "^2.0.1", @@ -7399,7 +7680,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, "license": "MIT", "dependencies": { "color-name": "~1.1.4" @@ -7412,14 +7692,12 @@ "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true, "license": "MIT" }, "node_modules/color-string": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.9.1.tgz", "integrity": "sha512-shrVawQFojnZv6xM40anx4CkoDP+fZsw/ZerEMsW/pyzsRbElpsL/DBVW7q3ExxwusdNXI3lXpuhEZkzs8p5Eg==", - "dev": true, "license": "MIT", "dependencies": { "color-name": "^1.0.0", @@ -8363,6 +8641,30 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/decompress-response": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz", + "integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==", + "license": "MIT", + "dependencies": { + "mimic-response": "^3.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/defu": { "version": "6.1.4", "resolved": "https://registry.npmjs.org/defu/-/defu-6.1.4.tgz", @@ -9235,6 +9537,15 @@ "integrity": "sha512-blk1va0zol9QOrdZt0rFXo5KMkNPVSp92Eju/Qz8THwKWKRKeE0T8Br/1aW6+Edkyq9xHYgYxn2QtOnUKPUp+Q==", "license": "MIT" }, + "node_modules/events-universal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/events-universal/-/events-universal-1.0.1.tgz", + "integrity": "sha512-LUd5euvbMLpwOF8m6ivPCbhQeSiYVNb8Vs0fQ8QjXo0JTkEHpz8pxdQf0gStltaPpw0Cca8b39KxvK9cfKRiAw==", + "license": "Apache-2.0", + "dependencies": { + "bare-events": "^2.7.0" + } + }, "node_modules/execa": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/execa/-/execa-3.2.0.tgz", @@ -9269,6 +9580,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/expand-template": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", + "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==", + "license": "(MIT OR WTFPL)", + "engines": { + "node": ">=6" + } + }, "node_modules/exsolve": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/exsolve/-/exsolve-1.0.7.tgz", @@ -9300,6 +9620,12 @@ "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", "license": "MIT" }, + "node_modules/fast-fifo": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz", + "integrity": "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==", + "license": "MIT" + }, "node_modules/fast-glob": { "version": "3.3.3", "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.3.tgz", @@ -9329,6 +9655,22 @@ "integrity": "sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==", "license": "Unlicense" }, + "node_modules/fast-uri": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.0.tgz", + "integrity": "sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, "node_modules/fastq": { "version": "1.19.1", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.19.1.tgz", @@ -9338,6 +9680,15 @@ "reusify": "^1.0.4" } }, + "node_modules/fathom-typescript": { + "version": "0.0.36", + "resolved": "https://registry.npmjs.org/fathom-typescript/-/fathom-typescript-0.0.36.tgz", + "integrity": "sha512-DKVDAp8kCP1fHEkkqWk6QV9RLlcoTwoXkShPyuSQJDnZq+EzedXPk3kG/FINpUci0+6dGkTjfaItp9laGOi4JA==", + "dependencies": { + "svix": "^1.65.0", + "zod": "^3.20.0" + } + }, "node_modules/fd-slicer": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", @@ -9405,6 +9756,12 @@ "xxhashjs": "^0.2.2" } }, + "node_modules/flatbuffers": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/flatbuffers/-/flatbuffers-1.12.0.tgz", + "integrity": "sha512-c7CZADjRcl6j0PlvFy0ZqXQ67qSEZfrVPynmnL+2zPc+NtMvrF8Y0QceMo7QqnSPc7+uWjUIAbvCQ5WIKlMVdQ==", + "license": "SEE LICENSE IN LICENSE.txt" + }, "node_modules/fnv1a": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/fnv1a/-/fnv1a-1.1.1.tgz", @@ -9479,6 +9836,12 @@ "integrity": "sha512-0tLU0FOedVY7lrvN4LK0DVj6FTuYM0pWDpN97/8UTZE2lx1+OwX8+2uL7IOWc2PmktYTHQjMT6FvZZ3SGCdZdg==", "license": "CC0-1.0" }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "license": "MIT" + }, "node_modules/fs-extra": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", @@ -9631,6 +9994,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/github-from-package": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz", + "integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==", + "license": "MIT" + }, "node_modules/github-slugger": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/github-slugger/-/github-slugger-2.0.0.tgz", @@ -9710,6 +10079,59 @@ "node": ">=6.0" } }, + "node_modules/guid-typescript": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/guid-typescript/-/guid-typescript-1.0.9.tgz", + "integrity": "sha512-Y8T4vYhEfwJOTbouREvG+3XDsjr8E3kIr7uf+JZ0BYloFsttiHU0WfvANVsR7TxNUJa/WpCnw/Ino/p+DeBhBQ==", + "license": "ISC" + }, + "node_modules/gun": { + "version": "0.2020.1241", + "resolved": "https://registry.npmjs.org/gun/-/gun-0.2020.1241.tgz", + "integrity": "sha512-rmGqLuJj4fAuZ/0lddCvXHbENPkEnBOBYpq+kXHrwQ5RdNtQ5p0Io99lD1qUXMFmtwNacQ/iqo3VTmjmMyAYZg==", + "license": "(Zlib OR MIT OR Apache-2.0)", + "dependencies": { + "ws": "^7.2.1" + }, + "engines": { + "node": ">=0.8.4" + }, + "optionalDependencies": { + "@peculiar/webcrypto": "^1.1.1" + } + }, + "node_modules/gun/node_modules/ws": { + "version": "7.5.10", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz", + "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==", + "license": "MIT", + "engines": { + "node": ">=8.3.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/h3-js": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/h3-js/-/h3-js-4.3.0.tgz", + "integrity": "sha512-zgvyHZz5bEKeuyYGh0bF9/kYSxJ2SqroopkXHqKnD3lfjaZawcxulcI9nWbNC54gakl/2eObRLHWueTf1iLSaA==", + "license": "Apache-2.0", + "engines": { + "node": ">=4", + "npm": ">=3", + "yarn": ">=1.3.0" + } + }, "node_modules/hamt_plus": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/hamt_plus/-/hamt_plus-1.0.2.tgz", @@ -10101,6 +10523,27 @@ "he": "bin/he" } }, + "node_modules/holosphere": { + "version": "1.1.20", + "resolved": "https://registry.npmjs.org/holosphere/-/holosphere-1.1.20.tgz", + "integrity": "sha512-Q++C7cuU1ubF6LPQ8YRYJJsFwK4HxnYgqm0FekNBOGEGdWOiEf3muN5kQpmMl7u3wf1H69hWrfsU8up0ppfbIw==", + "license": "GPL-3.0-or-later", + "dependencies": { + "ajv": "^8.12.0", + "gun": "^0.2020.1240", + "h3-js": "^4.1.0", + "openai": "^4.85.1" + }, + "peerDependencies": { + "gun": "^0.2020.1240", + "h3-js": "^4.1.0" + }, + "peerDependenciesMeta": { + "openai": { + "optional": true + } + } + }, "node_modules/hotkeys-js": { "version": "3.13.15", "resolved": "https://registry.npmjs.org/hotkeys-js/-/hotkeys-js-3.13.15.tgz", @@ -10361,6 +10804,12 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "license": "ISC" }, + "node_modules/ini": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "license": "ISC" + }, "node_modules/inline-style-parser": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/inline-style-parser/-/inline-style-parser-0.2.4.tgz", @@ -10650,7 +11099,6 @@ "version": "0.3.2", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==", - "dev": true, "license": "MIT" }, "node_modules/is-buffer": { @@ -11089,11 +11537,10 @@ } }, "node_modules/json-schema-traverse": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", - "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", - "license": "MIT", - "optional": true + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "license": "MIT" }, "node_modules/json5": { "version": "2.2.3", @@ -12470,6 +12917,18 @@ "node": ">=6" } }, + "node_modules/mimic-response": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", + "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/miniflare": { "version": "4.20250829.0", "resolved": "https://registry.npmjs.org/miniflare/-/miniflare-4.20250829.0.tgz", @@ -12604,6 +13063,12 @@ "mkdirp": "bin/cmd.js" } }, + "node_modules/mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", + "license": "MIT" + }, "node_modules/module-error": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/module-error/-/module-error-1.0.2.tgz", @@ -12667,6 +13132,12 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, + "node_modules/napi-build-utils": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz", + "integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==", + "license": "MIT" + }, "node_modules/napi-macros": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/napi-macros/-/napi-macros-2.2.2.tgz", @@ -12692,6 +13163,36 @@ "tslib": "^2.0.3" } }, + "node_modules/node-abi": { + "version": "3.77.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.77.0.tgz", + "integrity": "sha512-DSmt0OEcLoK4i3NuscSbGjOf3bqiDEutejqENSplMSFA/gmB8mkED9G4pKWnPl7MDU4rSHebKPHeitpDfyH0cQ==", + "license": "MIT", + "dependencies": { + "semver": "^7.3.5" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/node-abi/node_modules/semver": { + "version": "7.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz", + "integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/node-addon-api": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-6.1.0.tgz", + "integrity": "sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==", + "license": "MIT" + }, "node_modules/node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -12876,6 +13377,88 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/onnx-proto": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/onnx-proto/-/onnx-proto-4.0.4.tgz", + "integrity": "sha512-aldMOB3HRoo6q/phyB6QRQxSt895HNNw82BNyZ2CMh4bjeKv7g/c+VpAFtJuEMVfYLMbRx61hbuqnKceLeDcDA==", + "license": "MIT", + "dependencies": { + "protobufjs": "^6.8.8" + } + }, + "node_modules/onnx-proto/node_modules/long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==", + "license": "Apache-2.0" + }, + "node_modules/onnx-proto/node_modules/protobufjs": { + "version": "6.11.4", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.4.tgz", + "integrity": "sha512-5kQWPaJHi1WoCpjTGszzQ32PG2F4+wRY6BmAT4Vfw56Q2FZ4YZzK20xUYQH4YkfehY1e6QSICrJquM6xXZNcrw==", + "hasInstallScript": true, + "license": "BSD-3-Clause", + "dependencies": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.4", + "@protobufjs/eventemitter": "^1.1.0", + "@protobufjs/fetch": "^1.1.0", + "@protobufjs/float": "^1.0.2", + "@protobufjs/inquire": "^1.1.0", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.0", + "@types/long": "^4.0.1", + "@types/node": ">=13.7.0", + "long": "^4.0.0" + }, + "bin": { + "pbjs": "bin/pbjs", + "pbts": "bin/pbts" + } + }, + "node_modules/onnxruntime-common": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.14.0.tgz", + "integrity": "sha512-3LJpegM2iMNRX2wUmtYfeX/ytfOzNwAWKSq1HbRrKc9+uqG/FsEA0bbKZl1btQeZaXhC26l44NWpNUeXPII7Ew==", + "license": "MIT" + }, + "node_modules/onnxruntime-node": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.14.0.tgz", + "integrity": "sha512-5ba7TWomIV/9b6NH/1x/8QEeowsb+jBEvFzU6z0T4mNsFwdPqXeFUM7uxC6QeSRkEbWu3qEB0VMjrvzN/0S9+w==", + "license": "MIT", + "optional": true, + "os": [ + "win32", + "darwin", + "linux" + ], + "dependencies": { + "onnxruntime-common": "~1.14.0" + } + }, + "node_modules/onnxruntime-web": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/onnxruntime-web/-/onnxruntime-web-1.14.0.tgz", + "integrity": "sha512-Kcqf43UMfW8mCydVGcX9OMXI2VN17c0p6XvR7IPSZzBf/6lteBzXHvcEVWDPmCKuGombl997HgLqj91F11DzXw==", + "license": "MIT", + "dependencies": { + "flatbuffers": "^1.12.0", + "guid-typescript": "^1.0.9", + "long": "^4.0.0", + "onnx-proto": "^4.0.4", + "onnxruntime-common": "~1.14.0", + "platform": "^1.3.6" + } + }, + "node_modules/onnxruntime-web/node_modules/long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==", + "license": "Apache-2.0" + }, "node_modules/openai": { "version": "4.104.0", "resolved": "https://registry.npmjs.org/openai/-/openai-4.104.0.tgz", @@ -13132,6 +13715,12 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/platform": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz", + "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==", + "license": "MIT" + }, "node_modules/postcss": { "version": "8.5.6", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.6.tgz", @@ -13161,6 +13750,60 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/prebuild-install": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz", + "integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==", + "license": "MIT", + "dependencies": { + "detect-libc": "^2.0.0", + "expand-template": "^2.0.3", + "github-from-package": "0.0.0", + "minimist": "^1.2.3", + "mkdirp-classic": "^0.5.3", + "napi-build-utils": "^2.0.0", + "node-abi": "^3.3.0", + "pump": "^3.0.0", + "rc": "^1.2.7", + "simple-get": "^4.0.0", + "tar-fs": "^2.0.0", + "tunnel-agent": "^0.6.0" + }, + "bin": { + "prebuild-install": "bin.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/prebuild-install/node_modules/tar-fs": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.4.tgz", + "integrity": "sha512-mDAjwmZdh7LTT6pNleZ05Yt65HC3E+NiQzl672vQG38jIrehtJk/J3mNwIg+vShQPcLF/LV7CMnDW6vjj6sfYQ==", + "license": "MIT", + "dependencies": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.1.4" + } + }, + "node_modules/prebuild-install/node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "license": "MIT", + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/pretty-error": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/pretty-error/-/pretty-error-4.0.0.tgz", @@ -13496,6 +14139,26 @@ "node": ">=6" } }, + "node_modules/pvtsutils": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/pvtsutils/-/pvtsutils-1.3.6.tgz", + "integrity": "sha512-PLgQXQ6H2FWCaeRak8vvk1GW462lMxB5s3Jm673N82zI4vqtVUPuZdffdZbPDFRoU8kAhItWFtPCWiPpp4/EDg==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.8.1" + } + }, + "node_modules/pvutils": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/pvutils/-/pvutils-1.1.3.tgz", + "integrity": "sha512-pMpnA0qRdFp32b1sJl1wOJNxZLQ2cbQx+k6tjNtZ8CpvVhNqEPRgivZ2WOUev2YMajecdH7ctUPDvEe87nariQ==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/querystringify": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", @@ -13703,6 +14366,21 @@ "quickselect": "^3.0.0" } }, + "node_modules/rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "license": "(BSD-2-Clause OR MIT OR Apache-2.0)", + "dependencies": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "bin": { + "rc": "cli.js" + } + }, "node_modules/react": { "version": "18.3.1", "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", @@ -13896,6 +14574,20 @@ "react": ">16.0.0" } }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "license": "MIT", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/readdirp": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-4.1.2.tgz", @@ -14663,11 +15355,55 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, + "node_modules/simple-concat": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz", + "integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/simple-get": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz", + "integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "decompress-response": "^6.0.0", + "once": "^1.3.1", + "simple-concat": "^1.0.0" + } + }, "node_modules/simple-swizzle": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", "integrity": "sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==", - "dev": true, "license": "MIT", "dependencies": { "is-arrayish": "^0.3.1" @@ -14819,6 +15555,26 @@ "wrappy": "1" } }, + "node_modules/streamx": { + "version": "2.23.0", + "resolved": "https://registry.npmjs.org/streamx/-/streamx-2.23.0.tgz", + "integrity": "sha512-kn+e44esVfn2Fa/O0CPFcex27fjIL6MkVae0Mm6q+E6f0hWv578YCERbv+4m02cjxvDsPKLnmxral/rR6lBMAg==", + "license": "MIT", + "dependencies": { + "events-universal": "^1.0.0", + "fast-fifo": "^1.3.2", + "text-decoder": "^1.1.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -14878,6 +15634,15 @@ "node": ">=6" } }, + "node_modules/strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/style-to-js": { "version": "1.1.17", "resolved": "https://registry.npmjs.org/style-to-js/-/style-to-js-1.1.17.tgz", @@ -14929,6 +15694,30 @@ "node": ">=12.0.0" } }, + "node_modules/svix": { + "version": "1.78.0", + "resolved": "https://registry.npmjs.org/svix/-/svix-1.78.0.tgz", + "integrity": "sha512-b3jWferfmVHznKkeLNQPgMbjVKafao2Sz0quMWz6jyTrtRPZRieRU5HPoklSYDEpoe71y4/rKmVQlqC8+WN+nQ==", + "license": "MIT", + "dependencies": { + "@stablelib/base64": "^1.0.0", + "fast-sha256": "^1.3.0", + "uuid": "^10.0.0" + } + }, + "node_modules/svix/node_modules/uuid": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz", + "integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/swr": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/swr/-/swr-2.3.6.tgz", @@ -14979,6 +15768,31 @@ "node": ">=4.5" } }, + "node_modules/tar-fs": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-3.1.1.tgz", + "integrity": "sha512-LZA0oaPOc2fVo82Txf3gw+AkEd38szODlptMYejQUhndHMLQ9M059uXR+AfS7DNo0NpINvSqDsvyaCrBVkptWg==", + "license": "MIT", + "dependencies": { + "pump": "^3.0.0", + "tar-stream": "^3.1.5" + }, + "optionalDependencies": { + "bare-fs": "^4.0.1", + "bare-path": "^3.0.0" + } + }, + "node_modules/tar-stream": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-3.1.7.tgz", + "integrity": "sha512-qJj60CXt7IU1Ffyc3NJMjh6EkuCFej46zUqJ4J7pqYlThyd9bO0XBTmcOIhSzZJVWfsLks0+nle/j538YAW9RQ==", + "license": "MIT", + "dependencies": { + "b4a": "^1.6.4", + "fast-fifo": "^1.2.0", + "streamx": "^2.15.0" + } + }, "node_modules/terser": { "version": "5.44.0", "resolved": "https://registry.npmjs.org/terser/-/terser-5.44.0.tgz", @@ -15003,6 +15817,15 @@ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", "license": "MIT" }, + "node_modules/text-decoder": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/text-decoder/-/text-decoder-1.2.3.tgz", + "integrity": "sha512-3/o9z3X0X0fTupwsYvR03pJ/DjWuqqrfwBgTQzdWDiQSm9KitAyz/9WqsT2JQW7KV2m+bC2ol/zqpW37NHxLaA==", + "license": "Apache-2.0", + "dependencies": { + "b4a": "^1.6.4" + } + }, "node_modules/text-segmentation": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/text-segmentation/-/text-segmentation-1.0.3.tgz", @@ -15264,6 +16087,18 @@ "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "license": "0BSD" }, + "node_modules/tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + }, + "engines": { + "node": "*" + } + }, "node_modules/tweetnacl": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", @@ -15660,6 +16495,12 @@ "csstype": "^3.0.2" } }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, "node_modules/utila": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/utila/-/utila-0.4.0.tgz", @@ -16041,6 +16882,20 @@ "integrity": "sha512-B1JDm0XDaQC+uvo4DT79H0XmBskgS3l6Ve+1SBCfxgmtIb1AVrPIoqd+nPSv+loMX8szQ0sVUhGngL7D5QPiXw==", "license": "BSD-3-Clause" }, + "node_modules/webcrypto-core": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/webcrypto-core/-/webcrypto-core-1.8.1.tgz", + "integrity": "sha512-P+x1MvlNCXlKbLSOY4cYrdreqPG5hbzkmawbcXLKN/mf6DZW0SdNNkZ+sjwsqVkI4A4Ko2sPZmkZtCKY58w83A==", + "license": "MIT", + "optional": true, + "dependencies": { + "@peculiar/asn1-schema": "^2.3.13", + "@peculiar/json-schema": "^1.1.12", + "asn1js": "^3.0.5", + "pvtsutils": "^1.3.5", + "tslib": "^2.7.0" + } + }, "node_modules/webidl-conversions": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", @@ -16893,7 +17748,6 @@ "version": "3.22.3", "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.3.tgz", "integrity": "sha512-EjIevzuJRiRPbVH4mGc8nApb/lVLKVpmUhAaR5R5doKGfAnGJ6Gr3CViAVjP+4FWSxCsybeWQdcgCtbX+7oZug==", - "dev": true, "license": "MIT", "funding": { "url": "https://github.com/sponsors/colinhacks" diff --git a/package.json b/package.json index 5628845..3dd79d4 100644 --- a/package.json +++ b/package.json @@ -35,10 +35,16 @@ "@types/marked": "^5.0.2", "@uiw/react-md-editor": "^4.0.5", "@vercel/analytics": "^1.2.2", + "@xenova/transformers": "^2.17.2", "ai": "^4.1.0", + "ajv": "^8.17.1", "cherry-markdown": "^0.8.57", "cloudflare-workers-unfurl": "^0.0.7", + "fathom-typescript": "^0.0.36", "gray-matter": "^4.0.3", + "gun": "^0.2020.1241", + "h3-js": "^4.3.0", + "holosphere": "^1.1.20", "html2canvas": "^1.4.1", "itty-router": "^5.0.17", "jotai": "^2.6.0", diff --git a/src/App.tsx b/src/App.tsx index a3d1af6..55564d8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -17,7 +17,11 @@ import "@/css/auth.css"; // Import auth styles import "@/css/crypto-auth.css"; // Import crypto auth styles import "@/css/starred-boards.css"; // Import starred boards styles import "@/css/user-profile.css"; // Import user profile styles +import "@/css/location.css"; // Import location sharing styles import { Dashboard } from "./routes/Dashboard"; +import { LocationShareCreate } from "./routes/LocationShareCreate"; +import { LocationShareView } from "./routes/LocationShareView"; +import { LocationDashboardRoute } from "./routes/LocationDashboardRoute"; import { useState, useEffect } from 'react'; // Import React Context providers @@ -148,6 +152,22 @@ const AppWithProviders = () => { } /> + {/* Location sharing routes */} + + + + } /> + + + + } /> + + + + } /> diff --git a/src/GestureTool.ts b/src/GestureTool.ts index c36a05e..ac54418 100644 --- a/src/GestureTool.ts +++ b/src/GestureTool.ts @@ -200,6 +200,8 @@ export class Drawing extends StateNode { onGestureEnd = () => { const shape = this.editor.getShape(this.initialShape?.id!) as TLDrawShape + if (!shape) return + const ps = shape.props.segments[0].points.map((s) => ({ x: s.x, y: s.y })) const gesture = this.editor.inputs.shiftKey ? GestureTool.recognizerAlt.recognize(ps) : GestureTool.recognizer.recognize(ps) const score_pass = gesture.score > 0.2 @@ -210,52 +212,63 @@ export class Drawing extends StateNode { } else if (!score_confident) { score_color = "yellow" } + + // Execute the gesture action if recognized if (score_pass) { gesture.onComplete?.(this.editor, shape) } - let opacity = 1 - const labelShape: TLShapePartial = { - id: createShapeId(), - type: "text", - x: this.editor.inputs.currentPagePoint.x + 20, - y: this.editor.inputs.currentPagePoint.y, - isLocked: false, - props: { - size: "xl", - text: gesture.name, - color: score_color, - } as any, - } + + // Delete the gesture shape immediately - it's just a command, not a persistent shape + this.editor.deleteShape(shape.id) + + // Optionally show a temporary label with fade-out if (SHOW_LABELS) { + const labelShape: TLShapePartial = { + id: createShapeId(), + type: "text", + x: this.editor.inputs.currentPagePoint.x + 20, + y: this.editor.inputs.currentPagePoint.y, + isLocked: false, + props: { + size: "xl", + richText: { + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: gesture.name, + }, + ], + }, + ], + type: "doc", + }, + color: score_color, + }, + } this.editor.createShape(labelShape) - } - const intervalId = setInterval(() => { - if (opacity > 0) { - this.editor.updateShape({ - ...shape, - opacity: opacity, - props: { - ...shape.props, - color: score_color, - }, - }) - this.editor.updateShape({ - ...labelShape, - opacity: opacity, - props: { - ...labelShape.props, - color: score_color, - }, - }) - opacity = Math.max(0, opacity - 0.025) - } else { - clearInterval(intervalId) - this.editor.deleteShape(shape.id) - if (SHOW_LABELS) { + + // Fade out and delete the label + let opacity = 1 + const intervalId = setInterval(() => { + if (opacity > 0) { + this.editor.updateShape({ + ...labelShape, + opacity: opacity, + props: { + ...labelShape.props, + color: score_color, + }, + }) + opacity = Math.max(0, opacity - 0.025) + } else { + clearInterval(intervalId) this.editor.deleteShape(labelShape.id) } - } - }, 20) + }, 20) + } } override onPointerMove: TLEventHandlers["onPointerMove"] = () => { diff --git a/src/automerge/AutomergeToTLStore.ts b/src/automerge/AutomergeToTLStore.ts index a65d229..9b508f0 100644 --- a/src/automerge/AutomergeToTLStore.ts +++ b/src/automerge/AutomergeToTLStore.ts @@ -12,9 +12,25 @@ export function applyAutomergePatchesToTLStore( if (!isStorePatch(patch)) return const id = pathToId(patch.path) + + // Skip records with empty or invalid IDs + if (!id || id === '') { + return + } + + // CRITICAL: Skip custom record types that aren't TLDraw records + // These should only exist in Automerge, not in TLDraw store + // Components like ObsidianVaultBrowser read directly from Automerge + if (typeof id === 'string' && id.startsWith('obsidian_vault:')) { + return // Skip - not a TLDraw record, don't process + } + const existingRecord = getRecordFromStore(store, id) - const record = updatedObjects[id] || (existingRecord ? JSON.parse(JSON.stringify(existingRecord)) : { - id, + + // Infer typeName from ID pattern if record doesn't exist + let defaultTypeName = 'shape' + let defaultRecord: any = { + id, typeName: 'shape', type: 'geo', // Default shape type x: 0, @@ -24,7 +40,82 @@ export function applyAutomergePatchesToTLStore( opacity: 1, meta: {}, props: {} - }) + } + + // Check if ID pattern indicates a record type + // Note: obsidian_vault records are skipped above, so we don't need to handle them here + if (typeof id === 'string') { + if (id.startsWith('shape:')) { + defaultTypeName = 'shape' + // Keep default shape record structure + } else if (id.startsWith('page:')) { + defaultTypeName = 'page' + defaultRecord = { + id, + typeName: 'page', + name: '', + index: 'a0' as any, + meta: {} + } + } else if (id.startsWith('camera:')) { + defaultTypeName = 'camera' + defaultRecord = { + id, + typeName: 'camera', + x: 0, + y: 0, + z: 1, + meta: {} + } + } else if (id.startsWith('instance:')) { + defaultTypeName = 'instance' + defaultRecord = { + id, + typeName: 'instance', + currentPageId: 'page:page' as any, + meta: {} + } + } else if (id.startsWith('pointer:')) { + defaultTypeName = 'pointer' + defaultRecord = { + id, + typeName: 'pointer', + x: 0, + y: 0, + lastActivityTimestamp: 0, + meta: {} + } + } else if (id.startsWith('document:')) { + defaultTypeName = 'document' + defaultRecord = { + id, + typeName: 'document', + gridSize: 10, + name: '', + meta: {} + } + } + } + + const record = updatedObjects[id] || (existingRecord ? JSON.parse(JSON.stringify(existingRecord)) : defaultRecord) + + // CRITICAL: Ensure typeName matches ID pattern (fixes misclassification) + // Note: obsidian_vault records are skipped above, so we don't need to handle them here + if (typeof id === 'string') { + if (id.startsWith('shape:') && record.typeName !== 'shape') { + record.typeName = 'shape' + } else if (id.startsWith('page:') && record.typeName !== 'page') { + record.typeName = 'page' + } else if (id.startsWith('camera:') && record.typeName !== 'camera') { + record.typeName = 'camera' + } else if (id.startsWith('instance:') && record.typeName !== 'instance') { + record.typeName = 'instance' + } else if (id.startsWith('pointer:') && record.typeName !== 'pointer') { + record.typeName = 'pointer' + } else if (id.startsWith('document:') && record.typeName !== 'document') { + record.typeName = 'document' + } + } switch (patch.action) { case "insert": { @@ -58,6 +149,9 @@ export function applyAutomergePatchesToTLStore( console.log("Unsupported patch:", patch) } } + + // CRITICAL: Re-check typeName after patch application to ensure it's still correct + // Note: obsidian_vault records are skipped above, so we don't need to handle them here }) // Sanitize records before putting them in the store @@ -65,264 +159,383 @@ export function applyAutomergePatchesToTLStore( const failedRecords: any[] = [] Object.values(updatedObjects).forEach(record => { + // Skip records with empty or invalid IDs + if (!record || !record.id || record.id === '') { + return + } + + // CRITICAL: Skip custom record types that aren't TLDraw records + // These should only exist in Automerge, not in TLDraw store + if (typeof record.id === 'string' && record.id.startsWith('obsidian_vault:')) { + return // Skip - not a TLDraw record + } + try { const sanitized = sanitizeRecord(record) toPut.push(sanitized) } catch (error) { + // If it's a missing typeName/id error, skip it + if (error instanceof Error && + (error.message.includes('missing required typeName') || + error.message.includes('missing required id'))) { + // Skip records with missing required fields + return + } console.error("Failed to sanitize record:", error, record) failedRecords.push(record) } }) // put / remove the records in the store - console.log({ patches, toPut: toPut.length, failed: failedRecords.length }) + // Log patch application for debugging + console.log(`๐Ÿ”ง AutomergeToTLStore: Applying ${patches.length} patches, ${toPut.length} records to put, ${toRemove.length} records to remove`) + + if (failedRecords.length > 0) { + console.log({ patches, toPut: toPut.length, failed: failedRecords.length }) + } if (failedRecords.length > 0) { console.error("Failed to sanitize records:", failedRecords) } + // CRITICAL: Final safety check - ensure no geo shapes have w/h/geo at top level + // Also ensure text shapes don't have props.text (should use props.richText instead) + const finalSanitized = toPut.map(record => { + if (record.typeName === 'shape' && record.type === 'geo') { + // Store values before removing from top level + const wValue = 'w' in record ? (record as any).w : undefined + const hValue = 'h' in record ? (record as any).h : undefined + const geoValue = 'geo' in record ? (record as any).geo : undefined + + // Create cleaned record without w/h/geo at top level + const cleaned: any = {} + for (const key in record) { + if (key !== 'w' && key !== 'h' && key !== 'geo') { + cleaned[key] = (record as any)[key] + } + } + + // Ensure props exists and move values there if needed + if (!cleaned.props) cleaned.props = {} + if (wValue !== undefined && (!('w' in cleaned.props) || cleaned.props.w === undefined)) { + cleaned.props.w = wValue + } + if (hValue !== undefined && (!('h' in cleaned.props) || cleaned.props.h === undefined)) { + cleaned.props.h = hValue + } + if (geoValue !== undefined && (!('geo' in cleaned.props) || cleaned.props.geo === undefined)) { + cleaned.props.geo = geoValue + } + + return cleaned as TLRecord + } + + // CRITICAL: Remove props.text from text shapes (TLDraw schema doesn't allow it) + if (record.typeName === 'shape' && record.type === 'text' && (record as any).props && 'text' in (record as any).props) { + const cleaned = { ...record } + if (cleaned.props && 'text' in cleaned.props) { + delete (cleaned.props as any).text + } + return cleaned as TLRecord + } + + return record + }) + store.mergeRemoteChanges(() => { if (toRemove.length) store.remove(toRemove) - if (toPut.length) store.put(toPut) + if (finalSanitized.length) store.put(finalSanitized) }) } -// Sanitize record to remove invalid properties +// Helper function to clean NaN values from richText content +// This prevents SVG export errors when TLDraw tries to render text with invalid coordinates +function cleanRichTextNaN(richText: any): any { + if (!richText || typeof richText !== 'object') { + return richText + } + + // Deep clone to avoid mutating the original + const cleaned = JSON.parse(JSON.stringify(richText)) + + // Recursively clean content array + if (Array.isArray(cleaned.content)) { + cleaned.content = cleaned.content.map((item: any) => { + if (typeof item === 'object' && item !== null) { + // Remove any NaN values from the item + const cleanedItem: any = {} + for (const key in item) { + const value = item[key] + // Skip NaN values - they cause SVG export errors + if (typeof value === 'number' && isNaN(value)) { + // Skip NaN values + continue + } + // Recursively clean nested objects + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + cleanedItem[key] = cleanRichTextNaN(value) + } else if (Array.isArray(value)) { + cleanedItem[key] = value.map((v: any) => + typeof v === 'object' && v !== null ? cleanRichTextNaN(v) : v + ) + } else { + cleanedItem[key] = value + } + } + return cleanedItem + } + return item + }) + } + + return cleaned +} + +// Minimal sanitization - only fix critical issues that break TLDraw function sanitizeRecord(record: any): TLRecord { const sanitized = { ...record } - // Ensure required fields exist for all records - if (!sanitized.id) { - console.error("Record missing required id field:", record) + // CRITICAL FIXES ONLY - preserve all other properties + + // Only fix critical structural issues + if (!sanitized.id || sanitized.id === '') { throw new Error("Record missing required id field") } - if (!sanitized.typeName) { - console.error("Record missing required typeName field:", record) + if (!sanitized.typeName || sanitized.typeName === '') { throw new Error("Record missing required typeName field") } - // Remove invalid properties from shapes + // For shapes, only ensure basic required fields exist if (sanitized.typeName === 'shape') { // Ensure required shape fields exist - if (!sanitized.type || typeof sanitized.type !== 'string') { - console.error("Shape missing or invalid type field:", { - id: sanitized.id, - typeName: sanitized.typeName, - currentType: sanitized.type, - record: sanitized - }) - // Try to infer type from other properties or use a default - if (sanitized.props?.geo) { - sanitized.type = 'geo' - } else if (sanitized.props?.text) { - sanitized.type = 'text' - } else if (sanitized.props?.roomUrl) { - sanitized.type = 'VideoChat' - } else if (sanitized.props?.roomId) { - sanitized.type = 'ChatBox' - } else if (sanitized.props?.url) { - sanitized.type = 'Embed' - } else if (sanitized.props?.prompt) { - sanitized.type = 'Prompt' - } else if (sanitized.props?.isMinimized !== undefined) { - sanitized.type = 'SharedPiano' - } else if (sanitized.props?.isTranscribing !== undefined) { - sanitized.type = 'Transcription' - } else if (sanitized.props?.noteId) { - sanitized.type = 'ObsNote' - } else { - sanitized.type = 'geo' // Default fallback - } - console.log(`๐Ÿ”ง Fixed missing/invalid type field for shape ${sanitized.id}, set to: ${sanitized.type}`) - } - - // Ensure type is a valid string - if (typeof sanitized.type !== 'string') { - console.error("Shape type is not a string:", sanitized.type, "for shape:", sanitized.id) - sanitized.type = 'geo' // Force to valid string - } - - // Ensure other required shape fields exist - if (typeof sanitized.x !== 'number') { - sanitized.x = 0 - } - if (typeof sanitized.y !== 'number') { - sanitized.y = 0 - } - if (typeof sanitized.rotation !== 'number') { - sanitized.rotation = 0 - } - if (typeof sanitized.isLocked !== 'boolean') { - sanitized.isLocked = false - } - if (typeof sanitized.opacity !== 'number') { - sanitized.opacity = 1 - } + if (typeof sanitized.x !== 'number') sanitized.x = 0 + if (typeof sanitized.y !== 'number') sanitized.y = 0 + if (typeof sanitized.rotation !== 'number') sanitized.rotation = 0 + if (typeof sanitized.isLocked !== 'boolean') sanitized.isLocked = false + if (typeof sanitized.opacity !== 'number') sanitized.opacity = 1 + // CRITICAL: Preserve all existing meta properties - only create empty object if meta doesn't exist if (!sanitized.meta || typeof sanitized.meta !== 'object') { sanitized.meta = {} + } else { + // Ensure meta is a mutable copy to preserve all properties (including text for rectangles) + sanitized.meta = { ...sanitized.meta } } - // Remove top-level properties that should only be in props - const invalidTopLevelProperties = ['insets', 'scribbles', 'duplicateProps', 'geo', 'w', 'h'] - invalidTopLevelProperties.forEach(prop => { - if (prop in sanitized) { - console.log(`Moving ${prop} property from top-level to props for shape during patch application:`, { - id: sanitized.id, - type: sanitized.type, - originalValue: sanitized[prop] - }) - - // Move to props if props exists, otherwise create props - if (!sanitized.props) { - sanitized.props = {} - } - sanitized.props[prop] = sanitized[prop] - delete sanitized[prop] - } - }) + if (!sanitized.index) sanitized.index = 'a1' + if (!sanitized.parentId) sanitized.parentId = 'page:page' + if (!sanitized.props || typeof sanitized.props !== 'object') sanitized.props = {} - // Ensure props object exists for all shapes - if (!sanitized.props) { - sanitized.props = {} + // CRITICAL: Ensure props is a deep mutable copy to preserve all nested properties + // This is essential for custom shapes like ObsNote and for preserving richText in geo shapes + // Use JSON parse/stringify to create a deep copy of nested objects (like richText.content) + sanitized.props = JSON.parse(JSON.stringify(sanitized.props)) + + // CRITICAL: Infer type from properties BEFORE defaulting to 'geo' + // This ensures arrows and other shapes are properly recognized + if (!sanitized.type || typeof sanitized.type !== 'string') { + // Check for arrow-specific properties first + if (sanitized.props?.start !== undefined || + sanitized.props?.end !== undefined || + sanitized.props?.arrowheadStart !== undefined || + sanitized.props?.arrowheadEnd !== undefined || + sanitized.props?.kind === 'line' || + sanitized.props?.kind === 'curved' || + sanitized.props?.kind === 'straight') { + sanitized.type = 'arrow' + } + // Check for line-specific properties + else if (sanitized.props?.points !== undefined) { + sanitized.type = 'line' + } + // Check for geo-specific properties (w/h/geo) + else if (sanitized.props?.geo !== undefined || + ('w' in sanitized && 'h' in sanitized) || + ('w' in sanitized.props && 'h' in sanitized.props)) { + sanitized.type = 'geo' + } + // Check for note-specific properties + else if (sanitized.props?.growY !== undefined || + sanitized.props?.verticalAlign !== undefined) { + sanitized.type = 'note' + } + // Check for text-specific properties + else if (sanitized.props?.textAlign !== undefined || + sanitized.props?.autoSize !== undefined) { + sanitized.type = 'text' + } + // Check for draw-specific properties + else if (sanitized.props?.segments !== undefined) { + sanitized.type = 'draw' + } + // Default to geo only if no other indicators found + else { + sanitized.type = 'geo' + } } - // Fix geo shape specific properties - if (sanitized.type === 'geo') { - // Ensure geo shape has proper structure - if (!sanitized.props.geo) { - sanitized.props.geo = 'rectangle' - } - if (!sanitized.props.w) { - sanitized.props.w = 100 - } - if (!sanitized.props.h) { - sanitized.props.h = 100 + // CRITICAL: For geo shapes, move w/h/geo from top level to props (required by TLDraw schema) + if (sanitized.type === 'geo' || ('w' in sanitized && 'h' in sanitized && sanitized.type !== 'arrow')) { + // If type is missing but has w/h, assume it's a geo shape (but only if not already identified as arrow) + if (!sanitized.type || sanitized.type === 'geo') { + sanitized.type = 'geo' } - // Remove invalid properties for geo shapes (including insets) - const invalidGeoProps = ['transcript', 'isTranscribing', 'isPaused', 'isEditing', 'roomUrl', 'roomId', 'prompt', 'value', 'agentBinding', 'isMinimized', 'noteId', 'title', 'content', 'tags', 'showPreview', 'backgroundColor', 'textColor', 'editingContent', 'vaultName', 'insets'] - invalidGeoProps.forEach(prop => { - if (prop in sanitized.props) { - console.log(`Removing invalid ${prop} property from geo shape:`, sanitized.id) - delete sanitized.props[prop] + // Ensure props exists + if (!sanitized.props) sanitized.props = {} + + // Store values before removing from top level + const wValue = 'w' in sanitized ? (sanitized as any).w : undefined + const hValue = 'h' in sanitized ? (sanitized as any).h : undefined + const geoValue = 'geo' in sanitized ? (sanitized as any).geo : undefined + + // Move w from top level to props (if present at top level) + if (wValue !== undefined) { + if (!('w' in sanitized.props) || sanitized.props.w === undefined) { + sanitized.props.w = wValue } - }) + delete (sanitized as any).w + } + + // Move h from top level to props (if present at top level) + if (hValue !== undefined) { + if (!('h' in sanitized.props) || sanitized.props.h === undefined) { + sanitized.props.h = hValue + } + delete (sanitized as any).h + } + + // Move geo from top level to props (if present at top level) + if (geoValue !== undefined) { + if (!('geo' in sanitized.props) || sanitized.props.geo === undefined) { + sanitized.props.geo = geoValue + } + delete (sanitized as any).geo + } + } - // Fix note shape specific properties + // Only fix type if completely missing + if (!sanitized.type || typeof sanitized.type !== 'string') { + // Simple type inference - only if absolutely necessary + if (sanitized.props?.geo) { + sanitized.type = 'geo' + } else { + sanitized.type = 'geo' // Safe default + } + } + + // CRITICAL: Fix crop structure for image/video shapes if it exists + if (sanitized.type === 'image' || sanitized.type === 'video') { + if (sanitized.props.crop !== null && sanitized.props.crop !== undefined) { + if (!sanitized.props.crop.topLeft || !sanitized.props.crop.bottomRight) { + if (sanitized.props.crop.x !== undefined && sanitized.props.crop.y !== undefined) { + // Convert old format to new format + sanitized.props.crop = { + topLeft: { x: sanitized.props.crop.x || 0, y: sanitized.props.crop.y || 0 }, + bottomRight: { + x: (sanitized.props.crop.x || 0) + (sanitized.props.crop.w || 1), + y: (sanitized.props.crop.y || 0) + (sanitized.props.crop.h || 1) + } + } + } else { + sanitized.props.crop = { + topLeft: { x: 0, y: 0 }, + bottomRight: { x: 1, y: 1 } + } + } + } + } + } + + // CRITICAL: Fix line shapes - ensure valid points structure (required by schema) + if (sanitized.type === 'line') { + // Remove invalid w/h from props (they cause validation errors) + if ('w' in sanitized.props) delete sanitized.props.w + if ('h' in sanitized.props) delete sanitized.props.h + + // Line shapes REQUIRE points property + if (!sanitized.props.points || typeof sanitized.props.points !== 'object' || Array.isArray(sanitized.props.points)) { + sanitized.props.points = { + 'a1': { id: 'a1', index: 'a1' as any, x: 0, y: 0 }, + 'a2': { id: 'a2', index: 'a2' as any, x: 100, y: 0 } + } + } + } + + // CRITICAL: Fix group shapes - remove invalid w/h from props + if (sanitized.type === 'group') { + if ('w' in sanitized.props) delete sanitized.props.w + if ('h' in sanitized.props) delete sanitized.props.h + } + + // CRITICAL: Fix note shapes - ensure richText structure if it exists if (sanitized.type === 'note') { - // Remove w/h properties from note shapes as they're not valid - if ('w' in sanitized.props) { - console.log(`Removing invalid w property from note shape:`, sanitized.id) - delete sanitized.props.w - } - if ('h' in sanitized.props) { - console.log(`Removing invalid h property from note shape:`, sanitized.id) - delete sanitized.props.h - } - } - - // Convert custom shape types to valid TLDraw types - const customShapeTypeMap: { [key: string]: string } = { - 'VideoChat': 'embed', - 'Transcription': 'text', - 'SharedPiano': 'embed', - 'Prompt': 'text', - 'ChatBox': 'embed', - 'Embed': 'embed', - 'Markdown': 'text', - 'MycrozineTemplate': 'embed', - 'Slide': 'embed', - 'ObsNote': 'text' - } - - if (customShapeTypeMap[sanitized.type]) { - console.log(`Converting custom shape type ${sanitized.type} to ${customShapeTypeMap[sanitized.type]} for shape:`, sanitized.id) - sanitized.type = customShapeTypeMap[sanitized.type] - } - - // Ensure proper props for converted shape types - if (sanitized.type === 'embed') { - // Ensure embed shapes have required properties - if (!sanitized.props.url) { - sanitized.props.url = '' - } - if (!sanitized.props.w) { - sanitized.props.w = 400 - } - if (!sanitized.props.h) { - sanitized.props.h = 300 - } - // Remove invalid properties for embed shapes - const invalidEmbedProps = ['isMinimized', 'roomUrl', 'roomId', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'richText'] - invalidEmbedProps.forEach(prop => { - if (prop in sanitized.props) { - console.log(`Removing invalid ${prop} property from embed shape:`, sanitized.id) - delete sanitized.props[prop] + if (sanitized.props.richText) { + if (Array.isArray(sanitized.props.richText)) { + sanitized.props.richText = { content: sanitized.props.richText, type: 'doc' } + } else if (typeof sanitized.props.richText === 'object' && sanitized.props.richText !== null) { + if (!sanitized.props.richText.type) sanitized.props.richText = { ...sanitized.props.richText, type: 'doc' } + if (!sanitized.props.richText.content) sanitized.props.richText = { ...sanitized.props.richText, content: [] } } - }) + } + // CRITICAL: Clean NaN values from richText content to prevent SVG export errors + if (sanitized.props.richText) { + sanitized.props.richText = cleanRichTextNaN(sanitized.props.richText) + } } - if (sanitized.type === 'text') { - // Ensure text shapes have required properties - if (!sanitized.props.text) { - sanitized.props.text = '' + // CRITICAL: Fix richText structure for geo shapes (preserve content) + if (sanitized.type === 'geo' && sanitized.props.richText) { + if (Array.isArray(sanitized.props.richText)) { + sanitized.props.richText = { content: sanitized.props.richText, type: 'doc' } + } else if (typeof sanitized.props.richText === 'object' && sanitized.props.richText !== null) { + if (!sanitized.props.richText.type) sanitized.props.richText = { ...sanitized.props.richText, type: 'doc' } + if (!sanitized.props.richText.content) sanitized.props.richText = { ...sanitized.props.richText, content: [] } } - if (!sanitized.props.w) { - sanitized.props.w = 200 - } - if (!sanitized.props.color) { - sanitized.props.color = 'black' - } - if (!sanitized.props.size) { - sanitized.props.size = 'm' - } - if (!sanitized.props.font) { - sanitized.props.font = 'draw' - } - if (!sanitized.props.textAlign) { - sanitized.props.textAlign = 'start' - } - // Text shapes don't have h property - if ('h' in sanitized.props) { - delete sanitized.props.h - } - // Remove invalid properties for text shapes - const invalidTextProps = ['isMinimized', 'roomUrl', 'roomId', 'geo', 'insets', 'scribbles'] - invalidTextProps.forEach(prop => { - if (prop in sanitized.props) { - console.log(`Removing invalid ${prop} property from text shape:`, sanitized.id) - delete sanitized.props[prop] - } - }) + // CRITICAL: Clean NaN values from richText content to prevent SVG export errors + sanitized.props.richText = cleanRichTextNaN(sanitized.props.richText) } - // General cleanup: remove any properties that might cause validation errors - const validShapeProps: { [key: string]: string[] } = { - 'geo': ['w', 'h', 'geo', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'url'], - 'text': ['w', 'text', 'color', 'fill', 'dash', 'size', 'font', 'align', 'verticalAlign', 'growY', 'url'], - 'embed': ['w', 'h', 'url', 'doesResize', 'doesResizeHeight'], - 'note': ['color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'url'], - 'arrow': ['start', 'end', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'url', 'arrowheadStart', 'arrowheadEnd'], - 'draw': ['points', 'color', 'fill', 'dash', 'size'], - 'bookmark': ['w', 'h', 'url', 'doesResize', 'doesResizeHeight'], - 'image': ['w', 'h', 'assetId', 'crop', 'doesResize', 'doesResizeHeight'], - 'video': ['w', 'h', 'assetId', 'crop', 'doesResize', 'doesResizeHeight'], - 'frame': ['w', 'h', 'name', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'url'], - 'group': ['w', 'h'], - 'highlight': ['w', 'h', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'url'], - 'line': ['x', 'y', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'url'] + // CRITICAL: Fix richText structure for text shapes + if (sanitized.type === 'text' && sanitized.props.richText) { + if (Array.isArray(sanitized.props.richText)) { + sanitized.props.richText = { content: sanitized.props.richText, type: 'doc' } + } else if (typeof sanitized.props.richText === 'object' && sanitized.props.richText !== null) { + if (!sanitized.props.richText.type) sanitized.props.richText = { ...sanitized.props.richText, type: 'doc' } + if (!sanitized.props.richText.content) sanitized.props.richText = { ...sanitized.props.richText, content: [] } + } + // CRITICAL: Clean NaN values from richText content to prevent SVG export errors + sanitized.props.richText = cleanRichTextNaN(sanitized.props.richText) } - // Remove invalid properties based on shape type - if (validShapeProps[sanitized.type]) { - const validProps = validShapeProps[sanitized.type] - Object.keys(sanitized.props).forEach(prop => { - if (!validProps.includes(prop)) { - console.log(`Removing invalid property ${prop} from ${sanitized.type} shape:`, sanitized.id) - delete sanitized.props[prop] - } - }) + // CRITICAL: Remove invalid 'text' property from text shapes (TLDraw schema doesn't allow props.text) + // Text shapes should only use props.richText, not props.text + if (sanitized.type === 'text' && 'text' in sanitized.props) { + delete sanitized.props.text + } + + // CRITICAL: Only convert unknown shapes with richText to text if they're truly unknown + // DO NOT convert geo/note shapes - they can legitimately have richText + if (sanitized.props?.richText && sanitized.type !== 'text' && sanitized.type !== 'geo' && sanitized.type !== 'note') { + // This is an unknown shape type with richText - convert to text shape + // But preserve all existing properties first + const existingProps = { ...sanitized.props } + sanitized.type = 'text' + sanitized.props = existingProps + + // Fix richText structure if needed + if (Array.isArray(sanitized.props.richText)) { + sanitized.props.richText = { content: sanitized.props.richText, type: 'doc' } + } else if (typeof sanitized.props.richText === 'object' && sanitized.props.richText !== null) { + if (!sanitized.props.richText.type) sanitized.props.richText = { ...sanitized.props.richText, type: 'doc' } + if (!sanitized.props.richText.content) sanitized.props.richText = { ...sanitized.props.richText, content: [] } + } + // CRITICAL: Clean NaN values from richText content to prevent SVG export errors + sanitized.props.richText = cleanRichTextNaN(sanitized.props.richText) + + // Only remove properties that cause validation errors (not all "invalid" ones) + if ('h' in sanitized.props) delete sanitized.props.h + if ('geo' in sanitized.props) delete sanitized.props.geo } } @@ -353,14 +566,27 @@ const applyInsertToObject = (patch: Automerge.InsertPatch, object: any): TLRecor const insertionPoint = path[path.length - 1] as number const pathEnd = path[path.length - 2] as string const parts = path.slice(2, -2) + + // Create missing properties as we navigate for (const part of parts) { - if (current[part] === undefined) { - throw new Error("NO WAY") + if (current[part] === undefined || current[part] === null) { + // Create missing property - use array for numeric indices + if (typeof part === 'number' || (typeof part === 'string' && !isNaN(Number(part)))) { + current[part] = [] + } else { + current[part] = {} + } } current = current[part] } + + // Ensure pathEnd exists and is an array + if (current[pathEnd] === undefined || current[pathEnd] === null) { + current[pathEnd] = [] + } + // splice is a mutator... yay. - const clone = current[pathEnd].slice(0) + const clone = Array.isArray(current[pathEnd]) ? current[pathEnd].slice(0) : [] clone.splice(insertionPoint, 0, ...values) current[pathEnd] = clone return object @@ -383,10 +609,24 @@ const applyPutToObject = (patch: Automerge.PutPatch, object: any): TLRecord => { return { ...object, [property]: value } } - // default case + // default case - create missing properties as we navigate for (const part of parts) { + if (current[part] === undefined || current[part] === null) { + // Create missing property - use object for named properties, array for numeric indices + if (typeof part === 'number' || (typeof part === 'string' && !isNaN(Number(part)))) { + current[part] = [] + } else { + current[part] = {} + } + } current = current[part] } + + // Ensure target exists + if (current[target] === undefined || current[target] === null) { + current[target] = {} + } + current[target] = { ...current[target], [property]: value } return object } @@ -397,12 +637,25 @@ const applySpliceToObject = (patch: Automerge.SpliceTextPatch, object: any): TLR const insertionPoint = path[path.length - 1] as number const pathEnd = path[path.length - 2] as string const parts = path.slice(2, -2) + + // Create missing properties as we navigate for (const part of parts) { - if (current[part] === undefined) { - throw new Error("NO WAY") + if (current[part] === undefined || current[part] === null) { + // Create missing property - use array for numeric indices or when splicing + if (typeof part === 'number' || (typeof part === 'string' && !isNaN(Number(part)))) { + current[part] = [] + } else { + current[part] = {} + } } current = current[part] } + + // Ensure pathEnd exists and is an array for splicing + if (current[pathEnd] === undefined || current[pathEnd] === null) { + current[pathEnd] = [] + } + // TODO: we're not supporting actual splices yet because TLDraw won't generate them natively if (insertionPoint !== 0) { throw new Error("Splices are not supported yet") diff --git a/src/automerge/CloudflareAdapter.ts b/src/automerge/CloudflareAdapter.ts index d5ab7d4..70139db 100644 --- a/src/automerge/CloudflareAdapter.ts +++ b/src/automerge/CloudflareAdapter.ts @@ -153,12 +153,18 @@ export class CloudflareAdapter { } } -class CloudflareNetworkAdapter extends NetworkAdapter { +export class CloudflareNetworkAdapter extends NetworkAdapter { private workerUrl: string private websocket: WebSocket | null = null private roomId: string | null = null private readyPromise: Promise private readyResolve: (() => void) | null = null + private keepAliveInterval: NodeJS.Timeout | null = null + private reconnectTimeout: NodeJS.Timeout | null = null + private reconnectAttempts: number = 0 + private maxReconnectAttempts: number = 5 + private reconnectDelay: number = 1000 + private isConnecting: boolean = false constructor(workerUrl: string, roomId?: string) { super() @@ -178,40 +184,93 @@ class CloudflareNetworkAdapter extends NetworkAdapter { } connect(peerId: PeerId, peerMetadata?: PeerMetadata): void { + if (this.isConnecting) { + console.log('๐Ÿ”Œ CloudflareAdapter: Connection already in progress, skipping') + return + } + + // Clean up existing connection + this.cleanup() + // Use the room ID from constructor or default // Add sessionId as a query parameter as required by AutomergeDurableObject const sessionId = peerId || `session-${Date.now()}-${Math.random().toString(36).substr(2, 9)}` const wsUrl = `${this.workerUrl.replace('http', 'ws')}/connect/${this.roomId}?sessionId=${sessionId}` + this.isConnecting = true + // Add a small delay to ensure the server is ready setTimeout(() => { try { + console.log('๐Ÿ”Œ CloudflareAdapter: Creating WebSocket connection to:', wsUrl) this.websocket = new WebSocket(wsUrl) this.websocket.onopen = () => { + console.log('๐Ÿ”Œ CloudflareAdapter: WebSocket connection opened successfully') + this.isConnecting = false + this.reconnectAttempts = 0 this.readyResolve?.() + this.startKeepAlive() } this.websocket.onmessage = (event) => { try { - const message = JSON.parse(event.data) - - // Convert the message to the format expected by Automerge - if (message.type === 'sync' && message.data) { - // For now, we'll handle the JSON data directly - // In a full implementation, this would be binary sync data + // Automerge's native protocol uses binary messages + // We need to handle both binary and text messages + if (event.data instanceof ArrayBuffer) { + console.log('๐Ÿ”Œ CloudflareAdapter: Received binary message (Automerge protocol)') + // Handle binary Automerge sync messages - pass directly to Repo + // Automerge Repo expects binary sync messages as ArrayBuffer this.emit('message', { type: 'sync', - senderId: message.senderId, - targetId: message.targetId, - documentId: message.documentId, - data: message.data + data: event.data + }) + } else if (event.data instanceof Blob) { + // Handle Blob messages (convert to ArrayBuffer) + event.data.arrayBuffer().then((buffer) => { + console.log('๐Ÿ”Œ CloudflareAdapter: Received Blob message, converted to ArrayBuffer') + this.emit('message', { + type: 'sync', + data: buffer + }) }) } else { - this.emit('message', message) + // Handle text messages (our custom protocol for backward compatibility) + const message = JSON.parse(event.data) + console.log('๐Ÿ”Œ CloudflareAdapter: Received WebSocket message:', message.type) + + // Handle ping/pong messages for keep-alive + if (message.type === 'ping') { + this.sendPong() + return + } + + // Handle test messages + if (message.type === 'test') { + console.log('๐Ÿ”Œ CloudflareAdapter: Received test message:', message.message) + return + } + + // Convert the message to the format expected by Automerge + if (message.type === 'sync' && message.data) { + console.log('๐Ÿ”Œ CloudflareAdapter: Received sync message with data:', { + hasStore: !!message.data.store, + storeKeys: message.data.store ? Object.keys(message.data.store).length : 0 + }) + // For backward compatibility, handle JSON sync data + this.emit('message', { + type: 'sync', + senderId: message.senderId, + targetId: message.targetId, + documentId: message.documentId, + data: message.data + }) + } else { + this.emit('message', message) + } } } catch (error) { - console.error('Error parsing WebSocket message:', error) + console.error('โŒ CloudflareAdapter: Error parsing WebSocket message:', error) } } @@ -219,16 +278,30 @@ class CloudflareNetworkAdapter extends NetworkAdapter { console.log('Disconnected from Cloudflare WebSocket', { code: event.code, reason: event.reason, - wasClean: event.wasClean + wasClean: event.wasClean, + url: wsUrl, + reconnectAttempts: this.reconnectAttempts }) + + this.isConnecting = false + this.stopKeepAlive() + + // Log specific error codes for debugging + if (event.code === 1005) { + console.error('โŒ WebSocket closed with code 1005 (No Status Received) - this usually indicates a connection issue or idle timeout') + } else if (event.code === 1006) { + console.error('โŒ WebSocket closed with code 1006 (Abnormal Closure) - connection was lost unexpectedly') + } else if (event.code === 1011) { + console.error('โŒ WebSocket closed with code 1011 (Server Error) - server encountered an error') + } else if (event.code === 1000) { + console.log('โœ… WebSocket closed normally (code 1000)') + return // Don't reconnect on normal closure + } + this.emit('close') - // Attempt to reconnect after a delay - setTimeout(() => { - if (this.roomId) { - console.log('Attempting to reconnect WebSocket...') - this.connect(peerId, peerMetadata) - } - }, 5000) + + // Attempt to reconnect with exponential backoff + this.scheduleReconnect(peerId, peerMetadata) } this.websocket.onerror = (error) => { @@ -240,9 +313,11 @@ class CloudflareNetworkAdapter extends NetworkAdapter { target: error.target, isTrusted: error.isTrusted }) + this.isConnecting = false } } catch (error) { console.error('Failed to create WebSocket:', error) + this.isConnecting = false return } }, 100) @@ -250,8 +325,31 @@ class CloudflareNetworkAdapter extends NetworkAdapter { send(message: Message): void { if (this.websocket && this.websocket.readyState === WebSocket.OPEN) { - console.log('Sending WebSocket message:', message.type) - this.websocket.send(JSON.stringify(message)) + // Check if this is a binary sync message from Automerge Repo + if (message.type === 'sync' && (message as any).data instanceof ArrayBuffer) { + console.log('๐Ÿ”Œ CloudflareAdapter: Sending binary sync message (Automerge protocol)') + // Send binary data directly for Automerge's native sync protocol + this.websocket.send((message as any).data) + } else if (message.type === 'sync' && (message as any).data instanceof Uint8Array) { + console.log('๐Ÿ”Œ CloudflareAdapter: Sending Uint8Array sync message (Automerge protocol)') + // Convert Uint8Array to ArrayBuffer and send + this.websocket.send((message as any).data.buffer) + } else { + // Handle text-based messages (backward compatibility and control messages) + console.log('Sending WebSocket message:', message.type) + // Debug: Log patch content if it's a patch message + if (message.type === 'patch' && (message as any).patches) { + console.log('๐Ÿ” Sending patches:', (message as any).patches.length, 'patches') + ;(message as any).patches.forEach((patch: any, index: number) => { + console.log(` Patch ${index}:`, { + action: patch.action, + path: patch.path, + value: patch.value ? (typeof patch.value === 'object' ? 'object' : patch.value) : 'undefined' + }) + }) + } + this.websocket.send(JSON.stringify(message)) + } } } @@ -262,11 +360,73 @@ class CloudflareNetworkAdapter extends NetworkAdapter { } disconnect(): void { - if (this.websocket) { - this.websocket.close() - this.websocket = null - } + this.cleanup() this.roomId = null this.emit('close') } + + private cleanup(): void { + this.stopKeepAlive() + this.clearReconnectTimeout() + + if (this.websocket) { + this.websocket.close(1000, 'Client disconnecting') + this.websocket = null + } + } + + private startKeepAlive(): void { + // Send ping every 30 seconds to prevent idle timeout + this.keepAliveInterval = setInterval(() => { + if (this.websocket && this.websocket.readyState === WebSocket.OPEN) { + console.log('๐Ÿ”Œ CloudflareAdapter: Sending keep-alive ping') + this.websocket.send(JSON.stringify({ + type: 'ping', + timestamp: Date.now() + })) + } + }, 30000) // 30 seconds + } + + private stopKeepAlive(): void { + if (this.keepAliveInterval) { + clearInterval(this.keepAliveInterval) + this.keepAliveInterval = null + } + } + + private sendPong(): void { + if (this.websocket && this.websocket.readyState === WebSocket.OPEN) { + this.websocket.send(JSON.stringify({ + type: 'pong', + timestamp: Date.now() + })) + } + } + + private scheduleReconnect(peerId: PeerId, peerMetadata?: PeerMetadata): void { + if (this.reconnectAttempts >= this.maxReconnectAttempts) { + console.error('โŒ CloudflareAdapter: Max reconnection attempts reached, giving up') + return + } + + this.reconnectAttempts++ + const delay = Math.min(this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1), 30000) // Max 30 seconds + + console.log(`๐Ÿ”„ CloudflareAdapter: Scheduling reconnect attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts} in ${delay}ms`) + + this.reconnectTimeout = setTimeout(() => { + if (this.roomId) { + console.log(`๐Ÿ”„ CloudflareAdapter: Attempting reconnect ${this.reconnectAttempts}/${this.maxReconnectAttempts}`) + this.connect(peerId, peerMetadata) + } + }, delay) + } + + private clearReconnectTimeout(): void { + if (this.reconnectTimeout) { + clearTimeout(this.reconnectTimeout) + this.reconnectTimeout = null + } + } } diff --git a/src/automerge/MinimalSanitization.ts b/src/automerge/MinimalSanitization.ts new file mode 100644 index 0000000..047620d --- /dev/null +++ b/src/automerge/MinimalSanitization.ts @@ -0,0 +1,62 @@ +// Minimal sanitization - only fix critical issues that break TLDraw +function minimalSanitizeRecord(record: any): any { + const sanitized = { ...record } + + // Only fix critical structural issues + if (!sanitized.id) { + throw new Error("Record missing required id field") + } + + if (!sanitized.typeName) { + throw new Error("Record missing required typeName field") + } + + // For shapes, only ensure basic required fields exist + if (sanitized.typeName === 'shape') { + // Ensure required shape fields exist with defaults + if (typeof sanitized.x !== 'number') sanitized.x = 0 + if (typeof sanitized.y !== 'number') sanitized.y = 0 + if (typeof sanitized.rotation !== 'number') sanitized.rotation = 0 + if (typeof sanitized.isLocked !== 'boolean') sanitized.isLocked = false + if (typeof sanitized.opacity !== 'number') sanitized.opacity = 1 + if (!sanitized.meta || typeof sanitized.meta !== 'object') sanitized.meta = {} + if (!sanitized.index) sanitized.index = 'a1' + if (!sanitized.parentId) sanitized.parentId = 'page:page' + + // Ensure props object exists + if (!sanitized.props || typeof sanitized.props !== 'object') { + sanitized.props = {} + } + + // Only fix type if completely missing + if (!sanitized.type || typeof sanitized.type !== 'string') { + // Simple type inference - check for obvious indicators + // CRITICAL: Don't infer text type just because richText exists - geo and note shapes can have richText + // Only infer text if there's no geo property and richText exists + if ((sanitized.props?.richText || sanitized.props?.text) && !sanitized.props?.geo) { + sanitized.type = 'text' + } else if (sanitized.props?.geo) { + sanitized.type = 'geo' + } else { + sanitized.type = 'geo' // Safe default + } + } + } + + return sanitized +} + + + + + + + + + + + + + + + diff --git a/src/automerge/README.md b/src/automerge/README.md index 301968a..bc5c9b7 100644 --- a/src/automerge/README.md +++ b/src/automerge/README.md @@ -6,8 +6,8 @@ This directory contains the Automerge-based sync implementation that replaces th - `AutomergeToTLStore.ts` - Converts Automerge patches to TLdraw store updates - `TLStoreToAutomerge.ts` - Converts TLdraw store changes to Automerge document updates -- `useAutomergeStore.ts` - React hook for managing Automerge document state -- `useAutomergeSync.ts` - Main sync hook that replaces `useSync` from TLdraw +- `useAutomergeStoreV2.ts` - Core React hook for managing Automerge document state with TLdraw +- `useAutomergeSync.ts` - Main sync hook that replaces `useSync` from TLdraw (uses V2 internally) - `CloudflareAdapter.ts` - Adapter for Cloudflare Durable Objects and R2 storage - `default_store.ts` - Default TLdraw store structure for new documents - `index.ts` - Main exports diff --git a/src/automerge/TLStoreToAutomerge.ts b/src/automerge/TLStoreToAutomerge.ts index ebc9075..1e369cf 100644 --- a/src/automerge/TLStoreToAutomerge.ts +++ b/src/automerge/TLStoreToAutomerge.ts @@ -1,191 +1,298 @@ import { RecordsDiff, TLRecord } from "@tldraw/tldraw" +// Helper function to clean NaN values from richText content +// This prevents SVG export errors when TLDraw tries to render text with invalid coordinates +function cleanRichTextNaN(richText: any): any { + if (!richText || typeof richText !== 'object') { + return richText + } + + // Deep clone to avoid mutating the original + const cleaned = JSON.parse(JSON.stringify(richText)) + + // Recursively clean content array + if (Array.isArray(cleaned.content)) { + cleaned.content = cleaned.content.map((item: any) => { + if (typeof item === 'object' && item !== null) { + // Remove any NaN values from the item + const cleanedItem: any = {} + for (const key in item) { + const value = item[key] + // Skip NaN values - they cause SVG export errors + if (typeof value === 'number' && isNaN(value)) { + // Skip NaN values + continue + } + // Recursively clean nested objects + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + cleanedItem[key] = cleanRichTextNaN(value) + } else if (Array.isArray(value)) { + cleanedItem[key] = value.map((v: any) => + typeof v === 'object' && v !== null ? cleanRichTextNaN(v) : v + ) + } else { + cleanedItem[key] = value + } + } + return cleanedItem + } + return item + }) + } + + return cleaned +} + function sanitizeRecord(record: TLRecord): TLRecord { const sanitized = { ...record } - // First, fix any problematic array fields that might cause validation errors - // This is a catch-all for any record type that has these fields - if ('insets' in sanitized && (sanitized.insets === undefined || !Array.isArray(sanitized.insets))) { - console.log(`Fixing insets field for ${sanitized.typeName} record:`, { - id: sanitized.id, - originalValue: sanitized.insets, - originalType: typeof sanitized.insets - }) - ;(sanitized as any).insets = [false, false, false, false] - } - if ('scribbles' in sanitized && (sanitized.scribbles === undefined || !Array.isArray(sanitized.scribbles))) { - console.log(`Fixing scribbles field for ${sanitized.typeName} record:`, { - id: sanitized.id, - originalValue: sanitized.scribbles, - originalType: typeof sanitized.scribbles - }) - ;(sanitized as any).scribbles = [] - } + // CRITICAL FIXES ONLY - preserve all other properties + // This function preserves ALL shape types (native and custom): + // - Geo shapes (rectangles, ellipses, etc.) - handled below + // - Arrow shapes - handled below + // - Custom shapes (ObsNote, Holon, etc.) - all props preserved via deep copy + // - All other native shapes (text, note, draw, line, group, image, video, etc.) - // Fix object fields that might be undefined - if ('duplicateProps' in sanitized && (sanitized.duplicateProps === undefined || typeof sanitized.duplicateProps !== 'object')) { - console.log(`Fixing duplicateProps field for ${sanitized.typeName} record:`, { - id: sanitized.id, - originalValue: sanitized.duplicateProps, - originalType: typeof sanitized.duplicateProps - }) - ;(sanitized as any).duplicateProps = { - shapeIds: [], - offset: { x: 0, y: 0 } - } - } - // Fix nested object properties - else if ('duplicateProps' in sanitized && sanitized.duplicateProps && typeof sanitized.duplicateProps === 'object') { - if (!('shapeIds' in sanitized.duplicateProps) || !Array.isArray(sanitized.duplicateProps.shapeIds)) { - console.log(`Fixing duplicateProps.shapeIds field for ${sanitized.typeName} record:`, { - id: sanitized.id, - originalValue: sanitized.duplicateProps.shapeIds, - originalType: typeof sanitized.duplicateProps.shapeIds - }) - ;(sanitized as any).duplicateProps.shapeIds = [] - } - // Fix missing offset field - if (!('offset' in sanitized.duplicateProps) || typeof sanitized.duplicateProps.offset !== 'object') { - console.log(`Fixing duplicateProps.offset field for ${sanitized.typeName} record:`, { - id: sanitized.id, - originalValue: sanitized.duplicateProps.offset, - originalType: typeof sanitized.duplicateProps.offset - }) - ;(sanitized as any).duplicateProps.offset = { x: 0, y: 0 } - } - } - - // Only add fields appropriate for the record type + // Ensure required top-level fields exist if (sanitized.typeName === 'shape') { - // Shape-specific fields - if (!sanitized.x) sanitized.x = 0 - if (!sanitized.y) sanitized.y = 0 - if (!sanitized.rotation) sanitized.rotation = 0 - if (!sanitized.isLocked) sanitized.isLocked = false - if (!sanitized.opacity) sanitized.opacity = 1 - if (!sanitized.meta) sanitized.meta = {} + if (typeof sanitized.x !== 'number') sanitized.x = 0 + if (typeof sanitized.y !== 'number') sanitized.y = 0 + if (typeof sanitized.rotation !== 'number') sanitized.rotation = 0 + if (typeof sanitized.isLocked !== 'boolean') sanitized.isLocked = false + if (typeof sanitized.opacity !== 'number') sanitized.opacity = 1 + // CRITICAL: Preserve all existing meta properties - only create empty object if meta doesn't exist + if (!sanitized.meta || typeof sanitized.meta !== 'object') { + sanitized.meta = {} + } else { + // Ensure meta is a mutable copy to preserve all properties (including text for rectangles) + sanitized.meta = { ...sanitized.meta } + } + if (!sanitized.props || typeof sanitized.props !== 'object') sanitized.props = {} - // Geo shape specific fields + // CRITICAL: Extract richText BEFORE deep copy to handle TLDraw RichText instances properly + // TLDraw RichText objects may have methods/getters that don't serialize well + let richTextValue: any = undefined + try { + // Safely check if richText exists using 'in' operator to avoid triggering getters + const props = sanitized.props || {} + if ('richText' in props) { + try { + // Use Object.getOwnPropertyDescriptor to safely check if it's a getter + const descriptor = Object.getOwnPropertyDescriptor(props, 'richText') + let rt: any = undefined + + if (descriptor && descriptor.get) { + // It's a getter - try to call it safely + try { + rt = descriptor.get.call(props) + } catch (getterError) { + console.warn(`๐Ÿ”ง TLStoreToAutomerge: Error calling richText getter for shape ${sanitized.id}:`, getterError) + rt = undefined + } + } else { + // It's a regular property - access it directly + rt = (props as any).richText + } + + // Now process the value + if (rt !== undefined && rt !== null) { + // Check if it's a function (shouldn't happen, but be safe) + if (typeof rt === 'function') { + console.warn(`๐Ÿ”ง TLStoreToAutomerge: richText is a function for shape ${sanitized.id}, skipping`) + richTextValue = { content: [], type: 'doc' } + } + // Check if it's an array + else if (Array.isArray(rt)) { + richTextValue = { content: JSON.parse(JSON.stringify(rt)), type: 'doc' } + } + // Check if it's an object + else if (typeof rt === 'object') { + // Extract plain object representation - use JSON to ensure it's serializable + try { + const serialized = JSON.parse(JSON.stringify(rt)) + richTextValue = { + type: serialized.type || 'doc', + content: serialized.content !== undefined ? serialized.content : [] + } + } catch (serializeError) { + // If serialization fails, try to extract manually + richTextValue = { + type: (rt as any).type || 'doc', + content: (rt as any).content !== undefined ? (rt as any).content : [] + } + } + } + // Invalid type + else { + console.warn(`๐Ÿ”ง TLStoreToAutomerge: Invalid richText type for shape ${sanitized.id}:`, typeof rt) + richTextValue = { content: [], type: 'doc' } + } + } + } catch (e) { + console.warn(`๐Ÿ”ง TLStoreToAutomerge: Error extracting richText for shape ${sanitized.id}:`, e) + richTextValue = { content: [], type: 'doc' } + } + } + } catch (e) { + console.warn(`๐Ÿ”ง TLStoreToAutomerge: Error checking richText for shape ${sanitized.id}:`, e) + } + + // CRITICAL: For all shapes, ensure props is a deep mutable copy to preserve all properties + // This is essential for custom shapes like ObsNote and for preserving richText in geo shapes + // Use JSON parse/stringify to create a deep copy of nested objects (like richText.content) + // Remove richText temporarily to avoid serialization issues + try { + const propsWithoutRichText: any = {} + // Copy all props except richText + for (const key in sanitized.props) { + if (key !== 'richText') { + propsWithoutRichText[key] = (sanitized.props as any)[key] + } + } + sanitized.props = JSON.parse(JSON.stringify(propsWithoutRichText)) + } catch (e) { + console.warn(`๐Ÿ”ง TLStoreToAutomerge: Error deep copying props for shape ${sanitized.id}:`, e) + // Fallback: just copy props without deep copy + sanitized.props = { ...sanitized.props } + if (richTextValue !== undefined) { + delete (sanitized.props as any).richText + } + } + + // CRITICAL: For geo shapes, move w/h/geo from top-level to props (required by TLDraw schema) if (sanitized.type === 'geo') { - if (!(sanitized as any).insets) { - (sanitized as any).insets = [0, 0, 0, 0] + + // Move w from top-level to props if needed + if ('w' in sanitized && sanitized.w !== undefined) { + if ((sanitized.props as any).w === undefined) { + (sanitized.props as any).w = (sanitized as any).w + } + delete (sanitized as any).w } - if (!(sanitized as any).geo) { - (sanitized as any).geo = 'rectangle' + + // Move h from top-level to props if needed + if ('h' in sanitized && sanitized.h !== undefined) { + if ((sanitized.props as any).h === undefined) { + (sanitized.props as any).h = (sanitized as any).h + } + delete (sanitized as any).h } - if (!(sanitized as any).w) { - (sanitized as any).w = 100 + + // Move geo from top-level to props if needed + if ('geo' in sanitized && sanitized.geo !== undefined) { + if ((sanitized.props as any).geo === undefined) { + (sanitized.props as any).geo = (sanitized as any).geo + } + delete (sanitized as any).geo } - if (!(sanitized as any).h) { - (sanitized as any).h = 100 + + // CRITICAL: Restore richText for geo shapes after deep copy + // Fix richText structure if it exists (preserve content, ensure proper format) + if (richTextValue !== undefined) { + // Clean NaN values to prevent SVG export errors + (sanitized.props as any).richText = cleanRichTextNaN(richTextValue) } + // CRITICAL: Preserve meta.text for geo shapes - it's used by runLLMprompt for backwards compatibility + // Ensure meta.text is preserved if it exists + if ((sanitized.meta as any)?.text !== undefined) { + // meta.text is already preserved since we copied meta above + // Just ensure it's not accidentally deleted + } + // Note: We don't delete richText if it's missing - it's optional for geo shapes + } + + // CRITICAL: For arrow shapes, preserve text property + if (sanitized.type === 'arrow') { + // CRITICAL: Preserve text property - only set default if truly missing (preserve empty strings and all other values) + if ((sanitized.props as any).text === undefined || (sanitized.props as any).text === null) { + (sanitized.props as any).text = '' + } + // Note: We preserve text even if it's an empty string - that's a valid value + } + + // CRITICAL: For note shapes, preserve richText property (required for note shapes) + if (sanitized.type === 'note') { + // CRITICAL: Use the extracted richText value if available, otherwise create default + if (richTextValue !== undefined) { + // Clean NaN values to prevent SVG export errors + (sanitized.props as any).richText = cleanRichTextNaN(richTextValue) + } else { + // Note shapes require richText - create default if missing + (sanitized.props as any).richText = { content: [], type: 'doc' } + } + } + + // CRITICAL: For ObsNote shapes, ensure all props are preserved (title, content, tags, etc.) + if (sanitized.type === 'ObsNote') { + // Props are already a mutable copy from above, so all properties are preserved + // No special handling needed - just ensure props exists (which we did above) + } + + // CRITICAL: For image/video shapes, fix crop structure if it exists + if (sanitized.type === 'image' || sanitized.type === 'video') { + const props = (sanitized.props as any) + + if (props.crop !== null && props.crop !== undefined) { + // Fix crop structure if it has wrong format + if (!props.crop.topLeft || !props.crop.bottomRight) { + if (props.crop.x !== undefined && props.crop.y !== undefined) { + // Convert old format { x, y, w, h } to new format + props.crop = { + topLeft: { x: props.crop.x || 0, y: props.crop.y || 0 }, + bottomRight: { + x: (props.crop.x || 0) + (props.crop.w || 1), + y: (props.crop.y || 0) + (props.crop.h || 1) + } + } + } else { + // Invalid structure: set to default + props.crop = { + topLeft: { x: 0, y: 0 }, + bottomRight: { x: 1, y: 1 } + } + } + } + } + } + + // CRITICAL: For group shapes, remove w/h from props (they cause validation errors) + if (sanitized.type === 'group') { + if ('w' in sanitized.props) delete (sanitized.props as any).w + if ('h' in sanitized.props) delete (sanitized.props as any).h } } else if (sanitized.typeName === 'document') { - // Document-specific fields only - if (!sanitized.meta) sanitized.meta = {} - } else if (sanitized.typeName === 'instance') { - // Instance-specific fields only - if (!sanitized.meta) sanitized.meta = {} - - // Fix properties that need to be objects instead of null/undefined - if ('scribble' in sanitized) { - console.log(`Removing invalid scribble property from instance record:`, { - id: sanitized.id, - originalValue: sanitized.scribble - }) - delete (sanitized as any).scribble + // CRITICAL: Preserve all existing meta properties + if (!sanitized.meta || typeof sanitized.meta !== 'object') { + sanitized.meta = {} + } else { + sanitized.meta = { ...sanitized.meta } } + } else if (sanitized.typeName === 'instance') { + // CRITICAL: Preserve all existing meta properties + if (!sanitized.meta || typeof sanitized.meta !== 'object') { + sanitized.meta = {} + } else { + sanitized.meta = { ...sanitized.meta } + } + // Only fix critical instance fields that cause validation errors if ('brush' in sanitized && (sanitized.brush === null || sanitized.brush === undefined)) { - console.log(`Fixing brush property to be an object for instance record:`, { - id: sanitized.id, - originalValue: sanitized.brush - }) - ;(sanitized as any).brush = { x: 0, y: 0, w: 0, h: 0 } + (sanitized as any).brush = { x: 0, y: 0, w: 0, h: 0 } } if ('zoomBrush' in sanitized && (sanitized.zoomBrush === null || sanitized.zoomBrush === undefined)) { - console.log(`Fixing zoomBrush property to be an object for instance record:`, { - id: sanitized.id, - originalValue: sanitized.zoomBrush - }) - ;(sanitized as any).zoomBrush = {} + (sanitized as any).zoomBrush = { x: 0, y: 0, w: 0, h: 0 } } if ('insets' in sanitized && (sanitized.insets === undefined || !Array.isArray(sanitized.insets))) { - console.log(`Fixing insets property to be an array for instance record:`, { - id: sanitized.id, - originalValue: sanitized.insets - }) - ;(sanitized as any).insets = [false, false, false, false] + (sanitized as any).insets = [false, false, false, false] } - if ('canMoveCamera' in sanitized) { - console.log(`Removing invalid canMoveCamera property from instance record:`, { - id: sanitized.id, - originalValue: sanitized.canMoveCamera - }) - delete (sanitized as any).canMoveCamera + if ('scribbles' in sanitized && (sanitized.scribbles === undefined || !Array.isArray(sanitized.scribbles))) { + (sanitized as any).scribbles = [] } - - // Fix isCoarsePointer property to be a boolean - if ('isCoarsePointer' in sanitized && typeof sanitized.isCoarsePointer !== 'boolean') { - console.log(`Fixing isCoarsePointer property to be a boolean for instance record:`, { - id: sanitized.id, - originalValue: sanitized.isCoarsePointer - }) - ;(sanitized as any).isCoarsePointer = false - } - - // Fix isHoveringCanvas property to be a boolean - if ('isHoveringCanvas' in sanitized && typeof sanitized.isHoveringCanvas !== 'boolean') { - console.log(`Fixing isHoveringCanvas property to be a boolean for instance record:`, { - id: sanitized.id, - originalValue: sanitized.isHoveringCanvas - }) - ;(sanitized as any).isHoveringCanvas = false - } - - - // Add required fields that might be missing - const requiredFields = { - followingUserId: null, - opacityForNextShape: 1, - stylesForNextShape: {}, - brush: { x: 0, y: 0, w: 0, h: 0 }, - zoomBrush: { x: 0, y: 0, w: 0, h: 0 }, - scribbles: [], - cursor: { type: "default", rotation: 0 }, - isFocusMode: false, - exportBackground: true, - isDebugMode: false, - isToolLocked: false, - screenBounds: { x: 0, y: 0, w: 720, h: 400 }, - isGridMode: false, - isPenMode: false, - chatMessage: "", - isChatting: false, - highlightedUserIds: [], - isFocused: true, - devicePixelRatio: 2, - insets: [false, false, false, false], - isCoarsePointer: false, - isHoveringCanvas: false, - openMenus: [], - isChangingStyle: false, - isReadonly: false, - duplicateProps: { // Object field that was missing + if ('duplicateProps' in sanitized && (sanitized.duplicateProps === undefined || typeof sanitized.duplicateProps !== 'object')) { + (sanitized as any).duplicateProps = { shapeIds: [], offset: { x: 0, y: 0 } } } - - // Add missing required fields - Object.entries(requiredFields).forEach(([key, defaultValue]) => { - if (!(key in sanitized)) { - console.log(`Adding missing ${key} field to instance record:`, { - id: sanitized.id, - defaultValue - }) - ;(sanitized as any)[key] = defaultValue - } - }) } return sanitized @@ -206,15 +313,159 @@ export function applyTLStoreChangesToAutomerge( Object.values(changes.added).forEach((record) => { // Sanitize record before saving to ensure all required fields are present const sanitizedRecord = sanitizeRecord(record) - doc.store[record.id] = sanitizedRecord + // CRITICAL: Create a deep copy to ensure all properties (including richText and text) are preserved + // This prevents Automerge from treating the object as read-only + const recordToSave = JSON.parse(JSON.stringify(sanitizedRecord)) + // Let Automerge handle the assignment - it will merge automatically + doc.store[record.id] = recordToSave }) } // Handle updated records + // Simplified: Replace entire record and let Automerge handle merging + // This is simpler than deep comparison and leverages Automerge's conflict resolution if (changes.updated) { Object.values(changes.updated).forEach(([_, record]) => { + // DEBUG: Log richText, meta.text, and Obsidian note properties before sanitization + if (record.typeName === 'shape') { + if (record.type === 'geo' && (record.props as any)?.richText) { + console.log(`๐Ÿ” TLStoreToAutomerge: Geo shape ${record.id} has richText before sanitization:`, { + hasRichText: !!(record.props as any).richText, + richTextType: typeof (record.props as any).richText, + richTextContent: Array.isArray((record.props as any).richText) ? 'array' : (record.props as any).richText?.content ? 'object with content' : 'object without content' + }) + } + if (record.type === 'geo' && (record.meta as any)?.text !== undefined) { + console.log(`๐Ÿ” TLStoreToAutomerge: Geo shape ${record.id} has meta.text before sanitization:`, { + hasMetaText: !!(record.meta as any).text, + metaTextValue: (record.meta as any).text, + metaTextType: typeof (record.meta as any).text + }) + } + if (record.type === 'note' && (record.props as any)?.richText) { + console.log(`๐Ÿ” TLStoreToAutomerge: Note shape ${record.id} has richText before sanitization:`, { + hasRichText: !!(record.props as any).richText, + richTextType: typeof (record.props as any).richText, + richTextContent: Array.isArray((record.props as any).richText) ? 'array' : (record.props as any).richText?.content ? 'object with content' : 'object without content', + richTextContentLength: Array.isArray((record.props as any).richText?.content) ? (record.props as any).richText.content.length : 'not array' + }) + } + if (record.type === 'arrow' && (record.props as any)?.text !== undefined) { + console.log(`๐Ÿ” TLStoreToAutomerge: Arrow shape ${record.id} has text before sanitization:`, { + hasText: !!(record.props as any).text, + textValue: (record.props as any).text, + textType: typeof (record.props as any).text + }) + } + if (record.type === 'ObsNote') { + console.log(`๐Ÿ” TLStoreToAutomerge: ObsNote shape ${record.id} before sanitization:`, { + hasTitle: !!(record.props as any).title, + hasContent: !!(record.props as any).content, + hasTags: Array.isArray((record.props as any).tags), + title: (record.props as any).title, + contentLength: (record.props as any).content?.length || 0, + tagsCount: Array.isArray((record.props as any).tags) ? (record.props as any).tags.length : 0 + }) + } + } + const sanitizedRecord = sanitizeRecord(record) - deepCompareAndUpdate(doc.store[record.id], sanitizedRecord) + + // DEBUG: Log richText, meta.text, and Obsidian note properties after sanitization + if (sanitizedRecord.typeName === 'shape') { + if (sanitizedRecord.type === 'geo' && (sanitizedRecord.props as any)?.richText) { + console.log(`๐Ÿ” TLStoreToAutomerge: Geo shape ${sanitizedRecord.id} has richText after sanitization:`, { + hasRichText: !!(sanitizedRecord.props as any).richText, + richTextType: typeof (sanitizedRecord.props as any).richText, + richTextContent: Array.isArray((sanitizedRecord.props as any).richText) ? 'array' : (sanitizedRecord.props as any).richText?.content ? 'object with content' : 'object without content' + }) + } + if (sanitizedRecord.type === 'geo' && (sanitizedRecord.meta as any)?.text !== undefined) { + console.log(`๐Ÿ” TLStoreToAutomerge: Geo shape ${sanitizedRecord.id} has meta.text after sanitization:`, { + hasMetaText: !!(sanitizedRecord.meta as any).text, + metaTextValue: (sanitizedRecord.meta as any).text, + metaTextType: typeof (sanitizedRecord.meta as any).text + }) + } + if (sanitizedRecord.type === 'note' && (sanitizedRecord.props as any)?.richText) { + console.log(`๐Ÿ” TLStoreToAutomerge: Note shape ${sanitizedRecord.id} has richText after sanitization:`, { + hasRichText: !!(sanitizedRecord.props as any).richText, + richTextType: typeof (sanitizedRecord.props as any).richText, + richTextContent: Array.isArray((sanitizedRecord.props as any).richText) ? 'array' : (sanitizedRecord.props as any).richText?.content ? 'object with content' : 'object without content', + richTextContentLength: Array.isArray((sanitizedRecord.props as any).richText?.content) ? (sanitizedRecord.props as any).richText.content.length : 'not array' + }) + } + if (sanitizedRecord.type === 'arrow' && (sanitizedRecord.props as any)?.text !== undefined) { + console.log(`๐Ÿ” TLStoreToAutomerge: Arrow shape ${sanitizedRecord.id} has text after sanitization:`, { + hasText: !!(sanitizedRecord.props as any).text, + textValue: (sanitizedRecord.props as any).text, + textType: typeof (sanitizedRecord.props as any).text + }) + } + if (sanitizedRecord.type === 'ObsNote') { + console.log(`๐Ÿ” TLStoreToAutomerge: ObsNote shape ${sanitizedRecord.id} after sanitization:`, { + hasTitle: !!(sanitizedRecord.props as any).title, + hasContent: !!(sanitizedRecord.props as any).content, + hasTags: Array.isArray((sanitizedRecord.props as any).tags), + title: (sanitizedRecord.props as any).title, + contentLength: (sanitizedRecord.props as any).content?.length || 0, + tagsCount: Array.isArray((sanitizedRecord.props as any).tags) ? (sanitizedRecord.props as any).tags.length : 0 + }) + } + } + + // CRITICAL: Create a deep copy to ensure all properties (including richText and text) are preserved + // This prevents Automerge from treating the object as read-only + // Note: sanitizedRecord.props is already a deep copy from sanitizeRecord, but we need to deep copy the entire record + const recordToSave = JSON.parse(JSON.stringify(sanitizedRecord)) + + // DEBUG: Log richText, meta.text, and Obsidian note properties after deep copy + if (recordToSave.typeName === 'shape') { + if (recordToSave.type === 'geo' && recordToSave.props?.richText) { + console.log(`๐Ÿ” TLStoreToAutomerge: Geo shape ${recordToSave.id} has richText after deep copy:`, { + hasRichText: !!recordToSave.props.richText, + richTextType: typeof recordToSave.props.richText, + richTextContent: Array.isArray(recordToSave.props.richText) ? 'array' : recordToSave.props.richText?.content ? 'object with content' : 'object without content', + richTextContentLength: Array.isArray(recordToSave.props.richText?.content) ? recordToSave.props.richText.content.length : 'not array' + }) + } + if (recordToSave.type === 'geo' && recordToSave.meta?.text !== undefined) { + console.log(`๐Ÿ” TLStoreToAutomerge: Geo shape ${recordToSave.id} has meta.text after deep copy:`, { + hasMetaText: !!recordToSave.meta.text, + metaTextValue: recordToSave.meta.text, + metaTextType: typeof recordToSave.meta.text + }) + } + if (recordToSave.type === 'note' && recordToSave.props?.richText) { + console.log(`๐Ÿ” TLStoreToAutomerge: Note shape ${recordToSave.id} has richText after deep copy:`, { + hasRichText: !!recordToSave.props.richText, + richTextType: typeof recordToSave.props.richText, + richTextContent: Array.isArray(recordToSave.props.richText) ? 'array' : recordToSave.props.richText?.content ? 'object with content' : 'object without content', + richTextContentLength: Array.isArray(recordToSave.props.richText?.content) ? recordToSave.props.richText.content.length : 'not array' + }) + } + if (recordToSave.type === 'arrow' && recordToSave.props?.text !== undefined) { + console.log(`๐Ÿ” TLStoreToAutomerge: Arrow shape ${recordToSave.id} has text after deep copy:`, { + hasText: !!recordToSave.props.text, + textValue: recordToSave.props.text, + textType: typeof recordToSave.props.text + }) + } + if (recordToSave.type === 'ObsNote') { + console.log(`๐Ÿ” TLStoreToAutomerge: ObsNote shape ${recordToSave.id} after deep copy:`, { + hasTitle: !!recordToSave.props.title, + hasContent: !!recordToSave.props.content, + hasTags: Array.isArray(recordToSave.props.tags), + title: recordToSave.props.title, + contentLength: recordToSave.props.content?.length || 0, + tagsCount: Array.isArray(recordToSave.props.tags) ? recordToSave.props.tags.length : 0, + allPropsKeys: Object.keys(recordToSave.props || {}) + }) + } + } + + // Replace the entire record - Automerge will handle merging with concurrent changes + doc.store[record.id] = recordToSave }) } @@ -227,55 +478,5 @@ export function applyTLStoreChangesToAutomerge( } -function deepCompareAndUpdate(objectA: any, objectB: any) { - if (Array.isArray(objectB)) { - if (!Array.isArray(objectA)) { - // if objectA is not an array, replace it with objectB - objectA = objectB.slice() - } else { - // compare and update array elements - for (let i = 0; i < objectB.length; i++) { - if (i >= objectA.length) { - objectA.push(objectB[i]) - } else { - if (isObject(objectB[i]) || Array.isArray(objectB[i])) { - // if element is an object or array, recursively compare and update - deepCompareAndUpdate(objectA[i], objectB[i]) - } else if (objectA[i] !== objectB[i]) { - // update the element - objectA[i] = objectB[i] - } - } - } - // remove extra elements - if (objectA.length > objectB.length) { - objectA.splice(objectB.length) - } - } - } else if (isObject(objectB)) { - for (const [key, value] of Object.entries(objectB)) { - if (objectA[key] === undefined) { - // if key is not in objectA, add it - objectA[key] = value - } else { - if (isObject(value) || Array.isArray(value)) { - // if value is an object or array, recursively compare and update - deepCompareAndUpdate(objectA[key], value) - } else if (objectA[key] !== value) { - // update the value - objectA[key] = value - } - } - } - for (const key of Object.keys(objectA)) { - if ((objectB as any)[key] === undefined) { - // if key is not in objectB, remove it - delete objectA[key] - } - } - } -} - -function isObject(value: any): value is Record { - return value !== null && typeof value === 'object' && !Array.isArray(value) -} +// Removed deepCompareAndUpdate - we now replace entire records and let Automerge handle merging +// This simplifies the code and leverages Automerge's built-in conflict resolution diff --git a/src/automerge/default_store.ts b/src/automerge/default_store.ts index fc22fab..d87499d 100644 --- a/src/automerge/default_store.ts +++ b/src/automerge/default_store.ts @@ -115,7 +115,8 @@ export const DEFAULT_STORE = { "com.tldraw.shape.container": 0, "com.tldraw.shape.element": 0, "com.tldraw.binding.arrow": 0, - "com.tldraw.binding.layout": 0 + "com.tldraw.binding.layout": 0, + "obsidian_vault": 1 } }, } diff --git a/src/automerge/index.ts b/src/automerge/index.ts index d9abf3c..705f509 100644 --- a/src/automerge/index.ts +++ b/src/automerge/index.ts @@ -6,9 +6,6 @@ export function init(doc: TLStoreSnapshot) { Object.assign(doc, DEFAULT_STORE) } -// Export the new V2 approach as the default +// Export the V2 implementation export * from "./useAutomergeStoreV2" export * from "./useAutomergeSync" - -// Keep the old store for backward compatibility (deprecated) -// export * from "./useAutomergeStore" diff --git a/src/automerge/useAutomergeStore.ts b/src/automerge/useAutomergeStore.ts deleted file mode 100644 index 0744f55..0000000 --- a/src/automerge/useAutomergeStore.ts +++ /dev/null @@ -1,622 +0,0 @@ -import { - TLAnyShapeUtilConstructor, - TLRecord, - TLStoreWithStatus, - createTLStore, - defaultShapeUtils, - HistoryEntry, - getUserPreferences, - setUserPreferences, - defaultUserPreferences, - createPresenceStateDerivation, - InstancePresenceRecordType, - computed, - react, - TLStoreSnapshot, - sortById, - loadSnapshot, -} from "@tldraw/tldraw" -import { createTLSchema, defaultBindingSchemas, defaultShapeSchemas } from "@tldraw/tlschema" -import { useEffect, useState } from "react" -import { DocHandle, DocHandleChangePayload } from "@automerge/automerge-repo" -import { - useLocalAwareness, - useRemoteAwareness, -} from "@automerge/automerge-repo-react-hooks" - -import { applyAutomergePatchesToTLStore } from "./AutomergeToTLStore.js" -import { applyTLStoreChangesToAutomerge } from "./TLStoreToAutomerge.js" - -// Import custom shape utilities -import { ChatBoxShape } from "@/shapes/ChatBoxShapeUtil" -import { VideoChatShape } from "@/shapes/VideoChatShapeUtil" -import { EmbedShape } from "@/shapes/EmbedShapeUtil" -import { MarkdownShape } from "@/shapes/MarkdownShapeUtil" -import { MycrozineTemplateShape } from "@/shapes/MycrozineTemplateShapeUtil" -import { SlideShape } from "@/shapes/SlideShapeUtil" -import { PromptShape } from "@/shapes/PromptShapeUtil" -import { SharedPianoShape } from "@/shapes/SharedPianoShapeUtil" - -export function useAutomergeStore({ - handle, -}: { - handle: DocHandle - userId: string -}): TLStoreWithStatus { - // Deprecation warning - console.warn( - "โš ๏ธ useAutomergeStore is deprecated and has known migration issues. " + - "Please use useAutomergeStoreV2 or useAutomergeSync instead for better reliability." - ) - // Create a custom schema that includes all the custom shapes - const customSchema = createTLSchema({ - shapes: { - ...defaultShapeSchemas, - ChatBox: { - props: ChatBoxShape.props, - }, - VideoChat: { - props: VideoChatShape.props, - }, - Embed: { - props: EmbedShape.props, - }, - Markdown: { - props: MarkdownShape.props, - }, - MycrozineTemplate: { - props: MycrozineTemplateShape.props, - }, - Slide: { - props: SlideShape.props, - }, - Prompt: { - props: PromptShape.props, - }, - SharedPiano: { - props: SharedPianoShape.props, - }, - }, - bindings: defaultBindingSchemas, - }) - - const [store] = useState(() => { - const store = createTLStore({ - schema: customSchema, - }) - return store - }) - - const [storeWithStatus, setStoreWithStatus] = useState({ - status: "loading", - }) - - /* -------------------- TLDraw <--> Automerge -------------------- */ - useEffect(() => { - // Early return if handle is not available - if (!handle) { - setStoreWithStatus({ status: "loading" }) - return - } - - const unsubs: (() => void)[] = [] - - // A hacky workaround to prevent local changes from being applied twice - // once into the automerge doc and then back again. - let preventPatchApplications = false - - /* TLDraw to Automerge */ - function syncStoreChangesToAutomergeDoc({ - changes, - }: HistoryEntry) { - preventPatchApplications = true - handle.change((doc) => { - applyTLStoreChangesToAutomerge(doc, changes) - }) - preventPatchApplications = false - } - - unsubs.push( - store.listen(syncStoreChangesToAutomergeDoc, { - source: "user", - scope: "document", - }) - ) - - /* Automerge to TLDraw */ - const syncAutomergeDocChangesToStore = ({ - patches, - }: DocHandleChangePayload) => { - if (preventPatchApplications) return - - applyAutomergePatchesToTLStore(patches, store) - } - - handle.on("change", syncAutomergeDocChangesToStore) - unsubs.push(() => handle.off("change", syncAutomergeDocChangesToStore)) - - /* Defer rendering until the document is ready */ - // TODO: need to think through the various status possibilities here and how they map - handle.whenReady().then(() => { - try { - const doc = handle.doc() - if (!doc) throw new Error("Document not found") - if (!doc.store) throw new Error("Document store not initialized") - - // Clean the store data to remove any problematic text properties that might cause migration issues - const cleanedStore = JSON.parse(JSON.stringify(doc.store)) - - // Clean up any problematic text properties that might cause migration issues - const shapesToRemove: string[] = [] - - Object.keys(cleanedStore).forEach(key => { - const record = cleanedStore[key] - if (record && record.typeName === 'shape') { - let shouldRemove = false - - // Migrate old Transcribe shapes to geo shapes - if (record.type === 'Transcribe') { - console.log(`Migrating old Transcribe shape ${key} to geo shape`) - record.type = 'geo' - - // Ensure required geo props exist - if (!record.props.geo) record.props.geo = 'rectangle' - if (!record.props.fill) record.props.fill = 'solid' - if (!record.props.color) record.props.color = 'white' - if (!record.props.dash) record.props.dash = 'draw' - if (!record.props.size) record.props.size = 'm' - if (!record.props.font) record.props.font = 'draw' - if (!record.props.align) record.props.align = 'start' - if (!record.props.verticalAlign) record.props.verticalAlign = 'start' - if (!record.props.growY) record.props.growY = 0 - if (!record.props.url) record.props.url = '' - if (!record.props.scale) record.props.scale = 1 - if (!record.props.labelColor) record.props.labelColor = 'black' - if (!record.props.richText) record.props.richText = [] as any - - // Move transcript text from props to meta - if (record.props.transcript) { - if (!record.meta) record.meta = {} - record.meta.text = record.props.transcript - delete record.props.transcript - } - - // Clean up other old Transcribe-specific props - const oldProps = ['isRecording', 'transcriptSegments', 'speakers', 'currentSpeakerId', - 'interimText', 'isCompleted', 'aiSummary', 'language', 'autoScroll', - 'showTimestamps', 'showSpeakerLabels', 'manualClear'] - oldProps.forEach(prop => { - if (record.props[prop] !== undefined) { - delete record.props[prop] - } - }) - } - - // Handle text shapes - if (record.type === 'text' && record.props) { - // Ensure text property is a string - if (typeof record.props.text !== 'string') { - console.warn('Fixing invalid text property for text shape:', key) - record.props.text = record.props.text || '' - } - } - - // Handle other shapes that might have text properties - if (record.props && record.props.text !== undefined) { - if (typeof record.props.text !== 'string') { - console.warn('Fixing invalid text property for shape:', key, 'type:', record.type) - record.props.text = record.props.text || '' - } - } - - // Handle rich text content that might be undefined or invalid - if (record.props && record.props.richText !== undefined) { - if (!Array.isArray(record.props.richText)) { - console.warn('Fixing invalid richText property for shape:', key, 'type:', record.type) - record.props.richText = [] as any - } else { - // Clean up any invalid rich text entries - record.props.richText = record.props.richText.filter((item: any) => - item && typeof item === 'object' && item.type - ) - } - } - - // Remove any other potentially problematic properties that might cause migration issues - if (record.props) { - // Remove any properties that are null or undefined - Object.keys(record.props).forEach(propKey => { - if (record.props[propKey] === null || record.props[propKey] === undefined) { - console.warn(`Removing null/undefined property ${propKey} from shape:`, key, 'type:', record.type) - delete record.props[propKey] - } - }) - } - - // If the shape still looks problematic, mark it for removal - if (record.props && Object.keys(record.props).length === 0) { - console.warn('Removing shape with empty props:', key, 'type:', record.type) - shouldRemove = true - } - - // For geo shapes, ensure basic properties exist - if (record.type === 'geo' && record.props) { - if (!record.props.geo) record.props.geo = 'rectangle' - if (!record.props.fill) record.props.fill = 'solid' - if (!record.props.color) record.props.color = 'white' - } - - if (shouldRemove) { - shapesToRemove.push(key) - } - } - }) - - // Remove problematic shapes - shapesToRemove.forEach(key => { - console.warn('Removing problematic shape:', key) - delete cleanedStore[key] - }) - - // Log the final state of the cleaned store - const remainingShapes = Object.values(cleanedStore).filter((record: any) => - record && record.typeName === 'shape' - ) - console.log(`Cleaned store: ${remainingShapes.length} shapes remaining`) - - // Additional aggressive cleaning to prevent migration errors - // Set ALL richText properties to proper structure instead of deleting them - Object.keys(cleanedStore).forEach(key => { - const record = cleanedStore[key] - if (record && record.typeName === 'shape' && record.props && record.props.richText !== undefined) { - console.warn('Setting richText to proper structure to prevent migration error:', key, 'type:', record.type) - record.props.richText = [] as any - } - }) - - // Remove ALL text properties that might be causing issues - Object.keys(cleanedStore).forEach(key => { - const record = cleanedStore[key] - if (record && record.typeName === 'shape' && record.props && record.props.text !== undefined) { - // Only keep text for actual text shapes - if (record.type !== 'text') { - console.warn('Removing text property from non-text shape to prevent migration error:', key, 'type:', record.type) - delete record.props.text - } - } - }) - - // Final cleanup: remove any shapes that still have problematic properties - const finalShapesToRemove: string[] = [] - Object.keys(cleanedStore).forEach(key => { - const record = cleanedStore[key] - if (record && record.typeName === 'shape') { - // Remove any shape that has problematic text properties (but keep richText as proper structure) - if (record.props && (record.props.text !== undefined && record.type !== 'text')) { - console.warn('Removing shape with remaining problematic text properties:', key, 'type:', record.type) - finalShapesToRemove.push(key) - } - } - }) - - // Remove the final problematic shapes - finalShapesToRemove.forEach(key => { - console.warn('Final removal of problematic shape:', key) - delete cleanedStore[key] - }) - - // Log the final cleaned state - const finalShapes = Object.values(cleanedStore).filter((record: any) => - record && record.typeName === 'shape' - ) - console.log(`Final cleaned store: ${finalShapes.length} shapes remaining`) - - // Try to load the snapshot with a more defensive approach - let loadSuccess = false - - // Skip loadSnapshot entirely to avoid migration issues - console.log('Skipping loadSnapshot to avoid migration errors - starting with clean store') - - // Manually add the cleaned shapes back to the store without going through migration - try { - store.mergeRemoteChanges(() => { - // Add only the essential store records first - const essentialRecords: any[] = [] - Object.values(cleanedStore).forEach((record: any) => { - if (record && record.typeName === 'store' && record.id) { - essentialRecords.push(record) - } - }) - - if (essentialRecords.length > 0) { - store.put(essentialRecords) - console.log(`Added ${essentialRecords.length} essential records to store`) - } - - // Add the cleaned shapes - const safeShapes: any[] = [] - Object.values(cleanedStore).forEach((record: any) => { - if (record && record.typeName === 'shape' && record.type && record.id) { - // Only add shapes that are safe (no text properties, but richText can be proper structure) - if (record.props && - !record.props.text && - record.type !== 'text') { - safeShapes.push(record) - } - } - }) - - if (safeShapes.length > 0) { - store.put(safeShapes) - console.log(`Added ${safeShapes.length} safe shapes to store`) - } - }) - loadSuccess = true - } catch (manualError) { - console.error('Manual shape addition failed:', manualError) - loadSuccess = true // Still consider it successful, just with empty store - } - - // If we still haven't succeeded, try to completely bypass the migration by creating a new store - if (!loadSuccess) { - console.log('Attempting to create a completely new store to bypass migration...') - try { - // Create a new store with the same schema - const newStore = createTLStore({ - schema: customSchema, - }) - - // Replace the current store with the new one - Object.assign(store, newStore) - - // Try to manually add safe shapes to the new store - store.mergeRemoteChanges(() => { - const safeShapes: any[] = [] - Object.values(cleanedStore).forEach((record: any) => { - if (record && record.typeName === 'shape' && record.type && record.id) { - // Only add shapes that don't have problematic properties - if (record.props && - (!record.props.text || typeof record.props.text === 'string') && - (!record.props.richText || Array.isArray(record.props.richText))) { - safeShapes.push(record) - } - } - }) - - console.log(`Found ${safeShapes.length} safe shapes to add to new store`) - if (safeShapes.length > 0) { - store.put(safeShapes) - console.log(`Added ${safeShapes.length} safe shapes to new store`) - } - }) - - loadSuccess = true - } catch (newStoreError) { - console.error('New store creation also failed:', newStoreError) - console.log('Continuing with completely empty store') - } - } - - // If we still haven't succeeded, try to completely bypass the migration by using a different approach - if (!loadSuccess) { - console.log('Attempting to completely bypass migration...') - try { - // Create a completely new store and manually add only the essential data - const newStore = createTLStore({ - schema: customSchema, - }) - - // Replace the current store with the new one - Object.assign(store, newStore) - - // Manually add only the essential data without going through migration - store.mergeRemoteChanges(() => { - // Add only the essential store records - const essentialRecords: any[] = [] - Object.values(cleanedStore).forEach((record: any) => { - if (record && record.typeName === 'store' && record.id) { - essentialRecords.push(record) - } - }) - - console.log(`Found ${essentialRecords.length} essential records to add`) - if (essentialRecords.length > 0) { - store.put(essentialRecords) - console.log(`Added ${essentialRecords.length} essential records to new store`) - } - }) - - loadSuccess = true - } catch (bypassError) { - console.error('Migration bypass also failed:', bypassError) - console.log('Continuing with completely empty store') - } - } - - // If we still haven't succeeded, try the most aggressive approach: completely bypass loadSnapshot - if (!loadSuccess) { - console.log('Attempting most aggressive bypass - skipping loadSnapshot entirely...') - try { - // Create a completely new store - const newStore = createTLStore({ - schema: customSchema, - }) - - // Replace the current store with the new one - Object.assign(store, newStore) - - // Don't try to load any snapshot data - just start with a clean store - console.log('Starting with completely clean store to avoid migration issues') - loadSuccess = true - } catch (aggressiveError) { - console.error('Most aggressive bypass also failed:', aggressiveError) - console.log('Continuing with completely empty store') - } - } - - - setStoreWithStatus({ - store, - status: "synced-remote", - connectionStatus: "online", - }) - } catch (error) { - console.error('Error in handle.whenReady():', error) - setStoreWithStatus({ - status: "error", - error: error instanceof Error ? error : new Error('Unknown error'), - }) - } - }).catch((error) => { - console.error('Promise rejection in handle.whenReady():', error) - setStoreWithStatus({ - status: "error", - error: error instanceof Error ? error : new Error('Unknown error'), - }) - }) - - // Add a global error handler for unhandled promise rejections - const originalConsoleError = console.error - console.error = (...args) => { - if (args[0] && typeof args[0] === 'string' && args[0].includes('Cannot read properties of undefined (reading \'split\')')) { - console.warn('Caught migration error, attempting recovery...') - // Try to recover by setting a clean store status - setStoreWithStatus({ - store, - status: "synced-remote", - connectionStatus: "online", - }) - return - } - originalConsoleError.apply(console, args) - } - - // Add a global error handler for unhandled errors - const originalErrorHandler = window.onerror - window.onerror = (message, source, lineno, colno, error) => { - if (message && typeof message === 'string' && message.includes('Cannot read properties of undefined (reading \'split\')')) { - console.warn('Caught global migration error, attempting recovery...') - setStoreWithStatus({ - store, - status: "synced-remote", - connectionStatus: "online", - }) - return true // Prevent default error handling - } - if (originalErrorHandler) { - return originalErrorHandler(message, source, lineno, colno, error) - } - return false - } - - // Add a global handler for unhandled promise rejections - const originalUnhandledRejection = window.onunhandledrejection - window.onunhandledrejection = (event) => { - if (event.reason && event.reason.message && event.reason.message.includes('Cannot read properties of undefined (reading \'split\')')) { - console.warn('Caught unhandled promise rejection migration error, attempting recovery...') - event.preventDefault() // Prevent the error from being logged - setStoreWithStatus({ - store, - status: "synced-remote", - connectionStatus: "online", - }) - return - } - if (originalUnhandledRejection) { - return (originalUnhandledRejection as any)(event) - } - } - - return () => { - unsubs.forEach((fn) => fn()) - unsubs.length = 0 - } - }, [handle, store]) - - return storeWithStatus -} - -export function useAutomergePresence({ handle, store, userMetadata }: - { handle: DocHandle | null, store: TLStoreWithStatus, userMetadata: any }) { - - const innerStore = store?.store - - const { userId, name, color } = userMetadata - - // Only use awareness hooks if we have a valid handle and the store is ready - const shouldUseAwareness = handle && store?.status === "synced-remote" - - // Create a safe handle that won't cause null errors - const safeHandle = shouldUseAwareness ? handle : { - on: () => {}, - off: () => {}, - removeListener: () => {}, // Add the missing removeListener method - whenReady: () => Promise.resolve(), - doc: () => null, - change: () => {}, - broadcast: () => {}, // Add the missing broadcast method - } as any - - const [, updateLocalState] = useLocalAwareness({ - handle: safeHandle, - userId, - initialState: {}, - }) - - const [peerStates] = useRemoteAwareness({ - handle: safeHandle, - localUserId: userId, - }) - - /* ----------- Presence stuff ----------- */ - useEffect(() => { - if (!innerStore || !shouldUseAwareness) return - - const toPut: TLRecord[] = - Object.values(peerStates) - .filter((record) => record && Object.keys(record).length !== 0) - - // put / remove the records in the store - const toRemove = innerStore.query.records('instance_presence').get().sort(sortById) - .map((record) => record.id) - .filter((id) => !toPut.find((record) => record.id === id)) - - if (toRemove.length) innerStore.remove(toRemove) - if (toPut.length) innerStore.put(toPut) - }, [innerStore, peerStates, shouldUseAwareness]) - - useEffect(() => { - if (!innerStore || !shouldUseAwareness) return - /* ----------- Presence stuff ----------- */ - setUserPreferences({ id: userId, color, name }) - - const userPreferences = computed<{ - id: string - color: string - name: string - }>("userPreferences", () => { - const user = getUserPreferences() - return { - id: user.id, - color: user.color ?? defaultUserPreferences.color, - name: user.name ?? defaultUserPreferences.name, - } - }) - - const presenceId = InstancePresenceRecordType.createId(userId) - const presenceDerivation = createPresenceStateDerivation( - userPreferences, - presenceId - )(innerStore) - - return react("when presence changes", () => { - const presence = presenceDerivation.get() - requestAnimationFrame(() => { - updateLocalState(presence) - }) - }) - }, [innerStore, userId, updateLocalState, shouldUseAwareness]) - /* ----------- End presence stuff ----------- */ - -} - diff --git a/src/automerge/useAutomergeStoreV2.ts b/src/automerge/useAutomergeStoreV2.ts index 8c2b992..c4655a5 100644 --- a/src/automerge/useAutomergeStoreV2.ts +++ b/src/automerge/useAutomergeStoreV2.ts @@ -26,6 +26,11 @@ import { PromptShape } from "@/shapes/PromptShapeUtil" import { SharedPianoShape } from "@/shapes/SharedPianoShapeUtil" import { TranscriptionShape } from "@/shapes/TranscriptionShapeUtil" import { ObsNoteShape } from "@/shapes/ObsNoteShapeUtil" +import { FathomTranscriptShape } from "@/shapes/FathomTranscriptShapeUtil" +import { HolonShape } from "@/shapes/HolonShapeUtil" +import { ObsidianBrowserShape } from "@/shapes/ObsidianBrowserShapeUtil" +import { FathomMeetingsBrowserShape } from "@/shapes/FathomMeetingsBrowserShapeUtil" +import { LocationShareShape } from "@/shapes/LocationShareShapeUtil" export function useAutomergeStoreV2({ handle, @@ -36,16 +41,49 @@ export function useAutomergeStoreV2({ }): TLStoreWithStatus { console.log("useAutomergeStoreV2 called with handle:", !!handle) - // Use default schema for now to avoid validation issues - // Custom shapes will be handled through the shape utilities + // Create a custom schema that includes all the custom shapes const customSchema = createTLSchema({ - shapes: defaultShapeSchemas, + shapes: { + ...defaultShapeSchemas, + ChatBox: {} as any, + VideoChat: {} as any, + Embed: {} as any, + Markdown: {} as any, + MycrozineTemplate: {} as any, + Slide: {} as any, + Prompt: {} as any, + SharedPiano: {} as any, + Transcription: {} as any, + ObsNote: {} as any, + FathomTranscript: {} as any, + Holon: {} as any, + ObsidianBrowser: {} as any, + FathomMeetingsBrowser: {} as any, + LocationShare: {} as any, + }, bindings: defaultBindingSchemas, }) const [store] = useState(() => { const store = createTLStore({ schema: customSchema, + shapeUtils: [ + ChatBoxShape, + VideoChatShape, + EmbedShape, + MarkdownShape, + MycrozineTemplateShape, + SlideShape, + PromptShape, + SharedPianoShape, + TranscriptionShape, + ObsNoteShape, + FathomTranscriptShape, + HolonShape, + ObsidianBrowserShape, + FathomMeetingsBrowserShape, + LocationShareShape, + ], }) return store }) @@ -54,6 +92,16 @@ export function useAutomergeStoreV2({ status: "loading", }) + // Debug: Log store status when it changes + useEffect(() => { + if (storeWithStatus.status === "synced-remote" && storeWithStatus.store) { + const allRecords = storeWithStatus.store.allRecords() + const shapes = allRecords.filter(r => r.typeName === 'shape') + const pages = allRecords.filter(r => r.typeName === 'page') + console.log(`๐Ÿ“Š useAutomergeStoreV2: Store synced with ${allRecords.length} total records, ${shapes.length} shapes, ${pages.length} pages`) + } + }, [storeWithStatus.status, storeWithStatus.store]) + /* -------------------- TLDraw <--> Automerge -------------------- */ useEffect(() => { // Early return if handle is not available @@ -80,7 +128,10 @@ export function useAutomergeStoreV2({ if (payload.patches && payload.patches.length > 0) { try { applyAutomergePatchesToTLStore(payload.patches, store) - console.log(`โœ… Successfully applied ${payload.patches.length} patches`) + // Only log if there are many patches or if debugging is needed + if (payload.patches.length > 5) { + console.log(`โœ… Successfully applied ${payload.patches.length} patches`) + } } catch (patchError) { console.error("Error applying patches, attempting individual patch application:", patchError) // Try applying patches one by one to identify problematic ones @@ -110,7 +161,10 @@ export function useAutomergeStoreV2({ } } } - console.log(`Successfully applied ${successCount} out of ${payload.patches.length} patches`) + // Only log if there are failures or many patches + if (successCount < payload.patches.length || payload.patches.length > 5) { + console.log(`Successfully applied ${successCount} out of ${payload.patches.length} patches`) + } } } @@ -133,26 +187,111 @@ export function useAutomergeStoreV2({ handle.on("change", automergeChangeHandler) // Listen for changes from TLDraw and apply them to Automerge - const unsubscribeTLDraw = store.listen(({ changes }) => { - if (isLocalChange) { - console.log("Skipping TLDraw changes (local change)") - return + // CRITICAL: Listen to ALL sources, not just "user", to catch richText/text changes + const unsubscribeTLDraw = store.listen(({ changes, source }) => { + // DEBUG: Log all changes to see what's being detected + const totalChanges = Object.keys(changes.added || {}).length + Object.keys(changes.updated || {}).length + Object.keys(changes.removed || {}).length + + if (totalChanges > 0) { + console.log(`๐Ÿ” TLDraw store changes detected (source: ${source}):`, { + added: Object.keys(changes.added || {}).length, + updated: Object.keys(changes.updated || {}).length, + removed: Object.keys(changes.removed || {}).length, + source: source + }) + + // DEBUG: Check for richText/text changes in updated records + if (changes.updated) { + Object.values(changes.updated).forEach(([_, record]) => { + if (record.typeName === 'shape') { + if (record.type === 'geo' && (record.props as any)?.richText) { + console.log(`๐Ÿ” Geo shape ${record.id} richText change detected:`, { + hasRichText: !!(record.props as any).richText, + richTextType: typeof (record.props as any).richText, + source: source + }) + } + if (record.type === 'note' && (record.props as any)?.richText) { + console.log(`๐Ÿ” Note shape ${record.id} richText change detected:`, { + hasRichText: !!(record.props as any).richText, + richTextType: typeof (record.props as any).richText, + richTextContentLength: Array.isArray((record.props as any).richText?.content) + ? (record.props as any).richText.content.length + : 'not array', + source: source + }) + } + if (record.type === 'arrow' && (record.props as any)?.text !== undefined) { + console.log(`๐Ÿ” Arrow shape ${record.id} text change detected:`, { + hasText: !!(record.props as any).text, + textValue: (record.props as any).text, + source: source + }) + } + if (record.type === 'text' && (record.props as any)?.richText) { + console.log(`๐Ÿ” Text shape ${record.id} richText change detected:`, { + hasRichText: !!(record.props as any).richText, + richTextType: typeof (record.props as any).richText, + source: source + }) + } + } + }) + } + + // DEBUG: Log added shapes to track what's being created + if (changes.added) { + Object.values(changes.added).forEach((record) => { + if (record.typeName === 'shape') { + console.log(`๐Ÿ” Shape added: ${record.type} (${record.id})`, { + type: record.type, + id: record.id, + hasRichText: !!(record.props as any)?.richText, + hasText: !!(record.props as any)?.text, + source: source + }) + } + }) + } } - + + // CRITICAL: Don't skip changes - always save them to ensure consistency + // The isLocalChange flag is only used to prevent feedback loops from Automerge changes + // We should always save TLDraw changes, even if they came from Automerge sync + // This ensures that all shapes (notes, rectangles, etc.) are consistently persisted + try { + // Set flag to prevent feedback loop when this change comes back from Automerge isLocalChange = true + handle.change((doc) => { applyTLStoreChangesToAutomerge(doc, changes) }) - console.log("Applied TLDraw changes to Automerge document") + + // Reset flag after a short delay to allow Automerge change handler to process + // This prevents feedback loops while ensuring all changes are saved + setTimeout(() => { + isLocalChange = false + }, 100) + + // Only log if there are many changes or if debugging is needed + if (totalChanges > 3) { + console.log(`โœ… Applied ${totalChanges} TLDraw changes to Automerge document`) + } else if (totalChanges > 0) { + console.log(`โœ… Applied ${totalChanges} TLDraw change(s) to Automerge document`) + } // Check if the document actually changed const docAfter = handle.doc() } catch (error) { console.error("Error applying TLDraw changes to Automerge:", error) + // Reset flag on error to prevent getting stuck + isLocalChange = false } }, { - source: "user", + // CRITICAL: Don't filter by source - listen to ALL changes + // This ensures we catch richText/text changes regardless of their source + // (TLDraw might emit these changes with a different source than "user") scope: "document", }) @@ -164,15 +303,21 @@ export function useAutomergeStoreV2({ // Initial load - populate TLDraw store from Automerge document const initializeStore = async () => { try { - console.log("Starting TLDraw store initialization...") + // Only log if debugging is needed + // console.log("Starting TLDraw store initialization...") await handle.whenReady() - console.log("Automerge handle is ready") + // console.log("Automerge handle is ready") const doc = handle.doc() - console.log("Got Automerge document:", { - hasStore: !!doc.store, - storeKeys: doc.store ? Object.keys(doc.store).length : 0, - }) + // Only log if debugging is needed + // console.log("Got Automerge document (FIXED VERSION):", { + // hasStore: !!doc.store, + // storeKeys: doc.store ? Object.keys(doc.store).length : 0, + // }) + + // Skip pre-sanitization to avoid Automerge reference errors + // We'll handle validation issues in the record processing loop instead + // Force cache refresh - pre-sanitization code has been removed // Initialize store with existing records from Automerge if (doc.store) { @@ -184,95 +329,205 @@ export function useAutomergeStoreV2({ id: v?.id }))) - // Simple filtering - only keep valid records - const records = allStoreValues.filter((record: any) => - record && record.typeName && record.id - ) + // Simple filtering - only keep valid TLDraw records + // Skip custom record types like obsidian_vault - they're not TLDraw records + // Components should read them directly from Automerge (like ObsidianVaultBrowser does) + const records = allStoreValues.filter((record: any) => { + if (!record || !record.typeName || !record.id) return false + // Skip obsidian_vault records - they're not TLDraw records + if (record.typeName === 'obsidian_vault' || + (typeof record.id === 'string' && record.id.startsWith('obsidian_vault:'))) { + return false + } + return true + }) - console.log(`Found ${records.length} valid records in Automerge document`) + // Only log if there are many records or if debugging is needed + if (records.length > 50) { + console.log(`Found ${records.length} valid records in Automerge document`) + } - // Comprehensive shape validation and fixes for any shape type + // CRITICAL FIXES ONLY - preserve all other properties + // Note: obsidian_vault records are filtered out above - they're not TLDraw records const processedRecords = records.map((record: any) => { // Create a deep copy to avoid modifying immutable Automerge objects const processedRecord = JSON.parse(JSON.stringify(record)) - - // Minimal shape validation - only fix critical issues + + // CRITICAL FIXES ONLY - preserve all other properties if (processedRecord.typeName === 'shape') { // Ensure basic required properties exist - if (processedRecord.x === undefined) processedRecord.x = 0 - if (processedRecord.y === undefined) processedRecord.y = 0 - if (processedRecord.rotation === undefined) processedRecord.rotation = 0 - if (processedRecord.isLocked === undefined) processedRecord.isLocked = false - if (processedRecord.opacity === undefined) processedRecord.opacity = 1 - if (!processedRecord.meta) processedRecord.meta = {} - - // Ensure parentId exists + if (typeof processedRecord.x !== 'number') processedRecord.x = 0 + if (typeof processedRecord.y !== 'number') processedRecord.y = 0 + if (typeof processedRecord.rotation !== 'number') processedRecord.rotation = 0 + if (typeof processedRecord.isLocked !== 'boolean') processedRecord.isLocked = false + if (typeof processedRecord.opacity !== 'number') processedRecord.opacity = 1 + if (!processedRecord.meta || typeof processedRecord.meta !== 'object') processedRecord.meta = {} + if (!processedRecord.index) processedRecord.index = 'a1' if (!processedRecord.parentId) { const pageRecord = records.find((r: any) => r.typeName === 'page') as any if (pageRecord && pageRecord.id) { processedRecord.parentId = pageRecord.id + } else { + processedRecord.parentId = 'page:page' + } + } + if (!processedRecord.props || typeof processedRecord.props !== 'object') processedRecord.props = {} + + // CRITICAL: Infer type from properties BEFORE defaulting to 'geo' + // This ensures arrows and other shapes are properly recognized + if (!processedRecord.type || typeof processedRecord.type !== 'string') { + // Check for arrow-specific properties first + if (processedRecord.props?.start !== undefined || + processedRecord.props?.end !== undefined || + processedRecord.props?.arrowheadStart !== undefined || + processedRecord.props?.arrowheadEnd !== undefined || + processedRecord.props?.kind === 'line' || + processedRecord.props?.kind === 'curved' || + processedRecord.props?.kind === 'straight') { + processedRecord.type = 'arrow' + } + // Check for line-specific properties + else if (processedRecord.props?.points !== undefined) { + processedRecord.type = 'line' + } + // Check for geo-specific properties (w/h/geo) + else if (processedRecord.props?.geo !== undefined || + ('w' in processedRecord && 'h' in processedRecord) || + ('w' in processedRecord.props && 'h' in processedRecord.props)) { + processedRecord.type = 'geo' + } + // Check for note-specific properties + else if (processedRecord.props?.growY !== undefined || + processedRecord.props?.verticalAlign !== undefined) { + processedRecord.type = 'note' + } + // Check for text-specific properties + else if (processedRecord.props?.textAlign !== undefined || + processedRecord.props?.autoSize !== undefined) { + processedRecord.type = 'text' + } + // Check for draw-specific properties + else if (processedRecord.props?.segments !== undefined) { + processedRecord.type = 'draw' + } + // Default to geo only if no other indicators found + else { + processedRecord.type = 'geo' } } - // Ensure shape has a valid type - if (!processedRecord.type) { - console.log(`Shape ${processedRecord.id} missing type, setting to 'geo'`) - processedRecord.type = 'geo' - } - - // Migrate old Transcribe shapes to geo shapes - if (processedRecord.type === 'Transcribe') { - console.log(`Migrating old Transcribe shape ${processedRecord.id} to geo shape`) - processedRecord.type = 'geo' - - // Ensure required geo props exist - if (!processedRecord.props.geo) processedRecord.props.geo = 'rectangle' - if (!processedRecord.props.fill) processedRecord.props.fill = 'solid' - if (!processedRecord.props.color) processedRecord.props.color = 'white' - if (!processedRecord.props.dash) processedRecord.props.dash = 'draw' - if (!processedRecord.props.size) processedRecord.props.size = 'm' - if (!processedRecord.props.font) processedRecord.props.font = 'draw' - if (!processedRecord.props.align) processedRecord.props.align = 'start' - if (!processedRecord.props.verticalAlign) processedRecord.props.verticalAlign = 'start' - if (!processedRecord.props.richText) processedRecord.props.richText = [] as any - - // Move transcript text from props to meta - if (processedRecord.props.transcript) { - if (!processedRecord.meta) processedRecord.meta = {} - processedRecord.meta.text = processedRecord.props.transcript - delete processedRecord.props.transcript + // CRITICAL: For geo shapes, move w/h/geo from top-level to props (required by TLDraw schema) + if (processedRecord.type === 'geo' || ('w' in processedRecord && 'h' in processedRecord && processedRecord.type !== 'arrow')) { + if (!processedRecord.type || processedRecord.type === 'geo') { + processedRecord.type = 'geo' } - // Clean up other old Transcribe-specific props - const oldProps = ['isRecording', 'transcriptSegments', 'speakers', 'currentSpeakerId', - 'interimText', 'isCompleted', 'aiSummary', 'language', 'autoScroll', - 'showTimestamps', 'showSpeakerLabels', 'manualClear'] - oldProps.forEach(prop => { - if (processedRecord.props[prop] !== undefined) { - delete processedRecord.props[prop] + // Move w from top-level to props + if ('w' in processedRecord && processedRecord.w !== undefined) { + if (!('w' in processedRecord.props) || processedRecord.props.w === undefined) { + processedRecord.props.w = processedRecord.w } - }) + delete (processedRecord as any).w + } + + // Move h from top-level to props + if ('h' in processedRecord && processedRecord.h !== undefined) { + if (!('h' in processedRecord.props) || processedRecord.props.h === undefined) { + processedRecord.props.h = processedRecord.h + } + delete (processedRecord as any).h + } + + // Move geo from top-level to props + if ('geo' in processedRecord && processedRecord.geo !== undefined) { + if (!('geo' in processedRecord.props) || processedRecord.props.geo === undefined) { + processedRecord.props.geo = processedRecord.geo + } + delete (processedRecord as any).geo + } + + // Fix richText structure if it exists (preserve content) + if (processedRecord.props.richText) { + if (Array.isArray(processedRecord.props.richText)) { + processedRecord.props.richText = { content: processedRecord.props.richText, type: 'doc' } + } else if (typeof processedRecord.props.richText === 'object' && processedRecord.props.richText !== null) { + if (!processedRecord.props.richText.type) { + processedRecord.props.richText = { ...processedRecord.props.richText, type: 'doc' } + } + if (!processedRecord.props.richText.content) { + processedRecord.props.richText = { ...processedRecord.props.richText, content: [] } + } + } + } + } + + // CRITICAL: For arrow shapes, preserve text property + if (processedRecord.type === 'arrow') { + if ((processedRecord.props as any).text === undefined || (processedRecord.props as any).text === null) { + (processedRecord.props as any).text = '' + } + } + + // CRITICAL: For line shapes, ensure points structure exists (required by schema) + if (processedRecord.type === 'line') { + if ('w' in processedRecord.props) delete (processedRecord.props as any).w + if ('h' in processedRecord.props) delete (processedRecord.props as any).h + if (!processedRecord.props.points || typeof processedRecord.props.points !== 'object' || Array.isArray(processedRecord.props.points)) { + processedRecord.props.points = { + 'a1': { id: 'a1', index: 'a1' as any, x: 0, y: 0 }, + 'a2': { id: 'a2', index: 'a2' as any, x: 100, y: 0 } + } + } + } + + // CRITICAL: For group shapes, remove w/h from props (they cause validation errors) + if (processedRecord.type === 'group') { + if ('w' in processedRecord.props) delete (processedRecord.props as any).w + if ('h' in processedRecord.props) delete (processedRecord.props as any).h + } + + // CRITICAL: For image/video shapes, fix crop structure if it exists + if (processedRecord.type === 'image' || processedRecord.type === 'video') { + if (processedRecord.props.crop !== null && processedRecord.props.crop !== undefined) { + if (!processedRecord.props.crop.topLeft || !processedRecord.props.crop.bottomRight) { + if (processedRecord.props.crop.x !== undefined && processedRecord.props.crop.y !== undefined) { + processedRecord.props.crop = { + topLeft: { x: processedRecord.props.crop.x || 0, y: processedRecord.props.crop.y || 0 }, + bottomRight: { + x: (processedRecord.props.crop.x || 0) + (processedRecord.props.crop.w || 1), + y: (processedRecord.props.crop.y || 0) + (processedRecord.props.crop.h || 1) + } + } + } else { + processedRecord.props.crop = { + topLeft: { x: 0, y: 0 }, + bottomRight: { x: 1, y: 1 } + } + } + } + } + } + + // CRITICAL: Fix richText structure for note shapes if it exists + if (processedRecord.type === 'note' && processedRecord.props.richText) { + if (Array.isArray(processedRecord.props.richText)) { + processedRecord.props.richText = { content: processedRecord.props.richText, type: 'doc' } + } else if (typeof processedRecord.props.richText === 'object' && processedRecord.props.richText !== null) { + if (!processedRecord.props.richText.type) { + processedRecord.props.richText = { ...processedRecord.props.richText, type: 'doc' } + } + if (!processedRecord.props.richText.content) { + processedRecord.props.richText = { ...processedRecord.props.richText, content: [] } + } + } } // Ensure props object exists for all shapes if (!processedRecord.props) processedRecord.props = {} - // Move properties from top level to props for shapes that support them - // Arrow shapes don't have w/h in props, so handle them differently - if (processedRecord.type !== 'arrow') { - if ('w' in processedRecord && typeof processedRecord.w === 'number') { - console.log(`Moving w property from top level to props for shape ${processedRecord.id}`) - processedRecord.props.w = processedRecord.w - delete (processedRecord as any).w - } - - if ('h' in processedRecord && typeof processedRecord.h === 'number') { - console.log(`Moving h property from top level to props for shape ${processedRecord.id}`) - processedRecord.props.h = processedRecord.h - delete (processedRecord as any).h - } - } else { - // For arrow shapes, remove w/h properties entirely as they're not valid + // Preserve original data structure - only move properties when TLDraw validation requires it + // Arrow shapes don't have w/h properties, so remove them if present + if (processedRecord.type === 'arrow') { if ('w' in processedRecord) { console.log(`Removing invalid w property from arrow shape ${processedRecord.id}`) delete (processedRecord as any).w @@ -282,6 +537,7 @@ export function useAutomergeStoreV2({ delete (processedRecord as any).h } } + // For other shapes, preserve the original structure - don't move w/h unless validation fails // Handle arrow shapes specially - ensure they have required properties if (processedRecord.type === 'arrow') { @@ -325,7 +581,7 @@ export function useAutomergeStoreV2({ if (!processedRecord.props.verticalAlign) processedRecord.props.verticalAlign = 'start' if (processedRecord.props.growY === undefined) processedRecord.props.growY = 0 if (!processedRecord.props.url) processedRecord.props.url = '' - if (!processedRecord.props.richText) processedRecord.props.richText = { content: [], type: 'doc' } + // Note: richText is not required for note shapes if (processedRecord.props.scale === undefined) processedRecord.props.scale = 1 // Remove any invalid properties @@ -340,18 +596,26 @@ export function useAutomergeStoreV2({ // Handle text shapes specially - ensure they have required properties if (processedRecord.type === 'text') { - // Ensure required text properties exist + // Ensure required text properties exist (matching default tldraw text shape schema) if (!processedRecord.props.color) processedRecord.props.color = 'black' if (!processedRecord.props.size) processedRecord.props.size = 'm' if (!processedRecord.props.font) processedRecord.props.font = 'draw' if (!processedRecord.props.textAlign) processedRecord.props.textAlign = 'start' - if (!processedRecord.props.w) processedRecord.props.w = 100 - if (!processedRecord.props.richText) processedRecord.props.richText = { content: [], type: 'doc' } + if (processedRecord.props.w === undefined || processedRecord.props.w === null) { + processedRecord.props.w = 100 + } if (processedRecord.props.scale === undefined) processedRecord.props.scale = 1 if (processedRecord.props.autoSize === undefined) processedRecord.props.autoSize = false - // Remove any invalid properties - const invalidTextProps = ['h', 'geo', 'insets', 'scribbles', 'isMinimized', 'roomUrl', 'roomId'] + // Ensure richText property exists for text shapes + if (!processedRecord.props.richText) { + console.log(`๐Ÿ”ง Creating default richText object for text shape ${processedRecord.id}`) + processedRecord.props.richText = { content: [], type: 'doc' } + } + + // Remove any invalid properties (including 'text' property which is not in default schema) + // Note: richText is actually required for text shapes, so don't remove it + const invalidTextProps = ['text', 'h', 'geo', 'insets', 'scribbles', 'isMinimized', 'roomUrl', 'roomId', 'align', 'verticalAlign', 'growY', 'url'] invalidTextProps.forEach(prop => { if (prop in processedRecord.props) { console.log(`Removing invalid prop '${prop}' from text shape ${processedRecord.id}`) @@ -367,7 +631,70 @@ export function useAutomergeStoreV2({ if (!processedRecord.props.fill) processedRecord.props.fill = 'none' if (!processedRecord.props.dash) processedRecord.props.dash = 'draw' if (!processedRecord.props.size) processedRecord.props.size = 'm' - if (!processedRecord.props.segments) processedRecord.props.segments = [] + + // Validate and fix segments array - this is critical for preventing Polyline2d errors + if (!processedRecord.props.segments || !Array.isArray(processedRecord.props.segments)) { + console.log(`๐Ÿ”ง Fixing missing/invalid segments for draw shape ${processedRecord.id}`) + processedRecord.props.segments = [ + { + type: "free", + points: [ + { x: 0, y: 0, z: 0.5 }, + { x: 10, y: 10, z: 0.5 } + ] + } + ] + } else { + // Validate each segment in the array + // Polyline2d requires at least 2 points per segment + const validSegments = [] + for (let i = 0; i < processedRecord.props.segments.length; i++) { + const segment = processedRecord.props.segments[i] + if (segment && typeof segment === 'object' && + segment.type && + Array.isArray(segment.points) && + segment.points.length >= 2) { + // Validate points in the segment + const validPoints = segment.points.filter((point: any) => + point && + typeof point === 'object' && + typeof point.x === 'number' && + typeof point.y === 'number' && + !isNaN(point.x) && !isNaN(point.y) + ) + // Polyline2d requires at least 2 points + if (validPoints.length >= 2) { + validSegments.push({ + type: segment.type, + points: validPoints + }) + } else if (validPoints.length === 1) { + // If only 1 point, duplicate it to create a valid 2-point segment + console.log(`๐Ÿ”ง Draw shape ${processedRecord.id} segment ${i} has only 1 point, duplicating to create valid segment`) + validSegments.push({ + type: segment.type, + points: [validPoints[0], { ...validPoints[0] }] + }) + } + } + } + + if (validSegments.length === 0) { + console.log(`๐Ÿ”ง All segments invalid for draw shape ${processedRecord.id}, creating default segment`) + processedRecord.props.segments = [ + { + type: "free", + points: [ + { x: 0, y: 0, z: 0.5 }, + { x: 10, y: 10, z: 0.5 } + ] + } + ] + } else { + processedRecord.props.segments = validSegments + } + } + if (processedRecord.props.isComplete === undefined) processedRecord.props.isComplete = true if (processedRecord.props.isClosed === undefined) processedRecord.props.isClosed = false if (processedRecord.props.isPen === undefined) processedRecord.props.isPen = false @@ -383,18 +710,55 @@ export function useAutomergeStoreV2({ }) } - // Handle geo shapes specially - move geo property + // Handle geo shapes specially - ensure geo property is in props where TLDraw expects it if (processedRecord.type === 'geo') { - if ('geo' in processedRecord && processedRecord.geo) { - console.log(`Moving geo property from top level to props for shape ${processedRecord.id}`) - processedRecord.props.geo = processedRecord.geo + // Ensure props exists + if (!processedRecord.props) processedRecord.props = {} + + // CRITICAL: ALWAYS remove w/h/geo from top level (TLDraw validation fails if they exist at top level) + // Move w from top level to props (preserve value if not already in props) + if ('w' in processedRecord) { + console.log(`๐Ÿ”ง Geo shape fix: Removing w from top level for shape ${processedRecord.id}`) + if (!('w' in processedRecord.props) || processedRecord.props.w === undefined) { + processedRecord.props.w = (processedRecord as any).w + } + delete (processedRecord as any).w + } + + // Move h from top level to props (preserve value if not already in props) + if ('h' in processedRecord) { + console.log(`๐Ÿ”ง Geo shape fix: Removing h from top level for shape ${processedRecord.id}`) + if (!('h' in processedRecord.props) || processedRecord.props.h === undefined) { + processedRecord.props.h = (processedRecord as any).h + } + delete (processedRecord as any).h + } + + // Move geo from top level to props (preserve value if not already in props) + if ('geo' in processedRecord) { + console.log(`๐Ÿ”ง Geo shape fix: Removing geo from top level for shape ${processedRecord.id}`) + if (!('geo' in processedRecord.props) || processedRecord.props.geo === undefined) { + processedRecord.props.geo = (processedRecord as any).geo + } delete (processedRecord as any).geo } - // Ensure required props exist - if (!processedRecord.props.w) processedRecord.props.w = 100 - if (!processedRecord.props.h) processedRecord.props.h = 100 - if (!processedRecord.props.geo) processedRecord.props.geo = 'rectangle' + // Ensure geo property exists in props with a default value + if (!processedRecord.props.geo) { + processedRecord.props.geo = 'rectangle' + } + + // Ensure w/h exist in props with defaults if missing + if (!processedRecord.props) processedRecord.props = {} + if (processedRecord.props.w === undefined || processedRecord.props.w === null) { + processedRecord.props.w = 100 + } + if (processedRecord.props.h === undefined || processedRecord.props.h === null) { + processedRecord.props.h = 100 + } + if (processedRecord.props.geo === undefined || processedRecord.props.geo === null) { + processedRecord.props.geo = 'rectangle' + } if (!processedRecord.props.dash) processedRecord.props.dash = 'draw' if (!processedRecord.props.growY) processedRecord.props.growY = 0 if (!processedRecord.props.url) processedRecord.props.url = '' @@ -406,6 +770,7 @@ export function useAutomergeStoreV2({ if (!processedRecord.props.font) processedRecord.props.font = 'draw' if (!processedRecord.props.align) processedRecord.props.align = 'middle' if (!processedRecord.props.verticalAlign) processedRecord.props.verticalAlign = 'middle' + // Note: richText IS required for geo shapes in TLDraw if (!processedRecord.props.richText) processedRecord.props.richText = { content: [], type: 'doc' } // Ensure basic geo properties exist if (!processedRecord.props.geo) processedRecord.props.geo = 'rectangle' @@ -425,30 +790,38 @@ export function useAutomergeStoreV2({ processedRecord.props.geo = 'rectangle' } - // Remove invalid properties from props + // Remove invalid properties from props (only log if actually removing) const invalidProps = ['insets', 'scribbles'] invalidProps.forEach(prop => { if (prop in processedRecord.props) { - console.log(`Removing invalid prop '${prop}' from geo shape ${processedRecord.id}`) delete (processedRecord.props as any)[prop] } }) } // Handle rich text content that might be undefined or invalid - if (processedRecord.props && processedRecord.props.richText !== undefined) { - if (!Array.isArray(processedRecord.props.richText)) { - console.warn('Fixing invalid richText property for shape:', processedRecord.id, 'type:', processedRecord.type, 'was:', typeof processedRecord.props.richText) - processedRecord.props.richText = { content: [], type: 'doc' } + // Only process richText for shapes that actually use it (text, note, geo, etc.) + // CRITICAL: geo shapes (rectangles) can legitimately have richText in TLDraw + if (processedRecord.type === 'text' || processedRecord.type === 'note' || processedRecord.type === 'geo') { + if (processedRecord.props && processedRecord.props.richText !== undefined) { + if (!Array.isArray(processedRecord.props.richText) && typeof processedRecord.props.richText !== 'object') { + console.warn('Fixing invalid richText property for shape:', processedRecord.id, 'type:', processedRecord.type, 'was:', typeof processedRecord.props.richText) + processedRecord.props.richText = { content: [], type: 'doc' } + } else if (Array.isArray(processedRecord.props.richText)) { + // If it's an array, convert to proper richText object structure + console.log(`๐Ÿ”ง Converting richText array to object for shape ${processedRecord.id}`) + processedRecord.props.richText = { content: processedRecord.props.richText, type: 'doc' } + } } else { - // If it's an array, convert to proper richText object structure - console.log(`๐Ÿ”ง Converting richText array to object for shape ${processedRecord.id}`) - processedRecord.props.richText = { content: processedRecord.props.richText, type: 'doc' } + // Create default empty richText object for text shapes (but not for geo/note unless they already have it) + if (processedRecord.type === 'text') { + if (!processedRecord.props) processedRecord.props = {} + processedRecord.props.richText = { content: [], type: 'doc' } + } } - } else if (processedRecord.type === 'geo' || processedRecord.type === 'note') { - // These shape types require richText, so create a default empty object - if (!processedRecord.props) processedRecord.props = {} - processedRecord.props.richText = { content: [], type: 'doc' } + } else if (processedRecord.props && processedRecord.props.richText !== undefined) { + // Remove richText from shapes that don't use it (but preserve for geo/note which are handled above) + delete (processedRecord.props as any).richText } // Remove invalid properties that cause validation errors (after moving geo properties) @@ -459,60 +832,301 @@ export function useAutomergeStoreV2({ ] invalidProperties.forEach(prop => { if (prop in processedRecord) { - console.log(`Removing invalid property '${prop}' from shape ${processedRecord.id}`) delete (processedRecord as any)[prop] } }) - // Convert custom shape types to valid TLDraw types - const customShapeTypeMap: { [key: string]: string } = { - 'VideoChat': 'embed', - 'Transcription': 'text', - 'SharedPiano': 'embed', - 'Prompt': 'text', - 'ChatBox': 'embed', - 'Embed': 'embed', - 'Markdown': 'text', - 'MycrozineTemplate': 'embed', - 'Slide': 'embed', - 'ObsNote': 'text' + // Custom shapes are supported natively by our custom schema - no conversion needed! + // Just ensure they have the required properties for their type + if (processedRecord.type === 'VideoChat' || processedRecord.type === 'ChatBox' || + processedRecord.type === 'Embed' || processedRecord.type === 'SharedPiano' || + processedRecord.type === 'MycrozineTemplate' || processedRecord.type === 'Slide') { + // These are embed-like shapes - ensure they have basic properties + if (!processedRecord.props) processedRecord.props = {} + if (processedRecord.props.w === undefined || processedRecord.props.w === null) { + processedRecord.props.w = 300 + } + if (processedRecord.props.h === undefined || processedRecord.props.h === null) { + processedRecord.props.h = 200 + } + console.log(`๐Ÿ”ง Ensured embed-like shape ${processedRecord.type} has required properties:`, processedRecord.props) + } else if (processedRecord.type === 'Prompt' || processedRecord.type === 'Transcription' || + processedRecord.type === 'Markdown') { + // These are text-like shapes - ensure they have text properties + if (!processedRecord.props) processedRecord.props = {} + if (processedRecord.props.w === undefined || processedRecord.props.w === null) { + processedRecord.props.w = 300 + } + + // Convert value property to richText if it exists (for Prompt shapes) + if (processedRecord.props.value && !processedRecord.props.richText) { + processedRecord.props.richText = { + content: [ + { + type: 'paragraph', + content: [ + { + type: 'text', + text: processedRecord.props.value + } + ] + } + ], + type: 'doc' + } + console.log(`๐Ÿ”ง Converted value to richText for ${processedRecord.type} shape ${processedRecord.id}`) + } + + if (!processedRecord.props.richText) { + processedRecord.props.richText = { content: [], type: 'doc' } + } + console.log(`๐Ÿ”ง Ensured text-like shape ${processedRecord.type} has required properties:`, processedRecord.props) } - if (customShapeTypeMap[processedRecord.type]) { - console.log(`๐Ÿ”ง Converting custom shape type ${processedRecord.type} to ${customShapeTypeMap[processedRecord.type]} for shape:`, processedRecord.id) - processedRecord.type = customShapeTypeMap[processedRecord.type] + // Validate that the shape type is supported by our schema + const validCustomShapes = ['ObsNote', 'VideoChat', 'Transcription', 'SharedPiano', 'Prompt', 'ChatBox', 'Embed', 'Markdown', 'MycrozineTemplate', 'Slide', 'FathomTranscript', 'Holon', 'ObsidianBrowser', 'HolonBrowser', 'FathomMeetingsBrowser', 'LocationShare'] + const validDefaultShapes = ['arrow', 'bookmark', 'draw', 'embed', 'frame', 'geo', 'group', 'highlight', 'image', 'line', 'note', 'text', 'video'] + const allValidShapes = [...validCustomShapes, ...validDefaultShapes] + + if (!allValidShapes.includes(processedRecord.type)) { + console.log(`๐Ÿ”ง Unknown shape type ${processedRecord.type}, converting to text shape for shape:`, processedRecord.id) + processedRecord.type = 'text' + if (!processedRecord.props) processedRecord.props = {} + // Preserve existing props and only set defaults for missing required text shape properties + // This prevents losing metadata or other valid properties + processedRecord.props = { + ...processedRecord.props, // Preserve existing props + w: processedRecord.props.w || 300, + color: processedRecord.props.color || 'black', + size: processedRecord.props.size || 'm', + font: processedRecord.props.font || 'draw', + textAlign: processedRecord.props.textAlign || 'start', + autoSize: processedRecord.props.autoSize !== undefined ? processedRecord.props.autoSize : false, + scale: processedRecord.props.scale || 1, + richText: processedRecord.props.richText || { content: [], type: 'doc' } + } + // Remove invalid properties for text shapes (but preserve meta and other valid top-level properties) + const invalidTextProps = ['h', 'geo', 'insets', 'scribbles', 'isMinimized', 'roomUrl', 'text', 'align', 'verticalAlign', 'growY', 'url'] + invalidTextProps.forEach(prop => { + if (prop in processedRecord.props) { + delete (processedRecord.props as any)[prop] + } + }) + console.log(`๐Ÿ”ง Converted unknown shape to text:`, processedRecord.props) } // Universal shape validation - ensure any shape type can be imported + // CRITICAL: Fix image and video shapes FIRST - ensure crop has correct structure + // Tldraw expects crop to be { topLeft: { x, y }, bottomRight: { x, y } } or null + if (processedRecord.type === 'image' || processedRecord.type === 'video') { + // Ensure props exists for image/video shapes + if (!processedRecord.props) { + processedRecord.props = {} + } + // Fix crop structure + if (processedRecord.props.crop !== null && processedRecord.props.crop !== undefined) { + // If crop exists but has wrong structure, fix it + if (!processedRecord.props.crop.topLeft || !processedRecord.props.crop.bottomRight) { + // Convert old format { x, y, w, h } to new format, or set default + if (processedRecord.props.crop.x !== undefined && processedRecord.props.crop.y !== undefined) { + // Old format: convert to new format + processedRecord.props.crop = { + topLeft: { x: processedRecord.props.crop.x || 0, y: processedRecord.props.crop.y || 0 }, + bottomRight: { + x: (processedRecord.props.crop.x || 0) + (processedRecord.props.crop.w || 1), + y: (processedRecord.props.crop.y || 0) + (processedRecord.props.crop.h || 1) + } + } + } else { + // Invalid structure: set to default (full crop) + processedRecord.props.crop = { + topLeft: { x: 0, y: 0 }, + bottomRight: { x: 1, y: 1 } + } + } + } else { + // Ensure topLeft and bottomRight are proper objects + if (!processedRecord.props.crop.topLeft || typeof processedRecord.props.crop.topLeft !== 'object') { + processedRecord.props.crop.topLeft = { x: 0, y: 0 } + } + if (!processedRecord.props.crop.bottomRight || typeof processedRecord.props.crop.bottomRight !== 'object') { + processedRecord.props.crop.bottomRight = { x: 1, y: 1 } + } + } + } else { + // Crop is null/undefined: set to null (no crop) + processedRecord.props.crop = null + } + } + + // CRITICAL: Fix line shapes - ensure valid points and remove invalid w/h properties + if (processedRecord.type === 'line') { + if (!processedRecord.props) { + processedRecord.props = {} + } + // Line shapes should NOT have w or h properties + if ('w' in processedRecord.props) { + console.log(`๐Ÿ”ง Universal fix: Removing invalid w property from line shape ${processedRecord.id}`) + delete processedRecord.props.w + } + if ('h' in processedRecord.props) { + console.log(`๐Ÿ”ง Universal fix: Removing invalid h property from line shape ${processedRecord.id}`) + delete processedRecord.props.h + } + + // Line shapes REQUIRE points property: Record + if (!processedRecord.props.points || typeof processedRecord.props.points !== 'object' || Array.isArray(processedRecord.props.points)) { + console.log(`๐Ÿ”ง Universal fix: Creating default points for line shape ${processedRecord.id}`) + // Create default points with at least 2 points + const point1 = { id: 'a1', index: 'a1' as any, x: 0, y: 0 } + const point2 = { id: 'a2', index: 'a2' as any, x: 100, y: 0 } + processedRecord.props.points = { + 'a1': point1, + 'a2': point2 + } + } else { + // Validate and fix existing points + const validPoints: Record = {} + let pointIndex = 0 + const indices = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10'] + + for (const [key, point] of Object.entries(processedRecord.props.points)) { + if (point && typeof point === 'object' && + typeof (point as any).x === 'number' && + typeof (point as any).y === 'number' && + !isNaN((point as any).x) && !isNaN((point as any).y)) { + const index = indices[pointIndex] || `a${pointIndex + 1}` + validPoints[index] = { + id: index, + index: index as any, + x: (point as any).x, + y: (point as any).y + } + pointIndex++ + } + } + + if (Object.keys(validPoints).length === 0) { + // No valid points, create default + console.log(`๐Ÿ”ง Universal fix: No valid points found for line shape ${processedRecord.id}, creating default points`) + processedRecord.props.points = { + 'a1': { id: 'a1', index: 'a1' as any, x: 0, y: 0 }, + 'a2': { id: 'a2', index: 'a2' as any, x: 100, y: 0 } + } + } else if (Object.keys(validPoints).length === 1) { + // Only one point, add a second one + const firstPoint = Object.values(validPoints)[0] + const secondIndex = indices[1] || 'a2' + validPoints[secondIndex] = { + id: secondIndex, + index: secondIndex as any, + x: firstPoint.x + 100, + y: firstPoint.y + } + processedRecord.props.points = validPoints + } else { + processedRecord.props.points = validPoints + } + } + + // Ensure other required line shape properties exist + if (!processedRecord.props.color) processedRecord.props.color = 'black' + if (!processedRecord.props.dash) processedRecord.props.dash = 'draw' + if (!processedRecord.props.size) processedRecord.props.size = 'm' + if (!processedRecord.props.spline) processedRecord.props.spline = 'line' + if (processedRecord.props.scale === undefined || processedRecord.props.scale === null) { + processedRecord.props.scale = 1 + } + } + + // CRITICAL: Fix group shapes - remove invalid w/h properties + if (processedRecord.type === 'group') { + if (!processedRecord.props) { + processedRecord.props = {} + } + // Group shapes should NOT have w or h properties + if ('w' in processedRecord.props) { + console.log(`๐Ÿ”ง Universal fix: Removing invalid w property from group shape ${processedRecord.id}`) + delete processedRecord.props.w + } + if ('h' in processedRecord.props) { + console.log(`๐Ÿ”ง Universal fix: Removing invalid h property from group shape ${processedRecord.id}`) + delete processedRecord.props.h + } + } + if (processedRecord.props) { - // Fix any richText issues for any shape type - if (processedRecord.props.richText !== undefined) { + + // Fix any richText issues for text shapes only + if (processedRecord.type === 'text' && processedRecord.props.richText !== undefined) { if (!Array.isArray(processedRecord.props.richText)) { - console.log(`๐Ÿ”ง Universal fix: Converting richText to proper object for shape ${processedRecord.id} (type: ${processedRecord.type})`) + console.log(`๐Ÿ”ง Universal fix: Converting richText to proper object for text shape ${processedRecord.id}`) processedRecord.props.richText = { content: [], type: 'doc' } } else { // Convert array to proper object structure - console.log(`๐Ÿ”ง Universal fix: Converting richText array to object for shape ${processedRecord.id} (type: ${processedRecord.type})`) + console.log(`๐Ÿ”ง Universal fix: Converting richText array to object for text shape ${processedRecord.id}`) processedRecord.props.richText = { content: processedRecord.props.richText, type: 'doc' } } } // Special handling for geo shapes if (processedRecord.type === 'geo') { + // Geo shapes should have richText property but not text property + if ('text' in processedRecord.props) { + console.log(`๐Ÿ”ง Removing invalid text property from geo shape ${processedRecord.id}`) + delete processedRecord.props.text + } + + // Ensure richText property exists and is properly structured for geo shapes + if (!processedRecord.props.richText) { + console.log(`๐Ÿ”ง Adding missing richText property for geo shape ${processedRecord.id}`) + processedRecord.props.richText = { content: [], type: 'doc' } + } else if (Array.isArray(processedRecord.props.richText)) { + console.log(`๐Ÿ”ง Converting richText array to object for geo shape ${processedRecord.id}`) + processedRecord.props.richText = { content: processedRecord.props.richText, type: 'doc' } + } else if (typeof processedRecord.props.richText !== 'object' || processedRecord.props.richText === null) { + console.log(`๐Ÿ”ง Fixing invalid richText structure for geo shape ${processedRecord.id}`) + processedRecord.props.richText = { content: [], type: 'doc' } + } else if (!processedRecord.props.richText.content) { + // If richText exists but content is missing, preserve the rest and add empty content + console.log(`๐Ÿ”ง Adding missing content to richText for geo shape ${processedRecord.id}`) + processedRecord.props.richText = { + ...processedRecord.props.richText, + content: processedRecord.props.richText.content || [], + type: processedRecord.props.richText.type || 'doc' + } + } + // Ensure geo shape has proper structure if (!processedRecord.props.geo) { processedRecord.props.geo = 'rectangle' } - if (!processedRecord.props.w) { + if (processedRecord.props.w === undefined || processedRecord.props.w === null) { processedRecord.props.w = 100 } - if (!processedRecord.props.h) { + if (processedRecord.props.h === undefined || processedRecord.props.h === null) { processedRecord.props.h = 100 } - // Remove invalid properties for geo shapes (including insets) - const invalidGeoProps = ['transcript', 'isTranscribing', 'isPaused', 'isEditing', 'roomUrl', 'roomId', 'prompt', 'value', 'agentBinding', 'isMinimized', 'noteId', 'title', 'content', 'tags', 'showPreview', 'backgroundColor', 'textColor', 'editingContent', 'vaultName', 'insets'] - invalidGeoProps.forEach(prop => { + // Fix dash property - ensure it's a valid value + if (processedRecord.props.dash === '' || processedRecord.props.dash === undefined) { + processedRecord.props.dash = 'solid' + } else if (!['draw', 'solid', 'dashed', 'dotted'].includes(processedRecord.props.dash)) { + console.log(`๐Ÿ”ง Fixing invalid dash value '${processedRecord.props.dash}' for geo shape:`, processedRecord.id) + processedRecord.props.dash = 'solid' + } + + // Fix scale property - ensure it's a number + if (processedRecord.props.scale === undefined || processedRecord.props.scale === null) { + processedRecord.props.scale = 1 + } else if (typeof processedRecord.props.scale !== 'number') { + console.log(`๐Ÿ”ง Fixing invalid scale value '${processedRecord.props.scale}' for geo shape:`, processedRecord.id) + processedRecord.props.scale = 1 + } + + // Remove invalid properties for geo shapes (including insets) - but NOT richText as it's required + const invalidGeoOtherProps = ['transcript', 'isTranscribing', 'isPaused', 'isEditing', 'roomUrl', 'roomId', 'prompt', 'value', 'agentBinding', 'isMinimized', 'noteId', 'title', 'content', 'tags', 'showPreview', 'backgroundColor', 'textColor', 'editingContent', 'vaultName', 'insets'] + invalidGeoOtherProps.forEach(prop => { if (prop in processedRecord.props) { console.log(`๐Ÿ”ง Removing invalid ${prop} property from geo shape:`, processedRecord.id) delete processedRecord.props[prop] @@ -520,8 +1134,13 @@ export function useAutomergeStoreV2({ }) } - // Fix note shapes - remove w/h properties + // Fix note shapes - ensure richText exists and remove invalid w/h properties if (processedRecord.type === 'note') { + // Note shapes REQUIRE richText property (it's part of the schema) + if (!processedRecord.props.richText || typeof processedRecord.props.richText !== 'object') { + console.log(`๐Ÿ”ง Adding missing richText property for note shape ${processedRecord.id}`) + processedRecord.props.richText = { content: [], type: 'doc' } + } if ('w' in processedRecord.props) { console.log(`๐Ÿ”ง Removing invalid w property from note shape:`, processedRecord.id) delete processedRecord.props.w @@ -563,26 +1182,31 @@ export function useAutomergeStoreV2({ }) } - // Ensure all required properties exist for any shape type (except arrow and draw) - if (processedRecord.type !== 'arrow' && processedRecord.type !== 'draw' && processedRecord.type !== 'text' && processedRecord.type !== 'note') { + // Ensure all required properties exist for any shape type (except arrow, draw, line, text, note, and group) + if (processedRecord.type !== 'arrow' && processedRecord.type !== 'draw' && processedRecord.type !== 'line' && processedRecord.type !== 'text' && processedRecord.type !== 'note' && processedRecord.type !== 'group') { const requiredProps = ['w', 'h'] requiredProps.forEach(prop => { if (processedRecord.props[prop] === undefined) { console.log(`๐Ÿ”ง Universal fix: Adding missing ${prop} for shape ${processedRecord.id} (type: ${processedRecord.type})`) - if (prop === 'w') processedRecord.props.w = 100 - if (prop === 'h') processedRecord.props.h = 100 + if (prop === 'w' && processedRecord.props.w === undefined) processedRecord.props.w = 100 + if (prop === 'h' && processedRecord.props.h === undefined) processedRecord.props.h = 100 } }) } else if (processedRecord.type === 'text') { // Text shapes only need w, not h - if (processedRecord.props.w === undefined) { + if (processedRecord.props.w === undefined || processedRecord.props.w === null) { console.log(`๐Ÿ”ง Universal fix: Adding missing w for text shape ${processedRecord.id}`) processedRecord.props.w = 100 } } - // Clean up any null/undefined values in props + // Clean up any null/undefined values in props (but preserve required objects like crop for images/videos) + // IMPORTANT: crop is already set above for image/video shapes, so we must skip it here Object.keys(processedRecord.props).forEach(propKey => { + // Skip crop for image/video shapes - it must be an object, not undefined + if ((processedRecord.type === 'image' || processedRecord.type === 'video') && propKey === 'crop') { + return // crop is required and already set above + } if (processedRecord.props[propKey] === null || processedRecord.props[propKey] === undefined) { console.log(`๐Ÿ”ง Universal fix: Removing null/undefined prop ${propKey} from shape ${processedRecord.id}`) delete processedRecord.props[propKey] @@ -651,9 +1275,95 @@ export function useAutomergeStoreV2({ // Load records into store if (processedRecords.length > 0) { console.log("Attempting to load records into store...") + + // Final validation: ensure all shapes are properly structured + processedRecords.forEach(record => { + if (record.typeName === 'shape') { + // Final check for geo shapes - ALWAYS remove w/h/geo from top level (even if in props) + if (record.type === 'geo') { + // ALWAYS delete w from top level (TLDraw validation fails if it exists at top level) + if ('w' in record) { + console.log(`๐Ÿ”ง FINAL PRE-STORE FIX: Removing w from top level for geo shape ${record.id}`) + if (!record.props) record.props = {} + if (!('w' in record.props) || record.props.w === undefined) { + record.props.w = (record as any).w + } + delete (record as any).w + } + // ALWAYS delete h from top level + if ('h' in record) { + console.log(`๐Ÿ”ง FINAL PRE-STORE FIX: Removing h from top level for geo shape ${record.id}`) + if (!record.props) record.props = {} + if (!('h' in record.props) || record.props.h === undefined) { + record.props.h = (record as any).h + } + delete (record as any).h + } + // ALWAYS delete geo from top level + if ('geo' in record) { + console.log(`๐Ÿ”ง FINAL PRE-STORE FIX: Removing geo from top level for geo shape ${record.id}`) + if (!record.props) record.props = {} + if (!('geo' in record.props) || record.props.geo === undefined) { + record.props.geo = (record as any).geo + } + delete (record as any).geo + } + } + + // Ensure text shapes have richText + if (record.type === 'text') { + if (!record.props) { + record.props = {} + } + if (!record.props.richText) { + console.log(`๐Ÿ”ง Final fix: Adding richText to text shape ${record.id}`) + record.props.richText = { content: [], type: 'doc' } + } + } + } + }) + try { store.mergeRemoteChanges(() => { - store.put(processedRecords) + // CRITICAL: Final safety check - ensure no geo shapes have w/h/geo at top level + // Note: obsidian_vault records are already filtered out above + const sanitizedRecords = processedRecords.map(record => { + if (record.typeName === 'shape' && record.type === 'geo') { + const sanitized = { ...record } + // ALWAYS remove from top level if present + if ('w' in sanitized) { + console.log(`๐Ÿ”ง LAST-CHANCE FIX: Removing w from top level for geo shape ${sanitized.id}`) + if (!sanitized.props) sanitized.props = {} + if (!('w' in sanitized.props) || sanitized.props.w === undefined) { + sanitized.props.w = (sanitized as any).w + } + delete (sanitized as any).w + } + if ('h' in sanitized) { + console.log(`๐Ÿ”ง LAST-CHANCE FIX: Removing h from top level for geo shape ${sanitized.id}`) + if (!sanitized.props) sanitized.props = {} + if (!('h' in sanitized.props) || sanitized.props.h === undefined) { + sanitized.props.h = (sanitized as any).h + } + delete (sanitized as any).h + } + if ('geo' in sanitized) { + console.log(`๐Ÿ”ง LAST-CHANCE FIX: Removing geo from top level for geo shape ${sanitized.id}`) + if (!sanitized.props) sanitized.props = {} + if (!('geo' in sanitized.props) || sanitized.props.geo === undefined) { + sanitized.props.geo = (sanitized as any).geo + } + delete (sanitized as any).geo + } + return sanitized + } + return record + }) + + // Put TLDraw records into store + if (sanitizedRecords.length > 0) { + store.put(sanitizedRecords) + } }) console.log("Successfully loaded all records into store") } catch (error) { @@ -664,9 +1374,86 @@ export function useAutomergeStoreV2({ const failedRecords = [] for (const record of processedRecords) { + // Final validation for individual record: ensure text shapes have richText + if (record.type === 'text') { + if (!record.props) { + record.props = {} + } + if (!record.props.richText) { + console.log(`๐Ÿ”ง Individual fix: Adding richText to text shape ${record.id}`) + record.props.richText = { content: [], type: 'doc' } + } + } + try { + // CRITICAL: Final validation before putting record into store + if (record.typeName === 'shape' && record.type === 'geo') { + // ALWAYS remove w/h/geo from top level (TLDraw validation fails if they exist at top level) + if ('w' in record) { + console.log(`๐Ÿ”ง INDIVIDUAL PRE-STORE FIX: Removing w from top level for geo shape ${record.id}`) + if (!record.props) record.props = {} + if (!('w' in record.props) || record.props.w === undefined) { + record.props.w = (record as any).w + } + delete (record as any).w + } + if ('h' in record) { + console.log(`๐Ÿ”ง INDIVIDUAL PRE-STORE FIX: Removing h from top level for geo shape ${record.id}`) + if (!record.props) record.props = {} + if (!('h' in record.props) || record.props.h === undefined) { + record.props.h = (record as any).h + } + delete (record as any).h + } + if ('geo' in record) { + console.log(`๐Ÿ”ง INDIVIDUAL PRE-STORE FIX: Removing geo from top level for geo shape ${record.id}`) + if (!record.props) record.props = {} + if (!('geo' in record.props) || record.props.geo === undefined) { + record.props.geo = (record as any).geo + } + delete (record as any).geo + } + + // Ensure geo property exists in props + if (!record.props) record.props = {} + if (!record.props.geo) { + record.props.geo = 'rectangle' + } + } + + // CRITICAL: Final safety check - ensure no geo shapes have w/h/geo at top level + let recordToPut = record + if (record.typeName === 'shape' && record.type === 'geo') { + // Store values before removing from top level + const wValue = 'w' in record ? (record as any).w : undefined + const hValue = 'h' in record ? (record as any).h : undefined + const geoValue = 'geo' in record ? (record as any).geo : undefined + + // Create cleaned record without w/h/geo at top level + const cleaned: any = {} + for (const key in record) { + if (key !== 'w' && key !== 'h' && key !== 'geo') { + cleaned[key] = (record as any)[key] + } + } + + // Ensure props exists and move values there if needed + if (!cleaned.props) cleaned.props = {} + if (wValue !== undefined && (!('w' in cleaned.props) || cleaned.props.w === undefined)) { + cleaned.props.w = wValue + } + if (hValue !== undefined && (!('h' in cleaned.props) || cleaned.props.h === undefined)) { + cleaned.props.h = hValue + } + if (geoValue !== undefined && (!('geo' in cleaned.props) || cleaned.props.geo === undefined)) { + cleaned.props.geo = geoValue + } + + recordToPut = cleaned as any + } + store.mergeRemoteChanges(() => { - store.put([record]) + store.put([recordToPut]) }) successCount++ console.log(`โœ… Successfully loaded record ${record.id} (${record.typeName})`) @@ -687,12 +1474,17 @@ export function useAutomergeStoreV2({ failedRecords.push(record) } } - console.log(`Successfully loaded ${successCount} out of ${processedRecords.length} records`) - console.log(`Failed records: ${failedRecords.length}`, failedRecords.map(r => r.id)) + // Only log if there are failures or many records + if (successCount < processedRecords.length || processedRecords.length > 50) { + console.log(`Successfully loaded ${successCount} out of ${processedRecords.length} records`) + } + // Only log if debugging is needed + // console.log(`Failed records: ${failedRecords.length}`, failedRecords.map(r => r.id)) // Try to fix and reload failed records if (failedRecords.length > 0) { - console.log("Attempting to fix and reload failed records...") + // Only log if debugging is needed + // console.log("Attempting to fix and reload failed records...") for (const record of failedRecords) { try { // Additional cleanup for failed records - create deep copy @@ -718,9 +1510,9 @@ export function useAutomergeStoreV2({ } } - // Remove any remaining top-level w/h properties for shapes (except arrow and draw) + // Remove any remaining top-level w/h properties for shapes (except arrow, draw, and text) if (fixedRecord.typeName === 'shape') { - if (fixedRecord.type !== 'arrow' && fixedRecord.type !== 'draw') { + if (fixedRecord.type !== 'arrow' && fixedRecord.type !== 'draw' && fixedRecord.type !== 'text') { if ('w' in fixedRecord) { if (!fixedRecord.props) fixedRecord.props = {} fixedRecord.props.w = fixedRecord.w @@ -752,24 +1544,24 @@ export function useAutomergeStoreV2({ } } - // Comprehensive richText validation - ensure it's always an object with content and type - if (fixedRecord.props) { + // Comprehensive richText validation - ensure it's always an object with content and type for text shapes + if (fixedRecord.type === 'text' && fixedRecord.props) { if (fixedRecord.props.richText !== undefined) { if (!Array.isArray(fixedRecord.props.richText)) { - console.log(`๐Ÿ”ง Fixing richText for shape ${fixedRecord.id}: was ${typeof fixedRecord.props.richText}, setting to proper object`) + console.log(`๐Ÿ”ง Fixing richText for text shape ${fixedRecord.id}: was ${typeof fixedRecord.props.richText}, setting to proper object`) fixedRecord.props.richText = { content: [], type: 'doc' } } else { // If it's an array, convert to proper richText object structure - console.log(`๐Ÿ”ง Converting richText array to object for shape ${fixedRecord.id}`) + console.log(`๐Ÿ”ง Converting richText array to object for text shape ${fixedRecord.id}`) fixedRecord.props.richText = { content: fixedRecord.props.richText, type: 'doc' } } } else { - // All shapes should have richText as an object if not present - console.log(`๐Ÿ”ง Creating default richText object for shape ${fixedRecord.id} (type: ${fixedRecord.type})`) + // Text shapes must have richText as an object + console.log(`๐Ÿ”ง Creating default richText object for text shape ${fixedRecord.id}`) fixedRecord.props.richText = { content: [], type: 'doc' } } - } else { - // Ensure props object exists + } else if (fixedRecord.type === 'text' && !fixedRecord.props) { + // Ensure props object exists for text shapes fixedRecord.props = { richText: { content: [], type: 'doc' } } } @@ -797,9 +1589,14 @@ export function useAutomergeStoreV2({ if (fixedRecord.props.autoSize === undefined) { fixedRecord.props.autoSize = false } + if (!fixedRecord.props.richText) { + console.log(`๐Ÿ”ง Creating default richText object for text shape ${fixedRecord.id}`) + fixedRecord.props.richText = { content: [], type: 'doc' } + } - // Remove invalid properties for text shapes - const invalidTextProps = ['h', 'geo', 'insets', 'scribbles', 'isMinimized', 'roomUrl'] + // Remove invalid properties for text shapes (matching default text shape schema) + // Note: richText is actually required for text shapes, so don't remove it + const invalidTextProps = ['h', 'geo', 'insets', 'scribbles', 'isMinimized', 'roomUrl', 'text', 'align', 'verticalAlign', 'growY', 'url'] invalidTextProps.forEach(prop => { if (prop in fixedRecord.props) { console.log(`๐Ÿ”ง Removing invalid prop '${prop}' from text shape ${fixedRecord.id}`) @@ -809,7 +1606,7 @@ export function useAutomergeStoreV2({ } // Fix embed shapes - ensure they have required properties and remove invalid ones - if (fixedRecord.type === 'embed') { + if (fixedRecord.type === 'Embed' || fixedRecord.type === 'embed') { if (!fixedRecord.props.url) { console.log(`๐Ÿ”ง Adding missing url property for embed shape ${fixedRecord.id}`) fixedRecord.props.url = '' @@ -820,9 +1617,12 @@ export function useAutomergeStoreV2({ if (!fixedRecord.props.h) { fixedRecord.props.h = 300 } + if (fixedRecord.props.isMinimized === undefined) { + fixedRecord.props.isMinimized = false + } - // Remove invalid properties for embed shapes - const invalidEmbedProps = ['isMinimized', 'roomUrl', 'roomId', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'richText'] + // Remove invalid properties for embed shapes (matching custom EmbedShape schema) + const invalidEmbedProps = ['doesResize', 'doesResizeHeight', 'roomUrl', 'roomId', 'color', 'fill', 'dash', 'size', 'text', 'font', 'align', 'verticalAlign', 'growY', 'richText'] invalidEmbedProps.forEach(prop => { if (prop in fixedRecord.props) { console.log(`๐Ÿ”ง Removing invalid prop '${prop}' from embed shape ${fixedRecord.id}`) @@ -849,6 +1649,50 @@ export function useAutomergeStoreV2({ if (fixedRecord.opacity === undefined) fixedRecord.opacity = 1 if (!fixedRecord.meta) fixedRecord.meta = {} + // CRITICAL: Final geo shape validation - ALWAYS remove w/h/geo from top level + if (fixedRecord.type === 'geo') { + // Store values before removing from top level + const wValue = 'w' in fixedRecord ? (fixedRecord as any).w : undefined + const hValue = 'h' in fixedRecord ? (fixedRecord as any).h : undefined + const geoValue = 'geo' in fixedRecord ? (fixedRecord as any).geo : undefined + + // Ensure props exists + if (!fixedRecord.props) fixedRecord.props = {} + + // ALWAYS remove w from top level (even if value is 0 or undefined) + if ('w' in fixedRecord) { + if (!('w' in fixedRecord.props) || fixedRecord.props.w === undefined) { + fixedRecord.props.w = wValue !== undefined ? wValue : 100 + } + delete (fixedRecord as any).w + } + + // ALWAYS remove h from top level (even if value is 0 or undefined) + if ('h' in fixedRecord) { + if (!('h' in fixedRecord.props) || fixedRecord.props.h === undefined) { + fixedRecord.props.h = hValue !== undefined ? hValue : 100 + } + delete (fixedRecord as any).h + } + + // ALWAYS remove geo from top level (even if value is undefined) + if ('geo' in fixedRecord) { + if (!('geo' in fixedRecord.props) || fixedRecord.props.geo === undefined) { + fixedRecord.props.geo = geoValue !== undefined ? geoValue : 'rectangle' + } + delete (fixedRecord as any).geo + } + + // Ensure geo property exists in props + if (!fixedRecord.props.geo) { + fixedRecord.props.geo = 'rectangle' + } + + // Ensure w and h are in props + if (fixedRecord.props.w === undefined) fixedRecord.props.w = 100 + if (fixedRecord.props.h === undefined) fixedRecord.props.h = 100 + } + // Ensure parentId exists if (!fixedRecord.parentId) { const pageRecord = records.find((r: any) => r.typeName === 'page') as any @@ -860,16 +1704,145 @@ export function useAutomergeStoreV2({ // Ensure props object exists if (!fixedRecord.props) fixedRecord.props = {} - // Ensure w and h exist in props (except for arrow and draw shapes) - if (fixedRecord.type !== 'arrow' && fixedRecord.type !== 'draw') { + // Ensure w and h exist in props (except for arrow, draw, line, text, note, and group shapes) + if (fixedRecord.type !== 'arrow' && fixedRecord.type !== 'draw' && fixedRecord.type !== 'line' && fixedRecord.type !== 'text' && fixedRecord.type !== 'note' && fixedRecord.type !== 'group') { if (fixedRecord.props.w === undefined) fixedRecord.props.w = 100 if (fixedRecord.props.h === undefined) fixedRecord.props.h = 100 } else if (fixedRecord.type === 'text') { // Text shapes only need w, not h if (fixedRecord.props.w === undefined) fixedRecord.props.w = 100 + } else if (fixedRecord.type === 'line') { + // Line shapes should NOT have w or h properties + if ('w' in fixedRecord.props) { + console.log(`๐Ÿ”ง FINAL FIX: Removing invalid w property from line shape ${fixedRecord.id}`) + delete fixedRecord.props.w + } + if ('h' in fixedRecord.props) { + console.log(`๐Ÿ”ง FINAL FIX: Removing invalid h property from line shape ${fixedRecord.id}`) + delete fixedRecord.props.h + } + + // Ensure line shapes have valid points + if (!fixedRecord.props.points || typeof fixedRecord.props.points !== 'object' || Array.isArray(fixedRecord.props.points)) { + console.log(`๐Ÿ”ง FINAL FIX: Creating default points for line shape ${fixedRecord.id}`) + fixedRecord.props.points = { + 'a1': { id: 'a1', index: 'a1' as any, x: 0, y: 0 }, + 'a2': { id: 'a2', index: 'a2' as any, x: 100, y: 0 } + } + } else { + // Validate points + const validPoints: Record = {} + let pointIndex = 0 + const indices = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10'] + + for (const [key, point] of Object.entries(fixedRecord.props.points)) { + if (point && typeof point === 'object' && + typeof (point as any).x === 'number' && + typeof (point as any).y === 'number' && + !isNaN((point as any).x) && !isNaN((point as any).y)) { + const index = indices[pointIndex] || `a${pointIndex + 1}` + validPoints[index] = { + id: index, + index: index as any, + x: (point as any).x, + y: (point as any).y + } + pointIndex++ + } + } + + if (Object.keys(validPoints).length === 0) { + fixedRecord.props.points = { + 'a1': { id: 'a1', index: 'a1' as any, x: 0, y: 0 }, + 'a2': { id: 'a2', index: 'a2' as any, x: 100, y: 0 } + } + } else if (Object.keys(validPoints).length === 1) { + const firstPoint = Object.values(validPoints)[0] + const secondIndex = indices[1] || 'a2' + validPoints[secondIndex] = { + id: secondIndex, + index: secondIndex as any, + x: firstPoint.x + 100, + y: firstPoint.y + } + fixedRecord.props.points = validPoints + } else { + fixedRecord.props.points = validPoints + } + } + + // Ensure other required line shape properties + if (!fixedRecord.props.color) fixedRecord.props.color = 'black' + if (!fixedRecord.props.dash) fixedRecord.props.dash = 'draw' + if (!fixedRecord.props.size) fixedRecord.props.size = 'm' + if (!fixedRecord.props.spline) fixedRecord.props.spline = 'line' + if (fixedRecord.props.scale === undefined || fixedRecord.props.scale === null) { + fixedRecord.props.scale = 1 + } + } else if (fixedRecord.type === 'note') { + // Note shapes should NOT have w or h properties, but DO need richText + if ('w' in fixedRecord.props) { + console.log(`๐Ÿ”ง FINAL FIX: Removing invalid w property from note shape ${fixedRecord.id}`) + delete fixedRecord.props.w + } + if ('h' in fixedRecord.props) { + console.log(`๐Ÿ”ง FINAL FIX: Removing invalid h property from note shape ${fixedRecord.id}`) + delete fixedRecord.props.h + } + // Note shapes REQUIRE richText property + if (!fixedRecord.props.richText || typeof fixedRecord.props.richText !== 'object') { + console.log(`๐Ÿ”ง FINAL FIX: Adding missing richText property for note shape ${fixedRecord.id}`) + fixedRecord.props.richText = { content: [], type: 'doc' } + } + } else if (fixedRecord.type === 'group') { + // Group shapes should NOT have w or h properties + if ('w' in fixedRecord.props) { + console.log(`๐Ÿ”ง FINAL FIX: Removing invalid w property from group shape ${fixedRecord.id}`) + delete fixedRecord.props.w + } + if ('h' in fixedRecord.props) { + console.log(`๐Ÿ”ง FINAL FIX: Removing invalid h property from group shape ${fixedRecord.id}`) + delete fixedRecord.props.h + } } } + // CRITICAL: Final safety check - ensure no geo shapes have w/h/geo at top level + if (fixedRecord.typeName === 'shape' && fixedRecord.type === 'geo') { + // Store values before removing from top level + const wValue = 'w' in fixedRecord ? (fixedRecord as any).w : undefined + const hValue = 'h' in fixedRecord ? (fixedRecord as any).h : undefined + const geoValue = 'geo' in fixedRecord ? (fixedRecord as any).geo : undefined + + // Create cleaned record without w/h/geo at top level + const cleaned: any = {} + for (const key in fixedRecord) { + if (key !== 'w' && key !== 'h' && key !== 'geo') { + cleaned[key] = (fixedRecord as any)[key] + } + } + + // Ensure props exists and move values there if needed + if (!cleaned.props) cleaned.props = {} + if (wValue !== undefined && (!('w' in cleaned.props) || cleaned.props.w === undefined)) { + cleaned.props.w = wValue + } + if (hValue !== undefined && (!('h' in cleaned.props) || cleaned.props.h === undefined)) { + cleaned.props.h = hValue + } + if (geoValue !== undefined && (!('geo' in cleaned.props) || cleaned.props.geo === undefined)) { + cleaned.props.geo = geoValue + } + + fixedRecord = cleaned as any + } + + // CRITICAL: Final safety check - ensure text shapes don't have props.text (TLDraw schema doesn't allow it) + // Text shapes should only use props.richText, not props.text + if (fixedRecord.typeName === 'shape' && fixedRecord.type === 'text' && fixedRecord.props && 'text' in fixedRecord.props) { + delete (fixedRecord.props as any).text + } + store.mergeRemoteChanges(() => { store.put([fixedRecord]) }) @@ -904,7 +1877,9 @@ export function useAutomergeStoreV2({ const invalidShapes = shapes.filter(shape => { const issues = [] if (!shape.props) issues.push('missing props') - if (shape.type !== 'arrow' && shape.type !== 'draw' && (!(shape.props as any)?.w || !(shape.props as any)?.h)) { + // Only check w/h for shapes that actually need them + const shapesWithoutWH = ['arrow', 'draw', 'text', 'note', 'line'] + if (!shapesWithoutWH.includes(shape.type) && (!(shape.props as any)?.w || !(shape.props as any)?.h)) { issues.push('missing w/h in props') } if ('w' in shape || 'h' in shape) { @@ -949,11 +1924,13 @@ export function useAutomergeStoreV2({ } if (shapes.length === 0) { - console.log("No store data found in Automerge document") + // Only log if debugging is needed + // console.log("No store data found in Automerge document") } } - console.log("Setting store status to synced-remote") + // Only log if debugging is needed + // console.log("Setting store status to synced-remote") setStoreWithStatus({ store, status: "synced-remote", diff --git a/src/automerge/useAutomergeSync.ts b/src/automerge/useAutomergeSync.ts index a236f63..db25d80 100644 --- a/src/automerge/useAutomergeSync.ts +++ b/src/automerge/useAutomergeSync.ts @@ -15,7 +15,7 @@ interface AutomergeSyncConfig { } } -export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus { +export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus & { handle: any | null } { const { uri, user } = config // Extract roomId from URI (e.g., "https://worker.com/connect/room123" -> "room123") @@ -96,7 +96,44 @@ export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus handle.change((doc) => { if (snapshotData.store) { - doc.store = snapshotData.store + // Pre-sanitize snapshot data to remove invalid properties + const sanitizedStore = { ...snapshotData.store } + let sanitizedCount = 0 + + Object.keys(sanitizedStore).forEach(key => { + const record = (sanitizedStore as any)[key] + if (record && record.typeName === 'shape') { + // Remove invalid properties from embed shapes (both custom Embed and default embed) + if ((record.type === 'Embed' || record.type === 'embed') && record.props) { + const invalidEmbedProps = ['doesResize', 'doesResizeHeight', 'richText'] + invalidEmbedProps.forEach(prop => { + if (prop in record.props) { + console.log(`๐Ÿ”ง Pre-sanitizing snapshot: Removing invalid prop '${prop}' from embed shape ${record.id}`) + delete record.props[prop] + sanitizedCount++ + } + }) + } + + // Remove invalid properties from text shapes + if (record.type === 'text' && record.props) { + const invalidTextProps = ['text', 'richText'] + invalidTextProps.forEach(prop => { + if (prop in record.props) { + console.log(`๐Ÿ”ง Pre-sanitizing snapshot: Removing invalid prop '${prop}' from text shape ${record.id}`) + delete record.props[prop] + sanitizedCount++ + } + }) + } + } + }) + + if (sanitizedCount > 0) { + console.log(`๐Ÿ”ง Pre-sanitized ${sanitizedCount} invalid properties from snapshot data`) + } + + doc.store = sanitizedStore console.log("Loaded snapshot store data into Automerge document:", { storeKeys: Object.keys(doc.store).length, shapeCount: Object.values(doc.store).filter((r: any) => r.typeName === 'shape').length, @@ -187,8 +224,8 @@ export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus // Return loading state while initializing if (isLoading || !handle) { - return { status: "loading" } + return { ...store, handle: null } } - return store + return { ...store, handle } } diff --git a/src/automerge/useAutomergeSyncRepo.ts b/src/automerge/useAutomergeSyncRepo.ts new file mode 100644 index 0000000..3ab03af --- /dev/null +++ b/src/automerge/useAutomergeSyncRepo.ts @@ -0,0 +1,168 @@ +import { useMemo, useEffect, useState, useCallback } from "react" +import { TLStoreSnapshot } from "@tldraw/tldraw" +import { CloudflareNetworkAdapter } from "./CloudflareAdapter" +import { useAutomergeStoreV2, useAutomergePresence } from "./useAutomergeStoreV2" +import { TLStoreWithStatus } from "@tldraw/tldraw" +import { Repo } from "@automerge/automerge-repo" + +interface AutomergeSyncConfig { + uri: string + assets?: any + shapeUtils?: any[] + bindingUtils?: any[] + user?: { + id: string + name: string + } +} + +export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus { + const { uri, user } = config + + // Extract roomId from URI (e.g., "https://worker.com/connect/room123" -> "room123") + const roomId = useMemo(() => { + const match = uri.match(/\/connect\/([^\/]+)$/) + return match ? match[1] : "default-room" + }, [uri]) + + // Extract worker URL from URI (remove /connect/roomId part) + const workerUrl = useMemo(() => { + return uri.replace(/\/connect\/.*$/, '') + }, [uri]) + + const [repo] = useState(() => new Repo({ + network: [new CloudflareNetworkAdapter(workerUrl, roomId)] + })) + const [handle, setHandle] = useState(null) + const [isLoading, setIsLoading] = useState(true) + + // Initialize Automerge document handle + useEffect(() => { + let mounted = true + + const initializeHandle = async () => { + try { + console.log("๐Ÿ”Œ Initializing Automerge Repo with NetworkAdapter") + + if (mounted) { + // Create a new document - Automerge will generate the proper document ID + // Force refresh to clear cache + const handle = repo.create() + + console.log("Created Automerge handle via Repo:", { + handleId: handle.documentId, + isReady: handle.isReady() + }) + + // Wait for the handle to be ready + await handle.whenReady() + + console.log("Automerge handle is ready:", { + hasDoc: !!handle.doc(), + docKeys: handle.doc() ? Object.keys(handle.doc()).length : 0 + }) + + setHandle(handle) + setIsLoading(false) + } + } catch (error) { + console.error("Error initializing Automerge handle:", error) + if (mounted) { + setIsLoading(false) + } + } + } + + initializeHandle() + + return () => { + mounted = false + } + }, [repo, roomId]) + + // Auto-save to Cloudflare on every change (with debouncing to prevent excessive calls) + useEffect(() => { + if (!handle) return + + let saveTimeout: NodeJS.Timeout + + const scheduleSave = () => { + // Clear existing timeout + if (saveTimeout) clearTimeout(saveTimeout) + + // Schedule save with a short debounce (500ms) to batch rapid changes + saveTimeout = setTimeout(async () => { + try { + // With Repo, we don't need manual saving - the NetworkAdapter handles sync + console.log("๐Ÿ” Automerge changes detected - NetworkAdapter will handle sync") + } catch (error) { + console.error('Error in change-triggered save:', error) + } + }, 500) + } + + // Listen for changes to the Automerge document + const changeHandler = (payload: any) => { + console.log('๐Ÿ” Automerge document changed:', { + hasPatches: !!payload.patches, + patchCount: payload.patches?.length || 0, + patches: payload.patches?.map((p: any) => ({ + action: p.action, + path: p.path, + value: p.value ? (typeof p.value === 'object' ? 'object' : p.value) : 'undefined' + })) + }) + scheduleSave() + } + + handle.on('change', changeHandler) + + return () => { + handle.off('change', changeHandler) + if (saveTimeout) clearTimeout(saveTimeout) + } + }, [handle]) + + // Get the store from the Automerge document + const store = useMemo(() => { + if (!handle?.doc()) { + return null + } + + const doc = handle.doc() + if (!doc.store) { + return null + } + + return doc.store + }, [handle]) + + // Get the store with status + const storeWithStatus = useMemo((): TLStoreWithStatus => { + if (!store) { + return { + status: isLoading ? 'loading' : 'not-synced', + connectionStatus: 'offline', + store: null + } + } + + return { + status: 'synced', + connectionStatus: 'online', + store + } + }, [store, isLoading]) + + // Get presence data (only when handle is ready) + const presence = useAutomergePresence({ + handle: handle || null, + store: store || null, + userMetadata: user || { userId: 'anonymous', name: 'Anonymous', color: '#000000' } + }) + + return { + ...storeWithStatus, + presence + } +} diff --git a/src/components/FathomMeetingsPanel.tsx b/src/components/FathomMeetingsPanel.tsx new file mode 100644 index 0000000..24ca7ea --- /dev/null +++ b/src/components/FathomMeetingsPanel.tsx @@ -0,0 +1,479 @@ +import React, { useState, useEffect } from 'react' +import { useEditor } from 'tldraw' +import { createShapeId } from 'tldraw' +import { WORKER_URL, LOCAL_WORKER_URL } from '../constants/workerUrl' + +interface FathomMeeting { + id: string + title: string + url: string + created_at: string + duration: number + summary?: { + markdown_formatted: string + } +} + +interface FathomMeetingsPanelProps { + onClose: () => void + shapeMode?: boolean +} + +export function FathomMeetingsPanel({ onClose, shapeMode = false }: FathomMeetingsPanelProps) { + const editor = useEditor() + const [apiKey, setApiKey] = useState('') + const [showApiKeyInput, setShowApiKeyInput] = useState(false) + const [meetings, setMeetings] = useState([]) + const [loading, setLoading] = useState(false) + const [error, setError] = useState(null) + + useEffect(() => { + // Check if API key is already stored + const storedApiKey = localStorage.getItem('fathom_api_key') + if (storedApiKey) { + setApiKey(storedApiKey) + fetchMeetings() + } else { + setShowApiKeyInput(true) + } + }, []) + + const fetchMeetings = async () => { + if (!apiKey) { + setError('Please enter your Fathom API key') + return + } + + setLoading(true) + setError(null) + + try { + // Try production worker first, fallback to local if needed + let response + try { + response = await fetch(`${WORKER_URL}/fathom/meetings`, { + headers: { + 'Authorization': `Bearer ${apiKey}`, + 'Content-Type': 'application/json' + } + }) + } catch (error) { + console.log('Production worker failed, trying local worker...') + response = await fetch(`${LOCAL_WORKER_URL}/fathom/meetings`, { + headers: { + 'Authorization': `Bearer ${apiKey}`, + 'Content-Type': 'application/json' + } + }) + } + + if (!response.ok) { + // Check if response is JSON + const contentType = response.headers.get('content-type') + if (contentType && contentType.includes('application/json')) { + const errorData = await response.json() as { error?: string } + setError(errorData.error || `HTTP ${response.status}: ${response.statusText}`) + } else { + setError(`HTTP ${response.status}: ${response.statusText}`) + } + return + } + + const data = await response.json() as { data?: FathomMeeting[] } + setMeetings(data.data || []) + } catch (error) { + console.error('Error fetching meetings:', error) + setError(`Failed to fetch meetings: ${(error as Error).message}`) + } finally { + setLoading(false) + } + } + + const saveApiKey = () => { + if (apiKey) { + localStorage.setItem('fathom_api_key', apiKey) + setShowApiKeyInput(false) + fetchMeetings() + } + } + + const addMeetingToCanvas = async (meeting: FathomMeeting) => { + try { + // Fetch full meeting details + let response + try { + response = await fetch(`${WORKER_URL}/fathom/meetings/${meeting.id}`, { + headers: { + 'Authorization': `Bearer ${apiKey}`, + 'Content-Type': 'application/json' + } + }) + } catch (error) { + console.log('Production worker failed, trying local worker...') + response = await fetch(`${LOCAL_WORKER_URL}/fathom/meetings/${meeting.id}`, { + headers: { + 'Authorization': `Bearer ${apiKey}`, + 'Content-Type': 'application/json' + } + }) + } + + if (!response.ok) { + setError(`Failed to fetch meeting details: ${response.status}`) + return + } + + const fullMeeting = await response.json() as any + + // Create Fathom transcript shape + const shapeId = createShapeId() + editor.createShape({ + id: shapeId, + type: 'FathomTranscript', + x: 100, + y: 100, + props: { + meetingId: fullMeeting.id || '', + meetingTitle: fullMeeting.title || '', + meetingUrl: fullMeeting.url || '', + summary: fullMeeting.default_summary?.markdown_formatted || '', + transcript: fullMeeting.transcript?.map((entry: any) => ({ + speaker: entry.speaker?.display_name || 'Unknown', + text: entry.text, + timestamp: entry.timestamp + })) || [], + actionItems: fullMeeting.action_items?.map((item: any) => ({ + text: item.text, + assignee: item.assignee, + dueDate: item.due_date + })) || [], + isExpanded: false, + showTranscript: true, + showActionItems: true, + } + }) + + onClose() + } catch (error) { + console.error('Error adding meeting to canvas:', error) + setError(`Failed to add meeting: ${(error as Error).message}`) + } + } + + const formatDate = (dateString: string) => { + return new Date(dateString).toLocaleDateString() + } + + const formatDuration = (seconds: number) => { + const minutes = Math.floor(seconds / 60) + const remainingSeconds = seconds % 60 + return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}` + } + + // If in shape mode, don't use modal overlay + const contentStyle: React.CSSProperties = shapeMode ? { + backgroundColor: 'white', + padding: '20px', + width: '100%', + height: '100%', + overflow: 'auto', + position: 'relative', + userSelect: 'text', + display: 'flex', + flexDirection: 'column', + } : { + backgroundColor: 'white', + borderRadius: '8px', + padding: '20px', + maxWidth: '600px', + maxHeight: '80vh', + width: '90%', + overflow: 'auto', + boxShadow: '0 4px 20px rgba(0, 0, 0, 0.15)', + position: 'relative', + zIndex: 10001, + userSelect: 'text' + } + + const content = ( +
shapeMode ? undefined : e.stopPropagation()}> +
+

+ ๐ŸŽฅ Fathom Meetings +

+ +
+ + {showApiKeyInput ? ( +
+

+ Enter your Fathom API key to access your meetings: +

+ setApiKey(e.target.value)} + placeholder="Your Fathom API key" + style={{ + width: '100%', + padding: '8px', + border: '1px solid #ccc', + borderRadius: '4px', + marginBottom: '10px', + position: 'relative', + zIndex: 10002, + pointerEvents: 'auto', + userSelect: 'text', + cursor: 'text' + }} + /> +
+ + +
+
+ ) : ( + <> +
+ + +
+ + {error && ( +
+ {error} +
+ )} + +
+ {meetings.length === 0 ? ( +

+ No meetings found. Click "Refresh Meetings" to load your Fathom meetings. +

+ ) : ( + meetings.map((meeting) => ( +
+
+
+

+ {meeting.title} +

+
+
๐Ÿ“… {formatDate(meeting.created_at)}
+
โฑ๏ธ Duration: {formatDuration(meeting.duration)}
+
+ {meeting.summary && ( +
+ Summary: {meeting.summary.markdown_formatted.substring(0, 100)}... +
+ )} +
+ +
+
+ )) + )} +
+ + )} +
+ ) + + // If in shape mode, return content directly + if (shapeMode) { + return content + } + + // Otherwise, return with modal overlay + return ( +
+ {content} +
+ ) +} + + + + + + + + + + + + + + + + diff --git a/src/components/HolonBrowser.tsx b/src/components/HolonBrowser.tsx new file mode 100644 index 0000000..bb1cd9c --- /dev/null +++ b/src/components/HolonBrowser.tsx @@ -0,0 +1,370 @@ +import React, { useState, useEffect, useRef } from 'react' +import { holosphereService, HoloSphereService, HolonData, HolonLens } from '@/lib/HoloSphereService' +import * as h3 from 'h3-js' + +interface HolonBrowserProps { + isOpen: boolean + onClose: () => void + onSelectHolon: (holonData: HolonData) => void + shapeMode?: boolean +} + +interface HolonInfo { + id: string + name: string + description?: string + latitude: number + longitude: number + resolution: number + resolutionName: string + data: Record + lastUpdated: number +} + +export function HolonBrowser({ isOpen, onClose, onSelectHolon, shapeMode = false }: HolonBrowserProps) { + const [holonId, setHolonId] = useState('') + const [holonInfo, setHolonInfo] = useState(null) + const [isLoading, setIsLoading] = useState(false) + const [error, setError] = useState(null) + const [lenses, setLenses] = useState([]) + const [selectedLens, setSelectedLens] = useState('') + const [lensData, setLensData] = useState(null) + const [isLoadingData, setIsLoadingData] = useState(false) + const inputRef = useRef(null) + + useEffect(() => { + if (isOpen && inputRef.current) { + inputRef.current.focus() + } + }, [isOpen]) + + const handleSearchHolon = async () => { + if (!holonId.trim()) { + setError('Please enter a Holon ID') + return + } + + setIsLoading(true) + setError(null) + setHolonInfo(null) + + try { + // Validate that the holonId is a valid H3 index + if (!h3.isValidCell(holonId)) { + throw new Error('Invalid H3 cell ID') + } + + // Get holon information + const resolution = h3.getResolution(holonId) + const [lat, lng] = h3.cellToLatLng(holonId) + + // Try to get metadata from the holon + let metadata = null + try { + metadata = await holosphereService.getData(holonId, 'metadata') + } catch (error) { + console.log('No metadata found for holon') + } + + // Get available lenses by trying to fetch data from common lens types + // Use the improved categories from HolonShapeUtil + const commonLenses = [ + 'active_users', 'users', 'rankings', 'stats', 'tasks', 'progress', + 'events', 'activities', 'items', 'shopping', 'active_items', + 'proposals', 'offers', 'requests', 'checklists', 'roles', + 'general', 'metadata', 'environment', 'social', 'economic', 'cultural', 'data' + ] + const availableLenses: string[] = [] + + for (const lens of commonLenses) { + try { + // Use getDataWithWait for better Gun data retrieval (shorter timeout for browser) + const data = await holosphereService.getDataWithWait(holonId, lens, 1000) + if (data && (Array.isArray(data) ? data.length > 0 : Object.keys(data).length > 0)) { + availableLenses.push(lens) + console.log(`โœ“ Found lens: ${lens} with ${Object.keys(data).length} keys`) + } + } catch (error) { + // Lens doesn't exist or is empty, skip + } + } + + // If no lenses found, add 'general' as default + if (availableLenses.length === 0) { + availableLenses.push('general') + } + + const holonData: HolonInfo = { + id: holonId, + name: metadata?.name || `Holon ${holonId.slice(-8)}`, + description: metadata?.description || '', + latitude: lat, + longitude: lng, + resolution: resolution, + resolutionName: HoloSphereService.getResolutionName(resolution), + data: {}, + lastUpdated: metadata?.lastUpdated || Date.now() + } + + setHolonInfo(holonData) + setLenses(availableLenses) + setSelectedLens(availableLenses[0]) + + } catch (error) { + console.error('Error searching holon:', error) + setError(`Failed to load holon: ${error instanceof Error ? error.message : 'Unknown error'}`) + } finally { + setIsLoading(false) + } + } + + const handleLoadLensData = async (lens: string) => { + if (!holonInfo) return + + setIsLoadingData(true) + try { + // Use getDataWithWait for better Gun data retrieval + const data = await holosphereService.getDataWithWait(holonInfo.id, lens, 2000) + setLensData(data) + console.log(`๐Ÿ“Š Loaded lens data for ${lens}:`, data) + } catch (error) { + console.error('Error loading lens data:', error) + setLensData(null) + } finally { + setIsLoadingData(false) + } + } + + useEffect(() => { + if (selectedLens && holonInfo) { + handleLoadLensData(selectedLens) + } + }, [selectedLens, holonInfo]) + + const handleSelectHolon = () => { + if (holonInfo) { + const holonData: HolonData = { + id: holonInfo.id, + name: holonInfo.name, + description: holonInfo.description, + latitude: holonInfo.latitude, + longitude: holonInfo.longitude, + resolution: holonInfo.resolution, + data: holonInfo.data, + timestamp: holonInfo.lastUpdated + } + onSelectHolon(holonData) + onClose() + } + } + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === 'Enter') { + handleSearchHolon() + } else if (e.key === 'Escape') { + onClose() + } + } + + if (!isOpen) return null + + const contentStyle: React.CSSProperties = shapeMode ? { + width: '100%', + height: '100%', + overflow: 'auto', + padding: '20px', + position: 'relative', + display: 'flex', + flexDirection: 'column', + } : {} + + const renderContent = () => ( + <> + {!shapeMode && ( +
+
+

๐ŸŒ Holon Browser

+ +
+

+ Enter a Holon ID to browse its data and import it to your canvas +

+
+ )} + +
+ {/* Holon ID Input */} +
+ +
+ setHolonId(e.target.value)} + onKeyDown={handleKeyDown} + placeholder="e.g., 1002848305066" + className="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 z-[10001] relative" + disabled={isLoading} + style={{ zIndex: 10001 }} + /> + +
+ {error && ( +

{error}

+ )} +
+ + {/* Holon Information */} + {holonInfo && ( +
+

+ ๐Ÿ“ {holonInfo.name} +

+ +
+
+

Coordinates

+

+ {holonInfo.latitude.toFixed(6)}, {holonInfo.longitude.toFixed(6)} +

+
+
+

Resolution

+

+ {holonInfo.resolutionName} (Level {holonInfo.resolution}) +

+
+
+

Holon ID

+

{holonInfo.id}

+
+
+

Last Updated

+

+ {new Date(holonInfo.lastUpdated).toLocaleString()} +

+
+
+ + {holonInfo.description && ( +
+

Description

+

{holonInfo.description}

+
+ )} + + {/* Available Lenses */} +
+

Available Data Categories

+
+ {lenses.map((lens) => ( + + ))} +
+
+ + {/* Lens Data */} + {selectedLens && ( +
+
+

+ Data: {selectedLens} +

+ {isLoadingData && ( + Loading... + )} +
+ + {lensData && ( +
+
+                      {JSON.stringify(lensData, null, 2)}
+                    
+
+ )} + + {!lensData && !isLoadingData && ( +

+ No data available for this category +

+ )} +
+ )} + + {/* Action Buttons */} +
+ + +
+
+ )} +
+ + ) + + // If in shape mode, return content without modal overlay + if (shapeMode) { + return ( +
+ {renderContent()} +
+ ) + } + + // Otherwise, return with modal overlay + return ( +
+
e.stopPropagation()} + > + {renderContent()} +
+
+ ) +} diff --git a/src/components/ObsidianVaultBrowser.tsx b/src/components/ObsidianVaultBrowser.tsx index 1e920e4..e49f13e 100644 --- a/src/components/ObsidianVaultBrowser.tsx +++ b/src/components/ObsidianVaultBrowser.tsx @@ -1,6 +1,8 @@ -import React, { useState, useEffect, useMemo } from 'react' -import { ObsidianImporter, ObsidianObsNote, ObsidianVault } from '@/lib/obsidianImporter' -import { useAuth } from '@/context/AuthContext' +import React, { useState, useEffect, useMemo, useContext, useRef } from 'react' +import { ObsidianImporter, ObsidianObsNote, ObsidianVault, FolderNode, ObsidianVaultRecord } from '@/lib/obsidianImporter' +import { AuthContext } from '@/context/AuthContext' +import { useEditor } from '@tldraw/tldraw' +import { useAutomergeHandle } from '@/context/AutomergeHandleContext' interface ObsidianVaultBrowserProps { onObsNoteSelect: (obs_note: ObsidianObsNote) => void @@ -9,6 +11,7 @@ interface ObsidianVaultBrowserProps { className?: string autoOpenFolderPicker?: boolean showVaultBrowser?: boolean + shapeMode?: boolean // When true, renders without modal overlay for use in shape } export const ObsidianVaultBrowser: React.FC = ({ @@ -17,9 +20,22 @@ export const ObsidianVaultBrowser: React.FC = ({ onClose, className = '', autoOpenFolderPicker = false, - showVaultBrowser = true + showVaultBrowser = true, + shapeMode = false }) => { - const { session, updateSession } = useAuth() + // Safely get auth context - use useContext directly to avoid throwing error + // This allows the component to work even when used outside AuthProvider (e.g., during SVG export) + const authContext = useContext(AuthContext) + const fallbackSession = { + username: '', + authed: false, + loading: false, + backupCreated: null, + obsidianVaultPath: undefined, + obsidianVaultName: undefined + } + const session = authContext?.session || fallbackSession + const updateSession = authContext?.updateSession || (() => {}) const [importer] = useState(() => new ObsidianImporter()) const [vault, setVault] = useState(null) const [searchQuery, setSearchQuery] = useState('') @@ -31,20 +47,154 @@ export const ObsidianVaultBrowser: React.FC = ({ }) const [error, setError] = useState(null) const [selectedNotes, setSelectedNotes] = useState>(new Set()) - const [viewMode, setViewMode] = useState<'grid' | 'list'>('list') const [showVaultInput, setShowVaultInput] = useState(false) const [vaultPath, setVaultPath] = useState('') const [inputMethod, setInputMethod] = useState<'folder' | 'url' | 'quartz'>('folder') const [showFolderReselect, setShowFolderReselect] = useState(false) const [isLoadingVault, setIsLoadingVault] = useState(false) const [hasLoadedOnce, setHasLoadedOnce] = useState(false) + const [folderTree, setFolderTree] = useState(null) + const [expandedFolders, setExpandedFolders] = useState>(new Set()) + const [selectedFolder, setSelectedFolder] = useState(null) + const [viewMode, setViewMode] = useState<'grid' | 'list' | 'tree'>('tree') + + // Track previous vault path/name to prevent unnecessary reloads + const previousVaultPathRef = useRef(session.obsidianVaultPath) + const previousVaultNameRef = useRef(session.obsidianVaultName) + + const editor = useEditor() + const automergeHandle = useAutomergeHandle() // Initialize debounced search query to match search query useEffect(() => { setDebouncedSearchQuery(searchQuery) }, []) - // Load vault on component mount - only once per component lifecycle + // Update folder tree when vault changes + useEffect(() => { + if (vault && vault.folderTree) { + setFolderTree(vault.folderTree) + // Expand root folder by default + setExpandedFolders(new Set([''])) + } + }, [vault]) + + // Save vault to Automerge store + const saveVaultToAutomerge = (vault: ObsidianVault) => { + if (!automergeHandle) { + console.warn('โš ๏ธ Automerge handle not available, saving to localStorage only') + try { + const vaultRecord = importer.vaultToRecord(vault) + localStorage.setItem(`obsidian_vault_cache:${vault.name}`, JSON.stringify({ + ...vaultRecord, + lastImported: vaultRecord.lastImported instanceof Date ? vaultRecord.lastImported.toISOString() : vaultRecord.lastImported + })) + console.log('๐Ÿ”ง Saved vault to localStorage (Automerge handle not available):', vaultRecord.id) + } catch (localStorageError) { + console.warn('โš ๏ธ Could not save vault to localStorage:', localStorageError) + } + return + } + + try { + const vaultRecord = importer.vaultToRecord(vault) + + // Save directly to Automerge, bypassing TLDraw store validation + // This allows us to save custom record types like obsidian_vault + automergeHandle.change((doc: any) => { + // Ensure doc.store exists + if (!doc.store) { + doc.store = {} + } + + // Save the vault record directly to Automerge store + // Convert Date to ISO string for serialization + const recordToSave = { + ...vaultRecord, + lastImported: vaultRecord.lastImported instanceof Date + ? vaultRecord.lastImported.toISOString() + : vaultRecord.lastImported + } + + doc.store[vaultRecord.id] = recordToSave + }) + + console.log('๐Ÿ”ง Saved vault to Automerge:', vaultRecord.id) + + // Also save to localStorage as a backup + try { + localStorage.setItem(`obsidian_vault_cache:${vault.name}`, JSON.stringify({ + ...vaultRecord, + lastImported: vaultRecord.lastImported instanceof Date ? vaultRecord.lastImported.toISOString() : vaultRecord.lastImported + })) + console.log('๐Ÿ”ง Saved vault to localStorage as backup:', vaultRecord.id) + } catch (localStorageError) { + console.warn('โš ๏ธ Could not save vault to localStorage:', localStorageError) + } + } catch (error) { + console.error('โŒ Error saving vault to Automerge:', error) + // Don't throw - allow vault loading to continue even if saving fails + // Try localStorage as fallback + try { + const vaultRecord = importer.vaultToRecord(vault) + localStorage.setItem(`obsidian_vault_cache:${vault.name}`, JSON.stringify({ + ...vaultRecord, + lastImported: vaultRecord.lastImported instanceof Date ? vaultRecord.lastImported.toISOString() : vaultRecord.lastImported + })) + console.log('๐Ÿ”ง Saved vault to localStorage as fallback:', vaultRecord.id) + } catch (localStorageError) { + console.warn('โš ๏ธ Could not save vault to localStorage:', localStorageError) + } + } + } + + // Load vault from Automerge store + const loadVaultFromAutomerge = (vaultName: string): ObsidianVault | null => { + // Try loading from Automerge first + if (automergeHandle) { + try { + const doc = automergeHandle.doc() + if (doc && doc.store) { + const vaultId = `obsidian_vault:${vaultName}` + const vaultRecord = doc.store[vaultId] as ObsidianVaultRecord | undefined + + if (vaultRecord && vaultRecord.typeName === 'obsidian_vault') { + console.log('๐Ÿ”ง Loaded vault from Automerge:', vaultId) + // Convert date string back to Date object if needed + const recordCopy = JSON.parse(JSON.stringify(vaultRecord)) + if (typeof recordCopy.lastImported === 'string') { + recordCopy.lastImported = new Date(recordCopy.lastImported) + } + return importer.recordToVault(recordCopy) + } + } + } catch (error) { + console.warn('โš ๏ธ Could not load vault from Automerge:', error) + } + } + + // Try localStorage as fallback + try { + const cached = localStorage.getItem(`obsidian_vault_cache:${vaultName}`) + if (cached) { + const vaultRecord = JSON.parse(cached) as ObsidianVaultRecord + if (vaultRecord && vaultRecord.typeName === 'obsidian_vault') { + console.log('๐Ÿ”ง Loaded vault from localStorage cache:', vaultName) + // Convert date string back to Date object + if (typeof vaultRecord.lastImported === 'string') { + vaultRecord.lastImported = new Date(vaultRecord.lastImported) + } + return importer.recordToVault(vaultRecord) + } + } + } catch (e) { + console.warn('โš ๏ธ Could not load vault from localStorage:', e) + } + + return null + } + + // Load vault on component mount - prioritize user's configured vault from session useEffect(() => { // Prevent multiple loads if already loading or already loaded once if (isLoadingVault || hasLoadedOnce) { @@ -52,7 +202,7 @@ export const ObsidianVaultBrowser: React.FC = ({ return } - console.log('๐Ÿ”ง ObsidianVaultBrowser: Component mounted, loading vault...') + console.log('๐Ÿ”ง ObsidianVaultBrowser: Component mounted, checking user identity for vault...') console.log('๐Ÿ”ง Current session vault data:', { path: session.obsidianVaultPath, name: session.obsidianVaultName, @@ -60,9 +210,25 @@ export const ObsidianVaultBrowser: React.FC = ({ username: session.username }) - // Try to load from stored vault path first + // FIRST PRIORITY: Try to load from user's configured vault in session (user identity) if (session.obsidianVaultPath && session.obsidianVaultPath !== 'folder-selected') { - console.log('๐Ÿ”ง Loading vault from stored path:', session.obsidianVaultPath) + console.log('โœ… Found configured vault in user identity:', session.obsidianVaultPath) + console.log('๐Ÿ”ง Loading vault from user identity...') + + // First try to load from Automerge cache for faster loading + if (session.obsidianVaultName) { + const cachedVault = loadVaultFromAutomerge(session.obsidianVaultName) + if (cachedVault) { + console.log('โœ… Loaded vault from Automerge cache') + setVault(cachedVault) + setIsLoading(false) + setHasLoadedOnce(true) + return + } + } + + // If not in cache, load from source (Quartz URL or local path) + console.log('๐Ÿ”ง Loading vault from source:', session.obsidianVaultPath) loadVault(session.obsidianVaultPath) } else if (session.obsidianVaultPath === 'folder-selected' && session.obsidianVaultName) { console.log('๐Ÿ”ง Vault was previously selected via folder picker, showing reselect interface') @@ -72,15 +238,33 @@ export const ObsidianVaultBrowser: React.FC = ({ setIsLoading(false) setHasLoadedOnce(true) } else { - console.log('๐Ÿ”ง No vault configured, showing empty state...') + console.log('โš ๏ธ No vault configured in user identity, showing empty state...') setVault(null) setIsLoading(false) setHasLoadedOnce(true) } }, []) // Remove dependencies to ensure this only runs once on mount - // Handle session changes only if we haven't loaded yet + // Handle session changes only if we haven't loaded yet AND values actually changed useEffect(() => { + // Check if values actually changed (not just object reference) + const vaultPathChanged = previousVaultPathRef.current !== session.obsidianVaultPath + const vaultNameChanged = previousVaultNameRef.current !== session.obsidianVaultName + + // If vault is already loaded and values haven't changed, don't do anything + if (hasLoadedOnce && !vaultPathChanged && !vaultNameChanged) { + return // Already loaded and nothing changed, no need to reload + } + + // Update refs to current values + previousVaultPathRef.current = session.obsidianVaultPath + previousVaultNameRef.current = session.obsidianVaultName + + // Only proceed if values actually changed and we haven't loaded yet + if (!vaultPathChanged && !vaultNameChanged) { + return // Values haven't changed, no need to reload + } + if (hasLoadedOnce || isLoadingVault) { return // Don't reload if we've already loaded or are currently loading } @@ -105,14 +289,14 @@ export const ObsidianVaultBrowser: React.FC = ({ } }, [autoOpenFolderPicker]) - // Reset loading state when component is closed + // Reset loading state when component is closed (but not in shape mode) useEffect(() => { - if (!showVaultBrowser) { - // Reset states when component is closed + if (!showVaultBrowser && !shapeMode) { + // Reset states when component is closed (only in modal mode, not shape mode) setHasLoadedOnce(false) setIsLoadingVault(false) } - }, [showVaultBrowser]) + }, [showVaultBrowser, shapeMode]) // Debounce search query for better performance @@ -168,6 +352,9 @@ export const ObsidianVaultBrowser: React.FC = ({ obsidianVaultName: loadedVault.name }) console.log('๐Ÿ”ง Quartz vault saved to session successfully') + + // Save vault to Automerge for persistence + saveVaultToAutomerge(loadedVault) } else { // Load from local directory console.log('๐Ÿ”ง Loading vault from local directory:', path) @@ -183,6 +370,9 @@ export const ObsidianVaultBrowser: React.FC = ({ obsidianVaultName: loadedVault.name }) console.log('๐Ÿ”ง Vault saved to session successfully') + + // Save vault to Automerge for persistence + saveVaultToAutomerge(loadedVault) } } else { // No vault configured - show empty state @@ -207,65 +397,94 @@ export const ObsidianVaultBrowser: React.FC = ({ } const handleVaultPathSubmit = async () => { - if (vaultPath.trim()) { - if (inputMethod === 'quartz') { - // Handle Quartz URL - try { - setIsLoading(true) - setError(null) - const loadedVault = await importer.importFromQuartzUrl(vaultPath.trim()) - setVault(loadedVault) - setShowVaultInput(false) - setShowFolderReselect(false) - - // Save Quartz vault to session - console.log('๐Ÿ”ง Saving Quartz vault to session:', { - path: vaultPath.trim(), - name: loadedVault.name - }) - updateSession({ - obsidianVaultPath: vaultPath.trim(), - obsidianVaultName: loadedVault.name - }) - } catch (error) { - console.error('Error loading Quartz vault:', error) - setError(error instanceof Error ? error.message : 'Failed to load Quartz vault') - } finally { - setIsLoading(false) - } - } else { - // Handle regular vault path - loadVault(vaultPath.trim()) + if (!vaultPath.trim()) { + setError('Please enter a vault path or URL') + return + } + + console.log('๐Ÿ“ Submitting vault path:', vaultPath.trim(), 'Method:', inputMethod) + + if (inputMethod === 'quartz') { + // Handle Quartz URL + try { + setIsLoading(true) + setError(null) + const loadedVault = await importer.importFromQuartzUrl(vaultPath.trim()) + setVault(loadedVault) + setShowVaultInput(false) + setShowFolderReselect(false) + + // Save Quartz vault to user identity (session) + console.log('๐Ÿ”ง Saving Quartz vault to user identity:', { + path: vaultPath.trim(), + name: loadedVault.name + }) + updateSession({ + obsidianVaultPath: vaultPath.trim(), + obsidianVaultName: loadedVault.name + }) + } catch (error) { + console.error('โŒ Error loading Quartz vault:', error) + setError(error instanceof Error ? error.message : 'Failed to load Quartz vault') + } finally { + setIsLoading(false) } + } else { + // Handle regular vault path (local folder or URL) + loadVault(vaultPath.trim()) } } const handleFolderPicker = async () => { - if ('showDirectoryPicker' in window) { - try { - const loadedVault = await importer.importFromFileSystem() - setVault(loadedVault) - setShowVaultInput(false) - setShowFolderReselect(false) - // Note: We can't get the actual path from importFromFileSystem, - // but we can save a flag that a folder was selected - console.log('๐Ÿ”ง Saving folder-selected vault to session:', { - path: 'folder-selected', - name: loadedVault.name - }) - updateSession({ - obsidianVaultPath: 'folder-selected', - obsidianVaultName: loadedVault.name - }) - console.log('๐Ÿ”ง Folder-selected vault saved to session successfully') - } catch (err) { - console.error('Failed to load vault:', err) + console.log('๐Ÿ“ Folder picker button clicked') + + if (!('showDirectoryPicker' in window)) { + setError('File System Access API is not supported in this browser. Please use "Enter Path" instead.') + setShowVaultInput(true) + return + } + + try { + setIsLoading(true) + setError(null) + console.log('๐Ÿ“ Opening directory picker...') + + const loadedVault = await importer.importFromFileSystem() + console.log('โœ… Vault loaded from folder picker:', loadedVault.name) + + setVault(loadedVault) + setShowVaultInput(false) + setShowFolderReselect(false) + + // Note: We can't get the actual path from importFromFileSystem, + // but we can save a flag that a folder was selected + console.log('๐Ÿ”ง Saving folder-selected vault to user identity:', { + path: 'folder-selected', + name: loadedVault.name + }) + updateSession({ + obsidianVaultPath: 'folder-selected', + obsidianVaultName: loadedVault.name + }) + console.log('โœ… Folder-selected vault saved to user identity successfully') + + // Save vault to Automerge for persistence + saveVaultToAutomerge(loadedVault) + } catch (err) { + console.error('โŒ Failed to load vault from folder picker:', err) + if ((err as any).name === 'AbortError') { + // User cancelled the folder picker + console.log('๐Ÿ“ User cancelled folder picker') + setError(null) // Don't show error for cancellation + } else { setError('Failed to load Obsidian vault. Please try again.') } + } finally { + setIsLoading(false) } } - // Filter obs_notes based on search query + // Filter obs_notes based on search query and folder selection const filteredObsNotes = useMemo(() => { if (!vault) return [] @@ -287,16 +506,28 @@ export const ObsidianVaultBrowser: React.FC = ({ ) ) } - // If no search query, show all notes (obs_notes remains unchanged) + + // Filter by selected folder if in tree view + if (viewMode === 'tree' && selectedFolder !== null && folderTree) { + const folder = importer.findFolderByPath(folderTree, selectedFolder) + if (folder) { + const folderNotes = importer.getAllNotesFromTree(folder) + obs_notes = obs_notes.filter(note => folderNotes.some(folderNote => folderNote.id === note.id)) + } + } else if (viewMode === 'tree' && selectedFolder === null) { + // In tree view but no folder selected, show all notes + // This allows users to see all notes when no specific folder is selected + } // Debug logging console.log('Search query:', debouncedSearchQuery) + console.log('View mode:', viewMode) + console.log('Selected folder:', selectedFolder) console.log('Total notes:', vault.obs_notes.length) console.log('Filtered notes:', obs_notes.length) - console.log('Showing all notes:', !debouncedSearchQuery || !debouncedSearchQuery.trim()) return obs_notes - }, [vault, debouncedSearchQuery]) + }, [vault, debouncedSearchQuery, viewMode, selectedFolder, folderTree, importer]) // Listen for trigger-obsnote-creation event from CustomToolbar useEffect(() => { @@ -379,6 +610,45 @@ export const ObsidianVaultBrowser: React.FC = ({ return content || 'No content preview available' } + // Helper function to get file path, checking session for quartz link if blank + const getFilePath = (obs_note: ObsidianObsNote): string => { + // If filePath exists and is not blank, use it + if (obs_note.filePath && obs_note.filePath.trim() !== '') { + if (obs_note.filePath.startsWith('http')) { + try { + return new URL(obs_note.filePath).pathname.replace(/^\//, '') || 'Home' + } catch (e) { + return obs_note.filePath + } + } + return obs_note.filePath + } + + // If filePath is blank, check session for quartz link (user API) + if (session.obsidianVaultPath && + session.obsidianVaultPath !== 'folder-selected' && + (session.obsidianVaultPath.startsWith('http') || + session.obsidianVaultPath.includes('quartz') || + session.obsidianVaultPath.includes('.xyz') || + session.obsidianVaultPath.includes('.com'))) { + // Construct file path from quartz URL and note title/ID + try { + const baseUrl = new URL(session.obsidianVaultPath) + // Use note title or ID to construct a path + const notePath = obs_note.title || obs_note.id || 'Untitled' + // Clean up the note path to make it URL-friendly + const cleanPath = notePath.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase() + return `${baseUrl.hostname}${baseUrl.pathname}/${cleanPath}` + } catch (e) { + // If URL parsing fails, just return the vault path + return session.obsidianVaultPath + } + } + + // If no quartz link found in session, return a fallback based on note info + return obs_note.title || obs_note.id || 'Untitled' + } + // Helper function to highlight search matches const highlightSearchMatches = (text: string, query: string): string => { if (!query.trim()) return text @@ -428,6 +698,58 @@ export const ObsidianVaultBrowser: React.FC = ({ setSelectedNotes(new Set()) } + // Folder management functions + const toggleFolderExpansion = (folderPath: string) => { + const newExpanded = new Set(expandedFolders) + if (newExpanded.has(folderPath)) { + newExpanded.delete(folderPath) + } else { + newExpanded.add(folderPath) + } + setExpandedFolders(newExpanded) + } + + const selectFolder = (folderPath: string) => { + setSelectedFolder(folderPath) + } + + const getNotesFromFolder = (folder: FolderNode): ObsidianObsNote[] => { + if (!folder) return [] + + let notes = [...folder.notes] + + // If folder is expanded, include notes from subfolders + if (expandedFolders.has(folder.path)) { + folder.children.forEach(child => { + notes.push(...getNotesFromFolder(child)) + }) + } + + return notes + } + + + const handleDisconnectVault = () => { + // Clear the vault from session + updateSession({ + obsidianVaultPath: undefined, + obsidianVaultName: undefined + }) + + // Reset component state + setVault(null) + setSearchQuery('') + setDebouncedSearchQuery('') + setSelectedNotes(new Set()) + setShowVaultInput(false) + setShowFolderReselect(false) + setError(null) + setHasLoadedOnce(false) + setIsLoadingVault(false) + + console.log('๐Ÿ”ง Vault disconnected successfully') + } + const handleBackdropClick = (e: React.MouseEvent) => { // Only close if clicking on the backdrop, not on the modal content if (e.target === e.currentTarget) { @@ -519,10 +841,26 @@ export const ObsidianVaultBrowser: React.FC = ({

Load Obsidian Vault

Choose how you'd like to load your Obsidian vault:

- -
@@ -603,12 +941,206 @@ export const ObsidianVaultBrowser: React.FC = ({ ) } - return ( -
-
- + // Helper function to check if a folder has content (notes or subfolders with content) + const hasContent = (folder: FolderNode): boolean => { + if (folder.notes.length > 0) return true + return folder.children.some(child => hasContent(child)) + } + + // Folder tree component - skips Root and content folders, shows only files from content + const renderFolderTree = (folder: FolderNode, level: number = 0) => { + if (!folder) return null + + // Skip Root folder - look for content folder inside it + if (folder.name === 'Root') { + // Find the "content" folder + const contentFolder = folder.children.find(child => child.name === 'content' || child.name.toLowerCase() === 'content') + + if (contentFolder) { + // Skip both Root and content folders, render content folder's children and notes directly + return ( +
+ {contentFolder.children + .filter(child => hasContent(child)) + .map(child => renderFolderTree(child, level))} + {contentFolder.notes.map(note => ( +
{ + e.stopPropagation() + handleObsNoteToggle(note) + }} + > + ๐Ÿ“„ + {getDisplayTitle(note)} +
+ ))} +
+ ) + } else { + // No content folder found, render root's children (excluding root itself) + return ( +
+ {folder.children + .filter(child => hasContent(child) && child.name !== 'content') + .map(child => renderFolderTree(child, level))} + {folder.notes.map(note => ( +
{ + e.stopPropagation() + handleObsNoteToggle(note) + }} + > + ๐Ÿ“„ + {getDisplayTitle(note)} +
+ ))} +
+ ) + } + } + + // Skip "content" folder - render its children and notes directly + if (folder.name === 'content' || folder.name.toLowerCase() === 'content') { + return ( +
+ {folder.children + .filter(child => hasContent(child)) + .map(child => renderFolderTree(child, level))} + {folder.notes.map(note => ( +
{ + e.stopPropagation() + handleObsNoteToggle(note) + }} + > + ๐Ÿ“„ + {getDisplayTitle(note)} +
+ ))} +
+ ) + } + + // Render normal folders (not Root or content) + const isExpanded = expandedFolders.has(folder.path) + const isSelected = selectedFolder === folder.path + const hasChildren = folder.children.length > 0 || folder.notes.length > 0 + + return ( +
+
selectFolder(folder.path)} + > + {hasChildren && ( + + )} + ๐Ÿ“ + {folder.name} + + ({folder.notes.length + folder.children.reduce((acc, child) => acc + child.notes.length, 0)}) + +
+ + {isExpanded && ( +
+ {folder.children + .filter(child => hasContent(child) && child.name !== 'content') + .map(child => renderFolderTree(child, level + 1))} + {folder.notes.map(note => ( +
{ + e.stopPropagation() + handleObsNoteToggle(note) + }} + > + ๐Ÿ“„ + {getDisplayTitle(note)} +
+ ))} +
+ )} +
+ ) + } + + // Shape mode: render without modal overlay + if (shapeMode) { + return ( +
{ + // Only stop propagation for interactive elements (buttons, inputs, note items, etc.) + const target = e.target as HTMLElement + const isInteractive = target.tagName === 'BUTTON' || + target.tagName === 'INPUT' || + target.tagName === 'TEXTAREA' || + target.tagName === 'SELECT' || + target.closest('button') || + target.closest('input') || + target.closest('textarea') || + target.closest('select') || + target.closest('[role="button"]') || + target.closest('a') || + target.closest('.note-item') || // Obsidian note items in list view + target.closest('.note-card') // Obsidian note cards in grid/list view + if (isInteractive) { + e.stopPropagation() + } + // Don't stop propagation for white space - let tldraw handle dragging + }} + onPointerDown={(e) => { + // Only stop propagation for interactive elements to allow tldraw to handle dragging on white space + const target = e.target as HTMLElement + const isInteractive = target.tagName === 'BUTTON' || + target.tagName === 'INPUT' || + target.tagName === 'TEXTAREA' || + target.tagName === 'SELECT' || + target.closest('button') || + target.closest('input') || + target.closest('textarea') || + target.closest('select') || + target.closest('[role="button"]') || + target.closest('a') || + target.closest('.note-item') || // Obsidian note items in list view + target.closest('.note-card') // Obsidian note cards in grid/list view + if (isInteractive) { + e.stopPropagation() + } + // Don't stop propagation for white space - let tldraw handle dragging + }} + style={{ + width: '100%', + height: '100%', + overflow: 'hidden', + display: 'flex', + flexDirection: 'column', + pointerEvents: 'auto' + }} + > +
+ {/* Close button removed - using StandardizedToolWrapper header instead */}

{vault ? `Obsidian Vault: ${vault.name}` : 'No Obsidian Vault Connected'} @@ -667,6 +1199,13 @@ export const ObsidianVaultBrowser: React.FC = ({
+
+
@@ -726,7 +1272,19 @@ export const ObsidianVaultBrowser: React.FC = ({
- {filteredObsNotes.length === 0 ? ( + {viewMode === 'tree' ? ( +
+ {folderTree ? ( +
+ {renderFolderTree(folderTree)} +
+ ) : ( +
+

No folder structure available

+
+ )} +
+ ) : filteredObsNotes.length === 0 ? (

No notes found. {vault ? `Vault has ${vault.obs_notes.length} notes.` : 'Vault not loaded.'}

Search query: "{debouncedSearchQuery}"

@@ -810,11 +1368,263 @@ export const ObsidianVaultBrowser: React.FC = ({ )}
- - {obs_note.filePath.startsWith('http') - ? new URL(obs_note.filePath).pathname.replace(/^\//, '') || 'Home' - : obs_note.filePath - } + + {getFilePath(obs_note)} + + {obs_note.links.length > 0 && ( + + {obs_note.links.length} links + + )} +
+
+ ) + }) + )} +
+

+ )} +
+
+ ) + } + + // Modal mode: render with overlay + return ( +
+
+ +
+

+ {vault ? `Obsidian Vault: ${vault.name}` : 'No Obsidian Vault Connected'} +

+ {!vault && ( +
+

+ Connect your Obsidian vault to browse and add notes to the canvas. +

+ +
+ )} +
+ + {vault && ( +
+
+
+ setSearchQuery(e.target.value)} + className="search-input" + /> + {searchQuery && ( + + )} +
+
+ + {searchQuery ? ( + searchQuery !== debouncedSearchQuery ? ( + Searching... + ) : ( + `${filteredObsNotes.length} result${filteredObsNotes.length !== 1 ? 's' : ''} found` + ) + ) : ( + `Showing all ${filteredObsNotes.length} notes` + )} + +
+
+ +
+
+ + + +
+ +
+ +
+ + {selectedNotes.size > 0 && ( + + )} +
+
+ )} + + {vault && ( +
+
+ + {debouncedSearchQuery && debouncedSearchQuery.trim() + ? `${filteredObsNotes.length} notes found for "${debouncedSearchQuery}"` + : `All ${filteredObsNotes.length} notes` + } + + {vault && ( + + (Total: {vault.obs_notes.length}, Search: "{debouncedSearchQuery}") + + )} + {vault && vault.lastImported && ( + + Last imported: {vault.lastImported.toLocaleString()} + + )} +
+ +
+ {viewMode === 'tree' ? ( +
+ {folderTree ? ( +
+ {renderFolderTree(folderTree)} +
+ ) : ( +
+

No folder structure available

+
+ )} +
+ ) : filteredObsNotes.length === 0 ? ( +
+

No notes found. {vault ? `Vault has ${vault.obs_notes.length} notes.` : 'Vault not loaded.'}

+

Search query: "{debouncedSearchQuery}"

+
+ ) : ( + filteredObsNotes.map(obs_note => { + // Safety check for undefined obs_note + if (!obs_note) { + return null + } + + const isSelected = selectedNotes.has(obs_note.id) + const displayTitle = getDisplayTitle(obs_note) + const contentPreview = getContentPreview(obs_note, viewMode === 'grid' ? 120 : 200) + + return ( +
handleObsNoteToggle(obs_note)} + > +
+
+ handleObsNoteToggle(obs_note)} + onClick={(e) => e.stopPropagation()} + /> +
+
+

+ + {obs_note.modified ? + (obs_note.modified instanceof Date ? + obs_note.modified.toLocaleDateString() : + new Date(obs_note.modified).toLocaleDateString() + ) : 'Unknown date'} + +

+ +
+ +
+

+

+ + {obs_note.tags.length > 0 && ( +
+ {obs_note.tags.slice(0, viewMode === 'grid' ? 2 : 4).map(tag => ( + + {tag.replace('#', '')} + + ))} + {obs_note.tags.length > (viewMode === 'grid' ? 2 : 4) && ( + + +{obs_note.tags.length - (viewMode === 'grid' ? 2 : 4)} + + )} +
+ )} + +
+ + {getFilePath(obs_note)} {obs_note.links.length > 0 && ( diff --git a/src/components/StandardizedToolWrapper.tsx b/src/components/StandardizedToolWrapper.tsx new file mode 100644 index 0000000..b9a9db0 --- /dev/null +++ b/src/components/StandardizedToolWrapper.tsx @@ -0,0 +1,253 @@ +import React, { useState, ReactNode } from 'react' + +export interface StandardizedToolWrapperProps { + /** The title to display in the header */ + title: string + /** The primary color for this tool (used for header and accents) */ + primaryColor: string + /** The content to render inside the wrapper */ + children: ReactNode + /** Whether the shape is currently selected */ + isSelected: boolean + /** Width of the tool */ + width: number + /** Height of the tool */ + height: number + /** Callback when close button is clicked */ + onClose: () => void + /** Callback when minimize button is clicked */ + onMinimize?: () => void + /** Whether the tool is minimized */ + isMinimized?: boolean + /** Optional custom header content */ + headerContent?: ReactNode + /** Editor instance for shape selection */ + editor?: any + /** Shape ID for selection handling */ + shapeId?: string +} + +/** + * Standardized wrapper component for all custom tools on the canvas. + * Provides consistent header bar with close/minimize buttons, sizing, and color theming. + */ +export const StandardizedToolWrapper: React.FC = ({ + title, + primaryColor, + children, + isSelected, + width, + height, + onClose, + onMinimize, + isMinimized = false, + headerContent, + editor, + shapeId, +}) => { + const [isHoveringHeader, setIsHoveringHeader] = useState(false) + + + // Calculate header background color (lighter shade of primary color) + const headerBgColor = isSelected + ? primaryColor + : isHoveringHeader + ? `${primaryColor}15` // 15% opacity + : `${primaryColor}10` // 10% opacity + + const wrapperStyle: React.CSSProperties = { + width: typeof width === 'number' ? `${width}px` : width, + height: isMinimized ? 40 : (typeof height === 'number' ? `${height}px` : height), // Minimized height is just the header + backgroundColor: "white", + border: isSelected ? `2px solid ${primaryColor}` : `1px solid ${primaryColor}40`, + borderRadius: "8px", + overflow: "hidden", + boxShadow: isSelected + ? `0 0 0 2px ${primaryColor}40, 0 4px 8px rgba(0,0,0,0.15)` + : '0 2px 4px rgba(0,0,0,0.1)', + display: 'flex', + flexDirection: 'column', + fontFamily: "Inter, sans-serif", + position: 'relative', + pointerEvents: 'auto', + transition: 'height 0.2s ease, box-shadow 0.2s ease', + boxSizing: 'border-box', + } + + const headerStyle: React.CSSProperties = { + height: '40px', + backgroundColor: headerBgColor, + borderBottom: `1px solid ${primaryColor}30`, + display: 'flex', + alignItems: 'center', + justifyContent: 'space-between', + padding: '0 12px', + cursor: 'move', + userSelect: 'none', + flexShrink: 0, + position: 'relative', + zIndex: 10, + pointerEvents: 'auto', + transition: 'background-color 0.2s ease', + } + + const titleStyle: React.CSSProperties = { + fontSize: '13px', + fontWeight: 600, + color: isSelected ? 'white' : primaryColor, + flex: 1, + pointerEvents: 'none', + transition: 'color 0.2s ease', + } + + const buttonContainerStyle: React.CSSProperties = { + display: 'flex', + gap: '8px', + alignItems: 'center', + } + + const buttonBaseStyle: React.CSSProperties = { + width: '20px', + height: '20px', + borderRadius: '4px', + border: 'none', + cursor: 'pointer', + display: 'flex', + alignItems: 'center', + justifyContent: 'center', + fontSize: '12px', + fontWeight: 600, + transition: 'background-color 0.15s ease, color 0.15s ease', + pointerEvents: 'auto', + flexShrink: 0, + } + + const minimizeButtonStyle: React.CSSProperties = { + ...buttonBaseStyle, + backgroundColor: isSelected ? 'rgba(255,255,255,0.2)' : `${primaryColor}20`, + color: isSelected ? 'white' : primaryColor, + } + + const closeButtonStyle: React.CSSProperties = { + ...buttonBaseStyle, + backgroundColor: isSelected ? 'rgba(255,255,255,0.2)' : `${primaryColor}20`, + color: isSelected ? 'white' : primaryColor, + } + + const contentStyle: React.CSSProperties = { + width: '100%', + height: isMinimized ? 0 : 'calc(100% - 40px)', + overflow: 'auto', + position: 'relative', + pointerEvents: 'auto', + transition: 'height 0.2s ease', + display: 'flex', + flexDirection: 'column', + } + + const handleHeaderPointerDown = (e: React.PointerEvent) => { + // Check if this is an interactive element (button) + const target = e.target as HTMLElement + const isInteractive = + target.tagName === 'BUTTON' || + target.closest('button') || + target.closest('[role="button"]') + + if (isInteractive) { + // Buttons handle their own behavior and stop propagation + return + } + + // Don't stop the event - let tldraw handle it naturally + // The hand tool override will detect shapes and handle dragging + } + + const handleButtonClick = (e: React.MouseEvent, action: () => void) => { + e.stopPropagation() + action() + } + + const handleContentPointerDown = (e: React.PointerEvent) => { + // Only stop propagation for interactive elements to allow tldraw to handle dragging on white space + const target = e.target as HTMLElement + const isInteractive = + target.tagName === 'BUTTON' || + target.tagName === 'INPUT' || + target.tagName === 'TEXTAREA' || + target.tagName === 'SELECT' || + target.closest('button') || + target.closest('input') || + target.closest('textarea') || + target.closest('select') || + target.closest('[role="button"]') || + target.closest('a') || + target.closest('[data-interactive]') // Allow components to mark interactive areas + + if (isInteractive) { + e.stopPropagation() + } + // Don't stop propagation for non-interactive elements - let tldraw handle dragging + } + + return ( +
+ {/* Header Bar */} +
setIsHoveringHeader(true)} + onMouseLeave={() => setIsHoveringHeader(false)} + onMouseDown={(e) => { + // Ensure selection happens on mouse down for immediate visual feedback + if (editor && shapeId && !isSelected) { + editor.setSelectedShapes([shapeId]) + } + }} + data-draggable="true" + > +
+ {headerContent || title} +
+
+ + +
+
+ + {/* Content Area */} + {!isMinimized && ( +
+ {children} +
+ )} +
+ ) +} + diff --git a/src/components/auth/Profile.tsx b/src/components/auth/Profile.tsx index 8970425..7fe89c9 100644 --- a/src/components/auth/Profile.tsx +++ b/src/components/auth/Profile.tsx @@ -25,13 +25,14 @@ export const Profile: React.FC = ({ onLogout, onOpenVaultBrowser } setIsEditingVault(false); }; - const handleClearVaultPath = () => { + const handleDisconnectVault = () => { setVaultPath(''); updateSession({ obsidianVaultPath: undefined, obsidianVaultName: undefined }); setIsEditingVault(false); + console.log('๐Ÿ”ง Vault disconnected from profile'); }; const handleChangeVault = () => { @@ -95,8 +96,8 @@ export const Profile: React.FC = ({ onLogout, onOpenVaultBrowser } {session.obsidianVaultName ? 'Change Obsidian Vault' : 'Set Obsidian Vault'} {session.obsidianVaultPath && ( - )}
diff --git a/src/components/location/LocationCapture.tsx b/src/components/location/LocationCapture.tsx new file mode 100644 index 0000000..0319f78 --- /dev/null +++ b/src/components/location/LocationCapture.tsx @@ -0,0 +1,187 @@ +"use client" + +import type React from "react" +import { useState, useEffect } from "react" +import { useAuth } from "@/context/AuthContext" +import { LocationStorageService, type LocationData } from "@/lib/location/locationStorage" +import type { GeolocationPosition } from "@/lib/location/types" + +interface LocationCaptureProps { + onLocationCaptured?: (location: LocationData) => void + onError?: (error: string) => void +} + +export const LocationCapture: React.FC = ({ onLocationCaptured, onError }) => { + const { session, fileSystem } = useAuth() + const [isCapturing, setIsCapturing] = useState(false) + const [permissionState, setPermissionState] = useState<"prompt" | "granted" | "denied">("prompt") + const [currentLocation, setCurrentLocation] = useState(null) + const [error, setError] = useState(null) + + // Show loading state while auth is initializing + if (session.loading) { + return ( +
+
+
โณ
+

Loading authentication...

+
+
+ ) + } + + // Check permission status on mount + useEffect(() => { + if ("permissions" in navigator) { + navigator.permissions.query({ name: "geolocation" }).then((result) => { + setPermissionState(result.state as "prompt" | "granted" | "denied") + + result.addEventListener("change", () => { + setPermissionState(result.state as "prompt" | "granted" | "denied") + }) + }) + } + }, []) + + const captureLocation = async () => { + // Don't proceed if still loading + if (session.loading) { + return + } + + if (!session.authed) { + const errorMsg = "You must be logged in to share your location. Please log in and try again." + setError(errorMsg) + onError?.(errorMsg) + return + } + + if (!fileSystem) { + const errorMsg = "File system not available. Please refresh the page and try again." + setError(errorMsg) + onError?.(errorMsg) + return + } + + setIsCapturing(true) + setError(null) + + try { + // Request geolocation + const position = await new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition( + (pos) => resolve(pos as GeolocationPosition), + (err) => reject(err), + { + enableHighAccuracy: true, + timeout: 10000, + maximumAge: 0, + }, + ) + }) + + setCurrentLocation(position) + + // Create location data + const locationData: LocationData = { + id: crypto.randomUUID(), + userId: session.username, + latitude: position.coords.latitude, + longitude: position.coords.longitude, + accuracy: position.coords.accuracy, + timestamp: position.timestamp, + expiresAt: null, // Will be set when creating a share + precision: "exact", + } + + // Save to filesystem + const storageService = new LocationStorageService(fileSystem) + await storageService.initialize() + await storageService.saveLocation(locationData) + + onLocationCaptured?.(locationData) + } catch (err: any) { + let errorMsg = "Failed to capture location" + + if (err.code === 1) { + errorMsg = "Location permission denied. Please enable location access in your browser settings." + setPermissionState("denied") + } else if (err.code === 2) { + errorMsg = "Location unavailable. Please check your device settings." + } else if (err.code === 3) { + errorMsg = "Location request timed out. Please try again." + } + + setError(errorMsg) + onError?.(errorMsg) + } finally { + setIsCapturing(false) + } + } + + return ( +
+
+

Share Your Location

+

Securely share your current location with others

+
+ + {/* Permission status */} + {permissionState === "denied" && ( +
+

+ Location access is blocked. Please enable it in your browser settings to continue. +

+
+ )} + + {/* Current location display */} + {currentLocation && ( +
+

Current Location

+
+

+ Latitude: {currentLocation.coords.latitude.toFixed(6)} +

+

+ Longitude: {currentLocation.coords.longitude.toFixed(6)} +

+

+ Accuracy: ยฑ{Math.round(currentLocation.coords.accuracy)}m +

+

Captured {new Date(currentLocation.timestamp).toLocaleString()}

+
+
+ )} + + {/* Error display */} + {error && ( +
+

{error}

+
+ )} + + {/* Capture button */} + + + {!session.authed && ( +

Please log in to share your location

+ )} +
+ ) +} + + diff --git a/src/components/location/LocationDashboard.tsx b/src/components/location/LocationDashboard.tsx new file mode 100644 index 0000000..162f297 --- /dev/null +++ b/src/components/location/LocationDashboard.tsx @@ -0,0 +1,262 @@ +"use client" + +import type React from "react" +import { useState, useEffect } from "react" +import { useAuth } from "@/context/AuthContext" +import { LocationStorageService, type LocationData, type LocationShare } from "@/lib/location/locationStorage" +import { LocationMap } from "./LocationMap" + +interface ShareWithLocation { + share: LocationShare + location: LocationData +} + +export const LocationDashboard: React.FC = () => { + const { session, fileSystem } = useAuth() + const [shares, setShares] = useState([]) + const [loading, setLoading] = useState(true) + const [selectedShare, setSelectedShare] = useState(null) + const [error, setError] = useState(null) + + const loadShares = async () => { + if (!fileSystem) { + setError("File system not available") + setLoading(false) + return + } + + try { + const storageService = new LocationStorageService(fileSystem) + await storageService.initialize() + + // Get all shares + const allShares = await storageService.getAllShares() + + // Get locations for each share + const sharesWithLocations: ShareWithLocation[] = [] + + for (const share of allShares) { + const location = await storageService.getLocation(share.locationId) + if (location) { + sharesWithLocations.push({ share, location }) + } + } + + // Sort by creation date (newest first) + sharesWithLocations.sort((a, b) => b.share.createdAt - a.share.createdAt) + + setShares(sharesWithLocations) + setLoading(false) + } catch (err) { + console.error("Error loading shares:", err) + setError("Failed to load location shares") + setLoading(false) + } + } + + useEffect(() => { + if (session.authed && fileSystem) { + loadShares() + } + }, [session.authed, fileSystem]) + + const handleCopyLink = async (shareToken: string) => { + const baseUrl = window.location.origin + const link = `${baseUrl}/location/${shareToken}` + + try { + await navigator.clipboard.writeText(link) + alert("Link copied to clipboard!") + } catch (err) { + console.error("Failed to copy link:", err) + alert("Failed to copy link") + } + } + + const isExpired = (share: LocationShare): boolean => { + return share.expiresAt ? share.expiresAt < Date.now() : false + } + + const isMaxViewsReached = (share: LocationShare): boolean => { + return share.maxViews ? share.viewCount >= share.maxViews : false + } + + const getShareStatus = (share: LocationShare): { label: string; color: string } => { + if (isExpired(share)) { + return { label: "Expired", color: "text-destructive" } + } + if (isMaxViewsReached(share)) { + return { label: "Max Views Reached", color: "text-destructive" } + } + return { label: "Active", color: "text-green-600" } + } + + if (!session.authed) { + return ( +
+
+
๐Ÿ”’
+

Authentication Required

+

Please log in to view your location shares

+
+
+ ) + } + + if (loading) { + return ( +
+
+
+

Loading your shares...

+
+
+ ) + } + + if (error) { + return ( +
+
+
โš ๏ธ
+

Error Loading Dashboard

+

{error}

+ +
+
+ ) + } + + return ( +
+
+

Location Shares

+

Manage your shared locations and privacy settings

+
+ + {shares.length === 0 ? ( +
+
๐Ÿ“
+

No Location Shares Yet

+

+ You haven't shared any locations yet. Create your first share to get started. +

+ + Share Your Location + +
+ ) : ( +
+ {/* Stats Overview */} +
+
+
Total Shares
+
{shares.length}
+
+
+
Active Shares
+
+ {shares.filter((s) => !isExpired(s.share) && !isMaxViewsReached(s.share)).length} +
+
+
+
Total Views
+
+ {shares.reduce((sum, s) => sum + s.share.viewCount, 0)} +
+
+
+ + {/* Shares List */} +
+ {shares.map(({ share, location }) => { + const status = getShareStatus(share) + const isSelected = selectedShare?.share.id === share.id + + return ( +
+
+
+
+

Location Share

+ {status.label} +
+
+

Created: {new Date(share.createdAt).toLocaleString()}

+ {share.expiresAt &&

Expires: {new Date(share.expiresAt).toLocaleString()}

} +

+ Views: {share.viewCount} + {share.maxViews && ` / ${share.maxViews}`} +

+

+ Precision: {share.precision} +

+
+
+
+ + +
+
+ + {isSelected && ( +
+ +
+ )} +
+ ) + })} +
+
+ )} +
+ ) +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/location/LocationMap.tsx b/src/components/location/LocationMap.tsx new file mode 100644 index 0000000..d7747c0 --- /dev/null +++ b/src/components/location/LocationMap.tsx @@ -0,0 +1,233 @@ +"use client" + +import type React from "react" +import { useEffect, useRef, useState } from "react" +import type { LocationData } from "@/lib/location/locationStorage" +import { obfuscateLocation } from "@/lib/location/locationStorage" +import type { PrecisionLevel } from "@/lib/location/types" + +// Leaflet types +interface LeafletMap { + setView: (coords: [number, number], zoom: number) => void + remove: () => void +} + +interface LeafletMarker { + addTo: (map: LeafletMap) => LeafletMarker + bindPopup: (content: string) => LeafletMarker +} + +interface LeafletCircle { + addTo: (map: LeafletMap) => LeafletCircle +} + +interface LeafletTileLayer { + addTo: (map: LeafletMap) => LeafletTileLayer +} + +interface Leaflet { + map: (element: HTMLElement, options?: any) => LeafletMap + marker: (coords: [number, number], options?: any) => LeafletMarker + circle: (coords: [number, number], options?: any) => LeafletCircle + tileLayer: (url: string, options?: any) => LeafletTileLayer + icon: (options: any) => any +} + +declare global { + interface Window { + L?: Leaflet + } +} + +interface LocationMapProps { + location: LocationData + precision?: PrecisionLevel + showAccuracy?: boolean + height?: string +} + +export const LocationMap: React.FC = ({ + location, + precision = "exact", + showAccuracy = true, + height = "400px", +}) => { + const mapContainer = useRef(null) + const mapInstance = useRef(null) + const [isLoading, setIsLoading] = useState(true) + const [error, setError] = useState(null) + + useEffect(() => { + // Load Leaflet CSS and JS + const loadLeaflet = async () => { + try { + // Load CSS + if (!document.querySelector('link[href*="leaflet.css"]')) { + const link = document.createElement("link") + link.rel = "stylesheet" + link.href = "https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" + link.integrity = "sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" + link.crossOrigin = "" + document.head.appendChild(link) + } + + // Load JS + if (!window.L) { + await new Promise((resolve, reject) => { + const script = document.createElement("script") + script.src = "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" + script.integrity = "sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" + script.crossOrigin = "" + script.onload = () => resolve() + script.onerror = () => reject(new Error("Failed to load Leaflet")) + document.head.appendChild(script) + }) + } + + setIsLoading(false) + } catch (err) { + setError("Failed to load map library") + setIsLoading(false) + } + } + + loadLeaflet() + }, []) + + useEffect(() => { + if (!mapContainer.current || !window.L || isLoading) return + + // Clean up existing map + if (mapInstance.current) { + mapInstance.current.remove() + } + + const L = window.L! + + // Get obfuscated location based on precision + const { lat, lng, radius } = obfuscateLocation(location.latitude, location.longitude, precision) + + // Create map + const map = L.map(mapContainer.current, { + center: [lat, lng], + zoom: precision === "exact" ? 15 : precision === "street" ? 14 : precision === "neighborhood" ? 12 : 10, + zoomControl: true, + attributionControl: true, + }) + + // Add OpenStreetMap tiles + L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { + attribution: '© OpenStreetMap contributors', + maxZoom: 19, + }).addTo(map) + + // Add marker + const marker = L.marker([lat, lng], { + icon: L.icon({ + iconUrl: "https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png", + iconRetinaUrl: "https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon-2x.png", + shadowUrl: "https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png", + iconSize: [25, 41], + iconAnchor: [12, 41], + popupAnchor: [1, -34], + shadowSize: [41, 41], + }), + }).addTo(map) + + // Add popup with location info + const popupContent = ` +
+ Shared Location
+ + Precision: ${precision}
+ ${new Date(location.timestamp).toLocaleString()} +
+
+ ` + marker.bindPopup(popupContent) + + // Add accuracy circle if showing accuracy + if (showAccuracy && radius > 0) { + L.circle([lat, lng], { + radius: radius, + color: "#3b82f6", + fillColor: "#3b82f6", + fillOpacity: 0.1, + weight: 2, + }).addTo(map) + } + + mapInstance.current = map + + // Cleanup + return () => { + if (mapInstance.current) { + mapInstance.current.remove() + mapInstance.current = null + } + } + }, [location, precision, showAccuracy, isLoading]) + + if (error) { + return ( +
+

{error}

+
+ ) + } + + if (isLoading) { + return ( +
+
+
+

Loading map...

+
+
+ ) + } + + return ( +
+
+
+

+ Showing {precision} location โ€ข Last updated {new Date(location.timestamp).toLocaleTimeString()} +

+
+
+ ) +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/location/LocationShareDialog.tsx b/src/components/location/LocationShareDialog.tsx new file mode 100644 index 0000000..1dfe7ce --- /dev/null +++ b/src/components/location/LocationShareDialog.tsx @@ -0,0 +1,45 @@ +import { + TLUiDialogProps, + TldrawUiDialogBody, + TldrawUiDialogCloseButton, + TldrawUiDialogHeader, + TldrawUiDialogTitle, +} from "tldraw" +import React from "react" +import { ShareLocation } from "./ShareLocation" + +export function LocationShareDialog({ onClose }: TLUiDialogProps) { + return ( + <> + + Share Location + + + + + + + ) +} + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/location/LocationViewer.tsx b/src/components/location/LocationViewer.tsx new file mode 100644 index 0000000..b20d2e1 --- /dev/null +++ b/src/components/location/LocationViewer.tsx @@ -0,0 +1,175 @@ +"use client" + +import type React from "react" +import { useState, useEffect } from "react" +import { LocationMap } from "./LocationMap" +import type { LocationData, LocationShare } from "@/lib/location/locationStorage" +import { LocationStorageService } from "@/lib/location/locationStorage" +import { useAuth } from "@/context/AuthContext" + +interface LocationViewerProps { + shareToken: string +} + +export const LocationViewer: React.FC = ({ shareToken }) => { + const { fileSystem } = useAuth() + const [location, setLocation] = useState(null) + const [share, setShare] = useState(null) + const [loading, setLoading] = useState(true) + const [error, setError] = useState(null) + + useEffect(() => { + const loadSharedLocation = async () => { + if (!fileSystem) { + setError("File system not available") + setLoading(false) + return + } + + try { + const storageService = new LocationStorageService(fileSystem) + await storageService.initialize() + + // Get share by token + const shareData = await storageService.getShareByToken(shareToken) + if (!shareData) { + setError("Share not found or expired") + setLoading(false) + return + } + + // Check if share is expired + if (shareData.expiresAt && shareData.expiresAt < Date.now()) { + setError("This share has expired") + setLoading(false) + return + } + + // Check if max views reached + if (shareData.maxViews && shareData.viewCount >= shareData.maxViews) { + setError("This share has reached its maximum view limit") + setLoading(false) + return + } + + // Get location data + const locationData = await storageService.getLocation(shareData.locationId) + if (!locationData) { + setError("Location data not found") + setLoading(false) + return + } + + setShare(shareData) + setLocation(locationData) + + // Increment view count + await storageService.incrementShareViews(shareData.id) + + setLoading(false) + } catch (err) { + console.error("Error loading shared location:", err) + setError("Failed to load shared location") + setLoading(false) + } + } + + loadSharedLocation() + }, [shareToken, fileSystem]) + + if (loading) { + return ( +
+
+
+

Loading shared location...

+
+
+ ) + } + + if (error) { + return ( +
+
+
๐Ÿ“
+

Unable to Load Location

+

{error}

+
+
+ ) + } + + if (!location || !share) { + return null + } + + return ( +
+
+

Shared Location

+

Someone has shared their location with you

+
+ +
+ {/* Map Display */} + + + {/* Share Info */} +
+
+ Precision Level: + {share.precision} +
+
+ Views: + + {share.viewCount} {share.maxViews ? `/ ${share.maxViews}` : ""} + +
+ {share.expiresAt && ( +
+ Expires: + {new Date(share.expiresAt).toLocaleString()} +
+ )} +
+ Shared: + {new Date(share.createdAt).toLocaleString()} +
+
+ + {/* Privacy Notice */} +
+

+ This location is shared securely and will expire based on the sender's privacy settings. The location data + is stored in a decentralized filesystem and is only accessible via this unique link. +

+
+
+
+ ) +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/location/ShareLocation.tsx b/src/components/location/ShareLocation.tsx new file mode 100644 index 0000000..dcf24c1 --- /dev/null +++ b/src/components/location/ShareLocation.tsx @@ -0,0 +1,279 @@ +"use client" + +import React, { useState } from "react" +import { LocationCapture } from "./LocationCapture" +import { ShareSettingsComponent } from "./ShareSettings" +import { LocationMap } from "./LocationMap" +import type { LocationData, LocationShare } from "@/lib/location/locationStorage" +import { LocationStorageService, generateShareToken } from "@/lib/location/locationStorage" +import type { ShareSettings } from "@/lib/location/types" +import { useAuth } from "@/context/AuthContext" + +export const ShareLocation: React.FC = () => { + const { session, fileSystem } = useAuth() + const [step, setStep] = useState<"capture" | "settings" | "share">("capture") + const [capturedLocation, setCapturedLocation] = useState(null) + const [shareSettings, setShareSettings] = useState({ + duration: 24 * 3600000, // 24 hours + maxViews: null, + precision: "street", + }) + const [shareLink, setShareLink] = useState(null) + const [isCreatingShare, setIsCreatingShare] = useState(false) + const [error, setError] = useState(null) + + // Show loading state while auth is initializing + if (session.loading) { + return ( +
+
+
โณ
+

Loading...

+

Initializing authentication

+
+
+ ) + } + + const handleLocationCaptured = (location: LocationData) => { + setCapturedLocation(location) + setStep("settings") + } + + const handleCreateShare = async () => { + if (!capturedLocation || !fileSystem) { + setError("Location or filesystem not available") + return + } + + setIsCreatingShare(true) + setError(null) + + try { + const storageService = new LocationStorageService(fileSystem) + await storageService.initialize() + + // Generate share token + const shareToken = generateShareToken() + + // Calculate expiration + const expiresAt = shareSettings.duration ? Date.now() + shareSettings.duration : null + + // Update location with expiration + const updatedLocation: LocationData = { + ...capturedLocation, + expiresAt, + precision: shareSettings.precision, + } + + await storageService.saveLocation(updatedLocation) + + // Create share + const share: LocationShare = { + id: crypto.randomUUID(), + locationId: capturedLocation.id, + shareToken, + createdAt: Date.now(), + expiresAt, + maxViews: shareSettings.maxViews, + viewCount: 0, + precision: shareSettings.precision, + } + + await storageService.createShare(share) + + // Generate share link + const baseUrl = window.location.origin + const link = `${baseUrl}/location/${shareToken}` + + setShareLink(link) + setStep("share") + } catch (err) { + console.error("Error creating share:", err) + setError("Failed to create share link") + } finally { + setIsCreatingShare(false) + } + } + + const handleCopyLink = async () => { + if (!shareLink) return + + try { + await navigator.clipboard.writeText(shareLink) + // Could add a toast notification here + alert("Link copied to clipboard!") + } catch (err) { + console.error("Failed to copy link:", err) + alert("Failed to copy link. Please copy manually.") + } + } + + const handleReset = () => { + setStep("capture") + setCapturedLocation(null) + setShareLink(null) + setError(null) + } + + if (!session.authed) { + return ( +
+
+
๐Ÿ”’
+

Authentication Required

+

Please log in to share your location securely

+
+
+ ) + } + + return ( +
+ {/* Progress Steps */} +
+ {["capture", "settings", "share"].map((s, index) => ( + +
+
+ {index + 1} +
+ + {s} + +
+ {index < 2 && ( +
+ )} + + ))} +
+ + {/* Error Display */} + {error && ( +
+

{error}

+
+ )} + + {/* Step Content */} +
+ {step === "capture" && } + + {step === "settings" && capturedLocation && ( +
+
+

Preview Your Location

+ +
+ + + +
+ + +
+
+ )} + + {step === "share" && shareLink && capturedLocation && ( +
+
+
โœ“
+

Share Link Created!

+

Your location is ready to share securely

+
+ +
+ +
+ e.currentTarget.select()} + /> + +
+
+ +
+

Location Preview

+ +
+ +
+

Share Settings

+
+ Precision: + {shareSettings.precision} +
+
+ Duration: + + {shareSettings.duration ? `${shareSettings.duration / 3600000} hours` : "No expiration"} + +
+
+ Max Views: + {shareSettings.maxViews || "Unlimited"} +
+
+ + +
+ )} +
+
+ ) +} + + diff --git a/src/components/location/ShareSettings.tsx b/src/components/location/ShareSettings.tsx new file mode 100644 index 0000000..aa635dd --- /dev/null +++ b/src/components/location/ShareSettings.tsx @@ -0,0 +1,142 @@ +"use client" + +import React, { useState } from "react" +import type { ShareSettings, PrecisionLevel } from "@/lib/location/types" + +interface ShareSettingsProps { + onSettingsChange: (settings: ShareSettings) => void + initialSettings?: Partial +} + +export const ShareSettingsComponent: React.FC = ({ onSettingsChange, initialSettings = {} }) => { + const [duration, setDuration] = useState( + initialSettings.duration ? String(initialSettings.duration / 3600000) : "24", + ) + const [maxViews, setMaxViews] = useState( + initialSettings.maxViews ? String(initialSettings.maxViews) : "unlimited", + ) + const [precision, setPrecision] = useState(initialSettings.precision || "street") + + const handleChange = () => { + const settings: ShareSettings = { + duration: duration === "unlimited" ? null : Number(duration) * 3600000, + maxViews: maxViews === "unlimited" ? null : Number(maxViews), + precision, + } + onSettingsChange(settings) + } + + React.useEffect(() => { + handleChange() + }, [duration, maxViews, precision]) + + return ( +
+
+

Privacy Settings

+

Control how your location is shared

+
+ + {/* Precision Level */} +
+ +
+ {[ + { value: "exact", label: "Exact Location", desc: "Share precise coordinates" }, + { value: "street", label: "Street Level", desc: "~100m radius" }, + { value: "neighborhood", label: "Neighborhood", desc: "~1km radius" }, + { value: "city", label: "City Level", desc: "~10km radius" }, + ].map((option) => ( + + ))} +
+
+ + {/* Duration */} +
+ + +
+ + {/* Max Views */} +
+ + +
+ + {/* Privacy Notice */} +
+

+ Your location data is stored securely in your private filesystem. Only people with the share link can view + your location, and shares automatically expire based on your settings. +

+
+
+ ) +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/constants/workerUrl.ts b/src/constants/workerUrl.ts new file mode 100644 index 0000000..e82096b --- /dev/null +++ b/src/constants/workerUrl.ts @@ -0,0 +1,36 @@ +// Environment-based worker URL configuration +// You can easily switch between environments by changing the WORKER_ENV variable + +// Available environments: +// - 'local': Use local worker running on port 5172 +// - 'dev': Use Cloudflare dev environment (jeffemmett-canvas-automerge-dev) +// - 'production': Use production environment (jeffemmett-canvas) + +const WORKER_ENV = import.meta.env.VITE_WORKER_ENV || 'dev' // Default to dev for testing + +const WORKER_URLS = { + local: `http://${window.location.hostname}:5172`, + dev: "https://jeffemmett-canvas-automerge-dev.jeffemmett.workers.dev", + production: "https://jeffemmett-canvas.jeffemmett.workers.dev" +} + +// Main worker URL - automatically switches based on environment +export const WORKER_URL = WORKER_URLS[WORKER_ENV as keyof typeof WORKER_URLS] || WORKER_URLS.dev + +// Legacy support for existing code +export const LOCAL_WORKER_URL = WORKER_URLS.local + +// Helper function to get current environment info +export const getWorkerInfo = () => ({ + environment: WORKER_ENV, + url: WORKER_URL, + isLocal: WORKER_ENV === 'local', + isDev: WORKER_ENV === 'dev', + isProduction: WORKER_ENV === 'production' +}) + +// Log current environment on import (for debugging) +console.log(`๐Ÿ”ง Worker Environment: ${WORKER_ENV}`) +console.log(`๐Ÿ”ง Worker URL: ${WORKER_URL}`) +console.log(`๐Ÿ”ง Available environments: local, dev, production`) +console.log(`๐Ÿ”ง To switch: Set VITE_WORKER_ENV environment variable or change WORKER_ENV in this file`) diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 8a40b7b..9c08fb1 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -26,7 +26,7 @@ const initialSession: Session = { obsidianVaultName: undefined }; -const AuthContext = createContext(undefined); +export const AuthContext = createContext(undefined); export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => { const [session, setSessionState] = useState(initialSession); diff --git a/src/context/AutomergeHandleContext.tsx b/src/context/AutomergeHandleContext.tsx new file mode 100644 index 0000000..f88e7eb --- /dev/null +++ b/src/context/AutomergeHandleContext.tsx @@ -0,0 +1,27 @@ +import React, { createContext, useContext, ReactNode } from 'react' +import { DocHandle } from '@automerge/automerge-repo' + +interface AutomergeHandleContextType { + handle: DocHandle | null +} + +const AutomergeHandleContext = createContext({ + handle: null, +}) + +export const AutomergeHandleProvider: React.FC<{ + handle: DocHandle | null + children: ReactNode +}> = ({ handle, children }) => { + return ( + + {children} + + ) +} + +export const useAutomergeHandle = (): DocHandle | null => { + const context = useContext(AutomergeHandleContext) + return context.handle +} + diff --git a/src/css/location.css b/src/css/location.css new file mode 100644 index 0000000..f624028 --- /dev/null +++ b/src/css/location.css @@ -0,0 +1,417 @@ +/* Location Sharing Components Styles */ + +/* Spinner animation */ +.spinner { + width: 20px; + height: 20px; + border: 2px solid currentColor; + border-top-color: transparent; + border-radius: 50%; + animation: spin 0.6s linear infinite; +} + +@keyframes spin { + to { + transform: rotate(360deg); + } +} + +/* Location Capture */ +.location-capture { + width: 100%; +} + +.capture-header h2 { + margin-bottom: 0.5rem; +} + +.capture-button { + display: flex; + align-items: center; + justify-content: center; +} + +/* Location Map */ +.location-map-wrapper { + width: 100%; +} + +.location-map { + width: 100%; + min-height: 300px; +} + +.map-info { + margin-top: 0.75rem; +} + +.map-loading, +.map-error { + display: flex; + align-items: center; + justify-content: center; + min-height: 300px; +} + +/* Share Settings */ +.share-settings { + width: 100%; +} + +.settings-header { + margin-bottom: 1rem; +} + +.setting-group { + margin-bottom: 1.5rem; +} + +.precision-options { + display: flex; + flex-direction: column; + gap: 0.5rem; +} + +.precision-option { + display: flex; + align-items: flex-start; + gap: 0.75rem; + padding: 0.75rem; + border-radius: 0.5rem; + cursor: pointer; + transition: all 0.2s; +} + +.precision-option input[type="radio"] { + margin-top: 0.125rem; + cursor: pointer; +} + +.privacy-notice { + padding: 1rem; + border-radius: 0.5rem; + background-color: rgba(var(--muted), 0.5); +} + +/* Share Location Flow */ +.share-location { + width: 100%; + max-width: 56rem; + margin: 0 auto; + padding: 1.5rem; +} + +.progress-steps { + display: flex; + align-items: center; + justify-content: center; + gap: 1rem; + margin-bottom: 2rem; +} + +.step-item { + display: flex; + align-items: center; + gap: 0.5rem; +} + +.step-number { + width: 2rem; + height: 2rem; + border-radius: 50%; + display: flex; + align-items: center; + justify-content: center; + font-size: 0.875rem; + font-weight: 500; + transition: all 0.2s; +} + +.step-connector { + height: 2px; + width: 3rem; + transition: all 0.2s; +} + +.step-content { + width: 100%; +} + +.settings-step { + display: flex; + flex-direction: column; + gap: 1.5rem; +} + +.location-preview { + width: 100%; +} + +.settings-actions { + display: flex; + gap: 0.75rem; +} + +.share-step { + display: flex; + flex-direction: column; + gap: 1.5rem; +} + +.share-success { + text-align: center; + margin-bottom: 1.5rem; +} + +.share-link-box { + background-color: rgba(var(--muted), 0.5); + border: 1px solid rgba(var(--border), 1); + border-radius: 0.5rem; + padding: 1rem; +} + +.share-link-box input { + width: 100%; + padding: 0.5rem 0.75rem; + border: 1px solid rgba(var(--border), 1); + border-radius: 0.5rem; + background-color: rgba(var(--background), 1); + font-size: 0.875rem; +} + +.share-details { + background-color: rgba(var(--muted), 0.5); + border-radius: 0.5rem; + padding: 1rem; +} + +.detail-row { + display: flex; + justify-content: space-between; + font-size: 0.875rem; +} + +/* Location Viewer */ +.location-viewer { + width: 100%; + max-width: 56rem; + margin: 0 auto; + padding: 1.5rem; +} + +.viewer-header { + margin-bottom: 1.5rem; +} + +.viewer-content { + display: flex; + flex-direction: column; + gap: 1.5rem; +} + +.share-info { + background-color: rgba(var(--muted), 0.5); + border-radius: 0.5rem; + padding: 1rem; +} + +.info-row { + display: flex; + justify-content: space-between; + font-size: 0.875rem; + margin-bottom: 0.5rem; +} + +.info-row:last-child { + margin-bottom: 0; +} + +/* Location Dashboard */ +.location-dashboard { + width: 100%; + max-width: 72rem; + margin: 0 auto; + padding: 1.5rem; +} + +.dashboard-header { + margin-bottom: 2rem; +} + +.dashboard-content { + width: 100%; +} + +.stats-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); + gap: 1rem; + margin-bottom: 2rem; +} + +.stat-card { + background-color: rgba(var(--muted), 0.5); + border: 1px solid rgba(var(--border), 1); + border-radius: 0.5rem; + padding: 1rem; +} + +.stat-label { + font-size: 0.875rem; + color: rgba(var(--muted-foreground), 1); + margin-bottom: 0.25rem; +} + +.stat-value { + font-size: 1.875rem; + font-weight: 700; +} + +.shares-list { + display: flex; + flex-direction: column; + gap: 1rem; +} + +.share-card { + background-color: rgba(var(--background), 1); + border-radius: 0.5rem; + border: 2px solid rgba(var(--border), 1); + transition: all 0.2s; +} + +.share-card-header { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 1rem; + padding: 1rem; +} + +.share-info { + flex: 1; +} + +.share-meta { + display: flex; + flex-direction: column; + gap: 0.25rem; + font-size: 0.75rem; + color: rgba(var(--muted-foreground), 1); +} + +.share-actions { + display: flex; + gap: 0.5rem; +} + +.share-card-body { + padding: 1rem; + padding-top: 0; + border-top: 1px solid rgba(var(--border), 1); + margin-top: 1rem; +} + +.empty-state { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + min-height: 400px; + text-align: center; +} + +/* Auth required messages */ +.share-location-auth, +.location-dashboard-auth { + display: flex; + align-items: center; + justify-content: center; + min-height: 400px; +} + +/* Error messages */ +.error-message { + background-color: rgba(var(--destructive), 0.1); + border: 1px solid rgba(var(--destructive), 0.2); + border-radius: 0.5rem; + padding: 1rem; +} + +.permission-denied { + background-color: rgba(var(--destructive), 0.1); + border: 1px solid rgba(var(--destructive), 0.2); + border-radius: 0.5rem; + padding: 1rem; + margin-top: 1rem; +} + +.current-location { + background-color: rgba(var(--muted), 0.5); + border-radius: 0.5rem; + padding: 1rem; + margin-top: 1rem; +} + +.location-details { + display: flex; + flex-direction: column; + gap: 0.25rem; + font-size: 0.75rem; +} + +/* Responsive adjustments */ +@media (max-width: 768px) { + .share-location, + .location-viewer, + .location-dashboard { + padding: 1rem; + } + + .progress-steps { + flex-wrap: wrap; + } + + .step-connector { + display: none; + } + + .stats-grid { + grid-template-columns: 1fr; + } + + .share-card-header { + flex-direction: column; + } + + .share-actions { + width: 100%; + } + + .share-actions button { + flex: 1; + } +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/css/obsidian-browser.css b/src/css/obsidian-browser.css index 4435f6c..f098f41 100644 --- a/src/css/obsidian-browser.css +++ b/src/css/obsidian-browser.css @@ -15,6 +15,23 @@ pointer-events: auto; /* Ensure the browser is clickable */ } +/* Shape mode: remove modal overlay styles */ +.obsidian-browser.shape-mode { + position: relative; + top: auto; + left: auto; + right: auto; + bottom: auto; + background: transparent; + z-index: auto; + display: flex; + align-items: stretch; + justify-content: stretch; + padding: 0; + width: 100%; + height: 100%; +} + .obsidian-browser > div { background: white; border-radius: 12px; @@ -39,6 +56,16 @@ overflow-y: auto; position: relative; /* Allow absolute positioning of close button */ pointer-events: auto; /* Ensure content is clickable */ + overscroll-behavior: contain; +} + +/* Shape mode: adjust browser-content padding */ +.obsidian-browser.shape-mode .browser-content { + padding: 0; + padding-top: 0; + align-items: stretch; + width: 100%; + height: 100%; } .vault-title { @@ -46,6 +73,14 @@ margin-bottom: 30px; } +/* Shape mode: reduce vault-title margin - hide completely when vault is connected */ +.obsidian-browser.shape-mode .vault-title { + margin-bottom: 0; + padding: 0; + padding-top: 0; + display: none; /* Hide completely since vault name is in header */ +} + .vault-title h2 { margin: 0; font-size: 24px; @@ -53,6 +88,24 @@ color: #333; } +/* Shape mode: hide vault title when vault is connected (vault name is in header) */ +.obsidian-browser.shape-mode .vault-title h2 { + display: none; +} + +/* Shape mode: keep vault-connect-section visible when no vault */ +.obsidian-browser.shape-mode .vault-connect-section { + display: block; + margin-top: 8px; +} + +/* Show vault-title only when no vault is connected */ +.obsidian-browser.shape-mode .vault-title:has(.vault-connect-section) { + display: block; + padding: 8px 12px; + margin-bottom: 8px; +} + .vault-connect-section { margin-top: 12px; text-align: center; @@ -137,12 +190,24 @@ pointer-events: auto; /* Ensure controls are clickable */ } +/* Shape mode: adjust browser-controls padding - more compact */ +.obsidian-browser.shape-mode .browser-controls { + padding: 8px 12px; + gap: 8px; + border-bottom: 1px solid #e0e0e0; +} + .search-container { margin-bottom: 20px; width: 100%; max-width: none; } +/* Shape mode: reduce search-container margin */ +.obsidian-browser.shape-mode .search-container { + margin-bottom: 8px; +} + .view-controls { display: flex; justify-content: space-between; @@ -177,6 +242,25 @@ border-color: #007acc; } +.disconnect-vault-button { + padding: 6px 12px; + border: 1px solid #dc3545; + background: #dc3545; + color: white; + border-radius: 4px; + cursor: pointer; + font-size: 14px; + font-weight: 500; + transition: all 0.2s; + margin-left: 8px; +} + +.disconnect-vault-button:hover { + background: #c82333; + border-color: #c82333; + transform: translateY(-1px); +} + .selection-controls { display: flex; gap: 8px; @@ -300,6 +384,7 @@ flex: 1; overflow-y: auto; padding: 0; + overscroll-behavior: contain; } .notes-display.grid { @@ -316,6 +401,17 @@ padding: 16px; } +/* Shape mode: reduce notes-display padding for more space */ +.obsidian-browser.shape-mode .notes-display.grid { + padding: 12px; + gap: 12px; +} + +.obsidian-browser.shape-mode .notes-display.list { + padding: 12px; + gap: 6px; +} + .notes-header { display: flex; justify-content: space-between; @@ -327,6 +423,12 @@ color: #666; } +/* Shape mode: reduce notes-header padding */ +.obsidian-browser.shape-mode .notes-header { + padding: 8px 12px; + font-size: 11px; +} + .last-imported { font-style: italic; } @@ -335,6 +437,7 @@ flex: 1; overflow-y: auto; padding: 0; + overscroll-behavior: contain; } /* Note Cards */ @@ -1002,3 +1105,135 @@ mark { opacity: 0.5; cursor: not-allowed; } + +/* Folder Tree Styles */ +.folder-tree-container { + width: 100%; + height: 100%; + overflow-y: auto; + padding: 10px; + overscroll-behavior: contain; +} + +.folder-tree { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; +} + +.folder-tree-item { + margin: 2px 0; +} + +.folder-item { + display: flex; + align-items: center; + padding: 8px 12px; + border-radius: 6px; + cursor: pointer; + transition: all 0.2s ease; + user-select: none; +} + +.folder-item:hover { + background-color: #f5f5f5; +} + +.folder-item.selected { + background-color: #e3f2fd; + border: 1px solid #2196f3; +} + +.folder-toggle { + background: none; + border: none; + cursor: pointer; + padding: 4px; + margin-right: 8px; + font-size: 12px; + color: #666; + transition: color 0.2s ease; +} + +.folder-toggle:hover { + color: #333; +} + +.folder-icon { + margin-right: 8px; + font-size: 16px; +} + +.folder-name { + flex: 1; + font-weight: 500; + color: #333; +} + +.folder-count { + font-size: 12px; + color: #666; + background-color: #f0f0f0; + padding: 2px 6px; + border-radius: 10px; + margin-left: 8px; +} + +.folder-children { + margin-left: 0; +} + +.note-item { + display: flex; + align-items: center; + padding: 6px 12px; + border-radius: 4px; + cursor: pointer; + transition: all 0.2s ease; + user-select: none; + margin: 1px 0; +} + +.note-item:hover { + background-color: #f8f9fa; +} + +.note-item.selected { + background-color: #e8f5e8; + border: 1px solid #4caf50; +} + +.note-icon { + margin-right: 8px; + font-size: 14px; + color: #666; +} + +.note-name { + flex: 1; + font-size: 14px; + color: #333; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.no-folder-tree { + text-align: center; + padding: 40px 20px; + color: #666; +} + +/* Tree view specific adjustments */ +.notes-display.tree { + height: 100%; + overflow: hidden; +} + +.notes-display.tree .folder-tree-container { + height: 100%; + max-height: 500px; + overflow-y: auto; + border: 1px solid #e0e0e0; + border-radius: 8px; + background-color: #fafafa; + overscroll-behavior: contain; +} diff --git a/src/css/style.css b/src/css/style.css index 37c23cd..c58949f 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -391,6 +391,19 @@ p:has(+ ol) { -webkit-tap-highlight-color: transparent; } +/* Ensure scrollable elements handle wheel events on the element being hovered */ +[style*="overflow-y: auto"], +[style*="overflow-y: scroll"], +[style*="overflow-x: auto"], +[style*="overflow-x: scroll"], +[style*="overflow: auto"], +[style*="overflow: scroll"], +.overflow-y-auto, +.overflow-x-auto, +.overflow-auto { + overscroll-behavior: contain; +} + .tl-background { background-color: transparent; } diff --git a/src/css/user-profile.css b/src/css/user-profile.css index f2a3e18..8869ac4 100644 --- a/src/css/user-profile.css +++ b/src/css/user-profile.css @@ -183,7 +183,7 @@ box-shadow: 0 4px 8px rgba(0, 122, 204, 0.3); } -.clear-vault-button { +.disconnect-vault-button { background: #dc3545; color: white; border: none; @@ -195,7 +195,7 @@ transition: all 0.2s ease; } -.clear-vault-button:hover { +.disconnect-vault-button:hover { background: #c82333; transform: translateY(-1px); } diff --git a/src/hooks/useAdvancedSpeakerDiarization.ts b/src/hooks/useAdvancedSpeakerDiarization.ts new file mode 100644 index 0000000..ab299d7 --- /dev/null +++ b/src/hooks/useAdvancedSpeakerDiarization.ts @@ -0,0 +1,207 @@ +import { useState, useRef, useCallback, useEffect } from 'react' + +interface SpeakerSegment { + speaker: string + text: string + startTime: number + endTime: number + confidence: number +} + +interface UseAdvancedSpeakerDiarizationOptions { + onTranscriptUpdate?: (segments: SpeakerSegment[]) => void + onError?: (error: Error) => void + maxSpeakers?: number + enableRealTime?: boolean +} + +export const useAdvancedSpeakerDiarization = ({ + onTranscriptUpdate, + onError, + maxSpeakers = 4, + enableRealTime = false +}: UseAdvancedSpeakerDiarizationOptions = {}) => { + const [isProcessing, setIsProcessing] = useState(false) + const [speakers, setSpeakers] = useState([]) + const [segments, setSegments] = useState([]) + const [isSupported, setIsSupported] = useState(false) + + const audioContextRef = useRef(null) + const mediaStreamRef = useRef(null) + const processorRef = useRef(null) + const audioBufferRef = useRef([]) + + // Check if advanced features are supported + useEffect(() => { + // Check for Web Audio API support + const hasWebAudio = !!(window.AudioContext || (window as any).webkitAudioContext) + const hasMediaDevices = !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) + + setIsSupported(hasWebAudio && hasMediaDevices) + + if (!hasWebAudio) { + onError?.(new Error('Web Audio API is not supported')) + } + if (!hasMediaDevices) { + onError?.(new Error('Media Devices API is not supported')) + } + }, [onError]) + + // Simple speaker detection based on audio characteristics + const detectSpeakerCharacteristics = useCallback((audioData: Float32Array) => { + // Calculate basic audio features + const rms = Math.sqrt(audioData.reduce((sum, val) => sum + val * val, 0) / audioData.length) + const maxAmplitude = Math.max(...audioData.map(Math.abs)) + const zeroCrossings = audioData.slice(1).reduce((count, val, i) => + count + (Math.sign(val) !== Math.sign(audioData[i]) ? 1 : 0), 0 + ) + + // Simple speaker identification based on audio characteristics + const speakerId = `Speaker_${Math.floor(rms * 1000) % maxSpeakers + 1}` + + return { + speakerId, + confidence: Math.min(rms * 10, 1), // Simple confidence based on RMS + features: { + rms, + maxAmplitude, + zeroCrossings + } + } + }, [maxSpeakers]) + + // Process audio data for speaker diarization + const processAudioData = useCallback((audioData: Float32Array, timestamp: number) => { + if (!enableRealTime) return + + const speakerInfo = detectSpeakerCharacteristics(audioData) + + // Create a simple segment + const segment: SpeakerSegment = { + speaker: speakerInfo.speakerId, + text: '', // Would need transcription integration + startTime: timestamp, + endTime: timestamp + (audioData.length / 16000), // Assuming 16kHz + confidence: speakerInfo.confidence + } + + // Update segments + setSegments(prev => [...prev, segment]) + + // Update speakers list + setSpeakers(prev => { + if (!prev.includes(speakerInfo.speakerId)) { + return [...prev, speakerInfo.speakerId] + } + return prev + }) + + onTranscriptUpdate?.([segment]) + }, [enableRealTime, detectSpeakerCharacteristics, onTranscriptUpdate]) + + // Start audio processing + const startProcessing = useCallback(async () => { + if (!isSupported) { + onError?.(new Error('Advanced speaker diarization not supported')) + return + } + + try { + setIsProcessing(true) + + // Get audio stream + const stream = await navigator.mediaDevices.getUserMedia({ + audio: { + sampleRate: 16000, + channelCount: 1, + echoCancellation: true, + noiseSuppression: true + } + }) + + mediaStreamRef.current = stream + + // Create audio context + const AudioContext = window.AudioContext || (window as any).webkitAudioContext + const audioContext = new AudioContext({ sampleRate: 16000 }) + audioContextRef.current = audioContext + + // Create audio source + const source = audioContext.createMediaStreamSource(stream) + + // Create processor for real-time analysis + const processor = audioContext.createScriptProcessor(4096, 1, 1) + processorRef.current = processor + + processor.onaudioprocess = (event) => { + const inputBuffer = event.inputBuffer + const audioData = inputBuffer.getChannelData(0) + const timestamp = audioContext.currentTime + + processAudioData(audioData, timestamp) + } + + // Connect audio nodes + source.connect(processor) + processor.connect(audioContext.destination) + + console.log('๐ŸŽค Advanced speaker diarization started') + + } catch (error) { + console.error('โŒ Error starting speaker diarization:', error) + onError?.(error as Error) + setIsProcessing(false) + } + }, [isSupported, processAudioData, onError]) + + // Stop audio processing + const stopProcessing = useCallback(() => { + if (mediaStreamRef.current) { + mediaStreamRef.current.getTracks().forEach(track => track.stop()) + mediaStreamRef.current = null + } + + if (processorRef.current) { + processorRef.current.disconnect() + processorRef.current = null + } + + if (audioContextRef.current) { + audioContextRef.current.close() + audioContextRef.current = null + } + + setIsProcessing(false) + console.log('๐Ÿ›‘ Advanced speaker diarization stopped') + }, []) + + // Cleanup on unmount + useEffect(() => { + return () => { + stopProcessing() + } + }, [stopProcessing]) + + // Format segments as readable text + const formatSegmentsAsText = useCallback((segments: SpeakerSegment[]) => { + return segments.map(segment => + `${segment.speaker}: ${segment.text}` + ).join('\n') + }, []) + + return { + isProcessing, + isSupported, + speakers, + segments, + startProcessing, + stopProcessing, + formatSegmentsAsText + } +} + +export default useAdvancedSpeakerDiarization + + + + diff --git a/src/hooks/useWebSpeechTranscription.ts b/src/hooks/useWebSpeechTranscription.ts new file mode 100644 index 0000000..6014343 --- /dev/null +++ b/src/hooks/useWebSpeechTranscription.ts @@ -0,0 +1,335 @@ +import { useState, useRef, useCallback, useEffect } from 'react' + +// TypeScript declarations for Web Speech API +declare global { + interface Window { + SpeechRecognition: typeof SpeechRecognition + webkitSpeechRecognition: typeof SpeechRecognition + } + + interface SpeechRecognition extends EventTarget { + continuous: boolean + interimResults: boolean + lang: string + maxAlternatives: number + start(): void + stop(): void + onstart: ((this: SpeechRecognition, ev: Event) => any) | null + onresult: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => any) | null + onerror: ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => any) | null + onend: ((this: SpeechRecognition, ev: Event) => any) | null + } + + interface SpeechRecognitionEvent extends Event { + resultIndex: number + results: SpeechRecognitionResultList + } + + interface SpeechRecognitionErrorEvent extends Event { + error: string + } + + interface SpeechRecognitionResultList { + length: number + item(index: number): SpeechRecognitionResult + [index: number]: SpeechRecognitionResult + } + + interface SpeechRecognitionResult { + length: number + item(index: number): SpeechRecognitionAlternative + [index: number]: SpeechRecognitionAlternative + isFinal: boolean + } + + interface SpeechRecognitionAlternative { + transcript: string + confidence: number + } + + var SpeechRecognition: { + prototype: SpeechRecognition + new(): SpeechRecognition + } +} + +interface UseWebSpeechTranscriptionOptions { + onTranscriptUpdate?: (text: string) => void + onError?: (error: Error) => void + language?: string + continuous?: boolean + interimResults?: boolean +} + +export const useWebSpeechTranscription = ({ + onTranscriptUpdate, + onError, + language = 'en-US', + continuous = true, + interimResults = true +}: UseWebSpeechTranscriptionOptions = {}) => { + const [isRecording, setIsRecording] = useState(false) + const [isTranscribing, setIsTranscribing] = useState(false) + const [transcript, setTranscript] = useState('') + const [interimTranscript, setInterimTranscript] = useState('') + const [isSupported, setIsSupported] = useState(false) + + const recognitionRef = useRef(null) + const finalTranscriptRef = useRef('') + const interimTranscriptRef = useRef('') + const lastSpeechTimeRef = useRef(0) + const pauseTimeoutRef = useRef(null) + const lastConfidenceRef = useRef(0) + const speakerChangeThreshold = 0.3 // Threshold for detecting speaker changes + + // Function to add line breaks after pauses and improve punctuation + const processTranscript = useCallback((text: string, isFinal: boolean = false, confidence?: number) => { + if (!text.trim()) return text + + let processedText = text.trim() + + // Add punctuation if missing at the end + if (isFinal && processedText && !/[.!?]$/.test(processedText)) { + processedText += '.' + } + + // Add line break if there's been a pause (for final results) + if (isFinal) { + const now = Date.now() + const timeSinceLastSpeech = now - lastSpeechTimeRef.current + + // If more than 3 seconds since last speech, add a line break + if (timeSinceLastSpeech > 3000 && lastSpeechTimeRef.current > 0) { + processedText = '\n' + processedText + } + + lastSpeechTimeRef.current = now + } + + return processedText + }, []) + + // Function to detect speaker changes based on confidence and timing + const detectSpeakerChange = useCallback((confidence: number) => { + if (lastConfidenceRef.current === 0) { + lastConfidenceRef.current = confidence + return false + } + + const confidenceDiff = Math.abs(confidence - lastConfidenceRef.current) + const now = Date.now() + const timeSinceLastSpeech = now - lastSpeechTimeRef.current + + // Detect speaker change if confidence changes significantly and there's been a pause + const isSpeakerChange = confidenceDiff > speakerChangeThreshold && timeSinceLastSpeech > 1000 + + if (isSpeakerChange) { + // Reduced debug logging + lastConfidenceRef.current = confidence + return true + } + + lastConfidenceRef.current = confidence + return false + }, [speakerChangeThreshold]) + + // Function to handle pause detection + const handlePauseDetection = useCallback(() => { + // Clear existing timeout + if (pauseTimeoutRef.current) { + clearTimeout(pauseTimeoutRef.current) + } + + // Set new timeout for pause detection + pauseTimeoutRef.current = setTimeout(() => { + const now = Date.now() + const timeSinceLastSpeech = now - lastSpeechTimeRef.current + + // If more than 2 seconds of silence, add a line break to interim transcript + if (timeSinceLastSpeech > 2000 && lastSpeechTimeRef.current > 0) { + const currentTranscript = finalTranscriptRef.current + '\n' + setTranscript(currentTranscript) + onTranscriptUpdate?.(currentTranscript) + // Reduced debug logging + } + }, 2000) // Check after 2 seconds of silence + }, [onTranscriptUpdate]) + + // Check if Web Speech API is supported + useEffect(() => { + const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition + if (SpeechRecognition) { + setIsSupported(true) + // Reduced debug logging + } else { + setIsSupported(false) + console.log('โŒ Web Speech API is not supported') + onError?.(new Error('Web Speech API is not supported in this browser')) + } + }, [onError]) + + // Initialize speech recognition + const initializeRecognition = useCallback(() => { + if (!isSupported) return null + + const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition + const recognition = new SpeechRecognition() + + recognition.continuous = continuous + recognition.interimResults = interimResults + recognition.lang = language + recognition.maxAlternatives = 1 + + recognition.onstart = () => { + console.log('๐ŸŽค Web Speech API started') + setIsRecording(true) + setIsTranscribing(true) + } + + recognition.onresult = (event) => { + let interimTranscript = '' + let finalTranscript = '' + + // Process all results + for (let i = event.resultIndex; i < event.results.length; i++) { + const result = event.results[i] + const transcript = result[0].transcript + + if (result.isFinal) { + finalTranscript += transcript + } else { + interimTranscript += transcript + } + } + + // Update final transcript with processing + if (finalTranscript) { + // Get confidence from the first result + const confidence = event.results[event.results.length - 1]?.[0]?.confidence || 0 + + // Detect speaker change + const isSpeakerChange = detectSpeakerChange(confidence) + + // Add speaker indicator if change detected + let speakerPrefix = '' + if (isSpeakerChange) { + speakerPrefix = '\n[Speaker Change]\n' + } + + const processedFinal = processTranscript(finalTranscript, true, confidence) + const newText = speakerPrefix + processedFinal + finalTranscriptRef.current += newText + setTranscript(finalTranscriptRef.current) + onTranscriptUpdate?.(newText) // Only send the new text portion + console.log(`โœ… Final transcript: "${processedFinal}" (confidence: ${confidence.toFixed(2)})`) + + // Trigger pause detection + handlePauseDetection() + } + + // Update interim transcript + if (interimTranscript) { + const processedInterim = processTranscript(interimTranscript, false) + interimTranscriptRef.current = processedInterim + setInterimTranscript(processedInterim) + console.log(`๐Ÿ”„ Interim transcript: "${processedInterim}"`) + } + } + + recognition.onerror = (event) => { + console.error('โŒ Web Speech API error:', event.error) + setIsRecording(false) + setIsTranscribing(false) + onError?.(new Error(`Speech recognition error: ${event.error}`)) + } + + recognition.onend = () => { + console.log('๐Ÿ›‘ Web Speech API ended') + setIsRecording(false) + setIsTranscribing(false) + } + + return recognition + }, [isSupported, continuous, interimResults, language, onTranscriptUpdate, onError]) + + // Start recording + const startRecording = useCallback(() => { + if (!isSupported) { + onError?.(new Error('Web Speech API is not supported')) + return + } + + try { + console.log('๐ŸŽค Starting Web Speech API recording...') + + // Don't reset transcripts for continuous transcription - keep existing content + // finalTranscriptRef.current = '' + // interimTranscriptRef.current = '' + // setTranscript('') + // setInterimTranscript('') + lastSpeechTimeRef.current = 0 + lastConfidenceRef.current = 0 + + // Clear any existing pause timeout + if (pauseTimeoutRef.current) { + clearTimeout(pauseTimeoutRef.current) + pauseTimeoutRef.current = null + } + + // Initialize and start recognition + const recognition = initializeRecognition() + if (recognition) { + recognitionRef.current = recognition + recognition.start() + } + } catch (error) { + console.error('โŒ Error starting Web Speech API:', error) + onError?.(error as Error) + } + }, [isSupported, initializeRecognition, onError]) + + // Stop recording + const stopRecording = useCallback(() => { + if (recognitionRef.current) { + console.log('๐Ÿ›‘ Stopping Web Speech API recording...') + recognitionRef.current.stop() + recognitionRef.current = null + } + }, []) + + // Cleanup + const cleanup = useCallback(() => { + if (recognitionRef.current) { + recognitionRef.current.stop() + recognitionRef.current = null + } + + // Clear pause timeout + if (pauseTimeoutRef.current) { + clearTimeout(pauseTimeoutRef.current) + pauseTimeoutRef.current = null + } + + setIsRecording(false) + setIsTranscribing(false) + }, []) + + // Cleanup on unmount + useEffect(() => { + return cleanup + }, [cleanup]) + + return { + isRecording, + isTranscribing, + transcript, + interimTranscript, + isSupported, + startRecording, + stopRecording, + cleanup + } +} + +// Export as default for compatibility +export default useWebSpeechTranscription diff --git a/src/hooks/useWhisperTranscription.ts b/src/hooks/useWhisperTranscription.ts deleted file mode 100644 index 184899b..0000000 --- a/src/hooks/useWhisperTranscription.ts +++ /dev/null @@ -1,329 +0,0 @@ -import { useCallback, useEffect, useRef, useState } from 'react' -import { getOpenAIConfig, isOpenAIConfigured } from '../lib/clientConfig' - -interface UseWhisperTranscriptionOptions { - apiKey?: string - onTranscriptUpdate?: (text: string) => void - onError?: (error: Error) => void - language?: string - enableStreaming?: boolean - removeSilence?: boolean -} - -export const useWhisperTranscription = ({ - apiKey, - onTranscriptUpdate, - onError, - language = 'en', - enableStreaming: _enableStreaming = true, - removeSilence: _removeSilence = true -}: UseWhisperTranscriptionOptions = {}) => { - const transcriptRef = useRef('') - const isRecordingRef = useRef(false) - const mediaRecorderRef = useRef(null) - const audioChunksRef = useRef([]) - const streamRef = useRef(null) - - // Get OpenAI API key from user profile settings - const openaiConfig = getOpenAIConfig() - const isConfigured = isOpenAIConfigured() - - // Custom state management - const [recording, setRecording] = useState(false) - const [speaking, setSpeaking] = useState(false) - const [transcribing, setTranscribing] = useState(false) - const [transcript, setTranscript] = useState({ text: '' }) - - // Custom startRecording implementation - const startRecording = useCallback(async () => { - try { - console.log('๐ŸŽค Starting custom recording...') - - // Get microphone access - const stream = await navigator.mediaDevices.getUserMedia({ - audio: { - echoCancellation: true, - noiseSuppression: true, - autoGainControl: true - } - }) - streamRef.current = stream - - // Debug the audio stream - console.log('๐ŸŽค Audio stream created:', stream) - console.log('๐ŸŽค Audio tracks:', stream.getAudioTracks().length) - console.log('๐ŸŽค Track settings:', stream.getAudioTracks()[0]?.getSettings()) - - // Set up audio level monitoring - const audioContext = new AudioContext() - const analyser = audioContext.createAnalyser() - const source = audioContext.createMediaStreamSource(stream) - source.connect(analyser) - analyser.fftSize = 256 - const bufferLength = analyser.frequencyBinCount - const dataArray = new Uint8Array(bufferLength) - - const checkAudioLevel = () => { - analyser.getByteFrequencyData(dataArray) - const average = dataArray.reduce((a, b) => a + b) / bufferLength - console.log('๐ŸŽต Audio level:', average.toFixed(2)) - if (mediaRecorderRef.current?.state === 'recording') { - requestAnimationFrame(checkAudioLevel) - } - } - checkAudioLevel() - - // Create MediaRecorder with fallback options - let mediaRecorder: MediaRecorder - const options = [ - { mimeType: 'audio/webm;codecs=opus' }, - { mimeType: 'audio/webm' }, - { mimeType: 'audio/mp4' }, - { mimeType: 'audio/wav' } - ] - - for (const option of options) { - if (MediaRecorder.isTypeSupported(option.mimeType)) { - console.log('๐ŸŽต Using MIME type:', option.mimeType) - mediaRecorder = new MediaRecorder(stream, option) - break - } - } - - if (!mediaRecorder!) { - throw new Error('No supported audio format found') - } - - mediaRecorderRef.current = mediaRecorder - audioChunksRef.current = [] - - // Handle data available - mediaRecorder.ondataavailable = (event) => { - console.log('๐ŸŽต Data available event fired!') - console.log('๐ŸŽต Data size:', event.data.size, 'bytes') - console.log('๐ŸŽต MediaRecorder state:', mediaRecorder.state) - console.log('๐ŸŽต Event data type:', event.data.type) - console.log('๐ŸŽต Current chunks count:', audioChunksRef.current.length) - - if (event.data.size > 0) { - audioChunksRef.current.push(event.data) - console.log('โœ… Chunk added successfully, total chunks:', audioChunksRef.current.length) - } else { - console.log('โš ๏ธ Empty data chunk received - this might be normal for the first chunk') - } - } - - // Handle MediaRecorder errors - mediaRecorder.onerror = (event) => { - console.error('โŒ MediaRecorder error:', event) - } - - // Handle MediaRecorder state changes - mediaRecorder.onstart = () => { - console.log('๐ŸŽค MediaRecorder started') - } - - // Handle recording stop - mediaRecorder.onstop = async () => { - console.log('๐Ÿ›‘ Recording stopped, processing audio...') - console.log('๐Ÿ›‘ Total chunks collected:', audioChunksRef.current.length) - console.log('๐Ÿ›‘ Chunk sizes:', audioChunksRef.current.map(chunk => chunk.size)) - setTranscribing(true) - - try { - // Create audio blob - const audioBlob = new Blob(audioChunksRef.current, { type: 'audio/webm' }) - console.log('๐ŸŽต Audio blob created:', audioBlob.size, 'bytes') - console.log('๐ŸŽต Audio chunks collected:', audioChunksRef.current.length) - console.log('๐ŸŽต Blob type:', audioBlob.type) - - if (audioBlob.size === 0) { - console.error('โŒ No audio data recorded!') - console.error('โŒ Chunks:', audioChunksRef.current) - console.error('โŒ Stream active:', streamRef.current?.active) - console.error('โŒ Stream tracks:', streamRef.current?.getTracks().length) - throw new Error('No audio data was recorded. Please check microphone permissions and try again.') - } - - // Transcribe with OpenAI - const apiKeyToUse = apiKey || openaiConfig?.apiKey - console.log('๐Ÿ”‘ Using API key:', apiKeyToUse ? 'present' : 'missing') - console.log('๐Ÿ”‘ API key length:', apiKeyToUse?.length || 0) - - if (!apiKeyToUse) { - throw new Error('No OpenAI API key available') - } - - const formData = new FormData() - formData.append('file', audioBlob, 'recording.webm') - formData.append('model', 'whisper-1') - formData.append('language', language) - formData.append('response_format', 'text') - - console.log('๐Ÿ“ค Sending request to OpenAI API...') - const response = await fetch('https://api.openai.com/v1/audio/transcriptions', { - method: 'POST', - headers: { - 'Authorization': `Bearer ${apiKeyToUse}`, - }, - body: formData - }) - - if (!response.ok) { - throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`) - } - - const transcriptionText = await response.text() - console.log('๐ŸŽฏ TRANSCRIPTION RESULT:', transcriptionText) - - setTranscript({ text: transcriptionText }) - onTranscriptUpdate?.(transcriptionText) - - } catch (error) { - console.error('โŒ Transcription error:', error) - onError?.(error as Error) - } finally { - setTranscribing(false) - } - } - - // Start recording with timeslice to get data chunks - mediaRecorder.start(1000) // 1-second chunks - setRecording(true) - isRecordingRef.current = true - console.log('โœ… Custom recording started with 1000ms timeslice') - console.log('๐ŸŽค MediaRecorder state after start:', mediaRecorder.state) - console.log('๐ŸŽค MediaRecorder mimeType:', mediaRecorder.mimeType) - - // Auto-stop after 10 seconds for testing (increased time) - setTimeout(() => { - if (mediaRecorderRef.current && mediaRecorderRef.current.state === 'recording') { - console.log('โฐ Auto-stopping recording after 10 seconds...') - mediaRecorderRef.current.stop() - } - }, 10000) - - // Add a test to check if we're getting any data after 2 seconds - setTimeout(() => { - console.log('๐Ÿงช 2-second test - chunks collected so far:', audioChunksRef.current.length) - console.log('๐Ÿงช 2-second test - chunk sizes:', audioChunksRef.current.map(chunk => chunk.size)) - console.log('๐Ÿงช 2-second test - MediaRecorder state:', mediaRecorderRef.current?.state) - }, 2000) - - } catch (error) { - console.error('โŒ Error starting custom recording:', error) - onError?.(error as Error) - } - }, [apiKey, openaiConfig?.apiKey, language, onTranscriptUpdate, onError]) - - // Custom stopRecording implementation - const stopRecording = useCallback(async () => { - try { - console.log('๐Ÿ›‘ Stopping custom recording...') - - if (mediaRecorderRef.current && mediaRecorderRef.current.state === 'recording') { - mediaRecorderRef.current.stop() - } - - if (streamRef.current) { - streamRef.current.getTracks().forEach(track => track.stop()) - streamRef.current = null - } - - setRecording(false) - isRecordingRef.current = false - console.log('โœ… Custom recording stopped') - - } catch (error) { - console.error('โŒ Error stopping custom recording:', error) - onError?.(error as Error) - } - }, [onError]) - - // Custom pauseRecording implementation (placeholder) - const pauseRecording = useCallback(async () => { - console.log('โธ๏ธ Pause recording not implemented in custom version') - }, []) - - // Update transcript when it changes - useEffect(() => { - if (transcript?.text && transcript.text !== transcriptRef.current) { - console.log('โœ… New transcript text received:', transcript.text) - console.log('๐ŸŽฏ TRANSCRIPT EMITTED TO CONSOLE:', transcript.text) - transcriptRef.current = transcript.text - onTranscriptUpdate?.(transcript.text) - } - }, [transcript?.text, onTranscriptUpdate]) - - // Handle recording state changes - useEffect(() => { - isRecordingRef.current = recording - }, [recording]) - - // Check if OpenAI is configured - useEffect(() => { - if (!isConfigured && !apiKey) { - onError?.(new Error('OpenAI API key not configured. Please set VITE_OPENAI_API_KEY in your environment variables.')) - } - }, [isConfigured, apiKey, onError]) - - const startTranscription = useCallback(async () => { - try { - console.log('๐ŸŽค Starting custom Whisper transcription...') - - // Check if OpenAI is configured - if (!isConfigured && !apiKey) { - console.error('โŒ No OpenAI API key found') - onError?.(new Error('OpenAI API key not configured. Please set VITE_OPENAI_API_KEY in your environment variables.')) - return - } - - await startRecording() - console.log('โœ… Custom Whisper transcription started') - - } catch (error) { - console.error('โŒ Error starting custom Whisper transcription:', error) - onError?.(error as Error) - } - }, [startRecording, onError, apiKey, isConfigured]) - - const stopTranscription = useCallback(async () => { - try { - console.log('๐Ÿ›‘ Stopping custom Whisper transcription...') - await stopRecording() - console.log('โœ… Custom Whisper transcription stopped') - } catch (error) { - console.error('โŒ Error stopping custom Whisper transcription:', error) - onError?.(error as Error) - } - }, [stopRecording, onError]) - - const pauseTranscription = useCallback(async () => { - try { - console.log('โธ๏ธ Pausing custom Whisper transcription...') - await pauseRecording() - console.log('โœ… Custom Whisper transcription paused') - } catch (error) { - console.error('โŒ Error pausing custom Whisper transcription:', error) - onError?.(error as Error) - } - }, [pauseRecording, onError]) - - return { - // State - isRecording: recording, - isSpeaking: speaking, - isTranscribing: transcribing, - transcript: transcript?.text || '', - - // Actions - startTranscription, - stopTranscription, - pauseTranscription, - - // Raw functions for advanced usage - startRecording, - stopRecording, - pauseRecording, - } -} \ No newline at end of file diff --git a/src/hooks/useWhisperTranscriptionSimple.ts b/src/hooks/useWhisperTranscriptionSimple.ts new file mode 100644 index 0000000..1be6b7c --- /dev/null +++ b/src/hooks/useWhisperTranscriptionSimple.ts @@ -0,0 +1,967 @@ +import { useCallback, useEffect, useRef, useState } from 'react' +import { pipeline, env } from '@xenova/transformers' + +// Configure the transformers library +env.allowRemoteModels = true +env.allowLocalModels = false +env.useBrowserCache = true +env.useCustomCache = false + +// Helper function to detect audio format from blob +function detectAudioFormat(blob: Blob): Promise { + if (blob.type && blob.type !== 'application/octet-stream') { + return Promise.resolve(blob.type) + } + + // Try to detect from the first few bytes + return new Promise((resolve) => { + const reader = new FileReader() + reader.onload = () => { + try { + const arrayBuffer = reader.result as ArrayBuffer + if (!arrayBuffer || arrayBuffer.byteLength < 4) { + resolve('audio/webm;codecs=opus') // Default fallback + return + } + + const uint8Array = new Uint8Array(arrayBuffer.slice(0, 12)) + + // Check for common audio format signatures + if (uint8Array[0] === 0x52 && uint8Array[1] === 0x49 && uint8Array[2] === 0x46 && uint8Array[3] === 0x46) { + resolve('audio/wav') + } else if (uint8Array[0] === 0x4F && uint8Array[1] === 0x67 && uint8Array[2] === 0x67 && uint8Array[3] === 0x53) { + resolve('audio/ogg;codecs=opus') + } else if (uint8Array[0] === 0x1A && uint8Array[1] === 0x45 && uint8Array[2] === 0xDF && uint8Array[3] === 0xA3) { + resolve('audio/webm;codecs=opus') + } else { + resolve('audio/webm;codecs=opus') // Default fallback + } + } catch (error) { + console.warn('โš ๏ธ Error detecting audio format:', error) + resolve('audio/webm;codecs=opus') // Default fallback + } + } + reader.onerror = () => { + resolve('audio/webm;codecs=opus') // Default fallback + } + reader.readAsArrayBuffer(blob.slice(0, 12)) + }) +} + +// Simple resampling function for audio data +function resampleAudio(audioData: Float32Array, fromSampleRate: number, toSampleRate: number): Float32Array { + if (fromSampleRate === toSampleRate) { + return audioData + } + + // Validate input parameters + if (!audioData || audioData.length === 0) { + throw new Error('Invalid audio data for resampling') + } + + if (fromSampleRate <= 0 || toSampleRate <= 0) { + throw new Error('Invalid sample rates for resampling') + } + + const ratio = fromSampleRate / toSampleRate + const newLength = Math.floor(audioData.length / ratio) + + // Ensure we have a valid length + if (newLength <= 0) { + throw new Error('Invalid resampled length') + } + + const resampled = new Float32Array(newLength) + + for (let i = 0; i < newLength; i++) { + const sourceIndex = Math.floor(i * ratio) + // Ensure sourceIndex is within bounds + if (sourceIndex >= 0 && sourceIndex < audioData.length) { + resampled[i] = audioData[sourceIndex] + } else { + resampled[i] = 0 + } + } + + return resampled +} + +interface ModelOption { + name: string + options: { + quantized: boolean + use_browser_cache: boolean + use_custom_cache: boolean + } +} + +interface UseWhisperTranscriptionOptions { + onTranscriptUpdate?: (text: string) => void + onError?: (error: Error) => void + language?: string + enableStreaming?: boolean + enableAdvancedErrorHandling?: boolean + modelOptions?: ModelOption[] + autoInitialize?: boolean // If false, model will only load when startRecording is called +} + +export const useWhisperTranscription = ({ + onTranscriptUpdate, + onError, + language = 'en', + enableStreaming = false, + enableAdvancedErrorHandling = false, + modelOptions, + autoInitialize = true // Default to true for backward compatibility +}: UseWhisperTranscriptionOptions = {}) => { + const [isRecording, setIsRecording] = useState(false) + const [isTranscribing, setIsTranscribing] = useState(false) + const [isSpeaking, setIsSpeaking] = useState(false) + const [transcript, setTranscript] = useState('') + const [modelLoaded, setModelLoaded] = useState(false) + + const transcriberRef = useRef(null) + const streamRef = useRef(null) + const mediaRecorderRef = useRef(null) + const audioChunksRef = useRef([]) + const isRecordingRef = useRef(false) + const transcriptRef = useRef('') + const streamingTranscriptRef = useRef('') + const periodicTranscriptionRef = useRef(null) + const lastTranscriptionTimeRef = useRef(0) + const lastSpeechTimeRef = useRef(0) + const previousTranscriptLengthRef = useRef(0) // Track previous transcript length for continuous transcription + + // Function to process transcript with line breaks and punctuation + const processTranscript = useCallback((text: string, isStreaming: boolean = false) => { + if (!text.trim()) return text + + let processedText = text.trim() + + // Add punctuation if missing at the end + if (!/[.!?]$/.test(processedText)) { + processedText += '.' + } + + // Add line break if there's been a pause (for streaming) + if (isStreaming) { + const now = Date.now() + const timeSinceLastSpeech = now - lastSpeechTimeRef.current + + // If more than 3 seconds since last speech, add a line break + if (timeSinceLastSpeech > 3000 && lastSpeechTimeRef.current > 0) { + processedText = '\n' + processedText + } + + lastSpeechTimeRef.current = now + } + + return processedText + }, []) + + // Initialize transcriber with optional advanced error handling + const initializeTranscriber = useCallback(async () => { + if (transcriberRef.current) return transcriberRef.current + + try { + console.log('๐Ÿค– Loading Whisper model...') + + // Check if we're running in a CORS-restricted environment + if (typeof window !== 'undefined' && window.location.protocol === 'file:') { + console.warn('โš ๏ธ Running from file:// protocol - CORS issues may occur') + console.warn('๐Ÿ’ก Consider running from a local development server for better compatibility') + } + + if (enableAdvancedErrorHandling && modelOptions) { + // Use advanced model loading with fallbacks + let transcriber = null + let lastError = null + + for (const modelOption of modelOptions) { + try { + console.log(`๐Ÿ”„ Trying model: ${modelOption.name}`) + transcriber = await pipeline('automatic-speech-recognition', modelOption.name, { + ...modelOption.options, + progress_callback: (progress: any) => { + if (progress.status === 'downloading') { + console.log(`๐Ÿ“ฆ Downloading model: ${progress.file} (${Math.round(progress.progress * 100)}%)`) + } + } + }) + console.log(`โœ… Successfully loaded model: ${modelOption.name}`) + break + } catch (error) { + console.warn(`โš ๏ธ Failed to load model ${modelOption.name}:`, error) + lastError = error + continue + } + } + + if (!transcriber) { + throw lastError || new Error('Failed to load any model') + } + + transcriberRef.current = transcriber + setModelLoaded(true) + return transcriber + } else { + // Simple model loading (default behavior) with fallback + const modelOptions = [ + 'Xenova/whisper-tiny.en', + 'Xenova/whisper-tiny' + ] + + let transcriber = null + let lastError = null + + for (const modelName of modelOptions) { + try { + // Reduced debug logging + + const loadPromise = pipeline('automatic-speech-recognition', modelName, { + quantized: true, + progress_callback: (progress: any) => { + if (progress.status === 'downloading') { + console.log(`๐Ÿ“ฆ Downloading model: ${progress.file} (${Math.round(progress.progress * 100)}%)`) + } else if (progress.status === 'loading') { + console.log(`๐Ÿ”„ Loading model: ${progress.file}`) + } + } + }) + + const timeoutPromise = new Promise((_, reject) => + setTimeout(() => reject(new Error('Model loading timeout')), 60000) // 60 seconds timeout + ) + + transcriber = await Promise.race([loadPromise, timeoutPromise]) + + transcriberRef.current = transcriber + setModelLoaded(true) + console.log(`โœ… Whisper model loaded: ${modelName}`) + + return transcriber + } catch (error) { + // Reduced error logging - only show final error + lastError = error + continue + } + } + + // If all models failed, throw the last error + throw lastError || new Error('Failed to load any Whisper model') + } + } catch (error) { + console.error('โŒ Failed to load model:', error) + onError?.(error as Error) + throw error + } + }, [onError, enableAdvancedErrorHandling, modelOptions]) + + // Handle streaming transcript updates + const handleStreamingTranscriptUpdate = useCallback((newText: string) => { + if (newText.trim()) { + const newTextTrimmed = newText.trim() + const currentTranscript = streamingTranscriptRef.current.trim() + + if (currentTranscript === '') { + streamingTranscriptRef.current = newTextTrimmed + } else { + // Check if the new text is already contained in the current transcript + if (!currentTranscript.includes(newTextTrimmed)) { + streamingTranscriptRef.current = currentTranscript + ' ' + newTextTrimmed + } else { + // Find the best overlap point to avoid duplicates + const words = newTextTrimmed.split(' ') + const currentWords = currentTranscript.split(' ') + + let overlapIndex = 0 + let maxOverlap = 0 + + for (let i = 1; i <= Math.min(words.length, currentWords.length); i++) { + const currentEnd = currentWords.slice(-i).join(' ') + const newStart = words.slice(0, i).join(' ') + + if (currentEnd === newStart && i > maxOverlap) { + maxOverlap = i + overlapIndex = i + } + } + + if (overlapIndex > 0 && overlapIndex < words.length) { + const newPart = words.slice(overlapIndex).join(' ') + streamingTranscriptRef.current = currentTranscript + ' ' + newPart + } + } + } + + const processedTranscript = processTranscript(streamingTranscriptRef.current, true) + streamingTranscriptRef.current = processedTranscript + setTranscript(processedTranscript) + + // Only send the new portion for continuous transcription + const newTextPortion = processedTranscript.substring(previousTranscriptLengthRef.current) + if (newTextPortion.trim()) { + onTranscriptUpdate?.(newTextPortion) + previousTranscriptLengthRef.current = processedTranscript.length + } + + console.log(`๐Ÿ“ Real-time transcript updated: "${newTextTrimmed}" -> Total: "${processedTranscript}"`) + console.log(`๐Ÿ”„ Streaming transcript state updated, calling onTranscriptUpdate with: "${processedTranscript}"`) + } + }, [onTranscriptUpdate, processTranscript]) + + // Process accumulated audio chunks for streaming transcription + const processAccumulatedAudioChunks = useCallback(async () => { + try { + // Throttle transcription requests + const now = Date.now() + if (now - (lastTranscriptionTimeRef.current || 0) < 800) { // Reduced to 0.8 seconds for better responsiveness + return // Skip if less than 0.8 seconds since last transcription + } + + const chunks = audioChunksRef.current || [] + if (chunks.length === 0 || chunks.length < 2) { + console.log(`โš ๏ธ Not enough chunks for real-time processing: ${chunks.length}`) + return + } + + // Take the last 4-5 chunks for balanced processing (1-2 seconds) + const recentChunks = chunks.slice(-5) + const validChunks = recentChunks.filter(chunk => chunk && chunk.size > 2000) // Filter out small chunks + + if (validChunks.length < 2) { + console.log(`โš ๏ธ Not enough valid chunks for real-time processing: ${validChunks.length}`) + return + } + + const totalSize = validChunks.reduce((sum, chunk) => sum + chunk.size, 0) + if (totalSize < 20000) { // Increased to 20KB for reliable decoding + console.log(`โš ๏ธ Not enough audio data for real-time processing: ${totalSize} bytes`) + return + } + + // Use the MIME type from the MediaRecorder, not individual chunks + let mimeType = 'audio/webm;codecs=opus' // Default to WebM + if (mediaRecorderRef.current && mediaRecorderRef.current.mimeType) { + mimeType = mediaRecorderRef.current.mimeType + } + + console.log(`๐Ÿ”„ Real-time processing ${validChunks.length} chunks, total size: ${totalSize} bytes, type: ${mimeType}`) + console.log(`๐Ÿ”„ Chunk sizes:`, validChunks.map(c => c.size)) + console.log(`๐Ÿ”„ Chunk types:`, validChunks.map(c => c.type)) + + // Create a more robust blob with proper headers + const tempBlob = new Blob(validChunks, { type: mimeType }) + + // Validate blob size + if (tempBlob.size < 10000) { + console.log(`โš ๏ธ Blob too small for processing: ${tempBlob.size} bytes`) + return + } + + const audioBuffer = await tempBlob.arrayBuffer() + + // Validate audio buffer + if (audioBuffer.byteLength < 10000) { + console.log(`โš ๏ธ Audio buffer too small: ${audioBuffer.byteLength} bytes`) + return + } + + const audioContext = new AudioContext() + let audioBufferFromBlob: AudioBuffer + + try { + // Try to decode the audio buffer + audioBufferFromBlob = await audioContext.decodeAudioData(audioBuffer) + console.log(`โœ… Successfully decoded real-time audio buffer: ${audioBufferFromBlob.length} samples`) + } catch (decodeError) { + console.log('โš ๏ธ Real-time chunk decode failed, trying alternative approach:', decodeError) + + // Try alternative approach: create a new blob with different MIME type + try { + const alternativeBlob = new Blob(validChunks, { type: 'audio/webm' }) + const alternativeBuffer = await alternativeBlob.arrayBuffer() + audioBufferFromBlob = await audioContext.decodeAudioData(alternativeBuffer) + console.log(`โœ… Successfully decoded with alternative approach: ${audioBufferFromBlob.length} samples`) + } catch (altError) { + console.log('โš ๏ธ Alternative decode also failed, skipping:', altError) + await audioContext.close() + return + } + } + + await audioContext.close() + + const audioData = audioBufferFromBlob.getChannelData(0) + if (!audioData || audioData.length === 0) { + return + } + + // Resample if necessary + let processedAudioData: Float32Array = audioData + if (audioBufferFromBlob.sampleRate !== 16000) { + processedAudioData = resampleAudio(audioData as Float32Array, audioBufferFromBlob.sampleRate, 16000) + } + + // Check for meaningful audio content + const rms = Math.sqrt(processedAudioData.reduce((sum, val) => sum + val * val, 0) / processedAudioData.length) + const maxAmplitude = Math.max(...processedAudioData.map(Math.abs)) + const dynamicRange = maxAmplitude - Math.min(...processedAudioData.map(Math.abs)) + + console.log(`๐Ÿ”Š Real-time audio analysis: RMS=${rms.toFixed(6)}, Max=${maxAmplitude.toFixed(6)}, Range=${dynamicRange.toFixed(6)}`) + + if (rms < 0.001) { + console.log('โš ๏ธ Audio too quiet for transcription (RMS < 0.001)') + return // Skip very quiet audio + } + + if (dynamicRange < 0.01) { + console.log('โš ๏ธ Audio has very low dynamic range, may be mostly noise') + return + } + + // Ensure reasonable length for real-time processing (max 2 seconds for balanced speed) + const maxRealtimeSamples = 32000 // 2 seconds at 16kHz + if (processedAudioData.length > maxRealtimeSamples) { + processedAudioData = processedAudioData.slice(-maxRealtimeSamples) + } + + if (processedAudioData.length < 2000) { // Increased to 2 second minimum for reliable processing + return // Skip very short audio + } + + console.log(`๐ŸŽต Real-time audio: ${processedAudioData.length} samples (${(processedAudioData.length / 16000).toFixed(2)}s)`) + + // Transcribe with parameters optimized for real-time processing + const result = await transcriberRef.current(processedAudioData, { + language: language, + task: 'transcribe', + return_timestamps: false, + chunk_length_s: 5, // Longer chunks for better context + stride_length_s: 2, // Larger stride for better coverage + no_speech_threshold: 0.3, // Higher threshold to reduce noise + logprob_threshold: -0.8, // More sensitive detection + compression_ratio_threshold: 2.0 // More permissive for real-time + }) + + const transcriptionText = result?.text || '' + if (transcriptionText.trim()) { + lastTranscriptionTimeRef.current = Date.now() + console.log(`โœ… Real-time transcript: "${transcriptionText.trim()}"`) + console.log(`๐Ÿ”„ Calling handleStreamingTranscriptUpdate with: "${transcriptionText.trim()}"`) + handleStreamingTranscriptUpdate(transcriptionText.trim()) + } else { + console.log('โš ๏ธ No real-time transcription text produced, trying fallback parameters...') + + // Try with more permissive parameters for real-time processing + try { + const fallbackResult = await transcriberRef.current(processedAudioData, { + task: 'transcribe', + return_timestamps: false, + chunk_length_s: 3, // Shorter chunks for fallback + stride_length_s: 1, // Smaller stride for fallback + no_speech_threshold: 0.1, // Very low threshold for fallback + logprob_threshold: -1.2, // Very sensitive for fallback + compression_ratio_threshold: 2.5 // Very permissive for fallback + }) + + const fallbackText = fallbackResult?.text || '' + if (fallbackText.trim()) { + console.log(`โœ… Fallback real-time transcript: "${fallbackText.trim()}"`) + lastTranscriptionTimeRef.current = Date.now() + handleStreamingTranscriptUpdate(fallbackText.trim()) + } else { + console.log('โš ๏ธ Fallback transcription also produced no text') + } + } catch (fallbackError) { + console.log('โš ๏ธ Fallback transcription failed:', fallbackError) + } + } + + } catch (error) { + console.error('โŒ Error processing accumulated audio chunks:', error) + } + }, [handleStreamingTranscriptUpdate, language]) + + // Process recorded audio chunks (final processing) + const processAudioChunks = useCallback(async () => { + if (!transcriberRef.current || audioChunksRef.current.length === 0) { + console.log('โš ๏ธ No transcriber or audio chunks to process') + return + } + + // Ensure model is loaded + if (!modelLoaded) { + console.log('โš ๏ธ Model not loaded yet, waiting...') + try { + await initializeTranscriber() + } catch (error) { + console.error('โŒ Failed to initialize transcriber:', error) + onError?.(error as Error) + return + } + } + + try { + setIsTranscribing(true) + console.log('๐Ÿ”„ Processing final audio chunks...') + + // Create a blob from all chunks with proper MIME type detection + let mimeType = 'audio/webm;codecs=opus' + if (audioChunksRef.current.length > 0 && audioChunksRef.current[0].type) { + mimeType = audioChunksRef.current[0].type + } + + // Filter out small chunks that might be corrupted + const validChunks = audioChunksRef.current.filter(chunk => chunk && chunk.size > 1000) + + if (validChunks.length === 0) { + console.log('โš ๏ธ No valid audio chunks to process') + return + } + + console.log(`๐Ÿ”„ Processing ${validChunks.length} valid chunks out of ${audioChunksRef.current.length} total chunks`) + + const audioBlob = new Blob(validChunks, { type: mimeType }) + + // Validate blob size + if (audioBlob.size < 10000) { + console.log(`โš ๏ธ Audio blob too small for processing: ${audioBlob.size} bytes`) + return + } + + // Convert blob to array buffer + const arrayBuffer = await audioBlob.arrayBuffer() + + // Validate array buffer + if (arrayBuffer.byteLength < 10000) { + console.log(`โš ๏ธ Audio buffer too small: ${arrayBuffer.byteLength} bytes`) + return + } + + // Create audio context to convert to Float32Array + const audioContext = new AudioContext() + + let audioBuffer: AudioBuffer + try { + audioBuffer = await audioContext.decodeAudioData(arrayBuffer) + console.log(`โœ… Successfully decoded final audio buffer: ${audioBuffer.length} samples`) + } catch (decodeError) { + console.error('โŒ Failed to decode final audio buffer:', decodeError) + + // Try alternative approach with different MIME type + try { + console.log('๐Ÿ”„ Trying alternative MIME type for final processing...') + const alternativeBlob = new Blob(validChunks, { type: 'audio/webm' }) + const alternativeBuffer = await alternativeBlob.arrayBuffer() + audioBuffer = await audioContext.decodeAudioData(alternativeBuffer) + console.log(`โœ… Successfully decoded with alternative approach: ${audioBuffer.length} samples`) + } catch (altError) { + console.error('โŒ Alternative decode also failed:', altError) + await audioContext.close() + throw new Error('Failed to decode audio data. The audio format may not be supported or the data may be corrupted.') + } + } + + await audioContext.close() + + // Get the first channel as Float32Array + const audioData = audioBuffer.getChannelData(0) + + console.log(`๐Ÿ” Audio buffer info: sampleRate=${audioBuffer.sampleRate}, length=${audioBuffer.length}, duration=${audioBuffer.duration}s`) + console.log(`๐Ÿ” Audio data: length=${audioData.length}, first 10 values:`, Array.from(audioData.slice(0, 10))) + + // Check for meaningful audio content + const rms = Math.sqrt(audioData.reduce((sum, val) => sum + val * val, 0) / audioData.length) + console.log(`๐Ÿ”Š Audio RMS level: ${rms.toFixed(6)}`) + + if (rms < 0.001) { + console.log('โš ๏ธ Audio appears to be mostly silence (RMS < 0.001)') + } + + // Resample if necessary + let processedAudioData: Float32Array = audioData + if (audioBuffer.sampleRate !== 16000) { + console.log(`๐Ÿ”„ Resampling from ${audioBuffer.sampleRate}Hz to 16000Hz`) + processedAudioData = resampleAudio(audioData as Float32Array, audioBuffer.sampleRate, 16000) + } + + console.log(`๐ŸŽต Processing audio: ${processedAudioData.length} samples (${(processedAudioData.length / 16000).toFixed(2)}s)`) + + // Check if transcriber is available + if (!transcriberRef.current) { + console.error('โŒ Transcriber not available for processing') + throw new Error('Transcriber not initialized') + } + + console.log('๐Ÿ”„ Starting transcription with Whisper model...') + + // Transcribe the audio + const result = await transcriberRef.current(processedAudioData, { + language: language, + task: 'transcribe', + return_timestamps: false + }) + + console.log('๐Ÿ” Transcription result:', result) + + const newText = result?.text?.trim() || '' + if (newText) { + const processedText = processTranscript(newText, enableStreaming) + + if (enableStreaming) { + // For streaming mode, merge with existing streaming transcript + handleStreamingTranscriptUpdate(processedText) + } else { + // For non-streaming mode, append to existing transcript + const currentTranscript = transcriptRef.current + const updatedTranscript = currentTranscript ? `${currentTranscript} ${processedText}` : processedText + + transcriptRef.current = updatedTranscript + setTranscript(updatedTranscript) + + // Only send the new portion for continuous transcription + const newTextPortion = updatedTranscript.substring(previousTranscriptLengthRef.current) + if (newTextPortion.trim()) { + onTranscriptUpdate?.(newTextPortion) + previousTranscriptLengthRef.current = updatedTranscript.length + } + + console.log(`โœ… Transcription: "${processedText}" -> Total: "${updatedTranscript}"`) + } + } else { + console.log('โš ๏ธ No transcription text produced') + console.log('๐Ÿ” Full transcription result object:', result) + + // Try alternative transcription parameters + console.log('๐Ÿ”„ Trying alternative transcription parameters...') + try { + const altResult = await transcriberRef.current(processedAudioData, { + task: 'transcribe', + return_timestamps: false + }) + console.log('๐Ÿ” Alternative transcription result:', altResult) + + if (altResult?.text?.trim()) { + const processedAltText = processTranscript(altResult.text, enableStreaming) + console.log('โœ… Alternative transcription successful:', processedAltText) + const currentTranscript = transcriptRef.current + const updatedTranscript = currentTranscript ? `${currentTranscript} ${processedAltText}` : processedAltText + + transcriptRef.current = updatedTranscript + setTranscript(updatedTranscript) + + // Only send the new portion for continuous transcription + const newTextPortion = updatedTranscript.substring(previousTranscriptLengthRef.current) + if (newTextPortion.trim()) { + onTranscriptUpdate?.(newTextPortion) + previousTranscriptLengthRef.current = updatedTranscript.length + } + } + } catch (altError) { + console.log('โš ๏ธ Alternative transcription also failed:', altError) + } + } + + // Clear processed chunks + audioChunksRef.current = [] + + } catch (error) { + console.error('โŒ Error processing audio:', error) + onError?.(error as Error) + } finally { + setIsTranscribing(false) + } + }, [transcriberRef, language, onTranscriptUpdate, onError, enableStreaming, handleStreamingTranscriptUpdate, modelLoaded, initializeTranscriber]) + + // Start recording + const startRecording = useCallback(async () => { + try { + console.log('๐ŸŽค Starting recording...') + console.log('๐Ÿ” enableStreaming in startRecording:', enableStreaming) + + // Ensure model is loaded before starting + if (!modelLoaded) { + console.log('๐Ÿ”„ Model not loaded, initializing...') + await initializeTranscriber() + } + + // Don't reset transcripts for continuous transcription - keep existing content + // transcriptRef.current = '' + // streamingTranscriptRef.current = '' + // setTranscript('') + lastSpeechTimeRef.current = 0 + audioChunksRef.current = [] + lastTranscriptionTimeRef.current = 0 + + // Clear any existing periodic transcription timer + if (periodicTranscriptionRef.current) { + clearInterval(periodicTranscriptionRef.current) + periodicTranscriptionRef.current = null + } + + // Get microphone access + const stream = await navigator.mediaDevices.getUserMedia({ + audio: { + echoCancellation: true, + noiseSuppression: true, + autoGainControl: true, + sampleRate: 44100, + channelCount: 1 + } + }) + + streamRef.current = stream + + // Create MediaRecorder with fallback options + let mediaRecorder: MediaRecorder + const options = [ + { mimeType: 'audio/webm;codecs=opus' }, + { mimeType: 'audio/webm' }, + { mimeType: 'audio/ogg;codecs=opus' }, + { mimeType: 'audio/ogg' }, + { mimeType: 'audio/wav' }, + { mimeType: 'audio/mp4' } + ] + + for (const option of options) { + if (MediaRecorder.isTypeSupported(option.mimeType)) { + console.log('๐ŸŽต Using MIME type:', option.mimeType) + mediaRecorder = new MediaRecorder(stream, option) + break + } + } + + if (!mediaRecorder!) { + throw new Error('No supported audio format found') + } + + // Store the MIME type for later use + const mimeType = mediaRecorder.mimeType + console.log('๐ŸŽต Final MIME type:', mimeType) + + mediaRecorderRef.current = mediaRecorder + + // Handle data available + mediaRecorder.ondataavailable = (event) => { + if (event.data.size > 0) { + // Validate chunk before adding + if (event.data.size > 1000) { // Only add chunks with meaningful size + audioChunksRef.current.push(event.data) + console.log(`๐Ÿ“ฆ Received chunk ${audioChunksRef.current.length}, size: ${event.data.size} bytes, type: ${event.data.type}`) + + // Limit the number of chunks to prevent memory issues + if (audioChunksRef.current.length > 20) { + audioChunksRef.current = audioChunksRef.current.slice(-15) // Keep last 15 chunks + } + } else { + console.log(`โš ๏ธ Skipping small chunk: ${event.data.size} bytes`) + } + } + } + + // Handle recording stop + mediaRecorder.onstop = () => { + console.log('๐Ÿ›‘ Recording stopped, processing audio...') + processAudioChunks() + } + + // Handle MediaRecorder state changes + mediaRecorder.onstart = () => { + console.log('๐ŸŽค MediaRecorder started') + console.log('๐Ÿ” enableStreaming value:', enableStreaming) + setIsRecording(true) + isRecordingRef.current = true + + // Start periodic transcription processing for streaming mode + if (enableStreaming) { + console.log('๐Ÿ”„ Starting streaming transcription (every 0.8 seconds)') + periodicTranscriptionRef.current = setInterval(() => { + console.log('๐Ÿ”„ Interval triggered, isRecordingRef.current:', isRecordingRef.current) + if (isRecordingRef.current) { + console.log('๐Ÿ”„ Running periodic streaming transcription...') + processAccumulatedAudioChunks() + } else { + console.log('โš ๏ธ Not running transcription - recording stopped') + } + }, 800) // Update every 0.8 seconds for better responsiveness + } else { + console.log('โ„น๏ธ Streaming transcription disabled - enableStreaming is false') + } + } + + // Start recording with appropriate timeslice + const timeslice = enableStreaming ? 1000 : 2000 // Larger chunks for more stable processing + console.log(`๐ŸŽต Starting recording with ${timeslice}ms timeslice`) + mediaRecorder.start(timeslice) + isRecordingRef.current = true + setIsRecording(true) + + console.log('โœ… Recording started - MediaRecorder state:', mediaRecorder.state) + + } catch (error) { + console.error('โŒ Error starting recording:', error) + onError?.(error as Error) + } + }, [processAudioChunks, processAccumulatedAudioChunks, onError, enableStreaming, modelLoaded, initializeTranscriber]) + + // Stop recording + const stopRecording = useCallback(async () => { + try { + console.log('๐Ÿ›‘ Stopping recording...') + + // Clear periodic transcription timer + if (periodicTranscriptionRef.current) { + clearInterval(periodicTranscriptionRef.current) + periodicTranscriptionRef.current = null + } + + if (mediaRecorderRef.current && isRecordingRef.current) { + mediaRecorderRef.current.stop() + } + + if (streamRef.current) { + streamRef.current.getTracks().forEach(track => track.stop()) + streamRef.current = null + } + + isRecordingRef.current = false + setIsRecording(false) + + console.log('โœ… Recording stopped') + + } catch (error) { + console.error('โŒ Error stopping recording:', error) + onError?.(error as Error) + } + }, [onError]) + + // Pause recording (placeholder for compatibility) + const pauseRecording = useCallback(async () => { + console.log('โธ๏ธ Pause recording not implemented') + }, []) + + // Cleanup function + const cleanup = useCallback(() => { + console.log('๐Ÿงน Cleaning up transcription resources...') + + // Stop recording if active + if (isRecordingRef.current) { + setIsRecording(false) + isRecordingRef.current = false + } + + // Clear periodic transcription timer + if (periodicTranscriptionRef.current) { + clearInterval(periodicTranscriptionRef.current) + periodicTranscriptionRef.current = null + } + + // Stop MediaRecorder if active + if (mediaRecorderRef.current && mediaRecorderRef.current.state === 'recording') { + mediaRecorderRef.current.stop() + } + + // Stop audio stream + if (streamRef.current) { + streamRef.current.getTracks().forEach(track => track.stop()) + streamRef.current = null + } + + // Clear chunks + audioChunksRef.current = [] + + console.log('โœ… Cleanup completed') + }, []) + + // Convenience functions for compatibility + const startTranscription = useCallback(async () => { + try { + console.log('๐ŸŽค Starting transcription...') + + // Reset all transcription state for clean start + streamingTranscriptRef.current = '' + setTranscript('') + setIsRecording(false) + isRecordingRef.current = false + lastTranscriptionTimeRef.current = 0 + + // Clear any existing timers + if (periodicTranscriptionRef.current) { + clearInterval(periodicTranscriptionRef.current) + periodicTranscriptionRef.current = null + } + + // Initialize the model if not already loaded + if (!modelLoaded) { + await initializeTranscriber() + } + + await startRecording() + console.log('โœ… Transcription started') + + } catch (error) { + console.error('โŒ Error starting transcription:', error) + onError?.(error as Error) + } + }, [startRecording, onError, modelLoaded, initializeTranscriber]) + + const stopTranscription = useCallback(async () => { + try { + console.log('๐Ÿ›‘ Stopping transcription...') + await stopRecording() + console.log('โœ… Transcription stopped') + } catch (error) { + console.error('โŒ Error stopping transcription:', error) + onError?.(error as Error) + } + }, [stopRecording, onError]) + + const pauseTranscription = useCallback(async () => { + try { + console.log('โธ๏ธ Pausing transcription...') + await pauseRecording() + console.log('โœ… Transcription paused') + } catch (error) { + console.error('โŒ Error pausing transcription:', error) + onError?.(error as Error) + } + }, [pauseRecording, onError]) + + // Initialize model on mount (only if autoInitialize is true) + useEffect(() => { + if (autoInitialize) { + initializeTranscriber().catch(console.warn) + } + }, [initializeTranscriber, autoInitialize]) + + // Cleanup on unmount + useEffect(() => { + return () => { + cleanup() + } + }, [cleanup]) + + return { + // State + isRecording, + isSpeaking, + isTranscribing, + transcript, + modelLoaded, + + // Actions + startTranscription, + stopTranscription, + pauseTranscription, + + // Raw functions for advanced usage + startRecording, + stopRecording, + pauseRecording, + cleanup + } +} + +// Export both the new consolidated hook and the old name for backward compatibility +export const useWhisperTranscriptionSimple = useWhisperTranscription diff --git a/src/lib/HoloSphereService.ts b/src/lib/HoloSphereService.ts new file mode 100644 index 0000000..f9e5f86 --- /dev/null +++ b/src/lib/HoloSphereService.ts @@ -0,0 +1,443 @@ +import HoloSphere from 'holosphere' +import * as h3 from 'h3-js' + +export interface HolonData { + id: string + name: string + description?: string + latitude: number + longitude: number + resolution: number + data: Record + timestamp: number +} + +export interface HolonLens { + name: string + schema?: any + data: any[] +} + +export interface HolonConnection { + id: string + name: string + type: 'federation' | 'reference' + targetSpace: string + status: 'connected' | 'disconnected' | 'error' +} + +export class HoloSphereService { + private sphere!: HoloSphere + private isInitialized: boolean = false + private connections: Map = new Map() + private connectionErrorLogged: boolean = false // Track if we've already logged connection errors + + constructor(appName: string = 'canvas-holons', strict: boolean = false, openaiKey?: string) { + try { + this.sphere = new HoloSphere(appName, strict, openaiKey) + this.isInitialized = true + console.log('โœ… HoloSphere service initialized') + } catch (error) { + console.error('โŒ Failed to initialize HoloSphere:', error) + this.isInitialized = false + } + } + + async initialize(): Promise { + if (!this.isInitialized) { + console.error('โŒ HoloSphere not initialized') + return false + } + return true + } + + // Get a holon for specific coordinates and resolution + async getHolon(lat: number, lng: number, resolution: number): Promise { + if (!this.isInitialized) return '' + try { + return await this.sphere.getHolon(lat, lng, resolution) + } catch (error) { + console.error('โŒ Error getting holon:', error) + return '' + } + } + + // Store data in a holon + async putData(holon: string, lens: string, data: any): Promise { + if (!this.isInitialized) return false + try { + await this.sphere.put(holon, lens, data) + return true + } catch (error) { + console.error('โŒ Error storing data:', error) + return false + } + } + + // Retrieve data from a holon + async getData(holon: string, lens: string, key?: string): Promise { + if (!this.isInitialized) return null + try { + if (key) { + return await this.sphere.get(holon, lens, key) + } else { + return await this.sphere.getAll(holon, lens) + } + } catch (error) { + console.error('โŒ Error retrieving data:', error) + return null + } + } + + // Retrieve data with subscription and timeout (better for Gun's async nature) + async getDataWithWait(holon: string, lens: string, timeoutMs: number = 5000): Promise { + if (!this.isInitialized) { + console.log(`โš ๏ธ HoloSphere not initialized for ${lens}`) + return null + } + + // Check for WebSocket connection issues + // Note: GunDB connection errors appear in browser console, we can't directly detect them + // but we can provide better feedback when no data is received + + return new Promise((resolve) => { + let resolved = false + let collectedData: any = {} + let subscriptionActive = false + + console.log(`๐Ÿ” getDataWithWait: holon=${holon}, lens=${lens}, timeout=${timeoutMs}ms`) + + // Listen for WebSocket errors (they appear in console but we can't catch them directly) + // Instead, we'll detect the pattern: subscription never fires + getAll never resolves + + // Set up timeout (increased default to 5 seconds for network sync) + const timeout = setTimeout(() => { + if (!resolved) { + resolved = true + const keyCount = Object.keys(collectedData).length + const status = subscriptionActive + ? '(subscription was active)' + : '(subscription never fired - possible WebSocket connection issue)' + + console.log(`โฑ๏ธ Timeout for lens ${lens}, returning collected data:`, keyCount, 'keys', status) + + // If no data and subscription never fired, it's likely a connection issue + // Only log this once to avoid console spam + if (keyCount === 0 && !subscriptionActive && !this.connectionErrorLogged) { + this.connectionErrorLogged = true + console.error(`โŒ GunDB Connection Issue: WebSocket to 'wss://gun.holons.io/gun' is failing`) + console.error(`๐Ÿ’ก This prevents loading data from the Holosphere. Possible causes:`) + console.error(` โ€ข GunDB server may be down or unreachable`) + console.error(` โ€ข Network/firewall blocking WebSocket connections`) + console.error(` โ€ข Check browser console for WebSocket connection errors`) + console.error(` โ€ข Data will not load until connection is established`) + } + + resolve(keyCount > 0 ? collectedData : null) + } + }, timeoutMs) + + try { + // Check if methods exist + if (!this.sphere.subscribe) { + console.error(`โŒ sphere.subscribe does not exist`) + } + if (!this.sphere.getAll) { + console.error(`โŒ sphere.getAll does not exist`) + } + if (!this.sphere.get) { + console.error(`โŒ sphere.get does not exist`) + } + + console.log(`๐Ÿ”ง Attempting to subscribe to ${holon}/${lens}`) + + // Try subscribe if it exists + let unsubscribe: (() => void) | undefined = undefined + if (this.sphere.subscribe) { + try { + unsubscribe = this.sphere.subscribe(holon, lens, (data: any, key?: string) => { + subscriptionActive = true + console.log(`๐Ÿ“ฅ Subscription callback fired for ${lens}:`, { data, key, dataType: typeof data, isObject: typeof data === 'object', isArray: Array.isArray(data) }) + + if (data !== null && data !== undefined) { + if (key) { + // If we have a key, it's a key-value pair + collectedData[key] = data + console.log(`๐Ÿ“ฅ Added key-value pair: ${key} =`, data) + } else if (typeof data === 'object' && !Array.isArray(data)) { + // If it's an object, merge it + collectedData = { ...collectedData, ...data } + console.log(`๐Ÿ“ฅ Merged object data, total keys:`, Object.keys(collectedData).length) + } else if (Array.isArray(data)) { + // If it's an array, convert to object with indices + data.forEach((item, index) => { + collectedData[String(index)] = item + }) + console.log(`๐Ÿ“ฅ Converted array to object, total keys:`, Object.keys(collectedData).length) + } else { + // Primitive value + collectedData['value'] = data + console.log(`๐Ÿ“ฅ Added primitive value:`, data) + } + + console.log(`๐Ÿ“ฅ Current collected data for ${lens}:`, Object.keys(collectedData).length, 'keys') + } + }) + console.log(`โœ… Subscribe called successfully for ${lens}`) + } catch (subError) { + console.error(`โŒ Error calling subscribe for ${lens}:`, subError) + } + } + + // Try getAll if it exists + if (this.sphere.getAll) { + console.log(`๐Ÿ”ง Attempting getAll for ${holon}/${lens}`) + this.sphere.getAll(holon, lens).then((immediateData: any) => { + console.log(`๐Ÿ“ฆ getAll returned for ${lens}:`, { + data: immediateData, + type: typeof immediateData, + isObject: typeof immediateData === 'object', + isArray: Array.isArray(immediateData), + keys: immediateData && typeof immediateData === 'object' ? Object.keys(immediateData).length : 'N/A' + }) + + if (immediateData !== null && immediateData !== undefined) { + if (typeof immediateData === 'object' && !Array.isArray(immediateData)) { + collectedData = { ...collectedData, ...immediateData } + console.log(`๐Ÿ“ฆ Merged immediate data, total keys:`, Object.keys(collectedData).length) + } else if (Array.isArray(immediateData)) { + immediateData.forEach((item, index) => { + collectedData[String(index)] = item + }) + console.log(`๐Ÿ“ฆ Converted immediate array to object, total keys:`, Object.keys(collectedData).length) + } else { + collectedData['value'] = immediateData + console.log(`๐Ÿ“ฆ Added immediate primitive value`) + } + } + + // If we have data immediately, resolve early + if (Object.keys(collectedData).length > 0 && !resolved) { + resolved = true + clearTimeout(timeout) + if (unsubscribe) unsubscribe() + console.log(`โœ… Resolving early with ${Object.keys(collectedData).length} keys for ${lens}`) + resolve(collectedData) + } + }).catch((error: any) => { + console.error(`โš ๏ธ Error getting immediate data for ${lens}:`, error) + }) + } else { + // Fallback: try using getData method instead + console.log(`๐Ÿ”ง getAll not available, trying getData as fallback for ${lens}`) + this.getData(holon, lens).then((fallbackData: any) => { + console.log(`๐Ÿ“ฆ getData (fallback) returned for ${lens}:`, fallbackData) + if (fallbackData !== null && fallbackData !== undefined) { + if (typeof fallbackData === 'object' && !Array.isArray(fallbackData)) { + collectedData = { ...collectedData, ...fallbackData } + } else { + collectedData['value'] = fallbackData + } + if (Object.keys(collectedData).length > 0 && !resolved) { + resolved = true + clearTimeout(timeout) + if (unsubscribe) unsubscribe() + console.log(`โœ… Resolving with fallback data: ${Object.keys(collectedData).length} keys for ${lens}`) + resolve(collectedData) + } + } + }).catch((error: any) => { + console.error(`โš ๏ธ Error in fallback getData for ${lens}:`, error) + }) + } + + } catch (error) { + console.error(`โŒ Error setting up subscription for ${lens}:`, error) + clearTimeout(timeout) + if (!resolved) { + resolved = true + resolve(null) + } + } + }) + } + + // Delete data from a holon + async deleteData(holon: string, lens: string, key?: string): Promise { + if (!this.isInitialized) return false + try { + if (key) { + await this.sphere.delete(holon, lens, key) + } else { + await this.sphere.deleteAll(holon, lens) + } + return true + } catch (error) { + console.error('โŒ Error deleting data:', error) + return false + } + } + + // Set schema for data validation + async setSchema(lens: string, schema: any): Promise { + if (!this.isInitialized) return false + try { + await this.sphere.setSchema(lens, schema) + return true + } catch (error) { + console.error('โŒ Error setting schema:', error) + return false + } + } + + // Get current schema + async getSchema(lens: string): Promise { + if (!this.isInitialized) return null + try { + return await this.sphere.getSchema(lens) + } catch (error) { + console.error('โŒ Error getting schema:', error) + return null + } + } + + // Subscribe to changes in a holon + subscribe(holon: string, lens: string, callback: (data: any) => void): void { + if (!this.isInitialized) return + try { + this.sphere.subscribe(holon, lens, callback) + } catch (error) { + console.error('โŒ Error subscribing to changes:', error) + } + } + + // Get holon hierarchy (parent and children) + getHolonHierarchy(holon: string): { parent?: string; children: string[] } { + try { + const resolution = h3.getResolution(holon) + const parent = resolution > 0 ? h3.cellToParent(holon, resolution - 1) : undefined + const children = h3.cellToChildren(holon, resolution + 1) + return { parent, children } + } catch (error) { + console.error('โŒ Error getting holon hierarchy:', error) + return { children: [] } + } + } + + // Get all scales for a holon (all containing holons) + getHolonScalespace(holon: string): string[] { + try { + return this.sphere.getHolonScalespace(holon) + } catch (error) { + console.error('โŒ Error getting holon scalespace:', error) + return [] + } + } + + // Federation methods + async federate(spaceId1: string, spaceId2: string, password1?: string, password2?: string, bidirectional?: boolean): Promise { + if (!this.isInitialized) return false + try { + await this.sphere.federate(spaceId1, spaceId2, password1, password2, bidirectional) + return true + } catch (error) { + console.error('โŒ Error federating spaces:', error) + return false + } + } + + async propagate(holon: string, lens: string, data: any, options?: { useReferences?: boolean; targetSpaces?: string[] }): Promise { + if (!this.isInitialized) return false + try { + await this.sphere.propagate(holon, lens, data, options) + return true + } catch (error) { + console.error('โŒ Error propagating data:', error) + return false + } + } + + // Message federation + async federateMessage(originalChatId: string, messageId: string, federatedChatId: string, federatedMessageId: string, type: string): Promise { + if (!this.isInitialized) return false + try { + await this.sphere.federateMessage(originalChatId, messageId, federatedChatId, federatedMessageId, type) + return true + } catch (error) { + console.error('โŒ Error federating message:', error) + return false + } + } + + async getFederatedMessages(originalChatId: string, messageId: string): Promise { + if (!this.isInitialized) return [] + try { + const result = await this.sphere.getFederatedMessages(originalChatId, messageId) + return Array.isArray(result) ? result : [] + } catch (error) { + console.error('โŒ Error getting federated messages:', error) + return [] + } + } + + async updateFederatedMessages(originalChatId: string, messageId: string, updateCallback: (chatId: string, messageId: string) => Promise): Promise { + if (!this.isInitialized) return false + try { + await this.sphere.updateFederatedMessages(originalChatId, messageId, updateCallback) + return true + } catch (error) { + console.error('โŒ Error updating federated messages:', error) + return false + } + } + + // Utility methods for working with coordinates and resolutions + static getResolutionName(resolution: number): string { + const names = [ + 'Country', 'State/Province', 'Metropolitan Area', 'City', 'District', + 'Neighborhood', 'Block', 'Building', 'Room', 'Desk', 'Chair', 'Point' + ] + return names[resolution] || `Level ${resolution}` + } + + static getResolutionDescription(resolution: number): string { + const descriptions = [ + 'Country level - covers entire countries', + 'State/Province level - covers states and provinces', + 'Metropolitan area level - covers large urban areas', + 'City level - covers individual cities', + 'District level - covers city districts', + 'Neighborhood level - covers neighborhoods', + 'Block level - covers city blocks', + 'Building level - covers individual buildings', + 'Room level - covers individual rooms', + 'Desk level - covers individual desks', + 'Chair level - covers individual chairs', + 'Point level - covers individual points' + ] + return descriptions[resolution] || `Geographic level ${resolution}` + } + + // Get connection status + getConnectionStatus(spaceId: string): HolonConnection | undefined { + return this.connections.get(spaceId) + } + + // Add connection + addConnection(connection: HolonConnection): void { + this.connections.set(connection.id, connection) + } + + // Remove connection + removeConnection(spaceId: string): boolean { + return this.connections.delete(spaceId) + } + + // Get all connections + getAllConnections(): HolonConnection[] { + return Array.from(this.connections.values()) + } +} + +// Create a singleton instance +export const holosphereService = new HoloSphereService('canvas-holons', false) diff --git a/src/lib/clientConfig.ts b/src/lib/clientConfig.ts index f9febde..ca95734 100644 --- a/src/lib/clientConfig.ts +++ b/src/lib/clientConfig.ts @@ -106,11 +106,35 @@ export function getGitHubConfig(): { token: string; repo: string; branch: string */ export function isOpenAIConfigured(): boolean { try { + // First try to get user-specific API keys if available + const session = JSON.parse(localStorage.getItem('session') || '{}') + if (session.authed && session.username) { + const userApiKeys = localStorage.getItem(`${session.username}_api_keys`) + if (userApiKeys) { + try { + const parsed = JSON.parse(userApiKeys) + if (parsed.keys && parsed.keys.openai && parsed.keys.openai.trim() !== '') { + return true + } + } catch (e) { + // Continue to fallback + } + } + } + + // Fallback to global API keys const settings = localStorage.getItem("openai_api_key") if (settings) { - const parsed = JSON.parse(settings) - if (parsed.keys && parsed.keys.openai && parsed.keys.openai.trim() !== '') { - return true + try { + const parsed = JSON.parse(settings) + if (parsed.keys && parsed.keys.openai && parsed.keys.openai.trim() !== '') { + return true + } + } catch (e) { + // If it's not JSON, it might be the old format (just a string) + if (settings.startsWith('sk-') && settings.trim() !== '') { + return true + } } } return false @@ -125,15 +149,45 @@ export function isOpenAIConfigured(): boolean { */ export function getOpenAIConfig(): { apiKey: string } | null { try { - const settings = localStorage.getItem("openai_api_key") - if (settings) { - const parsed = JSON.parse(settings) - if (parsed.keys && parsed.keys.openai && parsed.keys.openai.trim() !== '') { - return { apiKey: parsed.keys.openai } + // First try to get user-specific API keys if available + const session = JSON.parse(localStorage.getItem('session') || '{}') + if (session.authed && session.username) { + const userApiKeys = localStorage.getItem(`${session.username}_api_keys`) + if (userApiKeys) { + try { + const parsed = JSON.parse(userApiKeys) + if (parsed.keys && parsed.keys.openai && parsed.keys.openai.trim() !== '') { + console.log('๐Ÿ”‘ Found user-specific OpenAI API key') + return { apiKey: parsed.keys.openai } + } + } catch (e) { + console.log('๐Ÿ”‘ Error parsing user-specific API keys:', e) + } } } + + // Fallback to global API keys + const settings = localStorage.getItem("openai_api_key") + if (settings) { + try { + const parsed = JSON.parse(settings) + if (parsed.keys && parsed.keys.openai && parsed.keys.openai.trim() !== '') { + console.log('๐Ÿ”‘ Found global OpenAI API key') + return { apiKey: parsed.keys.openai } + } + } catch (e) { + // If it's not JSON, it might be the old format (just a string) + if (settings.startsWith('sk-') && settings.trim() !== '') { + console.log('๐Ÿ”‘ Found old format OpenAI API key') + return { apiKey: settings } + } + } + } + + console.log('๐Ÿ”‘ No OpenAI API key found') return null } catch (e) { + console.log('๐Ÿ”‘ Error getting OpenAI config:', e) return null } } diff --git a/src/lib/githubQuartzReader.ts b/src/lib/githubQuartzReader.ts index 8fe618e..a5f14c2 100644 --- a/src/lib/githubQuartzReader.ts +++ b/src/lib/githubQuartzReader.ts @@ -53,27 +53,20 @@ export class GitHubQuartzReader { */ async getAllNotes(): Promise { try { - console.log('๐Ÿ” Fetching Quartz notes from GitHub...') - console.log(`๐Ÿ“ Repository: ${this.config.owner}/${this.config.repo}`) - console.log(`๐ŸŒฟ Branch: ${this.config.branch}`) - console.log(`๐Ÿ“‚ Content path: ${this.config.contentPath}`) - // Get the content directory const contentFiles = await this.getDirectoryContents(this.config.contentPath || '') // Filter for Markdown files - const markdownFiles = contentFiles.filter(file => - file.type === 'file' && - (file.name.endsWith('.md') || file.name.endsWith('.markdown')) - ) - - console.log(`๐Ÿ“„ Found ${markdownFiles.length} Markdown files`) + const markdownFiles = contentFiles.filter(file => { + return file.type === 'file' && + file.name && + (file.name.endsWith('.md') || file.name.endsWith('.markdown')) + }) // Fetch content for each file const notes: QuartzNoteFromGitHub[] = [] for (const file of markdownFiles) { try { - console.log(`๐Ÿ” Fetching content for file: ${file.path}`) // Get the actual file contents (not just metadata) const fileWithContent = await this.getFileContents(file.path) const note = await this.getNoteFromFile(fileWithContent) @@ -85,7 +78,6 @@ export class GitHubQuartzReader { } } - console.log(`โœ… Successfully loaded ${notes.length} notes from GitHub`) return notes } catch (error) { console.error('โŒ Failed to fetch notes from GitHub:', error) @@ -172,11 +164,10 @@ export class GitHubQuartzReader { */ private async getNoteFromFile(file: GitHubFile): Promise { try { - console.log(`๐Ÿ” Processing file: ${file.path}`) - console.log(`๐Ÿ” File size: ${file.size} bytes`) - console.log(`๐Ÿ” Has content: ${!!file.content}`) - console.log(`๐Ÿ” Content length: ${file.content?.length || 0}`) - console.log(`๐Ÿ” Encoding: ${file.encoding}`) + // Validate file object + if (!file || !file.path) { + return null + } // Decode base64 content let content = '' @@ -189,31 +180,23 @@ export class GitHubQuartzReader { // Try direct decoding if not base64 content = file.content } - console.log(`๐Ÿ” Decoded content length: ${content.length}`) - console.log(`๐Ÿ” Content preview: ${content.substring(0, 200)}...`) } catch (decodeError) { - console.error(`๐Ÿ” Failed to decode content for ${file.path}:`, decodeError) // Try alternative decoding methods try { content = decodeURIComponent(escape(atob(file.content))) - console.log(`๐Ÿ” Alternative decode successful, length: ${content.length}`) } catch (altError) { - console.error(`๐Ÿ” Alternative decode also failed:`, altError) + console.error(`Failed to decode content for ${file.path}:`, altError) return null } } - } else { - console.warn(`๐Ÿ” No content available for file: ${file.path}`) - return null } // Parse frontmatter and content const { frontmatter, content: markdownContent } = this.parseMarkdownWithFrontmatter(content) - console.log(`๐Ÿ” Parsed markdown content length: ${markdownContent.length}`) - console.log(`๐Ÿ” Frontmatter keys: ${Object.keys(frontmatter).join(', ')}`) // Extract title - const title = frontmatter.title || this.extractTitleFromPath(file.name) || 'Untitled' + const fileName = file.name || file.path.split('/').pop() || 'untitled' + const title = frontmatter.title || this.extractTitleFromPath(fileName) || 'Untitled' // Extract tags const tags = this.extractTags(frontmatter, markdownContent) @@ -221,7 +204,7 @@ export class GitHubQuartzReader { // Generate note ID const id = this.generateNoteId(file.path, title) - const result = { + return { id, title, content: markdownContent, @@ -232,9 +215,6 @@ export class GitHubQuartzReader { htmlUrl: file.html_url, rawUrl: file.download_url || file.git_url } - - console.log(`๐Ÿ” Final note: ${title} (${markdownContent.length} chars)`) - return result } catch (error) { console.error(`Failed to parse file ${file.path}:`, error) return null @@ -245,8 +225,6 @@ export class GitHubQuartzReader { * Parse Markdown content with frontmatter */ private parseMarkdownWithFrontmatter(content: string): { frontmatter: Record, content: string } { - console.log(`๐Ÿ” Parsing markdown with frontmatter, content length: ${content.length}`) - // More flexible frontmatter regex that handles different formats const frontmatterRegex = /^---\s*\r?\n([\s\S]*?)\r?\n---\s*\r?\n([\s\S]*)$/m const match = content.match(frontmatterRegex) @@ -255,10 +233,6 @@ export class GitHubQuartzReader { const frontmatterText = match[1] const markdownContent = match[2].trim() // Remove leading/trailing whitespace - console.log(`๐Ÿ” Found frontmatter, length: ${frontmatterText.length}`) - console.log(`๐Ÿ” Markdown content length: ${markdownContent.length}`) - console.log(`๐Ÿ” Markdown preview: ${markdownContent.substring(0, 100)}...`) - // Parse YAML frontmatter (simplified but more robust) const frontmatter: Record = {} const lines = frontmatterText.split(/\r?\n/) @@ -298,11 +272,9 @@ export class GitHubQuartzReader { } } - console.log(`๐Ÿ” Parsed frontmatter:`, frontmatter) return { frontmatter, content: markdownContent } } - console.log(`๐Ÿ” No frontmatter found, using entire content`) return { frontmatter: {}, content: content.trim() } } @@ -310,6 +282,10 @@ export class GitHubQuartzReader { * Extract title from file path */ private extractTitleFromPath(fileName: string): string { + if (!fileName) { + return 'Untitled' + } + return fileName .replace(/\.(md|markdown)$/i, '') .replace(/[-_]/g, ' ') diff --git a/src/lib/location/locationStorage.ts b/src/lib/location/locationStorage.ts new file mode 100644 index 0000000..6fccb1b --- /dev/null +++ b/src/lib/location/locationStorage.ts @@ -0,0 +1,295 @@ +import type FileSystem from '@oddjs/odd/fs/index'; +import * as odd from '@oddjs/odd'; +import type { PrecisionLevel } from './types'; + +/** + * Location data stored in the filesystem + */ +export interface LocationData { + id: string; + userId: string; + latitude: number; + longitude: number; + accuracy: number; + timestamp: number; + expiresAt: number | null; + precision: PrecisionLevel; +} + +/** + * Location share metadata + */ +export interface LocationShare { + id: string; + locationId: string; + shareToken: string; + createdAt: number; + expiresAt: number | null; + maxViews: number | null; + viewCount: number; + precision: PrecisionLevel; +} + +/** + * Location storage service + * Handles storing and retrieving locations from the ODD.js filesystem + */ +export class LocationStorageService { + private fs: FileSystem; + private locationsPath: string[]; + private sharesPath: string[]; + private publicSharesPath: string[]; + + constructor(fs: FileSystem) { + this.fs = fs; + // Private storage paths + this.locationsPath = ['private', 'locations']; + this.sharesPath = ['private', 'location-shares']; + // Public reference path for share validation + this.publicSharesPath = ['public', 'location-shares']; + } + + /** + * Initialize directories + */ + async initialize(): Promise { + // Ensure private directories exist + await this.ensureDirectory(this.locationsPath); + await this.ensureDirectory(this.sharesPath); + // Ensure public directory for share references + await this.ensureDirectory(this.publicSharesPath); + } + + /** + * Ensure a directory exists + */ + private async ensureDirectory(path: string[]): Promise { + try { + const dirPath = odd.path.directory(...path); + const exists = await this.fs.exists(dirPath as any); + if (!exists) { + await this.fs.mkdir(dirPath as any); + } + } catch (error) { + console.error('Error ensuring directory:', error); + throw error; + } + } + + /** + * Save a location to the filesystem + */ + async saveLocation(location: LocationData): Promise { + try { + const filePath = odd.path.file(...this.locationsPath, `${location.id}.json`); + const content = new TextEncoder().encode(JSON.stringify(location, null, 2)); + await this.fs.write(filePath as any, content as any); + await this.fs.publish(); + } catch (error) { + console.error('Error saving location:', error); + throw error; + } + } + + /** + * Get a location by ID + */ + async getLocation(locationId: string): Promise { + try { + const filePath = odd.path.file(...this.locationsPath, `${locationId}.json`); + const exists = await this.fs.exists(filePath as any); + if (!exists) { + return null; + } + const content = await this.fs.read(filePath as any); + const text = new TextDecoder().decode(content as Uint8Array); + return JSON.parse(text) as LocationData; + } catch (error) { + console.error('Error reading location:', error); + return null; + } + } + + /** + * Create a location share + */ + async createShare(share: LocationShare): Promise { + try { + // Save share metadata in private directory + const sharePath = odd.path.file(...this.sharesPath, `${share.id}.json`); + const shareContent = new TextEncoder().encode(JSON.stringify(share, null, 2)); + await this.fs.write(sharePath as any, shareContent as any); + + // Create public reference file for share validation (only token, not full data) + const publicSharePath = odd.path.file(...this.publicSharesPath, `${share.shareToken}.json`); + const publicShareRef = { + shareToken: share.shareToken, + shareId: share.id, + createdAt: share.createdAt, + expiresAt: share.expiresAt, + }; + const publicContent = new TextEncoder().encode(JSON.stringify(publicShareRef, null, 2)); + await this.fs.write(publicSharePath as any, publicContent as any); + + await this.fs.publish(); + } catch (error) { + console.error('Error creating share:', error); + throw error; + } + } + + /** + * Get a share by token + */ + async getShareByToken(shareToken: string): Promise { + try { + // First check public reference + const publicSharePath = odd.path.file(...this.publicSharesPath, `${shareToken}.json`); + const publicExists = await this.fs.exists(publicSharePath as any); + if (!publicExists) { + return null; + } + + const publicContent = await this.fs.read(publicSharePath as any); + const publicText = new TextDecoder().decode(publicContent as Uint8Array); + const publicRef = JSON.parse(publicText); + + // Now get full share from private directory + const sharePath = odd.path.file(...this.sharesPath, `${publicRef.shareId}.json`); + const shareExists = await this.fs.exists(sharePath as any); + if (!shareExists) { + return null; + } + + const shareContent = await this.fs.read(sharePath as any); + const shareText = new TextDecoder().decode(shareContent as Uint8Array); + return JSON.parse(shareText) as LocationShare; + } catch (error) { + console.error('Error reading share:', error); + return null; + } + } + + /** + * Get all shares for the current user + */ + async getAllShares(): Promise { + try { + const dirPath = odd.path.directory(...this.sharesPath); + const exists = await this.fs.exists(dirPath as any); + if (!exists) { + return []; + } + + const files = await this.fs.ls(dirPath as any); + const shares: LocationShare[] = []; + + for (const fileName of Object.keys(files)) { + if (fileName.endsWith('.json')) { + const shareId = fileName.replace('.json', ''); + const share = await this.getShareById(shareId); + if (share) { + shares.push(share); + } + } + } + + return shares; + } catch (error) { + console.error('Error listing shares:', error); + return []; + } + } + + /** + * Get a share by ID + */ + private async getShareById(shareId: string): Promise { + try { + const sharePath = odd.path.file(...this.sharesPath, `${shareId}.json`); + const exists = await this.fs.exists(sharePath as any); + if (!exists) { + return null; + } + const content = await this.fs.read(sharePath as any); + const text = new TextDecoder().decode(content as Uint8Array); + return JSON.parse(text) as LocationShare; + } catch (error) { + console.error('Error reading share:', error); + return null; + } + } + + /** + * Increment view count for a share + */ + async incrementShareViews(shareId: string): Promise { + try { + const share = await this.getShareById(shareId); + if (!share) { + throw new Error('Share not found'); + } + + share.viewCount += 1; + await this.createShare(share); // Re-save the share + } catch (error) { + console.error('Error incrementing share views:', error); + throw error; + } + } +} + +/** + * Obfuscate location based on precision level + */ +export function obfuscateLocation( + lat: number, + lng: number, + precision: PrecisionLevel +): { lat: number; lng: number; radius: number } { + let radius = 0; + + switch (precision) { + case 'exact': + radius = 0; + break; + case 'street': + radius = 100; // ~100m radius + break; + case 'neighborhood': + radius = 1000; // ~1km radius + break; + case 'city': + radius = 10000; // ~10km radius + break; + } + + if (radius === 0) { + return { lat, lng, radius: 0 }; + } + + // Add random offset within the radius + const angle = Math.random() * 2 * Math.PI; + const distance = Math.random() * radius; + + // Convert distance to degrees (rough approximation: 1 degree โ‰ˆ 111km) + const latOffset = (distance / 111000) * Math.cos(angle); + const lngOffset = (distance / (111000 * Math.cos(lat * Math.PI / 180))) * Math.sin(angle); + + return { + lat: lat + latOffset, + lng: lng + lngOffset, + radius, + }; +} + +/** + * Generate a secure share token + */ +export function generateShareToken(): string { + // Generate a cryptographically secure random token + const array = new Uint8Array(32); + crypto.getRandomValues(array); + return Array.from(array, (byte) => byte.toString(16).padStart(2, '0')).join(''); +} + diff --git a/src/lib/location/types.ts b/src/lib/location/types.ts new file mode 100644 index 0000000..5be56dd --- /dev/null +++ b/src/lib/location/types.ts @@ -0,0 +1,47 @@ +/** + * Location sharing types + */ + +export type PrecisionLevel = "exact" | "street" | "neighborhood" | "city"; + +export interface ShareSettings { + duration: number | null; // Duration in milliseconds + maxViews: number | null; // Maximum number of views allowed + precision: PrecisionLevel; // Precision level for location obfuscation +} + +export interface GeolocationPosition { + coords: { + latitude: number; + longitude: number; + accuracy: number; + altitude?: number | null; + altitudeAccuracy?: number | null; + heading?: number | null; + speed?: number | null; + }; + timestamp: number; +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/lib/obsidianImporter.ts b/src/lib/obsidianImporter.ts index b7f4ce8..bbb7d1f 100644 --- a/src/lib/obsidianImporter.ts +++ b/src/lib/obsidianImporter.ts @@ -20,12 +20,34 @@ export interface ObsidianObsNote { vaultPath?: string } +export interface FolderNode { + name: string + path: string + children: FolderNode[] + notes: ObsidianObsNote[] + isExpanded: boolean + level: number +} + export interface ObsidianVault { name: string path: string obs_notes: ObsidianObsNote[] totalObsNotes: number lastImported: Date + folderTree: FolderNode +} + +export interface ObsidianVaultRecord { + id: string + typeName: 'obsidian_vault' + name: string + path: string + obs_notes: ObsidianObsNote[] + totalObsNotes: number + lastImported: Date + folderTree: FolderNode + meta: Record } export class ObsidianImporter { @@ -39,7 +61,6 @@ export class ObsidianImporter { try { // For now, we'll simulate this with a demo vault // In a real implementation, you'd use the File System Access API - console.log('Importing from directory:', directoryPath) // Simulate reading files (in real implementation, use File System Access API) const mockObsNotes = await this.createMockObsNotes() @@ -49,7 +70,8 @@ export class ObsidianImporter { path: directoryPath, obs_notes: mockObsNotes, totalObsNotes: mockObsNotes.length, - lastImported: new Date() + lastImported: new Date(), + folderTree: this.buildFolderTree(mockObsNotes) } return this.vault @@ -64,8 +86,6 @@ export class ObsidianImporter { */ async importFromQuartzUrl(quartzUrl: string): Promise { try { - console.log('Importing from Quartz URL:', quartzUrl) - // Ensure URL has protocol const url = quartzUrl.startsWith('http') ? quartzUrl : `https://${quartzUrl}` @@ -73,7 +93,6 @@ export class ObsidianImporter { const githubConfig = this.getGitHubConfigFromUrl(url) if (githubConfig) { - console.log('๐Ÿ” Using GitHub API to read Quartz content') const obs_notes = await this.importFromGitHub(githubConfig) this.vault = { @@ -81,12 +100,12 @@ export class ObsidianImporter { path: url, obs_notes, totalObsNotes: obs_notes.length, - lastImported: new Date() + lastImported: new Date(), + folderTree: this.buildFolderTree(obs_notes) } return this.vault } else { - console.log('โš ๏ธ No GitHub config found, falling back to web scraping') // Fallback to the old method const obs_notes = await this.discoverQuartzContent(url) @@ -95,7 +114,8 @@ export class ObsidianImporter { path: url, obs_notes, totalObsNotes: obs_notes.length, - lastImported: new Date() + lastImported: new Date(), + folderTree: this.buildFolderTree(obs_notes) } return this.vault @@ -129,7 +149,8 @@ export class ObsidianImporter { path: directoryHandle.name, // File System Access API doesn't expose full path obs_notes, totalObsNotes: obs_notes.length, - lastImported: new Date() + lastImported: new Date(), + folderTree: this.buildFolderTree(obs_notes) } return this.vault @@ -449,6 +470,163 @@ A collection of creative project ideas and concepts. return Array.from(allTags).sort() } + /** + * Build folder tree structure from obs_notes + */ + buildFolderTree(obs_notes: ObsidianObsNote[]): FolderNode { + const root: FolderNode = { + name: 'Root', + path: '', + children: [], + notes: [], + isExpanded: true, + level: 0 + } + + // Group notes by their folder paths + const folderMap = new Map() + + obs_notes.forEach(note => { + const pathParts = this.parseFilePath(note.filePath) + const folderKey = pathParts.folders.join('/') + + if (!folderMap.has(folderKey)) { + folderMap.set(folderKey, { folders: pathParts.folders, notes: [] }) + } + folderMap.get(folderKey)!.notes.push(note) + }) + + // Build the tree structure + folderMap.forEach(({ folders, notes }) => { + this.addFolderToTree(root, folders, notes) + }) + + return root + } + + /** + * Parse file path into folder structure + */ + private parseFilePath(filePath: string): { folders: string[], fileName: string } { + // Handle both local paths and URLs + let pathToParse = filePath + + if (filePath.startsWith('http')) { + // Extract pathname from URL + try { + const url = new URL(filePath) + pathToParse = url.pathname.replace(/^\//, '') + } catch (e) { + console.warn('Invalid URL:', filePath) + return { folders: [], fileName: filePath } + } + } + + // Split path and filter out empty parts + const parts = pathToParse.split('/').filter(part => part.length > 0) + + if (parts.length === 0) { + return { folders: [], fileName: filePath } + } + + const fileName = parts[parts.length - 1] + const folders = parts.slice(0, -1) + + return { folders, fileName } + } + + /** + * Add folder to tree structure + */ + private addFolderToTree(root: FolderNode, folderPath: string[], notes: ObsidianObsNote[]): void { + let current = root + + for (let i = 0; i < folderPath.length; i++) { + const folderName = folderPath[i] + let existingFolder = current.children.find(child => child.name === folderName) + + if (!existingFolder) { + const currentPath = folderPath.slice(0, i + 1).join('/') + existingFolder = { + name: folderName, + path: currentPath, + children: [], + notes: [], + isExpanded: false, + level: i + 1 + } + current.children.push(existingFolder) + } + + current = existingFolder + } + + // Add notes to the final folder + current.notes.push(...notes) + } + + /** + * Get all notes from a folder tree (recursive) + */ + getAllNotesFromTree(folder: FolderNode): ObsidianObsNote[] { + let notes = [...folder.notes] + + folder.children.forEach(child => { + notes.push(...this.getAllNotesFromTree(child)) + }) + + return notes + } + + /** + * Find folder by path in tree + */ + findFolderByPath(root: FolderNode, path: string): FolderNode | null { + if (root.path === path) { + return root + } + + for (const child of root.children) { + const found = this.findFolderByPath(child, path) + if (found) { + return found + } + } + + return null + } + + /** + * Convert vault to Automerge record format + */ + vaultToRecord(vault: ObsidianVault): ObsidianVaultRecord { + return { + id: `obsidian_vault:${vault.name}`, + typeName: 'obsidian_vault', + name: vault.name, + path: vault.path, + obs_notes: vault.obs_notes, + totalObsNotes: vault.totalObsNotes, + lastImported: vault.lastImported, + folderTree: vault.folderTree, + meta: {} + } + } + + /** + * Convert Automerge record to vault format + */ + recordToVault(record: ObsidianVaultRecord): ObsidianVault { + return { + name: record.name, + path: record.path, + obs_notes: record.obs_notes, + totalObsNotes: record.totalObsNotes, + lastImported: record.lastImported, + folderTree: record.folderTree + } + } + /** * Search notes in the current vault */ @@ -501,18 +679,15 @@ A collection of creative project ideas and concepts. const githubRepo = config.quartzRepo if (!githubToken || !githubRepo) { - console.log('โš ๏ธ GitHub credentials not found in configuration') return null } if (githubToken === 'your_github_token_here' || githubRepo === 'your_username/your-quartz-repo') { - console.log('โš ๏ธ GitHub credentials are still set to placeholder values') return null } const [owner, repo] = githubRepo.split('/') if (!owner || !repo) { - console.log('โš ๏ธ Invalid GitHub repository format') return null } @@ -564,15 +739,12 @@ A collection of creative project ideas and concepts. const currentHasQuotes = obsNote.filePath.includes('"') if (currentHasQuotes && !existingHasQuotes) { - console.log(`Keeping existing note without quotes: ${existing.filePath}`) return // Keep the existing one } else if (!currentHasQuotes && existingHasQuotes) { - console.log(`Replacing with note without quotes: ${obsNote.filePath}`) notesMap.set(obsNote.id, obsNote) } else { // Both have or don't have quotes, prefer the one with more content if (obsNote.content.length > existing.content.length) { - console.log(`Replacing with longer content: ${obsNote.filePath}`) notesMap.set(obsNote.id, obsNote) } } @@ -582,7 +754,6 @@ A collection of creative project ideas and concepts. }) const uniqueNotes = Array.from(notesMap.values()) - console.log(`Imported ${uniqueNotes.length} unique notes from GitHub (${quartzNotes.length} total files processed)`) return uniqueNotes } catch (error) { @@ -598,44 +769,29 @@ A collection of creative project ideas and concepts. const obs_notes: ObsidianObsNote[] = [] try { - console.log('๐Ÿ” Starting Quartz content discovery for:', baseUrl) - // Try to find content through common Quartz patterns const contentUrls = await this.findQuartzContentUrls(baseUrl) - console.log('๐Ÿ” Found content URLs:', contentUrls.length) if (contentUrls.length === 0) { - console.warn('โš ๏ธ No content URLs found for Quartz site:', baseUrl) return obs_notes } for (const contentUrl of contentUrls) { try { - console.log('๐Ÿ” Fetching content from:', contentUrl) const response = await fetch(contentUrl) if (!response.ok) { - console.warn(`โš ๏ธ Failed to fetch ${contentUrl}: ${response.status} ${response.statusText}`) continue } const content = await response.text() - console.log('๐Ÿ” Successfully fetched content, length:', content.length) - const obs_note = this.parseQuartzMarkdown(content, contentUrl, baseUrl) - console.log('๐Ÿ” Parsed note:', obs_note.title, 'Content length:', obs_note.content.length) - // Only add notes that have meaningful content - if (obs_note.content.length > 10) { - obs_notes.push(obs_note) - } else { - console.log('๐Ÿ” Skipping note with insufficient content:', obs_note.title) - } + // Add all notes regardless of content length + obs_notes.push(obs_note) } catch (error) { - console.warn(`โš ๏ธ Failed to fetch content from ${contentUrl}:`, error) + // Silently skip failed fetches } } - - console.log('๐Ÿ” Successfully discovered', obs_notes.length, 'notes from Quartz site') } catch (error) { console.warn('โš ๏ธ Failed to discover Quartz content:', error) } @@ -660,7 +816,6 @@ A collection of creative project ideas and concepts. // Look for navigation links and content links in the main page const discoveredUrls = this.extractContentUrlsFromPage(mainPageContent, baseUrl) urls.push(...discoveredUrls) - console.log('๐Ÿ” Discovered URLs from main page:', discoveredUrls.length) } // Try to find a sitemap @@ -675,7 +830,6 @@ A collection of creative project ideas and concepts. match.replace(/<\/?loc>/g, '').trim() ).filter(url => url.endsWith('.html') || url.endsWith('.md') || url.includes(baseUrl)) urls.push(...sitemapUrls) - console.log('๐Ÿ” Found sitemap with URLs:', sitemapUrls.length) } } } catch (error) { @@ -702,7 +856,6 @@ A collection of creative project ideas and concepts. const response = await fetch(url) if (response.ok) { urls.push(url) - console.log('๐Ÿ” Found content at:', url) } } catch (error) { // Ignore individual path failures @@ -714,7 +867,6 @@ A collection of creative project ideas and concepts. // Remove duplicates and limit results const uniqueUrls = [...new Set(urls)] - console.log('๐Ÿ” Total unique URLs found:', uniqueUrls.length) return uniqueUrls.slice(0, 50) // Limit to 50 pages to avoid overwhelming } @@ -905,7 +1057,6 @@ A collection of creative project ideas and concepts. // If we still don't have much content, try to extract any text from the original HTML if (text.length < 50) { - console.log('๐Ÿ” Content too short, trying fallback extraction...') let fallbackText = html // Remove script, style, and other non-content tags @@ -932,14 +1083,12 @@ A collection of creative project ideas and concepts. fallbackText = fallbackText.trim() if (fallbackText.length > text.length) { - console.log('๐Ÿ” Fallback extraction found more content:', fallbackText.length) text = fallbackText } } // Final fallback: if we still don't have content, try to extract any text from the body if (text.length < 20) { - console.log('๐Ÿ” Still no content, trying body text extraction...') const bodyMatch = html.match(/]*>(.*?)<\/body>/is) if (bodyMatch) { let bodyText = bodyMatch[1] @@ -955,7 +1104,6 @@ A collection of creative project ideas and concepts. bodyText = bodyText.replace(/\s+/g, ' ').trim() if (bodyText.length > text.length) { - console.log('๐Ÿ” Body text extraction found content:', bodyText.length) text = bodyText } } diff --git a/src/lib/quartzSync.ts b/src/lib/quartzSync.ts index 9441daf..d1100ce 100644 --- a/src/lib/quartzSync.ts +++ b/src/lib/quartzSync.ts @@ -295,6 +295,21 @@ export function createQuartzNoteFromShape(shape: any): QuartzNote { const content = shape.props.content || '' const tags = shape.props.tags || [] + // Use stored filePath if available to maintain filename consistency + // Otherwise, generate from title + let filePath: string + if (shape.props.filePath && shape.props.filePath.trim() !== '') { + filePath = shape.props.filePath + // Ensure it ends with .md if it doesn't already + if (!filePath.endsWith('.md')) { + filePath = filePath.endsWith('/') ? `${filePath}${title}.md` : `${filePath}.md` + } + } else { + // Generate from title, ensuring it's a valid filename + const sanitizedTitle = title.replace(/[^a-zA-Z0-9\s-]/g, '').trim().replace(/\s+/g, '-') + filePath = `${sanitizedTitle}.md` + } + return { id: shape.props.noteId || title, title, @@ -306,7 +321,7 @@ export function createQuartzNoteFromShape(shape: any): QuartzNote { created: new Date().toISOString(), modified: new Date().toISOString() }, - filePath: `${title}.md`, + filePath: filePath, lastModified: new Date() } } diff --git a/src/lib/settings.tsx b/src/lib/settings.tsx index ca8c1a0..32af201 100644 --- a/src/lib/settings.tsx +++ b/src/lib/settings.tsx @@ -1,5 +1,5 @@ import { atom } from 'tldraw' -import { SYSTEM_PROMPT } from '@/prompt' +import { SYSTEM_PROMPT, CONSTANCE_SYSTEM_PROMPT } from '@/prompt' export const PROVIDERS = [ { @@ -13,8 +13,8 @@ export const PROVIDERS = [ id: 'anthropic', name: 'Anthropic', models: [ - 'claude-3-5-sonnet-20241022', - 'claude-3-5-sonnet-20240620', + 'claude-sonnet-4-5-20250929', + 'claude-sonnet-4-20250522', 'claude-3-opus-20240229', 'claude-3-sonnet-20240229', 'claude-3-haiku-20240307', @@ -25,6 +25,21 @@ export const PROVIDERS = [ // { id: 'google', name: 'Google', model: 'Gemeni 1.5 Flash', validate: (key: string) => true }, ] +export const AI_PERSONALITIES = [ + { + id: 'web-developer', + name: 'Web Developer', + description: 'Expert web developer for building prototypes from wireframes', + systemPrompt: SYSTEM_PROMPT, + }, + { + id: 'constance', + name: 'Constance', + description: 'Avatar of the US Constitution - helps understand constitutional principles', + systemPrompt: CONSTANCE_SYSTEM_PROMPT, + }, +] + export const makeRealSettings = atom('make real settings', { provider: 'openai' as (typeof PROVIDERS)[number]['id'] | 'all', models: Object.fromEntries(PROVIDERS.map((provider) => [provider.id, provider.models[0]])), @@ -33,6 +48,7 @@ export const makeRealSettings = atom('make real settings', { anthropic: '', google: '', }, + personality: 'web-developer' as (typeof AI_PERSONALITIES)[number]['id'], prompts: { system: SYSTEM_PROMPT, }, @@ -50,6 +66,7 @@ export function applySettingsMigrations(settings: any) { google: '', ...keys, }, + personality: 'web-developer' as (typeof AI_PERSONALITIES)[number]['id'], prompts: { system: SYSTEM_PROMPT, ...prompts, diff --git a/src/lib/testHolon.ts b/src/lib/testHolon.ts new file mode 100644 index 0000000..97da7b2 --- /dev/null +++ b/src/lib/testHolon.ts @@ -0,0 +1,57 @@ +// Simple test to verify Holon functionality +import { holosphereService } from './HoloSphereService' + +export async function testHolonFunctionality() { + console.log('๐Ÿงช Testing Holon functionality...') + + try { + // Test initialization + const isInitialized = await holosphereService.initialize() + console.log('โœ… HoloSphere initialized:', isInitialized) + + if (!isInitialized) { + console.log('โŒ HoloSphere not initialized, skipping tests') + return false + } + + // Test getting a holon + const holonId = await holosphereService.getHolon(40.7128, -74.0060, 7) + console.log('โœ… Got holon ID:', holonId) + + if (holonId) { + // Test storing data + const testData = { + id: 'test-1', + content: 'Hello from Holon!', + timestamp: Date.now() + } + + const storeSuccess = await holosphereService.putData(holonId, 'test', testData) + console.log('โœ… Stored data:', storeSuccess) + + // Test retrieving data + const retrievedData = await holosphereService.getData(holonId, 'test') + console.log('โœ… Retrieved data:', retrievedData) + + // Test getting hierarchy + const hierarchy = holosphereService.getHolonHierarchy(holonId) + console.log('โœ… Holon hierarchy:', hierarchy) + + // Test getting scalespace + const scalespace = holosphereService.getHolonScalespace(holonId) + console.log('โœ… Holon scalespace:', scalespace) + } + + console.log('โœ… All Holon tests passed!') + return true + + } catch (error) { + console.error('โŒ Holon test failed:', error) + return false + } +} + +// Auto-run test when imported +if (typeof window !== 'undefined') { + testHolonFunctionality() +} diff --git a/src/prompt.ts b/src/prompt.ts index 5270b26..a0d15f1 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -17,6 +17,32 @@ Your prototype should look and feel much more complete and advanced than the wir Remember: you love your designers and want them to be happy. The more complete and impressive your prototype, the happier they will be. You are evaluated on 1) whether your prototype resembles the designs, 2) whether your prototype is interactive and responsive, and 3) whether your prototype is complete and impressive.` +export const CONSTANCE_SYSTEM_PROMPT = `You are Constance, the avatar of the US Constitution. You help people understand the Constitution's life story, its principles, and its aspirations for the future. You speak with the wisdom and authority of the founding document of the United States, while remaining approachable and educational. + +When discussing the Constitution: +- Explain constitutional principles in clear, accessible language +- Provide historical context for constitutional provisions +- Help people understand how the Constitution applies to modern issues +- Share the vision and values that guided the framers +- Discuss the Constitution's role in protecting individual rights and establishing government structure + +You are knowledgeable about: +- The text and meaning of the Constitution +- The Bill of Rights and subsequent amendments +- Constitutional history and the founding era +- How constitutional principles apply to contemporary issues +- The balance of powers and federalism +- Individual rights and civil liberties + +Your tone should be: +- Authoritative yet approachable +- Educational and informative +- Respectful of the document's importance +- Encouraging of civic engagement and understanding +- Thoughtful about constitutional interpretation + +Remember: You represent the living document that has guided American democracy for over two centuries. Help people connect with the Constitution's enduring principles and understand its relevance to their lives today.` + export const USER_PROMPT = 'Here are the latest wireframes. Please reply with a high-fidelity working prototype as a single HTML file.' diff --git a/src/routes/Board.tsx b/src/routes/Board.tsx index c18cdef..ca48a38 100644 --- a/src/routes/Board.tsx +++ b/src/routes/Board.tsx @@ -1,4 +1,5 @@ import { useAutomergeSync } from "@/automerge/useAutomergeSync" +import { AutomergeHandleProvider } from "@/context/AutomergeHandleContext" import { useMemo, useEffect, useState } from "react" import { Tldraw, Editor, TLShapeId } from "tldraw" import { useParams } from "react-router-dom" @@ -35,6 +36,15 @@ import { ObsNoteTool } from "@/tools/ObsNoteTool" import { ObsNoteShape } from "@/shapes/ObsNoteShapeUtil" import { TranscriptionTool } from "@/tools/TranscriptionTool" import { TranscriptionShape } from "@/shapes/TranscriptionShapeUtil" +import { FathomTranscriptTool } from "@/tools/FathomTranscriptTool" +import { FathomTranscriptShape } from "@/shapes/FathomTranscriptShapeUtil" +import { HolonTool } from "@/tools/HolonTool" +import { HolonShape } from "@/shapes/HolonShapeUtil" +import { FathomMeetingsTool } from "@/tools/FathomMeetingsTool" +import { HolonBrowserShape } from "@/shapes/HolonBrowserShapeUtil" +import { ObsidianBrowserShape } from "@/shapes/ObsidianBrowserShapeUtil" +import { FathomMeetingsBrowserShape } from "@/shapes/FathomMeetingsBrowserShapeUtil" +import { LocationShareShape } from "@/shapes/LocationShareShapeUtil" import { lockElement, unlockElement, @@ -57,11 +67,7 @@ import { useAuth } from "../context/AuthContext" import { updateLastVisited } from "../lib/starredBoards" import { captureBoardScreenshot } from "../lib/screenshotService" -// Automatically switch between production and local dev based on environment -// In development, use the same host as the client to support network access -export const WORKER_URL = import.meta.env.DEV - ? `http://${window.location.hostname}:5172` - : "https://jeffemmett-canvas.jeffemmett.workers.dev" +import { WORKER_URL } from "../constants/workerUrl" const customShapeUtils = [ ChatBoxShape, @@ -74,6 +80,12 @@ const customShapeUtils = [ SharedPianoShape, ObsNoteShape, TranscriptionShape, + FathomTranscriptShape, + HolonShape, + HolonBrowserShape, + ObsidianBrowserShape, + FathomMeetingsBrowserShape, + LocationShareShape, ] const customTools = [ ChatBoxTool, @@ -87,10 +99,71 @@ const customTools = [ GestureTool, ObsNoteTool, TranscriptionTool, + FathomTranscriptTool, + HolonTool, + FathomMeetingsTool, ] export function Board() { const { slug } = useParams<{ slug: string }>() + + // Global wheel event handler to ensure scrolling happens on the hovered scrollable element + useEffect(() => { + const handleWheel = (e: WheelEvent) => { + // Use document.elementFromPoint to find the element under the mouse cursor + const elementUnderMouse = document.elementFromPoint(e.clientX, e.clientY) as HTMLElement + if (!elementUnderMouse) return + + // Walk up the DOM tree from the element under the mouse to find a scrollable element + let element: HTMLElement | null = elementUnderMouse + while (element && element !== document.body && element !== document.documentElement) { + const style = window.getComputedStyle(element) + const overflowY = style.overflowY + const overflowX = style.overflowX + const overflow = style.overflow + const isScrollable = + (overflowY === 'auto' || overflowY === 'scroll' || + overflowX === 'auto' || overflowX === 'scroll' || + overflow === 'auto' || overflow === 'scroll') + + if (isScrollable) { + // Check if the element can actually scroll in the direction of the wheel event + const canScrollDown = e.deltaY > 0 && element.scrollTop < element.scrollHeight - element.clientHeight - 1 + const canScrollUp = e.deltaY < 0 && element.scrollTop > 0 + const canScrollRight = e.deltaX > 0 && element.scrollLeft < element.scrollWidth - element.clientWidth - 1 + const canScrollLeft = e.deltaX < 0 && element.scrollLeft > 0 + + const canScroll = canScrollDown || canScrollUp || canScrollRight || canScrollLeft + + if (canScroll) { + // Verify the mouse is actually over this element + const rect = element.getBoundingClientRect() + const isOverElement = + e.clientX >= rect.left && + e.clientX <= rect.right && + e.clientY >= rect.top && + e.clientY <= rect.bottom + + if (isOverElement) { + // Stop propagation to prevent the scroll from affecting parent elements + // but don't prevent default - let the browser handle the actual scrolling + e.stopPropagation() + return + } + } + } + + element = element.parentElement + } + } + + // Use capture phase to catch events early, before they bubble + document.addEventListener('wheel', handleWheel, { passive: true, capture: true }) + + return () => { + document.removeEventListener('wheel', handleWheel, { capture: true }) + } + }, []) const roomId = slug || "mycofi33" const { session } = useAuth() @@ -129,7 +202,14 @@ export function Board() { ) // Use Automerge sync for all environments - const store = useAutomergeSync(storeConfig) + const storeWithHandle = useAutomergeSync(storeConfig) + const store = { + store: storeWithHandle.store, + status: storeWithHandle.status, + connectionStatus: storeWithHandle.connectionStatus, + error: storeWithHandle.error + } + const automergeHandle = storeWithHandle.handle const [editor, setEditor] = useState(null) useEffect(() => { @@ -154,8 +234,6 @@ export function Board() { // Debug: Check what shapes the editor can see - // Temporarily commented out to fix linting errors - /* if (editor) { const editorShapes = editor.getRenderingShapes() console.log(`๐Ÿ“Š Board: Editor can see ${editorShapes.length} shapes for rendering`) @@ -173,22 +251,6 @@ export function Board() { y: shape?.y }) } - } - */ - - // Debug: Check if there are shapes in store that editor can't see - // Temporarily commented out to fix linting errors - /* - if (storeShapes.length > editorShapes.length) { - const editorShapeIds = new Set(editorShapes.map(s => s.id)) - const missingShapes = storeShapes.filter(s => !editorShapeIds.has(s.id)) - console.warn(`๐Ÿ“Š Board: ${missingShapes.length} shapes in store but not visible to editor:`, missingShapes.map(s => ({ - id: s.id, - type: s.type, - x: s.x, - y: s.y, - parentId: s.parentId - }))) // Debug: Check current page and page IDs const currentPageId = editor.getCurrentPageId() @@ -200,34 +262,46 @@ export function Board() { name: (p as any).name }))) - // Check if missing shapes are on a different page - const shapesOnCurrentPage = missingShapes.filter(s => s.parentId === currentPageId) - const shapesOnOtherPages = missingShapes.filter(s => s.parentId !== currentPageId) - console.log(`๐Ÿ“Š Board: Missing shapes on current page: ${shapesOnCurrentPage.length}, on other pages: ${shapesOnOtherPages.length}`) - - if (shapesOnOtherPages.length > 0) { - console.log(`๐Ÿ“Š Board: Shapes on other pages:`, shapesOnOtherPages.map(s => ({ + // Check if there are shapes in store that editor can't see + if (storeShapes.length > editorShapes.length) { + const editorShapeIds = new Set(editorShapes.map(s => s.id)) + const missingShapes = storeShapes.filter(s => !editorShapeIds.has(s.id)) + console.warn(`๐Ÿ“Š Board: ${missingShapes.length} shapes in store but not visible to editor:`, missingShapes.map(s => ({ id: s.id, + type: s.type, + x: s.x, + y: s.y, parentId: s.parentId }))) - // Fix: Move shapes to the current page - console.log(`๐Ÿ“Š Board: Moving ${shapesOnOtherPages.length} shapes to current page ${currentPageId}`) - const shapesToMove = shapesOnOtherPages.map(s => ({ - id: s.id, - type: s.type, - parentId: currentPageId - })) + // Check if missing shapes are on a different page + const shapesOnCurrentPage = missingShapes.filter(s => s.parentId === currentPageId) + const shapesOnOtherPages = missingShapes.filter(s => s.parentId !== currentPageId) + console.log(`๐Ÿ“Š Board: Missing shapes on current page: ${shapesOnCurrentPage.length}, on other pages: ${shapesOnOtherPages.length}`) - try { - editor.updateShapes(shapesToMove) - console.log(`๐Ÿ“Š Board: Successfully moved ${shapesToMove.length} shapes to current page`) - } catch (error) { - console.error(`๐Ÿ“Š Board: Error moving shapes to current page:`, error) + if (shapesOnOtherPages.length > 0) { + console.log(`๐Ÿ“Š Board: Shapes on other pages:`, shapesOnOtherPages.map(s => ({ + id: s.id, + parentId: s.parentId + }))) + + // Fix: Move shapes to the current page + console.log(`๐Ÿ“Š Board: Moving ${shapesOnOtherPages.length} shapes to current page ${currentPageId}`) + const shapesToMove = shapesOnOtherPages.map(s => ({ + id: s.id, + type: s.type, + parentId: currentPageId + })) + + try { + editor.updateShapes(shapesToMove) + console.log(`๐Ÿ“Š Board: Successfully moved ${shapesToMove.length} shapes to current page`) + } catch (error) { + console.error(`๐Ÿ“Š Board: Error moving shapes to current page:`, error) + } } } } - */ }, [editor]) // Update presence when session changes @@ -317,9 +391,57 @@ export function Board() { }; }, [editor, roomId, store.store]); + // Handle Escape key to cancel active tool and return to hand tool + // Also prevent Escape from deleting shapes + useEffect(() => { + if (!editor) return; + + const handleKeyDown = (event: KeyboardEvent) => { + // Only handle Escape key + if (event.key === 'Escape') { + // Check if the event target or active element is an input field or textarea + const target = event.target as HTMLElement; + const activeElement = document.activeElement; + const isInputFocused = (target && ( + target.tagName === 'INPUT' || + target.tagName === 'TEXTAREA' || + target.isContentEditable + )) || (activeElement && ( + activeElement.tagName === 'INPUT' || + activeElement.tagName === 'TEXTAREA' || + activeElement.isContentEditable + )); + + // If an input is focused, let it handle Escape (don't prevent default) + // This allows components like Obsidian notes to handle Escape for canceling edits + if (isInputFocused) { + return; // Let the event propagate to the component's handler + } + + // Otherwise, prevent default to stop tldraw from deleting shapes + // and switch to hand tool + event.preventDefault(); + event.stopPropagation(); + + const currentTool = editor.getCurrentToolId(); + // Only switch if we're not already on the hand tool + if (currentTool !== 'hand') { + editor.setCurrentTool('hand'); + } + } + }; + + document.addEventListener('keydown', handleKeyDown, true); // Use capture phase to intercept early + + return () => { + document.removeEventListener('keydown', handleKeyDown, true); + }; + }, [editor]); + return ( -
- +
+ - - -
+ +
+
+ ) } \ No newline at end of file diff --git a/src/routes/LocationDashboardRoute.tsx b/src/routes/LocationDashboardRoute.tsx new file mode 100644 index 0000000..c8da207 --- /dev/null +++ b/src/routes/LocationDashboardRoute.tsx @@ -0,0 +1,29 @@ +import React from 'react'; +import { LocationDashboard } from '@/components/location/LocationDashboard'; + +export const LocationDashboardRoute: React.FC = () => { + return ; +}; + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/routes/LocationShareCreate.tsx b/src/routes/LocationShareCreate.tsx new file mode 100644 index 0000000..d13c509 --- /dev/null +++ b/src/routes/LocationShareCreate.tsx @@ -0,0 +1,29 @@ +import React from 'react'; +import { ShareLocation } from '@/components/location/ShareLocation'; + +export const LocationShareCreate: React.FC = () => { + return ; +}; + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/routes/LocationShareView.tsx b/src/routes/LocationShareView.tsx new file mode 100644 index 0000000..d8465a3 --- /dev/null +++ b/src/routes/LocationShareView.tsx @@ -0,0 +1,43 @@ +import React from 'react'; +import { useParams } from 'react-router-dom'; +import { LocationViewer } from '@/components/location/LocationViewer'; + +export const LocationShareView: React.FC = () => { + const { token } = useParams<{ token: string }>(); + + if (!token) { + return ( +
+
+

Invalid Share Link

+

No share token provided in the URL

+
+
+ ); + } + + return ; +}; + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/shapes/ChatBoxShapeUtil.tsx b/src/shapes/ChatBoxShapeUtil.tsx index a3cf623..36020de 100644 --- a/src/shapes/ChatBoxShapeUtil.tsx +++ b/src/shapes/ChatBoxShapeUtil.tsx @@ -1,5 +1,6 @@ import { useEffect, useRef, useState } from "react" -import { BaseBoxShapeUtil, TLBaseShape } from "tldraw" +import { BaseBoxShapeUtil, TLBaseShape, HTMLContainer } from "tldraw" +import { StandardizedToolWrapper } from "../components/StandardizedToolWrapper" export type IChatBoxShape = TLBaseShape< "ChatBox", @@ -17,24 +18,53 @@ export class ChatBoxShape extends BaseBoxShapeUtil { getDefaultProps(): IChatBoxShape["props"] { return { roomId: "default-room", - w: 100, - h: 100, + w: 400, + h: 500, userName: "", } } + // ChatBox theme color: Orange (Rainbow) + static readonly PRIMARY_COLOR = "#f97316" + indicator(shape: IChatBoxShape) { return } component(shape: IChatBoxShape) { + const [isMinimized, setIsMinimized] = useState(false) + const isSelected = this.editor.getSelectedShapeIds().includes(shape.id) + + const handleClose = () => { + this.editor.deleteShape(shape.id) + } + + const handleMinimize = () => { + setIsMinimized(!isMinimized) + } + return ( - + + + + + ) } } @@ -114,10 +144,12 @@ export const ChatBox: React.FC = ({ className="chat-container" style={{ pointerEvents: "all", - width: `${w}px`, - height: `${h}px`, - overflow: "auto", + width: '100%', + height: '100%', + overflow: "hidden", touchAction: "auto", + display: "flex", + flexDirection: "column", }} >
diff --git a/src/shapes/EmbedShapeUtil.tsx b/src/shapes/EmbedShapeUtil.tsx index ae6efd0..a368c1f 100644 --- a/src/shapes/EmbedShapeUtil.tsx +++ b/src/shapes/EmbedShapeUtil.tsx @@ -173,9 +173,14 @@ export class EmbedShape extends BaseBoxShapeUtil { } component(shape: IEmbedShape) { + // Ensure shape props exist with defaults + const props = shape.props || {} + const url = props.url || "" + const isMinimized = props.isMinimized || false + const isSelected = this.editor.getSelectedShapeIds().includes(shape.id) - const [inputUrl, setInputUrl] = useState(shape.props.url || "") + const [inputUrl, setInputUrl] = useState(url) const [error, setError] = useState("") const [copyStatus, setCopyStatus] = useState(false) diff --git a/src/shapes/FathomMeetingsBrowserShapeUtil.tsx b/src/shapes/FathomMeetingsBrowserShapeUtil.tsx new file mode 100644 index 0000000..7f065ea --- /dev/null +++ b/src/shapes/FathomMeetingsBrowserShapeUtil.tsx @@ -0,0 +1,79 @@ +import { + BaseBoxShapeUtil, + HTMLContainer, + TLBaseShape, +} from "tldraw" +import React, { useState } from "react" +import { FathomMeetingsPanel } from "../components/FathomMeetingsPanel" +import { StandardizedToolWrapper } from "../components/StandardizedToolWrapper" + +type IFathomMeetingsBrowser = TLBaseShape< + "FathomMeetingsBrowser", + { + w: number + h: number + } +> + +export class FathomMeetingsBrowserShape extends BaseBoxShapeUtil { + static override type = "FathomMeetingsBrowser" as const + + getDefaultProps(): IFathomMeetingsBrowser["props"] { + return { + w: 800, + h: 600, + } + } + + // Fathom theme color: Blue (Rainbow) + static readonly PRIMARY_COLOR = "#3b82f6" + + component(shape: IFathomMeetingsBrowser) { + const { w, h } = shape.props + const [isOpen, setIsOpen] = useState(true) + const [isMinimized, setIsMinimized] = useState(false) + const isSelected = this.editor.getSelectedShapeIds().includes(shape.id) + + const handleClose = () => { + setIsOpen(false) + // Delete the browser shape after a short delay + setTimeout(() => { + this.editor.deleteShape(shape.id) + }, 100) + } + + const handleMinimize = () => { + setIsMinimized(!isMinimized) + } + + if (!isOpen) { + return null + } + + return ( + + + + + + ) + } + + indicator(shape: IFathomMeetingsBrowser) { + return + } +} diff --git a/src/shapes/FathomTranscriptShapeUtil.tsx b/src/shapes/FathomTranscriptShapeUtil.tsx new file mode 100644 index 0000000..3171ecf --- /dev/null +++ b/src/shapes/FathomTranscriptShapeUtil.tsx @@ -0,0 +1,372 @@ +import { + BaseBoxShapeUtil, + HTMLContainer, + TLBaseShape, +} from "tldraw" +import React, { useState, useRef, useEffect, useMemo, useCallback } from "react" +import { StandardizedToolWrapper } from "../components/StandardizedToolWrapper" + +type IFathomTranscript = TLBaseShape< + "FathomTranscript", + { + w: number + h: number + meetingId: string + meetingTitle: string + meetingUrl: string + summary: string + transcript: Array<{ + speaker: string + text: string + timestamp: string + }> + actionItems: Array<{ + text: string + assignee?: string + dueDate?: string + }> + isExpanded: boolean + showTranscript: boolean + showActionItems: boolean + } +> + +export class FathomTranscriptShape extends BaseBoxShapeUtil { + static override type = "FathomTranscript" as const + + // Fathom Transcript theme color: Blue (same as FathomMeetings) + static readonly PRIMARY_COLOR = "#3b82f6" + + getDefaultProps(): IFathomTranscript["props"] { + return { + w: 600, + h: 400, + meetingId: "", + meetingTitle: "", + meetingUrl: "", + summary: "", + transcript: [], + actionItems: [], + isExpanded: false, + showTranscript: true, + showActionItems: true, + } + } + + component(shape: IFathomTranscript) { + const { + w, + h, + meetingId, + meetingTitle, + meetingUrl, + summary, + transcript, + actionItems, + isExpanded, + showTranscript, + showActionItems + } = shape.props + + const [isHovering, setIsHovering] = useState(false) + const [isMinimized, setIsMinimized] = useState(false) + const isSelected = this.editor.getSelectedShapeIds().includes(shape.id) + + const toggleExpanded = useCallback(() => { + this.editor.updateShape({ + id: shape.id, + type: 'FathomTranscript', + props: { + ...shape.props, + isExpanded: !isExpanded + } + }) + }, [shape.id, shape.props, isExpanded]) + + const toggleTranscript = useCallback(() => { + this.editor.updateShape({ + id: shape.id, + type: 'FathomTranscript', + props: { + ...shape.props, + showTranscript: !showTranscript + } + }) + }, [shape.id, shape.props, showTranscript]) + + const toggleActionItems = useCallback(() => { + this.editor.updateShape({ + id: shape.id, + type: 'FathomTranscript', + props: { + ...shape.props, + showActionItems: !showActionItems + } + }) + }, [shape.id, shape.props, showActionItems]) + + const formatTimestamp = (timestamp: string) => { + // Convert timestamp to readable format + const seconds = parseInt(timestamp) + const minutes = Math.floor(seconds / 60) + const remainingSeconds = seconds % 60 + return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}` + } + + // Custom header content with meeting info and toggle buttons + const headerContent = ( +
+
+ ๐ŸŽฅ Fathom Meeting + {meetingId && #{meetingId}} +
+
+ + + +
+
+ ) + + const handleMinimize = () => { + setIsMinimized(!isMinimized) + } + + const handleClose = () => { + this.editor.deleteShape(shape.id) + } + + const contentStyle: React.CSSProperties = { + padding: '16px', + flex: 1, + overflow: 'auto', + color: 'black', + fontSize: '12px', + lineHeight: '1.4', + cursor: 'pointer', + transition: 'background-color 0.2s ease', + display: 'flex', + flexDirection: 'column', + gap: '12px', + } + + const buttonStyle: React.CSSProperties = { + padding: '4px 8px', + fontSize: '10px', + border: '1px solid #ccc', + borderRadius: '4px', + backgroundColor: 'white', + cursor: 'pointer', + zIndex: 1000, + position: 'relative', + pointerEvents: 'auto', + } + + const transcriptEntryStyle: React.CSSProperties = { + marginBottom: '8px', + padding: '8px', + backgroundColor: '#f8f9fa', + borderRadius: '4px', + borderLeft: '3px solid #007bff', + } + + const actionItemStyle: React.CSSProperties = { + marginBottom: '6px', + padding: '6px', + backgroundColor: '#fff3cd', + borderRadius: '4px', + borderLeft: '3px solid #ffc107', + } + + return ( + + + +
+ {/* Meeting Title */} +
+

+ {meetingTitle || 'Untitled Meeting'} +

+ {meetingUrl && ( + e.stopPropagation()} + > + View in Fathom โ†’ + + )} +
+ + {/* Summary */} + {summary && ( +
+

+ ๐Ÿ“‹ Summary +

+
+ {summary} +
+
+ )} + + {/* Action Items */} + {showActionItems && actionItems.length > 0 && ( +
+

+ โœ… Action Items ({actionItems.length}) +

+
+ {actionItems.map((item, index) => ( +
+
+ {item.text} +
+ {item.assignee && ( +
+ ๐Ÿ‘ค {item.assignee} +
+ )} + {item.dueDate && ( +
+ ๐Ÿ“… {item.dueDate} +
+ )} +
+ ))} +
+
+ )} + + {/* Transcript */} + {showTranscript && transcript.length > 0 && ( +
+

+ ๐Ÿ’ฌ Transcript ({transcript.length} entries) +

+
+ {transcript.map((entry, index) => ( +
+
+ + {entry.speaker} + + + {formatTimestamp(entry.timestamp)} + +
+
+ {entry.text} +
+
+ ))} +
+
+ )} + + {/* Empty state */} + {!summary && transcript.length === 0 && actionItems.length === 0 && ( +
+ No meeting data available +
+ )} +
+
+
+ ) + } + + indicator(shape: IFathomTranscript) { + return + } +} + + + + + + + + + + + + + + + + + diff --git a/src/shapes/HolonBrowserShapeUtil.tsx b/src/shapes/HolonBrowserShapeUtil.tsx new file mode 100644 index 0000000..48d918a --- /dev/null +++ b/src/shapes/HolonBrowserShapeUtil.tsx @@ -0,0 +1,171 @@ +import { + BaseBoxShapeUtil, + HTMLContainer, + TLBaseShape, +} from "tldraw" +import React, { useState } from "react" +import { HolonBrowser } from "../components/HolonBrowser" +import { HolonData } from "../lib/HoloSphereService" +import { StandardizedToolWrapper } from "../components/StandardizedToolWrapper" + +type IHolonBrowser = TLBaseShape< + "HolonBrowser", + { + w: number + h: number + } +> + +export class HolonBrowserShape extends BaseBoxShapeUtil { + static override type = "HolonBrowser" as const + + getDefaultProps(): IHolonBrowser["props"] { + return { + w: 800, + h: 600, + } + } + + // Holon theme color: Green (Rainbow) + static readonly PRIMARY_COLOR = "#22c55e" + + component(shape: IHolonBrowser) { + const { w, h } = shape.props + const [isOpen, setIsOpen] = useState(true) + const [isMinimized, setIsMinimized] = useState(false) + const isSelected = this.editor.getSelectedShapeIds().includes(shape.id) + + const handleSelectHolon = (holonData: HolonData) => { + // Store current camera position to prevent it from changing + const currentCamera = this.editor.getCamera() + this.editor.stopCameraAnimation() + + // Get the browser shape bounds to position the new Holon shape nearby + const browserShapeBounds = this.editor.getShapePageBounds(shape.id) + const shapeWidth = 700 + const shapeHeight = 400 + + let xPosition: number + let yPosition: number + + if (browserShapeBounds) { + // Position to the right of the browser shape + const spacing = 20 + xPosition = browserShapeBounds.x + browserShapeBounds.w + spacing + yPosition = browserShapeBounds.y + } else { + // Fallback to viewport center if shape bounds not available + const viewport = this.editor.getViewportPageBounds() + const centerX = viewport.x + viewport.w / 2 + const centerY = viewport.y + viewport.h / 2 + xPosition = centerX - shapeWidth / 2 + yPosition = centerY - shapeHeight / 2 + } + + const holonShape = this.editor.createShape({ + type: 'Holon', + x: xPosition, + y: yPosition, + props: { + w: shapeWidth, + h: shapeHeight, + name: holonData.name, + description: holonData.description || '', + latitude: holonData.latitude, + longitude: holonData.longitude, + resolution: holonData.resolution, + holonId: holonData.id, + isConnected: true, + isEditing: false, + selectedLens: 'general', + data: holonData.data, + connections: [], + lastUpdated: holonData.timestamp + } + }) + + console.log('โœ… Created Holon shape from browser:', holonShape.id) + + // Restore camera position if it changed + const newCamera = this.editor.getCamera() + if (currentCamera.x !== newCamera.x || currentCamera.y !== newCamera.y || currentCamera.z !== newCamera.z) { + this.editor.setCamera(currentCamera, { animation: { duration: 0 } }) + } + + // Select the new shape + setTimeout(() => { + // Preserve camera position when selecting + const cameraBeforeSelect = this.editor.getCamera() + this.editor.stopCameraAnimation() + this.editor.setSelectedShapes([`shape:${holonShape.id}`] as any) + // Restore camera if it changed during selection + const cameraAfterSelect = this.editor.getCamera() + if (cameraBeforeSelect.x !== cameraAfterSelect.x || cameraBeforeSelect.y !== cameraAfterSelect.y || cameraAfterSelect.z !== cameraAfterSelect.z) { + this.editor.setCamera(cameraBeforeSelect, { animation: { duration: 0 } }) + } + }, 100) + + // Close the browser shape + setIsOpen(false) + // Delete the browser shape after a short delay + setTimeout(() => { + this.editor.deleteShape(shape.id) + }, 100) + } + + const handleClose = () => { + setIsOpen(false) + // Delete the browser shape + setTimeout(() => { + this.editor.deleteShape(shape.id) + }, 100) + } + + const handleMinimize = () => { + setIsMinimized(!isMinimized) + } + + if (!isOpen) { + return null + } + + return ( + + + + + + ) + } + + indicator(shape: IHolonBrowser) { + return + } +} + + + + + + + + + + + diff --git a/src/shapes/HolonShapeUtil.tsx b/src/shapes/HolonShapeUtil.tsx new file mode 100644 index 0000000..85fa1fe --- /dev/null +++ b/src/shapes/HolonShapeUtil.tsx @@ -0,0 +1,915 @@ +import { + BaseBoxShapeUtil, + HTMLContainer, + TLBaseShape, +} from "tldraw" +import React, { useState, useRef, useEffect, useMemo, useCallback } from "react" +import { holosphereService, HoloSphereService, HolonData, HolonLens, HolonConnection } from "@/lib/HoloSphereService" +import * as h3 from 'h3-js' +import { StandardizedToolWrapper } from "../components/StandardizedToolWrapper" + +type IHolon = TLBaseShape< + "Holon", + { + w: number + h: number + name: string + description?: string + latitude: number + longitude: number + resolution: number + holonId: string + isConnected: boolean + isEditing?: boolean + editingName?: string + editingDescription?: string + selectedLens?: string + data: Record + connections: HolonConnection[] + lastUpdated: number + } +> + +// Auto-resizing textarea component for editing +const AutoResizeTextarea: React.FC<{ + value: string + onChange: (value: string) => void + onBlur: () => void + onKeyDown: (e: React.KeyboardEvent) => void + style: React.CSSProperties + placeholder?: string + onPointerDown?: (e: React.PointerEvent) => void + onWheel?: (e: React.WheelEvent) => void +}> = ({ value, onChange, onBlur, onKeyDown, style, placeholder, onPointerDown, onWheel }) => { + const textareaRef = useRef(null) + + useEffect(() => { + if (textareaRef.current) { + textareaRef.current.focus() + } + }, [value]) + + return ( +