plan-mode-policy.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {
  2. ENTER_PLAN_MODE_TOOL_NAME,
  3. EXIT_PLAN_MODE_TOOL_NAME,
  4. type CollaborationMode,
  5. } from "@zcode/contracts";
  6. interface PlanModeTransitionContext {
  7. toolName: string;
  8. mode: CollaborationMode;
  9. planEnabled?: boolean;
  10. prePlanMode?: Exclude<CollaborationMode, "plan">;
  11. }
  12. export interface PlanModeTransitionPermission {
  13. behavior: "allow" | "deny";
  14. reason: string;
  15. ruleId: string;
  16. }
  17. export function resolvePlanModeTransitionPermission(
  18. context: PlanModeTransitionContext,
  19. ): PlanModeTransitionPermission | undefined {
  20. if (context.toolName === ENTER_PLAN_MODE_TOOL_NAME) {
  21. return {
  22. behavior: "allow",
  23. reason: "EnterPlanMode switches to plan mode without a permission prompt",
  24. ruleId: "tool.plan.enter",
  25. };
  26. }
  27. if (
  28. context.toolName === EXIT_PLAN_MODE_TOOL_NAME &&
  29. !(context.planEnabled ?? context.mode === "plan")
  30. ) {
  31. return {
  32. behavior: "deny",
  33. reason: "ExitPlanMode can only be used while plan mode is active",
  34. ruleId: "mode.plan.exitOnly",
  35. };
  36. }
  37. return undefined;
  38. }