{"record":{"id":"e202b9861b604ede","repo":"mastra-ai/mastra","slug":"not-logged-in-to-kimi-for-coding","errorCode":null,"errorMessage":"Not logged in to Kimi For Coding.","messagePattern":"Not logged in to Kimi For Coding\\.","errorType":"exception","errorClass":"ProviderAuthRequiredError","httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/providers/kimi-coding.ts","lineNumber":32,"sourceCode":"\nlet authStorageInstance: AuthStorage | null = null;\n\nexport function setAuthStorage(storage: AuthStorage | undefined): void {\n  authStorageInstance = storage ?? null;\n}\n\nfunction getAuthStorage(): AuthStorage {\n  if (!authStorageInstance) authStorageInstance = new AuthStorage();\n  return authStorageInstance;\n}\n\nexport function buildKimiCodingOAuthFetch(options: { credentialStore?: CredentialStore } = {}): typeof fetch {\n  return (async (input: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n    const store = options.credentialStore ?? getAuthStorage();\n    store.reload();\n    const credential = store.get(PROVIDER_ID);\n    if (!credential || credential.type !== 'oauth') {\n      throw new ProviderAuthRequiredError('Not logged in to Kimi For Coding.');\n    }\n    if (!isKimiCodingDeviceId(credential.deviceId)) {\n      throw new ProviderAuthRequiredError('Kimi For Coding credentials are invalid. Please reconnect the account.');\n    }\n    const deviceHeaders = getKimiCodingDeviceHeaders(credential.deviceId);\n    const token = await store.getApiKey(PROVIDER_ID);\n    if (!token) throw new ProviderAuthRequiredError('Failed to refresh the Kimi For Coding token.');\n\n    const headers = new Headers(input instanceof Request ? input.headers : undefined);\n    if (init?.headers) new Headers(init.headers).forEach((value, key) => headers.set(key, value));\n    for (const [key, value] of Object.entries(deviceHeaders)) headers.set(key, value);\n    headers.delete('authorization');\n    headers.delete('x-api-key');\n    headers.set('Authorization', `Bearer ${token}`);\n    return fetch(input, { ...init, headers });\n  }) as typeof fetch;\n}\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/providers/kimi-coding.ts#L14-L50","documentation":"buildKimiCodingOAuthFetch wraps fetch so every request to the Kimi For Coding API carries OAuth credentials plus device headers. Before each request it reloads the credential store and requires an entry with provider id 'kimi-for-coding' of type 'oauth'. If none exists (or it is an api-key credential), it throws ProviderAuthRequiredError('Not logged in to Kimi For Coding.').","triggerScenarios":"Any API call routed through the Kimi For Coding OAuth fetch wrapper when the credential store has no 'kimi-for-coding' entry, or the entry exists but its type is not 'oauth' (e.g. it is an api-key credential or null after reload).","commonSituations":"User never completed the Kimi For Coding device-OAuth login; credentials were logged out or cleared; a user switched from API-key auth to OAuth (or vice versa) so the credential type mismatches; pointing the SDK at a fresh/empty credential store in tests or CI.","solutions":["Complete the Kimi For Coding OAuth login flow to store the 'kimi-for-coding' oauth credential.","Check the credentials store for a 'kimi-for-coding' entry and confirm its type is 'oauth'.","If you intend API-key auth instead, use buildKimiCodingApiKeyFetch / pass an apiKey rather than the OAuth fetch wrapper.","Verify the credentialStore passed in options is the one the login flow wrote to."],"exampleFix":"// before\nconst fetchWithAuth = buildKimiCodingOAuthFetch(); // throws if never logged in\n// after\nif (!credentialStore.get('kimi-for-coding')) {\n  await kimiForCodingLogin(); // device OAuth flow\n}\nconst fetchWithAuth = buildKimiCodingOAuthFetch({ credentialStore });","handlingStrategy":"validation","validationCode":"const cred = store.get('kimi-for-coding');\nif (!cred || cred.type !== 'oauth') {\n  throw new Error('Kimi For Coding login required before making requests');\n}","typeGuard":"function isKimiOAuthCred(c: unknown): c is { type: 'oauth'; deviceId: string } {\n  return !!c && typeof c === 'object' && (c as any).type === 'oauth' && typeof (c as any).deviceId === 'string';\n}","tryCatchPattern":"try {\n  return await fetchWithAuth(input, init);\n} catch (e) {\n  if (e instanceof ProviderAuthRequiredError && /Not logged in/.test(e.message)) {\n    await kimiForCodingLogin(); // interactive re-auth\n    return await fetchWithAuth(input, init);\n  }\n  throw e;\n}","preventionTips":["Gate model calls behind a logged-in check at startup.","Use one shared credential store instance for login and requests.","Surface a clear 'run login' message to end users instead of raw errors."],"tags":["auth","oauth","kimi","login-required"],"backgroundTag":"provider-not-authenticated","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}