@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
103 lines • 4.61 kB
JavaScript
/**
* Relay a provider OAuth login (`modelRuntime.login`) onto the extension UI of
* a slash command such as `/gpt-account add`, mirroring what the `/login`
* dialog and `modes/rpc/login-prompts.ts` do for their surfaces.
*
* Three decisions are not visible from the code alone. The login owns its own
* `AbortController` rather than borrowing `ctx.signal`: that signal belongs to
* the active run, so binding a login to it made Esc/steer/timeout on the
* response kill a browser login the user never acted on (#1542). The login is
* cancelled only by dismissing one of its own dialogs or by re-issuing the
* command for the same provider. Every dialog is also bound to the per-prompt
* `AuthPrompt.signal`, so a manual-code dialog is released without cancelling
* the login when the provider's local callback server wins the race
* (`loginChatGptSubscription`). `auth_url` opens the browser only in the TUI, because
* an RPC client renders the notice on its own machine.
*/
import { redactOAuthUrl } from "@earendil-works/pi-ai/utils/oauth-url";
import { openBrowser as openPlatformBrowser } from "../../../utils/open-browser.js";
export const LOGIN_CANCELLED_MESSAGE = "Login cancelled";
const pendingLogins = new Map();
function cancelledError() {
return new Error(LOGIN_CANCELLED_MESSAGE);
}
function ownLoginController(providerId) {
const controller = new AbortController();
if (providerId === undefined)
return controller;
pendingLogins.get(providerId)?.abort(cancelledError());
pendingLogins.set(providerId, controller);
controller.signal.addEventListener("abort", () => {
if (pendingLogins.get(providerId) === controller)
pendingLogins.delete(providerId);
}, { once: true });
return controller;
}
export function createExtensionLoginInteraction(ctx, options) {
const openBrowser = options.openBrowser ?? openPlatformBrowser;
const controller = ownLoginController(options.providerId);
return {
signal: controller.signal,
prompt: (prompt) => relayPrompt(ctx, controller, prompt),
notify: (event) => relayEvent(ctx, event, options.providerLabel, openBrowser),
};
}
function dialogSignal(loginSignal, promptSignal) {
return promptSignal ? AbortSignal.any([loginSignal, promptSignal]) : loginSignal;
}
async function relayPrompt(ctx, login, prompt) {
const signal = dialogSignal(login.signal, prompt.signal);
if (signal.aborted)
throw cancelledError();
const answer = await answerPrompt(ctx, prompt, { signal });
if (signal.aborted)
throw cancelledError();
if (answer === undefined) {
// The user dismissed the login's own dialog: that is the one cancellation the login owns.
login.abort(cancelledError());
throw cancelledError();
}
return answer;
}
async function answerPrompt(ctx, prompt, dialogOptions) {
switch (prompt.type) {
case "select": {
const label = await ctx.ui.select(prompt.message, prompt.options.map((option) => option.label), dialogOptions);
return prompt.options.find((option) => option.label === label)?.id;
}
case "text":
case "manual_code":
return ctx.ui.input(prompt.message, prompt.placeholder, dialogOptions);
case "secret":
return ctx.ui.input(prompt.message, prompt.placeholder, ctx.ui.capabilities?.secretInput === true ? { ...dialogOptions, secret: true } : dialogOptions);
}
}
function relayEvent(ctx, event, providerLabel, openBrowser) {
switch (event.type) {
case "auth_url": {
if (ctx.mode === "tui")
openBrowser(event.url);
const lines = [`Open this URL to authorize ${providerLabel}:`, redactOAuthUrl(event.url)];
if (event.instructions)
lines.push(event.instructions);
ctx.ui.notify(lines.join("\n"), "info");
return;
}
case "device_code":
ctx.ui.notify([
`Open this URL to authorize ${providerLabel}:`,
event.verificationUri,
`Enter code: ${event.userCode}`,
].join("\n"), "info");
return;
case "info": {
const links = (event.links ?? []).map((link) => (link.label ? `${link.label}: ${link.url}` : link.url));
ctx.ui.notify([event.message, ...links].join("\n"), "info");
return;
}
case "progress":
ctx.ui.notify(event.message, "info");
return;
}
}
//# sourceMappingURL=oauth-login-interaction.js.map