mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 08:47:23 +00:00
agent instructions to assist with AI 1-shot
This commit is contained in:
1 parent
c669dd3604
commit
6ffa656680
1 file changed
+119
-3
@@ -863,7 +863,7 @@ function AIChat() {
|
|||||||
|
|
||||||
const handleSend = async () => {
|
const handleSend = async () => {
|
||||||
if (!input.trim()) return;
|
if (!input.trim()) return;
|
||||||
|
|
||||||
const newMessages = [...messages, { role: 'user', content: input }];
|
const newMessages = [...messages, { role: 'user', content: input }];
|
||||||
setMessages(newMessages);
|
setMessages(newMessages);
|
||||||
setInput('');
|
setInput('');
|
||||||
@@ -880,7 +880,7 @@ function AIChat() {
|
|||||||
return (
|
return (
|
||||||
<div className="max-w-2xl mx-auto p-4">
|
<div className="max-w-2xl mx-auto p-4">
|
||||||
{error && <div className="text-red-500 mb-4">{error}</div>}
|
{error && <div className="text-red-500 mb-4">{error}</div>}
|
||||||
|
|
||||||
<div className="space-y-2 mb-4">
|
<div className="space-y-2 mb-4">
|
||||||
{messages.map((msg, i) => (
|
{messages.map((msg, i) => (
|
||||||
<div key={i} className={`p-2 rounded ${msg.role === 'user' ? 'bg-blue-100' : 'bg-gray-100'}`}>
|
<div key={i} className={`p-2 rounded ${msg.role === 'user' ? 'bg-blue-100' : 'bg-gray-100'}`}>
|
||||||
@@ -888,7 +888,7 @@ function AIChat() {
|
|||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<input
|
<input
|
||||||
value={input}
|
value={input}
|
||||||
@@ -1044,6 +1044,122 @@ The router includes automatic scroll-to-top functionality and a 404 NotFound pag
|
|||||||
- Comprehensive provider setup with NostrLoginProvider, QueryClientProvider, and custom AppProvider
|
- Comprehensive provider setup with NostrLoginProvider, QueryClientProvider, and custom AppProvider
|
||||||
- **Never use the `any` type**: Always use proper TypeScript types for type safety
|
- **Never use the `any` type**: Always use proper TypeScript types for type safety
|
||||||
|
|
||||||
|
## Implementation Patterns and Best Practices
|
||||||
|
|
||||||
|
### Dialog Component Patterns
|
||||||
|
|
||||||
|
When using Dialog components, always ensure accessibility compliance by including required elements:
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog";
|
||||||
|
|
||||||
|
// ✅ Correct - Always include DialogHeader with DialogTitle
|
||||||
|
<Dialog open={isOpen} onOpenChange={onClose}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Dialog Title</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
Optional description for screen readers
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
{/* Dialog content */}
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important**: Even if you want to hide the title visually, use the `VisuallyHidden` component to maintain accessibility:
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
|
||||||
|
|
||||||
|
<DialogHeader>
|
||||||
|
<VisuallyHidden>
|
||||||
|
<DialogTitle>Hidden Title for Screen Readers</DialogTitle>
|
||||||
|
</VisuallyHidden>
|
||||||
|
</DialogHeader>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Streaming Response Handling
|
||||||
|
|
||||||
|
When implementing streaming chat interfaces, always accumulate streamed content in a local variable before clearing the streaming state to prevent content loss:
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
const handleStreamingResponse = async () => {
|
||||||
|
let streamedContent = ''; // ✅ Use local variable to accumulate content
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendStreamingMessage(messages, model, (chunk) => {
|
||||||
|
streamedContent += chunk; // ✅ Accumulate in local variable
|
||||||
|
setCurrentStreamingMessage(streamedContent); // Update UI
|
||||||
|
});
|
||||||
|
|
||||||
|
// ✅ Save accumulated content to persistent state
|
||||||
|
if (streamedContent.trim()) {
|
||||||
|
const assistantMessage: MessageDisplay = {
|
||||||
|
id: Date.now().toString(),
|
||||||
|
role: 'assistant',
|
||||||
|
content: streamedContent, // ✅ Use accumulated content
|
||||||
|
timestamp: new Date()
|
||||||
|
};
|
||||||
|
setMessages(prev => [...prev, assistantMessage]);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setCurrentStreamingMessage(''); // ✅ Clear streaming state after saving
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error Boundary Patterns
|
||||||
|
|
||||||
|
Always wrap AI components with error boundaries and provide user-friendly error messages for common failure scenarios:
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { ErrorBoundary } from '@/components/ErrorBoundary';
|
||||||
|
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||||
|
|
||||||
|
function AIChatWithErrorBoundary() {
|
||||||
|
return (
|
||||||
|
<ErrorBoundary
|
||||||
|
fallback={
|
||||||
|
<div className="p-4">
|
||||||
|
<Alert variant="destructive">
|
||||||
|
<AlertCircle className="h-4 w-4" />
|
||||||
|
<AlertDescription>
|
||||||
|
Something went wrong with the AI chat. Please refresh the page and try again.
|
||||||
|
</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<AIChat />
|
||||||
|
</ErrorBoundary>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// In your AI component, handle specific error types gracefully:
|
||||||
|
function useAIWithErrorHandling() {
|
||||||
|
const { sendChatMessage, error, clearError } = useShakespeare();
|
||||||
|
|
||||||
|
const sendMessage = async (messages: ChatMessage[]) => {
|
||||||
|
try {
|
||||||
|
await sendChatMessage(messages, 'tybalt');
|
||||||
|
} catch (err) {
|
||||||
|
// Handle specific error types with user-friendly messages
|
||||||
|
if (err.message.includes('401')) {
|
||||||
|
throw new Error('Authentication failed. Please log in again.');
|
||||||
|
} else if (err.message.includes('402')) {
|
||||||
|
throw new Error('Insufficient credits. Please add credits to use premium features.');
|
||||||
|
} else if (err.message.includes('network')) {
|
||||||
|
throw new Error('Network error. Please check your internet connection.');
|
||||||
|
}
|
||||||
|
throw err; // Re-throw for error boundary
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return { sendMessage, error, clearError };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Loading States
|
## Loading States
|
||||||
|
|
||||||
**Use skeleton loading** for structured content (feeds, profiles, forms). **Use spinners** only for buttons or short operations.
|
**Use skeleton loading** for structured content (feeds, profiles, forms). **Use spinners** only for buttons or short operations.
|
||||||
|
|||||||
Reference in new issue
Block a user