File size: 2,125 Bytes
f59fbe2 | 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 | import * as vscode from "vscode";
import { OAuthAccessDeniedError } from "@moonshot-ai/kimi-code-oauth";
import { Events, Methods } from "../../shared/bridge";
import type { LoginResult } from "../../shared/legacy-sdk";
import type { LoginStatus } from "../../shared/types";
import { updateLoginContext } from "../utils/context";
import type { Handler } from "./types";
export const authHandlers: Record<string, Handler<any, any>> = {
[Methods.CheckLoginStatus]: async (_, ctx): Promise<LoginStatus> => {
return { loggedIn: await updateLoginContext(ctx.harness) };
},
[Methods.Login]: async (_, ctx): Promise<LoginResult> => {
try {
await ctx.harness.auth.login(undefined, {
onDeviceCode: async (authorization) => {
const url = authorization.verificationUriComplete || authorization.verificationUri;
ctx.broadcast(Events.LoginUrl, { url }, ctx.webviewId);
await vscode.env.openExternal(vscode.Uri.parse(url));
},
});
await updateLoginContext(ctx.harness);
return { success: true };
} catch (error) {
ctx.logError("Kimi login failed", error);
await updateLoginContext(ctx.harness).catch((statusError: unknown) => {
ctx.logError("Unable to refresh login status after a failed login", statusError);
});
const message = error instanceof Error ? error.message : String(error);
return {
success: false,
error: error instanceof OAuthAccessDeniedError ? `Login cancelled: ${message}` : message,
};
}
},
[Methods.Logout]: async (_, ctx): Promise<LoginResult> => {
try {
await ctx.harness.auth.logout();
await updateLoginContext(ctx.harness);
return { success: true };
} catch (error) {
ctx.logError("Kimi logout failed", error);
await updateLoginContext(ctx.harness).catch((statusError: unknown) => {
ctx.logError("Unable to refresh login status after a failed logout", statusError);
});
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
},
};
|