Fixes
This commit is contained in:
@@ -2,7 +2,7 @@ services:
|
|||||||
webui:
|
webui:
|
||||||
image: minimax
|
image: minimax
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "4000:3000"
|
||||||
volumes:
|
volumes:
|
||||||
- ./data:/app/data
|
- ./data:/app/data
|
||||||
env_file:
|
env_file:
|
||||||
|
|||||||
@@ -80,7 +80,6 @@ pub struct RhaiAgent<R: Rng + 'static> {
|
|||||||
|
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
script: AST,
|
script: AST,
|
||||||
scope: Scope<'static>,
|
|
||||||
print_callback: Arc<dyn Fn(&str) + 'static>,
|
print_callback: Arc<dyn Fn(&str) + 'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +117,6 @@ impl<R: Rng + 'static> RhaiAgent<R> {
|
|||||||
|
|
||||||
// Do not use FULL, rand_* functions are not pure
|
// Do not use FULL, rand_* functions are not pure
|
||||||
engine.set_optimization_level(OptimizationLevel::Simple);
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
||||||
|
|
||||||
engine.disable_symbol("eval");
|
engine.disable_symbol("eval");
|
||||||
engine.set_max_expr_depths(100, 100);
|
engine.set_max_expr_depths(100, 100);
|
||||||
engine.set_max_strings_interned(1024);
|
engine.set_max_strings_interned(1024);
|
||||||
@@ -201,13 +199,11 @@ impl<R: Rng + 'static> RhaiAgent<R> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let script = engine.compile(script)?;
|
let script = engine.compile(script)?;
|
||||||
let scope = Scope::new(); // Not used
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
rng,
|
rng,
|
||||||
engine,
|
engine,
|
||||||
script,
|
script,
|
||||||
scope,
|
|
||||||
print_callback,
|
print_callback,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -227,7 +223,7 @@ impl<R: Rng + 'static> Agent for RhaiAgent<R> {
|
|||||||
fn step_min(&mut self, board: &Board) -> Result<PlayerAction, Self::ErrorType> {
|
fn step_min(&mut self, board: &Board) -> Result<PlayerAction, Self::ErrorType> {
|
||||||
let res = self.engine.call_fn_with_options::<PlayerAction>(
|
let res = self.engine.call_fn_with_options::<PlayerAction>(
|
||||||
CallFnOptions::new().eval_ast(false),
|
CallFnOptions::new().eval_ast(false),
|
||||||
&mut self.scope,
|
&mut Scope::new(),
|
||||||
&self.script,
|
&self.script,
|
||||||
"step_min",
|
"step_min",
|
||||||
(board.clone(),),
|
(board.clone(),),
|
||||||
@@ -242,7 +238,7 @@ impl<R: Rng + 'static> Agent for RhaiAgent<R> {
|
|||||||
fn step_max(&mut self, board: &Board) -> Result<PlayerAction, Self::ErrorType> {
|
fn step_max(&mut self, board: &Board) -> Result<PlayerAction, Self::ErrorType> {
|
||||||
let res = self.engine.call_fn_with_options::<PlayerAction>(
|
let res = self.engine.call_fn_with_options::<PlayerAction>(
|
||||||
CallFnOptions::new().eval_ast(false),
|
CallFnOptions::new().eval_ast(false),
|
||||||
&mut self.scope,
|
&mut Scope::new(),
|
||||||
&self.script,
|
&self.script,
|
||||||
"step_max",
|
"step_max",
|
||||||
(board.clone(),),
|
(board.clone(),),
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import { NextRequest, NextResponse } from "next/server";
|
|||||||
import { readFile } from "fs/promises";
|
import { readFile } from "fs/promises";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { existsSync } from "fs";
|
import { existsSync } from "fs";
|
||||||
import { SAVE_CONFIG } from "@/lib/saveConfig";
|
import { CONFIG } from "@/lib/config";
|
||||||
|
|
||||||
|
// Force dynamic rendering for this API route
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = request.nextUrl;
|
||||||
const name = searchParams.get("name");
|
const name = searchParams.get("name");
|
||||||
|
|
||||||
if (!name) {
|
if (!name) {
|
||||||
@@ -17,18 +20,17 @@ export async function GET(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate filename (same validation as save)
|
// Validate filename (same validation as save)
|
||||||
if (!SAVE_CONFIG.FILENAME_REGEX.test(name)) {
|
if (!CONFIG.FILENAME_REGEX.test(name)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Invalid script name" },
|
{ error: "Invalid script name" },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveDir = SAVE_CONFIG.SAVE_DIRECTORY;
|
const saveDir = CONFIG.SAVE_DIRECTORY;
|
||||||
const filename = `${name}.rhai`;
|
const filename = `${name}.rhai`;
|
||||||
const filepath = join(saveDir, filename);
|
const filepath = join(saveDir, filename);
|
||||||
|
|
||||||
// Check if file exists
|
|
||||||
if (!existsSync(filepath)) {
|
if (!existsSync(filepath)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: `Script "${name}" not found` },
|
{ error: `Script "${name}" not found` },
|
||||||
@@ -36,8 +38,7 @@ export async function GET(request: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and return file content
|
let content = await readFile(filepath, "utf8");
|
||||||
const content = await readFile(filepath, "utf8");
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
name,
|
name,
|
||||||
|
|||||||
@@ -1,31 +1,40 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { readdir } from "fs/promises";
|
import { readdir } from "fs/promises";
|
||||||
import { join } from "path";
|
|
||||||
import { existsSync } from "fs";
|
import { existsSync } from "fs";
|
||||||
import { SAVE_CONFIG } from "@/lib/saveConfig";
|
import { CONFIG } from "@/lib/config";
|
||||||
|
|
||||||
export async function GET() {
|
// Force dynamic rendering for this API route
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
"Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate",
|
||||||
|
Pragma: "no-cache",
|
||||||
|
Expires: "0",
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function GET(_request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const saveDir = SAVE_CONFIG.SAVE_DIRECTORY;
|
const saveDir = CONFIG.SAVE_DIRECTORY;
|
||||||
|
|
||||||
// If save directory doesn't exist, return empty array
|
// If save directory doesn't exist, return empty array
|
||||||
if (!existsSync(saveDir)) {
|
if (!existsSync(saveDir)) {
|
||||||
return NextResponse.json({ scripts: [] });
|
return NextResponse.json({ scripts: [] }, { headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read directory and filter for .rhai files
|
// Read directory and filter for .rhai files
|
||||||
const files = await readdir(saveDir);
|
const files = await readdir(saveDir);
|
||||||
|
|
||||||
const scripts = files
|
const scripts = files
|
||||||
.filter((file) => file.endsWith(".rhai"))
|
.filter((file) => file.endsWith(".rhai"))
|
||||||
.map((file) => file.replace(".rhai", ""))
|
.map((file) => file.replace(".rhai", ""))
|
||||||
.sort(); // Sort alphabetically
|
.sort(); // Sort alphabetically
|
||||||
|
|
||||||
return NextResponse.json({ scripts });
|
return NextResponse.json({ scripts }, { headers });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("List scripts error:", error);
|
console.error("List scripts error:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Failed to list scripts" },
|
{ error: "Failed to list scripts" },
|
||||||
{ status: 500 }
|
{ status: 500, headers }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ import { NextRequest, NextResponse } from "next/server";
|
|||||||
import { writeFile, mkdir } from "fs/promises";
|
import { writeFile, mkdir } from "fs/promises";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { existsSync } from "fs";
|
import { existsSync } from "fs";
|
||||||
import { SAVE_CONFIG } from "@/lib/saveConfig";
|
import { CONFIG } from "@/lib/config";
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
// Check if saving is enabled
|
// Check if saving is enabled
|
||||||
if (!SAVE_CONFIG.ENABLE_SAVE) {
|
if (!CONFIG.ENABLE_SAVE) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Script saving is disabled" },
|
{ error: "Script saving is disabled" },
|
||||||
{ status: 403 }
|
{ status: 403 }
|
||||||
@@ -16,15 +16,13 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
const { name, content, secret } = await request.json();
|
const { name, content, secret } = await request.json();
|
||||||
|
|
||||||
// Validate secret
|
if (secret !== CONFIG.SAVE_SECRET) {
|
||||||
if (secret !== SAVE_CONFIG.SAVE_SECRET) {
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Invalid save secret" },
|
{ error: "Invalid secret" },
|
||||||
{ status: 401 }
|
{ status: 401 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate required fields
|
|
||||||
if (!name || !content) {
|
if (!name || !content) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Name and content are required" },
|
{ error: "Name and content are required" },
|
||||||
@@ -32,17 +30,25 @@ export async function POST(request: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate filename
|
if (name.length > CONFIG.MAX_FILENAME_LENGTH) {
|
||||||
if (name.length > SAVE_CONFIG.MAX_FILENAME_LENGTH) {
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: `Filename must be ${SAVE_CONFIG.MAX_FILENAME_LENGTH} characters or less`,
|
error: `Filename must be ${CONFIG.MAX_FILENAME_LENGTH} characters or less`,
|
||||||
},
|
},
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SAVE_CONFIG.FILENAME_REGEX.test(name)) {
|
if (content.length > CONFIG.MAX_FILE_SIZE) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: `File is too large`,
|
||||||
|
},
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CONFIG.FILENAME_REGEX.test(name)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: "Filename can only contain alphanumerics, underscores, spaces, and hyphens",
|
error: "Filename can only contain alphanumerics, underscores, spaces, and hyphens",
|
||||||
@@ -52,7 +58,7 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure save directory exists
|
// Ensure save directory exists
|
||||||
const saveDir = SAVE_CONFIG.SAVE_DIRECTORY;
|
const saveDir = CONFIG.SAVE_DIRECTORY;
|
||||||
if (!existsSync(saveDir)) {
|
if (!existsSync(saveDir)) {
|
||||||
await mkdir(saveDir, { recursive: true });
|
await mkdir(saveDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,7 +128,8 @@ export const Editor = forwardRef<any, EditorProps>(function Editor(
|
|||||||
editorRef.current = null;
|
editorRef.current = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, []); // DO NOT FILL ARRAY
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []); // DO NOT FILL ARRAY - intentionally empty to prevent re-initialization
|
||||||
|
|
||||||
// Update font size when it changes
|
// Update font size when it changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -42,36 +42,32 @@ fn step_max(board) {
|
|||||||
const AGENTS = {
|
const AGENTS = {
|
||||||
// special-cased below
|
// special-cased below
|
||||||
Self: undefined,
|
Self: undefined,
|
||||||
|
|
||||||
Random: `fn random_action(board) {
|
|
||||||
let symb = rand_symb();
|
|
||||||
let pos = rand_int(0, 10);
|
|
||||||
let action = Action(symb, pos);
|
|
||||||
|
|
||||||
while !board.can_play(action) {
|
|
||||||
let symb = rand_symb();
|
|
||||||
let pos = rand_int(0, 10);
|
|
||||||
action = Action(symb, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
return action;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn step_min(board) {
|
|
||||||
return random_action(board);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn step_max(board) {
|
|
||||||
return random_action(board);
|
|
||||||
}`,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function Playground() {
|
export default function Playground() {
|
||||||
const [isScriptRunning, setIsScriptRunning] = useState(false);
|
const [isScriptRunning, setIsScriptRunning] = useState(false);
|
||||||
const [isEditorReady, setIsEditorReady] = useState(false);
|
const [isEditorReady, setIsEditorReady] = useState(false);
|
||||||
const [fontSize, setFontSize] = useState(14);
|
const [fontSize, setFontSize] = useState(() => {
|
||||||
const [bulkRounds, setBulkRounds] = useState(1000);
|
if (typeof window !== "undefined") {
|
||||||
const [selectedAgent, setSelectedAgent] = useState("Random");
|
const saved = localStorage.getItem("playground-fontSize");
|
||||||
|
return saved ? parseInt(saved, 10) : 14;
|
||||||
|
}
|
||||||
|
return 14;
|
||||||
|
});
|
||||||
|
const [bulkRounds, setBulkRounds] = useState(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
const saved = localStorage.getItem("playground-bulkRounds");
|
||||||
|
return saved ? parseInt(saved, 10) : 1000;
|
||||||
|
}
|
||||||
|
return 1000;
|
||||||
|
});
|
||||||
|
const [selectedAgent, setSelectedAgent] = useState(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
const saved = localStorage.getItem("playground-selectedAgent");
|
||||||
|
return saved || "Self";
|
||||||
|
}
|
||||||
|
return "Self";
|
||||||
|
});
|
||||||
const [isHelpOpen, setIsHelpOpen] = useState(false);
|
const [isHelpOpen, setIsHelpOpen] = useState(false);
|
||||||
const [scriptName, setScriptName] = useState("");
|
const [scriptName, setScriptName] = useState("");
|
||||||
const [saveSecret, setSaveSecret] = useState("");
|
const [saveSecret, setSaveSecret] = useState("");
|
||||||
@@ -126,10 +122,7 @@ export default function Playground() {
|
|||||||
// Combine hardcoded agents with saved scripts, ensuring Self and Random are first
|
// Combine hardcoded agents with saved scripts, ensuring Self and Random are first
|
||||||
const combinedAgents = [
|
const combinedAgents = [
|
||||||
"Self",
|
"Self",
|
||||||
"Random",
|
...Object.keys(AGENTS).filter((key) => key !== "Self"),
|
||||||
...Object.keys(AGENTS).filter(
|
|
||||||
(key) => key !== "Self" && key !== "Random"
|
|
||||||
),
|
|
||||||
...scripts,
|
...scripts,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -147,6 +140,30 @@ export default function Playground() {
|
|||||||
loadSavedScripts();
|
loadSavedScripts();
|
||||||
}, [loadSavedScripts]);
|
}, [loadSavedScripts]);
|
||||||
|
|
||||||
|
// Save font size to localStorage
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
localStorage.setItem("playground-fontSize", fontSize.toString());
|
||||||
|
}
|
||||||
|
}, [fontSize]);
|
||||||
|
|
||||||
|
// Save bulk rounds to localStorage
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
localStorage.setItem(
|
||||||
|
"playground-bulkRounds",
|
||||||
|
bulkRounds.toString()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, [bulkRounds]);
|
||||||
|
|
||||||
|
// Save selected agent to localStorage
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
localStorage.setItem("playground-selectedAgent", selectedAgent);
|
||||||
|
}
|
||||||
|
}, [selectedAgent]);
|
||||||
|
|
||||||
const runHuman = useCallback(async () => {
|
const runHuman = useCallback(async () => {
|
||||||
if (resultRef.current) {
|
if (resultRef.current) {
|
||||||
resultRef.current.value = "";
|
resultRef.current.value = "";
|
||||||
@@ -265,7 +282,7 @@ export default function Playground() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!saveSecret.trim()) {
|
if (!saveSecret.trim()) {
|
||||||
alert("Please enter the save secret");
|
alert("Please enter a secret");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,6 +340,11 @@ export default function Playground() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scriptContent.trim().startsWith("// SECRET")) {
|
||||||
|
alert("This script is hidden :)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Warn user about losing current content
|
// Warn user about losing current content
|
||||||
const confirmed = confirm(
|
const confirmed = confirm(
|
||||||
`This will replace your current script with "${selectedAgent}". Your current work will be lost. Continue?`
|
`This will replace your current script with "${selectedAgent}". Your current work will be lost. Continue?`
|
||||||
@@ -433,11 +455,11 @@ export default function Playground() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={styles.configField}>
|
<div className={styles.configField}>
|
||||||
<label>Save secret</label>
|
<label>Secret</label>
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
className={styles.saveInput}
|
className={styles.saveInput}
|
||||||
placeholder="Enter save secret..."
|
placeholder="Enter secret..."
|
||||||
value={saveSecret}
|
value={saveSecret}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
setSaveSecret(
|
setSaveSecret(
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
export const SAVE_CONFIG = {
|
export const CONFIG = {
|
||||||
ENABLE_SAVE: process.env.ENABLE_SAVE === 'true' || true,
|
ENABLE_SAVE: process.env.ENABLE_SAVE === "true" || true,
|
||||||
SAVE_SECRET: process.env.SAVE_SECRET || "save",
|
SAVE_SECRET: process.env.SAVE_SECRET || "save",
|
||||||
|
|
||||||
SAVE_DIRECTORY: process.env.SAVE_DIRECTORY || "./data/scripts",
|
SAVE_DIRECTORY: process.env.SAVE_DIRECTORY || "./data/scripts",
|
||||||
MAX_FILENAME_LENGTH: parseInt(process.env.MAX_FILENAME_LENGTH || "32"),
|
MAX_FILENAME_LENGTH: parseInt(process.env.MAX_FILENAME_LENGTH || "32"),
|
||||||
|
MAX_FILE_SIZE: parseInt(process.env.MAX_FILE_SIZE || "1048576"),
|
||||||
FILENAME_REGEX: /^[a-zA-Z0-9_\s-]+$/,
|
FILENAME_REGEX: /^[a-zA-Z0-9_\s-]+$/,
|
||||||
} as const;
|
} as const;
|
||||||
@@ -24,6 +24,7 @@ self.onmessage = async (event) => {
|
|||||||
|
|
||||||
if (type === "data") {
|
if (type === "data") {
|
||||||
if (currentGame !== null) {
|
if (currentGame !== null) {
|
||||||
|
try {
|
||||||
currentGame.take_input(event_data.data);
|
currentGame.take_input(event_data.data);
|
||||||
|
|
||||||
if (currentGame.is_error()) {
|
if (currentGame.is_error()) {
|
||||||
@@ -33,6 +34,9 @@ self.onmessage = async (event) => {
|
|||||||
currentGame = null;
|
currentGame = null;
|
||||||
self.postMessage({ type: "complete" });
|
self.postMessage({ type: "complete" });
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
self.postMessage({ type: "error", error: String(error) });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (type === "init") {
|
} else if (type === "init") {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user