Files
LEOLI 8765cf8f9c atomcode 0.3.0: AtomGit CodingPlan provider plugin (LILEOO, #716)
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.
2026-11-05 16:54:02 +08:00

216 lines
7.1 KiB
JavaScript

/**
* Auto-tracking of the upstream AtomCode client version.
*
* Why track at all: the official client force-updates itself on launch, so the
* gateway most plausibly treats version freshness as part of looking like the
* client (Atom2Api ships a whole UA checker that reads the same manifest for
* exactly this concern). This module keeps the plugin on the current version
* without releases or user action.
*
* Behavior contract (deliberately gentle — a metadata fetch must never break a
* model request):
*
* - the manifest is a public file (`raw.atomgit.com`, same URL Atom2Api's
* `ua.go` reads); it is fetched with a short timeout and validated strict;
* - a TTL cache (12 h) plus a short failure retry window (5 min) keep the
* network load at one GET per host process, retrying quietly;
* - `ensure()` never waits for the network; it starts one background refresh
* and immediately returns the last known version;
* - the fallback is the built-in anchor constant, so an unreachable manifest
* degrades to exactly the behavior we shipped and verified live.
*
* The version feeds three places and they must agree — `User-Agent`,
* `X-AtomCode-Ver`, and the signing salt (`sha256(client_version)`). Any
* self-consistent version produces a valid signature; freshness is the only
* thing being chased here.
*
* @module magpie-atomcode/version
*/
import { readCappedResponseText } from "./internal.mjs";
/** Atom2Api `ua.go` 同款 URL:官方仓库的 workspace 声明。 */
export const ATOMCODE_CARGO_URL = "https://raw.atomgit.com/atomgit_atomcode/atomcode/raw/main/Cargo.toml";
/** On a fresh fetch this version is trusted for… */
export const VERSION_TTL_MS = 12 * 3600_000;
/** …and after a failed fetch we stay quiet (and on the cached value) for… */
export const VERSION_FAIL_TTL_MS = 5 * 60_000;
/** HTTP timeout for one background manifest fetch. */
export const VERSION_TIMEOUT_MS = 10_000;
/** Upstream reads `workspace.package.version` (go-toml), semantic. */
const SEMVER = /^\d+\.\d+\.\d+(?:[-+][\w.-]+)?$/;
/**
* Pull `workspace.package.version` out of a Cargo.toml text.
*
* Line-based on purpose (no TOML dependency): only the top-level
* `[workspace.package]` section is read, and only semantic versions count.
* `[package]` versions (like the example crate) are ignored, mirroring
* Atom2Api's manifest struct.
*
* @param {string} text
* @returns {string|null} the version, or null when absent/invalid.
*/
export function extractVersionFromManifest(text) {
if (typeof text !== "string") return null;
let inSection = false;
for (const line of text.split(/\r?\n/)) {
const section = line.match(/^\s*\[([^\]]+)\]\s*$/);
if (section) {
inSection = section[1] === "workspace.package";
continue;
}
if (!inSection) continue;
const m = line.match(/^\s*version\s*=\s*"([^"]+)"/);
if (m) {
const version = m[1].trim();
return SEMVER.test(version) ? version : null;
}
}
return null;
}
/** download cap, mirroring Atom2Api's 1 MiB guard */
const MAX_MANIFEST_BYTES = 1 << 20;
/**
* Fetch and validate the version from the manifest.
*
* @param {{url?: string, fetchImpl?: Function, timeoutMs?: number}} [opts]
* @returns {Promise<string>} a semantic version.
* @throws on network failure, non-2xx, oversize body, missing version.
*/
export async function fetchAtomCodeVersion({
url = ATOMCODE_CARGO_URL,
fetchImpl = (...args) => fetch(...args),
timeoutMs = VERSION_TIMEOUT_MS,
} = {}) {
const res = await fetchImpl(url, {
redirect: "error",
headers: { accept: "text/plain" },
signal: AbortSignal.timeout(timeoutMs),
});
if (!res.ok) throw new Error(`AtomCode manifest returned ${res.status}`);
const text = await readCappedResponseText(res, {
maxBytes: MAX_MANIFEST_BYTES,
label: "AtomCode manifest",
});
const version = extractVersionFromManifest(text);
if (!version) throw new Error("AtomCode manifest has no valid workspace.package.version");
return version;
}
/**
* A version source: cache + inflight + bounded refresh.
*
* @param {{fallback?: string, url?: string, fetchImpl?: Function, timeoutMs?: number,
* ttlMs?: number, failTtlMs?: number, onError?: Function}} [opts]
* @returns {{get(): string, isLive(): boolean, status(): object, ensure(): Promise<string>, poke(): void}}
*/
export function createVersionSource({
fallback = "0.0.0",
url,
fetchImpl,
timeoutMs,
ttlMs = VERSION_TTL_MS,
failTtlMs = VERSION_FAIL_TTL_MS,
onError,
onChange,
} = {}) {
// Seeded with the fallback so `get()` is meaningful before any network:
// at=0 means "stale", which is what makes the first ensure()/poke() fetch.
const cache = { at: 0, version: fallback ?? "0.0.0" };
let failedAt = 0;
let inflight = null;
async function refresh() {
if (inflight) return inflight;
inflight = (async () => {
try {
const version = await fetchAtomCodeVersion({ url, fetchImpl, timeoutMs });
cache.at = Date.now();
cache.version = version;
failedAt = 0;
try {
onChange?.({
version,
source: "manifest",
checkedAt: cache.at,
failedAt: undefined,
live: true,
});
} catch {
/* the observer is best-effort */
}
return version;
} catch (error) {
failedAt = Date.now();
try {
onError?.(error);
} catch {
/* the logger is best-effort */
}
try {
onChange?.({
version: cache.version,
source: cache.at ? "cached" : "fallback",
checkedAt: cache.at || undefined,
failedAt,
live: false,
});
} catch {
/* the observer is best-effort */
}
return undefined;
} finally {
inflight = null;
}
})();
return inflight;
}
const fresh = () => Date.now() - cache.at < ttlMs;
const quiet = () => Date.now() - failedAt < failTtlMs;
return {
/** The current best version — synchronous, never throws. */
get() {
return cache.version;
},
/** True while the cache is inside its TTL. */
isLive() {
return fresh();
},
/** Where the current version came from, for persisted diagnostics. */
status() {
return {
version: cache.version,
source: cache.at ? (fresh() ? "manifest" : "cached") : "fallback",
checkedAt: cache.at || undefined,
failedAt: failedAt || undefined,
live: fresh(),
};
},
/**
* Best-effort current version.
*
* Always immediate: start one background refresh when stale and answer with
* the current cached version. The host-start poke normally completes before
* the first request, while an offline manifest can never delay it.
*/
async ensure() {
if (fresh()) return cache.version;
if (quiet()) return cache.version;
if (!inflight) refresh().catch(() => {});
return cache.version;
},
/** Fire-and-forget refresh for host startup; never throws. */
poke() {
if (fresh() || quiet() || inflight) return;
refresh().catch(() => {});
},
};
}