1
0
Fork 0

Compare commits

..

No commits in common. "9803512c0b07a3c503bd0dff40250fc70ba6145a" and "f5479690f9b03df7997959d7e6be77c6e1263d67" have entirely different histories.

6 changed files with 14 additions and 31 deletions

View File

@ -78,8 +78,6 @@
.hr { .hr {
border: none; border: none;
border-bottom: 1px solid var(--hrColor, #555); border-top: 1px solid var(--hrColor, #555);
margin: 0.5em 0; 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 => { export const highlight = (message: string, keepMarkup = true): string => {
let resultHTML = ''; let resultHTML = '';
const tokenRegex = /(\*{1,3}|"|```|`|(?:^|\n)#{1,3} |(?:^|\n)> |\n)|---/g; const tokenRegex = /(\*\*?|"|```|`|(?:^|\n)#{1,3} |(?:^|\n)> |\n)/g;
const headerRegex = /#{1,3} $/; const headerRegex = /#{1,3} $/;
const blockquoteRegex = /> $/; const blockquoteRegex = /> $/;
const stack: string[] = []; const stack: string[] = [];
@ -116,9 +116,7 @@ export const highlight = (message: string, keepMarkup = true): string => {
continue; continue;
} }
if (token === '***' || token === '---') { if (isClose) {
resultHTML += `<span class="${styles.hr}">${keepToken ? token : ''}</span>`;
} else if (isClose) {
stack.pop(); stack.pop();
resultHTML += `${keepToken ? token : ''}</span>`; resultHTML += `${keepToken ? token : ''}</span>`;
} else if (token === '*') { } else if (token === '*') {
@ -149,6 +147,7 @@ export const highlight = (message: string, keepMarkup = true): string => {
if (!keepMarkup) { if (!keepMarkup) {
resultHTML = resultHTML.replace(/((?:(?:^|\n)\|.+)+)/g, match => parseTable(match)); 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)[-+] .+)+)/g, match => parseList(match, false));
resultHTML = resultHTML.replace(/((?:(?:^|\n)\d+\. .+)+)/g, match => parseList(match, true)); 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 clsx from "clsx";
import { ContentEditable } from "@common/components/ContentEditable"; 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.\nWait for the approval after adding."; const CONTINUE_PROMPT = "Continue the story forward.\nUse `edit_text` tool in append mode to add new text to the story.";
interface RoleHeaderProps { interface RoleHeaderProps {
message: ChatMessage; message: ChatMessage;
@ -92,14 +92,10 @@ export const ChatSidebar = () => {
const countTokens = async () => { const countTokens = async () => {
try { try {
const messages: ChatMessage[] = []; const messages: LLM.ChatMessage[] = [];
if (input.trim()) { if (input.trim()) {
messages.push({ messages.push({ role: 'user', content: input.trim() });
id: crypto.randomUUID(),
role: 'user',
content: input.trim(),
});
} }
const chatRequest = Prompt.compilePrompt(appStateRef.current, messages); const chatRequest = Prompt.compilePrompt(appStateRef.current, messages);

View File

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

View File

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

View File

@ -399,10 +399,10 @@ export namespace Tools {
dispatchEdit(currentText + '\n' + args.new_text); dispatchEdit(currentText + '\n' + args.new_text);
appState.dispatch({ type: 'SET_CURRENT_TAB', id: appState.currentStory.id, tab }); appState.dispatch({ type: 'SET_CURRENT_TAB', id: appState.currentStory.id, tab });
let message = cropped let message = cropped
? `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!` ? `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.`
: `Text appended to ${target} successfully.`; : `Text appended to ${target} successfully.`;
message += `\nNote: you can't continue nor add more text until user's approval. Stop any attempts and wait.` message += `\nNote: you can't continue until user's approval, stop.`
return message; return message;
} }