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