Commit 36d818e16 for llama.cpp

commit 36d818e161c400eb856e4591be49f422fbeeea53
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date:   Wed Nov 25 08:42:41 2026 +0100

    ui: edit the system message in the chat form when it is hidden in conversations (#32967)

    * ui: edit the system message in the chat form when it is hidden in conversations

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: open the hidden system message from a chip under the chat form

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: keep a new chat's system message as a form attachment until the first message

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: open the chat form in two rows while it holds the system message attachment

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: show a conversation's hidden system message in a chip under the chat form

    The attachment stays for a new chat, until the first message sends it.

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: show the system message chip whenever a conversation has one

    Assisted-by: Claude (Opus 5.5 Medium)

diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsList.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsList.svelte
index 611e52a75..cac04cba5 100644
--- a/ui/apps/chat/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsList.svelte
+++ b/ui/apps/chat/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsList.svelte
@@ -14,6 +14,7 @@
 		isMcpResource,
 		isMediaExtra
 	} from '#lib/utils/index.ts';
+	import type { Snippet } from 'svelte';

 	interface Props {
 		class?: string;
@@ -34,6 +35,8 @@
 		activeModelId?: string;
 		/** A message's attachments: media opens the conversation's slideshow at it */
 		conversationGallery?: boolean;
+		/** Rendered before the attachments, e.g. the chat form's system message */
+		leading?: Snippet;
 	}

 	let {
@@ -45,6 +48,7 @@
 		imageClass = '',
 		imageHeight = 'h-24',
 		imageWidth = 'w-auto',
+		leading,
 		limitToSingleRow = false,
 		onFileRemove,
 		readonly = false,
@@ -104,16 +108,20 @@
 	/>
 {/snippet}

-{#if displayItems.length > 0}
+{#if displayItems.length > 0 || leading}
 	<div class={className} {style}>
 		{#if limitToSingleRow}
 			<ScrollCarousel bind:this={carouselRef} variant={ScrollCarouselVariant.CENTER}>
+				{@render leading?.()}
+
 				{#each displayItems as item (item.id)}
 					{@render attachmentitem(item)}
 				{/each}
 			</ScrollCarousel>
 		{:else}
 			<div class="flex flex-wrap items-start justify-end gap-3">
+				{@render leading?.()}
+
 				{#each displayItems as item (item.id)}
 					{@render attachmentitem(item)}
 				{/each}
diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte
index b9845cc86..f17cb3f90 100644
--- a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte
+++ b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte
@@ -13,6 +13,8 @@
 		ChatFormInputFileInputInvisible,
 		ChatFormMcpResourcesList,
 		ChatFormPickers,
+		ChatFormSystemMessageAttachment,
+		ChatFormSystemMessageChip,
 		DialogMcpResourcesBrowser
 	} from '#lib/components/app/index.ts';
 	import {
@@ -22,12 +24,14 @@
 		INITIAL_FILE_SIZE,
 		PROMPT_CONTENT_SEPARATOR,
 		SETTING_CONFIG_DEFAULT,
+		SYSTEM_MESSAGE_PLACEHOLDER,
 		VOICE_EDITOR_SELECTOR
 	} from '#lib/constants/index.ts';
 	import {
 		ContentPartType,
 		FileExtensionText,
 		KeyboardKey,
+		MessageRole,
 		MimeTypeText,
 		SpecialFileType,
 		ToolSource
@@ -87,6 +91,8 @@
 		placeholder?: string;
 		showAddButton?: boolean;
 		showModelSelector?: boolean;
+		/** Writing the system message: the form takes the system message's dashed, muted look. */
+		systemMode?: boolean;

 		// Event Handlers
 		onAttachmentRemove?: (index: number) => void;
@@ -120,6 +126,7 @@
 		showAddButton = true,
 		showModelSelector = true,
 		singleRow = false,
+		systemMode = false,
 		uploadedFiles = $bindable([]),
 		value = $bindable(''),
 		voiceRecorder
@@ -149,6 +156,23 @@
 		conversationsStore.activeConversation?.cwd ?? conversationsStore.preferences.pendingCwd
 	);

+	let systemMessage = $derived(
+		conversationsStore.activeMessages.find((m) => m.role === MessageRole.SYSTEM)
+	);
+	// A new chat shows the system message written for it as an attachment, until the first
+	// message sends it. A conversation with one offers it in a chip under the form.
+	// A placeholder is one nobody has written yet
+	let systemMessageAttachment = $derived.by(() => {
+		if (conversationsStore.activeConversation) return null;
+
+		const content = chatStore.pendingSystemMessage;
+
+		return content && content.trim() && content !== SYSTEM_MESSAGE_PLACEHOLDER ? content : null;
+	});
+	let systemMessageChip = $derived(
+		conversationsStore.activeConversation ? systemMessage?.content : undefined
+	);
+
 	let showWorkingDirectory = $derived(conversationsStore.preferences.hasEnabledCwdTools());
 	// On a desktop the chip under the form shows a directory once one is set, and the + menu
 	// sets one until then. A phone has no chip: a chat shows it in its header (see
@@ -295,6 +319,7 @@
 			!isExpanded &&
 			!voice.isActive &&
 			!hasAttachments &&
+			!systemMessageAttachment &&
 			!mcpStore.resources.hasAttachments &&
 			!value.includes('\n')
 	);
@@ -680,6 +705,12 @@

 <svelte:window onkeydown={handleWindowKeydown} />

+{#snippet systemMessageLeading()}
+	{#if systemMessageAttachment}
+		<ChatFormSystemMessageAttachment content={systemMessageAttachment} {disabled} />
+	{/if}
+{/snippet}
+
 <ChatFormInputFileInputInvisible bind:this={fileInputRef} onFileSelect={handleFileSelect} />

 <form
@@ -758,6 +789,7 @@
 				'max-md:border-transparent max-md:focus-within:border-transparent max-md:dark:border-transparent max-md:dark:focus-within:border-transparent',
 				EXPANDED_SHEET_FILL
 			],
+			systemMode && '!border-2 !border-dashed !border-border/50 !bg-muted',
 			disabled && 'cursor-not-allowed opacity-60'
 		]}
 		data-single-row={isSingleRow || undefined}
@@ -779,6 +811,7 @@
 			activeModelId={activeModelId ?? undefined}
 			{attachments}
 			class={isExpanded ? 'py-5 max-md:px-(--chat-form-side-inset)' : 'py-5'}
+			leading={systemMessageAttachment ? systemMessageLeading : undefined}
 			limitToSingleRow
 			onFileRemove={handleFileRemove}
 			style="scroll-padding: 1rem;"
@@ -898,21 +931,27 @@
 		></div>
 	{/if}

-	{#if showWorkingDirectory}
-		<!-- opened from the chip, the + menu, a phone chat's header or `/cwd` -->
-		<ChatFormCurrentWorkingDirectory
-			bind:query={pickers.workingDirectoryQuery}
-			class="self-start"
-			customAnchor={activePickerAnchor}
-			directory={cwd}
-			{disabled}
-			isOpen={pickers.isWorkingDirectoryPickerOpen}
-			onChange={handleWorkingDirectoryChange}
-			onClose={pickers.handleWorkingDirectoryClose}
-			onOpen={pickers.handleWorkingDirectoryOpen}
-			showChip={showWorkingDirectoryChip}
-		/>
-	{/if}
+	<div class="flex flex-wrap items-center gap-x-1 self-start">
+		{#if showWorkingDirectory}
+			<!-- opened from the chip, the + menu, a phone chat's header or `/cwd` -->
+			<ChatFormCurrentWorkingDirectory
+				bind:query={pickers.workingDirectoryQuery}
+				class="self-start"
+				customAnchor={activePickerAnchor}
+				directory={cwd}
+				{disabled}
+				isOpen={pickers.isWorkingDirectoryPickerOpen}
+				onChange={handleWorkingDirectoryChange}
+				onClose={pickers.handleWorkingDirectoryClose}
+				onOpen={pickers.handleWorkingDirectoryOpen}
+				showChip={showWorkingDirectoryChip}
+			/>
+		{/if}
+
+		{#if systemMessageChip}
+			<ChatFormSystemMessageChip content={systemMessageChip} {disabled} />
+		{/if}
+	</div>
 </form>

 <DialogMcpResourcesBrowser
diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormSystemMessageAttachment.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormSystemMessageAttachment.svelte
new file mode 100644
index 000000000..eaec0db17
--- /dev/null
+++ b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormSystemMessageAttachment.svelte
@@ -0,0 +1,65 @@
+<script lang="ts">
+	import { Card } from '@ggml-org/llama-ui/ui/card';
+	import { MessageSquareText, X } from '@lucide/svelte';
+	import { ActionIcon, DialogSystemMessageEdit } from '#lib/components/app/index.ts';
+	import { chatStore } from '#lib/stores/index.ts';
+
+	interface Props {
+		class?: string;
+		content: string;
+		disabled?: boolean;
+	}
+
+	let { class: className = '', content, disabled = false }: Props = $props();
+
+	let isOpen = $state(false);
+</script>
+
+<div
+	class="group relative flex max-w-75 min-w-50 flex-shrink-0 flex-col gap-2 first:ml-4 last:mr-4 {className}"
+>
+	<div class="inline-flex items-center gap-1.25 text-caption text-muted-foreground">
+		<MessageSquareText class="size-3.5 shrink-0" />
+
+		<span>Custom system message</span>
+	</div>
+
+	<button
+		aria-label="Edit custom system message"
+		class="text-left"
+		{disabled}
+		onclick={() => (isOpen = true)}
+		type="button"
+	>
+		<!-- the system message bubble's dashed border and muted fill -->
+		<Card
+			class="overflow-hidden rounded-2xl !border-2 !border-dashed !border-border/50 bg-muted py-0 text-foreground"
+		>
+			<div
+				class="overflow-y-auto px-3 py-2"
+				style="max-height: 6rem; overflow-wrap: anywhere; word-break: break-word;"
+			>
+				<span class="text-caption whitespace-pre-wrap">{content}</span>
+			</div>
+		</Card>
+	</button>
+
+	{#if !disabled}
+		<div
+			class="absolute top-10 right-2 flex items-center justify-center opacity-0 transition-opacity group-hover:opacity-100"
+		>
+			<ActionIcon
+				icon={X}
+				onclick={() => chatStore.removeSystemPrompt()}
+				stopPropagationOnClick
+				tooltip="Remove"
+			/>
+		</div>
+	{/if}
+</div>
+
+<DialogSystemMessageEdit
+	bind:open={isOpen}
+	{content}
+	onSave={(next) => chatStore.saveSystemPrompt(next)}
+/>
diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormSystemMessageChip.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormSystemMessageChip.svelte
new file mode 100644
index 000000000..d797ce208
--- /dev/null
+++ b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormSystemMessageChip.svelte
@@ -0,0 +1,40 @@
+<script lang="ts">
+	import { MessageSquareText } from '@lucide/svelte';
+	import { DialogSystemMessageEdit } from '#lib/components/app/index.ts';
+	import { SYSTEM_MESSAGE_PLACEHOLDER } from '#lib/constants/index.ts';
+	import { chatStore } from '#lib/stores/index.ts';
+
+	interface Props {
+		content: string;
+		disabled?: boolean;
+	}
+
+	let { content, disabled = false }: Props = $props();
+
+	let isOpen = $state(false);
+
+	// a placeholder is a system message nobody has written yet
+	const hasContent = $derived(content.trim() !== '' && content !== SYSTEM_MESSAGE_PLACEHOLDER);
+</script>
+
+{#if hasContent}
+	<div class="grid">
+		<button
+			class="mt-1.5 flex w-auto min-w-0 items-center gap-1 rounded-md px-2 py-1 text-caption text-muted-foreground backdrop-blur-2xl hover:text-foreground"
+			{disabled}
+			onclick={() => (isOpen = true)}
+			title={content}
+			type="button"
+		>
+			<MessageSquareText class="size-icon-sm" />
+
+			<span class="truncate">Custom system message</span>
+		</button>
+	</div>
+
+	<DialogSystemMessageEdit
+		bind:open={isOpen}
+		{content}
+		onSave={(next) => chatStore.saveSystemPrompt(next)}
+	/>
+{/if}
diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte
index 21bd62201..93dea8c1f 100644
--- a/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte
+++ b/ui/apps/chat/src/lib/components/app/chat/ChatScreen/ChatScreenForm.svelte
@@ -1,8 +1,9 @@
 <script lang="ts">
 	import { ChatForm } from '#lib/components/app/index.ts';
-	import { SETTINGS_KEYS } from '#lib/constants/index.ts';
+	import { SETTINGS_KEYS, SYSTEM_MESSAGE_PLACEHOLDER } from '#lib/constants/index.ts';
+	import { MessageRole } from '#lib/enums/index.ts';
 	import { useDraftMessages } from '#lib/hooks/use-draft-messages.svelte.ts';
-	import { deviceStore, settingsStore } from '#lib/stores/index.ts';
+	import { chatStore, conversationsStore, deviceStore, settingsStore } from '#lib/stores/index.ts';
 	import { hapticTap, shouldTypeToFocus } from '#lib/utils/index.ts';
 	import { afterNavigate } from '$app/navigation';
 	import { page } from '$app/state';
@@ -65,10 +66,13 @@
 	let message = $state('');
 	let previousIsLoading = $derived(isLoading);

+	let isSystemMode = $state(false);
+	let stashedDraft = $state<{ files: ChatUploadedFile[]; message: string } | null>(null);
+
 	const { clearDraft } = useDraftMessages({
 		getChatId: () => chatId,
-		getFiles: () => uploadedFiles,
-		getMessage: () => message,
+		getFiles: () => (isSystemMode ? (stashedDraft?.files ?? []) : uploadedFiles),
+		getMessage: () => (isSystemMode ? (stashedDraft?.message ?? '') : message),
 		setFiles: (f) => (uploadedFiles = f),
 		setMessage: (m) => (message = m)
 	});
@@ -78,6 +82,8 @@
 	}

 	async function handleSubmit() {
+		if (isSystemMode) return handleSystemSubmit();
+
 		if ((!message.trim() && uploadedFiles.length === 0) || disabled || hasLoadingAttachments)
 			return;

@@ -105,8 +111,54 @@
 		}
 	}

+	// With system messages hidden in the conversation there is nothing to edit in place, so the
+	// form itself turns into the system message editor
+
 	function handleSystemPromptClick() {
-		onSystemPromptAdd?.({ files: uploadedFiles, message });
+		if (settingsStore.config[SETTINGS_KEYS.SHOW_SYSTEM_MESSAGE] ?? true) {
+			onSystemPromptAdd?.({ files: uploadedFiles, message });
+
+			return;
+		}
+
+		const existing = conversationsStore.activeMessages.find((m) => m.role === MessageRole.SYSTEM);
+
+		stashedDraft = { files: uploadedFiles, message };
+		const current = conversationsStore.activeConversation
+			? existing?.content
+			: chatStore.pendingSystemMessage;
+
+		message = current && current !== SYSTEM_MESSAGE_PLACEHOLDER ? current : '';
+		uploadedFiles = [];
+		isSystemMode = true;
+		void tick().then(() => chatFormRef?.focus());
+	}
+
+	function exitSystemMode(restoreDraft: boolean) {
+		isSystemMode = false;
+
+		if (restoreDraft && stashedDraft) {
+			message = stashedDraft.message;
+			uploadedFiles = stashedDraft.files;
+		}
+
+		stashedDraft = null;
+		void tick().then(() => chatFormRef?.focus());
+	}
+
+	async function handleSystemSubmit() {
+		const content = message.trim();
+
+		if (!content) return;
+
+		const draft = stashedDraft ?? { files: [], message: '' };
+
+		isSystemMode = false;
+		stashedDraft = null;
+		message = draft.message;
+		uploadedFiles = draft.files;
+
+		await chatStore.saveSystemPrompt(content);
 	}

 	function handleUploadedFileRemove(fileId: string) {
@@ -130,6 +182,12 @@
 	// Typing while nothing takes text starts the message: focused during keydown, the
 	// character the key types lands in the form
 	function handleWindowKeydown(event: KeyboardEvent) {
+		if (isSystemMode && event.key === 'Escape') {
+			exitSystemMode(true);
+
+			return;
+		}
+
 		if (disabled || deviceStore.isMobile || !shouldTypeToFocus(event)) return;

 		chatFormRef?.focus();
@@ -172,6 +230,8 @@
 		onSubmit={handleSubmit}
 		onSystemPromptClick={handleSystemPromptClick}
 		onUploadedFileRemove={handleUploadedFileRemove}
+		placeholder={isSystemMode ? 'System message...' : undefined}
 		singleRow={settingsStore.config[SETTINGS_KEYS.SINGLE_ROW_CHAT_FORM] !== false}
+		systemMode={isSystemMode}
 	/>
 </div>
diff --git a/ui/apps/chat/src/lib/components/app/chat/index.ts b/ui/apps/chat/src/lib/components/app/chat/index.ts
index fd90e5d61..282c16e90 100644
--- a/ui/apps/chat/src/lib/components/app/chat/index.ts
+++ b/ui/apps/chat/src/lib/components/app/chat/index.ts
@@ -714,3 +714,16 @@ export { default as ChatScreenServerError } from './ChatScreen/ChatScreenServerE
  * Renders nothing otherwise. Shown inside ChatScreen only on an active conversation route.
  */
 export { default as ChatScreenStreamResumeStatus } from './ChatScreen/ChatScreenStreamResumeStatus.svelte';
+
+/**
+ * A new chat's custom system message as an attachment in the chat form, with the system
+ * message's dashed look, until the first message. Clicking it opens the message in a dialog
+ * to edit; it can be removed.
+ */
+export { default as ChatFormSystemMessageAttachment } from './ChatForm/ChatFormSystemMessageAttachment.svelte';
+
+/**
+ * Chip under the chat form that opens the conversation's system message for editing.
+ * Shown whenever the conversation has one.
+ */
+export { default as ChatFormSystemMessageChip } from './ChatForm/ChatFormSystemMessageChip.svelte';
diff --git a/ui/apps/chat/src/lib/components/app/dialogs/DialogSystemMessageEdit.svelte b/ui/apps/chat/src/lib/components/app/dialogs/DialogSystemMessageEdit.svelte
new file mode 100644
index 000000000..fb062c14a
--- /dev/null
+++ b/ui/apps/chat/src/lib/components/app/dialogs/DialogSystemMessageEdit.svelte
@@ -0,0 +1,65 @@
+<script lang="ts">
+	import * as AlertDialog from '@ggml-org/llama-ui/ui/alert-dialog';
+	import { Button } from '@ggml-org/llama-ui/ui/button';
+	import { Textarea } from '@ggml-org/llama-ui/ui/textarea';
+	import { MessageSquareText } from '@lucide/svelte';
+
+	interface Props {
+		open: boolean;
+		content: string;
+		onSave: (content: string) => void;
+	}
+
+	let { content, onSave, open = $bindable() }: Props = $props();
+
+	let draft = $state('');
+	let textareaRef = $state<HTMLTextAreaElement | null>(null);
+
+	const canSave = $derived(draft.trim().length > 0 && draft.trim() !== content.trim());
+
+	$effect(() => {
+		if (!open) return;
+
+		draft = content;
+		queueMicrotask(() => textareaRef?.focus());
+	});
+
+	function handleSave() {
+		if (!canSave) return;
+
+		onSave(draft.trim());
+		open = false;
+	}
+</script>
+
+<AlertDialog.Root bind:open>
+	<AlertDialog.Content>
+		<AlertDialog.Header>
+			<AlertDialog.Title class="flex items-center gap-2">
+				<MessageSquareText class="size-icon-lg" />
+				Custom system message
+			</AlertDialog.Title>
+
+			<AlertDialog.Description>
+				The starting message that defines how the model behaves in this conversation.
+			</AlertDialog.Description>
+		</AlertDialog.Header>
+
+		<Textarea
+			bind:ref={textareaRef}
+			bind:value={draft}
+			aria-label="System message"
+			class="max-h-80 min-h-32"
+			onkeydown={(event) => {
+				if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) handleSave();
+			}}
+			placeholder="System message..."
+		/>
+
+		<AlertDialog.Footer>
+			<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
+
+			<Button disabled={!canSave} onclick={handleSave} type="button">Save</Button>
+		</AlertDialog.Footer>
+	</AlertDialog.Content>
+</AlertDialog.Root>
diff --git a/ui/apps/chat/src/lib/components/app/dialogs/index.ts b/ui/apps/chat/src/lib/components/app/dialogs/index.ts
index e0c1e454e..af0204ca8 100644
--- a/ui/apps/chat/src/lib/components/app/dialogs/index.ts
+++ b/ui/apps/chat/src/lib/components/app/dialogs/index.ts
@@ -525,3 +525,11 @@ export { default as DialogMermaidPreview } from './DialogMermaidPreview.svelte';

 /** **DialogDiscoverModels** - shell for the Hugging Face model discovery view. */
 export { default as DialogDiscoverModels } from './DialogDiscoverModels.svelte';
+
+/**
+ * **DialogSystemMessageEdit** - Edit the conversation's system message
+ *
+ * Modal dialog with a textarea, opened from the custom system message attachment
+ * in the chat form.
+ */
+export { default as DialogSystemMessageEdit } from './DialogSystemMessageEdit.svelte';
diff --git a/ui/apps/chat/src/lib/stores/chat/index.svelte.ts b/ui/apps/chat/src/lib/stores/chat/index.svelte.ts
index 07207a488..d939077b2 100644
--- a/ui/apps/chat/src/lib/stores/chat/index.svelte.ts
+++ b/ui/apps/chat/src/lib/stores/chat/index.svelte.ts
@@ -76,6 +76,8 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 		this.chatReasoningStates.get(conversationsStore.activeConversation?.id ?? '') ?? false
 	);
 	pendingEditMessageId = $state<string | null>(null);
+	/** A system message written on the new-chat screen, stored with the conversation on its first send. */
+	pendingSystemMessage = $state<string | null>(null);
 	// resumable stream connection state for the active conversation
 	// streaming -> bytes flowing normally, resuming -> waiting on /v1/stream reconnect, lost -> unrecoverable
 	streamConnectionState = $state<StreamConnectionState>(StreamConnectionState.STREAMING);
@@ -246,7 +248,6 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 			this.preEncodeAbortController = null;
 		}
 	}
-
 	/**
 	 * Whether the branch holds a step to summarize before the steps a
 	 * compaction preserves.
@@ -264,6 +265,7 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 		this.clearChatStreaming(convId);
 		this.processing.setState(convId, null);
 	}
+
 	clearChatStreaming(convId: string, messageId?: string): void {
 		// session aware: a stale generation must not wipe a newer one's streaming state on the
 		// same conversation, that would drop the frozen stop identity and stop the wrong session
@@ -279,7 +281,6 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 		this.isEditModeActive = false;
 		this.addFilesHandler = null;
 	}
-
 	clearPendingEditMessageId(): void {
 		this.pendingEditMessageId = null;
 	}
@@ -686,6 +687,21 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 		return this.flows.regenerateMessageWithBranching(messageId, modelOverride);
 	}

+	/** Removes the system message from the new chat, or from the active conversation. */
+	async removeSystemPrompt(): Promise<void> {
+		if (!conversationsStore.activeConversation) {
+			this.pendingSystemMessage = null;
+
+			return;
+		}
+
+		const systemMessage = conversationsStore.activeMessages.find(
+			(m) => m.role === MessageRole.SYSTEM
+		);
+
+		if (systemMessage) await this.removeSystemPromptPlaceholder(systemMessage.id);
+	}
+
 	async removeSystemPromptPlaceholder(messageId: string): Promise<boolean> {
 		const activeConv = conversationsStore.activeConversation;

@@ -735,6 +751,32 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 		}
 	}

+	/**
+	 * Saves the system message written in the chat form or its dialog. A new chat keeps it
+	 * pending until the first message is sent, so no empty conversation is created.
+	 */
+	async saveSystemPrompt(content: string): Promise<void> {
+		if (!conversationsStore.activeConversation) {
+			this.pendingSystemMessage = content;
+
+			return;
+		}
+
+		await this.addSystemPrompt();
+
+		const id = this.pendingEditMessageId;
+
+		this.pendingEditMessageId = null;
+
+		if (!id) return;
+
+		await DatabaseService.updateMessage(id, { content });
+
+		const index = conversationsStore.findMessageIndex(id);
+
+		if (index !== -1) conversationsStore.updateMessageAtIndex(index, { content });
+	}
+
 	async sendMessage(content: string, extras?: DatabaseMessageExtra[]): Promise<void> {
 		if (!content.trim() && (!extras || extras.length === 0)) return;

@@ -781,7 +823,11 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost {
 			if (isNewConversation) {
 				const rootId = await DatabaseService.createRootMessage(currentConv.id);
 				const currentConfig = settingsStore.config;
-				const systemPrompt = currentConfig.systemMessage?.toString().trim();
+				const systemPrompt = (
+					this.pendingSystemMessage ?? currentConfig.systemMessage?.toString()
+				)?.trim();
+
+				this.pendingSystemMessage = null;

 				let sysOrRootId = rootId;