import React, { useState } from 'react'; const OPERATORS = [ { value: 'lt', label: '< (less than)' }, { value: 'lte', label: '≤ (less than or equal)' }, { value: 'eq', label: '= (equal to)' }, { value: 'gte', label: '≥ (greater than or equal)' }, { value: 'gt', label: '> (greater than)' }, ]; const ACTIONS = [ { value: 'seek_fulfillment', label: 'Seek Fulfillment' }, { value: 'alert', label: 'Trigger Alert' }, { value: 'change_state', label: 'Change Character State' }, { value: 'generate_event', label: 'Generate Roleplay Event' }, { value: 'activate_rule', label: 'Activate Other Rule' }, ]; export default function BrainRuleEditor({ brainRules, needs, onAdd, onUpdate, onDelete }) { const [editingId, setEditingId] = useState(null); const [editForm, setEditForm] = useState({}); const startEdit = (rule) => { setEditingId(rule.id); const condition = typeof rule.condition === 'string' ? JSON.parse(rule.condition) : rule.condition; const action = typeof rule.action === 'string' ? JSON.parse(rule.action) : rule.action; setEditForm({ ...rule, condition, action }); }; const cancelEdit = () => { setEditingId(null); setEditForm({}); }; const saveEdit = () => { onUpdate(editingId, { condition: editForm.condition, action: editForm.action, priority: editForm.priority, enabled: editForm.enabled, }); setEditingId(null); }; const handleAdd = () => { const newRule = { condition: { need: needs?.[0]?.name || '', operator: 'lt', value: 30 }, action: { type: 'seek_fulfillment', target: '' }, priority: 0, enabled: true, }; onAdd(newRule); }; return (

Brain Rules

{(!brainRules || brainRules.length === 0) ? (

No brain rules defined. Rules define how your character reacts to changing needs.

) : (
{brainRules.map((rule) => { const condition = typeof rule.condition === 'string' ? JSON.parse(rule.condition) : rule.condition; const action = typeof rule.action === 'string' ? JSON.parse(rule.action) : rule.action; return (
{editingId === rule.id ? (
setEditForm({ ...editForm, condition: { ...editForm.condition, value: +e.target.value } })} />
setEditForm({ ...editForm, action: { ...editForm.action, target: e.target.value } })} placeholder="Target need or behavior" />
setEditForm({ ...editForm, priority: +e.target.value })} />
setEditForm({ ...editForm, enabled: e.target.checked })} />
) : ( <>
IF {condition?.need} {OPERATORS.find(o => o.value === condition?.operator)?.label || condition?.operator} {condition?.value}
THEN {ACTIONS.find(a => a.value === action?.type)?.label || action?.type} {action?.target ? `: ${action.target}` : ''}
{rule.enabled ? 'Active' : 'Disabled'}
Priority: {rule.priority}
)}
); })}
)}
); }