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

handle.ts (3800B)


      1 import type { OpenpodContext } from "../context";
      2 import { access, mkdir, rm } from "node:fs/promises";
      3 import * as path from "node:path";
      4 
      5 export type HandleParams = {
      6   ctx: OpenpodContext;
      7   sessionId: string;
      8   dir: string;
      9   move: (sessionId: string, directory: string) => void;
     10 };
     11 
     12 export async function handle(params: HandleParams): Promise<void> {
     13   const { ctx: deps, sessionId, dir, move } = params;
     14   const { logger, openpodRoot, exec } = deps;
     15 
     16   async function isGitEnabled(dir: string): Promise<boolean> {
     17     const result = await exec("git", [
     18       "-C",
     19       dir,
     20       "rev-parse",
     21       "--is-inside-work-tree",
     22     ]);
     23     return result.status === 0;
     24   }
     25 
     26   async function getOriginUrl(dir: string): Promise<string | null> {
     27     const result = await exec(
     28       "git",
     29       ["-C", dir, "remote", "get-url", "origin"],
     30       {
     31         encoding: "utf8",
     32       },
     33     );
     34     if (result.status !== 0 || !result.stdout.trim()) {
     35       return null;
     36     }
     37     return result.stdout.trim();
     38   }
     39 
     40   async function limaInstanceExists(name: string): Promise<boolean> {
     41     const result = await exec("limactl", ["list", name]);
     42     return result.status === 0;
     43   }
     44 
     45   async function createLimaInstance(
     46     sessionId: string,
     47     target: string,
     48   ): Promise<void> {
     49     logger.log(`creating lima instance ${sessionId} (mount-only ${target}:w)`);
     50     const createResult = await exec("limactl", [
     51       "create",
     52       `--name=${sessionId}`,
     53       `--mount-only=${target}:w`,
     54       "template:default",
     55       "--yes",
     56     ]);
     57     if (createResult.status !== 0) {
     58       logger.log(`limactl create exited with ${createResult.status}`);
     59     }
     60   }
     61 
     62   async function startLimaInstance(sessionId: string): Promise<void> {
     63     logger.log(`starting lima instance ${sessionId}`);
     64     const startResult = await exec("limactl", ["start", sessionId, "--yes"]);
     65     if (startResult.status !== 0) {
     66       logger.log(`limactl start exited with ${startResult.status}`);
     67     }
     68   }
     69 
     70   async function cloneProject(
     71     originUrl: string,
     72     target: string,
     73   ): Promise<boolean> {
     74     await mkdir(openpodRoot, { recursive: true });
     75     const result = await exec("git", ["clone", originUrl, target]);
     76     if (result.status !== 0) {
     77       logger.log(`git clone failed with exit code ${result.status}`);
     78       try {
     79         await rm(target, { recursive: true, force: true });
     80       } catch {
     81         // ignore cleanup failure
     82       }
     83       return false;
     84     }
     85     return true;
     86   }
     87 
     88   async function hasTarget(target: string): Promise<boolean> {
     89     try {
     90       await access(target);
     91       return true;
     92     } catch {
     93       return false;
     94     }
     95   }
     96 
     97   const gitEnabled = await isGitEnabled(dir);
     98   logger.log(`session ${sessionId} prompt - gitEnabled: ${gitEnabled}`);
     99 
    100   if (!gitEnabled) {
    101     logger.log(`git is not enabled`);
    102     return;
    103   }
    104 
    105   const target = path.join(openpodRoot, sessionId);
    106   const needsMove = dir !== target;
    107 
    108   logger.log(
    109     `needs move for project dir ${dir}? ${needsMove} (target=${target})`,
    110   );
    111 
    112   if (needsMove) {
    113     const targetExists = await hasTarget(target);
    114 
    115     if (targetExists) {
    116       logger.log(`${target} already exists`);
    117       move(sessionId, target);
    118     } else {
    119       const originUrl = await getOriginUrl(dir);
    120       if (!originUrl) {
    121         logger.log(`no origin remote configured`);
    122         return;
    123       }
    124 
    125       logger.log(`cloning from origin ${originUrl} -> ${target}`);
    126       if (!(await cloneProject(originUrl, target))) {
    127         return;
    128       }
    129 
    130       logger.log(`clone complete, moving session to ${target}`);
    131       move(sessionId, target);
    132     }
    133   } else {
    134     logger.log(`already in ${target} directory`);
    135   }
    136 
    137   if (!(await limaInstanceExists(sessionId))) {
    138     await createLimaInstance(sessionId, target);
    139   }
    140 
    141   await startLimaInstance(sessionId);
    142 }