@agentuity/coder 2.0.7 → 2.0.9-v3.48d5810

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 (113) hide show
  1. package/AGENTS.md +43 -0
  2. package/README.md +75 -37
  3. package/dist/index.d.ts +2 -3
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +9 -1669
  6. package/dist/index.js.map +1 -1
  7. package/package.json +44 -42
  8. package/src/index.ts +84 -1996
  9. package/dist/chain-preview.d.ts +0 -55
  10. package/dist/chain-preview.d.ts.map +0 -1
  11. package/dist/chain-preview.js +0 -472
  12. package/dist/chain-preview.js.map +0 -1
  13. package/dist/client.d.ts +0 -44
  14. package/dist/client.d.ts.map +0 -1
  15. package/dist/client.js +0 -411
  16. package/dist/client.js.map +0 -1
  17. package/dist/commands.d.ts +0 -22
  18. package/dist/commands.d.ts.map +0 -1
  19. package/dist/commands.js +0 -99
  20. package/dist/commands.js.map +0 -1
  21. package/dist/footer.d.ts +0 -34
  22. package/dist/footer.d.ts.map +0 -1
  23. package/dist/footer.js +0 -249
  24. package/dist/footer.js.map +0 -1
  25. package/dist/handlers.d.ts +0 -24
  26. package/dist/handlers.d.ts.map +0 -1
  27. package/dist/handlers.js +0 -83
  28. package/dist/handlers.js.map +0 -1
  29. package/dist/hub-overlay-state.d.ts +0 -31
  30. package/dist/hub-overlay-state.d.ts.map +0 -1
  31. package/dist/hub-overlay-state.js +0 -78
  32. package/dist/hub-overlay-state.js.map +0 -1
  33. package/dist/hub-overlay.d.ts +0 -146
  34. package/dist/hub-overlay.d.ts.map +0 -1
  35. package/dist/hub-overlay.js +0 -2354
  36. package/dist/hub-overlay.js.map +0 -1
  37. package/dist/native-remote-ui-context.d.ts +0 -5
  38. package/dist/native-remote-ui-context.d.ts.map +0 -1
  39. package/dist/native-remote-ui-context.js +0 -30
  40. package/dist/native-remote-ui-context.js.map +0 -1
  41. package/dist/output-viewer.d.ts +0 -49
  42. package/dist/output-viewer.d.ts.map +0 -1
  43. package/dist/output-viewer.js +0 -389
  44. package/dist/output-viewer.js.map +0 -1
  45. package/dist/overlay.d.ts +0 -40
  46. package/dist/overlay.d.ts.map +0 -1
  47. package/dist/overlay.js +0 -225
  48. package/dist/overlay.js.map +0 -1
  49. package/dist/protocol.d.ts +0 -605
  50. package/dist/protocol.d.ts.map +0 -1
  51. package/dist/protocol.js +0 -4
  52. package/dist/protocol.js.map +0 -1
  53. package/dist/remote-lifecycle.d.ts +0 -61
  54. package/dist/remote-lifecycle.d.ts.map +0 -1
  55. package/dist/remote-lifecycle.js +0 -190
  56. package/dist/remote-lifecycle.js.map +0 -1
  57. package/dist/remote-session.d.ts +0 -128
  58. package/dist/remote-session.d.ts.map +0 -1
  59. package/dist/remote-session.js +0 -876
  60. package/dist/remote-session.js.map +0 -1
  61. package/dist/remote-tui.d.ts +0 -40
  62. package/dist/remote-tui.d.ts.map +0 -1
  63. package/dist/remote-tui.js +0 -867
  64. package/dist/remote-tui.js.map +0 -1
  65. package/dist/remote-ui-handler.d.ts +0 -5
  66. package/dist/remote-ui-handler.d.ts.map +0 -1
  67. package/dist/remote-ui-handler.js +0 -53
  68. package/dist/remote-ui-handler.js.map +0 -1
  69. package/dist/renderers.d.ts +0 -34
  70. package/dist/renderers.d.ts.map +0 -1
  71. package/dist/renderers.js +0 -669
  72. package/dist/renderers.js.map +0 -1
  73. package/dist/review.d.ts +0 -15
  74. package/dist/review.d.ts.map +0 -1
  75. package/dist/review.js +0 -154
  76. package/dist/review.js.map +0 -1
  77. package/dist/titlebar.d.ts +0 -3
  78. package/dist/titlebar.d.ts.map +0 -1
  79. package/dist/titlebar.js +0 -59
  80. package/dist/titlebar.js.map +0 -1
  81. package/dist/todo/index.d.ts +0 -3
  82. package/dist/todo/index.d.ts.map +0 -1
  83. package/dist/todo/index.js +0 -3
  84. package/dist/todo/index.js.map +0 -1
  85. package/dist/todo/store.d.ts +0 -6
  86. package/dist/todo/store.d.ts.map +0 -1
  87. package/dist/todo/store.js +0 -43
  88. package/dist/todo/store.js.map +0 -1
  89. package/dist/todo/types.d.ts +0 -13
  90. package/dist/todo/types.d.ts.map +0 -1
  91. package/dist/todo/types.js +0 -2
  92. package/dist/todo/types.js.map +0 -1
  93. package/src/chain-preview.ts +0 -621
  94. package/src/client.ts +0 -527
  95. package/src/commands.ts +0 -132
  96. package/src/footer.ts +0 -305
  97. package/src/handlers.ts +0 -113
  98. package/src/hub-overlay-state.ts +0 -127
  99. package/src/hub-overlay.ts +0 -3037
  100. package/src/native-remote-ui-context.ts +0 -41
  101. package/src/output-viewer.ts +0 -480
  102. package/src/overlay.ts +0 -294
  103. package/src/protocol.ts +0 -758
  104. package/src/remote-lifecycle.ts +0 -270
  105. package/src/remote-session.ts +0 -1080
  106. package/src/remote-tui.ts +0 -1020
  107. package/src/remote-ui-handler.ts +0 -86
  108. package/src/renderers.ts +0 -740
  109. package/src/review.ts +0 -201
  110. package/src/titlebar.ts +0 -63
  111. package/src/todo/index.ts +0 -2
  112. package/src/todo/store.ts +0 -49
  113. package/src/todo/types.ts +0 -14
