Skip to content

Commit 330bb94

Browse files
Fix system prompt flow: ensure system prompts reach OpenAI API
- Fixed critical bug where system prompts weren't making it to OpenAI completions API - Root cause: Chat initialization only created user message, ignored systemPromptDrafts - Updated chat initialization to check systemPromptDrafts.get(chatId) and build proper messages array - System prompt now becomes part of permanent chat history from start - OpenAI API now receives complete messages array with system prompt included 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: AnthonyRonning <AnthonyRonning@users.noreply.github.com>
1 parent 693bd3c commit 330bb94

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

frontend/src/routes/_auth.chat.$chatId.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function SystemMessage({ text, loading }: { text: string; loading?: boolean }) {
7070

7171
function ChatComponent() {
7272
const { chatId } = Route.useParams();
73-
const { model, persistChat, getChatById, userPrompt, setUserPrompt, addChat } = useLocalState();
73+
const { model, persistChat, getChatById, userPrompt, setUserPrompt, addChat, systemPromptDrafts } = useLocalState();
7474
const openai = useOpenAI();
7575
const queryClient = useQueryClient();
7676
const [showScrollButton, setShowScrollButton] = useState(false);
@@ -153,17 +153,29 @@ function ChatComponent() {
153153
}
154154
if (queryChat.messages.length === 0) {
155155
console.warn("Chat has no messages, using user prompt");
156-
const messages = userPrompt
157-
? ([{ role: "user", content: userPrompt }] as ChatMessage[])
158-
: [];
156+
157+
// Build messages array with system prompt first (if exists), then user prompt
158+
const messages: ChatMessage[] = [];
159+
160+
// Check for system prompt draft for this chat
161+
const systemPromptDraft = systemPromptDrafts.get(chatId);
162+
if (systemPromptDraft?.trim()) {
163+
messages.push({ role: "system", content: systemPromptDraft.trim() } as ChatMessage);
164+
}
165+
166+
// Add user prompt if exists
167+
if (userPrompt) {
168+
messages.push({ role: "user", content: userPrompt } as ChatMessage);
169+
}
170+
159171
setLocalChat((localChat) => ({ ...localChat, messages }));
160172
return;
161173
}
162174
setLocalChat(queryChat);
163175
}
164176
// I don't want to re-run this effect if the user prompt changes
165177
// eslint-disable-next-line react-hooks/exhaustive-deps
166-
}, [queryChat, chatId, isPending]);
178+
}, [queryChat, chatId, isPending, systemPromptDrafts]);
167179

168180
// IMPORTANT that this runs only once (because it uses the user's tokens!)
169181
const userPromptEffectRan = useRef(false);

0 commit comments

Comments
 (0)