Skip to content

refactor: raw sdk event driven chat#9

Merged
chryzxc merged 4 commits into
mainfrom
refactor/raw-sdk-event-driven-chat
Jul 17, 2026
Merged

refactor: raw sdk event driven chat#9
chryzxc merged 4 commits into
mainfrom
refactor/raw-sdk-event-driven-chat

Conversation

@chryzxc

@chryzxc chryzxc commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

This PR completes the refactoring of the chat system from custom message processing to a raw OpenCode SDK event-driven architecture. The changes establish proper parity between live streaming and session rehydration, improve message delivery reliability, and enhance performance.

Key Changes

Core Architecture Refactoring

  • Raw SDK Data Integration: Migrated from custom message transformation to direct SDK event processing
  • New Services: Introduced SdkMessageAdapter and SessionSnapshotLoader for proper SDK hydration
  • Event-Driven Flow: Replaced polling-based state management with live SDK event streaming

Message Delivery & Queue Management

  • Removed Extension-Side Queue: Eliminated custom queue status checks in favor of native OpenCode ordering
  • Inline Message Rendering: Normal composer sends now render immediately in the transcript
  • QueueManager Scope: Reserved for explicit legacy queue commands only

@ File References & Context Handling

  • SDK-Backed Mentions: Implemented proper @file mention resolution with SDK context attachment
  • Navigation Paths: File mentions now use correct workspace paths for navigation
  • Duplicate Prevention: Inline @ references no longer render redundant attachment chips

Activity Timeline & Tool Handling

  • Rehydrated Task Tools: Fixed task/question tool activity rendering from SDK parts
  • Question Completion: Improved completed question tool presentation across live/rehydrated boundaries
  • Output Capture: Ensured question tool answers survive session reload

Robustness & Error Handling

  • Defensive References: Added protection against malformed subagent summary references
  • State Reversion: Enhanced undo/revert UI with proper state cleanup
  • Hydration Safety: Improved resilience against legacy/malformed persisted data

Performance Improvements

  • Streaming Optimization: Enhanced terminal event handling and streaming performance
  • Markdown Rendering: Implemented debounced rendering during live streaming
  • Performance Probes: Added comprehensive streaming performance tracking

Testing Coverage

  • New Regression Tests: Added 6 comprehensive regression test files covering:
    • File mention SDK attachment paths
    • Hydrated file mention rendering
    • Inline queue delivery
    • Rehydrated task tool activity
    • Subagent references shape safety
    • Task tool message card ownership
  • Test Refactoring: Aligned existing tests with raw SDK architecture

Migration Impact

Breaking Changes

  • Extension-side queue management removed (replaced by SDK-native ordering)
  • Message hydration now requires proper SDK event structure

Compatibility

  • Maintains backward compatibility with existing session data
  • Defensive handling for legacy message formats

Testing

  • Comprehensive regression test coverage for critical delivery paths
  • Integration tests for SDK event handling
  • Performance validation for streaming scenarios

Summary by CodeRabbit

  • New Features

    • Chat history now reloads directly from the SDK, including tool activity, file changes, subagents, questions, and permissions.
    • Added more reliable file-mention attachments and inline pending messages during sends.
    • Added optional SDK event debugging and smoother streaming markdown rendering.
    • Improved activity timelines, terminal/glob previews, structured outputs, and plan display.
  • Bug Fixes

    • Improved stream completion, reasoning identity, undo targeting, and session isolation.
    • Preserved tool output and hydrated file-change summaries.
    • Prevented malformed subagent references from breaking chat hydration.
  • Performance

    • Reduced streaming UI stalls, unnecessary layout work, and transcript rendering overhead.

chryzxc added 4 commits July 15, 2026 14:19
- Updated TerminalBlockWithOutput to support glob patterns in command rendering.
- Improved ResponseMessageBody to handle streaming content more effectively.
- Added terminalEventMatchesAssistantTurn function to match terminal events with active assistant turns.
- Implemented a performance probe for streaming latency investigation.
- Introduced useStreamingDebouncedContent hook to optimize markdown rendering during live streaming.
- Enhanced appReducer and messageHandler to manage streaming state and events more efficiently.
- Updated MarkdownRenderer to support streaming-aware content debouncing.
@chryzxc
chryzxc merged commit 78f4f24 into main Jul 17, 2026
0 of 2 checks passed
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 364900ae-79f4-4d14-aed7-ef99c5ae2aa8

📥 Commits