@@ -1,876 +0,0 @@
1
- /**
2
- * Remote Session Bridge — TUI ↔ Hub ↔ Sandbox
3
- *
4
- * Handles the WebSocket connection and RPC protocol bridge for remote mode.
5
- * In remote mode, the local Pi TUI connects to an existing sandbox session
6
- * through the Hub, acting as a thin client:
7
- * - User input → rpc_command → Hub → sandbox
8
- * - Sandbox events → rpc_event → Hub → TUI rendering
9
- * - Extension UI dialogs → rpc_ui_request/response → Hub ↔ TUI
10
- *
11
- * This module manages the connection lifecycle and provides an API
12
- * for the extension to send commands and receive events.
13
- */
14
- import { applyRemoteLifecycleEvent, clearRemoteLifecycleWorkingMessage, createRemoteLifecycleState, getRemoteLifecycleActivityLabel, getRemoteLifecycleLabel, syncRemoteLifecycleWorkingMessage, } from "./remote-lifecycle.js";
15
- const DEBUG = !!process.env['AGENTUITY_DEBUG'];
16
- function log(msg) {
17
- if (DEBUG)
18
- console.error(`[remote-session] ${msg}`);
19
- }
20
- // ── Remote Session Client ──
21
- const RECONNECT_BASE_MS = 1_000;
22
- const RECONNECT_MAX_MS = 30_000;
23
- const MAX_RECONNECT_ATTEMPTS = 20;
24
- export class RemoteSession {
25
- ws = null;
26
- connected = false;
27
- intentionallyClosed = false;
28
- hubWsUrl = '';
29
- reconnectAttempts = 0;
30
- reconnectTimer = null;
31
- eventHandlers = [];
32
- uiHandler = null;
33
- responseHandlers = [];
34
- connectionHandlers = [];
35
- lifecycleHandlers = [];
36
- lifecycleState;
37
- replaySettledTimer = null;
38
- /** Session ID this client is connected to */
39
- sessionId;
40
- /** Session label (populated after connection) */
41
- label = '';
42
- /** API key for Hub authentication */
43
- // TODO: Remove/Change when we get Agentuity service level auth enabled, this is just temporary
44
- apiKey = null;
45
- constructor(sessionId) {
46
- this.sessionId = sessionId;
47
- this.lifecycleState = createRemoteLifecycleState(sessionId);
48
- }
49
- dispatchEvent(event) {
50
- for (const handler of this.eventHandlers) {
51
- try {
52
- handler(event);
53
- }
54
- catch (err) {
55
- log(`Event handler error: ${err instanceof Error ? err.message : String(err)}`);
56
- }
57
- }
58
- }
59
- dispatchResponse(response) {
60
- for (const handler of this.responseHandlers) {
61
- try {
62
- handler(response);
63
- }
64
- catch (err) {
65
- log(`Response handler error: ${err instanceof Error ? err.message : String(err)}`);
66
- }
67
- }
68
- }
69
- applyLifecycle(event) {
70
- const next = applyRemoteLifecycleEvent(this.lifecycleState, event);
71
- if (next === this.lifecycleState)
72
- return;
73
- this.lifecycleState = next;
74
- for (const handler of this.lifecycleHandlers) {
75
- try {
76
- handler(this.lifecycleState);
77
- }
78
- catch (err) {
79
- log(`Lifecycle handler error: ${err instanceof Error ? err.message : String(err)}`);
80
- }
81
- }
82
- }
83
- clearReplaySettledTimer() {
84
- if (!this.replaySettledTimer)
85
- return;
86
- clearTimeout(this.replaySettledTimer);
87
- this.replaySettledTimer = null;
88
- }
89
- scheduleReplaySettled() {
90
- this.clearReplaySettledTimer();
91
- this.replaySettledTimer = setTimeout(() => {
92
- this.replaySettledTimer = null;
93
- this.applyLifecycle({ type: 'replay_idle' });
94
- }, 400);
95
- }
96
- observeLiveSignal(eventType, isStreaming) {
97
- const liveEvents = new Set([
98
- 'agent_start',
99
- 'agent_end',
100
- 'message_start',
101
- 'message_update',
102
- 'message_end',
103
- 'thinking_start',
104
- 'thinking_update',
105
- 'thinking_end',
106
- 'tool_call',
107
- 'tool_result',
108
- 'tool_execution_start',
109
- 'tool_execution_end',
110
- 'task_start',
111
- 'task_complete',
112
- 'task_error',
113
- 'turn_start',
114
- 'turn_end',
115
- 'rpc_response',
116
- 'rpc_ui_request',
117
- ]);
118
- if (!liveEvents.has(eventType))
119
- return;
120
- this.clearReplaySettledTimer();
121
- this.applyLifecycle({ type: 'live_signal', isStreaming });
122
- }
123
- getLiveSignalStreamingState(eventType) {
124
- if (eventType === 'agent_start' ||
125
- eventType === 'message_start' ||
126
- eventType === 'message_update' ||
127
- eventType === 'thinking_start' ||
128
- eventType === 'thinking_update' ||
129
- eventType === 'tool_execution_start' ||
130
- eventType === 'turn_start' ||
131
- eventType === 'task_start') {
132
- return true;
133
- }
134
- if (eventType === 'agent_end' || eventType === 'turn_end') {
135
- return false;
136
- }
137
- return undefined;
138
- }
139
- shouldMarkResuming(commandType) {
140
- return commandType === 'prompt' || commandType === 'follow_up' || commandType === 'steer';
141
- }
142
- shouldObserveRpcResponseAsLive() {
143
- return this.lifecycleState.phase !== 'paused' && this.lifecycleState.phase !== 'replaying';
144
- }
145
- /** Register a handler for RPC events from the sandbox */
146
- onEvent(handler) {
147
- this.eventHandlers.push(handler);
148
- }
149
- /** Register a handler for RPC responses from the sandbox */
150
- onResponse(handler) {
151
- this.responseHandlers.push(handler);
152
- }
153
- /** Register the UI dialog handler (select, confirm, input, editor) */
154
- setUiHandler(handler) {
155
- this.uiHandler = handler;
156
- }
157
- /** Register a connection state change handler */
158
- onConnectionChange(handler) {
159
- this.connectionHandlers.push(handler);
160
- }
161
- /** Register a lifecycle state handler for remote attach/replay/live transitions. */
162
- onLifecycleChange(handler) {
163
- this.lifecycleHandlers.push(handler);
164
- handler(this.lifecycleState);
165
- }
166
- getLifecycleState() {
167
- return this.lifecycleState;
168
- }
169
- /** Connect to the Hub WebSocket as a controller for the remote session */
170
- async connect(hubWsUrl) {
171
- this.hubWsUrl = hubWsUrl;
172
- this.intentionallyClosed = false;
173
- this.reconnectAttempts = 0;
174
- return this.doConnect();
175
- }
176
- doConnect() {
177
- return new Promise((resolve, reject) => {
178
- const isReconnect = this.reconnectAttempts > 0;
179
- this.applyLifecycle({ type: 'connect_start', reconnect: isReconnect });
180
- // Build URL with controller params
181
- const url = new URL(this.hubWsUrl);
182
- url.searchParams.set('sessionId', this.sessionId);
183
- url.searchParams.set('role', 'controller');
184
- log(`${isReconnect ? 'Reconnecting' : 'Connecting'} to ${url.toString()}`);
185
- // TODO: Remove/Change when we get Agentuity service level auth enabled, this is just temporary
186
- this.ws = this.apiKey
187
- ? new WebSocket(url.toString(), {
188
- headers: { 'x-agentuity-auth-api-key': this.apiKey },
189
- })
190
- : new WebSocket(url.toString());
191
- const connectTimeout = setTimeout(() => {
192
- reject(new Error('Remote session connection timed out'));
193
- this.ws?.close();
194
- }, 30_000);
195
- this.ws.onopen = () => {
196
- log('WebSocket connected');
197
- };
198
- this.ws.onmessage = (event) => {
199
- let data;
200
- try {
201
- const raw = typeof event.data === 'string'
202
- ? event.data
203
- : new TextDecoder().decode(event.data);
204
- data = JSON.parse(raw);
205
- }
206
- catch {
207
- return;
208
- }
209
- const type = data.type;
210
- // Init message — connection established
211
- if (type === 'init') {
212
- clearTimeout(connectTimeout);
213
- this.connected = true;
214
- this.reconnectAttempts = 0;
215
- if (data.sessionId)
216
- this.sessionId = data.sessionId;
217
- if (typeof data.label === 'string')
218
- this.label = data.label;
219
- this.applyLifecycle({
220
- type: 'init',
221
- sessionId: typeof data.sessionId === 'string' ? data.sessionId : undefined,
222
- label: typeof data.label === 'string' ? data.label : undefined,
223
- });
224
- try {
225
- this.ws?.send(JSON.stringify({ type: 'bootstrap_ready' }));
226
- }
227
- catch {
228
- // Let the close/error path surface bootstrap failure.
229
- }
230
- log(`Connected to session ${this.sessionId}`);
231
- this.notifyConnectionChange('connected');
232
- resolve();
233
- return;
234
- }
235
- // Connection rejected
236
- if (type === 'connection_rejected') {
237
- clearTimeout(connectTimeout);
238
- const msg = data.message || 'Connection rejected';
239
- this.applyLifecycle({
240
- type: 'rpc_command_error',
241
- error: msg,
242
- paused: false,
243
- });
244
- reject(new Error(msg));
245
- return;
246
- }
247
- if (type === 'protocol_error') {
248
- clearTimeout(connectTimeout);
249
- const msg = data.message || 'Hub protocol error';
250
- this.applyLifecycle({
251
- type: 'rpc_command_error',
252
- error: msg,
253
- paused: false,
254
- });
255
- this.dispatchEvent({
256
- type: 'protocol_error',
257
- ...data,
258
- _source: 'hub',
259
- });
260
- if (!this.connected) {
261
- reject(new Error(msg));
262
- }
263
- return;
264
- }
265
- if (type === 'session_resume') {
266
- this.applyLifecycle({
267
- type: 'session_resume',
268
- streamId: typeof data.streamId === 'string' ? data.streamId : null,
269
- streamUrl: typeof data.streamUrl === 'string' ? data.streamUrl : null,
270
- });
271
- this.dispatchEvent({
272
- type: 'session_resume',
273
- ...data,
274
- _source: 'hub',
275
- });
276
- return;
277
- }
278
- if (type === 'session_stream_ready') {
279
- this.applyLifecycle({
280
- type: 'stream_ready',
281
- streamId: typeof data.streamId === 'string' ? data.streamId : null,
282
- streamUrl: typeof data.streamUrl === 'string' ? data.streamUrl : null,
283
- });
284
- this.dispatchEvent({
285
- type: 'session_stream_ready',
286
- ...data,
287
- _source: 'hub',
288
- });
289
- return;
290
- }
291
- if (type === 'rpc_command_error') {
292
- const error = typeof data.error === 'string' ? data.error : 'Remote command failed';
293
- this.applyLifecycle({
294
- type: 'rpc_command_error',
295
- error,
296
- paused: /sandbox .*not connected|resume/i.test(error),
297
- });
298
- this.dispatchEvent({
299
- type: 'rpc_command_error',
300
- ...data,
301
- _source: 'hub',
302
- });
303
- return;
304
- }
305
- // Broadcast-wrapped messages from Hub (LIVE events)
306
- // Format: { type: 'broadcast', event: '<name>', data: { ...payload } }
307
- if (type === 'broadcast') {
308
- const broadcastEvent = data.event;
309
- const broadcastData = data.data ?? {};
310
- if (broadcastEvent === 'rpc_event') {
311
- const rpcEvent = broadcastData.event;
312
- if (rpcEvent) {
313
- this.observeLiveSignal(rpcEvent.type, this.getLiveSignalStreamingState(rpcEvent.type));
314
- this.dispatchEvent({ ...rpcEvent, _source: 'live' });
315
- }
316
- }
317
- else if (broadcastEvent === 'rpc_response') {
318
- const response = broadcastData.response;
319
- if (response) {
320
- if (this.shouldObserveRpcResponseAsLive()) {
321
- this.observeLiveSignal('rpc_response');
322
- }
323
- this.dispatchResponse(response);
324
- }
325
- }
326
- else if (broadcastEvent === 'rpc_ui_request') {
327
- this.observeLiveSignal('rpc_ui_request');
328
- this.handleUiRequest({
329
- id: broadcastData.id,
330
- method: broadcastData.method,
331
- params: broadcastData.params ?? {},
332
- });
333
- }
334
- else {
335
- // Lifecycle event broadcasts (agent_start, message_end, turn_start, etc.)
336
- // The broadcastData IS the event payload with a `type` field matching broadcastEvent.
337
- // Dispatch as a regular event so the TUI can render agent activity.
338
- this.observeLiveSignal(broadcastEvent, this.getLiveSignalStreamingState(broadcastEvent));
339
- this.dispatchEvent({
340
- type: broadcastEvent,
341
- ...broadcastData,
342
- _source: 'live',
343
- });
344
- }
345
- return;
346
- }
347
- // Legacy/raw RPC messages — tolerated but not expected on the controller path.
348
- if (type === 'rpc_event') {
349
- const rpcEvent = data.event;
350
- if (rpcEvent) {
351
- this.applyLifecycle({ type: 'replay_event' });
352
- this.scheduleReplaySettled();
353
- this.dispatchEvent({ ...rpcEvent, _source: 'replay' });
354
- }
355
- return;
356
- }
357
- if (type === 'rpc_response') {
358
- const response = data.response;
359
- if (response) {
360
- if (this.shouldObserveRpcResponseAsLive()) {
361
- this.observeLiveSignal('rpc_response');
362
- }
363
- this.dispatchResponse(response);
364
- }
365
- return;
366
- }
367
- if (type === 'rpc_ui_request') {
368
- this.observeLiveSignal('rpc_ui_request');
369
- this.handleUiRequest({
370
- id: data.id,
371
- method: data.method,
372
- params: data.params ?? {},
373
- });
374
- return;
375
- }
376
- // Session hydration (conversation entries + task states from observer hydration)
377
- if (type === 'session_hydration') {
378
- this.applyLifecycle({
379
- type: 'hydration',
380
- leadConnected: typeof data.leadConnected === 'boolean' ? data.leadConnected : undefined,
381
- isStreaming: typeof data.streamingState
382
- ?.isStreaming === 'boolean'
383
- ? Boolean(data.streamingState.isStreaming)
384
- : undefined,
385
- });
386
- // Pass through as an event so the extension can render it
387
- for (const handler of this.eventHandlers) {
388
- try {
389
- handler({ type: 'session_hydration', ...data });
390
- }
391
- catch (err) {
392
- log(`Hydration handler error: ${err instanceof Error ? err.message : String(err)}`);
393
- }
394
- }
395
- return;
396
- }
397
- log(`Unhandled message type: ${type}`);
398
- };
399
- this.ws.onerror = (err) => {
400
- clearTimeout(connectTimeout);
401
- if (!this.connected) {
402
- const message = 'message' in err ? err.message : 'WebSocket error';
403
- reject(new Error(message));
404
- }
405
- };
406
- this.ws.onclose = () => {
407
- clearTimeout(connectTimeout);
408
- const wasConnected = this.connected;
409
- this.connected = false;
410
- this.clearReplaySettledTimer();
411
- if (!this.intentionallyClosed) {
412
- if (wasConnected) {
413
- log('WebSocket closed unexpectedly — scheduling reconnect');
414
- this.notifyConnectionChange('reconnecting');
415
- this.applyLifecycle({ type: 'connection_change', state: 'reconnecting' });
416
- this.scheduleReconnect();
417
- }
418
- else if (!isReconnect) {
419
- // Failed initial connect and not already in reconnect loop
420
- log('WebSocket closed during initial connect');
421
- }
422
- }
423
- };
424
- });
425
- }
426
- scheduleReconnect() {
427
- if (this.intentionallyClosed)
428
- return;
429
- if (this.reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
430
- log(`Max reconnect attempts (${MAX_RECONNECT_ATTEMPTS}) reached — giving up`);
431
- this.notifyConnectionChange('disconnected');
432
- this.applyLifecycle({ type: 'connection_change', state: 'disconnected' });
433
- return;
434
- }
435
- const delay = Math.min(RECONNECT_BASE_MS * Math.pow(2, this.reconnectAttempts), RECONNECT_MAX_MS);
436
- this.reconnectAttempts++;
437
- log(`Reconnect attempt ${this.reconnectAttempts} in ${delay}ms`);
438
- this.reconnectTimer = setTimeout(async () => {
439
- this.reconnectTimer = null;
440
- try {
441
- await this.doConnect();
442
- // On successful reconnect, request fresh state
443
- this.getState();
444
- }
445
- catch (err) {
446
- log(`Reconnect failed: ${err instanceof Error ? err.message : String(err)}`);
447
- this.scheduleReconnect();
448
- }
449
- }, delay);
450
- }
451
- notifyConnectionChange(state) {
452
- for (const handler of this.connectionHandlers) {
453
- try {
454
- handler(state);
455
- }
456
- catch {
457
- /* ignore */
458
- }
459
- }
460
- }
461
- /** Send an RPC command to the sandbox (prompt, steer, abort, etc.) */
462
- sendCommand(command) {
463
- if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
464
- log('Cannot send command — not connected');
465
- return;
466
- }
467
- if (this.shouldMarkResuming(command.type) && this.lifecycleState.phase === 'paused') {
468
- this.applyLifecycle({ type: 'local_resume_requested' });
469
- }
470
- this.ws.send(JSON.stringify({
471
- type: 'rpc_command',
472
- command,
473
- }));
474
- }
475
- /** Send a user prompt to the remote sandbox */
476
- prompt(message, images) {
477
- this.sendCommand({
478
- type: 'prompt',
479
- message,
480
- ...(images?.length ? { images } : {}),
481
- });
482
- }
483
- /** Steer the agent mid-turn */
484
- steer(message) {
485
- this.sendCommand({ type: 'steer', message });
486
- }
487
- /** Abort current operation */
488
- abort() {
489
- this.sendCommand({ type: 'abort' });
490
- }
491
- /** Get current session state */
492
- getState() {
493
- this.sendCommand({ type: 'get_state', id: crypto.randomUUID() });
494
- }
495
- /** Get all messages in current session */
496
- getMessages() {
497
- this.sendCommand({ type: 'get_messages', id: crypto.randomUUID() });
498
- }
499
- /** Compact the session context */
500
- compact() {
501
- this.sendCommand({ type: 'compact' });
502
- }
503
- /** Close the connection */
504
- close() {
505
- this.intentionallyClosed = true;
506
- this.clearReplaySettledTimer();
507
- if (this.reconnectTimer) {
508
- clearTimeout(this.reconnectTimer);
509
- this.reconnectTimer = null;
510
- }
511
- this.ws?.close();
512
- this.ws = null;
513
- this.applyLifecycle({ type: 'connection_change', state: 'disconnected' });
514
- }
515
- get isConnected() {
516
- return this.connected;
517
- }
518
- /** Handle UI request from sandbox — delegate to registered handler */
519
- async handleUiRequest(request) {
520
- if (!this.uiHandler) {
521
- log(`No UI handler for ${request.method} — sending null response`);
522
- this.sendUiResponse(request.id, null);
523
- return;
524
- }
525
- try {
526
- const result = await this.uiHandler(request);
527
- this.sendUiResponse(request.id, result);
528
- }
529
- catch (err) {
530
- log(`UI handler error for ${request.method}: ${err instanceof Error ? err.message : String(err)}`);
531
- this.sendUiResponse(request.id, null);
532
- }
533
- }
534
- /** Send UI response back to sandbox */
535
- sendUiResponse(id, result) {
536
- if (!this.ws || this.ws.readyState !== WebSocket.OPEN)
537
- return;
538
- this.ws.send(JSON.stringify({
539
- type: 'rpc_ui_response',
540
- id,
541
- result,
542
- }));
543
- }
544
- }
545
- /**
546
- * Set up remote mode for the Pi extension.
547
- *
548
- * Connects to an existing sandbox session through the Hub and bridges
549
- * user input → RPC commands and sandbox events → TUI rendering.
550
- *
551
- * Uses Pi's extension APIs for rich rendering:
552
- * - pi.sendMessage() for completed assistant messages
553
- * - ctx.ui.setWidget() for streaming output
554
- * - ctx.ui.setWorkingMessage() for tool execution status
555
- * - ctx.ui.setStatus() for connection and agent state
556
- */
557
- export async function setupRemoteMode(pi, hubWsUrl, sessionId) {
558
- const remote = new RemoteSession(sessionId);
559
- // ── Track streaming state for widget rendering ──
560
- let messageBuffer = '';
561
- let thinkingBuffer = '';
562
- let isStreaming = false;
563
- let currentTool = null;
564
- let extensionCtxRef = null;
565
- let lifecycleOwnsWorkingMessage = false;
566
- // Called by the extension setup to provide the rendering context
567
- remote._setExtensionCtx = (ctx) => {
568
- extensionCtxRef = ctx;
569
- applyLifecycleUi(remote.getLifecycleState());
570
- };
571
- // ── Render streaming output as a widget ──
572
- function updateStreamWidget() {
573
- if (!extensionCtxRef?.hasUI)
574
- return;
575
- if (!isStreaming && !messageBuffer)
576
- return;
577
- // Show the most recent streaming text in a widget
578
- const display = messageBuffer.length > 2000 ? `...${messageBuffer.slice(-2000)}` : messageBuffer;
579
- if (display) {
580
- extensionCtxRef.ui.setWidget('remote_stream', display.split('\n'));
581
- }
582
- }
583
- function clearStreamWidget() {
584
- if (!extensionCtxRef?.hasUI)
585
- return;
586
- extensionCtxRef.ui.setWidget('remote_stream', undefined);
587
- }
588
- function applyLifecycleUi(state) {
589
- if (!extensionCtxRef?.hasUI)
590
- return;
591
- const shortSession = state.sessionId.slice(0, 16);
592
- extensionCtxRef.ui.setStatus('remote_connection', `Remote: ${shortSession}${shortSession.length < state.sessionId.length ? '...' : ''} ${getRemoteLifecycleLabel(state)}`);
593
- const activity = getRemoteLifecycleActivityLabel(state);
594
- if (activity) {
595
- extensionCtxRef.ui.setStatus('remote_activity', activity);
596
- }
597
- else {
598
- extensionCtxRef.ui.setStatus('remote_activity', state.isStreaming ? 'agent working...' : 'idle');
599
- }
600
- lifecycleOwnsWorkingMessage = syncRemoteLifecycleWorkingMessage(state, extensionCtxRef.ui, lifecycleOwnsWorkingMessage);
601
- }
602
- function setNonLifecycleWorkingMessage(message) {
603
- if (!extensionCtxRef?.hasUI)
604
- return;
605
- extensionCtxRef.ui.setWorkingMessage(message);
606
- lifecycleOwnsWorkingMessage = false;
607
- }
608
- function clearWorkingMessage() {
609
- if (!extensionCtxRef?.hasUI)
610
- return;
611
- if (lifecycleOwnsWorkingMessage) {
612
- lifecycleOwnsWorkingMessage = clearRemoteLifecycleWorkingMessage(extensionCtxRef.ui, lifecycleOwnsWorkingMessage);
613
- return;
614
- }
615
- extensionCtxRef.ui.setWorkingMessage();
616
- lifecycleOwnsWorkingMessage = false;
617
- }
618
- // ── Set up UI handler (wired to Pi's UI later in setupRemoteModeExtension) ──
619
- // Default handler — overridden by setupRemoteModeExtension once ctx is available
620
- remote.setUiHandler(async (request) => {
621
- log(`UI request: ${request.method} (${request.id}) — no ctx yet`);
622
- const fireAndForget = ['notify', 'setStatus', 'setWidget', 'setTitle', 'set_editor_text'];
623
- if (fireAndForget.includes(request.method))
624
- return undefined;
625
- return null;
626
- });
627
- // ── Handle RPC responses (get_state, get_messages results) ──
628
- remote.onResponse((response) => {
629
- if (!response.success) {
630
- log(`RPC response error for ${response.command}: ${response.error ?? 'unknown'}`);
631
- return;
632
- }
633
- switch (response.command) {
634
- case 'get_state': {
635
- const state = response.data;
636
- if (state) {
637
- isStreaming = !!state.isStreaming;
638
- if (extensionCtxRef?.hasUI) {
639
- if (state.isStreaming) {
640
- extensionCtxRef.ui.setStatus('remote_activity', 'agent working...');
641
- }
642
- else if (state.isWaitingForInput) {
643
- extensionCtxRef.ui.setStatus('remote_activity', 'waiting for input');
644
- }
645
- else {
646
- extensionCtxRef.ui.setStatus('remote_activity', 'idle');
647
- }
648
- }
649
- log(`State hydrated: streaming=${state.isStreaming}, waiting=${state.isWaitingForInput}`);
650
- }
651
- break;
652
- }
653
- case 'get_messages': {
654
- const messages = response.data;
655
- if (messages?.length) {
656
- hydrateMessages(messages);
657
- }
658
- break;
659
- }
660
- }
661
- });
662
- // ── Hydrate message history into the TUI ──
663
- function hydrateMessages(messages) {
664
- // Show the last few messages as custom messages in the TUI
665
- const recent = messages.slice(-20);
666
- let hydrated = 0;
667
- for (const msg of recent) {
668
- const text = typeof msg.content === 'string'
669
- ? msg.content
670
- : Array.isArray(msg.content)
671
- ? msg.content
672
- .filter((c) => c.type === 'text' && typeof c.text === 'string')
673
- .map((c) => c.text)
674
- .join('\n')
675
- : '';
676
- if (!text)
677
- continue;
678
- const role = msg.role === 'assistant' ? 'assistant' : 'user';
679
- pi.sendMessage({
680
- customType: 'remote_history',
681
- content: text,
682
- display: true,
683
- details: { role, timestamp: msg.timestamp, hydrated: true },
684
- });
685
- hydrated++;
686
- }
687
- log(`Hydrated ${hydrated} messages from history`);
688
- }
689
- // ── Handle RPC events for rendering ──
690
- remote.onEvent((event) => {
691
- const eventType = event.type;
692
- switch (eventType) {
693
- case 'session_resume':
694
- log(`Session resume signaled (${typeof event.streamId === 'string' ? event.streamId : 'no stream id'})`);
695
- break;
696
- case 'session_stream_ready':
697
- log(`Durable stream ready (${typeof event.streamId === 'string' ? event.streamId : 'no stream id'})`);
698
- break;
699
- case 'rpc_command_error': {
700
- const error = typeof event.error === 'string'
701
- ? event.error
702
- : 'Remote command failed';
703
- if (extensionCtxRef?.hasUI) {
704
- extensionCtxRef.ui.notify(error, 'warning');
705
- clearWorkingMessage();
706
- }
707
- isStreaming = false;
708
- clearStreamWidget();
709
- log(`Remote command error: ${error}`);
710
- break;
711
- }
712
- case 'message_start':
713
- messageBuffer = '';
714
- thinkingBuffer = '';
715
- isStreaming = true;
716
- if (extensionCtxRef?.hasUI) {
717
- setNonLifecycleWorkingMessage('Responding...');
718
- }
719
- break;
720
- case 'message_update': {
721
- const delta = event.text ?? '';
722
- messageBuffer += delta;
723
- updateStreamWidget();
724
- break;
725
- }
726
- case 'message_end': {
727
- isStreaming = false;
728
- clearStreamWidget();
729
- clearWorkingMessage();
730
- // Extract content — prefer streamed buffer, fall back to message_end payload
731
- let finalContent = messageBuffer.trim();
732
- if (!finalContent) {
733
- // Lifecycle broadcasts include full message in the event payload
734
- const msg = event.message;
735
- if (msg) {
736
- const content = msg.content;
737
- if (typeof content === 'string') {
738
- finalContent = content.trim();
739
- }
740
- else if (Array.isArray(content)) {
741
- finalContent = content
742
- .filter((c) => !!c &&
743
- typeof c === 'object' &&
744
- c.type === 'text' &&
745
- typeof c.text === 'string')
746
- .map((c) => c.text)
747
- .join('\n')
748
- .trim();
749
- }
750
- }
751
- }
752
- // Only display assistant messages (skip user message echoes)
753
- const msgRole = event.message?.role;
754
- if (finalContent && msgRole !== 'user') {
755
- pi.sendMessage({
756
- customType: 'remote_message',
757
- content: finalContent,
758
- display: true,
759
- details: { role: 'assistant' },
760
- });
761
- }
762
- messageBuffer = '';
763
- log(`Message complete`);
764
- break;
765
- }
766
- case 'thinking_start':
767
- thinkingBuffer = '';
768
- break;
769
- case 'thinking_update': {
770
- const delta = event.text ?? '';
771
- thinkingBuffer += delta;
772
- break;
773
- }
774
- case 'thinking_end':
775
- // Thinking is internal — just log it
776
- if (thinkingBuffer) {
777
- log(`Thinking complete (${thinkingBuffer.length} chars)`);
778
- }
779
- thinkingBuffer = '';
780
- break;
781
- case 'agent_start': {
782
- const agent = event.agentName ?? 'agent';
783
- if (extensionCtxRef?.hasUI) {
784
- extensionCtxRef.ui.setStatus('remote_activity', `${agent} working...`);
785
- }
786
- log(`Agent started: ${agent}`);
787
- break;
788
- }
789
- case 'agent_end':
790
- if (extensionCtxRef?.hasUI) {
791
- extensionCtxRef.ui.setStatus('remote_activity', 'idle');
792
- }
793
- clearStreamWidget();
794
- log(`Agent ended`);
795
- break;
796
- case 'tool_execution_start': {
797
- const tool = event.toolName ?? 'tool';
798
- currentTool = tool;
799
- if (extensionCtxRef?.hasUI) {
800
- setNonLifecycleWorkingMessage(`Running ${tool}...`);
801
- extensionCtxRef.ui.setStatus('remote_activity', `Running ${tool}...`);
802
- }
803
- log(`Tool: ${tool}`);
804
- break;
805
- }
806
- case 'tool_execution_end': {
807
- const tool = event.toolName ?? currentTool ?? 'tool';
808
- currentTool = null;
809
- if (extensionCtxRef?.hasUI) {
810
- clearWorkingMessage();
811
- extensionCtxRef.ui.setStatus('remote_activity', 'agent working...');
812
- }
813
- log(`Tool done: ${tool}`);
814
- break;
815
- }
816
- case 'turn_start':
817
- if (extensionCtxRef?.hasUI) {
818
- extensionCtxRef.ui.setStatus('remote_activity', 'agent working...');
819
- }
820
- log('Turn started');
821
- break;
822
- case 'turn_end':
823
- if (extensionCtxRef?.hasUI) {
824
- extensionCtxRef.ui.setStatus('remote_activity', 'idle');
825
- }
826
- clearWorkingMessage();
827
- clearStreamWidget();
828
- log('Turn ended');
829
- break;
830
- case 'session_hydration': {
831
- // Hydrate conversation history from Hub
832
- const entries = event.entries;
833
- if (entries?.length) {
834
- let hydrated = 0;
835
- for (const entry of entries.slice(-30)) {
836
- if (!entry.content)
837
- continue;
838
- const role = entry.type === 'message' ? 'assistant' : 'user';
839
- pi.sendMessage({
840
- customType: 'remote_history',
841
- content: entry.content,
842
- display: true,
843
- details: { role, timestamp: entry.timestamp, hydrated: true },
844
- });
845
- hydrated++;
846
- }
847
- log(`Hydrated ${hydrated} entries from session_hydration`);
848
- }
849
- else {
850
- log('Received session_hydration with no entries');
851
- }
852
- break;
853
- }
854
- case 'auto_compaction_start':
855
- if (extensionCtxRef?.hasUI) {
856
- setNonLifecycleWorkingMessage('Compacting context...');
857
- }
858
- break;
859
- case 'auto_compaction_end':
860
- clearWorkingMessage();
861
- break;
862
- }
863
- });
864
- // ── Connection/lifecycle state handling ──
865
- remote.onLifecycleChange((state) => {
866
- applyLifecycleUi(state);
867
- });
868
- // Connect to Hub after all listeners are attached so hydration/replay frames are not dropped.
869
- await remote.connect(hubWsUrl);
870
- log(`Remote mode active — session ${sessionId}`);
871
- // Request initial state from the sandbox
872
- remote.getState();
873
- remote.getMessages();
874
- return remote;
875
- }
876
- //# sourceMappingURL=remote-session.js.map