feat: async + SSE message flow with interactive permission/question UI

Replace the synchronous /edit (wait for full markdown response) with an
async + SSE flow per demo.html so the user sees text stream in real-time
and can interact with permission/question events the agent raises.

Backend
-------
- EditHandler now calls OpenCode /session/:id/prompt_async and returns
  immediately with {ok, sessionId, notebookPath}; the LLM reply is no
  longer embedded in this response.
- New GlobalEventHandler proxies OpenCode /global/event as
  text/event-stream. Server forwards ALL events; the client filters.
  A too-eager server-side ?session= filter was silently dropping events
  the client would have accepted, so it was removed.
- New PermissionReplyHandler + QuestionReplyHandler forward user
  replies (once/always/reject and freeform answer) back to OpenCode
  Serve at /session/:sid/permissions/:permId and
  /session/:sid/question/:qId/reply.
- OpenCodeClient gains send_message_async(), stream_global_events()
  (async generator over the SSE feed via tornado streaming_callback),
  reply_permission() and reply_question(). Legacy send_message_sync
  removed.

Frontend
--------
- subscribeOpenCodeEvents() opens a fetch+reader SSE client (XSRF
  token injected from serverSettings); AbortController-backed close()
  is idempotent.
- OpenCodeInlinePrompt.applyEvent() routes events into the streaming
  UI: text delta -> assistant message (re-rendered as markdown on
  every delta so the user sees formatted <pre><code> blocks in
  real-time, not raw fence source); reasoning/tool/permission/question
  get collapsible details blocks. session.idle resets stream pointers
  and fires onStreamEnd.
- Permission/question blocks render real interactive UI: three
  buttons (once/always/reject) for permission, a text input + submit
  for question. Click handlers post through the new API routes and
  show success/failure status in place.
- Centralized idle detection (4 shapes: top-level + payload-nested
  x {session.idle, session.status/idle}) inside the prompt; cell
  action reacts via onStreamEnd callback rather than re-parsing
  event types.
- System / workspace / pty / lsp / mcp / installation events AND
  session-level control events (agent.switched, model.switched,
  file.edited) are not rendered in the frontend.

Tests
-----
- 52 backend pytest (was 43)
- 58 frontend jest (was 46)

design.md section 3 updated for the new async /edit response shape
and the new GET /events SSE endpoint.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
tao.chen
2026-07-27 17:00:16 +08:00
co-authored by Claude
parent 0bd04a825d
commit 4f8bbaf09f
12 changed files with 2532 additions and 243 deletions
+129
View File
@@ -210,3 +210,132 @@
opacity: 0.4;
cursor: default;
}
/* ------------------------------------------------------------------
Streaming event blocks (v4+ — see opencode_inline_prompt.applyEvent)
------------------------------------------------------------------ */
/* System / status line rendered for server.*, workspace.*, lsp.*, etc. */
.opencode-inline-prompt .opencode-msg-system {
font-size: var(--jp-ui-font-size0, 11px);
color: var(--jp-ui-font-color2, #888);
padding: 2px 4px;
font-family: var(--jp-code-font-family, monospace);
white-space: pre-wrap;
word-break: break-all;
}
/* Base collapsible block used for reasoning / tool / permission / question. */
.opencode-inline-prompt .opencode-msg-block {
margin: 4px 0;
border: 1px solid var(--jp-border-color2, #ddd);
border-radius: 3px;
background: var(--jp-layout-color1, #fafafa);
font-size: var(--jp-ui-font-size0, 11px);
overflow: hidden;
}
.opencode-inline-prompt .opencode-msg-block > summary {
padding: 2px 6px;
font-weight: 600;
cursor: pointer;
background: var(--jp-layout-color2, #f1f5f9);
color: var(--jp-ui-font-color2, #64748b);
user-select: none;
font-size: var(--jp-ui-font-size0, 11px);
}
.opencode-inline-prompt .opencode-msg-block-content {
padding: 4px 8px;
font-family: var(--jp-code-font-family, monospace);
white-space: pre-wrap;
word-break: break-all;
color: var(--jp-ui-font-color1, #334155);
max-height: 240px;
overflow-y: auto;
}
/* Reasoning: green tint (matches demo.html). */
.opencode-inline-prompt .opencode-msg-block-reasoning > summary {
background: #f0fdf4;
color: #15803d;
}
/* Tool call: yellow tint. */
.opencode-inline-prompt .opencode-msg-block-tool > summary {
background: #fef3c7;
color: #b45309;
}
/* Permission / question: pink tint. */
.opencode-inline-prompt .opencode-msg-block-interaction > summary {
background: #fce7f3;
color: #be185d;
}
/* Permission block: description + 3 buttons. */
.opencode-inline-prompt .opencode-perm-desc {
margin-bottom: 6px;
font-size: var(--jp-ui-font-size0, 11px);
color: var(--jp-ui-font-color1, #334155);
}
.opencode-inline-prompt .opencode-perm-buttons {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 4px;
}
.opencode-inline-prompt .opencode-perm-btn-allow,
.opencode-inline-prompt .opencode-perm-btn-reject {
border: none;
padding: 4px 10px;
border-radius: 3px;
cursor: pointer;
font-size: var(--jp-ui-font-size0, 11px);
color: white;
font-weight: 500;
}
.opencode-inline-prompt .opencode-perm-btn-allow {
background: #10b981;
}
.opencode-inline-prompt .opencode-perm-btn-allow:hover {
background: #059669;
}
.opencode-inline-prompt .opencode-perm-btn-reject {
background: #ef4444;
}
.opencode-inline-prompt .opencode-perm-btn-reject:hover {
background: #dc2626;
}
/* Question block: text + input + submit. */
.opencode-inline-prompt .opencode-q-desc {
margin-bottom: 6px;
font-size: var(--jp-ui-font-size0, 11px);
color: var(--jp-ui-font-color1, #334155);
}
.opencode-inline-prompt .opencode-q-input-row {
display: flex;
gap: 6px;
margin-top: 4px;
}
.opencode-inline-prompt .opencode-q-input {
flex: 1;
padding: 3px 6px;
border: 1px solid var(--jp-border-color2, #ccc);
border-radius: 3px;
font-size: var(--jp-ui-font-size0, 11px);
font-family: inherit;
background: var(--jp-layout-color1, #fff);
}
.opencode-inline-prompt .opencode-q-submit {
border: 1px solid var(--jp-border-color2, #ccc);
background: var(--jp-layout-color2, #f0f0f0);
padding: 3px 10px;
border-radius: 3px;
cursor: pointer;
font-size: var(--jp-ui-font-size0, 11px);
}
.opencode-inline-prompt .opencode-q-submit:hover {
background: var(--jp-layout-color3, #e0e0e0);
}