Skip to content

Commit 52b5125

Browse files
committed
Discounted rate is now getting applied
1 parent a24f59b commit 52b5125

File tree

16 files changed

+735
-405
lines changed

16 files changed

+735
-405
lines changed

bifrost/lib/clients/jawnTypes/private.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ export interface paths {
551551
delete: operations["RemoveOrgMember"];
552552
patch: operations["UpdateOrgMemberRole"];
553553
};
554+
"/v1/admin/org/{orgId}/pricing-config": {
555+
patch: operations["UpdateOrgPricingConfig"];
556+
};
554557
"/v1/admin/org/{orgId}/delete": {
555558
post: operations["DeleteOrg"];
556559
};
@@ -19522,6 +19525,29 @@ export interface operations {
1952219525
};
1952319526
};
1952419527
};
19528+
UpdateOrgPricingConfig: {
19529+
parameters: {
19530+
path: {
19531+
orgId: string;
19532+
};
19533+
};
19534+
requestBody: {
19535+
content: {
19536+
"application/json": {
19537+
/** Format: double */
19538+
heliconePricingMultiplier: number;
19539+
};
19540+
};
19541+
};
19542+
responses: {
19543+
/** @description Ok */
19544+
200: {
19545+
content: {
19546+
"application/json": components["schemas"]["Result_null.string_"];
19547+
};
19548+
};
19549+
};
19550+
};
1952519551
DeleteOrg: {
1952619552
parameters: {
1952719553
path: {

helicone-cron/src/db/database.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,7 @@ export type Database = {
17601760
owner: string
17611761
percent_to_log: number | null
17621762
playground_helicone: boolean
1763+
pricing_config: Json | null
17631764
referral: string | null
17641765
request_limit: number | null
17651766
size: string | null
@@ -1792,6 +1793,7 @@ export type Database = {
17921793
owner: string
17931794
percent_to_log?: number | null
17941795
playground_helicone?: boolean
1796+
pricing_config?: Json | null
17951797
referral?: string | null
17961798
request_limit?: number | null
17971799
size?: string | null
@@ -1824,6 +1826,7 @@ export type Database = {
18241826
owner?: string
18251827
percent_to_log?: number | null
18261828
playground_helicone?: boolean
1829+
pricing_config?: Json | null
18271830
referral?: string | null
18281831
request_limit?: number | null
18291832
size?: string | null

packages/cost/costCalc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ export function modelCostBreakdownFromRegistry(params: {
5353
provider: ModelProviderName;
5454
providerModelId: string;
5555
requestCount?: number;
56+
pricingMultiplier?: number;
5657
}): CostBreakdown | null {
5758
const breakdown = calculateModelCostBreakdown({
5859
modelUsage: params.modelUsage,
5960
providerModelId: params.providerModelId,
6061
provider: params.provider,
6162
requestCount: params.requestCount,
63+
pricingMultiplier: params.pricingMultiplier,
6264
});
63-
65+
6466
return breakdown;
6567
}

packages/cost/models/calculate-cost.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ export function calculateModelCostBreakdown(
3737
providerModelId: string;
3838
provider: ModelProviderName;
3939
requestCount?: number;
40+
pricingMultiplier?: number;
4041
}
4142
): CostBreakdown | null {
42-
const { modelUsage, providerModelId, provider, requestCount = 1 } = params;
43+
const { modelUsage, providerModelId, provider, requestCount = 1, pricingMultiplier = 1.0 } = params;
4344

4445
const configResult = registry.getModelProviderConfigByProviderModelId(providerModelId, provider);
4546
if (configResult.error || !configResult.data) return null;
@@ -112,7 +113,7 @@ export function calculateModelCostBreakdown(
112113
breakdown.requestCost = requestCount * pricing.request;
113114
}
114115

115-
breakdown.totalCost =
116+
breakdown.totalCost =
116117
breakdown.inputCost +
117118
breakdown.outputCost +
118119
breakdown.cachedInputCost +
@@ -125,5 +126,7 @@ export function calculateModelCostBreakdown(
125126
breakdown.imageCost +
126127
breakdown.requestCost;
127128

129+
breakdown.totalCost *= pricingMultiplier;
130+
128131
return breakdown;
129132
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table "public"."organization" add column "pricing_config" jsonb;

valhalla/jawn/src/controllers/private/adminController.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,36 @@ export class AdminController extends Controller {
13481348
return ok(null);
13491349
}
13501350

1351+
@Post("/org/{orgId}/pricing-config")
1352+
public async updateOrgPricingConfig(
1353+
@Request() request: JawnAuthenticatedRequest,
1354+
@Path() orgId: string,
1355+
@Body() body: { heliconePricingMultiplier: number }
1356+
): Promise<Result<null, string>> {
1357+
await authCheckThrow(request.authParams.userId);
1358+
1359+
const { heliconePricingMultiplier } = body;
1360+
1361+
if (
1362+
heliconePricingMultiplier < 0 ||
1363+
heliconePricingMultiplier > 2 ||
1364+
isNaN(heliconePricingMultiplier)
1365+
) {
1366+
return err("Pricing multiplier must be between 0 and 2");
1367+
}
1368+
1369+
const { error } = await dbExecute(
1370+
`UPDATE organization SET pricing_config = jsonb_set(COALESCE(pricing_config, '{}'), '{heliconePricingMultiplier}', $1::text::jsonb) WHERE id = $2`,
1371+
[heliconePricingMultiplier.toString(), orgId]
1372+
);
1373+
1374+
if (error) {
1375+
return err(error);
1376+
}
1377+
1378+
return ok(null);
1379+
}
1380+
13511381
@Post("/org/{orgId}/delete")
13521382
public async deleteOrg(
13531383
@Request() request: JawnAuthenticatedRequest,

valhalla/jawn/src/lib/db/database.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,7 @@ export type Database = {
17601760
owner: string
17611761
percent_to_log: number | null
17621762
playground_helicone: boolean
1763+
pricing_config: Json | null
17631764
referral: string | null
17641765
request_limit: number | null
17651766
size: string | null
@@ -1792,6 +1793,7 @@ export type Database = {
17921793
owner: string
17931794
percent_to_log?: number | null
17941795
playground_helicone?: boolean
1796+
pricing_config?: Json | null
17951797
referral?: string | null
17961798
request_limit?: number | null
17971799
size?: string | null
@@ -1824,6 +1826,7 @@ export type Database = {
18241826
owner?: string
18251827
percent_to_log?: number | null
18261828
playground_helicone?: boolean
1829+
pricing_config?: Json | null
18271830
referral?: string | null
18281831
request_limit?: number | null
18291832
size?: string | null

valhalla/jawn/src/tsoa-build/private/routes.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20628,6 +20628,39 @@ export function RegisterRoutes(app: Router) {
2062820628
}
2062920629
});
2063020630
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
20631+
const argsAdminController_updateOrgPricingConfig: Record<string, TsoaRoute.ParameterSchema> = {
20632+
request: {"in":"request","name":"request","required":true,"dataType":"object"},
20633+
orgId: {"in":"path","name":"orgId","required":true,"dataType":"string"},
20634+
body: {"in":"body","name":"body","required":true,"dataType":"nestedObjectLiteral","nestedProperties":{"heliconePricingMultiplier":{"dataType":"double","required":true}}},
20635+
};
20636+
app.patch('/v1/admin/org/:orgId/pricing-config',
20637+
authenticateMiddleware([{"api_key":[]}]),
20638+
...(fetchMiddlewares<RequestHandler>(AdminController)),
20639+
...(fetchMiddlewares<RequestHandler>(AdminController.prototype.updateOrgPricingConfig)),
20640+
20641+
async function AdminController_updateOrgPricingConfig(request: ExRequest, response: ExResponse, next: any) {
20642+
20643+
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
20644+
20645+
let validatedArgs: any[] = [];
20646+
try {
20647+
validatedArgs = templateService.getValidatedArgs({ args: argsAdminController_updateOrgPricingConfig, request, response });
20648+
20649+
const controller = new AdminController();
20650+
20651+
await templateService.apiHandler({
20652+
methodName: 'updateOrgPricingConfig',
20653+
controller,
20654+
response,
20655+
next,
20656+
validatedArgs,
20657+
successStatus: undefined,
20658+
});
20659+
} catch (err) {
20660+
return next(err);
20661+
}
20662+
});
20663+
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
2063120664
const argsAdminController_deleteOrg: Record<string, TsoaRoute.ParameterSchema> = {
2063220665
request: {"in":"request","name":"request","required":true,"dataType":"object"},
2063320666
orgId: {"in":"path","name":"orgId","required":true,"dataType":"string"},

valhalla/jawn/src/tsoa-build/private/swagger.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56871,6 +56871,60 @@
5687156871
}
5687256872
}
5687356873
},
56874+
"/v1/admin/org/{orgId}/pricing-config": {
56875+
"patch": {
56876+
"operationId": "UpdateOrgPricingConfig",
56877+
"responses": {
56878+
"200": {
56879+
"description": "Ok",
56880+
"content": {
56881+
"application/json": {
56882+
"schema": {
56883+
"$ref": "#/components/schemas/Result_null.string_"
56884+
}
56885+
}
56886+
}
56887+
}
56888+
},
56889+
"tags": [
56890+
"Admin"
56891+
],
56892+
"security": [
56893+
{
56894+
"api_key": []
56895+
}
56896+
],
56897+
"parameters": [
56898+
{
56899+
"in": "path",
56900+
"name": "orgId",
56901+
"required": true,
56902+
"schema": {
56903+
"type": "string"
56904+
}
56905+
}
56906+
],
56907+
"requestBody": {
56908+
"required": true,
56909+
"content": {
56910+
"application/json": {
56911+
"schema": {
56912+
"properties": {
56913+
"heliconePricingMultiplier": {
56914+
"type": "number",
56915+
"format": "double"
56916+
}
56917+
},
56918+
"required": [
56919+
"heliconePricingMultiplier"
56920+
],
56921+
"type": "object"
56922+
}
56923+
}
56924+
}
56925+
}
56926+
}
56927+
},
5687456928
"/v1/admin/org/{orgId}/delete": {
5687556929
"post": {
5687656930
"operationId": "DeleteOrg",

0 commit comments

Comments
 (0)