deepseek
Shared Rust client for the DeepSeek API with pluggable HTTP transport, agentic tool-use loop, and in-memory caching.
Modules
| Module | Feature | Description |
|---|---|---|
types |
— | Core API types: messages, requests, responses, models |
error |
— | DeepSeekError enum and Result<T> alias |
client |
— | HttpClient trait + generic DeepSeekClient<H> |
reqwest_client |
reqwest-client |
Native reqwest transport, client_from_env(), reason(), reason_with_retry() |
wasm |
wasm |
Cloudflare Workers fetch-based transport |
agent |
agent |
Tool trait, AgentBuilder, DeepSeekAgent, streaming run() loop with SdkMessage events |
agent::builtin_tools |
builtin-tools |
Read / Write / Edit / Glob / Grep / Bash, Claude-Code-style |
cache |
cache |
TTL cache + broadcast-based in-flight deduplication |
Feature Flags
| Flag | Default | Description |
|---|---|---|
reqwest-client |
yes | Native HTTP via reqwest + tokio |
wasm |
no | CF Workers WASM fetch transport (?Send) |
agent |
no | Streaming agent loop (run, SdkMessage, RunOptions, PermissionMode) |
builtin-tools |
no | Read / Write / Edit / Glob / Grep / Bash (implies agent) |
scheduler |
yes | Scheduler + CronCreate / CronList / CronDelete / Monitor tools and /loop semantics (implies agent + builtin-tools) |
cli |
no | deepseek-loop binary (implies scheduler + builtin-tools + reqwest-client) |
cache |
no | TTL cache + in-flight dedup (dashmap, sha2, tokio) |
Key Types
Models & Effort
// aliases: "r1"/"deep" → Reasoner, "v3"/"fast" → Chat
// Low: 0.1 temp / 2048 tokens → Max: 1.0 temp / 16384 tokens
Messages
// Constructors
Request Builder (free function)
Usable without a client instance — builds a typed ChatRequest with effort-mapped temperature/max_tokens.
Client
// reqwest-client feature
pub async
Agent (feature agent)
Streaming loop — Claude Code shape
The run() function (and the DeepSeekAgent::run() method) yields a typed
sequence of [SdkMessage] events that mirror the Claude Agent SDK's loop:
System{Init} ← session metadata, fresh uuid session id
Assistant{ ToolUse… } ← per turn the model requests tools
User{ ToolResult… } ← combined results fed back to the model
…repeat until no tool_use…
Assistant{ Text } ← final answer
Result{ subtype, result, total_cost_usd, usage, num_turns, session_id, stop_reason }
RunOptions lets you configure: model, system_prompt, allowed_tools,
disallowed_tools, max_turns, max_budget_usd, effort,
permission_mode, pre_tool_hook, session_id, base_url.
Read-only tools (those whose read_only_hint() == true) are dispatched in
parallel within a turn; mutating tools run sequentially. PermissionMode
mirrors the SDK: Default, AcceptEdits, Plan, DontAsk,
BypassPermissions. Hitting max_turns / max_budget_usd yields a
Result with the matching error subtype.
CLI (feature cli)
The binary streams SdkMessage events as NDJSON (one JSON object per line) on stdout. Pipe it into jq or any line-oriented consumer.
# One-shot run
# Read-only review (Plan mode blocks Bash/Write/Edit)
Scheduler / /loop (feature scheduler, on by default)
Matches Claude Code's /loop semantics. Four built-in tools (CronCreate, CronList, CronDelete, Monitor) plus three CLI invocation shapes:
# Fixed cron interval (5 minutes between fires)
# Dynamic interval — agent picks delay each iteration (60s floor)
# Bare /loop — runs the built-in maintenance prompt (or your loop.md)
# Resume a prior session's tasks
loop.md discovery: .claude/loop.md (project) → ~/.claude/loop.md (user) → built-in default. Capped at 25 KB.
Cron grammar: 5-field minute hour dom month dow with *, 5, */15, 1-5, 1,15,30. Extended syntax (L, W, ?, MON, JAN) is rejected.
Limits: 50 tasks per session, recurring tasks expire 7 days after creation, deterministic jitter is applied to fire times. Set CLAUDE_CODE_DISABLE_CRON=1 (or the DEEPSEEK_LOOP_DISABLE_CRON=1 alias) to disable the scheduler entirely.
Persistence: tasks are written to ${cache_dir}/deepseek-loop/sessions/<session_id>/tasks.json after each mutation, restored via --resume <session_id>.
Cache (feature cache)
// entry-level TTL, evicts expired on insert
// broadcast-based — first caller inserts, others subscribe
Retry Logic
reason_with_retry() retries only on 5xx / network errors with exponential backoff (1s → 2s → 4s). 4xx errors fail immediately.
Dependencies
Standalone library — no sibling crate dependencies. Used by sdd, research, and genesis.