Adds packages/atomcode (@magpie-community/opencode-atomcode-auth 0.3.0), an AtomGit CodingPlan provider: browser OAuth and AtomCode CLI sign-in (~/.atomcode/auth.toml), signed requests to AtomGit's own gateway, per-account models and quota, daily claim, a loopback-only diagnostics page, and OpenCode 1.x/2.x entries. Registry and README entries added. By LILEOO.
Reviewed at c57b099fff4689c0198c1c6414f04b27614b4092. The last blocking point (an inline TOML comment on a known line went into the uid and was written back) is fixed: values end at an unquoted #, quoted/escaped/literal strings keep their #, and a rewritten line keeps its comment.
Run on a separate test machine (Bun 1.4.2, sandbox HOME):
- PR head merged onto origin/main 24da52b8: bun scripts/check.mjs exit 0 (atomcode ok); whole-repo bun test 6103 pass, 0 fail. origin/main alone: 5942 pass, 0 fail.
- packages/atomcode at the head: bun test 161 pass; node --test test/*.test.mjs 161 pass.
- The new auth-file test against 55ff272's auth-file.mjs: red (1 fail), green at the head.
- Probe: the review's file (id = "123456" # c) parses to uid 123456 and renders back as id = "123456" # c; also #c with no space, CRLF with a tab before #, "1#2", and a literal-string id all round-trip to the same uid. At 55ff272 each gave a uid with the comment in it.
Not tried: a real AtomGit account. A [user] header with a trailing comment is refused ("no user id"), not corrupted; the CLI never writes one. First npm version needs an owner publish; until then magpie installs it from the dist branch.
170 lines
6.2 KiB
JavaScript
170 lines
6.2 KiB
JavaScript
/**
|
|
* One failure vocabulary for provider, quota and diagnostic paths.
|
|
*
|
|
* A 401 means the credential is no longer usable. A 403 is deliberately split:
|
|
* only an explicit token/session refusal may mark the account expired. Plan,
|
|
* quota and permission refusals are upstream answers about the account's
|
|
* entitlement and must not force a refresh or a re-login loop.
|
|
*
|
|
* @module magpie-atomcode/errors
|
|
*/
|
|
|
|
import { redactText } from "./internal.mjs";
|
|
|
|
export const FAILURE = Object.freeze({
|
|
NETWORK: "network",
|
|
TIMEOUT: "timeout",
|
|
UNAUTHORIZED: "unauthorized",
|
|
CREDENTIAL_FORBIDDEN: "credential_forbidden",
|
|
ENTITLEMENT_FORBIDDEN: "entitlement_forbidden",
|
|
FORBIDDEN: "forbidden",
|
|
RATE_LIMIT: "rate_limit",
|
|
SIGNING: "signing",
|
|
VERSION: "version",
|
|
SERVER: "upstream_server",
|
|
CLI_ACCOUNT_CHANGED: "cli_account_changed",
|
|
INELIGIBLE: "ineligible",
|
|
UNKNOWN: "unknown",
|
|
});
|
|
|
|
const CREDENTIAL_WORDS =
|
|
/(?:access|refresh|oauth|bearer|session)[\s_-]*token|token[\s_-]*(?:is[\s_-]*)?(?:expired|invalid|revoked)|(?:invalid|expired|revoked)[\s_-]*token|unauthori[sz]ed|unauthenticated|authentication|credential|sign[\s-]*in|log[\s-]*in|login|令牌|凭证|会话|登录|重新认证/i;
|
|
|
|
const ENTITLEMENT_WORDS =
|
|
/quota|rate[\s_-]*limit|usage[\s_-]*limit|limit[\s_-]*reached|too many requests|balance|credit|billing|subscription|entitlement|coding[\s_-]*plan|plan[\s_-]*(?:expired|unavailable|required)|no tier|not eligible|ineligible|余额|额度|套餐|名额|限额|限流|订阅|权益|档位|领取/i;
|
|
|
|
const VERSION_WORDS =
|
|
/(?:atomcode|client)[\s_-]*(?:version|ua)|user[\s_-]*agent|upgrade|update required|版本|客户端/i;
|
|
|
|
const SIGNING_WORDS =
|
|
/sign(?:ature|ing)|x-atomcode|hmac|master[\s_-]*key|签名|密钥/i;
|
|
|
|
const TIMEOUT_WORDS = /timeout|timed out|abort(?:ed)?|超时/i;
|
|
const NETWORK_WORDS =
|
|
/fetch failed|network|socket|econn(?:reset|refused|aborted)|enotfound|eai_again|dns|tls|certificate|网络|连接/i;
|
|
|
|
function messageOf(error) {
|
|
return redactText(error?.message ?? error ?? "", 300);
|
|
}
|
|
|
|
function numericStatus(error) {
|
|
const value = Number(error?.status ?? error?.statusCode);
|
|
return Number.isInteger(value) && value >= 100 && value <= 599 ? value : undefined;
|
|
}
|
|
|
|
function explicitCategory(error) {
|
|
const value = error?.category ?? error?.kind ?? error?.code;
|
|
return Object.values(FAILURE).includes(value) ? value : undefined;
|
|
}
|
|
|
|
function forbiddenCategory(message) {
|
|
if (CREDENTIAL_WORDS.test(message)) return FAILURE.CREDENTIAL_FORBIDDEN;
|
|
if (ENTITLEMENT_WORDS.test(message)) return FAILURE.ENTITLEMENT_FORBIDDEN;
|
|
return FAILURE.FORBIDDEN;
|
|
}
|
|
|
|
/**
|
|
* Classify an error without changing it.
|
|
*
|
|
* @param {unknown} error
|
|
* @param {{stage?: string}} [context]
|
|
* @returns {{category: string, status?: number, message: string, retryable: boolean,
|
|
* refresh: boolean, signInExpired: boolean}}
|
|
*/
|
|
export function classifyError(error, { stage } = {}) {
|
|
const status = numericStatus(error);
|
|
const message = messageOf(error);
|
|
let category = explicitCategory(error);
|
|
|
|
if (!category && error?.name === "TimeoutError") category = FAILURE.TIMEOUT;
|
|
if (!category && (error?.name === "AbortError" || TIMEOUT_WORDS.test(message))) {
|
|
category = FAILURE.TIMEOUT;
|
|
}
|
|
if (!category && stage === "signing") category = FAILURE.SIGNING;
|
|
if (!category && (SIGNING_WORDS.test(message) || /request body type for signing/i.test(message))) {
|
|
category = FAILURE.SIGNING;
|
|
}
|
|
if (!category && /changed accounts|账号已切换/i.test(message)) {
|
|
category = FAILURE.CLI_ACCOUNT_CHANGED;
|
|
}
|
|
if (!category && /no coding plan tier|not eligible|ineligible|no tier|名额|档位/i.test(message)) {
|
|
category = FAILURE.INELIGIBLE;
|
|
}
|
|
if (!category && status === 401) category = FAILURE.UNAUTHORIZED;
|
|
if (!category && status === 403) category = forbiddenCategory(message);
|
|
if (!category && status === 429) category = FAILURE.RATE_LIMIT;
|
|
if (!category && status !== undefined && status >= 500) category = FAILURE.SERVER;
|
|
if (!category && VERSION_WORDS.test(message)) category = FAILURE.VERSION;
|
|
if (!category && NETWORK_WORDS.test(message)) category = FAILURE.NETWORK;
|
|
category ??= FAILURE.UNKNOWN;
|
|
|
|
const retryable = [
|
|
FAILURE.NETWORK,
|
|
FAILURE.TIMEOUT,
|
|
FAILURE.RATE_LIMIT,
|
|
FAILURE.SERVER,
|
|
].includes(category);
|
|
const refresh =
|
|
category === FAILURE.UNAUTHORIZED ||
|
|
category === FAILURE.CREDENTIAL_FORBIDDEN ||
|
|
category === FAILURE.CLI_ACCOUNT_CHANGED;
|
|
|
|
return {
|
|
category,
|
|
...(status === undefined ? {} : { status }),
|
|
message,
|
|
retryable,
|
|
refresh,
|
|
signInExpired:
|
|
category === FAILURE.UNAUTHORIZED ||
|
|
category === FAILURE.CREDENTIAL_FORBIDDEN ||
|
|
category === FAILURE.CLI_ACCOUNT_CHANGED,
|
|
};
|
|
}
|
|
|
|
/** True only for a credential/session refusal that may be renewed. */
|
|
export function isCredentialFailure(error, context) {
|
|
return classifyError(error, context).refresh;
|
|
}
|
|
|
|
/** True for failures another attempt may recover from. */
|
|
export function isTransientFailure(error, context) {
|
|
return classifyError(error, context).retryable;
|
|
}
|
|
|
|
/**
|
|
* Attach the classification to an existing Error without copying its secrets
|
|
* into a new message. Existing `signIn` is preserved unless it is wrong.
|
|
*/
|
|
export function annotateError(error, context) {
|
|
if (!(error instanceof Error)) {
|
|
const original = error;
|
|
error = new Error(messageOf(original));
|
|
if (original?.status !== undefined) error.status = original.status;
|
|
if (original?.statusCode !== undefined) error.status = original.statusCode;
|
|
}
|
|
const result = classifyError(error, context);
|
|
error.category ??= result.category;
|
|
if (result.status !== undefined) error.status ??= result.status;
|
|
if (result.signInExpired) error.signIn = "expired";
|
|
else if (error.signIn === "expired") delete error.signIn;
|
|
return error;
|
|
}
|
|
|
|
/** The Magpie header that keeps an account signed in without reporting an error. */
|
|
export function keptResponseHeaders(response, classification) {
|
|
if (
|
|
response?.status === 403 &&
|
|
classification?.category !== FAILURE.CREDENTIAL_FORBIDDEN
|
|
) {
|
|
const headers = new Headers(response.headers);
|
|
headers.set("x-magpie-sign-in", "kept");
|
|
return new Response(response.body, {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers,
|
|
});
|
|
}
|
|
return response;
|
|
}
|