Skip to content

Add Cohere provider (native streaming + tool use) #18

@quangdang46

Description

@quangdang46

Add Cohere provider (native streaming + tool use)

Summary

Add a first-class Cohere provider with native streaming and tool calling. pi_agent_rust ships this as one of its 10 native providers; jcode does not currently support Cohere at all.

Reference: pi_agent_rust src/providers/cohere.rs and the provider list.

Why

Current state in jcode

  • src/provider/ lists Anthropic, OpenAI, Gemini, Bedrock, etc. No cohere.rs.
  • --provider CLI doc string in src/cli/args.rs does not list cohere.
  • No COHERE_API_KEY detection in src/cli/login/ flows.

Implementation checklist

1. Provider implementation

  • Add src/provider/cohere.rs implementing the Provider trait:
    • Auth: Bearer $COHERE_API_KEY.
    • Endpoint: https://api.cohere.com/v2/chat (or v2/chat-stream).
    • Streaming: parse Cohere's event-stream chunks (event: ... + data: ...).
    • Tool-call format: map jcode ToolCall { name, args } to Cohere's parameters block; map results back from tool_results.
    • Reasoning support: surface tool_plan if present as a thinking block.
  • Add Cohere to src/provider/mod.rs registry and the ProviderChoice enum.

2. Models

  • Register at least: command-r-08-2024, command-r-plus-08-2024, command-a-03-2025. Context windows: 128k for r/r+ and 256k for a. Pull these into src/provider_catalog.rs.
  • Document the canonical list and let users discover with jcode model list --provider cohere.

3. Auth + login

  • Detect COHERE_API_KEY env var.
  • Add jcode login --provider cohere flow: prompt for API key (with --api-key/--api-key-stdin), store via the standard auth.json mechanism.
  • Add Cohere to auth-test covered providers.

4. CLI surface

  • Update --provider doc string in src/cli/args.rs to include cohere.
  • Update README.md provider list.

5. Error handling

  • Map common error responses to typed errors: 401 → unauthenticated (surface in auth-test), 429 → rate-limit (apply existing retry/backoff), 400 with invalid_argument → log + redacted payload preview.

6. Telemetry

  • Reuse the existing provider telemetry hooks so usage/cost are tracked.

Testing

Unit

  • Streaming parser tests against fixture event streams from Cohere docs.
  • Tool-call round-trip: jcode tool spec → Cohere request body → mocked response → parsed tool call → result delivered back.
  • Auth-test "no key" path exits with an actionable error.

Live test (gated)

  • Add live_tests::test_cohere behind JCODE_LIVE=1. Asserts a known short response with one tool call.

Manual

  • jcode login --provider cohere, then jcode -m command-r-plus run "List files in src/" and verify a working tool call.

Acceptance criteria

  • jcode --provider cohere -m command-r-plus run "..." works.
  • Streaming, tool-call, and reasoning blocks all render in the TUI.
  • jcode auth-test --provider cohere returns a clean OK.
  • cargo test cohere passes; cargo clippy --all-targets -- -D warnings clean.

References


Implementation notes addendum (Devin gap-analysis pass, 2026-05-21)

Verified jcode code paths

  • Provider scaffolding mirrors existing native providers in src/provider/ (e.g. src/provider/anthropic.rs, src/provider/openai.rs, src/provider/claude.rs, src/provider/gemini.rs).
  • Provider catalog: src/provider_catalog.rs + crates/jcode-provider-metadata/src/catalog.rs.
  • Streaming SSE parser: src/transport/ (reuse, do not write a new one).
  • Per-provider tests pattern: tests/integration/<provider>.rs (offline fixtures via recorded JSON streams).

Cohere API specifics

  • API: Cohere v2 chat/completions (https://api.cohere.com/v2/chat).
  • Auth: Authorization: Bearer <api-key> from env CO_API_KEY (also accept COHERE_API_KEY for compat).
  • Streaming: Server-Sent Events; event types include message-start, content-delta, tool-call-start, tool-call-delta, tool-call-end, message-end. Map to jcode's internal stream events.
  • Tool use: Cohere has native tools array in the request; pass jcode's tool schemas through unchanged.
  • Models to register at launch: command-r-plus, command-r, command-r-08-2024, command-a-03-2025 (most recent at issue-time). Source via https://api.cohere.com/v1/models.

Acceptance criteria

  • jcode --provider cohere --model command-r-plus "..." works end-to-end.
  • Streaming output renders incrementally.
  • Tool calls work (test with read).
  • jcode --list-providers includes Cohere.
  • Token/cost accounting populated.
  • Tests cover one streamed response and one tool-call round-trip via offline fixtures.

Cross-references

Reference

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions