refactor and add list_snippets

This commit is contained in:
pablof7z
2025-03-31 12:26:50 +01:00
parent 6d0e5a8e79
commit f74736191a
15 changed files with 494 additions and 190 deletions

View File

@@ -1,43 +1,32 @@
import { Command } from 'commander';
import { ndk } from '../ndk.js';
import { knownUsers } from '../users.js';
import { identifierToPubkeys } from '../lib/nostr/utils.js';
import { Command } from "commander";
import { toPubkeys, formatUser } from "../lib/converters/index.js";
export function registerFindUserCommand(program: Command): void {
program
.command('find-user')
.description('Find a user by identifier')
.argument('<query>', 'User identifier to search for')
.action(async (query: string) => {
try {
const pubkeys = identifierToPubkeys(query);
if (pubkeys.length > 0) {
const result = pubkeys.map(formatUser).join('\n\n---\n\n');
console.log(result);
} else {
console.log("No user found matching the query.");
}
} catch (error) {
console.error('Error executing find-user command:', error);
process.exit(1);
// Create a command for finding a user
const findUserCommand = new Command("find-user")
.description("Find a user by name, npub, or other profile information")
.argument("<query>", "The search query to find a user")
.action(async (query: string) => {
try {
// Find matching pubkeys
const pubkeys = toPubkeys(query);
if (pubkeys.length === 0) {
console.log(`No users found matching query: ${query}`);
return;
}
});
}
// Helper function to format user profiles
function formatUser(pubkey: string) {
const profile = knownUsers[pubkey]?.profile;
const user = ndk.getUser({ pubkey });
const keys: Record<string, string> = {
Npub: user.npub,
};
// Format and display each matching user
console.log(`Found ${pubkeys.length} matching users:`);
for (let i = 0; i < pubkeys.length; i++) {
if (i > 0) console.log("\n---\n");
const pubkey = pubkeys[i];
if (pubkey) {
console.log(formatUser(pubkey));
}
}
} catch (error) {
console.error("Error:", error);
}
});
if (profile?.name) keys.Name = profile.name;
if (profile?.about) keys.About = profile.about;
if (profile?.picture) keys.Picture = profile.picture;
return Object.entries(keys)
.map(([key, value]) => `${key}: ${value}`)
.join("\n");
}
export default findUserCommand;

View File

@@ -1,5 +1,4 @@
import { Command } from 'commander';
import { registerFindUserCommand } from './find-user.js';
import { registerFindSnippetsCommand } from './find-snippets.js';
import { registerWotCommand } from './wot.js';
import { registerListUsernamesCommand } from './list-usernames.js';
@@ -17,7 +16,6 @@ program
// Register all commands
registerMcpCommand(program);
registerFindUserCommand(program);
registerFindSnippetsCommand(program);
registerWotCommand(program);
registerListUsernamesCommand(program);

View File

@@ -1,4 +1,4 @@
import { Command } from 'commander';
import type { Command } from 'commander';
import { readConfig } from "../config.js";
import { addCreatePubkeyCommand } from "../logic/create-pubkey.js";
import { addFindSnippetsCommand } from "../logic/find_snippets.js";
@@ -6,6 +6,8 @@ import { addFindUserCommand } from "../logic/find_user.js";
import { addListUsernamesCommand } from "../logic/list_usernames.js";
import { addPublishCodeSnippetCommand } from "../logic/publish-code-snippet.js";
import { addPublishCommand } from "../logic/publish.js";
import { addListSnippetsCommand } from "../logic/list_snippets.js";
import { addFetchSnippetByIdCommand } from "../logic/fetch_snippet_by_id.js";
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -20,6 +22,8 @@ const commandMap: Record<string, CommandFunction> = {
"find-user": addFindUserCommand,
"find-snippets": addFindSnippetsCommand,
"list-usernames": addListUsernamesCommand,
"list-snippets": addListSnippetsCommand,
"fetch-snippet-by-id": addFetchSnippetByIdCommand,
};
// Global server instance
@@ -70,5 +74,7 @@ export function registerMcpCommands(server: McpServer) {
addFindUserCommand(server);
addFindSnippetsCommand(server);
addListUsernamesCommand(server);
addListSnippetsCommand(server);
addFetchSnippetByIdCommand(server);
}
}