Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : ORIX App Universe8 * File : src/lib/models.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website : https://www.orixlab.dev12 * Created : 2026-11-2213 * Last Modified : 2026-11-2214 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { readFile } from "node:fs/promises";44import path from "node:path";4546/**47 * The on-device models ORIX applications can use, read from the same two catalogues the applications read48 * (ORIX_MODELS_DIR/v1/catalog.json for iPhone and iPad, catalog-mac.json for the Mac). Nothing is written by hand49 * here: a model appears on the site when it is in a published catalogue, with the figures recorded when it was verified.50 */5152type RawFile = { path: string; size: number };53type RawModel = {54 id: string; displayName: string; family?: string; publisher?: string; repo?: string; quantization?: string;55 parameterCount?: number; licenseID?: string; licenseURL?: string; redistributable?: boolean; baseURL?: string | null;56 totalBytes?: number; contextLength?: number; capabilities?: string[]; tags?: string[]; status?: string;57 minimumDeviceMemoryGB?: number; files?: RawFile[];58 verified?: { date?: string; machine?: string; platform?: string; tokensPerSecond?: number; timeToFirstToken?: number; peakMemoryBytes?: number };59};60type RawCatalog = { generatedAt?: string; models?: RawModel[] };6162export type ModelGroup = "mac" | "both" | "phone";63export type OrixModel = {64 id: string; name: string; family: string; publisher: string; repo: string; quantization: string;65 parameters: number | null; bytes: number; context: number | null; license: string; licenseURL: string | null;66 capabilities: string[]; tags: string[];67 /** Served by this site (true) or downloaded from Hugging Face by the iPhone app (false). */68 hosted: boolean;69 onMac: boolean; onPhone: boolean; group: ModelGroup;70 /** Estimated minimum memory of a Mac, from the Mac catalogue; null for a model that is not offered on the Mac. */71 macMemoryGB: number | null;72 speed: number | null; firstToken: number | null; peakBytes: number | null; machine: string | null; verifiedOn: string | null;73};74export type ModelLibrary = { models: OrixModel[]; updated: string | null; hostedBytes: number };7576const root = () => path.join(process.env.ORIX_MODELS_DIR ?? path.join(process.env.HOME ?? "", "apps-data", "orix-models"), "v1");7778async function read(name: string): Promise<RawCatalog | null> {79 try { return JSON.parse(await readFile(path.join(root(), name), "utf8")) as RawCatalog; } catch { return null; }80}8182const isHosted = (m: RawModel) => m.redistributable !== false && typeof m.baseURL === "string" && m.baseURL.startsWith("https://www.orixlab.dev/");8384export async function modelLibrary(): Promise<ModelLibrary> {85 const [phone, mac] = await Promise.all([read("catalog.json"), read("catalog-mac.json")]);86 const phoneModels = (phone?.models ?? []).filter((m) => (m.status ?? "verified") === "verified");87 const macModels = (mac?.models ?? []).filter((m) => (m.status ?? "verified") === "verified" && isHosted(m));88 const macById = new Map(macModels.map((m) => [m.id, m]));89 const phoneById = new Map(phoneModels.map((m) => [m.id, m]));90 const ids = [...new Set([...macModels.map((m) => m.id), ...phoneModels.map((m) => m.id)])];91 const models = ids.map((id): OrixModel => {92 const m = macById.get(id), p = phoneById.get(id), src = (m ?? p)!;93 const onMac = !!m, onPhone = !!p;94 return {95 id, name: src.displayName, family: src.family ?? "", publisher: src.publisher ?? "", repo: src.repo ?? "", quantization: src.quantization ?? "",96 parameters: src.parameterCount ?? null, bytes: src.totalBytes ?? (src.files ?? []).reduce((s, f) => s + f.size, 0), context: src.contextLength ?? null,97 license: src.licenseID ?? "", licenseURL: src.licenseURL ?? null, capabilities: src.capabilities ?? [], tags: src.tags ?? [],98 hosted: isHosted(src), onMac, onPhone, group: onMac && onPhone ? "both" : onMac ? "mac" : "phone",99 macMemoryGB: m?.minimumDeviceMemoryGB ?? null,100 speed: src.verified?.tokensPerSecond ?? null, firstToken: src.verified?.timeToFirstToken ?? null, peakBytes: src.verified?.peakMemoryBytes ?? null,101 machine: src.verified?.machine ?? null, verifiedOn: src.verified?.date ?? null,102 };103 }).sort((a, b) => a.bytes - b.bytes);104 const stamps = [phone?.generatedAt, mac?.generatedAt].filter((s): s is string => !!s).sort();105 return { models, updated: stamps.at(-1) ?? null, hostedBytes: models.filter((m) => m.hosted).reduce((s, m) => s + m.bytes, 0) };106}107108export const gigabytes = (bytes: number) => bytes / 1_073_741_824;109