archondev 2.19.53 → 2.19.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +52 -27
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3106,43 +3106,68 @@ async function fetchByokUsageStatsFromSupabase(accessToken, authId) {
3106
3106
  const { SUPABASE_URL: SUPABASE_URL2, SUPABASE_ANON_KEY: SUPABASE_ANON_KEY2 } = await import("./constants-XDIWFFPN.js");
3107
3107
  const { createAuthedSupabaseClient: createAuthedSupabaseClient2 } = await import("./client-PHW2C2HB.js");
3108
3108
  const supabase = createAuthedSupabaseClient2(SUPABASE_URL2, SUPABASE_ANON_KEY2, accessToken);
3109
- const { data: rawProfile, error: profileError } = await supabase.from("user_profiles").select("id, current_period_start, current_period_end").eq("auth_id", authId).single();
3110
- const profile = rawProfile;
3111
- if (profileError || !profile?.id) {
3109
+ const profileId = await resolveProfileIdForUsage(supabase, authId);
3110
+ if (!profileId) {
3112
3111
  return null;
3113
3112
  }
3114
3113
  const now = /* @__PURE__ */ new Date();
3115
- const defaultStart = new Date(now.getFullYear(), now.getMonth(), 1);
3116
- const defaultEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
3117
- const periodStart = profile.current_period_start ? new Date(profile.current_period_start) : defaultStart;
3118
- const periodEnd = profile.current_period_end ? new Date(profile.current_period_end) : defaultEnd;
3119
- const { data: rawUsageRows } = await supabase.from("token_usage").select("model, input_tokens, output_tokens, base_cost, total_cents, marked_up_cost").eq("user_id", profile.id).gte("created_at", periodStart.toISOString()).lte("created_at", periodEnd.toISOString());
3120
- const usageRows = rawUsageRows;
3121
- let totalInputTokens = 0;
3122
- let totalOutputTokens = 0;
3123
- let totalBaseCost = 0;
3124
- const byModelMap = /* @__PURE__ */ new Map();
3125
- for (const row of usageRows ?? []) {
3126
- totalInputTokens += row.input_tokens ?? 0;
3127
- totalOutputTokens += row.output_tokens ?? 0;
3128
- const baseCost = typeof row.base_cost === "number" ? row.base_cost : typeof row.total_cents === "number" ? row.total_cents / 100 : typeof row.marked_up_cost === "number" ? row.marked_up_cost : 0;
3129
- totalBaseCost += baseCost;
3130
- byModelMap.set(row.model, (byModelMap.get(row.model) ?? 0) + baseCost);
3114
+ const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
3115
+ const monthStats = await fetchByokUsageWindow(supabase, profileId, monthStart, now);
3116
+ if (hasAnyByokUsage(monthStats)) {
3117
+ return {
3118
+ ...monthStats,
3119
+ periodStart: monthStart.toISOString(),
3120
+ periodEnd: now.toISOString(),
3121
+ periodSource: "month"
3122
+ };
3131
3123
  }
3132
- const byModel = Array.from(byModelMap.entries()).map(([model, cost]) => ({ model, cost })).sort((a, b) => b.cost - a.cost);
3124
+ const trailingStart = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1e3);
3125
+ const trailingStats = await fetchByokUsageWindow(supabase, profileId, trailingStart, now);
3133
3126
  return {
3134
- totalInputTokens,
3135
- totalOutputTokens,
3136
- totalBaseCost,
3137
- byModel,
3138
- periodStart: periodStart.toISOString(),
3139
- periodEnd: periodEnd.toISOString(),
3140
- periodSource: profile.current_period_start ? "profile_period" : "month"
3127
+ ...trailingStats,
3128
+ periodStart: trailingStart.toISOString(),
3129
+ periodEnd: now.toISOString(),
3130
+ periodSource: "profile_period"
3141
3131
  };
3142
3132
  } catch {
3143
3133
  return null;
3144
3134
  }
3145
3135
  }
3136
+ async function resolveProfileIdForUsage(supabase, authId) {
3137
+ const { data: byAuthRaw } = await supabase.from("user_profiles").select("id").eq("auth_id", authId).maybeSingle();
3138
+ const byAuth = byAuthRaw;
3139
+ if (byAuth?.id) {
3140
+ return byAuth.id;
3141
+ }
3142
+ const { data: byProfileRaw } = await supabase.from("user_profiles").select("id").eq("id", authId).maybeSingle();
3143
+ const byProfile = byProfileRaw;
3144
+ return byProfile?.id ?? null;
3145
+ }
3146
+ async function fetchByokUsageWindow(supabase, profileId, start2, end) {
3147
+ const { data: rawUsageRows } = await supabase.from("token_usage").select("model, input_tokens, output_tokens, base_cost, total_cents, marked_up_cost").eq("user_id", profileId).gte("created_at", start2.toISOString()).lte("created_at", end.toISOString());
3148
+ const usageRows = rawUsageRows;
3149
+ let totalInputTokens = 0;
3150
+ let totalOutputTokens = 0;
3151
+ let totalBaseCost = 0;
3152
+ const byModelMap = /* @__PURE__ */ new Map();
3153
+ for (const row of usageRows ?? []) {
3154
+ totalInputTokens += row.input_tokens ?? 0;
3155
+ totalOutputTokens += row.output_tokens ?? 0;
3156
+ const baseCost = typeof row.base_cost === "number" ? row.base_cost : typeof row.total_cents === "number" ? row.total_cents / 100 : typeof row.marked_up_cost === "number" ? row.marked_up_cost : 0;
3157
+ totalBaseCost += baseCost;
3158
+ byModelMap.set(row.model, (byModelMap.get(row.model) ?? 0) + baseCost);
3159
+ }
3160
+ const byModel = Array.from(byModelMap.entries()).map(([model, cost]) => ({ model, cost })).sort((a, b) => b.cost - a.cost);
3161
+ return {
3162
+ totalInputTokens,
3163
+ totalOutputTokens,
3164
+ totalBaseCost,
3165
+ byModel
3166
+ };
3167
+ }
3168
+ function hasAnyByokUsage(stats) {
3169
+ return stats.totalInputTokens > 0 || stats.totalOutputTokens > 0 || stats.totalBaseCost > 0 || stats.byModel.length > 0;
3170
+ }
3146
3171
  async function fetchCreditsUsageStatsFromSupabase(accessToken, authId) {
3147
3172
  try {
3148
3173
  const { SUPABASE_URL: SUPABASE_URL2, SUPABASE_ANON_KEY: SUPABASE_ANON_KEY2 } = await import("./constants-XDIWFFPN.js");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "archondev",
3
- "version": "2.19.53",
3
+ "version": "2.19.54",
4
4
  "description": "Local-first AI-powered development governance system",
5
5
  "main": "dist/index.js",
6
6
  "bin": {