Skip to content

An AI-powered search engine with a generative UI

fix(prompt): keep the related-questions reminder out of replayed history (#967)

Yoshiki Miuraae8d7b17b905
diff --git a/lib/agents/__tests__/researcher.test.ts b/lib/agents/__tests__/researcher.test.ts
index 5ded76de..49ebe6d9 100644
--- a/lib/agents/__tests__/researcher.test.ts
+++ b/lib/agents/__tests__/researcher.test.ts
@@ -134,10 +134,11 @@ describe('createResearcher', () => {
expect(result.messages.at(-1)).toEqual({
role: 'user',
- content: [
- { type: 'text', text: 'Deep follow-up' },
- { type: 'text', text: RELATED_QUESTIONS_REMINDER }
- ]
+ content: [{ type: 'text', text: RELATED_QUESTIONS_REMINDER }]
+ })
+ expect(result.messages.at(-2)).toEqual({
+ role: 'user',
+ content: 'Deep follow-up'
})
})
})
diff --git a/lib/agents/prompts/__tests__/related-questions-reminder.test.ts b/lib/agents/prompts/__tests__/related-questions-reminder.test.ts
index c8b2749c..36d731f3 100644
--- a/lib/agents/prompts/__tests__/related-questions-reminder.test.ts
+++ b/lib/agents/prompts/__tests__/related-questions-reminder.test.ts
@@ -7,30 +7,27 @@ import {
} from '../related-questions-reminder'
describe('appendRelatedQuestionsReminder', () => {
- it('places the reminder after the current user text in a deep conversation', () => {
+ it('places the reminder in its own message after a deep conversation', () => {
const messages: ModelMessage[] = [
{ role: 'user', content: 'First question' },
{ role: 'assistant', content: 'First answer' },
{ role: 'user', content: 'Follow-up question' }
]
+ const originalMessages = structuredClone(messages)
const result = appendRelatedQuestionsReminder(messages)
expect(result).not.toBe(messages)
expect(result.at(-1)).toEqual({
role: 'user',
- content: [
- { type: 'text', text: 'Follow-up question' },
- { type: 'text', text: RELATED_QUESTIONS_REMINDER }
- ]
- })
- expect(messages.at(-1)).toEqual({
- role: 'user',
- content: 'Follow-up question'
+ content: [{ type: 'text', text: RELATED_QUESTIONS_REMINDER }]
})
+ expect(result.at(-2)).toEqual(messages.at(-1))
+ expect(result.at(-2)).toBe(messages.at(-1))
+ expect(messages).toEqual(originalMessages)
})
- it('preserves non-text parts on the current user message', () => {
+ it('leaves a user message carrying non-text parts untouched', () => {
const messages: ModelMessage[] = [
{
role: 'user',
@@ -44,17 +41,27 @@ describe('appendRelatedQuestionsReminder', () => {
]
}
]
+ const originalMessage = messages[0]
+ const originalContent = originalMessage.content
const result = appendRelatedQuestionsReminder(messages)
- expect(result[0].content).toEqual([
+ expect(result[0]).toEqual(originalMessage)
+ expect(result[0]).toBe(originalMessage)
+ expect(result[1]).toEqual({
+ role: 'user',
+ content: [{ type: 'text', text: RELATED_QUESTIONS_REMINDER }]
+ })
+ expect(messages).toHaveLength(1)
+ expect(messages[0]).toBe(originalMessage)
+ expect(messages[0].content).toBe(originalContent)
+ expect(messages[0].content).toEqual([
{ type: 'text', text: 'Describe this file' },
{
type: 'file',
data: new Uint8Array([1, 2, 3]),
mediaType: 'application/pdf'
- },
- { type: 'text', text: RELATED_QUESTIONS_REMINDER }
+ }
])
})
@@ -65,4 +72,71 @@ describe('appendRelatedQuestionsReminder', () => {
expect(appendRelatedQuestionsReminder(messages)).toBe(messages)
})
+
+ it('keeps replayed conversation history stable across turns', () => {
+ const turnNMessages: ModelMessage[] = [
+ { role: 'user', content: 'First question' },
+ { role: 'assistant', content: 'First answer' },
+ { role: 'user', content: 'Second question' }
+ ]
+ const turnNPlusOneMessages: ModelMessage[] = [
+ ...turnNMessages,
+ { role: 'assistant', content: 'Second answer' },
+ { role: 'user', content: 'Third question' }
+ ]
+
+ const turnNResult = appendRelatedQuestionsReminder(turnNMessages)
+ const turnNPlusOneResult =
+ appendRelatedQuestionsReminder(turnNPlusOneMessages)
+
+ expect(turnNResult.slice(0, turnNMessages.length)).toEqual(turnNMessages)
+ expect(turnNPlusOneResult.slice(0, turnNMessages.length)).toEqual(
+ turnNMessages
+ )
+ expect(turnNPlusOneResult[turnNMessages.length]).toEqual({
+ role: 'assistant',
+ content: 'Second answer'
+ })
+ })
+
+ it('never leaves two consecutive user turns for a provider that requires alternating roles', () => {
+ const messages: ModelMessage[] = [
+ { role: 'user', content: 'First question' },
+ { role: 'assistant', content: 'First answer' },
+ { role: 'user', content: 'Follow-up question' }
+ ]
+
+ const result = appendRelatedQuestionsReminder(messages, 'google')
+
+ expect(result.map(message => message.role)).toEqual([
+ 'user',
+ 'assistant',
+ 'user'
+ ])
+ expect(result.at(-1)).toEqual({
+ role: 'user',
+ content: [
+ { type: 'text', text: 'Follow-up question' },
+ { type: 'text', text: RELATED_QUESTIONS_REMINDER }
+ ]
+ })
+ expect(messages.at(-1)).toEqual({
+ role: 'user',
+ content: 'Follow-up question'
+ })
+ })
+
+ it('uses its own message for a provider that accepts consecutive user turns', () => {
+ const messages: ModelMessage[] = [
+ { role: 'user', content: 'Follow-up question' }
+ ]
+
+ const result = appendRelatedQuestionsReminder(messages, 'openai')
+
+ expect(result.at(-1)).toEqual({
+ role: 'user',
+ content: [{ type: 'text', text: RELATED_QUESTIONS_REMINDER }]
+ })
+ expect(result.at(-2)).toBe(messages.at(-1))
+ })
})
diff --git a/lib/agents/prompts/related-questions-reminder.ts b/lib/agents/prompts/related-questions-reminder.ts
index 0cc5f715..a9cc7692 100644
--- a/lib/agents/prompts/related-questions-reminder.ts
+++ b/lib/agents/prompts/related-questions-reminder.ts
@@ -2,12 +2,23 @@ import type { ModelMessage } from 'ai'
export const RELATED_QUESTIONS_REMINDER = `Response format reminder: When this turn produces a substantive answer, end it with exactly one related-questions \`\`\`spec JSONL block containing exactly three questions, using the schema in the system instructions. Omit the block only for the skip cases listed there.`
+// Providers whose API rejects a prompt that does not strictly alternate roles,
+// and so cannot be given the reminder as a message of its own.
+const PROVIDERS_REQUIRING_ALTERNATING_ROLES = new Set(['google'])
+
/**
* Keeps the related-questions requirement next to the current request instead
* of leaving it only at the start of an increasingly long conversation.
+ *
+ * The reminder goes in a message of its own, because folding it into the
+ * current user message rewrites history that the next turn replays without it:
+ * the prompt prefix would then differ on every turn and stop being cacheable.
+ * Providers that require alternating roles cannot take the extra message, so
+ * they fall back to folding and keep the prefix cost.
*/
export function appendRelatedQuestionsReminder(
- messages: ModelMessage[]
+ messages: ModelMessage[],
+ providerId?: string
): ModelMessage[] {
const lastUserIndex = messages.findLastIndex(
message => message.role === 'user'
@@ -15,13 +26,21 @@ export function appendRelatedQuestionsReminder(
if (lastUserIndex === -1) return messages
- const userMessage = messages[lastUserIndex]
- if (userMessage.role !== 'user') return messages
-
const reminderPart = {
type: 'text' as const,
text: RELATED_QUESTIONS_REMINDER
}
+
+ if (
+ providerId === undefined ||
+ !PROVIDERS_REQUIRING_ALTERNATING_ROLES.has(providerId)
+ ) {
+ return [...messages, { role: 'user', content: [reminderPart] }]
+ }
+
+ const userMessage = messages[lastUserIndex]
+ if (userMessage.role !== 'user') return messages
+
const content =
typeof userMessage.content === 'string'
? [{ type: 'text' as const, text: userMessage.content }, reminderPart]
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index b42099b1..f7e2f3fb 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -147,7 +147,10 @@ export function createResearcher({
prepareCall: options => ({
...options,
...(options.messages && {
- messages: appendRelatedQuestionsReminder(options.messages)
+ messages: appendRelatedQuestionsReminder(
+ options.messages,
+ modelConfig?.providerId
+ )
})
}),
...(providerOptions && { providerOptions }),