@gajae-code/ai 0.8.1 → 0.9.0
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/CHANGELOG.md +12 -0
- package/dist/types/auth-broker/remote-store.d.ts +1 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +1 -0
- package/dist/types/auth-storage.d.ts +3 -1
- package/dist/types/providers/openai-bounded-rate-limits.d.ts +3 -0
- package/package.json +2 -2
- package/src/auth-broker/remote-store.ts +17 -2
- package/src/auth-broker/wire-schemas.ts +1 -0
- package/src/auth-storage.ts +74 -7
- package/src/providers/anthropic.ts +0 -2
- package/src/providers/azure-openai-responses.ts +2 -1
- package/src/providers/openai-bounded-rate-limits.ts +57 -0
- package/src/providers/openai-completions-compat.ts +26 -13
- package/src/providers/openai-completions.ts +5 -1
- package/src/providers/openai-responses.ts +5 -1
- package/src/utils.ts +21 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.9.0] - 2026-07-07
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Capped OpenCode Go Kimi reasoning efforts that the Go chat-completions endpoint rejects (`kimi-k2.5:minimal` → `low`, `kimi-k2.7-code:xhigh|max` → `high`) and degraded forced `tool_choice` for those models so Kimi Go sessions and title-generation turns no longer fail with generic upstream 400s.
|
|
10
|
+
|
|
11
|
+
## [0.8.2] - 2026-07-06
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- Refreshed matching existing OAuth credentials during `importCredentialIfAbsent` and cleared provider usage caches after the write, so external credential import no longer keeps stale tokens or stale usage-limit reports for the same account.
|
|
16
|
+
|
|
5
17
|
## [0.7.9] - 2026-07-01
|
|
6
18
|
|
|
7
19
|
### Fixed
|
|
@@ -70,6 +70,7 @@ export declare class RemoteAuthCredentialStore implements AuthCredentialStore {
|
|
|
70
70
|
getCache(key: string): string | null;
|
|
71
71
|
setCache(key: string, value: string, expiresAtSec: number): void;
|
|
72
72
|
cleanExpiredCache(): void;
|
|
73
|
+
deleteCachePrefix(prefix: string): void;
|
|
73
74
|
/**
|
|
74
75
|
* Store-level hook consumed by `AuthStorage` — routes refresh through the
|
|
75
76
|
* broker so the actual refresh token never leaves the broker host. Returns
|
|
@@ -420,6 +420,7 @@ export declare const credentialIfAbsentUploadResponseSchema: z.ZodObject<{
|
|
|
420
420
|
"skipped-existing-fallback": "skipped-existing-fallback";
|
|
421
421
|
"skipped-existing-runtime": "skipped-existing-runtime";
|
|
422
422
|
"skipped-invalid": "skipped-invalid";
|
|
423
|
+
"updated-existing": "updated-existing";
|
|
423
424
|
}>;
|
|
424
425
|
entries: z.ZodArray<z.ZodObject<{
|
|
425
426
|
id: z.ZodNumber;
|
|
@@ -102,7 +102,7 @@ export interface AuthCredentialSnapshotEntry {
|
|
|
102
102
|
credential: SnapshotCredential;
|
|
103
103
|
identityKey: string | null;
|
|
104
104
|
}
|
|
105
|
-
export type AuthCredentialIfAbsentReason = "inserted" | "skipped-existing" | "skipped-existing-runtime" | "skipped-existing-config" | "skipped-existing-env" | "skipped-existing-fallback" | "skipped-invalid";
|
|
105
|
+
export type AuthCredentialIfAbsentReason = "inserted" | "updated-existing" | "skipped-existing" | "skipped-existing-runtime" | "skipped-existing-config" | "skipped-existing-env" | "skipped-existing-fallback" | "skipped-invalid";
|
|
106
106
|
export interface AuthCredentialIfAbsentResult {
|
|
107
107
|
inserted: boolean;
|
|
108
108
|
reason: AuthCredentialIfAbsentReason;
|
|
@@ -147,6 +147,7 @@ export interface AuthCredentialStore {
|
|
|
147
147
|
includeExpired?: boolean;
|
|
148
148
|
}): string | null;
|
|
149
149
|
setCache(key: string, value: string, expiresAtSec: number): void;
|
|
150
|
+
deleteCachePrefix?(prefix: string): void;
|
|
150
151
|
cleanExpiredCache(): void;
|
|
151
152
|
/**
|
|
152
153
|
* Optional store-supplied OAuth refresh. When present, `AuthStorage` uses
|
|
@@ -646,6 +647,7 @@ export declare class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
646
647
|
includeExpired?: boolean;
|
|
647
648
|
}): string | null;
|
|
648
649
|
setCache(key: string, value: string, expiresAtSec: number): void;
|
|
650
|
+
deleteCachePrefix(prefix: string): void;
|
|
649
651
|
cleanExpiredCache(): void;
|
|
650
652
|
/**
|
|
651
653
|
* Save OAuth credentials for a provider.
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { FetchImpl } from "../types";
|
|
2
|
+
export declare function isOpenAIUsageExhaustionResponse(bodyText: string, retryAfterMs: number | undefined, retryDelayCapMs: number): boolean;
|
|
3
|
+
export declare function wrapOpenAIFetchForBoundedRateLimits(baseFetch: FetchImpl, maxRetryDelayMs: number | undefined): FetchImpl;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@gajae-code/ai",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
6
6
|
"homepage": "https://gajae-code.com",
|
|
7
7
|
"author": "Yeachan-Heo",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@anthropic-ai/sdk": "^0.94.0",
|
|
45
45
|
"@bufbuild/protobuf": "^2.12.0",
|
|
46
|
-
"@gajae-code/utils": "0.
|
|
46
|
+
"@gajae-code/utils": "0.9.0",
|
|
47
47
|
"openai": "^6.36.0",
|
|
48
48
|
"partial-json": "^0.1.7",
|
|
49
49
|
"zod": "4.4.3"
|
|
@@ -86,6 +86,7 @@ export class RemoteAuthCredentialStore implements AuthCredentialStore {
|
|
|
86
86
|
#cache: Map<string, CacheEntry> = new Map();
|
|
87
87
|
#usageCache?: UsageCacheEntry;
|
|
88
88
|
#usageInflight?: Promise<UsageReport[] | null>;
|
|
89
|
+
#usageCacheEpoch = 0;
|
|
89
90
|
#closed = false;
|
|
90
91
|
/**
|
|
91
92
|
* `true` once the SSE consumer received its first frame and hasn't dropped
|
|
@@ -465,6 +466,19 @@ export class RemoteAuthCredentialStore implements AuthCredentialStore {
|
|
|
465
466
|
}
|
|
466
467
|
}
|
|
467
468
|
|
|
469
|
+
deleteCachePrefix(prefix: string): void {
|
|
470
|
+
for (const key of this.#cache.keys()) {
|
|
471
|
+
if (key.startsWith(prefix)) this.#cache.delete(key);
|
|
472
|
+
}
|
|
473
|
+
if (prefix.startsWith("usage_cache:")) this.#invalidateUsageCache();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#invalidateUsageCache(): void {
|
|
477
|
+
this.#usageCache = undefined;
|
|
478
|
+
this.#usageInflight = undefined;
|
|
479
|
+
this.#usageCacheEpoch += 1;
|
|
480
|
+
}
|
|
481
|
+
|
|
468
482
|
/**
|
|
469
483
|
* Store-level hook consumed by `AuthStorage` — routes refresh through the
|
|
470
484
|
* broker so the actual refresh token never leaves the broker host. Returns
|
|
@@ -561,10 +575,11 @@ export class RemoteAuthCredentialStore implements AuthCredentialStore {
|
|
|
561
575
|
return Promise.resolve(cached.reports);
|
|
562
576
|
}
|
|
563
577
|
if (this.#usageInflight) return this.#usageInflight;
|
|
578
|
+
const epoch = this.#usageCacheEpoch;
|
|
564
579
|
const inflight = this.#client
|
|
565
580
|
.fetchUsage()
|
|
566
581
|
.then(body => {
|
|
567
|
-
this.#usageCache = { reports: body.reports, fetchedAt: Date.now() };
|
|
582
|
+
if (this.#usageCacheEpoch === epoch) this.#usageCache = { reports: body.reports, fetchedAt: Date.now() };
|
|
568
583
|
return body.reports;
|
|
569
584
|
})
|
|
570
585
|
.catch(error => {
|
|
@@ -572,7 +587,7 @@ export class RemoteAuthCredentialStore implements AuthCredentialStore {
|
|
|
572
587
|
return null;
|
|
573
588
|
})
|
|
574
589
|
.finally(() => {
|
|
575
|
-
this.#usageInflight = undefined;
|
|
590
|
+
if (this.#usageCacheEpoch === epoch) this.#usageInflight = undefined;
|
|
576
591
|
});
|
|
577
592
|
this.#usageInflight = inflight;
|
|
578
593
|
return inflight;
|
|
@@ -187,6 +187,7 @@ export const credentialDisableResponseSchema = z
|
|
|
187
187
|
// ─── Upload ────────────────────────────────────────────────────────────────
|
|
188
188
|
const credentialIfAbsentReasonSchema = z.enum([
|
|
189
189
|
"inserted",
|
|
190
|
+
"updated-existing",
|
|
190
191
|
"skipped-existing",
|
|
191
192
|
"skipped-existing-runtime",
|
|
192
193
|
"skipped-existing-config",
|
package/src/auth-storage.ts
CHANGED
|
@@ -154,6 +154,7 @@ export interface AuthCredentialSnapshotEntry {
|
|
|
154
154
|
|
|
155
155
|
export type AuthCredentialIfAbsentReason =
|
|
156
156
|
| "inserted"
|
|
157
|
+
| "updated-existing"
|
|
157
158
|
| "skipped-existing"
|
|
158
159
|
| "skipped-existing-runtime"
|
|
159
160
|
| "skipped-existing-config"
|
|
@@ -210,6 +211,7 @@ export interface AuthCredentialStore {
|
|
|
210
211
|
deleteAuthCredentialsForProvider(provider: string, disabledCause: string): void;
|
|
211
212
|
getCache(key: string, options?: { includeExpired?: boolean }): string | null;
|
|
212
213
|
setCache(key: string, value: string, expiresAtSec: number): void;
|
|
214
|
+
deleteCachePrefix?(prefix: string): void;
|
|
213
215
|
cleanExpiredCache(): void;
|
|
214
216
|
/**
|
|
215
217
|
* Optional store-supplied OAuth refresh. When present, `AuthStorage` uses
|
|
@@ -472,6 +474,7 @@ interface UsageCache {
|
|
|
472
474
|
get<T>(key: string): UsageCacheEntry<T> | undefined;
|
|
473
475
|
getStale<T>(key: string): UsageCacheEntry<T> | undefined;
|
|
474
476
|
set<T>(key: string, entry: UsageCacheEntry<T>): void;
|
|
477
|
+
deletePrefix?(prefix: string): void;
|
|
475
478
|
cleanup?(): void;
|
|
476
479
|
}
|
|
477
480
|
|
|
@@ -662,6 +665,10 @@ class AuthStorageUsageCache implements UsageCache {
|
|
|
662
665
|
this.store.setCache(`${USAGE_CACHE_PREFIX}${key}`, payload, Math.floor(durableExpiresAt / 1000));
|
|
663
666
|
}
|
|
664
667
|
|
|
668
|
+
deletePrefix(prefix: string): void {
|
|
669
|
+
this.store.deleteCachePrefix?.(`${USAGE_CACHE_PREFIX}${prefix}`);
|
|
670
|
+
}
|
|
671
|
+
|
|
665
672
|
cleanup(): void {
|
|
666
673
|
this.store.cleanExpiredCache();
|
|
667
674
|
}
|
|
@@ -1297,8 +1304,6 @@ export class AuthStorage {
|
|
|
1297
1304
|
return this.#snapshotSkipResult(storageProvider, "skipped-existing-runtime");
|
|
1298
1305
|
if (this.#configOverrides.has(storageProvider))
|
|
1299
1306
|
return this.#snapshotSkipResult(storageProvider, "skipped-existing-config");
|
|
1300
|
-
if (this.#getCredentialsForProvider(storageProvider).length > 0)
|
|
1301
|
-
return this.#snapshotSkipResult(storageProvider, "skipped-existing");
|
|
1302
1307
|
if (getEnvApiKey(storageProvider)) return this.#snapshotSkipResult(storageProvider, "skipped-existing-env");
|
|
1303
1308
|
if (this.#fallbackResolver?.(storageProvider))
|
|
1304
1309
|
return this.#snapshotSkipResult(storageProvider, "skipped-existing-fallback");
|
|
@@ -1311,6 +1316,7 @@ export class AuthStorage {
|
|
|
1311
1316
|
result.entries.map(entry => ({ id: entry.id, credential: entry.credential })),
|
|
1312
1317
|
);
|
|
1313
1318
|
this.#resetProviderAssignments(storageProvider);
|
|
1319
|
+
if (result.inserted) this.#invalidateUsageCacheForProvider(storageProvider);
|
|
1314
1320
|
return {
|
|
1315
1321
|
inserted: result.inserted,
|
|
1316
1322
|
reason: result.reason,
|
|
@@ -1328,6 +1334,13 @@ export class AuthStorage {
|
|
|
1328
1334
|
stored.map(record => ({ id: record.id, credential: record.credential })),
|
|
1329
1335
|
);
|
|
1330
1336
|
this.#resetProviderAssignments(provider);
|
|
1337
|
+
this.#invalidateUsageCacheForProvider(provider);
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
#invalidateUsageCacheForProvider(provider: string): void {
|
|
1341
|
+
this.#usageRequestInFlight.clear();
|
|
1342
|
+
this.#usageReportsInFlight.clear();
|
|
1343
|
+
this.#usageCache.deletePrefix?.(`report:${provider}:`);
|
|
1331
1344
|
}
|
|
1332
1345
|
|
|
1333
1346
|
/**
|
|
@@ -3705,6 +3718,7 @@ export class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
3705
3718
|
#getCacheStmt: Statement;
|
|
3706
3719
|
#getCacheIncludingExpiredStmt: Statement;
|
|
3707
3720
|
#upsertCacheStmt: Statement;
|
|
3721
|
+
#deleteCachePrefixStmt: Statement;
|
|
3708
3722
|
#deleteExpiredCacheStmt: Statement;
|
|
3709
3723
|
#closed = false;
|
|
3710
3724
|
|
|
@@ -3744,6 +3758,7 @@ export class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
3744
3758
|
this.#upsertCacheStmt = this.#db.prepare(
|
|
3745
3759
|
"INSERT INTO cache (key, value, expires_at) VALUES (?, ?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value, expires_at = excluded.expires_at",
|
|
3746
3760
|
);
|
|
3761
|
+
this.#deleteCachePrefixStmt = this.#db.prepare("DELETE FROM cache WHERE substr(key, 1, ?) = ?");
|
|
3747
3762
|
this.#deleteExpiredCacheStmt = this.#db.prepare(`DELETE FROM cache WHERE expires_at <= ${SQLITE_NOW_EPOCH}`);
|
|
3748
3763
|
}
|
|
3749
3764
|
|
|
@@ -4102,16 +4117,58 @@ export class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
4102
4117
|
}
|
|
4103
4118
|
|
|
4104
4119
|
const writeIfAbsent = this.#db.transaction(
|
|
4105
|
-
(
|
|
4120
|
+
(
|
|
4121
|
+
providerName: string,
|
|
4122
|
+
item: AuthCredential,
|
|
4123
|
+
record: SerializedCredentialRecord,
|
|
4124
|
+
): AuthCredentialIfAbsentResult => {
|
|
4106
4125
|
const existingRows = this.#listActiveByProviderStmt.all(providerName) as AuthRow[];
|
|
4107
|
-
const existing:
|
|
4126
|
+
const existing: Array<{
|
|
4127
|
+
id: number;
|
|
4128
|
+
credential: AuthCredential;
|
|
4129
|
+
identityKey: string | null;
|
|
4130
|
+
}> = [];
|
|
4108
4131
|
for (const row of existingRows) {
|
|
4109
4132
|
const activeCredential = deserializeCredential(row);
|
|
4110
4133
|
if (!activeCredential) continue;
|
|
4111
|
-
existing.push(
|
|
4134
|
+
existing.push({
|
|
4135
|
+
id: row.id,
|
|
4136
|
+
credential: activeCredential,
|
|
4137
|
+
identityKey: resolveRowCredentialIdentityKey(providerName, row),
|
|
4138
|
+
});
|
|
4112
4139
|
}
|
|
4113
4140
|
if (existing.length > 0) {
|
|
4114
|
-
|
|
4141
|
+
let targetId: number | null = null;
|
|
4142
|
+
for (const row of existing) {
|
|
4143
|
+
if (!matchesReplacementCredential(providerName, row.credential, row.identityKey, item)) continue;
|
|
4144
|
+
if (targetId === null) {
|
|
4145
|
+
targetId = row.id;
|
|
4146
|
+
this.#updateStmt.run(record.credentialType, record.data, record.identityKey, row.id);
|
|
4147
|
+
} else {
|
|
4148
|
+
this.#deleteStmt.run("replaced by newer credential", row.id);
|
|
4149
|
+
}
|
|
4150
|
+
}
|
|
4151
|
+
|
|
4152
|
+
if (targetId !== null) {
|
|
4153
|
+
return {
|
|
4154
|
+
inserted: true,
|
|
4155
|
+
reason: "updated-existing",
|
|
4156
|
+
provider: providerName,
|
|
4157
|
+
entries: this.listAuthCredentials(providerName),
|
|
4158
|
+
};
|
|
4159
|
+
}
|
|
4160
|
+
|
|
4161
|
+
return {
|
|
4162
|
+
inserted: false,
|
|
4163
|
+
reason: "skipped-existing",
|
|
4164
|
+
provider: providerName,
|
|
4165
|
+
entries: existing.map(row => ({
|
|
4166
|
+
id: row.id,
|
|
4167
|
+
provider: providerName,
|
|
4168
|
+
credential: row.credential,
|
|
4169
|
+
disabledCause: null,
|
|
4170
|
+
})),
|
|
4171
|
+
};
|
|
4115
4172
|
}
|
|
4116
4173
|
|
|
4117
4174
|
this.#insertStmt.get(providerName, record.credentialType, record.data, record.identityKey);
|
|
@@ -4124,7 +4181,9 @@ export class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
4124
4181
|
},
|
|
4125
4182
|
);
|
|
4126
4183
|
|
|
4127
|
-
|
|
4184
|
+
const result = writeIfAbsent.immediate(provider, credential, serialized);
|
|
4185
|
+
if (result.inserted) this.#purgeSupersededDisabledRows(provider, result.entries);
|
|
4186
|
+
return result;
|
|
4128
4187
|
}
|
|
4129
4188
|
|
|
4130
4189
|
/**
|
|
@@ -4221,6 +4280,13 @@ export class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
4221
4280
|
}
|
|
4222
4281
|
}
|
|
4223
4282
|
|
|
4283
|
+
deleteCachePrefix(prefix: string): void {
|
|
4284
|
+
if (prefix.length === 0) return;
|
|
4285
|
+
try {
|
|
4286
|
+
this.#deleteCachePrefixStmt.run(prefix.length, prefix);
|
|
4287
|
+
} catch {}
|
|
4288
|
+
}
|
|
4289
|
+
|
|
4224
4290
|
cleanExpiredCache(): void {
|
|
4225
4291
|
try {
|
|
4226
4292
|
this.#deleteExpiredCacheStmt.run();
|
|
@@ -4311,6 +4377,7 @@ export class SqliteAuthCredentialStore implements AuthCredentialStore {
|
|
|
4311
4377
|
this.#getCacheStmt.finalize();
|
|
4312
4378
|
this.#getCacheIncludingExpiredStmt.finalize();
|
|
4313
4379
|
this.#upsertCacheStmt.finalize();
|
|
4380
|
+
this.#deleteCachePrefixStmt.finalize();
|
|
4314
4381
|
this.#deleteExpiredCacheStmt.finalize();
|
|
4315
4382
|
this.#db.close();
|
|
4316
4383
|
}
|
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
markToolChoiceIncapability,
|
|
36
36
|
resolveToolChoice,
|
|
37
37
|
} from "../utils/tool-choice-capability";
|
|
38
|
+
import { wrapOpenAIFetchForBoundedRateLimits } from "./openai-bounded-rate-limits";
|
|
38
39
|
import { normalizeOpenAIResponsesPromptCacheKey, supportsDeveloperRole } from "./openai-responses";
|
|
39
40
|
import {
|
|
40
41
|
appendResponsesToolResultMessages,
|
|
@@ -272,7 +273,7 @@ function createClient(model: Model<"azure-openai-responses">, apiKey: string, op
|
|
|
272
273
|
|
|
273
274
|
const { baseUrl, apiVersion } = resolveAzureConfig(model, options);
|
|
274
275
|
|
|
275
|
-
const baseFetch = options?.fetch ?? fetch;
|
|
276
|
+
const baseFetch = wrapOpenAIFetchForBoundedRateLimits(options?.fetch ?? fetch, options?.maxRetryDelayMs);
|
|
276
277
|
const onSseEvent = options?.onSseEvent;
|
|
277
278
|
return new AzureOpenAI({
|
|
278
279
|
apiKey,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { FetchImpl } from "../types";
|
|
2
|
+
import { getRetryAfterMsFromHeaders } from "../utils/retry-after";
|
|
3
|
+
|
|
4
|
+
const OPENAI_RETRY_DELAY_CAP_MS = 60_000;
|
|
5
|
+
|
|
6
|
+
// Mirror of `wrapAnthropicFetchForBoundedRateLimits`: OpenAI-compatible providers
|
|
7
|
+
// (e.g. opencode-go) return HTTP 429 for *permanent* usage/quota exhaustion — a
|
|
8
|
+
// monthly-cap reset that can be days away. The OpenAI SDK treats 429 as transient
|
|
9
|
+
// and retries up to `maxRetries`, honoring an out-of-range `Retry-After`; the
|
|
10
|
+
// `create()` call then hangs before the error can surface to the agent loop, so
|
|
11
|
+
// no assistant error is produced and the session-level retry/fallback never runs.
|
|
12
|
+
// Detect exhaustion and set `x-should-retry: false` so the SDK gives up at once
|
|
13
|
+
// and the session retry layer applies its own fail-fast (retry-after > maxDelayMs).
|
|
14
|
+
//
|
|
15
|
+
// Shared by every adapter that drives a raw OpenAI SDK client — openai-completions,
|
|
16
|
+
// openai-responses, and azure-openai-responses. Adapters that route through
|
|
17
|
+
// `fetchWithRetry` (codex, bedrock, ollama, gemini-cli) already bound 429 retries
|
|
18
|
+
// themselves and do not need this wrapper.
|
|
19
|
+
export function isOpenAIUsageExhaustionResponse(
|
|
20
|
+
bodyText: string,
|
|
21
|
+
retryAfterMs: number | undefined,
|
|
22
|
+
retryDelayCapMs: number,
|
|
23
|
+
): boolean {
|
|
24
|
+
if (retryAfterMs !== undefined && retryAfterMs > retryDelayCapMs) return true;
|
|
25
|
+
return /monthly usage limit|usage limit reached|usage_limit_reached|out_of_credits|insufficient_quota|quota[ _]?exceeded/i.test(
|
|
26
|
+
bodyText,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function wrapOpenAIFetchForBoundedRateLimits(
|
|
31
|
+
baseFetch: FetchImpl,
|
|
32
|
+
maxRetryDelayMs: number | undefined,
|
|
33
|
+
): FetchImpl {
|
|
34
|
+
const retryDelayCapMs = maxRetryDelayMs ?? OPENAI_RETRY_DELAY_CAP_MS;
|
|
35
|
+
return Object.assign(
|
|
36
|
+
async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
|
|
37
|
+
const response = await baseFetch(input, init);
|
|
38
|
+
if (response.status !== 429 || retryDelayCapMs === 0) return response;
|
|
39
|
+
|
|
40
|
+
const headers = new Headers(response.headers);
|
|
41
|
+
const retryAfterMs = getRetryAfterMsFromHeaders(headers);
|
|
42
|
+
const bodyText = await response
|
|
43
|
+
.clone()
|
|
44
|
+
.text()
|
|
45
|
+
.catch(() => "");
|
|
46
|
+
if (!isOpenAIUsageExhaustionResponse(bodyText, retryAfterMs, retryDelayCapMs)) return response;
|
|
47
|
+
|
|
48
|
+
headers.set("x-should-retry", "false");
|
|
49
|
+
return new Response(bodyText, {
|
|
50
|
+
status: response.status,
|
|
51
|
+
statusText: response.statusText,
|
|
52
|
+
headers,
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
baseFetch.preconnect ? { preconnect: baseFetch.preconnect } : {},
|
|
56
|
+
);
|
|
57
|
+
}
|
|
@@ -104,6 +104,10 @@ export function detectOpenAICompat(model: Model<"openai-completions">, resolvedB
|
|
|
104
104
|
baseUrl.includes("opencode.ai");
|
|
105
105
|
const isOpenCodeProvider = provider === "opencode-go" || provider === "opencode-zen";
|
|
106
106
|
const isOpenCodeGoReasoning = provider === "opencode-go" && Boolean(model.reasoning);
|
|
107
|
+
const isOpenCodeGoKimiReasoning = provider === "opencode-go" && isKimiModel && Boolean(model.reasoning);
|
|
108
|
+
const isOpenCodeGoKimi25Reasoning = isOpenCodeGoKimiReasoning && model.id === "kimi-k2.5";
|
|
109
|
+
const isOpenCodeGoKimi27CodeReasoning = isOpenCodeGoKimiReasoning && model.id === "kimi-k2.7-code";
|
|
110
|
+
const needsOpenCodeGoKimiEffortMap = isOpenCodeGoKimi25Reasoning || isOpenCodeGoKimi27CodeReasoning;
|
|
107
111
|
|
|
108
112
|
const useMaxTokens =
|
|
109
113
|
provider === "mistral" ||
|
|
@@ -170,22 +174,31 @@ export function detectOpenAICompat(model: Model<"openai-completions">, resolvedB
|
|
|
170
174
|
xhigh: "default",
|
|
171
175
|
max: "default",
|
|
172
176
|
} satisfies Partial<Record<OpenAIReasoningEffort, string>>)
|
|
173
|
-
:
|
|
177
|
+
: needsOpenCodeGoKimiEffortMap
|
|
174
178
|
? ({
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
179
|
+
// Live Go probes (2026-07-06) showed model-specific effort gaps:
|
|
180
|
+
// kimi-k2.5 rejects "minimal", while kimi-k2.7-code rejects
|
|
181
|
+
// OpenAI-style "xhigh" and "max"; all other Kimi efforts tested
|
|
182
|
+
// successfully and should pass through unchanged.
|
|
183
|
+
...(isOpenCodeGoKimi25Reasoning ? { minimal: "low" } : {}),
|
|
184
|
+
...(isOpenCodeGoKimi27CodeReasoning ? { xhigh: "high", max: "high" } : {}),
|
|
181
185
|
} satisfies Partial<Record<OpenAIReasoningEffort, string>>)
|
|
182
|
-
:
|
|
186
|
+
: isDeepseekFamily && model.reasoning
|
|
183
187
|
? ({
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
188
|
+
minimal: "high",
|
|
189
|
+
low: "high",
|
|
190
|
+
medium: "high",
|
|
191
|
+
high: "high",
|
|
192
|
+
xhigh: "max",
|
|
193
|
+
max: "max",
|
|
187
194
|
} satisfies Partial<Record<OpenAIReasoningEffort, string>>)
|
|
188
|
-
:
|
|
195
|
+
: isFireworks
|
|
196
|
+
? ({
|
|
197
|
+
// Fireworks' OpenAI-compatible endpoint rejects OpenAI's
|
|
198
|
+
// `minimal` literal but accepts `none` for the lowest setting.
|
|
199
|
+
minimal: "none",
|
|
200
|
+
} satisfies Partial<Record<OpenAIReasoningEffort, string>>)
|
|
201
|
+
: {};
|
|
189
202
|
|
|
190
203
|
return {
|
|
191
204
|
supportsStore: !isNonStandard,
|
|
@@ -198,7 +211,7 @@ export function detectOpenAICompat(model: Model<"openai-completions">, resolvedB
|
|
|
198
211
|
disableReasoningOnForcedToolChoice: isKimiModel || isAnthropicModel || isOpenCodeGoReasoning,
|
|
199
212
|
disableReasoningOnToolChoice: isDeepseekFamily && Boolean(model.reasoning) && !isOpenRouter,
|
|
200
213
|
supportsToolChoice: !isDirectDeepseekReasoning,
|
|
201
|
-
supportsForcedToolChoice:
|
|
214
|
+
supportsForcedToolChoice: !isOpenCodeGoKimiReasoning,
|
|
202
215
|
maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
|
|
203
216
|
requiresToolResultName: isMistral,
|
|
204
217
|
requiresAssistantAfterToolResult: false,
|
|
@@ -72,6 +72,7 @@ import {
|
|
|
72
72
|
hasCopilotVisionInput,
|
|
73
73
|
resolveGitHubCopilotBaseUrl,
|
|
74
74
|
} from "./github-copilot-headers";
|
|
75
|
+
import { wrapOpenAIFetchForBoundedRateLimits } from "./openai-bounded-rate-limits";
|
|
75
76
|
import { detectOpenAICompat, type ResolvedOpenAICompat, resolveOpenAICompat } from "./openai-completions-compat";
|
|
76
77
|
import {
|
|
77
78
|
applyOpenAIRequestTransformBody,
|
|
@@ -454,6 +455,7 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (
|
|
|
454
455
|
options?.authCredentialType,
|
|
455
456
|
options?.requestMaxRetries,
|
|
456
457
|
options?.sessionId,
|
|
458
|
+
options?.maxRetryDelayMs,
|
|
457
459
|
);
|
|
458
460
|
const premiumRequestsTotal = copilotPremiumRequests;
|
|
459
461
|
getCapturedErrorResponse = captureErrorResponse;
|
|
@@ -956,6 +958,7 @@ async function createClient(
|
|
|
956
958
|
authCredentialType?: OpenAICompletionsOptions["authCredentialType"],
|
|
957
959
|
requestMaxRetries?: number,
|
|
958
960
|
sessionId?: string,
|
|
961
|
+
maxRetryDelayMs?: number,
|
|
959
962
|
): Promise<{
|
|
960
963
|
client: OpenAI;
|
|
961
964
|
copilotPremiumRequests: number | undefined;
|
|
@@ -1063,8 +1066,9 @@ async function createClient(
|
|
|
1063
1066
|
},
|
|
1064
1067
|
baseFetch.preconnect ? { preconnect: baseFetch.preconnect } : {},
|
|
1065
1068
|
);
|
|
1069
|
+
const boundedFetch = wrapOpenAIFetchForBoundedRateLimits(wrappedFetch, maxRetryDelayMs);
|
|
1066
1070
|
const transformedFetch = wrapFetchForOpenAIRequestTransform(
|
|
1067
|
-
|
|
1071
|
+
boundedFetch,
|
|
1068
1072
|
model.requestTransform,
|
|
1069
1073
|
`Gajae-Code/${packageJson.version}`,
|
|
1070
1074
|
);
|
|
@@ -71,6 +71,7 @@ import {
|
|
|
71
71
|
resolveGitHubCopilotBaseUrl,
|
|
72
72
|
} from "./github-copilot-headers";
|
|
73
73
|
import { compactGrammarDefinition } from "./grammar";
|
|
74
|
+
import { wrapOpenAIFetchForBoundedRateLimits } from "./openai-bounded-rate-limits";
|
|
74
75
|
import {
|
|
75
76
|
applyOpenAIRequestTransformBody,
|
|
76
77
|
applyOpenAIRequestTransformHeaders,
|
|
@@ -274,6 +275,7 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses"> = (
|
|
|
274
275
|
options?.fetch,
|
|
275
276
|
options?.authCredentialType,
|
|
276
277
|
options?.requestMaxRetries,
|
|
278
|
+
options?.maxRetryDelayMs,
|
|
277
279
|
);
|
|
278
280
|
const premiumRequestsTotal = copilotPremiumRequests;
|
|
279
281
|
const providerSessionState = getOpenAIResponsesProviderSessionState(model, options?.providerSessionState);
|
|
@@ -400,6 +402,7 @@ function createClient(
|
|
|
400
402
|
fetchOverride?: FetchImpl,
|
|
401
403
|
authCredentialType?: OpenAIResponsesOptions["authCredentialType"],
|
|
402
404
|
requestMaxRetries?: number,
|
|
405
|
+
maxRetryDelayMs?: number,
|
|
403
406
|
): {
|
|
404
407
|
client: OpenAI;
|
|
405
408
|
copilotPremiumRequests: number | undefined;
|
|
@@ -446,8 +449,9 @@ function createClient(
|
|
|
446
449
|
headers["x-client-request-id"] ??= sessionId;
|
|
447
450
|
}
|
|
448
451
|
const baseFetch = fetchOverride ?? fetch;
|
|
452
|
+
const boundedFetch = wrapOpenAIFetchForBoundedRateLimits(baseFetch, maxRetryDelayMs);
|
|
449
453
|
const transformedFetch = wrapFetchForOpenAIRequestTransform(
|
|
450
|
-
|
|
454
|
+
boundedFetch,
|
|
451
455
|
model.requestTransform,
|
|
452
456
|
`Gajae-Code/${packageJson.version}`,
|
|
453
457
|
);
|
package/src/utils.ts
CHANGED
|
@@ -215,7 +215,11 @@ function sanitizeOpenAIResponsesHistoryItemForReplay(
|
|
|
215
215
|
// providerPayload stores raw output items; replay strips fields that are output-only.
|
|
216
216
|
const { id: _id, ...itemWithoutId } = item;
|
|
217
217
|
const sanitizedItem =
|
|
218
|
-
item.type === "computer_call"
|
|
218
|
+
item.type === "computer_call"
|
|
219
|
+
? sanitizeComputerCallForResponsesInput(itemWithoutId)
|
|
220
|
+
: item.type === "image_generation_call"
|
|
221
|
+
? sanitizeImageGenerationCallForResponsesInput(itemWithoutId)
|
|
222
|
+
: itemWithoutId;
|
|
219
223
|
if (typeof item.call_id === "string") {
|
|
220
224
|
sanitizedItem.call_id = normalizeReplayedResponsesHistoryCallId(item.call_id, normalizedCallIds);
|
|
221
225
|
}
|
|
@@ -231,6 +235,22 @@ function sanitizeComputerCallForResponsesInput(item: Record<string, unknown>): R
|
|
|
231
235
|
return inputSafeItem;
|
|
232
236
|
}
|
|
233
237
|
|
|
238
|
+
function sanitizeImageGenerationCallForResponsesInput(item: Record<string, unknown>): Record<string, unknown> {
|
|
239
|
+
// Image generation output items include request-time knobs that are not part of
|
|
240
|
+
// the Responses input replay schema. Replaying them verbatim makes OpenAI-compatible
|
|
241
|
+
// endpoints reject the next turn, e.g. `Unknown parameter: input[n].action`.
|
|
242
|
+
const {
|
|
243
|
+
action: _action,
|
|
244
|
+
background: _background,
|
|
245
|
+
output_format: _outputFormat,
|
|
246
|
+
quality: _quality,
|
|
247
|
+
revised_prompt: _revisedPrompt,
|
|
248
|
+
size: _size,
|
|
249
|
+
...inputSafeItem
|
|
250
|
+
} = item;
|
|
251
|
+
return inputSafeItem;
|
|
252
|
+
}
|
|
253
|
+
|
|
234
254
|
function normalizeReplayedResponsesHistoryCallId(value: string, normalizedValues: Map<string, string>): string {
|
|
235
255
|
const normalized = normalizedValues.get(value);
|
|
236
256
|
if (normalized) return normalized;
|