File tree Expand file tree Collapse file tree 4 files changed +31
-7
lines changed
Expand file tree Collapse file tree 4 files changed +31
-7
lines changed Original file line number Diff line number Diff line change 1+ import consola from "consola"
2+
3+ import { HTTPError } from "./http-error"
4+
5+ export const awaitApproval = async ( ) => {
6+ const response = await consola . prompt ( `Accept incoming request?` , {
7+ type : "confirm" ,
8+ } )
9+
10+ if ( ! response )
11+ throw new HTTPError ( "Request rejected" , Response . json ( { status : 429 } ) )
12+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,10 @@ export interface State {
99 vsCodeVersion ?: string
1010
1111 manualApprove : boolean
12+
13+ // Rate limiting configuration
14+ rateLimitSeconds ?: number
15+ lastRequestTimestamp ?: number
1216}
1317
1418export const state : State = {
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ interface RunServerOptions {
1616 verbose: boolean
1717 business: boolean
1818 manual: boolean
19+ rateLimit: number
1920}
2021
2122export async function runServer ( options : RunServerOptions ) : Promise < void > {
@@ -30,6 +31,7 @@ export async function runServer(options: RunServerOptions): Promise<void> {
3031 }
3132
3233 state . manualApprove = options . manual
34+ state . rateLimitSeconds = options . rateLimit
3335
3436 await ensurePaths ( )
3537 await cacheVSCodeVersion ( )
@@ -70,15 +72,23 @@ const main = defineCommand({
7072 default : false ,
7173 description : "Enable manual request approval" ,
7274 } ,
75+ rateLimit : {
76+ alias : "r" ,
77+ type : "string" ,
78+ default : "5" ,
79+ description : "Rate limit in seconds between requests" ,
80+ } ,
7381 } ,
7482 run ( { args } ) {
7583 const port = Number . parseInt ( args . port , 10 )
84+ const rateLimit = Number . parseInt ( args . rateLimit , 10 )
7685
7786 return runServer ( {
7887 port,
7988 verbose : args . verbose ,
8089 business : args . business ,
8190 manual : args . manual ,
91+ rateLimit,
8292 } )
8393 } ,
8494} )
Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ import type { Context } from "hono"
33import consola from "consola"
44import { streamSSE , type SSEMessage } from "hono/streaming"
55
6+ import { awaitApproval } from "~/lib/approval"
67import { isNullish } from "~/lib/is-nullish"
8+ import { checkRateLimit } from "~/lib/rate-limit"
79import { state } from "~/lib/state"
810import { getTokenCount } from "~/lib/tokenizer"
911import {
@@ -13,17 +15,13 @@ import {
1315} from "~/services/copilot/create-chat-completions"
1416
1517export async function handleCompletion ( c : Context ) {
18+ checkRateLimit ( state )
19+
1620 let payload = await c . req . json < ChatCompletionsPayload > ( )
1721
1822 consola . info ( "Current token count:" , getTokenCount ( payload . messages ) )
1923
20- if ( state . manualApprove ) {
21- const response = await consola . prompt ( `Accept incoming request?` , {
22- type : "confirm" ,
23- } )
24-
25- if ( ! response ) throw new Error ( "Request cancelled" )
26- }
24+ if ( state . manualApprove ) await awaitApproval ( )
2725
2826 if ( isNullish ( payload . max_tokens ) ) {
2927 const selectedModel = state . models ?. data . find (
You can’t perform that action at this time.
0 commit comments