| 97 | type SDKManagedConfig = KimiConfig & ManagedKimiConfigShape; |
| 98 | |
| 99 | export class KimiAuthFacade { |
| 100 | private readonly toolkit: KimiOAuthToolkit<SDKManagedConfig>; |
| 101 | |
| 102 | constructor(private readonly options: KimiAuthFacadeOptions) { |
| 103 | this.toolkit = new KimiOAuthToolkit<SDKManagedConfig>({ |
| 104 | homeDir: options.homeDir, |
| 105 | identity: options.identity, |
| 106 | onRefresh: options.onRefresh, |
| 107 | configAdapter: { |
| 108 | configPath: options.configPath, |
| 109 | // Write-path base read: strict (a salvaged base would drop the user's |
| 110 | // broken-but-fixable sections on rewrite) with an actionable message. |
| 111 | read: () => readConfigFileForUpdate(options.configPath) as SDKManagedConfig, |
| 112 | write: async (config) => { |
| 113 | await writeConfigFile(options.configPath, config); |
| 114 | }, |
| 115 | apply: applyManagedKimiCodeConfig, |
| 116 | remove: applyManagedKimiCodeLogoutConfig, |
| 117 | }, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | async status(providerName?: string | undefined): Promise<AuthStatus> { |
| 122 | return this.toolkit.status(providerName, this.resolveRuntimeManagedAuth(providerName).oauthRef); |
| 123 | } |
| 124 | |
| 125 | async login( |
| 126 | providerName: string | undefined = KIMI_CODE_PROVIDER_NAME, |
| 127 | options: KimiAuthLoginOptions = {}, |
| 128 | ): Promise<KimiAuthLoginResult> { |
| 129 | const auth = this.resolveManagedAuth(providerName); |
| 130 | const loginAuth = resolveKimiCodeLoginAuth({ |
| 131 | configuredBaseUrl: auth.baseUrl, |
| 132 | configuredOAuthRef: auth.oauthRef, |
| 133 | requestedBaseUrl: options.baseUrl, |
| 134 | requestedOAuthHost: options.oauthHost, |
| 135 | }); |
| 136 | const result = await this.toolkit.login(providerName, { |
| 137 | ...options, |
| 138 | baseUrl: loginAuth.baseUrl, |
| 139 | oauthHost: loginAuth.oauthHost, |
| 140 | oauthRef: options.oauthRef ?? loginAuth.oauthRef, |
| 141 | provisionConfig: true, |
| 142 | }); |
| 143 | if (result.provision === undefined) { |
| 144 | throw new Error('Kimi auth login did not provision model config.'); |
| 145 | } |
| 146 | const updated = readConfigFile(this.options.configPath); |
| 147 | this.options.onConfigUpdated?.(updated); |
| 148 | return { |
| 149 | providerName: result.providerName, |
| 150 | ok: true, |
| 151 | defaultModel: result.provision.defaultModel, |
| 152 | defaultThinking: result.provision.defaultThinking, |
| 153 | configPath: result.provision.configPath, |
| 154 | }; |
| 155 | } |
| 156 |
nothing calls this directly
no test coverage detected