Add two DeepSeek LLM adapters: dsh-llm-deepseek and dsh-llm-pi-ai

The first real LlmAdapter implementations, shipped as a deliberate pair:
same models and wire protocol, completely different internals, so the
StreamChunk protocol is verified across independent implementations.

- dsh-llm-deepseek: hand-rolled fetch + SSE parser + chunk-translation
  state machine against the official chat-completions format (thinking
  mode via top-level thinking/reasoning_effort; the empty-string
  reasoning_content first chunk; usage attached to the finish chunk or
  trailing; reasoning_content passback on tool-call turns; disjoint
  cache-token accounting).
- dsh-llm-pi-ai: the same endpoint through @earendil-works/pi-ai,
  mapping its event vocabulary (parsed tool arguments, in-stream error
  events, folded reasoning tokens) onto the same chunks.

The agent loop now honors the in-band error path: an adapter that ends
its stream with finish {kind:error|aborted} (the only option for
adapters that can't throw mid-stream, like pi-ai) is translated into a
step error, so the turn ends error/aborted with a logged error event
instead of a normal completed assistant message. This makes the
StreamChunk error contract real for both adapters; docs/architecture.md
and the StreamChunk doc are updated accordingly.

New yarn test:e2e (vitest.e2e.config.ts, *.e2e.ts) runs key-gated
real-API matrices for both adapters across V4 Flash/Pro and all
thinking/effort levels; it self-skips without DEEPSEEK_API_KEY. Unit
suites run against local node:http mock SSE servers at 100% per-file
coverage.
This commit is contained in:
Tianyi Cui
2026-06-13 00:28:29 +08:00
parent 8b5a3ef730
commit ab19fed77c
38 changed files with 4567 additions and 28 deletions

View File

@@ -112,9 +112,14 @@ into blocks/messages; the loop logs raw chunks (replay fidelity) while feeding
the same chunks through an assembler.
`LlmAdapter` is the provider seam: subclass, implement `stream()`, call
`ctx.llm.registerAdapter(models, adapter)`.
**TODO**: the DeepSeek V4 adapter is the first real adapter (next phase); the
streaming protocol gets a careful review then.
`ctx.llm.registerAdapter(models, adapter)`. Two real adapters implement it —
`dsh-llm-deepseek` (hand-rolled fetch/SSE against the DeepSeek API) and
`dsh-llm-pi-ai` (the same endpoint through the `@earendil-works/pi-ai`
library). They exist as a pair deliberately: two independent internals over
one contract verified the StreamChunk protocol, which is now documented (in
`dsh-llm/src/types.ts`) with the conventions that review pinned down — usage
before finish, nothing after finish, raw-string tool arguments, and the two
sanctioned error paths (thrown vs `finish {kind:'error'}`).
## Event-sourced sessions (dsh-session)
@@ -206,6 +211,9 @@ forever:
req = waterfall agent/request ⟵ hooks, compaction, model switch
stream ctx.llm.stream(req) ⟵ waterfall llm/stream (raw chunks)
session('assistant/chunk'); emit agent/stream-chunk
if assembler.finish is error/aborted: throw ⟵ adapter's in-band error path →
step error (turn ends error/aborted,
not a normal completed message)
msg = waterfall agent/step-result ⟵ runs BEFORE the log append, so the
session('assistant/message', 'usage') log records what tool dispatch uses
each tool-call (sequential, abort-checked between calls):
@@ -225,9 +233,12 @@ forever:
Error containment: a throwing `agent/turn-continuation` listener or a
rejecting `session/flush` ends the **turn** with an `error` event — never the
driver loop. `abort()` is honored mid-stream **and** between tool calls;
disposal mid-turn ends the turn with reason `disposed` and emits
`agent/status('disposed')`.
driver loop. An adapter that ends its stream with a `finish {kind:'error'}`
or `{kind:'aborted'}` chunk (the in-band error path, for adapters that can't
throw mid-stream) is likewise translated into a step error, so the turn ends
`error`/`aborted` instead of logging a normal `completed` assistant message.
`abort()` is honored mid-stream **and** between tool calls; disposal mid-turn
ends the turn with reason `disposed` and emits `agent/status('disposed')`.
### Event taxonomy
@@ -293,7 +304,7 @@ implements it **without modifying the loop**:
| Scheduled tasks (cron) | plugin registers model-callable scheduling tools; timer fires → `send(…, {source: {kind: 'cron', …}})` when idle / `inject()` notification when busy |
| UI (GUI; CLI emits JSONL) | listen `agent/stream-chunk` + `session/event`; input → `send()` |
| Telemetry / replayable trace | `session/event` → JSONL; replay = `sessions.create(id, seed)` |
| DeepSeek V4 (and other) models | `LlmAdapter` subclass via `registerAdapter` |
| DeepSeek V4 (and other) models | `LlmAdapter` subclass via `registerAdapter`. **Implemented twice**: `dsh-llm-deepseek` (hand-rolled) and `dsh-llm-pi-ai` (pi-ai-backed) |
| Plugin hot-reload | every registration is a `ctx.effect` → vendored HMR just works |
## Extension cookbook
@@ -375,8 +386,6 @@ Tracked here deliberately — each is designed-for but not implemented:
- **Compaction implementation** (auto thresholds, summarization prompts) on
the `agent/request` seam, with its session-event types added by declaration
merging.
- **DeepSeek V4 adapter** — first real `LlmAdapter`; triggers the
streaming-protocol review (`TODO(review)` markers in dsh-llm).
- **Parallel tool execution** (concurrency-safety hints on ToolDefinition).
- **Session branching/tree** (pi-style entry tree) if needed beyond seed-based
forking.