Fix: add client files properly (remove submodule reference)
This commit is contained in:
parent
85454f9737
commit
2b5b7bb50c
1
client
1
client
@ -1 +0,0 @@
|
|||||||
Subproject commit b9e538e1914933ac4eedbdf75328737e7df70caf
|
|
||||||
23
client/.gitignore
vendored
Normal file
23
client/.gitignore
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
|
||||||
|
# dependencies
|
||||||
|
/node_modules
|
||||||
|
/.pnp
|
||||||
|
.pnp.js
|
||||||
|
|
||||||
|
# testing
|
||||||
|
/coverage
|
||||||
|
|
||||||
|
# production
|
||||||
|
/build
|
||||||
|
|
||||||
|
# misc
|
||||||
|
.DS_Store
|
||||||
|
.env.local
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
70
client/README.md
Normal file
70
client/README.md
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
# Getting Started with Create React App
|
||||||
|
|
||||||
|
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||||
|
|
||||||
|
## Available Scripts
|
||||||
|
|
||||||
|
In the project directory, you can run:
|
||||||
|
|
||||||
|
### `npm start`
|
||||||
|
|
||||||
|
Runs the app in the development mode.\
|
||||||
|
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
|
||||||
|
|
||||||
|
The page will reload when you make changes.\
|
||||||
|
You may also see any lint errors in the console.
|
||||||
|
|
||||||
|
### `npm test`
|
||||||
|
|
||||||
|
Launches the test runner in the interactive watch mode.\
|
||||||
|
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||||
|
|
||||||
|
### `npm run build`
|
||||||
|
|
||||||
|
Builds the app for production to the `build` folder.\
|
||||||
|
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||||
|
|
||||||
|
The build is minified and the filenames include the hashes.\
|
||||||
|
Your app is ready to be deployed!
|
||||||
|
|
||||||
|
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||||
|
|
||||||
|
### `npm run eject`
|
||||||
|
|
||||||
|
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
|
||||||
|
|
||||||
|
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||||
|
|
||||||
|
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
|
||||||
|
|
||||||
|
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
|
||||||
|
|
||||||
|
## Learn More
|
||||||
|
|
||||||
|
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||||
|
|
||||||
|
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||||
|
|
||||||
|
### Code Splitting
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
|
||||||
|
|
||||||
|
### Analyzing the Bundle Size
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
|
||||||
|
|
||||||
|
### Making a Progressive Web App
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
|
||||||
|
|
||||||
|
### Advanced Configuration
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
|
||||||
|
|
||||||
|
### Deployment
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
|
||||||
|
|
||||||
|
### `npm run build` fails to minify
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
|
||||||
17494
client/package-lock.json
generated
Normal file
17494
client/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
client/package.json
Normal file
40
client/package.json
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"name": "client",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@testing-library/dom": "^10.4.1",
|
||||||
|
"@testing-library/jest-dom": "^6.9.1",
|
||||||
|
"@testing-library/react": "^16.3.2",
|
||||||
|
"@testing-library/user-event": "^13.5.0",
|
||||||
|
"react": "^19.2.7",
|
||||||
|
"react-dom": "^19.2.7",
|
||||||
|
"react-router-dom": "^7.18.1",
|
||||||
|
"react-scripts": "5.0.1",
|
||||||
|
"web-vitals": "^2.1.4"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"start": "react-scripts start",
|
||||||
|
"build": "react-scripts build",
|
||||||
|
"test": "react-scripts test",
|
||||||
|
"eject": "react-scripts eject"
|
||||||
|
},
|
||||||
|
"eslintConfig": {
|
||||||
|
"extends": [
|
||||||
|
"react-app",
|
||||||
|
"react-app/jest"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"browserslist": {
|
||||||
|
"production": [
|
||||||
|
">0.2%",
|
||||||
|
"not dead",
|
||||||
|
"not op_mini all"
|
||||||
|
],
|
||||||
|
"development": [
|
||||||
|
"last 1 chrome version",
|
||||||
|
"last 1 firefox version",
|
||||||
|
"last 1 safari version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
client/public/favicon.ico
Normal file
BIN
client/public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
43
client/public/index.html
Normal file
43
client/public/index.html
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<meta name="theme-color" content="#000000" />
|
||||||
|
<meta
|
||||||
|
name="description"
|
||||||
|
content="Web site created using create-react-app"
|
||||||
|
/>
|
||||||
|
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
|
||||||
|
<!--
|
||||||
|
manifest.json provides metadata used when your web app is installed on a
|
||||||
|
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||||
|
-->
|
||||||
|
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||||
|
<!--
|
||||||
|
Notice the use of %PUBLIC_URL% in the tags above.
|
||||||
|
It will be replaced with the URL of the `public` folder during the build.
|
||||||
|
Only files inside the `public` folder can be referenced from the HTML.
|
||||||
|
|
||||||
|
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||||
|
work correctly both with client-side routing and a non-root public URL.
|
||||||
|
Learn how to configure a non-root public URL by running `npm run build`.
|
||||||
|
-->
|
||||||
|
<title>React App</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
|
<div id="root"></div>
|
||||||
|
<!--
|
||||||
|
This HTML file is a template.
|
||||||
|
If you open it directly in the browser, you will see an empty page.
|
||||||
|
|
||||||
|
You can add webfonts, meta tags, or analytics to this file.
|
||||||
|
The build step will place the bundled scripts into the <body> tag.
|
||||||
|
|
||||||
|
To begin the development, run `npm start` or `yarn start`.
|
||||||
|
To create a production bundle, use `npm run build` or `yarn build`.
|
||||||
|
-->
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
client/public/logo192.png
Normal file
BIN
client/public/logo192.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
BIN
client/public/logo512.png
Normal file
BIN
client/public/logo512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.4 KiB |
25
client/public/manifest.json
Normal file
25
client/public/manifest.json
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"short_name": "React App",
|
||||||
|
"name": "Create React App Sample",
|
||||||
|
"icons": [
|
||||||
|
{
|
||||||
|
"src": "favicon.ico",
|
||||||
|
"sizes": "64x64 32x32 24x24 16x16",
|
||||||
|
"type": "image/x-icon"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "logo192.png",
|
||||||
|
"type": "image/png",
|
||||||
|
"sizes": "192x192"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "logo512.png",
|
||||||
|
"type": "image/png",
|
||||||
|
"sizes": "512x512"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"start_url": ".",
|
||||||
|
"display": "standalone",
|
||||||
|
"theme_color": "#000000",
|
||||||
|
"background_color": "#ffffff"
|
||||||
|
}
|
||||||
3
client/public/robots.txt
Normal file
3
client/public/robots.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# https://www.robotstxt.org/robotstxt.html
|
||||||
|
User-agent: *
|
||||||
|
Disallow:
|
||||||
1
client/src/App.css
Normal file
1
client/src/App.css
Normal file
@ -0,0 +1 @@
|
|||||||
|
/* App-level styles are in index.css */
|
||||||
55
client/src/App.js
Normal file
55
client/src/App.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
||||||
|
import { AuthProvider, useAuth } from './context/AuthContext';
|
||||||
|
import Navbar from './components/Navbar';
|
||||||
|
import Login from './pages/Login';
|
||||||
|
import Register from './pages/Register';
|
||||||
|
import Dashboard from './pages/Dashboard';
|
||||||
|
import Characters from './pages/Characters';
|
||||||
|
import CharacterEditor from './pages/CharacterEditor';
|
||||||
|
import Lorebooks from './pages/Lorebooks';
|
||||||
|
import LorebookEditor from './pages/LorebookEditor';
|
||||||
|
|
||||||
|
function PrivateRoute({ children }) {
|
||||||
|
const { user, loading } = useAuth();
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
return user ? children : <Navigate to="/login" />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function AppContent() {
|
||||||
|
const { user, loading } = useAuth();
|
||||||
|
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="app">
|
||||||
|
{user && <Navbar />}
|
||||||
|
<main className={user ? 'main-content' : 'main-content auth-main'}>
|
||||||
|
<Routes>
|
||||||
|
<Route path="/login" element={user ? <Navigate to="/" /> : <Login />} />
|
||||||
|
<Route path="/register" element={user ? <Navigate to="/" /> : <Register />} />
|
||||||
|
<Route path="/" element={<PrivateRoute><Dashboard /></PrivateRoute>} />
|
||||||
|
<Route path="/characters" element={<PrivateRoute><Characters /></PrivateRoute>} />
|
||||||
|
<Route path="/characters/new" element={<PrivateRoute><CharacterEditor /></PrivateRoute>} />
|
||||||
|
<Route path="/characters/:id" element={<PrivateRoute><CharacterEditor /></PrivateRoute>} />
|
||||||
|
<Route path="/lorebooks" element={<PrivateRoute><Lorebooks /></PrivateRoute>} />
|
||||||
|
<Route path="/lorebooks/new" element={<PrivateRoute><LorebookEditor /></PrivateRoute>} />
|
||||||
|
<Route path="/lorebooks/:id" element={<PrivateRoute><LorebookEditor /></PrivateRoute>} />
|
||||||
|
<Route path="*" element={<Navigate to="/" />} />
|
||||||
|
</Routes>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
return (
|
||||||
|
<Router>
|
||||||
|
<AuthProvider>
|
||||||
|
<AppContent />
|
||||||
|
</AuthProvider>
|
||||||
|
</Router>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
||||||
8
client/src/App.test.js
Normal file
8
client/src/App.test.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { render, screen } from '@testing-library/react';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
|
test('renders learn react link', () => {
|
||||||
|
render(<App />);
|
||||||
|
const linkElement = screen.getByText(/learn react/i);
|
||||||
|
expect(linkElement).toBeInTheDocument();
|
||||||
|
});
|
||||||
135
client/src/components/AIAssistant.js
Normal file
135
client/src/components/AIAssistant.js
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { ai } from '../services/api';
|
||||||
|
|
||||||
|
export default function AIAssistant({ onApplySuggestion, characterId }) {
|
||||||
|
const [prompt, setPrompt] = useState('');
|
||||||
|
const [suggestion, setSuggestion] = useState(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [showTrain, setShowTrain] = useState(false);
|
||||||
|
const [trainPrompt, setTrainPrompt] = useState('');
|
||||||
|
const [trainResponse, setTrainResponse] = useState('');
|
||||||
|
|
||||||
|
const handleSuggest = async () => {
|
||||||
|
if (!prompt.trim()) return;
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
const data = await ai.suggest({ prompt });
|
||||||
|
setSuggestion(data.suggestion);
|
||||||
|
} catch (err) {
|
||||||
|
alert(err.message);
|
||||||
|
}
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleApply = () => {
|
||||||
|
if (onApplySuggestion && suggestion) {
|
||||||
|
onApplySuggestion(suggestion);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleTrain = async () => {
|
||||||
|
if (!trainPrompt.trim() || !trainResponse.trim()) return;
|
||||||
|
try {
|
||||||
|
await ai.train({ prompt: trainPrompt, response: trainResponse, category: 'character_creation' });
|
||||||
|
setTrainPrompt('');
|
||||||
|
setTrainResponse('');
|
||||||
|
setShowTrain(false);
|
||||||
|
alert('Training data saved!');
|
||||||
|
} catch (err) {
|
||||||
|
alert(err.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="ai-assistant">
|
||||||
|
<div className="ai-header">
|
||||||
|
<h3>AI Character Assistant</h3>
|
||||||
|
<span className="ai-badge">Local Agent</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="ai-input-section">
|
||||||
|
<textarea
|
||||||
|
placeholder="Describe the character you want to create... (e.g., 'a fantasy rogue with a dark past', 'a cyberpunk hacker seeking redemption')"
|
||||||
|
value={prompt}
|
||||||
|
onChange={(e) => setPrompt(e.target.value)}
|
||||||
|
rows={3}
|
||||||
|
/>
|
||||||
|
<button className="btn-primary" onClick={handleSuggest} disabled={loading}>
|
||||||
|
{loading ? 'Thinking...' : 'Generate Suggestion'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{suggestion && (
|
||||||
|
<div className="ai-suggestion">
|
||||||
|
<div className="suggestion-header">
|
||||||
|
<h4>Suggested Character: {suggestion.name}</h4>
|
||||||
|
<div className="suggestion-actions">
|
||||||
|
<button className="btn-primary" onClick={handleApply}>Apply to Character</button>
|
||||||
|
<button className="btn-secondary" onClick={() => setSuggestion(null)}>Dismiss</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="suggestion-body">
|
||||||
|
<div className="suggestion-field">
|
||||||
|
<label>Description</label>
|
||||||
|
<p>{suggestion.description}</p>
|
||||||
|
</div>
|
||||||
|
<div className="suggestion-field">
|
||||||
|
<label>Personality Traits</label>
|
||||||
|
<div className="trait-list">
|
||||||
|
{suggestion.personality_traits.map((t, i) => (
|
||||||
|
<span key={i} className="trait-tag">{t}</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="suggestion-field">
|
||||||
|
<label>Backstory</label>
|
||||||
|
<p>{suggestion.backstory}</p>
|
||||||
|
</div>
|
||||||
|
<div className="suggestion-field">
|
||||||
|
<label>Needs Priority</label>
|
||||||
|
<div className="needs-preview">
|
||||||
|
{suggestion.suggested_needs?.map((n, i) => (
|
||||||
|
<div key={i} className="need-preview-item">
|
||||||
|
<span className="need-name">{n.name}</span>
|
||||||
|
<span className="need-enabled">{n.enabled ? '✓' : '✗'}</span>
|
||||||
|
<span className="need-priority">Priority: {n.priority}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{suggestion.reasoning && (
|
||||||
|
<div className="suggestion-reasoning">
|
||||||
|
<label>Assistant's Reasoning</label>
|
||||||
|
<p>{suggestion.reasoning}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="ai-training-section">
|
||||||
|
<button className="btn-text" onClick={() => setShowTrain(!showTrain)}>
|
||||||
|
{showTrain ? 'Cancel' : 'Train the Assistant'}
|
||||||
|
</button>
|
||||||
|
{showTrain && (
|
||||||
|
<div className="train-form">
|
||||||
|
<textarea
|
||||||
|
placeholder="What prompt would you give the assistant?"
|
||||||
|
value={trainPrompt}
|
||||||
|
onChange={(e) => setTrainPrompt(e.target.value)}
|
||||||
|
rows={2}
|
||||||
|
/>
|
||||||
|
<textarea
|
||||||
|
placeholder="What response would you expect?"
|
||||||
|
value={trainResponse}
|
||||||
|
onChange={(e) => setTrainResponse(e.target.value)}
|
||||||
|
rows={2}
|
||||||
|
/>
|
||||||
|
<button className="btn-primary" onClick={handleTrain}>Save Training</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
178
client/src/components/BrainRuleEditor.js
Normal file
178
client/src/components/BrainRuleEditor.js
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
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 (
|
||||||
|
<div className="brain-rule-editor">
|
||||||
|
<div className="editor-toolbar">
|
||||||
|
<h3>Brain Rules</h3>
|
||||||
|
<button className="btn-primary" onClick={handleAdd}>Add Rule</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{(!brainRules || brainRules.length === 0) ? (
|
||||||
|
<p className="empty-state">No brain rules defined. Rules define how your character reacts to changing needs.</p>
|
||||||
|
) : (
|
||||||
|
<div className="brain-rules-list">
|
||||||
|
{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 (
|
||||||
|
<div key={rule.id} className={`brain-rule-card ${rule.enabled ? '' : 'disabled-rule'}`}>
|
||||||
|
{editingId === rule.id ? (
|
||||||
|
<div className="rule-edit-form">
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Condition: Need</label>
|
||||||
|
<select
|
||||||
|
value={editForm.condition?.need || ''}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, condition: { ...editForm.condition, need: e.target.value } })}
|
||||||
|
>
|
||||||
|
{needs?.map((n) => (
|
||||||
|
<option key={n.id} value={n.name}>{n.name}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Operator</label>
|
||||||
|
<select
|
||||||
|
value={editForm.condition?.operator || 'lt'}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, condition: { ...editForm.condition, operator: e.target.value } })}
|
||||||
|
>
|
||||||
|
{OPERATORS.map((o) => (
|
||||||
|
<option key={o.value} value={o.value}>{o.label}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Threshold Value</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={editForm.condition?.value || 0}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, condition: { ...editForm.condition, value: +e.target.value } })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Action Type</label>
|
||||||
|
<select
|
||||||
|
value={editForm.action?.type || 'seek_fulfillment'}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, action: { ...editForm.action, type: e.target.value } })}
|
||||||
|
>
|
||||||
|
{ACTIONS.map((a) => (
|
||||||
|
<option key={a.value} value={a.value}>{a.label}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Action Target</label>
|
||||||
|
<input
|
||||||
|
value={editForm.action?.target || ''}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, action: { ...editForm.action, target: e.target.value } })}
|
||||||
|
placeholder="Target need or behavior"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Priority</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={editForm.priority || 0}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, priority: +e.target.value })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Enabled</label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={editForm.enabled}
|
||||||
|
onChange={(e) => setEditForm({ ...editForm, enabled: e.target.checked })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-actions">
|
||||||
|
<button className="btn-primary" onClick={saveEdit}>Save</button>
|
||||||
|
<button className="btn-secondary" onClick={cancelEdit}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="rule-header">
|
||||||
|
<div className="rule-condition">
|
||||||
|
<span className="condition-text">
|
||||||
|
IF <strong>{condition?.need}</strong> {OPERATORS.find(o => o.value === condition?.operator)?.label || condition?.operator} <strong>{condition?.value}</strong>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="rule-action-type">
|
||||||
|
<span className="action-text">
|
||||||
|
THEN <strong>{ACTIONS.find(a => a.value === action?.type)?.label || action?.type}</strong>
|
||||||
|
{action?.target ? `: ${action.target}` : ''}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<span className={`rule-status ${rule.enabled ? 'active' : 'inactive'}`}>
|
||||||
|
{rule.enabled ? 'Active' : 'Disabled'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="rule-meta">
|
||||||
|
<span>Priority: {rule.priority}</span>
|
||||||
|
</div>
|
||||||
|
<div className="rule-actions">
|
||||||
|
<button className="btn-small" onClick={() => startEdit(rule)}>Edit</button>
|
||||||
|
<button className="btn-danger-small" onClick={() => onDelete(rule.id)}>Delete</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
45
client/src/components/Navbar.js
Normal file
45
client/src/components/Navbar.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import React, { useState, useRef, useEffect } from 'react';
|
||||||
|
import { Link, useNavigate } from 'react-router-dom';
|
||||||
|
import { useAuth } from '../context/AuthContext';
|
||||||
|
|
||||||
|
export default function Navbar() {
|
||||||
|
const { user, logout } = useAuth();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [showMenu, setShowMenu] = useState(false);
|
||||||
|
const menuRef = useRef();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleClick = (e) => {
|
||||||
|
if (menuRef.current && !menuRef.current.contains(e.target)) setShowMenu(false);
|
||||||
|
};
|
||||||
|
document.addEventListener('mousedown', handleClick);
|
||||||
|
return () => document.removeEventListener('mousedown', handleClick);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleLogout = () => {
|
||||||
|
logout();
|
||||||
|
navigate('/login');
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<nav className="navbar">
|
||||||
|
<div className="navbar-brand">
|
||||||
|
<Link to="/">Character Sandbox</Link>
|
||||||
|
</div>
|
||||||
|
<div className="navbar-links">
|
||||||
|
<Link to="/characters">Characters</Link>
|
||||||
|
<Link to="/lorebooks">Lorebooks</Link>
|
||||||
|
<div className="user-menu" ref={menuRef}>
|
||||||
|
<button className="user-menu-btn" onClick={() => setShowMenu(!showMenu)}>
|
||||||
|
{user?.username}
|
||||||
|
</button>
|
||||||
|
{showMenu && (
|
||||||
|
<div className="user-dropdown">
|
||||||
|
<button onClick={handleLogout}>Sign Out</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
);
|
||||||
|
}
|
||||||
131
client/src/components/NeedsEditor.js
Normal file
131
client/src/components/NeedsEditor.js
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
|
||||||
|
const DEFAULT_NEEDS = [
|
||||||
|
{ name: 'Food', enabled: true, initial_value: 80, min_value: 0, max_value: 100, decay_rate: 1.5, priority: 3 },
|
||||||
|
{ name: 'Energy', enabled: true, initial_value: 90, min_value: 0, max_value: 100, decay_rate: 2, priority: 4 },
|
||||||
|
{ name: 'Bladder', enabled: true, initial_value: 30, min_value: 0, max_value: 100, decay_rate: 0.8, priority: 2 },
|
||||||
|
{ name: 'Bowel', enabled: false, initial_value: 20, min_value: 0, max_value: 100, decay_rate: 0.5, priority: 1 },
|
||||||
|
{ name: 'Hormones', enabled: true, initial_value: 40, min_value: 0, max_value: 100, decay_rate: 0.3, priority: 1 },
|
||||||
|
{ name: 'Intimate', enabled: true, initial_value: 30, min_value: 0, max_value: 100, decay_rate: 0.4, priority: 2 },
|
||||||
|
];
|
||||||
|
|
||||||
|
export default function NeedsEditor({ needs, onUpdate, onAdd, onDelete }) {
|
||||||
|
const [editingId, setEditingId] = useState(null);
|
||||||
|
const [editForm, setEditForm] = useState({});
|
||||||
|
|
||||||
|
const startEdit = (need) => {
|
||||||
|
setEditingId(need.id);
|
||||||
|
setEditForm({ ...need });
|
||||||
|
};
|
||||||
|
|
||||||
|
const cancelEdit = () => {
|
||||||
|
setEditingId(null);
|
||||||
|
setEditForm({});
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveEdit = () => {
|
||||||
|
onUpdate(editingId, editForm);
|
||||||
|
setEditingId(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAddDefault = (template) => {
|
||||||
|
onAdd(template);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="needs-editor">
|
||||||
|
<div className="editor-toolbar">
|
||||||
|
<h3>Character Needs</h3>
|
||||||
|
<div className="default-needs-buttons">
|
||||||
|
{DEFAULT_NEEDS.map((n) => (
|
||||||
|
<button
|
||||||
|
key={n.name}
|
||||||
|
className="btn-small"
|
||||||
|
onClick={() => handleAddDefault(n)}
|
||||||
|
disabled={needs?.some((existing) => existing.name === n.name)}
|
||||||
|
>
|
||||||
|
+ {n.name}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{(!needs || needs.length === 0) ? (
|
||||||
|
<p className="empty-state">No needs defined yet. Click a button above to add default needs.</p>
|
||||||
|
) : (
|
||||||
|
<div className="needs-list">
|
||||||
|
{needs.map((need) => (
|
||||||
|
<div key={need.id} className="need-card">
|
||||||
|
{editingId === need.id ? (
|
||||||
|
<div className="need-edit-form">
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Name</label>
|
||||||
|
<input value={editForm.name} onChange={(e) => setEditForm({ ...editForm, name: e.target.value })} />
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Enabled</label>
|
||||||
|
<input type="checkbox" checked={editForm.enabled} onChange={(e) => setEditForm({ ...editForm, enabled: e.target.checked })} />
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Initial Value</label>
|
||||||
|
<input type="number" value={editForm.initial_value} onChange={(e) => setEditForm({ ...editForm, initial_value: +e.target.value })} />
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Min / Max</label>
|
||||||
|
<div className="row-inputs">
|
||||||
|
<input type="number" value={editForm.min_value} onChange={(e) => setEditForm({ ...editForm, min_value: +e.target.value })} />
|
||||||
|
<input type="number" value={editForm.max_value} onChange={(e) => setEditForm({ ...editForm, max_value: +e.target.value })} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Decay Rate</label>
|
||||||
|
<input type="number" step="0.1" value={editForm.decay_rate} onChange={(e) => setEditForm({ ...editForm, decay_rate: +e.target.value })} />
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Priority</label>
|
||||||
|
<input type="number" value={editForm.priority} onChange={(e) => setEditForm({ ...editForm, priority: +e.target.value })} />
|
||||||
|
</div>
|
||||||
|
<div className="form-actions">
|
||||||
|
<button className="btn-primary" onClick={saveEdit}>Save</button>
|
||||||
|
<button className="btn-secondary" onClick={cancelEdit}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="need-header">
|
||||||
|
<h4>{need.name}</h4>
|
||||||
|
<span className={`need-status ${need.enabled ? 'enabled' : 'disabled'}`}>
|
||||||
|
{need.enabled ? 'Active' : 'Disabled'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="need-stats">
|
||||||
|
<div className="stat">
|
||||||
|
<span className="stat-label">Initial</span>
|
||||||
|
<span className="stat-value">{need.initial_value}</span>
|
||||||
|
</div>
|
||||||
|
<div className="stat">
|
||||||
|
<span className="stat-label">Range</span>
|
||||||
|
<span className="stat-value">{need.min_value} - {need.max_value}</span>
|
||||||
|
</div>
|
||||||
|
<div className="stat">
|
||||||
|
<span className="stat-label">Decay</span>
|
||||||
|
<span className="stat-value">{need.decay_rate}/s</span>
|
||||||
|
</div>
|
||||||
|
<div className="stat">
|
||||||
|
<span className="stat-label">Priority</span>
|
||||||
|
<span className="stat-value">{need.priority}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="need-actions">
|
||||||
|
<button className="btn-small" onClick={() => startEdit(need)}>Edit</button>
|
||||||
|
<button className="btn-danger-small" onClick={() => onDelete(need.id)}>Delete</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
193
client/src/components/SimulationView.js
Normal file
193
client/src/components/SimulationView.js
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
import React, { useState, useRef, useEffect } from 'react';
|
||||||
|
import { characters } from '../services/api';
|
||||||
|
|
||||||
|
export default function SimulationView({ characterId, needs }) {
|
||||||
|
const [steps, setSteps] = useState(20);
|
||||||
|
const [events, setEvents] = useState('');
|
||||||
|
const [simulation, setSimulation] = useState(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const canvasRef = useRef(null);
|
||||||
|
|
||||||
|
const runSimulation = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
const parsedEvents = events.trim()
|
||||||
|
? events.split('\n').filter(Boolean).map((line) => {
|
||||||
|
const parts = line.split(':').map((s) => s.trim());
|
||||||
|
const obj = {};
|
||||||
|
obj[parts[0]] = parseFloat(parts[1]);
|
||||||
|
return obj;
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const data = await characters.simulate(characterId, { steps, events: parsedEvents });
|
||||||
|
setSimulation(data);
|
||||||
|
} catch (err) {
|
||||||
|
alert(err.message);
|
||||||
|
}
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!simulation || !canvasRef.current) return;
|
||||||
|
|
||||||
|
const canvas = canvasRef.current;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
const width = canvas.width;
|
||||||
|
const height = canvas.height;
|
||||||
|
const padding = 40;
|
||||||
|
const chartWidth = width - padding * 2;
|
||||||
|
const chartHeight = height - padding * 2;
|
||||||
|
|
||||||
|
ctx.clearRect(0, 0, width, height);
|
||||||
|
|
||||||
|
const finalValues = simulation.simulation;
|
||||||
|
if (!finalValues || finalValues.length === 0) return;
|
||||||
|
|
||||||
|
const needNames = Object.keys(finalValues[0].values);
|
||||||
|
const colors = {
|
||||||
|
Food: '#e74c3c',
|
||||||
|
Energy: '#f39c12',
|
||||||
|
Bladder: '#3498db',
|
||||||
|
Bowel: '#8e44ad',
|
||||||
|
Hormones: '#e91e63',
|
||||||
|
Intimate: '#ff6b6b',
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.strokeStyle = '#333';
|
||||||
|
ctx.lineWidth = 1;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(padding, padding);
|
||||||
|
ctx.lineTo(padding, height - padding);
|
||||||
|
ctx.lineTo(width - padding, height - padding);
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
for (let i = 0; i <= 4; i++) {
|
||||||
|
const y = height - padding - (chartHeight * i) / 4;
|
||||||
|
ctx.strokeStyle = '#2a2a2a';
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(padding, y);
|
||||||
|
ctx.lineTo(width - padding, y);
|
||||||
|
ctx.stroke();
|
||||||
|
ctx.fillStyle = '#888';
|
||||||
|
ctx.font = '10px monospace';
|
||||||
|
ctx.fillText(`${i * 25}`, 5, y + 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
needNames.forEach((needName) => {
|
||||||
|
ctx.strokeStyle = colors[needName] || '#95a5a6';
|
||||||
|
ctx.lineWidth = 2;
|
||||||
|
ctx.beginPath();
|
||||||
|
|
||||||
|
finalValues.forEach((step, index) => {
|
||||||
|
const x = padding + (chartWidth * index) / (finalValues.length - 1);
|
||||||
|
const y = height - padding - (chartHeight * (step.values[needName] || 0)) / 100;
|
||||||
|
|
||||||
|
if (index === 0) ctx.moveTo(x, y);
|
||||||
|
else ctx.lineTo(x, y);
|
||||||
|
});
|
||||||
|
ctx.stroke();
|
||||||
|
});
|
||||||
|
|
||||||
|
const legendY = 15;
|
||||||
|
ctx.font = '11px monospace';
|
||||||
|
needNames.forEach((name, i) => {
|
||||||
|
const x = padding + i * 100;
|
||||||
|
ctx.fillStyle = colors[name] || '#95a5a6';
|
||||||
|
ctx.fillRect(x, legendY, 12, 12);
|
||||||
|
ctx.fillStyle = '#ccc';
|
||||||
|
ctx.fillText(name, x + 16, legendY + 10);
|
||||||
|
});
|
||||||
|
}, [simulation]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="simulation-view">
|
||||||
|
<div className="editor-toolbar">
|
||||||
|
<h3>Need Simulation</h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="simulation-controls">
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Simulation Steps</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={steps}
|
||||||
|
onChange={(e) => setSteps(Math.max(2, +e.target.value))}
|
||||||
|
min={2}
|
||||||
|
max={100}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Events (one per line, format: NeedName:value)</label>
|
||||||
|
<textarea
|
||||||
|
value={events}
|
||||||
|
onChange={(e) => setEvents(e.target.value)}
|
||||||
|
placeholder={'Food:30\nEnergy:20\nBladder:-10'}
|
||||||
|
rows={3}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button className="btn-primary" onClick={runSimulation} disabled={loading}>
|
||||||
|
{loading ? 'Simulating...' : 'Run Simulation'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{simulation && (
|
||||||
|
<div className="simulation-results">
|
||||||
|
<canvas ref={canvasRef} width={600} height={300} className="simulation-chart" />
|
||||||
|
|
||||||
|
<div className="final-values">
|
||||||
|
<h4>Final Values</h4>
|
||||||
|
<div className="final-values-grid">
|
||||||
|
{Object.entries(simulation.final_values || {}).map(([name, value]) => (
|
||||||
|
<div key={name} className="final-value-item">
|
||||||
|
<span className="final-name">{name}</span>
|
||||||
|
<div className="final-bar-container">
|
||||||
|
<div
|
||||||
|
className="final-bar"
|
||||||
|
style={{
|
||||||
|
width: `${value}%`,
|
||||||
|
backgroundColor: colors[name] || '#3498db',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<span className="final-number">{value}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="simulation-log">
|
||||||
|
<h4>Triggered Rules Log</h4>
|
||||||
|
<div className="log-entries">
|
||||||
|
{simulation.simulation.map((step) =>
|
||||||
|
step.triggered_rules.length > 0 && (
|
||||||
|
<div key={step.step} className="log-entry">
|
||||||
|
<span className="log-step">Step {step.step + 1}:</span>
|
||||||
|
{step.triggered_rules.map((rule, i) => (
|
||||||
|
<span key={i} className="log-rule">
|
||||||
|
{rule.action?.type || 'unknown action'}
|
||||||
|
{rule.action?.target ? ` → ${rule.action.target}` : ''}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
{!simulation.simulation.some((s) => s.triggered_rules.length > 0) && (
|
||||||
|
<p className="empty-state">No rules were triggered during this simulation.</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const colors = {
|
||||||
|
Food: '#e74c3c',
|
||||||
|
Energy: '#f39c12',
|
||||||
|
Bladder: '#3498db',
|
||||||
|
Bowel: '#8e44ad',
|
||||||
|
Hormones: '#e91e63',
|
||||||
|
Intimate: '#ff6b6b',
|
||||||
|
};
|
||||||
160
client/src/components/UIElementEditor.js
Normal file
160
client/src/components/UIElementEditor.js
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
|
||||||
|
const ELEMENT_TYPES = [
|
||||||
|
{ value: 'progress_bar', label: 'Progress Bar' },
|
||||||
|
{ value: 'gauge', label: 'Gauge' },
|
||||||
|
{ value: 'icon', label: 'Icon' },
|
||||||
|
{ value: 'text_label', label: 'Text Label' },
|
||||||
|
{ value: 'radial', label: 'Radial Dial' },
|
||||||
|
{ value: 'waveform', label: 'Waveform' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const COLORS = ['#e74c3c', '#f39c12', '#3498db', '#8e44ad', '#e91e63', '#ff6b6b', '#2ecc71', '#1abc9c'];
|
||||||
|
|
||||||
|
export default function UIElementEditor({ uiElements, needs, onAdd, onUpdate, onDelete }) {
|
||||||
|
const [editingId, setEditingId] = useState(null);
|
||||||
|
const [editForm, setEditForm] = useState({});
|
||||||
|
|
||||||
|
const startEdit = (el) => {
|
||||||
|
setEditingId(el.id);
|
||||||
|
setEditForm({ ...el, config: typeof el.config === 'string' ? JSON.parse(el.config) : el.config });
|
||||||
|
};
|
||||||
|
|
||||||
|
const cancelEdit = () => {
|
||||||
|
setEditingId(null);
|
||||||
|
setEditForm({});
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveEdit = () => {
|
||||||
|
onUpdate(editingId, editForm);
|
||||||
|
setEditingId(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAdd = () => {
|
||||||
|
onAdd({
|
||||||
|
need_id: needs?.[0]?.id || null,
|
||||||
|
element_type: 'progress_bar',
|
||||||
|
config: { color: '#3498db', label: 'New Element', animation: 'smooth' },
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="ui-element-editor">
|
||||||
|
<div className="editor-toolbar">
|
||||||
|
<h3>UI Elements</h3>
|
||||||
|
<button className="btn-primary" onClick={handleAdd}>Add UI Element</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{(!uiElements || uiElements.length === 0) ? (
|
||||||
|
<p className="empty-state">No UI elements defined. These represent how needs will visually appear.</p>
|
||||||
|
) : (
|
||||||
|
<div className="ui-element-list">
|
||||||
|
{uiElements.map((el) => {
|
||||||
|
const config = typeof el.config === 'string' ? JSON.parse(el.config) : el.config;
|
||||||
|
const need = needs?.find((n) => n.id === el.need_id);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div key={el.id} className="ui-element-card">
|
||||||
|
{editingId === el.id ? (
|
||||||
|
<div className="ui-edit-form">
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Linked Need</label>
|
||||||
|
<select value={editForm.need_id || ''} onChange={(e) => setEditForm({ ...editForm, need_id: e.target.value })}>
|
||||||
|
<option value="">None</option>
|
||||||
|
{needs?.map((n) => (
|
||||||
|
<option key={n.id} value={n.id}>{n.name}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Element Type</label>
|
||||||
|
<select value={editForm.element_type} onChange={(e) => setEditForm({ ...editForm, element_type: e.target.value })}>
|
||||||
|
{ELEMENT_TYPES.map((t) => (
|
||||||
|
<option key={t.value} value={t.value}>{t.label}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Color</label>
|
||||||
|
<div className="color-picker">
|
||||||
|
{COLORS.map((c) => (
|
||||||
|
<button
|
||||||
|
key={c}
|
||||||
|
className={`color-swatch ${editForm.config?.color === c ? 'selected' : ''}`}
|
||||||
|
style={{ backgroundColor: c }}
|
||||||
|
onClick={() => setEditForm({ ...editForm, config: { ...editForm.config, color: c } })}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Label</label>
|
||||||
|
<input value={editForm.config?.label || ''} onChange={(e) => setEditForm({ ...editForm, config: { ...editForm.config, label: e.target.value } })} />
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label>Animation</label>
|
||||||
|
<select value={editForm.config?.animation || 'none'} onChange={(e) => setEditForm({ ...editForm, config: { ...editForm.config, animation: e.target.value } })}>
|
||||||
|
<option value="none">None</option>
|
||||||
|
<option value="smooth">Smooth</option>
|
||||||
|
<option value="pulse">Pulse</option>
|
||||||
|
<option value="bounce">Bounce</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="form-actions">
|
||||||
|
<button className="btn-primary" onClick={saveEdit}>Save</button>
|
||||||
|
<button className="btn-secondary" onClick={cancelEdit}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="ui-element-preview" style={{ borderLeftColor: config?.color || '#95a5a6' }}>
|
||||||
|
<div className="ui-element-header">
|
||||||
|
<h4>{config?.label || el.element_type}</h4>
|
||||||
|
<span className="element-type-badge">{ELEMENT_TYPES.find(t => t.value === el.element_type)?.label || el.element_type}</span>
|
||||||
|
</div>
|
||||||
|
<div className="ui-element-details">
|
||||||
|
<span>Linked to: <strong>{need?.name || 'None'}</strong></span>
|
||||||
|
<span>Animation: <strong>{config?.animation || 'none'}</strong></span>
|
||||||
|
</div>
|
||||||
|
<div className="ui-element-visual">
|
||||||
|
{el.element_type === 'progress_bar' && (
|
||||||
|
<div className="visual-progress-bar">
|
||||||
|
<div className="visual-progress-fill" style={{ width: '60%', backgroundColor: config?.color }} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{el.element_type === 'gauge' && (
|
||||||
|
<div className="visual-gauge" style={{ color: config?.color }}>
|
||||||
|
<span className="gauge-value">75%</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{el.element_type === 'radial' && (
|
||||||
|
<div className="visual-radial" style={{ borderColor: config?.color }}>
|
||||||
|
<span>60%</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{(el.element_type === 'icon' || el.element_type === 'text_label') && (
|
||||||
|
<div className="visual-icon" style={{ color: config?.color }}>
|
||||||
|
{config?.label || el.element_type}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{el.element_type === 'waveform' && (
|
||||||
|
<div className="visual-waveform" style={{ color: config?.color }}>
|
||||||
|
~ ~ ~
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="ui-element-actions">
|
||||||
|
<button className="btn-small" onClick={() => startEdit(el)}>Edit</button>
|
||||||
|
<button className="btn-danger-small" onClick={() => onDelete(el.id)}>Delete</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
52
client/src/context/AuthContext.js
Normal file
52
client/src/context/AuthContext.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import React, { createContext, useState, useEffect, useContext } from 'react';
|
||||||
|
import { auth } from '../services/api';
|
||||||
|
|
||||||
|
const AuthContext = createContext(null);
|
||||||
|
|
||||||
|
export function AuthProvider({ children }) {
|
||||||
|
const [user, setUser] = useState(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (token) {
|
||||||
|
auth.me()
|
||||||
|
.then((data) => setUser(data.user))
|
||||||
|
.catch(() => localStorage.removeItem('token'))
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
} else {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const login = async (username, password) => {
|
||||||
|
const data = await auth.login({ username, password });
|
||||||
|
localStorage.setItem('token', data.token);
|
||||||
|
setUser(data.user);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const register = async (username, email, password) => {
|
||||||
|
const data = await auth.register({ username, email, password });
|
||||||
|
localStorage.setItem('token', data.token);
|
||||||
|
setUser(data.user);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
localStorage.removeItem('token');
|
||||||
|
setUser(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AuthContext.Provider value={{ user, loading, login, register, logout }}>
|
||||||
|
{children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useAuth() {
|
||||||
|
const context = useContext(AuthContext);
|
||||||
|
if (!context) throw new Error('useAuth must be used within AuthProvider');
|
||||||
|
return context;
|
||||||
|
}
|
||||||
1490
client/src/index.css
Normal file
1490
client/src/index.css
Normal file
File diff suppressed because it is too large
Load Diff
11
client/src/index.js
Normal file
11
client/src/index.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import ReactDOM from 'react-dom/client';
|
||||||
|
import './index.css';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
|
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||||
|
root.render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<App />
|
||||||
|
</React.StrictMode>
|
||||||
|
);
|
||||||
1
client/src/logo.svg
Normal file
1
client/src/logo.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>
|
||||||
|
After Width: | Height: | Size: 2.6 KiB |
339
client/src/pages/CharacterEditor.js
Normal file
339
client/src/pages/CharacterEditor.js
Normal file
@ -0,0 +1,339 @@
|
|||||||
|
import React, { useState, useEffect, useCallback } from 'react';
|
||||||
|
import { useParams, useNavigate } from 'react-router-dom';
|
||||||
|
import { characters } from '../services/api';
|
||||||
|
import NeedsEditor from '../components/NeedsEditor';
|
||||||
|
import UIElementEditor from '../components/UIElementEditor';
|
||||||
|
import BrainRuleEditor from '../components/BrainRuleEditor';
|
||||||
|
import SimulationView from '../components/SimulationView';
|
||||||
|
import AIAssistant from '../components/AIAssistant';
|
||||||
|
|
||||||
|
export default function CharacterEditor() {
|
||||||
|
const { id } = useParams();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const isNew = !id || id === 'new';
|
||||||
|
|
||||||
|
const [character, setCharacter] = useState({
|
||||||
|
name: '',
|
||||||
|
description: '',
|
||||||
|
personality_traits: [],
|
||||||
|
backstory: '',
|
||||||
|
});
|
||||||
|
const [needs, setNeeds] = useState([]);
|
||||||
|
const [uiElements, setUIElements] = useState([]);
|
||||||
|
const [brainRules, setBrainRules] = useState([]);
|
||||||
|
const [activeTab, setActiveTab] = useState('profile');
|
||||||
|
const [traitInput, setTraitInput] = useState('');
|
||||||
|
const [loading, setLoading] = useState(!isNew);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isNew) {
|
||||||
|
characters.get(id)
|
||||||
|
.then((data) => {
|
||||||
|
setCharacter({
|
||||||
|
name: data.character.name,
|
||||||
|
description: data.character.description || '',
|
||||||
|
personality_traits: JSON.parse(data.character.personality_traits || '[]'),
|
||||||
|
backstory: data.character.backstory || '',
|
||||||
|
});
|
||||||
|
setNeeds(data.needs || []);
|
||||||
|
setUIElements(data.ui_elements?.map(el => ({
|
||||||
|
...el,
|
||||||
|
config: typeof el.config === 'string' ? JSON.parse(el.config) : el.config
|
||||||
|
})) || []);
|
||||||
|
setBrainRules(data.brain_rules?.map(rule => ({
|
||||||
|
...rule,
|
||||||
|
condition: typeof rule.condition === 'string' ? JSON.parse(rule.condition) : rule.condition,
|
||||||
|
action: typeof rule.action === 'string' ? JSON.parse(rule.action) : rule.action,
|
||||||
|
})) || []);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
alert(err.message);
|
||||||
|
navigate('/characters');
|
||||||
|
})
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}
|
||||||
|
}, [id, isNew, navigate]);
|
||||||
|
|
||||||
|
const saveCharacter = async (charData = character) => {
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const payload = {
|
||||||
|
...charData,
|
||||||
|
personality_traits: charData.personality_traits || [],
|
||||||
|
};
|
||||||
|
|
||||||
|
let result;
|
||||||
|
if (isNew) {
|
||||||
|
result = await characters.create(payload);
|
||||||
|
navigate(`/characters/${result.character.id}`, { replace: true });
|
||||||
|
} else {
|
||||||
|
result = await characters.update(id, payload);
|
||||||
|
}
|
||||||
|
return result.character;
|
||||||
|
} catch (err) {
|
||||||
|
alert(err.message);
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const addTrait = () => {
|
||||||
|
if (traitInput.trim()) {
|
||||||
|
setCharacter({
|
||||||
|
...character,
|
||||||
|
personality_traits: [...character.personality_traits, traitInput.trim()],
|
||||||
|
});
|
||||||
|
setTraitInput('');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeTrait = (index) => {
|
||||||
|
setCharacter({
|
||||||
|
...character,
|
||||||
|
personality_traits: character.personality_traits.filter((_, i) => i !== index),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAddNeed = async (needData) => {
|
||||||
|
if (isNew) {
|
||||||
|
const char = await saveCharacter();
|
||||||
|
if (char) {
|
||||||
|
const result = await characters.createNeed(char.id, needData);
|
||||||
|
setNeeds([...needs, result.need]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const result = await characters.createNeed(id, needData);
|
||||||
|
setNeeds([...needs, result.need]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateNeed = async (needId, needData) => {
|
||||||
|
const result = await characters.updateNeed(id, needId, needData);
|
||||||
|
setNeeds(needs.map((n) => (n.id === needId ? result.need : n)));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteNeed = async (needId) => {
|
||||||
|
await characters.deleteNeed(id, needId);
|
||||||
|
setNeeds(needs.filter((n) => n.id !== needId));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAddUIElement = async (elData) => {
|
||||||
|
if (isNew) {
|
||||||
|
const char = await saveCharacter();
|
||||||
|
if (char) {
|
||||||
|
const result = await characters.createUIElement(char.id, elData);
|
||||||
|
setUIElements([...uiElements, result.ui_element]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const result = await characters.createUIElement(id, elData);
|
||||||
|
setUIElements([...uiElements, result.ui_element]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateUIElement = async (elemId, elData) => {
|
||||||
|
const result = await characters.updateUIElement(id, elemId, {
|
||||||
|
...elData,
|
||||||
|
config: elData.config,
|
||||||
|
});
|
||||||
|
setUIElements(uiElements.map((el) => (el.id === elemId ? { ...result.ui_element, config: typeof result.ui_element.config === 'string' ? JSON.parse(result.ui_element.config) : result.ui_element.config } : el)));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteUIElement = async (elemId) => {
|
||||||
|
await characters.deleteUIElement(id, elemId);
|
||||||
|
setUIElements(uiElements.filter((el) => el.id !== elemId));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAddBrainRule = async (ruleData) => {
|
||||||
|
if (isNew) {
|
||||||
|
const char = await saveCharacter();
|
||||||
|
if (char) {
|
||||||
|
const result = await characters.createBrainRule(char.id, ruleData);
|
||||||
|
setBrainRules([...brainRules, result.brain_rule]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const result = await characters.createBrainRule(id, ruleData);
|
||||||
|
setBrainRules([...brainRules, result.brain_rule]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateBrainRule = async (ruleId, ruleData) => {
|
||||||
|
const result = await characters.updateBrainRule(id, ruleId, ruleData);
|
||||||
|
setBrainRules(brainRules.map((r) => (r.id === ruleId ? { ...result.brain_rule, condition: typeof result.brain_rule.condition === 'string' ? JSON.parse(result.brain_rule.condition) : result.brain_rule.condition, action: typeof result.brain_rule.action === 'string' ? JSON.parse(result.brain_rule.action) : result.brain_rule.action } : r)));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteBrainRule = async (ruleId) => {
|
||||||
|
await characters.deleteBrainRule(id, ruleId);
|
||||||
|
setBrainRules(brainRules.filter((r) => r.id !== ruleId));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAISuggestion = useCallback((suggestion) => {
|
||||||
|
const needsWithDefaults = (suggestion.suggested_needs || []).map((n) => ({
|
||||||
|
name: n.name,
|
||||||
|
enabled: n.enabled,
|
||||||
|
initial_value: n.initial_value,
|
||||||
|
min_value: n.min_value,
|
||||||
|
max_value: n.max_value,
|
||||||
|
decay_rate: n.decay_rate,
|
||||||
|
priority: n.priority,
|
||||||
|
}));
|
||||||
|
|
||||||
|
setCharacter({
|
||||||
|
name: suggestion.name || '',
|
||||||
|
description: suggestion.description || '',
|
||||||
|
personality_traits: suggestion.personality_traits || [],
|
||||||
|
backstory: suggestion.backstory || '',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (id && !isNew) {
|
||||||
|
Promise.all([
|
||||||
|
...needsWithDefaults.map((n) => characters.createNeed(id, n)),
|
||||||
|
...(suggestion.suggested_ui_elements || []).map((ui) => characters.createUIElement(id, {
|
||||||
|
need_id: null,
|
||||||
|
element_type: ui.element_type,
|
||||||
|
config: ui.config,
|
||||||
|
})),
|
||||||
|
]).then((results) => {
|
||||||
|
const newNeeds = results.filter(r => r.need).map(r => r.need);
|
||||||
|
const newUI = results.filter(r => r.ui_element).map(r => r.ui_element);
|
||||||
|
setNeeds([...needs, ...newNeeds]);
|
||||||
|
setUIElements([...uiElements, ...newUI]);
|
||||||
|
}).catch(console.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
setActiveTab('profile');
|
||||||
|
}, [id, isNew, needs, uiElements]);
|
||||||
|
|
||||||
|
const saveProfile = async () => {
|
||||||
|
const saved = await saveCharacter();
|
||||||
|
if (saved) setActiveTab('needs');
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
const tabs = [
|
||||||
|
{ id: 'profile', label: 'Profile' },
|
||||||
|
{ id: 'needs', label: 'Needs' },
|
||||||
|
{ id: 'ui', label: 'UI Elements' },
|
||||||
|
{ id: 'brain', label: 'Brain Rules' },
|
||||||
|
{ id: 'simulation', label: 'Simulation' },
|
||||||
|
{ id: 'ai', label: 'AI Assistant' },
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="character-editor">
|
||||||
|
<div className="editor-header">
|
||||||
|
<h1>{isNew ? 'New Character' : `Editing: ${character.name}`}</h1>
|
||||||
|
<div className="editor-header-actions">
|
||||||
|
<button className="btn-primary" onClick={saveProfile} disabled={saving}>
|
||||||
|
{saving ? 'Saving...' : 'Save Character'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="editor-tabs">
|
||||||
|
{tabs.map((tab) => (
|
||||||
|
<button
|
||||||
|
key={tab.id}
|
||||||
|
className={`tab-btn ${activeTab === tab.id ? 'active' : ''}`}
|
||||||
|
onClick={() => setActiveTab(tab.id)}
|
||||||
|
>
|
||||||
|
{tab.label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="editor-content">
|
||||||
|
{activeTab === 'profile' && (
|
||||||
|
<div className="profile-editor">
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Character Name</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={character.name}
|
||||||
|
onChange={(e) => setCharacter({ ...character, name: e.target.value })}
|
||||||
|
placeholder="Enter character name"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Description</label>
|
||||||
|
<textarea
|
||||||
|
value={character.description}
|
||||||
|
onChange={(e) => setCharacter({ ...character, description: e.target.value })}
|
||||||
|
placeholder="Brief description of the character"
|
||||||
|
rows={3}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Personality Traits</label>
|
||||||
|
<div className="trait-input-row">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={traitInput}
|
||||||
|
onChange={(e) => setTraitInput(e.target.value)}
|
||||||
|
placeholder="Add a trait..."
|
||||||
|
onKeyDown={(e) => e.key === 'Enter' && addTrait()}
|
||||||
|
/>
|
||||||
|
<button className="btn-small" onClick={addTrait}>Add</button>
|
||||||
|
</div>
|
||||||
|
<div className="traits-display">
|
||||||
|
{character.personality_traits.map((trait, i) => (
|
||||||
|
<span key={i} className="trait-tag">
|
||||||
|
{trait}
|
||||||
|
<button className="trait-remove" onClick={() => removeTrait(i)}>×</button>
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Backstory</label>
|
||||||
|
<textarea
|
||||||
|
value={character.backstory}
|
||||||
|
onChange={(e) => setCharacter({ ...character, backstory: e.target.value })}
|
||||||
|
placeholder="Write the character's backstory..."
|
||||||
|
rows={6}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{activeTab === 'needs' && (
|
||||||
|
<NeedsEditor
|
||||||
|
needs={needs}
|
||||||
|
onUpdate={handleUpdateNeed}
|
||||||
|
onAdd={handleAddNeed}
|
||||||
|
onDelete={handleDeleteNeed}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{activeTab === 'ui' && (
|
||||||
|
<UIElementEditor
|
||||||
|
uiElements={uiElements}
|
||||||
|
needs={needs}
|
||||||
|
onAdd={handleAddUIElement}
|
||||||
|
onUpdate={handleUpdateUIElement}
|
||||||
|
onDelete={handleDeleteUIElement}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{activeTab === 'brain' && (
|
||||||
|
<BrainRuleEditor
|
||||||
|
brainRules={brainRules}
|
||||||
|
needs={needs}
|
||||||
|
onAdd={handleAddBrainRule}
|
||||||
|
onUpdate={handleUpdateBrainRule}
|
||||||
|
onDelete={handleDeleteBrainRule}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{activeTab === 'simulation' && id && (
|
||||||
|
<SimulationView characterId={id} needs={needs} />
|
||||||
|
)}
|
||||||
|
|
||||||
|
{activeTab === 'ai' && (
|
||||||
|
<AIAssistant onApplySuggestion={handleAISuggestion} characterId={id} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
59
client/src/pages/Characters.js
Normal file
59
client/src/pages/Characters.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import { Link, useNavigate } from 'react-router-dom';
|
||||||
|
import { characters } from '../services/api';
|
||||||
|
|
||||||
|
export default function Characters() {
|
||||||
|
const [chars, setChars] = useState([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
characters.list()
|
||||||
|
.then((data) => setChars(data.characters || []))
|
||||||
|
.catch(console.error)
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleDelete = async (id, e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (!window.confirm('Delete this character?')) return;
|
||||||
|
await characters.delete(id);
|
||||||
|
setChars(chars.filter((c) => c.id !== id));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="page">
|
||||||
|
<div className="page-header">
|
||||||
|
<h1>Characters</h1>
|
||||||
|
<Link to="/characters/new" className="btn-primary">New Character</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{chars.length === 0 ? (
|
||||||
|
<div className="empty-page">
|
||||||
|
<p>No characters yet. Create your first character!</p>
|
||||||
|
<Link to="/characters/new" className="btn-primary">Create Character</Link>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="grid-list">
|
||||||
|
{chars.map((c) => (
|
||||||
|
<div key={c.id} className="grid-card" onClick={() => navigate(`/characters/${c.id}`)}>
|
||||||
|
<div className="grid-card-header">
|
||||||
|
<h3>{c.name}</h3>
|
||||||
|
<button className="btn-danger-small" onClick={(e) => handleDelete(c.id, e)}>×</button>
|
||||||
|
</div>
|
||||||
|
<p className="grid-card-desc">{c.description || 'No description'}</p>
|
||||||
|
<div className="grid-card-traits">
|
||||||
|
{JSON.parse(c.personality_traits || '[]').slice(0, 3).map((t, i) => (
|
||||||
|
<span key={i} className="trait-tag">{t}</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<span className="grid-card-date">{new Date(c.updated_at).toLocaleDateString()}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
77
client/src/pages/Dashboard.js
Normal file
77
client/src/pages/Dashboard.js
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import { Link } from 'react-router-dom';
|
||||||
|
import { characters, lorebooks } from '../services/api';
|
||||||
|
|
||||||
|
export default function Dashboard() {
|
||||||
|
const [chars, setChars] = useState([]);
|
||||||
|
const [books, setBooks] = useState([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
Promise.all([characters.list(), lorebooks.list()])
|
||||||
|
.then(([charData, loreData]) => {
|
||||||
|
setChars(charData.characters || []);
|
||||||
|
setBooks(loreData.lorebooks || []);
|
||||||
|
})
|
||||||
|
.catch(console.error)
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="dashboard">
|
||||||
|
<div className="dashboard-header">
|
||||||
|
<h1>Dashboard</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="dashboard-grid">
|
||||||
|
<div className="dashboard-card">
|
||||||
|
<div className="card-header">
|
||||||
|
<h2>Characters</h2>
|
||||||
|
<Link to="/characters" className="btn-small">View All</Link>
|
||||||
|
</div>
|
||||||
|
{chars.length === 0 ? (
|
||||||
|
<p className="empty-state">No characters yet. <Link to="/characters/new">Create one!</Link></p>
|
||||||
|
) : (
|
||||||
|
<div className="card-list">
|
||||||
|
{chars.slice(0, 5).map((c) => (
|
||||||
|
<Link key={c.id} to={`/characters/${c.id}`} className="card-list-item">
|
||||||
|
<span className="item-name">{c.name}</span>
|
||||||
|
<span className="item-meta">{new Date(c.updated_at).toLocaleDateString()}</span>
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="dashboard-card">
|
||||||
|
<div className="card-header">
|
||||||
|
<h2>Lorebooks</h2>
|
||||||
|
<Link to="/lorebooks" className="btn-small">View All</Link>
|
||||||
|
</div>
|
||||||
|
{books.length === 0 ? (
|
||||||
|
<p className="empty-state">No lorebooks yet. <Link to="/lorebooks/new">Create one!</Link></p>
|
||||||
|
) : (
|
||||||
|
<div className="card-list">
|
||||||
|
{books.slice(0, 5).map((b) => (
|
||||||
|
<Link key={b.id} to={`/lorebooks/${b.id}`} className="card-list-item">
|
||||||
|
<span className="item-name">{b.name}</span>
|
||||||
|
<span className="item-meta">{new Date(b.updated_at).toLocaleDateString()}</span>
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="dashboard-card quick-actions">
|
||||||
|
<h2>Quick Actions</h2>
|
||||||
|
<div className="action-buttons">
|
||||||
|
<Link to="/characters/new" className="btn-primary">New Character</Link>
|
||||||
|
<Link to="/lorebooks/new" className="btn-secondary">New Lorebook</Link>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
37
client/src/pages/Login.js
Normal file
37
client/src/pages/Login.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { useNavigate, Link } from 'react-router-dom';
|
||||||
|
import { useAuth } from '../context/AuthContext';
|
||||||
|
|
||||||
|
export default function Login() {
|
||||||
|
const [username, setUsername] = useState('');
|
||||||
|
const [password, setPassword] = useState('');
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const { login } = useAuth();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const handleSubmit = async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
try {
|
||||||
|
await login(username, password);
|
||||||
|
navigate('/');
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="auth-page">
|
||||||
|
<div className="auth-card">
|
||||||
|
<h1>Character Sandbox</h1>
|
||||||
|
<h2>Sign In</h2>
|
||||||
|
{error && <div className="error-message">{error}</div>}
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<input type="text" placeholder="Username" value={username} onChange={(e) => setUsername(e.target.value)} required />
|
||||||
|
<input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} required />
|
||||||
|
<button type="submit" className="btn-primary">Sign In</button>
|
||||||
|
</form>
|
||||||
|
<p className="auth-link">Don't have an account? <Link to="/register">Register</Link></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
263
client/src/pages/LorebookEditor.js
Normal file
263
client/src/pages/LorebookEditor.js
Normal file
@ -0,0 +1,263 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import { useParams, useNavigate } from 'react-router-dom';
|
||||||
|
import { lorebooks, fragments } from '../services/api';
|
||||||
|
|
||||||
|
export default function LorebookEditor() {
|
||||||
|
const { id } = useParams();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const isNew = !id || id === 'new';
|
||||||
|
|
||||||
|
const [lorebook, setLorebook] = useState({ name: '', description: '' });
|
||||||
|
const [fragmentsList, setFragmentsList] = useState([]);
|
||||||
|
const [loading, setLoading] = useState(!isNew);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [editingFragment, setEditingFragment] = useState(null);
|
||||||
|
const [showFragmentForm, setShowFragmentForm] = useState(false);
|
||||||
|
const [tagFilter, setTagFilter] = useState('');
|
||||||
|
const [searchQuery, setSearchQuery] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isNew) {
|
||||||
|
lorebooks.get(id)
|
||||||
|
.then((data) => {
|
||||||
|
setLorebook({ name: data.lorebook.name, description: data.lorebook.description || '' });
|
||||||
|
setFragmentsList(data.fragments?.map(f => ({
|
||||||
|
...f,
|
||||||
|
tags: typeof f.tags === 'string' ? JSON.parse(f.tags) : f.tags,
|
||||||
|
linked_characters: typeof f.linked_characters === 'string' ? JSON.parse(f.linked_characters) : f.linked_characters,
|
||||||
|
})) || []);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
alert(err.message);
|
||||||
|
navigate('/lorebooks');
|
||||||
|
})
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}
|
||||||
|
}, [id, isNew, navigate]);
|
||||||
|
|
||||||
|
const saveLorebook = async () => {
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
let result;
|
||||||
|
if (isNew) {
|
||||||
|
result = await lorebooks.create(lorebook);
|
||||||
|
navigate(`/lorebooks/${result.lorebook.id}`, { replace: true });
|
||||||
|
} else {
|
||||||
|
result = await lorebooks.update(id, lorebook);
|
||||||
|
}
|
||||||
|
return result.lorebook;
|
||||||
|
} catch (err) {
|
||||||
|
alert(err.message);
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSaveLorebook = async () => {
|
||||||
|
if (isNew) {
|
||||||
|
await saveLorebook();
|
||||||
|
} else {
|
||||||
|
await lorebooks.update(id, lorebook);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const openNewFragment = () => {
|
||||||
|
setEditingFragment({ title: '', content: '', tags: [], linked_characters: [] });
|
||||||
|
setShowFragmentForm(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const editFragment = (frag) => {
|
||||||
|
setEditingFragment({ ...frag });
|
||||||
|
setShowFragmentForm(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveFragment = async () => {
|
||||||
|
if (!editingFragment.title.trim()) return;
|
||||||
|
try {
|
||||||
|
if (editingFragment.id) {
|
||||||
|
const result = await fragments.update(editingFragment.id, editingFragment);
|
||||||
|
setFragmentsList(fragmentsList.map((f) => (f.id === editingFragment.id ? { ...result.fragment, tags: typeof result.fragment.tags === 'string' ? JSON.parse(result.fragment.tags) : result.fragment.tags, linked_characters: typeof result.fragment.linked_characters === 'string' ? JSON.parse(result.fragment.linked_characters) : result.fragment.linked_characters } : f)));
|
||||||
|
} else {
|
||||||
|
const bookId = id || (await saveLorebook())?.id;
|
||||||
|
if (bookId) {
|
||||||
|
const result = await fragments.create(bookId, editingFragment);
|
||||||
|
setFragmentsList([...fragmentsList, { ...result.fragment, tags: typeof result.fragment.tags === 'string' ? JSON.parse(result.fragment.tags) : result.fragment.tags, linked_characters: typeof result.fragment.linked_characters === 'string' ? JSON.parse(result.fragment.linked_characters) : result.fragment.linked_characters }]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setShowFragmentForm(false);
|
||||||
|
setEditingFragment(null);
|
||||||
|
} catch (err) {
|
||||||
|
alert(err.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteFragment = async (fragId) => {
|
||||||
|
if (!window.confirm('Delete this fragment?')) return;
|
||||||
|
await fragments.delete(fragId);
|
||||||
|
setFragmentsList(fragmentsList.filter((f) => f.id !== fragId));
|
||||||
|
};
|
||||||
|
|
||||||
|
const addTagToFragment = (tag) => {
|
||||||
|
if (tag.trim() && !editingFragment.tags.includes(tag.trim())) {
|
||||||
|
setEditingFragment({ ...editingFragment, tags: [...editingFragment.tags, tag.trim()] });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeTagFromFragment = (tag) => {
|
||||||
|
setEditingFragment({ ...editingFragment, tags: editingFragment.tags.filter((t) => t !== tag) });
|
||||||
|
};
|
||||||
|
|
||||||
|
const filteredFragments = fragmentsList.filter((f) => {
|
||||||
|
if (tagFilter && !f.tags?.includes(tagFilter)) return false;
|
||||||
|
if (searchQuery && !f.title.toLowerCase().includes(searchQuery.toLowerCase()) && !f.content?.toLowerCase().includes(searchQuery.toLowerCase())) return false;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
const allTags = [...new Set(fragmentsList.flatMap((f) => f.tags || []))];
|
||||||
|
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="lorebook-editor">
|
||||||
|
<div className="editor-header">
|
||||||
|
<h1>{isNew ? 'New Lorebook' : `Lorebook: ${lorebook.name}`}</h1>
|
||||||
|
<button className="btn-primary" onClick={handleSaveLorebook} disabled={saving}>
|
||||||
|
{saving ? 'Saving...' : 'Save Lorebook'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="lorebook-form">
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Lorebook Name</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={lorebook.name}
|
||||||
|
onChange={(e) => setLorebook({ ...lorebook, name: e.target.value })}
|
||||||
|
placeholder="Enter lorebook name"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Description</label>
|
||||||
|
<textarea
|
||||||
|
value={lorebook.description}
|
||||||
|
onChange={(e) => setLorebook({ ...lorebook, description: e.target.value })}
|
||||||
|
placeholder="Describe what this lorebook covers"
|
||||||
|
rows={2}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="fragments-section">
|
||||||
|
<div className="fragments-header">
|
||||||
|
<h2>Fragments</h2>
|
||||||
|
<button className="btn-primary" onClick={openNewFragment}>New Fragment</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="fragments-toolbar">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search fragments..."
|
||||||
|
value={searchQuery}
|
||||||
|
onChange={(e) => setSearchQuery(e.target.value)}
|
||||||
|
className="search-input"
|
||||||
|
/>
|
||||||
|
<div className="tag-filters">
|
||||||
|
{allTags.map((tag) => (
|
||||||
|
<button
|
||||||
|
key={tag}
|
||||||
|
className={`tag-filter-btn ${tagFilter === tag ? 'active' : ''}`}
|
||||||
|
onClick={() => setTagFilter(tagFilter === tag ? '' : tag)}
|
||||||
|
>
|
||||||
|
{tag}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{showFragmentForm && (
|
||||||
|
<div className="fragment-form-card">
|
||||||
|
<h3>{editingFragment?.id ? 'Edit Fragment' : 'New Fragment'}</h3>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Title</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={editingFragment?.title || ''}
|
||||||
|
onChange={(e) => setEditingFragment({ ...editingFragment, title: e.target.value })}
|
||||||
|
placeholder="Fragment title"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Content</label>
|
||||||
|
<textarea
|
||||||
|
value={editingFragment?.content || ''}
|
||||||
|
onChange={(e) => setEditingFragment({ ...editingFragment, content: e.target.value })}
|
||||||
|
placeholder="Write your lore fragment content here..."
|
||||||
|
rows={8}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>Tags</label>
|
||||||
|
<div className="tag-input-row">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Add a tag and press Enter"
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
addTagToFragment(e.target.value);
|
||||||
|
e.target.value = '';
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="tags-display">
|
||||||
|
{editingFragment?.tags?.map((tag) => (
|
||||||
|
<span key={tag} className="trait-tag">
|
||||||
|
{tag}
|
||||||
|
<button className="trait-remove" onClick={() => removeTagFromFragment(tag)}>×</button>
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="form-actions">
|
||||||
|
<button className="btn-primary" onClick={saveFragment}>Save Fragment</button>
|
||||||
|
<button className="btn-secondary" onClick={() => { setShowFragmentForm(false); setEditingFragment(null); }}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="fragments-list">
|
||||||
|
{filteredFragments.length === 0 ? (
|
||||||
|
<p className="empty-state">
|
||||||
|
{searchQuery || tagFilter ? 'No fragments match your search.' : 'No fragments yet. Create your first lore fragment!'}
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
filteredFragments.map((frag) => (
|
||||||
|
<div key={frag.id} className="fragment-card">
|
||||||
|
<div className="fragment-header">
|
||||||
|
<h3>{frag.title}</h3>
|
||||||
|
<div className="fragment-actions">
|
||||||
|
<button className="btn-small" onClick={() => editFragment(frag)}>Edit</button>
|
||||||
|
<button className="btn-danger-small" onClick={() => deleteFragment(frag.id)}>Delete</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="fragment-content-preview">
|
||||||
|
{frag.content?.substring(0, 200)}{frag.content?.length > 200 ? '...' : ''}
|
||||||
|
</div>
|
||||||
|
<div className="fragment-tags">
|
||||||
|
{frag.tags?.map((tag) => (
|
||||||
|
<span key={tag} className="trait-tag">{tag}</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{frag.linked_characters?.length > 0 && (
|
||||||
|
<div className="fragment-links">
|
||||||
|
Linked characters: {frag.linked_characters.join(', ')}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
54
client/src/pages/Lorebooks.js
Normal file
54
client/src/pages/Lorebooks.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import { Link, useNavigate } from 'react-router-dom';
|
||||||
|
import { lorebooks } from '../services/api';
|
||||||
|
|
||||||
|
export default function Lorebooks() {
|
||||||
|
const [books, setBooks] = useState([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
lorebooks.list()
|
||||||
|
.then((data) => setBooks(data.lorebooks || []))
|
||||||
|
.catch(console.error)
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleDelete = async (id, e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (!window.confirm('Delete this lorebook?')) return;
|
||||||
|
await lorebooks.delete(id);
|
||||||
|
setBooks(books.filter((b) => b.id !== id));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loading) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="page">
|
||||||
|
<div className="page-header">
|
||||||
|
<h1>Lorebooks</h1>
|
||||||
|
<Link to="/lorebooks/new" className="btn-primary">New Lorebook</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{books.length === 0 ? (
|
||||||
|
<div className="empty-page">
|
||||||
|
<p>No lorebooks yet. Create your first lorebook to organize your character's world!</p>
|
||||||
|
<Link to="/lorebooks/new" className="btn-primary">Create Lorebook</Link>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="grid-list">
|
||||||
|
{books.map((b) => (
|
||||||
|
<div key={b.id} className="grid-card" onClick={() => navigate(`/lorebooks/${b.id}`)}>
|
||||||
|
<div className="grid-card-header">
|
||||||
|
<h3>{b.name}</h3>
|
||||||
|
<button className="btn-danger-small" onClick={(e) => handleDelete(b.id, e)}>×</button>
|
||||||
|
</div>
|
||||||
|
<p className="grid-card-desc">{b.description || 'No description'}</p>
|
||||||
|
<span className="grid-card-date">{new Date(b.updated_at).toLocaleDateString()}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
39
client/src/pages/Register.js
Normal file
39
client/src/pages/Register.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { useNavigate, Link } from 'react-router-dom';
|
||||||
|
import { useAuth } from '../context/AuthContext';
|
||||||
|
|
||||||
|
export default function Register() {
|
||||||
|
const [username, setUsername] = useState('');
|
||||||
|
const [email, setEmail] = useState('');
|
||||||
|
const [password, setPassword] = useState('');
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const { register } = useAuth();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const handleSubmit = async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
try {
|
||||||
|
await register(username, email, password);
|
||||||
|
navigate('/');
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="auth-page">
|
||||||
|
<div className="auth-card">
|
||||||
|
<h1>Character Sandbox</h1>
|
||||||
|
<h2>Create Account</h2>
|
||||||
|
{error && <div className="error-message">{error}</div>}
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<input type="text" placeholder="Username" value={username} onChange={(e) => setUsername(e.target.value)} required />
|
||||||
|
<input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} required />
|
||||||
|
<input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} required />
|
||||||
|
<button type="submit" className="btn-primary">Register</button>
|
||||||
|
</form>
|
||||||
|
<p className="auth-link">Already have an account? <Link to="/login">Sign In</Link></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
13
client/src/reportWebVitals.js
Normal file
13
client/src/reportWebVitals.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const reportWebVitals = onPerfEntry => {
|
||||||
|
if (onPerfEntry && onPerfEntry instanceof Function) {
|
||||||
|
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
||||||
|
getCLS(onPerfEntry);
|
||||||
|
getFID(onPerfEntry);
|
||||||
|
getFCP(onPerfEntry);
|
||||||
|
getLCP(onPerfEntry);
|
||||||
|
getTTFB(onPerfEntry);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default reportWebVitals;
|
||||||
74
client/src/services/api.js
Normal file
74
client/src/services/api.js
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:3001/api';
|
||||||
|
|
||||||
|
async function request(endpoint, options = {}) {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
const config = {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
|
...options.headers,
|
||||||
|
},
|
||||||
|
...options,
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch(`${API_URL}${endpoint}`, config);
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(data.error || 'Request failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const auth = {
|
||||||
|
register: (data) => request('/auth/register', { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
login: (data) => request('/auth/login', { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
me: () => request('/auth/me'),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const characters = {
|
||||||
|
list: () => request('/characters'),
|
||||||
|
get: (id) => request(`/characters/${id}`),
|
||||||
|
create: (data) => request('/characters', { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
update: (id, data) => request(`/characters/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||||
|
delete: (id) => request(`/characters/${id}`, { method: 'DELETE' }),
|
||||||
|
getNeeds: (id) => request(`/characters/${id}/needs`),
|
||||||
|
createNeed: (id, data) => request(`/characters/${id}/needs`, { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
updateNeed: (charId, needId, data) => request(`/characters/${charId}/needs/${needId}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||||
|
deleteNeed: (charId, needId) => request(`/characters/${charId}/needs/${needId}`, { method: 'DELETE' }),
|
||||||
|
getUIElements: (id) => request(`/characters/${id}/ui-elements`),
|
||||||
|
createUIElement: (id, data) => request(`/characters/${id}/ui-elements`, { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
updateUIElement: (charId, elemId, data) => request(`/characters/${charId}/ui-elements/${elemId}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||||
|
deleteUIElement: (charId, elemId) => request(`/characters/${charId}/ui-elements/${elemId}`, { method: 'DELETE' }),
|
||||||
|
getBrainRules: (id) => request(`/characters/${id}/brain-rules`),
|
||||||
|
createBrainRule: (id, data) => request(`/characters/${id}/brain-rules`, { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
updateBrainRule: (charId, ruleId, data) => request(`/characters/${charId}/brain-rules/${ruleId}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||||
|
deleteBrainRule: (charId, ruleId) => request(`/characters/${charId}/brain-rules/${ruleId}`, { method: 'DELETE' }),
|
||||||
|
simulate: (id, data) => request(`/characters/${id}/simulate`, { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const lorebooks = {
|
||||||
|
list: () => request('/lorebooks'),
|
||||||
|
get: (id) => request(`/lorebooks/${id}`),
|
||||||
|
create: (data) => request('/lorebooks', { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
update: (id, data) => request(`/lorebooks/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||||
|
delete: (id) => request(`/lorebooks/${id}`, { method: 'DELETE' }),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const fragments = {
|
||||||
|
search: (params) => request(`/fragments/search?${new URLSearchParams(params)}`),
|
||||||
|
get: (id) => request(`/fragments/${id}`),
|
||||||
|
create: (lorebookId, data) => request(`/fragments/lorebook/${lorebookId}`, { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
update: (id, data) => request(`/fragments/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||||
|
delete: (id) => request(`/fragments/${id}`, { method: 'DELETE' }),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ai = {
|
||||||
|
suggest: (prompt) => request('/ai/suggest', { method: 'POST', body: JSON.stringify({ prompt }) }),
|
||||||
|
generateName: (style) => request('/ai/generate-name', { method: 'POST', body: JSON.stringify({ style }) }),
|
||||||
|
train: (data) => request('/ai/train', { method: 'POST', body: JSON.stringify(data) }),
|
||||||
|
getTrainingData: () => request('/ai/training-data'),
|
||||||
|
deleteTrainingData: (id) => request(`/ai/training-data/${id}`, { method: 'DELETE' }),
|
||||||
|
getKnowledge: () => request('/ai/knowledge'),
|
||||||
|
};
|
||||||
5
client/src/setupTests.js
Normal file
5
client/src/setupTests.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||||
|
// allows you to do things like:
|
||||||
|
// expect(element).toHaveTextContent(/react/i)
|
||||||
|
// learn more: https://github.com/testing-library/jest-dom
|
||||||
|
import '@testing-library/jest-dom';
|
||||||
Loading…
x
Reference in New Issue
Block a user