import { defineHandler } from '@kb-labs/sdk';
import { SessionManager } from '@kb-labs/agent-core';
import { SnapshotStorage } from '@kb-labs/agent-history';

// src/rest/handlers/approve-handler.ts
var approve_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 body = input.body ?? {};
    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 storage = new SnapshotStorage(workingDir);
    let changes = await storage.listSnapshots(sessionId);
    if (body.runId) {
      changes = changes.filter((c) => c.runId === body.runId);
    } else if (body.changeIds?.length) {
      const ids = new Set(body.changeIds);
      changes = changes.filter((c) => ids.has(c.id));
    }
    const approvedAt = (/* @__PURE__ */ new Date()).toISOString();
    const approvedIds = [];
    for (const change of changes) {
      change.approved = true;
      change.approvedAt = approvedAt;
      await storage.saveSnapshot(sessionId, change);
      approvedIds.push(change.id);
    }
    if (body.runId && approvedIds.length > 0) {
      const updatedSummaries = changes.map((c) => ({
        changeId: c.id,
        filePath: c.filePath,
        operation: c.operation,
        timestamp: c.timestamp,
        linesAdded: c.metadata?.linesAdded,
        linesRemoved: c.metadata?.linesRemoved,
        isNew: !c.before,
        sizeAfter: c.after.size,
        approved: true,
        approvedAt
      }));
      await sessionManager.updateTurnFileChanges(sessionId, body.runId, updatedSummaries);
    }
    ctx.platform.logger.info(
      `[approve-handler] Session ${sessionId}: approved ${approvedIds.length} changes`
    );
    return { approved: approvedIds.length, changeIds: approvedIds, approvedAt };
  }
});

export { approve_handler_default as default };
//# sourceMappingURL=approve-handler.js.map
//# sourceMappingURL=approve-handler.js.map