188 lines
9.0 KiB
TypeScript
188 lines
9.0 KiB
TypeScript
import React from 'react';
|
|
import { Brain, Cpu, Zap, Lock, RotateCcw, Wifi, FileText, Shield } from 'lucide-react';
|
|
import { cn } from '@/lib/utils';
|
|
|
|
interface AiManagerProps {
|
|
aiConfig: any;
|
|
handleUpdateAiProvider: (provider: string) => void;
|
|
aiKeys: { gemini: string; claude: string };
|
|
setAiKeys: (keys: any) => void;
|
|
isSavingKeys: boolean;
|
|
onSaveAiKeys: () => void;
|
|
isTestingKeys: { gemini: boolean; claude: boolean };
|
|
onTestAiKey: (provider: 'gemini' | 'claude') => void;
|
|
aiPrompt: string;
|
|
setAiPrompt: (prompt: string) => void;
|
|
isSavingPrompt: boolean;
|
|
onUpdatePrompt: () => void;
|
|
}
|
|
|
|
export default function AiManager({
|
|
aiConfig,
|
|
handleUpdateAiProvider,
|
|
aiKeys,
|
|
setAiKeys,
|
|
isSavingKeys,
|
|
onSaveAiKeys,
|
|
isTestingKeys,
|
|
onTestAiKey,
|
|
aiPrompt,
|
|
setAiPrompt,
|
|
isSavingPrompt,
|
|
onUpdatePrompt
|
|
}: AiManagerProps) {
|
|
return (
|
|
<section data-testid="ai-config" className="bg-surface/50 border border-border/50 rounded-none p-4 md:p-6 shadow-none space-y-4 transition-all group/ai">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-3">
|
|
<div className="w-10 h-10 rounded-none bg-primary/10 flex items-center justify-center text-primary border border-primary/20">
|
|
<Brain size={20} />
|
|
</div>
|
|
<h2 className="text-xl font-normal text-white tracking-tight">AI Intelligence</h2>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
{aiConfig?.providers?.map((p: any) => (
|
|
<button
|
|
key={p.id}
|
|
data-testid="provider-option"
|
|
onClick={() => handleUpdateAiProvider(p.id)}
|
|
className={cn(
|
|
"p-4 lg:p-5 xl:p-6 lg:p-8 xl:p-10 rounded-none border transition-all text-left flex items-center justify-between group",
|
|
p.active
|
|
? "bg-primary border-primary shadow-none shadow-none"
|
|
: "bg-background/60 border-border hover:border-primary/40"
|
|
)}
|
|
>
|
|
<div className="flex items-center gap-3">
|
|
<div className={cn(
|
|
"w-8 h-8 rounded-none flex items-center justify-center transition-colors",
|
|
p.active ? "bg-white/20 text-white" : "bg-primary/10 text-primary group-hover:bg-primary/20"
|
|
)}>
|
|
{p.id === 'gemini' ? <Cpu size={16} /> : <Zap size={16} />}
|
|
</div>
|
|
<div>
|
|
<p className={cn("text-xs font-normal tracking-tight", p.active ? "text-white" : "text-secondary")}>{p.name}</p>
|
|
<p className={cn(
|
|
"text-xs font-normal mt-1 px-0.5 rounded",
|
|
p.active
|
|
? (p.configured ? "text-emerald-200" : "text-rose-100")
|
|
: (p.configured ? "text-emerald-500" : "text-rose-500")
|
|
)}>
|
|
{p.configured ? "● Key Configured" : "○ Missing Api Key"}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
{p.active && (
|
|
<div className="bg-white/20 px-2.5 py-1 rounded-none text-xs font-normal text-white tracking-tight">
|
|
Active
|
|
</div>
|
|
)}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="bg-primary/5 border border-primary/10 rounded-none p-5 space-y-3">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-3">
|
|
<div className="p-2 bg-primary/10 rounded-none text-primary"><Lock size={14} /></div>
|
|
<h3 className="text-sm font-normal text-secondary tracking-tight">Provider Access Keys</h3>
|
|
</div>
|
|
<button
|
|
onClick={onSaveAiKeys}
|
|
disabled={isSavingKeys}
|
|
data-testid="save-settings-button"
|
|
className="px-5 py-2 bg-primary hover:bg-primary text-white rounded-none text-sm font-normal transition-all active:scale-95 shadow-none shadow-none tracking-tight border border-primary/30 flex items-center gap-2"
|
|
>
|
|
{isSavingKeys ? <RotateCcw size={14} className="animate-spin" /> : <Lock size={14} />}
|
|
{isSavingKeys ? "Storing..." : "Store API Keys"}
|
|
</button>
|
|
</div>
|
|
|
|
<div className="grid md:grid-cols-2 gap-4">
|
|
<div className="space-y-1">
|
|
<label className="text-sm font-normal text-secondary tracking-tight ml-1">Gemini Api Key</label>
|
|
<div className="flex gap-2">
|
|
<input
|
|
data-testid="ai-api-key-input"
|
|
type="password"
|
|
value={aiKeys.gemini}
|
|
onChange={(e) => setAiKeys({...aiKeys, gemini: e.target.value})}
|
|
placeholder={aiConfig?.providers?.find((p: any) => p.id === 'gemini')?.masked_key || "Enter Gemini Key..."}
|
|
className="flex-1 bg-background/80 border border-border rounded-none py-1.5 px-4 text-xs text-white outline-none focus:border-primary/50 transition-all placeholder:text-secondary"
|
|
/>
|
|
<button
|
|
onClick={() => onTestAiKey('gemini')}
|
|
disabled={isTestingKeys.gemini}
|
|
className={cn(
|
|
"px-3 py-1.5 rounded-none text-xs font-normal tracking-tight transition-all border shadow-none flex items-center gap-1.5",
|
|
isTestingKeys.gemini
|
|
? "bg-surface-bright border-border text-muted cursor-wait"
|
|
: "bg-primary border-primary text-white hover:bg-primary"
|
|
)}
|
|
>
|
|
{isTestingKeys.gemini ? <RotateCcw size={12} className="animate-spin" /> : <Wifi size={12} />}
|
|
{isTestingKeys.gemini ? "..." : "Test"}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<label className="text-sm font-normal text-secondary tracking-tight ml-1">Claude Api Key</label>
|
|
<div className="flex gap-2">
|
|
<input
|
|
data-testid="ai-api-key-input"
|
|
type="password"
|
|
value={aiKeys.claude}
|
|
onChange={(e) => setAiKeys({...aiKeys, claude: e.target.value})}
|
|
placeholder={aiConfig?.providers?.find((p: any) => p.id === 'claude')?.masked_key || "Enter Claude Key..."}
|
|
className="flex-1 bg-background/80 border border-border rounded-none py-1.5 px-4 text-xs text-white outline-none focus:border-primary/50 transition-all placeholder:text-secondary"
|
|
/>
|
|
<button
|
|
onClick={() => onTestAiKey('claude')}
|
|
disabled={isTestingKeys.claude}
|
|
className={cn(
|
|
"px-3 py-1.5 rounded-none text-xs font-normal tracking-tight transition-all border shadow-none flex items-center gap-1.5",
|
|
isTestingKeys.claude
|
|
? "bg-surface-bright border-border text-muted cursor-wait"
|
|
: "bg-primary border-primary text-white hover:bg-primary"
|
|
)}
|
|
>
|
|
{isTestingKeys.claude ? <RotateCcw size={12} className="animate-spin" /> : <Wifi size={12} />}
|
|
{isTestingKeys.claude ? "..." : "Test"}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-2 pt-0">
|
|
<div className="space-y-1">
|
|
<div className="flex items-center justify-between px-1">
|
|
<label className="text-sm font-normal text-secondary tracking-tight">System Prompt (Vision Extraction)</label>
|
|
<button
|
|
onClick={onUpdatePrompt}
|
|
disabled={isSavingPrompt}
|
|
className="px-5 py-1.5 bg-primary hover:bg-primary text-white rounded-none text-sm font-normal transition-all active:scale-95 shadow-none shadow-none tracking-tight border border-primary/30 flex items-center gap-2"
|
|
>
|
|
{isSavingPrompt ? <RotateCcw size={14} className="animate-spin" /> : <FileText size={14} />}
|
|
{isSavingPrompt ? "Saving..." : "Save Prompt"}
|
|
</button>
|
|
</div>
|
|
<textarea
|
|
value={aiPrompt}
|
|
onChange={(e) => setAiPrompt(e.target.value)}
|
|
className="w-full bg-background/80 border border-border rounded-none p-4 text-xs font-mono font-normal text-secondary leading-relaxed outline-none focus:border-purple-500/50 transition-all min-h-[200px] custom-scrollbar shadow-none"
|
|
/>
|
|
</div>
|
|
<div className="bg-primary/5 border border-purple-500/10 rounded-none p-4 flex gap-3 items-start">
|
|
<div className="p-2 bg-primary/10 rounded-none text-purple-400 shrink-0"><Shield size={14} /></div>
|
|
<p className="text-xs font-normal text-secondary leading-relaxed">
|
|
This prompt instructs the Vision AI core on label interpretation. Ensure it defines explicit mapping for technical attributes like <span className="text-purple-400">Item</span>, <span className="text-purple-400">Type</span>, and <span className="text-purple-400">Part Number</span> to avoid extraction null-pointers and ensure inventory data integrity.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|