refactor: rewrite handler-base as Go module

Replace Python handler-base library with Go module providing:
- config: environment-based configuration
- health: HTTP health/readiness server for k8s probes
- natsutil: NATS/JetStream client with msgpack serialization
- telemetry: OpenTelemetry tracing and metrics setup
- clients: HTTP clients for LLM, embeddings, reranker, STT, TTS
- handler: base Handler runner wiring NATS + health + telemetry

Implements ADR-0061 Phase 1.
This commit is contained in:
2026-02-19 17:16:17 -05:00
parent 5eb2c43a5d
commit d321c9852b
38 changed files with 1345 additions and 6971 deletions

129
README.md
View File

@@ -1,109 +1,44 @@
# Handler Base
# handler-base
Shared base library for building NATS-based AI/ML handler services.
Go module providing shared infrastructure for NATS-based handler services.
## Installation
## Packages
```bash
pip install handler-base
```
| Package | Purpose |
|---------|---------|
| `config` | Environment-based configuration via struct fields |
| `health` | HTTP health/readiness server for Kubernetes probes |
| `natsutil` | NATS/JetStream client with msgpack serialization |
| `telemetry` | OpenTelemetry tracing and metrics setup |
| `clients` | HTTP clients for LLM, embeddings, reranker, STT, TTS |
| `handler` | Base Handler runner wiring NATS + health + telemetry |
Or from Gitea:
```bash
pip install git+https://git.daviestechlabs.io/daviestechlabs/handler-base.git
```
## Usage
## Quick Start
```go
package main
```python
from handler_base import Handler, Settings
from nats.aio.msg import Msg
class MyHandler(Handler):
async def setup(self):
# Initialize your clients
pass
async def handle_message(self, msg: Msg, data: dict):
# Process the message
result = {"processed": True}
return result
if __name__ == "__main__":
MyHandler(subject="my.subject").run()
```
## Features
- **Handler base class** - NATS subscription, graceful shutdown, signal handling
- **NATSClient** - Connection management, JetStream, msgpack serialization
- **Settings** - Pydantic-based configuration from environment
- **HealthServer** - Kubernetes liveness/readiness probes
- **Telemetry** - OpenTelemetry tracing and metrics
- **Service clients** - HTTP wrappers for AI services
## Service Clients
```python
from handler_base.clients import (
STTClient, # Whisper speech-to-text
TTSClient, # XTTS text-to-speech
LLMClient, # vLLM chat completions
EmbeddingsClient, # BGE embeddings
RerankerClient, # BGE reranker
MilvusClient, # Vector database
import (
"context"
"git.daviestechlabs.io/daviestechlabs/handler-base/config"
"git.daviestechlabs.io/daviestechlabs/handler-base/handler"
"github.com/nats-io/nats.go"
)
func main() {
cfg := config.Load()
cfg.ServiceName = "my-service"
h := handler.New("my.subject", cfg)
h.OnMessage(func(ctx context.Context, msg *nats.Msg, data map[string]any) (map[string]any, error) {
return map[string]any{"ok": true}, nil
})
h.Run()
}
```
## Configuration
## Testing
All settings via environment variables:
| Variable | Default | Description |
|----------|---------|-------------|
| `NATS_URL` | `nats://localhost:4222` | NATS server URL |
| `NATS_USER` | - | NATS username |
| `NATS_PASSWORD` | - | NATS password |
| `NATS_QUEUE_GROUP` | - | Queue group for load balancing |
| `HEALTH_PORT` | `8080` | Health check server port |
| `OTEL_ENABLED` | `true` | Enable OpenTelemetry |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4317` | OTLP endpoint |
| `OTEL_SERVICE_NAME` | `handler` | Service name for traces |
## Docker
```dockerfile
FROM ghcr.io/daviestechlabs/handler-base:latest
COPY my_handler.py /app/
CMD ["python", "/app/my_handler.py"]
```
Or build with audio support:
```bash
docker build --build-arg INSTALL_AUDIO=true -t my-handler .
go test ./...
```
## Module Structure
```
handler_base/
├── __init__.py # Public API exports
├── handler.py # Base Handler class
├── nats_client.py # NATS connection wrapper
├── config.py # Pydantic Settings
├── health.py # Health check server
├── telemetry.py # OpenTelemetry setup
└── clients/
├── embeddings.py
├── llm.py
├── milvus.py
├── reranker.py
├── stt.py
└── tts.py
```
## Related
- [voice-assistant](https://git.daviestechlabs.io/daviestechlabs/voice-assistant) - Voice pipeline using handler-base
- [homelab-design](https://git.daviestechlabs.io/daviestechlabs/homelab-design) - Architecture docs