@agentproto/apps 0.8.0 → 0.9.0

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.
Files changed (60) hide show
  1. package/README.md +29 -1
  2. package/dist/agents-overview/index.d.ts +54 -0
  3. package/dist/agents-overview/index.d.ts.map +1 -0
  4. package/dist/agents-overview/panel.d.ts +14 -0
  5. package/dist/agents-overview/panel.d.ts.map +1 -0
  6. package/dist/agents-overview/panel.mjs +306 -0
  7. package/dist/agents-overview/panel.mjs.map +1 -0
  8. package/dist/agents-overview.mjs +336 -0
  9. package/dist/agents-overview.mjs.map +1 -0
  10. package/dist/bin/sync.mjs +25 -11
  11. package/dist/bin/sync.mjs.map +1 -1
  12. package/dist/bureau-sessions/index.d.ts +52 -0
  13. package/dist/bureau-sessions/index.d.ts.map +1 -0
  14. package/dist/bureau-sessions/panel.d.ts +10 -0
  15. package/dist/bureau-sessions/panel.d.ts.map +1 -0
  16. package/dist/bureau-sessions/panel.mjs +297 -0
  17. package/dist/bureau-sessions/panel.mjs.map +1 -0
  18. package/dist/bureau-sessions.mjs +329 -0
  19. package/dist/bureau-sessions.mjs.map +1 -0
  20. package/dist/content-team/workflows/produce-content.d.ts +14 -2
  21. package/dist/content-team/workflows/produce-content.d.ts.map +1 -1
  22. package/dist/content-team/workflows/produce-cover.d.ts +13 -1
  23. package/dist/content-team/workflows/produce-cover.d.ts.map +1 -1
  24. package/dist/content-team.mjs +25 -11
  25. package/dist/content-team.mjs.map +1 -1
  26. package/dist/index.d.ts +24 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.mjs +2635 -12
  29. package/dist/index.mjs.map +1 -1
  30. package/dist/live-session/index.d.ts +46 -0
  31. package/dist/live-session/index.d.ts.map +1 -0
  32. package/dist/live-session/logic.d.ts +125 -0
  33. package/dist/live-session/logic.d.ts.map +1 -0
  34. package/dist/live-session/panel.d.ts +61 -0
  35. package/dist/live-session/panel.d.ts.map +1 -0
  36. package/dist/live-session/panel.mjs +993 -0
  37. package/dist/live-session/panel.mjs.map +1 -0
  38. package/dist/live-session.mjs +1032 -0
  39. package/dist/live-session.mjs.map +1 -0
  40. package/dist/mcp-app-types.d.ts +33 -0
  41. package/dist/mcp-app-types.d.ts.map +1 -0
  42. package/dist/panel-bridge.d.ts +38 -0
  43. package/dist/panel-bridge.d.ts.map +1 -0
  44. package/dist/session-story/index.d.ts +66 -0
  45. package/dist/session-story/index.d.ts.map +1 -0
  46. package/dist/session-story/panel.d.ts +31 -0
  47. package/dist/session-story/panel.d.ts.map +1 -0
  48. package/dist/session-story/panel.mjs +970 -0
  49. package/dist/session-story/panel.mjs.map +1 -0
  50. package/dist/session-story.mjs +1002 -0
  51. package/dist/session-story.mjs.map +1 -0
  52. package/dist/sessions-panel/index.d.ts +51 -0
  53. package/dist/sessions-panel/index.d.ts.map +1 -0
  54. package/dist/sessions-panel/panel.d.ts +20 -0
  55. package/dist/sessions-panel/panel.d.ts.map +1 -0
  56. package/dist/sessions-panel/panel.mjs +511 -0
  57. package/dist/sessions-panel/panel.mjs.map +1 -0
  58. package/dist/sessions-panel.mjs +545 -0
  59. package/dist/sessions-panel.mjs.map +1 -0
  60. package/package.json +57 -6
