|
| 1 | +import { GlobalState, ClineMessage, ClineAsk } from "@roo-code/types" |
| 2 | +import { getApiMetrics } from "../../shared/getApiMetrics" |
| 3 | +import { ClineAskResponse } from "../../shared/WebviewMessage" |
| 4 | + |
| 5 | +export interface AutoApprovalResult { |
| 6 | + shouldProceed: boolean |
| 7 | + requiresApproval: boolean |
| 8 | + approvalType?: "requests" | "cost" |
| 9 | + approvalCount?: number | string |
| 10 | +} |
| 11 | + |
| 12 | +export class AutoApprovalHandler { |
| 13 | + private consecutiveAutoApprovedRequestsCount: number = 0 |
| 14 | + private consecutiveAutoApprovedCost: number = 0 |
| 15 | + |
| 16 | + /** |
| 17 | + * Check if auto-approval limits have been reached and handle user approval if needed |
| 18 | + */ |
| 19 | + async checkAutoApprovalLimits( |
| 20 | + state: GlobalState | undefined, |
| 21 | + messages: ClineMessage[], |
| 22 | + askForApproval: ( |
| 23 | + type: ClineAsk, |
| 24 | + data: string, |
| 25 | + ) => Promise<{ response: ClineAskResponse; text?: string; images?: string[] }>, |
| 26 | + ): Promise<AutoApprovalResult> { |
| 27 | + // Check request count limit |
| 28 | + const requestResult = await this.checkRequestLimit(state, askForApproval) |
| 29 | + if (!requestResult.shouldProceed || requestResult.requiresApproval) { |
| 30 | + return requestResult |
| 31 | + } |
| 32 | + |
| 33 | + // Check cost limit |
| 34 | + const costResult = await this.checkCostLimit(state, messages, askForApproval) |
| 35 | + return costResult |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Increment the request counter and check if limit is exceeded |
| 40 | + */ |
| 41 | + private async checkRequestLimit( |
| 42 | + state: GlobalState | undefined, |
| 43 | + askForApproval: ( |
| 44 | + type: ClineAsk, |
| 45 | + data: string, |
| 46 | + ) => Promise<{ response: ClineAskResponse; text?: string; images?: string[] }>, |
| 47 | + ): Promise<AutoApprovalResult> { |
| 48 | + const maxRequests = state?.allowedMaxRequests || Infinity |
| 49 | + |
| 50 | + // Increment the counter for each new API request |
| 51 | + this.consecutiveAutoApprovedRequestsCount++ |
| 52 | + |
| 53 | + if (this.consecutiveAutoApprovedRequestsCount > maxRequests) { |
| 54 | + const { response } = await askForApproval( |
| 55 | + "auto_approval_max_req_reached", |
| 56 | + JSON.stringify({ count: maxRequests, type: "requests" }), |
| 57 | + ) |
| 58 | + |
| 59 | + // If we get past the promise, it means the user approved and did not start a new task |
| 60 | + if (response === "yesButtonClicked") { |
| 61 | + this.consecutiveAutoApprovedRequestsCount = 0 |
| 62 | + return { |
| 63 | + shouldProceed: true, |
| 64 | + requiresApproval: true, |
| 65 | + approvalType: "requests", |
| 66 | + approvalCount: maxRequests, |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + shouldProceed: false, |
| 72 | + requiresApproval: true, |
| 73 | + approvalType: "requests", |
| 74 | + approvalCount: maxRequests, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return { shouldProceed: true, requiresApproval: false } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Calculate current cost and check if limit is exceeded |
| 83 | + */ |
| 84 | + private async checkCostLimit( |
| 85 | + state: GlobalState | undefined, |
| 86 | + messages: ClineMessage[], |
| 87 | + askForApproval: ( |
| 88 | + type: ClineAsk, |
| 89 | + data: string, |
| 90 | + ) => Promise<{ response: ClineAskResponse; text?: string; images?: string[] }>, |
| 91 | + ): Promise<AutoApprovalResult> { |
| 92 | + const maxCost = state?.allowedMaxCost || Infinity |
| 93 | + |
| 94 | + // Calculate total cost from messages |
| 95 | + this.consecutiveAutoApprovedCost = getApiMetrics(messages).totalCost |
| 96 | + |
| 97 | + // Use epsilon for floating-point comparison to avoid precision issues |
| 98 | + const EPSILON = 0.0001 |
| 99 | + if (this.consecutiveAutoApprovedCost > maxCost + EPSILON) { |
| 100 | + const { response } = await askForApproval( |
| 101 | + "auto_approval_max_req_reached", |
| 102 | + JSON.stringify({ count: maxCost.toFixed(2), type: "cost" }), |
| 103 | + ) |
| 104 | + |
| 105 | + // If we get past the promise, it means the user approved and did not start a new task |
| 106 | + if (response === "yesButtonClicked") { |
| 107 | + // Note: We don't reset the cost to 0 here because the actual cost |
| 108 | + // is calculated from the messages. This is different from the request count. |
| 109 | + return { |
| 110 | + shouldProceed: true, |
| 111 | + requiresApproval: true, |
| 112 | + approvalType: "cost", |
| 113 | + approvalCount: maxCost.toFixed(2), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + shouldProceed: false, |
| 119 | + requiresApproval: true, |
| 120 | + approvalType: "cost", |
| 121 | + approvalCount: maxCost.toFixed(2), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return { shouldProceed: true, requiresApproval: false } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Reset the request counter (typically called when starting a new task) |
| 130 | + */ |
| 131 | + resetRequestCount(): void { |
| 132 | + this.consecutiveAutoApprovedRequestsCount = 0 |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get current approval state for debugging/testing |
| 137 | + */ |
| 138 | + getApprovalState(): { requestCount: number; currentCost: number } { |
| 139 | + return { |
| 140 | + requestCount: this.consecutiveAutoApprovedRequestsCount, |
| 141 | + currentCost: this.consecutiveAutoApprovedCost, |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments