OverviewFilesCommitsIssuesDocsReleasesPeers

ollama_semantic_function_calling / ollamatools-new / tests / update_both_identities.js

127 lines · javascript ref:
#!/usr/bin/env node
/**
 * Script to update both Keet identity files to be real identities
 * This updates both:
 * 1. The main identity file (storage/identity.json)
 * 2. The database identity file (storage/db/identity.json)
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { parseArgs } from 'node:util';

// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Configure logging
function log(level, message) {
  const timestamp = new Date().toISOString();
  console.log(`[${timestamp}] [${level}] ${message}`);
}

// Parse command-line arguments
function parseArguments() {
  const options = {
    storage: { type: 'string' },
    name: { type: 'string' },
    verbose: { type: 'boolean', default: false }
  };
  
  const { values } = parseArgs({ options });
  return values;
}

// Main function
async function main() {
  try {
    // Parse command-line arguments
    const args = parseArguments();
    
    // Create a storage directory if it doesn't exist
    const storagePath = args.storage || path.join(__dirname, '..', 'test-keet-storage');
    if (!fs.existsSync(storagePath)) {
      fs.mkdirSync(storagePath, { recursive: true });
    }
    
    // Create db directory if it doesn't exist
    const dbPath = path.join(storagePath, 'db');
    if (!fs.existsSync(dbPath)) {
      fs.mkdirSync(dbPath, { recursive: true });
    }
    
    // Path to the main identity file
    const mainIdentityPath = path.join(storagePath, 'identity.json');
    
    // Path to the database identity file
    const dbIdentityPath = path.join(dbPath, 'identity.json');
    
    // Check if the main identity file exists
    if (!fs.existsSync(mainIdentityPath)) {
      log('ERROR', `Main identity file not found at: ${mainIdentityPath}`);
      return 1;
    }
    
    // Read the main identity file
    log('INFO', `Reading main identity from: ${mainIdentityPath}`);
    const mainIdentity = JSON.parse(fs.readFileSync(mainIdentityPath, 'utf8'));
    log('INFO', `Current main identity: ${JSON.stringify(mainIdentity)}`);
    
    // Update the main identity
    const updatedMainIdentity = {
      ...mainIdentity,
      anonymous: false // Set to false for a real identity
    };
    
    // Update the display name if provided
    if (args.name) {
      updatedMainIdentity.displayName = args.name;
    }
    
    // Write the updated main identity back to the file
    log('INFO', `Writing updated main identity to: ${mainIdentityPath}`);
    fs.writeFileSync(mainIdentityPath, JSON.stringify(updatedMainIdentity));
    log('INFO', `Updated main identity: ${JSON.stringify(updatedMainIdentity)}`);
    
    // Create or update the database identity file
    let dbIdentity = {};
    if (fs.existsSync(dbIdentityPath)) {
      log('INFO', `Reading database identity from: ${dbIdentityPath}`);
      dbIdentity = JSON.parse(fs.readFileSync(dbIdentityPath, 'utf8'));
      log('INFO', `Current database identity: ${JSON.stringify(dbIdentity)}`);
    }
    
    // Update the database identity to match the main identity
    const updatedDbIdentity = {
      id: updatedMainIdentity.memberId,
      name: updatedMainIdentity.displayName,
      anonymous: false
    };
    
    // Write the updated database identity back to the file
    log('INFO', `Writing updated database identity to: ${dbIdentityPath}`);
    fs.writeFileSync(dbIdentityPath, JSON.stringify(updatedDbIdentity));
    log('INFO', `Updated database identity: ${JSON.stringify(updatedDbIdentity)}`);
    
    log('INFO', 'Both identity files updated successfully!');
    return 0;
  } catch (error) {
    log('ERROR', `Error updating identities: ${error.message}`);
    log('ERROR', error.stack);
    return 1;
  }
}

// Run the main function if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
  try {
    const exitCode = await main();
    process.exit(exitCode);
  } catch (error) {
    log('ERROR', `Unhandled error: ${error.message}`);
    log('ERROR', error.stack);
    process.exit(1);
  }
}