onWatch icon indicating copy to clipboard operation
onWatch copied to clipboard

feat: add Moonshot/Kimi and DeepSeek providers (balance-based)

Open papajade55-debug opened this issue 7 months ago • 1 comments

Summary

Adds two pay-as-you-go providers to onWatch by mirroring the existing OpenRouter pattern:

  • Moonshot/Kimi — GET https://api.moonshot.ai/v1/users/me/balance (CNY)
  • DeepSeek — GET https://api.deepseek.com/user/balance (USD/CNY)

Both expose a remaining-balance (not cumulative usage) → semantic inversion of the reset detection: a recharge (≥50% balance growth) is treated as cycle reset; TotalDelta cumulates balance drops (money spent).

What's new

Layer New
internal/api/ moonshot_{client,types}.go + deepseek_{client,types}.go (+tests)
internal/agent/ moonshot_agent.go, deepseek_agent.go
internal/store/ moonshot_store.go, deepseek_store.go (with currency column for DeepSeek; +tests)
internal/tracker/ moonshot_tracker.go, deepseek_tracker.go (+tests)
internal/web/ moonshot_handlers.go, deepseek_handlers.go

What's modified

  • internal/config/config.go — MOONSHOT_API_KEY, DEEPSEEK_API_KEY env wiring + extends AvailableProviders/HasProvider
  • internal/store/store.go — table migrations
  • internal/web/{handlers.go, static/app.js, templates/dashboard.html} — Material Design 3 dashboard tabs
  • internal/metrics/metrics.go — Prometheus gauges onwatch_credits_balance{unit="cny_available"|"cny_cash"|"usd_total"|"usd_topped_up"|"usd_granted"}
  • main.go — agent factory registration + boot sequence

Edge cases handled

  • DeepSeek is_available=false → log warning, skip snapshot
  • DeepSeek balance as JSON string → parsed via parseFloat
  • Currency priority: CNY > USD (configurable on tracker)
  • HTTP 401/429/5xx → log + retry on next cycle, don't crash
  • Zero balance → marked as "exhausted" state in cycle metadata

Tests

1224 unit tests pass across internal/api/..., internal/store/..., internal/tracker/.... Build green on Go 1.25.7.

$ go test ./internal/api/... ./internal/store/... ./internal/tracker/...
ok  ...  (1224 passed in 3 packages)

Live verification

Tested on Linux Mint 22.3, Cinnamon 6.6.7, kernel 6.17:

$ curl -u admin:pass http://localhost:9211/metrics | grep credits_balance
onwatch_credits_balance{provider="moonshot",unit="cny_available"} 19.4738
onwatch_credits_balance{provider="moonshot",unit="cny_cash"} 19.4738
onwatch_credits_balance{provider="deepseek",unit="usd_total"} 34.69
onwatch_credits_balance{provider="deepseek",unit="usd_topped_up"} 34.69
onwatch_credits_balance{provider="deepseek",unit="usd_granted"} 0

Note on Moonshot baseURL

Default endpoint set to api.moonshot.ai (international). Users on the Chinese moonshot.cn API endpoint may need to patch the baseURL constant or expose it via an env var (MOONSHOT_BASE_URL) — happy to add that as a follow-up if you'd like it in this PR.

Test plan

  • [ ] go build ./... succeeds
  • [ ] go test ./... green
  • [ ] MOONSHOT_API_KEY=... populates moonshot tab
  • [ ] DEEPSEEK_API_KEY=... populates deepseek tab
  • [ ] Recharge detection triggers a new cycle (balance growth ≥50%)

🤖 Generated with Claude Code — code by Jules

papajade55-debug avatar Apr 25 '26 01:04 papajade55-debug