@hcmai/sdk 0.15.10 → 0.15.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -98,6 +98,7 @@ var CliErrorCode = /* @__PURE__ */ ((CliErrorCode2) => {
98
98
  CliErrorCode2["IDENTITY_NOT_FOUND"] = "IDENTITY_NOT_FOUND";
99
99
  CliErrorCode2["IDENTITY_ALREADY_EXISTS"] = "IDENTITY_ALREADY_EXISTS";
100
100
  CliErrorCode2["IDENTITY_NOT_IN_ENV"] = "IDENTITY_NOT_IN_ENV";
101
+ CliErrorCode2["IDENTITY_META_INCOMPLETE"] = "IDENTITY_META_INCOMPLETE";
101
102
  CliErrorCode2["TOKEN_EXPIRED"] = "TOKEN_EXPIRED";
102
103
  CliErrorCode2["NO_ACTIVE_ENV"] = "NO_ACTIVE_ENV";
103
104
  CliErrorCode2["SKILL_ID_CONFLICT"] = "SKILL_ID_CONFLICT";
@@ -136,6 +137,7 @@ var CODE_TO_EXIT = {
136
137
  ["IDENTITY_NOT_FOUND" /* IDENTITY_NOT_FOUND */]: 5 /* CONFIG_ERROR */,
137
138
  ["IDENTITY_ALREADY_EXISTS" /* IDENTITY_ALREADY_EXISTS */]: 5 /* CONFIG_ERROR */,
138
139
  ["IDENTITY_NOT_IN_ENV" /* IDENTITY_NOT_IN_ENV */]: 5 /* CONFIG_ERROR */,
140
+ ["IDENTITY_META_INCOMPLETE" /* IDENTITY_META_INCOMPLETE */]: 5 /* CONFIG_ERROR */,
139
141
  ["TOKEN_EXPIRED" /* TOKEN_EXPIRED */]: 2 /* AUTH_ERROR */,
140
142
  ["NO_ACTIVE_ENV" /* NO_ACTIVE_ENV */]: 5 /* CONFIG_ERROR */,
141
143
  ["SKILL_ID_CONFLICT" /* SKILL_ID_CONFLICT */]: 3 /* BUSINESS_ERROR */,
@@ -147,6 +149,31 @@ function exitCodeFor(code) {
147
149
  return CODE_TO_EXIT[code] ?? 1 /* USAGE_ERROR */;
148
150
  }
149
151
 
152
+ // src/http/endpoint-url.ts
153
+ var REJECTED_BEFORE_ROUTING_CODE = "REQUEST_REJECTED_BEFORE_ROUTING";
154
+ function normalizeEndpoint(raw) {
155
+ const input = raw ?? "";
156
+ const normalized = input.trim().replace(/\/+$/, "");
157
+ return { normalized, changed: normalized !== input, raw: input };
158
+ }
159
+ function diagnoseRejectedBeforeRouting(requestUrl, baseUrl) {
160
+ const pathPart = doubleSlashPath(requestUrl, baseUrl);
161
+ if (!pathPart) return void 0;
162
+ const base = normalizeEndpoint(baseUrl);
163
+ const fix = base.changed ? `\u6539\u6389\u518D\u8BD5: hcm env edit endpoint ${base.normalized}` : `\u6838\u4E00\u773C\u5F53\u524D env \u7684 endpoint \u4E0E frontend: hcm env show`;
164
+ return `\u8FD9\u6B21\u8BF7\u6C42\u7684\u8DEF\u5F84\u662F '${pathPart}'\uFF0C\u91CC\u9762\u7684 '//' \u4F1A\u88AB\u5BB9\u5668\u5728\u8FDB\u5165\u8DEF\u7531\u4E4B\u524D\u62D2\u6389 \u2014\u2014 \u6700\u5E38\u89C1\u7684\u6210\u56E0\u662F env \u7684 endpoint \u672B\u5C3E\u591A\u4E86\u4E00\u4E2A '/'\u3002${fix}`;
165
+ }
166
+ function doubleSlashPath(requestUrl, baseUrl) {
167
+ if (!requestUrl) return void 0;
168
+ let pathname;
169
+ try {
170
+ pathname = new URL(requestUrl, baseUrl ?? void 0).pathname;
171
+ } catch {
172
+ return void 0;
173
+ }
174
+ return pathname.includes("//") ? pathname : void 0;
175
+ }
176
+
150
177
  // src/errors/cli-error.ts
151
178
  var CliError = class extends Error {
152
179
  code;
@@ -187,6 +214,9 @@ var CliError = class extends Error {
187
214
  if (causeText) out += `
188
215
  \u539F\u56E0: ${causeText}`;
189
216
  }
217
+ const localDiagnosis = this.context?.localDiagnosis;
218
+ if (typeof localDiagnosis === "string" && localDiagnosis) out += `
219
+ \u8BCA\u65AD: ${localDiagnosis}`;
190
220
  const nextStep = this.context?.nextStep;
191
221
  if (typeof nextStep === "string" && nextStep) out += `
192
222
  \u4E0B\u4E00\u6B65: ${nextStep}`;
@@ -210,8 +240,9 @@ function fromAxiosError(err, profile) {
210
240
  const respMsg = body?.message;
211
241
  const respField = body?.field;
212
242
  const respDetails = body?.details;
213
- const respNextStep = body?.nextStep;
243
+ const respNextStep = body?.next_step ?? body?.nextStep;
214
244
  const respRetryable = body?.retryable;
245
+ const localDiagnosis = respCode === REJECTED_BEFORE_ROUTING_CODE ? diagnoseRejectedBeforeRouting(err.config?.url, err.config?.baseURL) : void 0;
215
246
  let code;
216
247
  if (status === 401) code = "AUTH_FAILED" /* AUTH_FAILED */;
217
248
  else if (status === 403) code = "SCOPE_FORBIDDEN" /* SCOPE_FORBIDDEN */;
@@ -232,7 +263,9 @@ function fromAxiosError(err, profile) {
232
263
  ...respField ? { field: respField } : {},
233
264
  ...respDetails !== void 0 ? { details: respDetails } : {},
234
265
  ...respNextStep ? { nextStep: respNextStep } : {},
235
- ...respRetryable !== void 0 ? { retryable: respRetryable } : {}
266
+ ...respRetryable !== void 0 ? { retryable: respRetryable } : {},
267
+ // 与后端的 nextStep 并存、不互相覆盖:一个是服务端的通用路标,一个是本地对症结论。
268
+ ...localDiagnosis ? { localDiagnosis } : {}
236
269
  },
237
270
  traceId,
238
271
  profile,
@@ -440,6 +473,55 @@ var manifest = JSON.parse(
440
473
  );
441
474
  var SDK_VERSION = manifest.version;
442
475
 
476
+ // src/http/endpoint.ts
477
+ function assertUsableEndpoint(endpoint, field = "endpoint") {
478
+ const fail2 = (message, nextStep) => {
479
+ throw new CliError({
480
+ code: "INVALID_ARGUMENT" /* INVALID_ARGUMENT */,
481
+ message,
482
+ context: { field, nextStep }
483
+ });
484
+ };
485
+ if (!endpoint) {
486
+ return fail2(
487
+ `${field} \u4E0D\u80FD\u4E3A\u7A7A\u3002`,
488
+ "\u5F62\u5982 https://hcm.example.com\uFF08\u53EF\u5E26\u90E8\u7F72\u524D\u7F00\uFF0C\u5982 https://host/hcm\uFF09"
489
+ );
490
+ }
491
+ let url;
492
+ try {
493
+ url = new URL(endpoint);
494
+ } catch {
495
+ return fail2(
496
+ `${field} '${endpoint}' \u4E0D\u662F\u5408\u6CD5\u7684 URL\u3002`,
497
+ "\u8981\u5E26\u534F\u8BAE\u5934\uFF0C\u5F62\u5982 https://hcm.example.com \u2014\u2014 \u53EA\u5199\u4E3B\u673A\u540D\uFF08hcm.example.com\uFF09\u89E3\u6790\u4E0D\u4E86"
498
+ );
499
+ }
500
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
501
+ return fail2(
502
+ `${field} '${endpoint}' \u7684\u534F\u8BAE\u662F ${url.protocol.replace(":", "")}\uFF0C\u53EA\u652F\u6301 http / https\u3002`,
503
+ "\u6539\u6210 https://\u2026 \u6216 http://\u2026"
504
+ );
505
+ }
506
+ if (url.search) {
507
+ return fail2(
508
+ `${field} '${endpoint}' \u5E26\u4E86\u67E5\u8BE2\u4E32 '${url.search}'\uFF0C\u57FA\u5740\u4E0D\u80FD\u5E26\u67E5\u8BE2\u4E32\u3002`,
509
+ `\u53BB\u6389 '${url.search}' \u540E\u91CD\u8BD5`
510
+ );
511
+ }
512
+ if (url.hash) {
513
+ return fail2(
514
+ `${field} '${endpoint}' \u5E26\u4E86\u951A\u70B9 '${url.hash}'\uFF0C\u57FA\u5740\u4E0D\u80FD\u5E26\u951A\u70B9\u3002`,
515
+ `\u53BB\u6389 '${url.hash}' \u540E\u91CD\u8BD5`
516
+ );
517
+ }
518
+ }
519
+ function normalizeAndAssertEndpoint(raw, field = "endpoint") {
520
+ const result = normalizeEndpoint(raw);
521
+ assertUsableEndpoint(result.normalized, field);
522
+ return result;
523
+ }
524
+
443
525
  // src/http/client.ts
444
526
  var DEFAULT_HTTP_TIMEOUT_MS = 6e4;
445
527
  var HCM_LANG_ENV = "HCM_LANG";
@@ -452,7 +534,10 @@ function toAcceptLanguage(language) {
452
534
  function createHttpClient(opts) {
453
535
  const acceptLanguage = toAcceptLanguage(opts.language ?? process.env[HCM_LANG_ENV]);
454
536
  const client = axios.create({
455
- baseURL: opts.endpoint,
537
+ // 走相对路径时 axios 的 combineURLs 自己会吃掉尾斜杠,这里归一是为了让
538
+ // `danger-env` 那条 `new URL(request.baseURL!)` 以及任何读 baseURL 的地方
539
+ // 看到的都是同一个规范形态 —— 判据不该因为用户多打了一个 `/` 而分岔。
540
+ baseURL: normalizeEndpoint(opts.endpoint).normalized,
456
541
  timeout: opts.timeoutMs ?? DEFAULT_HTTP_TIMEOUT_MS,
457
542
  headers: {
458
543
  "User-Agent": opts.userAgent ?? `hcmai-sdk/${SDK_VERSION}`,
@@ -545,6 +630,11 @@ function assertNotReservedEnvName(name) {
545
630
  });
546
631
  }
547
632
  }
633
+ function normalizeEnvAddresses(cfg) {
634
+ const out = { ...cfg, endpoint: normalizeEndpoint(cfg.endpoint).normalized };
635
+ if (cfg.frontend !== void 0) out.frontend = normalizeEndpoint(cfg.frontend).normalized;
636
+ return out;
637
+ }
548
638
  async function saveEnv(name, cfg) {
549
639
  if (!cfg.endpoint) {
550
640
  throw new CliError({
@@ -553,12 +643,12 @@ async function saveEnv(name, cfg) {
553
643
  });
554
644
  }
555
645
  await fs.mkdir(envDir(name), { recursive: true });
556
- await fs.writeFile(envFile(name), yaml.dump(cfg), "utf-8");
646
+ await fs.writeFile(envFile(name), yaml.dump(normalizeEnvAddresses(cfg)), "utf-8");
557
647
  }
558
648
  async function loadEnv(name) {
559
649
  try {
560
650
  const content = await fs.readFile(envFile(name), "utf-8");
561
- return yaml.load(content);
651
+ return normalizeEnvAddresses(yaml.load(content));
562
652
  } catch (e) {
563
653
  if (e.code === "ENOENT") {
564
654
  throw new CliError({
@@ -1971,12 +2061,12 @@ var HcmClient = class _HcmClient {
1971
2061
  closeEphemeralSession;
1972
2062
  constructor(opts) {
1973
2063
  this.profile = opts.profile ?? "default";
1974
- this.endpoint = opts.endpoint;
2064
+ this.endpoint = normalizeEndpoint(opts.endpoint).normalized;
1975
2065
  this.tenantId = opts.tenantId;
1976
2066
  this.tokenStore = new TokenStore(this.profile);
1977
2067
  this.explicitToken = opts.accessToken;
1978
2068
  this.http = createHttpClient({
1979
- endpoint: opts.endpoint,
2069
+ endpoint: this.endpoint,
1980
2070
  timeoutMs: opts.timeoutMs,
1981
2071
  environmentSafety: opts.environmentSafety,
1982
2072
  getAccessToken: () => this.getAccessToken(),
@@ -2408,6 +2498,7 @@ import axios2 from "axios";
2408
2498
  import { setTimeout as delay } from "timers/promises";
2409
2499
  var CLI_AUTHORIZE_PATH = "/api/auth/cli/authorize";
2410
2500
  var CLI_EXCHANGE_PATH = "/api/auth/cli/exchange";
2501
+ var CLI_STATUS_PATH = "/api/auth/cli/status";
2411
2502
  var CLI_VERIFICATION_PATH = "/cli/authorize";
2412
2503
  function protocol(message) {
2413
2504
  return new CliError({
@@ -2416,11 +2507,57 @@ function protocol(message) {
2416
2507
  context: { nextStep: "\u68C0\u67E5\u540E\u7AEF CLI \u6388\u6743\u63A5\u53E3\u7248\u672C\uFF1B\u672C\u6B21\u6CA1\u6709\u4FDD\u5B58\u767B\u5F55\u51ED\u636E\u3002" }
2417
2508
  });
2418
2509
  }
2419
- function cliAuthorizationExpired() {
2510
+ function diagnoseExpiry(lastRejection, probe) {
2511
+ if (probe?.lastStatus === "DENIED") return "denied";
2512
+ if (probe?.lastStatus === "APPROVED") return "approved";
2513
+ if (lastRejection) return "blocked";
2514
+ if (probe && !probe.supported) return "unsupported";
2515
+ if (probe && probe.answeredAt === void 0) return "unreachable";
2516
+ if (probe?.answeredAt !== void 0 && probe.deadline - probe.answeredAt > BROWSER_STATUS_INTERVAL_MS) return "stale";
2517
+ return "quiet";
2518
+ }
2519
+ var expiryCopy = {
2520
+ denied: {
2521
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\u3002\u6388\u6743\u9875\u4E0A\u8FD9\u6B21\u767B\u5F55\u88AB\u62D2\u7EDD\u4E86\uFF0C\u6240\u4EE5\u8FD9\u53F0\u673A\u5668\u4E00\u76F4\u6CA1\u7B49\u5230\u56DE\u8DF3\u3002",
2522
+ nextStep: "\u5982\u679C\u4E0D\u662F\u4F60\u672C\u4EBA\u70B9\u7684\u62D2\u7EDD\uFF0C\u8BF4\u660E\u8FD9\u6761\u6388\u6743\u94FE\u63A5\u843D\u5230\u4E86\u522B\u4EBA\u624B\u91CC\uFF1B\u91CD\u65B0\u8FD0\u884C hcm login \u4F1A\u6362\u4E00\u6761\u65B0\u94FE\u63A5\u3002"
2523
+ },
2524
+ approved: {
2525
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\u3002\u6388\u6743\u5728\u6D4F\u89C8\u5668\u4E0A\u5DF2\u7ECF\u6279\u51C6\u4E86\uFF0C\u4F46\u8FD9\u53F0\u673A\u5668\u4E00\u76F4\u6CA1\u6536\u5230\u56DE\u8DF3\u2014\u2014\u6388\u6743\u9875\u591A\u534A\u662F\u5728\u522B\u7684\u673A\u5668\u4E0A\u6253\u5F00\u7684\u3002",
2526
+ nextStep: "\u6388\u6743\u9875\u8981\u5728\u300C\u8FD9\u53F0\u300D\u673A\u5668\u7684\u6D4F\u89C8\u5668\u91CC\u6253\u5F00\uFF1B\u8FDC\u7A0B/\u65E0\u6D4F\u89C8\u5668\u7EC8\u7AEF\u8BF7\u6539\u7528 hcm login --device-auth\u3002"
2527
+ },
2528
+ blocked: {
2529
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\uFF1B\u671F\u95F4\u6709\u4EBA\u6253\u5F00\u8FC7\u6388\u6743\u94FE\u63A5\uFF0C\u4F46\u6BCF\u6B21\u90FD\u88AB\u6321\u4E0B\uFF0C\u672C\u6B21\u6CA1\u6709\u5B8C\u6210\u767B\u5F55\u3002",
2530
+ nextStep: "\u5148\u5904\u7406\u4E0A\u9762\u90A3\u6761\u62D2\u7EDD\u539F\u56E0\uFF0C\u518D\u91CD\u65B0\u8FD0\u884C hcm login\u3002"
2531
+ },
2532
+ unsupported: {
2533
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\uFF0C\u672C\u6B21\u6CA1\u6709\u5B8C\u6210\u767B\u5F55\u3002\u8FD9\u4E2A\u540E\u7AEF\u8FD8\u4E0D\u652F\u6301\u5728\u7B49\u5F85\u671F\u95F4\u67E5\u770B\u88AB\u6321\u539F\u56E0\uFF0C\u6240\u4EE5\u8FD9\u4E00\u5934\u770B\u4E0D\u51FA\u6709\u6CA1\u6709\u4EBA\u6253\u5F00\u8FC7\u94FE\u63A5\u3002",
2534
+ nextStep: "\u5347\u7EA7\u670D\u52A1\u7AEF\u540E\uFF0C\u7B49\u5F85\u671F\u95F4\u5C31\u80FD\u770B\u5230\u88AB\u6321\u539F\u56E0\uFF1B\u672C\u6B21\u8BF7\u91CD\u65B0\u8FD0\u884C hcm login\u3002"
2535
+ },
2536
+ unreachable: {
2537
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\uFF0C\u672C\u6B21\u6CA1\u6709\u5B8C\u6210\u767B\u5F55\u3002\u7B49\u5F85\u671F\u95F4\u8FD9\u4E00\u5934\u4E00\u6B21\u90FD\u6CA1\u95EE\u901A\u72B6\u6001\u63A5\u53E3\uFF0C\u6240\u4EE5\u770B\u4E0D\u51FA\u6709\u6CA1\u6709\u4EBA\u6253\u5F00\u8FC7\u94FE\u63A5\u3002",
2538
+ nextStep: "\u672C\u6B21\u8BF7\u91CD\u65B0\u8FD0\u884C hcm login\uFF1B\u82E5\u53CD\u590D\u5982\u6B64\uFF0C\u591A\u534A\u662F\u7F51\u7EDC\u4E0D\u901A\u6216\u670D\u52A1\u7AEF\u9650\u6D41\u6321\u4F4F\u4E86\u72B6\u6001\u67E5\u8BE2\u3002"
2539
+ },
2540
+ stale: {
2541
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\uFF0C\u672C\u6B21\u6CA1\u6709\u5B8C\u6210\u767B\u5F55\u3002\u72B6\u6001\u67E5\u8BE2\u5728\u6700\u540E\u4E00\u6BB5\u65F6\u95F4\u91CC\u6CA1\u80FD\u95EE\u901A\uFF0C\u90A3\u6BB5\u65F6\u95F4\u91CC\u53D1\u751F\u4E86\u4EC0\u4E48\u8FD9\u4E00\u5934\u770B\u4E0D\u5230\u3002",
2542
+ nextStep: "\u672C\u6B21\u8BF7\u91CD\u65B0\u8FD0\u884C hcm login\uFF1B\u82E5\u53CD\u590D\u5982\u6B64\uFF0C\u591A\u534A\u662F\u670D\u52A1\u7AEF\u9650\u6D41\u6321\u4F4F\u4E86\u72B6\u6001\u67E5\u8BE2\u3002"
2543
+ },
2544
+ quiet: {
2545
+ message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\uFF0C\u672C\u6B21\u6CA1\u6709\u5B8C\u6210\u767B\u5F55\u3002",
2546
+ nextStep: "\u91CD\u65B0\u8FD0\u884C hcm login \u53D1\u8D77\u65B0\u6388\u6743\u3002"
2547
+ }
2548
+ };
2549
+ function cliAuthorizationExpired(lastRejection, probe) {
2550
+ const diagnosis = diagnoseExpiry(lastRejection, probe);
2551
+ const copy = expiryCopy[diagnosis];
2420
2552
  return new CliError({
2421
2553
  code: "AUTH_EXPIRED" /* AUTH_EXPIRED */,
2422
- message: "\u767B\u5F55\u6388\u6743\u7B49\u5F85\u5DF2\u5230\u671F\uFF0C\u672C\u6B21\u6CA1\u6709\u5B8C\u6210\u767B\u5F55\u3002",
2423
- context: { nextStep: "\u91CD\u65B0\u8FD0\u884C hcm login \u53D1\u8D77\u65B0\u6388\u6743\u3002" }
2554
+ message: copy.message,
2555
+ context: {
2556
+ ...lastRejection ? { lastRejection } : {},
2557
+ ...diagnosis === "denied" || diagnosis === "approved" ? { lastStatus: probe?.lastStatus } : {},
2558
+ ...diagnosis === "unsupported" || diagnosis === "unreachable" || diagnosis === "stale" ? { statusProbe: diagnosis } : {},
2559
+ nextStep: copy.nextStep
2560
+ }
2424
2561
  });
2425
2562
  }
2426
2563
  function checkCancelled(signal) {
@@ -2454,6 +2591,7 @@ function safeCliAuthorizationError(error) {
2454
2591
  const unsupported = known === void 0 && unsupportedBackend(status);
2455
2592
  const code = unsupported ? "PROTOCOL_UNSUPPORTED" /* PROTOCOL_UNSUPPORTED */ : timedOut ? "TIMEOUT" /* TIMEOUT */ : status === 401 ? "AUTH_FAILED" /* AUTH_FAILED */ : status === 403 ? "SCOPE_FORBIDDEN" /* SCOPE_FORBIDDEN */ : status && status >= 500 ? "SERVER_ERROR" /* SERVER_ERROR */ : status ? "AUTH_FAILED" /* AUTH_FAILED */ : "NETWORK_ERROR" /* NETWORK_ERROR */;
2456
2593
  const interval = error.response?.data?.error?.details?.interval;
2594
+ const lastRejection = rejectionOf(error.response?.data?.error?.details);
2457
2595
  return new CliError({
2458
2596
  code,
2459
2597
  message: unsupported ? "\u540E\u7AEF\u5C1A\u4E0D\u652F\u6301\u6D4F\u89C8\u5668/\u8BBE\u5907\u7801\u767B\u5F55\uFF0C\u9700\u8981\u5347\u7EA7\u670D\u52A1\u7AEF\u3002" : timedOut ? "\u767B\u5F55\u6388\u6743\u8BF7\u6C42\u8D85\u65F6\uFF0C\u5B8C\u6210\u72B6\u6001\u672A\u786E\u8BA4\u3002" : known ? errorMessages[known] : status === 401 ? "\u6388\u6743\u670D\u52A1\u8981\u6C42\u91CD\u65B0\u767B\u5F55\u3002" : "\u670D\u52A1\u7AEF\u8FD4\u56DE\u672A\u77E5\u6388\u6743\u9519\u8BEF\uFF0C\u5DF2\u505C\u6B62\u767B\u5F55\u3002",
@@ -2461,6 +2599,7 @@ function safeCliAuthorizationError(error) {
2461
2599
  ...status !== void 0 ? { status } : {},
2462
2600
  responseCode: known ?? "unrecognized_response",
2463
2601
  ...known === "slow_down" && typeof interval === "number" ? { interval } : {},
2602
+ ...lastRejection ? { lastRejection } : {},
2464
2603
  ...timedOut ? { requestTimeout: true } : {},
2465
2604
  nextStep: unsupported ? "\u5347\u7EA7\u540E\u7AEF\u4E0E\u524D\u7AEF\uFF1B\u65E7\u7248\u53EF\u5728\u7F51\u9875\u201C\u547D\u4EE4\u884C\u5DE5\u5177\u201D\u751F\u6210\u914D\u5BF9\u7801\uFF0C\u518D\u8FD0\u884C hcm login --code <\u914D\u5BF9\u7801>\u3002" : known === "authorization_pending" || known === "slow_down" ? "\u7B49\u5F85\u540C\u4E00\u6388\u6743\u5B8C\u6210\u3002" : "\u6838\u5BF9\u6388\u6743\u72B6\u6001\u540E\u91CD\u65B0\u8FD0\u884C hcm login\uFF1B\u672C\u6B21\u4E0D\u4F1A\u81EA\u52A8\u91CD\u65B0\u53D1\u8D77\u6388\u6743\u3002"
2466
2605
  }
@@ -2472,6 +2611,11 @@ function positive(value) {
2472
2611
  function text(value) {
2473
2612
  return typeof value === "string" && value.length > 0;
2474
2613
  }
2614
+ function rejectionOf(details) {
2615
+ const raw = details?.lastRejection;
2616
+ if (!raw || typeof raw !== "object" || !text(raw.reason) || !/^[a-z_]{1,64}$/.test(raw.reason) || !positive(raw.at)) return void 0;
2617
+ return { reason: raw.reason, at: raw.at };
2618
+ }
2475
2619
  function validateCliAuthorizationUrl(raw, label) {
2476
2620
  let url;
2477
2621
  try {
@@ -2613,16 +2757,70 @@ async function exchangeBrowserAuthorization(input) {
2613
2757
  }, Math.min(3e4, remaining));
2614
2758
  return readLoginResponse(input, data, "browser");
2615
2759
  }
2760
+ var BROWSER_STATUS_INTERVAL_MS = 5e3;
2761
+ var BROWSER_STATUS_MAX_INTERVAL_MS = 6e4;
2762
+ function watchBrowserAuthorization(input) {
2763
+ let stopped = false;
2764
+ let last;
2765
+ let interval = BROWSER_STATUS_INTERVAL_MS;
2766
+ let supported = true;
2767
+ let answeredAt;
2768
+ let lastStatus;
2769
+ void (async () => {
2770
+ while (!stopped) {
2771
+ const remaining = input.deadline - Date.now();
2772
+ if (remaining <= 0) return;
2773
+ try {
2774
+ await delay(Math.min(interval, remaining), void 0, { signal: input.signal });
2775
+ } catch {
2776
+ return;
2777
+ }
2778
+ if (stopped || Date.now() >= input.deadline) return;
2779
+ try {
2780
+ const data = await post(
2781
+ input,
2782
+ CLI_STATUS_PATH,
2783
+ { requestId: input.requestId, state: input.state },
2784
+ 1e4
2785
+ );
2786
+ interval = BROWSER_STATUS_INTERVAL_MS;
2787
+ answeredAt = Date.now();
2788
+ if (typeof data?.status === "string") {
2789
+ lastStatus = data.status;
2790
+ }
2791
+ const rejection = rejectionOf(data);
2792
+ if (rejection && (rejection.at !== last?.at || rejection.reason !== last.reason)) {
2793
+ last = rejection;
2794
+ input.onRejection(rejection);
2795
+ }
2796
+ } catch (error) {
2797
+ if (safeCliAuthorizationError(error).code === "PROTOCOL_UNSUPPORTED" /* PROTOCOL_UNSUPPORTED */) {
2798
+ supported = false;
2799
+ return;
2800
+ }
2801
+ interval = Math.min(interval * 2, BROWSER_STATUS_MAX_INTERVAL_MS);
2802
+ }
2803
+ }
2804
+ })();
2805
+ return {
2806
+ stop: () => {
2807
+ stopped = true;
2808
+ },
2809
+ lastRejection: () => last,
2810
+ probe: () => ({ supported, answeredAt, lastStatus, deadline: input.deadline })
2811
+ };
2812
+ }
2616
2813
  async function pollDeviceAuthorization(input) {
2617
2814
  let serverInterval = input.authorization.interval * 1e3;
2618
2815
  let interval = serverInterval;
2816
+ let lastRejection;
2619
2817
  while (Date.now() < input.authorization.deadline) {
2620
2818
  checkCancelled(input.signal);
2621
2819
  try {
2622
2820
  await delay(Math.min(interval, input.authorization.deadline - Date.now()), void 0, { signal: input.signal });
2623
2821
  } catch {
2624
2822
  checkCancelled(input.signal);
2625
- throw cliAuthorizationExpired();
2823
+ throw cliAuthorizationExpired(lastRejection);
2626
2824
  }
2627
2825
  const remaining = input.authorization.deadline - Date.now();
2628
2826
  if (remaining <= 0) break;
@@ -2633,7 +2831,14 @@ async function pollDeviceAuthorization(input) {
2633
2831
  const failure = safeCliAuthorizationError(error);
2634
2832
  const status = failure.context?.status;
2635
2833
  const code = failure.context?.responseCode;
2636
- if (status === 400 && code === "authorization_pending") continue;
2834
+ if (status === 400 && code === "authorization_pending") {
2835
+ const rejection = failure.context?.lastRejection;
2836
+ if (rejection && (rejection.at !== lastRejection?.at || rejection.reason !== lastRejection.reason)) {
2837
+ lastRejection = rejection;
2838
+ input.onRejection?.(rejection);
2839
+ }
2840
+ continue;
2841
+ }
2637
2842
  if (status === 400 && code === "slow_down") {
2638
2843
  const seconds = failure.context?.interval;
2639
2844
  if (!positive(seconds) || seconds > 900 || seconds * 1e3 < serverInterval) throw protocol("slow_down \u672A\u8FD4\u56DE\u6709\u6548\u4E14\u4E0D\u51CF\u901F\u7684 interval\uFF0C\u5DF2\u505C\u6B62\u8F6E\u8BE2\u3002");
@@ -2649,7 +2854,7 @@ async function pollDeviceAuthorization(input) {
2649
2854
  throw failure;
2650
2855
  }
2651
2856
  }
2652
- throw cliAuthorizationExpired();
2857
+ throw cliAuthorizationExpired(lastRejection);
2653
2858
  }
2654
2859
 
2655
2860
  // src/auth/change-password.ts
@@ -22365,6 +22570,7 @@ export {
22365
22570
  CHECK_GROUPS2 as CHECK_GROUPS,
22366
22571
  CLI_AUTHORIZE_PATH,
22367
22572
  CLI_EXCHANGE_PATH,
22573
+ CLI_STATUS_PATH,
22368
22574
  CLI_VERIFICATION_PATH,
22369
22575
  CNB_FAVICON_URL,
22370
22576
  CNB_ORIGIN,
@@ -22497,6 +22703,7 @@ export {
22497
22703
  REASON_BASELINE_ROUND_NOT_COMPLETE,
22498
22704
  VERDICTS2 as RECONCILE_VERDICTS,
22499
22705
  RECOVERY_WORDS,
22706
+ REJECTED_BEFORE_ROUTING_CODE,
22500
22707
  REQUIRED_SECTIONS,
22501
22708
  RESPOND_TIMEOUT_MS,
22502
22709
  RESTORE_CODES,
@@ -22570,6 +22777,7 @@ export {
22570
22777
  assertSafeReferencePath,
22571
22778
  assertSafeSkillFilePath,
22572
22779
  assertSafeSkillId,
22780
+ assertUsableEndpoint,
22573
22781
  baselineRoundPremise,
22574
22782
  bootstrapTenant,
22575
22783
  buildAnswer,
@@ -22622,6 +22830,7 @@ export {
22622
22830
  detectDelegationPause,
22623
22831
  detectDirectInteractionPending,
22624
22832
  detectPasswordChangeChallenge,
22833
+ diagnoseRejectedBeforeRouting,
22625
22834
  discardedColumnLines,
22626
22835
  discoverProjectEnv,
22627
22836
  displayWidth,
@@ -22725,6 +22934,8 @@ export {
22725
22934
  modelKeys,
22726
22935
  needsRefresh,
22727
22936
  newRoundId,
22937
+ normalizeAndAssertEndpoint,
22938
+ normalizeEndpoint,
22728
22939
  normalizeOrigin,
22729
22940
  normalizeReferences,
22730
22941
  notAsked,
@@ -22836,6 +23047,7 @@ export {
22836
23047
  verifyRc,
22837
23048
  verifyText,
22838
23049
  waitForInstallRun,
23050
+ watchBrowserAuthorization,
22839
23051
  workspaceLayer,
22840
23052
  writeChangeState,
22841
23053
  writeImportPlan,