import { defineHandler } from '@kb-labs/sdk';
import { SessionManager, PlanDocumentService } from '@kb-labs/agent-core';
import { promises } from 'fs';

// src/rest/handlers/get-session-plan-handler.ts
async function loadPlan(planPath) {
  try {
    const content = await promises.readFile(planPath, "utf-8");
    return JSON.parse(content);
  } catch {
    return null;
  }
}
async function pathExists(filePath) {
  try {
    await promises.access(filePath);
    return true;
  } catch {
    return false;
  }
}
var get_session_plan_handler_default = defineHandler({
  async execute(ctx, input) {
    const params = input.params;
    const sessionId = params?.sessionId;
    if (!sessionId) {
      throw new Error("Session ID is required");
    }
    const sessionManager = new SessionManager(ctx.cwd);
    const session = await sessionManager.loadSession(sessionId);
    if (!session) {
      throw new Error(`Session not found: ${sessionId}`);
    }
    const workingDir = session.workingDir || ctx.cwd;
    const manager = new SessionManager(workingDir);
    const planPath = manager.getSessionPlanPath(sessionId);
    const plan = await loadPlan(planPath);
    if (!plan) {
      return { sessionId, plan: null };
    }
    const documentService = new PlanDocumentService(workingDir);
    const canonicalPath = documentService.getPlanPath(plan);
    return {
      sessionId,
      plan,
      planPath: await pathExists(canonicalPath) ? canonicalPath : void 0
    };
  }
});

export { get_session_plan_handler_default as default };
//# sourceMappingURL=get-session-plan-handler.js.map
//# sourceMappingURL=get-session-plan-handler.js.map