@gajae-code/ai 0.5.0 → 0.5.2

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 CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.5.2] - 2026-06-15
6
+
7
+ ### Changed
8
+
9
+ - Changed the Anthropic provider's default prompt-cache retention to `long` (`ttl: "1h"`) when a request and model omit `cacheRetention`. The previous default (~5m) was too fragile for long-running Codex/Gajae-Code subagent workflows, where the cached prefix was frequently evicted between turns. The 1h `ttl` marker is only emitted on the canonical Anthropic API (`api.anthropic.com`) for models advertising `supportsLongCacheRetention`; proxies, gateways, and models without that capability still fall back to the default ephemeral breakpoint (Anthropic services it at ~5m). Explicit request/model `cacheRetention` and the `GJC_CACHE_RETENTION`/`PI_CACHE_RETENTION` env overrides continue to win, and `resolveCacheRetention` now accepts a `fallback` argument (defaulting to `"short"`) so non-Anthropic providers are unaffected.
10
+
11
+ ## [0.5.1] - 2026-06-14
12
+
13
+ ### Fixed
14
+
15
+ - Classified model/message limit exhaustion as persistent usage-limit errors so hosts fail fast or switch credentials instead of leaving sessions in an unbounded retry/working state.
16
+
5
17
  ## [0.5.0] - 2026-06-13
6
18
 
7
19
  ### Added
@@ -33,6 +33,7 @@ export * from "./usage/claude";
33
33
  export * from "./usage/gemini";
34
34
  export * from "./usage/github-copilot";
35
35
  export * from "./usage/google-antigravity";
36
+ export * from "./usage/grok-cli";
36
37
  export * from "./usage/kimi";
37
38
  export * from "./usage/minimax-code";
38
39
  export * from "./usage/openai-codex";
@@ -0,0 +1,10 @@
1
+ import type { CredentialRankingStrategy, UsageProvider } from "../usage";
2
+ interface BillingUsage {
3
+ monthlyLimit: number;
4
+ used: number;
5
+ billingPeriodEnd: string;
6
+ }
7
+ export declare function parseGrokCliBillingUsage(payload: unknown): BillingUsage;
8
+ export declare const grokCliUsageProvider: UsageProvider;
9
+ export declare const grokCliRankingStrategy: CredentialRankingStrategy;
10
+ export {};
@@ -8,16 +8,23 @@ interface XaiDiscovery {
8
8
  authorizationEndpoint: string;
9
9
  tokenEndpoint: string;
10
10
  }
11
+ export interface XaiOAuthFlowOptions {
12
+ extraAuthorizeParams?: Readonly<Record<string, string>>;
13
+ }
14
+ export interface XaiOAuthRefreshOptions {
15
+ signal?: AbortSignal;
16
+ extraTokenParams?: Readonly<Record<string, string>>;
17
+ }
11
18
  export declare function discoverXaiOAuthEndpoints(signal?: AbortSignal): Promise<XaiDiscovery>;
12
19
  export declare class XaiOAuthFlow extends OAuthCallbackFlow {
13
20
  #private;
14
- constructor(ctrl: OAuthController);
21
+ constructor(ctrl: OAuthController, options?: XaiOAuthFlowOptions);
15
22
  generateAuthUrl(state: string, redirectUri: string): Promise<{
16
23
  url: string;
17
24
  instructions?: string;
18
25
  }>;
19
26
  exchangeToken(code: string, _state: string, redirectUri: string): Promise<OAuthCredentials>;
20
27
  }
21
- export declare function loginXai(ctrl: OAuthController): Promise<OAuthCredentials>;
22
- export declare function refreshXaiToken(refreshToken: string, signal?: AbortSignal): Promise<OAuthCredentials>;
28
+ export declare function loginXai(ctrl: OAuthController, options?: XaiOAuthFlowOptions): Promise<OAuthCredentials>;
29
+ export declare function refreshXaiToken(refreshToken: string, options?: AbortSignal | XaiOAuthRefreshOptions): Promise<OAuthCredentials>;
23
30
  export {};
