From 69b314c831fd412af39a2a632eb7f5affbd667b4 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 21 Oct 2025 21:28:57 +0700 Subject: [PATCH] wip - document DMs to the LLM --- AGENTS.md | 74 +++++++- docs/NOSTR_DIRECT_MESSAGES.md | 330 ++++++++++++++++++++++++++++++++++ src/contexts/DMContext.tsx | 121 +++++++++++++ 3 files changed, 524 insertions(+), 1 deletion(-) create mode 100644 docs/NOSTR_DIRECT_MESSAGES.md diff --git a/AGENTS.md b/AGENTS.md index 548e869..09a9f24 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,9 +38,11 @@ This project is a Nostr client application built with React 18.x, TailwindCSS 3. - `useNWC`: Nostr Wallet Connect connection management - `useNWCContext`: Access NWC context provider - `useShakespeare`: AI chat completions with Shakespeare AI API + - `useDMContext`: Direct messaging system (NIP-04 & NIP-17) + - `useConversationMessages`: Paginated messages for specific conversations - `/src/pages/`: Page components used by React Router (Index, NotFound) - `/src/lib/`: Utility functions and shared logic -- `/src/contexts/`: React context providers (AppContext, NWCContext) +- `/src/contexts/`: React context providers (AppContext, NWCContext, DMContext) - `/src/test/`: Testing utilities including TestApp component - `/public/`: Static assets - `App.tsx`: Main app component with provider setup @@ -109,6 +111,8 @@ The project includes a **`docs/`** directory containing specialized documentatio - **`docs/NOSTR_INFINITE_SCROLL.md`**: Read when building feed interfaces, implementing pagination for Nostr events, or creating social media-style infinite scroll experiences. +- **`docs/NOSTR_DIRECT_MESSAGES.md`**: Read when implementing direct messaging features, building chat interfaces, or working with encrypted peer-to-peer communication (NIP-04 and NIP-17). + ## System Prompt Management The AI assistant's behavior and knowledge is defined by the AGENTS.md file, which serves as the system prompt. To modify the assistant's instructions or add new project-specific guidelines: @@ -744,6 +748,74 @@ function EditProfilePage() { The `EditProfileForm` component displays just the form. It requires no props, and will "just work" automatically. +### Direct Messaging (NIP-04 & NIP-17) + +The project includes a complete direct messaging system with real-time updates, encrypted storage, and support for both NIP-04 (legacy) and NIP-17 (modern private messaging) protocols. + +**Quick Setup:** + +```tsx +import { DMProvider } from '@/contexts/DMContext'; +import { PROTOCOL_MODE } from '@/lib/dmConstants'; + +// Wrap your app with DMProvider + + {children} + +``` + +**Send Messages:** + +```tsx +import { useDMContext } from '@/contexts/DMContext'; +import { MESSAGE_PROTOCOL } from '@/lib/dmConstants'; + +function MyComponent() { + const { sendMessage } = useDMContext(); + + await sendMessage({ + recipientPubkey: 'hex-pubkey', + content: 'Hello!', + protocol: MESSAGE_PROTOCOL.NIP17 // or NIP04 + }); +} +``` + +**Display Conversations:** + +```tsx +const { conversations, isLoading } = useDMContext(); + +// conversations = Array of conversation summaries with: +// - pubkey: The other participant +// - lastMessage: Most recent message +// - lastActivity: Unix timestamp +// - isKnown: User has sent messages +// - isRequest: Only received messages +``` + +**Display Messages:** + +```tsx +import { useConversationMessages } from '@/contexts/DMContext'; + +const { messages, hasMoreMessages, loadEarlierMessages } = useConversationMessages(pubkey); + +// messages = Paginated array of messages (25/page) +// hasMoreMessages = true if earlier messages available +// loadEarlierMessages() = Load next 25 messages +``` + +**Features:** +- **Dual Protocol Support**: NIP-04 (legacy) and NIP-17 (private with gift wrapping) +- **Real-time Updates**: WebSocket subscriptions for live messages +- **Encrypted Storage**: IndexedDB with NIP-44 encryption +- **Optimistic UI**: Instant message display with background confirmation +- **File Attachments**: Full support with `attachments` parameter +- **Cache-First Loading**: Instant UI with background sync + +For complete implementation details, file attachments, and advanced features, see **`docs/NOSTR_DIRECT_MESSAGES.md`**. + ### Uploading Files on Nostr Use the `useUploadFile` hook to upload files. This hook uses Blossom servers for file storage and returns NIP-94 compatible tags. diff --git a/docs/NOSTR_DIRECT_MESSAGES.md b/docs/NOSTR_DIRECT_MESSAGES.md new file mode 100644 index 0000000..a75092c --- /dev/null +++ b/docs/NOSTR_DIRECT_MESSAGES.md @@ -0,0 +1,330 @@ +### Direct Messaging on Nostr + +This project includes a complete direct messaging system supporting both NIP-04 (legacy) and NIP-17 (modern, - more - private) encrypted messages with real-time subscriptions, optimistic updates, and a persistent - cache first - local storage. + +## Quick Start + +### 1. Wrap Your App with DMProvider + +```tsx +import { DMProvider } from '@/contexts/DMContext'; +import { PROTOCOL_MODE } from '@/lib/dmConstants'; + +function App() { + return ( + + {/* Your app components */} + + ); +} +``` + +**Protocol Modes:** +- `PROTOCOL_MODE.NIP04_ONLY` - Legacy encryption only +- `PROTOCOL_MODE.NIP17_ONLY` - Modern private messages (recommended) +- `PROTOCOL_MODE.BOTH` - Support both protocols + +### 2. Send Messages + +```tsx +import { useDMContext } from '@/contexts/DMContext'; +import { MESSAGE_PROTOCOL } from '@/lib/dmConstants'; + +function ComposeMessage({ recipientPubkey }: { recipientPubkey: string }) { + const { sendMessage } = useDMContext(); + const [content, setContent] = useState(''); + + const handleSend = async () => { + await sendMessage({ + recipientPubkey, + content, + protocol: MESSAGE_PROTOCOL.NIP17, // Uses NIP-44 encryption + gift wrapping + }); + setContent(''); + }; + + return ( +
{ e.preventDefault(); handleSend(); }}> +