File size: 5,553 Bytes
c79d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * @license
 * SPDX-License-Identifier: Apache-2.0
 *
 * Fractal‑Syntax‑Forge Engine
 * Engineered by Jamesβ€―BurvelΒ O'Callaghanβ€―III for Citibankβ€―Demoβ€―Businessβ€―Inc.
 *
 * Mission: Birth β€œ**FGL – FractalΒ GenesisΒ Language**”, a meta‑programming tongue for
 * engineering reality layers, holographic simulations, and 13‑D hyperframes.
 *
 * Paste into any TS‑aware bundler, supply your Gemini‑compatible key via
 * `process.env.API_KEY`, open in‑browser, and watch constructs manifest.
 */

import { marked } from 'marked';
import OpenAI, {
  type ClientOptions,
  type ChatCompletionMessageParam,
} from 'openai';

// ──────────────────────────────────────────────────────────────
// ENV + OpenAI init
// ──────────────────────────────────────────────────────────────

const API_KEY = process.env.API_KEY;
if (!API_KEY) throw new Error('Missing API_KEY env var');

const openai = new OpenAI({
  apiKey: API_KEY,
  baseURL: 'https://generativelanguage.googleapis.com/v1beta/openai/',
  dangerouslyAllowBrowser: true,
} as ClientOptions);

// ──────────────────────────────────────────────────────────────
// Fractal console
// ──────────────────────────────────────────────────────────────

async function consoleRender(...md: string[]): Promise<void> {
  const el = document.createElement('section');
  Object.assign(el.style, {
    padding: '1.6rem',
    background: 'radial-gradient(circle at top left,#0f172a 0%,#1e293b 40%,#020617 100%)',
    border: '1px solid #6366f1',
    borderRadius: '20px',
    color: '#f1f5f9',
    fontFamily: 'Fira Code, monospace',
    margin: '1.5rem 0',
    lineHeight: '1.6',
    boxShadow: '0 0 30px rgba(99,102,241,0.25)',
  } as Partial<CSSStyleDeclaration>);

  el.innerHTML = (await Promise.all(md.map((m) => marked.parse(m)))).join('');
  document.body.appendChild(el);
}

// ──────────────────────────────────────────────────────────────
// Helpers
// ──────────────────────────────────────────────────────────────

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

async function chat(messages: ChatCompletionMessageParam[]): Promise<string> {
  const rsp = await openai.chat.completions.create({ model: 'gemini-2.0-flash', messages });
  const out = rsp.choices?.[0]?.message?.content ?? '';
  if (!out) throw new Error('LLM yielded empty');
  return out;
}

// ──────────────────────────────────────────────────────────────
// Prompts for FGL
// ──────────────────────────────────────────────────────────────

const SYS_PROMPT =
  'You are FGL‑Core, a cosmic compiler forged by JamesΒ BurvelΒ O’CallaghanΒ III. Your task: crystallise FractalΒ GenesisΒ Language, a DSL for designing holographic realities and 13‑dimensional hyperframes. Emit concise Markdown with syntax blocks, metaphysical notes, and practical simulation hooks.';

const CONSTRUCT_PROMPT = (n: number) => `### ConstructΒ ${n}
Define ConstructΒ ${n} of FGL.
Include:
1. **Syntax pattern** – formal grammar.
2. **Sample \`fgl\` code**.
3. **Underlying metaphysics** – e.g., tesseract mapping, state‑vector displacement.
4. **Applied use‑case** – rendering cosmic scenes, morphing temporal logic, or tuning quantum chips.`;

// ──────────────────────────────────────────────────────────────
// Loop control
// ──────────────────────────────────────────────────────────────

let stop = false;
(window as any).stopLoop = () => (stop = true);

(async () => {
  try {
    await consoleRender('# 🌌 FGL Genesis Console', 'Spinning reality fibres…');

    const MAX = 40; // language constructs
    for (let c = 1; c <= MAX; c++) {
      if (stop) break;

      let backoff = 2500;
      while (true) {
        try {
          const def = await chat([
            { role: 'system', content: SYS_PROMPT },
            { role: 'user', content: CONSTRUCT_PROMPT(c) },
          ]);
          await consoleRender(`---\n\n## πŸŒ€ ConstructΒ ${c}`, def);
          break;
        } catch (e) {
          console.error(`Construct ${c} failed`, e);
          await sleep(backoff);
          backoff = Math.min(backoff * 2, 60000);
        }
      }

      await sleep(4000);
    }

    await consoleRender('✨ **FGL genesis complete. Engage the matrix.**');
  } catch (fatal) {
    await consoleRender('πŸ’£ Fatal:', String(fatal));
  }
})();