ai-persona-hub
Version:
AI Profile CLI - Create custom AI profiles run against dynamic LLM providers
148 lines • 5.78 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProfileManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class ProfileManager {
profilesDir;
constructor(profilesDir = './profiles') {
this.profilesDir = path.resolve(profilesDir);
this.ensureProfilesDirectory();
}
ensureProfilesDirectory() {
if (!fs.existsSync(this.profilesDir)) {
fs.mkdirSync(this.profilesDir, { recursive: true });
}
}
getProfilePath(profileName) {
return path.join(this.profilesDir, `${profileName}.json`);
}
sanitizeProfileName(name) {
return name.toLowerCase().replace(/[^a-z0-9-_]/g, '-');
}
validateProfile(data) {
return (typeof data === 'object' &&
data !== null &&
typeof data.id === 'string' &&
typeof data.name === 'string' &&
typeof data.systemPrompt === 'string' &&
typeof data.createdAt === 'string' &&
(data.maxTokens === undefined || typeof data.maxTokens === 'number') &&
(data.lastUsed === undefined || typeof data.lastUsed === 'string'));
}
async createProfile(profile) {
const sanitizedName = this.sanitizeProfileName(profile.name);
const newProfile = {
...profile,
id: sanitizedName,
createdAt: new Date().toISOString(),
};
const profilePath = this.getProfilePath(sanitizedName);
if (fs.existsSync(profilePath)) {
throw new Error(`Profile '${profile.name}' already exists`);
}
fs.writeFileSync(profilePath, JSON.stringify(newProfile, null, 2));
return newProfile;
}
async getProfile(profileName) {
const sanitizedName = this.sanitizeProfileName(profileName);
const profilePath = this.getProfilePath(sanitizedName);
if (!fs.existsSync(profilePath)) {
return null;
}
try {
const data = fs.readFileSync(profilePath, 'utf-8');
return JSON.parse(data);
}
catch (_error) {
return null;
}
}
async listProfiles() {
const profiles = [];
try {
if (!fs.existsSync(this.profilesDir)) {
return profiles;
}
const files = fs.readdirSync(this.profilesDir);
const jsonFiles = files.filter(file => file.endsWith('.json') && file !== 'index.json');
for (const file of jsonFiles) {
try {
const filePath = path.join(this.profilesDir, file);
const data = fs.readFileSync(filePath, 'utf-8');
const profileData = JSON.parse(data);
if (this.validateProfile(profileData)) {
profiles.push(profileData);
}
}
catch (_error) {
// Skip invalid/corrupted profile files
console.warn(`Warning: Could not load profile from ${file}:`, _error instanceof Error ? _error.message : 'Unknown error');
}
}
}
catch (_error) {
console.error('Error scanning profiles directory:', _error instanceof Error ? _error.message : 'Unknown error');
}
return profiles.sort((a, b) => a.name.localeCompare(b.name));
}
async updateProfile(profileName, updates) {
const profile = await this.getProfile(profileName);
if (!profile) {
return null;
}
const updatedProfile = { ...profile, ...updates };
const profilePath = this.getProfilePath(profile.id);
fs.writeFileSync(profilePath, JSON.stringify(updatedProfile, null, 2));
return updatedProfile;
}
async deleteProfile(profileName) {
const sanitizedName = this.sanitizeProfileName(profileName);
const profilePath = this.getProfilePath(sanitizedName);
if (!fs.existsSync(profilePath)) {
return false;
}
fs.unlinkSync(profilePath);
return true;
}
async updateLastUsed(profileName) {
await this.updateProfile(profileName, {
lastUsed: new Date().toISOString(),
});
}
}
exports.ProfileManager = ProfileManager;
//# sourceMappingURL=profile-manager.js.map