mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 06:07:57 +01:00
scripts/site-template is now bitbaum/fleet templates/app — Next.js 16, better-auth accounts (password, link, passkey, 2FA, OrangeCat/bitbaum sign-in as env-gated OIDC), en/de/fr, nonce CSP, ai-kit/mail-kit/limitkit/bip-kit, tests and CI — copied at a pinned fleet commit by scripts/templates/sync-starter.sh. CI proves the copy is exact (sync-starter.sh --check), and starter-sync.yml opens the PR when fleet's starter moves. Never edited here. The kickoff's "nextjs-tailwind" starter seeds the same app from src/lib/starter-app.generated.ts (generated from the directory, pinned by scripts/test/starter-app.ts), minus the workflows register-site.sh adds with the site's CD. The repo is seeded in one Trees API call with inline contents instead of a blob POST per file (~85 for this starter). The widget token rides in .env.production; the starter's .gitleaks.toml allows NEXT_PUBLIC_* values. The new-from-scratch picker reads the light template list, so the starter's file bodies never reach a client bundle. Both paths give the site its database and auth/seal secrets ON THE BOX (scripts/hetzner/provision-db-on-box.sh: role + database, ~/.db-credentials, /opt/<slug>/shared/.env; existing secrets never replaced; nothing printed), and record the db in the register row so every deploy applies drizzle/. register-site.sh seeds the starter's full workflow set to starter repos and the golden CI floor to anything else, never overwriting another repo's ci.yml. test-site-template.sh follows the starter (verify SSOT, design tokens instead of a local palette) and its comment stripper is fixed: the sed range deleted the page from the doc comment to EOF, so every day-zero check had been passing against an empty string. check-template-contrast.mjs and the starter-lock generator are retired with the files they checked. Claude-Session: https://claude.ai/code/session_01W7K2Gm5wcbfCi7THtmV1Hy Co-authored-by: Claude <noreply@anthropic.com>
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import {
|
|
ChainExhaustedError,
|
|
chainFrom,
|
|
complete,
|
|
createHealthTracker,
|
|
freeChain,
|
|
usableChain,
|
|
type Env,
|
|
type Link,
|
|
} from "@bitbaum/ai-kit";
|
|
import { byokChain, type ByokConfig } from "@bitbaum/ai-kit/byok";
|
|
import { SITE } from "@/lib/site";
|
|
|
|
/**
|
|
* The model call is ai-kit's `complete()`, never ours: no vendor SDK, no
|
|
* hand-rolled fetch to a model, no pinned model id (fleet STACK.md, AI row).
|
|
* `complete()` owns the chain walk, the per-link deadline, the empty-200 and
|
|
* the three kinds of 429.
|
|
*
|
|
* Two ways to be served, and they never mix (fleet SHARED.md, "Your own
|
|
* model", rule 4):
|
|
* - the free shared chain, from whichever vendor keys this deployment has
|
|
* (GROQ_API_KEY, GEMINI_API_KEY, OPENROUTER_API_KEY — any one);
|
|
* - the person's own key, sealed at rest (lib/own-model.ts). Their key serves
|
|
* only their model and is never charged to the free budget or health.
|
|
*
|
|
* Nothing here logs a prompt or a reply.
|
|
*/
|
|
const ENV_PREFIX = "APP";
|
|
const health = createHealthTracker();
|
|
|
|
/** The free chain this deployment can walk. APP_MODEL picks a starting point, never a pin. */
|
|
export function freeLinks(env: Env): Link[] {
|
|
return chainFrom(env[`${ENV_PREFIX}_MODEL`], usableChain(freeChain(ENV_PREFIX), env));
|
|
}
|
|
|
|
export function freeAvailable(env: Env): boolean {
|
|
return freeLinks(env).length > 0;
|
|
}
|
|
|
|
export const MAX_PROMPT = 4_000;
|
|
|
|
export type AskOutcome =
|
|
| { ok: true; text: string; servedBy: string; ownModel: boolean }
|
|
| { ok: false; error: "not_configured" | "busy" | "empty_prompt" | "too_long" };
|
|
|
|
export async function ask(
|
|
prompt: string,
|
|
options: {
|
|
env: Env;
|
|
ownModel?: ByokConfig | null;
|
|
signal?: AbortSignal;
|
|
fetchImpl?: typeof fetch;
|
|
},
|
|
): Promise<AskOutcome> {
|
|
const text = prompt.trim();
|
|
if (!text) return { ok: false, error: "empty_prompt" };
|
|
if (text.length > MAX_PROMPT) return { ok: false, error: "too_long" };
|
|
|
|
const own = options.ownModel ?? null;
|
|
let chain: Link[];
|
|
let env = options.env;
|
|
let extraHeaders: Record<string, string> | undefined;
|
|
if (own) {
|
|
({ chain, env, extraHeaders } = byokChain(own, { url: SITE.url, title: SITE.title }));
|
|
} else {
|
|
chain = freeLinks(options.env);
|
|
if (chain.length === 0) return { ok: false, error: "not_configured" };
|
|
}
|
|
|
|
try {
|
|
const result = await complete({
|
|
chain,
|
|
env,
|
|
extraHeaders,
|
|
health: own ? undefined : health,
|
|
messages: [
|
|
{ role: "system", content: `You are the assistant inside ${SITE.title}. Answer briefly.` },
|
|
{ role: "user", content: text },
|
|
],
|
|
maxTokens: 800,
|
|
temperature: 0.3,
|
|
timeoutMs: 45_000,
|
|
signal: options.signal,
|
|
fetchImpl: options.fetchImpl,
|
|
});
|
|
return { ok: true, text: result.text, servedBy: result.id, ownModel: own !== null };
|
|
} catch (error) {
|
|
if (error instanceof ChainExhaustedError) return { ok: false, error: "busy" };
|
|
throw error;
|
|
}
|
|
}
|