ClaudeBar icon indicating copy to clipboard operation
ClaudeBar copied to clipboard

feat: Extend Mistral support with Code API probe for Vibe usage

Open AlexanderWillner opened this issue 5 months ago • 5 comments

Summary

Extend Mistral support with Code API probe for Vibe Coding Plan usage percentage from chat.mistral.ai.

Changes

New files:

  • Sources/Infrastructure/Mistral/MistralAPIUsageProbe.swift — NDJSON-parsing probe for chat.mistral.ai/api/code-trpc/
  • Sources/App/Views/Settings/MistralConfigCard.swift — Settings card with Local Logs / Code API mode picker
  • Sources/Domain/Provider/Mistral/MistralProbeMode.swift — localLogs vs api enum
  • Tests/InfrastructureTests/Mistral/MistralAPIUsageProbeParsingTests.swift — 7 parsing tests for NDJSON

Testing

890/890 tests passing, 0 build warnings.

Setup

export MISTRAL_CHAT_COOKIE="ory_session_...=...; csrftoken=...; ..."

Settings → Mistral → Code API Mode

Summary by CodeRabbit

  • New Features

    • Mistral provider now supports two usage detection modes (Local Logs and API) with a settings card to choose and persist the mode.
    • UI and settings expose Mistral credential storage for API-based usage checks; provider can use either mode at runtime.
  • Documentation

    • README updated with Mistral setup instructions and steps to obtain/refresh chat session credentials.
  • Tests

    • Added tests covering Mistral API response parsing and error cases.

AlexanderWillner avatar Jun 10 '26 06:06 AlexanderWillner

image

AlexanderWillner avatar Jun 10 '26 06:06 AlexanderWillner

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9216f381-1b7b-4fb1-9944-6af698c99da1

📥 Commits

Reviewing files that changed from the base of the PR and between 4883e725c8555af878304d46dc544206a1c46b54 and 2e8cae004ff393ba38bbceb4bd2fe4ff742fb6aa.

📒 Files selected for processing (5)
  • README.md
  • Sources/Domain/Provider/Mistral/MistralProvider.swift
  • Sources/Domain/Provider/ProviderSettingsRepository.swift
  • Sources/Infrastructure/Mistral/MistralAPIUsageProbe.swift
  • Tests/InfrastructureTests/Mistral/MistralAPIUsageProbeParsingTests.swift
✅ Files skipped from review due to trivial changes (1)
  • README.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • Sources/Domain/Provider/ProviderSettingsRepository.swift
  • Tests/InfrastructureTests/Mistral/MistralAPIUsageProbeParsingTests.swift
  • Sources/Domain/Provider/Mistral/MistralProvider.swift
  • Sources/Infrastructure/Mistral/MistralAPIUsageProbe.swift

📝 Walkthrough

Walkthrough

Adds selectable Mistral probe modes (Local Logs or API), persists mode and cookie settings, implements an authenticated Mistral tRPC API probe with NDJSON/JSON parsing, wires provider initialization, adds a Settings UI card, updates storage implementations, and adds parsing tests.

Changes

Mistral Provider API Support

Layer / File(s) Summary
Probe Mode and Settings Repository Contracts
Sources/Domain/Provider/Mistral/MistralProbeMode.swift, Sources/Domain/Provider/ProviderSettingsRepository.swift
Adds MistralProbeMode enum and MistralSettingsRepository protocol for probe mode, auth env-var name, and session cookie persistence.
MistralProvider Refactor for Dual-Probe Support
Sources/Domain/Provider/Mistral/MistralProvider.swift
Refactors provider to hold localLogsProbe and optional apiProbe, exposes probeMode backed by settings, selects activeProbe with fallback, and routes availability/refresh through it.
MistralAPIUsageProbe Implementation
Sources/Infrastructure/Mistral/MistralAPIUsageProbe.swift
Implements API probe resolving session cookies (env var or stored), performs tRPC batched GET to chat.mistral.ai, parses NDJSON/JSON for usagePercentage/resetAt, and maps errors to ProbeError.
Parsing Tests
Tests/InfrastructureTests/Mistral/MistralAPIUsageProbeParsingTests.swift
NDJSON/JSON fixtures and tests for normal, near-limit, missing-data, parse-failure, and unauthorized/error responses.
Settings Repository Implementations
Sources/Infrastructure/Storage/JSONSettingsRepository.swift, Sources/Infrastructure/Storage/UserDefaultsProviderSettingsRepository.swift, Sources/App/Settings/AppSettings.swift
Conform repository implementations to MistralSettingsRepository, persist probe mode and auth env var, and add session-cookie save/get/delete/exists; AppSettings exposes mistral accessor.
App Initialization Wiring
Sources/App/ClaudeBarApp.swift
Initializes MistralProvider with both MistralUsageProbe (local logs) and MistralAPIUsageProbe (API) using the shared settings repository.
Settings UI Integration
Sources/App/Views/Settings/MistralConfigCard.swift, Sources/App/Views/SettingsView.swift
Adds MistralConfigCard with a segmented probe-mode picker and descriptions; integrates into SettingsView under a new mistral provider conditional section.
User-Facing Documentation
README.md
Adds “Mistral Setup” instructions covering Local Logs and Code API modes and steps to export the session Cookie for API mode.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant SettingsUI as MistralConfigCard
  participant AppSettings
  participant MistralProvider
  participant ActiveProbe
  participant Network as chat.mistral.ai
  User->>SettingsUI: Select probe mode
  SettingsUI->>AppSettings: setMistralProbeMode(newValue)
  AppSettings->>AppSettings: Persist to repository
  SettingsUI->>MistralProvider: Trigger refresh for "mistral"
  MistralProvider->>AppSettings: Read probeMode
  alt Mode == .api and apiProbe available
    MistralProvider->>ActiveProbe: probe()
    ActiveProbe->>AppSettings: resolve session cookie (env or stored)
    ActiveProbe->>Network: GET tRPC batch with Cookie header
    Network-->>ActiveProbe: NDJSON/JSON response
    ActiveProbe->>MistralProvider: Return UsageSnapshot
  else Fallback to local logs
    MistralProvider->>ActiveProbe: probe() (localLogsProbe)
    ActiveProbe-->>MistralProvider: Return UsageSnapshot
  end
  MistralProvider-->>SettingsUI: Update quota display

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • tddworks/ClaudeBar#161: Prior Mistral local logs probe wiring and related provider refactor that this PR extends with API mode and dual-probe selection.

