Spaces:
Running
Running
File size: 16,416 Bytes
1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa 4944128 0bb4dfa 1961f74 0bb4dfa 1961f74 0bb4dfa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | import React, { useState, useEffect, useRef, useCallback, useMemo } from 'react';
import { Upload, Save, Trash2, Wand2 } from 'lucide-react';
import { generateRole, generateRoleFreeform, suggestModel } from '../utils/api';
/**
* Single source of truth for creating Expert Personas. Replaces the
* inline PersonaAccordion + DevMenu persona-mode/role-style settings
* from LLMChats3 - those choices now live inside this modal.
*
* Tabs: Structured | Freeform
* Role-style toggle: AI-completed | Exact (matches LLMChats3 semantics)
* Freeform tab supports a file upload for writing samples.
*/
export default function ExpertPersonaModal({
isOpen,
initial, // existing persona to edit, or null for new
onClose,
onSave,
onDelete,
allModels, // [{ id, name, provider }]
defaultModelId,
panelContext, // [{ name, model_id, provider }] — other panel members
orchestratorModelId, // optional override for the meta-LLM call
}) {
const [activeTab, setActiveTab] = useState('freeform');
const [name, setName] = useState('');
const [profile, setProfile] = useState('');
const [identity, setIdentity] = useState('');
const [samples, setSamples] = useState('');
const [freeText, setFreeText] = useState('');
const [roleStyle, setRoleStyle] = useState('ai_completed');
const [modelId, setModelId] = useState(defaultModelId || '');
const [generatedPrompt, setGeneratedPrompt] = useState('');
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const [suggestBusy, setSuggestBusy] = useState(false);
const [suggestion, setSuggestion] = useState(null);
const [suggestMessage, setSuggestMessage] = useState('');
const fileInputRef = useRef(null);
const rolePromptText = useMemo(
() => (generatedPrompt || '').trim(),
[generatedPrompt],
);
const composeSourceText = useCallback(() => {
if (activeTab === 'freeform') {
return freeText.trim();
}
return [identity, profile, samples]
.map(s => (s || '').trim())
.filter(Boolean)
.join('\n\n');
}, [activeTab, freeText, identity, profile, samples]);
const hasDescriptionContent = useMemo(() => {
if (activeTab === 'freeform') {
return Boolean(freeText.trim());
}
return Boolean(identity.trim() || profile.trim() || samples.trim());
}, [activeTab, freeText, identity, profile, samples]);
const modelNameForId = useCallback((id) => {
const m = (allModels || []).find(x => x.id === id);
return m ? m.name : id;
}, [allModels]);
const modelProviderForId = useCallback((id) => {
const m = (allModels || []).find(x => x.id === id);
return m?.provider || '';
}, [allModels]);
const resolveModelId = useCallback((preferred) => {
const models = allModels || [];
if (preferred && models.some(m => m.id === preferred)) {
return preferred;
}
return models[0]?.id || '';
}, [allModels]);
useEffect(() => {
if (!isOpen) return;
if (initial) {
setActiveTab(initial.input_mode || 'freeform');
setName(initial.name || '');
setProfile(initial.profile || '');
setIdentity(initial.identity || '');
setSamples(initial.samples || '');
setFreeText(initial.freeform || '');
setRoleStyle(initial.role_style || 'ai_completed');
setModelId(resolveModelId(initial.model_id || defaultModelId));
setGeneratedPrompt(initial.role_prompt || '');
} else {
setActiveTab('freeform');
setName('');
setProfile('');
setIdentity('');
setSamples('');
setFreeText('');
setRoleStyle('ai_completed');
setModelId(resolveModelId(defaultModelId));
setGeneratedPrompt('');
}
setError('');
setSuggestion(null);
setSuggestMessage('');
setSuggestBusy(false);
}, [isOpen, initial, defaultModelId, resolveModelId]);
if (!isOpen) return null;
const handleFileUpload = async (e) => {
const file = e.target.files?.[0];
if (!file) return;
try {
const text = await file.text();
setFreeText(prev => (prev ? prev + '\n\n' : '') + text);
} catch (err) {
setError(`File read failed: ${err.message}`);
}
e.target.value = '';
};
const handleGenerate = async () => {
setError('');
if (!name.trim()) {
setError('Persona needs a name.');
return;
}
if (!hasDescriptionContent) {
setError('Add a description before generating a role prompt.');
return;
}
setBusy(true);
try {
const result = activeTab === 'freeform'
? await generateRoleFreeform({
name: name.trim(),
text: freeText,
role_style: roleStyle,
orchestrator_model_id: orchestratorModelId || undefined,
})
: await generateRole({
name: name.trim(),
profile,
identity,
samples,
role_style: roleStyle,
orchestrator_model_id: orchestratorModelId || undefined,
});
setGeneratedPrompt(result.role_prompt || '');
} catch (err) {
setError(err.message || String(err));
} finally {
setBusy(false);
}
};
const handleSuggestModel = async () => {
setError('');
setSuggestion(null);
setSuggestMessage('');
const sourceText = composeSourceText();
if (!sourceText && !rolePromptText) {
setSuggestMessage(
'Enter a description or role prompt for a model to be suggested.',
);
return;
}
if (!(allModels || []).length) {
setSuggestMessage('No models available to suggest from.');
return;
}
setSuggestBusy(true);
try {
const result = await suggestModel({
persona_name: name.trim() || 'Unnamed',
source_text: sourceText,
role_prompt: rolePromptText,
available_models: allModels,
panel_context: panelContext || [],
orchestrator_model_id: orchestratorModelId || undefined,
});
setSuggestion({
modelId: result.recommended_model_id,
rationale: result.rationale || '',
});
} catch (err) {
setSuggestMessage(err.message || String(err));
} finally {
setSuggestBusy(false);
}
};
const handleAcceptSuggestion = () => {
if (!suggestion?.modelId) return;
setModelId(suggestion.modelId);
setSuggestion(null);
};
const canSave = name.trim() && modelId && generatedPrompt.trim();
const handleSave = () => {
if (!canSave) return;
onSave({
participant_id: initial?.participant_id || `expert_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
kind: 'expert',
name: name.trim(),
model_id: modelId,
role_prompt: generatedPrompt.trim(),
input_mode: activeTab,
role_style: roleStyle,
profile,
identity,
samples,
freeform: freeText,
});
};
return (
<div className="modal-overlay" onClick={onClose}>
<div
className="modal-content ccai-expert-modal"
onClick={e => e.stopPropagation()}
>
<div className="modal-header">
<h2>{initial ? `Edit Expert Persona: ${initial.name}` : 'Create Expert Persona'}</h2>
<button className="modal-close" onClick={onClose}>×</button>
</div>
<div className="modal-body">
<div className="ccai-expert-row">
<div className="ccai-expert-field">
<label>Name</label>
<input
type="text"
value={name}
placeholder="e.g. Dr. Patel - Pediatric Cardiologist"
onChange={e => setName(e.target.value)}
/>
</div>
<div className="ccai-expert-field ccai-expert-model-field">
<label>Powered by LLM</label>
<div className="ccai-expert-model-row">
<select
value={modelId}
onChange={e => setModelId(e.target.value)}
>
<option value="">Pick a model...</option>
{(allModels || []).map(m => (
<option key={m.id} value={m.id}>
{m.name} {m.provider ? `(${m.provider})` : ''}
</option>
))}
</select>
<button
type="button"
className="btn-sm btn-outline ccai-suggest-model-btn"
onClick={handleSuggestModel}
disabled={suggestBusy || !(allModels || []).length}
title="Analyze the role prompt and recommend a model"
>
<Wand2 size={12} />
{suggestBusy ? 'Suggesting...' : 'Suggest a model'}
</button>
</div>
{suggestMessage && (
<div className="ccai-expert-suggest-message">{suggestMessage}</div>
)}
{suggestion && (
<div className="ccai-model-suggestion">
<div className="ccai-model-suggestion-title">
Suggested: {modelNameForId(suggestion.modelId)}
{modelProviderForId(suggestion.modelId)
? ` (${modelProviderForId(suggestion.modelId)})`
: ''}
</div>
{suggestion.rationale && (
<p className="ccai-model-suggestion-rationale">{suggestion.rationale}</p>
)}
<div className="ccai-model-suggestion-actions">
<button
type="button"
className="btn-sm btn-primary"
onClick={handleAcceptSuggestion}
>
Use this model
</button>
<button
type="button"
className="btn-sm btn-secondary"
onClick={() => setSuggestion(null)}
>
Dismiss
</button>
</div>
</div>
)}
</div>
</div>
<div className="ccai-expert-input-mode">
<div className="ccai-tab-row">
<button
type="button"
className={'ccai-tab-btn' + (activeTab === 'freeform' ? ' ccai-tab-btn-active' : '')}
onClick={() => setActiveTab('freeform')}
title="One text area for background, bio, and writing samples."
>
Freeform
</button>
<button
type="button"
className={'ccai-tab-btn' + (activeTab === 'structured' ? ' ccai-tab-btn-active' : '')}
onClick={() => setActiveTab('structured')}
title="Separate fields for identity, profile, and writing samples."
>
Structured
</button>
</div>
<p className="ccai-expert-field-hint">
Freeform = one box for everything; Structured = separate fields for identity, profile, and samples.
</p>
</div>
<div className="ccai-expert-role-style">
<div className="ccai-tab-row ccai-tab-row-compact">
<label
className="ccai-role-style"
title="Fills in tone and style from your notes; stays within what you described."
>
<input
type="radio"
name="role-style"
checked={roleStyle === 'ai_completed'}
onChange={() => setRoleStyle('ai_completed')}
/>
AI-completed
</label>
<label
className="ccai-role-style"
title="Reorganizes your text only; won't add traits or facts you didn't provide."
>
<input
type="radio"
name="role-style"
checked={roleStyle === 'exact'}
onChange={() => setRoleStyle('exact')}
/>
Exact (no inferring)
</label>
</div>
<p className="ccai-expert-field-hint">
AI-completed fills in tone and style from your notes (no new facts); Exact uses only what you typed.
</p>
</div>
{activeTab === 'freeform' ? (
<div className="ccai-expert-freeform">
<div className="freeform-label-row">
<label>Persona description, writing samples, anything you want the LLM to know:</label>
<button
className="btn-sm btn-outline upload-btn"
onClick={() => fileInputRef.current?.click()}
>
<Upload size={12} /> Upload .txt
</button>
<input
type="file"
accept=".txt,.md"
ref={fileInputRef}
onChange={handleFileUpload}
style={{ display: 'none' }}
/>
</div>
<textarea
className="freeform-textarea"
value={freeText}
placeholder="Drop in any background, transcript, writing samples, biography, etc. Sparse input is fine - the AI-completed mode will fill in plausible details."
onChange={e => setFreeText(e.target.value)}
/>
</div>
) : (
<div className="ccai-expert-structured">
<div className="ccai-expert-field">
<label>Identity statement</label>
<input
type="text"
value={identity}
onChange={e => setIdentity(e.target.value)}
placeholder="One-sentence 'who are you'"
/>
</div>
<div className="ccai-expert-field">
<label>Profile / background</label>
<textarea
value={profile}
onChange={e => setProfile(e.target.value)}
rows={3}
/>
</div>
<div className="ccai-expert-field">
<label>Writing / speech samples</label>
<textarea
value={samples}
onChange={e => setSamples(e.target.value)}
rows={3}
/>
</div>
</div>
)}
<div className="ccai-expert-actions">
<button
className="btn-secondary"
onClick={handleGenerate}
disabled={busy || !name.trim() || !hasDescriptionContent}
>
{busy ? 'Generating role prompt...' : 'Generate role prompt'}
</button>
</div>
<div className="ccai-expert-prompt">
<label>Role prompt</label>
<textarea
value={generatedPrompt}
onChange={e => {
setGeneratedPrompt(e.target.value);
if (suggestMessage) setSuggestMessage('');
if (suggestion) setSuggestion(null);
}}
rows={6}
placeholder="Generate a role prompt above, or write one here. Suggest a model analyzes this text."
/>
</div>
{error && <div className="ccai-expert-error">{error}</div>}
<div className="ccai-expert-footer">
{initial && onDelete && (
<button
className="btn-sm ccai-remove-btn"
onClick={() => onDelete(initial.participant_id)}
>
<Trash2 size={12} /> Delete
</button>
)}
<div className="ccai-tab-spacer" />
<button className="btn-secondary" onClick={onClose}>Cancel</button>
<button
className="btn-primary"
disabled={!canSave}
onClick={handleSave}
>
<Save size={14} style={{ marginRight: 4, verticalAlign: 'middle' }} />
{initial ? 'Save changes' : 'Save persona'}
</button>
</div>
</div>
</div>
</div>
);
}
|