@@ -22,7 +22,13 @@ export declare function getOpenAIResponsesHistoryPayload(providerPayload: Provid
22
22
  export declare function getOpenAIResponsesHistoryItems(providerPayload: ProviderPayload | undefined, currentProvider: string, fallbackProvider?: string): Array<Record<string, unknown>> | undefined;
23
23
  /**
24
24
  * Resolve cache retention preference.
25
- * Defaults to "short" and uses GJC_CACHE_RETENTION, with PI_CACHE_RETENTION as a legacy fallback.
25
+ *
26
+ * Resolution order: explicit request value → `GJC_CACHE_RETENTION` →
27
+ * legacy `PI_CACHE_RETENTION` → `fallback`. Both env vars act as explicit
28
+ * opt-in (`"long"`) or opt-out (any other value) so a provider-specific
29
+ * `fallback` only applies when nothing else is configured. `fallback`
30
+ * defaults to `"short"` to preserve the historical behaviour for callers
31
+ * that don't pass one.
26
32
  */
27
- export declare function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention;
33
+ export declare function resolveCacheRetention(cacheRetention?: CacheRetention, fallback?: CacheRetention): CacheRetention;
28
34
  export declare function isAnthropicOAuthToken(key: string): boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@gajae-code/ai",
4
- "version": "0.5.0",
4
+ "version": "0.5.2",
5
5
  "description": "Unified LLM API with automatic model discovery and provider configuration",
6
6
  "homepage": "https://gaebal-gajae.dev",
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.5.0",
46
+ "@gajae-code/utils": "0.5.2",
47
47
  "openai": "^6.36.0",
48
48
  "partial-json": "^0.1.7",
49
49
  "zod": "4.4.3"
@@ -27,6 +27,7 @@ import { claudeRankingStrategy, claudeUsageProvider } from "./usage/claude";
27
27
  import { googleGeminiCliUsageProvider } from "./usage/gemini";
28
28
  import { githubCopilotUsageProvider } from "./usage/github-copilot";
29
29
  import { antigravityUsageProvider } from "./usage/google-antigravity";
30
+ import { grokCliRankingStrategy, grokCliUsageProvider } from "./usage/grok-cli";
30
31
  import { kimiUsageProvider } from "./usage/kimi";
31
32
  import { codexRankingStrategy, openaiCodexUsageProvider } from "./usage/openai-codex";
32
33
  import { zaiUsageProvider } from "./usage/zai";
@@ -370,6 +371,7 @@ const DEFAULT_USAGE_PROVIDERS: UsageProvider[] = [
370
371
  claudeUsageProvider,
371
372
  zaiUsageProvider,
372
373
  githubCopilotUsageProvider,
374
+ grokCliUsageProvider,
373
375
  ];
374
376
 
375
377
  const DEFAULT_USAGE_PROVIDER_MAP = new Map<Provider, UsageProvider>(
@@ -498,6 +500,7 @@ function resolveDefaultUsageProvider(provider: Provider): UsageProvider | undefi
498
500
  const DEFAULT_RANKING_STRATEGIES = new Map<Provider, CredentialRankingStrategy>([
499
501
  ["openai-codex", codexRankingStrategy],
500
502
  ["anthropic", claudeRankingStrategy],
503
+ ["grok-build", grokCliRankingStrategy],
501
504
  ]);
502
505
 
503
506
  function resolveDefaultRankingStrategy(provider: Provider): CredentialRankingStrategy | undefined {
package/src/index.ts CHANGED
@@ -33,6 +33,7 @@ export * from "./usage/claude";
33
33
  export * from "./usage/gemini";
34
34
  export * from "./usage/github-copilot";
35
35
  export * from "./usage/google-antigravity";
36
+ export * from "./usage/grok-cli";
36
37
  export * from "./usage/kimi";
37
38
  export * from "./usage/minimax-code";
38
39
  export * from "./usage/openai-codex";
package/src/models.json CHANGED
@@ -71048,6 +71048,30 @@
71048
71048
  "maxLevel": "xhigh"
71049
71049
  }
71050
71050
  },
71051
+ "glm-5.2": {
71052
+ "id": "glm-5.2",
71053
+ "name": "GLM-5.2",
71054
+ "api": "anthropic-messages",
71055
+ "provider": "zai",
71056
+ "baseUrl": "https://api.z.ai/api/anthropic",
71057
+ "reasoning": true,
71058
+ "input": [
71059
+ "text"
71060
+ ],
71061
+ "cost": {
71062
+ "input": 0,
71063
+ "output": 0,
71064
+ "cacheRead": 0,
71065
+ "cacheWrite": 0
71066
+ },
71067
+ "contextWindow": 200000,
71068
+ "maxTokens": 131072,
71069
+ "thinking": {
71070
+ "mode": "budget",
71071
+ "minLevel": "minimal",
71072
+ "maxLevel": "xhigh"
71073
+ }
71074
+ },
71051
71075
  "glm-5v-turbo": {
71052
71076
  "id": "glm-5v-turbo",
71053
71077
  "name": "GLM-5V-Turbo",
@@ -280,7 +280,7 @@ export const PROVIDER_DESCRIPTORS: readonly ProviderDescriptor[] = [
280
280
  config => zenmuxModelManagerOptions(config),
281
281
  catalog("ZenMux", ["ZENMUX_API_KEY"]),
282
282
  ),
283
- catalogDescriptor("zai", "glm-5.1", config => zaiModelManagerOptions(config), catalog("zAI", ["ZAI_API_KEY"])),
283
+ catalogDescriptor("zai", "glm-5.2", config => zaiModelManagerOptions(config), catalog("zAI", ["ZAI_API_KEY"])),
284
284
  descriptor("github-copilot", "gpt-4o", config => githubCopilotModelManagerOptions(config)),
285
285
  descriptor("google", "gemini-2.5-pro", config => googleModelManagerOptions(config)),
286
286
  catalogDescriptor(
@@ -353,10 +353,19 @@ function getCacheControl(
353
353
  baseUrl: string,
354
354
  cacheRetention?: CacheRetention,
355
355
  ): { retention: CacheRetention; cacheControl?: AnthropicCacheControl } {
356
- const retention = resolveCacheRetention(cacheRetention);
356
+ // Default Anthropic prompt caching to long (1h) retention. The provider
357
+ // default of ~5m is too fragile for long-running Codex/Gajae-Code subagent
358
+ // workflows, where the prefix is frequently evicted between turns. Explicit
359
+ // request/model `cacheRetention` and the GJC_CACHE_RETENTION /
360
+ // PI_CACHE_RETENTION env overrides still win.
361
+ const retention = resolveCacheRetention(cacheRetention, "long");
357
362
  if (retention === "none") {
358
363
  return { retention };
359
364
  }
365
+ // `ttl: "1h"` is only honoured on the canonical Anthropic API for models
366
+ // that advertise long-cache support. Everywhere else (proxies, gateways,
367
+ // models without the capability) we fall back to the default ephemeral
368
+ // breakpoint, which Anthropic services at the standard ~5m TTL.
360
369
  const ttl =
361
370
  retention === "long" && isAnthropicApiBaseUrl(baseUrl) && getAnthropicCompat(model).supportsLongCacheRetention
362
371
  ? "1h"
@@ -45,7 +45,16 @@ export function parseRateLimitReason(errorMessage: string): RateLimitReason {
45
45
  return "RATE_LIMIT_EXCEEDED";
46
46
  }
47
47
 
48
- if (lower.includes("exhausted") || lower.includes("quota") || lower.includes("usage limit")) {
48
+ if (
49
+ lower.includes("exhausted") ||
50
+ lower.includes("quota") ||
51
+ lower.includes("usage limit") ||
52
+ lower.includes("model limit") ||
53
+ lower.includes("model_limit") ||
54
+ lower.includes("message limit") ||
55
+ lower.includes("message_limit") ||
56
+ lower.includes("limit for this model")
57
+ ) {
49
58
  return "QUOTA_EXHAUSTED";
50
59
  }
51
60
 
@@ -77,7 +86,7 @@ export function calculateRateLimitBackoffMs(reason: RateLimitReason): number {
77
86
 
78
87
  /** Detect usage/quota limit errors in error messages (persistent, requires credential switch). */
79
88
  const USAGE_LIMIT_PATTERN =
80
- /usage.?limit|usage_limit_reached|usage_not_included|limit_reached|quota.?exceeded|resource.?exhausted/i;
89
+ /usage.?limit|usage_limit_reached|usage_not_included|limit_reached|model.?limit|model_limit_reached|message.?limit|message_limit_reached|limit for this model|quota.?exceeded|resource has been exhausted[^\n]*(?:quota|limit)/i;
81
90
 
82
91
  export function isUsageLimitError(errorMessage: string): boolean {
83
92
  return USAGE_LIMIT_PATTERN.test(errorMessage);
@@ -0,0 +1,163 @@
1
+ import type {
2
+ CredentialRankingStrategy,
3
+ UsageFetchContext,
4
+ UsageFetchParams,
5
+ UsageLimit,
6
+ UsageProvider,
7
+ UsageReport,
8
+ } from "../usage";
9
+
10
+ interface BillingUsage {
11
+ monthlyLimit: number;
12
+ used: number;
13
+ billingPeriodEnd: string;
14
+ }
15
+ const DEFAULT_GROK_BUILD_BASE_URL = "https://cli-chat-proxy.grok.com/v1";
16
+ const ALLOWED_GROK_BUILD_HOSTS = new Set(["cli-chat-proxy.grok.com"]);
17
+
18
+ function isRecord(value: unknown): value is Record<string, unknown> {
19
+ return !!value && typeof value === "object";
20
+ }
21
+
22
+ function finiteNumber(value: unknown): number | undefined {
23
+ return typeof value === "number" && Number.isFinite(value) ? value : undefined;
24
+ }
25
+
26
+ function parseValNumber(value: unknown): number | undefined {
27
+ return isRecord(value) ? finiteNumber(value.val) : undefined;
28
+ }
29
+
30
+ export function parseGrokCliBillingUsage(payload: unknown): BillingUsage {
31
+ if (!isRecord(payload) || !isRecord(payload.config)) {
32
+ throw new Error("invalid Grok CLI billing payload");
33
+ }
34
+ const monthlyLimit = parseValNumber(payload.config.monthlyLimit);
35
+ const used = parseValNumber(payload.config.used);
36
+ const billingPeriodEnd = payload.config.billingPeriodEnd;
37
+ if (
38
+ monthlyLimit === undefined ||
39
+ used === undefined ||
40
+ typeof billingPeriodEnd !== "string" ||
41
+ !Number.isFinite(new Date(billingPeriodEnd).getTime())
42
+ ) {
43
+ throw new Error("invalid Grok CLI billing payload");
44
+ }
45
+ return { monthlyLimit, used, billingPeriodEnd };
46
+ }
47
+
48
+ function isAllowedGrokCredentialHost(baseUrl: string): boolean {
49
+ try {
50
+ const url = new URL(baseUrl);
51
+ return url.protocol === "https:" && ALLOWED_GROK_BUILD_HOSTS.has(url.hostname.toLowerCase());
52
+ } catch {
53
+ return false;
54
+ }
55
+ }
56
+
57
+ function normalizeGrokBaseUrl(baseUrl?: string): string {
58
+ const normalized = (baseUrl?.trim() || DEFAULT_GROK_BUILD_BASE_URL).replace(/\/+$/, "");
59
+ return isAllowedGrokCredentialHost(normalized) ? normalized : DEFAULT_GROK_BUILD_BASE_URL;
60
+ }
61
+ function isUnsafeGrokBaseUrlOverride(baseUrl?: string): boolean {
62
+ const normalized = baseUrl?.trim().replace(/\/+$/, "");
63
+ return !!normalized && !isAllowedGrokCredentialHost(normalized);
64
+ }
65
+
66
+ function resolveAccessToken(params: UsageFetchParams): string | undefined {
67
+ const token = params.credential.accessToken ?? params.credential.apiKey ?? process.env.GROK_CLI_OAUTH_TOKEN;
68
+ return token?.trim() || undefined;
69
+ }
70
+
71
+ function buildMonthlyUsageLimit(usage: BillingUsage, nowMs: number): UsageLimit {
72
+ const usedFraction = usage.monthlyLimit > 0 ? usage.used / usage.monthlyLimit : 0;
73
+ const percent = usedFraction * 100;
74
+ const resetsAt = new Date(usage.billingPeriodEnd).getTime();
75
+ return {
76
+ id: "grok-build:7d",
77
+ label: "SuperGrok monthly credits",
78
+ scope: { provider: "grok-build", shared: true, windowId: "7d" },
79
+ window: {
80
+ id: "7d",
81
+ label: "Monthly credits",
82
+ resetsAt,
83
+ },
84
+ amount: {
85
+ unit: "percent",
86
+ used: percent,
87
+ limit: 100,
88
+ remaining: Math.max(0, 100 - percent),
89
+ usedFraction,
90
+ remainingFraction: Math.max(0, 1 - usedFraction),
91
+ },
92
+ status: percent >= 95 ? "exhausted" : percent >= 80 ? "warning" : "ok",
93
+ notes: [
94
+ `${usage.used}/${usage.monthlyLimit} credits used`,
95
+ `resets in ${Math.max(0, Math.round((resetsAt - nowMs) / 3_600_000))}h`,
96
+ ],
97
+ };
98
+ }
99
+
100
+ export const grokCliUsageProvider: UsageProvider = {
101
+ id: "grok-build",
102
+
103
+ supports(params) {
104
+ return params.provider === "grok-build";
105
+ },
106
+
107
+ async fetchUsage(params: UsageFetchParams, ctx: UsageFetchContext): Promise<UsageReport | null> {
108
+ const accessToken = resolveAccessToken(params);
109
+ if (!accessToken) {
110
+ ctx.logger?.warn("Grok Build usage: no access token", { provider: params.provider });
111
+ return null;
112
+ }
113
+
114
+ if (isUnsafeGrokBaseUrlOverride(params.baseUrl)) {
115
+ ctx.logger?.warn("Grok Build usage: ignoring unsafe base URL override for credential safety", {
116
+ provider: params.provider,
117
+ });
118
+ }
119
+ const billingBaseUrl = normalizeGrokBaseUrl(params.baseUrl);
120
+ const response = await ctx.fetch(`${billingBaseUrl}/billing`, {
121
+ headers: {
122
+ Authorization: `Bearer ${accessToken}`,
123
+ "x-xai-token-auth": "xai-grok-cli",
124
+ accept: "application/json",
125
+ },
126
+ signal: params.signal,
127
+ });
128
+ if (!response.ok) {
129
+ ctx.logger?.warn("Grok Build billing request failed", { status: response.status, provider: params.provider });
130
+ return null;
131
+ }
132
+
133
+ const payload = (await response.json()) as unknown;
134
+ let billing: BillingUsage;
135
+ try {
136
+ billing = parseGrokCliBillingUsage(payload);
137
+ } catch (error) {
138
+ ctx.logger?.warn("Grok Build billing parse failed", { error: String(error) });
139
+ return null;
140
+ }
141
+
142
+ const nowMs = Date.now();
143
+ return {
144
+ provider: "grok-build",
145
+ fetchedAt: nowMs,
146
+ limits: [buildMonthlyUsageLimit(billing, nowMs)],
147
+ metadata: {
148
+ email: params.credential.email,
149
+ accountId: params.credential.accountId,
150
+ subscription: true,
151
+ },
152
+ raw: payload,
153
+ };
154
+ },
155
+ };
156
+
157
+ export const grokCliRankingStrategy: CredentialRankingStrategy = {
158
+ findWindowLimits(report) {
159
+ const monthly = report.limits.find(limit => limit.id === "grok-build:7d");
160
+ return { secondary: monthly };
161
+ },
162
+ windowDefaults: { primaryMs: 5 * 60 * 60 * 1000, secondaryMs: 30 * 24 * 60 * 60 * 1000 },
163
+ };
@@ -30,6 +30,15 @@ interface XaiTokenPayload {
30
30
  token_type?: unknown;
31
31
  }
32
32
 
33
+ export interface XaiOAuthFlowOptions {
34
+ extraAuthorizeParams?: Readonly<Record<string, string>>;
35
+ }
36
+
37
+ export interface XaiOAuthRefreshOptions {
38
+ signal?: AbortSignal;
39
+ extraTokenParams?: Readonly<Record<string, string>>;
40
+ }
41
+
33
42
  interface XaiJwtPayload {
34
43
  sub?: unknown;
35
44
  email?: unknown;
@@ -41,6 +50,28 @@ function requestSignal(signal: AbortSignal | undefined): AbortSignal {
41
50
  return signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;
42
51
  }
43
52
 
53
+ function addNonOverridingParams(
54
+ target: URLSearchParams | Record<string, string>,
55
+ params: Readonly<Record<string, string>>,
56
+ ): void {
57
+ for (const [key, value] of Object.entries(params)) {
58
+ if (key.length === 0 || value.length === 0) continue;
59
+ if (target instanceof URLSearchParams) {
60
+ if (!target.has(key)) target.set(key, value);
61
+ } else if (!(key in target)) {
62
+ target[key] = value;
63
+ }
64
+ }
65
+ }
66
+
67
+ function isAbortSignal(value: AbortSignal | XaiOAuthRefreshOptions | undefined): value is AbortSignal {
68
+ return value instanceof AbortSignal;
69
+ }
70
+
71
+ function resolveRefreshOptions(options: AbortSignal | XaiOAuthRefreshOptions | undefined): XaiOAuthRefreshOptions {
72
+ return isAbortSignal(options) ? { signal: options } : (options ?? {});
73
+ }
74
+
44
75
  function validateXaiEndpoint(rawUrl: string): string {
45
76
  const parsed = new URL(rawUrl);
46
77
  const host = parsed.hostname.toLowerCase();
@@ -136,8 +167,9 @@ function credentialsFromTokenPayload(payload: XaiTokenPayload, refreshFallback =
136
167
  export class XaiOAuthFlow extends OAuthCallbackFlow {
137
168
  #verifier = "";
138
169
  #discovery: XaiDiscovery | undefined;
170
+ #extraAuthorizeParams: Readonly<Record<string, string>>;
139
171
 
140
- constructor(ctrl: OAuthController) {
172
+ constructor(ctrl: OAuthController, options: XaiOAuthFlowOptions = {}) {
141
173
  super(ctrl, {
142
174
  preferredPort: XAI_OAUTH_CALLBACK_PORT,
143
175
  callbackPath: XAI_OAUTH_CALLBACK_PATH,
@@ -145,6 +177,7 @@ export class XaiOAuthFlow extends OAuthCallbackFlow {
145
177
  callbackBindHostname: "127.0.0.1",
146
178
  redirectUri: `http://127.0.0.1:${XAI_OAUTH_CALLBACK_PORT}${XAI_OAUTH_CALLBACK_PATH}`,
147
179
  } satisfies OAuthCallbackFlowOptions);
180
+ this.#extraAuthorizeParams = options.extraAuthorizeParams ?? {};
148
181
  }
149
182
 
150
183
  async generateAuthUrl(state: string, redirectUri: string): Promise<{ url: string; instructions?: string }> {
@@ -161,6 +194,7 @@ export class XaiOAuthFlow extends OAuthCallbackFlow {
161
194
  state,
162
195
  nonce: crypto.randomUUID(),
163
196
  });
197
+ addNonOverridingParams(params, this.#extraAuthorizeParams);
164
198
  return {
165
199
  url: `${this.#discovery.authorizationEndpoint}?${params.toString()}`,
166
200
  instructions:
@@ -188,23 +222,25 @@ export class XaiOAuthFlow extends OAuthCallbackFlow {
188
222
  }
189
223
  }
190
224
 
191
- export async function loginXai(ctrl: OAuthController): Promise<OAuthCredentials> {
192
- return new XaiOAuthFlow(ctrl).login();
225
+ export async function loginXai(ctrl: OAuthController, options?: XaiOAuthFlowOptions): Promise<OAuthCredentials> {
226
+ return new XaiOAuthFlow(ctrl, options).login();
193
227
  }
194
228
 
195
- export async function refreshXaiToken(refreshToken: string, signal?: AbortSignal): Promise<OAuthCredentials> {
229
+ export async function refreshXaiToken(
230
+ refreshToken: string,
231
+ options?: AbortSignal | XaiOAuthRefreshOptions,
232
+ ): Promise<OAuthCredentials> {
196
233
  if (!refreshToken) {
197
234
  throw new Error("xAI credentials are expired and do not include a refresh token");
198
235
  }
236
+ const { signal, extraTokenParams = {} } = resolveRefreshOptions(options);
199
237
  const discovery = await discoverXaiOAuthEndpoints(signal);
200
- const tokenPayload = await postXaiToken(
201
- discovery.tokenEndpoint,
202
- {
203
- grant_type: "refresh_token",
204
- client_id: XAI_OAUTH_CLIENT_ID,
205
- refresh_token: refreshToken,
206
- },
207
- signal,
208
- );
238
+ const body = {
239
+ grant_type: "refresh_token",
240
+ client_id: XAI_OAUTH_CLIENT_ID,
241
+ refresh_token: refreshToken,
242
+ };
243
+ addNonOverridingParams(body, extraTokenParams);
244
+ const tokenPayload = await postXaiToken(discovery.tokenEndpoint, body, signal);
209
245
  return credentialsFromTokenPayload(tokenPayload, refreshToken);
210
246
  }
package/src/utils.ts CHANGED
@@ -153,14 +153,24 @@ export function getOpenAIResponsesHistoryItems(
153
153
 
154
154
  /**
155
155
  * Resolve cache retention preference.
156
- * Defaults to "short" and uses GJC_CACHE_RETENTION, with PI_CACHE_RETENTION as a legacy fallback.
156
+ *
157
+ * Resolution order: explicit request value → `GJC_CACHE_RETENTION` →
158
+ * legacy `PI_CACHE_RETENTION` → `fallback`. Both env vars act as explicit
159
+ * opt-in (`"long"`) or opt-out (any other value) so a provider-specific
160
+ * `fallback` only applies when nothing else is configured. `fallback`
161
+ * defaults to `"short"` to preserve the historical behaviour for callers
162
+ * that don't pass one.
157
163
  */
158
- export function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention {
164
+ export function resolveCacheRetention(
165
+ cacheRetention?: CacheRetention,
166
+ fallback: CacheRetention = "short",
167
+ ): CacheRetention {
159
168
  if (cacheRetention) return cacheRetention;
160
169
  if ($env.GJC_CACHE_RETENTION === "long") return "long";
161
170
  if ($env.GJC_CACHE_RETENTION !== undefined) return "short";
162
171
  if ($env.PI_CACHE_RETENTION === "long") return "long";
163
- return "short";
172
+ if ($env.PI_CACHE_RETENTION !== undefined) return "short";
173
+ return fallback;
164
174
  }
165
175
 
166
176
  export function isAnthropicOAuthToken(key: string): boolean {