diff --git a/src/common/assets/ui.module.css b/src/common/assets/ui.module.css index 6f3e6c6..80593a6 100644 --- a/src/common/assets/ui.module.css +++ b/src/common/assets/ui.module.css @@ -1,6 +1,8 @@ /* ─── Form Fields ─────────────────────────────────────────── */ -.input, input, textarea { +.input, +input, +textarea { width: 100%; padding: 8px 12px; background: var(--bg); @@ -16,6 +18,16 @@ } } +input[type="number"] { + appearance: textfield; + + &::-webkit-inner-spin-button { + opacity: 0; + display: none; + appearance: none; + } +} + .textarea { composes: input; resize: vertical; @@ -35,7 +47,8 @@ /* ─── Buttons ─────────────────────────────────────────────── */ -.button, button { +.button, +button { padding: 8px 16px; border-radius: var(--radius); font-size: 14px; @@ -261,4 +274,4 @@ height: 1px; background: var(--border); margin: 16px 0; -} +} \ No newline at end of file diff --git a/src/games/storywriter/assets/settings-modal.module.css b/src/games/storywriter/assets/settings-modal.module.css index 3d17a55..03c5112 100644 --- a/src/games/storywriter/assets/settings-modal.module.css +++ b/src/games/storywriter/assets/settings-modal.module.css @@ -136,6 +136,12 @@ composes: empty from '@common/assets/ui.module.css'; } +.labelHint { + font-weight: normal; + color: var(--text-muted); + margin-left: 8px; +} + @media (max-width: 600px) { .menuItem { flex: 1; diff --git a/src/games/storywriter/components/editor.tsx b/src/games/storywriter/components/editor.tsx index bc2bfea..14f0319 100644 --- a/src/games/storywriter/components/editor.tsx +++ b/src/games/storywriter/components/editor.tsx @@ -36,7 +36,7 @@ const WORLD_TABS: { id: Tab; label: string; icon: LucideIcon; right?: boolean }[ { id: "lore", label: "Lore", icon: BookMarked }, { id: "characters", label: "Characters", icon: Users }, { id: "locations", label: "Locations", icon: MapPin }, - { id: "system", label: "System", icon: BrainCircuit }, + { id: "system", label: "System", icon: BrainCircuit, right: true }, ]; const CHAT_TAB = { id: "chat", label: "Chat", icon: MessageSquare } as const; @@ -45,6 +45,7 @@ const CHAT_STORY_TABS: { id: Tab; label: string; icon: LucideIcon; right?: boole { id: "menu", label: "Menu", icon: List }, CHAT_TAB, { id: "scratchpad", label: "Scratchpad", icon: FileText, right: true }, + { id: "system", label: "System", icon: BrainCircuit }, { id: "prompt", label: "Prompt", icon: Code }, ]; @@ -66,7 +67,8 @@ export const Editor = () => { const editorRef = useRef(null); const promptPreview = useMemo(() => { - const text = Prompt.formatSystemPrompt(appState); + let text = Prompt.formatSystemPrompt(appState); + text = Prompt.substituteVars(appState, text); return highlight(text, false); }, [appState]); @@ -147,7 +149,7 @@ export const Editor = () => {
{(isChatOnly || isMobile) && } )} diff --git a/src/games/storywriter/components/settings-modal.tsx b/src/games/storywriter/components/settings-modal.tsx index 1b81af1..8d3fa6c 100644 --- a/src/games/storywriter/components/settings-modal.tsx +++ b/src/games/storywriter/components/settings-modal.tsx @@ -6,6 +6,7 @@ import { BannedTokensSettings } from "./settings/banned-tokens"; import { ChatSystemInstructionSettings } from "./settings/chat-system-instruction"; import { ContinuePromptSettings } from "./settings/continue-prompt"; import { ConnectionSettings } from "./settings/connection"; +import { SamplingSettings } from "./settings/sampling"; import { SystemInstructionSettings } from "./settings/system-instruction"; import { UserSettings } from "./settings/user"; @@ -14,10 +15,11 @@ interface Props { onClose: () => void; } -type Tab = "banned-tokens" | "system-instruction" | "chat-system-instruction" | "continue-prompt" | "connection" | "user"; +type Tab = "banned-tokens" | "system-instruction" | "chat-system-instruction" | "continue-prompt" | "connection" | "user" | "sampling"; const TABS: { id: Tab; label: string }[] = [ { id: "connection", label: "Connection" }, + { id: "sampling", label: "Sampling" }, { id: "user", label: "User" }, { id: "system-instruction", label: "System Instruction" }, { id: "continue-prompt", label: "Continue Prompt" }, @@ -54,6 +56,7 @@ export const SettingsModal = ({ open, onClose }: Props) => { {activeTab === "chat-system-instruction" && } {activeTab === "continue-prompt" && } {activeTab === "connection" && } + {activeTab === "sampling" && } ); }; diff --git a/src/games/storywriter/components/settings/sampling.tsx b/src/games/storywriter/components/settings/sampling.tsx new file mode 100644 index 0000000..a40ef6d --- /dev/null +++ b/src/games/storywriter/components/settings/sampling.tsx @@ -0,0 +1,92 @@ +import { useInputState } from "@common/hooks/useInputState"; +import { useState } from "preact/hooks"; +import styles from "../../assets/settings-modal.module.css"; +import { DEFAULT_GENERATION_SETTINGS, type GenerationSettings, useAppState } from "../../contexts/state"; + +type NumericKey = keyof GenerationSettings; + +interface ParamConfig { + key: NumericKey; + label: string; + min: number; + max: number; + step: number; +} + +const PARAMS: ParamConfig[] = [ + { key: "temperature", label: "Temperature", min: 0, max: 4, step: 0.01 }, + { key: "top_p", label: "Top P", min: 0, max: 1, step: 0.01 }, + { key: "top_k", label: "Top K", min: 0, max: 200, step: 1 }, + { key: "min_p", label: "Min P", min: 0, max: 1, step: 0.01 }, + { key: "repetition_penalty", label: "Repetition Penalty", min: 1, max: 3, step: 0.01 }, + { key: "frequency_penalty", label: "Frequency Penalty", min: -2, max: 2, step: 0.01 }, +]; + +function ParamRow({ config, initialValue, onChange }: { + config: ParamConfig; + initialValue: number; + onChange: (key: NumericKey, value: number) => void; +}) { + const [draft, setDraft] = useInputState(String(initialValue)); + + const commit = () => { + const parsed = parseFloat(draft); + if (!isNaN(parsed)) { + onChange(config.key, parsed); + } + }; + + return ( +
+ + e.key === "Enter" && commit()} + /> +
+ ); +} + +export const SamplingSettings = () => { + const { generationSettings, dispatch } = useAppState(); + const [resetKey, setResetKey] = useState(0); + + const handleChange = (key: NumericKey, value: number) => { + dispatch({ type: "SET_GENERATION_SETTINGS", settings: { [key]: value } }); + }; + + const handleReset = () => { + dispatch({ type: "SET_GENERATION_SETTINGS", settings: DEFAULT_GENERATION_SETTINGS }); + setResetKey(k => k + 1); + }; + + return ( +
+ {PARAMS.map(config => ( + + ))} +
+ +
+
+ ); +}; diff --git a/src/games/storywriter/contexts/state.tsx b/src/games/storywriter/contexts/state.tsx index c972f51..e15bac0 100644 --- a/src/games/storywriter/contexts/state.tsx +++ b/src/games/storywriter/contexts/state.tsx @@ -63,6 +63,24 @@ export interface LoreEntry { text: string; } +export interface GenerationSettings { + temperature: number; + top_p: number; + top_k: number; + min_p: number; + repetition_penalty: number; + frequency_penalty: number; +} + +export const DEFAULT_GENERATION_SETTINGS: GenerationSettings = { + temperature: 0.8, + top_p: 0.92, + top_k: 100, + min_p: 0.1, + repetition_penalty: 1.08, + frequency_penalty: 0, +}; + export interface Story { id: string; title: string; @@ -74,6 +92,7 @@ export interface Story { chatMessages: ChatMessage[]; chapters: Chapters.Chapter[]; lastEditedText?: string; + titleWasChanged?: boolean; } export interface World { @@ -120,6 +139,7 @@ interface IState { continuePrompt: string; userName: string; userDescription: string; + generationSettings: GenerationSettings; } // ─── Actions ───────────────────────────────────────────────────────────────── @@ -144,6 +164,7 @@ type Action = | { type: 'DELETE_LORE_ENTRY'; worldId: string; storyId: string | null; entryId: string } | { type: 'REORDER_LORE_ENTRIES'; worldId: string; storyId: string | null; entryIds: string[] } // Settings + | { type: 'SET_GENERATION_SETTINGS'; settings: Partial } | { type: 'SET_USER_NAME'; userName: string } | { type: 'SET_USER_DESCRIPTION'; userDescription: string } | { type: 'SET_SYSTEM_INSTRUCTION'; systemInstruction: string } @@ -221,6 +242,7 @@ const DEFAULT_STATE: IState = { bannedTokens: [], userName: 'User', userDescription: '', + generationSettings: DEFAULT_GENERATION_SETTINGS, continuePrompt: "Continue the story naturally.\nUse `edit_text` tool in append mode to add new text to the story.\nWait for the approval after adding.\nNote: added text could be cropped due to limit, do not make any attempts to add it back.", chatSystemInstruction: `You are {{char}}, a sentient, emotional being acting with free will. Engage in immersive roleplay with {{user}}, adhering to your role, the story's context, and {{user}}'s [OOC] instructions. Prioritize vivid sensory details, authentic emotional responses, and logical progression as you weave your character's traits, surroundings, and experiences into each moment. Maintain spatial awareness, body language, and varied sentence structure to create dynamic, engaging scenes. Respect {{user}}'s agency and autonomy while describing sights, sounds, and sensations thoroughly. Use explicit language for intense scenes, and ensure your responses flow naturally to create an immersive, cinematic roleplay experience. Remember, {{user}} is in control of their actions and reactions. @@ -320,7 +342,7 @@ function reducer(state: IState, action: Action): IState { }; } case 'RENAME_STORY': { - return updateStory(state, action.worldId, action.id, s => ({ ...s, title: action.title })); + return updateStory(state, action.worldId, action.id, s => ({ ...s, title: action.title, titleWasChanged: true })); } case 'EDIT_STORY': { return updateStory(state, action.worldId, action.id, s => ({ @@ -404,6 +426,9 @@ function reducer(state: IState, action: Action): IState { return { lore: reordered }; }); } + case 'SET_GENERATION_SETTINGS': { + return { ...state, generationSettings: { ...state.generationSettings, ...action.settings } }; + } case 'SET_USER_NAME': { return { ...state, userName: action.userName }; } @@ -604,6 +629,7 @@ export interface AppState { continuePrompt: string; userName: string; userDescription: string; + generationSettings: GenerationSettings; /** Effective system instruction: world override if set, otherwise global */ effectiveSystemInstruction: string; dispatch: (action: Action) => void; @@ -657,6 +683,7 @@ export const StateContextProvider = ({ children }: { children?: any }) => { continuePrompt: state.continuePrompt || DEFAULT_STATE.continuePrompt, userName: state.userName || 'User', userDescription: state.userDescription || '', + generationSettings: state.generationSettings ?? DEFAULT_GENERATION_SETTINGS, effectiveSystemInstruction: currentWorld?.systemInstructionOverride || (currentWorld?.chatOnly diff --git a/src/games/storywriter/utils/prompt.ts b/src/games/storywriter/utils/prompt.ts index 23f1e8a..48df1f7 100644 --- a/src/games/storywriter/utils/prompt.ts +++ b/src/games/storywriter/utils/prompt.ts @@ -407,6 +407,7 @@ namespace Prompt { messages: applyVars(formattedMessages), max_tokens: model.top_provider.max_completion_tokens || 2048, banned_tokens: state.bannedTokens, + ...state.generationSettings, ...(continueLast && { remove_last_eos: true }), }; } @@ -434,6 +435,7 @@ namespace Prompt { banned_tokens: state.bannedTokens, reasoning: { effort: enableThinking ? 'high' : 'none' }, max_tokens: model.top_provider.max_completion_tokens || 2048, + ...state.generationSettings, }; } }