'use client' import * as React from 'react' import { useState } from 'react' import { Copy, RefreshCw, AlertTriangle, Check, Key, Clock } from 'lucide-react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@open-mercato/ui/primitives/dialog' import { Button } from '@open-mercato/ui/primitives/button' import { apiCall } from '@open-mercato/ui/backend/utils/apiCall' import { useT } from '@open-mercato/shared/lib/i18n/context' import { useDialogKeyHandler } from '@open-mercato/ui/hooks/useDialogKeyHandler' type Props = { open: boolean onOpenChange: (next: boolean) => void } type SessionKeyResponse = { sessionToken: string expiresAt: string } export default function SessionKeyDialog({ open, onOpenChange }: Props) { const t = useT() const [sessionToken, setSessionToken] = useState(null) const [expiresAt, setExpiresAt] = useState(null) const [isGenerating, setIsGenerating] = useState(false) const [copiedToken, setCopiedToken] = useState(false) const [copiedInstructions, setCopiedInstructions] = useState(false) const [error, setError] = useState(null) const generateSessionKey = async () => { setIsGenerating(true) setError(null) try { const res = await apiCall('/api/ai_assistant/session-key', { method: 'POST', }) if (res.ok && res.result?.sessionToken) { setSessionToken(res.result.sessionToken) setExpiresAt(res.result.expiresAt) } else { setError(t('ai_assistant.session.error.failed')) } } catch (err) { setError(err instanceof Error ? err.message : t('ai_assistant.session.error.failed')) } finally { setIsGenerating(false) } } // Auto-generate on open if no token React.useEffect(() => { if (open && !sessionToken && !isGenerating) { generateSessionKey() } }, [open]) const llmInstructions = `You have access to Open Mercato MCP tools for managing business data (customers, orders, products, etc.). IMPORTANT: Include "_sessionToken" in EVERY tool call: { "_sessionToken": "${sessionToken || 'sess_abc123...'}", ...other parameters } Workflow (use this order): 1. "discover_schema" - Understand entities, fields, and relationships first Examples: { "query": "customer" }, { "query": "orders" }, { "query": "products" } 2. "find_api" - Find the API endpoint for your operation (list, get, create, update, delete) 3. "call_api" - Execute the API call with proper parameters` const copyToken = async () => { if (sessionToken) { await navigator.clipboard.writeText(sessionToken) setCopiedToken(true) setTimeout(() => setCopiedToken(false), 2000) } } const copyInstructions = async () => { await navigator.clipboard.writeText(llmInstructions) setCopiedInstructions(true) setTimeout(() => setCopiedInstructions(false), 2000) } const handleClose = () => { setSessionToken(null) setExpiresAt(null) setError(null) setCopiedToken(false) setCopiedInstructions(false) onOpenChange(false) } const handleKeyDown = useDialogKeyHandler({ onCancel: handleClose }) const formatExpiry = (isoDate: string | null) => { if (!isoDate) return t('ai_assistant.session.expiresDefault') const date = new Date(isoDate) return date.toLocaleString() } return ( {t('ai_assistant.session.title')} {t('ai_assistant.session.description')}
{isGenerating && !sessionToken ? (
{t('ai_assistant.session.generating')}
) : sessionToken ? ( <> {/* Session Token */}
{t('ai_assistant.session.tokenLabel')}
{sessionToken}
{/* Expiry Info */}
{t('ai_assistant.session.expires')} {formatExpiry(expiresAt)}
{/* Separator */}
{/* LLM Instructions */}
{t('ai_assistant.session.llmInstructions')}

{t('ai_assistant.session.copyToSystemPrompt')}

                  {llmInstructions}
                
) : null} {error && (
{error}
)}
{sessionToken && ( )}
) }