@@ -0,0 +1,511 @@
1
+ /**
2
+ * @agentproto/apps v0.1.0
3
+ * Ready-made agentproto apps (teams of agents + workflows) built on app-kit.
4
+ */
5
+
6
+ // src/panel-bridge.ts
7
+ function panelBridgeScript(appName) {
8
+ return `// \u2500\u2500 MCP Apps bridge (shared: panel-bridge.ts) \u2500\u2500
9
+ // JSON-RPC 2.0 over window.parent.postMessage \xB7 spec 2026-01-26
10
+ var _nextId = 1, _pending = {}, _notifyHandlers = [];
11
+ var _hostContext = null, _hostContextHandlers = [];
12
+ function post(msg){ window.parent.postMessage(msg, '*'); }
13
+ function getHostContext(){ return _hostContext; }
14
+ function onHostContext(cb){
15
+ _hostContextHandlers.push(cb);
16
+ // Replay the last context so a late subscriber isn't stuck blind.
17
+ if (_hostContext){ try { cb(_hostContext); } catch(_) {} }
18
+ }
19
+ function _setHostContext(ctx){
20
+ if (!ctx || typeof ctx !== 'object') return;
21
+ // ui/notifications/host-context-changed carries only the changed keys \u2014
22
+ // merge, matching the official ext-apps App behaviour.
23
+ _hostContext = Object.assign({}, _hostContext || {}, ctx);
24
+ for (var i = 0; i < _hostContextHandlers.length; i++){
25
+ try { _hostContextHandlers[i](_hostContext); } catch(_) {}
26
+ }
27
+ }
28
+ function rpcRequest(method, params){
29
+ return new Promise(function(resolve, reject){
30
+ var id = _nextId++;
31
+ _pending[id] = {resolve: resolve, reject: reject};
32
+ post({jsonrpc: '2.0', id: id, method: method, params: params || {}});
33
+ });
34
+ }
35
+ function rpcNotify(method, params){ post({jsonrpc: '2.0', method: method, params: params || {}}); }
36
+ function onHostNotification(cb){ _notifyHandlers.push(cb); }
37
+ window.addEventListener('message', function(evt){
38
+ var msg = evt.data;
39
+ if (!msg || typeof msg !== 'object' || msg.jsonrpc !== '2.0') return;
40
+ if (msg.id != null && msg.method == null){
41
+ var p = _pending[msg.id];
42
+ if (!p) return;
43
+ delete _pending[msg.id];
44
+ if (msg.error) p.reject(new Error(msg.error.message || ('rpc error ' + msg.error.code)));
45
+ else p.resolve(msg.result);
46
+ return;
47
+ }
48
+ if (msg.method){
49
+ if (msg.method === 'ui/notifications/host-context-changed'){
50
+ _setHostContext(msg.params || {});
51
+ }
52
+ for (var i = 0; i < _notifyHandlers.length; i++){
53
+ try { _notifyHandlers[i](msg.method, msg.params || {}); } catch(_) {}
54
+ }
55
+ }
56
+ });
57
+ function initBridge(){
58
+ return rpcRequest('ui/initialize', {
59
+ appInfo: {name: ${JSON.stringify(appName)}, version: '0.1.0'},
60
+ appCapabilities: {availableDisplayModes: ['inline', 'fullscreen', 'pip']},
61
+ protocolVersion: '2026-01-26'
62
+ }).then(function(result){
63
+ // The initialize result carries the initial hostContext (displayMode +
64
+ // availableDisplayModes) \u2014 capture it before notifying the host.
65
+ if (result && result.hostContext) _setHostContext(result.hostContext);
66
+ rpcNotify('ui/notifications/initialized', {});
67
+ });
68
+ }
69
+ function requestDisplayMode(mode){
70
+ return rpcRequest('ui/request-display-mode', {mode: mode});
71
+ }
72
+ function callTool(name, args){
73
+ return rpcRequest('tools/call', {name: name, arguments: args || {}}).then(function(result){
74
+ if (result.isError){
75
+ var e = (result.content && result.content[0] && result.content[0].text) || 'tool error';
76
+ throw new Error(e);
77
+ }
78
+ var text = (result.content && result.content[0] && result.content[0].text) || '{}';
79
+ return JSON.parse(text);
80
+ });
81
+ }
82
+
83
+ // \u2500\u2500 Display-mode toggle buttons (NO auto-request) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
84
+ // Injected by the shared bridge so every panel gets them without touching
85
+ // its own markup. Mirrors guilde canvas.app.ts canvasShellHtml(): the
86
+ // panel stays inline by default; the user expands on demand. Buttons only
87
+ // appear for modes the host advertises in hostContext.availableDisplayModes.
88
+ (function(){
89
+ function mount(){
90
+ var style = document.createElement('style');
91
+ style.textContent = '#dm,#pin{display:none;position:fixed;top:8px;z-index:10;'
92
+ + 'border:1px solid #d0d0d0;background:#fff;color:#1a1a1a;'
93
+ + 'font:600 13px/1 system-ui,sans-serif;padding:7px 12px;border-radius:6px;'
94
+ + 'cursor:pointer;box-shadow:0 1px 4px rgba(0,0,0,.18)}'
95
+ + '#dm{right:8px}#pin{right:118px}'
96
+ + '#dm:hover,#pin:hover{background:#f2f2f2;border-color:#b0b0b0}'
97
+ + '@media (prefers-color-scheme:dark){'
98
+ + '#dm,#pin{border-color:#555;background:#2a2a2a;color:#f0f0f0;box-shadow:0 1px 4px rgba(0,0,0,.5)}'
99
+ + '#dm:hover,#pin:hover{background:#333;border-color:#777}}';
100
+ document.head.appendChild(style);
101
+
102
+ var btn = document.createElement('button');
103
+ btn.id = 'dm'; btn.type = 'button'; btn.title = "Basculer l'affichage";
104
+ var pin = document.createElement('button');
105
+ pin.id = 'pin'; pin.type = 'button'; pin.title = '\xC9pingler sur le c\xF4t\xE9 (pip)';
106
+ document.body.appendChild(pin);
107
+ document.body.appendChild(btn);
108
+
109
+ function has(avail, m){ return !!avail && avail.indexOf(m) >= 0; }
110
+
111
+ // Re-sync button visibility + label from the current host context.
112
+ function syncBtn(ctx){
113
+ ctx = ctx || {};
114
+ var avail = ctx.availableDisplayModes;
115
+ // Diagnostic: what does THIS host actually advertise? (inline/fullscreen/pip)
116
+ console.log('[mcp-app] displayMode=', ctx.displayMode,
117
+ 'availableDisplayModes=', avail);
118
+
119
+ // Fullscreen toggle button.
120
+ if (has(avail, 'fullscreen')){
121
+ btn.style.display = 'block';
122
+ btn.textContent = (ctx.displayMode === 'fullscreen') ? '\u2921 R\xE9duire' : '\u2922 Agrandir';
123
+ } else { btn.style.display = 'none'; }
124
+
125
+ // Dedicated pip ("pinned on side") button \u2014 only if the host advertises pip.
126
+ if (has(avail, 'pip')){
127
+ pin.style.display = 'block';
128
+ pin.textContent = (ctx.displayMode === 'pip') ? '\u2921 D\xE9tacher' : '\u{1F4CC} \xC9pingler';
129
+ } else { pin.style.display = 'none'; }
130
+ }
131
+
132
+ onHostContext(syncBtn);
133
+
134
+ btn.addEventListener('click', function(){
135
+ var ctx = getHostContext() || {};
136
+ var inPanel = (ctx.displayMode === 'fullscreen' || ctx.displayMode === 'pip');
137
+ requestDisplayMode(inPanel ? 'inline' : 'fullscreen').catch(function(){});
138
+ });
139
+
140
+ pin.addEventListener('click', function(){
141
+ var ctx = getHostContext() || {};
142
+ requestDisplayMode(ctx.displayMode === 'pip' ? 'inline' : 'pip').catch(function(){});
143
+ });
144
+ }
145
+ if (document.body) mount();
146
+ else document.addEventListener('DOMContentLoaded', mount);
147
+ })();`;
148
+ }
149
+
150
+ // src/sessions-panel/panel.ts
151
+ var PANEL_HTML = `<!DOCTYPE html>
152
+ <html lang="en">
153
+ <head>
154
+ <meta charset="UTF-8">
155
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
156
+ <title>agentproto sessions</title>
157
+ <style>
158
+ *{box-sizing:border-box;margin:0;padding:0}
159
+ :root{
160
+ --bg:#0d1117;--bg2:#161b22;--bg3:#21262d;--border:#30363d;
161
+ --text:#e6edf3;--text2:#8b949e;
162
+ --green:#3fb950;--yellow:#d29922;--red:#f85149;--blue:#58a6ff;--purple:#bc8cff;
163
+ }
164
+ html,body{height:100%;font-family:Menlo,Monaco,'Courier New',monospace;font-size:13px;background:var(--bg);color:var(--text);overflow:hidden}
165
+ #app{display:flex;height:100%}
166
+ #sidebar{width:220px;min-width:180px;background:var(--bg2);border-right:1px solid var(--border);display:flex;flex-direction:column;flex-shrink:0}
167
+ #sidebar-hdr{padding:10px 12px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between}
168
+ #sidebar-hdr h1{font-size:11px;font-weight:600;color:var(--text2);text-transform:uppercase;letter-spacing:.06em}
169
+ #refresh-btn{background:none;border:none;cursor:pointer;color:var(--text2);font-size:15px;line-height:1;padding:2px 4px;border-radius:4px;font-family:inherit}
170
+ #refresh-btn:hover{color:var(--text);background:var(--bg3)}
171
+ #session-list{flex:1;overflow-y:auto;padding:4px 0}
172
+ .si{padding:8px 12px;cursor:pointer;border-left:2px solid transparent}
173
+ .si:hover{background:var(--bg3)}
174
+ .si.active{background:var(--bg3);border-left-color:var(--blue)}
175
+ .sn{font-size:12px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
176
+ .sm{font-size:11px;color:var(--text2);margin-top:2px;display:flex;gap:6px;align-items:center}
177
+ .badge{display:inline-block;padding:1px 5px;border-radius:10px;font-size:10px;font-weight:600}
178
+ .br{background:rgba(63,185,80,.15);color:var(--green)}
179
+ .bs{background:rgba(88,166,255,.15);color:var(--blue)}
180
+ .be{background:rgba(139,148,158,.1);color:var(--text2)}
181
+ .bk{background:rgba(248,81,73,.1);color:var(--red)}
182
+ .berr{background:rgba(248,81,73,.2);color:var(--red)}
183
+ #main{flex:1;display:flex;flex-direction:column;overflow:hidden;min-width:0}
184
+ #toolbar{padding:8px 12px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:8px;background:var(--bg2)}
185
+ #session-title{font-size:12px;font-weight:500;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
186
+ .abtn{background:var(--bg3);border:1px solid var(--border);color:var(--text);padding:4px 10px;border-radius:4px;cursor:pointer;font-size:12px;font-family:inherit}
187
+ .abtn:hover{background:var(--border)}
188
+ .abtn:disabled{opacity:.4;cursor:default}
189
+ .abtn.danger{border-color:var(--red);color:var(--red)}
190
+ .abtn.danger:hover{background:rgba(248,81,73,.1)}
191
+ #output{flex:1;overflow-y:auto;padding:8px 12px;background:var(--bg);font-size:12px;line-height:1.6}
192
+ .line{white-space:pre-wrap;word-break:break-all}
193
+ #empty{display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;color:var(--text2);gap:8px;text-align:center;padding:24px}
194
+ #empty .ico{font-size:32px}
195
+ #statusbar{padding:4px 12px;font-size:11px;color:var(--text2);border-top:1px solid var(--border);background:var(--bg2);flex-shrink:0}
196
+ .errmsg{color:var(--red);padding:12px}
197
+ /* ANSI SGR */
198
+ .ab{font-weight:bold}.ad{opacity:.6}.ai{font-style:italic}.au{text-decoration:underline}
199
+ .f0{color:#21262d}.f1{color:#f85149}.f2{color:#3fb950}.f3{color:#d29922}
200
+ .f4{color:#58a6ff}.f5{color:#bc8cff}.f6{color:#39d353}.f7{color:#e6edf3}
201
+ .f8{color:#8b949e}.f9{color:#ff7b72}.f10{color:#56d364}.f11{color:#e3b341}
202
+ .f12{color:#79c0ff}.f13{color:#d2a8ff}.f14{color:#56d364}.f15{color:#f0f6fc}
203
+ </style>
204
+ </head>
205
+ <body>
206
+ <div id="app">
207
+ <div id="sidebar">
208
+ <div id="sidebar-hdr">
209
+ <h1>Sessions</h1>
210
+ <button id="refresh-btn" title="Refresh" onclick="doRefresh()">&#8635;</button>
211
+ </div>
212
+ <div id="session-list"></div>
213
+ </div>
214
+ <div id="main">
215
+ <div id="toolbar" style="display:none">
216
+ <span id="session-title"></span>
217
+ <button class="abtn danger" id="kill-btn" onclick="doKill()">Kill</button>
218
+ </div>
219
+ <div id="output">
220
+ <div id="empty"><div class="ico">&#9889;</div><div>Select a session</div></div>
221
+ </div>
222
+ <div id="statusbar">Connecting to bridge&#8230;</div>
223
+ </div>
224
+ </div>
225
+ <script>
226
+ ${panelBridgeScript("agentproto-sessions-panel")}
227
+
228
+ // ============================================================
229
+ // ANSI-to-HTML renderer (SGR codes: colors 0-15, bold/dim/italic/underline)
230
+ // ============================================================
231
+
232
+ function escHtml(s) {
233
+ return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
234
+ }
235
+
236
+ function ansiToHtml(raw) {
237
+ // Strip CR overwrite sequences (progress bars: CR without LF)
238
+ var text = raw.replace(/[^\\n]*\\r([^\\n])/g, '$1').replace(/\\r/g, '');
239
+ var bold = false, dim = false, italic = false, underline = false, fg = -1;
240
+ var out = '';
241
+ // Split on ESC [ ... m (SGR) sequences; keep delimiters
242
+ var ESC = '\x1B';
243
+ var parts = text.split(/([\x1B]\\[[0-9;]*m)/);
244
+ for (var pi = 0; pi < parts.length; pi++) {
245
+ var part = parts[pi];
246
+ if (part.length === 0) continue;
247
+ if (part.charCodeAt(0) === 0x1b && part[1] === '[') {
248
+ // Parse SGR parameters
249
+ var inner = part.slice(2, part.length - 1); // strip ESC[ and m
250
+ var codes = inner === '' ? [0] : inner.split(';').map(Number);
251
+ for (var ci = 0; ci < codes.length; ci++) {
252
+ var c = codes[ci];
253
+ if (c === 0) { bold = false; dim = false; italic = false; underline = false; fg = -1; }
254
+ else if (c === 1) bold = true;
255
+ else if (c === 2) dim = true;
256
+ else if (c === 3) italic = true;
257
+ else if (c === 4) underline = true;
258
+ else if (c === 22) { bold = false; dim = false; }
259
+ else if (c === 23) italic = false;
260
+ else if (c === 24) underline = false;
261
+ else if (c >= 30 && c <= 37) fg = c - 30;
262
+ else if (c === 39) fg = -1;
263
+ else if (c >= 90 && c <= 97) fg = c - 90 + 8;
264
+ }
265
+ } else {
266
+ // Text segment \u2014 strip any remaining non-printable ESC sequences
267
+ var safe = escHtml(part).replace(/\x1B\\[[^m]*[A-Za-z]/g, '');
268
+ if (safe === '') continue;
269
+ var cls = '';
270
+ if (bold) cls += ' ab';
271
+ if (dim) cls += ' ad';
272
+ if (italic) cls += ' ai';
273
+ if (underline) cls += ' au';
274
+ if (fg >= 0 && fg <= 15) cls += ' f' + fg;
275
+ cls = cls.trim();
276
+ out += cls ? '<span class="' + cls + '">' + safe + '</span>' : safe;
277
+ }
278
+ }
279
+ return out;
280
+ }
281
+
282
+ // ============================================================
283
+ // App state
284
+ // ============================================================
285
+
286
+ var sessions = [];
287
+ var activeId = null;
288
+ var outputLines = [];
289
+ var nextCursor = 0;
290
+ var pollTimer = null;
291
+ var pollActive = false;
292
+
293
+ function setStatus(msg) {
294
+ document.getElementById('statusbar').textContent = msg;
295
+ }
296
+
297
+ // ============================================================
298
+ // Session list
299
+ // ============================================================
300
+
301
+ function badgeClass(status) {
302
+ if (status === 'running') return 'br';
303
+ if (status === 'starting') return 'bs';
304
+ if (status === 'killed') return 'bk';
305
+ if (status === 'error') return 'berr';
306
+ return 'be'; // exited
307
+ }
308
+
309
+ // Derive a turn-aware display badge. A session's 'status' only tracks process
310
+ // liveness, so an agent-cli process stays "running" while idle between turns.
311
+ // Read 'busy' / 'awaitingInput' to show real activity instead:
312
+ // working \u2014 a turn is in flight (busy)
313
+ // waiting \u2014 turn ended on awaiting-input (needs a reply)
314
+ // idle \u2014 process alive, no turn running (last turn done)
315
+ function displayBadge(s) {
316
+ if (s.status === 'running' && s.kind === 'agent-cli') {
317
+ if (s.busy) return { label: 'working', cls: 'br' };
318
+ if (s.awaitingInput) return { label: 'waiting', cls: 'bs' };
319
+ return { label: 'idle', cls: 'be' };
320
+ }
321
+ return { label: s.status, cls: badgeClass(s.status) };
322
+ }
323
+
324
+ function renderSidebar() {
325
+ var el = document.getElementById('session-list');
326
+ if (sessions.length === 0) {
327
+ el.innerHTML = '<div style="padding:12px;color:var(--text2);font-size:11px">No sessions</div>';
328
+ return;
329
+ }
330
+ var html = '';
331
+ for (var i = 0; i < sessions.length; i++) {
332
+ var s = sessions[i];
333
+ var label = s.label || s.name || (s.command ? s.command.split('/').pop() : null) || s.id.slice(0, 8);
334
+ var db = displayBadge(s);
335
+ var active = s.id === activeId ? ' active' : '';
336
+ // blockedOn (set while the turn waits on a spawned sub-agent or a
337
+ // shell command) rides next to the status badge; waiting-on-user is
338
+ // NOT here \u2014 that's awaitingInput, a different signal.
339
+ var blocked = '';
340
+ if (s.blockedOn === 'subagent') blocked = '<span class="badge bs">&#129513; sous-agent</span>';
341
+ else if (s.blockedOn === 'command') blocked = '<span class="badge bs">&#9203; commande</span>';
342
+ html += '<div class="si' + active + '" onclick="selectSession(\\'' + s.id + '\\')">'
343
+ + '<div class="sn">' + escHtml(label) + '</div>'
344
+ + '<div class="sm">'
345
+ + '<span class="badge ' + db.cls + '">' + db.label + '</span>'
346
+ + blocked
347
+ + '<span>' + escHtml(s.kind || '') + '</span>'
348
+ + '</div></div>';
349
+ }
350
+ el.innerHTML = html;
351
+ }
352
+
353
+ function loadSessions() {
354
+ // {kind:'all'} is session_list's live-able default \u2014 agent-CLI + terminal/
355
+ // PTY only. kind:"command" rows (a shell-execution log, not a resumable
356
+ // session) are excluded unless includeCommands is explicitly passed.
357
+ return callTool('session_list', {kind: 'all'}).then(function(data) {
358
+ sessions = data.sessions || [];
359
+ renderSidebar();
360
+ setStatus(sessions.length + ' session' + (sessions.length === 1 ? '' : 's') + ' \xB7 ' + new Date().toLocaleTimeString());
361
+ }).catch(function(e) {
362
+ setStatus('Error: ' + e.message);
363
+ });
364
+ }
365
+
366
+ // ============================================================
367
+ // Output panel
368
+ // ============================================================
369
+
370
+ function renderOutputFull() {
371
+ var el = document.getElementById('output');
372
+ if (outputLines.length === 0) {
373
+ el.innerHTML = '<div id="empty"><div class="ico">&#9889;</div><div>No output yet</div></div>';
374
+ return;
375
+ }
376
+ var html = '';
377
+ for (var i = 0; i < outputLines.length; i++) {
378
+ html += '<div class="line">' + ansiToHtml(outputLines[i]) + '</div>';
379
+ }
380
+ el.innerHTML = html;
381
+ el.scrollTop = el.scrollHeight;
382
+ }
383
+
384
+ function appendLines(lines) {
385
+ var el = document.getElementById('output');
386
+ // Remove empty-state placeholder if present
387
+ var empty = el.querySelector('#empty');
388
+ if (empty) empty.parentNode.removeChild(empty);
389
+ var frag = document.createDocumentFragment();
390
+ for (var i = 0; i < lines.length; i++) {
391
+ var div = document.createElement('div');
392
+ div.className = 'line';
393
+ div.innerHTML = ansiToHtml(lines[i]);
394
+ frag.appendChild(div);
395
+ }
396
+ el.appendChild(frag);
397
+ el.scrollTop = el.scrollHeight;
398
+ }
399
+
400
+ function selectSession(id) {
401
+ activeId = id;
402
+ outputLines = [];
403
+ nextCursor = 0;
404
+ renderSidebar();
405
+
406
+ var s = null;
407
+ for (var i = 0; i < sessions.length; i++) {
408
+ if (sessions[i].id === id) { s = sessions[i]; break; }
409
+ }
410
+
411
+ var toolbar = document.getElementById('toolbar');
412
+ toolbar.style.display = 'flex';
413
+ document.getElementById('session-title').textContent = (s && (s.label || s.name)) || id.slice(0, 12);
414
+
415
+ var killBtn = document.getElementById('kill-btn');
416
+ killBtn.disabled = !s || (s.status !== 'running' && s.status !== 'starting');
417
+
418
+ document.getElementById('output').innerHTML = '<div style="padding:12px;color:var(--text2)">Loading&#8230;</div>';
419
+
420
+ // Initial load
421
+ callTool('agent_output', {sessionId: id, lastN: 200}).then(function(data) {
422
+ outputLines = data.lines || [];
423
+ nextCursor = data.nextCursor || 0;
424
+ renderOutputFull();
425
+ }).catch(function(e) {
426
+ document.getElementById('output').innerHTML = '<div class="errmsg">Error: ' + escHtml(e.message) + '</div>';
427
+ });
428
+ }
429
+
430
+ // ============================================================
431
+ // Kill
432
+ // ============================================================
433
+
434
+ function doKill() {
435
+ if (!activeId) return;
436
+ var s = null;
437
+ for (var i = 0; i < sessions.length; i++) {
438
+ if (sessions[i].id === activeId) { s = sessions[i]; break; }
439
+ }
440
+ var toolName = (s && s.pty) ? 'terminal_kill' : 'agent_kill';
441
+ callTool(toolName, {sessionId: activeId}).then(function() {
442
+ return doRefresh();
443
+ }).catch(function(e) {
444
+ setStatus('Kill failed: ' + e.message);
445
+ });
446
+ }
447
+
448
+ // ============================================================
449
+ // Poll
450
+ // ============================================================
451
+
452
+ function doPoll() {
453
+ if (pollActive) return;
454
+ pollActive = true;
455
+ var p = loadSessions();
456
+ if (activeId) {
457
+ var capturedId = activeId;
458
+ var capturedCursor = nextCursor;
459
+ p = p.then(function() {
460
+ return callTool('agent_output', {sessionId: capturedId, since: capturedCursor});
461
+ }).then(function(data) {
462
+ if (capturedId !== activeId) return; // user switched sessions
463
+ var newLines = data.lines || [];
464
+ if (newLines.length > 0) {
465
+ outputLines = outputLines.concat(newLines);
466
+ if (outputLines.length > 2000) outputLines = outputLines.slice(-2000);
467
+ nextCursor = data.nextCursor || nextCursor;
468
+ appendLines(newLines);
469
+ }
470
+ }).catch(function() {});
471
+ }
472
+ p.then(function() {
473
+ pollActive = false;
474
+ pollTimer = setTimeout(doPoll, 3000);
475
+ }).catch(function() {
476
+ pollActive = false;
477
+ pollTimer = setTimeout(doPoll, 3000);
478
+ });
479
+ }
480
+
481
+ function doRefresh() {
482
+ return loadSessions().then(function() {
483
+ if (activeId) {
484
+ return callTool('agent_output', {sessionId: activeId, lastN: 200}).then(function(data) {
485
+ outputLines = data.lines || [];
486
+ nextCursor = data.nextCursor || 0;
487
+ renderOutputFull();
488
+ });
489
+ }
490
+ });
491
+ }
492
+
493
+ // ============================================================
494
+ // Boot
495
+ // ============================================================
496
+
497
+ initBridge().then(function() {
498
+ return loadSessions();
499
+ }).then(function() {
500
+ pollTimer = setTimeout(doPoll, 3000);
501
+ }).catch(function(e) {
502
+ setStatus('Bridge error: ' + e.message);
503
+ document.getElementById('output').innerHTML = '<div class="errmsg">Failed to connect to MCP bridge: ' + escHtml(e.message) + '</div>';
504
+ });
505
+ </script>
506
+ </body>
507
+ </html>`;
508
+
509
+ export { PANEL_HTML };
510
+ //# sourceMappingURL=panel.mjs.map
511
+ //# sourceMappingURL=panel.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/panel-bridge.ts","../../src/sessions-panel/panel.ts"],"names":[],"mappings":";;;;;;AAqCO,SAAS,kBAAkB,OAAA,EAAyB;AACzD,EAAA,OAAO,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAA,EAmDa,IAAA,CAAK,SAAA,CAAU,OAAO,CAAC,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,KAAA,CAAA;AAyF7C;;;AC3JO,IAAM,UAAA,GAAa,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA2ExB,iBAAA,CAAkB,2BAA2B,CAAC;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,OAAA","file":"panel.mjs","sourcesContent":["/**\n * Shared MCP Apps client bridge for agentproto panels.\n *\n * One spec-correct copy of the JSON-RPC-over-postMessage bridge that every\n * ui:// panel embeds in its <script> block. Mirrors the behaviour of\n * @agstudio/mcp-apps' mcpAppClientScript() — which we cannot import here:\n * agentproto is a standalone pnpm workspace with no @agstudio/* dependency\n * (same isolation invariant as mcp-apps-adapter.ts).\n *\n * Handshake (MCP Apps ext spec 2026-01-26, ext-apps McpUiInitializeRequest):\n * params REQUIRE `appInfo` (Implementation: {name, version}),\n * `appCapabilities`, and `protocolVersion`. Sending `clientInfo` instead\n * of `appInfo` fails the host's schema validation — hosts either reply\n * with an error or silently drop the request, so the panel's initBridge()\n * promise never resolves and the panel renders blank while every\n * server-side surface (tools, resources) works. That exact bug shipped in\n * 4 copy-pasted bridges; this module is the single corrected source.\n *\n * The script defines, in panel scope:\n * post / rpcRequest / rpcNotify — raw JSON-RPC plumbing\n * onHostNotification(cb) — subscribe to host-pushed\n * notifications (method, params)\n * initBridge() — ui/initialize → ui/notifications/initialized\n * callTool(name, args) — tools/call + JSON text unwrap\n * getHostContext() / onHostContext(cb) — host context (displayMode, theme, …),\n * captured from the initialize result and\n * merged on ui/notifications/host-context-changed\n * requestDisplayMode(mode) — ui/request-display-mode (inline|fullscreen|pip)\n *\n * Display-mode toggle: the bridge also injects two floating buttons\n * (fullscreen + pip) in the top-right corner of every panel, shown only for\n * the modes the host actually advertises via hostContext.availableDisplayModes\n * — same pattern as guilde's canvas.app.ts canvasShellHtml() (the reference\n * implementation that renders correctly in Claude hosts). No auto-request:\n * panels stay inline until the user clicks.\n */\n\nexport function panelBridgeScript(appName: string): string {\n return `// ── MCP Apps bridge (shared: panel-bridge.ts) ──\n// JSON-RPC 2.0 over window.parent.postMessage · spec 2026-01-26\nvar _nextId = 1, _pending = {}, _notifyHandlers = [];\nvar _hostContext = null, _hostContextHandlers = [];\nfunction post(msg){ window.parent.postMessage(msg, '*'); }\nfunction getHostContext(){ return _hostContext; }\nfunction onHostContext(cb){\n _hostContextHandlers.push(cb);\n // Replay the last context so a late subscriber isn't stuck blind.\n if (_hostContext){ try { cb(_hostContext); } catch(_) {} }\n}\nfunction _setHostContext(ctx){\n if (!ctx || typeof ctx !== 'object') return;\n // ui/notifications/host-context-changed carries only the changed keys —\n // merge, matching the official ext-apps App behaviour.\n _hostContext = Object.assign({}, _hostContext || {}, ctx);\n for (var i = 0; i < _hostContextHandlers.length; i++){\n try { _hostContextHandlers[i](_hostContext); } catch(_) {}\n }\n}\nfunction rpcRequest(method, params){\n return new Promise(function(resolve, reject){\n var id = _nextId++;\n _pending[id] = {resolve: resolve, reject: reject};\n post({jsonrpc: '2.0', id: id, method: method, params: params || {}});\n });\n}\nfunction rpcNotify(method, params){ post({jsonrpc: '2.0', method: method, params: params || {}}); }\nfunction onHostNotification(cb){ _notifyHandlers.push(cb); }\nwindow.addEventListener('message', function(evt){\n var msg = evt.data;\n if (!msg || typeof msg !== 'object' || msg.jsonrpc !== '2.0') return;\n if (msg.id != null && msg.method == null){\n var p = _pending[msg.id];\n if (!p) return;\n delete _pending[msg.id];\n if (msg.error) p.reject(new Error(msg.error.message || ('rpc error ' + msg.error.code)));\n else p.resolve(msg.result);\n return;\n }\n if (msg.method){\n if (msg.method === 'ui/notifications/host-context-changed'){\n _setHostContext(msg.params || {});\n }\n for (var i = 0; i < _notifyHandlers.length; i++){\n try { _notifyHandlers[i](msg.method, msg.params || {}); } catch(_) {}\n }\n }\n});\nfunction initBridge(){\n return rpcRequest('ui/initialize', {\n appInfo: {name: ${JSON.stringify(appName)}, version: '0.1.0'},\n appCapabilities: {availableDisplayModes: ['inline', 'fullscreen', 'pip']},\n protocolVersion: '2026-01-26'\n }).then(function(result){\n // The initialize result carries the initial hostContext (displayMode +\n // availableDisplayModes) — capture it before notifying the host.\n if (result && result.hostContext) _setHostContext(result.hostContext);\n rpcNotify('ui/notifications/initialized', {});\n });\n}\nfunction requestDisplayMode(mode){\n return rpcRequest('ui/request-display-mode', {mode: mode});\n}\nfunction callTool(name, args){\n return rpcRequest('tools/call', {name: name, arguments: args || {}}).then(function(result){\n if (result.isError){\n var e = (result.content && result.content[0] && result.content[0].text) || 'tool error';\n throw new Error(e);\n }\n var text = (result.content && result.content[0] && result.content[0].text) || '{}';\n return JSON.parse(text);\n });\n}\n\n// ── Display-mode toggle buttons (NO auto-request) ──────────────────────\n// Injected by the shared bridge so every panel gets them without touching\n// its own markup. Mirrors guilde canvas.app.ts canvasShellHtml(): the\n// panel stays inline by default; the user expands on demand. Buttons only\n// appear for modes the host advertises in hostContext.availableDisplayModes.\n(function(){\n function mount(){\n var style = document.createElement('style');\n style.textContent = '#dm,#pin{display:none;position:fixed;top:8px;z-index:10;'\n + 'border:1px solid #d0d0d0;background:#fff;color:#1a1a1a;'\n + 'font:600 13px/1 system-ui,sans-serif;padding:7px 12px;border-radius:6px;'\n + 'cursor:pointer;box-shadow:0 1px 4px rgba(0,0,0,.18)}'\n + '#dm{right:8px}#pin{right:118px}'\n + '#dm:hover,#pin:hover{background:#f2f2f2;border-color:#b0b0b0}'\n + '@media (prefers-color-scheme:dark){'\n + '#dm,#pin{border-color:#555;background:#2a2a2a;color:#f0f0f0;box-shadow:0 1px 4px rgba(0,0,0,.5)}'\n + '#dm:hover,#pin:hover{background:#333;border-color:#777}}';\n document.head.appendChild(style);\n\n var btn = document.createElement('button');\n btn.id = 'dm'; btn.type = 'button'; btn.title = \"Basculer l'affichage\";\n var pin = document.createElement('button');\n pin.id = 'pin'; pin.type = 'button'; pin.title = 'Épingler sur le côté (pip)';\n document.body.appendChild(pin);\n document.body.appendChild(btn);\n\n function has(avail, m){ return !!avail && avail.indexOf(m) >= 0; }\n\n // Re-sync button visibility + label from the current host context.\n function syncBtn(ctx){\n ctx = ctx || {};\n var avail = ctx.availableDisplayModes;\n // Diagnostic: what does THIS host actually advertise? (inline/fullscreen/pip)\n console.log('[mcp-app] displayMode=', ctx.displayMode,\n 'availableDisplayModes=', avail);\n\n // Fullscreen toggle button.\n if (has(avail, 'fullscreen')){\n btn.style.display = 'block';\n btn.textContent = (ctx.displayMode === 'fullscreen') ? '⤡ Réduire' : '⤢ Agrandir';\n } else { btn.style.display = 'none'; }\n\n // Dedicated pip (\"pinned on side\") button — only if the host advertises pip.\n if (has(avail, 'pip')){\n pin.style.display = 'block';\n pin.textContent = (ctx.displayMode === 'pip') ? '⤡ Détacher' : '📌 Épingler';\n } else { pin.style.display = 'none'; }\n }\n\n onHostContext(syncBtn);\n\n btn.addEventListener('click', function(){\n var ctx = getHostContext() || {};\n var inPanel = (ctx.displayMode === 'fullscreen' || ctx.displayMode === 'pip');\n requestDisplayMode(inPanel ? 'inline' : 'fullscreen').catch(function(){});\n });\n\n pin.addEventListener('click', function(){\n var ctx = getHostContext() || {};\n requestDisplayMode(ctx.displayMode === 'pip' ? 'inline' : 'pip').catch(function(){});\n });\n }\n if (document.body) mount();\n else document.addEventListener('DOMContentLoaded', mount);\n})();`\n}\n","/**\n * HTML bundle for the agentproto sessions panel — the `PANEL_HTML` served\n * as the `ui://agentproto_sessions/view` resource by `registerMcpApps`\n * (see ../mcp-app-types.ts and runtime's mcp-apps-adapter.ts). Mounted via\n * the `agentproto_sessions` tool defined in ./index.ts.\n *\n * Protocol: MCP Apps ext spec 2026-01-26\n * – Bridge: JSON-RPC 2.0 over window.parent.postMessage\n * – Handshake: ui/initialize → host result → ui/notifications/initialized\n * – Data: tools/call → session_list + agent_output (3 s poll)\n * – Kill action: tools/call → agent_kill | terminal_kill\n * (goes through Claude's normal tool approval flow)\n *\n * The HTML bundle is fully autonomous (zero CDN, zero external deps).\n * ANSI-to-HTML renderer handles SGR codes (colors 0-15, bold, dim,\n * italic, underline, reset). CR-overwrite sequences are stripped so\n * progress bars don't corrupt the output.\n */\n\nimport { panelBridgeScript } from \"../panel-bridge.js\"\n\n// ── HTML bundle ─────────────────────────────────────────────────────────────\n\nexport const PANEL_HTML = `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"UTF-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n<title>agentproto sessions</title>\n<style>\n*{box-sizing:border-box;margin:0;padding:0}\n:root{\n --bg:#0d1117;--bg2:#161b22;--bg3:#21262d;--border:#30363d;\n --text:#e6edf3;--text2:#8b949e;\n --green:#3fb950;--yellow:#d29922;--red:#f85149;--blue:#58a6ff;--purple:#bc8cff;\n}\nhtml,body{height:100%;font-family:Menlo,Monaco,'Courier New',monospace;font-size:13px;background:var(--bg);color:var(--text);overflow:hidden}\n#app{display:flex;height:100%}\n#sidebar{width:220px;min-width:180px;background:var(--bg2);border-right:1px solid var(--border);display:flex;flex-direction:column;flex-shrink:0}\n#sidebar-hdr{padding:10px 12px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between}\n#sidebar-hdr h1{font-size:11px;font-weight:600;color:var(--text2);text-transform:uppercase;letter-spacing:.06em}\n#refresh-btn{background:none;border:none;cursor:pointer;color:var(--text2);font-size:15px;line-height:1;padding:2px 4px;border-radius:4px;font-family:inherit}\n#refresh-btn:hover{color:var(--text);background:var(--bg3)}\n#session-list{flex:1;overflow-y:auto;padding:4px 0}\n.si{padding:8px 12px;cursor:pointer;border-left:2px solid transparent}\n.si:hover{background:var(--bg3)}\n.si.active{background:var(--bg3);border-left-color:var(--blue)}\n.sn{font-size:12px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n.sm{font-size:11px;color:var(--text2);margin-top:2px;display:flex;gap:6px;align-items:center}\n.badge{display:inline-block;padding:1px 5px;border-radius:10px;font-size:10px;font-weight:600}\n.br{background:rgba(63,185,80,.15);color:var(--green)}\n.bs{background:rgba(88,166,255,.15);color:var(--blue)}\n.be{background:rgba(139,148,158,.1);color:var(--text2)}\n.bk{background:rgba(248,81,73,.1);color:var(--red)}\n.berr{background:rgba(248,81,73,.2);color:var(--red)}\n#main{flex:1;display:flex;flex-direction:column;overflow:hidden;min-width:0}\n#toolbar{padding:8px 12px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:8px;background:var(--bg2)}\n#session-title{font-size:12px;font-weight:500;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n.abtn{background:var(--bg3);border:1px solid var(--border);color:var(--text);padding:4px 10px;border-radius:4px;cursor:pointer;font-size:12px;font-family:inherit}\n.abtn:hover{background:var(--border)}\n.abtn:disabled{opacity:.4;cursor:default}\n.abtn.danger{border-color:var(--red);color:var(--red)}\n.abtn.danger:hover{background:rgba(248,81,73,.1)}\n#output{flex:1;overflow-y:auto;padding:8px 12px;background:var(--bg);font-size:12px;line-height:1.6}\n.line{white-space:pre-wrap;word-break:break-all}\n#empty{display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;color:var(--text2);gap:8px;text-align:center;padding:24px}\n#empty .ico{font-size:32px}\n#statusbar{padding:4px 12px;font-size:11px;color:var(--text2);border-top:1px solid var(--border);background:var(--bg2);flex-shrink:0}\n.errmsg{color:var(--red);padding:12px}\n/* ANSI SGR */\n.ab{font-weight:bold}.ad{opacity:.6}.ai{font-style:italic}.au{text-decoration:underline}\n.f0{color:#21262d}.f1{color:#f85149}.f2{color:#3fb950}.f3{color:#d29922}\n.f4{color:#58a6ff}.f5{color:#bc8cff}.f6{color:#39d353}.f7{color:#e6edf3}\n.f8{color:#8b949e}.f9{color:#ff7b72}.f10{color:#56d364}.f11{color:#e3b341}\n.f12{color:#79c0ff}.f13{color:#d2a8ff}.f14{color:#56d364}.f15{color:#f0f6fc}\n</style>\n</head>\n<body>\n<div id=\"app\">\n <div id=\"sidebar\">\n <div id=\"sidebar-hdr\">\n <h1>Sessions</h1>\n <button id=\"refresh-btn\" title=\"Refresh\" onclick=\"doRefresh()\">&#8635;</button>\n </div>\n <div id=\"session-list\"></div>\n </div>\n <div id=\"main\">\n <div id=\"toolbar\" style=\"display:none\">\n <span id=\"session-title\"></span>\n <button class=\"abtn danger\" id=\"kill-btn\" onclick=\"doKill()\">Kill</button>\n </div>\n <div id=\"output\">\n <div id=\"empty\"><div class=\"ico\">&#9889;</div><div>Select a session</div></div>\n </div>\n <div id=\"statusbar\">Connecting to bridge&#8230;</div>\n </div>\n</div>\n<script>\n${panelBridgeScript(\"agentproto-sessions-panel\")}\n\n// ============================================================\n// ANSI-to-HTML renderer (SGR codes: colors 0-15, bold/dim/italic/underline)\n// ============================================================\n\nfunction escHtml(s) {\n return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');\n}\n\nfunction ansiToHtml(raw) {\n // Strip CR overwrite sequences (progress bars: CR without LF)\n var text = raw.replace(/[^\\\\n]*\\\\r([^\\\\n])/g, '$1').replace(/\\\\r/g, '');\n var bold = false, dim = false, italic = false, underline = false, fg = -1;\n var out = '';\n // Split on ESC [ ... m (SGR) sequences; keep delimiters\n var ESC = '\u001b';\n var parts = text.split(/([\u001b]\\\\[[0-9;]*m)/);\n for (var pi = 0; pi < parts.length; pi++) {\n var part = parts[pi];\n if (part.length === 0) continue;\n if (part.charCodeAt(0) === 0x1b && part[1] === '[') {\n // Parse SGR parameters\n var inner = part.slice(2, part.length - 1); // strip ESC[ and m\n var codes = inner === '' ? [0] : inner.split(';').map(Number);\n for (var ci = 0; ci < codes.length; ci++) {\n var c = codes[ci];\n if (c === 0) { bold = false; dim = false; italic = false; underline = false; fg = -1; }\n else if (c === 1) bold = true;\n else if (c === 2) dim = true;\n else if (c === 3) italic = true;\n else if (c === 4) underline = true;\n else if (c === 22) { bold = false; dim = false; }\n else if (c === 23) italic = false;\n else if (c === 24) underline = false;\n else if (c >= 30 && c <= 37) fg = c - 30;\n else if (c === 39) fg = -1;\n else if (c >= 90 && c <= 97) fg = c - 90 + 8;\n }\n } else {\n // Text segment — strip any remaining non-printable ESC sequences\n var safe = escHtml(part).replace(/\u001b\\\\[[^m]*[A-Za-z]/g, '');\n if (safe === '') continue;\n var cls = '';\n if (bold) cls += ' ab';\n if (dim) cls += ' ad';\n if (italic) cls += ' ai';\n if (underline) cls += ' au';\n if (fg >= 0 && fg <= 15) cls += ' f' + fg;\n cls = cls.trim();\n out += cls ? '<span class=\"' + cls + '\">' + safe + '</span>' : safe;\n }\n }\n return out;\n}\n\n// ============================================================\n// App state\n// ============================================================\n\nvar sessions = [];\nvar activeId = null;\nvar outputLines = [];\nvar nextCursor = 0;\nvar pollTimer = null;\nvar pollActive = false;\n\nfunction setStatus(msg) {\n document.getElementById('statusbar').textContent = msg;\n}\n\n// ============================================================\n// Session list\n// ============================================================\n\nfunction badgeClass(status) {\n if (status === 'running') return 'br';\n if (status === 'starting') return 'bs';\n if (status === 'killed') return 'bk';\n if (status === 'error') return 'berr';\n return 'be'; // exited\n}\n\n// Derive a turn-aware display badge. A session's 'status' only tracks process\n// liveness, so an agent-cli process stays \"running\" while idle between turns.\n// Read 'busy' / 'awaitingInput' to show real activity instead:\n// working — a turn is in flight (busy)\n// waiting — turn ended on awaiting-input (needs a reply)\n// idle — process alive, no turn running (last turn done)\nfunction displayBadge(s) {\n if (s.status === 'running' && s.kind === 'agent-cli') {\n if (s.busy) return { label: 'working', cls: 'br' };\n if (s.awaitingInput) return { label: 'waiting', cls: 'bs' };\n return { label: 'idle', cls: 'be' };\n }\n return { label: s.status, cls: badgeClass(s.status) };\n}\n\nfunction renderSidebar() {\n var el = document.getElementById('session-list');\n if (sessions.length === 0) {\n el.innerHTML = '<div style=\"padding:12px;color:var(--text2);font-size:11px\">No sessions</div>';\n return;\n }\n var html = '';\n for (var i = 0; i < sessions.length; i++) {\n var s = sessions[i];\n var label = s.label || s.name || (s.command ? s.command.split('/').pop() : null) || s.id.slice(0, 8);\n var db = displayBadge(s);\n var active = s.id === activeId ? ' active' : '';\n // blockedOn (set while the turn waits on a spawned sub-agent or a\n // shell command) rides next to the status badge; waiting-on-user is\n // NOT here — that's awaitingInput, a different signal.\n var blocked = '';\n if (s.blockedOn === 'subagent') blocked = '<span class=\"badge bs\">&#129513; sous-agent</span>';\n else if (s.blockedOn === 'command') blocked = '<span class=\"badge bs\">&#9203; commande</span>';\n html += '<div class=\"si' + active + '\" onclick=\"selectSession(\\\\'' + s.id + '\\\\')\">'\n + '<div class=\"sn\">' + escHtml(label) + '</div>'\n + '<div class=\"sm\">'\n + '<span class=\"badge ' + db.cls + '\">' + db.label + '</span>'\n + blocked\n + '<span>' + escHtml(s.kind || '') + '</span>'\n + '</div></div>';\n }\n el.innerHTML = html;\n}\n\nfunction loadSessions() {\n // {kind:'all'} is session_list's live-able default — agent-CLI + terminal/\n // PTY only. kind:\"command\" rows (a shell-execution log, not a resumable\n // session) are excluded unless includeCommands is explicitly passed.\n return callTool('session_list', {kind: 'all'}).then(function(data) {\n sessions = data.sessions || [];\n renderSidebar();\n setStatus(sessions.length + ' session' + (sessions.length === 1 ? '' : 's') + ' · ' + new Date().toLocaleTimeString());\n }).catch(function(e) {\n setStatus('Error: ' + e.message);\n });\n}\n\n// ============================================================\n// Output panel\n// ============================================================\n\nfunction renderOutputFull() {\n var el = document.getElementById('output');\n if (outputLines.length === 0) {\n el.innerHTML = '<div id=\"empty\"><div class=\"ico\">&#9889;</div><div>No output yet</div></div>';\n return;\n }\n var html = '';\n for (var i = 0; i < outputLines.length; i++) {\n html += '<div class=\"line\">' + ansiToHtml(outputLines[i]) + '</div>';\n }\n el.innerHTML = html;\n el.scrollTop = el.scrollHeight;\n}\n\nfunction appendLines(lines) {\n var el = document.getElementById('output');\n // Remove empty-state placeholder if present\n var empty = el.querySelector('#empty');\n if (empty) empty.parentNode.removeChild(empty);\n var frag = document.createDocumentFragment();\n for (var i = 0; i < lines.length; i++) {\n var div = document.createElement('div');\n div.className = 'line';\n div.innerHTML = ansiToHtml(lines[i]);\n frag.appendChild(div);\n }\n el.appendChild(frag);\n el.scrollTop = el.scrollHeight;\n}\n\nfunction selectSession(id) {\n activeId = id;\n outputLines = [];\n nextCursor = 0;\n renderSidebar();\n\n var s = null;\n for (var i = 0; i < sessions.length; i++) {\n if (sessions[i].id === id) { s = sessions[i]; break; }\n }\n\n var toolbar = document.getElementById('toolbar');\n toolbar.style.display = 'flex';\n document.getElementById('session-title').textContent = (s && (s.label || s.name)) || id.slice(0, 12);\n\n var killBtn = document.getElementById('kill-btn');\n killBtn.disabled = !s || (s.status !== 'running' && s.status !== 'starting');\n\n document.getElementById('output').innerHTML = '<div style=\"padding:12px;color:var(--text2)\">Loading&#8230;</div>';\n\n // Initial load\n callTool('agent_output', {sessionId: id, lastN: 200}).then(function(data) {\n outputLines = data.lines || [];\n nextCursor = data.nextCursor || 0;\n renderOutputFull();\n }).catch(function(e) {\n document.getElementById('output').innerHTML = '<div class=\"errmsg\">Error: ' + escHtml(e.message) + '</div>';\n });\n}\n\n// ============================================================\n// Kill\n// ============================================================\n\nfunction doKill() {\n if (!activeId) return;\n var s = null;\n for (var i = 0; i < sessions.length; i++) {\n if (sessions[i].id === activeId) { s = sessions[i]; break; }\n }\n var toolName = (s && s.pty) ? 'terminal_kill' : 'agent_kill';\n callTool(toolName, {sessionId: activeId}).then(function() {\n return doRefresh();\n }).catch(function(e) {\n setStatus('Kill failed: ' + e.message);\n });\n}\n\n// ============================================================\n// Poll\n// ============================================================\n\nfunction doPoll() {\n if (pollActive) return;\n pollActive = true;\n var p = loadSessions();\n if (activeId) {\n var capturedId = activeId;\n var capturedCursor = nextCursor;\n p = p.then(function() {\n return callTool('agent_output', {sessionId: capturedId, since: capturedCursor});\n }).then(function(data) {\n if (capturedId !== activeId) return; // user switched sessions\n var newLines = data.lines || [];\n if (newLines.length > 0) {\n outputLines = outputLines.concat(newLines);\n if (outputLines.length > 2000) outputLines = outputLines.slice(-2000);\n nextCursor = data.nextCursor || nextCursor;\n appendLines(newLines);\n }\n }).catch(function() {});\n }\n p.then(function() {\n pollActive = false;\n pollTimer = setTimeout(doPoll, 3000);\n }).catch(function() {\n pollActive = false;\n pollTimer = setTimeout(doPoll, 3000);\n });\n}\n\nfunction doRefresh() {\n return loadSessions().then(function() {\n if (activeId) {\n return callTool('agent_output', {sessionId: activeId, lastN: 200}).then(function(data) {\n outputLines = data.lines || [];\n nextCursor = data.nextCursor || 0;\n renderOutputFull();\n });\n }\n });\n}\n\n// ============================================================\n// Boot\n// ============================================================\n\ninitBridge().then(function() {\n return loadSessions();\n}).then(function() {\n pollTimer = setTimeout(doPoll, 3000);\n}).catch(function(e) {\n setStatus('Bridge error: ' + e.message);\n document.getElementById('output').innerHTML = '<div class=\"errmsg\">Failed to connect to MCP bridge: ' + escHtml(e.message) + '</div>';\n});\n</script>\n</body>\n</html>`\n"]}