dsh-coding-subscription-oauth 0.5.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 +136 -0
- package/CONTRIBUTING.md +109 -0
- package/INSTALL.md +220 -0
- package/LICENSE +19 -0
- package/NOTICE +11 -0
- package/README.de.md +254 -0
- package/README.es.md +254 -0
- package/README.fr.md +254 -0
- package/README.ja.md +254 -0
- package/README.ko.md +254 -0
- package/README.md +286 -0
- package/README.pt-BR.md +254 -0
- package/README.ru.md +254 -0
- package/README.zh-CN.md +271 -0
- package/cordis.patch.yml +13 -0
- package/docs/00-project-rules.md +195 -0
- package/docs/02-architecture.md +132 -0
- package/docs/02-architecture.zh-CN.md +128 -0
- package/lib/adapter.d.ts +24 -0
- package/lib/adapter.d.ts.map +1 -0
- package/lib/alias-adapter.d.ts +43 -0
- package/lib/alias-adapter.d.ts.map +1 -0
- package/lib/auth-routes.d.ts +155 -0
- package/lib/auth-routes.d.ts.map +1 -0
- package/lib/auth.d.ts +29 -0
- package/lib/auth.d.ts.map +1 -0
- package/lib/bin.d.ts +6 -0
- package/lib/bin.d.ts.map +1 -0
- package/lib/bin.js +27591 -0
- package/lib/bin.js.map +7 -0
- package/lib/capability-routes.d.ts +43 -0
- package/lib/capability-routes.d.ts.map +1 -0
- package/lib/capability-runtime.d.ts +56 -0
- package/lib/capability-runtime.d.ts.map +1 -0
- package/lib/capability-settings.d.ts +263 -0
- package/lib/capability-settings.d.ts.map +1 -0
- package/lib/capability-tools.d.ts +50 -0
- package/lib/capability-tools.d.ts.map +1 -0
- package/lib/catalog.d.ts +53 -0
- package/lib/catalog.d.ts.map +1 -0
- package/lib/client.js +3 -0
- package/lib/client.js.map +7 -0
- package/lib/codex-http.d.ts +77 -0
- package/lib/codex-http.d.ts.map +1 -0
- package/lib/codex-images.d.ts +109 -0
- package/lib/codex-images.d.ts.map +1 -0
- package/lib/codex-model-capabilities.d.ts +112 -0
- package/lib/codex-model-capabilities.d.ts.map +1 -0
- package/lib/codex-search.d.ts +96 -0
- package/lib/codex-search.d.ts.map +1 -0
- package/lib/codex-usage.d.ts +79 -0
- package/lib/codex-usage.d.ts.map +1 -0
- package/lib/gateway-anthropic-messages.d.ts +8 -0
- package/lib/gateway-anthropic-messages.d.ts.map +1 -0
- package/lib/gateway-auth.d.ts +22 -0
- package/lib/gateway-auth.d.ts.map +1 -0
- package/lib/gateway-backend.d.ts +62 -0
- package/lib/gateway-backend.d.ts.map +1 -0
- package/lib/gateway-body.d.ts +9 -0
- package/lib/gateway-body.d.ts.map +1 -0
- package/lib/gateway-config.d.ts +24 -0
- package/lib/gateway-config.d.ts.map +1 -0
- package/lib/gateway-http.d.ts +16 -0
- package/lib/gateway-http.d.ts.map +1 -0
- package/lib/gateway-openai-chat.d.ts +8 -0
- package/lib/gateway-openai-chat.d.ts.map +1 -0
- package/lib/gateway-openai-responses.d.ts +8 -0
- package/lib/gateway-openai-responses.d.ts.map +1 -0
- package/lib/gateway-parse.d.ts +10 -0
- package/lib/gateway-parse.d.ts.map +1 -0
- package/lib/gateway-protocol.d.ts +47 -0
- package/lib/gateway-protocol.d.ts.map +1 -0
- package/lib/gateway-routes.d.ts +21 -0
- package/lib/gateway-routes.d.ts.map +1 -0
- package/lib/gateway.d.ts +48 -0
- package/lib/gateway.d.ts.map +1 -0
- package/lib/grok-imagine.d.ts +271 -0
- package/lib/grok-imagine.d.ts.map +1 -0
- package/lib/grok-import.d.ts +21 -0
- package/lib/grok-import.d.ts.map +1 -0
- package/lib/http-json.d.ts +10 -0
- package/lib/http-json.d.ts.map +1 -0
- package/lib/ids.d.ts +33 -0
- package/lib/ids.d.ts.map +1 -0
- package/lib/imagine-routes.d.ts +59 -0
- package/lib/imagine-routes.d.ts.map +1 -0
- package/lib/index.d.ts +69 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +35355 -0
- package/lib/index.js.map +7 -0
- package/lib/invariant.d.ts +9 -0
- package/lib/invariant.d.ts.map +1 -0
- package/lib/invariant.js +14 -0
- package/lib/invariant.js.map +7 -0
- package/lib/kimi-errors.d.ts +13 -0
- package/lib/kimi-errors.d.ts.map +1 -0
- package/lib/media-store.d.ts +130 -0
- package/lib/media-store.d.ts.map +1 -0
- package/lib/oauth-import-routes.d.ts +52 -0
- package/lib/oauth-import-routes.d.ts.map +1 -0
- package/lib/oauth-providers.d.ts +26 -0
- package/lib/oauth-providers.d.ts.map +1 -0
- package/lib/oauth-session.d.ts +40 -0
- package/lib/oauth-session.d.ts.map +1 -0
- package/lib/oauth-sources.d.ts +205 -0
- package/lib/oauth-sources.d.ts.map +1 -0
- package/lib/oauth.d.ts +79 -0
- package/lib/oauth.d.ts.map +1 -0
- package/lib/provider.d.ts +38 -0
- package/lib/provider.d.ts.map +1 -0
- package/lib/proxy.d.ts +17 -0
- package/lib/proxy.d.ts.map +1 -0
- package/lib/redact.d.ts +5 -0
- package/lib/redact.d.ts.map +1 -0
- package/lib/session.d.ts +40 -0
- package/lib/session.d.ts.map +1 -0
- package/lib/store.d.ts +46 -0
- package/lib/store.d.ts.map +1 -0
- package/lib/web-origin.d.ts +10 -0
- package/lib/web-origin.d.ts.map +1 -0
- package/lib/web-routes.d.ts +20 -0
- package/lib/web-routes.d.ts.map +1 -0
- package/package.json +185 -0
- package/patches/dsh-agy@0.1.2.patch +25 -0
- package/scripts/release.mjs +166 -0
- package/scripts/smoke-deployed-routes.mjs +146 -0
- package/scripts/verify-deployed-catalog.mjs +87 -0
- package/src/adapter.ts +282 -0
- package/src/alias-adapter.ts +152 -0
- package/src/auth-routes.ts +871 -0
- package/src/auth.ts +67 -0
- package/src/bin.ts +350 -0
- package/src/capability-routes.ts +275 -0
- package/src/capability-runtime.ts +313 -0
- package/src/capability-settings.ts +657 -0
- package/src/capability-tools.ts +666 -0
- package/src/catalog.ts +271 -0
- package/src/client/GrokBuildSettings.tsx +2221 -0
- package/src/client/index.tsx +37 -0
- package/src/client/locales.ts +421 -0
- package/src/codex-http.ts +447 -0
- package/src/codex-images.ts +485 -0
- package/src/codex-model-capabilities.ts +320 -0
- package/src/codex-search.ts +245 -0
- package/src/codex-usage.ts +263 -0
- package/src/gateway-anthropic-messages.ts +84 -0
- package/src/gateway-auth.ts +100 -0
- package/src/gateway-backend.ts +274 -0
- package/src/gateway-body.ts +49 -0
- package/src/gateway-config.ts +76 -0
- package/src/gateway-http.ts +104 -0
- package/src/gateway-openai-chat.ts +124 -0
- package/src/gateway-openai-responses.ts +53 -0
- package/src/gateway-parse.ts +224 -0
- package/src/gateway-protocol.ts +52 -0
- package/src/gateway-routes.ts +152 -0
- package/src/gateway.ts +242 -0
- package/src/grok-imagine.ts +1627 -0
- package/src/grok-import.ts +151 -0
- package/src/http-json.ts +82 -0
- package/src/ids.ts +45 -0
- package/src/imagine-routes.ts +461 -0
- package/src/index.ts +598 -0
- package/src/invariant.ts +17 -0
- package/src/kimi-errors.ts +26 -0
- package/src/media-store.ts +927 -0
- package/src/oauth-import-routes.ts +314 -0
- package/src/oauth-providers.ts +152 -0
- package/src/oauth-session.ts +183 -0
- package/src/oauth-sources.ts +1104 -0
- package/src/oauth.ts +620 -0
- package/src/provider.ts +128 -0
- package/src/proxy.ts +99 -0
- package/src/redact.ts +72 -0
- package/src/session.ts +218 -0
- package/src/store.ts +217 -0
- package/src/web-origin.ts +60 -0
- package/src/web-routes.ts +75 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional xAI Grok Build bundle with OAuth, account model catalog,
|
|
3
|
+
* and an account section inside dsh Settings.
|
|
4
|
+
* @module dsh-coding-subscription-oauth
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
9
|
+
import type { AttachmentStore } from "@deepseek-ai/dsh-attachment";
|
|
10
|
+
import type { CredentialInfo, CredentialProvider, CredentialRef } from "@deepseek-ai/dsh-credentials";
|
|
11
|
+
import type {} from "@deepseek-ai/dsh-host-webserver";
|
|
12
|
+
import { assertUsableApiKey, type RetryPolicyConfig, RetryPolicySchema } from "@deepseek-ai/dsh-llm";
|
|
13
|
+
import z from "@deepseek-ai/schemastery";
|
|
14
|
+
import type { Credential, OAuthCredential } from "@earendil-works/pi-ai";
|
|
15
|
+
import { createCodingOAuthAdapter } from "./adapter.ts";
|
|
16
|
+
import { registerCodingOAuthRoutes } from "./auth-routes.ts";
|
|
17
|
+
import { registerCapabilityRoutes } from "./capability-routes.ts";
|
|
18
|
+
import {
|
|
19
|
+
bindCapabilitySearch,
|
|
20
|
+
bindCapabilityTools,
|
|
21
|
+
bindCodexFastRoute,
|
|
22
|
+
CapabilityRuntimeState,
|
|
23
|
+
type CapabilitySearchRegistry,
|
|
24
|
+
type CapabilityToolRegistry,
|
|
25
|
+
} from "./capability-runtime.ts";
|
|
26
|
+
import {
|
|
27
|
+
type CapabilitySettingsPatch,
|
|
28
|
+
CapabilitySettingsSchema,
|
|
29
|
+
type CapabilitySettingsService,
|
|
30
|
+
createCapabilitySettingsController,
|
|
31
|
+
resolveCapabilitySettings,
|
|
32
|
+
} from "./capability-settings.ts";
|
|
33
|
+
import {
|
|
34
|
+
CODEX_IMAGE_EDIT_TOOL,
|
|
35
|
+
CODEX_IMAGE_GENERATE_TOOL,
|
|
36
|
+
createCapabilityTools,
|
|
37
|
+
type ResolveCodexImageRoute,
|
|
38
|
+
resolveCodexImageRouteFromLlm,
|
|
39
|
+
} from "./capability-tools.ts";
|
|
40
|
+
import { codexAuthFromSession } from "./codex-http.ts";
|
|
41
|
+
import { createCodexModelCapabilities } from "./codex-model-capabilities.ts";
|
|
42
|
+
import { createCodexSearchProvider } from "./codex-search.ts";
|
|
43
|
+
import { createCodexUsageReader } from "./codex-usage.ts";
|
|
44
|
+
import { createCodingOAuthGatewayController } from "./gateway.ts";
|
|
45
|
+
import { type GatewayConfig, GatewayConfigSchema } from "./gateway-config.ts";
|
|
46
|
+
import { registerGatewayRoutes } from "./gateway-routes.ts";
|
|
47
|
+
import {
|
|
48
|
+
createGrokImagineClient,
|
|
49
|
+
GROK_IMAGINE_IMAGE_TOOL,
|
|
50
|
+
GROK_IMAGINE_VIDEO_STATUS_TOOL,
|
|
51
|
+
GROK_IMAGINE_VIDEO_TOOL,
|
|
52
|
+
GrokImagineError,
|
|
53
|
+
type ImagineOperation,
|
|
54
|
+
} from "./grok-imagine.ts";
|
|
55
|
+
import {
|
|
56
|
+
CLAUDE_PI_PROVIDER,
|
|
57
|
+
CODEX_PI_PROVIDER,
|
|
58
|
+
CODING_OAUTH_ROUTES,
|
|
59
|
+
KIMI_PI_PROVIDER,
|
|
60
|
+
XAI_PI_PROVIDER,
|
|
61
|
+
} from "./ids.ts";
|
|
62
|
+
import { registerImagineRoutes } from "./imagine-routes.ts";
|
|
63
|
+
import { MediaStore } from "./media-store.ts";
|
|
64
|
+
import {
|
|
65
|
+
type OAuthImportDestinationStore,
|
|
66
|
+
type OAuthImportDestinations,
|
|
67
|
+
registerOAuthImportRoutes,
|
|
68
|
+
} from "./oauth-import-routes.ts";
|
|
69
|
+
import { OAUTH_PROVIDER_DEFINITIONS } from "./oauth-providers.ts";
|
|
70
|
+
import { OAuthProviderSession } from "./oauth-session.ts";
|
|
71
|
+
import type { OAuthSourceCredential } from "./oauth-sources.ts";
|
|
72
|
+
import { ensureCodingOAuthProxy } from "./proxy.ts";
|
|
73
|
+
import { GrokBuildSession } from "./session.ts";
|
|
74
|
+
import { GrokBuildCredentialStore, type OAuthCredentialFileStore } from "./store.ts";
|
|
75
|
+
|
|
76
|
+
export { createCodingOAuthAdapter, createGrokBuildAdapter, preferredGrokBuildModel } from "./adapter.ts";
|
|
77
|
+
export type { AliasLlmRoutePolicy } from "./alias-adapter.ts";
|
|
78
|
+
export { AliasLlmAdapter } from "./alias-adapter.ts";
|
|
79
|
+
export type { GrokBuildAuthStatus } from "./auth.ts";
|
|
80
|
+
export {
|
|
81
|
+
grokBuildAuthStatus,
|
|
82
|
+
importGrokBuildFromGrok,
|
|
83
|
+
importGrokBuildSession,
|
|
84
|
+
loginGrokBuild,
|
|
85
|
+
loginGrokBuildSession,
|
|
86
|
+
logoutGrokBuild,
|
|
87
|
+
} from "./auth.ts";
|
|
88
|
+
export type {
|
|
89
|
+
CodingOAuthWebStatus,
|
|
90
|
+
GrokBuildLoginMethod,
|
|
91
|
+
GrokBuildWebAuthStatus,
|
|
92
|
+
LoginChallenge,
|
|
93
|
+
SubscriptionLoginChallenge,
|
|
94
|
+
SubscriptionWebAuthStatus,
|
|
95
|
+
} from "./auth-routes.ts";
|
|
96
|
+
export {
|
|
97
|
+
CODING_OAUTH_LOGIN_CANCEL_PATH,
|
|
98
|
+
CODING_OAUTH_LOGIN_CODE_PATH,
|
|
99
|
+
CODING_OAUTH_LOGIN_PATH,
|
|
100
|
+
CODING_OAUTH_LOGOUT_PATH,
|
|
101
|
+
CODING_OAUTH_MODELS_PATH,
|
|
102
|
+
CODING_OAUTH_STATUS_PATH,
|
|
103
|
+
GROK_BUILD_AUTH_IMPORT_PATH,
|
|
104
|
+
GROK_BUILD_AUTH_LOGIN_CANCEL_PATH,
|
|
105
|
+
GROK_BUILD_AUTH_LOGIN_CODE_PATH,
|
|
106
|
+
GROK_BUILD_AUTH_LOGIN_PATH,
|
|
107
|
+
GROK_BUILD_AUTH_LOGOUT_PATH,
|
|
108
|
+
GROK_BUILD_AUTH_MODELS_PATH,
|
|
109
|
+
GROK_BUILD_AUTH_STATUS_PATH,
|
|
110
|
+
GrokBuildWebAuth,
|
|
111
|
+
registerCodingOAuthRoutes,
|
|
112
|
+
registerGrokBuildAuthRoutes,
|
|
113
|
+
SubscriptionWebAuth,
|
|
114
|
+
} from "./auth-routes.ts";
|
|
115
|
+
export type { CatalogSource, LiveModelDescriptor } from "./catalog.ts";
|
|
116
|
+
export {
|
|
117
|
+
extractLiveModels,
|
|
118
|
+
extractModelIds,
|
|
119
|
+
fetchLiveModelIds,
|
|
120
|
+
fetchLiveModels,
|
|
121
|
+
materializeLiveModel,
|
|
122
|
+
mergeLiveCatalog,
|
|
123
|
+
preferredGrokBuildModelFrom,
|
|
124
|
+
thinkingLevelMapFromLiveEfforts,
|
|
125
|
+
} from "./catalog.ts";
|
|
126
|
+
export type { GrokImportProbe } from "./grok-import.ts";
|
|
127
|
+
export { grokAuthPath, importGrokAuth, parseGrokAuthDocument, probeGrokAuth } from "./grok-import.ts";
|
|
128
|
+
export type { CodingOAuthProviderSlug, CodingOAuthRoute } from "./ids.ts";
|
|
129
|
+
export {
|
|
130
|
+
ANTIGRAVITY_ROUTE,
|
|
131
|
+
CLAUDE_CODE_OAUTH_AUTH_FILENAME,
|
|
132
|
+
CLAUDE_CODE_OAUTH_MODELS_CACHE_FILENAME,
|
|
133
|
+
CLAUDE_CODE_OAUTH_ROUTE,
|
|
134
|
+
CLAUDE_PI_PROVIDER,
|
|
135
|
+
CODEX_OAUTH_AUTH_FILENAME,
|
|
136
|
+
CODEX_OAUTH_MODELS_CACHE_FILENAME,
|
|
137
|
+
CODEX_OAUTH_ROUTE,
|
|
138
|
+
CODEX_PI_PROVIDER,
|
|
139
|
+
CODING_OAUTH_ROUTES,
|
|
140
|
+
DEFAULT_GROK_BUILD_MODEL,
|
|
141
|
+
GROK_BUILD_AUTH_FILENAME,
|
|
142
|
+
GROK_BUILD_MODELS_CACHE_FILENAME,
|
|
143
|
+
GROK_BUILD_ROUTE,
|
|
144
|
+
GROK_BUILD_STREAM_IDLE_TIMEOUT_MS,
|
|
145
|
+
KIMI_CODE_OAUTH_AUTH_FILENAME,
|
|
146
|
+
KIMI_CODE_OAUTH_MODELS_CACHE_FILENAME,
|
|
147
|
+
KIMI_CODE_OAUTH_ROUTE,
|
|
148
|
+
KIMI_PI_PROVIDER,
|
|
149
|
+
XAI_PI_PROVIDER,
|
|
150
|
+
} from "./ids.ts";
|
|
151
|
+
export type { GrokBuildOAuthErrorCode, GrokBuildOAuthParams, PkceLoginCallbacks } from "./oauth.ts";
|
|
152
|
+
export {
|
|
153
|
+
buildAuthorizeUrl,
|
|
154
|
+
discoverOAuthEndpoints,
|
|
155
|
+
extractCode,
|
|
156
|
+
GROK_BUILD_OAUTH_CLIENT_ID,
|
|
157
|
+
GROK_BUILD_OAUTH_DEFAULT_PORT,
|
|
158
|
+
GROK_BUILD_OAUTH_ISSUER,
|
|
159
|
+
GROK_BUILD_OAUTH_SCOPE,
|
|
160
|
+
GrokBuildOAuthError,
|
|
161
|
+
generatePkce,
|
|
162
|
+
loginGrokBuildPkce,
|
|
163
|
+
refreshGrokBuildToken,
|
|
164
|
+
resolveOAuthParams,
|
|
165
|
+
} from "./oauth.ts";
|
|
166
|
+
export type { OAuthProviderDefinition, SubscriptionLoginMethod, SubscriptionProviderSlug } from "./oauth-providers.ts";
|
|
167
|
+
export {
|
|
168
|
+
CLAUDE_CODE_OAUTH_PROVIDER,
|
|
169
|
+
CODEX_OAUTH_PROVIDER,
|
|
170
|
+
KIMI_CODE_OAUTH_PROVIDER,
|
|
171
|
+
OAUTH_PROVIDER_DEFINITIONS,
|
|
172
|
+
oauthProviderDefinition,
|
|
173
|
+
} from "./oauth-providers.ts";
|
|
174
|
+
export type { OAuthProviderStatus } from "./oauth-session.ts";
|
|
175
|
+
export { OAuthProviderSession, oauthModelsCachePath } from "./oauth-session.ts";
|
|
176
|
+
export {
|
|
177
|
+
GROK_BUILD_BASE_URL,
|
|
178
|
+
GROK_BUILD_MODELS_URL,
|
|
179
|
+
GROK_CLIENT_VERSION,
|
|
180
|
+
grokBuildBaselineModels,
|
|
181
|
+
grokBuildFingerprintHeaders,
|
|
182
|
+
grokBuildProvider,
|
|
183
|
+
grokBuildReasoningMap,
|
|
184
|
+
} from "./provider.ts";
|
|
185
|
+
export type { CodingOAuthProxyOptions } from "./proxy.ts";
|
|
186
|
+
export {
|
|
187
|
+
codingOAuthProxyInEffect,
|
|
188
|
+
codingOAuthProxyUnreachableHint,
|
|
189
|
+
ensureCodingOAuthProxy,
|
|
190
|
+
ensureGrokBuildProxy,
|
|
191
|
+
grokBuildProxyInEffect,
|
|
192
|
+
} from "./proxy.ts";
|
|
193
|
+
export { redactProxyUrl, safeMessage } from "./redact.ts";
|
|
194
|
+
export { GrokBuildSession } from "./session.ts";
|
|
195
|
+
export {
|
|
196
|
+
GrokBuildCredentialStore,
|
|
197
|
+
grokBuildAuthPath,
|
|
198
|
+
OAuthCredentialFileStore,
|
|
199
|
+
oauthCredentialPath,
|
|
200
|
+
} from "./store.ts";
|
|
201
|
+
|
|
202
|
+
/** Stable Cordis plugin name. */
|
|
203
|
+
export const name = "llm-grok-build-oauth";
|
|
204
|
+
|
|
205
|
+
/** Separate API-key credential used only by official xAI Imagine REST calls. */
|
|
206
|
+
export const XAI_API_KEY_CREDENTIAL = "XAI_API_KEY";
|
|
207
|
+
/** Validate locally because `credentialRef()` is a value export of the optional credentials peer. */
|
|
208
|
+
const XAI_API_KEY_REF = fixedCredentialRef(XAI_API_KEY_CREDENTIAL);
|
|
209
|
+
|
|
210
|
+
function fixedCredentialRef(value: string): CredentialRef {
|
|
211
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(value)) {
|
|
212
|
+
throw new TypeError(`invalid credential reference ${JSON.stringify(value)}`);
|
|
213
|
+
}
|
|
214
|
+
return value as CredentialRef;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Owner-private artifact directory below the resolved DSH home. */
|
|
218
|
+
export const IMAGINE_MEDIA_STORE_DIRNAME = ".dsh-coding-subscription-oauth-media";
|
|
219
|
+
|
|
220
|
+
/** LLM registry required before the subscription route can register. */
|
|
221
|
+
export const inject = ["llm"];
|
|
222
|
+
|
|
223
|
+
/** Plugin configuration; every field is optional. */
|
|
224
|
+
export interface Config {
|
|
225
|
+
/** HTTP(S) proxy URL for the audited coding-subscription host allowlist. */
|
|
226
|
+
proxy?: string;
|
|
227
|
+
/** Kimi China traffic stays direct unless explicitly opted into the proxy. */
|
|
228
|
+
proxyKimi?: boolean;
|
|
229
|
+
/**
|
|
230
|
+
* Optional provider retry policy override for the four OAuth routes. When
|
|
231
|
+
* omitted, the plugin retries transient failures (rate limit, server,
|
|
232
|
+
* timeout, transport, empty response) plus AUTH — the latter is safe because
|
|
233
|
+
* the stored credential is invalidated on every AUTH finish, so the retried
|
|
234
|
+
* step refreshes before reuse. Quota exhaustion is never retried.
|
|
235
|
+
*/
|
|
236
|
+
retryPolicy?: RetryPolicyConfig;
|
|
237
|
+
/** Secret-free composition/YAML defaults below live user settings. */
|
|
238
|
+
capabilities?: CapabilitySettingsPatch;
|
|
239
|
+
/** Opt-in isolated local OpenAI-compatible gateway. Default off. */
|
|
240
|
+
gateway?: Partial<GatewayConfig>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export const Config: z<Config> = z.object({
|
|
244
|
+
proxy: z.string(),
|
|
245
|
+
proxyKimi: z.boolean().default(false),
|
|
246
|
+
retryPolicy: RetryPolicySchema,
|
|
247
|
+
capabilities: CapabilitySettingsSchema,
|
|
248
|
+
gateway: GatewayConfigSchema,
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const CODEX_TOOL_NAMES = new Set<string>([CODEX_IMAGE_GENERATE_TOOL, CODEX_IMAGE_EDIT_TOOL]);
|
|
252
|
+
const IMAGINE_TOOL_NAMES = new Set<string>([
|
|
253
|
+
GROK_IMAGINE_IMAGE_TOOL,
|
|
254
|
+
GROK_IMAGINE_VIDEO_TOOL,
|
|
255
|
+
GROK_IMAGINE_VIDEO_STATUS_TOOL,
|
|
256
|
+
]);
|
|
257
|
+
|
|
258
|
+
function requireSubscription(
|
|
259
|
+
subscriptions: readonly OAuthProviderSession[],
|
|
260
|
+
nativeProviderId: string,
|
|
261
|
+
): OAuthProviderSession {
|
|
262
|
+
const session = subscriptions.find((candidate) => candidate.definition.nativeProviderId === nativeProviderId);
|
|
263
|
+
if (session === undefined) throw new Error(`missing built-in OAuth provider ${nativeProviderId}`);
|
|
264
|
+
return session;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function asSourceCredential(credential: Credential | undefined): OAuthSourceCredential | undefined {
|
|
268
|
+
if (credential === undefined) return undefined;
|
|
269
|
+
if (credential.type !== "oauth") throw new Error("OAuth import destination contains a non-OAuth credential");
|
|
270
|
+
const accountId =
|
|
271
|
+
typeof credential.accountId === "string" && credential.accountId.length > 0 ? credential.accountId : undefined;
|
|
272
|
+
return {
|
|
273
|
+
type: "oauth",
|
|
274
|
+
access: credential.access,
|
|
275
|
+
refresh: credential.refresh,
|
|
276
|
+
expires: credential.expires,
|
|
277
|
+
...(accountId === undefined ? {} : { accountId }),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function asStoredCredential(credential: OAuthSourceCredential): OAuthCredential {
|
|
282
|
+
return {
|
|
283
|
+
type: "oauth",
|
|
284
|
+
access: credential.access,
|
|
285
|
+
refresh: credential.refresh,
|
|
286
|
+
expires: credential.expires,
|
|
287
|
+
...(credential.accountId === undefined ? {} : { accountId: credential.accountId }),
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function oauthImportStore(store: OAuthCredentialFileStore): OAuthImportDestinationStore {
|
|
292
|
+
return {
|
|
293
|
+
filename: store.filename,
|
|
294
|
+
async modify(providerId, fn) {
|
|
295
|
+
const result = await store.modify(providerId, async (current) => {
|
|
296
|
+
const next = await fn(asSourceCredential(current));
|
|
297
|
+
return next === undefined ? current : asStoredCredential(next);
|
|
298
|
+
});
|
|
299
|
+
return asSourceCredential(result);
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function oauthImportDestinations(
|
|
305
|
+
grok: GrokBuildSession,
|
|
306
|
+
subscriptions: readonly OAuthProviderSession[],
|
|
307
|
+
): OAuthImportDestinations {
|
|
308
|
+
const codex = requireSubscription(subscriptions, CODEX_PI_PROVIDER);
|
|
309
|
+
const kimi = requireSubscription(subscriptions, KIMI_PI_PROVIDER);
|
|
310
|
+
const claude = requireSubscription(subscriptions, CLAUDE_PI_PROVIDER);
|
|
311
|
+
return {
|
|
312
|
+
grok: { providerId: XAI_PI_PROVIDER, store: oauthImportStore(grok.store) },
|
|
313
|
+
codex: { providerId: codex.definition.nativeProviderId, store: oauthImportStore(codex.store) },
|
|
314
|
+
kimi: { providerId: kimi.definition.nativeProviderId, store: oauthImportStore(kimi.store) },
|
|
315
|
+
claude: { providerId: claude.definition.nativeProviderId, store: oauthImportStore(claude.store) },
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function describeImagineCredential(credentials: CredentialProvider | undefined): Promise<CredentialInfo> {
|
|
320
|
+
if (credentials === undefined) return { configured: false, writable: false };
|
|
321
|
+
return credentials.describe(XAI_API_KEY_REF);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async function resolveImagineApiKey(credentials: CredentialProvider, operation: ImagineOperation): Promise<string> {
|
|
325
|
+
const resolved = await credentials.resolve(XAI_API_KEY_REF);
|
|
326
|
+
if (resolved === undefined) {
|
|
327
|
+
throw new GrokImagineError(
|
|
328
|
+
"MISSING_CREDENTIAL",
|
|
329
|
+
`${XAI_API_KEY_CREDENTIAL} is not configured for ${operation}. Grok Imagine does not use OAuth.`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
return assertUsableApiKey(resolved.value, "dsh-coding-subscription-oauth", XAI_API_KEY_CREDENTIAL);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function unavailableImagineClient(): {
|
|
336
|
+
generateImage(): Promise<never>;
|
|
337
|
+
startVideo(): Promise<never>;
|
|
338
|
+
videoStatus(): Promise<never>;
|
|
339
|
+
} {
|
|
340
|
+
const unavailable = async (): Promise<never> => {
|
|
341
|
+
throw new GrokImagineError("MISSING_CREDENTIAL", "Grok Imagine services are not composed");
|
|
342
|
+
};
|
|
343
|
+
return { generateImage: unavailable, startVideo: unavailable, videoStatus: unavailable };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Register the `grok-build` LLM route with a provider-native OAuth store.
|
|
348
|
+
* @param ctx - plugin context carrying the LLM registry plus optional web server.
|
|
349
|
+
*/
|
|
350
|
+
export function apply(ctx: Context, config: Config): void {
|
|
351
|
+
ensureCodingOAuthProxy(config.proxy, config.proxyKimi === undefined ? {} : { proxyKimi: config.proxyKimi });
|
|
352
|
+
const logger = ctx.logger(name);
|
|
353
|
+
const baseCapabilities = resolveCapabilitySettings(config.capabilities);
|
|
354
|
+
const runtime = new CapabilityRuntimeState(baseCapabilities, () => {
|
|
355
|
+
logger.warn("an optional capability listener failed");
|
|
356
|
+
});
|
|
357
|
+
let active = true;
|
|
358
|
+
ctx.effect(
|
|
359
|
+
() => () => {
|
|
360
|
+
active = false;
|
|
361
|
+
},
|
|
362
|
+
"dsh-coding-subscription-oauth: startup lifetime",
|
|
363
|
+
);
|
|
364
|
+
let invalidateOptionalAuthState = (): void => undefined;
|
|
365
|
+
const notifyCatalogChange = (): void => {
|
|
366
|
+
if (!active) return;
|
|
367
|
+
try {
|
|
368
|
+
ctx.emit("llm/adapters-updated");
|
|
369
|
+
} catch (error) {
|
|
370
|
+
// Catalog observers are advisory; a broken listener must not turn an
|
|
371
|
+
// already-persisted OAuth login/logout into an apparent auth failure.
|
|
372
|
+
logger.warn("an llm/adapters-updated listener failed");
|
|
373
|
+
logger.warn(error);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
const grok = new GrokBuildSession(new GrokBuildCredentialStore(), notifyCatalogChange);
|
|
377
|
+
const subscriptions = OAUTH_PROVIDER_DEFINITIONS.map(
|
|
378
|
+
(definition) =>
|
|
379
|
+
new OAuthProviderSession(definition, () => {
|
|
380
|
+
if (definition.nativeProviderId === CODEX_PI_PROVIDER) invalidateOptionalAuthState();
|
|
381
|
+
notifyCatalogChange();
|
|
382
|
+
}),
|
|
383
|
+
);
|
|
384
|
+
const codex = requireSubscription(subscriptions, CODEX_PI_PROVIDER);
|
|
385
|
+
const codexAuth = codexAuthFromSession(codex);
|
|
386
|
+
const usage = createCodexUsageReader({ auth: codexAuth });
|
|
387
|
+
const codexModels = createCodexModelCapabilities({ auth: codexAuth });
|
|
388
|
+
const resolveCodexImageRoute: ResolveCodexImageRoute = (exec) =>
|
|
389
|
+
resolveCodexImageRouteFromLlm(exec, (provider, model, signal) => ctx.llm.resolveModelInfo(provider, model, signal));
|
|
390
|
+
invalidateOptionalAuthState = () => {
|
|
391
|
+
usage.clear();
|
|
392
|
+
codexModels.clear();
|
|
393
|
+
runtime.refresh();
|
|
394
|
+
};
|
|
395
|
+
const adapterRegistration = ctx.llm.registerAdapter(
|
|
396
|
+
[...CODING_OAUTH_ROUTES],
|
|
397
|
+
createCodingOAuthAdapter(grok, subscriptions, () => ctx.get("attachments"), config.retryPolicy, {
|
|
398
|
+
codexFast: {
|
|
399
|
+
isEligible: (modelId) => runtime.current().codexFast && codexModels.isPriorityEligible(modelId),
|
|
400
|
+
},
|
|
401
|
+
}),
|
|
402
|
+
);
|
|
403
|
+
ctx.effect(
|
|
404
|
+
() =>
|
|
405
|
+
bindCodexFastRoute(runtime, codexModels, adapterRegistration, {
|
|
406
|
+
onError: () => logger.warn("Codex Fast eligibility refresh failed closed"),
|
|
407
|
+
}),
|
|
408
|
+
"dsh-coding-subscription-oauth: Codex Fast route",
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
void Promise.allSettled([grok.loadCachedCatalog(), ...subscriptions.map((session) => session.loadCachedModels())])
|
|
412
|
+
.then(async (results) => {
|
|
413
|
+
if (!active) return;
|
|
414
|
+
if (results.some((result) => result.status === "rejected")) {
|
|
415
|
+
logger.warn("one or more OAuth model caches could not be loaded; using in-memory fallbacks");
|
|
416
|
+
}
|
|
417
|
+
await grok.refreshLiveCatalog();
|
|
418
|
+
})
|
|
419
|
+
.catch(() => {
|
|
420
|
+
// Contain every startup refresh failure so plugin activation cannot leave
|
|
421
|
+
// an unhandled rejection. The static provider catalogs remain usable.
|
|
422
|
+
if (active) logger.warn("background OAuth model catalog initialization failed; using static fallbacks");
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
let settingsOwner = 0;
|
|
426
|
+
let releaseActiveSettings = (): void => undefined;
|
|
427
|
+
ctx.effect(() => () => releaseActiveSettings(), "dsh-coding-subscription-oauth: capability settings bridge");
|
|
428
|
+
ctx.inject(["settings"], (settingsCtx) => {
|
|
429
|
+
// Re-injection may happen before Cordis runs the previous child effect's
|
|
430
|
+
// disposer. Release it synchronously so an obsolete watcher cannot race a
|
|
431
|
+
// newly attached settings service.
|
|
432
|
+
releaseActiveSettings();
|
|
433
|
+
const owner = ++settingsOwner;
|
|
434
|
+
const controller = createCapabilitySettingsController({
|
|
435
|
+
settings: settingsCtx.get("settings") as CapabilitySettingsService,
|
|
436
|
+
...(config.capabilities === undefined ? {} : { base: config.capabilities }),
|
|
437
|
+
onListenerError: () => logger.warn("a capability settings listener failed"),
|
|
438
|
+
});
|
|
439
|
+
runtime.set(controller.current());
|
|
440
|
+
const unsubscribe = controller.subscribe((snapshot) => {
|
|
441
|
+
runtime.set(snapshot.value);
|
|
442
|
+
});
|
|
443
|
+
let released = false;
|
|
444
|
+
const release = (): void => {
|
|
445
|
+
if (released) return;
|
|
446
|
+
released = true;
|
|
447
|
+
unsubscribe();
|
|
448
|
+
controller.dispose();
|
|
449
|
+
if (owner === settingsOwner) {
|
|
450
|
+
releaseActiveSettings = (): void => undefined;
|
|
451
|
+
runtime.set(baseCapabilities);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
releaseActiveSettings = release;
|
|
455
|
+
settingsCtx.effect(() => release, "dsh-coding-subscription-oauth: capability settings");
|
|
456
|
+
settingsCtx.inject(["webServer"], (webCtx) => {
|
|
457
|
+
registerCapabilityRoutes(webCtx, {
|
|
458
|
+
controller,
|
|
459
|
+
usage: () => usage.read(),
|
|
460
|
+
credentialInfo: () => describeImagineCredential(webCtx.get("credentials") as CredentialProvider | undefined),
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
const gateway = createCodingOAuthGatewayController({
|
|
466
|
+
...(config.gateway === undefined ? {} : { config: config.gateway }),
|
|
467
|
+
grok,
|
|
468
|
+
subscriptions,
|
|
469
|
+
onError: (error) => {
|
|
470
|
+
logger.warn("local API gateway failed to start; LLM routes are unchanged");
|
|
471
|
+
logger.warn(error);
|
|
472
|
+
},
|
|
473
|
+
});
|
|
474
|
+
ctx.effect(() => {
|
|
475
|
+
void gateway.startIfEnabled().then((started) => {
|
|
476
|
+
if (started !== undefined) {
|
|
477
|
+
logger.warn("local API gateway is enabled; exposing a subscription as a local API can violate provider ToS");
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return () => {
|
|
481
|
+
void gateway.stop();
|
|
482
|
+
};
|
|
483
|
+
}, "dsh-coding-subscription-oauth: local API gateway");
|
|
484
|
+
|
|
485
|
+
ctx.inject(["webServer"], (webCtx) => {
|
|
486
|
+
registerGatewayRoutes(webCtx, gateway);
|
|
487
|
+
registerCodingOAuthRoutes(webCtx, grok, subscriptions);
|
|
488
|
+
registerOAuthImportRoutes(webCtx, oauthImportDestinations(grok, subscriptions), {
|
|
489
|
+
onImported: (event) => {
|
|
490
|
+
if (event.kind === "codex") invalidateOptionalAuthState();
|
|
491
|
+
notifyCatalogChange();
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
const search = createCodexSearchProvider({
|
|
497
|
+
auth: codexAuth,
|
|
498
|
+
// Login, import, model selection, and catalog refreshes can all change the
|
|
499
|
+
// first visible Codex model after plugin startup.
|
|
500
|
+
model: () => codex.visibleModels()[0]?.id ?? "",
|
|
501
|
+
});
|
|
502
|
+
ctx.inject(["web"], (webCtx) => {
|
|
503
|
+
const web = webCtx.get("web") as CapabilitySearchRegistry;
|
|
504
|
+
webCtx.effect(() => bindCapabilitySearch(runtime, web, search), "dsh-coding-subscription-oauth: Codex search");
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
ctx.inject(["tools", "attachments"], async (toolCtx) => {
|
|
508
|
+
const tools = toolCtx.get("tools") as CapabilityToolRegistry;
|
|
509
|
+
const attachments = toolCtx.get("attachments") as AttachmentStore;
|
|
510
|
+
const definitions = (
|
|
511
|
+
await createCapabilityTools({
|
|
512
|
+
current: () => runtime.current(),
|
|
513
|
+
auth: codexAuth,
|
|
514
|
+
attachments,
|
|
515
|
+
imagine: unavailableImagineClient(),
|
|
516
|
+
resolveCodexImageRoute,
|
|
517
|
+
})
|
|
518
|
+
).filter((definition) => CODEX_TOOL_NAMES.has(definition.name));
|
|
519
|
+
toolCtx.effect(
|
|
520
|
+
() => bindCapabilityTools(runtime, tools, definitions),
|
|
521
|
+
"dsh-coding-subscription-oauth: Codex image tools",
|
|
522
|
+
);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
ctx.inject(["tools", "attachments", "credentials", "webServer"], async (toolCtx) => {
|
|
526
|
+
const tools = toolCtx.get("tools") as CapabilityToolRegistry;
|
|
527
|
+
const attachments = toolCtx.get("attachments") as AttachmentStore;
|
|
528
|
+
const credentials = toolCtx.get("credentials") as CredentialProvider;
|
|
529
|
+
const media = new MediaStore(join(dirname(grok.store.filename), IMAGINE_MEDIA_STORE_DIRNAME), {
|
|
530
|
+
retentionMs: runtime.current().videoArtifactTtlMs,
|
|
531
|
+
});
|
|
532
|
+
const routeRegistry = registerImagineRoutes(toolCtx, {
|
|
533
|
+
attachments,
|
|
534
|
+
media: { readForDownload: (artifactId, authz) => media.openDownload(artifactId, authz) },
|
|
535
|
+
});
|
|
536
|
+
const imagine = createGrokImagineClient({
|
|
537
|
+
resolveApiKey: (operation) => resolveImagineApiKey(credentials, operation),
|
|
538
|
+
attachments,
|
|
539
|
+
media,
|
|
540
|
+
});
|
|
541
|
+
const routedImagine = {
|
|
542
|
+
async generateImage(input: Parameters<typeof imagine.generateImage>[0], signal?: AbortSignal) {
|
|
543
|
+
const result = await imagine.generateImage(input, signal);
|
|
544
|
+
if (runtime.current().grokImagineImage) {
|
|
545
|
+
routeRegistry.rememberImages(result.images.map((image) => image.attachment));
|
|
546
|
+
}
|
|
547
|
+
return result;
|
|
548
|
+
},
|
|
549
|
+
startVideo: (input: Parameters<typeof imagine.startVideo>[0], signal?: AbortSignal) =>
|
|
550
|
+
imagine.startVideo(input, signal),
|
|
551
|
+
async videoStatus(requestId: string, options?: Parameters<typeof imagine.videoStatus>[1]) {
|
|
552
|
+
const result = await imagine.videoStatus(requestId, options);
|
|
553
|
+
if (result.artifact !== undefined && runtime.current().grokImagineVideo) {
|
|
554
|
+
routeRegistry.rememberArtifact(result.artifact);
|
|
555
|
+
}
|
|
556
|
+
return result;
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
const definitions = (
|
|
560
|
+
await createCapabilityTools({
|
|
561
|
+
current: () => runtime.current(),
|
|
562
|
+
auth: codexAuth,
|
|
563
|
+
attachments,
|
|
564
|
+
imagine: routedImagine,
|
|
565
|
+
resolveCodexImageRoute,
|
|
566
|
+
})
|
|
567
|
+
).filter((definition) => IMAGINE_TOOL_NAMES.has(definition.name));
|
|
568
|
+
let previousSettings = runtime.current();
|
|
569
|
+
const releaseRetention = runtime.subscribe((settings) => {
|
|
570
|
+
const previous = previousSettings;
|
|
571
|
+
previousSettings = settings;
|
|
572
|
+
if (previous.grokImagineImage && !settings.grokImagineImage) routeRegistry.revokeImages();
|
|
573
|
+
if (previous.grokImagineVideo && !settings.grokImagineVideo) routeRegistry.revokeArtifacts();
|
|
574
|
+
return media
|
|
575
|
+
.applyRetentionMs(settings.videoArtifactTtlMs)
|
|
576
|
+
.then(() => undefined)
|
|
577
|
+
.catch(() => logger.warn("Imagine media retention cleanup failed"));
|
|
578
|
+
});
|
|
579
|
+
toolCtx.effect(
|
|
580
|
+
() => bindCapabilityTools(runtime, tools, definitions),
|
|
581
|
+
"dsh-coding-subscription-oauth: Grok Imagine tools",
|
|
582
|
+
);
|
|
583
|
+
toolCtx.effect(
|
|
584
|
+
() => async () => {
|
|
585
|
+
// Abort new/in-flight work before cleanup regardless of Cordis disposer
|
|
586
|
+
// ordering. GrokImagineClient.dispose() is intentionally idempotent.
|
|
587
|
+
imagine.dispose();
|
|
588
|
+
releaseRetention();
|
|
589
|
+
try {
|
|
590
|
+
await media.cleanup();
|
|
591
|
+
} catch {
|
|
592
|
+
logger.warn("Imagine media cleanup failed");
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
"dsh-coding-subscription-oauth: Imagine client and media lifetime",
|
|
596
|
+
);
|
|
597
|
+
});
|
|
598
|
+
}
|
package/src/invariant.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package-owned invariant companion for `dsh-coding-subscription-oauth`.
|
|
3
|
+
* @module dsh-coding-subscription-oauth/invariant
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
7
|
+
import type { InvariantInstaller } from "@deepseek-ai/dsh-invariants";
|
|
8
|
+
|
|
9
|
+
const PACKAGE_NAME = "dsh-coding-subscription-oauth";
|
|
10
|
+
|
|
11
|
+
export const name = "grok-build-invariant";
|
|
12
|
+
export const inject = ["invariants"];
|
|
13
|
+
|
|
14
|
+
const install: InvariantInstaller = () => {};
|
|
15
|
+
|
|
16
|
+
export const apply = (ctx: Context): Promise<() => void> =>
|
|
17
|
+
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kimi Code error remapping.
|
|
3
|
+
* @module dsh-coding-subscription-oauth/kimi-errors
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { CONTEXT_WINDOW_EXCEEDED_CODE, isContextWindowExceededError } from "@deepseek-ai/dsh-llm";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Kimi Code returns HTTP 401 `authentication_error` for context overflow, e.g.
|
|
10
|
+
* `k3-256k supports only 256K context.` The harness pi-ai classifier sees the
|
|
11
|
+
* status and labels it AUTH, which would invalidate a still-valid token and
|
|
12
|
+
* retry a request that cannot succeed.
|
|
13
|
+
*/
|
|
14
|
+
const KIMI_CONTEXT_CAPACITY = /\bsupports only\s+\d+\s*k\s+context\b/i;
|
|
15
|
+
|
|
16
|
+
export function isMisclassifiedContextWindowError(detail: string): boolean {
|
|
17
|
+
return isContextWindowExceededError(detail) || KIMI_CONTEXT_CAPACITY.test(detail);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function remapAuthFailureIfContextOverflow(failure: { message: string; code: string }): {
|
|
21
|
+
message: string;
|
|
22
|
+
code: string;
|
|
23
|
+
} {
|
|
24
|
+
if (failure.code !== "AUTH" || !isMisclassifiedContextWindowError(failure.message)) return failure;
|
|
25
|
+
return { ...failure, code: CONTEXT_WINDOW_EXCEEDED_CODE };
|
|
26
|
+
}
|