The core data-handling logic has been successfully migrated to TypeScript and refactored into modular files. This directly addresses the reasoning persistence bug and improves overall code quality.
-
frontend/types.ts(392 lines)- Centralized all type definitions
- Added helper functions:
messageToUIMessage,createDefaultSessionTree - Defines:
Session,Message,Branch,SessionTree,UIMessage,AppState, etc.
-
frontend/core/db.ts(NEW - 175 lines)- Database initialization and schema
- Dexie setup with migrations v1-v11
- Handles prototype reset logic
-
frontend/core/sessions.ts(NEW - 470 lines)- Session CRUD operations
loadSessions,loadSession,saveSession(critical for reasoning persistence)createNewChat,selectChat,deleteChat,duplicateChat- Session tree and branch management
-
frontend/core/folders.ts(NEW - 93 lines)- Folder management
- CRUD operations for folders
-
frontend/core/messages.ts(NEW - 284 lines)- Message operations
ensureChatExistsForEdits,persistMessageAtIndexdeleteMessage,deleteMessagesBelowrerunFromUser,rerunAssistantwith branching support
-
frontend/core/import-export.ts(NEW - 337 lines)- Data import/export functionality
exportAllData,importDataReplace,importDataAppend
-
frontend/core/index.ts(NEW - 54 lines)- Central export file for all core modules
- Provides backwards-compatible API
-
frontend/core/jobManager.ts(172 lines)- Converted from
.js - SSE streaming for chat jobs
- Proper TypeScript types for job data
- Converted from
-
frontend/core/branchManager.ts(559 lines)- Already converted (previous work)
- Core branching operations
-
frontend/experimental/streaming.ts(432 lines)- Already converted (previous work)
- Streaming logic with reasoning persistence fix
-
deno.json- Added TypeScript compiler options
- Added import alias:
"@/types": "./frontend/types.ts" - Enabled strict type checking
-
Updated Imports
frontend/chatterApp.js- Uses'./core/index.ts'frontend/dataIO.js- Uses'./core/index.ts'
- ❌
frontend/core/chatCore.js(1310 lines) → Replaced by modular TypeScript files - ❌
frontend/core/jobManager.js→ Converted tojobManager.ts
The bug was caused by inconsistent data handling across multiple files. Now:
- All data types are centralized in
types.ts saveSessionexplicitly handlesreasoningfield with proper null checksstreaming.tsupdatesstate.messages[idx].reasoningduring streamingloadSessionpopulatesstate.messageReasoningMapfrom session tree
- Compile-time error detection
- Better IDE autocomplete
- Catch null/undefined issues before runtime
- Each file has a single responsibility
- Easier to find and update code
- Smaller, more manageable files
- Clear separation of concerns
- Easier to test individual modules
- Better code organization
frontend/
├── types.ts (392 lines) - Central type definitions
├── core/
│ ├── index.ts (54 lines) - Central export
│ ├── db.ts (175 lines) - Database init
│ ├── sessions.ts (470 lines) - Session operations
│ ├── folders.ts (93 lines) - Folder management
│ ├── messages.ts (284 lines) - Message operations
│ ├── import-export.ts (337 lines) - Data I/O
│ ├── jobManager.ts (172 lines) - Job streaming
│ └── branchManager.ts (559 lines) - Branching logic
├── experimental/
│ └── streaming.ts (432 lines) - Streaming logic
└── ...
- Database initialization works
- Create new chat
- Send message
- Message appears in UI
- Send message with reasoning enabled
- Verify reasoning appears in UI
- Refresh page
- Verify reasoning still appears
- Switch branches
- Verify reasoning persists across branches
- Fork at user message
- Fork at assistant message (regen)
- Switch between branches
- Delete message with branches
- Edit message
- Delete message
- Delete messages below
- Rerun from user
- Regenerate assistant
- Create folder
- Move chat to folder
- Toggle folder collapsed
- Export all data
- Import data (replace)
- Import data (append)
-
Convert
chatterApp.jsto TypeScript (1890 lines - large task)- Would provide even more type safety
- Not critical since core logic is already typed
-
Convert other utility files
apiClient.js,chatterLib.js,i18n.js, etc.- Lower priority
-
Add unit tests
- Test core functions in isolation
- Ensure reasoning persistence works correctly
- The migration was done without JSDoc comments to keep files clean and rely on TypeScript's type system
- All imports are backwards-compatible through
frontend/core/index.ts - No breaking changes to the API
- The modular structure makes future refactoring easier