Reviewing files that changed from the base of the PR and between 2da5a80 and fdf3b84.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (79)
  • package.json
  • src/providers/ChatViewProvider.ts
  • src/providers/chat/SessionHandler.ts
  • src/providers/chat/StreamEventHandler.ts
  • src/providers/chat/StructuredOutputProcessor.ts
  • src/services/OpencodeServerManager.ts
  • src/services/SdkMessageAdapter.ts
  • src/services/SessionService.ts
  • src/services/SessionSnapshotLoader.ts
  • src/services/SubagentTracker.ts
  • tests/components/QuotaMonitor.test.mjs
  • tests/components/model-dropdown.test.mjs
  • tests/components/plan-detection.test.mjs
  • tests/integration/code-selection-flow.test.mjs
  • tests/integration/conversation-pipeline.test.mjs
  • tests/providers/chat-send-deduplication-regression.test.mjs
  • tests/providers/chat-send-pipeline.test.mjs
  • tests/providers/session-revert-handler.test.mjs
  • tests/providers/stream-event-orchestration.test.mjs
  • tests/regression/diff-preview-block-grouping-regression.test.mjs
  • tests/regression/file-mention-sdk-attachment-regression.test.mjs
  • tests/regression/hydrated-file-mention-rendering-regression.test.mjs
  • tests/regression/implementation-plan-generic-response-lock-regression.test.mjs
  • tests/regression/inline-queue-delivery-regression.test.mjs
  • tests/regression/queue-visibility-drain-regression.test.mjs
  • tests/regression/rehydrated-task-tool-activity-regression.test.mjs
  • tests/regression/session-isolation-regression.test.mjs
  • tests/regression/store-message-canonicalization-regression.test.mjs
  • tests/regression/streaming-state-preservation-regression.test.mjs
  • tests/regression/subagent-references-shape-regression.test.mjs
  • tests/regression/subagent-restart-cancellation.test.mjs
  • tests/regression/task-tool-message-card-ownership-regression.test.mjs
  • tests/services/sdk-message-adapter.test.mjs
  • tests/services/session-crud.test.mjs
  • tests/unit/activity-timeline-collapse.test.mjs
  • tests/unit/centralized-debug-payload-filter-regression.test.mjs
  • tests/unit/centralized-filter-normalizer-paths.test.mjs
  • tests/webview/activity-timeline-attached-payloads-regression.test.mjs
  • tests/webview/activity-timeline-redundant-content-regression.test.mjs
  • tests/webview/background-task-modal-regression.test.mjs
  • tests/webview/centralized-debug-panel-performance.test.mjs
  • tests/webview/centralized-render-source-of-truth-regression.test.mjs
  • tests/webview/chat-history-performance-regression.test.mjs
  • tests/webview/code-selection-preview-modal.test.mjs
  • tests/webview/code-selection-rendering.test.mjs
  • tests/webview/file-changes-undo-message-id.test.mjs
  • tests/webview/file-chip-rendering.test.mjs
  • tests/webview/live-event-router-contract.test.mjs
  • tests/webview/live-progress-merge-regression.test.mjs
  • tests/webview/message-change-summary-card.test.mjs
  • tests/webview/message-handler-error-surfacing.test.mjs
  • tests/webview/mime-normalization.test.mjs
  • tests/webview/question-tool-activity-order-regression.test.mjs
  • tests/webview/rehydrated-activity-output-regression.test.mjs
  • tests/webview/render-contract-regression.test.mjs
  • tests/webview/revert-state-banner.test.mjs
  • tests/webview/revert-undo-restore-ui.test.mjs
  • tests/webview/session-status-rendering.test.mjs
  • tests/webview/stream-event-main-thread-performance.test.mjs
  • tests/webview/streaming-reasoning-identity-regression.test.mjs
  • tests/webview/subagent-raw-hydration-priority.test.mjs
  • webview/shared/src/chat/ChatShell.tsx
  • webview/shared/src/chat/MessageComponents.tsx
  • webview/shared/src/chat/PanelComponents.tsx
  • webview/shared/src/chat/SubagentDetailModal.tsx
  • webview/shared/src/chat/components/SessionModal.tsx
  • webview/shared/src/chat/lib/assistantBlockPresentation.test.ts
  • webview/shared/src/chat/lib/assistantBlockPresentation.ts
  • webview/shared/src/chat/lib/messageHandler.test.ts
  • webview/shared/src/chat/lib/messageHandler.ts
  • webview/shared/src/chat/lib/pendingUserMessages.ts
  • webview/shared/src/chat/lib/renderContract.ts
  • webview/shared/src/chat/lib/store.test.ts
  • webview/shared/src/chat/lib/store.ts
  • webview/shared/src/chat/lib/streamingPerfProbe.ts
  • webview/shared/src/chat/lib/subagents/stateManager.ts
  • webview/shared/src/chat/lib/types.ts
  • webview/shared/src/components/MarkdownRenderer.tsx
  • webview/shared/src/config.ts

📝 Walkthrough

Walkthrough

The chat system now hydrates history from OpenCode SDK snapshots, adapts SDK messages for rendering, removes centralized raw-event persistence, refines streaming identity and refresh handling, adds SDK event debugging and performance probes, and updates composer attachments, activity rendering, and regression coverage.

Changes

SDK-backed chat pipeline

Layer / File(s) Summary
SDK history and render adaptation
src/services/*, src/providers/chat/*, src/providers/ChatViewProvider.ts
Session messages are loaded from SDK snapshots and adapted into renderable messages, including tools, diffs, questions, permissions, subagents, structured output, and metadata.
Streaming and state ownership
src/providers/ChatViewProvider.ts, src/providers/chat/StreamEventHandler.ts, src/services/SessionService.ts
Raw SDK event persistence and centralized subagent persistence are removed; streaming payloads are cloned/truncated, terminal turns trigger SDK refreshes, and pending interactions are refreshed from SDK state.
Webview rendering and contracts
webview/shared/src/chat/*, webview/shared/src/components/MarkdownRenderer.tsx, webview/shared/src/config.ts
Hydrated SDK messages use typed render blocks, file-change rows, SDK debug data, stable assistant identities, streaming-aware markdown, and optional performance instrumentation.
Composer and activity behavior
webview/shared/src/chat/PanelComponents.tsx, webview/shared/src/chat/MessageComponents.tsx, webview/shared/src/chat/SubagentDetailModal.tsx
File mentions become SDK contexts, normal sends render pending messages, queue rendering is removed from that path, tool output and glob activity are displayed, and attachment chips use SDK paths.
Validation updates
tests/**
Tests cover SDK hydration, structured output, terminal identity, tool activity, render contracts, file mentions, queue behavior, debug routing, and malformed subagent references.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related PRs

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/raw-sdk-event-driven-chat

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.

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