Repository navigation
Expand file tree
/
Copy pathusageTargets.ts
More file actions
106 lines (103 loc) · 3.06 KB
/
Copy pathusageTargets.ts
File metadata and controls
106 lines (103 loc) · 3.06 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
import type { Account, AccountSettings, Agent, Session } from "./types";
export type UsageTarget = {
key: string;
provider: Agent["provider"];
environment: string;
managed: boolean;
accountID?: string;
account?: Account;
agentNames: string[];
sessionNames: string[];
};
/** Device login and each managed identity own separate quota snapshots. */
export function usageTargets(
agents: Agent[],
sessions: Session[],
accounts: Account[],
): UsageTarget[] {
const targets = new Map<string, UsageTarget>();
function add(
agent: Agent,
settings: AccountSettings,
environment: string,
session?: Session,
) {
const managed =
!!settings.account_id ||
(settings.account_policy ?? "manual") !== "manual";
const matching = accounts.filter(
(a) => a.provider === agent.provider && a.environment_id === environment,
);
const candidates = settings.account_id
? [settings.account_id]
: managed
? matching
.filter(
(a) =>
a.status !== "removed" &&
(!settings.account_ids?.length ||
settings.account_ids.includes(a.id)),
)
.map((a) => a.id)
: [];
for (const accountID of candidates.length ? candidates : [undefined]) {
const key = JSON.stringify([
environment,
agent.provider,
managed ? (accountID ?? "automatic") : null,
]);
let target = targets.get(key);
if (!target) {
target = {
key,
provider: agent.provider,
environment,
managed,
accountID,
account: matching.find((a) => a.id === accountID),
agentNames: [],
sessionNames: [],
};
targets.set(key, target);
}
if (!target.agentNames.includes(agent.name))
target.agentNames.push(agent.name);
if (session && !target.sessionNames.includes(session.title))
target.sessionNames.push(session.title);
}
}
for (const agent of agents)
add(agent, agent, agent.environment_id ?? "local");
for (const session of sessions) {
const agent = agents.find((a) => a.id === session.agent_id);
if (!agent) continue;
// Older API fixtures omitted account fields. Explicit null means device login,
// even when the agent now defaults to a different managed account.
const settings =
"account_id" in session || "account_policy" in session ? session : agent;
add(
agent,
settings,
session.environment_id ?? agent.environment_id ?? "local",
session,
);
}
return [...targets.values()];
}
export function quotaRefreshTargets(targets: UsageTarget[]) {
return targets
.filter(
(target) =>
!target.managed ||
(target.account &&
target.account.enabled !== false &&
["ready", "cooldown"].includes(target.account.status)),
)
.map(({ key, provider, environment, accountID, account }) => ({
key,
provider,
environment,
accountID,
generation: account?.generation,
}));
}