import { defineHandler } from '@kb-labs/sdk';
import { SessionManager } from '@kb-labs/agent-core';
import { ChangeStore, generateUnifiedDiff, countDiffLines } from '@kb-labs/agent-history';

// src/rest/handlers/get-file-diff-handler.ts
var get_file_diff_handler_default = defineHandler({
  async execute(ctx, input) {
    const params = input.params;
    const sessionId = params?.sessionId;
    const changeId = params?.changeId;
    if (!sessionId) {
      throw new Error("Session ID is required");
    }
    if (!changeId) {
      throw new Error("Change ID is required");
    }
    const sessionManager = new SessionManager(ctx.cwd);
    const session = await sessionManager.loadSession(sessionId);
    const workingDir = session?.workingDir || ctx.cwd;
    const store = new ChangeStore(workingDir);
    const change = await store.get(sessionId, changeId);
    if (!change) {
      throw new Error(`Change not found: ${changeId} in session ${sessionId}`);
    }
    const diff = generateUnifiedDiff(change.filePath, change.before?.content, change.after.content, change.operation);
    const { added, removed } = countDiffLines(diff);
    return {
      changeId: change.id,
      filePath: change.filePath,
      operation: change.operation,
      diff,
      before: change.before ? { hash: change.before.hash, size: change.before.size } : void 0,
      after: { hash: change.after.hash, size: change.after.size },
      linesAdded: added,
      linesRemoved: removed,
      isNew: !change.before,
      timestamp: change.timestamp
    };
  }
});

export { get_file_diff_handler_default as default };
//# sourceMappingURL=get-file-diff-handler.js.map
//# sourceMappingURL=get-file-diff-handler.js.map