GeezerVison is showing github.com — wip: zen · f2dmarques/opencode@cf069dd
File treeExpand file treeCollapse file tree Open diff view settings app/src/routes/workspace/[id]/billing
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/routes/workspace/[id]/billing/payment-section.module.css +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 45 | 45 | text-decoration: line-through; | 46 | 46 | } | 47 | 47 | } | | 48 | + | | 49 | + &[data-slot="payment-receipt"] { | | 50 | + span { | | 51 | + display: inline-block; | | 52 | + padding: var(--space-3) var(--space-4); | | 53 | + font-size: var(--font-size-sm); | | 54 | + line-height: 1.5; | | 55 | + } | | 56 | + | | 57 | + button { | | 58 | + font-size: var(--font-size-sm); | | 59 | + } | | 60 | + } | 48 | 61 | } | 49 | 62 |
| 50 | 63 | tbody tr { | | 54 | 67 | } | 55 | 68 |
| 56 | 69 | @media (max-width: 40rem) { | | 70 | + | 57 | 71 | th, | 58 | 72 | td { | 59 | 73 | padding: var(--space-2) var(--space-3); | 60 | 74 | font-size: var(--font-size-xs); | 61 | 75 | } | 62 | 76 |
| 63 | 77 | th { | 64 | | - &:nth-child(2) /* Payment ID */ { | | 78 | + &:nth-child(2) | | 79 | + | | 80 | + /* Payment ID */ | | 81 | + { | 65 | 82 | display: none; | 66 | 83 | } | 67 | 84 | } | 68 | 85 |
| 69 | 86 | td { | 70 | | - &:nth-child(2) /* Payment ID */ { | | 87 | + &:nth-child(2) | | 88 | + | | 89 | + /* Payment ID */ | | 90 | + { | 71 | 91 | display: none; | 72 | 92 | } | 73 | 93 | } | 74 | 94 | } | 75 | 95 | } | 76 | | -} | | 96 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/routes/workspace/[id]/billing/payment-section.tsx +17-11Lines changed: 17 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,6 +77,7 @@ export function PaymentSection() { | 77 | 77 | <For each={payments()!}> | 78 | 78 | {(payment) => { | 79 | 79 | const date = new Date(payment.timeCreated) | | 80 | + const isCredit = !payment.paymentID | 80 | 81 | return ( | 81 | 82 | <tr> | 82 | 83 | <td data-slot="payment-date" title={formatDateUTC(date)}> | @@ -85,19 +86,24 @@ export function PaymentSection() { | 85 | 86 | <td data-slot="payment-id">{payment.id}</td> | 86 | 87 | <td data-slot="payment-amount" data-refunded={!!payment.timeRefunded}> | 87 | 88 | ${((payment.amount ?? 0) / 100000000).toFixed(2)} | | 89 | + {isCredit ? " (credit)" : ""} | 88 | 90 | </td> | 89 | 91 | <td data-slot="payment-receipt"> | 90 | | - <button | 91 | | - onClick={async () => { | 92 | | - const receiptUrl = await downloadReceiptAction(params.id!, payment.paymentID!) | 93 | | - if (receiptUrl) { | 94 | | - window.open(receiptUrl, "_blank") | 95 | | - } | 96 | | - }} | 97 | | - data-slot="receipt-button" | 98 | | - > | 99 | | - View | 100 | | - </button> | | 92 | + {isCredit ? ( | | 93 | + <span>-</span> | | 94 | + ) : ( | | 95 | + <button | | 96 | + onClick={async () => { | | 97 | + const receiptUrl = await downloadReceiptAction(params.id!, payment.paymentID!) | | 98 | + if (receiptUrl) { | | 99 | + window.open(receiptUrl, "_blank") | | 100 | + } | | 101 | + }} | | 102 | + data-slot="receipt-button" | | 103 | + > | | 104 | + View | | 105 | + </button> | | 106 | + )} | 101 | 107 | </td> | 102 | 108 | </tr> | 103 | 109 | ) | |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Billing } from "../src/billing.js" | | 2 | + | | 3 | +// get input from command line | | 4 | +const workspaceID = process.argv[2] | | 5 | +const dollarAmount = process.argv[3] | | 6 | + | | 7 | +if (!workspaceID || !dollarAmount) { | | 8 | + console.error("Usage: bun credit-workspace.ts <workspaceID> <dollarAmount>") | | 9 | + process.exit(1) | | 10 | +} | | 11 | + | | 12 | +const amountInDollars = parseFloat(dollarAmount) | | 13 | +if (isNaN(amountInDollars) || amountInDollars <= 0) { | | 14 | + console.error("Error: dollarAmount must be a positive number") | | 15 | + process.exit(1) | | 16 | +} | | 17 | + | | 18 | +await Billing.grantCredit(workspaceID, amountInDollars) | | 19 | + | | 20 | +console.log(`Added payment of $${amountInDollars.toFixed(2)} to workspace ${workspaceID}`) |
Collapse file Copy file name to clipboardExpand all lines: packages/console/core/script/lookup-user.ts +27-1Lines changed: 27 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Database, eq, sql, inArray } from "../src/drizzle/index.js" | 2 | 2 | import { AuthTable } from "../src/schema/auth.sql.js" | 3 | 3 | import { UserTable } from "../src/schema/user.sql.js" | 4 | | -import { BillingTable, PaymentTable } from "../src/schema/billing.sql.js" | | 4 | +import { BillingTable, PaymentTable, UsageTable } from "../src/schema/billing.sql.js" | 5 | 5 | import { WorkspaceTable } from "../src/schema/workspace.sql.js" | 6 | 6 |
| 7 | 7 | // get input from command line | @@ -95,6 +95,32 @@ async function printWorkspace(workspaceID: string) { | 95 | 95 | })), | 96 | 96 | ), | 97 | 97 | ) | | 98 | + | | 99 | + await printTable("Usage", (tx) => | | 100 | + tx | | 101 | + .select({ | | 102 | + model: UsageTable.model, | | 103 | + provider: UsageTable.provider, | | 104 | + inputTokens: UsageTable.inputTokens, | | 105 | + outputTokens: UsageTable.outputTokens, | | 106 | + reasoningTokens: UsageTable.reasoningTokens, | | 107 | + cacheReadTokens: UsageTable.cacheReadTokens, | | 108 | + cacheWrite5mTokens: UsageTable.cacheWrite5mTokens, | | 109 | + cacheWrite1hTokens: UsageTable.cacheWrite1hTokens, | | 110 | + cost: UsageTable.cost, | | 111 | + timeCreated: UsageTable.timeCreated, | | 112 | + }) | | 113 | + .from(UsageTable) | | 114 | + .where(eq(UsageTable.workspaceID, workspace.id)) | | 115 | + .orderBy(sql`${UsageTable.timeCreated} DESC`) | | 116 | + .limit(1000) | | 117 | + .then((rows) => | | 118 | + rows.map((row) => ({ | | 119 | + ...row, | | 120 | + cost: `$${(row.cost / 100000000).toFixed(2)}`, | | 121 | + })), | | 122 | + ), | | 123 | + ) | 98 | 124 | } | 99 | 125 |
| 100 | 126 | function printHeader(title: string) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/console/core/src/billing.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -157,6 +157,24 @@ export namespace Billing { | 157 | 157 | }) | 158 | 158 | } | 159 | 159 |
| | 160 | + export const grantCredit = async (workspaceID: string, dollarAmount: number) => { | | 161 | + const amountInMicroCents = centsToMicroCents(dollarAmount * 100) | | 162 | + await Database.transaction(async (tx) => { | | 163 | + await tx | | 164 | + .update(BillingTable) | | 165 | + .set({ | | 166 | + balance: sql`${BillingTable.balance} + ${amountInMicroCents}`, | | 167 | + }) | | 168 | + .where(eq(BillingTable.workspaceID, workspaceID)) | | 169 | + await tx.insert(PaymentTable).values({ | | 170 | + workspaceID, | | 171 | + id: Identifier.create("payment"), | | 172 | + amount: amountInMicroCents, | | 173 | + }) | | 174 | + }) | | 175 | + return amountInMicroCents | | 176 | + } | | 177 | + | 160 | 178 | export const setMonthlyLimit = fn(z.number(), async (input) => { | 161 | 179 | return await Database.use((tx) => | 162 | 180 | tx | |
|