login-flow.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import type { TuiSelection, TuiSubmitPrompt } from "@zcode/tui";
  2. import { getZCodeCopy } from "@zcode/i18n";
  3. import type { CommandCenterApp, CommandCenterLoginResult } from "./types.js";
  4. import { randomUUID } from "node:crypto";
  5. export function buildLoginSelection(locale?: string): TuiSelection {
  6. const copy = getZCodeCopy(locale).tui.loginSetup;
  7. return {
  8. emptyMessage: copy.emptyMessage,
  9. filterable: false,
  10. help: copy.help,
  11. items: [
  12. {
  13. command: "/login zai-coding-plan",
  14. id: "zai-coding-plan",
  15. keywords: ["zai", "oauth", "coding", "plan"],
  16. pending: {
  17. cancelStatus: copy.pending.cancelStatus,
  18. help: copy.pending.help,
  19. primary: copy.options.zaiOauth.pendingPrimary,
  20. secondary: copy.options.zaiOauth.pendingSecondary,
  21. status: copy.pending.status,
  22. },
  23. primary: copy.options.zaiOauth.primary,
  24. secondary: copy.options.zaiOauth.secondary,
  25. },
  26. {
  27. command: "/login bigmodel-coding-plan",
  28. id: "bigmodel-coding-plan",
  29. keywords: ["bigmodel", "oauth", "coding", "plan"],
  30. pending: {
  31. cancelStatus: copy.pending.cancelStatus,
  32. help: copy.pending.help,
  33. primary: copy.options.bigmodelOauth.pendingPrimary,
  34. secondary: copy.options.bigmodelOauth.pendingSecondary,
  35. status: copy.pending.status,
  36. },
  37. primary: copy.options.bigmodelOauth.primary,
  38. secondary: copy.options.bigmodelOauth.secondary,
  39. },
  40. {
  41. command: "/login zai-coding-plan-api-key",
  42. id: "zai-coding-plan-api-key",
  43. input: {
  44. cancelStatus: copy.input.cancelStatus,
  45. clearStatus: copy.input.clearStatus,
  46. emptyStatus: copy.input.emptyStatus,
  47. help: copy.input.help,
  48. mask: true,
  49. placeholder: copy.input.placeholder,
  50. primary: copy.options.zaiApiKey.inputPrimary,
  51. secondary: copy.options.zaiApiKey.inputSecondary,
  52. status: copy.input.status,
  53. submitStatus: copy.input.submitStatus,
  54. },
  55. keywords: ["zai", "api", "key", "manual"],
  56. primary: copy.options.zaiApiKey.primary,
  57. secondary: copy.options.zaiApiKey.secondary,
  58. },
  59. {
  60. command: "/login bigmodel-coding-plan-api-key",
  61. id: "bigmodel-coding-plan-api-key",
  62. input: {
  63. cancelStatus: copy.input.cancelStatus,
  64. clearStatus: copy.input.clearStatus,
  65. emptyStatus: copy.input.emptyStatus,
  66. help: copy.input.help,
  67. mask: true,
  68. placeholder: copy.input.placeholder,
  69. primary: copy.options.bigmodelApiKey.inputPrimary,
  70. secondary: copy.options.bigmodelApiKey.inputSecondary,
  71. status: copy.input.status,
  72. submitStatus: copy.input.submitStatus,
  73. },
  74. keywords: ["bigmodel", "api", "key", "manual"],
  75. primary: copy.options.bigmodelApiKey.primary,
  76. secondary: copy.options.bigmodelApiKey.secondary,
  77. },
  78. ],
  79. prompt: copy.prompt,
  80. title: copy.title,
  81. };
  82. }
  83. export function loginSetupResponse(locale?: string): string {
  84. return getZCodeCopy(locale).tui.loginSetup.response;
  85. }
  86. export function formatLoginResult(result: CommandCenterLoginResult): string {
  87. const label = result.user.name || result.user.email || result.user.user_id;
  88. const browserNote =
  89. result.browser && !result.browser.opened
  90. ? `\nBrowser open failed: ${result.browser.reason ?? "unknown error"}`
  91. : "";
  92. return [
  93. `Configured Z.AI Coding Plan as ${label}.`,
  94. `Model: ${result.model}`,
  95. `Credentials: ${result.credentialsPath}`,
  96. `Model selection: ${result.configPath}${browserNote}`,
  97. ].join("\n");
  98. }
  99. export function formatProviderSetupResult(result: {
  100. configPath: string;
  101. model: string;
  102. providerId: "bigmodel" | "zai";
  103. }): string {
  104. const provider = result.providerId === "bigmodel" ? "BigModel" : "Z.AI";
  105. return [
  106. `Configured ${provider} Coding Plan.`,
  107. `Model: ${result.model}`,
  108. `Model selection: ${result.configPath}`,
  109. ].join("\n");
  110. }
  111. export async function emitLoginAuthorizeMessage(
  112. options: Parameters<TuiSubmitPrompt>[1],
  113. authorizeUrl: string,
  114. providerName: string,
  115. session: Pick<CommandCenterApp, "sessionId" | "traceId">,
  116. ): Promise<void> {
  117. const onEvent = options.onEvent;
  118. if (!onEvent) return;
  119. await onEvent({
  120. id: `local-login-authorize-${randomUUID()}` as never,
  121. payload: {
  122. content: [
  123. `Open this URL to sign in with ${providerName}:`,
  124. "",
  125. authorizeUrl,
  126. "",
  127. "After authorization, return here and I will finish the login automatically.",
  128. ].join("\n"),
  129. },
  130. sequenceNumber: 0,
  131. sessionId: session.sessionId as never,
  132. timestamp: new Date(),
  133. traceId: session.traceId as never,
  134. type: "assistant_message" as never,
  135. });
  136. }
  137. export function parseApiKeyLoginArgs(args: string): {
  138. apiKey: string;
  139. kind: "bigmodel-coding-plan-api-key" | "zai-coding-plan-api-key";
  140. providerId: "bigmodel" | "zai";
  141. } | null {
  142. const [kind, ...rest] = args.split(/\s+/u);
  143. if (kind !== "zai-coding-plan-api-key" && kind !== "bigmodel-coding-plan-api-key") {
  144. return null;
  145. }
  146. return {
  147. apiKey: rest.join(" ").trim(),
  148. kind,
  149. providerId: kind.startsWith("bigmodel") ? "bigmodel" : "zai",
  150. };
  151. }