Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
220d930
Add module infrastructure with feature-based architecture
aniketkno May 5, 2026
7bfe061
Add core and feature modules
aniketkno May 5, 2026
c63c5ce
Add app module with Compose UI
aniketkno May 5, 2026
7fffbfa
Update app source files for feature module integration
aniketkno May 5, 2026
328d0ee
Update app utility classes for refactored architecture
aniketkno May 5, 2026
a3d366d
Update .gitignore for new module structure
aniketkno May 5, 2026
7b72242
Update documentation for new feature-based architecture
aniketkno May 5, 2026
5c98dac
Remove deprecated build files
aniketkno May 5, 2026
20b7751
Add testing ground documentation and diagrams
aniketkno May 5, 2026
e9f0500
Fix build errors in core:ai module
aniketkno May 5, 2026
ff42498
Integrate LiteRT-LM with real Engine API
aniketkno May 5, 2026
68669df
Add ModelManager for model download management
aniketkno May 5, 2026
29f9fb1
Update Settings with ModelManager and Compose UI
aniketkno May 5, 2026
302bad3
Fix ChatViewModel to use real LiteRT-LM inference
aniketkno May 5, 2026
ad7fa3a
feat(ai): add model tracking, list, load, and delete per-path
aniketkno May 5, 2026
13a8f82
refactor(app): move ModelManager to core:ai module and fix imports
aniketkno May 5, 2026
2c25987
feat(ui): merge inference model management into settings tab
aniketkno May 5, 2026
0b659ec
feat(ui): restructure tabs - merge Process into Think tab
aniketkno May 5, 2026
a6b936f
docs: update architecture and documentation for new features
aniketkno May 5, 2026
8742532
feat(processing): implement real document parsers and extraction pipe…
aniketkno May 5, 2026
3f8b584
feat(ai): add ONNX MiniLM embedder, VectorStoreProvider, and GPU acce…
aniketkno May 5, 2026
c2e75d8
feat(chat): add conversation persistence, file attachments, and redes…
aniketkno May 5, 2026
23af643
feat(notebooks): auto-enqueue extraction jobs and observe worker status
aniketkno May 5, 2026
a5e9204
feat(app): integrate chat, notebooks, and ONNX embedder with DI wiring
aniketkno May 5, 2026
4731996
docs: update architecture docs and changelog for new features
aniketkno May 5, 2026
65fc840
docs: Update documentation for MessageParts and streaming architecture
aniketkno May 7, 2026
d5de863
feat(ai): Add streaming token filter and MessagePart architecture
aniketkno May 7, 2026
8a8bd5b
feat(ai): Update inference bridges for parts-based streaming
aniketkno May 7, 2026
143849d
feat(chat): Add rich message rendering with Markdown support
aniketkno May 7, 2026
899c462
feat(chat): Update ChatViewModel for parts-based inference and persis…
aniketkno May 7, 2026
7f16465
feat(notebooks): Add notebook picker and chat integration
aniketkno May 7, 2026
31f6a23
feat(settings): Add model download UI and status visualization
aniketkno May 7, 2026
3b02101
feat(processing): Add speech recognition and audio transcription
aniketkno May 7, 2026
8d5cbd7
chore(app): Update app shell and build configuration
aniketkno May 7, 2026
44e5c5c
docs: sync documentation with actual codebase state
aniketkno May 7, 2026
7a22700
docs: reorganize and condense documentation into docs/ folder
aniketkno May 7, 2026
67d7337
feat(ai): add unloadModel() and isUnloading to InferenceBridge interface
aniketkno May 8, 2026
44177db
feat(ai): implement unloadModel() and isUnloading in LiteRtInferenceB…
aniketkno May 8, 2026
4c39197
feat(ai): implement unloadModel() in OllamaInferenceBridge
aniketkno May 8, 2026
f025820
feat(app): add shared model status across all tabs in MainScreen
aniketkno May 8, 2026
e273106
refactor(chat): simplify ToggleModel handler in ChatViewModel
aniketkno May 8, 2026
e61e20b
feat(notebooks): add model status indicator to NotebookListScreen top…
aniketkno May 8, 2026
636096c
feat(ui): add ModelStatusIndicator component
aniketkno May 8, 2026
730d7d7
docs: update documentation with model toggle feature
aniketkno May 8, 2026
efb61ec
feat(core): implement audio recording, analysis, and inference bridge…
aniketkno May 8, 2026
6686fab
feat(feature): migrate notebooks to stacks and implement audio features
aniketkno May 8, 2026
3a446a6
chore(app): update build config, manifest, and app icon
aniketkno May 9, 2026
3e9665a
fix(ai): add LOADING/READY states and reflection fix for LiteRT API
aniketkno May 9, 2026
c9bf79b
refactor(app): rename notebooks to stacks and update chat/settings fe…
aniketkno May 9, 2026
83fbb0e
feat(app): implement audio recording and model status toggle
aniketkno May 9, 2026
439995e
docs: update documentation with model toggle and architecture changes
aniketkno May 9, 2026
878b0ee
docs: update documentation with LiteRT fixes and stacks refactoring
aniketkno May 9, 2026
a2ae554
feat(settings): add default system prompt configuration
aniketkno May 9, 2026
44f4abb
feat(chat): add system prompt over-drag panel and prepend to messages
aniketkno May 9, 2026
5658c23
refactor(data): add systemPrompt and agentPrompt to entities and DAOs
aniketkno May 9, 2026
6df8307
feat(processing): add agentPrompt parameter to WorkerLauncher
aniketkno May 9, 2026
5624671
docs: update TODO.md with completed prompt/agent steps
aniketkno May 9, 2026
ead8233
fix(data): bump database version to 6 for schema changes
aniketkno May 9, 2026
8dc9d2e
fix(stacks): stream AI analysis results progressively in ProcessBlocks
aniketkno May 9, 2026
3d4bbb3
fix(chat): properly extract model responses and update navigation
aniketkno May 9, 2026
53d9367
fix(ui): resolve FAB overlaps, pin chat TopBar, extend AI messages
aniketkno May 9, 2026
4cf5ea6
feat(chat): enhance MarkdownText with syntax highlighting and LaTeX s…
aniketkno May 10, 2026
45b3e60
feat(ui): update MainScreen FAB visibility and routing
aniketkno May 11, 2026
5024be7
feat(notebooks): add chat FAB and onNavigateToChat callback
aniketkno May 11, 2026
94ea7a5
feat(ui): adjust record audio button position in activity_main.xml
aniketkno May 11, 2026
f845f2b
feat(chat): enhance ChatScreen UI with cancel button and loading indi…
aniketkno May 11, 2026
1c74448
feat(chat): add retry logic and cancellation support in ChatViewModel
aniketkno May 11, 2026
ae48c8f
feat(stacks): update StackListScreen with selection mode and chat FAB
aniketkno May 11, 2026
59774e1
feat(stacks): refactor StackScreen organization and improve over-drag…
aniketkno May 11, 2026
da75e14
feat(stacks): add selection mode and auto-rename functionality
aniketkno May 11, 2026
9b44f26
feat(ai): add tool infrastructure (Tool, ToolSchema, ToolExecutor, To…
aniketkno May 11, 2026
7ce47f2
feat(ai): add web search and content fetching tools
aniketkno May 11, 2026
3f3033e
feat(ai): add builtin tools for knowledge search and stack reading
aniketkno May 11, 2026
1b0bfd7
feat(data): add getAllSourceIds for listing stored web sources
aniketkno May 11, 2026
5ccbb5e
feat(chat): add tool execution integration to ChatViewModel
aniketkno May 11, 2026
7f5ad58
docs: update architecture docs with agent framework documentation
aniketkno May 11, 2026
e1d7d10
fix(chat): add suspend modifier to executeToolAndContinue function
aniketkno May 11, 2026
86db53c
WIP DEMO
aniketkno May 12, 2026
8e89ff7
Fix for Kotlin version with Java target 17
aniketkno May 12, 2026
a98ae40
fixed huggingface token link
rendermaniac May 12, 2026
d008ccd
working demo quite fast
aniketkno May 12, 2026
abe76de
working Vectorstores needs better implementations
aniketkno May 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ google-services.json

# Android Profiling
*.hprof

models/
.ruff_cache/
.kotlin/
.idea/
.vscode
models/
810 changes: 0 additions & 810 deletions ARCHITECTURE.md

This file was deleted.

133 changes: 0 additions & 133 deletions CHANGELOG.md

This file was deleted.

252 changes: 252 additions & 0 deletions CORE_AI_REFERENCES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
# Core AI Module References

## Module Dependency Graph

```
:core:ai
├── depends on: :core:data
├── used by: :app
├── used by: :feature:chat
├── used by: :feature:inference
├── used by: :feature:notebooks
├── used by: :feature:process
├── used by: :feature:settings
└── used by: :core:processing
```

## Gradle Dependencies

### Direct Dependencies on `:core:ai`

| Module | File | Type |
|--------|------|------|
| `:app` | `app/build.gradle.kts:105` | `api(project(":core:ai"))` |
| `:feature:chat` | `feature/chat/build.gradle.kts:22` | `api(project(":core:ai"))` |
| `:feature:inference` | `feature/inference/build.gradle.kts:22` | `api(project(":core:ai"))` |
| `:feature:notebooks` | `feature/notebooks/build.gradle.kts:22` | `api(project(":core:ai"))` |
| `:feature:process` | `feature/process/build.gradle.kts:22` | `api(project(":core:ai"))` |
| `:feature:settings` | `feature/settings/build.gradle.kts:22` | `api(project(":core:ai"))` |
| `:core:processing` | `core/processing/build.gradle.kts:29` | `implementation(project(":core:ai"))` |

### Project Inclusion
- `settings.gradle.kts:23` — `include(":core:ai")`

## Design References

### Opencode Library Parts Architecture

The structured message parsing in Penpal is inspired by the [opencode](https://github.com/anomalyco/opencode) library's use of the Vercel AI SDK.

**Key Differences:**

| Aspect | Opencode (Vercel AI SDK) | Penpal (LiteRT-LM) |
|--------|--------------------------|-------------------|
| **Events** | `reasoning-start/delta/end`, `text-start/delta/end` | Raw Gemma 4 tokens (`<|channel>`, `<|tool_call>`) |
| **Parsing** | SDK handles provider-specific tokens | Custom `StreamingTokenFilter` with trie matching |
| **Parts** | `TextPart`, `ReasoningPart`, `ToolPart` | `TextPart`, `ReasoningPart`, `ToolCallPart`, `ToolResponsePart` |
| **Streaming** | Event-based state machine | `Flow<List<MessagePart>>` with mode transitions |
| **Tool Lifecycle** | `pending → running → completed/error` | `ToolStatus` enum with same states |

**Why We Built It Differently:**
- LiteRT-LM doesn't emit structured events like Vercel AI SDK
- We must parse raw tokens ourselves using `StreamingTokenFilter`
- The `MessagePartAggregator` serves as our equivalent to opencode's processor.ts state machine

**Reference Files:**
- `opencode/src/lib/ai/message-v2.ts` — Part type definitions (TextPart, ReasoningPart, ToolPart)
- `opencode/src/lib/ai/processor.ts` — Event processor that builds parts from streaming events

## Source Code References

### App Module (`:app`)

#### `app/src/main/java/com/drawapp/PenpalApplication.kt`
- **Line 6-11** — Imports `InferenceBridge`, `LiteRtInferenceBridge`, `MiniLmEmbedder`, `ModelManager`, `OnnxMiniLmEmbedder`, `VectorStoreRepositoryImpl`
- **Line 61-62** — Creates `WordPieceTokenizer` from assets with fallback
- **Line 75** — Sets `VectorStoreProvider.instance` for global access

**Usage:** Application-wide initialization of AI components (tokenizer, embedder, vector store)

#### `app/src/main/java/com/drawapp/MainActivity.kt`
- **Line 20** — Imports `ModelManager`

**Usage:** Likely checks model status or triggers model operations in main activity

#### `app/src/main/java/com/drawapp/NotebookSelectionActivity.kt`
- **Line 22** — Imports `ModelManager`

**Usage:** Model management in notebook selection UI

#### `app/src/main/java/com/drawapp/ModelDownloadReceiver.kt`
- **Line 7** — Imports `ModelManager`

**Usage:** Handles download completion broadcasts for model files

#### `app/src/main/java/com/drawapp/ModelDownloadHelper.kt`
- **Line 15** — Imports `ModelManager`

**Usage:** Helper utilities for model download operations

### Feature: Chat (`:feature:chat`)

#### `feature/chat/src/main/java/com/penpal/feature/chat/ChatViewModel.kt`
- **Line 8** — Imports `InferenceBridge`
- **Line 9** — Imports `VectorStoreRepository`

**Usage:**
- `InferenceBridge` — Powers chat message generation (streaming responses)
- `VectorStoreRepository` — Retrieves relevant context for RAG (Retrieval-Augmented Generation)

### Feature: Inference (`:feature:inference`)

#### `feature/inference/src/main/java/com/penpal/feature/inference/InferenceViewModel.kt`
- **Line 6** — Imports `DownloadProgress`
- **Line 7** — Imports `InferenceBridge`
- **Line 8** — Imports `ModelManager`

**Usage:**
- `DownloadProgress` — UI progress bar for model downloads
- `InferenceBridge` — Run inference operations
- `ModelManager` — Check model availability and download status

#### `feature/inference/src/main/java/com/penpal/feature/inference/InferenceScreen.kt`
- **Line 20** — Imports `ModelManager`

**Usage:** UI screen showing inference controls and model status

### Feature: Settings (`:feature:settings`)

#### `feature/settings/src/main/java/com/penpal/feature/settings/SettingsViewModel.kt`
- **Line 7** — Imports `InferenceBridge`
- **Line 8** — Imports `ModelManager`
- **Line 9** — Imports `ModelStatus`

**Usage:**
- `InferenceBridge` — Model initialization and status
- `ModelManager` — Model path management, token configuration
- `ModelStatus` — Display download/installation state in settings UI

#### `feature/settings/src/main/java/com/penpal/feature/settings/SettingsScreen.kt`
- **Line 16** — Imports `ModelManager`
- **Line 17** — Imports `ModelStatus`

**Usage:** Compose UI for settings screen showing model download progress and status

### Core: Processing (`:core:processing`)

#### `core/processing/src/main/java/com/penpal/core/processing/Parsers.kt`
- **Line 11** — Imports `RawChunk`

**Usage:** Document parsing that produces `RawChunk` objects for embedding and storage

#### `core/processing/src/main/java/com/penpal/core/processing/DocumentParser.kt`
- **Line 4** — Imports `RawChunk`

**Usage:** Parses documents into chunks that will be embedded by `VectorStoreRepository`

#### `core/processing/src/main/java/com/penpal/core/processing/ExtractionWorker.kt`
- **Line 8** — Imports `VectorStoreRepository`
- **Line 80** — Uses `VectorStoreProvider.instance` to access repository

**Usage:** Background WorkManager worker that extracts text and stores embeddings

## Reference Summary by Class

| Class | Used In | Purpose |
|-------|---------|---------|
| `InferenceBridge` | `ChatViewModel`, `SettingsViewModel`, `InferenceViewModel`, `PenpalApplication` | Main LLM inference contract |
| `LiteRtInferenceBridge` | `PenpalApplication` | On-device inference implementation |
| `ModelManager` | `MainActivity`, `NotebookSelectionActivity`, `ModelDownloadReceiver`, `ModelDownloadHelper`, `InferenceViewModel`, `SettingsViewModel`, `SettingsScreen`, `InferenceScreen` | Model file management and downloads |
| `ModelStatus` | `SettingsViewModel`, `SettingsScreen` | UI state for model installation |
| `DownloadProgress` | `InferenceViewModel` | Download progress tracking |
| `VectorStoreRepository` | `ChatViewModel`, `ExtractionWorker` | Semantic search and storage |
| `VectorStoreProvider` | `PenpalApplication`, `ExtractionWorker` | Global access to vector store |
| `VectorStoreRepositoryImpl` | `PenpalApplication` | Repository implementation |
| `OnnxMiniLmEmbedder` | `PenpalApplication` | Text embedding generation |
| `MiniLmEmbedder` | `PenpalApplication` | Fallback/mock embedder |
| `WordPieceTokenizer` | `PenpalApplication` | Text tokenization |
| `RawChunk` | `Parsers`, `DocumentParser` | Chunk data structure |
| `MessagePart` | `ChatViewModel`, `ChatScreen` | Structured message parts (Text, Reasoning, ToolCall) |
| `StreamingTokenFilter` | `LiteRtInferenceBridge`, `OllamaInferenceBridge` | Real-time special token filtering with mode transitions |
| `MessagePartAggregator` | `LiteRtInferenceBridge`, `OllamaInferenceBridge` | Builds MessageParts from streaming chunks |
| `MarkdownText` | `ChatScreen` | Lightweight markdown renderer for TextPart content |

## Usage Patterns

### 1. Application Initialization Pattern
**Location:** `PenpalApplication.kt`
**Pattern:** Initialize all AI components once at app startup and expose via singleton provider

### 2. ViewModel Injection Pattern
**Locations:** `ChatViewModel`, `InferenceViewModel`, `SettingsViewModel`
**Pattern:** ViewModels receive `InferenceBridge` and other AI components via constructor or factory

### 3. Background Worker Pattern
**Location:** `ExtractionWorker.kt`
**Pattern:** WorkManager workers access `VectorStoreRepository` via static `VectorStoreProvider` (avoids DI in workers)

### 4. UI Binding Pattern
**Locations:** `SettingsScreen.kt`, `InferenceScreen.kt`
**Pattern:** Compose UI collects StateFlow from ViewModels that wrap AI module state

### 5. Model Download Pattern
**Locations:** `ModelDownloadReceiver.kt`, `ModelDownloadHelper.kt`, `InferenceViewModel`
**Pattern:** DownloadManager + BroadcastReceiver for model file downloads with progress callbacks

### 6. Structured Message Parts Pattern
**Locations:** `ChatViewModel.kt`, `ChatScreen.kt`, `LiteRtInferenceBridge.kt`
**Pattern:**
1. `StreamingTokenFilter.appendWithTransitions()` detects mode changes from Gemma 4 tokens
2. `MessagePartAggregator.processChunk()` builds immutable `MessagePart` objects
3. `ChatViewModel` collects `Flow<List<MessagePart>>` and stores on `ChatMessage`
4. `ChatScreen` renders different part types with distinct UI (collapsible thinking, tool call cards)

## Impact Analysis

### If `:core:ai` is modified, these modules need testing:
1. **`:app`** — Application initialization may fail
2. **`:feature:chat`** — Chat functionality broken (especially MessagePart rendering)
3. **`:feature:inference`** — Model inference broken
4. **`:feature:settings`** — Model management UI broken
5. **`:core:processing`** — Document extraction and embedding broken
6. **`:feature:notebooks`** — Notebook features may use AI components

### MessagePart Architecture Impact
Changes to `MessagePart` sealed class affect:
- **UI**: `ChatScreen.kt` must handle all part subtypes
- **ViewModel**: `ChatViewModel.kt` stores parts on `ChatMessage`
- **Bridge**: `LiteRtInferenceBridge.kt` and `OllamaInferenceBridge.kt` must produce correct parts
- **Filter**: `StreamingTokenFilter.kt` mode transitions map directly to part boundaries
7. **`:feature:process`** — Process features may use AI components

### Public API Surface:
- `InferenceBridge` interface and all implementations
- `VectorStoreRepository` interface
- `ModelManager` object (singleton)
- `ModelStatus` enum
- `DownloadProgress` data class
- `RawChunk` data class
- `VectorStoreProvider` object
- `WordPieceTokenizer` class
- `StreamingTokenFilter` class
- All Ollama data classes

## External References

### LiteRT-LM Documentation
- [Overview](https://ai.google.dev/edge/litert-lm/overview)
- [Android Guide](https://ai.google.dev/edge/litert-lm/android)
- [GitHub Repository](https://github.com/google-ai-edge/LiteRT-LM)

### Models
- [HuggingFace LiteRT Community](https://huggingface.co/litert-community)
- [Gemma-4-E2B-it](https://huggingface.co/litert-community/gemma-4-E2B-it-litert-lm)
- [Google AI Edge Gallery](https://github.com/google-ai-edge/gallery)

### Gemma Documentation
- [Gemma Prompt Formatting](https://ai.google.dev/gemma/docs/core/prompt-formatting-gemma4)
- [Run Gemma](https://ai.google.dev/gemma/docs/run)

### Related Frameworks (Not Used)
- MediaPipe LLM Inference API — Deprecated for Android/iOS, migrate to LiteRT-LM
- Ollama — Used as alternative/development backend
Loading