-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudget.service.ts
More file actions
110 lines (95 loc) · 3.31 KB
/
Copy pathbudget.service.ts
File metadata and controls
110 lines (95 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Service, computed, signal } from '@angular/core';
export interface BudgetConfig {
readonly maxTokens: number | null;
readonly maxRounds: number | null;
readonly maxCostUsd: number | null;
}
export interface BudgetSnapshot {
readonly tokensUsed: number;
readonly roundsUsed: number;
readonly costUsd: number;
}
export type BudgetBreach =
| { readonly kind: 'tokens'; readonly limit: number; readonly used: number }
| { readonly kind: 'rounds'; readonly limit: number; readonly used: number }
| { readonly kind: 'cost'; readonly limit: number; readonly used: number };
const STORAGE_KEY = 'atlas:budget';
const DEFAULT_CONFIG: BudgetConfig = {
maxTokens: null,
maxRounds: null,
maxCostUsd: null,
};
@Service()
export class BudgetService {
private readonly _config = signal<BudgetConfig>(loadStored());
readonly config = this._config.asReadonly();
readonly hasAnyLimit = computed(() => {
const c = this._config();
return c.maxTokens !== null || c.maxRounds !== null || c.maxCostUsd !== null;
});
update(patch: Partial<BudgetConfig>): void {
this._config.update((current) => {
const next: BudgetConfig = { ...current, ...patch };
persist(next);
return next;
});
}
reset(): void {
this._config.set(DEFAULT_CONFIG);
persist(DEFAULT_CONFIG);
}
utilisation(snapshot: BudgetSnapshot): {
readonly tokens: number | null;
readonly rounds: number | null;
readonly cost: number | null;
} {
const c = this._config();
return {
tokens: c.maxTokens === null ? null : clamp(snapshot.tokensUsed / c.maxTokens, 0, 1.5),
rounds: c.maxRounds === null ? null : clamp(snapshot.roundsUsed / c.maxRounds, 0, 1.5),
cost: c.maxCostUsd === null ? null : clamp(snapshot.costUsd / c.maxCostUsd, 0, 1.5),
};
}
evaluate(snapshot: BudgetSnapshot): BudgetBreach | null {
const c = this._config();
if (c.maxRounds !== null && snapshot.roundsUsed >= c.maxRounds) {
return { kind: 'rounds', limit: c.maxRounds, used: snapshot.roundsUsed };
}
if (c.maxTokens !== null && snapshot.tokensUsed >= c.maxTokens) {
return { kind: 'tokens', limit: c.maxTokens, used: snapshot.tokensUsed };
}
if (c.maxCostUsd !== null && snapshot.costUsd >= c.maxCostUsd) {
return { kind: 'cost', limit: c.maxCostUsd, used: snapshot.costUsd };
}
return null;
}
}
function clamp(n: number, min: number, max: number): number {
return Math.max(min, Math.min(max, n));
}
function loadStored(): BudgetConfig {
if (typeof localStorage === 'undefined') return DEFAULT_CONFIG;
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return DEFAULT_CONFIG;
const parsed = JSON.parse(raw) as Partial<BudgetConfig>;
return {
maxTokens: numericOrNull(parsed.maxTokens),
maxRounds: numericOrNull(parsed.maxRounds),
maxCostUsd: numericOrNull(parsed.maxCostUsd),
};
} catch {
return DEFAULT_CONFIG;
}
}
function persist(config: BudgetConfig): void {
if (typeof localStorage === 'undefined') return;
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
} catch {
// localStorage unavailable (quota, private browsing) — keep in-memory state
}
}
function numericOrNull(v: unknown): number | null {
return typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : null;
}