Tool to send cross-session opencode messages, including as request-response pattern
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

implement active session streamer

rektide acb6818f e7c2a9a3

+53
+53
src/session/active.ts
··· 1 + import { Instance } from "../sensor/trait.js"; 2 + 3 + export interface ActiveSession { 4 + sessionID: string; 5 + port: number; 6 + status: "busy" | "retry" | "idle"; 7 + retryAttempt?: number; 8 + retryMessage?: string; 9 + retryNext?: number; 10 + } 11 + 12 + async function getSessionStatus(port: number): Promise<Record<string, any> | undefined> { 13 + try { 14 + const response = await fetch(`http://localhost:${port}/api/session/status`); 15 + if (!response.ok) { 16 + return undefined; 17 + } 18 + return await response.json(); 19 + } catch { 20 + return undefined; 21 + } 22 + } 23 + 24 + export async function* getActiveSessions( 25 + instances: AsyncGenerator<Instance>, 26 + ): AsyncGenerator<ActiveSession> { 27 + for await (const instance of instances) { 28 + const statusMap = await getSessionStatus(instance.port); 29 + 30 + if (!statusMap) { 31 + continue; 32 + } 33 + 34 + for (const [sessionID, status] of Object.entries(statusMap)) { 35 + if (typeof status !== "object" || status === null) { 36 + continue; 37 + } 38 + 39 + const statusType = status.type; 40 + 41 + if (statusType === "busy" || statusType === "retry" || statusType === "idle") { 42 + yield { 43 + sessionID, 44 + port: instance.port, 45 + status: statusType, 46 + retryAttempt: status.attempt, 47 + retryMessage: status.message, 48 + retryNext: status.next, 49 + }; 50 + } 51 + } 52 + } 53 + }