peppa/packages/core/src/rules.ts
Асель e1384a4c6f Push Peppa to Gitea; shared RULES.md loaded live into every session
- peppa-bot account: repos peppa-bot/peppa and peppa-bot/template
- git-credential-peppa: per-repo credential helper with the bot token
- template split: RULES.md (shared, stays in Gitea) + AGENTS.md (project)
- runner fetches RULES.md from Gitea on each launch (cached fallback) and
  passes it via --append-system-prompt-file; MCP get_rules for manual runs
- bootstrap-project.sh clones the template from Gitea

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:50:45 +05:00

35 lines
1.3 KiB
TypeScript

import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { loadEnv } from "@peppa/shared";
const CACHE = join(homedir(), ".peppa", "rules.md");
const LOCAL = resolve(dirname(fileURLToPath(import.meta.url)), "../../../template/RULES.md");
export interface SharedRules {
text: string;
source: "gitea" | "cache" | "local";
}
/**
* The shared agent rules (RULES.md in the template repo). Gitea is the source of truth;
* the last fetched copy is cached so sessions still start when Gitea is unreachable.
*/
export async function loadSharedRules(env = loadEnv()): Promise<SharedRules> {
try {
const res = await fetch(`${env.GITEA_URL}/api/v1/repos/${env.GITEA_TEMPLATE_REPO}/raw/RULES.md`, {
headers: env.GITEA_TOKEN ? { Authorization: `token ${env.GITEA_TOKEN}` } : {},
signal: AbortSignal.timeout(5000),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const text = await res.text();
mkdirSync(dirname(CACHE), { recursive: true });
writeFileSync(CACHE, text);
return { text, source: "gitea" };
} catch {
if (existsSync(CACHE)) return { text: readFileSync(CACHE, "utf8"), source: "cache" };
return { text: readFileSync(LOCAL, "utf8"), source: "local" };
}
}