fix: metering service global write and open ai default model (#1717)

This commit is contained in:
Daniel Salazar
2025-10-08 18:41:15 -07:00
committed by GitHub
parent 73a24af951
commit eea27be8eb
3 changed files with 17 additions and 10 deletions

View File

@@ -105,6 +105,10 @@ export class OpenAICompletionService {
return model_names;
}
get_default_model(){
return this.#defaultModel;
}
async complete({ messages, stream, model, tools, max_tokens, temperature }) {
return await this.#complete(messages, {
model: model,

View File

@@ -40,6 +40,10 @@ export class OpenAICompletionServiceWrapper extends BaseService {
return await this.openAICompletionService.checkModeration(text);
}
get_default_model() {
return this.openAICompletionService.get_default_model();
}
static IMPLEMENTS = {
['puter-chat-completion']: Object.getOwnPropertyNames(OpenAICompletionService.prototype)
.filter(n => n !== 'constructor')

View File

@@ -37,6 +37,7 @@ const POLICY_TYPES = {
const GLOBAL_APP_KEY = 'os-global'; // TODO DS: this should be loaded from config or db eventually
const METRICS_PREFIX = 'metering';
const POLICY_PREFIX = 'policy';
const PERIOD_ESCAPE = '_dot_'; // to replace dots in usage types for kvstore paths
/**
* Handles usage metering and supports stubbs for billing methods for current scoped actor
*/
@@ -75,6 +76,7 @@ export class MeteringAndBillingService {
return this.#superUserService.sudo(async () => {
const totalCost = (costOverride ?? COST_MAPS[usageType] * usageAmount) || 0; // TODO DS: apply our policy discounts here eventually
usageType = usageType.replace(/\./g, PERIOD_ESCAPE) as keyof typeof COST_MAPS; // replace dots with underscores for kvstore paths, TODO DS: map this back when reading
const appId = actor.type?.app?.uid || GLOBAL_APP_KEY
const actorId = actor.type?.user.uuid
const pathAndAmountMap = {
@@ -96,6 +98,12 @@ export class MeteringAndBillingService {
pathAndAmountMap,
})
const puterConsumptionKey = `${METRICS_PREFIX}:puter:${currentMonth}`; // global consumption across all users and apps
this.#kvClientWrapper.incr({
key: puterConsumptionKey,
pathAndAmountMap
})
const actorAppUsageKey = `${METRICS_PREFIX}:actor:${actorId}:app:${appId}:${currentMonth}`;
this.#kvClientWrapper.incr({
key: actorAppUsageKey,
@@ -116,16 +124,7 @@ export class MeteringAndBillingService {
[`${appId}.count`]: 1,
},
})
const puterConsumptionKey = `${METRICS_PREFIX}:puter:${currentMonth}`; // global consumption across all users and apps
this.#kvClientWrapper.incr({
key: puterConsumptionKey,
pathAndAmountMap: {
'total': totalCost,
[`${usageType}.units`]: usageAmount,
[`${usageType}.cost`]: totalCost,
[`${usageType}.count`]: 1,
}
})
return (await Promise.all([lastUpdatedPromise, actorUsagesPromise]))[1] as UsageByType;
})