mirror of
https://github.com/sst/opencode.git
synced 2025-08-22 14:04:07 +00:00
wip gateway
This commit is contained in:
parent
542186aa49
commit
20e818ad05
2 changed files with 445 additions and 451 deletions
|
@ -178,9 +178,27 @@ const RestAuth: MiddlewareHandler = async (c, next) => {
|
|||
const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; workspaceID: string } } }>()
|
||||
.get("/", (c) => c.text("Hello, world!"))
|
||||
.post("/v1/chat/completions", GatewayAuth, async (c) => {
|
||||
try {
|
||||
const body = await c.req.json<ChatCompletionCreateParamsBase>()
|
||||
const keyRecord = c.get("keyRecord")!
|
||||
|
||||
return await Actor.provide("system", { workspaceID: keyRecord.workspaceID }, async () => {
|
||||
try {
|
||||
// Check balance
|
||||
const customer = await Billing.get()
|
||||
if (customer.balance <= 0) {
|
||||
return c.json(
|
||||
{
|
||||
error: {
|
||||
message: "Insufficient balance",
|
||||
type: "insufficient_quota",
|
||||
param: null,
|
||||
code: "insufficient_quota",
|
||||
},
|
||||
},
|
||||
401,
|
||||
)
|
||||
}
|
||||
|
||||
const body = await c.req.json<ChatCompletionCreateParamsBase>()
|
||||
const model = SUPPORTED_MODELS[body.model as keyof typeof SUPPORTED_MODELS]?.model()
|
||||
if (!model) throw new Error(`Unsupported model: ${body.model}`)
|
||||
|
||||
|
@ -604,9 +622,6 @@ const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; wor
|
|||
}
|
||||
|
||||
async function trackUsage(model: string, usage: LanguageModelUsage, providerMetadata?: ProviderMetadata) {
|
||||
const keyRecord = c.get("keyRecord")
|
||||
if (!keyRecord) return
|
||||
|
||||
const modelData = SUPPORTED_MODELS[model as keyof typeof SUPPORTED_MODELS]
|
||||
if (!modelData) throw new Error(`Unsupported model: ${model}`)
|
||||
|
||||
|
@ -627,7 +642,6 @@ const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; wor
|
|||
const cacheWriteCost = modelData.cacheWrite * cacheWriteTokens
|
||||
const costInCents = (inputCost + outputCost + reasoningCost + cacheReadCost + cacheWriteCost) * 100
|
||||
|
||||
await Actor.provide("system", { workspaceID: keyRecord.workspaceID }, async () => {
|
||||
await Billing.consume({
|
||||
model,
|
||||
inputTokens,
|
||||
|
@ -637,7 +651,6 @@ const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; wor
|
|||
cacheWriteTokens,
|
||||
costInCents,
|
||||
})
|
||||
})
|
||||
|
||||
await Database.use((tx) =>
|
||||
tx
|
||||
|
@ -650,6 +663,7 @@ const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; wor
|
|||
return c.json({ error: { message: error.message } }, 500)
|
||||
}
|
||||
})
|
||||
})
|
||||
.use("/*", cors())
|
||||
.use(RestAuth)
|
||||
.get("/rest/account", async (c) => {
|
||||
|
|
|
@ -1,25 +1,5 @@
|
|||
{
|
||||
"$schema": "https://opencode.ai/config.json",
|
||||
"provider": {
|
||||
"oc-frank": {
|
||||
"npm": "@ai-sdk/openai-compatible",
|
||||
"name": "OC-Frank",
|
||||
"options": {
|
||||
"baseURL": "https://api.gateway.frank.dev.opencode.ai/v1"
|
||||
},
|
||||
"models": {
|
||||
"anthropic/claude-sonnet-4": {
|
||||
"name": "Claude Sonnet 4"
|
||||
},
|
||||
"openai/gpt-4.1": {
|
||||
"name": "GPT-4.1"
|
||||
},
|
||||
"zhipuai/glm-4.5-flash": {
|
||||
"name": "GLM-4.5 Flash"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"mcp": {
|
||||
"context7": {
|
||||
"type": "remote",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue