(apiKey, proxy = null)
| 144 | * @returns {Promise<{planName, dailyPercent, weeklyPercent, dailyResetAt, weeklyResetAt, prompt:{used,limit}, flex:{used,limit}, raw}>} |
| 145 | */ |
| 146 | export async function getUserStatus(apiKey, proxy = null) { |
| 147 | const body = { |
| 148 | metadata: { |
| 149 | apiKey, |
| 150 | ideName: 'windsurf', |
| 151 | ideVersion: '1.9600.41', |
| 152 | extensionName: 'windsurf', |
| 153 | extensionVersion: '1.9600.41', |
| 154 | locale: 'en', |
| 155 | }, |
| 156 | }; |
| 157 | |
| 158 | // Try with proxy first, then retry direct if proxy itself fails (407 etc.). |
| 159 | const proxyModes = proxy ? [proxy, null] : [null]; |
| 160 | let lastErr = null; |
| 161 | for (const px of proxyModes) { |
| 162 | for (const host of SERVER_HOSTS) { |
| 163 | try { |
| 164 | const res = await postJson(host, USER_STATUS_PATH, body, px); |
| 165 | if (res.status >= 400) { |
| 166 | lastErr = new Error(`GetUserStatus ${host} → ${res.status}: ${res.raw.slice(0, 160)}`); |
| 167 | continue; |
| 168 | } |
| 169 | return normalizeUserStatus(res.data); |
| 170 | } catch (e) { |
| 171 | lastErr = e; |
| 172 | log.debug(`getCreditUsage ${host} failed: ${e.message}`); |
| 173 | if (px && isProxyError(e)) break; // skip second host, go straight to direct |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | throw lastErr || new Error('GetUserStatus: all hosts failed'); |
| 178 | } |
| 179 | |
| 180 | function normalizeUserStatus(data) { |
| 181 | const ps = data?.userStatus?.planStatus || {}; |
no test coverage detected