askexperts
Version:
AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol
109 lines • 4.02 kB
JavaScript
import { debugError, enableAllDebug, enableErrorDebug } from "../../../common/debug.js";
import { getWalletByNameOrDefault } from "../../commands/wallet/utils.js";
import { addRemoteOptions } from "./index.js";
import { createDBClientForCommands } from "../utils.js";
/**
* Update an existing expert in the database
*
* @param pubkey Public key of the expert to update
* @param options Command options
*/
export async function updateExpert(pubkey, options) {
try {
if (options.debug)
enableAllDebug();
else
enableErrorDebug();
// Get expert client
const db = await createDBClientForCommands(options);
// Get the expert
const expert = await db.getExpert(pubkey);
if (!expert) {
throw new Error(`Expert with pubkey ${pubkey} not found`);
}
// Track if any changes were made
let changes = false;
// Update wallet if specified
if (options.wallet) {
const wallet = await getWalletByNameOrDefault(db, options.wallet);
expert.wallet_id = wallet.id;
changes = true;
}
// Update type if specified
if (options.type) {
// Validate type
if (options.type !== "nostr" && options.type !== "openai") {
throw new Error('Type must be either "nostr" or "openai"');
}
expert.type = options.type;
changes = true;
}
// Update nickname if specified
if (options.nickname) {
expert.nickname = options.nickname;
changes = true;
}
// Update environment variables if specified
if (options.env) {
expert.env = options.env.join("\n");
changes = true;
}
// Update docstores if specified
if (options.docstores) {
expert.docstores = options.docstores.join(",");
changes = true;
}
// Only update if changes were made
if (!changes) {
console.log("No changes specified. Expert not updated.");
return;
}
// Update expert in database
const success = await db.updateExpert(expert);
if (!success) {
throw new Error("Failed to update expert in database");
}
console.log(`Expert updated successfully:`);
console.log(` Pubkey: ${expert.pubkey}`);
console.log(` Type: ${expert.type}`);
console.log(` Nickname: ${expert.nickname}`);
console.log(` Wallet ID: ${expert.wallet_id}`);
if (expert.env)
console.log(` Environment Variables: ${expert.env.replace(/\n/g, ", ")}`);
if (expert.docstores)
console.log(` Docstores: ${expert.docstores}`);
}
catch (error) {
debugError("Error updating expert:", error);
throw error;
}
}
/**
* Register the update expert command with the CLI
*
* @param program The commander program or parent command
*/
export function registerUpdateCommand(program) {
const command = program
.command("update")
.description("Update an existing expert in the database")
.argument("<pubkey>", "Public key of the expert to update")
.option("-w, --wallet <name>", "Wallet name to use")
.option("-t, --type <type>", "Type of expert (nostr or openai)")
.option("-n, --nickname <name>", "Nickname for the expert")
.option("-e, --env <key:value...>", "Environment variables in key:value format")
.option("-s, --docstores <id...>", "Docstore IDs to use")
.option("-d, --debug", "Enable debug logging")
.action(async (pubkey, options) => {
try {
await updateExpert(pubkey, options);
}
catch (error) {
debugError("Error updating expert:", error);
process.exit(1);
}
});
// Add remote options
addRemoteOptions(command);
}
//# sourceMappingURL=update.js.map