All files / src pi-adapter.ts

77.77% Statements 7/9
100% Branches 0/0
75% Functions 6/8
77.77% Lines 7/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                            2x 45x                         25x       25x       20x       20x       13x      
import type {
  ExtensionAPI,
  ExtensionHandler,
  SessionStartEvent,
  SessionShutdownEvent,
  BeforeAgentStartEvent,
  BeforeAgentStartEventResult,
  InputEvent,
  InputEventResult,
  TurnEndEvent,
  RegisteredCommand,
} from "@earendil-works/pi-coding-agent";
 
/**
 * Sole isolation point for all Pi SDK calls.
 * No other Core file may import ExtensionAPI directly — pass PiAdapter instead.
 * This makes the Pi boundary explicit and allows tests to mock the adapter.
 *
 * Note: resources_discover is not wrapped here because its event/result types
 * are not exported from the SDK's main entry point. The gatekeeper extension
 * (a CS-specific extension being removed in Phase 5) calls pi.on directly.
 */
export class PiAdapter {
  constructor(private readonly pi: ExtensionAPI) {}
 
  onSessionStart(handler: ExtensionHandler<SessionStartEvent>): void {
    this.pi.on("session_start", handler);
  }
 
  onSessionShutdown(handler: ExtensionHandler<SessionShutdownEvent>): void {
    this.pi.on("session_shutdown", handler);
  }
 
  onBeforeAgentStart(
    handler: ExtensionHandler<BeforeAgentStartEvent, BeforeAgentStartEventResult>
  ): void {
    this.pi.on("before_agent_start", handler);
  }
 
  onInput(handler: ExtensionHandler<InputEvent, InputEventResult>): void {
    this.pi.on("input", handler);
  }
 
  onTurnEnd(handler: ExtensionHandler<TurnEndEvent>): void {
    this.pi.on("turn_end", handler);
  }
 
  registerCommand(name: string, options: Omit<RegisteredCommand, "name" | "sourceInfo">): void {
    this.pi.registerCommand(name, options);
  }
 
  sendUserMessage(content: string): void {
    this.pi.sendUserMessage(content);
  }
}