Repository navigation
feat(ai): add explicit provider-group routing (0.2.12 candidate) #65
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,98 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| /** Provider pricing filters, not a total-request budget. The host must verify | ||
| * eligible metadata and non-token charges; missing pricing is not zero. */ | ||
| export const providerMaxPrice = z | ||
| .object({ | ||
| /** USD per million text input tokens. */ | ||
| prompt: z.number().finite().nonnegative().max(Number.MAX_SAFE_INTEGER), | ||
| /** USD per million billed completion tokens, including reasoning. */ | ||
| completion: z.number().finite().nonnegative().max(Number.MAX_SAFE_INTEGER), | ||
| /** No per-request charge is approved. This filter is not metadata proof. */ | ||
| request: z.literal(0), | ||
| }) | ||
| .strict(); | ||
|
|
||
| export type SupportProviderMaxPrice = z.infer<typeof providerMaxPrice>; | ||
|
|
||
| interface Selection { | ||
| readonly endpointTag: string; | ||
| readonly providerSelectionScope?: "provider_group" | undefined; | ||
| readonly providerMaxPrice?: SupportProviderMaxPrice | undefined; | ||
| readonly approval: { | ||
| readonly providerSelectionScope?: "provider_group" | undefined; | ||
| }; | ||
| } | ||
|
|
||
| interface PriceBudget { | ||
| readonly maxInputTokens: number; | ||
| readonly maxOutputTokens: number; | ||
| readonly maxCostMicros: number; | ||
| readonly currency: string; | ||
| } | ||
|
|
||
| function decimal(value: number): { digits: bigint; scale: number } { | ||
| const [mantissa, exponent = "0"] = value.toString().split("e"); | ||
| const [whole, fraction = ""] = mantissa!.split("."); | ||
| return { | ||
| digits: BigInt(whole! + fraction), | ||
| scale: fraction.length - Number(exponent), | ||
| }; | ||
| } | ||
|
|
||
| /** Token price USD/million is numerically microUSD/token. Add exact decimal | ||
| * terms before rounding once; binary multiplication must not lower the bound. */ | ||
| function fitsPriceBudget(price: SupportProviderMaxPrice, budget: PriceBudget) { | ||
| if ( | ||
| budget.currency !== "USD" || | ||
| !Number.isSafeInteger(budget.maxInputTokens) || | ||
| budget.maxInputTokens < 1 || | ||
| !Number.isSafeInteger(budget.maxOutputTokens) || | ||
| budget.maxOutputTokens < 1 || | ||
| !Number.isSafeInteger(budget.maxCostMicros) || | ||
| budget.maxCostMicros < 0 | ||
| ) | ||
| return false; | ||
| const prompt = decimal(price.prompt); | ||
| const completion = decimal(price.completion); | ||
| const scale = Math.max(0, prompt.scale, completion.scale); | ||
| const denominator = 10n ** BigInt(scale); | ||
| const numerator = | ||
| BigInt(budget.maxInputTokens) * | ||
| prompt.digits * | ||
| 10n ** BigInt(scale - prompt.scale) + | ||
| BigInt(budget.maxOutputTokens) * | ||
| completion.digits * | ||
| 10n ** BigInt(scale - completion.scale); | ||
| const micros = (numerator + denominator - 1n) / denominator; | ||
| return micros <= BigInt(budget.maxCostMicros); | ||
| } | ||
|
|
||
| /** New policy is opt-in. Absence means no own property, not a property set to | ||
| * undefined. Incomplete policy objects fail closed even when authoring types | ||
| * can describe them. Truly absent keys add no wire property and retain the | ||
| * generic transport's existing selector contract. */ | ||
| export function selectedProviderMaxPrice( | ||
| route: Selection, | ||
| budget: PriceBudget, | ||
| allowProviderGroup: boolean, | ||
| ): SupportProviderMaxPrice | undefined { | ||
| const scoped = Object.hasOwn(route, "providerSelectionScope"); | ||
| const priced = Object.hasOwn(route, "providerMaxPrice"); | ||
| const approved = Object.hasOwn(route.approval, "providerSelectionScope"); | ||
| if (!scoped && !priced && !approved) return undefined; | ||
| if ( | ||
| !allowProviderGroup || | ||
| !scoped || | ||
| !priced || | ||
| !approved || | ||
| route.providerSelectionScope !== "provider_group" || | ||
| route.approval.providerSelectionScope !== "provider_group" || | ||
| !/^[a-z0-9-]+$/u.test(route.endpointTag) | ||
| ) | ||
| throw new TypeError("Provider-group policy unavailable"); | ||
| const price = providerMaxPrice.parse(route.providerMaxPrice); | ||
| if (!fitsPriceBudget(price, budget)) | ||
| throw new TypeError("Provider-group price exceeds total budget"); | ||
| return price; | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.