claude-codex-proxy 1.0.5-beta.4 → 1.0.5-beta.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -272
- package/bin/cli.js +37 -2
- package/package.json +1 -1
- package/public/css/style.css +132 -0
- package/public/js/app.js +105 -26
- package/public/views/accounts-modals.html +90 -29
- package/public/views/accounts.html +29 -10
- package/public/views/client-api-keys-modals.html +10 -1
- package/public/views/client-api-keys.html +68 -39
- package/public/views/logs.html +10 -3
- package/public/views/settings.html +11 -1
- package/src/account-manager.js +34 -3
- package/src/account-rotation/index.js +6 -0
- package/src/account-rotation/rate-limits.js +4 -0
- package/src/account-rotation/strategies/base-strategy.js +4 -0
- package/src/account-rotation/strategies/round-robin-strategy.js +78 -3
- package/src/account-rotation/strategies/sticky-strategy.js +7 -0
- package/src/admin-path.js +33 -0
- package/src/client-key-usage.js +193 -125
- package/src/daily-anchor-ping.js +149 -0
- package/src/direct-api.js +6 -5
- package/src/format-converter.js +83 -48
- package/src/index.js +13 -4
- package/src/middleware/auth.js +23 -20
- package/src/middleware/credentials.js +17 -7
- package/src/middleware/sse.js +72 -30
- package/src/model-api.js +86 -13
- package/src/model-mapper.js +3 -4
- package/src/model-pricing.js +187 -96
- package/src/response-streamer.js +18 -2
- package/src/routes/accounts-route.js +106 -2
- package/src/routes/api-routes.js +59 -42
- package/src/routes/chat-route.js +5 -4
- package/src/routes/claude-config-route.js +19 -10
- package/src/routes/client-quota-route.js +31 -0
- package/src/routes/logs-route.js +21 -2
- package/src/routes/messages-route.js +135 -55
- package/src/routes/models-route.js +5 -0
- package/src/routes/settings-route.js +40 -10
- package/src/server-settings.js +10 -3
- package/src/server.js +4 -0
- package/src/utils/logger.js +34 -2
- package/src/utils/proxy-logging.js +5 -5
package/src/client-key-usage.js
CHANGED
|
@@ -1,125 +1,193 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
return
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
export function
|
|
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
|
-
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'fs';
|
|
2
|
+
import { randomUUID } from 'crypto';
|
|
3
|
+
import { dirname, join } from 'path';
|
|
4
|
+
import { CONFIG_DIR } from './account-manager.js';
|
|
5
|
+
|
|
6
|
+
const USAGE_FILE = join(CONFIG_DIR, 'client-key-usage.json');
|
|
7
|
+
export const MICRO_USD_PER_USD = 1_000_000;
|
|
8
|
+
|
|
9
|
+
function loadUsage(usageFile = USAGE_FILE) {
|
|
10
|
+
if (!existsSync(usageFile)) return {};
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const data = JSON.parse(readFileSync(usageFile, 'utf8'));
|
|
14
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
15
|
+
throw new Error('usage file must contain a JSON object');
|
|
16
|
+
}
|
|
17
|
+
return data;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
throw new Error(`[ClientKeyUsage] Failed to read ${usageFile}: ${error.message}`, { cause: error });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function saveUsage(usage, usageFile = USAGE_FILE) {
|
|
24
|
+
const usageDirectory = dirname(usageFile);
|
|
25
|
+
if (!existsSync(usageDirectory)) {
|
|
26
|
+
mkdirSync(usageDirectory, { recursive: true, mode: 0o700 });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const tempFile = `${usageFile}.${process.pid}.${randomUUID()}.tmp`;
|
|
30
|
+
try {
|
|
31
|
+
writeFileSync(tempFile, JSON.stringify(usage, null, 2), { mode: 0o600 });
|
|
32
|
+
renameSync(tempFile, usageFile);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
try {
|
|
35
|
+
rmSync(tempFile, { force: true });
|
|
36
|
+
} catch {
|
|
37
|
+
// Preserve the original write error.
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns the current weekly period start in the server's local time zone.
|
|
45
|
+
* A period begins at Monday 08:00 and ends at the following Monday 08:00.
|
|
46
|
+
*/
|
|
47
|
+
export function getCurrentPeriodStart(now = new Date()) {
|
|
48
|
+
const periodStart = new Date(now);
|
|
49
|
+
const daysSinceMonday = (periodStart.getDay() + 6) % 7;
|
|
50
|
+
periodStart.setDate(periodStart.getDate() - daysSinceMonday);
|
|
51
|
+
periodStart.setHours(8, 0, 0, 0);
|
|
52
|
+
|
|
53
|
+
if (now < periodStart) {
|
|
54
|
+
periodStart.setDate(periodStart.getDate() - 7);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return periodStart.toISOString();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function getNextPeriodStart(now = new Date()) {
|
|
61
|
+
const nextPeriodStart = new Date(getCurrentPeriodStart(now));
|
|
62
|
+
nextPeriodStart.setDate(nextPeriodStart.getDate() + 7);
|
|
63
|
+
return nextPeriodStart.toISOString();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const FIVE_HOURS_MS = 5 * 60 * 60 * 1000;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Returns the start of the current globally aligned five-hour window.
|
|
70
|
+
* Boundaries occur every five hours from the Unix epoch and always fall on
|
|
71
|
+
* an exact hour.
|
|
72
|
+
*/
|
|
73
|
+
export function getCurrentFiveHourPeriodStart(now = new Date()) {
|
|
74
|
+
return new Date(Math.floor(now.getTime() / FIVE_HOURS_MS) * FIVE_HOURS_MS).toISOString();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function getNextFiveHourPeriodStart(now = new Date()) {
|
|
78
|
+
return new Date(new Date(getCurrentFiveHourPeriodStart(now)).getTime() + FIVE_HOURS_MS).toISOString();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function usdToMicrousd(usd) {
|
|
82
|
+
return Math.round(Number(usd) * MICRO_USD_PER_USD);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function microusdToUsd(microusd) {
|
|
86
|
+
return Number(microusd) / MICRO_USD_PER_USD;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns a key's usage after lazily resetting it for a new weekly period.
|
|
91
|
+
*/
|
|
92
|
+
export function getClientKeyUsage(keyId, now = new Date()) {
|
|
93
|
+
const usage = loadUsage();
|
|
94
|
+
const periodStart = getCurrentPeriodStart(now);
|
|
95
|
+
const fiveHourPeriodStart = getCurrentFiveHourPeriodStart(now);
|
|
96
|
+
const entry = usage[keyId];
|
|
97
|
+
const consumedMicrousd = entry?.periodStart === periodStart && Number.isSafeInteger(entry.consumedMicrousd)
|
|
98
|
+
? Math.max(0, entry.consumedMicrousd)
|
|
99
|
+
: 0;
|
|
100
|
+
const fiveHourConsumedMicrousd = entry?.fiveHourPeriodStart === fiveHourPeriodStart
|
|
101
|
+
&& Number.isSafeInteger(entry.fiveHourConsumedMicrousd)
|
|
102
|
+
? Math.max(0, entry.fiveHourConsumedMicrousd)
|
|
103
|
+
: 0;
|
|
104
|
+
|
|
105
|
+
if (!entry
|
|
106
|
+
|| entry.periodStart !== periodStart
|
|
107
|
+
|| entry.consumedMicrousd !== consumedMicrousd
|
|
108
|
+
|| entry.fiveHourPeriodStart !== fiveHourPeriodStart
|
|
109
|
+
|| entry.fiveHourConsumedMicrousd !== fiveHourConsumedMicrousd) {
|
|
110
|
+
usage[keyId] = {
|
|
111
|
+
periodStart,
|
|
112
|
+
consumedMicrousd,
|
|
113
|
+
fiveHourPeriodStart,
|
|
114
|
+
fiveHourConsumedMicrousd
|
|
115
|
+
};
|
|
116
|
+
saveUsage(usage);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
periodStart,
|
|
121
|
+
nextPeriodStart: getNextPeriodStart(now),
|
|
122
|
+
consumedMicrousd,
|
|
123
|
+
fiveHourPeriodStart,
|
|
124
|
+
nextFiveHourPeriodStart: getNextFiveHourPeriodStart(now),
|
|
125
|
+
fiveHourConsumedMicrousd
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Records actual USD consumption for a client key. Call this only after an
|
|
131
|
+
* upstream response provides a verified per-request charge.
|
|
132
|
+
*/
|
|
133
|
+
export function recordClientKeyUsage(keyId, consumedMicrousd, now = new Date()) {
|
|
134
|
+
if (!Number.isSafeInteger(consumedMicrousd) || consumedMicrousd < 0) {
|
|
135
|
+
throw new TypeError('consumedMicrousd must be a non-negative safe integer');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const current = getClientKeyUsage(keyId, now);
|
|
139
|
+
const usage = loadUsage();
|
|
140
|
+
usage[keyId] = {
|
|
141
|
+
periodStart: current.periodStart,
|
|
142
|
+
consumedMicrousd: current.consumedMicrousd + consumedMicrousd,
|
|
143
|
+
fiveHourPeriodStart: current.fiveHourPeriodStart,
|
|
144
|
+
fiveHourConsumedMicrousd: current.fiveHourConsumedMicrousd + consumedMicrousd
|
|
145
|
+
};
|
|
146
|
+
saveUsage(usage);
|
|
147
|
+
return usage[keyId];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function deleteClientKeyUsage(keyId) {
|
|
151
|
+
const usage = loadUsage();
|
|
152
|
+
if (!(keyId in usage)) return;
|
|
153
|
+
delete usage[keyId];
|
|
154
|
+
saveUsage(usage);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Returns a key's current quota summary without charging any request.
|
|
159
|
+
*/
|
|
160
|
+
export function getClientKeyQuota(keyId, weeklyLimitUsd, fiveHourLimit = {}, now = new Date()) {
|
|
161
|
+
// Preserve the previous third-argument `now` form for internal callers and tests.
|
|
162
|
+
if (fiveHourLimit instanceof Date) {
|
|
163
|
+
now = fiveHourLimit;
|
|
164
|
+
fiveHourLimit = {};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const usage = getClientKeyUsage(keyId, now);
|
|
168
|
+
const weeklyLimitMicrousd = usdToMicrousd(weeklyLimitUsd);
|
|
169
|
+
const remainingMicrousd = Math.max(0, weeklyLimitMicrousd - usage.consumedMicrousd);
|
|
170
|
+
const fiveHourLimitEnabled = fiveHourLimit.enabled === true;
|
|
171
|
+
const fiveHourLimitMicrousd = usdToMicrousd(fiveHourLimit.usd ?? 0);
|
|
172
|
+
const fiveHourRemainingMicrousd = Math.max(
|
|
173
|
+
0,
|
|
174
|
+
fiveHourLimitMicrousd - usage.fiveHourConsumedMicrousd
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
...usage,
|
|
179
|
+
weeklyLimitMicrousd,
|
|
180
|
+
remainingMicrousd,
|
|
181
|
+
weeklyLimitUsd: microusdToUsd(weeklyLimitMicrousd),
|
|
182
|
+
consumedUsd: microusdToUsd(usage.consumedMicrousd),
|
|
183
|
+
remainingUsd: microusdToUsd(remainingMicrousd),
|
|
184
|
+
fiveHourLimitEnabled,
|
|
185
|
+
fiveHourLimitMicrousd,
|
|
186
|
+
fiveHourRemainingMicrousd,
|
|
187
|
+
fiveHourLimitUsd: microusdToUsd(fiveHourLimitMicrousd),
|
|
188
|
+
fiveHourConsumedUsd: microusdToUsd(usage.fiveHourConsumedMicrousd),
|
|
189
|
+
fiveHourRemainingUsd: microusdToUsd(fiveHourRemainingMicrousd)
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { USAGE_FILE, loadUsage, saveUsage };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Daily Anchor Ping
|
|
3
|
+
* Every day at a fixed local hour (default 09:00) this module sends a minimal
|
|
4
|
+
* "你好" message to every configured account so the upstream Codex five-hour
|
|
5
|
+
* usage window is re-anchored to that moment. Because the upstream window
|
|
6
|
+
* advances from the first usage of the window, an anchor ping at 09:00 local
|
|
7
|
+
* time makes the upstream reset time settle at 14:00 local time (09:00 + 5h).
|
|
8
|
+
*
|
|
9
|
+
* The scheduler uses a self-rescheduling setTimeout that targets the exact
|
|
10
|
+
* next anchor instant, so the ping lands at 09:00:00.000 even across long
|
|
11
|
+
* uptimes; on the first run after a restart it only fires if 09:00 has not
|
|
12
|
+
* passed yet today, so it never disturbs an already-active window.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { loadAccounts } from './account-manager.js';
|
|
16
|
+
import { getCredentialsForAccount } from './middleware/credentials.js';
|
|
17
|
+
import { sendMessage } from './direct-api.js';
|
|
18
|
+
import { resolveModelRouting } from './model-mapper.js';
|
|
19
|
+
import { logger } from './utils/logger.js';
|
|
20
|
+
|
|
21
|
+
/** Local hour at which the anchor ping runs every day. */
|
|
22
|
+
export const ANCHOR_HOUR = 9;
|
|
23
|
+
|
|
24
|
+
/** Payload text sent upstream to advance the five-hour window start. */
|
|
25
|
+
export const PING_TEXT = '你好';
|
|
26
|
+
|
|
27
|
+
/** Maximum completion tokens for the anchor ping; only a single reply is needed. */
|
|
28
|
+
const PING_MAX_TOKENS = 16;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Hard cap for one anchor ping round. `sendMessage` has no built-in request
|
|
32
|
+
* timeout, so a hung upstream response could otherwise stall `pingAllAccounts`
|
|
33
|
+
* forever and break the self-rescheduling chain.
|
|
34
|
+
*/
|
|
35
|
+
const PING_TIMEOUT_MS = 5 * 60 * 1000;
|
|
36
|
+
|
|
37
|
+
/** Sample Claude model used to resolve the active upstream Codex model. */
|
|
38
|
+
const SAMPLE_MODEL = 'claude-sonnet-4-5';
|
|
39
|
+
|
|
40
|
+
let anchorTimer = null;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Computes the next Date at the given local hour (minutes/seconds zeroed)
|
|
44
|
+
* that is strictly after `now`.
|
|
45
|
+
*
|
|
46
|
+
* @param {Date} now - current time
|
|
47
|
+
* @param {number} [anchorHour] - local hour of the anchor
|
|
48
|
+
* @returns {Date} the next anchor instant
|
|
49
|
+
*/
|
|
50
|
+
export function getNextAnchorTime(now, anchorHour = ANCHOR_HOUR) {
|
|
51
|
+
const next = new Date(now);
|
|
52
|
+
next.setHours(anchorHour, 0, 0, 0);
|
|
53
|
+
if (next.getTime() <= now.getTime()) {
|
|
54
|
+
next.setDate(next.getDate() + 1);
|
|
55
|
+
}
|
|
56
|
+
return next;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Builds the minimal Anthropic-style request used for the anchor ping.
|
|
61
|
+
*/
|
|
62
|
+
function buildAnchorRequest() {
|
|
63
|
+
const { upstreamModel } = resolveModelRouting(SAMPLE_MODEL);
|
|
64
|
+
return {
|
|
65
|
+
model: upstreamModel,
|
|
66
|
+
max_tokens: PING_MAX_TOKENS,
|
|
67
|
+
messages: [{ role: 'user', content: PING_TEXT }]
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Sends the anchor message to every configured account. Each account is
|
|
73
|
+
* handled independently so a single failure never aborts the rest.
|
|
74
|
+
*/
|
|
75
|
+
async function pingAllAccounts() {
|
|
76
|
+
const data = loadAccounts();
|
|
77
|
+
const accounts = data.accounts || [];
|
|
78
|
+
if (accounts.length === 0) {
|
|
79
|
+
logger.info('[AnchorPing] No accounts configured; skipping daily ping');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const request = buildAnchorRequest();
|
|
84
|
+
logger.info(`[AnchorPing] Sending "${PING_TEXT}" to ${accounts.length} account(s) to anchor the five-hour window`);
|
|
85
|
+
|
|
86
|
+
for (const account of accounts) {
|
|
87
|
+
try {
|
|
88
|
+
const creds = await getCredentialsForAccount(account.email);
|
|
89
|
+
if (!creds) {
|
|
90
|
+
logger.warn(`[AnchorPing] Skip ${account.email}: no valid credentials`);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
await sendMessage(request, creds.accessToken, creds.accountId, {}, null, creds.email);
|
|
94
|
+
logger.info(`[AnchorPing] Sent "${PING_TEXT}" via ${account.email}`);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
logger.warn(`[AnchorPing] Failed for ${account.email}: ${error.message}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Starts the daily anchor ping scheduler. Safe to call multiple times.
|
|
103
|
+
*/
|
|
104
|
+
export function startDailyAnchorPing() {
|
|
105
|
+
if (anchorTimer) return;
|
|
106
|
+
|
|
107
|
+
const scheduleNext = () => {
|
|
108
|
+
const next = getNextAnchorTime(new Date());
|
|
109
|
+
const delay = next.getTime() - Date.now();
|
|
110
|
+
logger.info(`[AnchorPing] Next ping scheduled at ${next.toLocaleString()}`);
|
|
111
|
+
anchorTimer = setTimeout(async () => {
|
|
112
|
+
// Race the whole ping round against a hard cap: even if one `sendMessage`
|
|
113
|
+
// hangs upstream, `scheduleNext()` in the `finally` block still runs and
|
|
114
|
+
// tomorrow's ping is never lost. A stray hung request may linger in the
|
|
115
|
+
// background, but it can no longer break the daily chain.
|
|
116
|
+
const guard = new Promise(resolve => setTimeout(resolve, PING_TIMEOUT_MS));
|
|
117
|
+
try {
|
|
118
|
+
await Promise.race([pingAllAccounts(), guard]);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
// Guard against any unforeseen error so the loop always reschedules.
|
|
121
|
+
logger.warn(`[AnchorPing] Unexpected ping error: ${error.message}`);
|
|
122
|
+
} finally {
|
|
123
|
+
scheduleNext();
|
|
124
|
+
}
|
|
125
|
+
}, delay);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
scheduleNext();
|
|
129
|
+
logger.info(`[AnchorPing] Daily anchor ping enabled at local hour ${ANCHOR_HOUR}:00`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Stops the daily anchor ping scheduler and clears any pending timer.
|
|
134
|
+
*/
|
|
135
|
+
export function stopDailyAnchorPing() {
|
|
136
|
+
if (anchorTimer) {
|
|
137
|
+
clearTimeout(anchorTimer);
|
|
138
|
+
anchorTimer = null;
|
|
139
|
+
}
|
|
140
|
+
logger.info('[AnchorPing] Daily anchor ping stopped');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default {
|
|
144
|
+
startDailyAnchorPing,
|
|
145
|
+
stopDailyAnchorPing,
|
|
146
|
+
getNextAnchorTime,
|
|
147
|
+
ANCHOR_HOUR,
|
|
148
|
+
PING_TEXT
|
|
149
|
+
};
|
package/src/direct-api.js
CHANGED
|
@@ -14,7 +14,6 @@ const require = createRequire(import.meta.url);
|
|
|
14
14
|
const packageJson = require('../package.json');
|
|
15
15
|
const API_URL = 'https://chatgpt.com/backend-api/codex/responses';
|
|
16
16
|
const APP_VERSION = packageJson.version;
|
|
17
|
-
const UPSTREAM_REQUEST_TIMEOUT_MS = 120000;
|
|
18
17
|
|
|
19
18
|
function createAnthropicUsage(upstreamUsage = {}) {
|
|
20
19
|
const usage = {
|
|
@@ -129,8 +128,7 @@ async function createResponsesAPIStreamResponse(anthropicRequest, accessToken, a
|
|
|
129
128
|
const response = await fetch(API_URL, {
|
|
130
129
|
method: 'POST',
|
|
131
130
|
headers: buildOpencodeHeaders(accessToken, accountId, context),
|
|
132
|
-
body: JSON.stringify(request)
|
|
133
|
-
signal: AbortSignal.timeout(UPSTREAM_REQUEST_TIMEOUT_MS)
|
|
131
|
+
body: JSON.stringify(request)
|
|
134
132
|
});
|
|
135
133
|
|
|
136
134
|
if (response.ok) return response;
|
|
@@ -197,8 +195,7 @@ export async function sendMessage(anthropicRequest, accessToken, accountId, cont
|
|
|
197
195
|
const response = await fetch(API_URL, {
|
|
198
196
|
method: 'POST',
|
|
199
197
|
headers: buildOpencodeHeaders(accessToken, accountId, context),
|
|
200
|
-
body: JSON.stringify(request)
|
|
201
|
-
signal: AbortSignal.timeout(UPSTREAM_REQUEST_TIMEOUT_MS)
|
|
198
|
+
body: JSON.stringify(request)
|
|
202
199
|
});
|
|
203
200
|
|
|
204
201
|
if (!response.ok) {
|
|
@@ -219,6 +216,10 @@ export async function sendMessage(anthropicRequest, accessToken, accountId, cont
|
|
|
219
216
|
throw error;
|
|
220
217
|
}
|
|
221
218
|
|
|
219
|
+
if (response.status === 400) {
|
|
220
|
+
throw createHTTPError(`INVALID_REQUEST: ${errorText}`, 400, 'invalid_request_error', response.status);
|
|
221
|
+
}
|
|
222
|
+
|
|
222
223
|
throw createUpstreamHTTPError(response, errorText);
|
|
223
224
|
}
|
|
224
225
|
|