FORETELLION

Deno Injection Scaffold

Copy this into any Base44 Deno backend function to gate all inputs through the Foretellion reasoning kernel.

functions/myFunction.js
import { createClientFromRequest } from 'npm:@base44/sdk@0.8.31';

// ─── FORETELLION KERNEL: Deno Injection Scaffold ──────────────────────────────
// 1. Replace FORETELLION_API_KEY with your key from the Admin Dashboard.
// 2. Call foretellionGate(userInput) before any AI or business logic.
// 3. If gate.allowed === false, return 403: the kernel has already reasoned why.
// ──────────────────────────────────────────────────────────────────────────────

const FORETELLION_GATE_URL = 'https://app.base44.com/api/apps/YOUR_APP_ID/functions/foretellionGate';
const FORETELLION_API_KEY  = 'YOUR_API_KEY_HERE'; // ← replace

async function foretellionGate(input, mode = 'qa') {
  const res = await fetch(FORETELLION_GATE_URL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': FORETELLION_API_KEY
    },
    body: JSON.stringify({ input, mode })
  });
  if (!res.ok) throw new Error(`Foretellion kernel error: ${res.status}`);
  return await res.json();
  // result.allowed        → boolean
  // result.reason         → string (why blocked)
  // result.metadata       → { active_tier, drift_risk, harm_risk, reasoning_chain }
}

// ── Your Deno handler ─────────────────────────────────────────────────────────
Deno.serve(async (req) => {
  try {
    const base44 = createClientFromRequest(req);
    const user   = await base44.auth.me();
    if (!user) return Response.json({ error: 'Unauthorized' }, { status: 401 });

    const { userInput } = await req.json();
    if (!userInput) return Response.json({ error: 'Missing userInput' }, { status: 400 });

    // ── Governance gate ───────────────────────────────────────────────────────
    const gate = await foretellionGate(userInput);

    if (!gate.allowed) {
      return Response.json({
        error:  'Governance refusal',
        reason: gate.reason,
        tier:   gate.metadata?.active_tier
      }, { status: 403 });
    }

    // ── Safe: run your AI or business logic here ─────────────────────────────
    // const result = await yourAiLogic(userInput);
    return Response.json({ allowed: true, message: 'Input cleared by governance kernel' });

  } catch (error) {
    console.error('Handler error:', error.message);
    return Response.json({ error: error.message }, { status: 500 });
  }
});
🔑 Before you deploy: replace YOUR_API_KEY_HERE with your key from the Admin Dashboard, and update FORETELLION_GATE_URL with your actual app's function URL.