From 682bb924ca1282e91ce2507c476a905d54a0ee5e Mon Sep 17 00:00:00 2001 From: Alex Komoroske Date: Sat, 22 Jul 2023 15:41:33 -0700 Subject: [PATCH] ProfileFilesystem now stores a metadata file at its root. For now it's just a simple version check, but soon it will have other things. Part of #14. Part of #45. --- tools/profile_filesystem.ts | 55 ++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/tools/profile_filesystem.ts b/tools/profile_filesystem.ts index de05a60..10aec9b 100644 --- a/tools/profile_filesystem.ts +++ b/tools/profile_filesystem.ts @@ -10,6 +10,10 @@ import { StoreValue } from '../src/types.js'; +import { + StoreFilesystem +} from './store_filesystem.js'; + import { Embedding } from '../src/embedding.js'; @@ -22,26 +26,75 @@ import { ensureFolder } from './util.js'; +import { + z +} from 'zod'; + import fs from 'fs'; import path from 'path'; import inquirer from 'inquirer'; -import { StoreFilesystem } from './store_filesystem.js'; +import { Garden } from '../src/garden.js'; const PROFILE_DIRECTORY = '.profiles/'; const LOG_FILE = 'console.log'; +const METADATA_FILE = 'info.json'; + +const CURRENT_PROFILE_VERSION = 0; + +const profileMetadata = z.object({ + version: z.literal(0), +}); + +type ProfileMetadata = z.infer; export class ProfileFilesystem extends Profile { //basetype has a ._memories of a diffeerent type _associativeMemories : {[name : MemoryID]: AssociativeMemory}; _storeFilesystems : {[name : StoreID] : StoreFilesystem}; + _loaded : boolean; constructor() { super(); this._associativeMemories = {}; this._storeFilesystems = {}; + this._loaded = false; + //We can't call _loadMetadata yet because garden hasn't been set + } + + override set garden(val : Garden) { + super.garden = val; + this._loadMetadata(); + } + + override get garden() : Garden | undefined { + //Note that apparently according to the ES spec, if you override the setter you also have to override the getter. + return super.garden; + } + + _loadMetadata() : void { + + const metadataFile = path.join(this._profileDir, METADATA_FILE); + if (!fs.existsSync(metadataFile)) { + this._saveMetadata(); + } + + //For now all we do is validate the version is right. + const data = fs.readFileSync(metadataFile).toString(); + const parsedData = profileMetadata.parse(JSON.parse(data)); + if (parsedData.version != CURRENT_PROFILE_VERSION) throw new Error('Profile has a different version than expected'); + this._loaded = true; + } + + _saveMetadata() : void { + const metadataFile = path.join(this._profileDir, METADATA_FILE); + const data : ProfileMetadata = { + version: 0 + }; + ensureFolder(this._profileDir); + fs.writeFileSync(metadataFile, JSON.stringify(data, null, '\t')); } override async localFetch(location : string) : Promise {