Poem

🐰 Two probes, one choice—a rabbit's delight,
Local logs hum quiet, or API takes flight.
Cookies tucked in settings, mode switches with care,
Parsing NDJSON whispers the quota laid bare.
🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: Extend Mistral support with Code API probe for Vibe usage' accurately captures the primary change: adding a Code API probe to Mistral for reporting Vibe usage, with mode selection between Local Logs and Code API.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jun 10 '26 06:06 coderabbitai[bot]

Codecov Report

:x: Patch coverage is 34.42623% with 120 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 80.61%. Comparing base (3a5e737) to head (2e8cae0). :warning: Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../Infrastructure/Mistral/MistralAPIUsageProbe.swift 47.01% 71 Missing :warning:
...nfrastructure/Storage/JSONSettingsRepository.swift 0.00% 19 Missing :warning:
...orage/UserDefaultsProviderSettingsRepository.swift 0.00% 18 Missing :warning:
...ces/Domain/Provider/Mistral/MistralProbeMode.swift 0.00% 12 Missing :warning:

:x: Your patch check has failed because the patch coverage (34.42%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #209      +/-   ##
==========================================
- Coverage   81.58%   80.61%   -0.98%     
==========================================
  Files         115      117       +2     
  Lines        8679     8862     +183     
==========================================
+ Hits         7081     7144      +63     
- Misses       1598     1718     +120     
Files with missing lines Coverage Δ
...s/Domain/Provider/ProviderSettingsRepository.swift 100.00% <ø> (ø)
...ces/Domain/Provider/Mistral/MistralProbeMode.swift 0.00% <0.00%> (ø)
...orage/UserDefaultsProviderSettingsRepository.swift 74.01% <0.00%> (-7.17%) :arrow_down:
...nfrastructure/Storage/JSONSettingsRepository.swift 57.08% <0.00%> (-4.62%) :arrow_down:
.../Infrastructure/Mistral/MistralAPIUsageProbe.swift 47.01% <47.01%> (ø)
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Jun 10 '26 06:06 codecov[bot]

Addressed all CodeRabbit review findings in 2e8cae0:

Fixed:

  • Removed raw response body logging that leaked vibeApiKey into persistent file logs
  • Added nested tRPC error detection (findError) so wrapped errors like {"json":[[{"error":"Unauthorized"}]]} throw executionFailed instead of falling through as noData
  • Fixed "Vibe API" → "Code API" in doc comments (MistralProvider, ProviderSettingsRepository)
  • Pinned resetsAt test to UTC calendar (timezone-independent)
  • Tightened error tests to assert exact ProbeError cases
  • Improved README: Network tab in DevTools steps, security warning for cookie handling

Skipped:

  • @State private var settings = AppSettings.shared in MistralConfigCard — this is the established pattern across all 9+ config cards (ClaudeConfigCard, CopilotConfigCard, KimiConfigCard, etc.). Changing it would be inconsistent.

All 890+ tests pass.

AlexanderWillner avatar Jun 10 '26 12:06 AlexanderWillner

Yes, please add Mistral!

dzienisz avatar Jun 14 '26 17:06 dzienisz