-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOFv3_Core.js
More file actions
245 lines (206 loc) · 7.65 KB
/
Copy pathIOFv3_Core.js
File metadata and controls
245 lines (206 loc) · 7.65 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* IOF v3 — STATION 2 (FINAL UNIVERSAL BUILD)
* --------------------------------------------------
* Portable • Deterministic • AI-Readable • Modular
* --------------------------------------------------
* "The right frequency changes everything."
*/
// ─────────────────────────────────────────────
// 🔹 CORE CONSTANTS
// ─────────────────────────────────────────────
const IOF_CONSTANTS = {
MAX_VELOCITY: 2,
SPRING: 2.0,
DAMPING: 0.92,
STEP: 0.12,
KICK: 0.08
};
// ─────────────────────────────────────────────
// 🔹 PALINDROME BUFFER (AI-READABLE MEMORY)
// ─────────────────────────────────────────────
class PalindromeBuffer {
constructor(capacity = 128) {
this.capacity = capacity;
this.primary = new Array(capacity);
this.mirror = new Array(capacity);
this.cursor = 0;
}
classifyIntent(reason) {
if (!reason) return "unknown";
if (reason.includes("manual")) return "user_input";
if (reason.includes("auto")) return "system";
return "external";
}
write(key, value, prev, reason = "system") {
const ts = performance.now();
const delta = {
key,
from: prev,
to: value,
delta: (typeof value === "number" && typeof prev === "number") ? value - prev : null,
intent: this.classifyIntent(reason),
reason,
ts,
idx: this.cursor
};
this.primary[this.cursor % this.capacity] = { key, value, ts };
this.mirror[this.cursor % this.capacity] = delta;
this.cursor++;
return delta;
}
read(n = 25) {
const out = [];
const start = Math.max(0, this.cursor - n);
for (let i = start; i < this.cursor; i++) {
const d = this.mirror[i % this.capacity];
if (d) out.push(d);
}
return out;
}
}
// ─────────────────────────────────────────────
// 🔹 FLUX ENGINE (DETERMINISTIC CORE)
// ─────────────────────────────────────────────
class FluxEngine {
constructor(seedState) {
this.values = seedState || { F: 0.72, L: 0.45, U: 0.88, X: 0.61 };
this.targets = { ...this.values };
this.velocity = Object.fromEntries(Object.keys(this.values).map(k => [k, 0]));
this.buffer = new PalindromeBuffer(128);
this.subscribers = new Set();
this.lastTime = performance.now();
this.running = false;
}
// ───── Subscription Layer ─────
subscribe(fn) {
this.subscribers.add(fn);
return () => this.subscribers.delete(fn);
}
notify() {
const snapshot = this.getState();
this.subscribers.forEach(fn => fn(snapshot));
}
// ───── Core State ─────
getState() {
return {
values: { ...this.values },
targets: { ...this.targets },
deltas: this.buffer.read(20),
overall: this.getOverall(),
resonance: this.getResonance(),
timestamp: performance.now()
};
}
getOverall() {
const vals = Object.values(this.values);
return vals.reduce((a, b) => a + b, 0) / vals.length;
}
getResonance() {
const vals = Object.values(this.values);
const mean = this.getOverall();
const variance = vals.reduce((a, v) => a + Math.pow(v - mean, 2), 0) / vals.length;
return 1 - Math.min(1, variance * 4);
}
// ───── Input Layer ─────
nudge(key, dir = 1, reason = "manual-nudge") {
const prev = this.targets[key];
const next = Math.max(0, Math.min(1, prev + dir * IOF_CONSTANTS.STEP));
this.targets[key] = next;
this.velocity[key] += dir * IOF_CONSTANTS.KICK;
this.buffer.write(key, next, prev, reason);
this.notify();
}
setTarget(key, value, reason = "external-set") {
const prev = this.targets[key];
this.targets[key] = Math.max(0, Math.min(1, value));
this.buffer.write(key, this.targets[key], prev, reason);
this.notify();
}
// ───── Simulation Loop ─────
step(dt) {
for (const key in this.values) {
const dist = this.targets[key] - this.values[key];
const spring = dist * IOF_CONSTANTS.SPRING;
const damping = this.velocity[key] * IOF_CONSTANTS.DAMPING;
this.velocity[key] += (spring - damping) * dt;
// Clamp velocity
this.velocity[key] = Math.max(
-IOF_CONSTANTS.MAX_VELOCITY,
Math.min(IOF_CONSTANTS.MAX_VELOCITY, this.velocity[key])
);
this.values[key] += this.velocity[key] * dt;
// Clamp value
this.values[key] = Math.max(0, Math.min(1, this.values[key]));
}
}
loop = () => {
if (!this.running) return;
const now = performance.now();
const dt = (now - this.lastTime) / 1000;
this.lastTime = now;
this.step(dt);
this.notify();
requestAnimationFrame(this.loop);
};
start() {
if (!this.running) {
this.running = true;
this.lastTime = performance.now();
this.loop();
}
}
stop() {
this.running = false;
}
}
// ─────────────────────────────────────────────
// 🔹 UNIVERSAL PROTOCOL (AI INTEROP)
// ─────────────────────────────────────────────
const IOF_PROTOCOL = {
name: "IOFv3",
version: "3.0",
schema: {
values: "0..1 normalized axes",
targets: "desired state",
deltas: "change log with intent",
resonance: "system coherence metric"
},
actions: ["nudge", "setTarget", "subscribe"]
};
// ─────────────────────────────────────────────
// 🔹 OPTIONAL REACT UI (PLUG-IN)
// ─────────────────────────────────────────────
export function createIOFReactComponent(React) {
const { useState, useEffect, useRef } = React;
return function IOFDashboard() {
const engineRef = useRef(new FluxEngine());
const [state, setState] = useState(engineRef.current.getState());
useEffect(() => {
const engine = engineRef.current;
const unsub = engine.subscribe(setState);
engine.start();
return () => { unsub(); engine.stop(); };
}, []);
const AXES = ["F", "L", "U", "X"];
return React.createElement("div", { style: { padding: 20, fontFamily: "monospace", background: "#0a0a0a", color: "#fff" } },
AXES.map(k =>
React.createElement("div", { key: k },
`${k}: ${(state.values[k] * 100).toFixed(1)}% `,
React.createElement("button", { onClick: () => engineRef.current.nudge(k, -1) }, "-"),
React.createElement("button", { onClick: () => engineRef.current.nudge(k, 1) }, "+")
)
),
React.createElement("div", null, `Resonance: ${(state.resonance * 100).toFixed(1)}%`)
);
};
}
// ─────────────────────────────────────────────
// 🔹 AUTO-BOOT (BROWSER SAFE)
// ─────────────────────────────────────────────
if (typeof window !== "undefined") {
window.IOFv3 = {
FluxEngine,
PalindromeBuffer,
PROTOCOL: IOF_PROTOCOL
};
}