CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 10s
CI (SIAX Cloud) / sonar (pull_request) Skipped
Security Scan (SIAX Cloud) / security (push) Successful in 13s
masterplan-lock / check (pull_request) Successful in 7s
CI (SIAX Cloud) / security (push) Successful in 26s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 19s
CI (SIAX Cloud) / quality (push) Successful in 1m9s
CI (SIAX Cloud) / quality (pull_request) Successful in 2m26s
CI (SIAX Cloud) / integration (pull_request) Successful in 2m33s
- POST /api/0pt/documents/upload: real multipart bytes into DOC0 through the durable attempt journal and the existing coordinator; verified tenant/actor only, project authorization fail-closed. - POST /api/0pt/documents/structure: DOC0 segments -> INF0 -> evidence-bound validation -> optional JSON/CSV export, with credit reserve/settle. - Credits: balance, server-owned price book, Stripe Checkout and idempotent webhook reconciliation via a Stripe re-read of session + payment intent. - /portal/dokument and /portal/krediter: SIAX-styled workspace. - @siax/access: universal, framework-neutral OpenFGA project relationship primitive reusable by DOC0/N0D/other products. - Readiness: real Redis PING probe instead of presence-only; queue is now a live dependency when configured. - Dockerfile: copy every workspace manifest so --frozen-lockfile works. - Format/build hygiene: .prettierignore, .worktrees ignored, valid manifest YAML.
149 lines
4.8 KiB
TypeScript
149 lines
4.8 KiB
TypeScript
/**
|
|
* Server-owned, versioned credit price book.
|
|
*
|
|
* Prices are a business decision, not a code default: this module never
|
|
* invents a price. It parses an explicit `OPT_CREDIT_PRICE_BOOK` JSON document
|
|
* and fails closed when it is missing or invalid, so the checkout route can
|
|
* answer "price book not configured" instead of charging an arbitrary amount.
|
|
*
|
|
* Expected shape (all money in minor units, all quantities safe integers):
|
|
*
|
|
* {
|
|
* "version": "2026-11-01",
|
|
* "currency": "SEK",
|
|
* "packs": [{ "id": "starter", "credits": 100, "amountMinor": 9900 }],
|
|
* "metering": {
|
|
* "pagesPerCredit": 5,
|
|
* "inputTokensPerCredit": 20000,
|
|
* "outputTokensPerCredit": 4000
|
|
* }
|
|
* }
|
|
*/
|
|
|
|
export interface CreditPack {
|
|
id: string;
|
|
credits: number;
|
|
amountMinor: number;
|
|
}
|
|
|
|
export interface CreditMetering {
|
|
pagesPerCredit: number;
|
|
inputTokensPerCredit: number;
|
|
outputTokensPerCredit: number;
|
|
}
|
|
|
|
export interface CreditPriceBook {
|
|
version: string;
|
|
currency: string;
|
|
packs: readonly CreditPack[];
|
|
metering: CreditMetering;
|
|
}
|
|
|
|
const MAX_SAFE = Number.MAX_SAFE_INTEGER;
|
|
|
|
function safePositive(value: unknown): value is number {
|
|
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0 && value <= MAX_SAFE;
|
|
}
|
|
|
|
function safePositiveOrZero(value: unknown): value is number {
|
|
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 && value <= MAX_SAFE;
|
|
}
|
|
|
|
function parsePack(raw: unknown, currency: string): CreditPack {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
throw new Error('Each credit pack must be an object');
|
|
}
|
|
const pack = raw as Record<string, unknown>;
|
|
if (
|
|
typeof pack.id !== 'string' ||
|
|
!/^[a-z0-9][a-z0-9_-]{0,63}$/.test(pack.id) ||
|
|
!safePositive(pack.credits) ||
|
|
!safePositive(pack.amountMinor) ||
|
|
(pack.currency !== undefined && pack.currency !== currency)
|
|
) {
|
|
throw new Error('Invalid credit pack');
|
|
}
|
|
return { id: pack.id, credits: pack.credits, amountMinor: pack.amountMinor };
|
|
}
|
|
|
|
export function parseCreditPriceBook(raw: unknown): CreditPriceBook {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
throw new Error('Price book must be an object');
|
|
}
|
|
const doc = raw as Record<string, unknown>;
|
|
const version = doc.version;
|
|
const currency = doc.currency;
|
|
if (
|
|
typeof version !== 'string' ||
|
|
!/^\d{4}-\d{2}-\d{2}(?:\.\d+)?$/.test(version) ||
|
|
typeof currency !== 'string' ||
|
|
!/^[A-Z]{3}$/.test(currency) ||
|
|
!Array.isArray(doc.packs) ||
|
|
doc.packs.length === 0 ||
|
|
doc.packs.length > 100
|
|
) {
|
|
throw new Error('Invalid price book header or packs');
|
|
}
|
|
const packs = doc.packs.map((pack) => parsePack(pack, currency));
|
|
const ids = new Set(packs.map((pack) => pack.id));
|
|
if (ids.size !== packs.length) throw new Error('Duplicate credit pack id');
|
|
if (!doc.metering || typeof doc.metering !== 'object' || Array.isArray(doc.metering)) {
|
|
throw new Error('Price book metering is required');
|
|
}
|
|
const metering = doc.metering as Record<string, unknown>;
|
|
if (
|
|
!safePositive(metering.pagesPerCredit) ||
|
|
!safePositive(metering.inputTokensPerCredit) ||
|
|
!safePositive(metering.outputTokensPerCredit)
|
|
) {
|
|
throw new Error('Invalid metering rates');
|
|
}
|
|
return {
|
|
version,
|
|
currency,
|
|
packs,
|
|
metering: {
|
|
pagesPerCredit: metering.pagesPerCredit,
|
|
inputTokensPerCredit: metering.inputTokensPerCredit,
|
|
outputTokensPerCredit: metering.outputTokensPerCredit,
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Returns `undefined` (fail closed) when the price book is absent or invalid. */
|
|
export function readCreditPriceBook(env: NodeJS.ProcessEnv = process.env): CreditPriceBook | undefined {
|
|
const raw = env.OPT_CREDIT_PRICE_BOOK;
|
|
if (!raw || !raw.trim()) return undefined;
|
|
try {
|
|
return parseCreditPriceBook(JSON.parse(raw));
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function findPack(book: CreditPriceBook, packId: string): CreditPack | undefined {
|
|
return book.packs.find((pack) => pack.id === packId);
|
|
}
|
|
|
|
/** Round each metered unit up to its billing block, exactly like credit-quote. */
|
|
export function creditsForUsage(
|
|
book: CreditPriceBook,
|
|
units: { pages: number; inputTokens: number; outputTokens: number },
|
|
): number {
|
|
if (
|
|
!safePositiveOrZero(units.pages) ||
|
|
!safePositiveOrZero(units.inputTokens) ||
|
|
!safePositiveOrZero(units.outputTokens)
|
|
) {
|
|
throw new Error('Usage units must be nonnegative safe integers');
|
|
}
|
|
const blocks =
|
|
Math.ceil(units.pages / book.metering.pagesPerCredit) +
|
|
Math.ceil(units.inputTokens / book.metering.inputTokensPerCredit) +
|
|
Math.ceil(units.outputTokens / book.metering.outputTokensPerCredit);
|
|
if (!Number.isSafeInteger(blocks) || blocks < 0) {
|
|
throw new Error('Credit estimate outside supported range');
|
|
}
|
|
return blocks;
|
|
}
|