openpod

An opencode plugin that spins up a dedicated virtual machine for each session to isolate agent work.
git clone https://unitehenry.com/git/openpod.git
Log | Files | Refs | README

index.ts (1369B)


      1 import type { Plugin } from "@opencode/plugin";
      2 
      3 import type { OpenpodContext } from "../context";
      4 
      5 export async function registerController(
      6   ctx: Plugin.Context,
      7   deps: OpenpodContext,
      8 ): Promise<() => void> {
      9   // Listen for session removal (via events, since there is no ctx.session.hook("remove"))
     10   // and clean up the corresponding openpod directory + lima instance.
     11   const controller = new AbortController();
     12   void (async () => {
     13     try {
     14       for await (const ev of ctx.event.subscribe({
     15         signal: controller.signal,
     16       })) {
     17         if (ev.type === "session.deleted") {
     18           // eslint-disable-next-line @typescript-eslint/no-explicit-any
     19           const props: any = (ev as any).properties ?? ev;
     20           const sessionID: string | undefined =
     21             props.data?.sessionID ??
     22             props.sessionID ??
     23             props.id ??
     24             props.info?.id ??
     25             // eslint-disable-next-line @typescript-eslint/no-explicit-any
     26             (ev as any).sessionID;
     27           if (sessionID) {
     28             void deps.cleanup.execute(sessionID);
     29           }
     30         }
     31       }
     32       // eslint-disable-next-line @typescript-eslint/no-explicit-any
     33     } catch (err: any) {
     34       if (err?.name !== "AbortError") {
     35         deps.logger.log(`event subscription error: ${err}`);
     36       }
     37     }
     38   })();
     39 
     40   return () => controller.abort();
     41 }