gateway.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. import { z } from "zod"
  2. import { Hono, MiddlewareHandler } from "hono"
  3. import { cors } from "hono/cors"
  4. import { HTTPException } from "hono/http-exception"
  5. import { zValidator } from "@hono/zod-validator"
  6. import { Resource } from "sst"
  7. import { type ProviderMetadata, type LanguageModelUsage, generateText, streamText } from "ai"
  8. import { createAnthropic } from "@ai-sdk/anthropic"
  9. import { createOpenAI } from "@ai-sdk/openai"
  10. import { createOpenAICompatible } from "@ai-sdk/openai-compatible"
  11. import type { LanguageModelV2Prompt } from "@ai-sdk/provider"
  12. import { type ChatCompletionCreateParamsBase } from "openai/resources/chat/completions"
  13. import { Actor } from "@opencode/cloud-core/actor.js"
  14. import { and, Database, eq, sql } from "@opencode/cloud-core/drizzle/index.js"
  15. import { UserTable } from "@opencode/cloud-core/schema/user.sql.js"
  16. import { KeyTable } from "@opencode/cloud-core/schema/key.sql.js"
  17. import { createClient } from "@openauthjs/openauth/client"
  18. import { Log } from "@opencode/cloud-core/util/log.js"
  19. import { Billing } from "@opencode/cloud-core/billing.js"
  20. import { Workspace } from "@opencode/cloud-core/workspace.js"
  21. import { BillingTable, PaymentTable, UsageTable } from "@opencode/cloud-core/schema/billing.sql.js"
  22. import { centsToMicroCents } from "@opencode/cloud-core/util/price.js"
  23. import { Identifier } from "../../core/src/identifier"
  24. type Env = {}
  25. let _client: ReturnType<typeof createClient>
  26. const client = () => {
  27. if (_client) return _client
  28. _client = createClient({
  29. clientID: "api",
  30. issuer: Resource.AUTH_API_URL.value,
  31. })
  32. return _client
  33. }
  34. const SUPPORTED_MODELS = {
  35. "anthropic/claude-sonnet-4": {
  36. input: 0.0000015,
  37. output: 0.000006,
  38. reasoning: 0.0000015,
  39. cacheRead: 0.0000001,
  40. cacheWrite: 0.0000001,
  41. model: () =>
  42. createAnthropic({
  43. apiKey: Resource.ANTHROPIC_API_KEY.value,
  44. })("claude-sonnet-4-20250514"),
  45. },
  46. "openai/gpt-4.1": {
  47. input: 0.0000015,
  48. output: 0.000006,
  49. reasoning: 0.0000015,
  50. cacheRead: 0.0000001,
  51. cacheWrite: 0.0000001,
  52. model: () =>
  53. createOpenAI({
  54. apiKey: Resource.OPENAI_API_KEY.value,
  55. })("gpt-4.1"),
  56. },
  57. "zhipuai/glm-4.5-flash": {
  58. input: 0,
  59. output: 0,
  60. reasoning: 0,
  61. cacheRead: 0,
  62. cacheWrite: 0,
  63. model: () =>
  64. createOpenAICompatible({
  65. name: "Zhipu AI",
  66. baseURL: "https://api.z.ai/api/paas/v4",
  67. apiKey: Resource.ZHIPU_API_KEY.value,
  68. })("glm-4.5-flash"),
  69. },
  70. }
  71. const log = Log.create({
  72. namespace: "api",
  73. })
  74. const GatewayAuth: MiddlewareHandler = async (c, next) => {
  75. const authHeader = c.req.header("authorization")
  76. if (!authHeader || !authHeader.startsWith("Bearer ")) {
  77. return c.json(
  78. {
  79. error: {
  80. message: "Missing API key.",
  81. type: "invalid_request_error",
  82. param: null,
  83. code: "unauthorized",
  84. },
  85. },
  86. 401,
  87. )
  88. }
  89. const apiKey = authHeader.split(" ")[1]
  90. // Check against KeyTable
  91. const keyRecord = await Database.use((tx) =>
  92. tx
  93. .select({
  94. id: KeyTable.id,
  95. workspaceID: KeyTable.workspaceID,
  96. })
  97. .from(KeyTable)
  98. .where(eq(KeyTable.key, apiKey))
  99. .then((rows) => rows[0]),
  100. )
  101. if (!keyRecord) {
  102. return c.json(
  103. {
  104. error: {
  105. message: "Invalid API key.",
  106. type: "invalid_request_error",
  107. param: null,
  108. code: "unauthorized",
  109. },
  110. },
  111. 401,
  112. )
  113. }
  114. c.set("keyRecord", keyRecord)
  115. await next()
  116. }
  117. const RestAuth: MiddlewareHandler = async (c, next) => {
  118. const authorization = c.req.header("authorization")
  119. if (!authorization) {
  120. return Actor.provide("public", {}, next)
  121. }
  122. const token = authorization.split(" ")[1]
  123. if (!token)
  124. throw new HTTPException(403, {
  125. message: "Bearer token is required.",
  126. })
  127. const verified = await client().verify(token)
  128. if (verified.err) {
  129. throw new HTTPException(403, {
  130. message: "Invalid token.",
  131. })
  132. }
  133. let subject = verified.subject as Actor.Info
  134. if (subject.type === "account") {
  135. const workspaceID = c.req.header("x-opencode-workspace")
  136. const email = subject.properties.email
  137. if (workspaceID) {
  138. const user = await Database.use((tx) =>
  139. tx
  140. .select({
  141. id: UserTable.id,
  142. workspaceID: UserTable.workspaceID,
  143. email: UserTable.email,
  144. })
  145. .from(UserTable)
  146. .where(and(eq(UserTable.email, email), eq(UserTable.workspaceID, workspaceID)))
  147. .then((rows) => rows[0]),
  148. )
  149. if (!user)
  150. throw new HTTPException(403, {
  151. message: "You do not have access to this workspace.",
  152. })
  153. subject = {
  154. type: "user",
  155. properties: {
  156. userID: user.id,
  157. workspaceID: workspaceID,
  158. email: user.email,
  159. },
  160. }
  161. }
  162. }
  163. await Actor.provide(subject.type, subject.properties, next)
  164. }
  165. const app = new Hono<{ Bindings: Env; Variables: { keyRecord?: { id: string; workspaceID: string } } }>()
  166. .get("/", (c) => c.text("Hello, world!"))
  167. .post("/v1/chat/completions", GatewayAuth, async (c) => {
  168. const keyRecord = c.get("keyRecord")!
  169. return await Actor.provide("system", { workspaceID: keyRecord.workspaceID }, async () => {
  170. try {
  171. // Check balance
  172. const customer = await Billing.get()
  173. if (customer.balance <= 0) {
  174. return c.json(
  175. {
  176. error: {
  177. message: "Insufficient balance",
  178. type: "insufficient_quota",
  179. param: null,
  180. code: "insufficient_quota",
  181. },
  182. },
  183. 401,
  184. )
  185. }
  186. const body = await c.req.json<ChatCompletionCreateParamsBase>()
  187. const model = SUPPORTED_MODELS[body.model as keyof typeof SUPPORTED_MODELS]?.model()
  188. if (!model) throw new Error(`Unsupported model: ${body.model}`)
  189. const requestBody = transformOpenAIRequestToAiSDK()
  190. return body.stream ? await handleStream() : await handleGenerate()
  191. async function handleStream() {
  192. const result = await model.doStream({
  193. ...requestBody,
  194. })
  195. const encoder = new TextEncoder()
  196. const stream = new ReadableStream({
  197. async start(controller) {
  198. const id = `chatcmpl-${Date.now()}`
  199. const created = Math.floor(Date.now() / 1000)
  200. try {
  201. for await (const chunk of result.stream) {
  202. console.log("!!! CHUNK !!! : " + chunk.type)
  203. switch (chunk.type) {
  204. case "text-delta": {
  205. const data = {
  206. id,
  207. object: "chat.completion.chunk",
  208. created,
  209. model: body.model,
  210. choices: [
  211. {
  212. index: 0,
  213. delta: {
  214. content: chunk.delta,
  215. },
  216. finish_reason: null,
  217. },
  218. ],
  219. }
  220. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  221. break
  222. }
  223. case "reasoning-delta": {
  224. const data = {
  225. id,
  226. object: "chat.completion.chunk",
  227. created,
  228. model: body.model,
  229. choices: [
  230. {
  231. index: 0,
  232. delta: {
  233. reasoning_content: chunk.delta,
  234. },
  235. finish_reason: null,
  236. },
  237. ],
  238. }
  239. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  240. break
  241. }
  242. case "tool-call": {
  243. const data = {
  244. id,
  245. object: "chat.completion.chunk",
  246. created,
  247. model: body.model,
  248. choices: [
  249. {
  250. index: 0,
  251. delta: {
  252. tool_calls: [
  253. {
  254. index: 0,
  255. id: chunk.toolCallId,
  256. type: "function",
  257. function: {
  258. name: chunk.toolName,
  259. arguments: chunk.input,
  260. },
  261. },
  262. ],
  263. },
  264. finish_reason: null,
  265. },
  266. ],
  267. }
  268. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  269. break
  270. }
  271. case "error": {
  272. const data = {
  273. id,
  274. object: "chat.completion.chunk",
  275. created,
  276. model: body.model,
  277. choices: [
  278. {
  279. index: 0,
  280. delta: {},
  281. finish_reason: "stop",
  282. },
  283. ],
  284. error: {
  285. message: typeof chunk.error === "string" ? chunk.error : chunk.error,
  286. type: "server_error",
  287. },
  288. }
  289. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  290. controller.enqueue(encoder.encode("data: [DONE]\n\n"))
  291. controller.close()
  292. break
  293. }
  294. case "finish": {
  295. const data = {
  296. id,
  297. object: "chat.completion.chunk",
  298. created,
  299. model: body.model,
  300. choices: [
  301. {
  302. index: 0,
  303. delta: {},
  304. finish_reason:
  305. {
  306. stop: "stop",
  307. length: "length",
  308. "content-filter": "content_filter",
  309. "tool-calls": "tool_calls",
  310. error: "stop",
  311. other: "stop",
  312. unknown: "stop",
  313. }[chunk.finishReason] || "stop",
  314. },
  315. ],
  316. usage: {
  317. prompt_tokens: chunk.usage.inputTokens,
  318. completion_tokens: chunk.usage.outputTokens,
  319. total_tokens: chunk.usage.totalTokens,
  320. completion_tokens_details: {
  321. reasoning_tokens: chunk.usage.reasoningTokens,
  322. },
  323. prompt_tokens_details: {
  324. cached_tokens: chunk.usage.cachedInputTokens,
  325. },
  326. },
  327. }
  328. await trackUsage(body.model, chunk.usage, chunk.providerMetadata)
  329. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  330. controller.enqueue(encoder.encode("data: [DONE]\n\n"))
  331. controller.close()
  332. break
  333. }
  334. //case "stream-start":
  335. //case "response-metadata":
  336. case "text-start":
  337. case "text-end":
  338. case "reasoning-start":
  339. case "reasoning-end":
  340. case "tool-input-start":
  341. case "tool-input-delta":
  342. case "tool-input-end":
  343. case "raw":
  344. default:
  345. // Log unknown chunk types for debugging
  346. console.warn(`Unknown chunk type: ${(chunk as any).type}`)
  347. break
  348. }
  349. }
  350. } catch (error) {
  351. controller.error(error)
  352. }
  353. },
  354. })
  355. return new Response(stream, {
  356. headers: {
  357. "Content-Type": "text/plain; charset=utf-8",
  358. "Cache-Control": "no-cache",
  359. Connection: "keep-alive",
  360. },
  361. })
  362. }
  363. async function handleGenerate() {
  364. const response = await model.doGenerate({
  365. ...requestBody,
  366. })
  367. await trackUsage(body.model, response.usage, response.providerMetadata)
  368. return c.json({
  369. id: `chatcmpl-${Date.now()}`,
  370. object: "chat.completion" as const,
  371. created: Math.floor(Date.now() / 1000),
  372. model: body.model,
  373. choices: [
  374. {
  375. index: 0,
  376. message: {
  377. role: "assistant" as const,
  378. content: response.content?.find((c) => c.type === "text")?.text ?? "",
  379. reasoning_content: response.content?.find((c) => c.type === "reasoning")?.text,
  380. tool_calls: response.content
  381. ?.filter((c) => c.type === "tool-call")
  382. .map((toolCall) => ({
  383. id: toolCall.toolCallId,
  384. type: "function" as const,
  385. function: {
  386. name: toolCall.toolName,
  387. arguments: toolCall.input,
  388. },
  389. })),
  390. },
  391. finish_reason:
  392. (
  393. {
  394. stop: "stop",
  395. length: "length",
  396. "content-filter": "content_filter",
  397. "tool-calls": "tool_calls",
  398. error: "stop",
  399. other: "stop",
  400. unknown: "stop",
  401. } as const
  402. )[response.finishReason] || "stop",
  403. },
  404. ],
  405. usage: {
  406. prompt_tokens: response.usage?.inputTokens,
  407. completion_tokens: response.usage?.outputTokens,
  408. total_tokens: response.usage?.totalTokens,
  409. completion_tokens_details: {
  410. reasoning_tokens: response.usage?.reasoningTokens,
  411. },
  412. prompt_tokens_details: {
  413. cached_tokens: response.usage?.cachedInputTokens,
  414. },
  415. },
  416. })
  417. }
  418. function transformOpenAIRequestToAiSDK() {
  419. const prompt = transformMessages()
  420. const tools = transformTools()
  421. return {
  422. prompt,
  423. maxOutputTokens: body.max_tokens ?? body.max_completion_tokens ?? undefined,
  424. temperature: body.temperature ?? undefined,
  425. topP: body.top_p ?? undefined,
  426. frequencyPenalty: body.frequency_penalty ?? undefined,
  427. presencePenalty: body.presence_penalty ?? undefined,
  428. providerOptions: body.reasoning_effort
  429. ? {
  430. anthropic: {
  431. reasoningEffort: body.reasoning_effort,
  432. },
  433. }
  434. : undefined,
  435. stopSequences: (typeof body.stop === "string" ? [body.stop] : body.stop) ?? undefined,
  436. responseFormat: (() => {
  437. if (!body.response_format) return { type: "text" as const }
  438. if (body.response_format.type === "json_schema")
  439. return {
  440. type: "json" as const,
  441. schema: body.response_format.json_schema.schema,
  442. name: body.response_format.json_schema.name,
  443. description: body.response_format.json_schema.description,
  444. }
  445. if (body.response_format.type === "json_object") return { type: "json" as const }
  446. throw new Error("Unsupported response format")
  447. })(),
  448. seed: body.seed ?? undefined,
  449. tools: tools.tools,
  450. toolChoice: tools.toolChoice,
  451. }
  452. function transformTools() {
  453. const { tools, tool_choice } = body
  454. if (!tools || tools.length === 0) {
  455. return { tools: undefined, toolChoice: undefined }
  456. }
  457. const aiSdkTools = tools.map((tool) => {
  458. return {
  459. type: tool.type,
  460. name: tool.function.name,
  461. description: tool.function.description,
  462. inputSchema: tool.function.parameters!,
  463. }
  464. })
  465. let aiSdkToolChoice
  466. if (tool_choice == null) {
  467. aiSdkToolChoice = undefined
  468. } else if (tool_choice === "auto") {
  469. aiSdkToolChoice = { type: "auto" as const }
  470. } else if (tool_choice === "none") {
  471. aiSdkToolChoice = { type: "none" as const }
  472. } else if (tool_choice === "required") {
  473. aiSdkToolChoice = { type: "required" as const }
  474. } else if (tool_choice.type === "function") {
  475. aiSdkToolChoice = {
  476. type: "tool" as const,
  477. toolName: tool_choice.function.name,
  478. }
  479. }
  480. return { tools: aiSdkTools, toolChoice: aiSdkToolChoice }
  481. }
  482. function transformMessages() {
  483. const { messages } = body
  484. const prompt: LanguageModelV2Prompt = []
  485. for (const message of messages) {
  486. switch (message.role) {
  487. case "system": {
  488. prompt.push({
  489. role: "system",
  490. content: message.content as string,
  491. })
  492. break
  493. }
  494. case "user": {
  495. if (typeof message.content === "string") {
  496. prompt.push({
  497. role: "user",
  498. content: [{ type: "text", text: message.content }],
  499. })
  500. } else {
  501. const content = message.content.map((part) => {
  502. switch (part.type) {
  503. case "text":
  504. return { type: "text" as const, text: part.text }
  505. case "image_url":
  506. return {
  507. type: "file" as const,
  508. mediaType: "image/jpeg" as const,
  509. data: part.image_url.url,
  510. }
  511. default:
  512. throw new Error(`Unsupported content part type: ${(part as any).type}`)
  513. }
  514. })
  515. prompt.push({
  516. role: "user",
  517. content,
  518. })
  519. }
  520. break
  521. }
  522. case "assistant": {
  523. const content: Array<
  524. | { type: "text"; text: string }
  525. | {
  526. type: "tool-call"
  527. toolCallId: string
  528. toolName: string
  529. input: any
  530. }
  531. > = []
  532. if (message.content) {
  533. content.push({
  534. type: "text",
  535. text: message.content as string,
  536. })
  537. }
  538. if (message.tool_calls) {
  539. for (const toolCall of message.tool_calls) {
  540. content.push({
  541. type: "tool-call",
  542. toolCallId: toolCall.id,
  543. toolName: toolCall.function.name,
  544. input: JSON.parse(toolCall.function.arguments),
  545. })
  546. }
  547. }
  548. prompt.push({
  549. role: "assistant",
  550. content,
  551. })
  552. break
  553. }
  554. case "tool": {
  555. prompt.push({
  556. role: "tool",
  557. content: [
  558. {
  559. type: "tool-result",
  560. toolName: "placeholder",
  561. toolCallId: message.tool_call_id,
  562. output: {
  563. type: "text",
  564. value: message.content as string,
  565. },
  566. },
  567. ],
  568. })
  569. break
  570. }
  571. default: {
  572. throw new Error(`Unsupported message role: ${message.role}`)
  573. }
  574. }
  575. }
  576. return prompt
  577. }
  578. }
  579. async function trackUsage(model: string, usage: LanguageModelUsage, providerMetadata?: ProviderMetadata) {
  580. const modelData = SUPPORTED_MODELS[model as keyof typeof SUPPORTED_MODELS]
  581. if (!modelData) throw new Error(`Unsupported model: ${model}`)
  582. const inputTokens = usage.inputTokens ?? 0
  583. const outputTokens = usage.outputTokens ?? 0
  584. const reasoningTokens = usage.reasoningTokens ?? 0
  585. const cacheReadTokens = usage.cachedInputTokens ?? 0
  586. const cacheWriteTokens =
  587. providerMetadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
  588. // @ts-expect-error
  589. providerMetadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
  590. 0
  591. const inputCost = modelData.input * inputTokens
  592. const outputCost = modelData.output * outputTokens
  593. const reasoningCost = modelData.reasoning * reasoningTokens
  594. const cacheReadCost = modelData.cacheRead * cacheReadTokens
  595. const cacheWriteCost = modelData.cacheWrite * cacheWriteTokens
  596. const costInCents = (inputCost + outputCost + reasoningCost + cacheReadCost + cacheWriteCost) * 100
  597. await Billing.consume({
  598. model,
  599. inputTokens,
  600. outputTokens,
  601. reasoningTokens,
  602. cacheReadTokens,
  603. cacheWriteTokens,
  604. costInCents,
  605. })
  606. await Database.use((tx) =>
  607. tx
  608. .update(KeyTable)
  609. .set({ timeUsed: sql`now()` })
  610. .where(eq(KeyTable.id, keyRecord.id)),
  611. )
  612. }
  613. } catch (error: any) {
  614. return c.json({ error: { message: error.message } }, 500)
  615. }
  616. })
  617. })
  618. .use("/*", cors())
  619. .use(RestAuth)
  620. .get("/rest/account", async (c) => {
  621. const account = Actor.assert("account")
  622. let workspaces = await Workspace.list()
  623. if (workspaces.length === 0) {
  624. await Workspace.create()
  625. workspaces = await Workspace.list()
  626. }
  627. return c.json({
  628. id: account.properties.accountID,
  629. email: account.properties.email,
  630. workspaces,
  631. })
  632. })
  633. .get("/billing/info", async (c) => {
  634. const billing = await Billing.get()
  635. const payments = await Database.use((tx) =>
  636. tx
  637. .select()
  638. .from(PaymentTable)
  639. .where(eq(PaymentTable.workspaceID, Actor.workspace()))
  640. .orderBy(sql`${PaymentTable.timeCreated} DESC`)
  641. .limit(100),
  642. )
  643. const usage = await Database.use((tx) =>
  644. tx
  645. .select()
  646. .from(UsageTable)
  647. .where(eq(UsageTable.workspaceID, Actor.workspace()))
  648. .orderBy(sql`${UsageTable.timeCreated} DESC`)
  649. .limit(100),
  650. )
  651. return c.json({ billing, payments, usage })
  652. })
  653. .post(
  654. "/billing/checkout",
  655. zValidator(
  656. "json",
  657. z.custom<{
  658. success_url: string
  659. cancel_url: string
  660. }>(),
  661. ),
  662. async (c) => {
  663. const account = Actor.assert("user")
  664. const body = await c.req.json()
  665. const customer = await Billing.get()
  666. const session = await Billing.stripe().checkout.sessions.create({
  667. mode: "payment",
  668. line_items: [
  669. {
  670. price_data: {
  671. currency: "usd",
  672. product_data: {
  673. name: "opencode credits",
  674. },
  675. unit_amount: 2000, // $20 minimum
  676. },
  677. quantity: 1,
  678. },
  679. ],
  680. payment_intent_data: {
  681. setup_future_usage: "on_session",
  682. },
  683. ...(customer.customerID
  684. ? { customer: customer.customerID }
  685. : {
  686. customer_email: account.properties.email,
  687. customer_creation: "always",
  688. }),
  689. metadata: {
  690. workspaceID: Actor.workspace(),
  691. },
  692. currency: "usd",
  693. payment_method_types: ["card"],
  694. success_url: body.success_url,
  695. cancel_url: body.cancel_url,
  696. })
  697. return c.json({
  698. url: session.url,
  699. })
  700. },
  701. )
  702. .post("/billing/portal", async (c) => {
  703. const body = await c.req.json()
  704. const customer = await Billing.get()
  705. if (!customer?.customerID) {
  706. throw new Error("No stripe customer ID")
  707. }
  708. const session = await Billing.stripe().billingPortal.sessions.create({
  709. customer: customer.customerID,
  710. return_url: body.return_url,
  711. })
  712. return c.json({
  713. url: session.url,
  714. })
  715. })
  716. .post("/stripe/webhook", async (c) => {
  717. const body = await Billing.stripe().webhooks.constructEventAsync(
  718. await c.req.text(),
  719. c.req.header("stripe-signature")!,
  720. Resource.STRIPE_WEBHOOK_SECRET.value,
  721. )
  722. console.log(body.type, JSON.stringify(body, null, 2))
  723. if (body.type === "checkout.session.completed") {
  724. const workspaceID = body.data.object.metadata?.workspaceID
  725. const customerID = body.data.object.customer as string
  726. const paymentID = body.data.object.payment_intent as string
  727. const amount = body.data.object.amount_total
  728. if (!workspaceID) throw new Error("Workspace ID not found")
  729. if (!customerID) throw new Error("Customer ID not found")
  730. if (!amount) throw new Error("Amount not found")
  731. if (!paymentID) throw new Error("Payment ID not found")
  732. await Actor.provide("system", { workspaceID }, async () => {
  733. const customer = await Billing.get()
  734. if (customer?.customerID && customer.customerID !== customerID) throw new Error("Customer ID mismatch")
  735. // set customer metadata
  736. if (!customer?.customerID) {
  737. await Billing.stripe().customers.update(customerID, {
  738. metadata: {
  739. workspaceID,
  740. },
  741. })
  742. }
  743. // get payment method for the payment intent
  744. const paymentIntent = await Billing.stripe().paymentIntents.retrieve(paymentID, {
  745. expand: ["payment_method"],
  746. })
  747. const paymentMethod = paymentIntent.payment_method
  748. if (!paymentMethod || typeof paymentMethod === "string") throw new Error("Payment method not expanded")
  749. await Database.transaction(async (tx) => {
  750. await tx
  751. .update(BillingTable)
  752. .set({
  753. balance: sql`${BillingTable.balance} + ${centsToMicroCents(amount)}`,
  754. customerID,
  755. paymentMethodID: paymentMethod.id,
  756. paymentMethodLast4: paymentMethod.card!.last4,
  757. })
  758. .where(eq(BillingTable.workspaceID, workspaceID))
  759. await tx.insert(PaymentTable).values({
  760. workspaceID,
  761. id: Identifier.create("payment"),
  762. amount: centsToMicroCents(amount),
  763. paymentID,
  764. customerID,
  765. })
  766. })
  767. })
  768. }
  769. console.log("finished handling")
  770. return c.json("ok", 200)
  771. })
  772. .get("/keys", async (c) => {
  773. const user = Actor.assert("user")
  774. const keys = await Database.use((tx) =>
  775. tx
  776. .select({
  777. id: KeyTable.id,
  778. name: KeyTable.name,
  779. key: KeyTable.key,
  780. userID: KeyTable.userID,
  781. timeCreated: KeyTable.timeCreated,
  782. timeUsed: KeyTable.timeUsed,
  783. })
  784. .from(KeyTable)
  785. .where(eq(KeyTable.workspaceID, user.properties.workspaceID))
  786. .orderBy(sql`${KeyTable.timeCreated} DESC`),
  787. )
  788. return c.json({ keys })
  789. })
  790. .post("/keys", zValidator("json", z.object({ name: z.string().min(1).max(255) })), async (c) => {
  791. const user = Actor.assert("user")
  792. const { name } = c.req.valid("json")
  793. // Generate secret key: sk- + 64 random characters (upper, lower, numbers)
  794. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  795. let randomPart = ""
  796. for (let i = 0; i < 64; i++) {
  797. randomPart += chars.charAt(Math.floor(Math.random() * chars.length))
  798. }
  799. const secretKey = `sk-${randomPart}`
  800. const keyRecord = await Database.use((tx) =>
  801. tx
  802. .insert(KeyTable)
  803. .values({
  804. id: Identifier.create("key"),
  805. workspaceID: user.properties.workspaceID,
  806. userID: user.properties.userID,
  807. name,
  808. key: secretKey,
  809. timeUsed: null,
  810. })
  811. .returning(),
  812. )
  813. return c.json({
  814. key: secretKey,
  815. id: keyRecord[0].id,
  816. name: keyRecord[0].name,
  817. created: keyRecord[0].timeCreated,
  818. })
  819. })
  820. .delete("/keys/:id", async (c) => {
  821. const user = Actor.assert("user")
  822. const keyId = c.req.param("id")
  823. const result = await Database.use((tx) =>
  824. tx
  825. .delete(KeyTable)
  826. .where(and(eq(KeyTable.id, keyId), eq(KeyTable.workspaceID, user.properties.workspaceID)))
  827. .returning({ id: KeyTable.id }),
  828. )
  829. if (result.length === 0) {
  830. return c.json({ error: "Key not found" }, 404)
  831. }
  832. return c.json({ success: true, id: result[0].id })
  833. })
  834. .all("*", (c) => c.text("Not Found"))
  835. export type ApiType = typeof app
  836. export default app