-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaccount-pool.ts
More file actions
162 lines (143 loc) · 4.71 KB
/
account-pool.ts
File metadata and controls
162 lines (143 loc) · 4.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// CHANGE: add API-level account pool service with persistence and rate-limit monitoring
// WHY: enable automatic switching between registered accounts when one hits API rate limits
// QUOTE(ТЗ): "Сделать возможность регистрировать много аккаунтов codex, claude code и когда на одном лимиты закаончиваются он переходит на другой аккаунт"
// REF: issue-213
// SOURCE: n/a
// FORMAT THEOREM: ∀op ∈ PoolOperation: op(state) → persist(nextState) ∧ consistent(nextState)
// PURITY: SHELL
// EFFECT: Effect<Result, ApiError>
// INVARIANT: pool state is persisted to disk after every mutation; in-memory state is source of truth
// COMPLEXITY: O(n) per operation where n = total accounts
import { defaultProjectsRoot } from "@effect-template/lib/usecases/path-helpers"
import type {
AccountPoolProvider,
AccountPoolState,
AccountEntry,
RateLimitEvent
} from "@effect-template/lib/core/account-pool-domain"
import {
addAccount,
removeAccount,
markRateLimited,
clearCooldown,
selectNextAvailable,
advanceActiveIndex,
listAccounts,
listAllAccounts,
poolSummary,
emptyPoolState
} from "@effect-template/lib/usecases/account-pool"
import { detectRateLimit } from "@effect-template/lib/usecases/rate-limit-detector"
import { promises as fs } from "node:fs"
import { join } from "node:path"
let poolState: AccountPoolState = emptyPoolState(new Date().toISOString())
let initialized = false
const nowIso = (): string => new Date().toISOString()
const stateFilePath = (): string =>
join(defaultProjectsRoot(process.cwd()), ".orch", "state", "account-pool.json")
const persistState = async (): Promise<void> => {
const filePath = stateFilePath()
await fs.mkdir(join(filePath, ".."), { recursive: true })
await fs.writeFile(filePath, JSON.stringify(poolState, null, 2), "utf8")
}
const persistBestEffort = (): void => {
void persistState().catch(() => {
// best effort
})
}
export const initializeAccountPool = async (): Promise<void> => {
if (initialized) {
return
}
const filePath = stateFilePath()
const exists = await fs.stat(filePath).then(() => true).catch(() => false)
if (exists) {
const raw = await fs.readFile(filePath, "utf8")
const parsed = JSON.parse(raw) as AccountPoolState
poolState = {
pools: parsed.pools ?? [],
updatedAt: parsed.updatedAt ?? nowIso()
}
}
initialized = true
}
export const addPoolAccount = (
provider: AccountPoolProvider,
label: string
): AccountPoolState => {
const now = nowIso()
poolState = addAccount(poolState, provider, label, now)
persistBestEffort()
return poolState
}
export const removePoolAccount = (
provider: AccountPoolProvider,
label: string
): AccountPoolState => {
const now = nowIso()
poolState = removeAccount(poolState, provider, label, now)
persistBestEffort()
return poolState
}
export const markAccountRateLimited = (
event: RateLimitEvent
): AccountPoolState => {
const now = nowIso()
poolState = markRateLimited(poolState, event, now)
persistBestEffort()
return poolState
}
export const clearAccountCooldown = (
provider: AccountPoolProvider,
label: string
): AccountPoolState => {
const now = nowIso()
poolState = clearCooldown(poolState, provider, label, now)
persistBestEffort()
return poolState
}
export const selectNextPoolAccount = (
provider: AccountPoolProvider
): AccountEntry | undefined => {
const now = nowIso()
const account = selectNextAvailable(poolState, provider, now)
if (account !== undefined) {
poolState = advanceActiveIndex(poolState, provider, now)
persistBestEffort()
}
return account
}
export const listPoolAccounts = (
provider: AccountPoolProvider
): ReadonlyArray<AccountEntry> =>
listAccounts(poolState, provider)
export const listAllPoolAccounts = (): ReadonlyArray<AccountEntry> =>
listAllAccounts(poolState)
export const getPoolSummary = (
provider: AccountPoolProvider
): {
readonly total: number
readonly available: number
readonly coolingDown: number
readonly activeLabel: string | undefined
} => poolSummary(poolState, provider, nowIso())
export const getPoolState = (): AccountPoolState => poolState
/**
* Check an agent output line for rate-limit signals.
* If a rate-limit is detected, marks the account as rate-limited
* and returns the event for the caller to act upon.
*
* @effect mutates poolState on detection
*/
export const checkLineForRateLimit = (
provider: AccountPoolProvider,
label: string,
line: string
): RateLimitEvent | undefined => {
const now = nowIso()
const event = detectRateLimit(provider, label, line, now)
if (event !== undefined) {
markAccountRateLimited(event)
}
return event
}