peppa/packages/shared/src/env.ts
peppa-bot c83454889c
All checks were successful
peppa/lint lint: success за 3с
peppa/typecheck typecheck: success за 50с
peppa/build build: success за 34с
peppa/test test: success за 1416с
peppa-ci / ci (pull_request) Successful in 26m6s
PEP-252: вход в сервисы через OAuth (Google, GitHub) — поверх свежей main
Ядро (обновление токенов, «Войти заново», отзыв в сервисе), экран «Подключения»,
тексты ru/kk/en, мок и тесты. Пересобрано на main после PEP-251/258:
миграция теперь 0033_access_hub_oauth, маскировка ответа — connectionKeyForms
от текущего токена, проверка подключений принимает notify вместе с drivers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:00:23 +00:00

74 lines
3.7 KiB
TypeScript

import { homedir } from "node:os";
import { z } from "zod";
const schema = z.object({
// Peppa's SQLite database file (PEP-36); default ~/.peppa/peppa.db.
PEPPA_DB: z.string().optional(),
// The old Postgres database: only the source of `peppa db migrate-to-sqlite`.
DATABASE_URL: z.string().optional(),
GITEA_URL: z.string().url().default("https://git.telecom.quest"),
GITEA_TOKEN: z.string().optional(),
// PEP-4: the platform's repos live in the `peppa` organization.
GITEA_TEMPLATE_REPO: z.string().default("peppa/template"),
// Peppa's own repo (`owner/repo`): footer link, scripts/ci.sh --pr, scripts/peppa-release.sh.
GITEA_PLATFORM_REPO: z.string().default("peppa/peppa"),
// PEP-46: Peppa's own Gitea account — its comments in PRs/issues never go back to agents.
GITEA_BOT_LOGIN: z.string().default("peppa-bot"),
// PEP-230: author email of agent commits made in a runner container (default <bot>@telecom.quest).
PEPPA_GIT_EMAIL: z.string().optional(),
PEPPA_WORKSPACES: z.string().default("~/peppa/workspaces"),
TELEGRAM_BOT_TOKEN: z.string().optional(),
TELEGRAM_PM_CHAT_ID: z.string().optional(),
PEPPA_WEB_URL: z.string().default("http://localhost:3100"),
PEPPA_SECRET: z.string().optional(),
// PEP-19: the previous PEPPA_SECRET during a rotation (read-only: decrypt/verify), then removed.
PEPPA_SECRET_PREVIOUS: z.string().optional(),
GITEA_OAUTH_CLIENT_ID: z.string().optional(),
GITEA_OAUTH_CLIENT_SECRET: z.string().optional(),
// eGov QR login (PEP-65) through the KazEDS relay, already trusted by eGov Mobile.
KAZEDS_RELAY_BASE: z.string().url().default("https://sign.aitu.uz/relay/v1"),
EGOV_ORGANISATION_NAME: z.string().default("Peppa"),
// Google login (PEP-90): OAuth client from Google Cloud; without both the button is hidden.
GOOGLE_CLIENT_ID: z.string().optional(),
GOOGLE_CLIENT_SECRET: z.string().optional(),
// Test override of accounts.google.com / oauth2.googleapis.com (mock-google).
GOOGLE_OAUTH_BASE: z.string().url().optional(),
// Access Hub sign-in (PEP-252): FORGE's OAuth clients for connections; Google falls back to the login client.
PEPPA_OAUTH_GOOGLE_CLIENT_ID: z.string().optional(),
PEPPA_OAUTH_GOOGLE_CLIENT_SECRET: z.string().optional(),
PEPPA_OAUTH_GOOGLE_BASE: z.string().url().optional(),
PEPPA_OAUTH_GITHUB_CLIENT_ID: z.string().optional(),
PEPPA_OAUTH_GITHUB_CLIENT_SECRET: z.string().optional(),
PEPPA_OAUTH_GITHUB_BASE: z.string().url().optional(),
EGOV_ORGANISATION_BIN: z.string().regex(/^\d{12}$/).default("241140100000"),
});
export type Env = z.infer<typeof schema> & { PEPPA_DB: string };
export function loadEnv(source: NodeJS.ProcessEnv = process.env): Env {
const env = schema.parse(source);
env.PEPPA_WORKSPACES = env.PEPPA_WORKSPACES.replace(/^~(?=$|\/)/, homedir());
const db = (env.PEPPA_DB ?? `${source.PEPPA_HOME ?? `${homedir()}/.peppa`}/peppa.db`).replace(/^~(?=$|\/)/, homedir());
return { ...env, PEPPA_DB: db };
}
/** The Peppa checkout; bins, hooks and the template are resolved from it. */
export function peppaRoot(fallback?: string): string {
if (process.env.PEPPA_ROOT) return process.env.PEPPA_ROOT;
if (fallback) return fallback;
throw new Error("PEPPA_ROOT is not set");
}
/** Peppa's local state dir (logs, run files, caches). Tests point it elsewhere. */
export function peppaHome(): string {
return process.env.PEPPA_HOME ?? `${homedir()}/.peppa`;
}
/**
* PEP-108: voice stories in Telegram work — speech to text is set up (PEPPA_TRANSCRIBE_URL, the
* bot's apps/bot/src/transcribe.ts). The web and the bot's /help mention voice only then.
*/
export function voiceStoriesEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
return !!env.PEPPA_TRANSCRIBE_URL?.trim();
}