File size: 4,467 Bytes
68d7816 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import type {
AuthManagedUsageResult,
AuthManagedUserInfoResult,
BearerTokenProvider,
KimiOAuthLoginOptions,
KimiOAuthLoginResult,
KimiOAuthLogoutResult,
KimiOAuthTokenRef,
KimiRegion,
} from '@moonshot-ai/kimi-code-oauth';
import { createDecorator, type ServiceIdentifier } from '#/_base/di/instantiation';
import { Error2 } from '#/_base/errors/errors';
import type { OAuthRef } from '#/llm-adapter/provider/provider';
import { AuthErrors } from './errors';
import type {
OAuthFlowSnapshot,
OAuthFlowStart,
OAuthLoginCancelResponse,
OAuthLogoutResponse,
RefreshOAuthProviderModelsResponse,
} from './oauthProtocol';
export interface AuthStatus {
readonly loggedIn: boolean;
readonly provider?: string;
}
export interface OAuthLoginOptions {
readonly region?: KimiRegion;
}
export interface IOAuthService {
readonly _serviceBrand: undefined;
startLogin(provider?: string, options?: OAuthLoginOptions): Promise<OAuthFlowStart>;
getFlow(provider?: string): OAuthFlowSnapshot | undefined;
cancelLogin(provider?: string): Promise<OAuthLoginCancelResponse>;
logout(provider?: string): Promise<OAuthLogoutResponse>;
status(provider?: string): Promise<AuthStatus>;
refreshOAuthProviderModels(): Promise<RefreshOAuthProviderModelsResponse>;
getManagedUsage(provider?: string): Promise<AuthManagedUsageResult>;
getManagedUserInfo(provider?: string): Promise<AuthManagedUserInfoResult>;
resolveTokenProvider(provider: string, oauthRef?: OAuthRef): BearerTokenProvider | undefined;
getCachedAccessToken(provider: string, oauthRef?: OAuthRef): Promise<string | undefined>;
getRegion(): KimiRegion;
}
export const IOAuthService: ServiceIdentifier<IOAuthService> =
createDecorator<IOAuthService>('oauthService');
export interface IOAuthToolkit {
readonly _serviceBrand: undefined;
login(providerName?: string, options?: KimiOAuthLoginOptions): Promise<KimiOAuthLoginResult>;
logout(providerName?: string, oauthRef?: KimiOAuthTokenRef): Promise<KimiOAuthLogoutResult>;
getCachedAccessToken(
providerName?: string,
oauthRef?: KimiOAuthTokenRef,
): Promise<string | undefined>;
tokenProvider(providerName?: string, oauthRef?: KimiOAuthTokenRef): BearerTokenProvider;
getManagedUsage(
providerName?: string,
options?: { readonly oauthRef?: KimiOAuthTokenRef; readonly baseUrl?: string },
): Promise<AuthManagedUsageResult>;
getManagedUserInfo(
providerName?: string,
options?: { readonly oauthRef?: KimiOAuthTokenRef; readonly baseUrl?: string },
): Promise<AuthManagedUserInfoResult>;
}
export const IOAuthToolkit: ServiceIdentifier<IOAuthToolkit> =
createDecorator<IOAuthToolkit>('oauthToolkit');
export interface IAuthSummaryService {
readonly _serviceBrand: undefined;
summarize(): Promise<readonly AuthStatus[]>;
ensureReady(modelOverride?: string): Promise<void>;
}
export const IAuthSummaryService: ServiceIdentifier<IAuthSummaryService> =
createDecorator<IAuthSummaryService>('authSummaryService');
export class AuthProvisioningRequiredError extends Error2 {
constructor() {
super(
AuthErrors.codes.AUTH_PROVISIONING_REQUIRED,
'no provider configured; complete onboarding via /login or the providers endpoint',
{ name: 'AuthProvisioningRequiredError' },
);
}
}
export class AuthTokenMissingError extends Error2 {
readonly providerId: string;
constructor(providerId: string) {
super(
AuthErrors.codes.AUTH_TOKEN_MISSING,
`provider ${providerId} has no credential configured`,
{ details: { provider_id: providerId }, name: 'AuthTokenMissingError' },
);
this.providerId = providerId;
}
}
export class AuthModelNotResolvedError extends Error2 {
readonly modelId: string | undefined;
readonly providerId: string | undefined;
constructor(modelId: string | undefined, providerId?: string) {
const details: Record<string, unknown> = {};
if (modelId !== undefined) details['model_id'] = modelId;
if (providerId !== undefined) details['provider_id'] = providerId;
super(
AuthErrors.codes.AUTH_MODEL_NOT_RESOLVED,
modelId === undefined
? 'no default model configured'
: `model ${modelId} does not resolve to a configured provider`,
{
details: Object.keys(details).length === 0 ? undefined : details,
name: 'AuthModelNotResolvedError',
},
);
this.modelId = modelId;
this.providerId = providerId;
}
}
|