Skip to content

Commit 9a09b85

Browse files
committed
feat: token counting and manual approval
1 parent fdd94eb commit 9a09b85

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/lib/state.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ export interface State {
88
models?: ModelsResponse
99
vsCodeVersion?: string
1010

11-
estimateToken: boolean
1211
manualApprove: boolean
1312
}
1413

1514
export const state: State = {
1615
accountType: "individual",
17-
estimateToken: true,
1816
manualApprove: false,
1917
}

src/lib/tokenizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { countTokens } from "gpt-tokenizer/model/gpt-4o"
22

33
import type { Message } from "~/services/copilot/create-chat-completions"
44

5-
export const getTokenLength = (messages: Array<Message>) => {
5+
export const getTokenCount = (messages: Array<Message>) => {
66
const input = messages.filter((m) => m.role !== "assistant")
77
const output = messages.filter((m) => m.role === "assistant")
88

src/main.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface RunServerOptions {
1515
port: number
1616
verbose: boolean
1717
business: boolean
18+
manual: boolean
1819
}
1920

2021
export async function runServer(options: RunServerOptions): Promise<void> {
@@ -28,6 +29,8 @@ export async function runServer(options: RunServerOptions): Promise<void> {
2829
consola.info("Using business plan GitHub account")
2930
}
3031

32+
state.manualApprove = options.manual
33+
3134
await ensurePaths()
3235
await cacheVSCodeVersion()
3336
await setupGitHubToken()
@@ -62,6 +65,11 @@ const main = defineCommand({
6265
default: false,
6366
description: "Use a business plan GitHub Account",
6467
},
68+
manual: {
69+
type: "boolean",
70+
default: false,
71+
description: "Enable manual request approval",
72+
},
6573
},
6674
run({ args }) {
6775
const port = Number.parseInt(args.port, 10)
@@ -70,6 +78,7 @@ const main = defineCommand({
7078
port,
7179
verbose: args.verbose,
7280
business: args.business,
81+
manual: args.manual,
7382
})
7483
},
7584
})

src/routes/chat-completions/handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { Context } from "hono"
22

3+
import consola from "consola"
34
import { streamSSE, type SSEMessage } from "hono/streaming"
45

56
import { isNullish } from "~/lib/is-nullish"
67
import { state } from "~/lib/state"
8+
import { getTokenCount } from "~/lib/tokenizer"
79
import {
810
createChatCompletions,
911
type ChatCompletionResponse,
@@ -13,6 +15,16 @@ import {
1315
export async function handleCompletion(c: Context) {
1416
let payload = await c.req.json<ChatCompletionsPayload>()
1517

18+
consola.info("Current token count:", getTokenCount(payload.messages))
19+
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+
}
27+
1628
if (isNullish(payload.max_tokens)) {
1729
const selectedModel = state.models?.data.find(
1830
(model) => model.id === payload.model,

0 commit comments

Comments
 (0)