@neuroequality/neuroadapt-core
Version:
Core sensory, cognitive, and preference management for NeuroAdapt SDK
150 lines (149 loc) • 4.01 kB
JavaScript
import { SCHEMA_VERSION } from "./schemas.js";
class MigrationRegistry {
constructor() {
this.migrations = /* @__PURE__ */ new Map();
}
/**
* Register a migration from one version to another
*/
registerMigration(fromVersion, migration) {
this.migrations.set(fromVersion, migration);
}
/**
* Apply migrations to bring preferences to current schema version
*/
async migrate(preferences) {
if (!preferences || typeof preferences !== "object" || preferences === null) {
return this.getDefaultPreferences();
}
const prefs = preferences;
let currentVersion = prefs.schemaVersion;
if (!currentVersion) {
currentVersion = "1.0.0";
}
if (currentVersion === SCHEMA_VERSION) {
return this.ensureValidStructure(prefs);
}
let migratedPrefs = prefs;
const migrationPath = this.getMigrationPath(currentVersion, SCHEMA_VERSION);
for (const version of migrationPath) {
const migration = this.migrations.get(version);
if (migration) {
migratedPrefs = await migration(migratedPrefs);
}
}
const result = migratedPrefs;
result.schemaVersion = SCHEMA_VERSION;
result.lastModified = (/* @__PURE__ */ new Date()).toISOString();
return this.ensureValidStructure(result);
}
/**
* Get the migration path from source to target version
*/
getMigrationPath(fromVersion, toVersion) {
const versions = ["1.0.0", "1.0.1", "1.1.0"];
const fromIndex = versions.indexOf(fromVersion);
const toIndex = versions.indexOf(toVersion);
if (fromIndex === -1 || toIndex === -1 || fromIndex >= toIndex) {
return [];
}
return versions.slice(fromIndex, toIndex);
}
/**
* Ensure preferences have valid structure with defaults for missing fields
*/
ensureValidStructure(preferences) {
const defaults = this.getDefaultPreferences();
return {
...preferences,
schemaVersion: SCHEMA_VERSION,
lastModified: (/* @__PURE__ */ new Date()).toISOString(),
sensory: {
...defaults.sensory,
...preferences.sensory
},
cognitive: {
...defaults.cognitive,
...preferences.cognitive
},
ai: {
...defaults.ai,
...preferences.ai
},
vr: {
...defaults.vr,
...preferences.vr
}
};
}
/**
* Get default preferences for current schema version
*/
getDefaultPreferences() {
return {
schemaVersion: SCHEMA_VERSION,
lastModified: (/* @__PURE__ */ new Date()).toISOString(),
sensory: {
motionReduction: false,
highContrast: false,
colorVisionFilter: "none",
fontSize: 1,
reducedFlashing: false,
darkMode: false
},
cognitive: {
readingSpeed: "medium",
explanationLevel: "detailed",
processingPace: "standard",
chunkSize: 5,
allowInterruptions: true,
preferVisualCues: false
},
ai: {
tone: "neutral",
responseLength: "standard",
consistencyLevel: "moderate",
useAnalogies: true,
allowUndo: true
},
vr: {
comfortRadius: 1.5,
safeSpaceEnabled: true,
locomotionType: "comfort",
personalSpace: 1,
panicButtonEnabled: true
}
};
}
}
const defaultMigrationRegistry = new MigrationRegistry();
defaultMigrationRegistry.registerMigration("1.0.0", (prefs) => {
const p = prefs;
return {
...p,
vr: {
comfortRadius: 1.5,
safeSpaceEnabled: true,
locomotionType: "comfort",
personalSpace: 1,
panicButtonEnabled: true,
...p.vr || {}
}
};
});
defaultMigrationRegistry.registerMigration("1.0.1", (prefs) => {
const p = prefs;
const ai = p.ai || {};
return {
...p,
ai: {
...ai,
consistencyLevel: ai.consistencyLevel || "moderate"
}
};
});
export {
MigrationRegistry,
defaultMigrationRegistry
};
//# sourceMappingURL=migration.js.map