gateway.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 { 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 { LanguageModelV2Usage, 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. try {
  169. const body = await c.req.json<ChatCompletionCreateParamsBase>()
  170. console.log(body)
  171. const model = SUPPORTED_MODELS[body.model as keyof typeof SUPPORTED_MODELS]?.model()
  172. if (!model) throw new Error(`Unsupported model: ${body.model}`)
  173. const requestBody = transformOpenAIRequestToAiSDK()
  174. return body.stream ? await handleStream() : await handleGenerate()
  175. async function handleStream() {
  176. const result = await streamText({
  177. model,
  178. ...requestBody,
  179. })
  180. const encoder = new TextEncoder()
  181. const stream = new ReadableStream({
  182. async start(controller) {
  183. const id = `chatcmpl-${Date.now()}`
  184. const created = Math.floor(Date.now() / 1000)
  185. try {
  186. for await (const chunk of result.fullStream) {
  187. // TODO
  188. //console.log("!!! CHUCK !!!", chunk);
  189. switch (chunk.type) {
  190. case "text-delta": {
  191. const data = {
  192. id,
  193. object: "chat.completion.chunk",
  194. created,
  195. model: body.model,
  196. choices: [
  197. {
  198. index: 0,
  199. delta: {
  200. content: chunk.text,
  201. },
  202. finish_reason: null,
  203. },
  204. ],
  205. }
  206. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  207. break
  208. }
  209. case "reasoning-delta": {
  210. const data = {
  211. id,
  212. object: "chat.completion.chunk",
  213. created,
  214. model: body.model,
  215. choices: [
  216. {
  217. index: 0,
  218. delta: {
  219. reasoning_content: chunk.text,
  220. },
  221. finish_reason: null,
  222. },
  223. ],
  224. }
  225. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  226. break
  227. }
  228. case "tool-call": {
  229. const data = {
  230. id,
  231. object: "chat.completion.chunk",
  232. created,
  233. model: body.model,
  234. choices: [
  235. {
  236. index: 0,
  237. delta: {
  238. tool_calls: [
  239. {
  240. id: chunk.toolCallId,
  241. type: "function",
  242. function: {
  243. name: chunk.toolName,
  244. arguments: JSON.stringify(chunk.input),
  245. },
  246. },
  247. ],
  248. },
  249. finish_reason: null,
  250. },
  251. ],
  252. }
  253. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  254. break
  255. }
  256. case "error": {
  257. const data = {
  258. id,
  259. object: "chat.completion.chunk",
  260. created,
  261. model: body.model,
  262. error: {
  263. message: chunk.error,
  264. type: "server_error",
  265. },
  266. }
  267. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  268. controller.enqueue(encoder.encode("data: [DONE]\n\n"))
  269. controller.close()
  270. break
  271. }
  272. case "finish": {
  273. const finishReason =
  274. {
  275. stop: "stop",
  276. length: "length",
  277. "content-filter": "content_filter",
  278. "tool-calls": "tool_calls",
  279. error: "stop",
  280. other: "stop",
  281. unknown: "stop",
  282. }[chunk.finishReason] || "stop"
  283. const data = {
  284. id,
  285. object: "chat.completion.chunk",
  286. created,
  287. model: body.model,
  288. choices: [
  289. {
  290. index: 0,
  291. delta: {},
  292. finish_reason: finishReason,
  293. },
  294. ],
  295. usage: {
  296. prompt_tokens: chunk.totalUsage.inputTokens,
  297. completion_tokens: chunk.totalUsage.outputTokens,
  298. total_tokens: chunk.totalUsage.totalTokens,
  299. completion_tokens_details: {
  300. reasoning_tokens: chunk.totalUsage.reasoningTokens,
  301. },
  302. prompt_tokens_details: {
  303. cached_tokens: chunk.totalUsage.cachedInputTokens,
  304. },
  305. },
  306. }
  307. controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
  308. controller.enqueue(encoder.encode("data: [DONE]\n\n"))
  309. controller.close()
  310. break
  311. }
  312. //case "stream-start":
  313. //case "response-metadata":
  314. case "start-step":
  315. case "finish-step":
  316. case "text-start":
  317. case "text-end":
  318. case "reasoning-start":
  319. case "reasoning-end":
  320. case "tool-input-start":
  321. case "tool-input-delta":
  322. case "tool-input-end":
  323. case "raw":
  324. default:
  325. // Log unknown chunk types for debugging
  326. console.warn(`Unknown chunk type: ${(chunk as any).type}`)
  327. break
  328. }
  329. }
  330. } catch (error) {
  331. controller.error(error)
  332. }
  333. },
  334. })
  335. return new Response(stream, {
  336. headers: {
  337. "Content-Type": "text/plain; charset=utf-8",
  338. "Cache-Control": "no-cache",
  339. Connection: "keep-alive",
  340. },
  341. })
  342. }
  343. async function handleGenerate() {
  344. const response = await generateText({
  345. model,
  346. ...requestBody,
  347. })
  348. await trackUsage(body.model, response.usage)
  349. return c.json({
  350. id: `chatcmpl-${Date.now()}`,
  351. object: "chat.completion" as const,
  352. created: Math.floor(Date.now() / 1000),
  353. model: body.model,
  354. choices: [
  355. {
  356. index: 0,
  357. message: {
  358. role: "assistant" as const,
  359. content: response.content?.find((c) => c.type === "text")?.text ?? "",
  360. reasoning_content: response.content?.find((c) => c.type === "reasoning")?.text,
  361. tool_calls: response.content
  362. ?.filter((c) => c.type === "tool-call")
  363. .map((toolCall) => ({
  364. id: toolCall.toolCallId,
  365. type: "function" as const,
  366. function: {
  367. name: toolCall.toolName,
  368. arguments: toolCall.input,
  369. },
  370. })),
  371. },
  372. finish_reason:
  373. (
  374. {
  375. stop: "stop",
  376. length: "length",
  377. "content-filter": "content_filter",
  378. "tool-calls": "tool_calls",
  379. error: "stop",
  380. other: "stop",
  381. unknown: "stop",
  382. } as const
  383. )[response.finishReason] || "stop",
  384. },
  385. ],
  386. usage: {
  387. prompt_tokens: response.usage?.inputTokens,
  388. completion_tokens: response.usage?.outputTokens,
  389. total_tokens: response.usage?.totalTokens,
  390. completion_tokens_details: {
  391. reasoning_tokens: response.usage?.reasoningTokens,
  392. },
  393. prompt_tokens_details: {
  394. cached_tokens: response.usage?.cachedInputTokens,
  395. },
  396. },
  397. })
  398. }
  399. function transformOpenAIRequestToAiSDK() {
  400. const prompt = transformMessages()
  401. return {
  402. prompt,
  403. maxOutputTokens: body.max_tokens ?? body.max_completion_tokens ?? undefined,
  404. temperature: body.temperature ?? undefined,
  405. topP: body.top_p ?? undefined,
  406. frequencyPenalty: body.frequency_penalty ?? undefined,
  407. presencePenalty: body.presence_penalty ?? undefined,
  408. providerOptions: body.reasoning_effort
  409. ? {
  410. anthropic: {
  411. reasoningEffort: body.reasoning_effort,
  412. },
  413. }
  414. : undefined,
  415. stopSequences: (typeof body.stop === "string" ? [body.stop] : body.stop) ?? undefined,
  416. responseFormat: (() => {
  417. if (!body.response_format) return { type: "text" }
  418. if (body.response_format.type === "json_schema")
  419. return {
  420. type: "json",
  421. schema: body.response_format.json_schema.schema,
  422. name: body.response_format.json_schema.name,
  423. description: body.response_format.json_schema.description,
  424. }
  425. if (body.response_format.type === "json_object") return { type: "json" }
  426. throw new Error("Unsupported response format")
  427. })(),
  428. seed: body.seed ?? undefined,
  429. }
  430. function transformTools() {
  431. const { tools, tool_choice } = body
  432. if (!tools || tools.length === 0) {
  433. return { tools: undefined, toolChoice: undefined }
  434. }
  435. const aiSdkTools = tools.reduce(
  436. (acc, tool) => {
  437. acc[tool.function.name] = {
  438. type: "function" as const,
  439. name: tool.function.name,
  440. description: tool.function.description,
  441. inputSchema: tool.function.parameters,
  442. }
  443. return acc
  444. },
  445. {} as Record<string, any>,
  446. )
  447. let aiSdkToolChoice
  448. if (tool_choice == null) {
  449. aiSdkToolChoice = undefined
  450. } else if (tool_choice === "auto") {
  451. aiSdkToolChoice = "auto"
  452. } else if (tool_choice === "none") {
  453. aiSdkToolChoice = "none"
  454. } else if (tool_choice === "required") {
  455. aiSdkToolChoice = "required"
  456. } else if (tool_choice.type === "function") {
  457. aiSdkToolChoice = {
  458. type: "tool",
  459. toolName: tool_choice.function.name,
  460. }
  461. }
  462. return { tools: aiSdkTools, toolChoice: aiSdkToolChoice }
  463. }
  464. function transformMessages() {
  465. const { messages } = body
  466. const prompt: LanguageModelV2Prompt = []
  467. for (const message of messages) {
  468. switch (message.role) {
  469. case "system": {
  470. prompt.push({
  471. role: "system",
  472. content: message.content as string,
  473. })
  474. break
  475. }
  476. case "user": {
  477. if (typeof message.content === "string") {
  478. prompt.push({
  479. role: "user",
  480. content: [{ type: "text", text: message.content }],
  481. })
  482. } else {
  483. const content = message.content.map((part) => {
  484. switch (part.type) {
  485. case "text":
  486. return { type: "text" as const, text: part.text }
  487. case "image_url":
  488. return {
  489. type: "file" as const,
  490. mediaType: "image/jpeg" as const,
  491. data: part.image_url.url,
  492. }
  493. default:
  494. throw new Error(`Unsupported content part type: ${(part as any).type}`)
  495. }
  496. })
  497. prompt.push({
  498. role: "user",
  499. content,
  500. })
  501. }
  502. break
  503. }
  504. case "assistant": {
  505. const content: Array<
  506. | { type: "text"; text: string }
  507. | {
  508. type: "tool-call"
  509. toolCallId: string
  510. toolName: string
  511. input: any
  512. }
  513. > = []
  514. if (message.content) {
  515. content.push({
  516. type: "text",
  517. text: message.content as string,
  518. })
  519. }
  520. if (message.tool_calls) {
  521. for (const toolCall of message.tool_calls) {
  522. content.push({
  523. type: "tool-call",
  524. toolCallId: toolCall.id,
  525. toolName: toolCall.function.name,
  526. input: JSON.parse(toolCall.function.arguments),
  527. })
  528. }
  529. }
  530. prompt.push({
  531. role: "assistant",
  532. content,
  533. })
  534. break
  535. }
  536. case "tool": {
  537. prompt.push({
  538. role: "tool",
  539. content: [
  540. {
  541. type: "tool-result",
  542. toolName: "placeholder",
  543. toolCallId: message.tool_call_id,
  544. output: {
  545. type: "text",
  546. value: message.content as string,
  547. },
  548. },
  549. ],
  550. })
  551. break
  552. }
  553. default: {
  554. throw new Error(`Unsupported message role: ${message.role}`)
  555. }
  556. }
  557. }
  558. return prompt
  559. }
  560. }
  561. async function trackUsage(model: string, usage: LanguageModelV2Usage) {
  562. const keyRecord = c.get("keyRecord")
  563. if (!keyRecord) return
  564. const modelData = SUPPORTED_MODELS[model as keyof typeof SUPPORTED_MODELS]
  565. if (!modelData) throw new Error(`Unsupported model: ${model}`)
  566. const inputCost = modelData.input * (usage.inputTokens ?? 0)
  567. const outputCost = modelData.output * (usage.outputTokens ?? 0)
  568. const reasoningCost = modelData.reasoning * (usage.reasoningTokens ?? 0)
  569. const cacheReadCost = modelData.cacheRead * (usage.cachedInputTokens ?? 0)
  570. const cacheWriteCost = modelData.cacheWrite * (usage.outputTokens ?? 0)
  571. const totalCost = inputCost + outputCost + reasoningCost + cacheReadCost + cacheWriteCost
  572. await Actor.provide("system", { workspaceID: keyRecord.workspaceID }, async () => {
  573. await Billing.consume({
  574. model,
  575. inputTokens: usage.inputTokens ?? 0,
  576. outputTokens: usage.outputTokens ?? 0,
  577. reasoningTokens: usage.reasoningTokens ?? 0,
  578. cacheReadTokens: usage.cachedInputTokens ?? 0,
  579. cacheWriteTokens: usage.outputTokens ?? 0,
  580. costInCents: totalCost * 100,
  581. })
  582. })
  583. await Database.use((tx) =>
  584. tx
  585. .update(KeyTable)
  586. .set({ timeUsed: sql`now()` })
  587. .where(eq(KeyTable.id, keyRecord.id)),
  588. )
  589. }
  590. } catch (error: any) {
  591. return c.json({ error: { message: error.message } }, 500)
  592. }
  593. })
  594. .use("/*", cors())
  595. .use(RestAuth)
  596. .get("/rest/account", async (c) => {
  597. const account = Actor.assert("account")
  598. let workspaces = await Workspace.list()
  599. if (workspaces.length === 0) {
  600. await Workspace.create()
  601. workspaces = await Workspace.list()
  602. }
  603. return c.json({
  604. id: account.properties.accountID,
  605. email: account.properties.email,
  606. workspaces,
  607. })
  608. })
  609. .get("/billing/info", async (c) => {
  610. const billing = await Billing.get()
  611. const payments = await Database.use((tx) =>
  612. tx
  613. .select()
  614. .from(PaymentTable)
  615. .where(eq(PaymentTable.workspaceID, Actor.workspace()))
  616. .orderBy(sql`${PaymentTable.timeCreated} DESC`)
  617. .limit(100),
  618. )
  619. const usage = await Database.use((tx) =>
  620. tx
  621. .select()
  622. .from(UsageTable)
  623. .where(eq(UsageTable.workspaceID, Actor.workspace()))
  624. .orderBy(sql`${UsageTable.timeCreated} DESC`)
  625. .limit(100),
  626. )
  627. return c.json({ billing, payments, usage })
  628. })
  629. .post(
  630. "/billing/checkout",
  631. zValidator(
  632. "json",
  633. z.custom<{
  634. success_url: string
  635. cancel_url: string
  636. }>(),
  637. ),
  638. async (c) => {
  639. const account = Actor.assert("user")
  640. const body = await c.req.json()
  641. const customer = await Billing.get()
  642. const session = await Billing.stripe().checkout.sessions.create({
  643. mode: "payment",
  644. line_items: [
  645. {
  646. price_data: {
  647. currency: "usd",
  648. product_data: {
  649. name: "OpenControl credits",
  650. },
  651. unit_amount: 2000, // $20 minimum
  652. },
  653. quantity: 1,
  654. },
  655. ],
  656. payment_intent_data: {
  657. setup_future_usage: "on_session",
  658. },
  659. ...(customer.customerID
  660. ? { customer: customer.customerID }
  661. : {
  662. customer_email: account.properties.email,
  663. customer_creation: "always",
  664. }),
  665. metadata: {
  666. workspaceID: Actor.workspace(),
  667. },
  668. currency: "usd",
  669. payment_method_types: ["card"],
  670. success_url: body.success_url,
  671. cancel_url: body.cancel_url,
  672. })
  673. return c.json({
  674. url: session.url,
  675. })
  676. },
  677. )
  678. .post("/billing/portal", async (c) => {
  679. const body = await c.req.json()
  680. const customer = await Billing.get()
  681. if (!customer?.customerID) {
  682. throw new Error("No stripe customer ID")
  683. }
  684. const session = await Billing.stripe().billingPortal.sessions.create({
  685. customer: customer.customerID,
  686. return_url: body.return_url,
  687. })
  688. return c.json({
  689. url: session.url,
  690. })
  691. })
  692. .post("/stripe/webhook", async (c) => {
  693. const body = await Billing.stripe().webhooks.constructEventAsync(
  694. await c.req.text(),
  695. c.req.header("stripe-signature")!,
  696. Resource.STRIPE_WEBHOOK_SECRET.value,
  697. )
  698. console.log(body.type, JSON.stringify(body, null, 2))
  699. if (body.type === "checkout.session.completed") {
  700. const workspaceID = body.data.object.metadata?.workspaceID
  701. const customerID = body.data.object.customer as string
  702. const paymentID = body.data.object.payment_intent as string
  703. const amount = body.data.object.amount_total
  704. if (!workspaceID) throw new Error("Workspace ID not found")
  705. if (!customerID) throw new Error("Customer ID not found")
  706. if (!amount) throw new Error("Amount not found")
  707. if (!paymentID) throw new Error("Payment ID not found")
  708. await Actor.provide("system", { workspaceID }, async () => {
  709. const customer = await Billing.get()
  710. if (customer?.customerID && customer.customerID !== customerID) throw new Error("Customer ID mismatch")
  711. // set customer metadata
  712. if (!customer?.customerID) {
  713. await Billing.stripe().customers.update(customerID, {
  714. metadata: {
  715. workspaceID,
  716. },
  717. })
  718. }
  719. // get payment method for the payment intent
  720. const paymentIntent = await Billing.stripe().paymentIntents.retrieve(paymentID, {
  721. expand: ["payment_method"],
  722. })
  723. const paymentMethod = paymentIntent.payment_method
  724. if (!paymentMethod || typeof paymentMethod === "string") throw new Error("Payment method not expanded")
  725. await Database.transaction(async (tx) => {
  726. await tx
  727. .update(BillingTable)
  728. .set({
  729. balance: sql`${BillingTable.balance} + ${centsToMicroCents(amount)}`,
  730. customerID,
  731. paymentMethodID: paymentMethod.id,
  732. paymentMethodLast4: paymentMethod.card!.last4,
  733. })
  734. .where(eq(BillingTable.workspaceID, workspaceID))
  735. await tx.insert(PaymentTable).values({
  736. workspaceID,
  737. id: Identifier.create("payment"),
  738. amount: centsToMicroCents(amount),
  739. paymentID,
  740. customerID,
  741. })
  742. })
  743. })
  744. }
  745. console.log("finished handling")
  746. return c.json("ok", 200)
  747. })
  748. .get("/keys", async (c) => {
  749. const user = Actor.assert("user")
  750. const keys = await Database.use((tx) =>
  751. tx
  752. .select({
  753. id: KeyTable.id,
  754. name: KeyTable.name,
  755. key: KeyTable.key,
  756. userID: KeyTable.userID,
  757. timeCreated: KeyTable.timeCreated,
  758. timeUsed: KeyTable.timeUsed,
  759. })
  760. .from(KeyTable)
  761. .where(eq(KeyTable.workspaceID, user.properties.workspaceID))
  762. .orderBy(sql`${KeyTable.timeCreated} DESC`),
  763. )
  764. return c.json({ keys })
  765. })
  766. .post("/keys", zValidator("json", z.object({ name: z.string().min(1).max(255) })), async (c) => {
  767. const user = Actor.assert("user")
  768. const { name } = c.req.valid("json")
  769. // Generate secret key: sk- + 64 random characters (upper, lower, numbers)
  770. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  771. let randomPart = ""
  772. for (let i = 0; i < 64; i++) {
  773. randomPart += chars.charAt(Math.floor(Math.random() * chars.length))
  774. }
  775. const secretKey = `sk-${randomPart}`
  776. const keyRecord = await Database.use((tx) =>
  777. tx
  778. .insert(KeyTable)
  779. .values({
  780. id: Identifier.create("key"),
  781. workspaceID: user.properties.workspaceID,
  782. userID: user.properties.userID,
  783. name,
  784. key: secretKey,
  785. timeUsed: null,
  786. })
  787. .returning(),
  788. )
  789. return c.json({
  790. key: secretKey,
  791. id: keyRecord[0].id,
  792. name: keyRecord[0].name,
  793. created: keyRecord[0].timeCreated,
  794. })
  795. })
  796. .delete("/keys/:id", async (c) => {
  797. const user = Actor.assert("user")
  798. const keyId = c.req.param("id")
  799. const result = await Database.use((tx) =>
  800. tx
  801. .delete(KeyTable)
  802. .where(and(eq(KeyTable.id, keyId), eq(KeyTable.workspaceID, user.properties.workspaceID)))
  803. .returning({ id: KeyTable.id }),
  804. )
  805. if (result.length === 0) {
  806. return c.json({ error: "Key not found" }, 404)
  807. }
  808. return c.json({ success: true, id: result[0].id })
  809. })
  810. .all("*", (c) => c.text("Not Found"))
  811. export type ApiType = typeof app
  812. export default app