2026-02-08 18:02:25 -08:00
|
|
|
import type { UIMessage } from "ai";
|
2026-02-21 12:32:37 -08:00
|
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
|
|
|
import { join } from "node:path";
|
Dench workspace: Tiptap markdown editor, subagent sessions, and error surfacing
── Tiptap Markdown Editor ──
- Add full Tiptap-based WYSIWYG markdown editor (markdown-editor.tsx, 709 LOC)
with bubble menu, auto-save (debounced), image drag-and-drop/paste upload,
table editing, task list checkboxes, and frontmatter preservation on save.
- Add slash command system (slash-command.tsx, 607 LOC) with "/" trigger for
block insertion (headings, lists, tables, code blocks, images, reports) and
"@" trigger for file/document mention with fuzzy search across the workspace
tree.
- Add ReportBlockNode (report-block-node.tsx) — custom Tiptap node that renders
embedded report-json blocks as interactive ReportCard widgets inline in the
editor, with expand/collapse and edit-JSON support.
- Add workspace asset serving API (api/workspace/assets/[...path]/route.ts) to
serve images from the workspace with proper MIME types.
- Add workspace file upload orkspace/upload/route.ts) for multipart
image uploads (10 MB limit, image types only), saving to assets/ directory.
- Add ~500 lines of Tiptap editor CSS to globals.css (editor layout, task lists,
images, tables, slash command dropdown, bubble menu toolbar, code blocks, etc.).
- Add 14 @tiptap/* dependencies to apps/web/package.json (react, starter-kit,
markdown, image, link, table, task-list, suggestion, placeholder, etc.).
── Document View: Edit/Read Mode Toggle ──
- document-view.tsx: Add edit/read mode toggle; defaults to edit mode when a
filePath is available. Lazy-loads MarkdownEditor to keep initial bundle light.
- workspace/page.tsx: Pass activePath, tree, onSave, onNavigate, and
onRefreshTree through to DocumentView for full editor integration with
workspace navigation and tree refresh after saves.
── Subagent Session Isolation ──
- agent-runner.ts: Add RunAgentOptions with optional sessionId; when set, spawns
the agent with --session-key agent:main:subagent:<id> ant so
file-scoped sidebar chats run in isolated sessions independent of the main
agent.
- route.ts (chat API): Accept sessionId from request body and forward it to
runAgent. Resolve workspace file path prefixes (resolveAgentWorkspacePrefix)
so tree-relative paths become agent-cwd-relative.
- chat-panel.tsx: Create per-instance DefaultChatTransport that injects sessionId
via body function and a ref (avoids stale closures). On file change, auto-load
the most recent session and its messages. Refresh session tab list after
streaming ends. Stop ongoing stream when switching sessions.
- register.agent.ts: Add --session-key <key> and --lane <lane> CLI flags.
- agent-via-gateway.ts: Wire sessionKey into session resolution and validation
for both interactive and --stream-json code paths.
- workspace.ts: Add resolveAgentWorkspacePrefix() to map workspace-root-relative
paths to repo-root-relative paths for the agent process.
── Error Surfacing ──
- agent-runner.ts: Add onAgentError callback extraction helpers
(parseAgentErrorMessage, parseErrorBody, parseErrorFromStderr) to surface
API-level errors (402 payment, rate limits, etc.) to the UI. Captures stderr
for fallback error detection on non-zero exit.
- route.ts: Wire onAgentError into the SSE stream as [error]-prefixed text
parts. Improve onError and onClose handlers with clearer error messages and
exit code reporting.
- chat-message.tsx: Detect [error]-prefixed text segments and render them as
styled error banners with alert icon instead of plain text.
- chat-panel.tsx: Restyle the transport-level error bar with themed colors and
an alert icon consistent with in-message error styling.
2026-02-11 20:54:30 -08:00
|
|
|
import { resolveAgentWorkspacePrefix } from "@/lib/workspace";
|
2026-02-12 13:37:40 -08:00
|
|
|
import {
|
|
|
|
|
startRun,
|
|
|
|
|
hasActiveRun,
|
|
|
|
|
subscribeToRun,
|
|
|
|
|
persistUserMessage,
|
2026-02-21 12:32:37 -08:00
|
|
|
type SseEvent as ParentSseEvent,
|
2026-02-12 13:37:40 -08:00
|
|
|
} from "@/lib/active-runs";
|
2026-02-21 12:32:37 -08:00
|
|
|
import {
|
|
|
|
|
hasActiveSubagent,
|
|
|
|
|
isSubagentRunning,
|
|
|
|
|
ensureRegisteredFromDisk,
|
|
|
|
|
subscribeToSubagent,
|
|
|
|
|
persistUserMessage as persistSubagentUserMessage,
|
|
|
|
|
reactivateSubagent,
|
|
|
|
|
spawnSubagentMessage,
|
|
|
|
|
type SseEvent as SubagentSseEvent,
|
|
|
|
|
} from "@/lib/subagent-runs";
|
|
|
|
|
import { resolveOpenClawStateDir } from "@/lib/workspace";
|
2026-02-08 18:02:25 -08:00
|
|
|
|
|
|
|
|
// Force Node.js runtime (required for child_process)
|
|
|
|
|
export const runtime = "nodejs";
|
2026-02-06 15:28:35 -08:00
|
|
|
|
|
|
|
|
// Allow streaming responses up to 10 minutes
|
|
|
|
|
export const maxDuration = 600;
|
|
|
|
|
|
2026-02-21 12:32:37 -08:00
|
|
|
function deriveSubagentParentSessionId(sessionKey: string): string {
|
|
|
|
|
const registryPath = join(resolveOpenClawStateDir(), "subagents", "runs.json");
|
|
|
|
|
if (!existsSync(registryPath)) {return "";}
|
|
|
|
|
try {
|
|
|
|
|
const raw = JSON.parse(readFileSync(registryPath, "utf-8")) as {
|
|
|
|
|
runs?: Record<string, Record<string, unknown>>;
|
|
|
|
|
};
|
|
|
|
|
for (const entry of Object.values(raw.runs ?? {})) {
|
|
|
|
|
if (entry.childSessionKey !== sessionKey) {continue;}
|
|
|
|
|
const requester = typeof entry.requesterSessionKey === "string" ? entry.requesterSessionKey : "";
|
|
|
|
|
const match = requester.match(/^agent:[^:]+:web:(.+)$/);
|
|
|
|
|
return match?.[1] ?? "";
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ensureSubagentRegistered(sessionKey: string): boolean {
|
|
|
|
|
if (hasActiveSubagent(sessionKey)) {return true;}
|
|
|
|
|
const parentWebSessionId = deriveSubagentParentSessionId(sessionKey);
|
|
|
|
|
return ensureRegisteredFromDisk(sessionKey, parentWebSessionId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 15:28:35 -08:00
|
|
|
export async function POST(req: Request) {
|
2026-02-12 13:37:40 -08:00
|
|
|
const {
|
|
|
|
|
messages,
|
|
|
|
|
sessionId,
|
2026-02-21 12:32:37 -08:00
|
|
|
sessionKey,
|
|
|
|
|
}: { messages: UIMessage[]; sessionId?: string; sessionKey?: string } = await req.json();
|
2026-02-08 21:59:08 -08:00
|
|
|
|
|
|
|
|
// Extract the latest user message text
|
|
|
|
|
const lastUserMessage = messages.filter((m) => m.role === "user").pop();
|
|
|
|
|
const userText =
|
|
|
|
|
lastUserMessage?.parts
|
|
|
|
|
?.filter(
|
2026-02-12 13:37:40 -08:00
|
|
|
(p): p is { type: "text"; text: string } =>
|
|
|
|
|
p.type === "text",
|
2026-02-08 21:59:08 -08:00
|
|
|
)
|
|
|
|
|
.map((p) => p.text)
|
|
|
|
|
.join("\n") ?? "";
|
|
|
|
|
|
|
|
|
|
if (!userText.trim()) {
|
|
|
|
|
return new Response("No message provided", { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 12:32:37 -08:00
|
|
|
const isSubagentSession = typeof sessionKey === "string" && sessionKey.includes(":subagent:");
|
|
|
|
|
|
2026-02-12 13:37:40 -08:00
|
|
|
// Reject if a run is already active for this session.
|
2026-02-21 12:32:37 -08:00
|
|
|
if (!isSubagentSession && sessionId && hasActiveRun(sessionId)) {
|
2026-02-12 13:37:40 -08:00
|
|
|
return new Response("Active run in progress", { status: 409 });
|
|
|
|
|
}
|
2026-02-21 12:32:37 -08:00
|
|
|
if (isSubagentSession && isSubagentRunning(sessionKey)) {
|
|
|
|
|
return new Response("Active subagent run in progress", { status: 409 });
|
|
|
|
|
}
|
2026-02-12 13:37:40 -08:00
|
|
|
|
Dench workspace: Tiptap markdown editor, subagent sessions, and error surfacing
── Tiptap Markdown Editor ──
- Add full Tiptap-based WYSIWYG markdown editor (markdown-editor.tsx, 709 LOC)
with bubble menu, auto-save (debounced), image drag-and-drop/paste upload,
table editing, task list checkboxes, and frontmatter preservation on save.
- Add slash command system (slash-command.tsx, 607 LOC) with "/" trigger for
block insertion (headings, lists, tables, code blocks, images, reports) and
"@" trigger for file/document mention with fuzzy search across the workspace
tree.
- Add ReportBlockNode (report-block-node.tsx) — custom Tiptap node that renders
embedded report-json blocks as interactive ReportCard widgets inline in the
editor, with expand/collapse and edit-JSON support.
- Add workspace asset serving API (api/workspace/assets/[...path]/route.ts) to
serve images from the workspace with proper MIME types.
- Add workspace file upload orkspace/upload/route.ts) for multipart
image uploads (10 MB limit, image types only), saving to assets/ directory.
- Add ~500 lines of Tiptap editor CSS to globals.css (editor layout, task lists,
images, tables, slash command dropdown, bubble menu toolbar, code blocks, etc.).
- Add 14 @tiptap/* dependencies to apps/web/package.json (react, starter-kit,
markdown, image, link, table, task-list, suggestion, placeholder, etc.).
── Document View: Edit/Read Mode Toggle ──
- document-view.tsx: Add edit/read mode toggle; defaults to edit mode when a
filePath is available. Lazy-loads MarkdownEditor to keep initial bundle light.
- workspace/page.tsx: Pass activePath, tree, onSave, onNavigate, and
onRefreshTree through to DocumentView for full editor integration with
workspace navigation and tree refresh after saves.
── Subagent Session Isolation ──
- agent-runner.ts: Add RunAgentOptions with optional sessionId; when set, spawns
the agent with --session-key agent:main:subagent:<id> ant so
file-scoped sidebar chats run in isolated sessions independent of the main
agent.
- route.ts (chat API): Accept sessionId from request body and forward it to
runAgent. Resolve workspace file path prefixes (resolveAgentWorkspacePrefix)
so tree-relative paths become agent-cwd-relative.
- chat-panel.tsx: Create per-instance DefaultChatTransport that injects sessionId
via body function and a ref (avoids stale closures). On file change, auto-load
the most recent session and its messages. Refresh session tab list after
streaming ends. Stop ongoing stream when switching sessions.
- register.agent.ts: Add --session-key <key> and --lane <lane> CLI flags.
- agent-via-gateway.ts: Wire sessionKey into session resolution and validation
for both interactive and --stream-json code paths.
- workspace.ts: Add resolveAgentWorkspacePrefix() to map workspace-root-relative
paths to repo-root-relative paths for the agent process.
── Error Surfacing ──
- agent-runner.ts: Add onAgentError callback extraction helpers
(parseAgentErrorMessage, parseErrorBody, parseErrorFromStderr) to surface
API-level errors (402 payment, rate limits, etc.) to the UI. Captures stderr
for fallback error detection on non-zero exit.
- route.ts: Wire onAgentError into the SSE stream as [error]-prefixed text
parts. Improve onError and onClose handlers with clearer error messages and
exit code reporting.
- chat-message.tsx: Detect [error]-prefixed text segments and render them as
styled error banners with alert icon instead of plain text.
- chat-panel.tsx: Restyle the transport-level error bar with themed colors and
an alert icon consistent with in-message error styling.
2026-02-11 20:54:30 -08:00
|
|
|
// Resolve workspace file paths to be agent-cwd-relative.
|
|
|
|
|
let agentMessage = userText;
|
|
|
|
|
const wsPrefix = resolveAgentWorkspacePrefix();
|
|
|
|
|
if (wsPrefix) {
|
|
|
|
|
agentMessage = userText.replace(
|
|
|
|
|
/\[Context: workspace file '([^']+)'\]/,
|
|
|
|
|
`[Context: workspace file '${wsPrefix}/$1']`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 13:37:40 -08:00
|
|
|
// Persist the user message server-side so it survives a page reload
|
|
|
|
|
// even if the client never gets a chance to save.
|
2026-02-21 12:32:37 -08:00
|
|
|
if (isSubagentSession && sessionKey && lastUserMessage) {
|
|
|
|
|
if (!ensureSubagentRegistered(sessionKey)) {
|
|
|
|
|
return new Response("Subagent not found", { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
persistSubagentUserMessage(sessionKey, {
|
|
|
|
|
id: lastUserMessage.id,
|
|
|
|
|
text: userText,
|
|
|
|
|
});
|
|
|
|
|
} else if (sessionId && lastUserMessage) {
|
2026-02-12 13:37:40 -08:00
|
|
|
persistUserMessage(sessionId, {
|
|
|
|
|
id: lastUserMessage.id,
|
|
|
|
|
content: userText,
|
|
|
|
|
parts: lastUserMessage.parts as unknown[],
|
|
|
|
|
});
|
|
|
|
|
}
|
Dench workspace: Tiptap markdown editor, subagent sessions, and error surfacing
── Tiptap Markdown Editor ──
- Add full Tiptap-based WYSIWYG markdown editor (markdown-editor.tsx, 709 LOC)
with bubble menu, auto-save (debounced), image drag-and-drop/paste upload,
table editing, task list checkboxes, and frontmatter preservation on save.
- Add slash command system (slash-command.tsx, 607 LOC) with "/" trigger for
block insertion (headings, lists, tables, code blocks, images, reports) and
"@" trigger for file/document mention with fuzzy search across the workspace
tree.
- Add ReportBlockNode (report-block-node.tsx) — custom Tiptap node that renders
embedded report-json blocks as interactive ReportCard widgets inline in the
editor, with expand/collapse and edit-JSON support.
- Add workspace asset serving API (api/workspace/assets/[...path]/route.ts) to
serve images from the workspace with proper MIME types.
- Add workspace file upload orkspace/upload/route.ts) for multipart
image uploads (10 MB limit, image types only), saving to assets/ directory.
- Add ~500 lines of Tiptap editor CSS to globals.css (editor layout, task lists,
images, tables, slash command dropdown, bubble menu toolbar, code blocks, etc.).
- Add 14 @tiptap/* dependencies to apps/web/package.json (react, starter-kit,
markdown, image, link, table, task-list, suggestion, placeholder, etc.).
── Document View: Edit/Read Mode Toggle ──
- document-view.tsx: Add edit/read mode toggle; defaults to edit mode when a
filePath is available. Lazy-loads MarkdownEditor to keep initial bundle light.
- workspace/page.tsx: Pass activePath, tree, onSave, onNavigate, and
onRefreshTree through to DocumentView for full editor integration with
workspace navigation and tree refresh after saves.
── Subagent Session Isolation ──
- agent-runner.ts: Add RunAgentOptions with optional sessionId; when set, spawns
the agent with --session-key agent:main:subagent:<id> ant so
file-scoped sidebar chats run in isolated sessions independent of the main
agent.
- route.ts (chat API): Accept sessionId from request body and forward it to
runAgent. Resolve workspace file path prefixes (resolveAgentWorkspacePrefix)
so tree-relative paths become agent-cwd-relative.
- chat-panel.tsx: Create per-instance DefaultChatTransport that injects sessionId
via body function and a ref (avoids stale closures). On file change, auto-load
the most recent session and its messages. Refresh session tab list after
streaming ends. Stop ongoing stream when switching sessions.
- register.agent.ts: Add --session-key <key> and --lane <lane> CLI flags.
- agent-via-gateway.ts: Wire sessionKey into session resolution and validation
for both interactive and --stream-json code paths.
- workspace.ts: Add resolveAgentWorkspacePrefix() to map workspace-root-relative
paths to repo-root-relative paths for the agent process.
── Error Surfacing ──
- agent-runner.ts: Add onAgentError callback extraction helpers
(parseAgentErrorMessage, parseErrorBody, parseErrorFromStderr) to surface
API-level errors (402 payment, rate limits, etc.) to the UI. Captures stderr
for fallback error detection on non-zero exit.
- route.ts: Wire onAgentError into the SSE stream as [error]-prefixed text
parts. Improve onError and onClose handlers with clearer error messages and
exit code reporting.
- chat-message.tsx: Detect [error]-prefixed text segments and render them as
styled error banners with alert icon instead of plain text.
- chat-panel.tsx: Restyle the transport-level error bar with themed colors and
an alert icon consistent with in-message error styling.
2026-02-11 20:54:30 -08:00
|
|
|
|
2026-02-12 13:37:40 -08:00
|
|
|
// Start the agent run (decoupled from this HTTP connection).
|
|
|
|
|
// The child process will keep running even if this response is cancelled.
|
2026-02-21 12:32:37 -08:00
|
|
|
if (isSubagentSession && sessionKey) {
|
|
|
|
|
if (!reactivateSubagent(sessionKey)) {
|
|
|
|
|
return new Response("Subagent not found", { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
if (!spawnSubagentMessage(sessionKey, agentMessage)) {
|
|
|
|
|
return new Response("Failed to start subagent run", { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
} else if (sessionId) {
|
2026-02-12 13:37:40 -08:00
|
|
|
try {
|
|
|
|
|
startRun({
|
|
|
|
|
sessionId,
|
|
|
|
|
message: agentMessage,
|
|
|
|
|
agentSessionId: sessionId,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return new Response(
|
|
|
|
|
err instanceof Error ? err.message : String(err),
|
|
|
|
|
{ status: 500 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
Dench workspace: Tiptap markdown editor, subagent sessions, and error surfacing
── Tiptap Markdown Editor ──
- Add full Tiptap-based WYSIWYG markdown editor (markdown-editor.tsx, 709 LOC)
with bubble menu, auto-save (debounced), image drag-and-drop/paste upload,
table editing, task list checkboxes, and frontmatter preservation on save.
- Add slash command system (slash-command.tsx, 607 LOC) with "/" trigger for
block insertion (headings, lists, tables, code blocks, images, reports) and
"@" trigger for file/document mention with fuzzy search across the workspace
tree.
- Add ReportBlockNode (report-block-node.tsx) — custom Tiptap node that renders
embedded report-json blocks as interactive ReportCard widgets inline in the
editor, with expand/collapse and edit-JSON support.
- Add workspace asset serving API (api/workspace/assets/[...path]/route.ts) to
serve images from the workspace with proper MIME types.
- Add workspace file upload orkspace/upload/route.ts) for multipart
image uploads (10 MB limit, image types only), saving to assets/ directory.
- Add ~500 lines of Tiptap editor CSS to globals.css (editor layout, task lists,
images, tables, slash command dropdown, bubble menu toolbar, code blocks, etc.).
- Add 14 @tiptap/* dependencies to apps/web/package.json (react, starter-kit,
markdown, image, link, table, task-list, suggestion, placeholder, etc.).
── Document View: Edit/Read Mode Toggle ──
- document-view.tsx: Add edit/read mode toggle; defaults to edit mode when a
filePath is available. Lazy-loads MarkdownEditor to keep initial bundle light.
- workspace/page.tsx: Pass activePath, tree, onSave, onNavigate, and
onRefreshTree through to DocumentView for full editor integration with
workspace navigation and tree refresh after saves.
── Subagent Session Isolation ──
- agent-runner.ts: Add RunAgentOptions with optional sessionId; when set, spawns
the agent with --session-key agent:main:subagent:<id> ant so
file-scoped sidebar chats run in isolated sessions independent of the main
agent.
- route.ts (chat API): Accept sessionId from request body and forward it to
runAgent. Resolve workspace file path prefixes (resolveAgentWorkspacePrefix)
so tree-relative paths become agent-cwd-relative.
- chat-panel.tsx: Create per-instance DefaultChatTransport that injects sessionId
via body function and a ref (avoids stale closures). On file change, auto-load
the most recent session and its messages. Refresh session tab list after
streaming ends. Stop ongoing stream when switching sessions.
- register.agent.ts: Add --session-key <key> and --lane <lane> CLI flags.
- agent-via-gateway.ts: Wire sessionKey into session resolution and validation
for both interactive and --stream-json code paths.
- workspace.ts: Add resolveAgentWorkspacePrefix() to map workspace-root-relative
paths to repo-root-relative paths for the agent process.
── Error Surfacing ──
- agent-runner.ts: Add onAgentError callback extraction helpers
(parseAgentErrorMessage, parseErrorBody, parseErrorFromStderr) to surface
API-level errors (402 payment, rate limits, etc.) to the UI. Captures stderr
for fallback error detection on non-zero exit.
- route.ts: Wire onAgentError into the SSE stream as [error]-prefixed text
parts. Improve onError and onClose handlers with clearer error messages and
exit code reporting.
- chat-message.tsx: Detect [error]-prefixed text segments and render them as
styled error banners with alert icon instead of plain text.
- chat-panel.tsx: Restyle the transport-level error bar with themed colors and
an alert icon consistent with in-message error styling.
2026-02-11 20:54:30 -08:00
|
|
|
|
2026-02-12 13:37:40 -08:00
|
|
|
// Stream SSE events to the client using the AI SDK v6 wire format.
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
let closed = false;
|
|
|
|
|
let unsubscribe: (() => void) | null = null;
|
Dench workspace: Tiptap markdown editor, subagent sessions, and error surfacing
── Tiptap Markdown Editor ──
- Add full Tiptap-based WYSIWYG markdown editor (markdown-editor.tsx, 709 LOC)
with bubble menu, auto-save (debounced), image drag-and-drop/paste upload,
table editing, task list checkboxes, and frontmatter preservation on save.
- Add slash command system (slash-command.tsx, 607 LOC) with "/" trigger for
block insertion (headings, lists, tables, code blocks, images, reports) and
"@" trigger for file/document mention with fuzzy search across the workspace
tree.
- Add ReportBlockNode (report-block-node.tsx) — custom Tiptap node that renders
embedded report-json blocks as interactive ReportCard widgets inline in the
editor, with expand/collapse and edit-JSON support.
- Add workspace asset serving API (api/workspace/assets/[...path]/route.ts) to
serve images from the workspace with proper MIME types.
- Add workspace file upload orkspace/upload/route.ts) for multipart
image uploads (10 MB limit, image types only), saving to assets/ directory.
- Add ~500 lines of Tiptap editor CSS to globals.css (editor layout, task lists,
images, tables, slash command dropdown, bubble menu toolbar, code blocks, etc.).
- Add 14 @tiptap/* dependencies to apps/web/package.json (react, starter-kit,
markdown, image, link, table, task-list, suggestion, placeholder, etc.).
── Document View: Edit/Read Mode Toggle ──
- document-view.tsx: Add edit/read mode toggle; defaults to edit mode when a
filePath is available. Lazy-loads MarkdownEditor to keep initial bundle light.
- workspace/page.tsx: Pass activePath, tree, onSave, onNavigate, and
onRefreshTree through to DocumentView for full editor integration with
workspace navigation and tree refresh after saves.
── Subagent Session Isolation ──
- agent-runner.ts: Add RunAgentOptions with optional sessionId; when set, spawns
the agent with --session-key agent:main:subagent:<id> ant so
file-scoped sidebar chats run in isolated sessions independent of the main
agent.
- route.ts (chat API): Accept sessionId from request body and forward it to
runAgent. Resolve workspace file path prefixes (resolveAgentWorkspacePrefix)
so tree-relative paths become agent-cwd-relative.
- chat-panel.tsx: Create per-instance DefaultChatTransport that injects sessionId
via body function and a ref (avoids stale closures). On file change, auto-load
the most recent session and its messages. Refresh session tab list after
streaming ends. Stop ongoing stream when switching sessions.
- register.agent.ts: Add --session-key <key> and --lane <lane> CLI flags.
- agent-via-gateway.ts: Wire sessionKey into session resolution and validation
for both interactive and --stream-json code paths.
- workspace.ts: Add resolveAgentWorkspacePrefix() to map workspace-root-relative
paths to repo-root-relative paths for the agent process.
── Error Surfacing ──
- agent-runner.ts: Add onAgentError callback extraction helpers
(parseAgentErrorMessage, parseErrorBody, parseErrorFromStderr) to surface
API-level errors (402 payment, rate limits, etc.) to the UI. Captures stderr
for fallback error detection on non-zero exit.
- route.ts: Wire onAgentError into the SSE stream as [error]-prefixed text
parts. Improve onError and onClose handlers with clearer error messages and
exit code reporting.
- chat-message.tsx: Detect [error]-prefixed text segments and render them as
styled error banners with alert icon instead of plain text.
- chat-panel.tsx: Restyle the transport-level error bar with themed colors and
an alert icon consistent with in-message error styling.
2026-02-11 20:54:30 -08:00
|
|
|
|
2026-02-12 13:37:40 -08:00
|
|
|
const stream = new ReadableStream({
|
|
|
|
|
start(controller) {
|
2026-02-21 12:32:37 -08:00
|
|
|
if (!sessionId && !sessionKey) {
|
2026-02-12 13:37:40 -08:00
|
|
|
// No session — shouldn't happen but close gracefully.
|
|
|
|
|
controller.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-08 21:59:08 -08:00
|
|
|
|
2026-02-21 12:32:37 -08:00
|
|
|
unsubscribe = isSubagentSession && sessionKey
|
|
|
|
|
? subscribeToSubagent(
|
|
|
|
|
sessionKey,
|
|
|
|
|
(event: SubagentSseEvent | null) => {
|
|
|
|
|
if (closed) {return;}
|
|
|
|
|
if (event === null) {
|
|
|
|
|
closed = true;
|
|
|
|
|
try {
|
|
|
|
|
controller.close();
|
|
|
|
|
} catch {
|
|
|
|
|
/* already closed */
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const json = JSON.stringify(event);
|
|
|
|
|
controller.enqueue(encoder.encode(`data: ${json}\n\n`));
|
|
|
|
|
} catch {
|
|
|
|
|
/* ignore enqueue errors on closed stream */
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ replay: false },
|
|
|
|
|
)
|
|
|
|
|
: subscribeToRun(
|
|
|
|
|
sessionId as string,
|
|
|
|
|
(event: ParentSseEvent | null) => {
|
2026-02-12 13:37:40 -08:00
|
|
|
if (closed) {return;}
|
|
|
|
|
if (event === null) {
|
|
|
|
|
// Run completed — close the SSE stream.
|
|
|
|
|
closed = true;
|
|
|
|
|
try {
|
|
|
|
|
controller.close();
|
|
|
|
|
} catch {
|
|
|
|
|
/* already closed */
|
2026-02-08 21:59:08 -08:00
|
|
|
}
|
2026-02-12 13:37:40 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const json = JSON.stringify(event);
|
|
|
|
|
controller.enqueue(
|
|
|
|
|
encoder.encode(`data: ${json}\n\n`),
|
|
|
|
|
);
|
|
|
|
|
} catch {
|
|
|
|
|
/* ignore enqueue errors on closed stream */
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// Don't replay — we just created the run, the buffer is empty.
|
|
|
|
|
{ replay: false },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!unsubscribe) {
|
|
|
|
|
// Race: run was cleaned up between startRun and subscribe.
|
|
|
|
|
closed = true;
|
|
|
|
|
controller.close();
|
2026-02-08 21:59:08 -08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
cancel() {
|
2026-02-12 13:37:40 -08:00
|
|
|
// Client disconnected — unsubscribe but keep the run alive.
|
|
|
|
|
// The ActiveRunManager continues buffering + persisting in the background.
|
2026-02-08 21:59:08 -08:00
|
|
|
closed = true;
|
2026-02-12 13:37:40 -08:00
|
|
|
unsubscribe?.();
|
2026-02-08 21:59:08 -08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new Response(stream, {
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "text/event-stream",
|
|
|
|
|
"Cache-Control": "no-cache, no-transform",
|
|
|
|
|
Connection: "keep-alive",
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-02-06 15:28:35 -08:00
|
|
|
}
|