Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"*": "bun run lint --fix"
},
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.965.0",
"citty": "^0.1.6",
"clipboardy": "^5.0.0",
"consola": "^3.4.2",
Expand Down
10 changes: 10 additions & 0 deletions src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ export interface State {
// Rate limiting configuration
rateLimitSeconds?: number
lastRequestTimestamp?: number

// AWS Bedrock configuration
awsRegion?: string
awsAccessKeyId?: string
awsSecretAccessKey?: string
awsSessionToken?: string

// Provider round-robin state
providerRoundRobinCounter: number
}

export const state: State = {
accountType: "individual",
manualApprove: false,
rateLimitWait: false,
showToken: false,
providerRoundRobinCounter: 0,
}
15 changes: 8 additions & 7 deletions src/routes/chat-completions/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import type { Context } from "hono"
import consola from "consola"
import { streamSSE, type SSEMessage } from "hono/streaming"

import type {
ChatCompletionResponse,
ChatCompletionsPayload,
} from "~/services/copilot/create-chat-completions"

import { awaitApproval } from "~/lib/approval"
import { checkRateLimit } from "~/lib/rate-limit"
import { state } from "~/lib/state"
import { getTokenCount } from "~/lib/tokenizer"
import { isNullish } from "~/lib/utils"
import {
createChatCompletions,
type ChatCompletionResponse,
type ChatCompletionsPayload,
} from "~/services/copilot/create-chat-completions"
import { routeToProvider } from "~/services/provider-router"

export async function handleCompletion(c: Context) {
await checkRateLimit(state)
Expand Down Expand Up @@ -47,7 +48,7 @@ export async function handleCompletion(c: Context) {
consola.debug("Set max_tokens to:", JSON.stringify(payload.max_tokens))
}

const response = await createChatCompletions(payload)
const response = await routeToProvider(payload)

if (isNonStreaming(response)) {
consola.debug("Non-streaming response:", JSON.stringify(response))
Expand All @@ -64,5 +65,5 @@ export async function handleCompletion(c: Context) {
}

const isNonStreaming = (
response: Awaited<ReturnType<typeof createChatCompletions>>,
response: Awaited<ReturnType<typeof routeToProvider>>,
): response is ChatCompletionResponse => Object.hasOwn(response, "choices")
15 changes: 8 additions & 7 deletions src/routes/messages/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import type { Context } from "hono"
import consola from "consola"
import { streamSSE } from "hono/streaming"

import type {
ChatCompletionChunk,
ChatCompletionResponse,
} from "~/services/copilot/create-chat-completions"

import { awaitApproval } from "~/lib/approval"
import { checkRateLimit } from "~/lib/rate-limit"
import { state } from "~/lib/state"
import {
createChatCompletions,
type ChatCompletionChunk,
type ChatCompletionResponse,
} from "~/services/copilot/create-chat-completions"
import { routeToProvider } from "~/services/provider-router"

import {
type AnthropicMessagesPayload,
Expand Down Expand Up @@ -38,7 +39,7 @@ export async function handleCompletion(c: Context) {
await awaitApproval()
}

const response = await createChatCompletions(openAIPayload)
const response = await routeToProvider(openAIPayload)

if (isNonStreaming(response)) {
consola.debug(
Expand Down Expand Up @@ -87,5 +88,5 @@ export async function handleCompletion(c: Context) {
}

const isNonStreaming = (
response: Awaited<ReturnType<typeof createChatCompletions>>,
response: Awaited<ReturnType<typeof routeToProvider>>,
): response is ChatCompletionResponse => Object.hasOwn(response, "choices")
Loading