Skip to content

feat(cli): stream incremental reasoning/text deltas in run --format json#38504

Open
mustafa3rsan wants to merge 1 commit into
anomalyco:devfrom
mustafa3rsan:stream-reasoning-deltas-json
Open

feat(cli): stream incremental reasoning/text deltas in run --format json#38504
mustafa3rsan wants to merge 1 commit into
anomalyco:devfrom
mustafa3rsan:stream-reasoning-deltas-json

Conversation

@mustafa3rsan

@mustafa3rsan mustafa3rsan commented Jul 23, 2026

Copy link
Copy Markdown

Issue for this PR

N/A — no existing issue.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

opencode run --format json --thinking never streams reasoning. During a long thinking phase the JSON stream is silent, then the whole reasoning block appears at once when it ends.

The loop in run.ts only handles message.part.updated, and gates reasoning/text on part.time?.end, which is only set once the part finishes (SessionProcessor.finishReasoning). The incremental tokens are published as message.part.delta (session.updatePartDelta), but run never listens for them, so they're dropped.

This adds one branch that emits message.part.delta as part_delta events when --format json. A partID -> part.type map (filled from message.part.updated) tags each delta so consumers know whether it's reasoning or text:

{"type":"part_delta","partID":"prt_...","field":"text","delta":"...","partType":"reasoning"}

emit() is a no-op unless --format json, so the formatted/TTY output is untouched (it still waits for the whole part on purpose). It's a new event type, so existing JSON consumers ignore it; the final reasoning/text events are still emitted at time.end.

How did you verify your code works?

Ran the patched build from source against a real model:

bun dev run --thinking --variant high --format json --model anthropic/claude-opus-4-8 "..."

The JSON stream now includes part_delta events (it didn't before): 4 part_delta / 2 reasoning / 2 text / 1 step_start / 1 step_finish. Reasoning tokens arrive incrementally instead of only at block end, e.g.:

{"type":"part_delta","partID":"prt_f8f0...","field":"text","delta":"I","partType":"reasoning"}
{"type":"part_delta","partID":"prt_f8f0...","field":"text","delta":"'m calculating that 17 times 23...","partType":"reasoning"}

bun run typecheck (tsgo --noEmit) passes and oxlint reports 0 errors on the file.

Screenshots / recordings

N/A — not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Jul 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant