From f685118da0f5c86048c1d0c9502de6915ca1b04a Mon Sep 17 00:00:00 2001 From: Pabloader Date: Tue, 7 Apr 2026 19:41:22 +0000 Subject: [PATCH] Add usernames to chat messages --- .../storywriter/components/chat-sidebar.tsx | 53 +++++++++++-------- src/games/storywriter/contexts/state.tsx | 2 +- src/games/storywriter/utils/llm.ts | 1 + src/games/storywriter/utils/prompt.ts | 31 ++++++++++- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/games/storywriter/components/chat-sidebar.tsx b/src/games/storywriter/components/chat-sidebar.tsx index 553fe95..3004d44 100644 --- a/src/games/storywriter/components/chat-sidebar.tsx +++ b/src/games/storywriter/components/chat-sidebar.tsx @@ -167,6 +167,8 @@ export const ChatPanel = () => { } try { + const charName = currentWorld.title ?? 'Assistant'; + const prefix = `${charName}: `; let accumulatedContent = ''; let accumulatedReasoning = ''; let tool_calls: LLM.ToolCall[] | undefined; @@ -184,6 +186,9 @@ export const ChatPanel = () => { const content = delta?.content; if (content) { accumulatedContent += content; + if (currentWorld?.chatOnly && accumulatedContent.startsWith(prefix)) { + accumulatedContent = accumulatedContent.slice(prefix.length); + } } const reasoningContent = delta?.reasoning_content; if (reasoningContent) { @@ -496,30 +501,32 @@ export const ChatPanel = () => { )} {currentStory && (
-
- -
- {tokenCount && {tokenCount.taken} / {tokenCount.total} tokens} - + {!currentWorld?.chatOnly && ( +
+ +
+ {tokenCount && {tokenCount.taken} / {tokenCount.total} tokens} + +
-
+ )} = [], excludedMessageIds: Iterable = [], ): LLM.ChatCompletionRequest | null { - const { currentStory, model, enableThinking } = state; + const { currentStory, model, enableThinking, currentWorld } = state; if (!currentStory || !model) { return null; @@ -345,6 +345,33 @@ namespace Prompt { } } + // Chat-only world: format messages with name prefixes + if (currentWorld?.chatOnly) { + const charName = currentWorld.title ?? 'Assistant'; + const formattedMessages: ChatMessage[] = messages.map(msg => { + const prefix = msg.role === 'user' ? 'User' : charName; + return { + ...msg, + content: `${prefix}: ${msg.content}`, + }; + }); + + // Prepend system message + formattedMessages.unshift({ + id: crypto.randomUUID(), + role: 'system', + content: formatSystemPrompt(state, 0), + }); + + return { + model: model.id, + messages: formattedMessages, + enable_thinking: false, + max_tokens: model.max_length ? model.max_length : 2048, + add_generation_prompt: true, + banned_tokens: state.bannedTokens, + }; + } // Estimate token budget for story text let storyTokenBudget = 0; @@ -365,7 +392,7 @@ namespace Prompt { return { model: model.id, messages, - tools: state.currentWorld?.chatOnly ? undefined : Tools.getTools(), + tools: Tools.getTools(), banned_tokens: state.bannedTokens, enable_thinking: enableThinking, max_tokens: model.max_length ? model.max_length : 2048,