File size: 6,719 Bytes
0c8b3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e5ecc4
0c8b3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e5ecc4
0c8b3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
7e5ecc4
 
 
 
 
 
 
 
 
0c8b3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * AccountPersistence — file-system persistence for AccountPool.
 * Handles load/save/migrate operations as an injectable dependency.
 */

import {
  readFileSync,
  writeFileSync,
  renameSync,
  existsSync,
  mkdirSync,
} from "fs";
import { resolve, dirname } from "path";
import { randomBytes } from "crypto";
import { getDataDir } from "../paths.js";
import {
  extractChatGptAccountId,
  extractUserProfile,
  isTokenExpired,
} from "./jwt-utils.js";
import type { AccountEntry, AccountsFile } from "./types.js";

export interface AccountPersistence {
  load(): { entries: AccountEntry[]; needsPersist: boolean };
  save(accounts: AccountEntry[]): void;
}

function getAccountsFile(): string {
  return resolve(getDataDir(), "accounts.json");
}
function getLegacyAuthFile(): string {
  return resolve(getDataDir(), "auth.json");
}

export function createFsPersistence(): AccountPersistence {
  const persistence: AccountPersistence = {
    load(): { entries: AccountEntry[]; needsPersist: boolean } {
      // Migrate from legacy auth.json if needed
      const migrated = migrateFromLegacy();

      // Load from accounts.json
      const { entries: loaded, needsPersist } = loadPersisted();

      const entries = migrated.length > 0 && loaded.length === 0 ? migrated : loaded;

      // Auto-persist when backfill was applied (preserves original behavior)
      if (needsPersist && loaded.length > 0) {
        persistence.save(loaded);
      }

      return { entries, needsPersist };
    },

    save(accounts: AccountEntry[]): void {
      try {
        const accountsFile = getAccountsFile();
        const dir = dirname(accountsFile);
        if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
        const data: AccountsFile = { accounts };
        const tmpFile = accountsFile + ".tmp";
        writeFileSync(tmpFile, JSON.stringify(data, null, 2), "utf-8");
        renameSync(tmpFile, accountsFile);
      } catch (err) {
        console.error("[AccountPool] Failed to persist accounts:", err instanceof Error ? err.message : err);
      }
    },
  };
  return persistence;
}

function migrateFromLegacy(): AccountEntry[] {
  try {
    const accountsFile = getAccountsFile();
    const legacyAuthFile = getLegacyAuthFile();
    if (existsSync(accountsFile)) return []; // already migrated
    if (!existsSync(legacyAuthFile)) return [];

    const raw = readFileSync(legacyAuthFile, "utf-8");
    const data = JSON.parse(raw) as {
      token: string;
      proxyApiKey?: string | null;
      userInfo?: { email?: string; accountId?: string; planType?: string } | null;
    };

    if (!data.token) return [];

    const id = randomBytes(8).toString("hex");
    const accountId = extractChatGptAccountId(data.token);
    const entry: AccountEntry = {
      id,
      token: data.token,
      refreshToken: null,
      email: data.userInfo?.email ?? null,
      accountId: accountId,
      userId: extractUserProfile(data.token)?.chatgpt_user_id ?? null,
      planType: data.userInfo?.planType ?? null,
      proxyApiKey: data.proxyApiKey ?? "codex-proxy-" + randomBytes(24).toString("hex"),
      status: isTokenExpired(data.token) ? "expired" : "active",
      usage: {
        request_count: 0,
        input_tokens: 0,
        output_tokens: 0,
        empty_response_count: 0,
        last_used: null,
        rate_limit_until: null,
        window_request_count: 0,
        window_input_tokens: 0,
        window_output_tokens: 0,
        window_counters_reset_at: null,
        limit_window_seconds: null,
      },
      addedAt: new Date().toISOString(),
      cachedQuota: null,
      quotaFetchedAt: null,
    };

    // Write new format
    const dir = dirname(accountsFile);
    if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
    const accountsData: AccountsFile = { accounts: [entry] };
    writeFileSync(accountsFile, JSON.stringify(accountsData, null, 2), "utf-8");

    // Rename old file
    renameSync(legacyAuthFile, legacyAuthFile + ".bak");
    console.log("[AccountPool] Migrated from auth.json → accounts.json");
    return [entry];
  } catch (err) {
    console.warn("[AccountPool] Migration failed:", err);
    return [];
  }
}

function loadPersisted(): { entries: AccountEntry[]; needsPersist: boolean } {
  try {
    const accountsFile = getAccountsFile();
    if (!existsSync(accountsFile)) return { entries: [], needsPersist: false };
    const raw = readFileSync(accountsFile, "utf-8");
    const data = JSON.parse(raw) as AccountsFile;
    if (!Array.isArray(data.accounts)) return { entries: [], needsPersist: false };

    const entries: AccountEntry[] = [];
    let needsPersist = false;

    for (const entry of data.accounts) {
      if (!entry.id || !entry.token) continue;

      // Backfill missing fields from JWT
      if (!entry.planType || !entry.email || !entry.accountId || !entry.userId) {
        const profile = extractUserProfile(entry.token);
        const accountId = extractChatGptAccountId(entry.token);
        if (!entry.planType && profile?.chatgpt_plan_type) {
          entry.planType = profile.chatgpt_plan_type;
          needsPersist = true;
        }
        if (!entry.email && profile?.email) {
          entry.email = profile.email;
          needsPersist = true;
        }
        if (!entry.accountId && accountId) {
          entry.accountId = accountId;
          needsPersist = true;
        }
        if (!entry.userId && profile?.chatgpt_user_id) {
          entry.userId = profile.chatgpt_user_id;
          needsPersist = true;
        }
      }
      // Backfill userId for entries missing it (pre-v1.0.68)
      if (entry.userId === undefined) {
        entry.userId = null;
        needsPersist = true;
      }
      // Backfill empty_response_count
      if (entry.usage.empty_response_count == null) {
        entry.usage.empty_response_count = 0;
        needsPersist = true;
      }
      // Backfill window counter fields
      if (entry.usage.window_request_count == null) {
        entry.usage.window_request_count = 0;
        entry.usage.window_input_tokens = 0;
        entry.usage.window_output_tokens = 0;
        entry.usage.window_counters_reset_at = null;
        entry.usage.limit_window_seconds = null;
        needsPersist = true;
      }
      // Backfill cachedQuota fields
      if (entry.cachedQuota === undefined) {
        entry.cachedQuota = null;
        entry.quotaFetchedAt = null;
        needsPersist = true;
      }
      entries.push(entry);
    }

    return { entries, needsPersist };
  } catch (err) {
    console.warn("[AccountPool] Failed to load accounts:", err instanceof Error ? err.message : err);
    return { entries: [], needsPersist: false };
  }
}