Skip to content

🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.

feat(site): GitHub 星数每 2 分钟刷新,页面打开时从站内取

dyk9835fbf96c568e
diff --git a/site/README.md b/site/README.md
index 85ab402..c757d4f 100644
--- a/site/README.md
+++ b/site/README.md
@@ -227,7 +227,7 @@ docker compose -f deploy/compose.yml --env-file deploy/.env up -d --build
## 数据从哪来
- **价格**:构建时请求 `https://modelflare.dev/api/pricing`,用 `src/lib/pricing.ts` 算出默认线路价、分档、条件价线路和官方价(规则与 modelflare.dev/pricing 前端一致:有 `official_input_price` 用它,否则用基础价),写入 `src/data/pricing.snapshot.json`。抓取失败或数据异常时沿用快照,页面显示快照日期。
-- **GitHub 星数**:`scripts/fetch-github-stars.ts` 在 `npm run build` 时取,写进 `src/data/github.json`,所以数字最多晚一天(每天 04:00 构建一次,部署时也会构建);取不到就沿用上一个值,不影响构建。不在浏览器里请求 GitHub:不把访客 IP 发给第三方。
+- **GitHub 星数**:线上的 builder 每 2 分钟向 GitHub 取一次,写到 `/srv/site/live/stars.json`,不用重新构建;页面打开时取站内的 `/stars.json`(`src/scripts/stars.ts`,缓存 1 分钟),所以数字最多晚 3 分钟左右。`scripts/fetch-github-stars.ts` 在 `npm run build` 时另取一次写进 `src/data/github.json`,作为页面的初始值和 `/stars.json` 取不到时的备用值;取不到就沿用上一个值,不影响构建。不在浏览器里请求 GitHub:不把访客 IP 发给第三方。
- **机器人**:`scripts/sync-from-app.ts` 从 `../ui/design/bots/*.webp` 复制精灵图、从 `../THIRD_PARTY_NOTICES.md` 同步许可(Docker 构建上下文里没有应用目录,用已提交的副本);几何(`src/lib/bots.ts`)与 `../ui/components/avatar.js` 的 `BOT_CHARS` 不一致时会警告。头像样式 `src/styles/bots.css` 移植自 `../ui/design/components/avatar.css`。
- 精灵图另有同分辨率的 AVIF 版(`public/bots/*.avif`,约为 WebP 的一半),支持的浏览器经 `image-set()` 优先用它,其他照旧用 WebP。AVIF 必须从原图编码(`npm run bots:avif`:重跑 `../scripts/build-bot-avatars.py --png` 拿无损精灵图,q58 的画质按 PSNR 不低于 WebP q86),不能拿 WebP 再转。生成时把对应 WebP 的哈希记在 `src/data/bots-avif.json`;WebP 更新了而 AVIF 没重做,`check-images` 会失败。
- **演示**:Hero 的"看演示"和"它能动手"都是动画(`src/components/DemoTour.astro`、`src/scripts/tour.ts`),不依赖视频文件。"看演示"点开就自动轮播、播完从头循环,没有播放 / 暂停 / 翻页按钮,底部进度条可点跳步。
diff --git a/site/deploy/builder.mjs b/site/deploy/builder.mjs
index 13c9213..046bbaa 100644
--- a/site/deploy/builder.mjs
+++ b/site/deploy/builder.mjs
@@ -2,12 +2,16 @@
// 启动时构建一次 → 每天 04:00(UTC+8)再构建一次(价格每日刷新)。
// 每次构建输出到 /srv/site/builds/<时间戳>,成功后把 /srv/site/current 这个软链接原子地换过去;失败则继续用上一版。
// 只保留最近 3 版。SITE_BUILD=draft 时用草稿构建(部署值没填齐时也能先跑起来,页面上会留着占位符)。
+// GitHub 星数另外每 2 分钟刷新一次,不用重新构建:写到 /srv/site/live/stars.json,由 nginx 以 /stars.json 提供。
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
+import { fetchStars } from "../scripts/github-stars.mjs";
const OUT = process.env.SITE_OUT || "/srv/site";
const KEEP = 3;
+// 没配 GITHUB_TOKEN 时匿名限额是每小时 60 次,每小时 30 次留够余量
+const STARS_EVERY_MS = 2 * 60_000;
const mode = process.env.SITE_BUILD === "draft" ? "build:draft" : "build";
const log = (...a) => console.log(new Date().toISOString(), ...a);
@@ -51,6 +55,20 @@ function untilNextRun(now = new Date()) {
return next - now;
}
+/** 取不到就留着上一个文件;先写临时文件再改名,nginx 不会读到写了一半的内容 */
+async function refreshStars() {
+ try {
+ const content = JSON.stringify({ stars: await fetchStars(), fetchedAt: new Date().toISOString() }) + "\n";
+ const file = path.join(OUT, "live", "stars.json");
+ const tmp = `${file}.${process.pid}`;
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(tmp, content);
+ fs.renameSync(tmp, file);
+ } catch (e) {
+ log(`GitHub 星数未刷新(${e.message}),沿用上一个值`);
+ }
+}
+
let timer;
async function loop() {
await build();
@@ -59,4 +77,8 @@ async function loop() {
timer = setTimeout(loop, ms);
}
for (const sig of ["SIGTERM", "SIGINT"]) process.on(sig, () => { clearTimeout(timer); process.exit(0); });
+if (process.env.GITHUB_URL) {
+ refreshStars();
+ setInterval(refreshStars, STARS_EVERY_MS);
+}
loop();
diff --git a/site/deploy/nginx.conf b/site/deploy/nginx.conf
index 7a39788..baeaeb0 100644
--- a/site/deploy/nginx.conf
+++ b/site/deploy/nginx.conf
@@ -25,6 +25,13 @@ server {
# 机器人精灵图(.webp / .avif):文件名不带哈希,但只在角色重画时才变,缓存一天(过期后用 Last-Modified 重新验证)
location /bots/ { add_header Cache-Control "public, max-age=86400"; add_header X-Content-Type-Options "nosniff" always; }
+ # GitHub 星数:builder 每 2 分钟刷新,不在构建产物里。文件还没生成时返回 404,页面继续用构建时的数字
+ location = /stars.json {
+ alias /srv/site/live/stars.json;
+ add_header Cache-Control "public, max-age=60";
+ add_header X-Content-Type-Options "nosniff" always;
+ }
+
# 页面:价格每天更新,短缓存
location / {
try_files $uri $uri/index.html $uri.html =404;
diff --git a/site/scripts/fetch-github-stars.ts b/site/scripts/fetch-github-stars.ts
index b424bc2..4d71f50 100644
--- a/site/scripts/fetch-github-stars.ts
+++ b/site/scripts/fetch-github-stars.ts
@@ -1,31 +1,19 @@
-// 构建时取 GitHub 星数,写进 src/data/github.json(首屏的 GitHub 按钮显示,见 sections/Hero.astro)。
-// 不在访客的浏览器里请求 GitHub:不用把访客 IP 发给第三方(隐私说明页的承诺),也不受匿名每小时 60 次的限制。
-// 代价是数字最多晚一天:builder 每天 04:00(UTC+8)构建一次,部署时也会构建。
+// 构建时取 GitHub 星数,写进 src/data/github.json:首屏 GitHub 按钮的初始值(见 sections/Hero.astro),也是 /stars.json 取不到时的备用值。
+// 上线后的刷新见 deploy/builder.mjs 和 src/scripts/stars.ts。
// GITHUB_URL 仓库地址,如 https://github.com/modelflare-ai/AnyModelBot;没配时跳过
// GITHUB_TOKEN 可选;匿名请求一般够用
// 取不到不算构建失败:沿用文件里的上一个值,页面上只是数字旧一点。
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
+import { fetchStars } from "./github-stars.mjs";
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const file = path.join(root, "src/data/github.json");
const keep = (why: string) => console.warn(`GitHub 星数未更新(${why}),沿用上一个值`);
-const m = /github\.com\/([^/]+)\/([^/.]+)/.exec(process.env.GITHUB_URL || "");
-if (!m) {
- keep("GITHUB_URL 不是 GitHub 仓库地址");
- process.exit(0);
-}
-
-const headers: Record<string, string> = { accept: "application/vnd.github+json", "user-agent": "anymodelbot-site" };
-if (process.env.GITHUB_TOKEN) headers.authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
-
try {
- const res = await fetch(`https://api.github.com/repos/${m[1]}/${m[2]}`, { headers, signal: AbortSignal.timeout(15_000) });
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
- const stars = (await res.json()).stargazers_count;
- if (!Number.isInteger(stars) || stars < 0) throw new Error(`返回的星数不对:${stars}`);
+ const stars = await fetchStars();
fs.writeFileSync(file, JSON.stringify({ stars, fetchedAt: new Date().toISOString() }, null, 2) + "\n");
console.log(`GitHub 星数:${stars}`);
} catch (e) {
diff --git a/site/scripts/github-stars.mjs b/site/scripts/github-stars.mjs
new file mode 100644
index 0000000..0098e4b
--- /dev/null
+++ b/site/scripts/github-stars.mjs
@@ -0,0 +1,14 @@
+// 向 GitHub 取星数,失败时抛错。构建(scripts/fetch-github-stars.ts)和线上的定时刷新(deploy/builder.mjs)共用。
+// GITHUB_URL 仓库地址,如 https://github.com/modelflare-ai/AnyModelBot
+// GITHUB_TOKEN 可选;匿名每小时 60 次,够用
+export async function fetchStars() {
+ const m = /github\.com\/([^/]+)\/([^/.]+)/.exec(process.env.GITHUB_URL || "");
+ if (!m) throw new Error("GITHUB_URL 不是 GitHub 仓库地址");
+ const headers = { accept: "application/vnd.github+json", "user-agent": "anymodelbot-site" };
+ if (process.env.GITHUB_TOKEN) headers.authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
+ const res = await fetch(`https://api.github.com/repos/${m[1]}/${m[2]}`, { headers, signal: AbortSignal.timeout(15_000) });
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
+ const stars = (await res.json()).stargazers_count;
+ if (!Number.isInteger(stars) || stars < 0) throw new Error(`返回的星数不对:${stars}`);
+ return stars;
+}
diff --git a/site/src/data/github.json b/site/src/data/github.json
index facf27a..f8844f1 100644
--- a/site/src/data/github.json
+++ b/site/src/data/github.json
@@ -1,4 +1,4 @@
{
- "stars": 142,
- "fetchedAt": "2026-11-01T17:26:54.503Z"
+ "stars": 750,
+ "fetchedAt": "2026-11-02T05:39:05.903Z"
}
diff --git a/site/src/lib/site.ts b/site/src/lib/site.ts
index 938b3cb..269ef6e 100644
--- a/site/src/lib/site.ts
+++ b/site/src/lib/site.ts
@@ -15,7 +15,7 @@ const optional = (k: string) => (import.meta.env[k] as string | undefined) || pr
export const ANALYTICS_SCRIPT_URL = optional("ANALYTICS_SCRIPT_URL");
export const ANALYTICS_SITE_ID = optional("ANALYTICS_SITE_ID");
export const ORIGIN = `https://${SITE_DOMAIN}`;
-/** GitHub 星数:构建时取(scripts/fetch-github-stars.ts),不在浏览器里请求 */
+/** GitHub 星数的初始值:构建时取(scripts/fetch-github-stars.ts);页面打开后由 scripts/stars.ts 换成站内 /stars.json 的值 */
export const githubStars = z.object({ stars: z.number().int().nonnegative() }).parse(githubJson).stars;
/** 跳到 Modelflare 的链接都带来源参数:全项目统一 utm_source=anymodelbot&utm_campaign=amb_<位置>,官网是 amb_homepage */
diff --git a/site/src/lib/stars.ts b/site/src/lib/stars.ts
index abd0e9f..e65a87f 100644
--- a/site/src/lib/stars.ts
+++ b/site/src/lib/stars.ts
@@ -1,3 +1,9 @@
+/** 站内 /stars.json 的内容里的星数;内容不对时返回 null,页面继续用构建时的数字 */
+export function parseStars(data: unknown): number | null {
+ const stars = (data as { stars?: unknown } | null)?.stars;
+ return Number.isInteger(stars) && (stars as number) > 0 ? (stars as number) : null;
+}
+
// GitHub 星数的显示:1000 以内写全,往上用 k(1.2k、12k)。两种语言写法一样。
export function fmtStars(n: number): string {
if (n < 1000) return String(n);
diff --git a/site/src/scripts/client.ts b/site/src/scripts/client.ts
index 36e456e..4541f94 100644
--- a/site/src/scripts/client.ts
+++ b/site/src/scripts/client.ts
@@ -3,6 +3,7 @@
import { mountEyes } from "../lib/eyes";
import { detectPlatform } from "../lib/platform";
import { initBanner } from "./banner";
+import "./stars";
import "./sections";
type Data = {
diff --git a/site/src/scripts/stars.ts b/site/src/scripts/stars.ts
new file mode 100644
index 0000000..5569496
--- /dev/null
+++ b/site/src/scripts/stars.ts
@@ -0,0 +1,18 @@
+// 首屏 GitHub 按钮的星数:页面里是构建时的数字,打开时再取一次站内的 /stars.json(线上每 2 分钟刷新,见 deploy/builder.mjs)。
+// 只请求自己的站,不请求 GitHub,访客的 IP 不会发给第三方。取不到就留着构建时的数字。
+import { fmtStars, parseStars } from "../lib/stars";
+
+const link = document.querySelector<HTMLElement>("[data-stars-label]");
+const count = link?.querySelector<HTMLElement>("[data-stars]");
+
+if (link && count) {
+ fetch("/stars.json")
+ .then((res) => (res.ok ? res.json() : null))
+ .then((data) => {
+ const stars = parseStars(data);
+ if (stars === null) return;
+ count.textContent = fmtStars(stars);
+ link.setAttribute("aria-label", link.dataset.starsLabel!.replace("{stars}", String(stars)));
+ })
+ .catch(() => { /* 留着构建时的数字 */ });
+}
diff --git a/site/src/sections/Hero.astro b/site/src/sections/Hero.astro
index 4148e0c..6388c70 100644
--- a/site/src/sections/Hero.astro
+++ b/site/src/sections/Hero.astro
@@ -255,8 +255,8 @@ const ic = (d: string, size = 16, cls = "aic") => `<svg class="${cls}" width="${
<DownloadButton lang={lang} placement="hero" id="cta-hero">
<Fragment slot="after">
<button class="btn btn--secondary" type="button" data-open-dialog="demo-tour" data-track="video_play" data-track-video_id="overview"><Icon name="play" />{t.hero.demo}</button>
- <a class="btn btn--secondary hero__gh" href={GITHUB_URL} target="_blank" rel="noopener" aria-label={githubStars ? fmt(t.hero.github, { stars: githubStars }) : t.nav.github} data-track="github_click" data-track-location="hero">
- <Icon name="github" size={18} />{t.nav.github}{githubStars > 0 && <span class="hero__gh-stars"><Icon name="star" size={14} />{fmtStars(githubStars)}</span>}
+ <a class="btn btn--secondary hero__gh" href={GITHUB_URL} target="_blank" rel="noopener" aria-label={githubStars ? fmt(t.hero.github, { stars: githubStars }) : t.nav.github} data-track="github_click" data-track-location="hero" data-stars-label={t.hero.github}>
+ <Icon name="github" size={18} />{t.nav.github}{githubStars > 0 && <span class="hero__gh-stars"><Icon name="star" size={14} /><span data-stars>{fmtStars(githubStars)}</span></span>}
</a>
</Fragment>
</DownloadButton>
diff --git a/site/tests/stars.test.ts b/site/tests/stars.test.ts
index f8711b6..76e1247 100644
--- a/site/tests/stars.test.ts
+++ b/site/tests/stars.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { fmtStars } from "../src/lib/stars";
+import { fmtStars, parseStars } from "../src/lib/stars";
describe("fmtStars", () => {
it.each([
@@ -8,3 +8,10 @@ describe("fmtStars", () => {
[12345, "12k"], [128600, "129k"],
])("%i → %s", (n, text) => expect(fmtStars(n)).toBe(text));
});
+
+describe("parseStars", () => {
+ it("取出星数", () => expect(parseStars({ stars: 745, fetchedAt: "2026-11-02T00:00:00Z" })).toBe(745));
+ it.each([[null], [{}], [{ stars: "745" }], [{ stars: 1.5 }], [{ stars: -1 }], [{ stars: 0 }]])("内容不对:%j", (data) =>
+ expect(parseStars(data)).toBeNull(),
+ );
+});