1
0
Fork 0

Compare commits

..

4 Commits

Author SHA1 Message Date
Pabloader 9803512c0b Fix prompt message duplication issue. 2026-03-27 15:41:54 +00:00
Pabloader 77e3983d5e Highlight separators as span 2026-03-27 15:40:56 +00:00
Pabloader df5077e107 Highlight separators 2026-03-27 15:36:36 +00:00
Pabloader 3db25c5af3 Some prompt tweaks 2026-03-26 19:21:57 +00:00
6 changed files with 31 additions and 14 deletions

View File

@ -78,6 +78,8 @@
.hr {
border: none;
border-top: 1px solid var(--hrColor, #555);
border-bottom: 1px solid var(--hrColor, #555);
margin: 0.5em 0;
display: block;
text-align: center;
}

View File

@ -41,7 +41,7 @@ export const parseTable = (table: string): string => {
export const highlight = (message: string, keepMarkup = true): string => {
let resultHTML = '';
const tokenRegex = /(\*\*?|"|```|`|(?:^|\n)#{1,3} |(?:^|\n)> |\n)/g;
const tokenRegex = /(\*{1,3}|"|```|`|(?:^|\n)#{1,3} |(?:^|\n)> |\n)|---/g;
const headerRegex = /#{1,3} $/;
const blockquoteRegex = /> $/;
const stack: string[] = [];
@ -116,7 +116,9 @@ export const highlight = (message: string, keepMarkup = true): string => {
continue;
}
if (isClose) {
if (token === '***' || token === '---') {
resultHTML += `<span class="${styles.hr}">${keepToken ? token : ''}</span>`;
} else if (isClose) {
stack.pop();
resultHTML += `${keepToken ? token : ''}</span>`;
} else if (token === '*') {
@ -147,7 +149,6 @@ export const highlight = (message: string, keepMarkup = true): string => {
if (!keepMarkup) {
resultHTML = resultHTML.replace(/((?:(?:^|\n)\|.+)+)/g, match => parseTable(match));
resultHTML = resultHTML.replace(/(^|\n)---(\n|$)/g, (_, pre, post) => `${pre}<hr class="${styles.hr}">${post}`);
resultHTML = resultHTML.replace(/((?:(?:^|\n)[-+] .+)+)/g, match => parseList(match, false));
resultHTML = resultHTML.replace(/((?:(?:^|\n)\d+\. .+)+)/g, match => parseList(match, true));
}

View File

@ -12,7 +12,7 @@ import { Sparkles, ChevronsRight } from "lucide-preact";
import clsx from "clsx";
import { ContentEditable } from "@common/components/ContentEditable";
const CONTINUE_PROMPT = "Continue the story forward.\nUse `edit_text` tool in append mode to add new text to the story.";
const CONTINUE_PROMPT = "Continue the story forward.\nUse `edit_text` tool in append mode to add new text to the story.\nWait for the approval after adding.";
interface RoleHeaderProps {
message: ChatMessage;
@ -92,10 +92,14 @@ export const ChatSidebar = () => {
const countTokens = async () => {
try {
const messages: LLM.ChatMessage[] = [];
const messages: ChatMessage[] = [];
if (input.trim()) {
messages.push({ role: 'user', content: input.trim() });
messages.push({
id: crypto.randomUUID(),
role: 'user',
content: input.trim(),
});
}
const chatRequest = Prompt.compilePrompt(appStateRef.current, messages);

View File

@ -41,6 +41,7 @@ export enum LocationScale {
Room = 'room',
House = 'house',
Street = 'street',
Village = 'village',
City = 'city',
Region = 'region',
Country = 'country',

View File

@ -1,6 +1,6 @@
import LLM from "./llm";
import Chapters from "./chapters";
import { type AppState, CharacterRole } from "../contexts/state";
import { type AppState, CharacterRole, type ChatMessage } from "../contexts/state";
import { Tools } from "./tools";
namespace Prompt {
@ -313,7 +313,7 @@ namespace Prompt {
return parts.join('\n\n');
}
export function compilePrompt(state: AppState, newMessages: LLM.ChatMessage[] = []): LLM.ChatCompletionRequest | null {
export function compilePrompt(state: AppState, newMessages: ChatMessage[] = []): LLM.ChatCompletionRequest | null {
const { currentStory, model, enableThinking } = state;
if (!currentStory || !model) {
@ -332,12 +332,21 @@ namespace Prompt {
storyTokenBudget = model.max_context - otherTokens;
}
const messages: LLM.ChatMessage[] = [
{ role: 'system', content: formatSystemPrompt(state, storyTokenBudget) },
const messages: ChatMessage[] = [
{
id: crypto.randomUUID(),
role: 'system',
content: formatSystemPrompt(state, storyTokenBudget),
},
...currentStory.chatMessages,
];
messages.push(...newMessages);
const presentMessages = new Set(messages.map(m => m.id));
for (const newMessage of newMessages) {
if (!presentMessages.has(newMessage.id)) {
messages.push(newMessage);
}
}
return {
model: model.id,

View File

@ -399,10 +399,10 @@ export namespace Tools {
dispatchEdit(currentText + '\n' + args.new_text);
appState.dispatch({ type: 'SET_CURRENT_TAB', id: appState.currentStory.id, tab });
let message = cropped
? `Added text:\n${args.new_text.split('\n').filter(l => l.trim()).map(l => '> ' + l).join('\n')}\n\nWarning: The rest was cropped due to ${LINES_LIMIT} lines limit! Write less next time.`
? `Added text:\n${args.new_text.split('\n').filter(l => l.trim()).map(l => '> ' + l).join('\n')}\n\nNote: The rest was cropped due to ${LINES_LIMIT} lines limit!`
: `Text appended to ${target} successfully.`;
message += `\nNote: you can't continue until user's approval, stop.`
message += `\nNote: you can't continue nor add more text until user's approval. Stop any attempts and wait.`
return message;
}