feat(#123): port Ollama LLM provider
Implemented first concrete LLM provider following the provider interface pattern. Implementation: - OllamaProvider class implementing LlmProviderInterface - All required methods: initialize(), checkHealth(), listModels(), chat(), chatStream(), embed(), getConfig() - OllamaProviderConfig extending LlmProviderConfig - Proper error handling with NestJS Logger - Configuration immutability protection Features: - System prompt injection support - Temperature and max tokens configuration - Embedding with truncation control (defaults to enabled) - Streaming and non-streaming chat completions - Health check with model listing Testing: - 21 comprehensive test cases (TDD approach) - 100% statement, function, and line coverage - 86.36% branch coverage (exceeds 85% requirement) - All error scenarios tested - Mock-based unit tests Code Review Fixes: - Fixed truncate logic to match original LlmService behavior (defaults to true) - Added test for system prompt deduplication - Increased branch coverage from 77% to 86% Quality Gates: - ✅ All 21 tests passing - ✅ Linting clean - ✅ Type checking passed - ✅ Code review approved Fixes #123 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
82
docs/scratchpads/123-ollama-provider.md
Normal file
82
docs/scratchpads/123-ollama-provider.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# Issue #123: Port Ollama LLM Provider
|
||||
|
||||
## Objective
|
||||
|
||||
Create a reusable OllamaProvider class that implements LlmProviderInterface. This extracts the existing Ollama functionality from LlmService into a provider-based architecture, enabling multi-provider support.
|
||||
|
||||
## Approach
|
||||
|
||||
Following TDD principles:
|
||||
|
||||
1. Write tests first for OllamaProvider
|
||||
2. Implement provider to satisfy interface
|
||||
3. Port existing functionality from LlmService.ts
|
||||
4. Ensure all tests pass with ≥85% coverage
|
||||
|
||||
## Design Decisions
|
||||
|
||||
### OllamaProviderConfig
|
||||
|
||||
Extends LlmProviderConfig with Ollama-specific options:
|
||||
|
||||
```typescript
|
||||
interface OllamaProviderConfig extends LlmProviderConfig {
|
||||
endpoint: string; // Base config (e.g., "http://localhost:11434")
|
||||
timeout?: number; // Base config (default: 30000ms)
|
||||
}
|
||||
```
|
||||
|
||||
### Implementation Details
|
||||
|
||||
- Uses `ollama` npm package (already installed)
|
||||
- Implements all LlmProviderInterface methods
|
||||
- Handles errors gracefully with proper logging
|
||||
- Returns copies of config to prevent external modification
|
||||
|
||||
### Methods to Implement
|
||||
|
||||
- ✓ `initialize()` - Set up Ollama client
|
||||
- ✓ `checkHealth()` - Verify connection and list models
|
||||
- ✓ `listModels()` - Return available model names
|
||||
- ✓ `chat()` - Synchronous chat completion
|
||||
- ✓ `chatStream()` - Streaming chat completion
|
||||
- ✓ `embed()` - Generate embeddings
|
||||
- ✓ `getConfig()` - Return configuration copy
|
||||
|
||||
## Progress
|
||||
|
||||
- [x] Create scratchpad
|
||||
- [x] Write test file (ollama.provider.spec.ts)
|
||||
- [x] Implement OllamaProvider class
|
||||
- [x] Verify all tests pass (20/20 passing)
|
||||
- [x] Check code coverage ≥85% (100% achieved)
|
||||
- [x] Run lint and typecheck (all passing)
|
||||
- [x] Stage files for commit
|
||||
|
||||
## Final Results
|
||||
|
||||
- All 20 tests passing
|
||||
- Code coverage: 100% statements, 100% functions, 100% lines, 77.27% branches
|
||||
- Lint: Clean (auto-fixed formatting)
|
||||
- TypeCheck: No errors
|
||||
- TDD workflow successfully followed (RED → GREEN → REFACTOR)
|
||||
|
||||
## Testing
|
||||
|
||||
Tests cover:
|
||||
|
||||
- Initialization
|
||||
- Health checks (success and failure)
|
||||
- Model listing
|
||||
- Chat completion (sync and stream)
|
||||
- Embeddings
|
||||
- Error handling
|
||||
- Configuration management
|
||||
|
||||
## Notes
|
||||
|
||||
- Existing LlmService.ts has working Ollama implementation to port
|
||||
- Interface already defined with clear contracts
|
||||
- DTOs already created and validated
|
||||
- Focus on clean separation of concerns
|
||||
- Provider should be stateless except for Ollama client instance
|
||||
Reference in New Issue
Block a user