mirror of
https://github.com/sst/opencode.git
synced 2025-08-22 22:14:14 +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 } } }>()
|
const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; workspaceID: string } } }>()
|
||||||
.get("/", (c) => c.text("Hello, world!"))
|
.get("/", (c) => c.text("Hello, world!"))
|
||||||
.post("/v1/chat/completions", GatewayAuth, async (c) => {
|
.post("/v1/chat/completions", GatewayAuth, async (c) => {
|
||||||
try {
|
const keyRecord = c.get("keyRecord")!
|
||||||
const body = await c.req.json<ChatCompletionCreateParamsBase>()
|
|
||||||
|
|
||||||
|
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()
|
const model = SUPPORTED_MODELS[body.model as keyof typeof SUPPORTED_MODELS]?.model()
|
||||||
if (!model) throw new Error(`Unsupported model: ${body.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) {
|
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]
|
const modelData = SUPPORTED_MODELS[model as keyof typeof SUPPORTED_MODELS]
|
||||||
if (!modelData) throw new Error(`Unsupported model: ${model}`)
|
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 cacheWriteCost = modelData.cacheWrite * cacheWriteTokens
|
||||||
const costInCents = (inputCost + outputCost + reasoningCost + cacheReadCost + cacheWriteCost) * 100
|
const costInCents = (inputCost + outputCost + reasoningCost + cacheReadCost + cacheWriteCost) * 100
|
||||||
|
|
||||||
await Actor.provide("system", { workspaceID: keyRecord.workspaceID }, async () => {
|
|
||||||
await Billing.consume({
|
await Billing.consume({
|
||||||
model,
|
model,
|
||||||
inputTokens,
|
inputTokens,
|
||||||
|
@ -637,7 +651,6 @@ const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; wor
|
||||||
cacheWriteTokens,
|
cacheWriteTokens,
|
||||||
costInCents,
|
costInCents,
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
await Database.use((tx) =>
|
await Database.use((tx) =>
|
||||||
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)
|
return c.json({ error: { message: error.message } }, 500)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
.use("/*", cors())
|
.use("/*", cors())
|
||||||
.use(RestAuth)
|
.use(RestAuth)
|
||||||
.get("/rest/account", async (c) => {
|
.get("/rest/account", async (c) => {
|
||||||
|
|
|
@ -1,25 +1,5 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://opencode.ai/config.json",
|
"$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": {
|
"mcp": {
|
||||||
"context7": {
|
"context7": {
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue