openpencil/apps/web/server/api/mcp/events.get.ts
Kayshen-X a7d73ebb62 feat(ai): pencil-style agentic design tool-loop, multi-chat tabs, #27 panel restyle
Built-in design generation now runs as an agentic MCP tool-loop (reusing the
agent-rs BuiltInProvider), gated behind OPENPENCIL_DESIGN_AGENT_LOOP / the
Settings experimental toggle; the orchestrator stays the default.

- design-agent system prompt + in-process design toolset (parity-locked with
  the MCP surface) + flag-gated Intent::Design routing
- spawn_agents execution as sequential sub-loops + live creation-mode badges
  (per-agent glow + 'N/M designing...' header)
- new MCP tools: get_guidelines, ToolSearch, get_screenshot, get_editor_state,
  export_nodes, spawn_agents; style-guide local audit
- #27 AI panel restyle: rounded tool cards + green check-rings, gray user
  bubbles, model-pill bottom toolbar, header, empty-state pills, the
  PARALLEL AGENTS (agent_team_size) 1x-6x chip dropdown
- multi-chat tabs: ChatSessions model (Deref-to-active) + tab row UI
  (switch / close / + / Cmd+T) with each run bound to its tab

Large checkpoint commit spanning the working tree (Rust shell crates).
2026-07-02 21:21:06 +08:00

54 lines
1.5 KiB
TypeScript

import { defineEventHandler, createEventStream } from 'h3';
import { randomUUID } from 'node:crypto';
import {
registerSSEClient,
unregisterSSEClient,
getSyncDocument,
} from '../../utils/mcp-sync-state';
// Bun.serve has a default idleTimeout of 10s. Heartbeat must be shorter
// to prevent the SSE connection from being killed.
const HEARTBEAT_MS = 8_000;
/** GET /api/mcp/events — SSE stream for renderer to subscribe to live document changes. */
export default defineEventHandler((event) => {
const clientId = randomUUID();
const stream = createEventStream(event);
let closed = false;
const cleanup = () => {
if (closed) return;
closed = true;
clearInterval(heartbeat);
unregisterSSEClient(clientId);
stream.close();
};
const write = (data: string) => {
if (closed) return;
stream.push(data).catch(cleanup);
};
// Send client ID so renderer can use it as sourceClientId when pushing back
write(JSON.stringify({ type: 'client:id', clientId }));
// Send current document as initial state (if any)
const { doc, version } = getSyncDocument();
if (doc) {
write(JSON.stringify({ type: 'document:init', version, document: doc }));
}
registerSSEClient(clientId, { push: write });
// Keep-alive heartbeat — must be shorter than Bun's idle timeout (10s)
const heartbeat = setInterval(() => {
if (closed) return;
stream.push(':heartbeat').catch(cleanup);
}, HEARTBEAT_MS);
// Clean up when client disconnects
stream.onClosed(cleanup);
return stream.send();
});