Repository navigation
feat(usage): one row per provider account with limits, priority, pause and names #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
1zun4
wants to merge
7
commits into
ompweb-dev:main
Choose a base branch
from
1zun4:feat/provider-usage-accounts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,806
−423
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
daeceec
feat(usage): one row per provider account with limits, priority, paus…
1zun4 c304689
fix(usage): drop the highest-usage summary from the panel header
1zun4 5f3b837
feat(usage): one meter line per quota and drag to reorder accounts
1zun4 5fd9033
fix(usage): an open account row shows its quotas once
1zun4 f8a33cd
fix(usage): smooth expand layout, non-default badges, and review fixes
1zun4 a0cc8b8
Merge remote-tracking branch 'ompweb-dev/main' into feat/provider-usa…
1zun4 a63abb9
fix(usage): keep provider accounts visible
1zun4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { invalidateModelsCache } from "@/lib/models-cache"; | ||
| import { getProviderUsageAccounts, renameProviderUsageAccount, updateProviderUsageAccount } from "@/lib/provider-usage"; | ||
| import { saveWebServerSettings } from "@/lib/web-settings"; | ||
| import { isRecord } from "@/lib/type-guards"; | ||
|
|
||
| const PROVIDER_RE = /^[A-Za-z0-9._:/-]{1,200}$/; | ||
|
|
||
| export async function GET(request: Request) { | ||
| try { | ||
| return Response.json(await getProviderUsageAccounts(new URL(request.url).searchParams.get("refresh") === "true")); | ||
| } catch { | ||
| return Response.json( | ||
| { error: "Provider usage is currently unavailable", code: "provider_usage_unavailable" }, | ||
| { status: 502 }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Body `{ order }` saves the rows' order (`ProviderUsageAccount.key`s); `{ key, name }` names an account | ||
| * (`name: null` or blank clears it); `{ provider, credentialId, paused?, limitPercent?, priority? }` | ||
| * changes a stored one, `null` clearing a limit or priority. | ||
| */ | ||
| export async function PATCH(request: Request) { | ||
| const body: unknown = await request.json().catch(() => null); | ||
| if (!isRecord(body)) return Response.json({ error: "Body must be a JSON object" }, { status: 400 }); | ||
| if (body.order !== undefined) { | ||
| const { order } = body; | ||
| if (!Array.isArray(order) || order.length > 500 || order.some((key) => typeof key !== "string" || key.length > 300)) { | ||
| return Response.json({ error: "order must be an array of at most 500 account keys" }, { status: 400 }); | ||
| } | ||
| saveWebServerSettings({ accountOrder: order as string[] }); | ||
| return Response.json({ ok: true }); | ||
| } | ||
| if (body.key !== undefined) { | ||
| const { key, name } = body; | ||
| if (typeof key !== "string" || key.length > 300 || (name !== null && (typeof name !== "string" || name.length > 80))) { | ||
| return Response.json({ error: "key must be a string and name a string of at most 80 characters, or null" }, { status: 400 }); | ||
| } | ||
| renameProviderUsageAccount(key, name); | ||
| return Response.json({ ok: true }); | ||
| } | ||
| const { provider, credentialId, paused, limitPercent, priority } = body; | ||
| if (typeof provider !== "string" || !PROVIDER_RE.test(provider) || !Number.isSafeInteger(credentialId)) { | ||
| return Response.json({ error: "provider and an integer credentialId are required" }, { status: 400 }); | ||
| } | ||
| if (paused !== undefined && typeof paused !== "boolean") { | ||
| return Response.json({ error: "paused must be a boolean" }, { status: 400 }); | ||
| } | ||
| if (limitPercent !== undefined && limitPercent !== null | ||
| && (!Number.isInteger(limitPercent) || (limitPercent as number) < 1 || (limitPercent as number) > 100)) { | ||
| return Response.json({ error: "limitPercent must be an integer from 1 to 100, or null" }, { status: 400 }); | ||
| } | ||
| if (priority !== undefined && priority !== null && (!Number.isInteger(priority) || Math.abs(priority as number) > 1000)) { | ||
| return Response.json({ error: "priority must be an integer from -1000 to 1000, or null" }, { status: 400 }); | ||
| } | ||
| try { | ||
| await updateProviderUsageAccount(provider, credentialId as number, { | ||
| ...(paused !== undefined ? { paused } : {}), | ||
| ...(limitPercent !== undefined ? { limitPercent: limitPercent as number | null } : {}), | ||
| ...(priority !== undefined ? { priority: priority as number | null } : {}), | ||
| }); | ||
| if (paused !== undefined) invalidateModelsCache(); | ||
| return Response.json({ ok: true }); | ||
| } catch (error) { | ||
| return Response.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve the old response value in the delayed GET.
The fetch mock reads
cachedPercentafter the wait at Line 25. After Line 59, the original GET therefore returns 65. This test also passes ifreload()only returns the existing request.Capture
cachedPercentbefore the wait. Also assert thatreload()starts an additional GET withoutrefresh=true.