2026-03-12 23:45:45 +02:00
|
|
|
import { createOpencodeClient } from '@opencode-ai/sdk/v2';
|
2026-01-28 14:26:42 +02:00
|
|
|
import type { OpenCodeManager } from './opencode';
|
|
|
|
|
|
2026-06-03 02:42:00 +03:00
|
|
|
// Session activity tracking (mirrors web server and desktop behavior)
|
2026-01-28 14:26:42 +02:00
|
|
|
type ActivityPhase = 'idle' | 'busy' | 'cooldown';
|
|
|
|
|
|
|
|
|
|
interface SessionActivity {
|
|
|
|
|
sessionId: string;
|
|
|
|
|
phase: ActivityPhase;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sessionActivityPhases = new Map<string, { phase: ActivityPhase; updatedAt: number }>();
|
|
|
|
|
const sessionActivityCooldowns = new Map<string, NodeJS.Timeout>();
|
|
|
|
|
const SESSION_COOLDOWN_DURATION_MS = 2000;
|
|
|
|
|
|
|
|
|
|
let globalEventWatcherAbortController: AbortController | null = null;
|
|
|
|
|
let chatViewProvider: { postMessage: (message: unknown) => void } | null = null;
|
2026-05-12 04:09:49 -04:00
|
|
|
let globalEventWatcherRetryTimer: NodeJS.Timeout | null = null;
|
|
|
|
|
let globalEventWatcherStartToken = 0;
|
|
|
|
|
|
|
|
|
|
const clearGlobalEventWatcherRetry = (): void => {
|
|
|
|
|
if (!globalEventWatcherRetryTimer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
clearTimeout(globalEventWatcherRetryTimer);
|
|
|
|
|
globalEventWatcherRetryTimer = null;
|
|
|
|
|
};
|
2026-01-28 14:26:42 +02:00
|
|
|
|
2026-05-19 02:06:32 +03:00
|
|
|
const unwrapGlobalEventPayload = (eventData: unknown): Record<string, unknown> | null => {
|
|
|
|
|
if (!eventData || typeof eventData !== 'object') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const record = eventData as { payload?: unknown };
|
|
|
|
|
if (record.payload && typeof record.payload === 'object') {
|
|
|
|
|
return record.payload as Record<string, unknown>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return eventData as Record<string, unknown>;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-06 23:45:31 +07:00
|
|
|
const reconcileSessionActivityFromStatus = async (manager: OpenCodeManager): Promise<void> => {
|
|
|
|
|
const baseUrl = manager.getApiUrl();
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const url = new URL('/session/status', baseUrl);
|
|
|
|
|
const response = await fetch(url.toString(), {
|
|
|
|
|
headers: manager.getOpenCodeAuthHeaders(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`session status fetch failed (${response.status})`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const statuses = await response.json() as Record<string, { type?: string }>;
|
|
|
|
|
const knownSessionIds = new Set(Object.keys(statuses || {}));
|
|
|
|
|
|
|
|
|
|
for (const [sessionId, data] of Object.entries(statuses || {})) {
|
|
|
|
|
const type = typeof data?.type === 'string' ? data.type : 'idle';
|
|
|
|
|
const phase: ActivityPhase = type === 'busy' || type === 'retry' ? 'busy' : 'idle';
|
|
|
|
|
setSessionActivityPhase(sessionId, phase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Drop stale in-memory activity entries not present in authoritative status.
|
|
|
|
|
for (const sessionId of Array.from(sessionActivityPhases.keys())) {
|
|
|
|
|
if (!knownSessionIds.has(sessionId)) {
|
|
|
|
|
setSessionActivityPhase(sessionId, 'idle');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 14:26:42 +02:00
|
|
|
const setSessionActivityPhase = (sessionId: string, phase: ActivityPhase): void => {
|
|
|
|
|
if (!sessionId) return;
|
|
|
|
|
|
|
|
|
|
const existingTimer = sessionActivityCooldowns.get(sessionId);
|
|
|
|
|
if (existingTimer) {
|
|
|
|
|
clearTimeout(existingTimer);
|
|
|
|
|
sessionActivityCooldowns.delete(sessionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const current = sessionActivityPhases.get(sessionId);
|
2026-05-19 02:06:32 +03:00
|
|
|
if (current?.phase === phase) return;
|
2026-01-28 14:26:42 +02:00
|
|
|
|
|
|
|
|
sessionActivityPhases.set(sessionId, { phase, updatedAt: Date.now() });
|
|
|
|
|
|
2026-05-19 02:06:32 +03:00
|
|
|
chatViewProvider?.postMessage({
|
|
|
|
|
type: 'openchamber:session-activity',
|
|
|
|
|
properties: {
|
|
|
|
|
sessionId,
|
|
|
|
|
phase,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-01-28 14:26:42 +02:00
|
|
|
|
|
|
|
|
if (phase === 'cooldown') {
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
const now = sessionActivityPhases.get(sessionId);
|
|
|
|
|
if (now?.phase === 'cooldown') {
|
|
|
|
|
sessionActivityPhases.set(sessionId, { phase: 'idle', updatedAt: Date.now() });
|
2026-05-19 02:06:32 +03:00
|
|
|
chatViewProvider?.postMessage({
|
|
|
|
|
type: 'openchamber:session-activity',
|
|
|
|
|
properties: {
|
|
|
|
|
sessionId,
|
|
|
|
|
phase: 'idle',
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-01-28 14:26:42 +02:00
|
|
|
}
|
|
|
|
|
sessionActivityCooldowns.delete(sessionId);
|
|
|
|
|
}, SESSION_COOLDOWN_DURATION_MS);
|
|
|
|
|
sessionActivityCooldowns.set(sessionId, timer);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-31 18:47:00 +03:00
|
|
|
export const getSessionActivitySnapshot = (): Record<string, { type: ActivityPhase }> => {
|
|
|
|
|
const snapshot: Record<string, { type: ActivityPhase }> = {};
|
|
|
|
|
for (const [sessionId, data] of sessionActivityPhases.entries()) {
|
|
|
|
|
snapshot[sessionId] = { type: data.phase };
|
|
|
|
|
}
|
|
|
|
|
return snapshot;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 14:26:42 +02:00
|
|
|
const deriveSessionActivity = (payload: Record<string, unknown>): SessionActivity | null => {
|
|
|
|
|
if (!payload || typeof payload !== 'object') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const type = payload.type as string;
|
|
|
|
|
const properties = (payload.properties ?? payload) as Record<string, unknown>;
|
|
|
|
|
|
|
|
|
|
if (type === 'session.status') {
|
|
|
|
|
const status = properties?.status as Record<string, unknown> | undefined;
|
2026-05-06 23:45:31 +07:00
|
|
|
const info = properties?.info as Record<string, unknown> | undefined;
|
2026-01-28 14:26:42 +02:00
|
|
|
const sessionId = (properties?.sessionID ?? properties?.sessionId) as string;
|
2026-05-06 23:45:31 +07:00
|
|
|
const statusType = (status?.type ?? info?.type) as string;
|
2026-01-28 14:26:42 +02:00
|
|
|
|
|
|
|
|
if (typeof sessionId === 'string' && sessionId.length > 0 && typeof statusType === 'string') {
|
|
|
|
|
const phase = statusType === 'busy' || statusType === 'retry' ? 'busy' : 'idle';
|
|
|
|
|
return { sessionId, phase };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 18:25:03 +02:00
|
|
|
if (type === 'message.updated' || type === 'message.part.updated' || type === 'message.part.delta') {
|
2026-01-28 14:26:42 +02:00
|
|
|
const info = properties?.info as Record<string, unknown> | undefined;
|
|
|
|
|
const sessionId = (info?.sessionID ?? info?.sessionId ?? properties?.sessionID ?? properties?.sessionId) as string;
|
|
|
|
|
const role = info?.role as string;
|
|
|
|
|
const finish = info?.finish as string;
|
|
|
|
|
if (typeof sessionId === 'string' && sessionId.length > 0 && role === 'assistant' && finish === 'stop') {
|
|
|
|
|
return { sessionId, phase: 'cooldown' };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'session.idle') {
|
|
|
|
|
const sessionId = (properties?.sessionID ?? properties?.sessionId) as string;
|
|
|
|
|
if (typeof sessionId === 'string' && sessionId.length > 0) {
|
|
|
|
|
return { sessionId, phase: 'idle' };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const waitForOpenCodePort = async (manager: OpenCodeManager, timeoutMs = 30000): Promise<number | null> => {
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
while (Date.now() - start < timeoutMs) {
|
|
|
|
|
const apiUrl = manager.getApiUrl();
|
|
|
|
|
if (apiUrl) {
|
|
|
|
|
try {
|
|
|
|
|
const url = new URL(apiUrl);
|
|
|
|
|
if (url.port) {
|
|
|
|
|
return parseInt(url.port, 10);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await new Promise(r => setTimeout(r, 500));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const startGlobalEventWatcher = async (
|
|
|
|
|
manager: OpenCodeManager,
|
|
|
|
|
provider: { postMessage: (message: unknown) => void }
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
if (globalEventWatcherAbortController) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 04:09:49 -04:00
|
|
|
const startToken = ++globalEventWatcherStartToken;
|
|
|
|
|
clearGlobalEventWatcherRetry();
|
2026-01-28 14:26:42 +02:00
|
|
|
chatViewProvider = provider;
|
|
|
|
|
|
|
|
|
|
const port = await waitForOpenCodePort(manager);
|
2026-05-12 04:09:49 -04:00
|
|
|
if (startToken !== globalEventWatcherStartToken) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-28 14:26:42 +02:00
|
|
|
if (!port) {
|
|
|
|
|
console.warn('[VSCode:Activity] OpenCode port unavailable; will retry');
|
2026-05-12 04:09:49 -04:00
|
|
|
globalEventWatcherRetryTimer = setTimeout(() => {
|
|
|
|
|
globalEventWatcherRetryTimer = null;
|
|
|
|
|
if (startToken === globalEventWatcherStartToken) {
|
|
|
|
|
void startGlobalEventWatcher(manager, provider);
|
|
|
|
|
}
|
|
|
|
|
}, 2000);
|
2026-01-28 14:26:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalEventWatcherAbortController = new AbortController();
|
|
|
|
|
const signal = globalEventWatcherAbortController.signal;
|
|
|
|
|
|
|
|
|
|
let attempt = 0;
|
|
|
|
|
|
|
|
|
|
const run = async (): Promise<void> => {
|
|
|
|
|
while (!signal.aborted) {
|
|
|
|
|
attempt += 1;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const baseUrl = manager.getApiUrl();
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
|
throw new Error('OpenCode API URL not available');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:45:45 +02:00
|
|
|
const client = createOpencodeClient({
|
|
|
|
|
baseUrl,
|
|
|
|
|
headers: manager.getOpenCodeAuthHeaders(),
|
|
|
|
|
});
|
2026-05-06 23:45:31 +07:00
|
|
|
try {
|
|
|
|
|
await reconcileSessionActivityFromStatus(manager);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(
|
|
|
|
|
'[VSCode:Activity] session status reconcile failed',
|
|
|
|
|
error instanceof Error ? error.message : error,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-12 23:45:45 +02:00
|
|
|
const result = await client.global.event({
|
2026-01-28 14:26:42 +02:00
|
|
|
signal,
|
2026-03-12 23:45:45 +02:00
|
|
|
sseMaxRetryAttempts: 0,
|
2026-01-28 14:26:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('[VSCode:Activity] connected');
|
|
|
|
|
|
2026-05-19 02:06:32 +03:00
|
|
|
for await (const event of result.stream) {
|
|
|
|
|
const payload = unwrapGlobalEventPayload((event as { payload?: unknown }).payload ?? event);
|
|
|
|
|
if (payload) {
|
|
|
|
|
const activity = deriveSessionActivity(payload);
|
|
|
|
|
if (activity) {
|
|
|
|
|
setSessionActivityPhase(activity.sessionId, activity.phase);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:45:45 +02:00
|
|
|
if (signal.aborted) {
|
2026-01-28 14:26:42 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (signal.aborted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.warn('[VSCode:Activity] disconnected', error instanceof Error ? error.message : error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const backoffMs = Math.min(1000 * Math.pow(2, Math.min(attempt, 5)), 30000);
|
|
|
|
|
await new Promise(r => setTimeout(r, backoffMs));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void run();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const stopGlobalEventWatcher = (): void => {
|
2026-05-12 04:09:49 -04:00
|
|
|
globalEventWatcherStartToken += 1;
|
|
|
|
|
clearGlobalEventWatcherRetry();
|
|
|
|
|
|
|
|
|
|
if (globalEventWatcherAbortController) {
|
|
|
|
|
try {
|
|
|
|
|
globalEventWatcherAbortController.abort();
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2026-01-28 14:26:42 +02:00
|
|
|
}
|
|
|
|
|
globalEventWatcherAbortController = null;
|
|
|
|
|
chatViewProvider = null;
|
|
|
|
|
|
|
|
|
|
for (const timer of sessionActivityCooldowns.values()) {
|
|
|
|
|
clearTimeout(timer);
|
|
|
|
|
}
|
|
|
|
|
sessionActivityCooldowns.clear();
|
2026-05-12 04:12:01 -04:00
|
|
|
sessionActivityPhases.clear();
|
2026-01-28 14:26:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setChatViewProvider = (provider: { postMessage: (message: unknown) => void } | null): void => {
|
|
|
|
|
chatViewProvider = provider;
|
|
|
|
|
};
|