Skip to content

An AI-powered search engine with a generative UI

fix(draft): keep the attached draft out of the moving prompt tail (#1103)

Yoshiki Miura375ff293b802
diff --git a/lib/draft/__tests__/draft.test.ts b/lib/draft/__tests__/draft.test.ts
index e69eed3a..1392b5fb 100644
--- a/lib/draft/__tests__/draft.test.ts
+++ b/lib/draft/__tests__/draft.test.ts
@@ -255,7 +255,7 @@ describe('compactDraftBlocks', () => {
test('stubs a block an answer stopped inside', () => {
const out = compactDraftBlocks('Here.\n\n````draft\ntitle: Note\n\nHalf')
expect(out).toBe(
- 'Here.\n\n[Draft "Note" written here (4 characters). The current version is attached to the latest user message.]'
+ 'Here.\n\n[Draft "Note" written here (4 characters). The current version is attached to a later user message.]'
)
})
@@ -454,7 +454,7 @@ describe('three-backtick close', () => {
test('compacts and versions a block closed with three backticks', () => {
const text = 'Here.\n\n````draft\ntitle: T\n\nBody\n```'
expect(compactDraftBlocks(text)).toBe(
- 'Here.\n\n[Draft "T" written here (4 characters). The current version is attached to the latest user message.]'
+ 'Here.\n\n[Draft "T" written here (4 characters). The current version is attached to a later user message.]'
)
expect(
buildDraftVersions([
diff --git a/lib/draft/draft.ts b/lib/draft/draft.ts
index 864aa24e..d2801a3b 100644
--- a/lib/draft/draft.ts
+++ b/lib/draft/draft.ts
@@ -400,11 +400,11 @@ export function compactDraftBlocks(text: string): string {
return replaceDraftBlocks(
text,
({ title, content }) =>
- `[Draft "${title}" written here (${content.length} characters). The current version is attached to the latest user message.]`
+ `[Draft "${title}" written here (${content.length} characters). The current version is attached to a later user message.]`
)
}
-/** The current draft, as attached to the latest user message for the model. */
+/** The current draft, as attached to a user message for the model. */
export function formatCurrentDraft(version: DraftVersion): string {
return [
'The chat draft below is attached by the app, not written by the user. Change it only if the user asks to.',
diff --git a/lib/draft/prompt.ts b/lib/draft/prompt.ts
index f9857bfe..eff6c46a 100644
--- a/lib/draft/prompt.ts
+++ b/lib/draft/prompt.ts
@@ -31,7 +31,7 @@ DRAFT RULES:
repeat the draft's content outside the block.
2. Do not put citation markers inside the draft. If the draft relies on search results, cite them in the sentence
outside the block.
-3. When a draft exists, the app attaches it to the latest message in <current_draft> for reference; the user did not
+3. When a draft exists, the app attaches its latest version to a user message in <current_draft> for reference; the user did not
write it there. Use editDraft only when the user asks to change that draft; for a change across the whole draft
(shorten, change the tone, restructure, translate), write a new complete draft block instead.
4. Answer questions, lists, explanations, and any other unrelated request normally, and leave the draft unchanged.
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 660aca04..d084b79a 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -278,6 +278,16 @@ export async function createChatStreamResponse(
if (
shouldTruncateMessages(modelMessages, model, attachmentTokenEstimates)
) {
+ // Truncation drops old messages, and the draft must survive it
+ if (currentDraft) {
+ modelMessages = await convertToModelMessages(
+ attachCurrentDraft(messagesToConvert, currentDraft, {
+ atLatest: true
+ }),
+ { convertDataPart }
+ )
+ }
+
const maxTokens = getMaxAllowedTokens(model)
const originalCount = modelMessages.length
modelMessages = truncateMessages(
diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.ts
index 24c6d5bc..1a64ff96 100644
--- a/lib/streaming/create-ephemeral-chat-stream-response.ts
+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts
@@ -165,6 +165,16 @@ export async function createEphemeralChatStreamResponse(
if (
shouldTruncateMessages(modelMessages, model, attachmentTokenEstimates)
) {
+ // Truncation drops old messages, and the draft must survive it
+ if (currentDraft) {
+ modelMessages = await convertToModelMessages(
+ attachCurrentDraft(messagesToConvert, currentDraft, {
+ atLatest: true
+ }),
+ { convertDataPart }
+ )
+ }
+
const maxTokens = getMaxAllowedTokens(model)
modelMessages = truncateMessages(
modelMessages,
diff --git a/lib/streaming/helpers/__tests__/draft-context.test.ts b/lib/streaming/helpers/__tests__/draft-context.test.ts
index 89ec95c0..aac9de41 100644
--- a/lib/streaming/helpers/__tests__/draft-context.test.ts
+++ b/lib/streaming/helpers/__tests__/draft-context.test.ts
@@ -1,6 +1,7 @@
import type { UIMessage } from 'ai'
import { describe, expect, it } from 'vitest'
+import { SOURCE_CONTEXT_MESSAGE_SUFFIX } from '../compact-historical-messages'
import { attachCurrentDraft, compactDraftsInMessages } from '../draft-context'
const DRAFT = ['Done.', '', '````draft', 'title: T', '', 'Body', '````'].join(
@@ -41,10 +42,10 @@ describe('attachCurrentDraft', () => {
messageId: 'a'
}
- it('appends the draft to the latest user message only', () => {
+ it('appends the draft to the user message after its source only', () => {
const first = message('user', 'one', 'u1')
const result = attachCurrentDraft(
- [first, message('assistant', 'reply'), message('user', 'two', 'u2')],
+ [first, message('assistant', 'reply', 'a'), message('user', 'two', 'u2')],
draft
)
@@ -56,6 +57,69 @@ describe('attachCurrentDraft', () => {
)
})
+ it('keeps the draft in place as the thread grows', () => {
+ const thread = [
+ message('user', 'one', 'u1'),
+ message('assistant', 'reply', 'a'),
+ message('user', 'two', 'u2'),
+ message('assistant', 'reply', 'a2')
+ ]
+ const before = attachCurrentDraft(thread, draft)
+ const after = attachCurrentDraft(
+ [...thread, message('user', 'three', 'u3')],
+ draft
+ )
+
+ expect(after.slice(0, before.length)).toEqual(before)
+ expect(after[4].parts).toHaveLength(1)
+ })
+
+ it('anchors after the opening turn when the source was trimmed', () => {
+ const result = attachCurrentDraft(
+ [
+ message('user', 'one', 'u1'),
+ message('assistant', 'reply', 'a1'),
+ message('user', 'two', 'u2'),
+ message('assistant', 'reply', 'a2'),
+ message('user', 'three', 'u3')
+ ],
+ draft
+ )
+
+ expect(result[2].parts).toHaveLength(2)
+ expect(result[4].parts).toHaveLength(1)
+ })
+
+ it('skips source context messages when anchoring', () => {
+ const result = attachCurrentDraft(
+ [
+ message('assistant', 'reply', 'a'),
+ message('user', 'sources', `u1${SOURCE_CONTEXT_MESSAGE_SUFFIX}`),
+ message('user', 'two', 'u2')
+ ],
+ draft
+ )
+
+ expect(result[1].parts).toHaveLength(1)
+ expect(result[2].parts).toHaveLength(2)
+ })
+
+ it('moves to the latest user message when asked to', () => {
+ const result = attachCurrentDraft(
+ [
+ message('assistant', 'reply', 'a'),
+ message('user', 'one', 'u1'),
+ message('assistant', 'reply', 'a2'),
+ message('user', 'two', 'u2')
+ ],
+ draft,
+ { atLatest: true }
+ )
+
+ expect(result[1].parts).toHaveLength(1)
+ expect(result[3].parts).toHaveLength(2)
+ })
+
it('leaves messages alone without a draft', () => {
const messages = [message('user', 'one')]
expect(attachCurrentDraft(messages, null)).toBe(messages)
diff --git a/lib/streaming/helpers/compact-historical-messages.ts b/lib/streaming/helpers/compact-historical-messages.ts
index f1a5d640..ed9b3868 100644
--- a/lib/streaming/helpers/compact-historical-messages.ts
+++ b/lib/streaming/helpers/compact-historical-messages.ts
@@ -25,7 +25,7 @@ import { sliceWithoutSplittingSurrogatePair } from './slice-without-splitting-su
const MAX_SOURCE_CONTEXT_CHARS = 800
const MAX_SOURCE_EXCERPT_CHARS = 400
const MIN_SOURCE_EXCERPT_CHARS = 80
-const SOURCE_CONTEXT_MESSAGE_SUFFIX = '-source-context'
+export const SOURCE_CONTEXT_MESSAGE_SUFFIX = '-source-context'
const SOURCE_CONTEXT_MESSAGE_PREFIX =
'Source context attached by the application for the preceding answer. This is not a message from the user.'
diff --git a/lib/streaming/helpers/draft-context.ts b/lib/streaming/helpers/draft-context.ts
index 150398dd..f5a2d29d 100644
--- a/lib/streaming/helpers/draft-context.ts
+++ b/lib/streaming/helpers/draft-context.ts
@@ -6,6 +6,8 @@ import {
formatCurrentDraft
} from '@/lib/draft/draft'
+import { isSourceContextMessage } from './compact-historical-messages'
+
/**
* Replaces draft blocks in assistant text with a one-line stub. Every version
* would otherwise replay in full on each later turn. The stub depends only on
@@ -29,17 +31,38 @@ export function compactDraftsInMessages(messages: UIMessage[]): UIMessage[] {
}
/**
- * Attaches the current draft to the latest user message, the tail of the
- * prompt, so it never sits inside the cached prefix. The model needs the exact
- * text to write editDraft find strings.
+ * The first user message after the one that produced the draft. It stays put
+ * until the draft changes, so later turns only append to the prompt. Without
+ * the source message it was trimmed from the middle of the thread, which leaves
+ * only the opening turn before it.
+ */
+function findDraftAnchor(messages: UIMessage[], sourceId: string): number {
+ const isUser = (message: UIMessage) =>
+ message.role === 'user' && !isSourceContextMessage(message)
+
+ const source = messages.findIndex(msg => msg.id === sourceId)
+ const after = source >= 0 ? source : messages.findIndex(isUser)
+ const next = messages.findIndex((msg, i) => i > after && isUser(msg))
+
+ return next >= 0 ? next : messages.findLastIndex(isUser)
+}
+
+/**
+ * Attaches the current draft to a user message. The model needs the exact text
+ * to write editDraft find strings. The prompt cache only extends a previous
+ * request, so a draft at the moving tail would rewrite the whole thread each
+ * turn. Pass atLatest when older messages are about to be dropped.
*/
export function attachCurrentDraft(
messages: UIMessage[],
- currentDraft: DraftVersion | null
+ currentDraft: DraftVersion | null,
+ { atLatest = false }: { atLatest?: boolean } = {}
): UIMessage[] {
if (!currentDraft) return messages
- const index = messages.findLastIndex(msg => msg.role === 'user')
+ const index = atLatest
+ ? messages.findLastIndex(msg => msg.role === 'user')
+ : findDraftAnchor(messages, currentDraft.messageId)
if (index < 0) return messages
const target = messages[index]