Skip to content

An AI-powered search engine with a generative UI

refactor: pasted content & URLs as structured data parts (#887)

Yoshiki Miuracd5e4bf7787f
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index 6e44643f..6a0c4a69 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -270,17 +270,37 @@ export function ChatPanel({
)}
<form
onSubmit={e => {
- // Fold content cards (target) + input (instruction) into one
- // message, then re-submit through the normal path.
+ // Pasted attachments (content cards / URL chips) are sent as
+ // structured data parts alongside the instruction text part — no
+ // in-band markers. The server maps them to the model prompt.
if (contentCards.length > 0 || urlCards.length > 0) {
e.preventDefault()
- const combined = [
- ...contentCards.map(c => `<user-content>\n${c}\n</user-content>`),
- ...urlCards,
- input
+ if (adaptiveModeSubmitBlocked) {
+ onAdaptiveModeAuthRequired?.()
+ return
+ }
+ if (!hasAvailableModels) {
+ toast.error('No enabled model is available')
+ return
+ }
+ const uploaded = uploadedFiles.filter(f => f.status === 'uploaded')
+ const parts = [
+ ...contentCards.map(text => ({
+ type: 'data-pastedContent',
+ data: { text }
+ })),
+ ...urlCards.map(url => ({
+ type: 'data-sourceUrl',
+ data: { url }
+ })),
+ ...uploaded.map(f => ({
+ type: 'file',
+ url: f.url!,
+ filename: f.name!,
+ mediaType: f.file.type
+ })),
+ ...(input.trim() ? [{ type: 'text', text: input }] : [])
]
- .filter(s => s && s.trim())
- .join('\n\n')
if (contentCards.length > 0) {
captureClient('content_card_submitted', {
cardCount: contentCards.length,
@@ -294,13 +314,13 @@ export function ChatPanel({
}
setContentCards([])
setUrlCards([])
+ setUploadedFiles([])
handleInputChange({
- target: { value: combined }
+ target: { value: '' }
} as React.ChangeEvent<HTMLTextAreaElement>)
- setTimeout(
- () => inputRef.current?.form?.requestSubmit(),
- INPUT_UPDATE_DELAY_MS
- )
+ append({ role: 'user', parts })
+ setIsInputFocused(false)
+ inputRef.current?.blur()
return
}
if (adaptiveModeSubmitBlocked) {
diff --git a/components/chat.tsx b/components/chat.tsx
index e20cdac0..5a742abb 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -415,9 +415,18 @@ export function Chat({
if (messageIndex === -1) return prevMessages
const updatedMessages = [...prevMessages]
+ const original = updatedMessages[messageIndex]
+ // Only replace the text part — keep pasted-content / URL / file parts
+ // so editing the instruction doesn't drop the structured context.
+ const oldParts = (original.parts ?? []) as any[]
+ const newParts = oldParts.some((p: any) => p.type === 'text')
+ ? oldParts.map((p: any) =>
+ p.type === 'text' ? { type: 'text', text: newContentText } : p
+ )
+ : [...oldParts, { type: 'text', text: newContentText }]
updatedMessages[messageIndex] = {
- ...updatedMessages[messageIndex],
- parts: [{ type: 'text', text: newContentText }]
+ ...original,
+ parts: newParts
}
return updatedMessages
diff --git a/components/pasted-parts.tsx b/components/pasted-parts.tsx
new file mode 100644
index 00000000..360a70c5
--- /dev/null
+++ b/components/pasted-parts.tsx
@@ -0,0 +1,63 @@
+'use client'
+
+import { useState } from 'react'
+
+import {
+ IconChevronDown as ChevronDown,
+ IconChevronUp as ChevronUp,
+ IconFileText as FileText
+} from '@tabler/icons-react'
+
+// Collapsed card for a pasted text blob (a `data-pastedContent` part, or a
+// legacy `<user-content>` block in old messages).
+export function PastedContentCard({ text }: { text: string }) {
+ const [open, setOpen] = useState(false)
+ return (
+ <div>
+ <button
+ type="button"
+ className="flex w-fit items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground"
+ onClick={() => setOpen(o => !o)}
+ >
+ <FileText className="size-3.5 shrink-0" />
+ Pasted content · {text.length.toLocaleString()} chars
+ {open ? (
+ <ChevronUp className="size-3.5 shrink-0" />
+ ) : (
+ <ChevronDown className="size-3.5 shrink-0" />
+ )}
+ </button>
+ {open && (
+ <p className="mt-1.5 max-h-60 overflow-auto whitespace-pre-wrap break-words text-xs text-muted-foreground/80">
+ {text}
+ </p>
+ )}
+ </div>
+ )
+}
+
+// Favicon chip for a pasted URL (a `data-sourceUrl` part). Clickable.
+export function UrlChip({ url }: { url: string }) {
+ let host = url
+ try {
+ host = new URL(url).host.replace(/^www\./, '')
+ } catch {}
+ return (
+ <a
+ href={url}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="inline-flex w-fit items-center gap-1.5 rounded-full border border-input bg-background py-1 pl-2 pr-2.5 text-xs text-muted-foreground hover:text-foreground"
+ >
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img
+ src={`https://www.google.com/s2/favicons?domain=${host}&sz=32`}
+ alt=""
+ width={14}
+ height={14}
+ className="size-3.5 shrink-0 rounded-sm"
+ />
+ <span className="max-w-[220px] truncate">{host}</span>
+ </a>
+ )
+}
diff --git a/components/render-message.tsx b/components/render-message.tsx
index 3cc63d8e..a7cb52cd 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -51,34 +51,36 @@ export function RenderMessage({
// Use provided citation maps (from all messages)
if (message.role === 'user') {
+ const parts = (message.parts ?? []) as any[]
+ const textPart = parts.find((part: any) => part.type === 'text')
+ const files = parts.filter((part: any) => part.type === 'file')
+ const pastedTexts = parts
+ .filter((part: any) => part.type === 'data-pastedContent')
+ .map((part: any) => part.data?.text ?? '')
+ const urls = parts
+ .filter((part: any) => part.type === 'data-sourceUrl')
+ .map((part: any) => part.data?.url ?? '')
return (
<>
- {message.parts?.map((part: any, index: number) => {
- switch (part.type) {
- case 'text':
- return (
- <UserTextSection
- key={`${messageId}-user-text-${index}`}
- content={part.text}
- messageId={messageId}
- onUpdateMessage={onUpdateMessage}
- />
- )
- case 'file':
- return (
- <UserFileSection
- key={`${messageId}-user-file-${index}`}
- file={{
- name: part.filename || 'Unknown file',
- url: part.url,
- contentType: part.mediaType
- }}
- />
- )
- default:
- return null
- }
- })}
+ {files.map((part: any, index: number) => (
+ <UserFileSection
+ key={`${messageId}-user-file-${index}`}
+ file={{
+ name: part.filename || 'Unknown file',
+ url: part.url,
+ contentType: part.mediaType
+ }}
+ />
+ ))}
+ {(textPart || pastedTexts.length > 0 || urls.length > 0) && (
+ <UserTextSection
+ content={textPart?.text ?? ''}
+ pastedTexts={pastedTexts}
+ urls={urls}
+ messageId={messageId}
+ onUpdateMessage={onUpdateMessage}
+ />
+ )}
</>
)
}
diff --git a/components/user-text-section.tsx b/components/user-text-section.tsx
index 15fe2a9c..100487d7 100644
--- a/components/user-text-section.tsx
+++ b/components/user-text-section.tsx
@@ -8,7 +8,6 @@ import {
IconChevronDown as ChevronDown,
IconChevronUp as ChevronUp,
IconCopy as Copy,
- IconFileText as FileText,
IconPencil as Pencil
} from '@tabler/icons-react'
@@ -16,10 +15,11 @@ import { cn } from '@/lib/utils'
import { Button } from './ui/button'
import { CollapsibleMessage } from './collapsible-message'
+import { PastedContentCard, UrlChip } from './pasted-parts'
-// Messages may carry the user's target material wrapped in <user-content>
-// tags (see chat-panel). Split it out so we can render it as a collapsed card
-// and keep the instruction as the prominent text.
+// Legacy: old messages carry pasted material wrapped in <user-content> tags
+// (new messages use `data-pastedContent` parts). Split it out so we can render
+// it as a collapsed card and keep the instruction as the prominent text.
const PASTED_RE = /<user-content>\n?([\s\S]*?)\n?<\/user-content>/g
function splitPastedContent(content: string): {
@@ -36,89 +36,25 @@ function splitPastedContent(content: string): {
return { cards, rest }
}
-// URL cards (see chat-panel) are folded into the message as bare-URL lines
-// BEFORE the instruction. Only pull the LEADING run of URL lines, so the chip
-// count matches what the composer showed — a URL the user typed inside the
-// instruction body stays as plain text.
-const BARE_URL_RE = /^https?:\/\/\S+$/
-
-function splitUrls(content: string): { urls: string[]; rest: string } {
- const lines = content.split('\n')
- const urls: string[] = []
- let i = 0
- for (; i < lines.length; i++) {
- const trimmed = lines[i].trim()
- if (trimmed === '') continue // blank line between leading chips
- if (!BARE_URL_RE.test(trimmed)) break // instruction starts here
- urls.push(trimmed)
- }
- return { urls, rest: lines.slice(i).join('\n').trim() }
-}
-
-function UrlChip({ url }: { url: string }) {
- let host = url
- try {
- host = new URL(url).host.replace(/^www\./, '')
- } catch {}
- return (
- <a
- href={url}
- target="_blank"
- rel="noopener noreferrer"
- className="inline-flex w-fit items-center gap-1.5 rounded-full border border-input bg-background py-1 pl-2 pr-2.5 text-xs text-muted-foreground hover:text-foreground"
- >
- {/* eslint-disable-next-line @next/next/no-img-element */}
- <img
- src={`https://www.google.com/s2/favicons?domain=${host}&sz=32`}
- alt=""
- width={14}
- height={14}
- className="size-3.5 shrink-0 rounded-sm"
- />
- <span className="max-w-[220px] truncate">{host}</span>
- </a>
- )
-}
-
-function PastedContentCard({ text }: { text: string }) {
- const [open, setOpen] = useState(false)
- return (
- <div>
- <button
- type="button"
- className="flex w-fit items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground"
- onClick={() => setOpen(o => !o)}
- >
- <FileText className="size-3.5 shrink-0" />
- Pasted content · {text.length.toLocaleString()} chars
- {open ? (
- <ChevronUp className="size-3.5 shrink-0" />
- ) : (
- <ChevronDown className="size-3.5 shrink-0" />
- )}
- </button>
- {open && (
- <p className="mt-1.5 max-h-60 overflow-auto whitespace-pre-wrap break-words text-xs text-muted-foreground/80">
- {text}
- </p>
- )}
- </div>
- )
-}
-
interface UserTextSectionProps {
content: string
+ // Pasted attachments (new data parts), placed below the instruction.
+ pastedTexts?: string[]
+ urls?: string[]
messageId?: string
onUpdateMessage?: (messageId: string, newContent: string) => Promise<void>
}
export const UserTextSection: React.FC<UserTextSectionProps> = ({
content,
+ pastedTexts = [],
+ urls = [],
messageId,
onUpdateMessage
}) => {
- const { cards, rest: afterCards } = splitPastedContent(content)
- const { urls, rest } = splitUrls(afterCards)
+ // Legacy cards live inside the text; new pasted texts arrive as props.
+ const { cards: legacyCards, rest } = splitPastedContent(content)
+ const cards = [...legacyCards, ...pastedTexts]
const [isEditing, setIsEditing] = useState(false)
const [editedContent, setEditedContent] = useState(rest)
const [isComposing, setIsComposing] = useState(false)
@@ -168,10 +104,10 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({
setIsEditing(false)
- // Re-wrap the preserved pasted cards (target) with the edited instruction.
+ // Re-wrap only the legacy text-embedded cards; new pasted texts are
+ // separate parts and are not part of the editable text.
const wrapped = [
- ...cards.map(c => `<user-content>\n${c}\n</user-content>`),
- ...urls,
+ ...legacyCards.map(c => `<user-content>\n${c}\n</user-content>`),
editedContent
]
.filter(s => s && s.trim())
@@ -240,6 +176,18 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({
}, 50)
}
+ // Pasted attachments (cards + URL chips), placed below the instruction.
+ const attachments = (cards.length > 0 || urls.length > 0) && (
+ <div className="mt-2 flex flex-col items-start gap-1.5">
+ {cards.map((c, i) => (
+ <PastedContentCard key={`card-${i}`} text={c} />
+ ))}
+ {urls.map((u, i) => (
+ <UrlChip key={`url-${i}`} url={u} />
+ ))}
+ </div>
+ )
+
return (
<CollapsibleMessage role="user">
<div
@@ -248,20 +196,6 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({
>
{isEditing ? (
<div className="flex flex-col gap-2">
- {cards.length > 0 && (
- <div className="flex flex-col gap-1.5">
- {cards.map((c, i) => (
- <PastedContentCard key={i} text={c} />
- ))}
- </div>
- )}
- {urls.length > 0 && (
- <div className="flex flex-wrap gap-1.5">
- {urls.map((u, i) => (
- <UrlChip key={i} url={u} />
- ))}
- </div>
- )}
<TextareaAutosize
value={editedContent}
onChange={e => setEditedContent(e.target.value)}
@@ -273,6 +207,7 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({
minRows={2}
maxRows={10}
/>
+ {attachments}
<div className="flex justify-end gap-2">
<Button variant="secondary" size="sm" onClick={handleCancelClick}>
Cancel
@@ -284,20 +219,6 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({
</div>
) : (
<div className="relative">
- {cards.length > 0 && (
- <div className="mb-2 flex flex-col gap-1.5">
- {cards.map((c, i) => (
- <PastedContentCard key={i} text={c} />
- ))}
- </div>
- )}
- {urls.length > 0 && (
- <div className="mb-2 flex flex-wrap gap-1.5">
- {urls.map((u, i) => (
- <UrlChip key={i} url={u} />
- ))}
- </div>
- )}
<div
ref={contentRef}
className={cn(
@@ -324,6 +245,7 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({
)}
</button>
)}
+ {attachments}
<div
className={cn(
'absolute -top-1 -right-1 flex items-center gap-0.5 p-0.5 transition-opacity bg-background rounded-full shadow-sm border',
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index e290290d..de62cd35 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -25,6 +25,7 @@ import { getTextFromParts } from '../utils/message-utils'
import { perfLog, perfTime } from '../utils/perf-logging'
import { isUsageLogging, logUsage } from '../utils/usage-logging'
+import { convertDataPart } from './helpers/convert-data-part'
import { persistStreamResults } from './helpers/persist-stream-results'
import { prepareMessages } from './helpers/prepare-messages'
import { stripReasoningParts } from './helpers/strip-reasoning-parts'
@@ -141,7 +142,9 @@ export async function createChatStreamResponse(
: messagesWithoutSpec
// Convert to model messages and apply context window management
- let modelMessages = await convertToModelMessages(messagesToConvert)
+ let modelMessages = await convertToModelMessages(messagesToConvert, {
+ convertDataPart
+ })
// Prune messages to reduce token usage while keeping recent context
modelMessages = pruneMessages({
diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.ts
index f2db0caf..54e4cdec 100644
--- a/lib/streaming/create-ephemeral-chat-stream-response.ts
+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts
@@ -22,6 +22,7 @@ import {
} from '../utils/context-window'
import { isUsageLogging, logUsage } from '../utils/usage-logging'
+import { convertDataPart } from './helpers/convert-data-part'
import { stripReasoningParts } from './helpers/strip-reasoning-parts'
import { stripSpecFromMessages } from './helpers/strip-spec-from-messages'
import { BaseStreamConfig } from './types'
@@ -73,7 +74,9 @@ export async function createEphemeralChatStreamResponse(
? stripReasoningParts(messagesWithoutSpec)
: messagesWithoutSpec
- let modelMessages = await convertToModelMessages(messagesToConvert)
+ let modelMessages = await convertToModelMessages(messagesToConvert, {
+ convertDataPart
+ })
modelMessages = pruneMessages({
messages: modelMessages,
diff --git a/lib/streaming/helpers/convert-data-part.ts b/lib/streaming/helpers/convert-data-part.ts
new file mode 100644
index 00000000..fae6a563
--- /dev/null
+++ b/lib/streaming/helpers/convert-data-part.ts
@@ -0,0 +1,35 @@
+import type { FilePart, TextPart } from '@ai-sdk/provider-utils'
+import { randomUUID } from 'crypto'
+
+/**
+ * Maps Morphic's user-authored data parts into model input for
+ * `convertToModelMessages({ convertDataPart })`. Returning `undefined` drops
+ * the part from the model message.
+ *
+ * Pasted content is wrapped in a nonce-delimited block so the content itself
+ * can never spoof the boundary — this replaces the old in-band `<user-content>`
+ * marker and removes its prompt-injection / boundary-collision risk.
+ */
+export function convertDataPart(part: {
+ type: string
+ data?: unknown
+}): TextPart | FilePart | undefined {
+ if (part.type === 'data-pastedContent') {
+ const data = part.data as { text?: unknown } | undefined
+ const text = typeof data?.text === 'string' ? data.text : ''
+ if (!text) return undefined
+ const nonce = randomUUID().slice(0, 8)
+ return {
+ type: 'text',
+ text: `[user-pasted-content ${nonce}]\n${text}\n[/user-pasted-content ${nonce}]`
+ }
+ }
+
+ if (part.type === 'data-sourceUrl') {
+ const data = part.data as { url?: unknown } | undefined
+ const url = typeof data?.url === 'string' ? data.url : ''
+ return url ? { type: 'text', text: url } : undefined
+ }
+
+ return undefined
+}
diff --git a/lib/types/ai.ts b/lib/types/ai.ts
index 95c10959..d3ef8911 100644
--- a/lib/types/ai.ts
+++ b/lib/types/ai.ts
@@ -27,6 +27,9 @@ export type UIMessage<
export type UIDataTypes = {
sources?: any[]
+ // User-authored attachments (composer): a pasted text blob and a pasted URL.
+ pastedContent?: { text: string }
+ sourceUrl?: { url: string }
}
// Create todo tools instance for type inference