tandem-editor 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Bryan Kolb
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bryan Kolb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,75 +2,133 @@
2
2
  <img src="docs/assets/banner.png" alt="Tandem — Collaborative AI-Human Document Editor" width="800">
3
3
  </p>
4
4
 
5
- A collaborative document editor where Claude and a human work on the same document in real-time -- editing, highlighting, commenting, and annotating together.
5
+ An AI document reviewer open a progress report, RFP, or compliance filing and Claude reviews it alongside you in real time. Highlights, comments, suggestions, and questions appear as first-class annotations you accept, dismiss, or discuss. The original file is never modified unless you save.
6
6
 
7
7
  ![Tandem editor showing a document with annotations, side panel, and Claude's presence](docs/screenshots/01-editor-overview.png)
8
8
 
9
- ```mermaid
10
- graph LR
11
- Browser["Browser<br/>Tiptap Editor"] <-->|WebSocket| Server["Tandem Server<br/>Node.js"]
12
- Server <-->|MCP HTTP| Claude["Claude Code"]
13
- Server <-->|File I/O| Files[".md .txt .docx"]
14
- Server -->|SSE events| Shim["Channel Shim"]
15
- Shim -->|push notifications| Claude
16
- ```
17
-
18
- ## Getting Started
9
+ ## Quick Start
19
10
 
20
11
  ### Prerequisites
21
12
 
22
13
  - **Node.js 22+** ([download](https://nodejs.org))
23
14
  - **Claude Code** (`irm https://claude.ai/install.ps1 | iex`)
24
15
 
25
- ### Option A: Global Install (recommended)
16
+ ### Install and Run
26
17
 
27
18
  ```bash
28
19
  npm install -g tandem-editor
29
- tandem setup # registers MCP tools with Claude Code / Claude Desktop
20
+ tandem setup # registers MCP tools + installs Claude Code skill
30
21
  tandem # starts server + opens browser
31
22
  ```
32
23
 
33
- `tandem setup` auto-detects Claude Code and Claude Desktop and writes MCP configuration so tools work from any directory. Re-run after upgrading (`npm update -g tandem-editor`).
24
+ `tandem setup` auto-detects Claude Code and Claude Desktop, writes MCP configuration, and installs a skill (`~/.claude/skills/tandem/SKILL.md`) that teaches Claude how to use Tandem's tools effectively. Re-run after upgrading (`npm update -g tandem-editor && tandem setup`).
25
+
26
+ ### Connect Claude Code
34
27
 
35
- ### Option B: Development Setup
28
+ Start Claude Code with channel push for real-time notifications:
36
29
 
37
30
  ```bash
38
- git clone https://github.com/bloknayrb/tandem.git
39
- cd tandem
40
- npm install
41
- npm run dev:standalone # starts server (:3478/:3479) + browser client (:5173)
31
+ claude --dangerously-load-development-channels server:tandem-channel
42
32
  ```
43
33
 
44
- Open http://localhost:5173 -- you'll see `sample/welcome.md` loaded automatically on first run. The `.mcp.json` in the repo configures Claude Code automatically when run from this directory.
45
-
46
- ### Connect Claude Code
47
-
48
- Start Claude Code and try:
34
+ Then try:
49
35
 
50
36
  ```
51
37
  "Review the welcome document with me"
52
38
  ```
53
39
 
54
- Claude calls `tandem_open`, the document appears in the browser, and annotations start flowing.
40
+ Claude calls `tandem_open`, the document appears in the browser, and annotations start flowing. Chat messages, annotation actions, and text selections push to Claude instantly.
55
41
 
56
- ### Verify (if something seems wrong)
42
+ **Without channels:** Use the `/loop` skill in Claude Code to poll:
43
+
44
+ ```
45
+ /loop 30s check tandem inbox and respond to any new messages
46
+ ```
47
+
48
+ ### Verify
57
49
 
58
50
  ```bash
59
- npm run doctor # from the repo, or after global install
51
+ npm run doctor # checks Node.js, MCP config, server health, ports
60
52
  ```
61
53
 
62
- This checks Node.js version, MCP configuration (both `.mcp.json` and `~/.claude/mcp_settings.json`), server health, and port status with actionable fix suggestions. You can also check the raw health endpoint:
54
+ Or check the raw health endpoint:
63
55
 
64
56
  ```bash
65
57
  curl http://localhost:3479/health
66
- # → {"status":"ok","version":"0.1.0","transport":"http","hasSession":false}
58
+ # → {"status":"ok","version":"0.1.2","transport":"http","hasSession":false}
67
59
  ```
68
60
 
69
61
  `hasSession` becomes `true` once Claude Code connects.
70
62
 
63
+ <details>
64
+ <summary><strong>Development Setup</strong> (contributing / building from source)</summary>
65
+
66
+ ```bash
67
+ git clone https://github.com/bloknayrb/tandem.git
68
+ cd tandem
69
+ npm install
70
+ npm run dev:standalone # starts server (:3478/:3479) + browser client (:5173)
71
+ ```
72
+
73
+ Open http://localhost:5173 — you'll see `sample/welcome.md` loaded automatically on first run. The `.mcp.json` in the repo configures Claude Code automatically when run from this directory.
74
+
75
+ </details>
76
+
77
+ ## Features
78
+
79
+ ### Annotations
80
+
81
+ ![Side panel showing annotation cards with filtering, bulk actions, and text previews](docs/screenshots/03-side-panel.png)
82
+
83
+ Claude adds highlights, comments, suggestions, and flags directly in the document. The side panel lists all annotations with filtering by type, author, and status. Accept, dismiss, or edit each one individually — or use bulk actions to process them in batches.
84
+
85
+ ### Chat
86
+
87
+ ![Chat sidebar showing messages, typing indicator, and panel toggle](docs/screenshots/02-chat-sidebar.png)
88
+
89
+ Send freeform messages to Claude alongside annotation review. Select text before sending to attach it as a clickable anchor — clicking it later scrolls back to that passage.
90
+
91
+ ### Review Mode
92
+
93
+ ![Review mode with dimmed editor and active annotation highlighted](docs/screenshots/05-review-mode.png)
94
+
95
+ Press **Ctrl+Shift+R** to enter keyboard review mode. Navigate with **Tab**, accept with **Y**, dismiss with **N**, examine with **E**. A 10-second undo window lets you reverse accidental accepts. The side panel tracks your position.
96
+
97
+ ### More
98
+
99
+ - **Multi-document tabs** — open `.md`, `.txt`, `.docx` files side by side; drag to reorder
100
+ - **.docx review-only mode** — open Word documents for annotation; imported Word comments appear alongside Claude's
101
+ - **Session persistence** — documents and annotations survive server restarts
102
+ - **Real-time channel push** — annotation actions, chat, and selections push to Claude instantly
103
+ - **Keyboard shortcuts** — press `?` for the full reference
104
+ - **Unsaved-changes indicator** — dot on tab title when a document has pending edits
105
+ - **Configurable display name** — set your name so Claude knows who's reviewing
106
+ - **Atomic file saves** — write to temp, then rename, preventing partial writes
107
+ - **E2E tested** — Playwright tests cover the annotation lifecycle end-to-end
108
+
109
+ ## Documentation
110
+
111
+ - **[User Guide](docs/user-guide.md)** — How to use Tandem: browser UI, annotations, chat, review mode, keyboard shortcuts
112
+ - [MCP Tool Reference](docs/mcp-tools.md) — 30 MCP tools + channel API endpoints
113
+ - [Architecture](docs/architecture.md) — System design, data flows, coordinate systems, channel push
114
+ - [Workflows](docs/workflows.md) — Claude Code usage patterns: document review, cross-referencing, multi-model
115
+ - [Roadmap](docs/roadmap.md) — Phase 2+ roadmap, known issues, future extensions
116
+ - [Design Decisions](docs/decisions.md) — ADR-001 through ADR-021
117
+ - [Lessons Learned](docs/lessons-learned.md) — 31 implementation lessons
118
+
119
+ ## CLI Commands
120
+
121
+ | Command | What it does |
122
+ |---------|-------------|
123
+ | `tandem` | Start server and open browser (global install) |
124
+ | `tandem setup` | Register MCP tools with Claude Code / Claude Desktop |
125
+ | `tandem setup --force` | Register to default paths regardless of auto-detection |
126
+ | `tandem --version` | Show installed version |
127
+ | `tandem --help` | Show usage |
128
+
71
129
  ## MCP Configuration
72
130
 
73
- Tandem uses two MCP connections: **HTTP** for document tools (27 tools including annotation editing), and a **channel shim** for real-time push notifications.
131
+ Tandem uses two MCP connections: **HTTP** for document tools (28 tools including annotation editing), and a **channel shim** for real-time push notifications.
74
132
 
75
133
  **Global install** (`tandem setup`): Automatically writes both entries to `~/.claude/mcp_settings.json` (Claude Code) and/or `claude_desktop_config.json` (Claude Desktop) with absolute paths. No manual configuration needed.
76
134
 
@@ -92,15 +150,11 @@ Tandem uses two MCP connections: **HTTP** for document tools (27 tools including
92
150
  }
93
151
  ```
94
152
 
95
- Both entries are cross-platform -- no platform-specific configuration needed.
96
-
97
- The channel shim is optional -- without it, Claude relies on polling via `tandem_checkInbox` instead of receiving real-time push events.
98
-
99
- **Important:** The server must be running before Claude Code connects.
153
+ Both entries are cross-platform no platform-specific configuration needed.
100
154
 
101
155
  ## Environment Variables
102
156
 
103
- All optional -- defaults work out of the box.
157
+ All optional defaults work out of the box.
104
158
 
105
159
  | Variable | Default | Description |
106
160
  |----------|---------|-------------|
@@ -109,6 +163,7 @@ All optional -- defaults work out of the box.
109
163
  | `TANDEM_URL` | `http://localhost:3479` | Channel shim server URL |
110
164
  | `TANDEM_TRANSPORT` | `http` | Transport mode (`http` or `stdio`) |
111
165
  | `TANDEM_NO_SAMPLE` | unset | Set to `1` to skip auto-opening `sample/welcome.md` |
166
+ | `TANDEM_CLAUDE_CMD` | `claude` | Claude Code executable name (for `tandem setup` auto-detection) |
112
167
 
113
168
  See `.env.example` for a copy-paste template.
114
169
 
@@ -123,7 +178,7 @@ Start the server first (`tandem` for global install, or `npm run dev:standalone`
123
178
  Tandem kills stale processes on :3478/:3479 at startup. If another app uses those ports, set `TANDEM_PORT` / `TANDEM_MCP_PORT` to different values and update `TANDEM_URL` to match.
124
179
 
125
180
  **Channel shim fails to start**
126
- The `tandem-channel` entry spawns a subprocess. For global installs, `tandem setup` writes absolute paths to the bundled `dist/channel/index.js` -- re-run `tandem setup` after upgrading. For dev setup, if you see `MODULE_NOT_FOUND` with a production config (`node dist/channel/index.js`), run `npm run build`. The default dev config uses `npx tsx` and doesn't require a build step.
181
+ The `tandem-channel` entry spawns a subprocess. For global installs, `tandem setup` writes absolute paths to the bundled `dist/channel/index.js` re-run `tandem setup` after upgrading. For dev setup, if you see `MODULE_NOT_FOUND` with a production config (`node dist/channel/index.js`), run `npm run build`. The default dev config uses `npx tsx` and doesn't require a build step.
127
182
 
128
183
  **Browser shows "Cannot reach the Tandem server"**
129
184
  The browser connects to the server via WebSocket. For global installs, run `tandem` to start the server. For dev setup, use `npm run dev:standalone` (or `npm run dev:server`). The message appears after 3 seconds of failed connection.
@@ -131,83 +186,11 @@ The browser connects to the server via WebSocket. For global installs, run `tand
131
186
  **Empty browser with no document**
132
187
  On first run, `sample/welcome.md` auto-opens. If you've cleared sessions or deleted the sample file, click the **+** button in the tab bar or drop a file onto the editor.
133
188
 
134
- ## Features
135
-
136
- ### Annotations
137
-
138
- Claude adds highlights, comments, suggestions, and flags directly in the document. Each annotation type has distinct styling -- colored backgrounds for highlights, dashed underlines for comments, wavy underlines for suggestions -- so you can scan at a glance.
139
-
140
- ![Side panel showing annotation cards with text excerpts, accept/dismiss buttons, filtering, and bulk actions](docs/screenshots/03-side-panel.png)
141
-
142
- The side panel lists all annotations with filtering by type, author, and status. Each card shows a preview of the annotated text. Bulk accept/dismiss buttons appear when multiple annotations are pending. Annotations can be edited after creation via the pencil button -- typos in comments or suggestions no longer require deleting and recreating.
143
-
144
- ### Chat Sidebar
145
-
146
- ![Chat sidebar showing Claude review messages, typing indicator, and panel toggle tabs](docs/screenshots/02-chat-sidebar.png)
147
-
148
- The right sidebar toggles between **Annotations** and **Chat** views. In chat mode, send freeform messages to Claude alongside annotation review. If you have text selected when you hit Send, it is attached as a clickable anchor -- clicking it later scrolls the editor back to that passage. Claude's responses are rendered as Markdown. An unread badge on the Chat tab appears when Claude has replied while you were in the Annotations view.
149
-
150
- ### Toolbar
151
-
152
- ![Toolbar with annotation buttons, multiple document tabs with format indicators, and side panel header](docs/screenshots/04-toolbar-actions.png)
153
-
154
- Select text in the editor to activate the toolbar buttons: Highlight (with color picker), Comment, Suggest, Flag, and Ask Claude. The tab bar shows open documents with format indicators (M for Markdown, W for Word, T for Text). Tabs scroll horizontally when they overflow, and can be reordered via drag-and-drop or Alt+Left/Right.
155
-
156
- ### Keyboard Review Mode
157
-
158
- ![Review mode with dimmed editor text and active annotation highlighted](docs/screenshots/05-review-mode.png)
159
-
160
- Press **Ctrl+Shift+R** or click "Review in sequence" to enter review mode. The editor dims non-annotated text so annotations stand out. Navigate with **Tab/Shift+Tab**, accept with **Y**, dismiss with **N**, or examine with **E**. The side panel tracks your position (e.g. "Reviewing 1 / 7").
161
-
162
- ### Claude's Presence
163
-
164
- ![Status bar showing connection state, document count, interruption mode, and Claude's current activity](docs/screenshots/06-claude-presence.png)
165
-
166
- The status bar shows real-time connection state with reconnect attempt count and elapsed time, open document count, and Claude's current activity. A prominent banner appears after 30 seconds of continuous disconnect with actionable guidance. Claude's focus paragraph gets a subtle blue highlight in the editor. Interruption modes (All / Urgent / Paused) control which annotations surface immediately vs. get held for later.
167
-
168
- ### Toast Notifications
169
-
170
- ![Toast notification showing an annotation failure message with dismiss button](docs/screenshots/07-toast-notification.png)
171
-
172
- Annotation failures and save errors surface as dismissible toast notifications. Toasts auto-dismiss by severity (errors linger longest) and deduplicate with a count badge when the same message repeats.
173
-
174
- ### Onboarding Tutorial
175
-
176
- ![Onboarding tutorial card showing step 1 of 3 with progress dots and next button](docs/screenshots/08-onboarding-tutorial.png)
177
-
178
- On first launch, a 3-step guided walkthrough appears over the welcome document. Pre-placed annotations let you practice reviewing, asking Claude a question, and editing -- then the tutorial dismisses itself. Progress persists in localStorage so it only shows once.
179
-
180
- ### More
181
-
182
- - **Multi-document tabs** -- open `.md`, `.txt`, `.docx` files side by side, each in its own Y.Doc room
183
- - **Browser file open** -- click "+" in the tab bar or drag-and-drop a file onto the editor (no Claude needed); recent files remembered
184
- - **Markdown round-trip** -- lossless MDAST-based conversion preserves formatting through load/save cycles
185
- - **.docx review-only mode** -- open Word documents for annotation; a banner makes clear edits aren't saved back to the original
186
- - **Session persistence** -- Y.Doc state and annotations survive server restarts
187
- - **Real-time channel push** -- annotation accepts/dismisses, chat messages, and document switches push to Claude instantly via the Channels API (no polling)
188
- - **User→Claude inbox** -- highlights, comments, and questions you add are surfaced to Claude via push events or `tandem_checkInbox` fallback
189
- - **Unsaved-changes indicator** -- dot on the tab title when a document has pending edits
190
- - **Configurable display name** -- set your name so Claude knows who's reviewing
191
- - **Annotation text preview** -- each card in the side panel shows an excerpt of the annotated text
192
- - **Keyboard shortcuts reference** -- press `?` to open the in-app shortcut reference
193
- - **E2E tested** -- Playwright tests cover the annotation lifecycle end-to-end
194
- - **Atomic file saves** -- write to temp, then rename, preventing partial writes
195
-
196
- ## CLI Commands
197
-
198
- | Command | What it does |
199
- |---------|-------------|
200
- | `tandem` | Start server and open browser (global install) |
201
- | `tandem setup` | Register MCP tools with Claude Code / Claude Desktop |
202
- | `tandem setup --force` | Register to default paths regardless of auto-detection |
203
- | `tandem --version` | Show installed version |
204
- | `tandem --help` | Show usage |
205
-
206
- ## Development Scripts
189
+ ## Development
207
190
 
208
191
  | Command | What it does |
209
192
  |---------|-------------|
210
- | `npm run dev:standalone` | **Recommended** -- both frontend + backend (via concurrently) |
193
+ | `npm run dev:standalone` | **Recommended** both frontend + backend (via concurrently) |
211
194
  | `npm run dev:server` | Backend only: Hocuspocus (:3478) + MCP HTTP (:3479) |
212
195
  | `npm run dev:client` | Frontend only: Vite dev server (:5173) |
213
196
  | `npm run build` | Production build (`dist/server/` + `dist/channel/` + `dist/cli/` + `dist/client/`) |
@@ -215,18 +198,4 @@ On first launch, a 3-step guided walkthrough appears over the welcome document.
215
198
  | `npm run test:e2e` | Run Playwright E2E tests |
216
199
  | `npm run test:e2e:ui` | Playwright UI mode |
217
200
 
218
- ## Documentation
219
-
220
- - [MCP Tool Reference](docs/mcp-tools.md) -- 27 MCP tools + channel API endpoints
221
- - [Architecture](docs/architecture.md) -- System design, data flows, coordinate systems, channel push
222
- - [Workflows](docs/workflows.md) -- Real-world usage patterns
223
- - [Roadmap](docs/roadmap.md) -- Phase 2+ roadmap, known issues, future extensions
224
- - [Design Decisions](docs/decisions.md) -- ADR-001 through ADR-021
225
- - [Lessons Learned](docs/lessons-learned.md) -- 31 implementation lessons
226
-
227
- ## Tech Stack
228
-
229
- **Frontend:** React 19, Tiptap, Vite, TypeScript
230
- **Backend:** Node.js, Hocuspocus (Yjs WebSocket), MCP SDK (Streamable HTTP transport), Express
231
- **Collaboration:** Yjs (CRDT), @hocuspocus/provider, y-prosemirror
232
- **File I/O:** mammoth.js (.docx), unified/remark (.md)
201
+ **Tech Stack:** React 19, Tiptap, Vite, TypeScript | Node.js, Hocuspocus (Yjs WebSocket), MCP SDK, Express | Yjs (CRDT), y-prosemirror | mammoth.js (.docx), unified/remark (.md)
@@ -97,6 +97,7 @@ function formatEventMeta(event) {
97
97
 
98
98
  // src/channel/event-bridge.ts
99
99
  var AWARENESS_DEBOUNCE_MS = 500;
100
+ var SELECTION_DEBOUNCE_MS = 1500;
100
101
  async function startEventBridge(mcp2, tandemUrl) {
101
102
  let retries = 0;
102
103
  let lastEventId;
@@ -167,7 +168,35 @@ async function connectAndStream(mcp2, tandemUrl, lastEventId, onEventId) {
167
168
  if (awarenessTimer) clearTimeout(awarenessTimer);
168
169
  awarenessTimer = setTimeout(flushAwareness, AWARENESS_DEBOUNCE_MS);
169
170
  }
171
+ let selectionTimer = null;
172
+ let pendingSelection = null;
173
+ let transportBroken = false;
174
+ async function flushSelection() {
175
+ if (!pendingSelection) return;
176
+ const { event, eventId } = pendingSelection;
177
+ pendingSelection = null;
178
+ if (eventId) onEventId(eventId);
179
+ try {
180
+ await mcp2.notification({
181
+ method: "notifications/claude/channel",
182
+ params: {
183
+ content: formatEventContent(event),
184
+ meta: formatEventMeta(event)
185
+ }
186
+ });
187
+ } catch (err) {
188
+ console.error("[Channel] MCP notification failed (transport broken?):", err);
189
+ transportBroken = true;
190
+ return;
191
+ }
192
+ scheduleAwareness(event);
193
+ }
194
+ function isSelectionCleared(event) {
195
+ const p = event.payload;
196
+ return !p || p.from === p.to && !p.selectedText;
197
+ }
170
198
  while (true) {
199
+ if (transportBroken) throw new Error("MCP transport broken (detected in debounced flush)");
171
200
  const { done, value } = await reader.read();
172
201
  if (done) throw new Error("SSE stream ended");
173
202
  buffer += decoder.decode(value, { stream: true });
@@ -194,6 +223,14 @@ async function connectAndStream(mcp2, tandemUrl, lastEventId, onEventId) {
194
223
  console.error("[Channel] Received invalid SSE event, skipping");
195
224
  continue;
196
225
  }
226
+ if (event.type === "selection:changed") {
227
+ if (eventId) onEventId(eventId);
228
+ if (isSelectionCleared(event)) continue;
229
+ pendingSelection = { event, eventId };
230
+ if (selectionTimer) clearTimeout(selectionTimer);
231
+ selectionTimer = setTimeout(flushSelection, SELECTION_DEBOUNCE_MS);
232
+ continue;
233
+ }
197
234
  if (eventId) onEventId(eventId);
198
235
  try {
199
236
  await mcp2.notification({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/channel/index.ts","../../src/shared/constants.ts","../../src/server/events/types.ts","../../src/channel/event-bridge.ts"],"sourcesContent":["#!/usr/bin/env node\r\n/**\r\n * Tandem Channel Shim — Claude Code spawns this as a subprocess.\r\n *\r\n * Bridges Tandem's SSE event stream → Claude Code channel notifications,\r\n * and exposes a `tandem_reply` tool for Claude to respond to chat messages.\r\n *\r\n * Uses the low-level MCP `Server` class (not `McpServer`) as required by\r\n * the Channels API spec.\r\n */\r\n\r\nimport { createConnection } from \"node:net\";\r\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\r\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\r\nimport { CallToolRequestSchema, ListToolsRequestSchema } from \"@modelcontextprotocol/sdk/types.js\";\r\nimport { z } from \"zod\";\r\nimport { DEFAULT_MCP_PORT } from \"../shared/constants.js\";\r\nimport { startEventBridge } from \"./event-bridge.js\";\r\n\r\n// stdout is the MCP wire — redirect console.log to stderr\r\nconsole.log = console.error;\r\nconsole.warn = console.error;\r\nconsole.info = console.error;\r\n\r\nconst TANDEM_URL = process.env.TANDEM_URL || \"http://localhost:3479\";\r\n\r\n// --- Pre-flight: verify Tandem server is reachable before MCP handshake ---\r\n\r\nasync function checkServerReachable(url: string, timeoutMs = 2000): Promise<boolean> {\r\n let parsed: URL;\r\n try {\r\n parsed = new URL(url);\r\n } catch {\r\n console.error(\r\n `[Channel] Invalid TANDEM_URL: \"${url}\" — expected format: http://localhost:3479`,\r\n );\r\n return false;\r\n }\r\n const port = parseInt(parsed.port || String(DEFAULT_MCP_PORT), 10);\r\n return new Promise((resolve) => {\r\n const socket = createConnection({ port, host: parsed.hostname }, () => {\r\n socket.destroy();\r\n resolve(true);\r\n });\r\n socket.setTimeout(timeoutMs);\r\n socket.on(\"timeout\", () => {\r\n socket.destroy();\r\n resolve(false);\r\n });\r\n socket.on(\"error\", (err) => {\r\n console.error(`[Channel] Server probe failed: ${err.message}`);\r\n socket.destroy();\r\n resolve(false);\r\n });\r\n });\r\n}\r\n\r\n// --- MCP Server setup ---\r\n\r\nconst mcp = new Server(\r\n { name: \"tandem-channel\", version: \"0.1.0\" },\r\n {\r\n capabilities: {\r\n experimental: {\r\n \"claude/channel\": {},\r\n \"claude/channel/permission\": {},\r\n },\r\n tools: {},\r\n },\r\n instructions: [\r\n 'Events from Tandem arrive as <channel source=\"tandem-channel\" event_type=\"...\" document_id=\"...\">.',\r\n \"These are real-time push notifications of user actions in the collaborative document editor.\",\r\n \"Event types: annotation:created, annotation:accepted, annotation:dismissed,\",\r\n \"chat:message, selection:changed, document:opened, document:closed, document:switched.\",\r\n \"Use your tandem MCP tools (tandem_getTextContent, tandem_comment, tandem_highlight, etc.) to act on them.\",\r\n \"Reply to chat messages using tandem_reply. Pass document_id from the tag attributes.\",\r\n \"Do not reply to non-chat events — just act on them using tools.\",\r\n \"If you haven't received channel notifications recently, call tandem_checkInbox as a fallback.\",\r\n ].join(\" \"),\r\n },\r\n);\r\n\r\n// --- Tool: tandem_reply (forwarded to Tandem HTTP server) ---\r\n\r\nmcp.setRequestHandler(ListToolsRequestSchema, async () => ({\r\n tools: [\r\n {\r\n name: \"tandem_reply\",\r\n description: \"Reply to a chat message in Tandem\",\r\n inputSchema: {\r\n type: \"object\" as const,\r\n properties: {\r\n text: { type: \"string\", description: \"The reply message\" },\r\n documentId: {\r\n type: \"string\",\r\n description: \"Document ID from the channel event (optional)\",\r\n },\r\n replyTo: {\r\n type: \"string\",\r\n description: \"Message ID being replied to (optional)\",\r\n },\r\n },\r\n required: [\"text\"],\r\n },\r\n },\r\n ],\r\n}));\r\n\r\nmcp.setRequestHandler(CallToolRequestSchema, async (req) => {\r\n if (req.params.name === \"tandem_reply\") {\r\n const args = req.params.arguments as Record<string, unknown>;\r\n try {\r\n const res = await fetch(`${TANDEM_URL}/api/channel-reply`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify(args),\r\n });\r\n let data: unknown;\r\n try {\r\n data = await res.json();\r\n } catch {\r\n data = { message: \"Non-JSON response\" };\r\n }\r\n if (!res.ok) {\r\n return {\r\n content: [\r\n {\r\n type: \"text\" as const,\r\n text: `Reply failed (${res.status}): ${JSON.stringify(data)}`,\r\n },\r\n ],\r\n isError: true,\r\n };\r\n }\r\n return { content: [{ type: \"text\" as const, text: JSON.stringify(data) }] };\r\n } catch (err) {\r\n return {\r\n content: [\r\n {\r\n type: \"text\" as const,\r\n text: `Failed to send reply: ${err instanceof Error ? err.message : String(err)}`,\r\n },\r\n ],\r\n isError: true,\r\n };\r\n }\r\n }\r\n throw new Error(`Unknown tool: ${req.params.name}`);\r\n});\r\n\r\n// --- Permission relay: forward Claude Code's tool approval prompts to Tandem browser ---\r\n\r\nconst PermissionRequestSchema = z.object({\r\n method: z.literal(\"notifications/claude/channel/permission_request\"),\r\n params: z.object({\r\n request_id: z.string(),\r\n tool_name: z.string(),\r\n description: z.string(),\r\n input_preview: z.string(),\r\n }),\r\n});\r\n\r\nmcp.setNotificationHandler(PermissionRequestSchema, async ({ params }) => {\r\n try {\r\n const res = await fetch(`${TANDEM_URL}/api/channel-permission`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n requestId: params.request_id,\r\n toolName: params.tool_name,\r\n description: params.description,\r\n inputPreview: params.input_preview,\r\n }),\r\n });\r\n if (!res.ok) {\r\n console.error(\r\n `[Channel] Permission relay got HTTP ${res.status} — browser may not see prompt`,\r\n );\r\n }\r\n } catch (err) {\r\n console.error(\"[Channel] Failed to forward permission request:\", err);\r\n }\r\n});\r\n\r\n// --- Connect and start ---\r\n\r\nasync function main() {\r\n console.error(`[Channel] Tandem channel shim starting (server: ${TANDEM_URL})`);\r\n\r\n const reachable = await checkServerReachable(TANDEM_URL);\r\n if (!reachable) {\r\n console.error(`[Channel] Cannot reach Tandem server at ${TANDEM_URL}`);\r\n console.error(\"[Channel] Start it with: npm run dev:standalone\");\r\n // Continue anyway — the event bridge will retry, and the server may start later\r\n }\r\n\r\n // Connect to Claude Code over stdio\r\n const transport = new StdioServerTransport();\r\n await mcp.connect(transport);\r\n console.error(\"[Channel] Connected to Claude Code via stdio\");\r\n\r\n // Start the SSE event bridge (runs until disconnected or max retries)\r\n startEventBridge(mcp, TANDEM_URL).catch((err) => {\r\n console.error(\"[Channel] Event bridge failed unexpectedly:\", err);\r\n process.exit(1);\r\n });\r\n}\r\n\r\nmain().catch((err) => {\r\n console.error(\"[Channel] Fatal error:\", err);\r\n process.exit(1);\r\n});\r\n","export const DEFAULT_WS_PORT = 3478;\r\nexport const DEFAULT_MCP_PORT = 3479;\r\n\r\n/** File extensions the server accepts for opening. */\r\nexport const SUPPORTED_EXTENSIONS = new Set([\".md\", \".txt\", \".html\", \".htm\", \".docx\"]);\r\nexport const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB\r\nexport const MAX_WS_PAYLOAD = 10 * 1024 * 1024; // 10MB\r\nexport const MAX_WS_CONNECTIONS = 4;\r\nexport const IDLE_TIMEOUT = 30 * 60 * 1000; // 30 minutes\r\nexport const SESSION_MAX_AGE = 30 * 24 * 60 * 60 * 1000; // 30 days\r\nexport const TYPING_DEBOUNCE = 3000; // 3 seconds\r\nexport const DISCONNECT_DEBOUNCE_MS = 3000; // 3 seconds before showing \"server not reachable\"\r\nexport const PROLONGED_DISCONNECT_MS = 30_000; // 30 seconds before showing App-level disconnect banner\r\nexport const OVERLAY_STALE_DEBOUNCE = 200; // 200ms\r\nexport const REVIEW_BANNER_THRESHOLD = 5;\r\n\r\nexport const HIGHLIGHT_COLORS: Record<string, string> = {\r\n yellow: \"rgba(255, 235, 59, 0.3)\",\r\n red: \"rgba(244, 67, 54, 0.3)\",\r\n green: \"rgba(76, 175, 80, 0.3)\",\r\n blue: \"rgba(33, 150, 243, 0.3)\",\r\n purple: \"rgba(156, 39, 176, 0.3)\",\r\n};\r\n\r\nexport const INTERRUPTION_MODE_DEFAULT = \"all\" as const;\r\nexport const INTERRUPTION_MODE_KEY = \"tandem:interruptionMode\";\r\n\r\n// Large file thresholds\r\nexport const CHARS_PER_PAGE = 3_000;\r\nexport const LARGE_FILE_PAGE_THRESHOLD = 50;\r\nexport const VERY_LARGE_FILE_PAGE_THRESHOLD = 100;\r\n\r\nexport const CLAUDE_PRESENCE_COLOR = \"#6366f1\";\r\nexport const CLAUDE_FOCUS_OPACITY = 0.1;\r\n\r\nexport const CTRL_ROOM = \"__tandem_ctrl__\";\r\n\r\n/** Y.Map key constants — centralized to prevent silent bugs from string typos. */\r\nexport const Y_MAP_ANNOTATIONS = \"annotations\";\r\nexport const Y_MAP_AWARENESS = \"awareness\";\r\nexport const Y_MAP_USER_AWARENESS = \"userAwareness\";\r\nexport const Y_MAP_CHAT = \"chat\";\r\nexport const Y_MAP_DOCUMENT_META = \"documentMeta\";\r\nexport const Y_MAP_SAVED_AT_VERSION = \"savedAtVersion\";\r\n\r\nexport const SERVER_INFO_DIR = \".tandem\";\r\nexport const SERVER_INFO_FILE = \".tandem/.server-info\";\r\n\r\nexport const RECENT_FILES_KEY = \"tandem:recentFiles\";\r\nexport const RECENT_FILES_CAP = 20;\r\n\r\nexport const USER_NAME_KEY = \"tandem:userName\";\r\nexport const USER_NAME_DEFAULT = \"You\";\r\n\r\n// Toast notifications\r\nexport const TOAST_DISMISS_MS = { error: 8000, warning: 6000, info: 4000 } as const;\r\nexport const MAX_VISIBLE_TOASTS = 5;\r\nexport const NOTIFICATION_BUFFER_SIZE = 50;\r\n\r\n// Onboarding tutorial\r\nexport const TUTORIAL_COMPLETED_KEY = \"tandem:tutorialCompleted\";\r\nexport const TUTORIAL_ANNOTATION_PREFIX = \"tutorial-\";\r\n\r\n// Channel / event queue\r\nexport const CHANNEL_EVENT_BUFFER_SIZE = 200;\r\nexport const CHANNEL_EVENT_BUFFER_AGE_MS = 60_000; // 60 seconds\r\nexport const CHANNEL_SSE_KEEPALIVE_MS = 15_000; // 15 seconds\r\nexport const CHANNEL_MAX_RETRIES = 5;\r\nexport const CHANNEL_RETRY_DELAY_MS = 2_000;\r\n","/**\r\n * Event types for the Tandem → Claude Code channel.\r\n *\r\n * These events flow from browser-originated Y.Map changes through an SSE\r\n * endpoint to the channel shim, which pushes them into Claude Code as\r\n * `notifications/claude/channel` messages.\r\n */\r\n\r\n// --- Per-event payload interfaces ---\r\n\r\nexport interface AnnotationCreatedPayload {\r\n annotationId: string;\r\n annotationType: string;\r\n content: string;\r\n textSnippet: string;\r\n}\r\n\r\nexport interface AnnotationAcceptedPayload {\r\n annotationId: string;\r\n textSnippet: string;\r\n}\r\n\r\nexport interface AnnotationDismissedPayload {\r\n annotationId: string;\r\n textSnippet: string;\r\n}\r\n\r\nexport interface ChatMessagePayload {\r\n messageId: string;\r\n text: string;\r\n replyTo: string | null;\r\n anchor: { from: number; to: number; textSnapshot: string } | null;\r\n}\r\n\r\nexport interface SelectionChangedPayload {\r\n from: number;\r\n to: number;\r\n selectedText: string;\r\n}\r\n\r\nexport interface DocumentOpenedPayload {\r\n fileName: string;\r\n format: string;\r\n}\r\n\r\nexport interface DocumentClosedPayload {\r\n fileName: string;\r\n}\r\n\r\nexport interface DocumentSwitchedPayload {\r\n fileName: string;\r\n}\r\n\r\n// --- Discriminated union ---\r\n\r\ninterface TandemEventBase {\r\n /** Timestamp-based unique ID for SSE `Last-Event-ID` reconnection. Format: `evt_<timestamp>_<rand>`. Roughly ordered but not strictly monotonic. */\r\n id: string;\r\n timestamp: number;\r\n /** Which document this event relates to (absent for global events). */\r\n documentId?: string;\r\n}\r\n\r\nexport type TandemEvent =\r\n | (TandemEventBase & { type: \"annotation:created\"; payload: AnnotationCreatedPayload })\r\n | (TandemEventBase & { type: \"annotation:accepted\"; payload: AnnotationAcceptedPayload })\r\n | (TandemEventBase & { type: \"annotation:dismissed\"; payload: AnnotationDismissedPayload })\r\n | (TandemEventBase & { type: \"chat:message\"; payload: ChatMessagePayload })\r\n | (TandemEventBase & { type: \"selection:changed\"; payload: SelectionChangedPayload })\r\n | (TandemEventBase & { type: \"document:opened\"; payload: DocumentOpenedPayload })\r\n | (TandemEventBase & { type: \"document:closed\"; payload: DocumentClosedPayload })\r\n | (TandemEventBase & { type: \"document:switched\"; payload: DocumentSwitchedPayload });\r\n\r\n/** Union of all event type discriminants. */\r\nexport type TandemEventType = TandemEvent[\"type\"];\r\n\r\n// Re-export from shared utils (single ID generation pattern)\r\nexport { generateEventId } from \"../../shared/utils.js\";\r\n\r\n// --- Parse guard for SSE consumers ---\r\n\r\nconst VALID_EVENT_TYPES = new Set<TandemEventType>([\r\n \"annotation:created\",\r\n \"annotation:accepted\",\r\n \"annotation:dismissed\",\r\n \"chat:message\",\r\n \"selection:changed\",\r\n \"document:opened\",\r\n \"document:closed\",\r\n \"document:switched\",\r\n]);\r\n\r\n/**\r\n * Validate a JSON-parsed value as a TandemEvent.\r\n * Used by the event-bridge to safely consume SSE data.\r\n */\r\nexport function parseTandemEvent(raw: unknown): TandemEvent | null {\r\n if (\r\n typeof raw !== \"object\" ||\r\n raw === null ||\r\n !(\"id\" in raw) ||\r\n typeof (raw as Record<string, unknown>).id !== \"string\" ||\r\n !(\"type\" in raw) ||\r\n !VALID_EVENT_TYPES.has((raw as Record<string, unknown>).type as TandemEventType) ||\r\n !(\"timestamp\" in raw) ||\r\n typeof (raw as Record<string, unknown>).timestamp !== \"number\" ||\r\n !(\"payload\" in raw) ||\r\n typeof (raw as Record<string, unknown>).payload !== \"object\"\r\n ) {\r\n return null;\r\n }\r\n return raw as TandemEvent;\r\n}\r\n\r\n/**\r\n * Convert a TandemEvent into a human-readable string for the channel `content` field.\r\n * Claude sees this text inside `<channel source=\"tandem-channel\">` tags.\r\n */\r\nexport function formatEventContent(event: TandemEvent): string {\r\n const doc = event.documentId ? ` [doc: ${event.documentId}]` : \"\";\r\n\r\n switch (event.type) {\r\n case \"annotation:created\": {\r\n const { annotationType, content, textSnippet } = event.payload;\r\n const snippet = textSnippet ? ` on \"${textSnippet}\"` : \"\";\r\n return `User created ${annotationType}${snippet}: ${content || \"(no content)\"}${doc}`;\r\n }\r\n case \"annotation:accepted\": {\r\n const { annotationId, textSnippet } = event.payload;\r\n return `User accepted annotation ${annotationId}${textSnippet ? ` (\"${textSnippet}\")` : \"\"}${doc}`;\r\n }\r\n case \"annotation:dismissed\": {\r\n const { annotationId, textSnippet } = event.payload;\r\n return `User dismissed annotation ${annotationId}${textSnippet ? ` (\"${textSnippet}\")` : \"\"}${doc}`;\r\n }\r\n case \"chat:message\": {\r\n const { text, replyTo } = event.payload;\r\n const reply = replyTo ? ` (replying to ${replyTo})` : \"\";\r\n return `User says${reply}: ${text}${doc}`;\r\n }\r\n case \"selection:changed\": {\r\n const { from, to, selectedText } = event.payload;\r\n if (!selectedText) return `User cleared selection${doc}`;\r\n return `User selected text (${from}-${to}): \"${selectedText}\"${doc}`;\r\n }\r\n case \"document:opened\": {\r\n const { fileName, format } = event.payload;\r\n return `User opened document: ${fileName} (${format})${doc}`;\r\n }\r\n case \"document:closed\": {\r\n const { fileName } = event.payload;\r\n return `User closed document: ${fileName}${doc}`;\r\n }\r\n case \"document:switched\": {\r\n const { fileName } = event.payload;\r\n return `User switched to document: ${fileName}${doc}`;\r\n }\r\n default: {\r\n const _exhaustive: never = event;\r\n return `Unknown event${doc}`;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Build the `meta` record for a channel notification.\r\n * Keys use underscores only (Channels API silently drops hyphenated keys).\r\n */\r\nexport function formatEventMeta(event: TandemEvent): Record<string, string> {\r\n const meta: Record<string, string> = {\r\n event_type: event.type,\r\n };\r\n if (event.documentId) meta.document_id = event.documentId;\r\n\r\n switch (event.type) {\r\n case \"annotation:created\":\r\n case \"annotation:accepted\":\r\n case \"annotation:dismissed\":\r\n meta.annotation_id = event.payload.annotationId;\r\n break;\r\n case \"chat:message\":\r\n meta.message_id = event.payload.messageId;\r\n break;\r\n }\r\n\r\n return meta;\r\n}\r\n","/**\r\n * SSE event bridge: connects to Tandem server's /api/events endpoint\r\n * and pushes received events to Claude Code as channel notifications.\r\n */\r\n\r\nimport type { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\r\nimport type { TandemEvent } from \"../server/events/types.js\";\r\nimport { formatEventContent, formatEventMeta, parseTandemEvent } from \"../server/events/types.js\";\r\nimport { CHANNEL_MAX_RETRIES, CHANNEL_RETRY_DELAY_MS } from \"../shared/constants.js\";\r\n\r\nconst AWARENESS_DEBOUNCE_MS = 500;\r\n\r\nexport async function startEventBridge(mcp: Server, tandemUrl: string): Promise<void> {\r\n let retries = 0;\r\n let lastEventId: string | undefined;\r\n\r\n while (retries < CHANNEL_MAX_RETRIES) {\r\n try {\r\n await connectAndStream(mcp, tandemUrl, lastEventId, (id) => {\r\n lastEventId = id;\r\n retries = 0;\r\n });\r\n } catch (err) {\r\n retries++;\r\n console.error(\r\n `[Channel] SSE connection failed (${retries}/${CHANNEL_MAX_RETRIES}):`,\r\n err instanceof Error ? err.message : err,\r\n );\r\n\r\n if (retries >= CHANNEL_MAX_RETRIES) {\r\n console.error(\"[Channel] SSE connection exhausted, reporting error and exiting\");\r\n try {\r\n await fetch(`${tandemUrl}/api/channel-error`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n error: \"CHANNEL_CONNECT_FAILED\",\r\n message: `Channel shim lost connection after ${CHANNEL_MAX_RETRIES} retries.`,\r\n }),\r\n });\r\n } catch (reportErr) {\r\n console.error(\r\n \"[Channel] Could not report failure to server:\",\r\n reportErr instanceof Error ? reportErr.message : reportErr,\r\n );\r\n }\r\n process.exit(1);\r\n }\r\n\r\n await new Promise((r) => setTimeout(r, CHANNEL_RETRY_DELAY_MS));\r\n }\r\n }\r\n}\r\n\r\nasync function connectAndStream(\r\n mcp: Server,\r\n tandemUrl: string,\r\n lastEventId: string | undefined,\r\n onEventId: (id: string) => void,\r\n): Promise<void> {\r\n const headers: Record<string, string> = { Accept: \"text/event-stream\" };\r\n if (lastEventId) headers[\"Last-Event-ID\"] = lastEventId;\r\n\r\n const res = await fetch(`${tandemUrl}/api/events`, { headers });\r\n if (!res.ok) throw new Error(`SSE endpoint returned ${res.status}`);\r\n if (!res.body) throw new Error(\"SSE endpoint returned no body\");\r\n\r\n const reader = res.body.getReader();\r\n const decoder = new TextDecoder();\r\n let buffer = \"\";\r\n\r\n // Debounced awareness: only send the latest status after a quiet period\r\n let awarenessTimer: ReturnType<typeof setTimeout> | null = null;\r\n let pendingAwareness: TandemEvent | null = null;\r\n\r\n function flushAwareness() {\r\n if (!pendingAwareness) return;\r\n const event = pendingAwareness;\r\n pendingAwareness = null;\r\n fetch(`${tandemUrl}/api/channel-awareness`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n documentId: event.documentId,\r\n status: `processing: ${event.type}`,\r\n active: true,\r\n }),\r\n }).catch((err) => {\r\n console.error(\"[Channel] Awareness update failed:\", err instanceof Error ? err.message : err);\r\n });\r\n }\r\n\r\n function scheduleAwareness(event: TandemEvent) {\r\n pendingAwareness = event;\r\n if (awarenessTimer) clearTimeout(awarenessTimer);\r\n awarenessTimer = setTimeout(flushAwareness, AWARENESS_DEBOUNCE_MS);\r\n }\r\n\r\n while (true) {\r\n const { done, value } = await reader.read();\r\n if (done) throw new Error(\"SSE stream ended\");\r\n\r\n buffer += decoder.decode(value, { stream: true });\r\n\r\n let boundary: number;\r\n while ((boundary = buffer.indexOf(\"\\n\\n\")) !== -1) {\r\n const frame = buffer.slice(0, boundary);\r\n buffer = buffer.slice(boundary + 2);\r\n\r\n if (frame.startsWith(\":\")) continue;\r\n\r\n let eventId: string | undefined;\r\n let data: string | undefined;\r\n\r\n for (const line of frame.split(\"\\n\")) {\r\n if (line.startsWith(\"id: \")) eventId = line.slice(4);\r\n else if (line.startsWith(\"data: \")) data = line.slice(6);\r\n }\r\n\r\n if (!data) continue;\r\n\r\n let event: TandemEvent | null;\r\n try {\r\n event = parseTandemEvent(JSON.parse(data));\r\n } catch {\r\n console.error(\"[Channel] Malformed SSE event data (skipping):\", data.slice(0, 200));\r\n continue;\r\n }\r\n if (!event) {\r\n console.error(\"[Channel] Received invalid SSE event, skipping\");\r\n continue;\r\n }\r\n\r\n if (eventId) onEventId(eventId);\r\n\r\n try {\r\n await mcp.notification({\r\n method: \"notifications/claude/channel\",\r\n params: {\r\n content: formatEventContent(event),\r\n meta: formatEventMeta(event),\r\n },\r\n });\r\n } catch (err) {\r\n console.error(\"[Channel] MCP notification failed (transport broken?):\", err);\r\n throw err;\r\n }\r\n\r\n scheduleAwareness(event);\r\n }\r\n }\r\n}\r\n"],"mappings":";;;AAWA,SAAS,wBAAwB;AACjC,SAAS,cAAc;AACvB,SAAS,4BAA4B;AACrC,SAAS,uBAAuB,8BAA8B;AAC9D,SAAS,SAAS;;;ACdX,IAAM,mBAAmB;AAIzB,IAAM,gBAAgB,KAAK,OAAO;AAClC,IAAM,iBAAiB,KAAK,OAAO;AAEnC,IAAM,eAAe,KAAK,KAAK;AAC/B,IAAM,kBAAkB,KAAK,KAAK,KAAK,KAAK;AA0D5C,IAAM,sBAAsB;AAC5B,IAAM,yBAAyB;;;ACatC,IAAM,oBAAoB,oBAAI,IAAqB;AAAA,EACjD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAMM,SAAS,iBAAiB,KAAkC;AACjE,MACE,OAAO,QAAQ,YACf,QAAQ,QACR,EAAE,QAAQ,QACV,OAAQ,IAAgC,OAAO,YAC/C,EAAE,UAAU,QACZ,CAAC,kBAAkB,IAAK,IAAgC,IAAuB,KAC/E,EAAE,eAAe,QACjB,OAAQ,IAAgC,cAAc,YACtD,EAAE,aAAa,QACf,OAAQ,IAAgC,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,CAAC,QAAQ;AACpB,UAAQ,MAAM,0BAA0B,GAAG;AAC3C,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["mcp"]}
1
+ {"version":3,"sources":["../../src/channel/index.ts","../../src/shared/constants.ts","../../src/server/events/types.ts","../../src/channel/event-bridge.ts"],"sourcesContent":["#!/usr/bin/env node\r\n/**\r\n * Tandem Channel Shim — Claude Code spawns this as a subprocess.\r\n *\r\n * Bridges Tandem's SSE event stream → Claude Code channel notifications,\r\n * and exposes a `tandem_reply` tool for Claude to respond to chat messages.\r\n *\r\n * Uses the low-level MCP `Server` class (not `McpServer`) as required by\r\n * the Channels API spec.\r\n */\r\n\r\nimport { createConnection } from \"node:net\";\r\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\r\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\r\nimport { CallToolRequestSchema, ListToolsRequestSchema } from \"@modelcontextprotocol/sdk/types.js\";\r\nimport { z } from \"zod\";\r\nimport { DEFAULT_MCP_PORT } from \"../shared/constants.js\";\r\nimport { startEventBridge } from \"./event-bridge.js\";\r\n\r\n// stdout is the MCP wire — redirect console.log to stderr\r\nconsole.log = console.error;\r\nconsole.warn = console.error;\r\nconsole.info = console.error;\r\n\r\nconst TANDEM_URL = process.env.TANDEM_URL || \"http://localhost:3479\";\r\n\r\n// --- Pre-flight: verify Tandem server is reachable before MCP handshake ---\r\n\r\nasync function checkServerReachable(url: string, timeoutMs = 2000): Promise<boolean> {\r\n let parsed: URL;\r\n try {\r\n parsed = new URL(url);\r\n } catch {\r\n console.error(\r\n `[Channel] Invalid TANDEM_URL: \"${url}\" — expected format: http://localhost:3479`,\r\n );\r\n return false;\r\n }\r\n const port = parseInt(parsed.port || String(DEFAULT_MCP_PORT), 10);\r\n return new Promise((resolve) => {\r\n const socket = createConnection({ port, host: parsed.hostname }, () => {\r\n socket.destroy();\r\n resolve(true);\r\n });\r\n socket.setTimeout(timeoutMs);\r\n socket.on(\"timeout\", () => {\r\n socket.destroy();\r\n resolve(false);\r\n });\r\n socket.on(\"error\", (err) => {\r\n console.error(`[Channel] Server probe failed: ${err.message}`);\r\n socket.destroy();\r\n resolve(false);\r\n });\r\n });\r\n}\r\n\r\n// --- MCP Server setup ---\r\n\r\nconst mcp = new Server(\r\n { name: \"tandem-channel\", version: \"0.1.0\" },\r\n {\r\n capabilities: {\r\n experimental: {\r\n \"claude/channel\": {},\r\n \"claude/channel/permission\": {},\r\n },\r\n tools: {},\r\n },\r\n instructions: [\r\n 'Events from Tandem arrive as <channel source=\"tandem-channel\" event_type=\"...\" document_id=\"...\">.',\r\n \"These are real-time push notifications of user actions in the collaborative document editor.\",\r\n \"Event types: annotation:created, annotation:accepted, annotation:dismissed,\",\r\n \"chat:message, selection:changed, document:opened, document:closed, document:switched.\",\r\n \"Use your tandem MCP tools (tandem_getTextContent, tandem_comment, tandem_highlight, etc.) to act on them.\",\r\n \"Reply to chat messages using tandem_reply. Pass document_id from the tag attributes.\",\r\n \"Do not reply to non-chat events — just act on them using tools.\",\r\n \"If you haven't received channel notifications recently, call tandem_checkInbox as a fallback.\",\r\n ].join(\" \"),\r\n },\r\n);\r\n\r\n// --- Tool: tandem_reply (forwarded to Tandem HTTP server) ---\r\n\r\nmcp.setRequestHandler(ListToolsRequestSchema, async () => ({\r\n tools: [\r\n {\r\n name: \"tandem_reply\",\r\n description: \"Reply to a chat message in Tandem\",\r\n inputSchema: {\r\n type: \"object\" as const,\r\n properties: {\r\n text: { type: \"string\", description: \"The reply message\" },\r\n documentId: {\r\n type: \"string\",\r\n description: \"Document ID from the channel event (optional)\",\r\n },\r\n replyTo: {\r\n type: \"string\",\r\n description: \"Message ID being replied to (optional)\",\r\n },\r\n },\r\n required: [\"text\"],\r\n },\r\n },\r\n ],\r\n}));\r\n\r\nmcp.setRequestHandler(CallToolRequestSchema, async (req) => {\r\n if (req.params.name === \"tandem_reply\") {\r\n const args = req.params.arguments as Record<string, unknown>;\r\n try {\r\n const res = await fetch(`${TANDEM_URL}/api/channel-reply`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify(args),\r\n });\r\n let data: unknown;\r\n try {\r\n data = await res.json();\r\n } catch {\r\n data = { message: \"Non-JSON response\" };\r\n }\r\n if (!res.ok) {\r\n return {\r\n content: [\r\n {\r\n type: \"text\" as const,\r\n text: `Reply failed (${res.status}): ${JSON.stringify(data)}`,\r\n },\r\n ],\r\n isError: true,\r\n };\r\n }\r\n return { content: [{ type: \"text\" as const, text: JSON.stringify(data) }] };\r\n } catch (err) {\r\n return {\r\n content: [\r\n {\r\n type: \"text\" as const,\r\n text: `Failed to send reply: ${err instanceof Error ? err.message : String(err)}`,\r\n },\r\n ],\r\n isError: true,\r\n };\r\n }\r\n }\r\n throw new Error(`Unknown tool: ${req.params.name}`);\r\n});\r\n\r\n// --- Permission relay: forward Claude Code's tool approval prompts to Tandem browser ---\r\n\r\nconst PermissionRequestSchema = z.object({\r\n method: z.literal(\"notifications/claude/channel/permission_request\"),\r\n params: z.object({\r\n request_id: z.string(),\r\n tool_name: z.string(),\r\n description: z.string(),\r\n input_preview: z.string(),\r\n }),\r\n});\r\n\r\nmcp.setNotificationHandler(PermissionRequestSchema, async ({ params }) => {\r\n try {\r\n const res = await fetch(`${TANDEM_URL}/api/channel-permission`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n requestId: params.request_id,\r\n toolName: params.tool_name,\r\n description: params.description,\r\n inputPreview: params.input_preview,\r\n }),\r\n });\r\n if (!res.ok) {\r\n console.error(\r\n `[Channel] Permission relay got HTTP ${res.status} — browser may not see prompt`,\r\n );\r\n }\r\n } catch (err) {\r\n console.error(\"[Channel] Failed to forward permission request:\", err);\r\n }\r\n});\r\n\r\n// --- Connect and start ---\r\n\r\nasync function main() {\r\n console.error(`[Channel] Tandem channel shim starting (server: ${TANDEM_URL})`);\r\n\r\n const reachable = await checkServerReachable(TANDEM_URL);\r\n if (!reachable) {\r\n console.error(`[Channel] Cannot reach Tandem server at ${TANDEM_URL}`);\r\n console.error(\"[Channel] Start it with: npm run dev:standalone\");\r\n // Continue anyway — the event bridge will retry, and the server may start later\r\n }\r\n\r\n // Connect to Claude Code over stdio\r\n const transport = new StdioServerTransport();\r\n await mcp.connect(transport);\r\n console.error(\"[Channel] Connected to Claude Code via stdio\");\r\n\r\n // Start the SSE event bridge (runs until disconnected or max retries)\r\n startEventBridge(mcp, TANDEM_URL).catch((err) => {\r\n console.error(\"[Channel] Event bridge failed unexpectedly:\", err);\r\n process.exit(1);\r\n });\r\n}\r\n\r\nmain().catch((err) => {\r\n console.error(\"[Channel] Fatal error:\", err);\r\n process.exit(1);\r\n});\r\n","export const DEFAULT_WS_PORT = 3478;\r\nexport const DEFAULT_MCP_PORT = 3479;\r\n\r\n/** File extensions the server accepts for opening. */\r\nexport const SUPPORTED_EXTENSIONS = new Set([\".md\", \".txt\", \".html\", \".htm\", \".docx\"]);\r\nexport const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB\r\nexport const MAX_WS_PAYLOAD = 10 * 1024 * 1024; // 10MB\r\nexport const MAX_WS_CONNECTIONS = 4;\r\nexport const IDLE_TIMEOUT = 30 * 60 * 1000; // 30 minutes\r\nexport const SESSION_MAX_AGE = 30 * 24 * 60 * 60 * 1000; // 30 days\r\nexport const TYPING_DEBOUNCE = 3000; // 3 seconds\r\nexport const DISCONNECT_DEBOUNCE_MS = 3000; // 3 seconds before showing \"server not reachable\"\r\nexport const PROLONGED_DISCONNECT_MS = 30_000; // 30 seconds before showing App-level disconnect banner\r\nexport const OVERLAY_STALE_DEBOUNCE = 200; // 200ms\r\nexport const REVIEW_BANNER_THRESHOLD = 5;\r\n\r\nexport const HIGHLIGHT_COLORS: Record<string, string> = {\r\n yellow: \"rgba(255, 235, 59, 0.3)\",\r\n red: \"rgba(244, 67, 54, 0.3)\",\r\n green: \"rgba(76, 175, 80, 0.3)\",\r\n blue: \"rgba(33, 150, 243, 0.3)\",\r\n purple: \"rgba(156, 39, 176, 0.3)\",\r\n};\r\n\r\nexport const INTERRUPTION_MODE_DEFAULT = \"all\" as const;\r\nexport const INTERRUPTION_MODE_KEY = \"tandem:interruptionMode\";\r\n\r\n// Large file thresholds\r\nexport const CHARS_PER_PAGE = 3_000;\r\nexport const LARGE_FILE_PAGE_THRESHOLD = 50;\r\nexport const VERY_LARGE_FILE_PAGE_THRESHOLD = 100;\r\n\r\nexport const CLAUDE_PRESENCE_COLOR = \"#6366f1\";\r\nexport const CLAUDE_FOCUS_OPACITY = 0.1;\r\n\r\nexport const CTRL_ROOM = \"__tandem_ctrl__\";\r\n\r\n/** Y.Map key constants — centralized to prevent silent bugs from string typos. */\r\nexport const Y_MAP_ANNOTATIONS = \"annotations\";\r\nexport const Y_MAP_AWARENESS = \"awareness\";\r\nexport const Y_MAP_USER_AWARENESS = \"userAwareness\";\r\nexport const Y_MAP_CHAT = \"chat\";\r\nexport const Y_MAP_DOCUMENT_META = \"documentMeta\";\r\nexport const Y_MAP_SAVED_AT_VERSION = \"savedAtVersion\";\r\n\r\nexport const SERVER_INFO_DIR = \".tandem\";\r\nexport const SERVER_INFO_FILE = \".tandem/.server-info\";\r\n\r\nexport const RECENT_FILES_KEY = \"tandem:recentFiles\";\r\nexport const RECENT_FILES_CAP = 20;\r\n\r\nexport const USER_NAME_KEY = \"tandem:userName\";\r\nexport const USER_NAME_DEFAULT = \"You\";\r\n\r\n// Toast notifications\r\nexport const TOAST_DISMISS_MS = { error: 8000, warning: 6000, info: 4000 } as const;\r\nexport const MAX_VISIBLE_TOASTS = 5;\r\nexport const NOTIFICATION_BUFFER_SIZE = 50;\r\n\r\n// Onboarding tutorial\r\nexport const TUTORIAL_COMPLETED_KEY = \"tandem:tutorialCompleted\";\r\nexport const TUTORIAL_ANNOTATION_PREFIX = \"tutorial-\";\r\n\r\n// Channel / event queue\r\nexport const CHANNEL_EVENT_BUFFER_SIZE = 200;\r\nexport const CHANNEL_EVENT_BUFFER_AGE_MS = 60_000; // 60 seconds\r\nexport const CHANNEL_SSE_KEEPALIVE_MS = 15_000; // 15 seconds\r\nexport const CHANNEL_MAX_RETRIES = 5;\r\nexport const CHANNEL_RETRY_DELAY_MS = 2_000;\r\n","/**\r\n * Event types for the Tandem → Claude Code channel.\r\n *\r\n * These events flow from browser-originated Y.Map changes through an SSE\r\n * endpoint to the channel shim, which pushes them into Claude Code as\r\n * `notifications/claude/channel` messages.\r\n */\r\n\r\n// --- Per-event payload interfaces ---\r\n\r\nexport interface AnnotationCreatedPayload {\r\n annotationId: string;\r\n annotationType: string;\r\n content: string;\r\n textSnippet: string;\r\n}\r\n\r\nexport interface AnnotationAcceptedPayload {\r\n annotationId: string;\r\n textSnippet: string;\r\n}\r\n\r\nexport interface AnnotationDismissedPayload {\r\n annotationId: string;\r\n textSnippet: string;\r\n}\r\n\r\nexport interface ChatMessagePayload {\r\n messageId: string;\r\n text: string;\r\n replyTo: string | null;\r\n anchor: { from: number; to: number; textSnapshot: string } | null;\r\n}\r\n\r\nexport interface SelectionChangedPayload {\r\n from: number;\r\n to: number;\r\n selectedText: string;\r\n}\r\n\r\nexport interface DocumentOpenedPayload {\r\n fileName: string;\r\n format: string;\r\n}\r\n\r\nexport interface DocumentClosedPayload {\r\n fileName: string;\r\n}\r\n\r\nexport interface DocumentSwitchedPayload {\r\n fileName: string;\r\n}\r\n\r\n// --- Discriminated union ---\r\n\r\ninterface TandemEventBase {\r\n /** Timestamp-based unique ID for SSE `Last-Event-ID` reconnection. Format: `evt_<timestamp>_<rand>`. Roughly ordered but not strictly monotonic. */\r\n id: string;\r\n timestamp: number;\r\n /** Which document this event relates to (absent for global events). */\r\n documentId?: string;\r\n}\r\n\r\nexport type TandemEvent =\r\n | (TandemEventBase & { type: \"annotation:created\"; payload: AnnotationCreatedPayload })\r\n | (TandemEventBase & { type: \"annotation:accepted\"; payload: AnnotationAcceptedPayload })\r\n | (TandemEventBase & { type: \"annotation:dismissed\"; payload: AnnotationDismissedPayload })\r\n | (TandemEventBase & { type: \"chat:message\"; payload: ChatMessagePayload })\r\n | (TandemEventBase & { type: \"selection:changed\"; payload: SelectionChangedPayload })\r\n | (TandemEventBase & { type: \"document:opened\"; payload: DocumentOpenedPayload })\r\n | (TandemEventBase & { type: \"document:closed\"; payload: DocumentClosedPayload })\r\n | (TandemEventBase & { type: \"document:switched\"; payload: DocumentSwitchedPayload });\r\n\r\n/** Union of all event type discriminants. */\r\nexport type TandemEventType = TandemEvent[\"type\"];\r\n\r\n// Re-export from shared utils (single ID generation pattern)\r\nexport { generateEventId } from \"../../shared/utils.js\";\r\n\r\n// --- Parse guard for SSE consumers ---\r\n\r\nconst VALID_EVENT_TYPES = new Set<TandemEventType>([\r\n \"annotation:created\",\r\n \"annotation:accepted\",\r\n \"annotation:dismissed\",\r\n \"chat:message\",\r\n \"selection:changed\",\r\n \"document:opened\",\r\n \"document:closed\",\r\n \"document:switched\",\r\n]);\r\n\r\n/**\r\n * Validate a JSON-parsed value as a TandemEvent.\r\n * Used by the event-bridge to safely consume SSE data.\r\n */\r\nexport function parseTandemEvent(raw: unknown): TandemEvent | null {\r\n if (\r\n typeof raw !== \"object\" ||\r\n raw === null ||\r\n !(\"id\" in raw) ||\r\n typeof (raw as Record<string, unknown>).id !== \"string\" ||\r\n !(\"type\" in raw) ||\r\n !VALID_EVENT_TYPES.has((raw as Record<string, unknown>).type as TandemEventType) ||\r\n !(\"timestamp\" in raw) ||\r\n typeof (raw as Record<string, unknown>).timestamp !== \"number\" ||\r\n !(\"payload\" in raw) ||\r\n typeof (raw as Record<string, unknown>).payload !== \"object\"\r\n ) {\r\n return null;\r\n }\r\n return raw as TandemEvent;\r\n}\r\n\r\n/**\r\n * Convert a TandemEvent into a human-readable string for the channel `content` field.\r\n * Claude sees this text inside `<channel source=\"tandem-channel\">` tags.\r\n */\r\nexport function formatEventContent(event: TandemEvent): string {\r\n const doc = event.documentId ? ` [doc: ${event.documentId}]` : \"\";\r\n\r\n switch (event.type) {\r\n case \"annotation:created\": {\r\n const { annotationType, content, textSnippet } = event.payload;\r\n const snippet = textSnippet ? ` on \"${textSnippet}\"` : \"\";\r\n return `User created ${annotationType}${snippet}: ${content || \"(no content)\"}${doc}`;\r\n }\r\n case \"annotation:accepted\": {\r\n const { annotationId, textSnippet } = event.payload;\r\n return `User accepted annotation ${annotationId}${textSnippet ? ` (\"${textSnippet}\")` : \"\"}${doc}`;\r\n }\r\n case \"annotation:dismissed\": {\r\n const { annotationId, textSnippet } = event.payload;\r\n return `User dismissed annotation ${annotationId}${textSnippet ? ` (\"${textSnippet}\")` : \"\"}${doc}`;\r\n }\r\n case \"chat:message\": {\r\n const { text, replyTo } = event.payload;\r\n const reply = replyTo ? ` (replying to ${replyTo})` : \"\";\r\n return `User says${reply}: ${text}${doc}`;\r\n }\r\n case \"selection:changed\": {\r\n const { from, to, selectedText } = event.payload;\r\n if (!selectedText) return `User cleared selection${doc}`;\r\n return `User selected text (${from}-${to}): \"${selectedText}\"${doc}`;\r\n }\r\n case \"document:opened\": {\r\n const { fileName, format } = event.payload;\r\n return `User opened document: ${fileName} (${format})${doc}`;\r\n }\r\n case \"document:closed\": {\r\n const { fileName } = event.payload;\r\n return `User closed document: ${fileName}${doc}`;\r\n }\r\n case \"document:switched\": {\r\n const { fileName } = event.payload;\r\n return `User switched to document: ${fileName}${doc}`;\r\n }\r\n default: {\r\n const _exhaustive: never = event;\r\n return `Unknown event${doc}`;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Build the `meta` record for a channel notification.\r\n * Keys use underscores only (Channels API silently drops hyphenated keys).\r\n */\r\nexport function formatEventMeta(event: TandemEvent): Record<string, string> {\r\n const meta: Record<string, string> = {\r\n event_type: event.type,\r\n };\r\n if (event.documentId) meta.document_id = event.documentId;\r\n\r\n switch (event.type) {\r\n case \"annotation:created\":\r\n case \"annotation:accepted\":\r\n case \"annotation:dismissed\":\r\n meta.annotation_id = event.payload.annotationId;\r\n break;\r\n case \"chat:message\":\r\n meta.message_id = event.payload.messageId;\r\n break;\r\n }\r\n\r\n return meta;\r\n}\r\n","/**\r\n * SSE event bridge: connects to Tandem server's /api/events endpoint\r\n * and pushes received events to Claude Code as channel notifications.\r\n */\r\n\r\nimport type { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\r\nimport type { TandemEvent } from \"../server/events/types.js\";\r\nimport { formatEventContent, formatEventMeta, parseTandemEvent } from \"../server/events/types.js\";\r\nimport { CHANNEL_MAX_RETRIES, CHANNEL_RETRY_DELAY_MS } from \"../shared/constants.js\";\r\n\r\nconst AWARENESS_DEBOUNCE_MS = 500;\r\nconst SELECTION_DEBOUNCE_MS = 1500;\r\n\r\nexport async function startEventBridge(mcp: Server, tandemUrl: string): Promise<void> {\r\n let retries = 0;\r\n let lastEventId: string | undefined;\r\n\r\n while (retries < CHANNEL_MAX_RETRIES) {\r\n try {\r\n await connectAndStream(mcp, tandemUrl, lastEventId, (id) => {\r\n lastEventId = id;\r\n retries = 0;\r\n });\r\n } catch (err) {\r\n retries++;\r\n console.error(\r\n `[Channel] SSE connection failed (${retries}/${CHANNEL_MAX_RETRIES}):`,\r\n err instanceof Error ? err.message : err,\r\n );\r\n\r\n if (retries >= CHANNEL_MAX_RETRIES) {\r\n console.error(\"[Channel] SSE connection exhausted, reporting error and exiting\");\r\n try {\r\n await fetch(`${tandemUrl}/api/channel-error`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n error: \"CHANNEL_CONNECT_FAILED\",\r\n message: `Channel shim lost connection after ${CHANNEL_MAX_RETRIES} retries.`,\r\n }),\r\n });\r\n } catch (reportErr) {\r\n console.error(\r\n \"[Channel] Could not report failure to server:\",\r\n reportErr instanceof Error ? reportErr.message : reportErr,\r\n );\r\n }\r\n process.exit(1);\r\n }\r\n\r\n await new Promise((r) => setTimeout(r, CHANNEL_RETRY_DELAY_MS));\r\n }\r\n }\r\n}\r\n\r\nasync function connectAndStream(\r\n mcp: Server,\r\n tandemUrl: string,\r\n lastEventId: string | undefined,\r\n onEventId: (id: string) => void,\r\n): Promise<void> {\r\n const headers: Record<string, string> = { Accept: \"text/event-stream\" };\r\n if (lastEventId) headers[\"Last-Event-ID\"] = lastEventId;\r\n\r\n const res = await fetch(`${tandemUrl}/api/events`, { headers });\r\n if (!res.ok) throw new Error(`SSE endpoint returned ${res.status}`);\r\n if (!res.body) throw new Error(\"SSE endpoint returned no body\");\r\n\r\n const reader = res.body.getReader();\r\n const decoder = new TextDecoder();\r\n let buffer = \"\";\r\n\r\n // Debounced awareness: only send the latest status after a quiet period\r\n let awarenessTimer: ReturnType<typeof setTimeout> | null = null;\r\n let pendingAwareness: TandemEvent | null = null;\r\n\r\n function flushAwareness() {\r\n if (!pendingAwareness) return;\r\n const event = pendingAwareness;\r\n pendingAwareness = null;\r\n fetch(`${tandemUrl}/api/channel-awareness`, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n documentId: event.documentId,\r\n status: `processing: ${event.type}`,\r\n active: true,\r\n }),\r\n }).catch((err) => {\r\n console.error(\"[Channel] Awareness update failed:\", err instanceof Error ? err.message : err);\r\n });\r\n }\r\n\r\n function scheduleAwareness(event: TandemEvent) {\r\n pendingAwareness = event;\r\n if (awarenessTimer) clearTimeout(awarenessTimer);\r\n awarenessTimer = setTimeout(flushAwareness, AWARENESS_DEBOUNCE_MS);\r\n }\r\n\r\n // Debounced selection: coalesce rapid selection changes, skip cleared selections\r\n let selectionTimer: ReturnType<typeof setTimeout> | null = null;\r\n let pendingSelection: { event: TandemEvent; eventId?: string } | null = null;\r\n let transportBroken = false;\r\n\r\n async function flushSelection() {\r\n if (!pendingSelection) return;\r\n const { event, eventId } = pendingSelection;\r\n pendingSelection = null;\r\n if (eventId) onEventId(eventId);\r\n try {\r\n await mcp.notification({\r\n method: \"notifications/claude/channel\",\r\n params: {\r\n content: formatEventContent(event),\r\n meta: formatEventMeta(event),\r\n },\r\n });\r\n } catch (err) {\r\n console.error(\"[Channel] MCP notification failed (transport broken?):\", err);\r\n transportBroken = true;\r\n return;\r\n }\r\n scheduleAwareness(event);\r\n }\r\n\r\n function isSelectionCleared(event: TandemEvent): boolean {\r\n const p = event.payload as { from?: number; to?: number; selectedText?: string } | undefined;\r\n return !p || (p.from === p.to && !p.selectedText);\r\n }\r\n\r\n while (true) {\r\n if (transportBroken) throw new Error(\"MCP transport broken (detected in debounced flush)\");\r\n const { done, value } = await reader.read();\r\n if (done) throw new Error(\"SSE stream ended\");\r\n\r\n buffer += decoder.decode(value, { stream: true });\r\n\r\n let boundary: number;\r\n while ((boundary = buffer.indexOf(\"\\n\\n\")) !== -1) {\r\n const frame = buffer.slice(0, boundary);\r\n buffer = buffer.slice(boundary + 2);\r\n\r\n if (frame.startsWith(\":\")) continue;\r\n\r\n let eventId: string | undefined;\r\n let data: string | undefined;\r\n\r\n for (const line of frame.split(\"\\n\")) {\r\n if (line.startsWith(\"id: \")) eventId = line.slice(4);\r\n else if (line.startsWith(\"data: \")) data = line.slice(6);\r\n }\r\n\r\n if (!data) continue;\r\n\r\n let event: TandemEvent | null;\r\n try {\r\n event = parseTandemEvent(JSON.parse(data));\r\n } catch {\r\n console.error(\"[Channel] Malformed SSE event data (skipping):\", data.slice(0, 200));\r\n continue;\r\n }\r\n if (!event) {\r\n console.error(\"[Channel] Received invalid SSE event, skipping\");\r\n continue;\r\n }\r\n\r\n // Selection events: drop cleared selections, debounce the rest\r\n if (event.type === \"selection:changed\") {\r\n if (eventId) onEventId(eventId);\r\n if (isSelectionCleared(event)) continue; // silently drop\r\n pendingSelection = { event, eventId };\r\n if (selectionTimer) clearTimeout(selectionTimer);\r\n selectionTimer = setTimeout(flushSelection, SELECTION_DEBOUNCE_MS);\r\n continue;\r\n }\r\n\r\n if (eventId) onEventId(eventId);\r\n\r\n try {\r\n await mcp.notification({\r\n method: \"notifications/claude/channel\",\r\n params: {\r\n content: formatEventContent(event),\r\n meta: formatEventMeta(event),\r\n },\r\n });\r\n } catch (err) {\r\n console.error(\"[Channel] MCP notification failed (transport broken?):\", err);\r\n throw err;\r\n }\r\n\r\n scheduleAwareness(event);\r\n }\r\n }\r\n}\r\n"],"mappings":";;;AAWA,SAAS,wBAAwB;AACjC,SAAS,cAAc;AACvB,SAAS,4BAA4B;AACrC,SAAS,uBAAuB,8BAA8B;AAC9D,SAAS,SAAS;;;ACdX,IAAM,mBAAmB;AAIzB,IAAM,gBAAgB,KAAK,OAAO;AAClC,IAAM,iBAAiB,KAAK,OAAO;AAEnC,IAAM,eAAe,KAAK,KAAK;AAC/B,IAAM,kBAAkB,KAAK,KAAK,KAAK,KAAK;AA0D5C,IAAM,sBAAsB;AAC5B,IAAM,yBAAyB;;;ACatC,IAAM,oBAAoB,oBAAI,IAAqB;AAAA,EACjD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAMM,SAAS,iBAAiB,KAAkC;AACjE,MACE,OAAO,QAAQ,YACf,QAAQ,QACR,EAAE,QAAQ,QACV,OAAQ,IAAgC,OAAO,YAC/C,EAAE,UAAU,QACZ,CAAC,kBAAkB,IAAK,IAAgC,IAAuB,KAC/E,EAAE,eAAe,QACjB,OAAQ,IAAgC,cAAc,YACtD,EAAE,aAAa,QACf,OAAQ,IAAgC,YAAY,UACpD;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAMO,SAAS,mBAAmB,OAA4B;AAC7D,QAAM,MAAM,MAAM,aAAa,UAAU,MAAM,UAAU,MAAM;AAE/D,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK,sBAAsB;AACzB,YAAM,EAAE,gBAAgB,SAAS,YAAY,IAAI,MAAM;AACvD,YAAM,UAAU,cAAc,QAAQ,WAAW,MAAM;AACvD,aAAO,gBAAgB,cAAc,GAAG,OAAO,KAAK,WAAW,cAAc,GAAG,GAAG;AAAA,IACrF;AAAA,IACA,KAAK,uBAAuB;AAC1B,YAAM,EAA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