Repository Public MIT
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : VQuant8 * File : client/src/lib/research/approval.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/vquant11 * Project Website : https://vquant.spboucher.ai12 * Created : 2026-11-2013 * Last Modified : 2026-11-2014 *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: contact@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 * SPDX Identifier : MIT34 * License : MIT License35 * License File : LICENSE36 *37 * Do not embed credentials, private keys, access tokens,38 * certificates or confidential personal information.39 *40 * =============================================================================41 * END OF SP BOUCHER SOURCE HEADER42 * =============================================================================43 */4445/** Approval requests (CONTRACT §9.7): what is asked, and the 15 minutes after which silence is a refusal. */46import { formatCost, formatNumber } from "@/lib/format";47import type { Approval } from "./types";4849export const APPROVAL_TTL_MS = 15 * 60_000;5051export const APPROVAL_TITLE: Record<string, string> = {52 open_network: "Ouvrir le réseau pour une exécution",53 send_data: "Envoyer des données à un service externe",54 raise_budget: "Dépasser le budget de cette recherche",55 other: "Autorisation demandée par l'agent",56};5758export const APPROVAL_SCOPE: Record<string, string> = {59 open_network: "Une seule exécution pourra joindre n'importe quel hôte public (les adresses privées et locales restent refusées). Chaque destination est consignée dans logs/network.log.",60 send_data: "Des données de l'espace de recherche sortiront du serveur vers le service indiqué. L'envoi est consigné dans le journal d'audit.",61 raise_budget: "Les limites de coût ou de durée de cette analyse seront relevées aux valeurs demandées.",62 other: "L'agent estime que cette étape demande votre accord.",63};6465export interface Countdown {66 /** ms left before the request counts as refused; 0 once expired; null when the request has no creation time */67 remainingMs: number | null;68 expired: boolean;69 /** `mm:ss` */70 label: string;71 /** share of the 15 minutes already spent, 0–1 */72 elapsedShare: number;73 /** under two minutes: the card insists */74 urgent: boolean;75}7677export function approvalDeadline(a: Pick<Approval, "createdAt">): number | null {78 return a.createdAt == null ? null : a.createdAt + APPROVAL_TTL_MS;79}8081export function countdown(a: Pick<Approval, "createdAt" | "status">, now: number): Countdown {82 const deadline = approvalDeadline(a);83 if (deadline == null) return { remainingMs: null, expired: a.status === "expired", label: "—", elapsedShare: 0, urgent: false };84 const remainingMs = Math.max(0, deadline - now);85 const s = Math.ceil(remainingMs / 1000);86 return {87 remainingMs,88 expired: a.status === "expired" || (a.status === "pending" && remainingMs === 0),89 label: `${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")}`,90 elapsedShare: Math.min(1, Math.max(0, 1 - remainingMs / APPROVAL_TTL_MS)),91 urgent: remainingMs > 0 && remainingMs <= 120_000,92 };93}9495/** The request can still be answered: pending on the server AND inside its 15 minutes on this clock. */96export function isAnswerable(a: Pick<Approval, "createdAt" | "status">, now: number): boolean {97 return a.status === "pending" && !countdown(a, now).expired;98}99100/** The `detail` object of a request as label/value pairs a person can read. */101export function approvalDetails(a: Pick<Approval, "action" | "detail">): Array<{ label: string; value: string }> {102 const d = a.detail ?? {};103 const out: Array<{ label: string; value: string }> = [];104 for (const [k, v] of Object.entries(d)) {105 if (v == null || v === "") continue;106 if (k === "maxCostUsd" && typeof v === "number") out.push({ label: "Nouveau plafond de coût", value: formatCost(v) });107 else if (k === "maxMinutes" && typeof v === "number") out.push({ label: "Nouvelle durée maximale", value: `${formatNumber(v, { decimals: 0 })} min` });108 else if (k === "host" || k === "hosts" || k === "destination" || k === "url") out.push({ label: "Destination", value: Array.isArray(v) ? v.join(", ") : String(v) });109 else if (k === "purpose") out.push({ label: "But", value: String(v) });110 else if (k === "path" || k === "script") out.push({ label: "Script", value: String(v) });111 else if (k === "label") out.push({ label: "Exécution concernée", value: String(v) });112 else if (k === "code") out.push({ label: "Programme", value: v === "(inline code)" ? "code fourni par l'agent, visible dans l'onglet Code une fois lancé" : String(v) });113 else out.push({ label: k, value: typeof v === "object" ? JSON.stringify(v) : String(v) });114 }115 return out;116}117118export const APPROVAL_STATUS_LABEL: Record<string, string> = { pending: "En attente de votre réponse", approved: "Autorisé", denied: "Refusé", expired: "Sans réponse : refusé" };119